public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* EBPF/libpbf initialising/reading raw (hardware) performance counters BPF_PERF_ARRAY
@ 2023-04-06 10:41 Andrew Nisbet
  2023-04-06 13:24 ` Jiri Olsa
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Nisbet @ 2023-04-06 10:41 UTC (permalink / raw)
  To: bpf@vger.kernel.org 

Hi,
apologies in advance if I have missed an obvious information resource.

I've used BCC/eBPF before to generate a tool that attached to sched_switch with performance counters and that traces the per-scheduling thread quanta changes in counters for threads of interest. 
I'd like to use libbbpf to create similar functionality to something like ...

perf stat -e LLC-loads,LLC-load-misses,LLC-stores,LLC-prefetches -p PID --per-thread
The actual counters are immaterial - except that I expect to be sometimes using RAW performance counters, and I typically extract the perf_event_attr information such as event/config etc using 
perf stat -vvv -e counter-name /bin/ls 

I can see from the libbpf examples how to catch the pid of the command as it enters the runqueue, but I dont see how to initialise the raw hardware performance counters to only count the pid and its threads (is this even possible or does one just accumulate counters for each relevant thread in sched_switch - this is what we do currently). I understand how to put these values into a map. 

I'm looking for (if possible) example code or information pointers on how to initialise and read the counters, not using sampling - presumably using a BPF_PERF_ARRAY map and a call to the  perf_counter_read or the older perf_read on the map. 

I'm only just getting up to speed with libbpf, so apologies if I've misunderstood and presented BCC ways that do not apply. 

Thanks,
Andy

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: EBPF/libpbf initialising/reading raw (hardware) performance counters BPF_PERF_ARRAY
  2023-04-06 10:41 EBPF/libpbf initialising/reading raw (hardware) performance counters BPF_PERF_ARRAY Andrew Nisbet
@ 2023-04-06 13:24 ` Jiri Olsa
  0 siblings, 0 replies; 2+ messages in thread
From: Jiri Olsa @ 2023-04-06 13:24 UTC (permalink / raw)
  To: Andrew Nisbet; +Cc: bpf@vger.kernel.org , Arnaldo Carvalho de Melo

On Thu, Apr 06, 2023 at 10:41:35AM +0000, Andrew Nisbet wrote:
> Hi,
> apologies in advance if I have missed an obvious information resource.
> 
> I've used BCC/eBPF before to generate a tool that attached to sched_switch with performance counters and that traces the per-scheduling thread quanta changes in counters for threads of interest. 
> I'd like to use libbbpf to create similar functionality to something like ...
> 
> perf stat -e LLC-loads,LLC-load-misses,LLC-stores,LLC-prefetches -p PID --per-thread
> The actual counters are immaterial - except that I expect to be sometimes using RAW performance counters, and I typically extract the perf_event_attr information such as event/config etc using 
> perf stat -vvv -e counter-name /bin/ls 
> 
> I can see from the libbpf examples how to catch the pid of the command as it enters the runqueue, but I dont see how to initialise the raw hardware performance counters to only count the pid and its threads (is this even possible or does one just accumulate counters for each relevant thread in sched_switch - this is what we do currently). I understand how to put these values into a map. 

yes, you need to agregate counts yourself for more pids

you can create perf counter for single entities like pid/tid/cpu/task/cgroup,
so if you want to meassure more pids you need to create counter for each and
aggregate their counts, that's what perf tool does

> 
> I'm looking for (if possible) example code or information pointers on how to initialise and read the counters, not using sampling - presumably using a BPF_PERF_ARRAY map and a call to the  perf_counter_read or the older perf_read on the map. 

there's 'bpftool prog profile' that creates perf counters,
but I think there's no aggregation, still nice example:
(see do_profile function)
  https://github.com/torvalds/linux/blob/master/tools/bpf/bpftool/prog.c

also there's libperf if you need the abstraction to create more
counters and read from them:

  https://github.com/torvalds/linux/tree/master/tools/lib/perf/
  https://github.com/torvalds/linux/tree/master/tools/lib/perf/Documentation/examples

jirka

> 
> I'm only just getting up to speed with libbpf, so apologies if I've misunderstood and presented BCC ways that do not apply. 
> 
> Thanks,
> Andy

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-04-06 13:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-06 10:41 EBPF/libpbf initialising/reading raw (hardware) performance counters BPF_PERF_ARRAY Andrew Nisbet
2023-04-06 13:24 ` Jiri Olsa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox