Skip to content

Commit

Permalink
ebpf: export PossibleCPU
Browse files Browse the repository at this point in the history
Make PossibleCPU available to users of the package. This allows pre-allocating per-CPU slices.

Signed-off-by: Alun Evans <alun@badgerous.net>
  • Loading branch information
alxn authored Nov 15, 2023
1 parent 0acd95c commit e1bf3cc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
24 changes: 20 additions & 4 deletions internal/cpu.go → cpu.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
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 possibleCPU = internal.Memoize(func() (int, error) {
return parseCPUsFromFile("/sys/devices/system/cpu/possible")
})

// PossibleCPU returns the max number of CPUs a system may possibly have
// Logical CPU numbers must be of the form 0-n
func PossibleCPU() (int, error) {
return possibleCPU()
}

// MustPossibleCPU is a helper that wraps a call to PossibleCPU and panics if
// the error is non-nil.
func MustPossibleCPU() int {
cpus, err := PossibleCPU()
if err != nil {
panic(err)
}
return cpus
}

func parseCPUsFromFile(path string) (int, error) {
spec, err := os.ReadFile(path)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/cpu_test.go → cpu_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package ebpf

import (
"testing"
Expand Down
4 changes: 2 additions & 2 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (spec *MapSpec) fixupMagicFields() (*MapSpec, error) {
spec.KeySize = 4
spec.ValueSize = 4

n, err := internal.PossibleCPUs()
n, err := PossibleCPU()
if err != nil {
return nil, fmt.Errorf("fixup perf event array: %w", err)
}
Expand Down Expand Up @@ -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 := PossibleCPU()
if err != nil {
return nil, err
}
Expand Down
10 changes: 2 additions & 8 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1314,10 +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()
if err != nil {
t.Fatal(err)
}
numCPU := MustPossibleCPU()
if numCPU < 2 {
t.Skip("Test requires at least two CPUs")
}
Expand Down Expand Up @@ -1372,10 +1369,7 @@ type bpfCgroupStorageKey struct {
}

func TestCgroupPerCPUStorageMarshaling(t *testing.T) {
numCPU, err := internal.PossibleCPUs()
if err != nil {
t.Fatal(err)
}
numCPU := MustPossibleCPU()
if numCPU < 2 {
t.Skip("Test requires at least two CPUs")
}
Expand Down
4 changes: 2 additions & 2 deletions marshalers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := PossibleCPU()
if err != nil {
return sys.Pointer{}, err
}
Expand Down Expand Up @@ -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 := PossibleCPU()
if err != nil {
return err
}
Expand Down

0 comments on commit e1bf3cc

Please sign in to comment.