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() */ 24a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, }, 25f8ebb0cdSNamhyung Kim /* perf [perf] main() */ 26a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, }, 27f8ebb0cdSNamhyung Kim /* perf [perf] cmd_record() */ 28a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_CMD_RECORD, }, 29f8ebb0cdSNamhyung Kim /* bash [bash] xmalloc() */ 30a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, }, 31f8ebb0cdSNamhyung Kim /* bash [libc] malloc() */ 32a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, }, 33f8ebb0cdSNamhyung Kim }; 34f8ebb0cdSNamhyung Kim 35f8ebb0cdSNamhyung Kim static struct sample fake_samples[][5] = { 36f8ebb0cdSNamhyung Kim { 37f8ebb0cdSNamhyung Kim /* perf [perf] run_command() */ 38a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_RUN_COMMAND, }, 39f8ebb0cdSNamhyung Kim /* perf [libc] malloc() */ 40a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, }, 41f8ebb0cdSNamhyung Kim /* perf [kernel] page_fault() */ 42a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_PAGE_FAULT, }, 43f8ebb0cdSNamhyung Kim /* perf [kernel] sys_perf_event_open() */ 44a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN, }, 45f8ebb0cdSNamhyung Kim /* bash [libc] free() */ 46a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_FREE, }, 47f8ebb0cdSNamhyung Kim }, 48f8ebb0cdSNamhyung Kim { 49f8ebb0cdSNamhyung Kim /* perf [libc] free() */ 50a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_LIBC_FREE, }, 51f8ebb0cdSNamhyung Kim /* bash [libc] malloc() */ 52a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, }, /* will be merged */ 53f8ebb0cdSNamhyung Kim /* bash [bash] xfee() */ 54a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XFREE, }, 55f8ebb0cdSNamhyung Kim /* bash [libc] realloc() */ 56a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_REALLOC, }, 57f8ebb0cdSNamhyung Kim /* bash [kernel] page_fault() */ 58a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, }, 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; 67fd36f3ddSNamhyung Kim struct perf_sample sample = { .period = 1, .weight = 1, }; 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) { 764ea062edSArnaldo Carvalho de Melo struct hists *hists = evsel__hists(evsel); 774ea062edSArnaldo Carvalho de Melo 78f8ebb0cdSNamhyung Kim for (k = 0; k < ARRAY_SIZE(fake_common_samples); k++) { 79473398a2SArnaldo Carvalho de Melo sample.cpumode = PERF_RECORD_MISC_USER; 80ef89325fSAdrian Hunter sample.pid = fake_common_samples[k].pid; 8113ce34dfSNamhyung Kim sample.tid = fake_common_samples[k].pid; 82ef89325fSAdrian Hunter sample.ip = fake_common_samples[k].ip; 83bb3eb566SArnaldo Carvalho de Melo 84bb3eb566SArnaldo Carvalho de Melo if (machine__resolve(machine, &al, &sample) < 0) 85f8ebb0cdSNamhyung Kim goto out; 86f8ebb0cdSNamhyung Kim 87*0102ef3eSJiri Olsa he = hists__add_entry(hists, &al, NULL, 88fd36f3ddSNamhyung Kim NULL, NULL, &sample, true); 89b91fc39fSArnaldo Carvalho de Melo if (he == NULL) { 90b91fc39fSArnaldo Carvalho de Melo addr_location__put(&al); 91f8ebb0cdSNamhyung Kim goto out; 92b91fc39fSArnaldo Carvalho de Melo } 93f8ebb0cdSNamhyung Kim 94f8ebb0cdSNamhyung Kim fake_common_samples[k].thread = al.thread; 95f8ebb0cdSNamhyung Kim fake_common_samples[k].map = al.map; 96f8ebb0cdSNamhyung Kim fake_common_samples[k].sym = al.sym; 97f8ebb0cdSNamhyung Kim } 98f8ebb0cdSNamhyung Kim 99f8ebb0cdSNamhyung Kim for (k = 0; k < ARRAY_SIZE(fake_samples[i]); k++) { 100ef89325fSAdrian Hunter sample.pid = fake_samples[i][k].pid; 10113ce34dfSNamhyung Kim sample.tid = fake_samples[i][k].pid; 102ef89325fSAdrian Hunter sample.ip = fake_samples[i][k].ip; 103bb3eb566SArnaldo Carvalho de Melo if (machine__resolve(machine, &al, &sample) < 0) 104f8ebb0cdSNamhyung Kim goto out; 105f8ebb0cdSNamhyung Kim 106*0102ef3eSJiri Olsa he = hists__add_entry(hists, &al, NULL, 107fd36f3ddSNamhyung Kim NULL, NULL, &sample, true); 108b91fc39fSArnaldo Carvalho de Melo if (he == NULL) { 109b91fc39fSArnaldo Carvalho de Melo addr_location__put(&al); 110f8ebb0cdSNamhyung Kim goto out; 111b91fc39fSArnaldo Carvalho de Melo } 112f8ebb0cdSNamhyung Kim 113f8ebb0cdSNamhyung Kim fake_samples[i][k].thread = al.thread; 114f8ebb0cdSNamhyung Kim fake_samples[i][k].map = al.map; 115f8ebb0cdSNamhyung Kim fake_samples[i][k].sym = al.sym; 116f8ebb0cdSNamhyung Kim } 117f8ebb0cdSNamhyung Kim i++; 118f8ebb0cdSNamhyung Kim } 119f8ebb0cdSNamhyung Kim 120f8ebb0cdSNamhyung Kim return 0; 121f8ebb0cdSNamhyung Kim 122f8ebb0cdSNamhyung Kim out: 123f8ebb0cdSNamhyung Kim pr_debug("Not enough memory for adding a hist entry\n"); 124f8ebb0cdSNamhyung Kim return -1; 125f8ebb0cdSNamhyung Kim } 126f8ebb0cdSNamhyung Kim 127f8ebb0cdSNamhyung Kim static int find_sample(struct sample *samples, size_t nr_samples, 128f8ebb0cdSNamhyung Kim struct thread *t, struct map *m, struct symbol *s) 129f8ebb0cdSNamhyung Kim { 130f8ebb0cdSNamhyung Kim while (nr_samples--) { 131f8ebb0cdSNamhyung Kim if (samples->thread == t && samples->map == m && 132f8ebb0cdSNamhyung Kim samples->sym == s) 133f8ebb0cdSNamhyung Kim return 1; 134f8ebb0cdSNamhyung Kim samples++; 135f8ebb0cdSNamhyung Kim } 136f8ebb0cdSNamhyung Kim return 0; 137f8ebb0cdSNamhyung Kim } 138f8ebb0cdSNamhyung Kim 139f8ebb0cdSNamhyung Kim static int __validate_match(struct hists *hists) 140f8ebb0cdSNamhyung Kim { 141f8ebb0cdSNamhyung Kim size_t count = 0; 142f8ebb0cdSNamhyung Kim struct rb_root *root; 143f8ebb0cdSNamhyung Kim struct rb_node *node; 144f8ebb0cdSNamhyung Kim 145f8ebb0cdSNamhyung Kim /* 146f8ebb0cdSNamhyung Kim * Only entries from fake_common_samples should have a pair. 147f8ebb0cdSNamhyung Kim */ 14852225036SJiri Olsa if (hists__has(hists, need_collapse)) 149f8ebb0cdSNamhyung Kim root = &hists->entries_collapsed; 150f8ebb0cdSNamhyung Kim else 151f8ebb0cdSNamhyung Kim root = hists->entries_in; 152f8ebb0cdSNamhyung Kim 153f8ebb0cdSNamhyung Kim node = rb_first(root); 154f8ebb0cdSNamhyung Kim while (node) { 155f8ebb0cdSNamhyung Kim struct hist_entry *he; 156f8ebb0cdSNamhyung Kim 157f8ebb0cdSNamhyung Kim he = rb_entry(node, struct hist_entry, rb_node_in); 158f8ebb0cdSNamhyung Kim 159f8ebb0cdSNamhyung Kim if (hist_entry__has_pairs(he)) { 160f8ebb0cdSNamhyung Kim if (find_sample(fake_common_samples, 161f8ebb0cdSNamhyung Kim ARRAY_SIZE(fake_common_samples), 162f8ebb0cdSNamhyung Kim he->thread, he->ms.map, he->ms.sym)) { 163f8ebb0cdSNamhyung Kim count++; 164f8ebb0cdSNamhyung Kim } else { 165f8ebb0cdSNamhyung Kim pr_debug("Can't find the matched entry\n"); 166f8ebb0cdSNamhyung Kim return -1; 167f8ebb0cdSNamhyung Kim } 168f8ebb0cdSNamhyung Kim } 169f8ebb0cdSNamhyung Kim 170f8ebb0cdSNamhyung Kim node = rb_next(node); 171f8ebb0cdSNamhyung Kim } 172f8ebb0cdSNamhyung Kim 173f8ebb0cdSNamhyung Kim if (count != ARRAY_SIZE(fake_common_samples)) { 174f8ebb0cdSNamhyung Kim pr_debug("Invalid count for matched entries: %zd of %zd\n", 175f8ebb0cdSNamhyung Kim count, ARRAY_SIZE(fake_common_samples)); 176f8ebb0cdSNamhyung Kim return -1; 177f8ebb0cdSNamhyung Kim } 178f8ebb0cdSNamhyung Kim 179f8ebb0cdSNamhyung Kim return 0; 180f8ebb0cdSNamhyung Kim } 181f8ebb0cdSNamhyung Kim 182f8ebb0cdSNamhyung Kim static int validate_match(struct hists *leader, struct hists *other) 183f8ebb0cdSNamhyung Kim { 184f8ebb0cdSNamhyung Kim return __validate_match(leader) || __validate_match(other); 185f8ebb0cdSNamhyung Kim } 186f8ebb0cdSNamhyung Kim 187f8ebb0cdSNamhyung Kim static int __validate_link(struct hists *hists, int idx) 188f8ebb0cdSNamhyung Kim { 189f8ebb0cdSNamhyung Kim size_t count = 0; 190f8ebb0cdSNamhyung Kim size_t count_pair = 0; 191f8ebb0cdSNamhyung Kim size_t count_dummy = 0; 192f8ebb0cdSNamhyung Kim struct rb_root *root; 193f8ebb0cdSNamhyung Kim struct rb_node *node; 194f8ebb0cdSNamhyung Kim 195f8ebb0cdSNamhyung Kim /* 196f8ebb0cdSNamhyung Kim * Leader hists (idx = 0) will have dummy entries from other, 197f8ebb0cdSNamhyung Kim * and some entries will have no pair. However every entry 198f8ebb0cdSNamhyung Kim * in other hists should have (dummy) pair. 199f8ebb0cdSNamhyung Kim */ 20052225036SJiri Olsa if (hists__has(hists, need_collapse)) 201f8ebb0cdSNamhyung Kim root = &hists->entries_collapsed; 202f8ebb0cdSNamhyung Kim else 203f8ebb0cdSNamhyung Kim root = hists->entries_in; 204f8ebb0cdSNamhyung Kim 205f8ebb0cdSNamhyung Kim node = rb_first(root); 206f8ebb0cdSNamhyung Kim while (node) { 207f8ebb0cdSNamhyung Kim struct hist_entry *he; 208f8ebb0cdSNamhyung Kim 209f8ebb0cdSNamhyung Kim he = rb_entry(node, struct hist_entry, rb_node_in); 210f8ebb0cdSNamhyung Kim 211f8ebb0cdSNamhyung Kim if (hist_entry__has_pairs(he)) { 212f8ebb0cdSNamhyung Kim if (!find_sample(fake_common_samples, 213f8ebb0cdSNamhyung Kim ARRAY_SIZE(fake_common_samples), 214f8ebb0cdSNamhyung Kim he->thread, he->ms.map, he->ms.sym) && 215f8ebb0cdSNamhyung Kim !find_sample(fake_samples[idx], 216f8ebb0cdSNamhyung Kim ARRAY_SIZE(fake_samples[idx]), 217f8ebb0cdSNamhyung Kim he->thread, he->ms.map, he->ms.sym)) { 218f8ebb0cdSNamhyung Kim count_dummy++; 219f8ebb0cdSNamhyung Kim } 220f8ebb0cdSNamhyung Kim count_pair++; 221f8ebb0cdSNamhyung Kim } else if (idx) { 222f8ebb0cdSNamhyung Kim pr_debug("A entry from the other hists should have pair\n"); 223f8ebb0cdSNamhyung Kim return -1; 224f8ebb0cdSNamhyung Kim } 225f8ebb0cdSNamhyung Kim 226f8ebb0cdSNamhyung Kim count++; 227f8ebb0cdSNamhyung Kim node = rb_next(node); 228f8ebb0cdSNamhyung Kim } 229f8ebb0cdSNamhyung Kim 230f8ebb0cdSNamhyung Kim /* 231f8ebb0cdSNamhyung Kim * Note that we have a entry collapsed in the other (idx = 1) hists. 232f8ebb0cdSNamhyung Kim */ 233f8ebb0cdSNamhyung Kim if (idx == 0) { 234f8ebb0cdSNamhyung Kim if (count_dummy != ARRAY_SIZE(fake_samples[1]) - 1) { 235f8ebb0cdSNamhyung Kim pr_debug("Invalid count of dummy entries: %zd of %zd\n", 236f8ebb0cdSNamhyung Kim count_dummy, ARRAY_SIZE(fake_samples[1]) - 1); 237f8ebb0cdSNamhyung Kim return -1; 238f8ebb0cdSNamhyung Kim } 239f8ebb0cdSNamhyung Kim if (count != count_pair + ARRAY_SIZE(fake_samples[0])) { 240f8ebb0cdSNamhyung Kim pr_debug("Invalid count of total leader entries: %zd of %zd\n", 241f8ebb0cdSNamhyung Kim count, count_pair + ARRAY_SIZE(fake_samples[0])); 242f8ebb0cdSNamhyung Kim return -1; 243f8ebb0cdSNamhyung Kim } 244f8ebb0cdSNamhyung Kim } else { 245f8ebb0cdSNamhyung Kim if (count != count_pair) { 246f8ebb0cdSNamhyung Kim pr_debug("Invalid count of total other entries: %zd of %zd\n", 247f8ebb0cdSNamhyung Kim count, count_pair); 248f8ebb0cdSNamhyung Kim return -1; 249f8ebb0cdSNamhyung Kim } 250f8ebb0cdSNamhyung Kim if (count_dummy > 0) { 251f8ebb0cdSNamhyung Kim pr_debug("Other hists should not have dummy entries: %zd\n", 252f8ebb0cdSNamhyung Kim count_dummy); 253f8ebb0cdSNamhyung Kim return -1; 254f8ebb0cdSNamhyung Kim } 255f8ebb0cdSNamhyung Kim } 256f8ebb0cdSNamhyung Kim 257f8ebb0cdSNamhyung Kim return 0; 258f8ebb0cdSNamhyung Kim } 259f8ebb0cdSNamhyung Kim 260f8ebb0cdSNamhyung Kim static int validate_link(struct hists *leader, struct hists *other) 261f8ebb0cdSNamhyung Kim { 262f8ebb0cdSNamhyung Kim return __validate_link(leader, 0) || __validate_link(other, 1); 263f8ebb0cdSNamhyung Kim } 264f8ebb0cdSNamhyung Kim 265721a1f53SArnaldo Carvalho de Melo int test__hists_link(int subtest __maybe_unused) 266f8ebb0cdSNamhyung Kim { 267f8ebb0cdSNamhyung Kim int err = -1; 2684ea062edSArnaldo Carvalho de Melo struct hists *hists, *first_hists; 269876650e6SArnaldo Carvalho de Melo struct machines machines; 270f8ebb0cdSNamhyung Kim struct machine *machine = NULL; 271f8ebb0cdSNamhyung Kim struct perf_evsel *evsel, *first; 272334fe7a3SNamhyung Kim struct perf_evlist *evlist = perf_evlist__new(); 273f8ebb0cdSNamhyung Kim 274f8ebb0cdSNamhyung Kim if (evlist == NULL) 275f8ebb0cdSNamhyung Kim return -ENOMEM; 276f8ebb0cdSNamhyung Kim 277b39b8393SJiri Olsa err = parse_events(evlist, "cpu-clock", NULL); 278f8ebb0cdSNamhyung Kim if (err) 279f8ebb0cdSNamhyung Kim goto out; 280b39b8393SJiri Olsa err = parse_events(evlist, "task-clock", NULL); 281f8ebb0cdSNamhyung Kim if (err) 282f8ebb0cdSNamhyung Kim goto out; 283f8ebb0cdSNamhyung Kim 284b0500c16SWang Nan err = TEST_FAIL; 285f8ebb0cdSNamhyung Kim /* default sort order (comm,dso,sym) will be used */ 28640184c46SNamhyung Kim if (setup_sorting(NULL) < 0) 28755309985SNamhyung Kim goto out; 288f8ebb0cdSNamhyung Kim 289876650e6SArnaldo Carvalho de Melo machines__init(&machines); 290876650e6SArnaldo Carvalho de Melo 291f8ebb0cdSNamhyung Kim /* setup threads/dso/map/symbols also */ 292876650e6SArnaldo Carvalho de Melo machine = setup_fake_machine(&machines); 293f8ebb0cdSNamhyung Kim if (!machine) 294f8ebb0cdSNamhyung Kim goto out; 295f8ebb0cdSNamhyung Kim 296f8ebb0cdSNamhyung Kim if (verbose > 1) 297f8ebb0cdSNamhyung Kim machine__fprintf(machine, stderr); 298f8ebb0cdSNamhyung Kim 299f8ebb0cdSNamhyung Kim /* process sample events */ 300f8ebb0cdSNamhyung Kim err = add_hist_entries(evlist, machine); 301f8ebb0cdSNamhyung Kim if (err < 0) 302f8ebb0cdSNamhyung Kim goto out; 303f8ebb0cdSNamhyung Kim 3040050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 3054ea062edSArnaldo Carvalho de Melo hists = evsel__hists(evsel); 3064ea062edSArnaldo Carvalho de Melo hists__collapse_resort(hists, NULL); 307f8ebb0cdSNamhyung Kim 308f8ebb0cdSNamhyung Kim if (verbose > 2) 3094ea062edSArnaldo Carvalho de Melo print_hists_in(hists); 310f8ebb0cdSNamhyung Kim } 311f8ebb0cdSNamhyung Kim 312f8ebb0cdSNamhyung Kim first = perf_evlist__first(evlist); 313f8ebb0cdSNamhyung Kim evsel = perf_evlist__last(evlist); 314f8ebb0cdSNamhyung Kim 3154ea062edSArnaldo Carvalho de Melo first_hists = evsel__hists(first); 3164ea062edSArnaldo Carvalho de Melo hists = evsel__hists(evsel); 3174ea062edSArnaldo Carvalho de Melo 318f8ebb0cdSNamhyung Kim /* match common entries */ 3194ea062edSArnaldo Carvalho de Melo hists__match(first_hists, hists); 3204ea062edSArnaldo Carvalho de Melo err = validate_match(first_hists, hists); 321f8ebb0cdSNamhyung Kim if (err) 322f8ebb0cdSNamhyung Kim goto out; 323f8ebb0cdSNamhyung Kim 324f8ebb0cdSNamhyung Kim /* link common and/or dummy entries */ 3254ea062edSArnaldo Carvalho de Melo hists__link(first_hists, hists); 3264ea062edSArnaldo Carvalho de Melo err = validate_link(first_hists, 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); 335f21d1815SNamhyung Kim reset_output_field(); 336876650e6SArnaldo Carvalho de Melo machines__exit(&machines); 337f8ebb0cdSNamhyung Kim 338f8ebb0cdSNamhyung Kim return err; 339f8ebb0cdSNamhyung Kim } 340