Skip to content

Commit 97cfce5

Browse files
learnitallti-mo
authored andcommitted
map: automatically set CPUMap MaxEntries based on possible CPUs
This commit automatically populates a CPUMap's MaxEntries with the amount of possible CPUs on the machine. This keeps code portable across different hosts. Signed-off-by: Ryan Drew <[email protected]>
1 parent e8b05c5 commit 97cfce5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

map.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ func (spec *MapSpec) fixupMagicFields() (*MapSpec, error) {
158158
// behaviour in the past.
159159
spec.MaxEntries = n
160160
}
161+
162+
case CPUMap:
163+
n, err := PossibleCPU()
164+
if err != nil {
165+
return nil, fmt.Errorf("fixup cpu map: %w", err)
166+
}
167+
168+
if n := uint32(n); spec.MaxEntries == 0 || spec.MaxEntries > n {
169+
// Perform clamping similar to PerfEventArray.
170+
spec.MaxEntries = n
171+
}
161172
}
162173

163174
return spec, nil

map_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,15 @@ func TestPerfEventArray(t *testing.T) {
981981
}
982982
}
983983

984+
func TestCPUMap(t *testing.T) {
985+
testutils.SkipOnOldKernel(t, "4.15", "cpu map")
986+
987+
m, err := NewMap(&MapSpec{Type: CPUMap, KeySize: 4, ValueSize: 4})
988+
qt.Assert(t, qt.IsNil(err))
989+
qt.Assert(t, qt.Equals(m.MaxEntries(), uint32(MustPossibleCPU())))
990+
m.Close()
991+
}
992+
984993
func createMapInMap(t *testing.T, typ MapType) *Map {
985994
t.Helper()
986995

0 commit comments

Comments
 (0)