-
Notifications
You must be signed in to change notification settings - Fork 331
Optimize PendingTrace span registration and time tracking #11078
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
Merged
gh-worker-dd-mergequeue-cf854d
merged 2 commits into
master
from
brian.marks/perf-pending-trace
Apr 10, 2026
+110
−5
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
99 changes: 99 additions & 0 deletions
99
dd-trace-core/src/jmh/java/datadog/trace/core/TimeSourceBenchmark.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,99 @@ | ||
| package datadog.trace.core; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.NANOSECONDS; | ||
|
|
||
| import datadog.trace.api.DDTraceId; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.TearDown; | ||
| import org.openjdk.jmh.annotations.Threads; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
|
|
||
| @State(Scope.Thread) | ||
| @Warmup(iterations = 3) | ||
| @Measurement(iterations = 5) | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @Threads(8) | ||
| @OutputTimeUnit(NANOSECONDS) | ||
| @Fork(value = 1) | ||
| public class TimeSourceBenchmark { | ||
|
|
||
| static final CoreTracer TRACER = CoreTracer.builder().build(); | ||
|
|
||
| private PendingTrace pendingTrace; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() { | ||
| TraceCollector collector = TRACER.createTraceCollector(DDTraceId.ONE); | ||
| pendingTrace = (PendingTrace) collector; | ||
| } | ||
|
|
||
| @TearDown(Level.Trial) | ||
| public void teardown() { | ||
| pendingTrace = null; | ||
| } | ||
|
|
||
| @Benchmark | ||
| public long getCurrentTimeNano() { | ||
| return pendingTrace.getCurrentTimeNano(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public long systemNanoTime() { | ||
| return System.nanoTime(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public long systemCurrentTimeMillis() { | ||
| return System.currentTimeMillis(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public long traceGetTimeWithNanoTicks() { | ||
| return TRACER.getTimeWithNanoTicks(System.nanoTime()); | ||
| } | ||
|
|
||
| /** | ||
| * Measures a full span start + finish cycle, exercising both the {@code rootSpan} CAS guard in | ||
| * {@link PendingTrace#registerSpan} and the {@code lazySet} of {@code lastReferenced} in {@link | ||
| * PendingTrace#getCurrentTimeNano}. | ||
| */ | ||
| @Benchmark | ||
| public void startAndFinishSpan() { | ||
| TRACER.startSpan("benchmark", "op").finish(); | ||
| } | ||
|
|
||
| @State(Scope.Benchmark) | ||
| public static class SharedState { | ||
| PendingTrace sharedTrace; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() { | ||
| TraceCollector collector = TRACER.createTraceCollector(DDTraceId.ONE); | ||
| sharedTrace = (PendingTrace) collector; | ||
| } | ||
|
|
||
| @TearDown(Level.Trial) | ||
| public void teardown() { | ||
| sharedTrace = null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Measures {@link PendingTrace#getCurrentTimeNano()} under cross-thread contention on a single | ||
| * shared {@code PendingTrace}. All threads write to the same {@code lastReferenced} field, | ||
| * demonstrating the benefit of {@code lazySet} over a volatile store under contention. | ||
| */ | ||
| @Benchmark | ||
| public long getCurrentTimeNano_contended(SharedState shared) { | ||
| return shared.sharedTrace.getCurrentTimeNano(); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Interesting. I haven't really benchmarked lazySet. My guess is this makes a bigger difference on ARM with its more relaxed memory model than x86, but we should measure.