Skip to content

Commit

Permalink
map: automatically set CPUMap MaxEntries based on possible CPUs
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
learnitall authored and ti-mo committed Dec 18, 2024
1 parent e8b05c5 commit 97cfce5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ func (spec *MapSpec) fixupMagicFields() (*MapSpec, error) {
// behaviour in the past.
spec.MaxEntries = n
}

case CPUMap:
n, err := PossibleCPU()
if err != nil {
return nil, fmt.Errorf("fixup cpu map: %w", err)
}

if n := uint32(n); spec.MaxEntries == 0 || spec.MaxEntries > n {
// Perform clamping similar to PerfEventArray.
spec.MaxEntries = n
}
}

return spec, nil
Expand Down
9 changes: 9 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,15 @@ func TestPerfEventArray(t *testing.T) {
}
}

func TestCPUMap(t *testing.T) {
testutils.SkipOnOldKernel(t, "4.15", "cpu map")

m, err := NewMap(&MapSpec{Type: CPUMap, KeySize: 4, ValueSize: 4})
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(m.MaxEntries(), uint32(MustPossibleCPU())))
m.Close()
}

func createMapInMap(t *testing.T, typ MapType) *Map {
t.Helper()

Expand Down

0 comments on commit 97cfce5

Please sign in to comment.