Skip to content

Commit d771e21

Browse files
kkdwvdAlexei Starovoitov
authored andcommitted
samples: bpf: Add cpumap tracepoint statistics support
This consolidates retrieval and printing into the XDP sample helper. For the kthread stats, it expands xdp_stats separately with its own per-CPU stats. For cpumap enqueue, we display FROM->TO stats also with its per-CPU stats. The help out explains in detail the various aspects of the output. Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20210821002010.845777-10-memxor@gmail.com
1 parent 0cf3c2f commit d771e21

2 files changed

Lines changed: 224 additions & 1 deletion

File tree

‎samples/bpf/xdp_sample_user.c‎

Lines changed: 218 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
enum map_type {
7575
MAP_RX,
7676
MAP_REDIRECT_ERR,
77+
MAP_CPUMAP_ENQUEUE,
78+
MAP_CPUMAP_KTHREAD,
7779
MAP_EXCEPTION,
7880
NUM_MAP,
7981
};
@@ -99,13 +101,16 @@ struct map_entry {
99101
struct stats_record {
100102
struct record rx_cnt;
101103
struct record redir_err[XDP_REDIRECT_ERR_MAX];
104+
struct record kthread;
102105
struct record exception[XDP_ACTION_MAX];
106+
struct record enq[];
103107
};
104108

105109
struct sample_output {
106110
struct {
107111
__u64 rx;
108112
__u64 redir;
113+
__u64 drop;
109114
__u64 err;
110115
} totals;
111116
struct {
@@ -228,6 +233,30 @@ static void sample_print_help(int mask)
228233
printf(" \n\t\t\t\terror/s - Packets that failed redirection per second\n\n");
229234
}
230235

236+
if (mask & SAMPLE_CPUMAP_ENQUEUE_CNT) {
237+
printf(" enqueue to cpu N\tDisplays the number of packets enqueued to bulk queue of CPU N\n"
238+
" \t\t\tExpands to cpu:FROM->N to display enqueue stats for each CPU enqueuing to CPU N\n"
239+
" \t\t\tReceived packets can be associated with the CPU redirect program is enqueuing \n"
240+
" \t\t\tpackets to.\n"
241+
" \t\t\t\tpkt/s - Packets enqueued per second from other CPU to CPU N\n"
242+
" \t\t\t\tdrop/s - Packets dropped when trying to enqueue to CPU N\n"
243+
" \t\t\t\tbulk-avg - Average number of packets processed for each event\n\n");
244+
}
245+
246+
if (mask & SAMPLE_CPUMAP_KTHREAD_CNT) {
247+
printf(" kthread\t\tDisplays the number of packets processed in CPUMAP kthread for each CPU\n"
248+
" \t\t\tPackets consumed from ptr_ring in kthread, and its xdp_stats (after calling \n"
249+
" \t\t\tCPUMAP bpf prog) are expanded below this. xdp_stats are expanded as a total and\n"
250+
" \t\t\tthen per-CPU to associate it to each CPU's pinned CPUMAP kthread.\n"
251+
" \t\t\t\tpkt/s - Packets consumed per second from ptr_ring\n"
252+
" \t\t\t\tdrop/s - Packets dropped per second in kthread\n"
253+
" \t\t\t\tsched - Number of times kthread called schedule()\n\n"
254+
" \t\t\txdp_stats (also expands to per-CPU counts)\n"
255+
" \t\t\t\tpass/s - XDP_PASS count for CPUMAP program execution\n"
256+
" \t\t\t\tdrop/s - XDP_DROP count for CPUMAP program execution\n"
257+
" \t\t\t\tredir/s - XDP_REDIRECT count for CPUMAP program execution\n\n");
258+
}
259+
231260
if (mask & SAMPLE_EXCEPTION_CNT) {
232261
printf(" xdp_exception\t\tDisplays xdp_exception tracepoint events\n"
233262
" \t\t\tThis can occur due to internal driver errors, unrecognized\n"
@@ -357,6 +386,14 @@ static struct stats_record *alloc_stats_record(void)
357386
}
358387
}
359388
}
389+
if (sample_mask & SAMPLE_CPUMAP_KTHREAD_CNT) {
390+
rec->kthread.cpu = alloc_record_per_cpu();
391+
if (!rec->kthread.cpu) {
392+
fprintf(stderr,
393+
"Failed to allocate kthread per-CPU array\n");
394+
goto end_redir;
395+
}
396+
}
360397
if (sample_mask & SAMPLE_EXCEPTION_CNT) {
361398
for (i = 0; i < XDP_ACTION_MAX; i++) {
362399
rec->exception[i].cpu = alloc_record_per_cpu();
@@ -367,13 +404,32 @@ static struct stats_record *alloc_stats_record(void)
367404
action2str(i));
368405
while (i--)
369406
free(rec->exception[i].cpu);
370-
goto end_redir;
407+
goto end_kthread;
408+
}
409+
}
410+
}
411+
if (sample_mask & SAMPLE_CPUMAP_ENQUEUE_CNT) {
412+
for (i = 0; i < sample_n_cpus; i++) {
413+
rec->enq[i].cpu = alloc_record_per_cpu();
414+
if (!rec->enq[i].cpu) {
415+
fprintf(stderr,
416+
"Failed to allocate enqueue per-CPU array for "
417+
"CPU %d\n",
418+
i);
419+
while (i--)
420+
free(rec->enq[i].cpu);
421+
goto end_exception;
371422
}
372423
}
373424
}
374425

