Copilot commented on code in PR #17541:
URL: https://github.com/apache/pinot/pull/17541#discussion_r2710365060


##########
pinot-spi/src/main/java/org/apache/pinot/spi/accounting/ThreadResourceUsageProvider.java:
##########
@@ -40,135 +36,180 @@ private ThreadResourceUsageProvider() {
 
   // used for getting the memory allocation function in hotspot jvm through 
reflection
   private static final String SUN_THREAD_MXBEAN_CLASS_NAME = 
"com.sun.management.ThreadMXBean";
-  private static final String 
SUN_THREAD_MXBEAN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME
-      = "isThreadAllocatedMemorySupported";
-  private static final String 
SUN_THREAD_MXBEAN_IS_THREAD_ALLOCATED_MEMORY_ENABLED_NAME
-      = "isThreadAllocatedMemoryEnabled";
-  private static final String 
SUN_THREAD_MXBEAN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME
-      = "setThreadAllocatedMemoryEnabled";
-  private static final String SUN_THREAD_MXBEAN_GET_BYTES_ALLOCATED_NAME = 
"getThreadAllocatedBytes";
-  private static final Method SUN_THREAD_MXBEAN_GET_BYTES_ALLOCATED_METHOD;
+  private static final String SUN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME = 
"isThreadAllocatedMemorySupported";
+  private static final String SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME = 
"setThreadAllocatedMemoryEnabled";
+  private static final String SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME = 
"getCurrentThreadAllocatedBytes";
+  private static final String SUN_GET_THREAD_ALLOCATED_BYTES_NAME = 
"getThreadAllocatedBytes";
 
   private static final ThreadMXBean MX_BEAN = 
ManagementFactory.getThreadMXBean();
   private static final boolean IS_CURRENT_THREAD_CPU_TIME_SUPPORTED = 
MX_BEAN.isCurrentThreadCpuTimeSupported();
   private static final boolean IS_THREAD_ALLOCATED_MEMORY_SUPPORTED;
-  private static final boolean IS_THREAD_ALLOCATED_MEMORY_ENABLED_DEFAULT;
-  private static boolean _isThreadCpuTimeMeasurementEnabled = false;
-  private static boolean _isThreadMemoryMeasurementEnabled = false;
-
-  public static int getThreadCount() {
-    return MX_BEAN.getThreadCount();
-  }
-
-  public static long getTotalStartedThreadCount() {
-    return MX_BEAN.getTotalStartedThreadCount();
-  }
+  private static final Method SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_METHOD;
+  private static final Method SUN_GET_THREAD_ALLOCATED_BYTES_METHOD;
+  private static final Method SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_METHOD;
 
-  public static long getCurrentThreadCpuTime() {
-    return _isThreadCpuTimeMeasurementEnabled ? 
MX_BEAN.getCurrentThreadCpuTime() : 0;
-  }
+  private static boolean _isThreadCpuTimeMeasurementEnabled;
+  private static boolean _isThreadMemoryMeasurementEnabled;
 
-  public static long getCurrentThreadAllocatedBytes() {
+  // Initialize the com.sun.management.ThreadMXBean related variables using 
reflection
+  static {
+    Class<?> sunThreadMXBeanClass = null;
     try {
-      return _isThreadMemoryMeasurementEnabled ? (long) 
SUN_THREAD_MXBEAN_GET_BYTES_ALLOCATED_METHOD
-          .invoke(MX_BEAN, Thread.currentThread().getId()) : 0;
-    } catch (IllegalAccessException | InvocationTargetException e) {
-      LOGGER.error("Exception happened during the invocation of getting 
current bytes allocated", e);
-      return 0;
+      sunThreadMXBeanClass = Class.forName(SUN_THREAD_MXBEAN_CLASS_NAME);
+    } catch (Exception e) {
+      LOGGER.error("Caught exception while loading: {}, you are probably not 
using Hotspot jvm",
+          SUN_THREAD_MXBEAN_CLASS_NAME, e);
     }
-  }
 
-  /// Returns an approximation of the total garbage collection time in 
milliseconds.
-  public static long getGcTime() {
-    long totalGCTime = 0;
-    List<GarbageCollectorMXBean> gcBeans = 
ManagementFactory.getGarbageCollectorMXBeans();
-    for (GarbageCollectorMXBean gcBean : gcBeans) {
-      long gcTime = gcBean.getCollectionTime();
-      if (gcTime > 0) {
-        totalGCTime += gcTime;
+    boolean isThreadAllocatedMemorySupported = false;
+    Method setThreadAllocatedMemoryEnabled = null;
+    Method getCurrentThreadAllocatedBytes = null;
+    Method getThreadAllocatedBytes = null;
+    if (sunThreadMXBeanClass != null) {
+      try {
+        isThreadAllocatedMemorySupported =
+            (boolean) 
sunThreadMXBeanClass.getMethod(SUN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME).invoke(MX_BEAN);
+      } catch (Exception e) {
+        LOGGER.error("Caught exception invoking method: {}", 
SUN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME, e);
+      }
+      if (isThreadAllocatedMemorySupported) {
+        try {
+          setThreadAllocatedMemoryEnabled =
+              
sunThreadMXBeanClass.getMethod(SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME, 
boolean.class);
+        } catch (Exception e) {
+          LOGGER.error("Caught exception loading method: {}", 
SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME, e);
+          isThreadAllocatedMemorySupported = false;
+        }
+      }
+      if (isThreadAllocatedMemorySupported) {
+        try {
+          getCurrentThreadAllocatedBytes = 
sunThreadMXBeanClass.getMethod(SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME);
+        } catch (Exception e1) {
+          LOGGER.info("Failed to load method: {}, loading: {} instead", 
SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME,
+              SUN_GET_THREAD_ALLOCATED_BYTES_NAME);
+          try {
+            getThreadAllocatedBytes = 
sunThreadMXBeanClass.getMethod(SUN_GET_THREAD_ALLOCATED_BYTES_NAME, long.class);
+          } catch (Exception e2) {
+            LOGGER.error("Caught exception loading method: {}", 
SUN_GET_THREAD_ALLOCATED_BYTES_NAME, e2);
+            isThreadAllocatedMemorySupported = false;
+          }
+        }
       }
     }
-    return totalGCTime;
+
+    LOGGER.info("Current thread CPU time supported: {}", 
IS_CURRENT_THREAD_CPU_TIME_SUPPORTED);
+    LOGGER.info("Thread allocated memory supported: {}", 
isThreadAllocatedMemorySupported);
+    if (isThreadAllocatedMemorySupported) {
+      LOGGER.info("Using: {} to read current thread allocated bytes",
+          getCurrentThreadAllocatedBytes != null ? 
SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME
+              : SUN_GET_THREAD_ALLOCATED_BYTES_NAME);
+    }
+    IS_THREAD_ALLOCATED_MEMORY_SUPPORTED = isThreadAllocatedMemorySupported;
+    SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_METHOD = 
setThreadAllocatedMemoryEnabled;
+    SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_METHOD = 
getCurrentThreadAllocatedBytes;
+    SUN_GET_THREAD_ALLOCATED_BYTES_METHOD = getThreadAllocatedBytes;
   }
 
   public static boolean isThreadCpuTimeMeasurementEnabled() {
     return _isThreadCpuTimeMeasurementEnabled;
   }
 
   public static void setThreadCpuTimeMeasurementEnabled(boolean enable) {
-    _isThreadCpuTimeMeasurementEnabled = enable && 
IS_CURRENT_THREAD_CPU_TIME_SUPPORTED;
+    if (!IS_CURRENT_THREAD_CPU_TIME_SUPPORTED) {
+      assert !_isThreadCpuTimeMeasurementEnabled;

Review Comment:
   The assertion at line 119 may fail if 
`setThreadCpuTimeMeasurementEnabled(true)` was called previously when CPU time 
measurement was supported, and the code is later run in an environment where 
it's not supported. Consider removing this assertion or documenting the 
assumption that the support status cannot change at runtime.
   ```suggestion
   
   ```



##########
pinot-spi/src/main/java/org/apache/pinot/spi/accounting/ThreadResourceUsageProvider.java:
##########
@@ -40,135 +36,180 @@ private ThreadResourceUsageProvider() {
 
   // used for getting the memory allocation function in hotspot jvm through 
reflection
   private static final String SUN_THREAD_MXBEAN_CLASS_NAME = 
"com.sun.management.ThreadMXBean";
-  private static final String 
SUN_THREAD_MXBEAN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME
-      = "isThreadAllocatedMemorySupported";
-  private static final String 
SUN_THREAD_MXBEAN_IS_THREAD_ALLOCATED_MEMORY_ENABLED_NAME
-      = "isThreadAllocatedMemoryEnabled";
-  private static final String 
SUN_THREAD_MXBEAN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME
-      = "setThreadAllocatedMemoryEnabled";
-  private static final String SUN_THREAD_MXBEAN_GET_BYTES_ALLOCATED_NAME = 
"getThreadAllocatedBytes";
-  private static final Method SUN_THREAD_MXBEAN_GET_BYTES_ALLOCATED_METHOD;
+  private static final String SUN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME = 
"isThreadAllocatedMemorySupported";
+  private static final String SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME = 
"setThreadAllocatedMemoryEnabled";
+  private static final String SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME = 
"getCurrentThreadAllocatedBytes";
+  private static final String SUN_GET_THREAD_ALLOCATED_BYTES_NAME = 
"getThreadAllocatedBytes";
 
   private static final ThreadMXBean MX_BEAN = 
ManagementFactory.getThreadMXBean();
   private static final boolean IS_CURRENT_THREAD_CPU_TIME_SUPPORTED = 
MX_BEAN.isCurrentThreadCpuTimeSupported();
   private static final boolean IS_THREAD_ALLOCATED_MEMORY_SUPPORTED;
-  private static final boolean IS_THREAD_ALLOCATED_MEMORY_ENABLED_DEFAULT;
-  private static boolean _isThreadCpuTimeMeasurementEnabled = false;
-  private static boolean _isThreadMemoryMeasurementEnabled = false;
-
-  public static int getThreadCount() {
-    return MX_BEAN.getThreadCount();
-  }
-
-  public static long getTotalStartedThreadCount() {
-    return MX_BEAN.getTotalStartedThreadCount();
-  }
+  private static final Method SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_METHOD;
+  private static final Method SUN_GET_THREAD_ALLOCATED_BYTES_METHOD;
+  private static final Method SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_METHOD;
 
-  public static long getCurrentThreadCpuTime() {
-    return _isThreadCpuTimeMeasurementEnabled ? 
MX_BEAN.getCurrentThreadCpuTime() : 0;
-  }
+  private static boolean _isThreadCpuTimeMeasurementEnabled;
+  private static boolean _isThreadMemoryMeasurementEnabled;
 
-  public static long getCurrentThreadAllocatedBytes() {
+  // Initialize the com.sun.management.ThreadMXBean related variables using 
reflection
+  static {
+    Class<?> sunThreadMXBeanClass = null;
     try {
-      return _isThreadMemoryMeasurementEnabled ? (long) 
SUN_THREAD_MXBEAN_GET_BYTES_ALLOCATED_METHOD
-          .invoke(MX_BEAN, Thread.currentThread().getId()) : 0;
-    } catch (IllegalAccessException | InvocationTargetException e) {
-      LOGGER.error("Exception happened during the invocation of getting 
current bytes allocated", e);
-      return 0;
+      sunThreadMXBeanClass = Class.forName(SUN_THREAD_MXBEAN_CLASS_NAME);
+    } catch (Exception e) {
+      LOGGER.error("Caught exception while loading: {}, you are probably not 
using Hotspot jvm",
+          SUN_THREAD_MXBEAN_CLASS_NAME, e);
     }
-  }
 
-  /// Returns an approximation of the total garbage collection time in 
milliseconds.
-  public static long getGcTime() {
-    long totalGCTime = 0;
-    List<GarbageCollectorMXBean> gcBeans = 
ManagementFactory.getGarbageCollectorMXBeans();
-    for (GarbageCollectorMXBean gcBean : gcBeans) {
-      long gcTime = gcBean.getCollectionTime();
-      if (gcTime > 0) {
-        totalGCTime += gcTime;
+    boolean isThreadAllocatedMemorySupported = false;
+    Method setThreadAllocatedMemoryEnabled = null;
+    Method getCurrentThreadAllocatedBytes = null;
+    Method getThreadAllocatedBytes = null;
+    if (sunThreadMXBeanClass != null) {
+      try {
+        isThreadAllocatedMemorySupported =
+            (boolean) 
sunThreadMXBeanClass.getMethod(SUN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME).invoke(MX_BEAN);
+      } catch (Exception e) {
+        LOGGER.error("Caught exception invoking method: {}", 
SUN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME, e);
+      }
+      if (isThreadAllocatedMemorySupported) {
+        try {
+          setThreadAllocatedMemoryEnabled =
+              
sunThreadMXBeanClass.getMethod(SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME, 
boolean.class);
+        } catch (Exception e) {
+          LOGGER.error("Caught exception loading method: {}", 
SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME, e);
+          isThreadAllocatedMemorySupported = false;
+        }
+      }
+      if (isThreadAllocatedMemorySupported) {
+        try {
+          getCurrentThreadAllocatedBytes = 
sunThreadMXBeanClass.getMethod(SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME);
+        } catch (Exception e1) {
+          LOGGER.info("Failed to load method: {}, loading: {} instead", 
SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME,
+              SUN_GET_THREAD_ALLOCATED_BYTES_NAME);
+          try {
+            getThreadAllocatedBytes = 
sunThreadMXBeanClass.getMethod(SUN_GET_THREAD_ALLOCATED_BYTES_NAME, long.class);
+          } catch (Exception e2) {
+            LOGGER.error("Caught exception loading method: {}", 
SUN_GET_THREAD_ALLOCATED_BYTES_NAME, e2);
+            isThreadAllocatedMemorySupported = false;
+          }
+        }
       }
     }
-    return totalGCTime;
+
+    LOGGER.info("Current thread CPU time supported: {}", 
IS_CURRENT_THREAD_CPU_TIME_SUPPORTED);
+    LOGGER.info("Thread allocated memory supported: {}", 
isThreadAllocatedMemorySupported);
+    if (isThreadAllocatedMemorySupported) {
+      LOGGER.info("Using: {} to read current thread allocated bytes",
+          getCurrentThreadAllocatedBytes != null ? 
SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME
+              : SUN_GET_THREAD_ALLOCATED_BYTES_NAME);
+    }
+    IS_THREAD_ALLOCATED_MEMORY_SUPPORTED = isThreadAllocatedMemorySupported;
+    SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_METHOD = 
setThreadAllocatedMemoryEnabled;
+    SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_METHOD = 
getCurrentThreadAllocatedBytes;
+    SUN_GET_THREAD_ALLOCATED_BYTES_METHOD = getThreadAllocatedBytes;
   }
 
   public static boolean isThreadCpuTimeMeasurementEnabled() {
     return _isThreadCpuTimeMeasurementEnabled;
   }
 
   public static void setThreadCpuTimeMeasurementEnabled(boolean enable) {
-    _isThreadCpuTimeMeasurementEnabled = enable && 
IS_CURRENT_THREAD_CPU_TIME_SUPPORTED;
+    if (!IS_CURRENT_THREAD_CPU_TIME_SUPPORTED) {
+      assert !_isThreadCpuTimeMeasurementEnabled;
+      if (enable) {
+        LOGGER.error("Not enabling thread CPU time measurement because it is 
not supported");
+      }
+      return;
+    }
+    if (_isThreadCpuTimeMeasurementEnabled != enable) {
+      if (enable) {
+        LOGGER.info("Enabling thread CPU time measurement");
+      } else {
+        LOGGER.info("Disabling thread CPU time measurement");
+      }
+    }
+    try {
+      MX_BEAN.setThreadCpuTimeEnabled(enable);
+      _isThreadCpuTimeMeasurementEnabled = enable;
+    } catch (Exception e) {
+      LOGGER.error("Caught exception {} thread CPU time measurement", enable ? 
"enabling" : "disabling", e);
+      _isThreadCpuTimeMeasurementEnabled = false;
+    }
   }
 
   public static boolean isThreadMemoryMeasurementEnabled() {
     return _isThreadMemoryMeasurementEnabled;
   }
 
   public static void setThreadMemoryMeasurementEnabled(boolean enable) {
-
-    boolean isThreadAllocateMemoryEnabled = 
IS_THREAD_ALLOCATED_MEMORY_ENABLED_DEFAULT;
-    // if the jvm default enabling config is different
-    if (enable != IS_THREAD_ALLOCATED_MEMORY_ENABLED_DEFAULT) {
-      try {
-        Class<?> sunThreadMXBeanClass = 
Class.forName(SUN_THREAD_MXBEAN_CLASS_NAME);
-        
sunThreadMXBeanClass.getMethod(SUN_THREAD_MXBEAN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME,
 Boolean.TYPE)
-            .invoke(MX_BEAN, enable);
-        isThreadAllocateMemoryEnabled = (boolean) sunThreadMXBeanClass
-            
.getMethod(SUN_THREAD_MXBEAN_IS_THREAD_ALLOCATED_MEMORY_ENABLED_NAME)
-            .invoke(MX_BEAN);
-      } catch (ClassNotFoundException | IllegalAccessException | 
NoSuchMethodException | InvocationTargetException e) {
-        LOGGER.error("Not able to call isThreadAllocatedMemoryEnabled or 
setThreadAllocatedMemoryEnabled, ", e);
+    if (!IS_THREAD_ALLOCATED_MEMORY_SUPPORTED) {
+      assert !_isThreadMemoryMeasurementEnabled;
+      if (enable) {
+        LOGGER.error("Not enabling thread memory measurement because it is not 
supported");
       }
+      return;
     }
-    _isThreadMemoryMeasurementEnabled = enable && 
IS_THREAD_ALLOCATED_MEMORY_SUPPORTED && isThreadAllocateMemoryEnabled;
-  }
-
-  //initialize the com.sun.management.ThreadMXBean related variables using 
reflection
-  static {
-    Class<?> sunThreadMXBeanClass;
-    try {
-      sunThreadMXBeanClass = Class.forName(SUN_THREAD_MXBEAN_CLASS_NAME);
-    } catch (ClassNotFoundException e) {
-      LOGGER.error("Not able to load com.sun.management.ThreadMXBean, you are 
probably not using Hotspot jvm");
-      sunThreadMXBeanClass = null;
+    if (_isThreadMemoryMeasurementEnabled != enable) {
+      if (enable) {
+        LOGGER.info("Enabling thread memory measurement");
+      } else {
+        LOGGER.info("Disabling thread memory measurement");
+      }
     }
-
-    boolean isThreadAllocateMemorySupported = false;
     try {
-      isThreadAllocateMemorySupported =
-          sunThreadMXBeanClass != null && (boolean) sunThreadMXBeanClass
-              
.getMethod(SUN_THREAD_MXBEAN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME)
-              .invoke(MX_BEAN);
-    } catch (IllegalAccessException | NoSuchMethodException | 
InvocationTargetException e) {
-      LOGGER.error("Not able to call isThreadAllocatedMemorySupported, ", e);
+      SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_METHOD.invoke(MX_BEAN, enable);
+      _isThreadMemoryMeasurementEnabled = enable;
+    } catch (Exception e) {
+      LOGGER.error("Caught exception {} thread memory measurement", enable ? 
"enabling" : "disabling", e);
+      _isThreadMemoryMeasurementEnabled = false;
     }
-    IS_THREAD_ALLOCATED_MEMORY_SUPPORTED = isThreadAllocateMemorySupported;
+  }
 
-    boolean isThreadAllocateMemoryEnabled = false;
-    try {
-      isThreadAllocateMemoryEnabled =
-          sunThreadMXBeanClass != null && (boolean) sunThreadMXBeanClass
-              
.getMethod(SUN_THREAD_MXBEAN_IS_THREAD_ALLOCATED_MEMORY_ENABLED_NAME)
-              .invoke(MX_BEAN);
-    } catch (IllegalAccessException | NoSuchMethodException | 
InvocationTargetException e) {
-      LOGGER.error("Not able to call isThreadAllocatedMemoryEnabled, ", e);
-    }
-    IS_THREAD_ALLOCATED_MEMORY_ENABLED_DEFAULT = isThreadAllocateMemoryEnabled;
+  public static int getThreadCount() {
+    return MX_BEAN.getThreadCount();
+  }
+
+  public static long getTotalStartedThreadCount() {
+    return MX_BEAN.getTotalStartedThreadCount();
+  }
 
-    Method threadAllocateBytes = null;
-    if (IS_THREAD_ALLOCATED_MEMORY_SUPPORTED) {
+  public static long getCurrentThreadCpuTime() {
+    return _isThreadCpuTimeMeasurementEnabled ? 
MX_BEAN.getCurrentThreadCpuTime() : 0;
+  }
+
+  public static long getCurrentThreadAllocatedBytes() {
+    if (!_isThreadMemoryMeasurementEnabled) {
+      return 0;
+    }
+    if (SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_METHOD != null) {
       try {
-        threadAllocateBytes = sunThreadMXBeanClass
-            .getMethod(SUN_THREAD_MXBEAN_GET_BYTES_ALLOCATED_NAME, long.class);
-      } catch (NoSuchMethodException ignored) {
+        return (long) 
SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_METHOD.invoke(MX_BEAN);
+      } catch (Exception e) {
+        LOGGER.error("Caught exception invoking method: {}", 
SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME, e);
+        return 0;
+      }
+    } else {
+      assert SUN_GET_THREAD_ALLOCATED_BYTES_METHOD != null;

Review Comment:
   This assertion assumes that if 
`SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_METHOD` is null, then 
`SUN_GET_THREAD_ALLOCATED_BYTES_METHOD` must be non-null. However, the 
initialization logic in the static block shows that both could be null if 
loading fails. Consider either handling this case explicitly or adding a guard 
condition.



##########
pinot-spi/src/main/java/org/apache/pinot/spi/accounting/ThreadResourceUsageProvider.java:
##########
@@ -40,135 +36,180 @@ private ThreadResourceUsageProvider() {
 
   // used for getting the memory allocation function in hotspot jvm through 
reflection
   private static final String SUN_THREAD_MXBEAN_CLASS_NAME = 
"com.sun.management.ThreadMXBean";
-  private static final String 
SUN_THREAD_MXBEAN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME
-      = "isThreadAllocatedMemorySupported";
-  private static final String 
SUN_THREAD_MXBEAN_IS_THREAD_ALLOCATED_MEMORY_ENABLED_NAME
-      = "isThreadAllocatedMemoryEnabled";
-  private static final String 
SUN_THREAD_MXBEAN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME
-      = "setThreadAllocatedMemoryEnabled";
-  private static final String SUN_THREAD_MXBEAN_GET_BYTES_ALLOCATED_NAME = 
"getThreadAllocatedBytes";
-  private static final Method SUN_THREAD_MXBEAN_GET_BYTES_ALLOCATED_METHOD;
+  private static final String SUN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME = 
"isThreadAllocatedMemorySupported";
+  private static final String SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME = 
"setThreadAllocatedMemoryEnabled";
+  private static final String SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME = 
"getCurrentThreadAllocatedBytes";
+  private static final String SUN_GET_THREAD_ALLOCATED_BYTES_NAME = 
"getThreadAllocatedBytes";
 
   private static final ThreadMXBean MX_BEAN = 
ManagementFactory.getThreadMXBean();
   private static final boolean IS_CURRENT_THREAD_CPU_TIME_SUPPORTED = 
MX_BEAN.isCurrentThreadCpuTimeSupported();
   private static final boolean IS_THREAD_ALLOCATED_MEMORY_SUPPORTED;
-  private static final boolean IS_THREAD_ALLOCATED_MEMORY_ENABLED_DEFAULT;
-  private static boolean _isThreadCpuTimeMeasurementEnabled = false;
-  private static boolean _isThreadMemoryMeasurementEnabled = false;
-
-  public static int getThreadCount() {
-    return MX_BEAN.getThreadCount();
-  }
-
-  public static long getTotalStartedThreadCount() {
-    return MX_BEAN.getTotalStartedThreadCount();
-  }
+  private static final Method SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_METHOD;
+  private static final Method SUN_GET_THREAD_ALLOCATED_BYTES_METHOD;
+  private static final Method SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_METHOD;
 
-  public static long getCurrentThreadCpuTime() {
-    return _isThreadCpuTimeMeasurementEnabled ? 
MX_BEAN.getCurrentThreadCpuTime() : 0;
-  }
+  private static boolean _isThreadCpuTimeMeasurementEnabled;
+  private static boolean _isThreadMemoryMeasurementEnabled;
 
-  public static long getCurrentThreadAllocatedBytes() {
+  // Initialize the com.sun.management.ThreadMXBean related variables using 
reflection
+  static {
+    Class<?> sunThreadMXBeanClass = null;
     try {
-      return _isThreadMemoryMeasurementEnabled ? (long) 
SUN_THREAD_MXBEAN_GET_BYTES_ALLOCATED_METHOD
-          .invoke(MX_BEAN, Thread.currentThread().getId()) : 0;
-    } catch (IllegalAccessException | InvocationTargetException e) {
-      LOGGER.error("Exception happened during the invocation of getting 
current bytes allocated", e);
-      return 0;
+      sunThreadMXBeanClass = Class.forName(SUN_THREAD_MXBEAN_CLASS_NAME);
+    } catch (Exception e) {
+      LOGGER.error("Caught exception while loading: {}, you are probably not 
using Hotspot jvm",
+          SUN_THREAD_MXBEAN_CLASS_NAME, e);
     }
-  }
 
-  /// Returns an approximation of the total garbage collection time in 
milliseconds.
-  public static long getGcTime() {
-    long totalGCTime = 0;
-    List<GarbageCollectorMXBean> gcBeans = 
ManagementFactory.getGarbageCollectorMXBeans();
-    for (GarbageCollectorMXBean gcBean : gcBeans) {
-      long gcTime = gcBean.getCollectionTime();
-      if (gcTime > 0) {
-        totalGCTime += gcTime;
+    boolean isThreadAllocatedMemorySupported = false;
+    Method setThreadAllocatedMemoryEnabled = null;
+    Method getCurrentThreadAllocatedBytes = null;
+    Method getThreadAllocatedBytes = null;
+    if (sunThreadMXBeanClass != null) {
+      try {
+        isThreadAllocatedMemorySupported =
+            (boolean) 
sunThreadMXBeanClass.getMethod(SUN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME).invoke(MX_BEAN);
+      } catch (Exception e) {
+        LOGGER.error("Caught exception invoking method: {}", 
SUN_IS_THREAD_ALLOCATED_MEMORY_SUPPORTED_NAME, e);
+      }
+      if (isThreadAllocatedMemorySupported) {
+        try {
+          setThreadAllocatedMemoryEnabled =
+              
sunThreadMXBeanClass.getMethod(SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME, 
boolean.class);
+        } catch (Exception e) {
+          LOGGER.error("Caught exception loading method: {}", 
SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME, e);
+          isThreadAllocatedMemorySupported = false;
+        }
+      }
+      if (isThreadAllocatedMemorySupported) {
+        try {
+          getCurrentThreadAllocatedBytes = 
sunThreadMXBeanClass.getMethod(SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME);
+        } catch (Exception e1) {
+          LOGGER.info("Failed to load method: {}, loading: {} instead", 
SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME,
+              SUN_GET_THREAD_ALLOCATED_BYTES_NAME);
+          try {
+            getThreadAllocatedBytes = 
sunThreadMXBeanClass.getMethod(SUN_GET_THREAD_ALLOCATED_BYTES_NAME, long.class);
+          } catch (Exception e2) {
+            LOGGER.error("Caught exception loading method: {}", 
SUN_GET_THREAD_ALLOCATED_BYTES_NAME, e2);
+            isThreadAllocatedMemorySupported = false;
+          }
+        }
       }
     }
-    return totalGCTime;
+
+    LOGGER.info("Current thread CPU time supported: {}", 
IS_CURRENT_THREAD_CPU_TIME_SUPPORTED);
+    LOGGER.info("Thread allocated memory supported: {}", 
isThreadAllocatedMemorySupported);
+    if (isThreadAllocatedMemorySupported) {
+      LOGGER.info("Using: {} to read current thread allocated bytes",
+          getCurrentThreadAllocatedBytes != null ? 
SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_NAME
+              : SUN_GET_THREAD_ALLOCATED_BYTES_NAME);
+    }
+    IS_THREAD_ALLOCATED_MEMORY_SUPPORTED = isThreadAllocatedMemorySupported;
+    SUN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_METHOD = 
setThreadAllocatedMemoryEnabled;
+    SUN_GET_CURRENT_THREAD_ALLOCATED_BYTES_METHOD = 
getCurrentThreadAllocatedBytes;
+    SUN_GET_THREAD_ALLOCATED_BYTES_METHOD = getThreadAllocatedBytes;
   }
 
   public static boolean isThreadCpuTimeMeasurementEnabled() {
     return _isThreadCpuTimeMeasurementEnabled;
   }
 
   public static void setThreadCpuTimeMeasurementEnabled(boolean enable) {
-    _isThreadCpuTimeMeasurementEnabled = enable && 
IS_CURRENT_THREAD_CPU_TIME_SUPPORTED;
+    if (!IS_CURRENT_THREAD_CPU_TIME_SUPPORTED) {
+      assert !_isThreadCpuTimeMeasurementEnabled;
+      if (enable) {
+        LOGGER.error("Not enabling thread CPU time measurement because it is 
not supported");
+      }
+      return;
+    }
+    if (_isThreadCpuTimeMeasurementEnabled != enable) {
+      if (enable) {
+        LOGGER.info("Enabling thread CPU time measurement");
+      } else {
+        LOGGER.info("Disabling thread CPU time measurement");
+      }
+    }
+    try {
+      MX_BEAN.setThreadCpuTimeEnabled(enable);
+      _isThreadCpuTimeMeasurementEnabled = enable;
+    } catch (Exception e) {
+      LOGGER.error("Caught exception {} thread CPU time measurement", enable ? 
"enabling" : "disabling", e);
+      _isThreadCpuTimeMeasurementEnabled = false;
+    }
   }
 
   public static boolean isThreadMemoryMeasurementEnabled() {
     return _isThreadMemoryMeasurementEnabled;
   }
 
   public static void setThreadMemoryMeasurementEnabled(boolean enable) {
-
-    boolean isThreadAllocateMemoryEnabled = 
IS_THREAD_ALLOCATED_MEMORY_ENABLED_DEFAULT;
-    // if the jvm default enabling config is different
-    if (enable != IS_THREAD_ALLOCATED_MEMORY_ENABLED_DEFAULT) {
-      try {
-        Class<?> sunThreadMXBeanClass = 
Class.forName(SUN_THREAD_MXBEAN_CLASS_NAME);
-        
sunThreadMXBeanClass.getMethod(SUN_THREAD_MXBEAN_SET_THREAD_ALLOCATED_MEMORY_ENABLED_NAME,
 Boolean.TYPE)
-            .invoke(MX_BEAN, enable);
-        isThreadAllocateMemoryEnabled = (boolean) sunThreadMXBeanClass
-            
.getMethod(SUN_THREAD_MXBEAN_IS_THREAD_ALLOCATED_MEMORY_ENABLED_NAME)
-            .invoke(MX_BEAN);
-      } catch (ClassNotFoundException | IllegalAccessException | 
NoSuchMethodException | InvocationTargetException e) {
-        LOGGER.error("Not able to call isThreadAllocatedMemoryEnabled or 
setThreadAllocatedMemoryEnabled, ", e);
+    if (!IS_THREAD_ALLOCATED_MEMORY_SUPPORTED) {
+      assert !_isThreadMemoryMeasurementEnabled;

Review Comment:
   Similar to the CPU time assertion, this assertion at line 147 may fail if 
`setThreadMemoryMeasurementEnabled(true)` was called previously when memory 
measurement was supported. Consider removing this assertion or documenting the 
assumption that the support status cannot change at runtime.
   ```suggestion
   
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to