Skip to content

Let an idle trace writer release its payload buffer's surplus chunks - #13507

Open
dougqh wants to merge 6 commits into
dougqh/chunked-dsm-statsfrom
dougqh/chunked-idle-trim
Open

dougqh wants to merge 6 commits into
dougqh/chunked-dsm-statsfrom
dougqh/chunked-idle-trim

Conversation

@dougqh

@dougqh dougqh commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What Does This Do

When the tracer has no traces to send, it now gradually releases the serialization buffer memory left over from the last burst of traffic. After a couple of seconds with nothing to send, the trace writer's payload buffer shrinks to a single small chunk instead of keeping the burst's chunks indefinitely.

Motivation

Steady-state heap dumps of PetClinic at a 128m heap show the trace writer's buffer holding chunks but no messages. Each dump was taken after 150 s of load, then idle until past 6.5 minutes of uptime, so past the agent's 5-minute type-pool clear:

Protocol Chunks held Retained Messages
v0.4 (default) 13 3.4 MB 0
v0.5 8 2.1 MB 0

That is about a quarter of the agent's whole steady-state heap cost over the same app without the agent (+13.4 MB with v0.4). It is retained for as long as the service stays quiet, which is exactly when it is easiest to give back. On small heaps, it competes with the application's live set.

ChunkedBuffer already releases surplus chunks gradually: each reset() keeps what the completed messages used plus half of any earlier surplus. But the trace writer only resets when it sends a payload, so once traffic stops, nothing releases anything.

Additional Notes

  • ChunkedBuffer.trim() applies reset()'s retention to an idle buffer. It does nothing while the buffer holds any data, completed or unfinished. Otherwise it halves the surplus, so repeated calls drain it to one chunk.

  • PayloadDispatcherImpl.flush() calls trim() when there is nothing to send and no payload has been handed over for at least 2 s. The time of the last payload resets whenever one is sent, from a full buffer or from a flush. After that, the periodic flush (1 s by default) drains an idle writer in a few calls: for the v0.4 case above, 13 → 7 → 4 → 2 → 1 chunks.

    • Steady traffic, and bursts with gaps of less than 2 s, keep reusing their chunks instead of freeing and reallocating them for the next payload.
    • The delay is measured in time, not in idle flushes, so it doesn't shrink when dd.trace.flush.interval is lowered.
  • Why the dispatcher and not ChunkedStreamingBuffer.flush(): MsgPackWriter.flush() never calls the buffer when it is empty. Its oversize-retry path also already calls reset() after an empty flush, so trimming inside the buffer's flush would halve twice there.

  • Stacked on Serialize client stats payloads into a chunked buffer #13097, which makes chunks start at 4 KiB and double. With both changes, an idle writer drains to a 4 KiB chunk rather than a 256 KiB one.

  • Not affected: the client stats and data streams writers. Each of their payloads is a single marked message that they flush or reset themselves.

  • Measured (before/after heap dumps):

    Heap dumps of PetClinic at a 128m heap with the default v0.4 protocol. Each was taken at about 390 s of uptime, after 150 s of owners-query load, so past the agent's 5-minute type-pool clear. Retained sizes are MAT dominator-tree sizes, one dump per jar:

    Jar Idle trace buffer PayloadDispatcherImpl retained
    master 10 chunks, 2.6 MB, 0 messages 2,569 KB
    master + Serialize client stats payloads into a chunked buffer #13097 24 chunks, 5.0 MB, 0 messages 4,869 KB
    master + Serialize client stats payloads into a chunked buffer #13097 + this PR (+ Size client stats cardinality tables to the values seen, not the budget #13508) 1 chunk, 5 KB, 0 messages 12 KB

    Without the trim, the idle buffer holds whatever the last payload before going idle needed: 2.6 MB and 5.0 MB here, and 3.4 MB in an earlier dump. That is why the two "before" rows differ. With the trim, it drains to the single 4 KiB first chunk every time.

🤖 Generated with Claude Code

@dougqh dougqh added type: feature Enhancements and improvements comp: core Tracer core tag: performance Performance related changes tag: ai generated Largely based on code generated by an AI or LLM labels Nov 17, 2026
@dougqh
dougqh marked this pull request as ready for review November 17, 2026 20:51
@dougqh
dougqh requested a review from a team as a code owner November 17, 2026 20:51
@dougqh
dougqh requested review from jandro996 and removed request for a team November 17, 2026 20:51
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T20:53:01.969082Z b0026c8 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-official

datadog-official Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 59.58% (+0.01%)

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

@datadog-official datadog-official Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

Source inspection supports that idle trimming preserves completed and unfinished messages, and payload handoff restarts the two-second grace period. No actionable regressions were identified.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit b0026c8

