xref: /openbmc/linux/tools/perf/tests/cpumap.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
26c872901SJiri Olsa #include "tests.h"
33ac55b1dSArnaldo Carvalho de Melo #include <stdio.h>
46c872901SJiri Olsa #include "cpumap.h"
53ac55b1dSArnaldo Carvalho de Melo #include "event.h"
6ea49e01cSArnaldo Carvalho de Melo #include "util/synthetic-events.h"
73ac55b1dSArnaldo Carvalho de Melo #include <string.h>
83ac55b1dSArnaldo Carvalho de Melo #include <linux/bitops.h>
9c4d9d95fSIan Rogers #include <internal/cpumap.h>
103ac55b1dSArnaldo Carvalho de Melo #include "debug.h"
113ac55b1dSArnaldo Carvalho de Melo 
123ac55b1dSArnaldo Carvalho de Melo struct machine;
136c872901SJiri Olsa 
process_event_mask(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)146c872901SJiri Olsa static int process_event_mask(struct perf_tool *tool __maybe_unused,
156c872901SJiri Olsa 			 union perf_event *event,
166c872901SJiri Olsa 			 struct perf_sample *sample __maybe_unused,
176c872901SJiri Olsa 			 struct machine *machine __maybe_unused)
186c872901SJiri Olsa {
1972932371SJiri Olsa 	struct perf_record_cpu_map *map_event = &event->cpu_map;
2072932371SJiri Olsa 	struct perf_record_cpu_map_data *data;
21f854839bSJiri Olsa 	struct perf_cpu_map *map;
22b2f10cd4SIan Rogers 	unsigned int long_size;
236c872901SJiri Olsa 
24f77b57adSJiri Olsa 	data = &map_event->data;
256c872901SJiri Olsa 
266c872901SJiri Olsa 	TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK);
276c872901SJiri Olsa 
28b2f10cd4SIan Rogers 	long_size = data->mask32_data.long_size;
296c872901SJiri Olsa 
30b2f10cd4SIan Rogers 	TEST_ASSERT_VAL("wrong long_size", long_size == 4 || long_size == 8);
31b2f10cd4SIan Rogers 
32b2f10cd4SIan Rogers 	TEST_ASSERT_VAL("wrong nr",   data->mask32_data.nr == 1);
336c872901SJiri Olsa 
34c7202d20SIan Rogers 	TEST_ASSERT_VAL("wrong cpu", perf_record_cpu_map_data__test_bit(0, data));
35c7202d20SIan Rogers 	TEST_ASSERT_VAL("wrong cpu", !perf_record_cpu_map_data__test_bit(1, data));
36c7202d20SIan Rogers 	for (int i = 2; i <= 20; i++)
37b2f10cd4SIan Rogers 		TEST_ASSERT_VAL("wrong cpu", perf_record_cpu_map_data__test_bit(i, data));
386c872901SJiri Olsa 
39f77b57adSJiri Olsa 	map = cpu_map__new_data(data);
40c56c3927SIan Rogers 	TEST_ASSERT_VAL("wrong nr",  perf_cpu_map__nr(map) == 20);
41f77b57adSJiri Olsa 
42c7202d20SIan Rogers 	TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 0).cpu == 0);
43c7202d20SIan Rogers 	for (int i = 2; i <= 20; i++)
44c7202d20SIan Rogers 		TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, i - 1).cpu == i);
45f77b57adSJiri Olsa 
4638f01d8dSJiri Olsa 	perf_cpu_map__put(map);
476c872901SJiri Olsa 	return 0;
486c872901SJiri Olsa }
496c872901SJiri Olsa 
process_event_cpus(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)506c872901SJiri Olsa static int process_event_cpus(struct perf_tool *tool __maybe_unused,
516c872901SJiri Olsa 			 union perf_event *event,
526c872901SJiri Olsa 			 struct perf_sample *sample __maybe_unused,
536c872901SJiri Olsa 			 struct machine *machine __maybe_unused)
546c872901SJiri Olsa {
5572932371SJiri Olsa 	struct perf_record_cpu_map *map_event = &event->cpu_map;
5672932371SJiri Olsa 	struct perf_record_cpu_map_data *data;
57f854839bSJiri Olsa 	struct perf_cpu_map *map;
586c872901SJiri Olsa 
59f77b57adSJiri Olsa 	data = &map_event->data;
606c872901SJiri Olsa 
616c872901SJiri Olsa 	TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS);
626c872901SJiri Olsa 
63b2f10cd4SIan Rogers 	TEST_ASSERT_VAL("wrong nr",   data->cpus_data.nr == 2);
64b2f10cd4SIan Rogers 	TEST_ASSERT_VAL("wrong cpu",  data->cpus_data.cpu[0] == 1);
65b2f10cd4SIan Rogers 	TEST_ASSERT_VAL("wrong cpu",  data->cpus_data.cpu[1] == 256);
66f77b57adSJiri Olsa 
67f77b57adSJiri Olsa 	map = cpu_map__new_data(data);
68c56c3927SIan Rogers 	TEST_ASSERT_VAL("wrong nr",  perf_cpu_map__nr(map) == 2);
69c56c3927SIan Rogers 	TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 0).cpu == 1);
70c56c3927SIan Rogers 	TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 1).cpu == 256);
71*4121234aSArnaldo Carvalho de Melo 	TEST_ASSERT_VAL("wrong refcnt", refcount_read(perf_cpu_map__refcnt(map)) == 1);
7238f01d8dSJiri Olsa 	perf_cpu_map__put(map);
736c872901SJiri Olsa 	return 0;
746c872901SJiri Olsa }
756c872901SJiri Olsa 
process_event_range_cpus(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)76c7202d20SIan Rogers static int process_event_range_cpus(struct perf_tool *tool __maybe_unused,
77c7202d20SIan Rogers 				union perf_event *event,
78c7202d20SIan Rogers 				struct perf_sample *sample __maybe_unused,
79c7202d20SIan Rogers 				struct machine *machine __maybe_unused)
80c7202d20SIan Rogers {
81c7202d20SIan Rogers 	struct perf_record_cpu_map *map_event = &event->cpu_map;
82c7202d20SIan Rogers 	struct perf_record_cpu_map_data *data;
83c7202d20SIan Rogers 	struct perf_cpu_map *map;
84c7202d20SIan Rogers 
85c7202d20SIan Rogers 	data = &map_event->data;
86c7202d20SIan Rogers 
87c7202d20SIan Rogers 	TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__RANGE_CPUS);
88c7202d20SIan Rogers 
89c7202d20SIan Rogers 	TEST_ASSERT_VAL("wrong any_cpu",   data->range_cpu_data.any_cpu == 0);
90c7202d20SIan Rogers 	TEST_ASSERT_VAL("wrong start_cpu", data->range_cpu_data.start_cpu == 1);
91c7202d20SIan Rogers 	TEST_ASSERT_VAL("wrong end_cpu",   data->range_cpu_data.end_cpu == 256);
92c7202d20SIan Rogers 
93c7202d20SIan Rogers 	map = cpu_map__new_data(data);
94c7202d20SIan Rogers 	TEST_ASSERT_VAL("wrong nr",  perf_cpu_map__nr(map) == 256);
95c7202d20SIan Rogers 	TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 0).cpu == 1);
96c7202d20SIan Rogers 	TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__max(map).cpu == 256);
97*4121234aSArnaldo Carvalho de Melo 	TEST_ASSERT_VAL("wrong refcnt", refcount_read(perf_cpu_map__refcnt(map)) == 1);
98c7202d20SIan Rogers 	perf_cpu_map__put(map);
99c7202d20SIan Rogers 	return 0;
100c7202d20SIan Rogers }
101c7202d20SIan Rogers 
1026c872901SJiri Olsa 
test__cpu_map_synthesize(struct test_suite * test __maybe_unused,int subtest __maybe_unused)10333f44bfdSIan Rogers static int test__cpu_map_synthesize(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
1046c872901SJiri Olsa {
105f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
1066c872901SJiri Olsa 
107c7202d20SIan Rogers 	/* This one is better stored in a mask. */
108c7202d20SIan Rogers 	cpus = perf_cpu_map__new("0,2-20");
1096c872901SJiri Olsa 
1106c872901SJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize map",
1116c872901SJiri Olsa 		!perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL));
1126c872901SJiri Olsa 
11338f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
1146c872901SJiri Olsa 
115c7202d20SIan Rogers 	/* This one is better stored in cpu values. */
1169c3516d1SJiri Olsa 	cpus = perf_cpu_map__new("1,256");
1176c872901SJiri Olsa 
1186c872901SJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize map",
1196c872901SJiri Olsa 		!perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL));
1206c872901SJiri Olsa 
12138f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
122c7202d20SIan Rogers 
123c7202d20SIan Rogers 	/* This one is better stored as a range. */
124c7202d20SIan Rogers 	cpus = perf_cpu_map__new("1-256");
125c7202d20SIan Rogers 
126c7202d20SIan Rogers 	TEST_ASSERT_VAL("failed to synthesize map",
127c7202d20SIan Rogers 		!perf_event__synthesize_cpu_map(NULL, cpus, process_event_range_cpus, NULL));
128c7202d20SIan Rogers 
129c7202d20SIan Rogers 	perf_cpu_map__put(cpus);
1306c872901SJiri Olsa 	return 0;
1316c872901SJiri Olsa }
132a24020e6SJiri Olsa 
cpu_map_print(const char * str)133a24020e6SJiri Olsa static int cpu_map_print(const char *str)
134a24020e6SJiri Olsa {
1359c3516d1SJiri Olsa 	struct perf_cpu_map *map = perf_cpu_map__new(str);
136a24020e6SJiri Olsa 	char buf[100];
137a24020e6SJiri Olsa 
138a24020e6SJiri Olsa 	if (!map)
139a24020e6SJiri Olsa 		return -1;
140a24020e6SJiri Olsa 
141a24020e6SJiri Olsa 	cpu_map__snprint(map, buf, sizeof(buf));
142690d91f5SNamhyung Kim 	perf_cpu_map__put(map);
143690d91f5SNamhyung Kim 
144a24020e6SJiri Olsa 	return !strcmp(buf, str);
145a24020e6SJiri Olsa }
146a24020e6SJiri Olsa 
test__cpu_map_print(struct test_suite * test __maybe_unused,int subtest __maybe_unused)14733f44bfdSIan Rogers static int test__cpu_map_print(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
148a24020e6SJiri Olsa {
149a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
150a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
151a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
152a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
153a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
154a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
155a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40"));
156a24020e6SJiri Olsa 	return 0;
157a24020e6SJiri Olsa }
158a2408a70SAndi Kleen 
test__cpu_map_merge(struct test_suite * test __maybe_unused,int subtest __maybe_unused)15933f44bfdSIan Rogers static int test__cpu_map_merge(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
160a2408a70SAndi Kleen {
161a2408a70SAndi Kleen 	struct perf_cpu_map *a = perf_cpu_map__new("4,2,1");
162a2408a70SAndi Kleen 	struct perf_cpu_map *b = perf_cpu_map__new("4,5,7");
163a2408a70SAndi Kleen 	struct perf_cpu_map *c = perf_cpu_map__merge(a, b);
164a2408a70SAndi Kleen 	char buf[100];
165a2408a70SAndi Kleen 
166c56c3927SIan Rogers 	TEST_ASSERT_VAL("failed to merge map: bad nr", perf_cpu_map__nr(c) == 5);
167a2408a70SAndi Kleen 	cpu_map__snprint(c, buf, sizeof(buf));
168a2408a70SAndi Kleen 	TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, "1-2,4-5,7"));
169a2408a70SAndi Kleen 	perf_cpu_map__put(b);
170a2408a70SAndi Kleen 	perf_cpu_map__put(c);
171a2408a70SAndi Kleen 	return 0;
172a2408a70SAndi Kleen }
173d68f0365SIan Rogers 
__test__cpu_map_intersect(const char * lhs,const char * rhs,int nr,const char * expected)174d68f0365SIan Rogers static int __test__cpu_map_intersect(const char *lhs, const char *rhs, int nr, const char *expected)
175d68f0365SIan Rogers {
176d68f0365SIan Rogers 	struct perf_cpu_map *a = perf_cpu_map__new(lhs);
177 	struct perf_cpu_map *b = perf_cpu_map__new(rhs);
178 	struct perf_cpu_map *c = perf_cpu_map__intersect(a, b);
179 	char buf[100];
180 
181 	TEST_ASSERT_EQUAL("failed to intersect map: bad nr", perf_cpu_map__nr(c), nr);
182 	cpu_map__snprint(c, buf, sizeof(buf));
183 	TEST_ASSERT_VAL("failed to intersect map: bad result", !strcmp(buf, expected));
184 	perf_cpu_map__put(a);
185 	perf_cpu_map__put(b);
186 	perf_cpu_map__put(c);
187 	return 0;
188 }
189 
test__cpu_map_intersect(struct test_suite * test __maybe_unused,int subtest __maybe_unused)190 static int test__cpu_map_intersect(struct test_suite *test __maybe_unused,
191 				   int subtest __maybe_unused)
192 {
193 	int ret;
194 
195 	ret = __test__cpu_map_intersect("4,2,1", "4,5,7", 1, "4");
196 	if (ret)
197 		return ret;
198 	ret = __test__cpu_map_intersect("1-8", "6-9", 3, "6-8");
199 	if (ret)
200 		return ret;
201 	ret = __test__cpu_map_intersect("1-8,12-20", "6-9,15", 4, "6-8,15");
202 	if (ret)
203 		return ret;
204 	ret = __test__cpu_map_intersect("4,2,1", "1", 1, "1");
205 	if (ret)
206 		return ret;
207 	ret = __test__cpu_map_intersect("1", "4,2,1", 1, "1");
208 	if (ret)
209 		return ret;
210 	ret = __test__cpu_map_intersect("1", "1", 1, "1");
211 	return ret;
212 }
213 
test__cpu_map_equal(struct test_suite * test __maybe_unused,int subtest __maybe_unused)214 static int test__cpu_map_equal(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
215 {
216 	struct perf_cpu_map *any = perf_cpu_map__dummy_new();
217 	struct perf_cpu_map *one = perf_cpu_map__new("1");
218 	struct perf_cpu_map *two = perf_cpu_map__new("2");
219 	struct perf_cpu_map *empty = perf_cpu_map__intersect(one, two);
220 	struct perf_cpu_map *pair = perf_cpu_map__new("1-2");
221 	struct perf_cpu_map *tmp;
222 	struct perf_cpu_map *maps[] = {empty, any, one, two, pair};
223 
224 	for (size_t i = 0; i < ARRAY_SIZE(maps); i++) {
225 		/* Maps equal themself. */
226 		TEST_ASSERT_VAL("equal", perf_cpu_map__equal(maps[i], maps[i]));
227 		for (size_t j = 0; j < ARRAY_SIZE(maps); j++) {
228 			/* Maps dont't equal each other. */
229 			if (i == j)
230 				continue;
231 			TEST_ASSERT_VAL("not equal", !perf_cpu_map__equal(maps[i], maps[j]));
232 		}
233 	}
234 
235 	/* Maps equal made maps. */
236 	tmp = perf_cpu_map__merge(perf_cpu_map__get(one), two);
237 	TEST_ASSERT_VAL("pair", perf_cpu_map__equal(pair, tmp));
238 	perf_cpu_map__put(tmp);
239 
240 	tmp = perf_cpu_map__intersect(pair, one);
241 	TEST_ASSERT_VAL("one", perf_cpu_map__equal(one, tmp));
242 	perf_cpu_map__put(tmp);
243 
244 	for (size_t i = 0; i < ARRAY_SIZE(maps); i++)
245 		perf_cpu_map__put(maps[i]);
246 
247 	return TEST_OK;
248 }
249 
250 static struct test_case tests__cpu_map[] = {
251 	TEST_CASE("Synthesize cpu map", cpu_map_synthesize),
252 	TEST_CASE("Print cpu map", cpu_map_print),
253 	TEST_CASE("Merge cpu map", cpu_map_merge),
254 	TEST_CASE("Intersect cpu map", cpu_map_intersect),
255 	TEST_CASE("Equal cpu map", cpu_map_equal),
256 	{	.name = NULL, }
257 };
258 
259 struct test_suite suite__cpu_map = {
260 	.desc = "CPU map",
261 	.test_cases = tests__cpu_map,
262 };
263