From c57113647a504623d3c4116ffb36284a6fa9ec33 Mon Sep 17 00:00:00 2001 From: egmc Date: Tue, 14 Apr 2026 08:11:57 +0900 Subject: [PATCH] fix iteration problem(add limit for each iteration - fix #25 --- bpf/php.bpf.c | 4 +++- main.go | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/bpf/php.bpf.c b/bpf/php.bpf.c index 9f05ab0..bfd1073 100644 --- a/bpf/php.bpf.c +++ b/bpf/php.bpf.c @@ -29,7 +29,9 @@ int BPF_USDT(compile_file_return, char *arg0, char *arg1) { u64 ts = bpf_ktime_get_ns(); - debug_printk("compile file return: %s, %s\n", arg0, arg1); + char comm[16]; + bpf_get_current_comm(&comm, sizeof(comm)); + debug_printk("compile file return: comm=%s, %s, %s\n", comm, arg0, arg1); bpf_probe_read_user_str(&filename, sizeof(filename), arg0); diff --git a/main.go b/main.go index 3049681..a245fe8 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( _ "embed" "encoding/binary" "encoding/json" + "errors" "fmt" "log/slog" "net/http" @@ -534,16 +535,31 @@ func displayMapContents(ctx context.Context, bpfMap *bpf.BPFMap) { fmt.Printf("%-60s %s\n", "Filename", "Count") fmt.Println("-----------------------------------------------------------") + // Cap iteration well below the BPF map's max_entries (see bpf/php.bpf.c). + // Under heavy concurrent inserts, bpf_map_get_next_key on an LRU hash + // can keep returning newly inserted keys and never terminate. + // Remaining entries are picked up on the next tick. + const maxIter = 10000 + iter := bpfMap.Iterator() count := 0 + iterated := 0 var keysToDelete [][]byte for iter.Next() { + if iterated >= maxIter { + slog.Warn("Reached BPF map iteration cap; remaining entries will be read on next tick", "cap", maxIter) + break + } + iterated++ keyBytes := iter.Key() v, err := bpfMap.GetValue(unsafe.Pointer(&keyBytes[0])) if err != nil { - slog.Warn("Failed to get value from BPF map", "error", err) + // ENOENT is expected under LRU eviction between Next() and GetValue(). + if !errors.Is(err, syscall.ENOENT) { + slog.Warn("Failed to get value from BPF map", "error", err) + } continue }