xref: /openbmc/linux/tools/perf/tests/cpumap.c (revision b2f10cd4e805eb647773df273eb1a6ff9e6ea45d)
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>
99c3516d1SJiri Olsa #include <perf/cpumap.h>
103ac55b1dSArnaldo Carvalho de Melo #include "debug.h"
113ac55b1dSArnaldo Carvalho de Melo 
123ac55b1dSArnaldo Carvalho de Melo struct machine;
136c872901SJiri Olsa 
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;
226c872901SJiri Olsa 	int i;
23*b2f10cd4SIan Rogers 	unsigned int long_size;
246c872901SJiri Olsa 
25f77b57adSJiri Olsa 	data = &map_event->data;
266c872901SJiri Olsa 
276c872901SJiri Olsa 	TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK);
286c872901SJiri Olsa 
29*b2f10cd4SIan Rogers 	long_size = data->mask32_data.long_size;
306c872901SJiri Olsa 
31*b2f10cd4SIan Rogers 	TEST_ASSERT_VAL("wrong long_size", long_size == 4 || long_size == 8);
32*b2f10cd4SIan Rogers 
33*b2f10cd4SIan Rogers 	TEST_ASSERT_VAL("wrong nr",   data->mask32_data.nr == 1);
346c872901SJiri Olsa 
356c872901SJiri Olsa 	for (i = 0; i < 20; i++) {
36*b2f10cd4SIan Rogers 		TEST_ASSERT_VAL("wrong cpu", perf_record_cpu_map_data__test_bit(i, data));
376c872901SJiri Olsa 	}
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 
42f77b57adSJiri Olsa 	for (i = 0; i < 20; i++) {
43c56c3927SIan Rogers 		TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, i).cpu == i);
44f77b57adSJiri Olsa 	}
45f77b57adSJiri Olsa 
4638f01d8dSJiri Olsa 	perf_cpu_map__put(map);
476c872901SJiri Olsa 	return 0;
486c872901SJiri Olsa }
496c872901SJiri Olsa 
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 
63*b2f10cd4SIan Rogers 	TEST_ASSERT_VAL("wrong nr",   data->cpus_data.nr == 2);
64*b2f10cd4SIan Rogers 	TEST_ASSERT_VAL("wrong cpu",  data->cpus_data.cpu[0] == 1);
65*b2f10cd4SIan 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);
71ec09a42aSElena Reshetova 	TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1);
7238f01d8dSJiri Olsa 	perf_cpu_map__put(map);
736c872901SJiri Olsa 	return 0;
746c872901SJiri Olsa }
756c872901SJiri Olsa 
766c872901SJiri Olsa 
7733f44bfdSIan Rogers static int test__cpu_map_synthesize(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
786c872901SJiri Olsa {
79f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
806c872901SJiri Olsa 
816c872901SJiri Olsa 	/* This one is better stores in mask. */
829c3516d1SJiri Olsa 	cpus = perf_cpu_map__new("0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19");
836c872901SJiri Olsa 
846c872901SJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize map",
856c872901SJiri Olsa 		!perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL));
866c872901SJiri Olsa 
8738f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
886c872901SJiri Olsa 
896c872901SJiri Olsa 	/* This one is better stores in cpu values. */
909c3516d1SJiri Olsa 	cpus = perf_cpu_map__new("1,256");
916c872901SJiri Olsa 
926c872901SJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize map",
936c872901SJiri Olsa 		!perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL));
946c872901SJiri Olsa 
9538f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
966c872901SJiri Olsa 	return 0;
976c872901SJiri Olsa }
98a24020e6SJiri Olsa 
99a24020e6SJiri Olsa static int cpu_map_print(const char *str)
100a24020e6SJiri Olsa {
1019c3516d1SJiri Olsa 	struct perf_cpu_map *map = perf_cpu_map__new(str);
102a24020e6SJiri Olsa 	char buf[100];
103a24020e6SJiri Olsa 
104a24020e6SJiri Olsa 	if (!map)
105a24020e6SJiri Olsa 		return -1;
106a24020e6SJiri Olsa 
107a24020e6SJiri Olsa 	cpu_map__snprint(map, buf, sizeof(buf));
108690d91f5SNamhyung Kim 	perf_cpu_map__put(map);
109690d91f5SNamhyung Kim 
110a24020e6SJiri Olsa 	return !strcmp(buf, str);
111a24020e6SJiri Olsa }
112a24020e6SJiri Olsa 
11333f44bfdSIan Rogers static int test__cpu_map_print(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
114a24020e6SJiri Olsa {
115a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
116a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
117a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
118a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
119a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
120a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
121a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40"));
122a24020e6SJiri Olsa 	return 0;
123a24020e6SJiri Olsa }
124a2408a70SAndi Kleen 
12533f44bfdSIan Rogers static int test__cpu_map_merge(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
126a2408a70SAndi Kleen {
127a2408a70SAndi Kleen 	struct perf_cpu_map *a = perf_cpu_map__new("4,2,1");
128a2408a70SAndi Kleen 	struct perf_cpu_map *b = perf_cpu_map__new("4,5,7");
129a2408a70SAndi Kleen 	struct perf_cpu_map *c = perf_cpu_map__merge(a, b);
130a2408a70SAndi Kleen 	char buf[100];
131a2408a70SAndi Kleen 
132c56c3927SIan Rogers 	TEST_ASSERT_VAL("failed to merge map: bad nr", perf_cpu_map__nr(c) == 5);
133a2408a70SAndi Kleen 	cpu_map__snprint(c, buf, sizeof(buf));
134a2408a70SAndi Kleen 	TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, "1-2,4-5,7"));
135a2408a70SAndi Kleen 	perf_cpu_map__put(b);
136a2408a70SAndi Kleen 	perf_cpu_map__put(c);
137a2408a70SAndi Kleen 	return 0;
138a2408a70SAndi Kleen }
139d68f0365SIan Rogers 
140d68f0365SIan Rogers DEFINE_SUITE("Synthesize cpu map", cpu_map_synthesize);
141d68f0365SIan Rogers DEFINE_SUITE("Print cpu map", cpu_map_print);
142d68f0365SIan Rogers DEFINE_SUITE("Merge cpu map", cpu_map_merge);
143