375426
return rec;
376427

428+
end_exception:
429+
for (i = 0; i < XDP_ACTION_MAX; i++)
430+
free(rec->exception[i].cpu);
431+
end_kthread:
432+
free(rec->kthread.cpu);
377433
end_redir:
378434
for (i = 0; i < XDP_REDIRECT_ERR_MAX; i++)
379435
free(rec->redir_err[i].cpu);
@@ -390,8 +446,11 @@ static void free_stats_record(struct stats_record *r)
390446
struct map_entry *e;
391447
int i;
392448

449+
for (i = 0; i < sample_n_cpus; i++)
450+
free(r->enq[i].cpu);
393451
for (i = 0; i < XDP_ACTION_MAX; i++)
394452
free(r->exception[i].cpu);
453+
free(r->kthread.cpu);
395454
for (i = 0; i < XDP_REDIRECT_ERR_MAX; i++)
396455
free(r->redir_err[i].cpu);
397456
free(r->rx_cnt.cpu);
@@ -519,6 +578,137 @@ static void stats_get_rx_cnt(struct stats_record *stats_rec,
519578
}
520579
}
521580

581+
static void stats_get_cpumap_enqueue(struct stats_record *stats_rec,
582+
struct stats_record *stats_prev,
583+
unsigned int nr_cpus)
584+
{
585+
struct record *rec, *prev;
586+
double t, pps, drop, err;
587+
int i, to_cpu;
588+
589+
/* cpumap enqueue stats */
590+
for (to_cpu = 0; to_cpu < sample_n_cpus; to_cpu++) {
591+
rec = &stats_rec->enq[to_cpu];
592+
prev = &stats_prev->enq[to_cpu];
593+
t = calc_period(rec, prev);
594+
595+
pps = calc_pps(&rec->total, &prev->total, t);
596+
drop = calc_drop_pps(&rec->total, &prev->total, t);
597+
err = calc_errs_pps(&rec->total, &prev->total, t);
598+
599+
if (pps > 0 || drop > 0) {
600+
char str[64];
601+
602+
snprintf(str, sizeof(str), "enqueue to cpu %d", to_cpu);
603+
604+
if (err > 0)
605+
err = pps / err; /* calc average bulk size */
606+
607+
print_err(drop,
608+
" %-20s " FMT_COLUMNf FMT_COLUMNf __COLUMN(
609+
".2f") "\n",
610+
str, PPS(pps), DROP(drop), err, "bulk-avg");
611+
}
612+
613+
for (i = 0; i < nr_cpus; i++) {
614+
struct datarec *r = &rec->cpu[i];
615+
struct datarec *p = &prev->cpu[i];
616+
char str[64];
617+
618+
pps = calc_pps(r, p, t);
619+
drop = calc_drop_pps(r, p, t);
620+
err = calc_errs_pps(r, p, t);
621+
if (!pps && !drop && !err)
622+
continue;
623+
624+
snprintf(str, sizeof(str), "cpu:%d->%d", i, to_cpu);
625+
if (err > 0)
626+
err = pps / err; /* calc average bulk size */
627+
print_default(
628+
" %-18s " FMT_COLUMNf FMT_COLUMNf __COLUMN(
629+
".2f") "\n",
630+
str, PPS(pps), DROP(drop), err, "bulk-avg");
631+
}
632+
}
633+
}
634+
635+
static void stats_get_cpumap_remote(struct stats_record *stats_rec,
636+
struct stats_record *stats_prev,
637+
unsigned int nr_cpus)
638+
{
639+
double xdp_pass, xdp_drop, xdp_redirect;
640+
struct record *rec, *prev;
641+
double t;
642+
int i;
643+
644+
rec = &stats_rec->kthread;
645+
prev = &stats_prev->kthread;
646+
t = calc_period(rec, prev);
647+
648+
calc_xdp_pps(&rec->total, &prev->total, &xdp_pass, &xdp_drop,
649+
&xdp_redirect, t);
650+
if (xdp_pass || xdp_drop || xdp_redirect) {
651+
print_err(xdp_drop,
652+
" %-18s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf "\n",
653+
"xdp_stats", PASS(xdp_pass), DROP(xdp_drop),
654+
REDIR(xdp_redirect));
655+
}
656+
657+
for (i = 0; i < nr_cpus; i++) {
658+
struct datarec *r = &rec->cpu[i];
659+
struct datarec *p = &prev->cpu[i];
660+
char str[64];
661+
662+
calc_xdp_pps(r, p, &xdp_pass, &xdp_drop, &xdp_redirect, t);
663+
if (!xdp_pass && !xdp_drop && !xdp_redirect)
664+
continue;
665+
666+
snprintf(str, sizeof(str), "cpu:%d", i);
667+
print_default(" %-16s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
668+
"\n",
669+
str, PASS(xdp_pass), DROP(xdp_drop),
670+
REDIR(xdp_redirect));
671+
}
672+
}
673+
674+
static void stats_get_cpumap_kthread(struct stats_record *stats_rec,
675+
struct stats_record *stats_prev,
676+
unsigned int nr_cpus)
677+
{
678+
struct record *rec, *prev;
679+
double t, pps, drop, err;
680+
int i;
681+
682+
rec = &stats_rec->kthread;
683+
prev = &stats_prev->kthread;
684+
t = calc_period(rec, prev);
685+
686+
pps = calc_pps(&rec->total, &prev->total, t);
687+
drop = calc_drop_pps(&rec->total, &prev->total, t);
688+
err = calc_errs_pps(&rec->total, &prev->total, t);
689+
690+
print_err(drop, " %-20s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf "\n",
691+
pps ? "kthread total" : "kthread", PPS(pps), DROP(drop), err,
692+
"sched");
693+
694+
for (i = 0; i < nr_cpus; i++) {
695+
struct datarec *r = &rec->cpu[i];
696+
struct datarec *p = &prev->cpu[i];
697+
char str[64];
698+
699+
pps = calc_pps(r, p, t);
700+
drop = calc_drop_pps(r, p, t);
701+
err = calc_errs_pps(r, p, t);
702+
if (!pps && !drop && !err)
703+
continue;
704+
705+
snprintf(str, sizeof(str), "cpu:%d", i);
706+
print_default(" %-18s " FMT_COLUMNf FMT_COLUMNf FMT_COLUMNf
707+
"\n",
708+
str, PPS(pps), DROP(drop), err, "sched");
709+
}
710+
}
711+
522712
static void stats_get_redirect_cnt(struct stats_record *stats_rec,
523713
struct stats_record *stats_prev,
524714
unsigned int nr_cpus,
@@ -656,6 +846,9 @@ static void stats_print(const char *prefix, int mask, struct stats_record *r,
656846
print_always(FMT_COLUMNl, RX(out->totals.rx));
657847
if (mask & SAMPLE_REDIRECT_CNT)
658848
print_always(FMT_COLUMNl, REDIR(out->totals.redir));
849+
printf(FMT_COLUMNl,
850+
out->totals.err + out->totals.drop + out->totals.drop_xmit,
851+
"err,drop/s");
659852
printf("\n");
660853

661854
if (mask & SAMPLE_RX_CNT) {
@@ -670,6 +863,14 @@ static void stats_print(const char *prefix, int mask, struct stats_record *r,
670863
stats_get_rx_cnt(r, p, nr_cpus, NULL);
671864
}
672865

866+
if (mask & SAMPLE_CPUMAP_ENQUEUE_CNT)
867+
stats_get_cpumap_enqueue(r, p, nr_cpus);
868+
869+
if (mask & SAMPLE_CPUMAP_KTHREAD_CNT) {
870+
stats_get_cpumap_kthread(r, p, nr_cpus);
871+
stats_get_cpumap_remote(r, p, nr_cpus);
872+
}
873+
673874
if (mask & SAMPLE_REDIRECT_CNT) {
674875
str = out->redir_cnt.suc ? "redirect total" : "redirect";
675876
print_default(" %-20s " FMT_COLUMNl "\n", str,
@@ -714,6 +915,7 @@ int sample_setup_maps(struct bpf_map **maps)
714915

715916
switch (i) {
716917
case MAP_RX:
918+
case MAP_CPUMAP_KTHREAD:
717919
sample_map_count[i] = sample_n_cpus;
718920
break;
719921
case MAP_REDIRECT_ERR:
@@ -722,6 +924,8 @@ int sample_setup_maps(struct bpf_map **maps)
722924
break;
723925
case MAP_EXCEPTION:
724926
sample_map_count[i] = XDP_ACTION_MAX * sample_n_cpus;
927+
case MAP_CPUMAP_ENQUEUE:
928+
sample_map_count[i] = sample_n_cpus * sample_n_cpus;
725929
break;
726930
default:
727931
return -EINVAL;
@@ -850,6 +1054,9 @@ static void sample_summary_print(void)
8501054
print_always(" Average redir/s : %'-10.0f\n",
8511055
sample_round(pkts / period));
8521056
}
1057+
if (sample_out.totals.drop)
1058+
print_always(" Rx dropped : %'-10llu\n",
1059+
sample_out.totals.drop);
8531060
if (sample_out.totals.err)
8541061
print_always(" Errors recorded : %'-10llu\n",
8551062
sample_out.totals.err);
@@ -894,6 +1101,15 @@ static int sample_stats_collect(struct stats_record *rec)
8941101
&rec->redir_err[i]);
8951102
}
8961103

1104+
if (sample_mask & SAMPLE_CPUMAP_ENQUEUE_CNT)
1105+
for (i = 0; i < sample_n_cpus; i++)
1106+
map_collect_percpu(&sample_mmap[MAP_CPUMAP_ENQUEUE][i * sample_n_cpus],
1107+
&rec->enq[i]);
1108+
1109+
if (sample_mask & SAMPLE_CPUMAP_KTHREAD_CNT)
1110+
map_collect_percpu(sample_mmap[MAP_CPUMAP_KTHREAD],
1111+
&rec->kthread);
1112+
8971113
if (sample_mask & SAMPLE_EXCEPTION_CNT)
8981114
for (i = 0; i < XDP_ACTION_MAX; i++)
8991115
map_collect_percpu(&sample_mmap[MAP_EXCEPTION][i * sample_n_cpus],
@@ -906,6 +1122,7 @@ static void sample_summary_update(struct sample_output *out, int interval)
9061122
{
9071123
sample_out.totals.rx += out->totals.rx;
9081124
sample_out.totals.redir += out->totals.redir;
1125+
sample_out.totals.drop += out->totals.drop;
9091126
sample_out.totals.err += out->totals.err;
9101127
sample_out.rx_cnt.pps += interval;
9111128
}

‎samples/bpf/xdp_sample_user.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ enum stats_mask {
1111
_SAMPLE_REDIRECT_MAP = 1U << 0,
1212
SAMPLE_RX_CNT = 1U << 1,
1313
SAMPLE_REDIRECT_ERR_CNT = 1U << 2,
14+
SAMPLE_CPUMAP_ENQUEUE_CNT = 1U << 3,
15+
SAMPLE_CPUMAP_KTHREAD_CNT = 1U << 4,
1416
SAMPLE_EXCEPTION_CNT = 1U << 5,
1517
SAMPLE_REDIRECT_CNT = 1U << 7,
1618
SAMPLE_REDIRECT_MAP_CNT = SAMPLE_REDIRECT_CNT | _SAMPLE_REDIRECT_MAP,
@@ -76,6 +78,10 @@ static inline char *safe_strncpy(char *dst, const char *src, size_t size)
7678
__attach_tp(tp_xdp_redirect_map_err); \
7779
if (mask & SAMPLE_REDIRECT_ERR_CNT) \
7880
__attach_tp(tp_xdp_redirect_err); \
81+
if (mask & SAMPLE_CPUMAP_ENQUEUE_CNT) \
82+
__attach_tp(tp_xdp_cpumap_enqueue); \
83+
if (mask & SAMPLE_CPUMAP_KTHREAD_CNT) \
84+
__attach_tp(tp_xdp_cpumap_kthread); \
7985
if (mask & SAMPLE_EXCEPTION_CNT) \
8086
__attach_tp(tp_xdp_exception); \
8187
return 0; \

0 commit comments

Comments
 (0)