1 // SPDX-License-Identifier: GPL-2.0
5 #include "linux/perf_event.h"
11 #include "../perf-sys.h"
13 /* hw: cycles, sw: context-switch, uncore: [arch dependent] */
14 static int types
[] = {0, 1, -1};
15 static unsigned long configs
[] = {0, 3, 0};
17 #define NR_UNCORE_PMUS 5
19 /* Uncore pmus that support more than 3 counters */
20 static struct uncore_pmus
{
23 } uncore_pmus
[NR_UNCORE_PMUS
] = {
26 { "uncore_imc_0", 0x1 }, /* Intel */
27 { "core_imc", 0x318 }, /* PowerPC: core_imc/CPM_STCX_FIN/ */
28 { "hv_24x7", 0x22000000003 }, /* PowerPC: hv_24x7/CPM_STCX_FIN/ */
31 static int event_open(int type
, unsigned long config
, int group_fd
)
33 struct perf_event_attr attr
;
35 memset(&attr
, 0, sizeof(struct perf_event_attr
));
37 attr
.size
= sizeof(struct perf_event_attr
);
40 * When creating an event group, typically the group leader is
41 * initialized with disabled set to 1 and any child events are
42 * initialized with disabled set to 0. Despite disabled being 0,
43 * the child events will not start until the group leader is
46 attr
.disabled
= group_fd
== -1 ? 1 : 0;
48 return sys_perf_event_open(&attr
, -1, 0, group_fd
, 0);
51 static int setup_uncore_event(void)
53 struct perf_pmu
*pmu
= NULL
;
56 while ((pmu
= perf_pmus__scan(pmu
)) != NULL
) {
57 for (i
= 0; i
< NR_UNCORE_PMUS
; i
++) {
58 if (!strcmp(uncore_pmus
[i
].name
, pmu
->name
)) {
59 pr_debug("Using %s for uncore pmu event\n", pmu
->name
);
61 configs
[2] = uncore_pmus
[i
].config
;
63 * Check if the chosen uncore pmu event can be
64 * used in the test. For example, incase of accessing
65 * hv_24x7 pmu counters, partition should have
66 * additional permissions. If not, event open will
67 * fail. So check if the event open succeeds
70 fd
= event_open(types
[2], configs
[2], -1);
81 static int run_test(int i
, int j
, int k
)
83 int erroneous
= ((((1 << i
) | (1 << j
) | (1 << k
)) & 5) == 5);
84 int group_fd
, sibling_fd1
, sibling_fd2
;
86 group_fd
= event_open(types
[i
], configs
[i
], -1);
90 sibling_fd1
= event_open(types
[j
], configs
[j
], group_fd
);
91 if (sibling_fd1
== -1) {
93 return erroneous
? 0 : -1;
96 sibling_fd2
= event_open(types
[k
], configs
[k
], group_fd
);
97 if (sibling_fd2
== -1) {
100 return erroneous
? 0 : -1;
106 return erroneous
? -1 : 0;
109 static int test__event_groups(struct test_suite
*text __maybe_unused
, int subtest __maybe_unused
)
115 ret
= setup_uncore_event();
116 if (ret
|| types
[2] == -1)
120 for (i
= 0; i
< 3; i
++) {
121 for (j
= 0; j
< 3; j
++) {
122 for (k
= 0; k
< 3; k
++) {
123 r
= run_test(i
, j
, k
);
127 pr_debug("0x%x 0x%lx, 0x%x 0x%lx, 0x%x 0x%lx: %s\n",
128 types
[i
], configs
[i
], types
[j
], configs
[j
],
129 types
[k
], configs
[k
], r
? "Fail" : "Pass");
136 DEFINE_SUITE("Event groups", event_groups
);