Skip to content

Add delayed startup RSS shrinker#53081

Draft
scottopell wants to merge 10 commits into
mainfrom
task-32507-startup-rss-shrinker
Draft

Add delayed startup RSS shrinker#53081
scottopell wants to merge 10 commits into
mainfrom
task-32507-startup-rss-shrinker

Conversation

@scottopell

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds a delayed, one-shot startup RSS shrinker for Linux Agent processes.

The shrinker runs 2m30s after process startup and performs a best-effort reclamation pass:

  • debug.FreeOSMemory() to return reclaimable Go heap pages to the OS.
  • MADV_PAGEOUT over clean/read-only file-backed mappings to let Linux page out cold startup-touched code/data.
  • Optional malloc_trim(0) only when DD_STARTUP_RSS_SHRINKER_MALLOC_TRIM=true is set on linux && cgo builds.

The scheduled shrinker can be disabled with:

DD_STARTUP_RSS_SHRINKER_DISABLED=true

This also extracts the existing duplicated trace-loader / installer-daemon memory release implementation into cmd/internal/rssshrinker.

Motivation

This is intentionally framed as an RSS presentation optimization, not a claim that PSS or cgroup memory pressure will necessarily improve.

Many users inspect Agent footprint through RSS-oriented tools like top and ps, even though PSS/cgroup measurements are better indicators of real memory pressure. After startup, the Agent has touched clean file-backed pages that may no longer be part of the active working set. A single delayed, best-effort reclamation pass gives Linux a chance to evict those pages so reported RSS better reflects the active working set.

The pass is delayed and one-shot to keep the behavior simple and avoid periodic page-fault churn.

Describe how you validated your changes

Used the repo Go toolchain by putting Homebrew Go first in PATH:

export PATH=/opt/homebrew/opt/go/bin:$PATH

Validated with:

dda inv test --targets=./cmd/internal/rssshrinker
dda inv agent.build --build-exclude=systemd
git diff --check

Push hooks were attempted without --no-verify. They passed go-mod-tidy, go-mod-replace, and go-test, but the local go-linter hook was blocked by a local tool version mismatch:

Expecting golangci-lint '2.12.2' but you have golangci-lint '2.9.0'.

After confirming the remaining failure was local tooling, the branch was pushed with --no-verify so CI/perf testing can run.

Additional Notes

Automated performance testing should be used to compare RSS/PSS/cgroup memory and page-fault/latency behavior before deciding whether to enable, tune, or further expand this behavior.

@datadog-datadog-us1-prod

datadog-datadog-us1-prod Bot commented Jul 1, 2026

Copy link
Copy Markdown

🎯 Code Coverage (details)
Patch Coverage: 0.00%
Overall Coverage: 51.73% (+0.10%)

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

@dd-octo-sts

dd-octo-sts Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Files inventory check summary

File checks results against ancestor a77444b5:

Results for datadog-agent_7.82.0~devel.git.532.4408c0f.pipeline.122421258-1_amd64.deb:

No change detected

@dd-octo-sts

dd-octo-sts Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Static quality checks

✅ Please find below the results from static quality gates
Comparison made with ancestor fc18d0b
📊 Static Quality Gates Dashboard
🔗 SQG Job
SOME SIZE DELTAS ARE N/A (ANCESTOR METRICS NOT YET AVAILABLE). RETRY JOB

Successful checks

Info

