Skip to content

Cap opengrep jobs and fit --max-memory to the pod limit - #50

Merged
lolgab merged 2 commits into
mainfrom
opengrep-memory-limits
Dec 3, 2026
Merged

lolgab merged 2 commits into
mainfrom
opengrep-memory-limits

Conversation

@lolgab

@lolgab lolgab commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Opengrep pods have no CPU limit (codacy-tools #1262), so cgroup cpu.max reads max and the job count falls back to runtime.NumCPU(): every core of the node. Opengrep v2.1.0 runs its jobs as OCaml 5 domains in one process, so memory grows with the job count: each domain has its own 32 MiB minor heap, outside the --max-memory heap cap, and more files are in memory at once. On a large node that pushes the pod towards its 7Gi limit, where the kernel kills the whole pod and the batch fails, instead of Opengrep failing one file.

  • Jobs: OPENGREP_JOBS, then the cgroup CPU quota, as before. The last fallback is now the CPU count capped at 4, the pod's CPU request in Opengrep.scala. Opengrep's own default caps at 16, but the wrapper always passes -j, so there was no cap at all.
  • --max-memory: 70% of the container memory limit (memory.max), at most 5000 MiB. At today's 7Gi limit the value stays 5000. If the limit is lowered (e.g. when right-sizing the 2Gi request / 7Gi limit), Opengrep still fails a single file instead of the pod being killed.

Not done on purpose: reading the CPU request from cpu.weight. runc changed its shares-to-weight conversion, so the value can't be trusted across nodes.

  • Build the image and run the multiple-tests suite (CircleCI plugins_test)
  • After deploy, 7 days: Opengrep ToolOutOfMemory count and per-pod memory peak/p95 vs before
  • After deploy, 7 days: p99 run time and timeouts (fewer jobs can be slower on idle nodes)

Tool pods have no CPU limit, so the job count fell back to the node's
core count. Cap that fallback at 4, the pod's CPU request. Derive
--max-memory from the container memory limit (70%, at most 5000 MiB).
@codacy-production

codacy-production Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 9 complexity

Metric Results
Complexity 9

View in Codacy

AI Reviewer: first review requested successfully. AI can make mistakes. Always validate suggestions.

Run reviewer

TIP This summary will be updated as you push new changes.

@lolgab
lolgab marked this pull request as ready for review December 3, 2026 09:41

@codacy-production codacy-production 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.

Pull Request Overview

✅ No blocking issues noticed

No blocking findings were identified.

Codacy also found:

  • Complexity increased in internal/tool/jobs.go and internal/tool/jobs_test.go due to cgroup parsing and memory fallback branches.

Test suggestions

  • Verify explicit OPENGREP_JOBS overrides automatic job selection.
  • Verify CPU quota parsing and fallback to a bounded CPU count when no quota exists.
  • Verify memory is capped at 5000 MiB for large limits and calculated as 70% for smaller limits.
  • Verify unlimited, malformed, or unavailable memory limits use the default.
  • Verify command parameters contain the dynamically computed --max-memory and -j values.
Prompt proposal for missing tests
Consider implementing these tests if applicable:
1. Verify command parameters contain the dynamically computed --max-memory and -j values.

TIP Add review instructions in .codacy/instructions/review.md, or an AGENTS.md, so the reviewer knows your conventions.
TIP How was this review? Give us feedback

@lolgab
lolgab enabled auto-merge (squash) December 3, 2026 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants