-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
windows: trade heap for stack to build process tree for stats in line…
…ar space (#24182) In #20619 we overhauled how we were gathering stats for Windows processes. Unlike in Linux where we can ask for processes in a cgroup, on Windows we have to make a single expensive syscall to get all the processes and then build the tree ourselves. Our algorithm to do so is recursive and quadratic in both steps and space with the number of processes on the host. For busy hosts this hits the stack limit and panics the Nomad client. We already build a map of parent PID to PID, so modify this to be a map of parent PID to slice of children and then traverse that tree only from the root we care about (the executor PID). This moves the allocations to the heap but makes the stats gathering linear in steps and space required. This changeset also moves as much of this code as possible into an area not conditionally-compiled by OS, as the tagged test file was not being run in CI. Fixes: #23984
- Loading branch information
Showing
4 changed files
with
162 additions
and
147 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package procstats | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/mitchellh/go-ps" | ||
"github.com/shoenig/test/must" | ||
) | ||
|
||
type mockProcess struct { | ||
pid int | ||
ppid int | ||
} | ||
|
||
func (p *mockProcess) Pid() int { | ||
return p.pid | ||
} | ||
|
||
func (p *mockProcess) PPid() int { | ||
return p.ppid | ||
} | ||
|
||
func (p *mockProcess) Executable() string { | ||
return "" | ||
} | ||
|
||
func mockProc(pid, ppid int) *mockProcess { | ||
return &mockProcess{pid: pid, ppid: ppid} | ||
} | ||
|
||
func genMockProcs(needles, haystack int) ([]ps.Process, []ProcessID) { | ||
|
||
procs := []ps.Process{mockProc(1, 1), mockProc(42, 1)} | ||
expect := []ProcessID{42} | ||
|
||
// TODO: make this into a tree structure, not just a linear tree | ||
for i := 0; i < needles; i++ { | ||
parent := 42 + i | ||
pid := parent + 1 | ||
procs = append(procs, mockProc(pid, parent)) | ||
expect = append(expect, pid) | ||
} | ||
|
||
for i := 0; i < haystack; i++ { | ||
parent := 200 + i | ||
pid := parent + 1 | ||
procs = append(procs, mockProc(pid, parent)) | ||
} | ||
|
||
rand.Shuffle(len(procs), func(i, j int) { | ||
procs[i], procs[j] = procs[j], procs[i] | ||
}) | ||
|
||
return procs, expect | ||
} | ||
|
||
func Test_list(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
needles int | ||
haystack int | ||
expect int | ||
}{ | ||
{ | ||
name: "minimal", | ||
needles: 2, | ||
haystack: 10, | ||
expect: 16, | ||
}, | ||
{ | ||
name: "small needles small haystack", | ||
needles: 5, | ||
haystack: 200, | ||
expect: 212, | ||
}, | ||
{ | ||
name: "small needles large haystack", | ||
needles: 10, | ||
haystack: 1000, | ||
expect: 1022, | ||
}, | ||
{ | ||
name: "moderate needles giant haystack", | ||
needles: 20, | ||
haystack: 2000, | ||
expect: 2042, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
const executorPID = 42 | ||
t.Run(tc.name, func(t *testing.T) { | ||
|
||
procs, expect := genMockProcs(tc.needles, tc.haystack) | ||
lister := func() ([]ps.Process, error) { | ||
return procs, nil | ||
} | ||
|
||
result, examined := list(executorPID, lister) | ||
must.SliceContainsAll(t, expect, result.Slice(), | ||
must.Sprintf("exp: %v; got: %v", expect, result), | ||
) | ||
must.Eq(t, tc.expect, examined) | ||
}) | ||
} | ||
} |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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