1 // SPDX-License-Identifier: GPL-2.0
6 #include "util/synthetic-events.h"
8 #include <linux/bitops.h>
9 #include <perf/cpumap.h>
14 static int process_event_mask(struct perf_tool
*tool __maybe_unused
,
15 union perf_event
*event
,
16 struct perf_sample
*sample __maybe_unused
,
17 struct machine
*machine __maybe_unused
)
19 struct perf_record_cpu_map
*map_event
= &event
->cpu_map
;
20 struct perf_record_record_cpu_map
*mask
;
21 struct perf_record_cpu_map_data
*data
;
22 struct perf_cpu_map
*map
;
25 data
= &map_event
->data
;
27 TEST_ASSERT_VAL("wrong type", data
->type
== PERF_CPU_MAP__MASK
);
29 mask
= (struct perf_record_record_cpu_map
*)data
->data
;
31 TEST_ASSERT_VAL("wrong nr", mask
->nr
== 1);
33 for (i
= 0; i
< 20; i
++) {
34 TEST_ASSERT_VAL("wrong cpu", test_bit(i
, mask
->mask
));
37 map
= cpu_map__new_data(data
);
38 TEST_ASSERT_VAL("wrong nr", map
->nr
== 20);
40 for (i
= 0; i
< 20; i
++) {
41 TEST_ASSERT_VAL("wrong cpu", map
->map
[i
] == i
);
44 perf_cpu_map__put(map
);
48 static int process_event_cpus(struct perf_tool
*tool __maybe_unused
,
49 union perf_event
*event
,
50 struct perf_sample
*sample __maybe_unused
,
51 struct machine
*machine __maybe_unused
)
53 struct perf_record_cpu_map
*map_event
= &event
->cpu_map
;
54 struct cpu_map_entries
*cpus
;
55 struct perf_record_cpu_map_data
*data
;
56 struct perf_cpu_map
*map
;
58 data
= &map_event
->data
;
60 TEST_ASSERT_VAL("wrong type", data
->type
== PERF_CPU_MAP__CPUS
);
62 cpus
= (struct cpu_map_entries
*)data
->data
;
64 TEST_ASSERT_VAL("wrong nr", cpus
->nr
== 2);
65 TEST_ASSERT_VAL("wrong cpu", cpus
->cpu
[0] == 1);
66 TEST_ASSERT_VAL("wrong cpu", cpus
->cpu
[1] == 256);
68 map
= cpu_map__new_data(data
);
69 TEST_ASSERT_VAL("wrong nr", map
->nr
== 2);
70 TEST_ASSERT_VAL("wrong cpu", map
->map
[0] == 1);
71 TEST_ASSERT_VAL("wrong cpu", map
->map
[1] == 256);
72 TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map
->refcnt
) == 1);
73 perf_cpu_map__put(map
);
78 int test__cpu_map_synthesize(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
80 struct perf_cpu_map
*cpus
;
82 /* This one is better stores in mask. */
83 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");
85 TEST_ASSERT_VAL("failed to synthesize map",
86 !perf_event__synthesize_cpu_map(NULL
, cpus
, process_event_mask
, NULL
));
88 perf_cpu_map__put(cpus
);
90 /* This one is better stores in cpu values. */
91 cpus
= perf_cpu_map__new("1,256");
93 TEST_ASSERT_VAL("failed to synthesize map",
94 !perf_event__synthesize_cpu_map(NULL
, cpus
, process_event_cpus
, NULL
));
96 perf_cpu_map__put(cpus
);
100 static int cpu_map_print(const char *str
)
102 struct perf_cpu_map
*map
= perf_cpu_map__new(str
);
108 cpu_map__snprint(map
, buf
, sizeof(buf
));
109 return !strcmp(buf
, str
);
112 int test__cpu_map_print(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
114 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1"));
115 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5"));
116 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40"));
117 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5"));
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,3-6,8-10,24,35-37"));
120 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40"));
124 int test__cpu_map_merge(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
126 struct perf_cpu_map
*a
= perf_cpu_map__new("4,2,1");
127 struct perf_cpu_map
*b
= perf_cpu_map__new("4,5,7");
128 struct perf_cpu_map
*c
= perf_cpu_map__merge(a
, b
);
131 TEST_ASSERT_VAL("failed to merge map: bad nr", c
->nr
== 5);
132 cpu_map__snprint(c
, buf
, sizeof(buf
));
133 TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf
, "1-2,4-5,7"));
134 perf_cpu_map__put(a
);
135 perf_cpu_map__put(b
);
136 perf_cpu_map__put(c
);