This is an automated email from the ASF dual-hosted git repository. marklau99 pushed a commit to branch fix-npe-in-cpu-metrics in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 94a98db7e27ca53f83bfbaba361cba311dbf8c73 Author: Liu Xuxin <[email protected]> AuthorDate: Wed Jun 7 16:35:23 2023 +0800 remove null thread info --- .../metrics/metricsets/cpu/CpuUsageMetrics.java | 31 ++++++++++++++-------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/CpuUsageMetrics.java b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/CpuUsageMetrics.java index b2896d1e130..8c163caca3b 100644 --- a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/CpuUsageMetrics.java +++ b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/CpuUsageMetrics.java @@ -32,8 +32,10 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.concurrent.atomic.AtomicLong; import java.util.function.UnaryOperator; +import java.util.stream.Collectors; public class CpuUsageMetrics implements IMetricSet { private static final String MODULE_CPU_USAGE = "module_cpu_usage"; @@ -165,10 +167,12 @@ public class CpuUsageMetrics implements IMetricSet { // update long[] taskIds = threadMxBean.getAllThreadIds(); ThreadInfo[] threadInfos = threadMxBean.getThreadInfo(taskIds); + List<ThreadInfo> threadInfoList = + Arrays.stream(threadInfos).filter(Objects::nonNull).collect(Collectors.toList()); Map<Long, Long> currentThreadCpuTime = new HashMap<>(taskIds.length + 1, 1.0f); Map<Long, Long> currentThreadUserTime = new HashMap<>(taskIds.length + 1, 1.0f); - collectThreadCpuInfo(currentThreadCpuTime, currentThreadUserTime, threadInfos); + collectThreadCpuInfo(currentThreadCpuTime, currentThreadUserTime, threadInfoList); Map<String, Long> moduleIncrementCpuTimeMap = new HashMap<>(modules.size() + 1, 1.0f); Map<String, Long> moduleIncrementUserTimeMap = new HashMap<>(modules.size() + 1, 1.0f); @@ -185,7 +189,7 @@ public class CpuUsageMetrics implements IMetricSet { lastThreadUserTime, currentThreadCpuTime, currentThreadUserTime, - threadInfos); + threadInfoList); if (totalIncrementTime == 0L) { return; @@ -214,14 +218,16 @@ public class CpuUsageMetrics implements IMetricSet { } private void collectThreadCpuInfo( - Map<Long, Long> cpuTimeMap, Map<Long, Long> userTimeMap, ThreadInfo[] threadInfos) { - Arrays.stream(threadInfos) - .forEach( - info -> { - cpuTimeMap.put(info.getThreadId(), threadMxBean.getThreadCpuTime(info.getThreadId())); - userTimeMap.put( - info.getThreadId(), threadMxBean.getThreadUserTime(info.getThreadId())); - }); + Map<Long, Long> cpuTimeMap, Map<Long, Long> userTimeMap, List<ThreadInfo> threadInfos) { + threadInfos.forEach( + info -> { + long cpuTime = threadMxBean.getThreadCpuTime(info.getThreadId()); + long userTime = threadMxBean.getThreadUserTime(info.getThreadId()); + if (cpuTime != -1L && userTime != -1L) { + cpuTimeMap.put(info.getThreadId(), cpuTime); + userTimeMap.put(info.getThreadId(), userTime); + } + }); } @SuppressWarnings("java:S107") @@ -234,7 +240,7 @@ public class CpuUsageMetrics implements IMetricSet { Map<Long, Long> beforeThreadUserTime, Map<Long, Long> afterThreadCpuTime, Map<Long, Long> afterThreadUserTime, - ThreadInfo[] threadInfos) { + List<ThreadInfo> threadInfos) { long totalIncrementTime = 0L; for (ThreadInfo threadInfo : threadInfos) { long id = threadInfo.getThreadId(); @@ -242,6 +248,9 @@ public class CpuUsageMetrics implements IMetricSet { long afterCpuTime = afterThreadCpuTime.get(id); long beforeUserTime = beforeThreadUserTime.getOrDefault(id, 0L); long afterUserTime = afterThreadUserTime.get(id); + if (afterCpuTime < beforeCpuTime) { + continue; + } totalIncrementTime += afterCpuTime - beforeCpuTime; String module = getThreadModuleById(id, threadInfo); String pool = getThreadPoolById(id, threadInfo);
