diff mbox series

[v7,7/9] perf: arm64: Add test for userspace counter access on heterogeneous systems

Message ID 20210420031511.2348977-8-robh@kernel.org (mailing list archive)
State New, archived
Headers show
Series arm64 userspace counter access support | expand

Commit Message

Rob Herring (Arm) April 20, 2021, 3:15 a.m. UTC
Userspace counter access only works on heterogeneous systems with some
restrictions. The userspace process must be pinned to a homogeneous
subset of CPUs and must open the corresponding PMU for those CPUs. This
commit adds a test implementing these requirements.

Signed-off-by: Rob Herring <robh@kernel.org>
---
v6:
 - Add a check on cap_user_rdpmc
v5:
 - Adapt to libperf mmap API changes
v4:
 - Update perf_evsel__mmap params
v2:
 - Drop all but heterogeneous test as others covered by libperf tests
 - Rework to use libperf
---
 tools/perf/arch/arm64/include/arch-tests.h |   7 +
 tools/perf/arch/arm64/tests/Build          |   1 +
 tools/perf/arch/arm64/tests/arch-tests.c   |   4 +
 tools/perf/arch/arm64/tests/user-events.c  | 177 +++++++++++++++++++++
 4 files changed, 189 insertions(+)
 create mode 100644 tools/perf/arch/arm64/tests/user-events.c

Comments

Masayoshi Mizuma April 30, 2021, 4:46 p.m. UTC | #1
On Mon, Apr 19, 2021 at 10:15:09PM -0500, Rob Herring wrote:
> Userspace counter access only works on heterogeneous systems with some
> restrictions. The userspace process must be pinned to a homogeneous
> subset of CPUs and must open the corresponding PMU for those CPUs. This
> commit adds a test implementing these requirements.
> 
> Signed-off-by: Rob Herring <robh@kernel.org>
> ---
> v6:
>  - Add a check on cap_user_rdpmc
> v5:
>  - Adapt to libperf mmap API changes
> v4:
>  - Update perf_evsel__mmap params
> v2:
>  - Drop all but heterogeneous test as others covered by libperf tests
>  - Rework to use libperf
> ---
>  tools/perf/arch/arm64/include/arch-tests.h |   7 +
>  tools/perf/arch/arm64/tests/Build          |   1 +
>  tools/perf/arch/arm64/tests/arch-tests.c   |   4 +
>  tools/perf/arch/arm64/tests/user-events.c  | 177 +++++++++++++++++++++
>  4 files changed, 189 insertions(+)
>  create mode 100644 tools/perf/arch/arm64/tests/user-events.c
> 
> diff --git a/tools/perf/arch/arm64/include/arch-tests.h b/tools/perf/arch/arm64/include/arch-tests.h
> index 90ec4c8cb880..380ad34a3f09 100644
> --- a/tools/perf/arch/arm64/include/arch-tests.h
> +++ b/tools/perf/arch/arm64/include/arch-tests.h
> @@ -2,11 +2,18 @@
>  #ifndef ARCH_TESTS_H
>  #define ARCH_TESTS_H
>  
> +#include <linux/compiler.h>
> +
>  #ifdef HAVE_DWARF_UNWIND_SUPPORT
>  struct thread;
>  struct perf_sample;
> +int test__arch_unwind_sample(struct perf_sample *sample,
> +			     struct thread *thread);
>  #endif

Hello,

