Skip to content

chore(profiling): record memalloc truncation stats and walk time - #22367

Open
vlad-scherbich wants to merge 4 commits into
mainfrom
vlad/profiling-truncation-metrics-stats
Open

vlad-scherbich wants to merge 4 commits into
mainfrom
vlad/profiling-truncation-metrics-stats

Conversation

@vlad-scherbich

@vlad-scherbich vlad-scherbich commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

On top of the omitted-frame count fix (#22207), this PR records memalloc alloc/heap truncation stats (frame_truncation depth buckets, dropped frames, max depth) and memalloc_walk_ns in profile internal metadata, hands them off under the upload lock via ddup.take_frame_stats, and emits profiler telemetry frames.* (including frames.samples_truncated) plus memalloc.walk_ns after each flush.

If only this PR merges onto the fix base: customers already see true omitted counts; what newly ships is metadata + instrumentation telemetry. Stack and lock collectors stay unchanged.

Testing

Local:

  • pytest tests/profiling/collector/test_memalloc.py::test_memory_collector_reports_true_omitted_frame_count tests/profiling/test_scheduler.py::test_flush_reports_frame_stats_to_telemetry tests/profiling/test_scheduler.py::test_flush_survives_frame_stats_failure
  • Native (includes test_frame_truncation): ./ddtrace/internal/datadog/profiling/build_standalone.sh -- RelWithDebInfo stack_test

Pins: alloc/heap frame_truncation + memalloc_walk_ns > 0; gtest depth buckets; scheduler emits frames.* / memalloc.walk_ns and flush still uploads when take/report fails.

Risks

Two monotonic clock reads per memalloc sample. ddup_take_frame_stats runs under the upload lock. Stats can be taken after a failed serialize, so telemetry may cover a window that did not ship.

Additional Notes

Base: #22207. This PR: observe (metadata + telemetry) only.

dd-go companion (FPD registry): https://github.com/ddoghq/dd-go/pull/25879 — registers frames.samples / frames.samples_truncated / frames.stack_depth / frames.max_depth / memalloc.walk_ns. Intake uses frames.samples_truncated (no frames.dropped registry entry).

changelog/no-changelog — internal metadata/telemetry; customer-facing reno is on the fix PR.

https://datadoghq.atlassian.net/browse/PROF-16353

Pre-review checklist

Before submitting your PR for review, please make sure the following have
been done/checked.

  • The changes have been tested in real services in staging
  • The PR does not result in new crashes

@vlad-scherbich vlad-scherbich added the changelog/no-changelog A changelog entry is not required for this PR. label Dec 3, 2026
@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codeowners resolved as

Resolved from the full PR diff against vlad/profiling-truncation-metrics using the target branch CODEOWNERS file.
CODEOWNERS team requests not listed below are not required by the current file set.

ddtrace/internal/datadog/profiling/dd_wrapper/include/ddup_interface.hpp  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/include/profile.hpp       @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/include/profiler_state.hpp  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/include/profiler_stats.hpp  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/include/sample.hpp        @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/ddup_interface.cpp    @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/profile.cpp           @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/profiler_state.cpp    @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/profiler_stats.cpp    @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/sample.cpp            @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/src/uploader_builder.cpp  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/test/CMakeLists.txt       @DataDog/profiling-python
ddtrace/internal/datadog/profiling/dd_wrapper/test/test_frame_truncation.cpp  @DataDog/profiling-python
ddtrace/internal/datadog/profiling/ddup/_ddup.pyi                       @DataDog/profiling-python
ddtrace/internal/datadog/profiling/ddup/_ddup.pyx                       @DataDog/profiling-python
ddtrace/profiling/collector/_memalloc_tb.cpp                            @DataDog/profiling-python
ddtrace/profiling/scheduler.py                                          @DataDog/profiling-python
tests/profiling/collector/test_memalloc.py                              @DataDog/profiling-python
tests/profiling/test_scheduler.py                                       @DataDog/profiling-python

@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Dependency direction analysis

⚠️ Existing dependency direction violations

There are 23 dependency direction violations that already exist on the base branch and have not been changed by this PR.

Show existing violations (showing 5 of 23 highest severity)
ddtrace.internal.tracemethods -×-> ddtrace.trace  (internal-core -> product:tracing, score=99)
ddtrace.llmobs._integrations.base -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=97)
ddtrace.llmobs._integrations.bedrock_agents -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=97)
ddtrace.internal.span_bus -×-> ddtrace._trace.span  (internal-core -> product:tracing, score=35)
ddtrace.llmobs._llmobs -×-> ddtrace._trace.span  (product:llmobs -> product:tracing, score=33)

