1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 #include "tests.h" 2 #include "tests.h" 3 #include <stdio.h> 3 #include <stdio.h> 4 #include "cpumap.h" 4 #include "cpumap.h" 5 #include "event.h" 5 #include "event.h" 6 #include "util/synthetic-events.h" 6 #include "util/synthetic-events.h" 7 #include <string.h> 7 #include <string.h> 8 #include <linux/bitops.h> 8 #include <linux/bitops.h> 9 #include <internal/cpumap.h> !! 9 #include <perf/cpumap.h> 10 #include "debug.h" 10 #include "debug.h" 11 11 12 struct machine; 12 struct machine; 13 13 14 static int process_event_mask(const struct per !! 14 static int process_event_mask(struct perf_tool *tool __maybe_unused, 15 union perf_event *eve 15 union perf_event *event, 16 struct perf_sample *s 16 struct perf_sample *sample __maybe_unused, 17 struct machine *machi 17 struct machine *machine __maybe_unused) 18 { 18 { 19 struct perf_record_cpu_map *map_event 19 struct perf_record_cpu_map *map_event = &event->cpu_map; 20 struct perf_record_cpu_map_data *data; 20 struct perf_record_cpu_map_data *data; 21 struct perf_cpu_map *map; 21 struct perf_cpu_map *map; 22 unsigned int long_size; 22 unsigned int long_size; 23 23 24 data = &map_event->data; 24 data = &map_event->data; 25 25 26 TEST_ASSERT_VAL("wrong type", data->ty 26 TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__MASK); 27 27 28 long_size = data->mask32_data.long_siz 28 long_size = data->mask32_data.long_size; 29 29 30 TEST_ASSERT_VAL("wrong long_size", lon 30 TEST_ASSERT_VAL("wrong long_size", long_size == 4 || long_size == 8); 31 31 32 TEST_ASSERT_VAL("wrong nr", data->ma 32 TEST_ASSERT_VAL("wrong nr", data->mask32_data.nr == 1); 33 33 34 TEST_ASSERT_VAL("wrong cpu", perf_reco 34 TEST_ASSERT_VAL("wrong cpu", perf_record_cpu_map_data__test_bit(0, data)); 35 TEST_ASSERT_VAL("wrong cpu", !perf_rec 35 TEST_ASSERT_VAL("wrong cpu", !perf_record_cpu_map_data__test_bit(1, data)); 36 for (int i = 2; i <= 20; i++) 36 for (int i = 2; i <= 20; i++) 37 TEST_ASSERT_VAL("wrong cpu", p 37 TEST_ASSERT_VAL("wrong cpu", perf_record_cpu_map_data__test_bit(i, data)); 38 38 39 map = cpu_map__new_data(data); 39 map = cpu_map__new_data(data); 40 TEST_ASSERT_VAL("wrong nr", perf_cpu_ 40 TEST_ASSERT_VAL("wrong nr", perf_cpu_map__nr(map) == 20); 41 41 42 TEST_ASSERT_VAL("wrong cpu", perf_cpu_ 42 TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 0).cpu == 0); 43 for (int i = 2; i <= 20; i++) 43 for (int i = 2; i <= 20; i++) 44 TEST_ASSERT_VAL("wrong cpu", p 44 TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, i - 1).cpu == i); 45 45 46 perf_cpu_map__put(map); 46 perf_cpu_map__put(map); 47 return 0; 47 return 0; 48 } 48 } 49 49 50 static int process_event_cpus(const struct per !! 50 static int process_event_cpus(struct perf_tool *tool __maybe_unused, 51 union perf_event *eve 51 union perf_event *event, 52 struct perf_sample *s 52 struct perf_sample *sample __maybe_unused, 53 struct machine *machi 53 struct machine *machine __maybe_unused) 54 { 54 { 55 struct perf_record_cpu_map *map_event 55 struct perf_record_cpu_map *map_event = &event->cpu_map; 56 struct perf_record_cpu_map_data *data; 56 struct perf_record_cpu_map_data *data; 57 struct perf_cpu_map *map; 57 struct perf_cpu_map *map; 58 58 59 data = &map_event->data; 59 data = &map_event->data; 60 60 61 TEST_ASSERT_VAL("wrong type", data->ty 61 TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__CPUS); 62 62 63 TEST_ASSERT_VAL("wrong nr", data->cp 63 TEST_ASSERT_VAL("wrong nr", data->cpus_data.nr == 2); 64 TEST_ASSERT_VAL("wrong cpu", data->cp 64 TEST_ASSERT_VAL("wrong cpu", data->cpus_data.cpu[0] == 1); 65 TEST_ASSERT_VAL("wrong cpu", data->cp 65 TEST_ASSERT_VAL("wrong cpu", data->cpus_data.cpu[1] == 256); 66 66 67 map = cpu_map__new_data(data); 67 map = cpu_map__new_data(data); 68 TEST_ASSERT_VAL("wrong nr", perf_cpu_ 68 TEST_ASSERT_VAL("wrong nr", perf_cpu_map__nr(map) == 2); 69 TEST_ASSERT_VAL("wrong cpu", perf_cpu_ 69 TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 0).cpu == 1); 70 TEST_ASSERT_VAL("wrong cpu", perf_cpu_ 70 TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 1).cpu == 256); 71 TEST_ASSERT_VAL("wrong refcnt", refcou !! 71 TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1); 72 perf_cpu_map__put(map); 72 perf_cpu_map__put(map); 73 return 0; 73 return 0; 74 } 74 } 75 75 76 static int process_event_range_cpus(const stru !! 76 static int process_event_range_cpus(struct perf_tool *tool __maybe_unused, 77 union perf_eve 77 union perf_event *event, 78 struct perf_sa 78 struct perf_sample *sample __maybe_unused, 79 struct machine 79 struct machine *machine __maybe_unused) 80 { 80 { 81 struct perf_record_cpu_map *map_event 81 struct perf_record_cpu_map *map_event = &event->cpu_map; 82 struct perf_record_cpu_map_data *data; 82 struct perf_record_cpu_map_data *data; 83 struct perf_cpu_map *map; 83 struct perf_cpu_map *map; 84 84 85 data = &map_event->data; 85 data = &map_event->data; 86 86 87 TEST_ASSERT_VAL("wrong type", data->ty 87 TEST_ASSERT_VAL("wrong type", data->type == PERF_CPU_MAP__RANGE_CPUS); 88 88 89 TEST_ASSERT_VAL("wrong any_cpu", dat 89 TEST_ASSERT_VAL("wrong any_cpu", data->range_cpu_data.any_cpu == 0); 90 TEST_ASSERT_VAL("wrong start_cpu", dat 90 TEST_ASSERT_VAL("wrong start_cpu", data->range_cpu_data.start_cpu == 1); 91 TEST_ASSERT_VAL("wrong end_cpu", dat 91 TEST_ASSERT_VAL("wrong end_cpu", data->range_cpu_data.end_cpu == 256); 92 92 93 map = cpu_map__new_data(data); 93 map = cpu_map__new_data(data); 94 TEST_ASSERT_VAL("wrong nr", perf_cpu_ 94 TEST_ASSERT_VAL("wrong nr", perf_cpu_map__nr(map) == 256); 95 TEST_ASSERT_VAL("wrong cpu", perf_cpu_ 95 TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__cpu(map, 0).cpu == 1); 96 TEST_ASSERT_VAL("wrong cpu", perf_cpu_ 96 TEST_ASSERT_VAL("wrong cpu", perf_cpu_map__max(map).cpu == 256); 97 TEST_ASSERT_VAL("wrong refcnt", refcou !! 97 TEST_ASSERT_VAL("wrong refcnt", refcount_read(&map->refcnt) == 1); 98 perf_cpu_map__put(map); 98 perf_cpu_map__put(map); 99 return 0; 99 return 0; 100 } 100 } 101 101 102 102 103 static int test__cpu_map_synthesize(struct tes 103 static int test__cpu_map_synthesize(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 104 { 104 { 105 struct perf_cpu_map *cpus; 105 struct perf_cpu_map *cpus; 106 106 107 /* This one is better stored in a mask 107 /* This one is better stored in a mask. */ 108 cpus = perf_cpu_map__new("0,2-20"); 108 cpus = perf_cpu_map__new("0,2-20"); 109 109 110 TEST_ASSERT_VAL("failed to synthesize 110 TEST_ASSERT_VAL("failed to synthesize map", 111 !perf_event__synthesize_cpu_ma 111 !perf_event__synthesize_cpu_map(NULL, cpus, process_event_mask, NULL)); 112 112 113 perf_cpu_map__put(cpus); 113 perf_cpu_map__put(cpus); 114 114 115 /* This one is better stored in cpu va 115 /* This one is better stored in cpu values. */ 116 cpus = perf_cpu_map__new("1,256"); 116 cpus = perf_cpu_map__new("1,256"); 117 117 118 TEST_ASSERT_VAL("failed to synthesize 118 TEST_ASSERT_VAL("failed to synthesize map", 119 !perf_event__synthesize_cpu_ma 119 !perf_event__synthesize_cpu_map(NULL, cpus, process_event_cpus, NULL)); 120 120 121 perf_cpu_map__put(cpus); 121 perf_cpu_map__put(cpus); 122 122 123 /* This one is better stored as a rang 123 /* This one is better stored as a range. */ 124 cpus = perf_cpu_map__new("1-256"); 124 cpus = perf_cpu_map__new("1-256"); 125 125 126 TEST_ASSERT_VAL("failed to synthesize 126 TEST_ASSERT_VAL("failed to synthesize map", 127 !perf_event__synthesize_cpu_ma 127 !perf_event__synthesize_cpu_map(NULL, cpus, process_event_range_cpus, NULL)); 128 128 129 perf_cpu_map__put(cpus); 129 perf_cpu_map__put(cpus); 130 return 0; 130 return 0; 131 } 131 } 132 132 133 static int cpu_map_print(const char *str) 133 static int cpu_map_print(const char *str) 134 { 134 { 135 struct perf_cpu_map *map = perf_cpu_ma 135 struct perf_cpu_map *map = perf_cpu_map__new(str); 136 char buf[100]; 136 char buf[100]; 137 137 138 if (!map) 138 if (!map) 139 return -1; 139 return -1; 140 140 141 cpu_map__snprint(map, buf, sizeof(buf) 141 cpu_map__snprint(map, buf, sizeof(buf)); 142 perf_cpu_map__put(map); 142 perf_cpu_map__put(map); 143 143 144 return !strcmp(buf, str); 144 return !strcmp(buf, str); 145 } 145 } 146 146 147 static int test__cpu_map_print(struct test_sui 147 static int test__cpu_map_print(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 148 { 148 { 149 TEST_ASSERT_VAL("failed to convert map 149 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1")); 150 TEST_ASSERT_VAL("failed to convert map 150 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,5")); 151 TEST_ASSERT_VAL("failed to convert map 151 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3,5,7,9,11,13,15,17,19,21-40")); 152 TEST_ASSERT_VAL("failed to convert map 152 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("2-5")); 153 TEST_ASSERT_VAL("failed to convert map 153 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37")); 154 TEST_ASSERT_VAL("failed to convert map 154 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1,3-6,8-10,24,35-37")); 155 TEST_ASSERT_VAL("failed to convert map 155 TEST_ASSERT_VAL("failed to convert map", cpu_map_print("1-10,12-20,22-30,32-40")); 156 return 0; 156 return 0; 157 } 157 } 158 158 159 static int test__cpu_map_merge(struct test_sui 159 static int test__cpu_map_merge(struct test_suite *test __maybe_unused, int subtest __maybe_unused) 160 { 160 { 161 struct perf_cpu_map *a = perf_cpu_map_ 161 struct perf_cpu_map *a = perf_cpu_map__new("4,2,1"); 162 struct perf_cpu_map *b = perf_cpu_map_ 162 struct perf_cpu_map *b = perf_cpu_map__new("4,5,7"); 163 struct perf_cpu_map *c = perf_cpu_map_ 163 struct perf_cpu_map *c = perf_cpu_map__merge(a, b); 164 char buf[100]; 164 char buf[100]; 165 165 166 TEST_ASSERT_VAL("failed to merge map: 166 TEST_ASSERT_VAL("failed to merge map: bad nr", perf_cpu_map__nr(c) == 5); 167 cpu_map__snprint(c, buf, sizeof(buf)); 167 cpu_map__snprint(c, buf, sizeof(buf)); 168 TEST_ASSERT_VAL("failed to merge map: 168 TEST_ASSERT_VAL("failed to merge map: bad result", !strcmp(buf, "1-2,4-5,7")); 169 perf_cpu_map__put(b); 169 perf_cpu_map__put(b); 170 perf_cpu_map__put(c); 170 perf_cpu_map__put(c); 171 return 0; 171 return 0; 172 } 172 } 173 173 174 static int __test__cpu_map_intersect(const cha !! 174 DEFINE_SUITE("Synthesize cpu map", cpu_map_synthesize); 175 { !! 175 DEFINE_SUITE("Print cpu map", cpu_map_print); 176 struct perf_cpu_map *a = perf_cpu_map_ !! 176 DEFINE_SUITE("Merge cpu map", cpu_map_merge); 177 struct perf_cpu_map *b = perf_cpu_map_ << 178 struct perf_cpu_map *c = perf_cpu_map_ << 179 char buf[100]; << 180 << 181 TEST_ASSERT_EQUAL("failed to intersect << 182 cpu_map__snprint(c, buf, sizeof(buf)); << 183 TEST_ASSERT_VAL("failed to intersect m << 184 perf_cpu_map__put(a); << 185 perf_cpu_map__put(b); << 186 perf_cpu_map__put(c); << 187 return 0; << 188 } << 189 << 190 static int test__cpu_map_intersect(struct test << 191 int subtest << 192 { << 193 int ret; << 194 << 195 ret = __test__cpu_map_intersect("4,2,1 << 196 if (ret) << 197 return ret; << 198 ret = __test__cpu_map_intersect("1-8", << 199 if (ret) << 200 return ret; << 201 ret = __test__cpu_map_intersect("1-8,1 << 202 if (ret) << 203 return ret; << 204 ret = __test__cpu_map_intersect("4,2,1 << 205 if (ret) << 206 return ret; << 207 ret = __test__cpu_map_intersect("1", " << 208 if (ret) << 209 return ret; << 210 ret = __test__cpu_map_intersect("1", " << 211 return ret; << 212 } << 213 << 214 static int test__cpu_map_equal(struct test_sui << 215 { << 216 struct perf_cpu_map *any = perf_cpu_ma << 217 struct perf_cpu_map *one = perf_cpu_ma << 218 struct perf_cpu_map *two = perf_cpu_ma << 219 struct perf_cpu_map *empty = perf_cpu_ << 220 struct perf_cpu_map *pair = perf_cpu_m << 221 struct perf_cpu_map *tmp; << 222 struct perf_cpu_map *maps[] = {empty, << 223 << 224 for (size_t i = 0; i < ARRAY_SIZE(maps << 225 /* Maps equal themself. */ << 226 TEST_ASSERT_VAL("equal", perf_ << 227 for (size_t j = 0; j < ARRAY_S << 228 /* Maps dont't equal e << 229 if (i == j) << 230 continue; << 231 TEST_ASSERT_VAL("not e << 232 } << 233 } << 234 << 235 /* Maps equal made maps. */ << 236 tmp = perf_cpu_map__merge(perf_cpu_map << 237 TEST_ASSERT_VAL("pair", perf_cpu_map__ << 238 perf_cpu_map__put(tmp); << 239 << 240 tmp = perf_cpu_map__intersect(pair, on << 241 TEST_ASSERT_VAL("one", perf_cpu_map__e << 242 perf_cpu_map__put(tmp); << 243 << 244 for (size_t i = 0; i < ARRAY_SIZE(maps << 245 perf_cpu_map__put(maps[i]); << 246 << 247 return TEST_OK; << 248 } << 249 << 250 static struct test_case tests__cpu_map[] = { << 251 TEST_CASE("Synthesize cpu map", cpu_ma << 252 TEST_CASE("Print cpu map", cpu_map_pri << 253 TEST_CASE("Merge cpu map", cpu_map_mer << 254 TEST_CASE("Intersect cpu map", cpu_map << 255 TEST_CASE("Equal cpu map", cpu_map_equ << 256 { .name = NULL, } << 257 }; << 258 << 259 struct test_suite suite__cpu_map = { << 260 .desc = "CPU map", << 261 .test_cases = tests__cpu_map, << 262 }; << 263 177
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.