Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move PossibleCPUs to a public API #1219

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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