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