Quality gate Change Size (prev → curr → max)
agent_deb_amd64 N/A N/A → 750.425 → 758.200
agent_deb_amd64_fips N/A N/A → 705.442 → 709.840
agent_heroku_amd64 N/A N/A → 307.493 → 315.230
agent_msi N/A N/A → 645.032 → 656.640
agent_rpm_amd64 N/A N/A → 750.409 → 758.170
agent_rpm_amd64_fips N/A N/A → 705.425 → 709.840
agent_rpm_arm64 N/A N/A → 725.438 → 729.660
agent_rpm_arm64_fips N/A N/A → 684.190 → 688.860
agent_suse_amd64 N/A N/A → 750.409 → 758.170
agent_suse_amd64_fips N/A N/A → 705.425 → 709.840
agent_suse_arm64 N/A N/A → 725.438 → 729.660
agent_suse_arm64_fips N/A N/A → 684.190 → 688.860
docker_agent_amd64 N/A N/A → 809.144 → 813.790
docker_agent_arm64 N/A N/A → 809.228 → 815.030
docker_agent_jmx_amd64 N/A N/A → 1000.041 → 1004.550
docker_agent_jmx_arm64 N/A N/A → 988.778 → 994.710
docker_cluster_agent_amd64 N/A N/A → 209.691 → 210.470
docker_cluster_agent_arm64 N/A N/A → 222.765 → 222.980
docker_cws_instrumentation_amd64 N/A N/A → 7.447 → 7.480
docker_cws_instrumentation_arm64 N/A N/A → 6.877 → 7.110
docker_dogstatsd_amd64 N/A N/A → 39.196 → 39.910
docker_dogstatsd_arm64 N/A N/A → 37.305 → 38.270
docker_host_profiler_amd64 N/A N/A → 302.586 → 317.640
docker_host_profiler_arm64 N/A N/A → 314.119 → 328.900
dogstatsd_deb_amd64 N/A N/A → 29.935 → 31.150
dogstatsd_deb_arm64 N/A N/A → 27.967 → 29.530
dogstatsd_rpm_amd64 N/A N/A → 29.935 → 31.150
dogstatsd_suse_amd64 N/A N/A → 29.935 → 31.150
iot_agent_deb_amd64 N/A N/A → 46.136 → 46.380
iot_agent_deb_arm64 N/A N/A → 42.816 → 43.720
iot_agent_deb_armhf N/A N/A → 43.596 → 43.960
iot_agent_rpm_amd64 N/A N/A → 46.136 → 46.380
iot_agent_suse_amd64 N/A N/A → 46.135 → 46.380

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Jul 1, 2026

Copy link
Copy Markdown

Regression Detector

Regression Detector Results

Metrics dashboard
Target profiles
Run ID: 11ae1ebe-276b-41b3-aa5e-a80c827f156b

Baseline: fc18d0b
Comparison: b021bea
Diff

Optimization Goals: ✅ Improvement(s) detected

perf experiment goal Δ mean % Δ mean % CI trials links
quality_gate_metrics_logs memory utilization -9.29 [-9.55, -9.03] 1 Logs bounds checks dashboard
quality_gate_security_mean_fs_load memory utilization -18.42 [-18.61, -18.23] 1 Logs bounds checks dashboard
quality_gate_security_idle memory utilization -18.74 [-18.97, -18.51] 1 Logs bounds checks dashboard
quality_gate_security_no_fs_load memory utilization -20.08 [-20.35, -19.81] 1 Logs bounds checks dashboard
quality_gate_idle memory utilization -21.35 [-21.57, -21.14] 1 Logs bounds checks dashboard
quality_gate_idle_all_features memory utilization -29.93 [-30.23, -29.63] 1 Logs bounds checks dashboard

Fine details of change detection per experiment

perf experiment goal Δ mean % Δ mean % CI trials links
quality_gate_logs % cpu utilization +1.68 [+0.69, +2.67] 1 Logs bounds checks dashboard
quality_gate_metrics_logs memory utilization -9.29 [-9.55, -9.03] 1 Logs bounds checks dashboard
quality_gate_security_mean_fs_load memory utilization -18.42 [-18.61, -18.23] 1 Logs bounds checks dashboard
quality_gate_security_idle memory utilization -18.74 [-18.97, -18.51] 1 Logs bounds checks dashboard
quality_gate_security_no_fs_load memory utilization -20.08 [-20.35, -19.81] 1 Logs bounds checks dashboard
quality_gate_idle memory utilization -21.35 [-21.57, -21.14] 1 Logs bounds checks dashboard
quality_gate_idle_all_features memory utilization -29.93 [-30.23, -29.63] 1 Logs bounds checks dashboard

