selftests/powerpc/pmu: Add selftest for PERF_TYPE_HARDWARE events valid check
authorAthira Rajeev <atrajeev@linux.vnet.ibm.com>
Fri, 10 Jun 2022 13:41:06 +0000 (19:11 +0530)
committerMichael Ellerman <mpe@ellerman.id.au>
Tue, 28 Jun 2022 22:57:44 +0000 (08:57 +1000)
Testcase to ensure that using invalid event in generic event for
PERF_TYPE_HARDWARE will fail. Invalid generic events in power10 are:
- PERF_COUNT_HW_BUS_CYCLES
- PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
- PERF_COUNT_HW_STALLED_CYCLES_BACKEND
- PERF_COUNT_HW_REF_CPU_CYCLES

Invalid generic events in power9 are:
- PERF_COUNT_HW_BUS_CYCLES
- PERF_COUNT_HW_REF_CPU_CYCLES

Testcase does event open for valid and invalid generic events to ensure
event open works for all valid events and fails for invalid events.

Signed-off-by: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20220610134113.62991-29-atrajeev@linux.vnet.ibm.com
tools/testing/selftests/powerpc/pmu/event_code_tests/Makefile
tools/testing/selftests/powerpc/pmu/event_code_tests/generic_events_valid_test.c [new file with mode: 0644]

index 50bcc036dddfcae4b7a3f5102a7decfcb115e16b..0d56f1ef530f3561071a22bf14506c8cfc20fe2b 100644 (file)
@@ -4,7 +4,7 @@ CFLAGS += -m64
 TEST_GEN_PROGS := group_constraint_pmc56_test group_pmc56_exclude_constraints_test group_constraint_pmc_count_test \
        group_constraint_repeat_test group_constraint_radix_scope_qual_test reserved_bits_mmcra_sample_elig_mode_test \
        group_constraint_mmcra_sample_test invalid_event_code_test reserved_bits_mmcra_thresh_ctl_test \
-       blacklisted_events_test event_alternatives_tests_p9 event_alternatives_tests_p10
+       blacklisted_events_test event_alternatives_tests_p9 event_alternatives_tests_p10 generic_events_valid_test
 
 top_srcdir = ../../../../../..
 include ../../../lib.mk
diff --git a/tools/testing/selftests/powerpc/pmu/event_code_tests/generic_events_valid_test.c b/tools/testing/selftests/powerpc/pmu/event_code_tests/generic_events_valid_test.c
new file mode 100644 (file)
index 0000000..0d237c1
--- /dev/null
@@ -0,0 +1,130 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright 2022, Athira Rajeev, IBM Corp.
+ */
+
+#include <stdio.h>
+#include <sys/prctl.h>
+#include <limits.h>
+#include "../event.h"
+#include "../sampling_tests/misc.h"
+
+/*
+ * Testcase to ensure that using invalid event in generic
+ * event for PERF_TYPE_HARDWARE should fail
+ */
+
+static int generic_events_valid_test(void)
+{
+       struct event event;
+
+       /* Check for platform support for the test */
+       SKIP_IF(platform_check_for_tests());
+
+       /* generic events is different in compat_mode */
+       SKIP_IF(check_for_generic_compat_pmu());
+
+       /*
+        * Invalid generic events in power10:
+        * - PERF_COUNT_HW_BUS_CYCLES
+        * - PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
+        * - PERF_COUNT_HW_STALLED_CYCLES_BACKEND
+        * - PERF_COUNT_HW_REF_CPU_CYCLES
+        */
+       if (PVR_VER(mfspr(SPRN_PVR)) == POWER10) {
+               event_init_opts(&event, PERF_COUNT_HW_CPU_CYCLES, PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_INSTRUCTIONS,
+                               PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_CACHE_REFERENCES,
+                               PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_CACHE_MISSES, PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_BRANCH_INSTRUCTIONS,
+                               PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_BRANCH_MISSES, PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_BUS_CYCLES, PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(!event_open(&event));
+
+               event_init_opts(&event, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND,
+                               PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(!event_open(&event));
+
+               event_init_opts(&event, PERF_COUNT_HW_STALLED_CYCLES_BACKEND,
+                               PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(!event_open(&event));
+
+               event_init_opts(&event, PERF_COUNT_HW_REF_CPU_CYCLES, PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(!event_open(&event));
+       } else if (PVR_VER(mfspr(SPRN_PVR)) == POWER9) {
+               /*
+                * Invalid generic events in power9:
+                * - PERF_COUNT_HW_BUS_CYCLES
+                * - PERF_COUNT_HW_REF_CPU_CYCLES
+                */
+               event_init_opts(&event, PERF_COUNT_HW_CPU_CYCLES, PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_INSTRUCTIONS, PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_CACHE_REFERENCES,
+                               PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_CACHE_MISSES, PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_BRANCH_INSTRUCTIONS,
+                               PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_BRANCH_MISSES, PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_BUS_CYCLES, PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(!event_open(&event));
+
+               event_init_opts(&event, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND,
+                               PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_STALLED_CYCLES_BACKEND,
+                               PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(event_open(&event));
+               event_close(&event);
+
+               event_init_opts(&event, PERF_COUNT_HW_REF_CPU_CYCLES, PERF_TYPE_HARDWARE, "event");
+               FAIL_IF(!event_open(&event));
+       }
+
+       return 0;
+}
+
+int main(void)
+{
+       return test_harness(generic_events_valid_test, "generic_events_valid_test");
+}