1 #include "perf.h" 2 #include "tests.h" 3 #include "debug.h" 4 #include "symbol.h" 5 #include "sort.h" 6 #include "evsel.h" 7 #include "evlist.h" 8 #include "machine.h" 9 #include "thread.h" 10 #include "parse-events.h" 11 #include "hists_common.h" 12 13 struct sample { 14 u32 pid; 15 u64 ip; 16 struct thread *thread; 17 struct map *map; 18 struct symbol *sym; 19 }; 20 21 /* For the numbers, see hists_common.c */ 22 static struct sample fake_common_samples[] = { 23 /* perf [kernel] schedule() */ 24 { .pid = 100, .ip = 0xf0000 + 700, }, 25 /* perf [perf] main() */ 26 { .pid = 200, .ip = 0x40000 + 700, }, 27 /* perf [perf] cmd_record() */ 28 { .pid = 200, .ip = 0x40000 + 900, }, 29 /* bash [bash] xmalloc() */ 30 { .pid = 300, .ip = 0x40000 + 800, }, 31 /* bash [libc] malloc() */ 32 { .pid = 300, .ip = 0x50000 + 700, }, 33 }; 34 35 static struct sample fake_samples[][5] = { 36 { 37 /* perf [perf] run_command() */ 38 { .pid = 100, .ip = 0x40000 + 800, }, 39 /* perf [libc] malloc() */ 40 { .pid = 100, .ip = 0x50000 + 700, }, 41 /* perf [kernel] page_fault() */ 42 { .pid = 100, .ip = 0xf0000 + 800, }, 43 /* perf [kernel] sys_perf_event_open() */ 44 { .pid = 200, .ip = 0xf0000 + 900, }, 45 /* bash [libc] free() */ 46 { .pid = 300, .ip = 0x50000 + 800, }, 47 }, 48 { 49 /* perf [libc] free() */ 50 { .pid = 200, .ip = 0x50000 + 800, }, 51 /* bash [libc] malloc() */ 52 { .pid = 300, .ip = 0x50000 + 700, }, /* will be merged */ 53 /* bash [bash] xfee() */ 54 { .pid = 300, .ip = 0x40000 + 900, }, 55 /* bash [libc] realloc() */ 56 { .pid = 300, .ip = 0x50000 + 900, }, 57 /* bash [kernel] page_fault() */ 58 { .pid = 300, .ip = 0xf0000 + 800, }, 59 }, 60 }; 61 62 static int add_hist_entries(struct perf_evlist *evlist, struct machine *machine) 63 { 64 struct perf_evsel *evsel; 65 struct addr_location al; 66 struct hist_entry *he; 67 struct perf_sample sample = { .cpu = 0, }; 68 size_t i = 0, k; 69 70 /* 71 * each evsel will have 10 samples - 5 common and 5 distinct. 72 * However the second evsel also has a collapsed entry for 73 * "bash [libc] malloc" so total 9 entries will be in the tree. 74 */ 75 evlist__for_each(evlist, evsel) { 76 for (k = 0; k < ARRAY_SIZE(fake_common_samples); k++) { 77 const union perf_event event = { 78 .header = { 79 .misc = PERF_RECORD_MISC_USER, 80 }, 81 }; 82 83 sample.pid = fake_common_samples[k].pid; 84 sample.ip = fake_common_samples[k].ip; 85 if (perf_event__preprocess_sample(&event, machine, &al, 86 &sample) < 0) 87 goto out; 88 89 he = __hists__add_entry(&evsel->hists, &al, NULL, 90 NULL, NULL, 1, 1, 0); 91 if (he == NULL) 92 goto out; 93 94 fake_common_samples[k].thread = al.thread; 95 fake_common_samples[k].map = al.map; 96 fake_common_samples[k].sym = al.sym; 97 } 98 99 for (k = 0; k < ARRAY_SIZE(fake_samples[i]); k++) { 100 const union perf_event event = { 101 .header = { 102 .misc = PERF_RECORD_MISC_USER, 103 }, 104 }; 105 106 sample.pid = fake_samples[i][k].pid; 107 sample.ip = fake_samples[i][k].ip; 108 if (perf_event__preprocess_sample(&event, machine, &al, 109 &sample) < 0) 110 goto out; 111 112 he = __hists__add_entry(&evsel->hists, &al, NULL, 113 NULL, NULL, 1, 1, 0); 114 if (he == NULL) 115 goto out; 116 117 fake_samples[i][k].thread = al.thread; 118 fake_samples[i][k].map = al.map; 119 fake_samples[i][k].sym = al.sym; 120 } 121 i++; 122 } 123 124 return 0; 125 126 out: 127 pr_debug("Not enough memory for adding a hist entry\n"); 128 return -1; 129 } 130 131 static int find_sample(struct sample *samples, size_t nr_samples, 132 struct thread *t, struct map *m, struct symbol *s) 133 { 134 while (nr_samples--) { 135 if (samples->thread == t && samples->map == m && 136 samples->sym == s) 137 return 1; 138 samples++; 139 } 140 return 0; 141 } 142 143 static int __validate_match(struct hists *hists) 144 { 145 size_t count = 0; 146 struct rb_root *root; 147 struct rb_node *node; 148 149 /* 150 * Only entries from fake_common_samples should have a pair. 151 */ 152 if (sort__need_collapse) 153 root = &hists->entries_collapsed; 154 else 155 root = hists->entries_in; 156 157 node = rb_first(root); 158 while (node) { 159 struct hist_entry *he; 160 161 he = rb_entry(node, struct hist_entry, rb_node_in); 162 163 if (hist_entry__has_pairs(he)) { 164 if (find_sample(fake_common_samples, 165 ARRAY_SIZE(fake_common_samples), 166 he->thread, he->ms.map, he->ms.sym)) { 167 count++; 168 } else { 169 pr_debug("Can't find the matched entry\n"); 170 return -1; 171 } 172 } 173 174 node = rb_next(node); 175 } 176 177 if (count != ARRAY_SIZE(fake_common_samples)) { 178 pr_debug("Invalid count for matched entries: %zd of %zd\n", 179 count, ARRAY_SIZE(fake_common_samples)); 180 return -1; 181 } 182 183 return 0; 184 } 185 186 static int validate_match(struct hists *leader, struct hists *other) 187 { 188 return __validate_match(leader) || __validate_match(other); 189 } 190 191 static int __validate_link(struct hists *hists, int idx) 192 { 193 size_t count = 0; 194 size_t count_pair = 0; 195 size_t count_dummy = 0; 196 struct rb_root *root; 197 struct rb_node *node; 198 199 /* 200 * Leader hists (idx = 0) will have dummy entries from other, 201 * and some entries will have no pair. However every entry 202 * in other hists should have (dummy) pair. 203 */ 204 if (sort__need_collapse) 205 root = &hists->entries_collapsed; 206 else 207 root = hists->entries_in; 208 209 node = rb_first(root); 210 while (node) { 211 struct hist_entry *he; 212 213 he = rb_entry(node, struct hist_entry, rb_node_in); 214 215 if (hist_entry__has_pairs(he)) { 216 if (!find_sample(fake_common_samples, 217 ARRAY_SIZE(fake_common_samples), 218 he->thread, he->ms.map, he->ms.sym) && 219 !find_sample(fake_samples[idx], 220 ARRAY_SIZE(fake_samples[idx]), 221 he->thread, he->ms.map, he->ms.sym)) { 222 count_dummy++; 223 } 224 count_pair++; 225 } else if (idx) { 226 pr_debug("A entry from the other hists should have pair\n"); 227 return -1; 228 } 229 230 count++; 231 node = rb_next(node); 232 } 233 234 /* 235 * Note that we have a entry collapsed in the other (idx = 1) hists. 236 */ 237 if (idx == 0) { 238 if (count_dummy != ARRAY_SIZE(fake_samples[1]) - 1) { 239 pr_debug("Invalid count of dummy entries: %zd of %zd\n", 240 count_dummy, ARRAY_SIZE(fake_samples[1]) - 1); 241 return -1; 242 } 243 if (count != count_pair + ARRAY_SIZE(fake_samples[0])) { 244 pr_debug("Invalid count of total leader entries: %zd of %zd\n", 245 count, count_pair + ARRAY_SIZE(fake_samples[0])); 246 return -1; 247 } 248 } else { 249 if (count != count_pair) { 250 pr_debug("Invalid count of total other entries: %zd of %zd\n", 251 count, count_pair); 252 return -1; 253 } 254 if (count_dummy > 0) { 255 pr_debug("Other hists should not have dummy entries: %zd\n", 256 count_dummy); 257 return -1; 258 } 259 } 260 261 return 0; 262 } 263 264 static int validate_link(struct hists *leader, struct hists *other) 265 { 266 return __validate_link(leader, 0) || __validate_link(other, 1); 267 } 268 269 static void print_hists(struct hists *hists) 270 { 271 int i = 0; 272 struct rb_root *root; 273 struct rb_node *node; 274 275 if (sort__need_collapse) 276 root = &hists->entries_collapsed; 277 else 278 root = hists->entries_in; 279 280 pr_info("----- %s --------\n", __func__); 281 node = rb_first(root); 282 while (node) { 283 struct hist_entry *he; 284 285 he = rb_entry(node, struct hist_entry, rb_node_in); 286 287 pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n", 288 i, thread__comm_str(he->thread), he->ms.map->dso->short_name, 289 he->ms.sym->name, he->stat.period); 290 291 i++; 292 node = rb_next(node); 293 } 294 } 295 296 int test__hists_link(void) 297 { 298 int err = -1; 299 struct machines machines; 300 struct machine *machine = NULL; 301 struct perf_evsel *evsel, *first; 302 struct perf_evlist *evlist = perf_evlist__new(); 303 304 if (evlist == NULL) 305 return -ENOMEM; 306 307 err = parse_events(evlist, "cpu-clock"); 308 if (err) 309 goto out; 310 err = parse_events(evlist, "task-clock"); 311 if (err) 312 goto out; 313 314 /* default sort order (comm,dso,sym) will be used */ 315 if (setup_sorting() < 0) 316 goto out; 317 318 machines__init(&machines); 319 320 /* setup threads/dso/map/symbols also */ 321 machine = setup_fake_machine(&machines); 322 if (!machine) 323 goto out; 324 325 if (verbose > 1) 326 machine__fprintf(machine, stderr); 327 328 /* process sample events */ 329 err = add_hist_entries(evlist, machine); 330 if (err < 0) 331 goto out; 332 333 evlist__for_each(evlist, evsel) { 334 hists__collapse_resort(&evsel->hists, NULL); 335 336 if (verbose > 2) 337 print_hists(&evsel->hists); 338 } 339 340 first = perf_evlist__first(evlist); 341 evsel = perf_evlist__last(evlist); 342 343 /* match common entries */ 344 hists__match(&first->hists, &evsel->hists); 345 err = validate_match(&first->hists, &evsel->hists); 346 if (err) 347 goto out; 348 349 /* link common and/or dummy entries */ 350 hists__link(&first->hists, &evsel->hists); 351 err = validate_link(&first->hists, &evsel->hists); 352 if (err) 353 goto out; 354 355 err = 0; 356 357 out: 358 /* tear down everything */ 359 perf_evlist__delete(evlist); 360 machines__exit(&machines); 361 362 return err; 363 } 364