Add a shared CPU budget to fix negative scaling at high core counts#229
Merged
Merged
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4067588356
ℹ️ 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".
|
🎯 Code Coverage (details) 🔗 Commit SHA: f4ea05a | Docs | Datadog PR Page | Give us feedback! |
whitemerch
previously approved these changes
Jul 1, 2026
whitemerch
previously approved these changes
Jul 1, 2026
whitemerch
approved these changes
Jul 1, 2026
whitemerch
left a comment
Contributor
There was a problem hiding this comment.
Re-verified, good to go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
A scan runs several worker pools, and they nest. The scanner fans out one goroutine per service, and inside each service the query-evaluation pool spins up its own
GOMAXPROCSworkers. There were five such pools in total (query eval, file reading, HCL body parsing, module-variable parsing, file-type detection), each independently sizing itself to roughly the core count, with no shared notion of how much CPU work was allowed to run at once.The result: in-flight CPU-bound goroutines scaled as
Nservices × GOMAXPROCS, well over 100 goroutines competing for 16 cores (on my laptop). Past a point, adding cores made scans slower, not faster (negative scaling): the pools oversubscribed the machine and the extra goroutines added scheduler churn, cache thrashing, and lock contention. AGOMAXPROCSsweep confirmed it, latency got worse going from 8 to 16 cores, and concurrent scans showed almost no throughput gain.On top of that, the five pools were inconsistent: only the file reader clamped its worker count to
[4, 64]; the module-variable pool ignored the auto-sizing helper and hardcoded4; the file-type analyzer is I/O-bound (it reads file content) but was capped atGOMAXPROCSwhile the otherwise-identical file reader fanned out to 64; and all five were hand-rolled copies of the same channel +WaitGroup+ closer boilerplate.JIRA Ticket: K9CODESEC-2731
AvailableCPUs()CPU-bound goroutines run at once regardless of how deep the pools stack.utils.ForEach, errgroup-backed), replacing the duplicated fan-out/WaitGroup/closer boilerplate.IOMinWorkers/IOMaxWorkers([4, 64]) constants inworkers.go. Fixes the analyzer being throttled toGOMAXPROCS.4in module-variable parsing and made that pass deterministic (sorted iteration order).utils.AvailableCPUs()) used everywhere instead of ad-hocGOMAXPROCSreads.GOMAXPROCSsweep.Author Checklist
QA Instruction
The change is internal (concurrency), so the observable behavior must be identical while scaling improves:
GOMAXPROCS=4,8,16(e.g.GOMAXPROCS=16 datadog-iac-scanner scan -p <repo> -o <out>). Higher core counts should be the same or faster — in particular 16 cores should no longer be slower than 8 (the previous negative-scaling regression).go test -race ./pkg/engine/... ./pkg/utils/ ./pkg/analyzer/ ./pkg/parser/terraform/modules/passes clean.Blast Radius
Datadog IaC Scanner only. The change is confined to the scanner's internal worker/concurrency layer (
pkg/utils,pkg/engine,pkg/analyzer,pkg/parser/terraform/modules,pkg/engine/provider). No public API, CLI flag, rule, or output-format change. No impact on other services or documentation.I submit this contribution under the Apache-2.0 license.