libperf: Add support for user space counter access
RFC 1/1 libperf: Add support for user space counter access
x86 and arm64 can both support direct access of event counters in userspace. The access sequence is less than trivial and currently exists in perf test code (tools/perf/arch/x86/tests/rdpmc.c) with copies in projects such as PAPI and libpfm4.
Patches to add arm64 userspace support are pending[1].
For this RFC, looking for a yes, seems like a good idea, or no, go away we don't want this in libperf.
TODO:
- Handle userspace access not enabled.
- Handle pmc_width and cap_user_time_short in read loop.
- Move existing rdpmc test to libperf based test?
- Abstract out rdtsc/rdpmc (currently only builds on x86 and need to add Arm versions)
[1] https://lore.kernel.org/r/[email protected]/
--- .../perf/Documentation/examples/user_read.c | 103 ++++++++++++++++++ tools/lib/perf/include/perf/mmap.h | 2 + tools/lib/perf/libperf.map | 1 + tools/lib/perf/mmap.c | 72 ++++++++++++ 4 files changed, 178 insertions(+) create mode 100644 tools/lib/perf/Documentation/examples/user_read.c
tools/lib/perf/Documentation/examples/user_read.c new file +103 -0
@@ -0,0 +1,103 @@ +#include <linux/perf_event.h> +#include <perf/evlist.h> +#include <perf/evsel.h> +#include <perf/cpumap.h> +#include <perf/threadmap.h> +#include <perf/mmap.h> +#include <perf/core.h> +#include <perf/event.h> +#include <stdio.h> +#include <unistd.h> + +static int libperf_print (enum libperf_print_level level, +const char *fmt,va_list ap) +{ +return vfprintf (stderr, fmt, ap); +} + +static unsigned long long read_counter (struct perf_evlist *evlist) +{ +struct perf_mmap *map; +unsigned long long cnt; + +perf_evlist__for_each_mmap (evlist, map, false) { +while ((cnt =perf_mmap__read_self (map)) == ~0ULL ) ; +return cnt; + } +return 0 ; +} + +int main (int argc,char **argv) +{ +int count =100000 , err =0 ; +unsigned long long start, end; +struct perf_evlist *evlist; +struct perf_evsel *evsel; +struct perf_thread_map *threads; +struct perf_cpu_map *cpus; +struct perf_counts_values counts; + +struct perf_event_attr attr1 = { + .type =PERF_TYPE_HARDWARE , + .config =PERF_COUNT_HW_CPU_CYCLES , + .disabled =1 , + .exclude_kernel =1 , + }; + +libperf_init (libperf_print); + + evlist =perf_evlist__new (); +if (!evlist) { +fprintf (stderr,"failed to create evlist\n" ); + goto out_threads; + } + evsel =perf_evsel__new (&attr1); +if (!evsel) { +fprintf (stderr,"failed to create evsel1\n" ); + goto out_evlist; + } +perf_evlist__add (evlist, evsel); + + threads =perf_thread_map__new_dummy (); +if (!threads) { +fprintf (stderr,"failed to create threads\n" ); +return -1 ; + } +perf_thread_map__set_pid (threads,0 ,0 ); + cpus =perf_cpu_map__dummy_new (); +if (!cpus) { +fprintf (stderr,"failed to create cpus\n" ); +return -1 ; + } +perf_evlist__set_maps (evlist, cpus, threads); + + err =perf_evlist__open (evlist); +if (err) { +fprintf (stderr,"failed to open evsel\n" ); + goto out_evlist; + } + + err =perf_evlist__mmap (evlist,0 ); +if (err) { +fprintf (stderr,"failed to mmap evlist\n" ); + goto out_evlist; + } + +perf_evlist__enable (evlist); + + start =read_counter (evlist); +while (count--); + end =read_counter (evlist); + +perf_evlist__disable (evlist); + +fprintf (stdout,"start %llu, end %llu, delta %llu\n" , + start, end, end-start); + +perf_evlist__close (evlist); +out_evlist: +perf_evlist__delete (evlist); +out_threads: +perf_thread_map__put (threads); +return err; +}
tools/lib/perf/include/perf/mmap.h +2 -0
@@ -12,4 +12,6 @@ LIBPERF_API int perf_mmap__read_init(struct perf_mmap *map); LIBPERF_API void perf_mmap__read_done(struct perf_mmap *map); LIBPERF_API union perf_event *perf_mmap__read_event(struct perf_mmap *map); +LIBPERF_API __u64 perf_mmap__read_self(struct perf_mmap *map); +#endif /* __LIBPERF_MMAP_H */
tools/lib/perf/libperf.map +1 -0
@@ -46,6 +46,7 @@ LIBPERF_0.0.1 { perf_mmap__read_init; perf_mmap__read_done; perf_mmap__read_event; + perf_mmap__read_self; local: *; };
tools/lib/perf/mmap.c +72 -0
75 lines not on this page · raw message
Signature · 1 line
2.25.1
Quoted 9 lines · Rob Herring
x86 and arm64 can both support direct access of event counters in userspace. The access sequence is less than trivial and currently exists in perf test code (
tools/perf/arch/x86/tests/rdpmc.c) with copies in projects such as PAPI and libpfm4.Patches to add arm64 userspace support are pending[1].
For this RFC, looking for a yes, seems like a good idea, or no, go away we don't want this in libperf.
hi,
looks great!
I wanted to add this for very long time.. so yes, we want this ;-)
Quoted 7 lines
TODO:
- Handle userspace access not enabled.
- Handlepmc_widthandcap_user_time_shortin read loop.
- Move existing rdpmc test to libperf based test?
- Abstract out rdtsc/rdpmc (currently only builds on x86 and need to add Arm versions)
all of them seem good, also please add test for perf_mmap__read_self itself and update the doc/man
SNIP
Quoted 9 lines
tools/lib/perf/mmap.c +9
-0+{ +struct perf_event_mmap_page *pc = map->base; +u32 seq, idx, time_mult =0 , time_shift =0 ; +u64 count, cyc =0 , time_offset =0 , enabled, running, delta; + +/* +* Check if event was unmapped due to a POLLHUP/POLLERR. +*/ +if (!refcount_read (&map->refcnt))
tools/lib/perf/mmap.c union perf_event *perf_mmap__read_event(struct perf_mmap *map) · line 314
Quoted 10 lines
tools/lib/perf/mmap.c +10
-0+return ~0 ; + + do { + seq = pc->lock; + barrier(); + + enabled = pc->time_enabled; + running = pc->time_running; + + if (enabled != running) {
should you check pc->cap_usr_time in here?
tools/lib/perf/mmap.c union perf_event *perf_mmap__read_event(struct perf_mmap *map) · line 323
Quoted 9 lines
tools/lib/perf/mmap.c +9
-0+ cyc =rdtsc (); + time_mult = pc->time_mult; + time_shift = pc->time_shift; + time_offset = pc->time_offset; + } + + idx = pc->index; + count = pc->offset; +if (idx)
and pc->cap_user_rdpmc in here?
ok, it's probably in the TODO
thanks,
jirka
Quoted 9 lines · Jiri Olsa
Quoted 7 lines · Rob Herring
x86 and arm64 can both support direct access of event counters in userspace. The access sequence is less than trivial and currently exists in perf test code (
tools/perf/arch/x86/tests/rdpmc.c) with copies in projects such as PAPI and libpfm4.Patches to add arm64 userspace support are pending[1].
Quoted 7 lines
Quoted 2 lines
For this RFC, looking for a yes, seems like a good idea, or no, go away we don't want this in libperf.
hi,
looks great!I wanted to add this for very long time.. so yes, we want this ;-)
Thanks for the quick feedback. Would this be better implemented as a fast path for perf_evsel__read()? If so, how to get the mmap data which is associated with a evlist rather than a evsel?
Rob
Quoted 13 lines · Rob Herring
Quoted 12 lines · Jiri Olsa
Quoted 9 lines · Rob Herring
x86 and arm64 can both support direct access of event counters in userspace. The access sequence is less than trivial and currently exists in perf test code (
tools/perf/arch/x86/tests/rdpmc.c) with copies in projects such as PAPI and libpfm4.Patches to add arm64 userspace support are pending[1].
For this RFC, looking for a yes, seems like a good idea, or no, go away we don't want this in libperf.
Quoted 7 lines
Quoted 4 lines
hi,
looks great!I wanted to add this for very long time.. so yes, we want this ;-)
Thanks for the quick feedback. Would this be better implemented as a fast path for
perf_evsel__read()? If so, how to get the mmap data
if it works for all events, which I'm not sure of
Quoted 1 line
which is associated with a evlist rather than a evsel?
not sure what you mean, you can mmap evsel, not evlist
jirka
[email protected]
maintainerQuoted 1 line · Jiri Olsa
if it works for all events, which I'm not sure of
That's what we have cap_user_rdpmc for.
Quoted 20 lines · Jiri Olsa
Quoted 18 lines · Rob Herring
Quoted 16 lines · Jiri Olsa
Quoted 9 lines · Rob Herring
x86 and arm64 can both support direct access of event counters in userspace. The access sequence is less than trivial and currently exists in perf test code (
tools/perf/arch/x86/tests/rdpmc.c) with copies in projects such as PAPI and libpfm4.Patches to add arm64 userspace support are pending[1].
For this RFC, looking for a yes, seems like a good idea, or no, go away we don't want this in libperf.
hi,
looks great!I wanted to add this for very long time.. so yes, we want this ;-)
Quoted 8 lines
Quoted 2 lines
Thanks for the quick feedback. Would this be better implemented as a fast path for
perf_evsel__read()? If so, how to get the mmap dataif it works for all events, which I'm not sure of
Quoted 1 line
which is associated with a evlist rather than a evsel?
not sure what you mean, you can mmap evsel, not evlist
While yes the mmap is created from an evsel fd, they are ultimately associated with the evlist struct and are per thread or cpu. If there's more than 1 evsel, then the additional ones are set to the 1st mmap with PERF_EVENT_IOC_SET_OUTPUT. Which I now realize means this RFC only works for the first evsel. So I guess the API needs to work something like this:
threads = perf_thread_map__new_dummy();perf_thread_map__set_pid(threads, 0, 0);
evsel = perf_evsel__new(&attr);perf_evsel__open(evsel, NULL, threads);
perf_evsel__mmap(evsel); <--- *new*
perf_evsel__read(evsel, 0, 0, &counts); // If we have an mmap, then try a direct read
Perhaps some refactoring of the mmap code in evlist.c will be needed, but the usage seems pretty orthogonal. I'd propose that mmapping via perf_evlist__mmap() behavior remain unchanged and direct access is not supported in that case.
Rob
Quoted 37 lines · Rob Herring
Quoted 20 lines · Jiri Olsa
Quoted 18 lines · Rob Herring
Quoted 16 lines · Jiri Olsa
Quoted 9 lines · Rob Herring
x86 and arm64 can both support direct access of event counters in userspace. The access sequence is less than trivial and currently exists in perf test code (
tools/perf/arch/x86/tests/rdpmc.c) with copies in projects such as PAPI and libpfm4.Patches to add arm64 userspace support are pending[1].
For this RFC, looking for a yes, seems like a good idea, or no, go away we don't want this in libperf.
hi,
looks great!I wanted to add this for very long time.. so yes, we want this ;-)
Quoted 8 lines
Quoted 2 lines
Thanks for the quick feedback. Would this be better implemented as a fast path for
perf_evsel__read()? If so, how to get the mmap dataif it works for all events, which I'm not sure of
Quoted 1 line
which is associated with a evlist rather than a evsel?
not sure what you mean, you can mmap evsel, not evlist
While yes the mmap is created from an evsel fd, they are ultimately associated with the evlist struct and are per thread or cpu. If there's more than 1 evsel, then the additional ones are set to the 1st mmap with
PERF_EVENT_IOC_SET_OUTPUT. Which I now realize means this RFC only works for the first evsel. So I guess the API needs to work something like this:
Quoted 6 lines
threads =
perf_thread_map__new_dummy();perf_thread_map__set_pid(threads, 0, 0);evsel =
perf_evsel__new(&attr);perf_evsel__open(evsel, NULL, threads);
hum, I wonder we should remove maps from perf_evsel__open args and factor out some perf_evsel__set_map function..
Quoted 1 line
perf_evsel__mmap(evsel); <--- *new*
.. because you'll need those maps in here, right?
Quoted 9 lines
perf_evsel__read(evsel, 0, 0, &counts); // If we have an mmap, then try a direct readPerhaps some refactoring of the mmap code in
evlist.cwill be needed, but the usage seems pretty orthogonal. I'd propose that mmapping viaperf_evlist__mmap()behavior remain unchanged and direct access is not supported in that case.
seems ok to me, perf_evlist__mmap and perf_evsel__mmap would mean to mutually exclusive usages
thanks,
jirka
Peter Zijlstra
maintainerQuoted 9 lines · Rob Herring
x86 and arm64 can both support direct access of event counters in userspace. The access sequence is less than trivial and currently exists in perf test code (
tools/perf/arch/x86/tests/rdpmc.c) with copies in projects such as PAPI and libpfm4.Patches to add arm64 userspace support are pending[1].
For this RFC, looking for a yes, seems like a good idea, or no, go away we don't want this in libperf.
I'd like it lots better if you'd at least take an optimized version of this, also see this thread:
https://lkml.kernel.org/r/[email protected]
Also, I usually strip out all the multiplexing crud out (and use pinned counters), which saves a bunch.
static inline u64 mmap_read_pinned(void *addr)
{
struct perf_event_mmap_page *pc = addr;
u32 seq, idx, width = 0;
u64 count;
s64 pmc = 0;
do {
seq = pc->lock;
barrier();
idx = pc->index;
count = pc->offset;
if (pc->cap_user_rdpmc && idx) {
width = pc->pmc_width;
pmc = rdpmc(idx - 1);
}
barrier();
} while (pc->lock != seq);
if (idx) {
pmc <<= 64 - width;
pmc >>= 64 - width; /* shift right signed */
count += pmc;
}
return count;
}