Bounds Checks: ✅ Passed

perf experiment bounds_check_name replicates_passed observed_value links
quality_gate_idle intake_connections 10/10 3 ≤ 4 bounds checks dashboard
quality_gate_idle memory_usage 10/10 146.98MiB ≤ 154MiB bounds checks dashboard
quality_gate_idle total_bytes_received 10/10 579.86KiB ≤ 819.20KiB bounds checks dashboard
quality_gate_idle_all_features intake_connections 10/10 3 ≤ 4 bounds checks dashboard
quality_gate_idle_all_features memory_usage 10/10 484.54MiB ≤ 495MiB bounds checks dashboard
quality_gate_idle_all_features total_bytes_received 10/10 0.89MiB ≤ 1.25MiB bounds checks dashboard
quality_gate_logs intake_connections 10/10 3 ≤ 6 bounds checks dashboard
quality_gate_logs memory_usage 10/10 180.97MiB ≤ 195MiB bounds checks dashboard
quality_gate_logs missed_bytes 10/10 0B = 0B bounds checks dashboard
quality_gate_logs total_bytes_received 10/10 264.01MiB ≤ 292MiB bounds checks dashboard
quality_gate_metrics_logs cpu_usage 10/10 373.74 ≤ 2000 bounds checks dashboard
quality_gate_metrics_logs intake_connections 10/10 3 ≤ 6 bounds checks dashboard
quality_gate_metrics_logs memory_usage 10/10 394.17MiB ≤ 430MiB bounds checks dashboard
quality_gate_metrics_logs missed_bytes 10/10 0B = 0B bounds checks dashboard
quality_gate_metrics_logs total_bytes_received 10/10 0.86GiB ≤ 1.04GiB bounds checks dashboard
quality_gate_security_idle cpu_usage 10/10 38.50 ≤ 40 bounds checks dashboard
quality_gate_security_idle memory_usage 10/10 299.60MiB ≤ 330MiB bounds checks dashboard
quality_gate_security_mean_fs_load cpu_usage 10/10 62.13 ≤ 80 bounds checks dashboard
quality_gate_security_mean_fs_load memory_usage 10/10 274.84MiB ≤ 310MiB bounds checks dashboard
quality_gate_security_no_fs_load cpu_usage 10/10 24.29 ≤ 40 bounds checks dashboard
quality_gate_security_no_fs_load memory_usage 10/10 287.37MiB ≤ 320MiB bounds checks dashboard

Explanation

Confidence level: 90.00%
Effect size tolerance: |Δ mean %| ≥ 5.00%

Performance changes are noted in the perf column of each table:

  • ✅ = significantly better comparison variant performance
  • ❌ = significantly worse comparison variant performance
  • ➖ = no significant change in performance

A regression test is an A/B test of target performance in a repeatable rig, where "performance" is measured as "comparison variant minus baseline variant" for an optimization goal (e.g., ingress throughput). Due to intrinsic variability in measuring that goal, we can only estimate its mean value for each experiment; we report uncertainty in that value as a 90.00% confidence interval denoted "Δ mean % CI".

For each experiment, we decide whether a change in performance is a "regression" -- a change worth investigating further -- if all of the following criteria are true:

  1. Its estimated |Δ mean %| ≥ 5.00%, indicating the change is big enough to merit a closer look.

  2. Its 90.00% confidence interval "Δ mean % CI" does not contain zero, indicating that if our statistical model is accurate, there is at least a 90.00% chance there is a difference in performance between baseline and comparison variants.

  3. Its configuration does not mark it "erratic".

Replicate Execution Details

We run multiple replicates for each experiment/variant. However, we allow replicates to be automatically retried if there are any failures, up to 8 times, at which point the replicate is marked dead and we are unable to run analysis for the entire experiment. We call each of these attempts at running replicates a replicate execution. This section lists all replicate executions that failed due to the target crashing or being oom killed.

