hists_link.c 11 KB

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