diff --git a/libbpfgo.go b/libbpfgo.go index 9936a121..4494c4e4 100644 --- a/libbpfgo.go +++ b/libbpfgo.go @@ -640,7 +640,6 @@ func (b *BPFMap) GetValue(key unsafe.Pointer) ([]byte, error) { func (b *BPFMap) GetValue2(key unsafe.Pointer, value *[]byte) error { valuePtr := unsafe.Pointer(&(*value)[0]) - fmt.Println("fd: ", b.fd) errC := C.bpf_map_lookup_elem(b.fd, key, valuePtr) if errC != 0 { return fmt.Errorf("failed to lookup value %v in map %s: %w", key, b.name, syscall.Errno(-errC)) diff --git a/selftest/percpu/main.bpf.c b/selftest/percpu/main.bpf.c index d4c79ff7..8a35d902 100644 --- a/selftest/percpu/main.bpf.c +++ b/selftest/percpu/main.bpf.c @@ -11,7 +11,7 @@ struct { __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); __uint(max_entries, 1); - __type(key, __u64); + __type(key, __u32); __type(value, __u64); } percpu_hash SEC(".maps"); diff --git a/selftest/percpu/main.go b/selftest/percpu/main.go index 7f652ff6..79602ace 100644 --- a/selftest/percpu/main.go +++ b/selftest/percpu/main.go @@ -3,6 +3,7 @@ package main import "C" import ( + "encoding/binary" "fmt" "os" "runtime" @@ -28,20 +29,12 @@ func main() { os.Exit(-1) } - bpfModule.ListProgramNames() - prog, err := bpfModule.GetProgram("mmap_fentry") if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(-1) } - lostEventCounterMap, err := bpfModule.GetMap("percpu_hash") - if err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(-1) - } - link, err := prog.AttachGeneric() if err != nil { fmt.Fprintln(os.Stderr, err) @@ -58,11 +51,7 @@ func main() { } }() - // all values regardless of size are rounded up to 8 for struct - // padding in PERCPU maps - valueSize := 8 * runtime.NumCPU() - fmt.Println(valueSize) - err = lostEventCounterMap.SetValueSize(uint32(valueSize)) + lostEventCounterMap, err := bpfModule.GetMap("percpu_hash") if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(-1) @@ -70,18 +59,16 @@ func main() { time.Sleep(time.Second * 2) key := 0 - val, err := lostEventCounterMap.GetValue(unsafe.Pointer(&key)) + values := make([]byte, 8*runtime.NumCPU()) + err = lostEventCounterMap.GetValue2(unsafe.Pointer(&key), &values) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(-1) } last := 0 - fmt.Println("len", len(val)) for i := 0; i < runtime.NumCPU(); i++ { - fmt.Println((val[last : last+8])) + fmt.Printf("CPU %d: %d\n", i, binary.LittleEndian.Uint32(values[last:last+8])) last += 8 } - - time.Sleep(time.Minute) }