Skip to content

Commit

Permalink
Update selftest
Browse files Browse the repository at this point in the history
Signed-off-by: grantseltzer <grantseltzer@gmail.com>
  • Loading branch information
grantseltzer committed Apr 27, 2022
1 parent a27f64b commit 3a5a471
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 20 deletions.
1 change: 0 additions & 1 deletion libbpfgo.go
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion selftest/percpu/main.bpf.c
Expand Up @@ -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");

Expand Down
23 changes: 5 additions & 18 deletions selftest/percpu/main.go
Expand Up @@ -3,6 +3,7 @@ package main
import "C"

import (
"encoding/binary"
"fmt"
"os"
"runtime"
Expand All @@ -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)
Expand All @@ -58,30 +51,24 @@ 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)
}

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)
}

0 comments on commit 3a5a471

Please sign in to comment.