OneSizeFitsQuorum commented on code in PR #9743:
URL: https://github.com/apache/iotdb/pull/9743#discussion_r1180161727


##########
metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/jvm/JvmThreadMetrics.java:
##########
@@ -85,9 +93,25 @@ public void unbindFrom(AbstractMetricService metricService) {
 
   // VisibleForTesting
   static long getThreadStateCount(ThreadMXBean threadBean, Thread.State state) 
{
-    return 
Arrays.stream(threadBean.getThreadInfo(threadBean.getAllThreadIds()))
-        .filter(threadInfo -> threadInfo != null && 
threadInfo.getThreadState() == state)
-        .count();
+    checkAndUpdate(threadBean);
+    return threadStateCountMap.getOrDefault(state, 0);
+  }
+
+  private static void checkAndUpdate(ThreadMXBean threadBean) {
+    if (System.currentTimeMillis() - lastUpdateTime < UPDATE_INTERVAL) {
+      return;
+    }
+    lastUpdateTime = System.currentTimeMillis();
+    threadStateCountMap.clear();
+    List<ThreadInfo> infoList =
+        Arrays.asList(threadBean.getThreadInfo(threadBean.getAllThreadIds()));
+    infoList.forEach(
+        info -> {
+          if (info != null) {
+            Thread.State state = info.getThreadState();
+            threadStateCountMap.put(state, 
threadStateCountMap.getOrDefault(state, 0) + 1);

Review Comment:
   Use the compute API to reduce the number of hashes



##########
metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/jvm/JvmThreadMetrics.java:
##########
@@ -25,11 +25,19 @@
 import org.apache.iotdb.metrics.utils.MetricType;
 
 import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
 import java.lang.management.ThreadMXBean;
 import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /** This file is modified from 
io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics */
 public class JvmThreadMetrics implements IMetricSet {
+  private static long lastUpdateTime = 0L;
+  private static final long UPDATE_INTERVAL = 10_000L;
+  private static Map<Thread.State, Integer> threadStateCountMap = new 
HashMap<>();

Review Comment:
   Set the initial size?



##########
server/src/main/java/org/apache/iotdb/db/service/metrics/FileMetrics.java:
##########
@@ -245,9 +251,17 @@ private void 
unbindSystemRelatedMetrics(AbstractMetricService metricService) {
   }
 
   private long getOpenFileHandlersNumber() {

Review Comment:
   make this metric item from `core` to `important` or `normal`? If you think 
the implementation is fast on linux, you can put it at important level



-- 
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]

Reply via email to