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
39 changes: 25 additions & 14 deletions src/java/org/apache/cassandra/service/GCInspector.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,30 @@ public class GCInspector implements NotificationListener, GCInspectorMXBean

static
{
Field totalTempField = null;
Field maxTempField = null;
Field reservedTempField = null;
Class<?> bitsClass = null;

try
{
Class<?> bitsClass = Class.forName("java.nio.Bits");
totalTempField = getField(bitsClass, "TOTAL_CAPACITY");
// Returns the maximum amount of allocatable direct buffer memory.
maxTempField = getField(bitsClass, "MAX_MEMORY");
reservedTempField = getField(bitsClass, "RESERVED_MEMORY");
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
}
BITS_TOTAL_CAPACITY = totalTempField;
BITS_MAX = maxTempField;
BITS_RESERVED = reservedTempField;

if (bitsClass != null)
{
BITS_TOTAL_CAPACITY = getField(bitsClass, "TOTAL_CAPACITY");
// Returns the maximum amount of allocatable direct buffer memory.
BITS_MAX = getField(bitsClass, "MAX_MEMORY");
BITS_RESERVED = getField(bitsClass, "RESERVED_MEMORY");
}
else
{
BITS_TOTAL_CAPACITY = null;
BITS_MAX = null;
BITS_RESERVED = null;
}
}

static final class State
Expand Down Expand Up @@ -397,9 +402,15 @@ private static Field getField(Class<?> clazz, String fieldName)
}

/**
* Retrieves the value of a Field, handling both regular long fields and AtomicLong fields.
*
* From the implementation of java.nio.Bits, we can infer that TOTAL_CAPACITY/RESERVED_MEMORY is AtomicLong
* and MAX_MEMORY is long. This method works well with JDK 11/17
* */
* and MAX_MEMORY is long.
*
* @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;
Expand Down