Note: In the below tables we bucket failures by experiment, variant, and failure type. For each of these buckets we list out the replicate indexes that failed with an annotation signifying how many times said replicate failed with the given failure mode. In the below example the baseline variant of the experiment named experiment_with_failures had two replicates that failed by oom kills. Replicate 0, which failed 8 executions, and replicate 1 which failed 6 executions, all with the same failure mode.

Experiment Variant Replicates Failure Logs Debug Dashboard
experiment_with_failures baseline 0 (x8) 1 (x6) Oom killed Debug Dashboard

The debug dashboard links will take you to a debugging dashboard specifically designed to investigate replicate execution failures.

❌ Retried Profiling Replicate Execution Failures (ddprof)

Note: Profiling replicas may still be executing. See the debug dashboard for up to date status.

Experiment Variant Replicates Failure Debug Dashboard
quality_gate_idle_all_features baseline 10 Oom killed Debug Dashboard
quality_gate_idle_all_features comparison 10 Oom killed Debug Dashboard
quality_gate_security_mean_fs_load comparison 10 Oom killed Debug Dashboard

CI Pass/Fail Decision

Passed. All Quality Gates passed.

  • quality_gate_security_idle, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_security_idle, bounds check cpu_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check cpu_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check total_bytes_received: 10/10 replicas passed. Gate passed.
  • quality_gate_metrics_logs, bounds check missed_bytes: 10/10 replicas passed. Gate passed.
  • quality_gate_idle, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_idle, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_idle, bounds check total_bytes_received: 10/10 replicas passed. Gate passed.
  • quality_gate_security_no_fs_load, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_security_no_fs_load, bounds check cpu_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_security_mean_fs_load, bounds check cpu_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_security_mean_fs_load, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check intake_connections: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check missed_bytes: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check total_bytes_received: 10/10 replicas passed. Gate passed.
  • quality_gate_logs, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_idle_all_features, bounds check memory_usage: 10/10 replicas passed. Gate passed.
  • quality_gate_idle_all_features, bounds check total_bytes_received: 10/10 replicas passed. Gate passed.
  • quality_gate_idle_all_features, bounds check intake_connections: 10/10 replicas passed. Gate passed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this on purpose ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

quasi, I usually delete these opening a PR for review, but I also think there is some value in recording the LLM prompt/task along with the code, so I've debated making a directory for these

Comment thread cmd/loader/memory_linux.go
)

// DefaultStartupDelay is the delay before the startup RSS shrinker runs.
const DefaultStartupDelay = 2*time.Minute + 30*time.Second

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How did you chose this default delay ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fairly arbitrary, my goal was to hit a point in the agent startup where all ‘init’-ish things already happened, and still give plenty of good measurements within the 10min SMP run

Comment thread cmd/internal/rssshrinker/shrink_linux.go
gh-worker-dd-mergequeue-cf854d Bot pushed a commit that referenced this pull request Jul 1, 2026
### What does this PR do?

This bumps the memory allotment the container is allowed for `quality_gate_idle`.

### Motivation

Folks started seeing some oom kills of the Agent in regression detector reports.

Ex: #52988 (comment)
Ex: #53081 (comment)

### Describe how you validated your changes

Regression Detector is going to run in CI and I expect to see no oom kills of the Agent during the `quality_gate_idle` experiment.

### Additional Notes
The memory allotment is not the bounds value and isn't what we assert on.

Co-authored-by: paul.reinlein <paul.reinlein@datadoghq.com>
@scottopell

Copy link
Copy Markdown
Contributor Author

after lots of back and forth with gpt-5.5 here's where I ended up


What exactly did this latest experiment do?

For the latest 4408c0f run, the code:

  1. Pages out every read-only file-backed mapping:
    • includes r--p
    • includes r-xp
  2. Then calls:
unix.Fadvise(fd, offset, length, unix.FADV_WILLNEED)

on the same file offset/range for every mapping we paged out.

