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