-
Notifications
You must be signed in to change notification settings - Fork 336
PROF-1037: Capture thread CPU time elapsed in scope #1358
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
Changes from all commits
4d0d36e
9bd8d14
c9dad48
1511296
7597450
551e331
d283b3e
d579738
91a4146
f59cf0e
e1350c2
e41bafc
e43088e
a992b16
17c01c6
2515520
ac70cad
ea821f0
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 |
|---|---|---|
|
|
@@ -2,12 +2,14 @@ | |
|
|
||
| import datadog.opentracing.DDSpanContext; | ||
| import datadog.opentracing.jfr.DDScopeEvent; | ||
| import datadog.trace.common.util.ThreadCpuTimeAccess; | ||
| import jdk.jfr.Category; | ||
| import jdk.jfr.Description; | ||
| import jdk.jfr.Event; | ||
| import jdk.jfr.Label; | ||
| import jdk.jfr.Name; | ||
| import jdk.jfr.StackTrace; | ||
| import jdk.jfr.Timespan; | ||
|
|
||
| @Name("datadog.Scope") | ||
| @Label("Scope") | ||
|
|
@@ -38,13 +40,19 @@ public final class ScopeEvent extends Event implements DDScopeEvent { | |
| @Label("Operation Name") | ||
| private String operationName; | ||
|
|
||
| @Label("Thread CPU Time") | ||
| @Timespan | ||
| // does not need to be volatile since the event is created and committed from the same thread | ||
| private long cpuTime = 0L; | ||
|
|
||
| ScopeEvent(final DDSpanContext spanContext) { | ||
| this.spanContext = spanContext; | ||
| } | ||
|
|
||
| @Override | ||
| public void start() { | ||
| if (isEnabled()) { | ||
| cpuTime = ThreadCpuTimeAccess.getCurrentThreadCpuTime(); | ||
| begin(); | ||
| } | ||
| } | ||
|
|
@@ -53,6 +61,9 @@ public void start() { | |
| public void finish() { | ||
| end(); | ||
| if (shouldCommit()) { | ||
| if (cpuTime > 0) { | ||
| cpuTime = ThreadCpuTimeAccess.getCurrentThreadCpuTime() - cpuTime; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any risk of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is called solely from
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say that if this happens things will not blow up but data we produce would be off. But arguably this is the best we can do in this situation anyway. |
||
| } | ||
|
jbachorik marked this conversation as resolved.
|
||
| traceId = spanContext.getTraceId().toString(IDS_RADIX); | ||
| spanId = spanContext.getSpanId().toString(IDS_RADIX); | ||
| parentId = spanContext.getParentId().toString(IDS_RADIX); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package datadog.trace.common.util; | ||
|
|
||
| import java.lang.management.ManagementFactory; | ||
| import java.lang.management.ThreadMXBean; | ||
|
|
||
| /** Thread CPU time provider based on {@linkplain ThreadMXBean#getCurrentThreadCpuTime()} */ | ||
| final class JmxThreadCpuTimeProvider implements ThreadCpuTimeProvider { | ||
| private final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); | ||
|
|
||
| public static final JmxThreadCpuTimeProvider INSTANCE = new JmxThreadCpuTimeProvider(); | ||
|
|
||
| private JmxThreadCpuTimeProvider() {} | ||
|
|
||
| /** | ||
| * @return the actual thread CPU time as reported by {@linkplain | ||
| * ThreadMXBean#getCurrentThreadCpuTime()} | ||
| */ | ||
| @Override | ||
| public long getThreadCpuTime() { | ||
| return threadMXBean.getCurrentThreadCpuTime(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package datadog.trace.common.util; | ||
|
|
||
| final class NoneThreadCpuTimeProvider implements ThreadCpuTimeProvider { | ||
| @Override | ||
| public long getThreadCpuTime() { | ||
| return Long.MIN_VALUE; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.