1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 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 "parse-events.h" 106e344a95SNamhyung Kim #include "hists_common.h" 11e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h" 12a43783aeSArnaldo Carvalho de Melo #include <errno.h> 13877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h> 14f8ebb0cdSNamhyung Kim 15f8ebb0cdSNamhyung Kim struct sample { 16f8ebb0cdSNamhyung Kim u32 pid; 17f8ebb0cdSNamhyung Kim u64 ip; 18f8ebb0cdSNamhyung Kim struct thread *thread; 19f8ebb0cdSNamhyung Kim struct map *map; 20f8ebb0cdSNamhyung Kim struct symbol *sym; 21f8ebb0cdSNamhyung Kim }; 22f8ebb0cdSNamhyung Kim 236e344a95SNamhyung Kim /* For the numbers, see hists_common.c */ 24f8ebb0cdSNamhyung Kim static struct sample fake_common_samples[] = { 25f8ebb0cdSNamhyung Kim /* perf [kernel] schedule() */ 26a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, }, 27f8ebb0cdSNamhyung Kim /* perf [perf] main() */ 28a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, }, 29f8ebb0cdSNamhyung Kim /* perf [perf] cmd_record() */ 30a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_CMD_RECORD, }, 31f8ebb0cdSNamhyung Kim /* bash [bash] xmalloc() */ 32a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, }, 33f8ebb0cdSNamhyung Kim /* bash [libc] malloc() */ 34a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, }, 35f8ebb0cdSNamhyung Kim }; 36f8ebb0cdSNamhyung Kim 37f8ebb0cdSNamhyung Kim static struct sample fake_samples[][5] = { 38f8ebb0cdSNamhyung Kim { 39f8ebb0cdSNamhyung Kim /* perf [perf] run_command() */ 40a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_RUN_COMMAND, }, 41f8ebb0cdSNamhyung Kim /* perf [libc] malloc() */ 42a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, }, 43f8ebb0cdSNamhyung Kim /* perf [kernel] page_fault() */ 44a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_PAGE_FAULT, }, 45f8ebb0cdSNamhyung Kim /* perf [kernel] sys_perf_event_open() */ 46a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN, }, 47f8ebb0cdSNamhyung Kim /* bash [libc] free() */ 48a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_FREE, }, 49f8ebb0cdSNamhyung Kim }, 50f8ebb0cdSNamhyung Kim { 51f8ebb0cdSNamhyung Kim /* perf [libc] free() */ 52a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_LIBC_FREE, }, 53f8ebb0cdSNamhyung Kim /* bash [libc] malloc() */ 54a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, }, /* will be merged */ 55f8ebb0cdSNamhyung Kim /* bash [bash] xfee() */ 56a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XFREE, }, 57f8ebb0cdSNamhyung Kim /* bash [libc] realloc() */ 58a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_REALLOC, }, 59f8ebb0cdSNamhyung Kim /* bash [kernel] page_fault() */ 60a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, }, 61f8ebb0cdSNamhyung Kim }, 62f8ebb0cdSNamhyung Kim }; 63f8ebb0cdSNamhyung Kim 6463503dbaSJiri Olsa static int add_hist_entries(struct evlist *evlist, struct machine *machine) 65f8ebb0cdSNamhyung Kim { 6632dcd021SJiri Olsa struct evsel *evsel; 67f8ebb0cdSNamhyung Kim struct addr_location al; 68f8ebb0cdSNamhyung Kim struct hist_entry *he; 69fd36f3ddSNamhyung Kim struct perf_sample sample = { .period = 1, .weight = 1, }; 70f8ebb0cdSNamhyung Kim size_t i = 0, k; 71f8ebb0cdSNamhyung Kim 72f8ebb0cdSNamhyung Kim /* 73f8ebb0cdSNamhyung Kim * each evsel will have 10 samples - 5 common and 5 distinct. 74f8ebb0cdSNamhyung Kim * However the second evsel also has a collapsed entry for 75f8ebb0cdSNamhyung Kim * "bash [libc] malloc" so total 9 entries will be in the tree. 76f8ebb0cdSNamhyung Kim */ 77e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 784ea062edSArnaldo Carvalho de Melo struct hists *hists = evsel__hists(evsel); 794ea062edSArnaldo Carvalho de Melo 80f8ebb0cdSNamhyung Kim for (k = 0; k < ARRAY_SIZE(fake_common_samples); k++) { 81473398a2SArnaldo Carvalho de Melo sample.cpumode = PERF_RECORD_MISC_USER; 82ef89325fSAdrian Hunter sample.pid = fake_common_samples[k].pid; 8313ce34dfSNamhyung Kim sample.tid = fake_common_samples[k].pid; 84ef89325fSAdrian Hunter sample.ip = fake_common_samples[k].ip; 85bb3eb566SArnaldo Carvalho de Melo 86bb3eb566SArnaldo Carvalho de Melo if (machine__resolve(machine, &al, &sample) < 0) 87f8ebb0cdSNamhyung Kim goto out; 88f8ebb0cdSNamhyung Kim 890102ef3eSJiri Olsa he = hists__add_entry(hists, &al, NULL, 90fd36f3ddSNamhyung Kim NULL, NULL, &sample, true); 91b91fc39fSArnaldo Carvalho de Melo if (he == NULL) { 92b91fc39fSArnaldo Carvalho de Melo addr_location__put(&al); 93f8ebb0cdSNamhyung Kim goto out; 94b91fc39fSArnaldo Carvalho de Melo } 95f8ebb0cdSNamhyung Kim 96f8ebb0cdSNamhyung Kim fake_common_samples[k].thread = al.thread; 97f8ebb0cdSNamhyung Kim fake_common_samples[k].map = al.map; 98f8ebb0cdSNamhyung Kim fake_common_samples[k].sym = al.sym; 99f8ebb0cdSNamhyung Kim } 100f8ebb0cdSNamhyung Kim 101f8ebb0cdSNamhyung Kim for (k = 0; k < ARRAY_SIZE(fake_samples[i]); k++) { 102ef89325fSAdrian Hunter sample.pid = fake_samples[i][k].pid; 10313ce34dfSNamhyung Kim sample.tid = fake_samples[i][k].pid; 104ef89325fSAdrian Hunter sample.ip = fake_samples[i][k].ip; 105bb3eb566SArnaldo Carvalho de Melo if (machine__resolve(machine, &al, &sample) < 0) 106f8ebb0cdSNamhyung Kim goto out; 107f8ebb0cdSNamhyung Kim 1080102ef3eSJiri Olsa he = hists__add_entry(hists, &al, NULL, 109fd36f3ddSNamhyung Kim NULL, NULL, &sample, true); 110b91fc39fSArnaldo Carvalho de Melo if (he == NULL) { 111b91fc39fSArnaldo Carvalho de Melo addr_location__put(&al); 112f8ebb0cdSNamhyung Kim goto out; 113b91fc39fSArnaldo Carvalho de Melo } 114f8ebb0cdSNamhyung Kim 115f8ebb0cdSNamhyung Kim fake_samples[i][k].thread = al.thread; 116f8ebb0cdSNamhyung Kim fake_samples[i][k].map = al.map; 117f8ebb0cdSNamhyung Kim fake_samples[i][k].sym = al.sym; 118f8ebb0cdSNamhyung Kim } 119f8ebb0cdSNamhyung Kim i++; 120f8ebb0cdSNamhyung Kim } 121f8ebb0cdSNamhyung Kim 122f8ebb0cdSNamhyung Kim return 0; 123f8ebb0cdSNamhyung Kim 124f8ebb0cdSNamhyung Kim out: 125f8ebb0cdSNamhyung Kim pr_debug("Not enough memory for adding a hist entry\n"); 126f8ebb0cdSNamhyung Kim return -1; 127f8ebb0cdSNamhyung Kim } 128f8ebb0cdSNamhyung Kim 129f8ebb0cdSNamhyung Kim static int find_sample(struct sample *samples, size_t nr_samples, 130f8ebb0cdSNamhyung Kim struct thread *t, struct map *m, struct symbol *s) 131f8ebb0cdSNamhyung Kim { 132f8ebb0cdSNamhyung Kim while (nr_samples--) { 133f8ebb0cdSNamhyung Kim if (samples->thread == t && samples->map == m && 134f8ebb0cdSNamhyung Kim samples->sym == s) 135f8ebb0cdSNamhyung Kim return 1; 136f8ebb0cdSNamhyung Kim samples++; 137f8ebb0cdSNamhyung Kim } 138f8ebb0cdSNamhyung Kim return 0; 139f8ebb0cdSNamhyung Kim } 140f8ebb0cdSNamhyung Kim 141f8ebb0cdSNamhyung Kim static int __validate_match(struct hists *hists) 142f8ebb0cdSNamhyung Kim { 143f8ebb0cdSNamhyung Kim size_t count = 0; 1442eb3d689SDavidlohr Bueso struct rb_root_cached *root; 145f8ebb0cdSNamhyung Kim struct rb_node *node; 146f8ebb0cdSNamhyung Kim 147f8ebb0cdSNamhyung Kim /* 148f8ebb0cdSNamhyung Kim * Only entries from fake_common_samples should have a pair. 149f8ebb0cdSNamhyung Kim */ 15052225036SJiri Olsa if (hists__has(hists, need_collapse)) 151f8ebb0cdSNamhyung Kim root = &hists->entries_collapsed; 152f8ebb0cdSNamhyung Kim else 153f8ebb0cdSNamhyung Kim root = hists->entries_in; 154f8ebb0cdSNamhyung Kim 1552eb3d689SDavidlohr Bueso node = rb_first_cached(root); 156f8ebb0cdSNamhyung Kim while (node) { 157f8ebb0cdSNamhyung Kim struct hist_entry *he; 158f8ebb0cdSNamhyung Kim 159f8ebb0cdSNamhyung Kim he = rb_entry(node, struct hist_entry, rb_node_in); 160f8ebb0cdSNamhyung Kim 161f8ebb0cdSNamhyung Kim if (hist_entry__has_pairs(he)) { 162f8ebb0cdSNamhyung Kim if (find_sample(fake_common_samples, 163f8ebb0cdSNamhyung Kim ARRAY_SIZE(fake_common_samples), 164f8ebb0cdSNamhyung Kim he->thread, he->ms.map, he->ms.sym)) { 165f8ebb0cdSNamhyung Kim count++; 166f8ebb0cdSNamhyung Kim } else { 167f8ebb0cdSNamhyung Kim pr_debug("Can't find the matched entry\n"); 168f8ebb0cdSNamhyung Kim return -1; 169f8ebb0cdSNamhyung Kim } 170f8ebb0cdSNamhyung Kim } 171f8ebb0cdSNamhyung Kim 172f8ebb0cdSNamhyung Kim node = rb_next(node); 173f8ebb0cdSNamhyung Kim } 174f8ebb0cdSNamhyung Kim 175f8ebb0cdSNamhyung Kim if (count != ARRAY_SIZE(fake_common_samples)) { 176f8ebb0cdSNamhyung Kim pr_debug("Invalid count for matched entries: %zd of %zd\n", 177f8ebb0cdSNamhyung Kim count, ARRAY_SIZE(fake_common_samples)); 178f8ebb0cdSNamhyung Kim return -1; 179f8ebb0cdSNamhyung Kim } 180f8ebb0cdSNamhyung Kim 181f8ebb0cdSNamhyung Kim return 0; 182f8ebb0cdSNamhyung Kim } 183f8ebb0cdSNamhyung Kim 184f8ebb0cdSNamhyung Kim static int validate_match(struct hists *leader, struct hists *other) 185f8ebb0cdSNamhyung Kim { 186f8ebb0cdSNamhyung Kim return __validate_match(leader) || __validate_match(other); 187f8ebb0cdSNamhyung Kim } 188f8ebb0cdSNamhyung Kim 189f8ebb0cdSNamhyung Kim static int __validate_link(struct hists *hists, int idx) 190f8ebb0cdSNamhyung Kim { 191f8ebb0cdSNamhyung Kim size_t count = 0; 192f8ebb0cdSNamhyung Kim size_t count_pair = 0; 193f8ebb0cdSNamhyung Kim size_t count_dummy = 0; 1942eb3d689SDavidlohr Bueso struct rb_root_cached *root; 195f8ebb0cdSNamhyung Kim struct rb_node *node; 196f8ebb0cdSNamhyung Kim 197f8ebb0cdSNamhyung Kim /* 198f8ebb0cdSNamhyung Kim * Leader hists (idx = 0) will have dummy entries from other, 199f8ebb0cdSNamhyung Kim * and some entries will have no pair. However every entry 200f8ebb0cdSNamhyung Kim * in other hists should have (dummy) pair. 201f8ebb0cdSNamhyung Kim */ 20252225036SJiri Olsa if (hists__has(hists, need_collapse)) 203f8ebb0cdSNamhyung Kim root = &hists->entries_collapsed; 204f8ebb0cdSNamhyung Kim else 205f8ebb0cdSNamhyung Kim root = hists->entries_in; 206f8ebb0cdSNamhyung Kim 2072eb3d689SDavidlohr Bueso node = rb_first_cached(root); 208f8ebb0cdSNamhyung Kim while (node) { 209f8ebb0cdSNamhyung Kim struct hist_entry *he; 210f8ebb0cdSNamhyung Kim 211f8ebb0cdSNamhyung Kim he = rb_entry(node, struct hist_entry, rb_node_in); 212f8ebb0cdSNamhyung Kim 213f8ebb0cdSNamhyung Kim if (hist_entry__has_pairs(he)) { 214f8ebb0cdSNamhyung Kim if (!find_sample(fake_common_samples, 215f8ebb0cdSNamhyung Kim ARRAY_SIZE(fake_common_samples), 216f8ebb0cdSNamhyung Kim he->thread, he->ms.map, he->ms.sym) && 217f8ebb0cdSNamhyung Kim !find_sample(fake_samples[idx], 218f8ebb0cdSNamhyung Kim ARRAY_SIZE(fake_samples[idx]), 219f8ebb0cdSNamhyung Kim he->thread, he->ms.map, he->ms.sym)) { 220f8ebb0cdSNamhyung Kim count_dummy++; 221f8ebb0cdSNamhyung Kim } 222f8ebb0cdSNamhyung Kim count_pair++; 223f8ebb0cdSNamhyung Kim } else if (idx) { 224f8ebb0cdSNamhyung Kim pr_debug("A entry from the other hists should have pair\n"); 225f8ebb0cdSNamhyung Kim return -1; 226f8ebb0cdSNamhyung Kim } 227f8ebb0cdSNamhyung Kim 228f8ebb0cdSNamhyung Kim count++; 229f8ebb0cdSNamhyung Kim node = rb_next(node); 230f8ebb0cdSNamhyung Kim } 231f8ebb0cdSNamhyung Kim 232f8ebb0cdSNamhyung Kim /* 233f8ebb0cdSNamhyung Kim * Note that we have a entry collapsed in the other (idx = 1) hists. 234f8ebb0cdSNamhyung Kim */ 235f8ebb0cdSNamhyung Kim if (idx == 0) { 236f8ebb0cdSNamhyung Kim if (count_dummy != ARRAY_SIZE(fake_samples[1]) - 1) { 237f8ebb0cdSNamhyung Kim pr_debug("Invalid count of dummy entries: %zd of %zd\n", 238f8ebb0cdSNamhyung Kim count_dummy, ARRAY_SIZE(fake_samples[1]) - 1); 239f8ebb0cdSNamhyung Kim return -1; 240f8ebb0cdSNamhyung Kim } 241f8ebb0cdSNamhyung Kim if (count != count_pair + ARRAY_SIZE(fake_samples[0])) { 242f8ebb0cdSNamhyung Kim pr_debug("Invalid count of total leader entries: %zd of %zd\n", 243f8ebb0cdSNamhyung Kim count, count_pair + ARRAY_SIZE(fake_samples[0])); 244f8ebb0cdSNamhyung Kim return -1; 245f8ebb0cdSNamhyung Kim } 246f8ebb0cdSNamhyung Kim } else { 247f8ebb0cdSNamhyung Kim if (count != count_pair) { 248f8ebb0cdSNamhyung Kim pr_debug("Invalid count of total other entries: %zd of %zd\n", 249f8ebb0cdSNamhyung Kim count, count_pair); 250f8ebb0cdSNamhyung Kim return -1; 251f8ebb0cdSNamhyung Kim } 252f8ebb0cdSNamhyung Kim if (count_dummy > 0) { 253f8ebb0cdSNamhyung Kim pr_debug("Other hists should not have dummy entries: %zd\n", 254f8ebb0cdSNamhyung Kim count_dummy); 255f8ebb0cdSNamhyung Kim return -1; 256f8ebb0cdSNamhyung Kim } 257f8ebb0cdSNamhyung Kim } 258f8ebb0cdSNamhyung Kim 259f8ebb0cdSNamhyung Kim return 0; 260f8ebb0cdSNamhyung Kim } 261f8ebb0cdSNamhyung Kim 262f8ebb0cdSNamhyung Kim static int validate_link(struct hists *leader, struct hists *other) 263f8ebb0cdSNamhyung Kim { 264f8ebb0cdSNamhyung Kim return __validate_link(leader, 0) || __validate_link(other, 1); 265f8ebb0cdSNamhyung Kim } 266f8ebb0cdSNamhyung Kim 26781f17c90SArnaldo Carvalho de Melo int test__hists_link(struct test *test __maybe_unused, int subtest __maybe_unused) 268f8ebb0cdSNamhyung Kim { 269f8ebb0cdSNamhyung Kim int err = -1; 2704ea062edSArnaldo Carvalho de Melo struct hists *hists, *first_hists; 271876650e6SArnaldo Carvalho de Melo struct machines machines; 272f8ebb0cdSNamhyung Kim struct machine *machine = NULL; 27332dcd021SJiri Olsa struct evsel *evsel, *first; 2740f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 275f8ebb0cdSNamhyung Kim 276f8ebb0cdSNamhyung Kim if (evlist == NULL) 277f8ebb0cdSNamhyung Kim return -ENOMEM; 278f8ebb0cdSNamhyung Kim 279b39b8393SJiri Olsa err = parse_events(evlist, "cpu-clock", NULL); 280f8ebb0cdSNamhyung Kim if (err) 281f8ebb0cdSNamhyung Kim goto out; 282b39b8393SJiri Olsa err = parse_events(evlist, "task-clock", NULL); 283f8ebb0cdSNamhyung Kim if (err) 284f8ebb0cdSNamhyung Kim goto out; 285f8ebb0cdSNamhyung Kim 286b0500c16SWang Nan err = TEST_FAIL; 287f8ebb0cdSNamhyung Kim /* default sort order (comm,dso,sym) will be used */ 28840184c46SNamhyung Kim if (setup_sorting(NULL) < 0) 28955309985SNamhyung Kim goto out; 290f8ebb0cdSNamhyung Kim 291876650e6SArnaldo Carvalho de Melo machines__init(&machines); 292876650e6SArnaldo Carvalho de Melo 293f8ebb0cdSNamhyung Kim /* setup threads/dso/map/symbols also */ 294876650e6SArnaldo Carvalho de Melo machine = setup_fake_machine(&machines); 295f8ebb0cdSNamhyung Kim if (!machine) 296f8ebb0cdSNamhyung Kim goto out; 297f8ebb0cdSNamhyung Kim 298f8ebb0cdSNamhyung Kim if (verbose > 1) 299f8ebb0cdSNamhyung Kim machine__fprintf(machine, stderr); 300f8ebb0cdSNamhyung Kim 301f8ebb0cdSNamhyung Kim /* process sample events */ 302f8ebb0cdSNamhyung Kim err = add_hist_entries(evlist, machine); 303f8ebb0cdSNamhyung Kim if (err < 0) 304f8ebb0cdSNamhyung Kim goto out; 305f8ebb0cdSNamhyung Kim 306e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3074ea062edSArnaldo Carvalho de Melo hists = evsel__hists(evsel); 3084ea062edSArnaldo Carvalho de Melo hists__collapse_resort(hists, NULL); 309f8ebb0cdSNamhyung Kim 310f8ebb0cdSNamhyung Kim if (verbose > 2) 3114ea062edSArnaldo Carvalho de Melo print_hists_in(hists); 312f8ebb0cdSNamhyung Kim } 313f8ebb0cdSNamhyung Kim 314*515dbe48SJiri Olsa first = evlist__first(evlist); 315*515dbe48SJiri Olsa evsel = evlist__last(evlist); 316f8ebb0cdSNamhyung Kim 3174ea062edSArnaldo Carvalho de Melo first_hists = evsel__hists(first); 3184ea062edSArnaldo Carvalho de Melo hists = evsel__hists(evsel); 3194ea062edSArnaldo Carvalho de Melo 320f8ebb0cdSNamhyung Kim /* match common entries */ 3214ea062edSArnaldo Carvalho de Melo hists__match(first_hists, hists); 3224ea062edSArnaldo Carvalho de Melo err = validate_match(first_hists, hists); 323f8ebb0cdSNamhyung Kim if (err) 324f8ebb0cdSNamhyung Kim goto out; 325f8ebb0cdSNamhyung Kim 326f8ebb0cdSNamhyung Kim /* link common and/or dummy entries */ 3274ea062edSArnaldo Carvalho de Melo hists__link(first_hists, hists); 3284ea062edSArnaldo Carvalho de Melo err = validate_link(first_hists, hists); 329f8ebb0cdSNamhyung Kim if (err) 330f8ebb0cdSNamhyung Kim goto out; 331f8ebb0cdSNamhyung Kim 332f8ebb0cdSNamhyung Kim err = 0; 333f8ebb0cdSNamhyung Kim 334f8ebb0cdSNamhyung Kim out: 335f8ebb0cdSNamhyung Kim /* tear down everything */ 336c12995a5SJiri Olsa evlist__delete(evlist); 337f21d1815SNamhyung Kim reset_output_field(); 338876650e6SArnaldo Carvalho de Melo machines__exit(&machines); 339f8ebb0cdSNamhyung Kim 340f8ebb0cdSNamhyung Kim return err; 341f8ebb0cdSNamhyung Kim } 342