@dd-octo-sts

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

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.95 s 14.79 s [+0.2%; +1.9%] (maybe worse)
startup:insecure-bank:tracing:Agent 13.66 s 13.79 s [-1.6%; -0.2%] (maybe better)
startup:petclinic:appsec:Agent 17.33 s 17.27 s [-0.6%; +1.3%] (no difference)
startup:petclinic:iast:Agent 17.05 s 17.21 s [-1.9%; -0.1%] (maybe better)
startup:petclinic:profiling:Agent 16.74 s 17.01 s [-2.8%; -0.4%] (maybe better)
startup:petclinic:sca:Agent 17.27 s 17.21 s [-0.4%; +1.1%] (no difference)
startup:petclinic:tracing:Agent 16.34 s 16.41 s [-1.2%; +0.4%] (no difference)

Commit: d7753806 · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

@pr-commenter

pr-commenter Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Kafka / producer-benchmark

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
git_branch dougqh/chunked-dsm-stats dougqh/chunked-idle-trim
git_commit_date 1796073946 1796074076
git_commit_sha 2451cbb d775380
See matching parameters
Baseline Candidate
ci_job_date 1796075384 1796075384
ci_job_id 2312788391 2312788391
ci_pipeline_id 157074357 157074357
cpu_model Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
jdkVersion 11.0.31 11.0.31
jmhVersion 1.36 1.36
jvm /usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/lib/jvm/java-11-openjdk-amd64/bin/java
jvmArgs -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=15002 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=15002 -Dhttp.nonProxyHosts=localhost *.localhost
kernel_version Linux runner-zfyrx7zua-project-304-concurrent-0-33qel2du 6.8.0-1031-aws #33~22.04.1-Ubuntu SMP Thu Jun 26 14:22:30 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux Linux runner-zfyrx7zua-project-304-concurrent-0-33qel2du 6.8.0-1031-aws #33~22.04.1-Ubuntu SMP Thu Jun 26 14:22:30 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
vmName OpenJDK 64-Bit Server VM OpenJDK 64-Bit Server VM
vmVersion 11.0.31+11-post-1ubuntu1-22.04.2-Ubuntu 11.0.31+11-post-1ubuntu1-22.04.2-Ubuntu

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 3 metrics, 0 unstable metrics.

See unchanged results
scenario Δ mean throughput
scenario:not-instrumented/KafkaProduceBenchmark.benchProduce same
scenario:only-tracing-dsm-disabled-benchmarks/KafkaProduceBenchmark.benchProduce same
scenario:only-tracing-dsm-enabled-benchmarks/KafkaProduceBenchmark.benchProduce same

@pr-commenter

pr-commenter Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Kafka / consumer-benchmark

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
git_branch dougqh/chunked-dsm-stats dougqh/chunked-idle-trim
git_commit_date 1796073946 1796074076
git_commit_sha 2451cbb d775380
See matching parameters
Baseline Candidate
ci_job_date 1796075367 1796075367
ci_job_id 2312788392 2312788392
ci_pipeline_id 157074357 157074357
cpu_model Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
jdkVersion 11.0.31 11.0.31
jmhVersion 1.36 1.36
jvm /usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/lib/jvm/java-11-openjdk-amd64/bin/java
jvmArgs -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=15002 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=15002 -Dhttp.nonProxyHosts=localhost *.localhost
kernel_version Linux runner-zfyrx7zua-project-304-concurrent-0-8wn8qw2s 6.8.0-1031-aws #33~22.04.1-Ubuntu SMP Thu Jun 26 14:22:30 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux Linux runner-zfyrx7zua-project-304-concurrent-0-8wn8qw2s 6.8.0-1031-aws #33~22.04.1-Ubuntu SMP Thu Jun 26 14:22:30 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
vmName OpenJDK 64-Bit Server VM OpenJDK 64-Bit Server VM
vmVersion 11.0.31+11-post-1ubuntu1-22.04.2-Ubuntu 11.0.31+11-post-1ubuntu1-22.04.2-Ubuntu

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 3 metrics, 0 unstable metrics.

See unchanged results
scenario Δ mean throughput
scenario:not-instrumented/KafkaConsumerBenchmark.benchConsume unsure
[+2741.143op/s; +17217.372op/s] or [+0.926%; +5.817%]
scenario:only-tracing-dsm-disabled-benchmarks/KafkaConsumerBenchmark.benchConsume same
scenario:only-tracing-dsm-enabled-benchmarks/KafkaConsumerBenchmark.benchConsume same

@dougqh
dougqh requested a review from PerfectSlayer November 19, 2026 22:50
@dougqh
dougqh force-pushed the dougqh/chunked-dsm-stats branch from dcc3b8d to 6d6d2c7 Compare November 24, 2026 16:48
@dougqh
dougqh requested review from a team as code owners November 24, 2026 16:48
@dougqh
dougqh requested review from ValentinZakharov and removed request for a team November 24, 2026 16:48
@dougqh
dougqh force-pushed the dougqh/chunked-idle-trim branch from b0026c8 to 09bbaa5 Compare November 24, 2026 16:48

@datadog-official datadog-official Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

