xref: /linux/tools/perf/tests/event_groups.c (revision 7685b334d1e4927cc73b62c65293ba65748d9c52)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <string.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include "linux/perf_event.h"
6 #include "tests.h"
7 #include "debug.h"
8 #include "pmu.h"
9 #include "pmus.h"
10 #include "header.h"
11 #include "../perf-sys.h"
12 
13 /* hw: cycles,instructions sw: context-switch, uncore: [arch dependent] */
14 static int types[] = {0, 1, -1};
15 static unsigned long configs[] = {0, 3, 0};
16 static unsigned long configs_hw[] = {1};
17 
18 #define NR_UNCORE_PMUS 5
19 
20 /* Uncore pmus that support more than 3 counters */
21 static struct uncore_pmus {
22 	const char *name;
23 	__u64 config;
24 } uncore_pmus[NR_UNCORE_PMUS] = {
25 	{ "amd_l3", 0x0 },
26 	{ "amd_df", 0x0 },
27 	{ "uncore_imc_0", 0x1 },         /* Intel */
28 	{ "core_imc", 0x318 },           /* PowerPC: core_imc/CPM_STCX_FIN/ */
29 	{ "hv_24x7", 0x22000000003 },    /* PowerPC: hv_24x7/CPM_STCX_FIN/ */
30 };
31 
32 static int event_open(int type, unsigned long config, int group_fd)
33 {
34 	struct perf_event_attr attr;
35 
36 	memset(&attr, 0, sizeof(struct perf_event_attr));
37 	attr.type = type;
38 	attr.size = sizeof(struct perf_event_attr);
39 	attr.config = config;
40 	/*
41 	 * When creating an event group, typically the group leader is
42 	 * initialized with disabled set to 1 and any child events are
43 	 * initialized with disabled set to 0. Despite disabled being 0,
44 	 * the child events will not start until the group leader is
45 	 * enabled.
46 	 */
47 	attr.disabled = group_fd == -1 ? 1 : 0;
48 
49 	return sys_perf_event_open(&attr, -1, 0, group_fd, 0);
50 }
51 
52 static int setup_uncore_event(void)
53 {
54 	struct perf_pmu *pmu = NULL;
55 	int i, fd;
56 
57 	while ((pmu = perf_pmus__scan(pmu)) != NULL) {
58 		for (i = 0; i < NR_UNCORE_PMUS; i++) {
59 			if (!strcmp(uncore_pmus[i].name, pmu->name)) {
60 				pr_debug("Using %s for uncore pmu event\n", pmu->name);
61 				types[2] = pmu->type;
62 				configs[2] = uncore_pmus[i].config;
63 				/*
64 				 * Check if the chosen uncore pmu event can be
65 				 * used in the test. For example, incase of accessing
66 				 * hv_24x7 pmu counters, partition should have
67 				 * additional permissions. If not, event open will
68 				 * fail. So check if the event open succeeds
69 				 * before proceeding.
70 				 */
71 				fd = event_open(types[2], configs[2], -1);
72 				if (fd < 0)
73 					return -1;
74 				close(fd);
75 				return 0;
76 			}
77 		}
78 	}
79 	return -1;
80 }
81 
82 static int run_test(int i, int j, int k)
83 {
84 	int erroneous = ((((1 << i) | (1 << j) | (1 << k)) & 5) == 5);
85 	int group_fd, sibling_fd1, sibling_fd2;
86 
87 	group_fd = event_open(types[i], configs[i], -1);
88 	if (group_fd == -1)
89 		return -1;
90 
91 	sibling_fd1 = event_open(types[j], configs[j], group_fd);
92 	if (sibling_fd1 == -1) {
93 		close(group_fd);
94 		return erroneous ? 0 : -1;
95 	}
96 
97 	/*
98 	 * if all three events (leader and two sibling events)
99 	 * are hardware events, use instructions as one of the
100 	 * sibling event. There is event constraint in powerpc that
101 	 * events using same counter cannot be programmed in a group.
102 	 * Since PERF_COUNT_HW_INSTRUCTIONS is a generic hardware
103 	 * event and present in all platforms, lets use that.
104 	 */
105 	if (!i && !j && !k)
106 		sibling_fd2 = event_open(types[k], configs_hw[k], group_fd);
107 	else
108 		sibling_fd2 = event_open(types[k], configs[k], group_fd);
109 	if (sibling_fd2 == -1) {
110 		close(sibling_fd1);
111 		close(group_fd);
112 		return erroneous ? 0 : -1;
113 	}
114 
115 	close(sibling_fd2);
116 	close(sibling_fd1);
117 	close(group_fd);
118 	return erroneous ? -1 : 0;
119 }
120 
121 static int test__event_groups(struct test_suite *text __maybe_unused, int subtest __maybe_unused)
122 {
123 	int i, j, k;
124 	int ret;
125 	int r;
126 
127 	ret = setup_uncore_event();
128 	if (ret || types[2] == -1)
129 		return TEST_SKIP;
130 
131 	ret = TEST_OK;
132 	for (i = 0; i < 3; i++) {
133 		for (j = 0; j < 3; j++) {
134 			for (k = 0; k < 3; k++) {
135 				r = run_test(i, j, k);
136 				if (r)
137 					ret = TEST_FAIL;
138 
139 				/*
140 				 * For all three events as HW events, second sibling
141 				 * event is picked from configs_hw. So print accordingly
142 				 */
143 				if (!i && !j && !k)
144 					pr_debug("0x%x 0x%lx, 0x%x 0x%lx, 0x%x 0x%lx: %s\n",
145 						 types[i], configs[i], types[j], configs[j],
146 						 types[k], configs_hw[k], r ? "Fail" : "Pass");
147 				else
148 					pr_debug("0x%x 0x%lx, 0x%x 0x%lx, 0x%x 0x%lx: %s\n",
149 						 types[i], configs[i], types[j], configs[j],
150 						 types[k], configs[k], r ? "Fail" : "Pass");
151 			}
152 		}
153 	}
154 	return ret;
155 }
156 
157 DEFINE_SUITE("Event groups", event_groups);
158