Skip to content

Commit bfccfe6

Browse files
namhyungyonghong-song
authored andcommitted
libbpf-tools/klockstat: Print human friendly time stats
Instead of nsec, it can show the time with unit like below: $ sudo klockstat -n 5 Tracing mutex/sem lock events... Hit Ctrl-C to end ^C Caller Avg Wait Count Max Wait Total Wait iwl_mvm_mac_sta_statistics+0x6b 703.9 us 4 2.5 ms 2.8 ms i915_vma_pin_ww+0x6ff 1.0 us 5224 1.8 ms 5.2 ms do_epoll_wait+0x1d5 1.9 us 8569 651.3 us 16.0 ms kernfs_dop_revalidate+0x35 1.1 us 1176 540.2 us 1.3 ms kernfs_iop_permission+0x2a 1.0 us 1512 528.6 us 1.5 ms Caller Avg Hold Count Max Hold Total Hold __fdget_pos+0x42 13.4 ms 201 19.7 ms 2.7 s genl_rcv+0x15 1.8 ms 8 6.3 ms 14.3 ms nl80211_pre_doit+0xdb 1.9 ms 4 6.2 ms 7.4 ms ieee80211_get_station+0x2a 1.8 ms 4 6.2 ms 7.1 ms bpf_tracing_prog_attach+0x264 2.9 ms 15 3.8 ms 43.4 ms Exiting trace of mutex/sem locks
1 parent 67b8cfb commit bfccfe6

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

libbpf-tools/klockstat.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,32 @@ static char *symname(struct ksyms *ksyms, uint64_t pc, char *buf, size_t n)
327327
return buf;
328328
}
329329

330+
static char *print_time(char *buf, int size, uint64_t nsec)
331+
{
332+
struct {
333+
float base;
334+
char *unit;
335+
} table[] = {
336+
{ 1e9 * 3600, "h " },
337+
{ 1e9 * 60, "m " },
338+
{ 1e9, "s " },
339+
{ 1e6, "ms" },
340+
{ 1e3, "us" },
341+
{ 0, NULL },
342+
};
343+
344+
for (int i = 0; table[i].base; i++) {
345+
if (nsec < table[i].base)
346+
continue;
347+
348+
snprintf(buf, size, "%.1f %s", nsec / table[i].base, table[i].unit);
349+
return buf;
350+
}
351+
352+
snprintf(buf, size, "%u ns", (unsigned)nsec);
353+
return buf;
354+
}
355+
330356
static void print_acq_header(void)
331357
{
332358
printf("\n Caller Avg Wait Count Max Wait Total Wait\n");
@@ -336,14 +362,17 @@ static void print_acq_stat(struct ksyms *ksyms, struct stack_stat *ss,
336362
int nr_stack_entries)
337363
{
338364
char buf[40];
365+
char avg[40];
366+
char max[40];
367+
char tot[40];
339368
int i;
340369

341-
printf("%37s %9llu %8llu %10llu %12llu\n",
370+
printf("%37s %9s %8llu %10s %12s\n",
342371
symname(ksyms, ss->bt[0], buf, sizeof(buf)),
343-
ss->ls.acq_total_time / ss->ls.acq_count,
372+
print_time(avg, sizeof(avg), ss->ls.acq_total_time / ss->ls.acq_count),
344373
ss->ls.acq_count,
345-
ss->ls.acq_max_time,
346-
ss->ls.acq_total_time);
374+
print_time(max, sizeof(max), ss->ls.acq_max_time),
375+
print_time(tot, sizeof(tot), ss->ls.acq_total_time));
347376
for (i = 1; i < nr_stack_entries; i++) {
348377
if (!ss->bt[i])
349378
break;
@@ -364,14 +393,17 @@ static void print_hld_stat(struct ksyms *ksyms, struct stack_stat *ss,
364393
int nr_stack_entries)
365394
{
366395
char buf[40];
396+
char avg[40];
397+
char max[40];
398+
char tot[40];
367399
int i;
368400

369-
printf("%37s %9llu %8llu %10llu %12llu\n",
401+
printf("%37s %9s %8llu %10s %12s\n",
370402
symname(ksyms, ss->bt[0], buf, sizeof(buf)),
371-
ss->ls.hld_total_time / ss->ls.hld_count,
403+
print_time(avg, sizeof(avg), ss->ls.hld_total_time / ss->ls.hld_count),
372404
ss->ls.hld_count,
373-
ss->ls.hld_max_time,
374-
ss->ls.hld_total_time);
405+
print_time(max, sizeof(max), ss->ls.hld_max_time),
406+
print_time(tot, sizeof(tot), ss->ls.hld_total_time));
375407
for (i = 1; i < nr_stack_entries; i++) {
376408
if (!ss->bt[i])
377409
break;

0 commit comments

Comments
 (0)