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" 12*877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h> 13f8ebb0cdSNamhyung Kim 14f8ebb0cdSNamhyung Kim struct sample { 15f8ebb0cdSNamhyung Kim u32 pid; 16f8ebb0cdSNamhyung Kim u64 ip; 17f8ebb0cdSNamhyung Kim struct thread *thread; 18f8ebb0cdSNamhyung Kim struct map *map; 19f8ebb0cdSNamhyung Kim struct symbol *sym; 20f8ebb0cdSNamhyung Kim }; 21f8ebb0cdSNamhyung Kim 226e344a95SNamhyung Kim /* For the numbers, see hists_common.c */ 23f8ebb0cdSNamhyung Kim static struct sample fake_common_samples[] = { 24f8ebb0cdSNamhyung Kim /* perf [kernel] schedule() */ 25a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, }, 26f8ebb0cdSNamhyung Kim /* perf [perf] main() */ 27a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, }, 28f8ebb0cdSNamhyung Kim /* perf [perf] cmd_record() */ 29a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_CMD_RECORD, }, 30f8ebb0cdSNamhyung Kim /* bash [bash] xmalloc() */ 31a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, }, 32f8ebb0cdSNamhyung Kim /* bash [libc] malloc() */ 33a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, }, 34f8ebb0cdSNamhyung Kim }; 35f8ebb0cdSNamhyung Kim 36f8ebb0cdSNamhyung Kim static struct sample fake_samples[][5] = { 37f8ebb0cdSNamhyung Kim { 38f8ebb0cdSNamhyung Kim /* perf [perf] run_command() */ 39a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_RUN_COMMAND, }, 40f8ebb0cdSNamhyung Kim /* perf [libc] malloc() */ 41a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, }, 42f8ebb0cdSNamhyung Kim /* perf [kernel] page_fault() */ 43a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_PAGE_FAULT, }, 44f8ebb0cdSNamhyung Kim /* perf [kernel] sys_perf_event_open() */ 45a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN, }, 46f8ebb0cdSNamhyung Kim /* bash [libc] free() */ 47a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_FREE, }, 48f8ebb0cdSNamhyung Kim }, 49f8ebb0cdSNamhyung Kim { 50f8ebb0cdSNamhyung Kim /* perf [libc] free() */ 51a1891aa4SNamhyung Kim { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_LIBC_FREE, }, 52f8ebb0cdSNamhyung Kim /* bash [libc] malloc() */ 53a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, }, /* will be merged */ 54f8ebb0cdSNamhyung Kim /* bash [bash] xfee() */ 55a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XFREE, }, 56f8ebb0cdSNamhyung Kim /* bash [libc] realloc() */ 57a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_REALLOC, }, 58f8ebb0cdSNamhyung Kim /* bash [kernel] page_fault() */ 59a1891aa4SNamhyung Kim { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, }, 60f8ebb0cdSNamhyung Kim }, 61f8ebb0cdSNamhyung Kim }; 62f8ebb0cdSNamhyung Kim 63f8ebb0cdSNamhyung Kim static int add_hist_entries(struct perf_evlist *evlist, struct machine *machine) 64f8ebb0cdSNamhyung Kim { 65f8ebb0cdSNamhyung Kim struct perf_evsel *evsel; 66f8ebb0cdSNamhyung Kim struct addr_location al; 67f8ebb0cdSNamhyung Kim struct hist_entry *he; 68fd36f3ddSNamhyung Kim struct perf_sample sample = { .period = 1, .weight = 1, }; 69f8ebb0cdSNamhyung Kim size_t i = 0, k; 70f8ebb0cdSNamhyung Kim 71f8ebb0cdSNamhyung Kim /* 72f8ebb0cdSNamhyung Kim * each evsel will have 10 samples - 5 common and 5 distinct. 73f8ebb0cdSNamhyung Kim * However the second evsel also has a collapsed entry for 74f8ebb0cdSNamhyung Kim * "bash [libc] malloc" so total 9 entries will be in the tree. 75f8ebb0cdSNamhyung Kim */ 76e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 774ea062edSArnaldo Carvalho de Melo struct hists *hists = evsel__hists(evsel); 784ea062edSArnaldo Carvalho de Melo 79f8ebb0cdSNamhyung Kim for (k = 0; k < ARRAY_SIZE(fake_common_samples); k++) { 80473398a2SArnaldo Carvalho de Melo sample.cpumode = PERF_RECORD_MISC_USER; 81ef89325fSAdrian Hunter sample.pid = fake_common_samples[k].pid; 8213ce34dfSNamhyung Kim sample.tid = fake_common_samples[k].pid; 83ef89325fSAdrian Hunter sample.ip = fake_common_samples[k].ip; 84bb3eb566SArnaldo Carvalho de Melo 85bb3eb566SArnaldo Carvalho de Melo if (machine__resolve(machine, &al, &sample) < 0) 86f8ebb0cdSNamhyung Kim goto out; 87f8ebb0cdSNamhyung Kim 880102ef3eSJiri Olsa he = hists__add_entry(hists, &al, NULL, 89fd36f3ddSNamhyung Kim NULL, NULL, &sample, true); 90b91fc39fSArnaldo Carvalho de Melo if (he == NULL) { 91b91fc39fSArnaldo Carvalho de Melo addr_location__put(&al); 92f8ebb0cdSNamhyung Kim goto out; 93b91fc39fSArnaldo Carvalho de Melo } 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++) { 101ef89325fSAdrian Hunter sample.pid = fake_samples[i][k].pid; 10213ce34dfSNamhyung Kim sample.tid = fake_samples[i][k].pid; 103ef89325fSAdrian Hunter sample.ip = fake_samples[i][k].ip; 104bb3eb566SArnaldo Carvalho de Melo if (machine__resolve(machine, &al, &sample) < 0) 105f8ebb0cdSNamhyung Kim goto out; 106f8ebb0cdSNamhyung Kim 1070102ef3eSJiri Olsa he = hists__add_entry(hists, &al, NULL, 108fd36f3ddSNamhyung Kim NULL, NULL, &sample, true); 109b91fc39fSArnaldo Carvalho de Melo if (he == NULL) { 110b91fc39fSArnaldo Carvalho de Melo addr_location__put(&al); 111f8ebb0cdSNamhyung Kim goto out; 112b91fc39fSArnaldo Carvalho de Melo } 113f8ebb0cdSNamhyung Kim 114f8ebb0cdSNamhyung Kim fake_samples[i][k].thread = al.thread; 115f8ebb0cdSNamhyung Kim fake_samples[i][k].map = al.map; 116f8ebb0cdSNamhyung Kim fake_samples[i][k].sym = al.sym; 117f8ebb0cdSNamhyung Kim } 118f8ebb0cdSNamhyung Kim i++; 119f8ebb0cdSNamhyung Kim } 120f8ebb0cdSNamhyung Kim 121f8ebb0cdSNamhyung Kim return 0; 122f8ebb0cdSNamhyung Kim 123f8ebb0cdSNamhyung Kim out: 124f8ebb0cdSNamhyung Kim pr_debug("Not enough memory for adding a hist entry\n"); 125f8ebb0cdSNamhyung Kim return -1; 126f8ebb0cdSNamhyung Kim } 127f8ebb0cdSNamhyung Kim 128f8ebb0cdSNamhyung Kim static int find_sample(struct sample *samples, size_t nr_samples, 129f8ebb0cdSNamhyung Kim struct thread *t, struct map *m, struct symbol *s) 130f8ebb0cdSNamhyung Kim { 131f8ebb0cdSNamhyung Kim while (nr_samples--) { 132f8ebb0cdSNamhyung Kim if (samples->thread == t && samples->map == m && 133f8ebb0cdSNamhyung Kim samples->sym == s) 134f8ebb0cdSNamhyung Kim return 1; 135f8ebb0cdSNamhyung Kim samples++; 136f8ebb0cdSNamhyung Kim } 137f8ebb0cdSNamhyung Kim return 0; 138f8ebb0cdSNamhyung Kim } 139f8ebb0cdSNamhyung Kim 140f8ebb0cdSNamhyung Kim static int __validate_match(struct hists *hists) 141f8ebb0cdSNamhyung Kim { 142f8ebb0cdSNamhyung Kim size_t count = 0; 143f8ebb0cdSNamhyung Kim struct rb_root *root; 144f8ebb0cdSNamhyung Kim struct rb_node *node; 145f8ebb0cdSNamhyung Kim 146f8ebb0cdSNamhyung Kim /* 147f8ebb0cdSNamhyung Kim * Only entries from fake_common_samples should have a pair. 148f8ebb0cdSNamhyung Kim */ 14952225036SJiri Olsa if (hists__has(hists, need_collapse)) 150f8ebb0cdSNamhyung Kim root = &hists->entries_collapsed; 151f8ebb0cdSNamhyung Kim else 152f8ebb0cdSNamhyung Kim root = hists->entries_in; 153f8ebb0cdSNamhyung Kim 154f8ebb0cdSNamhyung Kim node = rb_first(root); 155f8ebb0cdSNamhyung Kim while (node) { 156f8ebb0cdSNamhyung Kim struct hist_entry *he; 157f8ebb0cdSNamhyung Kim 158f8ebb0cdSNamhyung Kim he = rb_entry(node, struct hist_entry, rb_node_in); 159f8ebb0cdSNamhyung Kim 160f8ebb0cdSNamhyung Kim if (hist_entry__has_pairs(he)) { 161f8ebb0cdSNamhyung Kim if (find_sample(fake_common_samples, 162f8ebb0cdSNamhyung Kim ARRAY_SIZE(fake_common_samples), 163f8ebb0cdSNamhyung Kim he->thread, he->ms.map, he->ms.sym)) { 164f8ebb0cdSNamhyung Kim count++; 165f8ebb0cdSNamhyung Kim } else { 166f8ebb0cdSNamhyung Kim pr_debug("Can't find the matched entry\n"); 167f8ebb0cdSNamhyung Kim return -1; 168f8ebb0cdSNamhyung Kim } 169f8ebb0cdSNamhyung Kim } 170f8ebb0cdSNamhyung Kim 171f8ebb0cdSNamhyung Kim node = rb_next(node); 172f8ebb0cdSNamhyung Kim } 173f8ebb0cdSNamhyung Kim 174f8ebb0cdSNamhyung Kim if (count != ARRAY_SIZE(fake_common_samples)) { 175f8ebb0cdSNamhyung Kim pr_debug("Invalid count for matched entries: %zd of %zd\n", 176f8ebb0cdSNamhyung Kim count, ARRAY_SIZE(fake_common_samples)); 177f8ebb0cdSNamhyung Kim return -1; 178f8ebb0cdSNamhyung Kim } 179f8ebb0cdSNamhyung Kim 180f8ebb0cdSNamhyung Kim return 0; 181f8ebb0cdSNamhyung Kim } 182f8ebb0cdSNamhyung Kim 183f8ebb0cdSNamhyung Kim static int validate_match(struct hists *leader, struct hists *other) 184f8ebb0cdSNamhyung Kim { 185f8ebb0cdSNamhyung Kim return __validate_match(leader) || __validate_match(other); 186f8ebb0cdSNamhyung Kim } 187f8ebb0cdSNamhyung Kim 188f8ebb0cdSNamhyung Kim static int __validate_link(struct hists *hists, int idx) 189f8ebb0cdSNamhyung Kim { 190f8ebb0cdSNamhyung Kim size_t count = 0; 191f8ebb0cdSNamhyung Kim size_t count_pair = 0; 192f8ebb0cdSNamhyung Kim size_t count_dummy = 0; 193f8ebb0cdSNamhyung Kim struct rb_root *root; 194f8ebb0cdSNamhyung Kim struct rb_node *node; 195f8ebb0cdSNamhyung Kim 196f8ebb0cdSNamhyung Kim /* 197f8ebb0cdSNamhyung Kim * Leader hists (idx = 0) will have dummy entries from other, 198f8ebb0cdSNamhyung Kim * and some entries will have no pair. However every entry 199f8ebb0cdSNamhyung Kim * in other hists should have (dummy) pair. 200f8ebb0cdSNamhyung Kim */ 20152225036SJiri Olsa if (hists__has(hists, need_collapse)) 202f8ebb0cdSNamhyung Kim root = &hists->entries_collapsed; 203f8ebb0cdSNamhyung Kim else 204f8ebb0cdSNamhyung Kim root = hists->entries_in; 205f8ebb0cdSNamhyung Kim 206f8ebb0cdSNamhyung Kim node = rb_first(root); 207f8ebb0cdSNamhyung Kim while (node) { 208f8ebb0cdSNamhyung Kim struct hist_entry *he; 209f8ebb0cdSNamhyung Kim 210f8ebb0cdSNamhyung Kim he = rb_entry(node, struct hist_entry, rb_node_in); 211f8ebb0cdSNamhyung Kim 212f8ebb0cdSNamhyung Kim if (hist_entry__has_pairs(he)) { 213f8ebb0cdSNamhyung Kim if (!find_sample(fake_common_samples, 214f8ebb0cdSNamhyung Kim ARRAY_SIZE(fake_common_samples), 215f8ebb0cdSNamhyung Kim he->thread, he->ms.map, he->ms.sym) && 216f8ebb0cdSNamhyung Kim !find_sample(fake_samples[idx], 217f8ebb0cdSNamhyung Kim ARRAY_SIZE(fake_samples[idx]), 218f8ebb0cdSNamhyung Kim he->thread, he->ms.map, he->ms.sym)) { 219f8ebb0cdSNamhyung Kim count_dummy++; 220f8ebb0cdSNamhyung Kim } 221f8ebb0cdSNamhyung Kim count_pair++; 222f8ebb0cdSNamhyung Kim } else if (idx) { 223f8ebb0cdSNamhyung Kim pr_debug("A entry from the other hists should have pair\n"); 224f8ebb0cdSNamhyung Kim return -1; 225f8ebb0cdSNamhyung Kim } 226f8ebb0cdSNamhyung Kim 227f8ebb0cdSNamhyung Kim count++; 228f8ebb0cdSNamhyung Kim node = rb_next(node); 229f8ebb0cdSNamhyung Kim } 230f8ebb0cdSNamhyung Kim 231f8ebb0cdSNamhyung Kim /* 232f8ebb0cdSNamhyung Kim * Note that we have a entry collapsed in the other (idx = 1) hists. 233f8ebb0cdSNamhyung Kim */ 234f8ebb0cdSNamhyung Kim if (idx == 0) { 235f8ebb0cdSNamhyung Kim if (count_dummy != ARRAY_SIZE(fake_samples[1]) - 1) { 236f8ebb0cdSNamhyung Kim pr_debug("Invalid count of dummy entries: %zd of %zd\n", 237f8ebb0cdSNamhyung Kim count_dummy, ARRAY_SIZE(fake_samples[1]) - 1); 238f8ebb0cdSNamhyung Kim return -1; 239f8ebb0cdSNamhyung Kim } 240f8ebb0cdSNamhyung Kim if (count != count_pair + ARRAY_SIZE(fake_samples[0])) { 241f8ebb0cdSNamhyung Kim pr_debug("Invalid count of total leader entries: %zd of %zd\n", 242f8ebb0cdSNamhyung Kim count, count_pair + ARRAY_SIZE(fake_samples[0])); 243f8ebb0cdSNamhyung Kim return -1; 244f8ebb0cdSNamhyung Kim } 245f8ebb0cdSNamhyung Kim } else { 246f8ebb0cdSNamhyung Kim if (count != count_pair) { 247f8ebb0cdSNamhyung Kim pr_debug("Invalid count of total other entries: %zd of %zd\n", 248f8ebb0cdSNamhyung Kim count, count_pair); 249f8ebb0cdSNamhyung Kim return -1; 250f8ebb0cdSNamhyung Kim } 251f8ebb0cdSNamhyung Kim if (count_dummy > 0) { 252f8ebb0cdSNamhyung Kim pr_debug("Other hists should not have dummy entries: %zd\n", 253f8ebb0cdSNamhyung Kim count_dummy); 254f8ebb0cdSNamhyung Kim return -1; 255f8ebb0cdSNamhyung Kim } 256f8ebb0cdSNamhyung Kim } 257f8ebb0cdSNamhyung Kim 258f8ebb0cdSNamhyung Kim return 0; 259f8ebb0cdSNamhyung Kim } 260f8ebb0cdSNamhyung Kim 261f8ebb0cdSNamhyung Kim static int validate_link(struct hists *leader, struct hists *other) 262f8ebb0cdSNamhyung Kim { 263f8ebb0cdSNamhyung Kim return __validate_link(leader, 0) || __validate_link(other, 1); 264f8ebb0cdSNamhyung Kim } 265f8ebb0cdSNamhyung Kim 266721a1f53SArnaldo Carvalho de Melo int test__hists_link(int subtest __maybe_unused) 267f8ebb0cdSNamhyung Kim { 268f8ebb0cdSNamhyung Kim int err = -1; 2694ea062edSArnaldo Carvalho de Melo struct hists *hists, *first_hists; 270876650e6SArnaldo Carvalho de Melo struct machines machines; 271f8ebb0cdSNamhyung Kim struct machine *machine = NULL; 272f8ebb0cdSNamhyung Kim struct perf_evsel *evsel, *first; 273334fe7a3SNamhyung Kim struct perf_evlist *evlist = perf_evlist__new(); 274f8ebb0cdSNamhyung Kim 275f8ebb0cdSNamhyung Kim if (evlist == NULL) 276f8ebb0cdSNamhyung Kim return -ENOMEM; 277f8ebb0cdSNamhyung Kim 278b39b8393SJiri Olsa err = parse_events(evlist, "cpu-clock", NULL); 279f8ebb0cdSNamhyung Kim if (err) 280f8ebb0cdSNamhyung Kim goto out; 281b39b8393SJiri Olsa err = parse_events(evlist, "task-clock", NULL); 282f8ebb0cdSNamhyung Kim if (err) 283f8ebb0cdSNamhyung Kim goto out; 284f8ebb0cdSNamhyung Kim 285b0500c16SWang Nan err = TEST_FAIL; 286f8ebb0cdSNamhyung Kim /* default sort order (comm,dso,sym) will be used */ 28740184c46SNamhyung Kim if (setup_sorting(NULL) < 0) 28855309985SNamhyung Kim goto out; 289f8ebb0cdSNamhyung Kim 290876650e6SArnaldo Carvalho de Melo machines__init(&machines); 291876650e6SArnaldo Carvalho de Melo 292f8ebb0cdSNamhyung Kim /* setup threads/dso/map/symbols also */ 293876650e6SArnaldo Carvalho de Melo machine = setup_fake_machine(&machines); 294f8ebb0cdSNamhyung Kim if (!machine) 295f8ebb0cdSNamhyung Kim goto out; 296f8ebb0cdSNamhyung Kim 297f8ebb0cdSNamhyung Kim if (verbose > 1) 298f8ebb0cdSNamhyung Kim machine__fprintf(machine, stderr); 299f8ebb0cdSNamhyung Kim 300f8ebb0cdSNamhyung Kim /* process sample events */ 301f8ebb0cdSNamhyung Kim err = add_hist_entries(evlist, machine); 302f8ebb0cdSNamhyung Kim if (err < 0) 303f8ebb0cdSNamhyung Kim goto out; 304f8ebb0cdSNamhyung Kim 305e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3064ea062edSArnaldo Carvalho de Melo hists = evsel__hists(evsel); 3074ea062edSArnaldo Carvalho de Melo hists__collapse_resort(hists, NULL); 308f8ebb0cdSNamhyung Kim 309f8ebb0cdSNamhyung Kim if (verbose > 2) 3104ea062edSArnaldo Carvalho de Melo print_hists_in(hists); 311f8ebb0cdSNamhyung Kim } 312f8ebb0cdSNamhyung Kim 313f8ebb0cdSNamhyung Kim first = perf_evlist__first(evlist); 314f8ebb0cdSNamhyung Kim evsel = perf_evlist__last(evlist); 315f8ebb0cdSNamhyung Kim 3164ea062edSArnaldo Carvalho de Melo first_hists = evsel__hists(first); 3174ea062edSArnaldo Carvalho de Melo hists = evsel__hists(evsel); 3184ea062edSArnaldo Carvalho de Melo 319f8ebb0cdSNamhyung Kim /* match common entries */ 3204ea062edSArnaldo Carvalho de Melo hists__match(first_hists, hists); 3214ea062edSArnaldo Carvalho de Melo err = validate_match(first_hists, hists); 322f8ebb0cdSNamhyung Kim if (err) 323f8ebb0cdSNamhyung Kim goto out; 324f8ebb0cdSNamhyung Kim 325f8ebb0cdSNamhyung Kim /* link common and/or dummy entries */ 3264ea062edSArnaldo Carvalho de Melo hists__link(first_hists, hists); 3274ea062edSArnaldo Carvalho de Melo err = validate_link(first_hists, hists); 328f8ebb0cdSNamhyung Kim if (err) 329f8ebb0cdSNamhyung Kim goto out; 330f8ebb0cdSNamhyung Kim 331f8ebb0cdSNamhyung Kim err = 0; 332f8ebb0cdSNamhyung Kim 333f8ebb0cdSNamhyung Kim out: 334f8ebb0cdSNamhyung Kim /* tear down everything */ 335f8ebb0cdSNamhyung Kim perf_evlist__delete(evlist); 336f21d1815SNamhyung Kim reset_output_field(); 337876650e6SArnaldo Carvalho de Melo machines__exit(&machines); 338f8ebb0cdSNamhyung Kim 339f8ebb0cdSNamhyung Kim return err; 340f8ebb0cdSNamhyung Kim } 341