Idle trimming preserves buffers containing completed or unfinished data, and sending a payload restarts the two-second retention delay. The completed static review reported no actionable defects.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 09bbaa5

@dougqh
dougqh force-pushed the dougqh/chunked-idle-trim branch from 09bbaa5 to f8763de Compare November 24, 2026 18:07

@datadog-official datadog-official Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

The trim guard preserves completed and unfinished messages, and the dispatcher delays trimming until two seconds after the last payload. The completed static review found no actionable defects; no human PR discussion was present.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit f8763de

@dougqh
dougqh force-pushed the dougqh/chunked-idle-trim branch from f8763de to 180e327 Compare November 24, 2026 20:48

@datadog-official datadog-official Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

The idle-trim guards preserve completed and unfinished data, and payload handoffs restart the two-second delay. The completed static review identified no reportable defects.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 180e327

@dougqh
dougqh force-pushed the dougqh/chunked-idle-trim branch from 180e327 to 94c486b Compare November 24, 2026 22:06

@datadog-official datadog-official Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

A failed switch from v0.4 to v0.5 can leave several MiB of obsolete payload chunks pinned. Idle flushes cannot release them while the dispatcher has no packer.

Open Bits AI session

🤖 Bits Code Review · Commit 94c486b


private Recording batchTimer;
private RemoteMapper mapper;
private ChunkedStreamingBuffer buffer;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Release the old payload buffer before mapper rediscovery

When a writer switches from its v0.4 fallback to v0.5 after a large payload, selectMapper() clears packer but the new buffer field keeps the old chunks alive while the new mapper allocates its 2 MiB dictionary. If that allocation fails, the trace worker catches the error and idle flushes return early because packer is null. The old chunks remain pinned until a later mapper initialization succeeds, leaving less heap available for recovery. Clear buffer alongside packer before rediscovery.

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

dougqh and others added 6 commits November 30, 2026 16:27
ChunkedBuffer only releases chunks in reset(), and the trace writer only
resets when it sends a payload. A service that goes quiet after a burst
kept the burst's chunks indefinitely: a quiet-phase heap dump of
PetClinic at 128m showed the writer's buffer holding 6 chunks (1.5 MB)
with no messages in it.

ChunkedBuffer.trim() applies reset()'s retention to an idle buffer: it
does nothing while the buffer holds any data, completed or unfinished,
and otherwise halves the surplus, so repeated calls drain it to one
chunk. PayloadDispatcherImpl.flush() calls it when there is nothing to
send, so the periodic flush drains an idle writer within a few seconds
(6 -> 3 -> 2 -> 1 chunks), down to the small first chunk.

The trim sits in the dispatcher rather than in ChunkedStreamingBuffer
.flush(): MsgPackWriter.flush() never reaches the buffer when it's
empty, and its oversize-retry path already resets after an empty flush,
which would have halved twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Covers the halving sequence without writes in between, that completed
and unfinished messages are left alone, that a rewound message counts as
empty, and that trimming a fresh buffer allocates nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Trimming on every idle flush meant traffic that pauses for a second
between large payloads freed chunks only to reallocate them for the next
payload. The dispatcher now records when it last handed over a payload,
whether the buffer filled or a flush sent it, and an idle flush trims
only once that was at least 2 s ago. A quiet writer still drains to one
chunk within a few seconds; bursts with shorter gaps keep their chunks.

The delay is measured in time rather than in idle flushes so it doesn't
shrink with dd.trace.flush.interval. The dispatcher takes a TimeSource
for tests, and exposes its buffer's chunk count to them; that needs
ChunkedBuffer.allocatedChunks() to be public.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The class Javadoc said resets only happen when payloads do, which is how
the trace writer uses the buffer, not a property of ChunkedBuffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Covers that an idle flush keeps the chunks until the delay has passed,
that later idle flushes drain them to one, and that a newer payload
restarts the delay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dougqh
dougqh force-pushed the dougqh/chunked-idle-trim branch from 94c486b to d775380 Compare November 30, 2026 21:29

@datadog-official datadog-official Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

A trace payload send that takes two seconds can use up the idle delay while it still holds the buffer. The next empty flush can release chunks just before a new burst needs them.

Open Bits AI session

🤖 Bits Code Review · Commit d775380

// or when the packer is flushed at a heartbeat
if (messageCount > 0) {
payloadStarted = false;
lastPayloadTicks = timeSource.getNanoTicks();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Start the idle trim delay after the synchronous send

The dispatcher starts the two-second idle timer before calling sendSerializedTraces(), and the buffer stays in use until the synchronous send returns. If the send takes at least two seconds, the next empty flush can release chunks almost immediately. A burst that arrives just after that flush can have to reallocate chunks even though the buffer was available for reuse for less than two seconds. Start the delay when the send finishes so the buffer gets the full reuse window.

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

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

comp: core Tracer core tag: ai generated Largely based on code generated by an AI or LLM tag: performance Performance related changes type: feature Enhancements and improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant