samples/bpf: Add a trace tool with perf PMU counters
PATCH 1/1 samples/bpf: Add a trace tool with perf PMU counters
Developers might need to profile a program with fine-grained granularity. E.g., a user case is to account the CPU cycles for a small program or for a specific function within the program.
This commit introduces a small tool with using eBPF program to read the perf PMU counters for performance metrics. As the first step, the four counters are supported with the '-e' option: cycles, instructions, branches, branch-misses.
The '-r' option is provided for support raw event number. This option is mutually exclusive to the '-e' option, users either pass a raw event number or a counter name.
The tool enables the counters for the entire trace session in free-run mode. It reads the beginning values for counters when the profiled program is scheduled in, and calculate the interval when the task is scheduled out. The advantage of this approach is to dismiss the statistics noise (e.g. caused by the tool itself) as possible.
The tool can support function based tracing. By using the '-f' option, users can specify the traced function. The eBPF program enables tracing at the function entry and disables trace upon exit from the function.
The '-u' option can be specified for tracing user mode only.
Below are several usage examples.
Trace CPU cycles for the whole program:
# ./trace_counter -e cycles -- /mnt/sort
Or
# ./trace_counter -e cycles /mnt/sort
Create process for the workload.
Enable the event cycles.
Bubble sorting array of 3000 elements
551 ms
Finished the workload.
Event (cycles) statistics:
+-----------+------------------+
| CPU[0000] | 29093250 |
+-----------+------------------+
| CPU[0002] | 75672820 |
+-----------+------------------+
| CPU[0006] | 1067458735 |
+-----------+------------------+
Total : 1172224805Trace branches for the user mode only:
# ./trace_counter -e branches -u -- /mnt/sort
Create process for the workload.
Enable the event branches.
Bubble sorting array of 3000 elements
541 ms
Finished the workload.
Event (branches) statistics:
+-----------+------------------+
| CPU[0007] | 88112669 |
+-----------+------------------+
Total : 88112669Trace instructions for the 'bubble_sort' function:
# ./trace_counter -f bubble_sort -e instructions -- /mnt/sort
Create process for the workload.
Enable the event instructions.
Bubble sorting array of 3000 elements
541 ms
Finished the workload.
Event (instructions) statistics:
+-----------+------------------+
| CPU[0006] | 1169810201 |
+-----------+------------------+
Total : 1169810201
Function (bubble_sort) duration statistics:
Count : 5
Minimum : 232009928
Maximum : 236742006
Average : 233962040Trace the raw event '0x5' (L1D_TLB_REFILL):
# ./trace_counter -r 0x5 -u -- /mnt/sort
Create process for the workload.
Enable the raw event 0x5.
Bubble sorting array of 3000 elements
540 ms
Finished the workload.
Event (0x5) statistics:
+-----------+------------------+
| CPU[0007] | 174 |
+-----------+------------------+
Total : 174Trace for the function and set CPU affinity for the profiled program:
# ./trace_counter -f bubble_sort -x /mnt/sort -e cycles \
-- taskset -c 2 /mnt/sort
Create process for the workload.
Enable the event cycles.
Bubble sorting array of 3000 elements
619 ms
Finished the workload.
Event (cycles) statistics:
+-----------+------------------+
| CPU[0002] | 1169913056 |
+-----------+------------------+
Total : 1169913056
Function (bubble_sort) duration statistics:
Count : 5
Minimum : 232054101
Maximum : 236769623
Average : 233982611The command above sets the CPU affinity with taskset command. The profiled function 'bubble_sort' is in the executable '/mnt/sort' but not in the taskset binary. The '-x' option is used to tell the tool the correct executable path.
--- samples/bpf/Makefile | 7 +- samples/bpf/trace_counter.bpf.c | 222 +++++++++++++ samples/bpf/trace_counter_user.c | 528 +++++++++++++++++++++++++++++++ 3 files changed, 756 insertions(+), 1 deletion(-) create mode 100644 samples/bpf/trace_counter.bpf.c create mode 100644 samples/bpf/trace_counter_user.c
samples/bpf/Makefile +6 -1
@@ -23,6 +23,7 @@ tprogs-y += offwaketime tprogs-y += spintest tprogs-y += map_perf_test tprogs-y += xdp_router_ipv4 +tprogs-y += trace_counter tprogs-y += trace_event tprogs-y += sampleip tprogs-y += tc_l2_redirect
@@ -64,6 +65,7 @@ offwaketime-objs := offwaketime_user.o $(TRACE_HELPERS) spintest-objs := spintest_user.o $(TRACE_HELPERS) map_perf_test-objs := map_perf_test_user.o test_overhead-objs := test_overhead_user.o +trace_counter-objs := trace_counter_user.o $(TRACE_HELPERS) trace_event-objs := trace_event_user.o $(TRACE_HELPERS) sampleip-objs := sampleip_user.o $(TRACE_HELPERS) tc_l2_redirect-objs := tc_l2_redirect_user.o
@@ -99,6 +101,7 @@ always-y += offwaketime.bpf.o always-y += spintest.bpf.o always-y += map_perf_test.bpf.o always-y += parse_varlen.o parse_simple.o parse_ldabs.o +always-y += trace_counter.bpf.o always-y += trace_event_kern.o always-y += sampleip_kern.o always-y += lwt_len_hist.bpf.o
@@ -283,6 +286,7 @@ $(obj)/$(TRACE_HELPERS) $(obj)/$(CGROUP_HELPERS) $(obj)/$(XDP_SAMPLE): | libbpf_ .PHONY: libbpf_hdrs $(obj)/xdp_router_ipv4_user.o: $(obj)/xdp_router_ipv4.skel.h +$(obj)/trace_counter_user.o: $(obj)/trace_counter.skel.h $(obj)/tracex5.bpf.o: $(obj)/syscall_nrs.h $(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h
@@ -347,10 +351,11 @@ $(obj)/%.bpf.o: $(src)/%.bpf.c $(obj)/vmlinux.h $(src)/xdp_sample.bpf.h $(src)/x -I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \ -c $(filter %.bpf.c,$^) -o $@ -LINKED_SKELS := xdp_router_ipv4.skel.h +LINKED_SKELS := xdp_router_ipv4.skel.h trace_counter.skel.h clean-files += $(LINKED_SKELS) xdp_router_ipv4.skel.h-deps := xdp_router_ipv4.bpf.o xdp_sample.bpf.o +trace_counter.skel.h-deps := trace_counter.bpf.o LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.bpf.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
samples/bpf/trace_counter.bpf.c new file +222 -0
222 lines not on this page · raw message
samples/bpf/trace_counter_user.c new file +528 -0
528 lines not on this page · raw message
Signature · 1 line
2.34.1
Daniel Borkmann
maintainerHi Leo,
Quoted 119 lines · Leo Yan
Developers might need to profile a program with fine-grained granularity. E.g., a user case is to account the CPU cycles for a small program or for a specific function within the program.
This commit introduces a small tool with using eBPF program to read the perf PMU counters for performance metrics. As the first step, the four counters are supported with the '-e' option: cycles, instructions, branches, branch-misses.
The '-r' option is provided for support raw event number. This option is mutually exclusive to the '-e' option, users either pass a raw event number or a counter name.
The tool enables the counters for the entire trace session in free-run mode. It reads the beginning values for counters when the profiled program is scheduled in, and calculate the interval when the task is scheduled out. The advantage of this approach is to dismiss the statistics noise (e.g. caused by the tool itself) as possible.
The tool can support function based tracing. By using the '-f' option, users can specify the traced function. The eBPF program enables tracing at the function entry and disables trace upon exit from the function.
The '-u' option can be specified for tracing user mode only.
Below are several usage examples.
Trace CPU cycles for the whole program:
# ./trace_counter -e cycles -- /mnt/sort Or # ./trace_counter -e cycles /mnt/sort Create process for the workload. Enable the event cycles. Bubble sorting array of 3000 elements 551 ms Finished the workload. Event (cycles) statistics: +-----------+------------------+ | CPU[0000] | 29093250 | +-----------+------------------+ | CPU[0002] | 75672820 | +-----------+------------------+ | CPU[0006] | 1067458735 | +-----------+------------------+ Total : 1172224805Trace branches for the user mode only:
# ./trace_counter -e branches -u -- /mnt/sort Create process for the workload. Enable the event branches. Bubble sorting array of 3000 elements 541 ms Finished the workload. Event (branches) statistics: +-----------+------------------+ | CPU[0007] | 88112669 | +-----------+------------------+ Total : 88112669Trace instructions for the '
bubble_sort' function:# ./trace_counter -f bubble_sort -e instructions -- /mnt/sort Create process for the workload. Enable the event instructions. Bubble sorting array of 3000 elements 541 ms Finished the workload. Event (instructions) statistics: +-----------+------------------+ | CPU[0006] | 1169810201 | +-----------+------------------+ Total : 1169810201 Function (bubble_sort) duration statistics: Count : 5 Minimum : 232009928 Maximum : 236742006 Average : 233962040Trace the raw event '
0x5' (L1D_TLB_REFILL):# ./trace_counter -r 0x5 -u -- /mnt/sort Create process for the workload. Enable the raw event 0x5. Bubble sorting array of 3000 elements 540 ms Finished the workload. Event (0x5) statistics: +-----------+------------------+ | CPU[0007] | 174 | +-----------+------------------+ Total : 174Trace for the function and set CPU affinity for the profiled program:
# ./trace_counter -f bubble_sort -x /mnt/sort -e cycles \ -- taskset -c 2 /mnt/sort Create process for the workload. Enable the event cycles. Bubble sorting array of 3000 elements 619 ms Finished the workload. Event (cycles) statistics: +-----------+------------------+ | CPU[0002] | 1169913056 | +-----------+------------------+ Total : 1169913056 Function (bubble_sort) duration statistics: Count : 5 Minimum : 232054101 Maximum : 236769623 Average : 233982611The command above sets the CPU affinity with taskset command. The profiled function '
bubble_sort' is in the executable '/mnt/sort' but not in the taskset binary. The '-x' option is used to tell the tool the correct executable path.
Quoted 8 lines
Signed-off-by Leo Yan <[email protected]>--- samples/bpf/Makefile | 7 +- samples/bpf/trace_counter.bpf.c | 222 +++++++++++++ samples/bpf/trace_counter_user.c | 528 +++++++++++++++++++++++++++++++ 3 files changed, 756 insertions(+), 1 deletion(-)create mode 100644
samples/bpf/trace_counter.bpf.ccreate mode 100644samples/bpf/trace_counter_user.c
Thanks for this work! Few suggestions.. the contents of samples/bpf/ are in process of being migrated into BPF selftests given they have been bit-rotting for quite some time, so we'd like to migrate missing coverage into BPF CI (see test_progs in tools/testing/selftests/bpf/). That could be one option, or an alternative is to extend bpftool for profiling BPF programs (see 47c09d6a9f67 ("bpftool: Introduce "prog profile" command")).
Hi Daniel,
Quoted 22 lines · Daniel Borkmann
Hi Leo,
Quoted 18 lines · Leo Yan
Developers might need to profile a program with fine-grained granularity. E.g., a user case is to account the CPU cycles for a small program or for a specific function within the program.
This commit introduces a small tool with using eBPF program to read the perf PMU counters for performance metrics. As the first step, the four counters are supported with the '-e' option: cycles, instructions, branches, branch-misses.
The '-r' option is provided for support raw event number. This option is mutually exclusive to the '-e' option, users either pass a raw event number or a counter name.
The tool enables the counters for the entire trace session in free-run mode. It reads the beginning values for counters when the profiled program is scheduled in, and calculate the interval when the task is scheduled out. The advantage of this approach is to dismiss the statistics noise (e.g. caused by the tool itself) as possible.
[...]
Quoted 5 lines
Thanks for this work! Few suggestions.. the contents of
samples/bpf/are in process of being migrated into BPF selftests given they have been bit-rotting for quite some time, so we'd like to migrate missing coverage into BPF CI (seetest_progsintools/testing/selftests/bpf/). That could be one option, or an alternative is to extend bpftool for profiling BPF programs (see47c09d6a9f67("bpftool: Introduce "prog profile" command")).
Thanks for suggestions!
The naming or info in the commit log might cause confuse. The purpose of this patch is to measure PMU counters for normal programs (not BPF program specific). To achieve profiling accuracy, it opens perf event in thread mode, and support function based trace and can be userspace mode only.
My understanding for bpftool is for eBPF program specific. I looked into a bit the commit 47c09d6a9f67, it is nature for integrating the tracing feature for eBPF program specific. My patch is for tracing normal userspace programs, I am not sure if this is really wanted by bpftool. I would like to hear opinions from bpftool maintainer before proceeding.
My program mainly uses eBPF attaching to uprobe. selftest/bpf has contained the related functionality testing, actually I refered the test for writing my code :). So maybe it is not quite useful for merging the code as a test?
If both options are not ideal, I would spend time to land the feature in perf tool - the perf tool has supported eBPF backend for reading PMU counters, but it is absent function based profiling.
Thanks,
Leo
Quoted 50 lines · Leo Yan
Hi Daniel,
Quoted 22 lines · Daniel Borkmann
Hi Leo,
Quoted 18 lines · Leo Yan
Developers might need to profile a program with fine-grained granularity. E.g., a user case is to account the CPU cycles for a small program or for a specific function within the program.
This commit introduces a small tool with using eBPF program to read the perf PMU counters for performance metrics. As the first step, the four counters are supported with the '-e' option: cycles, instructions, branches, branch-misses.
The '-r' option is provided for support raw event number. This option is mutually exclusive to the '-e' option, users either pass a raw event number or a counter name.
The tool enables the counters for the entire trace session in free-run mode. It reads the beginning values for counters when the profiled program is scheduled in, and calculate the interval when the task is scheduled out. The advantage of this approach is to dismiss the statistics noise (e.g. caused by the tool itself) as possible.
[...]
Quoted 5 lines
Thanks for this work! Few suggestions.. the contents of
samples/bpf/are in process of being migrated into BPF selftests given they have been bit-rotting for quite some time, so we'd like to migrate missing coverage into BPF CI (seetest_progsintools/testing/selftests/bpf/). That could be one option, or an alternative is to extend bpftool for profiling BPF programs (see47c09d6a9f67("bpftool: Introduce "prog profile" command")).Thanks for suggestions!
The naming or info in the commit log might cause confuse. The purpose of this patch is to measure PMU counters for normal programs (not BPF program specific). To achieve profiling accuracy, it opens perf event in thread mode, and support function based trace and can be userspace mode only.
My understanding for bpftool is for eBPF program specific. I looked into a bit the commit
47c09d6a9f67, it is nature for integrating the tracing feature for eBPF program specific. My patch is for tracing normal userspace programs, I am not sure if this is really wanted by bpftool. I would like to hear opinions from bpftool maintainer before proceeding.
Quoted 8 lines
My program mainly uses eBPF attaching to uprobe. selftest/bpf has contained the related functionality testing, actually I refered the test for writing my code :). So maybe it is not quite useful for merging the code as a test?
If both options are not ideal, I would spend time to land the feature in perf tool - the perf tool has supported eBPF backend for reading PMU counters, but it is absent function based profiling.
We don't add tools to kernel repo. bpftool is an exception because it's used during the selftest build. 'perf' is another exception for historical reasons.
This particular feature fits 'perf' the best.
Daniel Borkmann
maintainerQuoted 38 lines · Alexei Starovoitov
Quoted 37 lines · Leo Yan
Quoted 19 lines · Daniel Borkmann
Quoted 18 lines · Leo Yan
Developers might need to profile a program with fine-grained granularity. E.g., a user case is to account the CPU cycles for a small program or for a specific function within the program.
This commit introduces a small tool with using eBPF program to read the perf PMU counters for performance metrics. As the first step, the four counters are supported with the '-e' option: cycles, instructions, branches, branch-misses.
The '-r' option is provided for support raw event number. This option is mutually exclusive to the '-e' option, users either pass a raw event number or a counter name.
The tool enables the counters for the entire trace session in free-run mode. It reads the beginning values for counters when the profiled program is scheduled in, and calculate the interval when the task is scheduled out. The advantage of this approach is to dismiss the statistics noise (e.g. caused by the tool itself) as possible.
[...]
Quoted 5 lines
Thanks for this work! Few suggestions.. the contents of
samples/bpf/are in process of being migrated into BPF selftests given they have been bit-rotting for quite some time, so we'd like to migrate missing coverage into BPF CI (seetest_progsintools/testing/selftests/bpf/). That could be one option, or an alternative is to extend bpftool for profiling BPF programs (see47c09d6a9f67("bpftool: Introduce "prog profile" command")).Thanks for suggestions!
The naming or info in the commit log might cause confuse. The purpose of this patch is to measure PMU counters for normal programs (not BPF program specific). To achieve profiling accuracy, it opens perf event in thread mode, and support function based trace and can be userspace mode only.
Quoted 6 lines
Quoted 6 lines
My understanding for bpftool is for eBPF program specific. I looked into a bit the commit
47c09d6a9f67, it is nature for integrating the tracing feature for eBPF program specific. My patch is for tracing normal userspace programs, I am not sure if this is really wanted by bpftool. I would like to hear opinions from bpftool maintainer before proceeding.
Yes, that suggestion was if it would have been applicable also for the existing bpftool (BPF program) profiling functionality.
Quoted 9 lines
Quoted 8 lines
My program mainly uses eBPF attaching to uprobe. selftest/bpf has contained the related functionality testing, actually I refered the test for writing my code :). So maybe it is not quite useful for merging the code as a test?
If both options are not ideal, I would spend time to land the feature in perf tool - the perf tool has supported eBPF backend for reading PMU counters, but it is absent function based profiling.
Quoted 5 lines
We don't add tools to kernel repo. bpftool is an exception because it's used during the selftest build. 'perf' is another exception for historical reasons.
This particular feature fits 'perf' the best.
Agree, looks like perf is the best target for integration then.
Quoted 2 lines · Daniel Borkmann
Quoted 1 line · Alexei Starovoitov
On Mon, Jan 20, 2025 at 10:50 AM Leo Yan <[email protected]> wrote:
[...]
Quoted 19 lines
Quoted 6 lines
Quoted 6 lines
My understanding for bpftool is for eBPF program specific. I looked into a bit the commit
47c09d6a9f67, it is nature for integrating the tracing feature for eBPF program specific. My patch is for tracing normal userspace programs, I am not sure if this is really wanted by bpftool. I would like to hear opinions from bpftool maintainer before proceeding.Yes, that suggestion was if it would have been applicable also for the existing bpftool (BPF program) profiling functionality.
Quoted 9 lines
Quoted 8 lines
My program mainly uses eBPF attaching to uprobe. selftest/bpf has contained the related functionality testing, actually I refered the test for writing my code :). So maybe it is not quite useful for merging the code as a test?
If both options are not ideal, I would spend time to land the feature in perf tool - the perf tool has supported eBPF backend for reading PMU counters, but it is absent function based profiling.
Quoted 7 lines
Quoted 5 lines
We don't add tools to kernel repo. bpftool is an exception because it's used during the selftest build. 'perf' is another exception for historical reasons.
This particular feature fits 'perf' the best.
Agree, looks like perf is the best target for integration then.
Thanks for suggestions, Alexei and Daniel. It makes sense for me to move to perf, and now I understand the policy for moving code from samples/bpf.
It may be irrelevant to the patch itself. I know we have great BPF toolings (BCC/bpftrace, etc), but it would be a bit confused for me that we don't have a offical repo to maintain C based BPF toolkits.
Sometimes, C based BPF tool is small and handy ...
Thanks,
Leo