Skip to content

Commit 2f6593d

Browse files
authored
Merge pull request #26 from egmc/fix-iteration-issue
fix iteration problem(add limit for each iteration
2 parents 69a2a89 + c571136 commit 2f6593d

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

bpf/php.bpf.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ int BPF_USDT(compile_file_return, char *arg0, char *arg1)
2929
{
3030
u64 ts = bpf_ktime_get_ns();
3131

32-
debug_printk("compile file return: %s, %s\n", arg0, arg1);
32+
char comm[16];
33+
bpf_get_current_comm(&comm, sizeof(comm));
34+
debug_printk("compile file return: comm=%s, %s, %s\n", comm, arg0, arg1);
3335

3436

3537
bpf_probe_read_user_str(&filename, sizeof(filename), arg0);

main.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
_ "embed"
77
"encoding/binary"
88
"encoding/json"
9+
"errors"
910
"fmt"
1011
"log/slog"
1112
"net/http"
@@ -534,16 +535,31 @@ func displayMapContents(ctx context.Context, bpfMap *bpf.BPFMap) {
534535
fmt.Printf("%-60s %s\n", "Filename", "Count")
535536
fmt.Println("-----------------------------------------------------------")
536537

538+
// Cap iteration well below the BPF map's max_entries (see bpf/php.bpf.c).
539+
// Under heavy concurrent inserts, bpf_map_get_next_key on an LRU hash
540+
// can keep returning newly inserted keys and never terminate.
541+
// Remaining entries are picked up on the next tick.
542+
const maxIter = 10000
543+
537544
iter := bpfMap.Iterator()
538545
count := 0
546+
iterated := 0
539547
var keysToDelete [][]byte
540548

541549
for iter.Next() {
550+
if iterated >= maxIter {
551+
slog.Warn("Reached BPF map iteration cap; remaining entries will be read on next tick", "cap", maxIter)
552+
break
553+
}
554+
iterated++
542555
keyBytes := iter.Key()
543556

544557
v, err := bpfMap.GetValue(unsafe.Pointer(&keyBytes[0]))
545558
if err != nil {
546-
slog.Warn("Failed to get value from BPF map", "error", err)
559+
// ENOENT is expected under LRU eviction between Next() and GetValue().
560+
if !errors.Is(err, syscall.ENOENT) {
561+
slog.Warn("Failed to get value from BPF map", "error", err)
562+
}
547563
continue
548564
}
549565

0 commit comments

Comments
 (0)