Skip to content

Commit 78012cf

Browse files
use real time threshold for queue times
1 parent 16c56bf commit 78012cf

2 files changed

Lines changed: 13 additions & 27 deletions

File tree

dd-java-agent/agent-profiling/profiling-ddprof/src/main/java/com/datadog/profiling/ddprof/DatadogProfiler.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import com.datadog.profiling.controller.OngoingRecording;
3131
import com.datadog.profiling.utils.ProfilingMode;
32-
import com.datadog.profiling.utils.Timestamper;
3332
import com.datadoghq.profiler.ContextSetter;
3433
import com.datadoghq.profiler.JavaProfiler;
3534
import datadog.trace.api.profiling.RecordingData;
@@ -97,7 +96,7 @@ public static DatadogProfiler newInstance(ConfigProvider configProvider) {
9796

9897
private final List<String> orderedContextAttributes;
9998

100-
private final double queueTimeThresholdNanos;
99+
private final long queueTimeThresholdMillis;
101100

102101
private DatadogProfiler(ConfigProvider configProvider) {
103102
this(configProvider, getContextAttributes(configProvider));
@@ -134,11 +133,10 @@ private DatadogProfiler(ConfigProvider configProvider) {
134133
orderedContextAttributes.add(RESOURCE);
135134
}
136135
this.contextSetter = new ContextSetter(profiler, orderedContextAttributes);
137-
this.queueTimeThresholdNanos =
138-
1_000_000D
139-
* configProvider.getLong(
140-
PROFILING_QUEUEING_TIME_THRESHOLD_MILLIS,
141-
PROFILING_QUEUEING_TIME_THRESHOLD_MILLIS_DEFAULT);
136+
this.queueTimeThresholdMillis =
137+
configProvider.getLong(
138+
PROFILING_QUEUEING_TIME_THRESHOLD_MILLIS,
139+
PROFILING_QUEUEING_TIME_THRESHOLD_MILLIS_DEFAULT);
142140
}
143141

144142
void addThread() {
@@ -377,34 +375,21 @@ public void recordSetting(String name, String value, String unit) {
377375
}
378376

379377
public QueueTimeTracker newQueueTimeTracker() {
380-
return new QueueTimeTracker(this, timestamper().timestamp());
378+
return new QueueTimeTracker(this, profiler.getCurrentTicks());
381379
}
382380

383-
void recordQueueTimeEvent(long startTicks, Object task, Class<?> scheduler, Thread origin) {
381+
void recordQueueTimeEvent(
382+
long startMillis, long startTicks, Object task, Class<?> scheduler, Thread origin) {
384383
if (profiler != null) {
385-
Timestamper timestamper = timestamper();
386-
long endTicks = timestamper.timestamp();
387-
double durationNanos = timestamper.toNanosConversionFactor() * (endTicks - startTicks);
388-
if (durationNanos >= queueTimeThresholdNanos) {
384+
if (System.currentTimeMillis() - startMillis >= queueTimeThresholdMillis) {
389385
// note: because this type traversal can update secondary_super_cache (see JDK-8180450)
390386
// we avoid doing this unless we are absolutely certain we will record the event
391387
Class<?> taskType = TaskWrapper.getUnwrappedType(task);
392388
if (taskType != null) {
389+
long endTicks = profiler.getCurrentTicks();
393390
profiler.recordQueueTime(startTicks, endTicks, taskType, scheduler, origin);
394391
}
395392
}
396393
}
397394
}
398-
399-
private Timestamper timestamper() {
400-
// FIXME intended to be injectable, but still a singleton for currently pragmatic reasons.
401-
// We need a way to make the Controller responsible for creating the context integration
402-
// for the tracer, which also allows the context integration to be constant in the tracer,
403-
// as well as allowing for the various late initialisation needs for JFR on certain JDK
404-
// versions
405-
// note that this access does not risk using the default version so long as queue time is
406-
// guarded
407-
// by checking if JFR is ready (this currently happens in QueueTimeHelper, so this is safe)
408-
return Timestamper.timestamper();
409-
}
410395
}

dd-java-agent/agent-profiling/profiling-ddprof/src/main/java/com/datadog/profiling/ddprof/QueueTimeTracker.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class QueueTimeTracker implements QueueTiming {
88
private final DatadogProfiler profiler;
99
private final Thread origin;
1010
private final long startTicks;
11+
private final long startMillis;
1112
private WeakReference<Object> weakTask;
1213
// FIXME this can be eliminated by altering the instrumentation
1314
// since it is known when the item is polled from the queue
@@ -16,8 +17,8 @@ public class QueueTimeTracker implements QueueTiming {
1617
public QueueTimeTracker(DatadogProfiler profiler, long startTicks) {
1718
this.profiler = profiler;
1819
this.origin = Thread.currentThread();
19-
// TODO get this from JFR if available instead of making a JNI call
2020
this.startTicks = startTicks;
21+
this.startMillis = System.currentTimeMillis();
2122
}
2223

2324
@Override
@@ -36,7 +37,7 @@ public void close() {
3637
Object task = this.weakTask.get();
3738
if (task != null) {
3839
// indirection reduces shallow size of the tracker instance
39-
profiler.recordQueueTimeEvent(startTicks, task, scheduler, origin);
40+
profiler.recordQueueTimeEvent(startMillis, startTicks, task, scheduler, origin);
4041
}
4142
}
4243
}

0 commit comments

Comments
 (0)