To see all violations, download the layers-base.json and layers-pr.json artifacts from this CI job and run:

uv run --script scripts/import-analysis/layers.py compare layers-base.json layers-pr.json

@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Pipelines  Tests

❌ Errors

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

🚦 2 Pipeline jobs failed

System Tests | integration-frameworks-system-tests / End-to-end #1 / anthropic-py@0.84.0 1 — 🔧 Needs a code fix, caused by this PR

View more details · View in GitHub Actions

System Tests | system-tests finished

View more details · View in GitHub Actions

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

Useful? React with 👍 / 👎

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

@pr-commenter

pr-commenter Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-12-04 16:23:10

Comparing candidate commit 58b63e0 in PR branch vlad/profiling-truncation-metrics-stats with baseline commit ee39e0a in branch vlad/profiling-truncation-metrics.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 574 metrics, 10 unstable metrics, 1 flaky benchmarks without significant changes.

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

Unstable benchmarks

These benchmarks have a confidence interval too wide to call a change; treat them as noise rather than signal.

scenario:coreapiscenario-context_with_data_listeners

  • unstable execution_time [-661.087ns; +802.439ns] or [-6.177%; +7.498%]

scenario:coreapiscenario-core_dispatch_1_listener

  • unstable execution_time [-18.193ns; +62.199ns] or [-2.823%; +9.650%]

scenario:coreapiscenario-core_dispatch_50_listeners

  • unstable execution_time [-2087.923ns; +1719.601ns] or [-10.379%; +8.548%]

scenario:coreapiscenario-core_dispatch_exception_listeners

  • unstable execution_time [-2467.720ns; +1169.675ns] or [-12.562%; +5.954%]

scenario:coreapiscenario-core_dispatch_listeners

  • unstable execution_time [-337.660ns; +413.996ns] or [-7.879%; +9.660%]

scenario:coreapiscenario-core_dispatch_no_args_listeners

  • unstable execution_time [-201.627ns; +256.503ns] or [-7.483%; +9.519%]

scenario:coreapiscenario-core_dispatch_with_results_1_listener

  • unstable execution_time [-23.482ns; +156.937ns] or [-1.757%; +11.745%]

scenario:coreapiscenario-core_dispatch_with_results_50_listeners

  • unstable execution_time [-4914.174ns; +4205.754ns] or [-10.192%; +8.723%]

scenario:coreapiscenario-core_dispatch_with_results_listeners

  • unstable execution_time [-865.826ns; +1042.607ns] or [-8.523%; +10.264%]

scenario:packagesupdateimporteddependencies-import_many_stdlib_cached

  • unstable execution_time [-49.766µs; +64.871µs] or [-8.447%; +11.010%]

Known flaky benchmarks

These benchmarks are marked as flaky and will not trigger a failure. Modify FLAKY_BENCHMARKS_REGEX to control which benchmarks are marked as flaky.

Known flaky benchmarks without significant changes:

  • scenario:packagespackageforrootmodulemapping-cache_off

@vlad-scherbich
vlad-scherbich force-pushed the vlad/profiling-memory-omitted-frames branch from e74939e to b927be9 Compare December 3, 2026 20:44
@vlad-scherbich
vlad-scherbich force-pushed the vlad/profiling-truncation-metrics-stats branch from 16c7f8a to a6a327d Compare December 3, 2026 20:44
@vlad-scherbich
vlad-scherbich changed the base branch from vlad/profiling-memory-omitted-frames to vlad/profiling-truncation-metrics December 3, 2026 20:44
@vlad-scherbich
vlad-scherbich requested a balanced review from Copilot December 4, 2026 12:28

Copilot AI 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.

🟡 Changes recommended

The change still emits an unregistered frames.dropped metric, and the overhead of added allocation-path clock reads has not been benchmarked.

2 open issues
What changed in this PR

Adds memory-profiler diagnostics on top of #22207’s omitted-frame count fix, without changing stack or lock collectors.

Changes:

  • Records allocation/heap truncation statistics and stack-walk time in profile metadata.
  • Hands statistics to Python under the upload lock and emits telemetry after flushes.
  • Adds native and Python coverage for statistics and reporting failures.
File Description
tests/​profiling/​test_scheduler.py Tests telemetry emission and failure handling.
tests/​profiling/​collector/​test_memalloc.py Checks truncation metadata and walk time.
ddtrace/​profiling/​scheduler.py Reports frame statistics after flushing.
ddtrace/​profiling/​collector/​_memalloc_tb.cpp Measures allocation stack-walk time.
ddtrace/​internal/​datadog/​profiling/​ddup/​_ddup.pyx Exposes native statistics to Python.
ddtrace/​internal/​datadog/​profiling/​ddup/​_ddup.pyi Declares the statistics API.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​test/​test_frame_truncation.cpp Tests counters, buckets, handoff, and interning failure.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​test/​CMakeLists.txt Registers the native test.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​src/​uploader_builder.cpp Snapshots statistics before serialization.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​src/​sample.cpp Supplies per-sample truncation statistics.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​src/​profiler_stats.cpp Aggregates and serializes statistics.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​src/​profiler_state.cpp Clears pending statistics after fork.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​src/​profile.cpp Records statistics during collection.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​src/​ddup_interface.cpp Transfers statistics under the upload lock.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​include/​sample.hpp Adds sample classification and timing state.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​include/​profiler_stats.hpp Defines counters and depth buckets.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​include/​profiler_state.hpp Stores pending upload statistics.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​include/​profile.hpp Adds per-sample statistics to collection.
ddtrace/​internal/​datadog/​profiling/​dd_wrapper/​include/​ddup_interface.hpp Declares the native statistics handoff.

Review effort: Balanced

Comment thread ddtrace/profiling/scheduler.py Resolved
Memory samples always showed "<1 frame omitted>" because the memalloc
walk stopped at DD_PROFILING_MAX_FRAMES. Keep walking (pointer reads
only, up to the existing 1024-frame cap) and report the real count.
@vlad-scherbich
vlad-scherbich force-pushed the vlad/profiling-truncation-metrics branch from b927be9 to ee39e0a Compare December 4, 2026 12:37
Record per-profile frame_truncation counters and memalloc_walk_ns for
alloc/heap samples, hand them off under the upload lock, and emit
frames.* plus memalloc.walk_ns instrumentation telemetry after each
flush. Stack and lock collectors are unchanged.
take_frame_stats already returns frames_dropped; report it as the
frames.dropped count metric alongside the other per-collector counters.
@vlad-scherbich
vlad-scherbich force-pushed the vlad/profiling-truncation-metrics-stats branch from a6a327d to 5ed0031 Compare December 4, 2026 12:39
Intake (dd-go #25879) registers frames.samples_truncated, not frames.dropped;
keep frames_dropped in profile metadata only.

Copilot AI 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.

🟢 Approval recommended

No active review issues are identified for the memory-profiler statistics and telemetry changes.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@vlad-scherbich
vlad-scherbich marked this pull request as ready for review December 4, 2026 16:03
@vlad-scherbich
vlad-scherbich requested a review from a team as a code owner December 4, 2026 16:03
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 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-12-04T16:08:17.190916Z 58b63e0 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.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 58b63e090d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@datadog-datadog-prod-us1 datadog-datadog-prod-us1 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

Frame statistics are handed off under the upload lock, and telemetry reporting failures are isolated from profile uploads. Runtime overhead and external metric registrations remain unvalidated.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 58b63e0

gh-worker-dd-mergequeue-cf854d Bot pushed a commit that referenced this pull request Dec 4, 2026
…22207)

## Description

Memory samples always showed `<1 frame omitted>` because the memalloc stack walk stopped at `DD_PROFILING_MAX_FRAMES`. It now keeps walking (pointer reads only, up to the existing 1024 walked-frame cap) and reports the real omitted-frame count on the sample label.

No stack content, default, or limit changes.

## Testing

Local: `pytest tests/profiling/collector/test_memalloc.py::test_memory_collector_reports_true_omitted_frame_count`

Pins: omitted-frame label equals measured depth − `max_nframe` (not always `1`).

## Risks

Memalloc samples walk past the frame limit with pointer reads only (≤1024 walked frames).

## Additional Notes

First PR in the max-depth stack (on `main`). Follow-ups: truncation stats (#22367), Sample reserve (#22206), `DD_PROFILING_MEMORY_MAX_FRAMES` (#22208).

https://datadoghq.atlassian.net/browse/PROF-16353

## Pre-review checklist

Before submitting your PR for review, please make sure the following have
been done/checked.

- [ ] The changes have been tested in real services in staging
- [ ] The PR does not result in new crashes

Co-authored-by: vlad.scherbich <vlad.scherbich@datadoghq.com>
Base automatically changed from vlad/profiling-truncation-metrics to main December 4, 2026 16:47
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot requested a review from a team as a code owner December 4, 2026 16:47

@emmettbutler emmettbutler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

note lgtm

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

Labels

changelog/no-changelog A changelog entry is not required for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants