Skip to content

[NO-TICKET] Fix over-counting of the first heap/allocation sample at profiler startup#5881

Merged
ivoanjo merged 1 commit into
masterfrom
ivoanjo/fix-overweight-first-samples
Jun 10, 2026
Merged

[NO-TICKET] Fix over-counting of the first heap/allocation sample at profiler startup#5881
ivoanjo merged 1 commit into
masterfrom
ivoanjo/fix-overweight-first-samples

Conversation

@ivoanjo

@ivoanjo ivoanjo commented Jun 9, 2026

Copy link
Copy Markdown
Member

What does this PR do?

This PR lowers the allocation profiler's base sampling interval (BASE_SAMPLING_INTERVAL in the DiscreteDynamicSampler) from 50 to 1, so we start out sampling every allocation until the sampler has measured enough to back off on its own.

The sampler primes itself on every reset so that the first allocation is always sampled. But the same events_since_last_sample counter that drives that decision is also reused as the sample's weight -- how many allocations the sample stands in for. With a base interval of 50, that first post-reset sample was therefore weighted as 50, even though only a single allocation had actually happened, overshooting the real count by ~49 phantom allocations. (Resets happen whenever the profiler starts or restarts)

Starting at an interval of 1 makes the priming offset 0, so the first sample -- and every sample until the first readjustment -- is weighted as 1, exactly the allocations it represents. The overshoot is gone.

Motivation:

This improves accuracy of allocation and heap profiles from the very first sample, including on short-lived and forking processes.

This is expected to address the issue we saw in
https://github.com/DataDog/prof-correctness/actions/runs/27137527347 where a big object, combined with the high original weight can throw off short-running tests.

Change log entry

Yes. Fix over-counting of the first allocation sample at profiler startup

Additional Notes:

We actually started with 1 back when the dynamic allocation sampling feature was still in development and then raised it to 50 in commit 4a18b4d, "Improve tests and initial sampling state" (#3395) as a conservative initial guess -- at the time, the only thing bounding the initial "blind" window was a 1-second adjustment timer.

What makes 1 safe now is the sample-count preemptive readjustment (ADJUSTMENT_WINDOW_SAMPLES), which was added afterwards in #3434. With it, even at interval 1 the initial "blind" window is bounded to at most 100 samples before we readjust based on observed overhead, so sampling everything at startup can no longer "run away" regardless of load.

The impact is low in both directions:

  • For typical long-running processes the old over-count was a one-time +49 against a background of millions of allocations, i.e. negligible; the real win is for short or heavily-forking workloads.
  • The only downside of sampling everything at startup is a slightly noisier first rate estimate (the first readjustment runs over a shorter window). It errs on the safe side though: a fast startup produces a high estimated rate and therefore a larger interval, never an overhead blow-up.

How to test the change?

Added two unit tests to discrete_dynamic_sampler_spec.rb: one pinning the new starting interval, and one that drives the startup window to show we sample every event up to the 100-sample cap, readjust exactly there, and then back off. The existing sampler and allocation specs stay green.

…ler startup

**What does this PR do?**

This PR lowers the allocation profiler's base sampling interval
(`BASE_SAMPLING_INTERVAL` in the `DiscreteDynamicSampler`) from 50 to 1,
so we start out sampling every allocation until the sampler has measured
enough to back off on its own.

The sampler primes itself on every reset so that the first allocation is
always sampled. But the same `events_since_last_sample` counter that
drives that decision is also reused as the sample's *weight* -- how many
allocations the sample stands in for. With a base interval of 50, that
first post-reset sample was therefore weighted as 50, even though only a
single allocation had actually happened, overshooting the real count by
~49 phantom allocations. (Resets happen whenever the profiler starts
or restarts)

Starting at an interval of 1 makes the priming offset 0, so the first
sample -- and every sample until the first readjustment -- is weighted
as 1, exactly the allocations it represents. The overshoot is gone.

**Motivation:**

This improves accuracy of allocation and heap profiles from the very first
sample, including on short-lived and forking processes.

This is expected to address the issue we saw in
https://github.com/DataDog/prof-correctness/actions/runs/27137527347
where a big object, combined with the high original weight can throw off
short-running tests.

**Change log entry**

Yes. Fix over-counting of the first allocation sample at profiler startup

**Additional Notes:**

We actually started with 1 back when the dynamic
allocation sampling feature was still in development and then raised it
to 50 in commit 4a18b4d,
"Improve tests and initial sampling state"
(#3395) as a conservative
initial guess -- at the time, the only thing bounding the initial
"blind" window was a 1-second adjustment timer.

What makes 1 safe now is the sample-count preemptive readjustment
(`ADJUSTMENT_WINDOW_SAMPLES`), which was added afterwards in
#3434. With it,
even at interval 1 the initial "blind" window is bounded to at most 100
samples before we readjust based on observed overhead, so sampling
everything at startup can no longer "run away" regardless of load.

The impact is low in both directions:

- For typical long-running processes the old over-count was a one-time
  +49 against a background of millions of allocations, i.e. negligible;
  the real win is for short or heavily-forking workloads.
- The only downside of sampling everything at startup is a slightly
  noisier first rate estimate (the first readjustment runs over a
  shorter window). It errs on the safe side though: a fast startup
  produces a high estimated rate and therefore a larger interval, never
  an overhead blow-up.

**How to test the change?**

Added two unit tests to `discrete_dynamic_sampler_spec.rb`: one pinning
the new starting interval, and one that drives the startup window to
show we sample every event up to the 100-sample cap, readjust exactly
there, and then back off. The existing sampler and allocation specs
stay green.
@ivoanjo
ivoanjo requested review from a team as code owners June 9, 2026 17:25
@dd-octo-sts dd-octo-sts Bot added the profiling Involves Datadog profiling label Jun 9, 2026
@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Jun 9, 2026

Copy link
Copy Markdown

Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 90.05% (-0.03%)

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

@pr-commenter

pr-commenter Bot commented Jun 9, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-06-09 17:49:38

Comparing candidate commit fc6b7b4 in PR branch ivoanjo/fix-overweight-first-samples with baseline commit e014f83 in branch master.

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

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 ----------------------------------'

@ivoanjo
ivoanjo requested a review from eregon June 10, 2026 10:47
@ivoanjo
ivoanjo merged commit e1e4a07 into master Jun 10, 2026
874 of 876 checks passed
@ivoanjo
ivoanjo deleted the ivoanjo/fix-overweight-first-samples branch June 10, 2026 15:41
@ivoanjo ivoanjo added this to the 2.36.0 milestone Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

profiling Involves Datadog profiling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants