Skip to content

feat(taskblock): emit the elapsed part of open blocked intervals at each dump (PROF-16858) - #1052

Draft
kaahos wants to merge 18 commits into
paul.fournillon/native_socket_nettyfrom
paul.fournillon/prof-16858-open-interval-segments
Draft

kaahos wants to merge 18 commits into
paul.fournillon/native_socket_nettyfrom
paul.fournillon/prof-16858-open-interval-segments

Conversation

@kaahos

@kaahos kaahos commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?:
At each dump, and at stop, java-profiler now emits the elapsed part of every still-open owned blocked interval as a TaskBlock. Long blocks therefore count in every upload they span.

  • Segment state: each tracker slot gets a {control, start[2]} pair. control packs a generation and an index into start[].
  • Dump side: the dump thread, the only one that advances segments, writes the next start into the other entry and CASes control.
  • Exit side: the exit, and every path that ends a run, closes control with one fetch_and. The exit then emits only the last segment.
  • Exactly once: exit and dump race on that one word, so each tick is reported once. Signal handlers never touch segments.
  • Stacks: captured with one batched JVMTI GetThreadListStackTraces before the dump takes the stripes. Only threads whose JVMTI state matches their block are reported, so abandoned intervals are skipped.
  • Schema: new boolean field continued on datadog.TaskBlock, true on all segments but the last.

Motivation:
A dump used to emit nothing for an open interval, so earlier uploads lost long blocks and a thread that never unblocked was never reported (PROF-16858). dd-trace-java#11546's sleep-across-uploads smoke test expects segments.

Additional Notes:

  • Stacked on feat(natsock): cover validated Netty epoll images and composed lifecycle (PROF-14931) #730 (merged up to feat(natsock): cover validated Netty epoll images and composed lifecycle (PROF-14931) #730 at 12ddc3b). Independent of feat(taskblock): capture contended-monitor stacks before the lock is held (PROF-16870) #1051, whose entry-stack pool is reverted here (PROF-16870: about +9% CPU under short contention).
  • Accepted behaviour, reviewer acknowledgment needed:
    • Segment content: dump segments carry the blocked frame's stack from frame 0, with no blocker, unblocking span or context attributes. Exit stacks start at the hook's caller.
    • Counter: TASK_BLOCK_EMITTED now counts segments, not intervals.
    • Drops at rotation: an exit dropped during dump or stop rotation loses its remainder (ticks are reported at most once), so a continued chain can lack its last segment.
  • Closing segment when a dump passes the sampled end: a monitor exit samples its end before claiming the segment. If a dump advanced the segment past that end, the exit writes a one-tick closing segment, so the chain still ends with continued=false and the blocker. Eligibility is judged on the real end.
  • Cost: one JVMTI batch call per dump, only when blocked threads exist. On JDK 8 and 11 that call is one safepoint.
  • Changed tests: three encoded the old "nothing is emitted for a block still open at dump or stop" contract and now assert segmentation: liveDumpPreservesCompletedTaskBlockInterval, the NativeSocketTaskBlockLifecycleTest stop case, and NettyEpollTaskBlockTest.verifySurvivorAcrossRestart. In the last one, a Netty wait that survives a restart now leaves exactly one continued segment in the stopping recording. The Netty interval-counting helper skips continued segments, so they cannot hide a lost completed interval.
  • Open: a retried stop() skips collection.
  • Human review: mandatory. It touches the block-owner protocol read by the wall-clock signal handler, the dump path and the JFR schema (backend owner needed for continued).
  • Review: a fresh-context agent review; its one High (abandoned intervals reported at every dump) is fixed, with a test.

How to test the change?:

  • dd-trace-java smoke tests against this branch (Linux arm64, JDK 17, on 998dd6c): NativeIo, LockSupport, ObjectWait and BlockingMix passed. ThreadSleepTaskBlockProfilingTest passed 2 of 3 runs; that class fails 3/3 against feat(natsock): cover validated Netty epoll images and composed lifecycle (PROF-14931) #730. The failing case, onlySpanlessPlatformSleepsEmitTaskBlocks, saw one 4 ms span-free event on the "active" thread, outside its span. The test counts every TaskBlock on that thread, including span-free class-loading contention at thread start, which this app shows routinely on other threads. It did not recur in 10 reruns. The test fix is on dd-trace-java#11546.
  • Backend: on a real recording with a 3 s sleep across two dumps, profiling-backend#9442 reconstructs each upload's segment exactly, and the sum matches the interval.
  • nativeReadSpanningDumpContributesToBothChunks: counts only the stream read and its blocker-less dump segment. Passed 20/20 on Linux aarch64 JDK 17 debug (on 998dd6c, a fresh clone per run), plus a full *TaskBlock* run.
  • taskBlockSegments_ut: a dump-vs-exit race run 100,000 times. Passed on debug and TSan, Linux aarch64 (podman).
  • *TaskBlock* Java tests (137): passed on macOS, and on Linux aarch64 JDK 21 including the native read case.
  • gtests, spotlessCheck, qualityChecks, buildRelease: passed.
  • Mutations (token check, CAS, last segment, claim check, continued, stop close): all killed.

For Datadog employees:

  • If this PR touches code that signs or publishes builds or packages, or handles
    credentials of any kind, I've requested a security review (run the dd:platform-security-review
    skill, or file a request via the PSEC review form).
    bewaire also runs automatically on every PR.
  • This PR doesn't touch any of that.
  • JIRA: PROF-16858

@kaahos kaahos added the human_review:mandatory Human review required — see PR description label Nov 24, 2026
@kaahos

kaahos commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@DataDog review

@datadog-official datadog-official Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: FAIL

Open TaskBlock duration can be omitted while a Java-owned wait reacquires its monitor, or when stop succeeds only after a retry. Both paths can close pending segments without reporting their elapsed duration.

Open Bits AI session

🤖 Bits Code Review · Commit bcd7894

OSThreadState state = block != nullptr ? block->activeBlockState() : OSThreadState::UNKNOWN;
// Same rejections as the exit: a context crossing or a too-short segment.
if (identity == nullptr || state == OSThreadState::UNKNOWN ||
!blockedInJvmti(candidate, state) ||

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Preserve Java-owned waits during monitor reacquisition

With Java-owned Object.wait instrumentation, a notified waiter remains inside the call until it reacquires the monitor. JDK 17/21 probes observed BLOCKED while Object.wait remained on the stack. Requiring IN_OBJECT_WAIT therefore rejects a legitimate open interval during reacquisition: dumps omit its duration, and stop closes it without reporting it. Accept monitor-entry blocking for JAVA-owned OBJECT_WAIT runs.

Suggested change
!blockedInJvmti(candidate, state) ||
!(blockedInJvmti(candidate, state) ||
(state == OSThreadState::OBJECT_WAIT &&
block->activeBlockOwner() == BlockRunOwner::JAVA &&
blockedInJvmti(candidate, OSThreadState::MONITOR_WAIT))) ||

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🤖 Bits Code Review · Open Bits AI session

}
// Stop is a final dump: capture open intervals before TaskBlock is disabled.
OpenTaskBlockSegments open_segments(_max_stack_depth);
if (taskBlockEnabled()) open_segments.collect(&_thread_filter, &_block_tracker);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Retain open-segment collection when stop is retried

A signal-drain or TaskBlock-rotation timeout returns after disabling TaskBlock, destroying this collector while leaving the profiler RUNNING. A subsequent successful stop skips collection because taskBlockEnabled() is false, then closes all remaining segments without reporting their pending duration. Collection eligibility must survive producer shutdown; update the admission-dependent collection gates in dump and stop consistently, or preserve collection across retries.

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🤖 Bits Code Review · Open Bits AI session

@dd-octo-sts

dd-octo-sts Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

CI Test Results

Run: #45835135905 | Commit: de86727 | Duration: 29m 35s (longest job)

✅ All 32 test jobs passed

Status Overview

JDK glibc-aarch64/debug glibc-amd64/debug musl-aarch64/debug musl-amd64/debug
8 - ✅ - -
8-ibm - ✅ - -
8-j9 ✅ ✅ - -
8-librca - - ✅ ✅
8-orcl - ✅ - -
11 - ✅ - -
11-j9 ✅ ✅ - -
11-librca - - ✅ ✅
17 ✅ ✅ - -
17-graal ✅ ✅ - -
17-j9 ✅ ✅ - -
17-librca - - ✅ ✅
21 ✅ ✅ - -
21-graal ✅ ✅ - -
21-librca - - ✅ ✅
25 ✅ ✅ - -
25-graal ✅ ✅ - -
25-librca - - ✅ ✅

Legend: ✅ passed | ❌ failed | ⚪ skipped | 🚫 cancelled

Summary: Total: 32 | Passed: 32 | Failed: 0


Updated: 2026-11-26 14:35:12 UTC

@kaahos
kaahos changed the base branch from paul.fournillon/native_socket_netty to paul.fournillon/prof-16870-monitor-entry-capture November 24, 2026 14:22
@kaahos
kaahos changed the base branch from paul.fournillon/prof-16870-monitor-entry-capture to paul.fournillon/native_socket_netty November 24, 2026 21:41
@datadog-official

