1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2014, Michael Ellerman, IBM Corp.
13 #include <sys/prctl.h>
20 * Test that per-event excludes work.
23 static int per_event_excludes(void)
25 struct event *e, events[4];
28 SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_2_07));
31 * We need to create the events disabled, otherwise the running/enabled
32 * counts don't match up.
35 event_init_opts(e, PERF_COUNT_HW_INSTRUCTIONS,
36 PERF_TYPE_HARDWARE, "instructions");
40 event_init_opts(e, PERF_COUNT_HW_INSTRUCTIONS,
41 PERF_TYPE_HARDWARE, "instructions(k)");
43 e->attr.exclude_user = 1;
44 e->attr.exclude_hv = 1;
47 event_init_opts(e, PERF_COUNT_HW_INSTRUCTIONS,
48 PERF_TYPE_HARDWARE, "instructions(h)");
50 e->attr.exclude_user = 1;
51 e->attr.exclude_kernel = 1;
54 event_init_opts(e, PERF_COUNT_HW_INSTRUCTIONS,
55 PERF_TYPE_HARDWARE, "instructions(u)");
57 e->attr.exclude_hv = 1;
58 e->attr.exclude_kernel = 1;
60 FAIL_IF(event_open(&events[0]));
63 * The open here will fail if we don't have per event exclude support,
64 * because the second event has an incompatible set of exclude settings
65 * and we're asking for the events to be in a group.
67 for (i = 1; i < 4; i++)
68 FAIL_IF(event_open_with_group(&events[i], events[0].fd));
71 * Even though the above will fail without per-event excludes we keep
72 * testing in order to be thorough.
74 prctl(PR_TASK_PERF_EVENTS_ENABLE);
76 /* Spin for a while */
77 for (i = 0; i < INT_MAX; i++)
78 asm volatile("" : : : "memory");
80 prctl(PR_TASK_PERF_EVENTS_DISABLE);
82 for (i = 0; i < 4; i++) {
83 FAIL_IF(event_read(&events[i]));
84 event_report(&events[i]);
88 * We should see that all events have enabled == running. That
89 * shows that they were all on the PMU at once.
91 for (i = 0; i < 4; i++)
92 FAIL_IF(events[i].result.running != events[i].result.enabled);
95 * We can also check that the result for instructions is >= all the
96 * other counts. That's because it is counting all instructions while
97 * the others are counting a subset.
99 for (i = 1; i < 4; i++)
100 FAIL_IF(events[0].result.value < events[i].result.value);
102 for (i = 0; i < 4; i++)
103 event_close(&events[i]);
110 return test_harness(per_event_excludes, "per_event_excludes");