-
Notifications
You must be signed in to change notification settings - Fork 253
Add a metric that exposes IO thread utilization #192
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
Draft
jasonk000
wants to merge
1
commit into
master
Choose a base branch
from
jkoch/loop-cpu-utilization-metric
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
evcache-core/src/main/java/com/netflix/evcache/pool/EVCacheLoopProbe.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package com.netflix.evcache.pool; | ||
|
|
||
| import java.lang.management.ManagementFactory; | ||
| import java.lang.management.ThreadMXBean; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Publishes event-loop CPU utilization from the loop thread itself. | ||
| * | ||
| * <p>The loop thread periodically publishes an immutable {@code long[]} snapshot | ||
| * containing {@code {threadCpuNs, wallNs}}. Spectator's polling thread reads the | ||
| * latest snapshot and computes the delta ratio without performing cross-thread | ||
| * ThreadMXBean lookups.</p> | ||
| */ | ||
| public final class EVCacheLoopProbe { | ||
| private static final Logger log = LoggerFactory.getLogger(EVCacheLoopProbe.class); | ||
| private static final ThreadMXBean THREAD_MX_BEAN = ManagementFactory.getThreadMXBean(); | ||
| private static final long PUBLISH_INTERVAL_NS = TimeUnit.MILLISECONDS.toNanos(1_000); | ||
| private static final int CPU_UTILIZATION_WARNING_THRESHOLD = 3; | ||
|
|
||
| private final AtomicReference<long[]> snapshot = new AtomicReference<long[]>(new long[] { 0L, 0L }); | ||
| private final boolean cpuTimeAvailable; | ||
|
|
||
| // Loop-thread-private throttle state. | ||
| private long nextPublishNs; | ||
| private boolean tickFailureLogged; | ||
| private boolean negativeCpuTimeLogged; | ||
|
|
||
| // PolledMeter-reader-private state. | ||
| private long prevCpuNs; | ||
| private long prevWallNs; | ||
| private int aboveOneSamples; | ||
| private boolean aboveOneLogged; | ||
|
|
||
| public EVCacheLoopProbe() { | ||
| this.cpuTimeAvailable = isCurrentThreadCpuTimeAvailable(); | ||
| if (!cpuTimeAvailable) { | ||
| log.warn("Thread CPU time is not available; EVCache loop CPU utilization will report NaN"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Publish the current thread's CPU time and wall time at most every second. | ||
| * | ||
| * <p>This method is intentionally no-throw: it is called from the EVCache IO | ||
| * loop in a finally block and must never terminate the loop.</p> | ||
| */ | ||
| public void tick() { | ||
| try { | ||
| tickInternal(); | ||
| } catch (Throwable t) { | ||
| if (!tickFailureLogged) { | ||
| tickFailureLogged = true; | ||
| try { | ||
| log.warn("EVCache loop CPU utilization probe failed; suppressing future probe errors", t); | ||
| } catch (Throwable ignored) { | ||
| // Keep the event loop alive even if logging fails. | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void tickInternal() { | ||
| if (!cpuTimeAvailable) return; | ||
|
|
||
| final long now = System.nanoTime(); | ||
| if (nextPublishNs != 0L && now - nextPublishNs < 0L) return; | ||
| nextPublishNs = now + PUBLISH_INTERVAL_NS; | ||
|
|
||
| final long cpuNs = THREAD_MX_BEAN.getCurrentThreadCpuTime(); | ||
| if (cpuNs < 0L) { | ||
| if (!negativeCpuTimeLogged) { | ||
| negativeCpuTimeLogged = true; | ||
| log.warn("Thread CPU time returned a negative value; skipping EVCache loop CPU utilization publish"); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| snapshot.lazySet(new long[] { cpuNs, now }); | ||
| } | ||
|
|
||
| /** | ||
| * Return loop-thread CPU utilization over the interval since the previous poll. | ||
| */ | ||
| public double sampleUtilization() { | ||
| if (!cpuTimeAvailable) return Double.NaN; | ||
|
|
||
| final long[] s = snapshot.get(); | ||
| final long cpuNs = s[0]; | ||
| final long wallNs = s[1]; | ||
| if (prevWallNs == 0L) { | ||
| prevCpuNs = cpuNs; | ||
| prevWallNs = wallNs; | ||
| return Double.NaN; | ||
| } | ||
|
|
||
| final long dWall = wallNs - prevWallNs; | ||
| if (dWall <= 0L) return 0.0; | ||
|
|
||
| final long dCpu = cpuNs - prevCpuNs; | ||
| prevCpuNs = cpuNs; | ||
| prevWallNs = wallNs; | ||
|
|
||
| double utilization = (double) dCpu / (double) dWall; | ||
| if (utilization < 0.0) return 0.0; | ||
|
|
||
| if (utilization > 1.0) { | ||
| aboveOneSamples++; | ||
| if (aboveOneSamples >= CPU_UTILIZATION_WARNING_THRESHOLD && !aboveOneLogged) { | ||
| aboveOneLogged = true; | ||
| log.warn("EVCache loop CPU utilization exceeded 1.0 for {} consecutive samples; latest value={}", | ||
| CPU_UTILIZATION_WARNING_THRESHOLD, utilization); | ||
| } | ||
| } else { | ||
| aboveOneSamples = 0; | ||
| } | ||
|
|
||
| return Math.min(utilization, 1.05); | ||
| } | ||
|
|
||
| private static boolean isCurrentThreadCpuTimeAvailable() { | ||
| try { | ||
| return THREAD_MX_BEAN.isThreadCpuTimeSupported() && THREAD_MX_BEAN.isThreadCpuTimeEnabled(); | ||
| } catch (Throwable t) { | ||
| log.warn("Unable to determine ThreadMXBean CPU-time capability", t); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm a little confused as to what we're measuring in this PR - is this different from looking the values reported for CPU time on a thread dump?
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.
Yes, that's exactly it. Exposing this as a metric allows us to look at it easily and assess this as a potential tuning/hotspot if we move a client to a larger instance type (e.g. will 2x the traffic lead to congestion at evcache client).