xref: /openbmc/linux/tools/perf/tests/cpumap.c (revision ea49e01cfabd73c94a61649cd04fa524a2beff3c)
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"
6*ea49e01cSArnaldo 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_record_cpu_map *mask;
2172932371SJiri Olsa 	struct perf_record_cpu_map_data *data;
22f854839bSJiri Olsa 	struct perf_cpu_map *map;
236c872901SJiri Olsa 	int i;
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 
2972932371SJiri Olsa 	mask = (struct perf_record_record_cpu_map *)data->data;
306c872901SJiri Olsa 
316c872901SJiri Olsa 	TEST_ASSERT_VAL("wrong nr",   mask->nr == 1);
326c872901SJiri Olsa 
336c872901SJiri Olsa 	for (i = 0; i < 20; i++) {
346c872901SJiri Olsa 		TEST_ASSERT_VAL("wrong cpu", test_bit(i, mask->mask));
356c872901SJiri Olsa 	}
366c872901SJiri Olsa 
37f77b57adSJiri Olsa 	map = cpu_map__new_data(data);
38f77b57adSJiri Olsa 	TEST_ASSERT_VAL("wrong nr",  map->nr == 20);
39f77b57adSJiri Olsa 
40f77b57adSJiri Olsa 	for (i = 0; i < 20; i++) {
41f77b57adSJiri Olsa 		TEST_ASSERT_VAL("wrong cpu", map->map[i] == i);
42f77b57adSJiri Olsa 	}
43f77b57adSJiri Olsa 
4438f01d8dSJiri Olsa 	perf_cpu_map__put(map);
456c872901SJiri Olsa 	return 0;
466c872901SJiri Olsa }
476c872901SJiri Olsa 
486c872901SJiri Olsa static int process_event_cpus(struct perf_tool *tool __maybe_unused,
496c872901SJiri Olsa 			 union perf_event *event,
506c872901SJiri Olsa 			 struct perf_sample *sample __maybe_unused,
516c872901SJiri Olsa 			 struct machine *machine __maybe_unused)
526c872901SJiri Olsa {
5372932371SJiri Olsa 	struct perf_record_cpu_map *map_event = &event->cpu_map;
546c872901SJiri Olsa 	struct cpu_map_entries *cpus;
5572932371SJiri Olsa 	struct perf_record_cpu_map_data *data;
56f854839bSJiri Olsa 	struct perf_cpu_map *map;
576c872901SJiri Olsa 
58f77b57adSJiri Olsa 	data = &map_event->data;
596c872901SJiri Olsa 
606c872901SJiri Olsa 	TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS);
616c872901SJiri Olsa 
626c872901SJiri Olsa 	cpus = (struct cpu_map_entries *)data->data;
636c872901SJiri Olsa 
646c872901SJiri Olsa 	TEST_ASSERT_VAL("wrong nr",   cpus->nr == 2);
656c872901SJiri Olsa 	TEST_ASSERT_VAL("wrong cpu",  cpus->cpu[0] == 1);
666c872901SJiri Olsa 	TEST_ASSERT_VAL("wrong cpu",  cpus->cpu[1] == 256);
67f77b57adSJiri Olsa 
68f77b57adSJiri Olsa 	map = cpu_map__new_data(data);
69f77b57adSJiri Olsa 	TEST_ASSERT_VAL("wrong nr",  map->nr == 2);
70f77b57adSJiri Olsa 	TEST_ASSERT_VAL("wrong cpu", map->map[0] == 1);
71f77b57adSJiri Olsa 	TEST_ASSERT_VAL("wrong cpu", map->map[1] == 256);
72ec09a42aSElena Reshetova 	TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1);
7338f01d8dSJiri Olsa 	perf_cpu_map__put(map);
746c872901SJiri Olsa 	return 0;
756c872901SJiri Olsa }
766c872901SJiri Olsa 
776c872901SJiri Olsa 
7881f17c90SArnaldo Carvalho de Melo int test__cpu_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused)
796c872901SJiri Olsa {
80f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
816c872901SJiri Olsa 
826c872901SJiri Olsa 	/* This one is better stores in mask. */
839c3516d1SJiri 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");
846c872901SJiri Olsa 
856c872901SJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize map",
866c872901SJiri Olsa 		!perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL));
876c872901SJiri Olsa 
8838f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
896c872901SJiri Olsa 
906c872901SJiri Olsa 	/* This one is better stores in cpu values. */
919c3516d1SJiri Olsa 	cpus = perf_cpu_map__new("1,256");
926c872901SJiri Olsa 
936c872901SJiri Olsa 	TEST_ASSERT_VAL("failed to synthesize map",
946c872901SJiri Olsa 		!perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL));
956c872901SJiri Olsa 
9638f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
976c872901SJiri Olsa 	return 0;
986c872901SJiri Olsa }
99a24020e6SJiri Olsa 
100a24020e6SJiri Olsa static int cpu_map_print(const char *str)
101a24020e6SJiri Olsa {
1029c3516d1SJiri Olsa 	struct perf_cpu_map *map = perf_cpu_map__new(str);
103a24020e6SJiri Olsa 	char buf[100];
104a24020e6SJiri Olsa 
105a24020e6SJiri Olsa 	if (!map)
106a24020e6SJiri Olsa 		return -1;
107a24020e6SJiri Olsa 
108a24020e6SJiri Olsa 	cpu_map__snprint(map, buf, sizeof(buf));
109a24020e6SJiri Olsa 	return !strcmp(buf, str);
110a24020e6SJiri Olsa }
111a24020e6SJiri Olsa 
11281f17c90SArnaldo Carvalho de Melo int test__cpu_map_print(struct test *test __maybe_unused, int subtest __maybe_unused)
113a24020e6SJiri Olsa {
114a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
115a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
116a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
117a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
118a24020e6SJiri Olsa 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
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-10,12-20,22-30,32-40"));
121a24020e6SJiri Olsa 	return 0;
122a24020e6SJiri Olsa }
123