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