hists_link.c 11 KB

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