Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/tool/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func createCommandParameters(configurationFile *os.File, filesToAnalyse []string
"--max-target-bytes", "150000",
"--taint-intrafile",
//"--error-recovery",
"--max-memory", "5000",
"--max-memory", strconv.Itoa(opengrepMaxMemory()),
"-j", strconv.Itoa(opengrepJobs()),
//"-fast",
// adding pro features
Expand Down
61 changes: 58 additions & 3 deletions internal/tool/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,39 @@ const jobsEnvVar = "OPENGREP_JOBS"

const cgroupV2CPUMax = "/sys/fs/cgroup/cpu.max"

const cgroupV2MemoryMax = "/sys/fs/cgroup/memory.max"

// maxDefaultJobs caps the job count when neither the env var nor a CPU limit
// sets it. Tool pods have no CPU limit, so the CPU count is the whole node's,
// and every opengrep job holds its own files in memory. It matches the
// pod's CPU request in codacy-tools (Opengrep.scala).
const maxDefaultJobs = 4

// maxMemoryMiB is the largest heap opengrep may use. Past it, opengrep stops
// the file that crossed it and reports a file error, instead of the kernel
// killing the whole pod.
const maxMemoryMiB = 5000

// memoryLimitShare is the part of the container memory limit given to
// opengrep's heap. The rest covers what the heap limit does not count:
// each job's minor heap, the binary, GC overhead and this wrapper.
const memoryLimitShare = 0.7

// opengrepJobs returns the number of parallel jobs opengrep should use:
// the env var if set, else the cgroup CPU quota, else the number of CPUs.
// the env var if set, else the cgroup CPU quota, else the number of CPUs
// capped at maxDefaultJobs.
func opengrepJobs() int {
return jobsForQuota(cgroupV2CPUMax)
}

func jobsForQuota(path string) int {
if jobs, err := strconv.Atoi(os.Getenv(jobsEnvVar)); err == nil && jobs > 0 {
return jobs
}
if jobs, ok := cgroupJobs(cgroupV2CPUMax); ok {
if jobs, ok := cgroupJobs(path); ok {
return jobs
}
return runtime.NumCPU()
return min(runtime.NumCPU(), maxDefaultJobs)
}

// cgroupJobs parses a cgroup v2 cpu.max file ("<quota> <period>" or "max <period>").
Expand All @@ -46,3 +69,35 @@ func cgroupJobs(path string) (int, bool) {
}
return max(1, int(math.Ceil(quota/period))), true
}

// opengrepMaxMemory returns the --max-memory value in MiB: maxMemoryMiB,
// lowered to fit the container memory limit when there is one.
func opengrepMaxMemory() int {
return maxMemoryForLimit(cgroupV2MemoryMax)
}

func maxMemoryForLimit(path string) int {
limitBytes, ok := cgroupMemoryLimit(path)
if !ok {
return maxMemoryMiB
}
share := int(float64(limitBytes) * memoryLimitShare / (1024 * 1024))
return max(1, min(maxMemoryMiB, share))
}

// cgroupMemoryLimit parses a cgroup v2 memory.max file ("<bytes>" or "max").
func cgroupMemoryLimit(path string) (int64, bool) {
content, err := os.ReadFile(path)
if err != nil {
return 0, false
}
value := strings.TrimSpace(string(content))
if value == "max" {
return 0, false
}
limit, err := strconv.ParseInt(value, 10, 64)
if err != nil || limit <= 0 {
return 0, false
}
return limit, true
}
25 changes: 25 additions & 0 deletions internal/tool/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@ func TestCgroupJobs(t *testing.T) {
assert.False(t, ok)
}

func TestMaxMemoryForLimit(t *testing.T) {
write := func(content string) string {
p := filepath.Join(t.TempDir(), "memory.max")
assert.NoError(t, os.WriteFile(p, []byte(content), 0o600))
return p
}

// 7Gi, Opengrep's pod limit: capped at maxMemoryMiB
assert.Equal(t, maxMemoryMiB, maxMemoryForLimit(write("7516192768\n")))

// 4Gi: 70% of it
assert.Equal(t, 2867, maxMemoryForLimit(write("4294967296\n")))

assert.Equal(t, maxMemoryMiB, maxMemoryForLimit(write("max\n")))
assert.Equal(t, maxMemoryMiB, maxMemoryForLimit(write("bogus\n")))
assert.Equal(t, maxMemoryMiB, maxMemoryForLimit(filepath.Join(t.TempDir(), "missing")))
}

func TestOpengrepJobsDefaultIsCapped(t *testing.T) {
t.Setenv(jobsEnvVar, "")
jobs := jobsForQuota(filepath.Join(t.TempDir(), "missing"))
assert.GreaterOrEqual(t, jobs, 1)
assert.LessOrEqual(t, jobs, maxDefaultJobs)
}

func TestOpengrepJobsEnvOverride(t *testing.T) {
t.Setenv(jobsEnvVar, "3")
assert.Equal(t, 3, opengrepJobs())
Expand Down