-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Remove redundant method calls from ThreadStatesGaugeSet
#5098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release/4.2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,8 @@ | |
| import java.lang.management.ThreadInfo; | ||
| import java.lang.management.ThreadMXBean; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.EnumMap; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
|
|
@@ -21,6 +22,7 @@ public class ThreadStatesGaugeSet implements MetricSet { | |
|
|
||
| // do not compute stack traces. | ||
| private final static int STACK_TRACE_DEPTH = 0; | ||
| private final static int[] ZERO_COUNT = new int[1]; | ||
|
|
||
| private final ThreadMXBean threads; | ||
| private final ThreadDeadlockDetector deadlockDetector; | ||
|
|
@@ -46,34 +48,35 @@ public ThreadStatesGaugeSet(ThreadMXBean threads, | |
|
|
||
| @Override | ||
| public Map<String, Metric> getMetrics() { | ||
| final Map<String, Metric> gauges = new HashMap<>(); | ||
| final Map<String, Metric> gauges = new LinkedHashMap<>(); // deterministic order | ||
|
|
||
| // expensive methods only once, whether or not "cached" | ||
| ThreadInfo[] threadInfos = getThreadInfo(); | ||
| Set<String> deadlockedThreads = deadlockDetector.getDeadlockedThreads(); | ||
|
|
||
| EnumMap<Thread.State, int[]> byState = new EnumMap<>(Thread.State.class); | ||
| for (ThreadInfo threadInfo : threadInfos) { | ||
| Thread.State tState; | ||
| if (threadInfo != null && (tState = threadInfo.getThreadState()) != null) { | ||
| byState.computeIfAbsent(tState, (k) -> new int[1])[0]++; | ||
| } | ||
| } | ||
|
|
||
| for (final Thread.State state : Thread.State.values()) { | ||
| gauges.put(name(state.toString().toLowerCase(), "count"), | ||
| (Gauge<Object>) () -> getThreadCount(state)); | ||
| (Gauge<Object>) () -> byState.getOrDefault(state, ZERO_COUNT)[0]); | ||
| } | ||
|
|
||
| gauges.put("count", (Gauge<Integer>) threads::getThreadCount); | ||
| gauges.put("daemon.count", (Gauge<Integer>) threads::getDaemonThreadCount); | ||
| gauges.put("peak.count", (Gauge<Integer>) threads::getPeakThreadCount); | ||
| gauges.put("total_started.count", (Gauge<Long>) threads::getTotalStartedThreadCount); | ||
| gauges.put("deadlock.count", (Gauge<Integer>) () -> deadlockDetector.getDeadlockedThreads().size()); | ||
| gauges.put("deadlocks", (Gauge<Set<String>>) deadlockDetector::getDeadlockedThreads); | ||
| gauges.put("deadlock.count", (Gauge<Integer>) deadlockedThreads::size); | ||
| gauges.put("deadlocks", (Gauge<Set<String>>) () -> deadlockedThreads); | ||
|
Comment on lines
+54
to
+75
|
||
|
|
||
| return Collections.unmodifiableMap(gauges); | ||
| } | ||
|
|
||
| private int getThreadCount(Thread.State state) { | ||
| final ThreadInfo[] allThreads = getThreadInfo(); | ||
| int count = 0; | ||
| for (ThreadInfo info : allThreads) { | ||
| if (info != null && info.getThreadState() == state) { | ||
| count++; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| ThreadInfo[] getThreadInfo() { | ||
| return threads.getThreadInfo(threads.getAllThreadIds(), STACK_TRACE_DEPTH); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ZERO_COUNT constant is defined as an int array
new int[1]but is used as a shared constant. While this works for the intended purpose (returning 0 for thread states with no threads), using a mutable array as a constant is an anti-pattern. If any code were to modify ZERO_COUNT[0], it would affect all thread states that use it. Consider using a constant int value0directly, or if an array is needed for the getOrDefault pattern, create a new array each time or document that the array must never be modified.