From 9b8b16570aa8a3e44a8e86504265ad8e90514cc7 Mon Sep 17 00:00:00 2001 From: Nicolae Mogoreanu Date: Tue, 28 Oct 2025 10:54:13 -0700 Subject: [PATCH 1/2] Delete the element from the `starts` map after it has been used. I think it has been an omission, as opposed to an optimization, in the original change that introduced this. The python version of the `funclatency` has the delete after the lookup. Before the change: ./funclatency nvme_setup_cmd Tracing nvme_setup_cmd. Hit Ctrl-C to exit ^C nsec : count distribution 0 -> 1 : 0 | | 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 0 | | 16 -> 31 : 0 | | 32 -> 63 : 0 | | 64 -> 127 : 0 | | 128 -> 255 : 0 | | 256 -> 511 : 0 | | 512 -> 1023 : 243 |****************************************| 1024 -> 2047 : 43 |******* | 2048 -> 4095 : 59 |********* | 4096 -> 8191 : 26 |**** | After the change: ./funclatency nvme_setup_cmd Tracing nvme_setup_cmd. Hit Ctrl-C to exit ^C nsec : count distribution 0 -> 1 : 0 | | 2 -> 3 : 0 | | 4 -> 7 : 0 | | 8 -> 15 : 0 | | 16 -> 31 : 0 | | 32 -> 63 : 0 | | 64 -> 127 : 0 | | 128 -> 255 : 0 | | 256 -> 511 : 4 | | 512 -> 1023 : 624 |****************************************| 1024 -> 2047 : 39 |** | 2048 -> 4095 : 98 |****** | 4096 -> 8191 : 71 |**** | 8192 -> 16383 : 1 | | --- libbpf-tools/funclatency.bpf.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libbpf-tools/funclatency.bpf.c b/libbpf-tools/funclatency.bpf.c index 97804147ca93..66f936a6a81b 100644 --- a/libbpf-tools/funclatency.bpf.c +++ b/libbpf-tools/funclatency.bpf.c @@ -72,6 +72,7 @@ static void exit(void) start = bpf_map_lookup_elem(&starts, &pid); if (!start) return; + bpf_map_delete_elem(&starts, &pid); delta = nsec - *start; From 2d3f7212616506a5c481803f4fcdffae4317d536 Mon Sep 17 00:00:00 2001 From: Nicolae Mogoreanu Date: Thu, 30 Oct 2025 08:48:23 -0700 Subject: [PATCH 2/2] funclatency.bpf.c Move bpf_map_delete_elem to the end of the function. --- libbpf-tools/funclatency.bpf.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libbpf-tools/funclatency.bpf.c b/libbpf-tools/funclatency.bpf.c index 66f936a6a81b..6d6559adfe9d 100644 --- a/libbpf-tools/funclatency.bpf.c +++ b/libbpf-tools/funclatency.bpf.c @@ -72,7 +72,6 @@ static void exit(void) start = bpf_map_lookup_elem(&starts, &pid); if (!start) return; - bpf_map_delete_elem(&starts, &pid); delta = nsec - *start; @@ -89,6 +88,8 @@ static void exit(void) if (slot >= MAX_SLOTS) slot = MAX_SLOTS - 1; __sync_fetch_and_add(&hist[slot], 1); + + bpf_map_delete_elem(&starts, &pid); } SEC("fexit/dummy_fexit")