Skip to content

Commit

Permalink
BatchPerCPU: add an example
Browse files Browse the repository at this point in the history
Signed-off-by: Alun Evans <[email protected]>
  • Loading branch information
alxn committed Nov 1, 2023
1 parent c0acc35 commit c59e653
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions examples/kprobe_percpu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"time"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"
)
Expand Down Expand Up @@ -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")
}
}

0 comments on commit c59e653

Please sign in to comment.