Skip to content

Commit cdba08b

Browse files
jbachorikclaude
andcommitted
Add walkVM flow counters and stacktrace debug output
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1744519 commit cdba08b

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

ddprof-lib/src/main/cpp/counters.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@
7373
X(WALKVM_ANCHOR_NULL, "walkvm_anchor_null") \
7474
X(WALKVM_CACHED_NOT_JAVA, "walkvm_cached_not_java") \
7575
X(WALKVM_NO_VMTHREAD, "walkvm_no_vmthread") \
76-
X(WALKVM_VMTHREAD_OK, "walkvm_vmthread_ok")
76+
X(WALKVM_VMTHREAD_OK, "walkvm_vmthread_ok") \
77+
X(WALKVM_ANCHOR_USED_INLINE, "walkvm_anchor_used_inline") \
78+
X(WALKVM_ANCHOR_FALLBACK, "walkvm_anchor_fallback") \
79+
X(WALKVM_CODEH_NO_VM, "walkvm_codeh_no_vm") \
80+
X(WALKVM_DEPTH_ZERO, "walkvm_depth_zero") \
81+
X(WALKVM_HIT_CODEHEAP, "walkvm_hit_codeheap")
7782
#define X_ENUM(a, b) a,
7883
typedef enum CounterId : int {
7984
DD_COUNTER_TABLE(X_ENUM) DD_NUM_COUNTERS

ddprof-lib/src/main/cpp/stackWalker.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ __attribute__((no_sanitize("address"))) int StackWalker::walkVM(void* ucontext,
329329
// Walk until the bottom of the stack or until the first Java frame
330330
while (depth < actual_max_depth) {
331331
if (CodeHeap::contains(pc)) {
332+
Counters::increment(WALKVM_HIT_CODEHEAP);
332333
// If we're in JVM-generated code but don't have a VMThread, we cannot safely
333334
// walk the Java stack because crash protection is not set up.
334335
//
@@ -343,6 +344,7 @@ __attribute__((no_sanitize("address"))) int StackWalker::walkVM(void* ucontext,
343344
//
344345
// The missing VMThread is a timing issue during thread lifecycle.
345346
if (vm_thread == NULL) {
347+
Counters::increment(WALKVM_CODEH_NO_VM);
346348
fillFrame(frames[depth++], BCI_ERROR, "break_no_vmthread");
347349
break;
348350
}
@@ -560,6 +562,7 @@ __attribute__((no_sanitize("address"))) int StackWalker::walkVM(void* ucontext,
560562
// These workarounds will minimize the number of unknown frames for 'vm'
561563
// We want to keep the 'raw' data in 'vmx', though
562564
if (anchor) {
565+
Counters::increment(WALKVM_ANCHOR_USED_INLINE);
563566
uintptr_t prev_sp = sp;
564567
sp = anchor->lastJavaSP();
565568
fp = anchor->lastJavaFP();
@@ -673,6 +676,7 @@ __attribute__((no_sanitize("address"))) int StackWalker::walkVM(void* ucontext,
673676
// If we did not meet Java frame but current thread has JavaFrameAnchor set,
674677
// retry stack walking from the anchor
675678
if (anchor != NULL && anchor->getFrame(pc, sp, fp)) {
679+
Counters::increment(WALKVM_ANCHOR_FALLBACK);
676680
anchor = NULL;
677681
while (depth > 0 && frames[depth - 1].method_id == NULL) depth--; // pop unknown frames
678682
goto unwind_loop;
@@ -689,6 +693,10 @@ __attribute__((no_sanitize("address"))) int StackWalker::walkVM(void* ucontext,
689693
// aggregation by lumping unrelated samples under a single "unknown" entry
690694
depth = StackWalkValidation::dropUnknownLeaf(frames, depth);
691695

696+
if (depth == 0) {
697+
Counters::increment(WALKVM_DEPTH_ZERO);
698+
}
699+
692700
if (truncated) {
693701
if (depth > max_depth) {
694702
*truncated = true;

ddprof-test/src/test/java/com/datadoghq/profiler/context/TagContextTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public void test() throws InterruptedException {
5959
jfrCounters.put(nameAccessor.getMember(item), countAccessor.getMember(item).longValue());
6060
}
6161
}
62+
int debugPrinted = 0;
63+
int nullTraceCount = 0;
64+
int emptyTraceCount = 0;
6265
for (IItemIterable wallclockSamples : events) {
6366
IMemberAccessor<IQuantity, IItem> weightAccessor = WEIGHT.getAccessor(wallclockSamples.getType());
6467
// this will become more generic in the future
@@ -69,6 +72,12 @@ public void test() throws InterruptedException {
6972
IMemberAccessor<String, IItem> stacktraceAccessor = JdkAttributes.STACK_TRACE_STRING.getAccessor(wallclockSamples.getType());
7073
for (IItem sample : wallclockSamples) {
7174
String stacktrace = stacktraceAccessor.getMember(sample);
75+
if (stacktrace == null) { nullTraceCount++; continue; }
76+
if (stacktrace.isEmpty()) { emptyTraceCount++; continue; }
77+
if (debugPrinted < 5) {
78+
System.out.println("[DEBUG-TRACE-" + debugPrinted + "] " + stacktrace.substring(0, Math.min(500, stacktrace.length())));
79+
debugPrinted++;
80+
}
7281
if (!stacktrace.contains("sleep")) {
7382
// we don't know the context has been set for sure until the sleep has started
7483
continue;
@@ -134,13 +143,19 @@ public void test() throws InterruptedException {
134143
System.out.printf("Sample statistics: %d total (%d dropped, %.2f%%), weight %d total (%d dropped, %.2f%%)%n",
135144
totalSamplesCount, droppedSamplesCount, dropRate,
136145
totalSamplesWeight, droppedSamplesWeight, dropWeightRate);
146+
System.out.println("nullTraces=" + nullTraceCount + " emptyTraces=" + emptyTraceCount);
137147
// Print walkvm diagnostic counters
138148
System.out.println("walkvm_vmthread_ok=" + jfrCounters.getOrDefault("walkvm_vmthread_ok", 0L)
139149
+ " walkvm_no_vmthread=" + jfrCounters.getOrDefault("walkvm_no_vmthread", 0L)
140150
+ " walkvm_thread_inaccessible=" + jfrCounters.getOrDefault("walkvm_thread_inaccessible", 0L)
141151
+ " walkvm_anchor_null=" + jfrCounters.getOrDefault("walkvm_anchor_null", 0L)
142152
+ " walkvm_cached_not_java=" + jfrCounters.getOrDefault("walkvm_cached_not_java", 0L)
143153
+ " thread_entry_mark_detections=" + jfrCounters.getOrDefault("thread_entry_mark_detections", 0L));
154+
System.out.println("walkvm_hit_codeheap=" + jfrCounters.getOrDefault("walkvm_hit_codeheap", 0L)
155+
+ " walkvm_codeh_no_vm=" + jfrCounters.getOrDefault("walkvm_codeh_no_vm", 0L)
156+
+ " walkvm_anchor_used_inline=" + jfrCounters.getOrDefault("walkvm_anchor_used_inline", 0L)
157+
+ " walkvm_anchor_fallback=" + jfrCounters.getOrDefault("walkvm_anchor_fallback", 0L)
158+
+ " walkvm_depth_zero=" + jfrCounters.getOrDefault("walkvm_depth_zero", 0L));
144159
}
145160
}
146161

0 commit comments

Comments
 (0)