libperf userspace counter access
v8 cover libperf userspace counter access
I'm resending just the libperf userspace counter access without the Arm bits so hopefully it can be picked up for 5.13. The Arm bits seem to be a never ending review filled with long periods of silence. :(
Prior versions are here[1][2][3][4][5][6][7][8].
Rob
[1] https://lore.kernel.org/r/[email protected]/
[2] https://lore.kernel.org/r/[email protected]/
[3] https://lore.kernel.org/r/[email protected]/
[4] https://lore.kernel.org/r/[email protected]/
[5] https://lore.kernel.org/r/[email protected]/
[6] https://lore.kernel.org/r/[email protected]/
[7] https://lore.kernel.org/r/[email protected]/
[8] https://lore.kernel.org/r/[email protected]/
Rob Herring (4):
tools/include: Add an initial math64.h
libperf: Add evsel mmap support
libperf: tests: Add support for verbose printing
libperf: Add support for user space counter access
tools/include/linux/math64.h | 75 ++++++++++++++++++++ tools/lib/perf/Documentation/libperf.txt | 3 + tools/lib/perf/evsel.c | 80 +++++++++++++++++++++ tools/lib/perf/include/internal/evsel.h | 1 + tools/lib/perf/include/internal/mmap.h | 3 + tools/lib/perf/include/internal/tests.h | 32 +++++++++ tools/lib/perf/include/perf/evsel.h | 3 + tools/lib/perf/libperf.map | 3 + tools/lib/perf/mmap.c | 88 ++++++++++++++++++++++++ tools/lib/perf/tests/Makefile | 6 +- tools/lib/perf/tests/test-evsel.c | 66 ++++++++++++++++++ 11 files changed, 358 insertions(+), 2 deletions(-) create mode 100644 tools/include/linux/math64.h
Signature · 1 line
2.27.0
PATCH v8 1/4 tools/include: Add an initial math64.h
Add an initial math64.h similar to linux/math64.h with functions mul_u64_u64_div64() and mul_u64_u32_shr(). This isn't a direct copy of include/linux/math64.h as that doesn't define mul_u64_u64_div64().
Implementation was written by Peter Zilkstra based on linux/math64.h and div64.h[1]. The original implementation was not optimal on arm64 as __int128 division is not optimal with a call out to __udivti3, so I dropped the __int128 variant of mul_u64_u64_div64().
[1] https://lore.kernel.org/lkml/[email protected]/
--- tools/include/linux/math64.h | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tools/include/linux/math64.h
tools/include/linux/math64.h new file +75 -0
@@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_MATH64_H +#define _LINUX_MATH64_H + +#include <linux/types.h> + +#ifdef __x86_64__ +static inline u64 mul_u64_u64_div64 (u64 a,u64 b,u64 c) +{ +u64 q; + + asm ("mulq %2; divq %3" :"=a" (q) + :"a" (a),"rm" (b),"rm" (c) + :"rdx" ); + +return q; +} +#define mul_u64_u64_div64 mul_u64_u64_div64 +#endif + +#ifdef __SIZEOF_INT128__ +static inline u64 mul_u64_u32_shr (u64 a,u32 b,unsigned int shift) +{ +return (u64)(((unsigned __int128 )a * b) >> shift); +} + +#else + +#ifdef __i386__ +static inline u64 mul_u32_u32 (u32 a,u32 b) +{ +u32 high, low; + + asm ("mull %[b]" :"=a" (low),"=d" (high) + : [a]"a" (a), [b]"rm" (b) ); + +return low | ((u64 )high) <<32 ; +} +#else +static inline u64 mul_u32_u32 (u32 a,u32 b) +{ +return (u64 )a * b; +} +#endif + +static inline u64 mul_u64_u32_shr (u64 a,u32 b,unsigned int shift) +{ +u32 ah, al; +u64 ret; + + al = a; + ah = a >>32 ; + + ret =mul_u32_u32 (al, b) >> shift; +if (ah) + ret +=mul_u32_u32 (ah, b) << (32 - shift); + +return ret; +} + +#endif /* __SIZEOF_INT128__ */ + +#ifndef mul_u64_u64_div64 +static inline u64 mul_u64_u64_div64 (u64 a,u64 b,u64 c) +{ +u64 quot, rem; + + quot = a / c; + rem = a % c; + +return quot * b + (rem * b) / c; +} +#endif + +#endif /* _LINUX_MATH64_H */
Signature · 1 line
2.27.0
Namhyung Kim
maintainerRe: [PATCH v8 2/4] libperf: Add evsel mmap support
Hello,
Review
Quoted 4 lines · Rob Herring
(quoted diff) +4
-0+void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu, int thread) +{ + if (FD(evsel, cpu, thread) < 0 || MMAP(evsel, cpu, thread) == NULL) + return NULL;
I think you should check the cpu and the thread is in a valid range. Currently xyarray__entry() simply accesses the content without checking the boundaries.
Thanks,
Namhyung
Quoted 49 lines
(quoted diff) +4
-0+ + return MMAP(evsel, cpu, thread)->base; +} + int perf_evsel__read_size(struct perf_evsel *evsel) { u64 read_format = evsel->attr.read_format;tools/lib/perf/include/internal/evsel.h +1
-0@@ -41,6 +41,7 @@ struct perf_evsel {struct perf_cpu_map *own_cpus;struct perf_thread_map *threads;struct xyarray *fd; +struct xyarray *mmap;struct xyarray *sample_id;u64 *id;u32 ids;tools/lib/perf/include/perf/evsel.h +3
-0@@ -27,6 +27,9 @@ LIBPERF_API int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map * struct perf_thread_map *threads); LIBPERF_API void perf_evsel__close(struct perf_evsel *evsel); LIBPERF_API void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu); +LIBPERF_API int perf_evsel__mmap(struct perf_evsel *evsel, int pages); +LIBPERF_API void perf_evsel__munmap(struct perf_evsel *evsel); +LIBPERF_API void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu, int thread); LIBPERF_API int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,struct perf_counts_values *count); LIBPERF_API int perf_evsel__enable(struct perf_evsel *evsel);tools/lib/perf/libperf.map +3
-1@@ -23,6 +23,9 @@ LIBPERF_0.0.1 { perf_evsel__disable; perf_evsel__open; perf_evsel__close; + perf_evsel__mmap; + perf_evsel__munmap; + perf_evsel__mmap_base; perf_evsel__read; perf_evsel__cpus; perf_evsel__threads; --2.27.0
Re: [PATCH v8 2/4] libperf: Add evsel mmap support
Quoted 4 lines · Namhyung Kim
Hello,
On Thu, Apr 15, 2021 at 1:07 AM Rob Herring <[email protected]> wrote:
Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu, int thread) +{ + if (FD(evsel, cpu, thread) < 0 || MMAP(evsel, cpu, thread) == NULL) + return NULL;I think you should check the cpu and the thread is in a valid range. Currently
xyarray__entry()simply accesses the content without checking the boundaries.
Happy to add a patch to do that if desired, but I think that's separate from this series. That would be something to add to xyarray__entry().
Rob
Namhyung Kim
maintainerRe: [PATCH v8 2/4] libperf: Add evsel mmap support
Quoted 15 lines · Rob Herring
Quoted 4 lines · Namhyung Kim
Hello,
On Thu, Apr 15, 2021 at 1:07 AM Rob Herring <[email protected]> wrote:
Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu, int thread) +{ + if (FD(evsel, cpu, thread) < 0 || MMAP(evsel, cpu, thread) == NULL) + return NULL;I think you should check the cpu and the thread is in a valid range. Currently
xyarray__entry()simply accesses the content without checking the boundaries.
Quoted 3 lines
Happy to add a patch to do that if desired, but I think that's separate from this series. That would be something to add to
xyarray__entry().
Sure, we can do that separately.
Thanks,
Namhyung
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v8 2/4] libperf: Add evsel mmap support
In reply to Namhyung KimEm Thu, Apr 15, 2021 at 01:41:35AM +0900, Namhyung Kim escreveu:
Quoted 11 lines
Hello,
Review
Quoted 4 lines · Rob Herring
(quoted diff) +4
-0+void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu, int thread) +{ + if (FD(evsel, cpu, thread) < 0 || MMAP(evsel, cpu, thread) == NULL) + return NULL;I think you should check the cpu and the thread is in a valid range. Currently
xyarray__entry()simply accesses the content without checking the boundaries.
So, since xyarray has the bounds, it should check it, i.e. we need to have a __xyarray__entry() that is what xyarray__entry() does, i.e. assume the values have been bounds checked, then a new method, xyarray__entry() that does bounds check, if it fails, return NULL, otherwise calls __xyarray__entry().
I see this is frustrating and I should've chimed in earlier, but at least now this is getting traction, and the end result will be better not just for the feature you've been dilligently working on,
Thank you for your persistence,
- Arnaldo
Quoted 53 lines
Thanks,
NamhyungQuoted 49 lines
(quoted diff) +4
-0+ + return MMAP(evsel, cpu, thread)->base; +} + int perf_evsel__read_size(struct perf_evsel *evsel) { u64 read_format = evsel->attr.read_format;tools/lib/perf/include/internal/evsel.h +1
-0@@ -41,6 +41,7 @@ struct perf_evsel {struct perf_cpu_map *own_cpus;struct perf_thread_map *threads;struct xyarray *fd; +struct xyarray *mmap;struct xyarray *sample_id;u64 *id;u32 ids;tools/lib/perf/include/perf/evsel.h +3
-0@@ -27,6 +27,9 @@ LIBPERF_API int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map * struct perf_thread_map *threads); LIBPERF_API void perf_evsel__close(struct perf_evsel *evsel); LIBPERF_API void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu); +LIBPERF_API int perf_evsel__mmap(struct perf_evsel *evsel, int pages); +LIBPERF_API void perf_evsel__munmap(struct perf_evsel *evsel); +LIBPERF_API void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu, int thread); LIBPERF_API int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,struct perf_counts_values *count); LIBPERF_API int perf_evsel__enable(struct perf_evsel *evsel);tools/lib/perf/libperf.map +3
-1@@ -23,6 +23,9 @@ LIBPERF_0.0.1 { perf_evsel__disable; perf_evsel__open; perf_evsel__close; + perf_evsel__mmap; + perf_evsel__munmap; + perf_evsel__mmap_base; perf_evsel__read; perf_evsel__cpus; perf_evsel__threads; --2.27.0
Signature · 1 line
- Arnaldo
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v8 2/4] libperf: Add evsel mmap support
Em Wed, Apr 14, 2021 at 03:02:08PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 19 lines
Em Thu, Apr 15, 2021 at 01:41:35AM +0900, Namhyung Kim escreveu:
Quoted 11 lines
Hello,
Review
Quoted 4 lines · Rob Herring
(quoted diff) +4
-0+void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu, int thread) +{ + if (FD(evsel, cpu, thread) < 0 || MMAP(evsel, cpu, thread) == NULL) + return NULL;I think you should check the cpu and the thread is in a valid range. Currently
xyarray__entry()simply accesses the content without checking the boundaries.So, since xyarray has the bounds, it should check it, i.e. we need to have a
__xyarray__entry()that is whatxyarray__entry()does, i.e. assume the values have been bounds checked, then a new method,xyarray__entry()that does bounds check, if it fails, return NULL, otherwise calls__xyarray__entry().
Quoted 5 lines
I see this is frustrating and I should've chimed in earlier, but at least now this is getting traction, and the end result will be better not just for the feature you've been dilligently working on,
Thank you for your persistence,
Re-reading, yeah, this can be done in a separate patch, Namhyung, can I have your Reviewed-by? That or an Acked-by?
- Arnaldo
Quoted 59 lines
- Arnaldo
Quoted 53 lines
Thanks,
NamhyungQuoted 49 lines
(quoted diff) +4
-0+ + return MMAP(evsel, cpu, thread)->base; +} + int perf_evsel__read_size(struct perf_evsel *evsel) { u64 read_format = evsel->attr.read_format;tools/lib/perf/include/internal/evsel.h +1
-0@@ -41,6 +41,7 @@ struct perf_evsel {struct perf_cpu_map *own_cpus;struct perf_thread_map *threads;struct xyarray *fd; +struct xyarray *mmap;struct xyarray *sample_id;u64 *id;u32 ids;tools/lib/perf/include/perf/evsel.h +3
-0@@ -27,6 +27,9 @@ LIBPERF_API int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map * struct perf_thread_map *threads); LIBPERF_API void perf_evsel__close(struct perf_evsel *evsel); LIBPERF_API void perf_evsel__close_cpu(struct perf_evsel *evsel, int cpu); +LIBPERF_API int perf_evsel__mmap(struct perf_evsel *evsel, int pages); +LIBPERF_API void perf_evsel__munmap(struct perf_evsel *evsel); +LIBPERF_API void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu, int thread); LIBPERF_API int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,struct perf_counts_values *count); LIBPERF_API int perf_evsel__enable(struct perf_evsel *evsel);tools/lib/perf/libperf.map +3
-1@@ -23,6 +23,9 @@ LIBPERF_0.0.1 { perf_evsel__disable; perf_evsel__open; perf_evsel__close; + perf_evsel__mmap; + perf_evsel__munmap; + perf_evsel__mmap_base; perf_evsel__read; perf_evsel__cpus; perf_evsel__threads; --2.27.0
Signature · 1 line
- Arnaldo
Signature · 1 line
- Arnaldo
Re: [PATCH v8 2/4] libperf: Add evsel mmap support
Quoted 21 lines · Arnaldo Carvalho de Melo
Em Wed, Apr 14, 2021 at 03:02:08PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 19 lines
Em Thu, Apr 15, 2021 at 01:41:35AM +0900, Namhyung Kim escreveu:
Quoted 11 lines
Hello,
Review
Quoted 4 lines · Rob Herring
(quoted diff) +4
-0+void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu, int thread) +{ + if (FD(evsel, cpu, thread) < 0 || MMAP(evsel, cpu, thread) == NULL) + return NULL;I think you should check the cpu and the thread is in a valid range. Currently
xyarray__entry()simply accesses the content without checking the boundaries.So, since xyarray has the bounds, it should check it, i.e. we need to have a
__xyarray__entry()that is whatxyarray__entry()does, i.e. assume the values have been bounds checked, then a new method,xyarray__entry()that does bounds check, if it fails, return NULL, otherwise calls__xyarray__entry().
Quoted 8 lines
Quoted 5 lines
I see this is frustrating and I should've chimed in earlier, but at least now this is getting traction, and the end result will be better not just for the feature you've been dilligently working on,
Thank you for your persistence,
Re-reading, yeah, this can be done in a separate patch, Namhyung, can I have your Reviewed-by? That or an Acked-by?
Sure, for the series:
Thanks,
Namhyung
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v8 2/4] libperf: Add evsel mmap support
Em Thu, Apr 15, 2021 at 04:14:31AM +0900, Namhyung Kim escreveu:
Quoted 27 lines
Quoted 25 lines · Arnaldo Carvalho de Melo
Em Wed, Apr 14, 2021 at 03:02:08PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 23 lines
Em Thu, Apr 15, 2021 at 01:41:35AM +0900, Namhyung Kim escreveu:
Quoted 11 lines
Hello,
Review
Quoted 4 lines · Rob Herring
(quoted diff) +4
-0+void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu, int thread) +{ + if (FD(evsel, cpu, thread) < 0 || MMAP(evsel, cpu, thread) == NULL) + return NULL;I think you should check the cpu and the thread is in a valid range. Currently
xyarray__entry()simply accesses the content without checking the boundaries.So, since xyarray has the bounds, it should check it, i.e. we need to have a
__xyarray__entry()that is whatxyarray__entry()does, i.e. assume the values have been bounds checked, then a new method,xyarray__entry()that does bounds check, if it fails, return NULL, otherwise calls__xyarray__entry().I see this is frustrating and I should've chimed in earlier, but at least now this is getting traction, and the end result will be better not just for the feature you've been dilligently working on,
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Thank you for your persistence,
Re-reading, yeah, this can be done in a separate patch, Namhyung, can I have your Reviewed-by? That or an Acked-by?
Sure, for the series:
Acked-by Namhyung Kim <[email protected]>
Ok, b4 failed on it, probably some missing Reply to, so I'll apply it by hand:
[acme@five perf]$ b4 am -t -s -l --cc-trailers [email protected] Looking up https://lore.kernel.org/r/20210414155412.3697605-1-robh%40kernel.org
Grabbing thread from lore.kernel.org/lkml
Analyzing 11 messages in the thread
---
Thread incomplete, attempting to backfill
---
Writing ./v8_20210414_robh_libperf_userspace_counter_access.mbx
[PATCH v8 1/4] tools/include: Add an initial math64.h
+ Acked-by: Namhyung Kim <[email protected]>
+ Acked-by: Jiri Olsa <[email protected]> (✓ DKIM/redhat.com)
+ Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
+ Link: https://lore.kernel.org/r/[email protected]
+ Cc: Catalin Marinas <[email protected]>
+ Cc: Mark Rutland <[email protected]>
+ Cc: Itaru Kitayama <[email protected]>
+ Cc: Arnaldo Carvalho de Melo <[email protected]>
+ Cc: Will Deacon <[email protected]>
+ Cc: Ingo Molnar <[email protected]>
+ Cc: [email protected]
ERROR: missing [2/4]!
[PATCH v8 3/4] libperf: tests: Add support for verbose printing
+ Acked-by: Jiri Olsa <[email protected]> (✓ DKIM/redhat.com)
+ Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
+ Link: https://lore.kernel.org/r/[email protected]
+ Cc: Catalin Marinas <[email protected]>
+ Cc: Mark Rutland <[email protected]>
+ Cc: Itaru Kitayama <[email protected]>
+ Cc: Peter Zijlstra <[email protected]>
+ Cc: Arnaldo Carvalho de Melo <[email protected]>
+ Cc: Namhyung Kim <[email protected]>
+ Cc: Will Deacon <[email protected]>
+ Cc: Ingo Molnar <[email protected]>
+ Cc: [email protected]
[PATCH v8 4/4] libperf: Add support for user space counter access
+ Acked-by: Jiri Olsa <[email protected]> (✓ DKIM/redhat.com)
+ Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
+ Link: https://lore.kernel.org/r/[email protected]
+ Cc: Catalin Marinas <[email protected]>
+ Cc: Mark Rutland <[email protected]>
+ Cc: Itaru Kitayama <[email protected]>
+ Cc: Peter Zijlstra <[email protected]>
+ Cc: Arnaldo Carvalho de Melo <[email protected]>
+ Cc: Namhyung Kim <[email protected]>
+ Cc: Will Deacon <[email protected]>
+ Cc: Ingo Molnar <[email protected]>
+ Cc: [email protected]
---
Total patches: 3
---
WARNING: Thread incomplete!
Cover: ./v8_20210414_robh_libperf_userspace_counter_access.cover
Base: not found
git am ./v8_20210414_robh_libperf_userspace_counter_access.mbx
[acme@five perf]$
Re: [PATCH v8 2/4] libperf: Add evsel mmap support
Quoted 34 lines · Arnaldo Carvalho de Melo
Em Thu, Apr 15, 2021 at 04:14:31AM +0900, Namhyung Kim escreveu:
Quoted 32 lines
Quoted 29 lines · Arnaldo Carvalho de Melo
Em Wed, Apr 14, 2021 at 03:02:08PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 19 lines
Em Thu, Apr 15, 2021 at 01:41:35AM +0900, Namhyung Kim escreveu:
Quoted 11 lines
Hello,
Review
Quoted 4 lines · Rob Herring
(quoted diff) +4
-0+void *perf_evsel__mmap_base(struct perf_evsel *evsel, int cpu, int thread) +{ + if (FD(evsel, cpu, thread) < 0 || MMAP(evsel, cpu, thread) == NULL) + return NULL;I think you should check the cpu and the thread is in a valid range. Currently
xyarray__entry()simply accesses the content without checking the boundaries.So, since xyarray has the bounds, it should check it, i.e. we need to have a
__xyarray__entry()that is whatxyarray__entry()does, i.e. assume the values have been bounds checked, then a new method,xyarray__entry()that does bounds check, if it fails, return NULL, otherwise calls__xyarray__entry().Quoted 5 lines
I see this is frustrating and I should've chimed in earlier, but at least now this is getting traction, and the end result will be better not just for the feature you've been dilligently working on,
Thank you for your persistence,
Re-reading, yeah, this can be done in a separate patch, Namhyung, can I have your Reviewed-by? That or an Acked-by?
Quoted 6 lines
Quoted 3 lines
Sure, for the series:
Acked-by Namhyung Kim <[email protected]>Ok, b4 failed on it, probably some missing Reply to, so I'll apply it by hand:
That's my fault. A duplicate message-id is the issue. git-send-email died after patch 1/4 (can't say I've ever had that happen). So in my attempt to manually resend 2-4, I was off by 1 in the message-id and duplicated patch 1's message-id. I should have just resent the whole thing.
Rob
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v8 2/4] libperf: Add evsel mmap support
Em Thu, Apr 15, 2021 at 03:09:28PM -0500, Rob Herring escreveu:
Quoted 9 lines · Arnaldo Carvalho de Melo
Quoted 2 lines
Ok, b4 failed on it, probably some missing Reply to, so I'll apply it by hand:
That's my fault. A duplicate message-id is the issue. git-send-email died after patch 1/4 (can't say I've ever had that happen). So in my attempt to manually resend 2-4, I was off by 1 in the message-id and duplicated patch 1's message-id. I should have just resent the whole thing.
No problem, it is already in, just letting you know to fix your scripts :-)
- Arnaldo
PATCH v8 3/4 libperf: tests: Add support for verbose printing
In reply to Rob HerringAdd __T_VERBOSE() so tests can add verbose output. The verbose output is enabled with the '-v' command line option. Running 'make tests V=1' will enable the '-v' option when running the tests.
---
v8:
- Update commit msg with running 'make tests V=1'.
v5:
- Pass verbose flag to static tests
- Fix getopt loop with unsigned char (arm64)
v3:
- New patch
--- tools/lib/perf/include/internal/tests.h | 32 +++++++++++++++++++++++++ tools/lib/perf/tests/Makefile | 6 +++-- 2 files changed, 36 insertions(+), 2 deletions(-)
tools/lib/perf/include/internal/tests.h +32 -0
@@ -3,11 +3,32 @@#define __LIBPERF_INTERNAL_TESTS_H#include <stdio.h> +#include <unistd.h> int tests_failed; +int tests_verbose; + +static inline int get_verbose (char **argv,int argc) +{ +int c; +int verbose =0 ; + +while ((c =getopt (argc, argv,"v" )) !=-1 ) { +switch (c) + { +case 'v' : + verbose =1 ; +break ; +default : +break ; + } + } +return verbose; +}#define __T_START \ do { \ + tests_verbose = get_verbose(argv, argc); \ fprintf(stdout, "- running %s...", __FILE__); \ fflush(NULL); \ tests_failed = 0; \
@@ -30,4 +51,15 @@ do { } \ } while (0) +#define __T_VERBOSE (...) \ +do { \ + if (tests_verbose) { \ + if (tests_verbose == 1) { \ + fputc('\n', stderr); \ + tests_verbose++; \ + } \ + fprintf(stderr, ##__VA_ARGS__); \ + } \ +} while (0) +#endif /* __LIBPERF_INTERNAL_TESTS_H */
tools/lib/perf/tests/Makefile +4 -2
@@ -5,6 +5,8 @@ TESTS = test-cpumap test-threadmap test-evlist test-evsel TESTS_SO := $(addsuffix -so,$(TESTS)) TESTS_A := $(addsuffix -a,$(TESTS)) +TEST_ARGS := $(if $(V),-v) + # Set compile option CFLAGS ifdef EXTRA_CFLAGS CFLAGS := $(EXTRA_CFLAGS)
@@ -28,9 +30,9 @@ all: $(TESTS_A) $(TESTS_SO) run: @echo "running static:" - @for i in $(TESTS_A); do ./$$i; done + @for i in $(TESTS_A); do ./$$i $(TEST_ARGS); done @echo "running dynamic:" - @for i in $(TESTS_SO); do LD_LIBRARY_PATH=../ ./$$i; done + @for i in $(TESTS_SO); do LD_LIBRARY_PATH=../ ./$$i $(TEST_ARGS); done clean: $(call QUIET_CLEAN, tests)$(RM) $(TESTS_A) $(TESTS_SO)
--
2.27.0
PATCH v8 4/4 libperf: Add support for user space counter access
In reply to Rob Herringx86 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.
In order to support usersapce access, an event must be mmapped first with perf_evsel__mmap(). Then subsequent calls to perf_evsel__read() will use the fast path (assuming the arch supports it).
---
v8:
- Add call to perf_evsel_munmap() in evsel test
v6:
- Adapt to mmap changes adding MMAP NULL check
v5:
- Make raw count s64 instead of u64 so that counter width shifting works
- Adapt to mmap changes
v4:
- Update perf_evsel__mmap size to pages
v3:
- Split out perf_evsel__mmap() to separate patch
--- tools/lib/perf/evsel.c | 4 ++ tools/lib/perf/include/internal/mmap.h | 3 + tools/lib/perf/mmap.c | 88 ++++++++++++++++++++++++++ tools/lib/perf/tests/test-evsel.c | 66 +++++++++++++++++++ 4 files changed, 161 insertions(+)
tools/lib/perf/evsel.c +4 -0
@@ -267,6 +267,10 @@ int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,if (FD (evsel, cpu, thread) <0 )return -EINVAL ; +if (MMAP (evsel, cpu, thread) && + !perf_mmap__read_self (MMAP (evsel, cpu, thread), count)) +return 0 ; +if (readn (FD (evsel, cpu, thread), count->values, size) <=0 )return -errno;
tools/lib/perf/include/internal/mmap.h +3 -0
@@ -11,6 +11,7 @@#define PERF_SAMPLE_MAX_SIZE (1 << 16)struct perf_mmap ; +struct perf_counts_values ;typedef void (*libperf_unmap_cb_t )(struct perf_mmap *map);
@@ -52,4 +53,6 @@ void perf_mmap__put(struct perf_mmap *map);u64 perf_mmap__read_head (struct perf_mmap *map); +int perf_mmap__read_self (struct perf_mmap *map,struct perf_counts_values *count); +#endif /* __LIBPERF_INTERNAL_MMAP_H */
tools/lib/perf/mmap.c +88 -0
100 lines not on this page · raw message
tools/lib/perf/tests/test-evsel.c +66 -0
78 lines not on this page · raw message
--
2.27.0
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v8 4/4] libperf: Add support for user space counter access
Em Wed, Apr 14, 2021 at 11:07:39AM -0500, Rob Herring escreveu:
Quoted 8 lines
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.In order to support usersapce access, an event must be mmapped first with
perf_evsel__mmap(). Then subsequent calls toperf_evsel__read()will use the fast path (assuming the arch supports it).
Had to apply this to fix the build on the other arches:
Quoted 12 lines
(quoted diff) +12
-0+#if defined(__i386__) || defined(__x86_64__) +static u64 read_perf_counter(unsigned int counter) +{ + unsigned int low, high; + + asm volatile("rdpmc" : "=a" (low), "=d" (high) : "c" (counter)); + + return low | ((u64)high) << 32; +} + +static u64 read_timestamp(void) +{
Review
Quoted 10 lines
(quoted diff) +10
-0+ unsigned int low, high; + + asm volatile("rdtsc" : "=a" (low), "=d" (high)); + + return low | ((u64)high) << 32; +} +#else +static u64 read_perf_counter(unsigned int counter) { return 0; } +static u64 read_timestamp(void) { return 0; } +#endif
tools/lib/perf/mmap.c +1 -1
@@ -295,7 +295,7 @@ static u64 read_timestamp(void) return low | ((u64)high) << 32; }#else -static u64 read_perf_counter (unsigned int counter) {return 0 ; } +static u64 read_perf_counter (unsigned int counter__maybe_unused ) {return 0 ; }static u64 read_timestamp (void ) {return 0 ; }#endif
Quoted 17 lines · Rob Herring
I'm resending just the libperf userspace counter access without the Arm bits so hopefully it can be picked up for 5.13. The Arm bits seem to be a never ending review filled with long periods of silence. :(
Prior versions are here[1][2][3][4][5][6][7][8].
Rob
[1] https://lore.kernel.org/r/[email protected]/
[2] https://lore.kernel.org/r/[email protected]/
[3] https://lore.kernel.org/r/[email protected]/
[4] https://lore.kernel.org/r/[email protected]/
[5] https://lore.kernel.org/r/[email protected]/
[6] https://lore.kernel.org/r/[email protected]/
[7] https://lore.kernel.org/r/[email protected]/
[8] https://lore.kernel.org/r/[email protected]/
Quoted 5 lines
Rob Herring (4):
tools/include: Add an initialmath64.h
libperf: Add evsel mmap support
libperf: tests: Add support for verbose printing
libperf: Add support for user space counter access
thanks,
jirka
Quoted 18 lines
tools/include/linux/math64.h | 75 ++++++++++++++++++++ tools/lib/perf/Documentation/libperf.txt | 3 + tools/lib/perf/evsel.c | 80 +++++++++++++++++++++ tools/lib/perf/include/internal/evsel.h | 1 + tools/lib/perf/include/internal/mmap.h | 3 + tools/lib/perf/include/internal/tests.h | 32 +++++++++ tools/lib/perf/include/perf/evsel.h | 3 + tools/lib/perf/libperf.map | 3 + tools/lib/perf/mmap.c | 88 ++++++++++++++++++++++++ tools/lib/perf/tests/Makefile | 6 +- tools/lib/perf/tests/test-evsel.c | 66 ++++++++++++++++++ 11 files changed, 358 insertions(+), 2 deletions(-) create mode 100644 tools/include/linux/math64.hSignature · 1 line
2.27.0