Fix num cpu (#1561)

* add a map of profilers to CPUids

`runtime.NumCPU()` returns the number of CPUs that the process can run
on. This number does not necessarily correlate to CPU ids if the
affinity mask of the process is set.

This change maintains the current behavior as default, but also allows
the user to specify a range of CPUids to use instead.

The CPU id is stored as the value of a map keyed on the profiler
object's address.

Signed-off-by: Joe Damato <jdamato@fastly.com>
Signed-off-by: Daniel Hodges <hodges.daniel.scott@gmail.com>
Signed-off-by: Daniel Hodges <hodges@uber.com>

Co-authored-by: jdamato-fsly <55214354+jdamato-fsly@users.noreply.github.com>
This commit is contained in:
Daniel Hodges 2020-02-20 05:36:33 -05:00 committed by GitHub
commit ec62141388
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 215 additions and 36 deletions

View file

@ -54,3 +54,76 @@ func TestPerfCollector(t *testing.T) {
t.Fatal(err)
}
}
func TestPerfCPUFlagToCPUs(t *testing.T) {
tests := []struct {
name string
flag string
exCpus []int
errStr string
}{
{
name: "valid single cpu",
flag: "1",
exCpus: []int{1},
},
{
name: "valid range cpus",
flag: "1-5",
exCpus: []int{1, 2, 3, 4, 5},
},
{
name: "valid double digit",
flag: "10",
exCpus: []int{10},
},
{
name: "valid double digit range",
flag: "10-12",
exCpus: []int{10, 11, 12},
},
{
name: "valid double digit stride",
flag: "10-20:5",
exCpus: []int{10, 15, 20},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cpus, err := perfCPUFlagToCPUs(test.flag)
if test.errStr != "" {
if err != nil {
t.Fatal("expected error to not be nil")
}
if test.errStr != err.Error() {
t.Fatalf(
"expected error %q, got %q",
test.errStr,
err.Error(),
)
}
return
}
if err != nil {
t.Fatal(err)
}
if len(cpus) != len(test.exCpus) {
t.Fatalf(
"expected cpus %v, got %v",
test.exCpus,
cpus,
)
}
for i := range cpus {
if test.exCpus[i] != cpus[i] {
t.Fatalf(
"expected cpus %v, got %v",
test.exCpus,
cpus,
)
}
}
})
}
}