xref: /openbmc/linux/tools/perf/tests/cpumap.c (revision 9fb29c73)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "tests.h"
3 #include <stdio.h>
4 #include "cpumap.h"
5 #include "event.h"
6 #include <string.h>
7 #include <linux/bitops.h>
8 #include "debug.h"
9 
10 struct machine;
11 
12 static int process_event_mask(struct perf_tool *tool __maybe_unused,
13 			 union perf_event *event,
14 			 struct perf_sample *sample __maybe_unused,
15 			 struct machine *machine __maybe_unused)
16 {
17 	struct cpu_map_event *map_event = &event->cpu_map;
18 	struct cpu_map_mask *mask;
19 	struct cpu_map_data *data;
20 	struct cpu_map *map;
21 	int i;
22 
23 	data = &map_event->data;
24 
25 	TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK);
26 
27 	mask = (struct cpu_map_mask *)data->data;
28 
29 	TEST_ASSERT_VAL("wrong nr",   mask->nr == 1);
30 
31 	for (i = 0; i < 20; i++) {
32 		TEST_ASSERT_VAL("wrong cpu", test_bit(i, mask->mask));
33 	}
34 
35 	map = cpu_map__new_data(data);
36 	TEST_ASSERT_VAL("wrong nr",  map->nr == 20);
37 
38 	for (i = 0; i < 20; i++) {
39 		TEST_ASSERT_VAL("wrong cpu", map->map[i] == i);
40 	}
41 
42 	cpu_map__put(map);
43 	return 0;
44 }
45 
46 static int process_event_cpus(struct perf_tool *tool __maybe_unused,
47 			 union perf_event *event,
48 			 struct perf_sample *sample __maybe_unused,
49 			 struct machine *machine __maybe_unused)
50 {
51 	struct cpu_map_event *map_event = &event->cpu_map;
52 	struct cpu_map_entries *cpus;
53 	struct cpu_map_data *data;
54 	struct cpu_map *map;
55 
56 	data = &map_event->data;
57 
58 	TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS);
59 
60 	cpus = (struct cpu_map_entries *)data->data;
61 
62 	TEST_ASSERT_VAL("wrong nr",   cpus->nr == 2);
63 	TEST_ASSERT_VAL("wrong cpu",  cpus->cpu[0] == 1);
64 	TEST_ASSERT_VAL("wrong cpu",  cpus->cpu[1] == 256);
65 
66 	map = cpu_map__new_data(data);
67 	TEST_ASSERT_VAL("wrong nr",  map->nr == 2);
68 	TEST_ASSERT_VAL("wrong cpu", map->map[0] == 1);
69 	TEST_ASSERT_VAL("wrong cpu", map->map[1] == 256);
70 	TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1);
71 	cpu_map__put(map);
72 	return 0;
73 }
74 
75 
76 int test__cpu_map_synthesize(struct test *test __maybe_unused, int subtest __maybe_unused)
77 {
78 	struct cpu_map *cpus;
79 
80 	/* This one is better stores in mask. */
81 	cpus = cpu_map__new("0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19");
82 
83 	TEST_ASSERT_VAL("failed to synthesize map",
84 		!perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL));
85 
86 	cpu_map__put(cpus);
87 
88 	/* This one is better stores in cpu values. */
89 	cpus = cpu_map__new("1,256");
90 
91 	TEST_ASSERT_VAL("failed to synthesize map",
92 		!perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL));
93 
94 	cpu_map__put(cpus);
95 	return 0;
96 }
97 
98 static int cpu_map_print(const char *str)
99 {
100 	struct cpu_map *map = cpu_map__new(str);
101 	char buf[100];
102 
103 	if (!map)
104 		return -1;
105 
106 	cpu_map__snprint(map, buf, sizeof(buf));
107 	return !strcmp(buf, str);
108 }
109 
110 int test__cpu_map_print(struct test *test __maybe_unused, int subtest __maybe_unused)
111 {
112 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
113 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
114 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
115 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
116 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
117 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37"));
118 	TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40"));
119 	return 0;
120 }
121