datadog-official Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Pipelines

✨ Unblock PR with BitsAI

❌ Errors

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 1 Pipeline job failed

DataDog/java-profiler | memcheck:report — 🔧 Needs a code fix, caused by this PR

View more details · View in GitLab

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: d6353c2 | Docs | View more details | Give us feedback!

@dd-octo-sts

dd-octo-sts Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Memory overhead check: ❌ over budget

Profiler d6353c2d0, pipeline. Budget: utils/memcheck/budget.txt. NMT and /proc values are profiled minus no-profiler runs on the same runner.

scenario metric value Δ since budget set limit headroom
allocs native_mem_live_bytes 22.09 MiB +414 KiB 21.95 MiB −148 KiB ❌ OVER
allocs native_mem_max_bytes 23.34 MiB +403 KiB 23.20 MiB −147 KiB ❌ OVER
allocs nmt.class 301 KiB +4 KiB 563 KiB 263 KiB ok
allocs nmt.internal 369 KiB +0 KiB 666 KiB 297 KiB ok
classesM native_mem_live_bytes (coverage-corrected) 23.71 MiB +436 KiB 23.55 MiB −160 KiB ❌ OVER
classesM native_mem_max_bytes (coverage-corrected) 25.60 MiB +418 KiB 25.45 MiB −152 KiB ❌ OVER
classesM nmt.class 2.94 MiB −4 KiB 3.20 MiB 271 KiB ok
classesM nmt.internal 3.19 MiB −7 KiB 3.50 MiB 315 KiB ok
mixed native_mem_live_bytes 23.59 MiB +446 KiB 23.45 MiB −139 KiB ❌ OVER
mixed native_mem_max_bytes 41.17 MiB +456 KiB 41.00 MiB −169 KiB ❌ OVER
mixed nmt.class 300 KiB −7 KiB 563 KiB 263 KiB ok
mixed nmt.internal 411 KiB +2 KiB 717 KiB 305 KiB ok
threads native_mem_live_bytes 19.60 MiB +523 KiB 19.35 MiB −257 KiB ❌ OVER
threads native_mem_max_bytes 20.63 MiB +532 KiB 20.40 MiB −236 KiB ❌ OVER
threads nmt.class 110 KiB −3 KiB 410 KiB 300 KiB ok
threads nmt.internal 158 KiB −6 KiB 461 KiB 303 KiB ok
traces native_mem_live_bytes 20.63 MiB +412 KiB 20.50 MiB −136 KiB ❌ OVER
traces native_mem_max_bytes 21.84 MiB +444 KiB 21.70 MiB −147 KiB ❌ OVER
traces nmt.class 468 KiB −3 KiB 768 KiB 300 KiB ok
traces nmt.internal 615 KiB +1 KiB 870 KiB 255 KiB ok

✅ nmt.tracing absent in every scenario.

Not budgeted (too noisy on shared runners)
scenario nmt.code nmt.gc nmt.total proc.VmRSS proc.RssAnon
allocs 1.72 MiB 3 KiB 2.80 MiB 31.50 MiB 27.00 MiB
classesM 5.41 MiB 57 KiB 13.68 MiB 41.09 MiB 36.44 MiB
mixed 1.45 MiB −1 KiB 2.79 MiB 32.15 MiB 27.38 MiB
threads 4 KiB 0 KiB 815 KiB 25.16 MiB 20.03 MiB
traces 3.04 MiB −23 KiB 4.53 MiB 30.34 MiB 25.74 MiB

A limit was exceeded. Either fix the regression, or raise the limit in utils/memcheck/budget.txt in this PR and explain why in the PR description.

@dd-octo-sts

dd-octo-sts Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Scan-Build Report

User:runner@runnervm0hi8g
Working Directory:/home/runner/work/java-profiler/java-profiler/ddprof-lib/src/test/make
Command Line:make -j4 all
Clang Version:Ubuntu clang version 21.1.8 (6ubuntu1)
Date:Wed Nov 25 18:20:34 2026

Bug Summary

Bug TypeQuantityDisplay?
All Bugs1
Memory error
Insufficient storage for placement new1

Filter Results by File

Reports

Bug Group Bug Type ▾ File Function/Method Line Path Length
Memory errorInsufficient storage for placement newbuffers.hRecordingBuffer20414

@dd-octo-sts

dd-octo-sts Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

✅ All 40 integration tests passed

📊 Dashboard · 👷 Pipeline · 📦 d6353c2d

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

human_review:mandatory Human review required — see PR description

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant