Skip to content

Commit

Permalink
libbpf-tools: Fix klockstat to finish with -R
Browse files Browse the repository at this point in the history
On extremely heavy lock contention, klockstat can be stuck in the loop
in print_stats() since it keeps adding new entries to the stat_map.

Instead of deleting the elements in the loop, process them all and then
delete them at the end.
  • Loading branch information
namhyung authored and yonghong-song committed Oct 8, 2022
1 parent 465f292 commit 536155a
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions libbpf-tools/klockstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,16 +526,9 @@ static int print_stats(struct ksyms *ksyms, int stack_map, int stat_map)
warn("Out of memory\n");
return -1;
}
ss->stack_id = stack_id;
if (env.reset) {
ret = bpf_map_lookup_and_delete_elem(stat_map,
&stack_id,
&ss->ls);
lookup_key = 0;
} else {
ret = bpf_map_lookup_elem(stat_map, &stack_id, &ss->ls);
lookup_key = stack_id;
}

lookup_key = ss->stack_id = stack_id;
ret = bpf_map_lookup_elem(stat_map, &stack_id, &ss->ls);
if (ret) {
free(ss);
continue;
Expand Down Expand Up @@ -575,8 +568,11 @@ static int print_stats(struct ksyms *ksyms, int stack_map, int stat_map)
print_hld_stat(ksyms, stats[i], nr_stack_entries);
}

for (i = 0; i < stat_idx; i++)
for (i = 0; i < stat_idx; i++) {
if (env.reset)
bpf_map_delete_elem(stat_map, &ss->stack_id);
free(stats[i]);
}
free(stats);

return 0;
Expand Down

0 comments on commit 536155a

Please sign in to comment.