diff --git a/examples/kprobe_percpu/main.go b/examples/kprobe_percpu/main.go index c4123841a..a2c3ca72c 100644 --- a/examples/kprobe_percpu/main.go +++ b/examples/kprobe_percpu/main.go @@ -8,6 +8,7 @@ import ( "log" "time" + "github.com/cilium/ebpf" "github.com/cilium/ebpf/link" "github.com/cilium/ebpf/rlimit" ) @@ -50,14 +51,27 @@ func main() { log.Println("Waiting for events..") + batch := true for range ticker.C { var all_cpu_value []uint64 - if err := objs.KprobeMap.Lookup(mapKey, &all_cpu_value); err != nil { - log.Fatalf("reading map: %v", err) + if batch { + var nextKeyOut uint32 + lookupKeys := make([]uint32, 2) + lookupValues := make([][]uint64, 2) + count, err := objs.KprobeMap.BatchLookup(nil, &nextKeyOut, lookupKeys, lookupValues, &ebpf.BatchOptions{}) + if count != 1 { + log.Fatalf("batch reading map: %v", err) + } + all_cpu_value = lookupValues[0] + } else { + if err := objs.KprobeMap.Lookup(mapKey, &all_cpu_value); err != nil { + log.Fatalf("reading map: %v", err) + } } for cpuid, cpuvalue := range all_cpu_value { log.Printf("%s called %d times on CPU%v\n", fn, cpuvalue, cpuid) } + batch = !batch log.Printf("\n") } }