Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
map: use possible CPUs to size PerfEventArray
We currently use the highest possible CPU to determine the size of a PerfEventArray. This has several problems: first, the code to parse the online CPUs doesn't correctly handle ranges: "0,2-7" is treated like there is only one online CPU. This can happen when a CPU is disabled at runtime using echo 0 | sudo tee /sys/devices/system/cpu/cpu1/online We silently create a map with an incorrect size, and user code starts to fail with E2BIG. Fix this by using the number of possible CPUs as the map size. The number can only be changed by a reboot and so is safe to use. It's also simpler to use, since we don't have to deal with multiple ranges like in the online CPU case.
- Loading branch information
Showing
3 changed files
with
45 additions
and
46 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
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 |
---|---|---|
@@ -1,31 +1,32 @@ | ||
package internal | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"testing" | ||
) | ||
|
||
func TestParseCPUs(t *testing.T) { | ||
for str, result := range map[string]int{ | ||
"0-1": 2, | ||
"0": 1, | ||
"0-1": 2, | ||
"0-2\n": 3, | ||
"0": 1, | ||
} { | ||
fh, err := ioutil.TempFile("", "ebpf") | ||
n, err := parseCPUs(str) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if _, err := io.WriteString(fh, str); err != nil { | ||
t.Fatal(err) | ||
} | ||
fh.Close() | ||
|
||
n, err := parseCPUs(fh.Name()) | ||
if err != nil { | ||
t.Error("Can't parse", str, err) | ||
t.Errorf("Can't parse `%s`: %v", str, err) | ||
} else if n != result { | ||
t.Error("Parsing", str, "returns", n, "instead of", result) | ||
} | ||
} | ||
|
||
for _, str := range []string{ | ||
"0,3-4", | ||
"0-", | ||
"1,", | ||
"", | ||
} { | ||
_, err := parseCPUs(str) | ||
if err == nil { | ||
t.Error("Parsed invalid format:", str) | ||
} | ||
} | ||
} |
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