perf test: Add event group test for events in multiple PMUs
authorRavi Bangoria <ravi.bangoria@amd.com>
Tue, 6 Dec 2022 04:32:37 +0000 (10:02 +0530)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 14 Dec 2022 14:16:12 +0000 (11:16 -0300)
Multiple events in a group can belong to one or more PMUs, however
there are some limitations.

One of the limitations is that perf doesn't allow creating a group of
events from different hw PMUs.

Write a simple test to create various combinations of hw, sw and uncore
PMU events and verify group creation succeeds or fails as expected.

Signed-off-by: Ravi Bangoria <ravi.bangoria@amd.com>
Acked-by: Ian Rogers <irogers@google.com>
Acked-by: Kan Liang <kan.liang@linux.intel.com>
Acked-by: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ananth Narayan <ananth.narayan@amd.com>
Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
Cc: Carsten Haitzler <carsten.haitzler@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Santosh Shukla <santosh.shukla@amd.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Link: https://lore.kernel.org/r/20221206043237.12159-3-ravi.bangoria@amd.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/Build
tools/perf/tests/builtin-test.c
tools/perf/tests/event_groups.c [new file with mode: 0644]
tools/perf/tests/tests.h

index 11b69023011b08b69e402946a7a243a63f41e381..658b5052c24d2d6ef17cd13a7801171a95398fb3 100644 (file)
@@ -67,6 +67,7 @@ perf-y += expand-cgroup.o
 perf-y += perf-time-to-tsc.o
 perf-y += dlfilter-test.o
 perf-y += sigtrap.o
+perf-y += event_groups.o
 
 $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
        $(call rule_mkdir)
index 4c6ae59a4dfd7de23702d6abfcb348e14dc73af1..ddd8262bfa26453c319e39b45bb20058a4320560 100644 (file)
@@ -110,6 +110,7 @@ static struct test_suite *generic_tests[] = {
        &suite__perf_time_to_tsc,
        &suite__dlfilter,
        &suite__sigtrap,
+       &suite__event_groups,
        NULL,
 };
 
diff --git a/tools/perf/tests/event_groups.c b/tools/perf/tests/event_groups.c
new file mode 100644 (file)
index 0000000..612c044
--- /dev/null
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include "linux/perf_event.h"
+#include "tests.h"
+#include "debug.h"
+#include "pmu.h"
+#include "pmus.h"
+#include "header.h"
+#include "../perf-sys.h"
+
+/* hw: cycles, sw: context-switch, uncore: [arch dependent] */
+static int types[] = {0, 1, -1};
+static unsigned long configs[] = {0, 3, 0};
+
+#define NR_UNCORE_PMUS 5
+
+/* Uncore pmus that support more than 3 counters */
+static struct uncore_pmus {
+       const char *name;
+       __u64 config;
+} uncore_pmus[NR_UNCORE_PMUS] = {
+       { "amd_l3", 0x0 },
+       { "amd_df", 0x0 },
+       { "uncore_imc_0", 0x1 },         /* Intel */
+       { "core_imc", 0x318 },           /* PowerPC: core_imc/CPM_STCX_FIN/ */
+       { "hv_24x7", 0x22000000003 },    /* PowerPC: hv_24x7/CPM_STCX_FIN/ */
+};
+
+static int event_open(int type, unsigned long config, int group_fd)
+{
+       struct perf_event_attr attr;
+
+       memset(&attr, 0, sizeof(struct perf_event_attr));
+       attr.type = type;
+       attr.size = sizeof(struct perf_event_attr);
+       attr.config = config;
+       /*
+        * When creating an event group, typically the group leader is
+        * initialized with disabled set to 1 and any child events are
+        * initialized with disabled set to 0. Despite disabled being 0,
+        * the child events will not start until the group leader is
+        * enabled.
+        */
+       attr.disabled = group_fd == -1 ? 1 : 0;
+
+       return sys_perf_event_open(&attr, -1, 0, group_fd, 0);
+}
+
+static int setup_uncore_event(void)
+{
+       struct perf_pmu *pmu;
+       int i;
+
+       if (list_empty(&pmus))
+               perf_pmu__scan(NULL);
+
+       perf_pmus__for_each_pmu(pmu) {
+               for (i = 0; i < NR_UNCORE_PMUS; i++) {
+                       if (!strcmp(uncore_pmus[i].name, pmu->name)) {
+                               pr_debug("Using %s for uncore pmu event\n", pmu->name);
+                               types[2] = pmu->type;
+                               configs[2] = uncore_pmus[i].config;
+                               return 0;
+                       }
+               }
+       }
+       return -1;
+}
+
+static int run_test(int i, int j, int k)
+{
+       int erroneous = ((((1 << i) | (1 << j) | (1 << k)) & 5) == 5);
+       int group_fd, sibling_fd1, sibling_fd2;
+
+       group_fd = event_open(types[i], configs[i], -1);
+       if (group_fd == -1)
+               return -1;
+
+       sibling_fd1 = event_open(types[j], configs[j], group_fd);
+       if (sibling_fd1 == -1) {
+               close(group_fd);
+               return erroneous ? 0 : -1;
+       }
+
+       sibling_fd2 = event_open(types[k], configs[k], group_fd);
+       if (sibling_fd2 == -1) {
+               close(sibling_fd1);
+               close(group_fd);
+               return erroneous ? 0 : -1;
+       }
+
+       close(sibling_fd2);
+       close(sibling_fd1);
+       close(group_fd);
+       return erroneous ? -1 : 0;
+}
+
+static int test__event_groups(struct test_suite *text __maybe_unused, int subtest __maybe_unused)
+{
+       int i, j, k;
+       int ret;
+       int r;
+
+       ret = setup_uncore_event();
+       if (ret || types[2] == -1)
+               return TEST_SKIP;
+
+       ret = TEST_OK;
+       for (i = 0; i < 3; i++) {
+               for (j = 0; j < 3; j++) {
+                       for (k = 0; k < 3; k++) {
+                               r = run_test(i, j, k);
+                               if (r)
+                                       ret = TEST_FAIL;
+
+                               pr_debug("0x%x 0x%lx, 0x%x 0x%lx, 0x%x 0x%lx: %s\n",
+                                        types[i], configs[i], types[j], configs[j],
+                                        types[k], configs[k], r ? "Fail" : "Pass");
+                       }
+               }
+       }
+       return ret;
+}
+
+DEFINE_SUITE("Event groups", event_groups);
index e15f24cfc9094a86e4275db93073e92a1ec87fd5..fb4b5ad4dd0fe920b2a9f9437337e8ae92955bb4 100644 (file)
@@ -147,6 +147,7 @@ DECLARE_SUITE(expand_cgroup_events);
 DECLARE_SUITE(perf_time_to_tsc);
 DECLARE_SUITE(dlfilter);
 DECLARE_SUITE(sigtrap);
+DECLARE_SUITE(event_groups);
 
 /*
  * PowerPC and S390 do not support creation of instruction breakpoints using the