I got the following compile error with aarch64 on Fedora33.

    # make tools/perf
    ...
    In file included from arch/arm64/tests/arch-tests.c:4:
    /root//libperf_v7/tools/perf/arch/arm64/include/arch-tests.h:10:5: error: redundant redeclaration of ‘test__arch_unwind_sample’ [-Werror=redundant-decls]
       10 | int test__arch_unwind_sample(struct perf_sample *sample,
          |     ^~~~~~~~~~~~~~~~~~~~~~~~
    In file included from arch/arm64/tests/arch-tests.c:3:
    /root//libperf_v7/tools/perf/tests/tests.h:140:5: note: previous declaration of ‘test__arch_unwind_sample’ was here
      140 | int test__arch_unwind_sample(struct perf_sample *sample,
          |     ^~~~~~~~~~~~~~~~~~~~~~~~
    cc1: all warnings being treated as errors
    make[8]: *** [/root//libperf_v7/tools/build/Makefile.build:97: /root/libperf_v7/tools/perf/arch/arm64/tests/arch-tests.o] Error 1
    make[8]: *** Waiting for unfinished jobs....
    In file included from arch/arm64/tests/user-events.c:13:
    /root//libperf_v7/tools/perf/arch/arm64/include/arch-tests.h:10:5: error: redundant redeclaration of ‘test__arch_unwind_sample’ [-Werror=redundant-decls]
       10 | int test__arch_unwind_sample(struct perf_sample *sample,
          |     ^~~~~~~~~~~~~~~~~~~~~~~~
    In file included from arch/arm64/tests/user-events.c:12:
    /root//libperf_v7/tools/perf/tests/tests.h:140:5: note: previous declaration of ‘test__arch_unwind_sample’ was here
      140 | int test__arch_unwind_sample(struct perf_sample *sample,
          |     ^~~~~~~~~~~~~~~~~~~~~~~~
    cc1: all warnings being treated as errors
    ...

The error is gone after the following patch is applied.

---
 tools/perf/arch/arm64/include/arch-tests.h | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/tools/perf/arch/arm64/include/arch-tests.h b/tools/perf/arch/arm64/include/arch-tests.h
index ddfa7460e..7ff2e29bd 100644
--- a/tools/perf/arch/arm64/include/arch-tests.h
+++ b/tools/perf/arch/arm64/include/arch-tests.h
@@ -4,13 +4,6 @@
 
 #include <linux/compiler.h>
 
-#ifdef HAVE_DWARF_UNWIND_SUPPORT
-struct thread;
-struct perf_sample;
-int test__arch_unwind_sample(struct perf_sample *sample,
-			     struct thread *thread);
-#endif
-
 extern struct test arch_tests[];
 int test__rd_pinned(struct test __maybe_unused *test,
 		       int __maybe_unused subtest);
Rob Herring (Arm) April 30, 2021, 6:17 p.m. UTC | #2
On Fri, Apr 30, 2021 at 11:46 AM Masayoshi Mizuma <msys.mizuma@gmail.com> wrote:
>
> On Mon, Apr 19, 2021 at 10:15:09PM -0500, Rob Herring wrote:
> > Userspace counter access only works on heterogeneous systems with some
> > restrictions. The userspace process must be pinned to a homogeneous
> > subset of CPUs and must open the corresponding PMU for those CPUs. This
> > commit adds a test implementing these requirements.
> >
> > Signed-off-by: Rob Herring <robh@kernel.org>
> > ---
> > v6:
> >  - Add a check on cap_user_rdpmc
> > v5:
> >  - Adapt to libperf mmap API changes
> > v4:
> >  - Update perf_evsel__mmap params
> > v2:
> >  - Drop all but heterogeneous test as others covered by libperf tests
> >  - Rework to use libperf
> > ---
> >  tools/perf/arch/arm64/include/arch-tests.h |   7 +
> >  tools/perf/arch/arm64/tests/Build          |   1 +
> >  tools/perf/arch/arm64/tests/arch-tests.c   |   4 +
> >  tools/perf/arch/arm64/tests/user-events.c  | 177 +++++++++++++++++++++
> >  4 files changed, 189 insertions(+)
> >  create mode 100644 tools/perf/arch/arm64/tests/user-events.c
> >
> > diff --git a/tools/perf/arch/arm64/include/arch-tests.h b/tools/perf/arch/arm64/include/arch-tests.h
> > index 90ec4c8cb880..380ad34a3f09 100644
> > --- a/tools/perf/arch/arm64/include/arch-tests.h
> > +++ b/tools/perf/arch/arm64/include/arch-tests.h
> > @@ -2,11 +2,18 @@
> >  #ifndef ARCH_TESTS_H
> >  #define ARCH_TESTS_H
> >
> > +#include <linux/compiler.h>
> > +
> >  #ifdef HAVE_DWARF_UNWIND_SUPPORT
> >  struct thread;
> >  struct perf_sample;
> > +int test__arch_unwind_sample(struct perf_sample *sample,
> > +                          struct thread *thread);
> >  #endif
>
> Hello,
>
> I got the following compile error with aarch64 on Fedora33.
>
>     # make tools/perf
>     ...
>     In file included from arch/arm64/tests/arch-tests.c:4:
>     /root//libperf_v7/tools/perf/arch/arm64/include/arch-tests.h:10:5: error: redundant redeclaration of ‘test__arch_unwind_sample’ [-Werror=redundant-decls]
>        10 | int test__arch_unwind_sample(struct perf_sample *sample,
>           |     ^~~~~~~~~~~~~~~~~~~~~~~~
>     In file included from arch/arm64/tests/arch-tests.c:3:
>     /root//libperf_v7/tools/perf/tests/tests.h:140:5: note: previous declaration of ‘test__arch_unwind_sample’ was here
>       140 | int test__arch_unwind_sample(struct perf_sample *sample,
>           |     ^~~~~~~~~~~~~~~~~~~~~~~~
>     cc1: all warnings being treated as errors
>     make[8]: *** [/root//libperf_v7/tools/build/Makefile.build:97: /root/libperf_v7/tools/perf/arch/arm64/tests/arch-tests.o] Error 1
>     make[8]: *** Waiting for unfinished jobs....
>     In file included from arch/arm64/tests/user-events.c:13:
>     /root//libperf_v7/tools/perf/arch/arm64/include/arch-tests.h:10:5: error: redundant redeclaration of ‘test__arch_unwind_sample’ [-Werror=redundant-decls]
>        10 | int test__arch_unwind_sample(struct perf_sample *sample,
>           |     ^~~~~~~~~~~~~~~~~~~~~~~~
>     In file included from arch/arm64/tests/user-events.c:12:
>     /root//libperf_v7/tools/perf/tests/tests.h:140:5: note: previous declaration of ‘test__arch_unwind_sample’ was here
>       140 | int test__arch_unwind_sample(struct perf_sample *sample,
>           |     ^~~~~~~~~~~~~~~~~~~~~~~~
>     cc1: all warnings being treated as errors
>     ...
>
> The error is gone after the following patch is applied.

Thanks. Honestly, I'm not sure why it was there in the first place.
Looking at the git history and this series history doesn't give any
clues.

> ---
>  tools/perf/arch/arm64/include/arch-tests.h | 7 -------
>  1 file changed, 7 deletions(-)
>
> diff --git a/tools/perf/arch/arm64/include/arch-tests.h b/tools/perf/arch/arm64/include/arch-tests.h
> index ddfa7460e..7ff2e29bd 100644
> --- a/tools/perf/arch/arm64/include/arch-tests.h
> +++ b/tools/perf/arch/arm64/include/arch-tests.h
> @@ -4,13 +4,6 @@
>
>  #include <linux/compiler.h>
>
> -#ifdef HAVE_DWARF_UNWIND_SUPPORT
> -struct thread;
> -struct perf_sample;
> -int test__arch_unwind_sample(struct perf_sample *sample,
> -                            struct thread *thread);
> -#endif
> -
>  extern struct test arch_tests[];
>  int test__rd_pinned(struct test __maybe_unused *test,
>                        int __maybe_unused subtest);
> --
>
> Thanks!
> Masa
>
> >
> >  extern struct test arch_tests[];
> > +int test__rd_pinned(struct test __maybe_unused *test,
> > +                    int __maybe_unused subtest);
> > +
> >
> >  #endif
> > diff --git a/tools/perf/arch/arm64/tests/Build b/tools/perf/arch/arm64/tests/Build
> > index a61c06bdb757..3f9a20c17fc6 100644
> > --- a/tools/perf/arch/arm64/tests/Build
> > +++ b/tools/perf/arch/arm64/tests/Build
> > @@ -1,4 +1,5 @@
> >  perf-y += regs_load.o
> >  perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
> >
> > +perf-y += user-events.o
> >  perf-y += arch-tests.o
> > diff --git a/tools/perf/arch/arm64/tests/arch-tests.c b/tools/perf/arch/arm64/tests/arch-tests.c
> > index 5b1543c98022..80ce7bd3c16d 100644
> > --- a/tools/perf/arch/arm64/tests/arch-tests.c
> > +++ b/tools/perf/arch/arm64/tests/arch-tests.c
> > @@ -10,6 +10,10 @@ struct test arch_tests[] = {
> >               .func = test__dwarf_unwind,
> >       },
> >  #endif
> > +     {
> > +             .desc = "Pinned CPU user counter access",
> > +             .func = test__rd_pinned,
> > +     },
> >       {
> >               .func = NULL,
> >       },
> > diff --git a/tools/perf/arch/arm64/tests/user-events.c b/tools/perf/arch/arm64/tests/user-events.c
> > new file mode 100644
> > index 000000000000..c8efc6b369e6
> > --- /dev/null
> > +++ b/tools/perf/arch/arm64/tests/user-events.c
> > @@ -0,0 +1,177 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +#include <unistd.h>
> > +#include <sched.h>
> > +#include <cpumap.h>
> > +
> > +#include <perf/core.h>
> > +#include <perf/threadmap.h>
> > +#include <perf/evsel.h>
> > +
> > +#include "pmu.h"
> > +#include "debug.h"
> > +#include "tests/tests.h"
> > +#include "arch-tests.h"
> > +
> > +static int run_test(struct perf_evsel *evsel)
> > +{
> > +     int n;
> > +     volatile int tmp = 0;
> > +     u64 delta, i, loops = 1000;
> > +     struct perf_counts_values counts = { .val = 0 };
> > +
> > +     for (n = 0; n < 6; n++) {
> > +             u64 stamp, now;
> > +
> > +             perf_evsel__read(evsel, 0, 0, &counts);
> > +             stamp = counts.val;
> > +
> > +             for (i = 0; i < loops; i++)
> > +                     tmp++;
> > +
> > +             perf_evsel__read(evsel, 0, 0, &counts);
> > +             now = counts.val;
> > +             loops *= 10;
> > +
> > +             delta = now - stamp;
> > +             pr_debug("%14d: %14llu\n", n, (long long)delta);
> > +
> > +             if (!delta)
> > +                     break;
> > +     }
> > +     return delta ? 0 : -1;
> > +}
> > +
> > +static struct perf_pmu *pmu_for_cpu(int cpu)
> > +{
> > +     int acpu, idx;
> > +     struct perf_pmu *pmu = NULL;
> > +
> > +     while ((pmu = perf_pmu__scan(pmu)) != NULL) {
> > +             if (pmu->is_uncore)
> > +                     continue;
> > +             perf_cpu_map__for_each_cpu(acpu, idx, pmu->cpus)
> > +                     if (acpu == cpu)
> > +                             return pmu;
> > +     }
> > +     return NULL;
> > +}
> > +
> > +static bool pmu_is_homogeneous(void)
> > +{
> > +     int core_cnt = 0;
> > +     struct perf_pmu *pmu = NULL;
> > +
> > +     while ((pmu = perf_pmu__scan(pmu)) != NULL) {
> > +             if (!pmu->is_uncore && !perf_cpu_map__empty(pmu->cpus))
> > +                     core_cnt++;
> > +     }
> > +     return core_cnt == 1;
> > +}
> > +
> > +static int libperf_print(enum libperf_print_level level,
> > +                      const char *fmt, va_list ap)
> > +{
> > +     (void)level;
> > +     return vfprintf(stderr, fmt, ap);
> > +}
> > +
> > +static struct perf_evsel *perf_init(struct perf_event_attr *attr)
> > +{
> > +     int err;
> > +     struct perf_thread_map *threads;
> > +     struct perf_evsel *evsel;
> > +     struct perf_event_mmap_page *pc;
> > +
> > +     libperf_init(libperf_print);
> > +
> > +     threads = perf_thread_map__new_dummy();
> > +     if (!threads) {
> > +             pr_err("failed to create threads\n");
> > +             return NULL;
> > +     }
> > +
> > +     perf_thread_map__set_pid(threads, 0, 0);
> > +
> > +     evsel = perf_evsel__new(attr);
> > +     if (!evsel) {
> > +             pr_err("failed to create evsel\n");
> > +             goto out_thread;
> > +     }
> > +
> > +     err = perf_evsel__open(evsel, NULL, threads);
> > +     if (err) {
> > +             pr_err("failed to open evsel\n");
> > +             goto out_open;
> > +     }
> > +
> > +     if (perf_evsel__mmap(evsel, 0)) {
> > +             pr_err("failed to mmap evsel\n");
> > +             goto out_mmap;
> > +     }
> > +
> > +     pc = perf_evsel__mmap_base(evsel, 0, 0);
> > +     if (!pc->cap_user_rdpmc) {
> > +             pr_err("userspace access not enabled\n");
> > +             goto out_mmap;
> > +     }
> > +
> > +     return evsel;
> > +
> > +out_mmap:
> > +     perf_evsel__close(evsel);
> > +out_open:
> > +     perf_evsel__delete(evsel);
> > +out_thread:
> > +     perf_thread_map__put(threads);
> > +     return NULL;
> > +}
> > +
> > +int test__rd_pinned(struct test __maybe_unused *test,
> > +                 int __maybe_unused subtest)
> > +{
> > +     int cpu, cputmp, ret = -1;
> > +     struct perf_evsel *evsel;
> > +     struct perf_event_attr attr = {
> > +             .config = 0x8, /* Instruction count */
> > +             .config1 = 0, /* 32-bit counter */
> > +             .exclude_kernel = 1,
> > +     };
> > +     cpu_set_t cpu_set;
> > +     struct perf_pmu *pmu;
> > +
> > +     if (pmu_is_homogeneous())
> > +             return TEST_SKIP;
> > +
> > +     cpu = sched_getcpu();
> > +     pmu = pmu_for_cpu(cpu);
> > +     if (!pmu)
> > +             return -1;
> > +     attr.type = pmu->type;
> > +
> > +     CPU_ZERO(&cpu_set);
> > +     perf_cpu_map__for_each_cpu(cpu, cputmp, pmu->cpus)
> > +             CPU_SET(cpu, &cpu_set);
> > +     if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0)
> > +             pr_err("Could not set affinity\n");
> > +
> > +     evsel = perf_init(&attr);
> > +     if (!evsel)
> > +             return -1;
> > +
> > +     perf_cpu_map__for_each_cpu(cpu, cputmp, pmu->cpus) {
> > +             CPU_ZERO(&cpu_set);
> > +             CPU_SET(cpu, &cpu_set);
> > +             if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0)
> > +                     pr_err("Could not set affinity\n");
> > +
> > +             pr_debug("Running on CPU %d\n", cpu);
> > +
> > +             ret = run_test(evsel);
> > +             if (ret)
> > +                     break;
> > +     }
> > +
> > +     perf_evsel__close(evsel);
> > +     perf_evsel__delete(evsel);
> > +     return ret;
> > +}
> > --
> > 2.27.0
> >
> >
> > _______________________________________________
> > linux-arm-kernel mailing list
> > linux-arm-kernel@lists.infradead.org
> > http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Rob Herring (Arm) April 30, 2021, 6:20 p.m. UTC | #3
On Fri, Apr 30, 2021 at 1:17 PM Rob Herring <robh@kernel.org> wrote:
>
> On Fri, Apr 30, 2021 at 11:46 AM Masayoshi Mizuma <msys.mizuma@gmail.com> wrote:
> >
> > On Mon, Apr 19, 2021 at 10:15:09PM -0500, Rob Herring wrote:
> > > Userspace counter access only works on heterogeneous systems with some
> > > restrictions. The userspace process must be pinned to a homogeneous
> > > subset of CPUs and must open the corresponding PMU for those CPUs. This
> > > commit adds a test implementing these requirements.
> > >
> > > Signed-off-by: Rob Herring <robh@kernel.org>
> > > ---
> > > v6:
> > >  - Add a check on cap_user_rdpmc
> > > v5:
> > >  - Adapt to libperf mmap API changes
> > > v4:
> > >  - Update perf_evsel__mmap params
> > > v2:
> > >  - Drop all but heterogeneous test as others covered by libperf tests
> > >  - Rework to use libperf
> > > ---
> > >  tools/perf/arch/arm64/include/arch-tests.h |   7 +
> > >  tools/perf/arch/arm64/tests/Build          |   1 +
> > >  tools/perf/arch/arm64/tests/arch-tests.c   |   4 +
> > >  tools/perf/arch/arm64/tests/user-events.c  | 177 +++++++++++++++++++++
> > >  4 files changed, 189 insertions(+)
> > >  create mode 100644 tools/perf/arch/arm64/tests/user-events.c
> > >
> > > diff --git a/tools/perf/arch/arm64/include/arch-tests.h b/tools/perf/arch/arm64/include/arch-tests.h
> > > index 90ec4c8cb880..380ad34a3f09 100644
> > > --- a/tools/perf/arch/arm64/include/arch-tests.h
> > > +++ b/tools/perf/arch/arm64/include/arch-tests.h
> > > @@ -2,11 +2,18 @@
> > >  #ifndef ARCH_TESTS_H
> > >  #define ARCH_TESTS_H
> > >
> > > +#include <linux/compiler.h>
> > > +
> > >  #ifdef HAVE_DWARF_UNWIND_SUPPORT
> > >  struct thread;
> > >  struct perf_sample;
> > > +int test__arch_unwind_sample(struct perf_sample *sample,
> > > +                          struct thread *thread);
> > >  #endif
> >
> > Hello,
> >
> > I got the following compile error with aarch64 on Fedora33.
> >
> >     # make tools/perf
> >     ...
> >     In file included from arch/arm64/tests/arch-tests.c:4:
> >     /root//libperf_v7/tools/perf/arch/arm64/include/arch-tests.h:10:5: error: redundant redeclaration of ‘test__arch_unwind_sample’ [-Werror=redundant-decls]
> >        10 | int test__arch_unwind_sample(struct perf_sample *sample,
> >           |     ^~~~~~~~~~~~~~~~~~~~~~~~
> >     In file included from arch/arm64/tests/arch-tests.c:3:
> >     /root//libperf_v7/tools/perf/tests/tests.h:140:5: note: previous declaration of ‘test__arch_unwind_sample’ was here
> >       140 | int test__arch_unwind_sample(struct perf_sample *sample,
> >           |     ^~~~~~~~~~~~~~~~~~~~~~~~
> >     cc1: all warnings being treated as errors
> >     make[8]: *** [/root//libperf_v7/tools/build/Makefile.build:97: /root/libperf_v7/tools/perf/arch/arm64/tests/arch-tests.o] Error 1
> >     make[8]: *** Waiting for unfinished jobs....
> >     In file included from arch/arm64/tests/user-events.c:13:
> >     /root//libperf_v7/tools/perf/arch/arm64/include/arch-tests.h:10:5: error: redundant redeclaration of ‘test__arch_unwind_sample’ [-Werror=redundant-decls]
> >        10 | int test__arch_unwind_sample(struct perf_sample *sample,
> >           |     ^~~~~~~~~~~~~~~~~~~~~~~~
> >     In file included from arch/arm64/tests/user-events.c:12:
> >     /root//libperf_v7/tools/perf/tests/tests.h:140:5: note: previous declaration of ‘test__arch_unwind_sample’ was here
> >       140 | int test__arch_unwind_sample(struct perf_sample *sample,
> >           |     ^~~~~~~~~~~~~~~~~~~~~~~~
> >     cc1: all warnings being treated as errors
> >     ...
> >
> > The error is gone after the following patch is applied.
>
> Thanks. Honestly, I'm not sure why it was there in the first place.
> Looking at the git history and this series history doesn't give any
> clues.

Well, except that both x86 and powerpc have the same hunk in their
arch-tests.h. Do you see errors on those arches?

Rob
Masayoshi Mizuma April 30, 2021, 8:16 p.m. UTC | #4
On Fri, Apr 30, 2021 at 01:20:58PM -0500, Rob Herring wrote:
> On Fri, Apr 30, 2021 at 1:17 PM Rob Herring <robh@kernel.org> wrote:
> >
> > On Fri, Apr 30, 2021 at 11:46 AM Masayoshi Mizuma <msys.mizuma@gmail.com> wrote:
> > >
> > > On Mon, Apr 19, 2021 at 10:15:09PM -0500, Rob Herring wrote:
> > > > Userspace counter access only works on heterogeneous systems with some
> > > > restrictions. The userspace process must be pinned to a homogeneous
> > > > subset of CPUs and must open the corresponding PMU for those CPUs. This
> > > > commit adds a test implementing these requirements.
> > > >
> > > > Signed-off-by: Rob Herring <robh@kernel.org>
> > > > ---
> > > > v6:
> > > >  - Add a check on cap_user_rdpmc
> > > > v5:
> > > >  - Adapt to libperf mmap API changes
> > > > v4:
> > > >  - Update perf_evsel__mmap params
> > > > v2:
> > > >  - Drop all but heterogeneous test as others covered by libperf tests
> > > >  - Rework to use libperf
> > > > ---
> > > >  tools/perf/arch/arm64/include/arch-tests.h |   7 +
> > > >  tools/perf/arch/arm64/tests/Build          |   1 +
> > > >  tools/perf/arch/arm64/tests/arch-tests.c   |   4 +
> > > >  tools/perf/arch/arm64/tests/user-events.c  | 177 +++++++++++++++++++++
> > > >  4 files changed, 189 insertions(+)
> > > >  create mode 100644 tools/perf/arch/arm64/tests/user-events.c
> > > >
> > > > diff --git a/tools/perf/arch/arm64/include/arch-tests.h b/tools/perf/arch/arm64/include/arch-tests.h
> > > > index 90ec4c8cb880..380ad34a3f09 100644
> > > > --- a/tools/perf/arch/arm64/include/arch-tests.h
> > > > +++ b/tools/perf/arch/arm64/include/arch-tests.h
> > > > @@ -2,11 +2,18 @@
> > > >  #ifndef ARCH_TESTS_H
> > > >  #define ARCH_TESTS_H
> > > >
> > > > +#include <linux/compiler.h>
> > > > +
> > > >  #ifdef HAVE_DWARF_UNWIND_SUPPORT
> > > >  struct thread;
> > > >  struct perf_sample;
> > > > +int test__arch_unwind_sample(struct perf_sample *sample,
> > > > +                          struct thread *thread);
> > > >  #endif
> > >
> > > Hello,
> > >
> > > I got the following compile error with aarch64 on Fedora33.
> > >
> > >     # make tools/perf
> > >     ...
> > >     In file included from arch/arm64/tests/arch-tests.c:4:
> > >     /root//libperf_v7/tools/perf/arch/arm64/include/arch-tests.h:10:5: error: redundant redeclaration of ‘test__arch_unwind_sample’ [-Werror=redundant-decls]
> > >        10 | int test__arch_unwind_sample(struct perf_sample *sample,
> > >           |     ^~~~~~~~~~~~~~~~~~~~~~~~
> > >     In file included from arch/arm64/tests/arch-tests.c:3:
> > >     /root//libperf_v7/tools/perf/tests/tests.h:140:5: note: previous declaration of ‘test__arch_unwind_sample’ was here
> > >       140 | int test__arch_unwind_sample(struct perf_sample *sample,
> > >           |     ^~~~~~~~~~~~~~~~~~~~~~~~
> > >     cc1: all warnings being treated as errors
> > >     make[8]: *** [/root//libperf_v7/tools/build/Makefile.build:97: /root/libperf_v7/tools/perf/arch/arm64/tests/arch-tests.o] Error 1
> > >     make[8]: *** Waiting for unfinished jobs....
> > >     In file included from arch/arm64/tests/user-events.c:13:
> > >     /root//libperf_v7/tools/perf/arch/arm64/include/arch-tests.h:10:5: error: redundant redeclaration of ‘test__arch_unwind_sample’ [-Werror=redundant-decls]
> > >        10 | int test__arch_unwind_sample(struct perf_sample *sample,
> > >           |     ^~~~~~~~~~~~~~~~~~~~~~~~
> > >     In file included from arch/arm64/tests/user-events.c:12:
> > >     /root//libperf_v7/tools/perf/tests/tests.h:140:5: note: previous declaration of ‘test__arch_unwind_sample’ was here
> > >       140 | int test__arch_unwind_sample(struct perf_sample *sample,
> > >           |     ^~~~~~~~~~~~~~~~~~~~~~~~
> > >     cc1: all warnings being treated as errors
> > >     ...
> > >
> > > The error is gone after the following patch is applied.
> >
> > Thanks. Honestly, I'm not sure why it was there in the first place.
> > Looking at the git history and this series history doesn't give any
> > clues.
> 
> Well, except that both x86 and powerpc have the same hunk in their
> arch-tests.h. Do you see errors on those arches?

I didn't see the errors on x86_64.
It seems that the errors happen on aarch64 because 
test__arch_unwind_sample() is defined only if the arch
is arm or arm64 in tools/perf/tests/tests.h:

    #if defined(__arm__) || defined(__aarch64__)
    #ifdef HAVE_DWARF_UNWIND_SUPPORT
    struct thread;
    struct perf_sample;
    int test__arch_unwind_sample(struct perf_sample *sample,
                                 struct thread *thread);
    #endif
    #endif

The following patch may be another solution which is same way as
commit d8b167f9d8af ("perf tests: Move x86 tests into arch directory").

---
 tools/perf/arch/arm64/tests/dwarf-unwind.c | 1 +
 tools/perf/tests/dwarf-unwind.c            | 2 +-
 tools/perf/tests/tests.h                   | 2 +-
 3 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/tools/perf/arch/arm64/tests/dwarf-unwind.c b/tools/perf/arch/arm64/tests/dwarf-unwind.c
index 46147a483..02ba87f2b 100644
--- a/tools/perf/arch/arm64/tests/dwarf-unwind.c
+++ b/tools/perf/arch/arm64/tests/dwarf-unwind.c
@@ -7,6 +7,7 @@
 #include "event.h"
 #include "debug.h"
 #include "tests/tests.h"
+#include "arch-tests.h"
 
 #define STACK_SIZE 8192
 
diff --git a/tools/perf/tests/dwarf-unwind.c b/tools/perf/tests/dwarf-unwind.c
index 83638097c..daffe2d66 100644
--- a/tools/perf/tests/dwarf-unwind.c
+++ b/tools/perf/tests/dwarf-unwind.c
@@ -17,7 +17,7 @@
 #include "callchain.h"
 #include "util/synthetic-events.h"
 
-#if defined (__x86_64__) || defined (__i386__) || defined (__powerpc__)
+#if defined (__x86_64__) || defined (__i386__) || defined (__powerpc__) || defined(__aarch64__)
 #include "arch-tests.h"
 #endif
 
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index b85f00530..40cbdfa46 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -133,7 +133,7 @@ bool test__bp_account_is_supported(void);
 bool test__wp_is_supported(void);
 bool test__tsc_is_supported(void);
 
-#if defined(__arm__) || defined(__aarch64__)
+#if defined(__arm__)
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
 struct thread;
 struct perf_sample;
nakamura.shun@fujitsu.com May 13, 2021, 9:05 a.m. UTC | #5
Hi Rob

> Userspace counter access only works on heterogeneous systems with some
> restrictions. The userspace process must be pinned to a homogeneous
> subset of CPUs and must open the corresponding PMU for those CPUs. This
> commit adds a test implementing these requirements.

Are you planning to change x86 tests (tools/perf/arch/x86/tests/rdpmc.c) 
to use libperf as well?

Best Regards
Shunsuke
Rob Herring (Arm) May 13, 2021, 12:37 p.m. UTC | #6
On Thu, May 13, 2021 at 4:06 AM nakamura.shun@fujitsu.com
<nakamura.shun@fujitsu.com> wrote:
>
> Hi Rob
>
> > Userspace counter access only works on heterogeneous systems with some
> > restrictions. The userspace process must be pinned to a homogeneous
> > subset of CPUs and must open the corresponding PMU for those CPUs. This
> > commit adds a test implementing these requirements.
>
> Are you planning to change x86 tests (tools/perf/arch/x86/tests/rdpmc.c)
> to use libperf as well?

The test can be removed as the libperf unit tests have an equivalent
test. I had a patch doing this, but there were objections removing it
until 'perf test' can run the libperf tests.

Rob
diff mbox series

Patch

diff --git a/tools/perf/arch/arm64/include/arch-tests.h b/tools/perf/arch/arm64/include/arch-tests.h
index 90ec4c8cb880..380ad34a3f09 100644
--- a/tools/perf/arch/arm64/include/arch-tests.h
+++ b/tools/perf/arch/arm64/include/arch-tests.h
@@ -2,11 +2,18 @@ 
 #ifndef ARCH_TESTS_H
 #define ARCH_TESTS_H
 
+#include <linux/compiler.h>
+
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
 struct thread;
 struct perf_sample;
+int test__arch_unwind_sample(struct perf_sample *sample,
+			     struct thread *thread);
 #endif
 
 extern struct test arch_tests[];
+int test__rd_pinned(struct test __maybe_unused *test,
+		       int __maybe_unused subtest);
+
 
 #endif
diff --git a/tools/perf/arch/arm64/tests/Build b/tools/perf/arch/arm64/tests/Build
index a61c06bdb757..3f9a20c17fc6 100644
--- a/tools/perf/arch/arm64/tests/Build
+++ b/tools/perf/arch/arm64/tests/Build
@@ -1,4 +1,5 @@ 
 perf-y += regs_load.o
 perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
 
+perf-y += user-events.o
 perf-y += arch-tests.o
diff --git a/tools/perf/arch/arm64/tests/arch-tests.c b/tools/perf/arch/arm64/tests/arch-tests.c
index 5b1543c98022..80ce7bd3c16d 100644
--- a/tools/perf/arch/arm64/tests/arch-tests.c
+++ b/tools/perf/arch/arm64/tests/arch-tests.c
@@ -10,6 +10,10 @@  struct test arch_tests[] = {
 		.func = test__dwarf_unwind,
 	},
 #endif
+	{
+		.desc = "Pinned CPU user counter access",
+		.func = test__rd_pinned,
+	},
 	{
 		.func = NULL,
 	},
diff --git a/tools/perf/arch/arm64/tests/user-events.c b/tools/perf/arch/arm64/tests/user-events.c
new file mode 100644
index 000000000000..c8efc6b369e6
--- /dev/null
+++ b/tools/perf/arch/arm64/tests/user-events.c
@@ -0,0 +1,177 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <unistd.h>
+#include <sched.h>
+#include <cpumap.h>
+
+#include <perf/core.h>
+#include <perf/threadmap.h>
+#include <perf/evsel.h>
+
+#include "pmu.h"
+#include "debug.h"
+#include "tests/tests.h"
+#include "arch-tests.h"
+
+static int run_test(struct perf_evsel *evsel)
+{
+	int n;
+	volatile int tmp = 0;
+	u64 delta, i, loops = 1000;
+	struct perf_counts_values counts = { .val = 0 };
+
+	for (n = 0; n < 6; n++) {
+		u64 stamp, now;
+
+		perf_evsel__read(evsel, 0, 0, &counts);
+		stamp = counts.val;
+
+		for (i = 0; i < loops; i++)
+			tmp++;
+
+		perf_evsel__read(evsel, 0, 0, &counts);
+		now = counts.val;
+		loops *= 10;
+
+		delta = now - stamp;
+		pr_debug("%14d: %14llu\n", n, (long long)delta);
+
+		if (!delta)
+			break;
+	}
+	return delta ? 0 : -1;
+}
+
+static struct perf_pmu *pmu_for_cpu(int cpu)
+{
+	int acpu, idx;
+	struct perf_pmu *pmu = NULL;
+
+	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
+		if (pmu->is_uncore)
+			continue;
+		perf_cpu_map__for_each_cpu(acpu, idx, pmu->cpus)
+			if (acpu == cpu)
+				return pmu;
+	}
+	return NULL;
+}
+
+static bool pmu_is_homogeneous(void)
+{
+	int core_cnt = 0;
+	struct perf_pmu *pmu = NULL;
+
+	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
+		if (!pmu->is_uncore && !perf_cpu_map__empty(pmu->cpus))
+			core_cnt++;
+	}
+	return core_cnt == 1;
+}
+
+static int libperf_print(enum libperf_print_level level,
+			 const char *fmt, va_list ap)
+{
+	(void)level;
+	return vfprintf(stderr, fmt, ap);
+}
+
+static struct perf_evsel *perf_init(struct perf_event_attr *attr)
+{
+	int err;
+	struct perf_thread_map *threads;
+	struct perf_evsel *evsel;
+	struct perf_event_mmap_page *pc;
+
+	libperf_init(libperf_print);
+
+	threads = perf_thread_map__new_dummy();
+	if (!threads) {
+		pr_err("failed to create threads\n");
+		return NULL;
+	}
+
+	perf_thread_map__set_pid(threads, 0, 0);
+
+	evsel = perf_evsel__new(attr);
+	if (!evsel) {
+		pr_err("failed to create evsel\n");
+		goto out_thread;
+	}
+
+	err = perf_evsel__open(evsel, NULL, threads);
+	if (err) {
+		pr_err("failed to open evsel\n");
+		goto out_open;
+	}
+
+	if (perf_evsel__mmap(evsel, 0)) {
+		pr_err("failed to mmap evsel\n");
+		goto out_mmap;
+	}
+
+	pc = perf_evsel__mmap_base(evsel, 0, 0);
+	if (!pc->cap_user_rdpmc) {
+		pr_err("userspace access not enabled\n");
+		goto out_mmap;
+	}
+
+	return evsel;
+
+out_mmap:
+	perf_evsel__close(evsel);
+out_open:
+	perf_evsel__delete(evsel);
+out_thread:
+	perf_thread_map__put(threads);
+	return NULL;
+}
+
+int test__rd_pinned(struct test __maybe_unused *test,
+		    int __maybe_unused subtest)
+{
+	int cpu, cputmp, ret = -1;
+	struct perf_evsel *evsel;
+	struct perf_event_attr attr = {
+		.config = 0x8, /* Instruction count */
+		.config1 = 0, /* 32-bit counter */
+		.exclude_kernel = 1,
+	};
+	cpu_set_t cpu_set;
+	struct perf_pmu *pmu;
+
+	if (pmu_is_homogeneous())
+		return TEST_SKIP;
+
+	cpu = sched_getcpu();
+	pmu = pmu_for_cpu(cpu);
+	if (!pmu)
+		return -1;
+	attr.type = pmu->type;
+
+	CPU_ZERO(&cpu_set);
+	perf_cpu_map__for_each_cpu(cpu, cputmp, pmu->cpus)
+		CPU_SET(cpu, &cpu_set);
+	if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0)
+		pr_err("Could not set affinity\n");
+
+	evsel = perf_init(&attr);
+	if (!evsel)
+		return -1;
+
+	perf_cpu_map__for_each_cpu(cpu, cputmp, pmu->cpus) {
+		CPU_ZERO(&cpu_set);
+		CPU_SET(cpu, &cpu_set);
+		if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0)
+			pr_err("Could not set affinity\n");
+
+		pr_debug("Running on CPU %d\n", cpu);
+
+		ret = run_test(evsel);
+		if (ret)
+			break;
+	}
+
+	perf_evsel__close(evsel);
+	perf_evsel__delete(evsel);
+	return ret;
+}