diff --git a/internal/cpu.go b/cpu.go similarity index 78% rename from internal/cpu.go rename to cpu.go index 9e908b610..466bf9e1b 100644 --- a/internal/cpu.go +++ b/cpu.go @@ -1,17 +1,29 @@ -package internal +package ebpf import ( "fmt" "os" "strings" + + "github.com/cilium/ebpf/internal" ) // PossibleCPUs returns the max number of CPUs a system may possibly have // Logical CPU numbers must be of the form 0-n -var PossibleCPUs = Memoize(func() (int, error) { +var PossibleCPUs = internal.Memoize(func() (int, error) { return parseCPUsFromFile("/sys/devices/system/cpu/possible") }) +// MustPossibleCPUs is a helper that wraps a call to PossibleCPUs and panics if +// the error is non-nil. +func MustPossibleCPUs() int { + cpus, err := PossibleCPUs() + if err != nil { + panic(err) + } + return cpus +} + func parseCPUsFromFile(path string) (int, error) { spec, err := os.ReadFile(path) if err != nil { diff --git a/internal/cpu_test.go b/cpu_test.go similarity index 96% rename from internal/cpu_test.go rename to cpu_test.go index 7ecee74ba..fa5cdcf11 100644 --- a/internal/cpu_test.go +++ b/cpu_test.go @@ -1,4 +1,4 @@ -package internal +package ebpf import ( "testing" diff --git a/map.go b/map.go index ce945ace0..204bf715c 100644 --- a/map.go +++ b/map.go @@ -133,7 +133,7 @@ func (spec *MapSpec) fixupMagicFields() (*MapSpec, error) { spec.KeySize = 4 spec.ValueSize = 4 - n, err := internal.PossibleCPUs() + n, err := PossibleCPUs() if err != nil { return nil, fmt.Errorf("fixup perf event array: %w", err) } @@ -515,7 +515,7 @@ func newMap(fd *sys.FD, name string, typ MapType, keySize, valueSize, maxEntries return m, nil } - possibleCPUs, err := internal.PossibleCPUs() + possibleCPUs, err := PossibleCPUs() if err != nil { return nil, err } diff --git a/map_test.go b/map_test.go index 7bd7194c2..9f3cd860d 100644 --- a/map_test.go +++ b/map_test.go @@ -1314,7 +1314,7 @@ func TestIterateMapInMap(t *testing.T) { func TestPerCPUMarshaling(t *testing.T) { for _, typ := range []MapType{PerCPUHash, PerCPUArray, LRUCPUHash} { t.Run(typ.String(), func(t *testing.T) { - numCPU, err := internal.PossibleCPUs() + numCPU, err := PossibleCPUs() if err != nil { t.Fatal(err) } @@ -1372,7 +1372,7 @@ type bpfCgroupStorageKey struct { } func TestCgroupPerCPUStorageMarshaling(t *testing.T) { - numCPU, err := internal.PossibleCPUs() + numCPU, err := PossibleCPUs() if err != nil { t.Fatal(err) } diff --git a/marshalers.go b/marshalers.go index e89a12f0f..6c10e83d8 100644 --- a/marshalers.go +++ b/marshalers.go @@ -53,7 +53,7 @@ func marshalPerCPUValue(slice any, elemLength int) (sys.Pointer, error) { return sys.Pointer{}, errors.New("per-CPU value requires slice") } - possibleCPUs, err := internal.PossibleCPUs() + possibleCPUs, err := PossibleCPUs() if err != nil { return sys.Pointer{}, err } @@ -91,7 +91,7 @@ func unmarshalPerCPUValue(slicePtr any, elemLength int, buf []byte) error { return fmt.Errorf("per-cpu value requires pointer to slice") } - possibleCPUs, err := internal.PossibleCPUs() + possibleCPUs, err := PossibleCPUs() if err != nil { return err }