Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 55 additions & 23 deletions src/java/org/apache/cassandra/service/GCInspector.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import javax.management.MBeanServer;
Expand Down Expand Up @@ -56,36 +57,34 @@ public class GCInspector implements NotificationListener, GCInspectorMXBean

/*
* The field from java.nio.Bits that tracks the total number of allocated
* bytes of direct memory requires via ByteBuffer.allocateDirect that have not been GCed.
* bytes of direct memory requested via ByteBuffer.allocateDirect that have not been GCed.
*/
final static Field BITS_TOTAL_CAPACITY;
final static Field BITS_TOTAL_CAPACITY_JAVA_8;
final static Field BITS_TOTAL_CAPACITY_JAVA_11;


static
{
Field temp = null;
Class<?> bitsClass = null;

try
{
Class<?> bitsClass = Class.forName("java.nio.Bits");
Field f;
try
{
f = bitsClass.getDeclaredField("totalCapacity");
}
catch (NoSuchFieldException ex)
{
// in Java11 it changed name to "TOTAL_CAPACITY"
f = bitsClass.getDeclaredField("TOTAL_CAPACITY");
}
f.setAccessible(true);
temp = f;
bitsClass = Class.forName("java.nio.Bits");
}
catch (Throwable t)
{
logger.debug("Error accessing field of java.nio.Bits", t);
//Don't care, will just return the dummy value -1 if we can't get at the field in this JVM
logger.debug("Error returning class of java.nio.Bits", t);
}

if (bitsClass != null)
{
BITS_TOTAL_CAPACITY_JAVA_8 = getField(bitsClass, "totalCapacity");
BITS_TOTAL_CAPACITY_JAVA_11 = getField(bitsClass, "TOTAL_CAPACITY");
}
else
{
BITS_TOTAL_CAPACITY_JAVA_8 = null;
BITS_TOTAL_CAPACITY_JAVA_11 = null;
}
BITS_TOTAL_CAPACITY = temp;
}

static final class State
Expand Down Expand Up @@ -326,14 +325,47 @@ public double[] getAndResetStats()

private static long getAllocatedDirectMemory()
{
if (BITS_TOTAL_CAPACITY == null) return -1;
long fieldValue = getFieldValue(BITS_TOTAL_CAPACITY_JAVA_8, true);
Copy link

Copilot AI Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isAtomicLong parameter should be false for the Java 8 field. In Java 8, totalCapacity is a regular long field, not an AtomicLong. Only in Java 11 was it changed to TOTAL_CAPACITY as an AtomicLong.

This will cause a ClassCastException when running on Java 8, as the code will attempt to cast a Long to AtomicLong.

Suggested fix:

long fieldValue = getFieldValue(BITS_TOTAL_CAPACITY_JAVA_8, false);
Suggested change
long fieldValue = getFieldValue(BITS_TOTAL_CAPACITY_JAVA_8, true);
long fieldValue = getFieldValue(BITS_TOTAL_CAPACITY_JAVA_8, false);

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad bot! This is not true, both of them are AtomicLong, it is just in Java 8 and 11 they differ on names.


if (fieldValue == -1)
fieldValue = getFieldValue(BITS_TOTAL_CAPACITY_JAVA_11, true);

return fieldValue;
}

private static Field getField(Class<?> clazz, String fieldName)
{
try
{
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
}
catch (Throwable t)
{
logger.trace("Error accessing field {} of {}", fieldName, clazz.getName(), t);
// Return null to indicate failure
return null;
}
}

/**
* Retrieves the value of a Field, handling both regular long fields and AtomicLong fields.
*
* @param field the Field to retrieve the value from
* @param isAtomicLong true if the field is an AtomicLong, false if it's a regular long
* @return the field value, or -1 if retrieval fails or field is null.
*/
private static long getFieldValue(Field field, boolean isAtomicLong)
{
if (field == null) return -1;
try
{
return BITS_TOTAL_CAPACITY.getLong(null);
return isAtomicLong ? ((AtomicLong) field.get(null)).get() : field.getLong(null);
}
catch (Throwable t)
{
logger.trace("Error accessing field of java.nio.Bits", t);
logger.trace("Error accessing field value of {}", field.getName(), t);
//Don't care how or why we failed to get the value in this JVM. Return -1 to indicate failure
return -1;
}
Expand Down