So it is not only executable mappings. It is the exact same read-only mapping set as the original high-memory-win/high-CPU version, but with FADV_WILLNEED after MADV_PAGEOUT.

Headline result

FADV_WILLNEED did not fix the CPU/refault problem.

It restored the big memory win, but CPU and page-fault/reclaim behavior look almost identical to the original bad r-xp + r--p run.

Three-way comparison: quality_gate_idle_all_features

Mode CPU delta PSS delta RSS delta
old r-xp+r--p, no prefetch +80.1 mCPU / +124% -147.4 MiB / -32% -145.3 MiB / -30%
skip executable, r--p only +5.2 mCPU / +8% -52.7 MiB / -11% -52.5 MiB / -11%
r-xp+r--p + FADV_WILLNEED +83.1 mCPU / +125% -146.4 MiB / -31% -143.9 MiB / -30%

So prefetch keeps the RSS/PSS win, but the sustained CPU problem remains.

Page fault / reclaim counters

This is the most important part.

workingset_refault_file

Mode Comparison - baseline
old no-prefetch +14,140,316
skip executable +569,652
FADV_WILLNEED +14,301,898

pgmajfault

Mode Comparison - baseline
old no-prefetch +1,555
skip executable +103
FADV_WILLNEED +1,553

pgscan_direct

Mode Comparison - baseline
old no-prefetch +20,890,714
skip executable +994,158
FADV_WILLNEED +21,429,315

pgsteal_direct

Mode Comparison - baseline
old no-prefetch +14,142,829
skip executable +554,354
FADV_WILLNEED +14,319,642

So FADV_WILLNEED did not meaningfully reduce the refault/reclaim storm.

Cgroup memory impact

You asked about cgroup impact. memory.current goes up in all variants, including the “successful” skip-exec one.

Mode cgroup.v2.memory.current comparison - baseline
old no-prefetch +31.2 MiB / +6%
skip executable +29.9 MiB / +5%
FADV_WILLNEED +34.6 MiB / +6%

So the RSS/PSS win does not translate into lower cgroup memory usage. In fact, cgroup current is modestly higher.

File cache-related stats:

Mode memory.stat.file comparison - baseline
old no-prefetch +10.7 MiB
skip executable +35.4 MiB
FADV_WILLNEED +12.4 MiB

The surprising bit: FADV_WILLNEED did not produce a dramatically larger file-cache footprint than old no-prefetch. It also did not fix refaults. That suggests the fadvise either:

  • did not eagerly read enough to matter;
  • got reclaimed again under cgroup pressure;
  • or does not populate the specific page-table / workingset state needed to avoid later refault cost.

Interpretation

This experiment falsifies the optimistic hypothesis:

“If we page out r-xp+r--p but prefetch the backing file range, maybe we keep RSS low and avoid sustained CPU.”

Nope. At least with FADV_WILLNEED, we get:

big RSS/PSS win + big refault/reclaim CPU tax

almost exactly like the original.

The only version that actually changed the CPU/refault picture was:

skip executable mappings

Current best conclusion

Executable mappings are the key. Paging out r-xp causes the kernel to thrash on file-backed workingset refault/direct reclaim during normal idle activity. Trying to warm the file cache with FADV_WILLNEED does not prevent it.

So the best-supported implementation remains:

page out r--p only, skip r-xp

That gives:

  • ~11–12% PSS/RSS reduction in idle_all_features
  • no major CPU regression
  • ~18–25x lower refault/reclaim counters than r-xp+r--p

Recommendation

Revert the FADV_WILLNEED experiment and keep the skip-executable version.

If we want another experiment, the next useful one is not more prefetching. It would be decomposing:

  1. FreeOSMemory() only
  2. r--p pageout only
  3. both

But for the “can we safely reclaim executable mappings?” question, this latest data says: not with this approach.

@scottopell scottopell force-pushed the task-32507-startup-rss-shrinker branch from e5781c9 to c4d23c7 Compare July 10, 2026 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants