hists_link.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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, 0);
  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, NULL);
  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,
  195. NULL, NULL, 1, 1, 0);
  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. .header = {
  205. .misc = PERF_RECORD_MISC_USER,
  206. },
  207. };
  208. sample.pid = fake_samples[i][k].pid;
  209. sample.ip = fake_samples[i][k].ip;
  210. if (perf_event__preprocess_sample(&event, machine, &al,
  211. &sample) < 0)
  212. goto out;
  213. he = __hists__add_entry(&evsel->hists, &al, NULL,
  214. NULL, NULL, 1, 1, 0);
  215. if (he == NULL)
  216. goto out;
  217. fake_samples[i][k].thread = al.thread;
  218. fake_samples[i][k].map = al.map;
  219. fake_samples[i][k].sym = al.sym;
  220. }
  221. i++;
  222. }
  223. return 0;
  224. out:
  225. pr_debug("Not enough memory for adding a hist entry\n");
  226. return -1;
  227. }
  228. static int find_sample(struct sample *samples, size_t nr_samples,
  229. struct thread *t, struct map *m, struct symbol *s)
  230. {
  231. while (nr_samples--) {
  232. if (samples->thread == t && samples->map == m &&
  233. samples->sym == s)
  234. return 1;
  235. samples++;
  236. }
  237. return 0;
  238. }
  239. static int __validate_match(struct hists *hists)
  240. {
  241. size_t count = 0;
  242. struct rb_root *root;
  243. struct rb_node *node;
  244. /*
  245. * Only entries from fake_common_samples should have a pair.
  246. */
  247. if (sort__need_collapse)
  248. root = &hists->entries_collapsed;
  249. else
  250. root = hists->entries_in;
  251. node = rb_first(root);
  252. while (node) {
  253. struct hist_entry *he;
  254. he = rb_entry(node, struct hist_entry, rb_node_in);
  255. if (hist_entry__has_pairs(he)) {
  256. if (find_sample(fake_common_samples,
  257. ARRAY_SIZE(fake_common_samples),
  258. he->thread, he->ms.map, he->ms.sym)) {
  259. count++;
  260. } else {
  261. pr_debug("Can't find the matched entry\n");
  262. return -1;
  263. }
  264. }
  265. node = rb_next(node);
  266. }
  267. if (count != ARRAY_SIZE(fake_common_samples)) {
  268. pr_debug("Invalid count for matched entries: %zd of %zd\n",
  269. count, ARRAY_SIZE(fake_common_samples));
  270. return -1;
  271. }
  272. return 0;
  273. }
  274. static int validate_match(struct hists *leader, struct hists *other)
  275. {
  276. return __validate_match(leader) || __validate_match(other);
  277. }
  278. static int __validate_link(struct hists *hists, int idx)
  279. {
  280. size_t count = 0;
  281. size_t count_pair = 0;
  282. size_t count_dummy = 0;
  283. struct rb_root *root;
  284. struct rb_node *node;
  285. /*
  286. * Leader hists (idx = 0) will have dummy entries from other,
  287. * and some entries will have no pair. However every entry
  288. * in other hists should have (dummy) pair.
  289. */
  290. if (sort__need_collapse)
  291. root = &hists->entries_collapsed;
  292. else
  293. root = hists->entries_in;
  294. node = rb_first(root);
  295. while (node) {
  296. struct hist_entry *he;
  297. he = rb_entry(node, struct hist_entry, rb_node_in);
  298. if (hist_entry__has_pairs(he)) {
  299. if (!find_sample(fake_common_samples,
  300. ARRAY_SIZE(fake_common_samples),
  301. he->thread, he->ms.map, he->ms.sym) &&
  302. !find_sample(fake_samples[idx],
  303. ARRAY_SIZE(fake_samples[idx]),
  304. he->thread, he->ms.map, he->ms.sym)) {
  305. count_dummy++;
  306. }
  307. count_pair++;
  308. } else if (idx) {
  309. pr_debug("A entry from the other hists should have pair\n");
  310. return -1;
  311. }
  312. count++;
  313. node = rb_next(node);
  314. }
  315. /*
  316. * Note that we have a entry collapsed in the other (idx = 1) hists.
  317. */
  318. if (idx == 0) {
  319. if (count_dummy != ARRAY_SIZE(fake_samples[1]) - 1) {
  320. pr_debug("Invalid count of dummy entries: %zd of %zd\n",
  321. count_dummy, ARRAY_SIZE(fake_samples[1]) - 1);
  322. return -1;
  323. }
  324. if (count != count_pair + ARRAY_SIZE(fake_samples[0])) {
  325. pr_debug("Invalid count of total leader entries: %zd of %zd\n",
  326. count, count_pair + ARRAY_SIZE(fake_samples[0]));
  327. return -1;
  328. }
  329. } else {
  330. if (count != count_pair) {
  331. pr_debug("Invalid count of total other entries: %zd of %zd\n",
  332. count, count_pair);
  333. return -1;
  334. }
  335. if (count_dummy > 0) {
  336. pr_debug("Other hists should not have dummy entries: %zd\n",
  337. count_dummy);
  338. return -1;
  339. }
  340. }
  341. return 0;
  342. }
  343. static int validate_link(struct hists *leader, struct hists *other)
  344. {
  345. return __validate_link(leader, 0) || __validate_link(other, 1);
  346. }
  347. static void print_hists(struct hists *hists)
  348. {
  349. int i = 0;
  350. struct rb_root *root;
  351. struct rb_node *node;
  352. if (sort__need_collapse)
  353. root = &hists->entries_collapsed;
  354. else
  355. root = hists->entries_in;
  356. pr_info("----- %s --------\n", __func__);
  357. node = rb_first(root);
  358. while (node) {
  359. struct hist_entry *he;
  360. he = rb_entry(node, struct hist_entry, rb_node_in);
  361. pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n",
  362. i, thread__comm_str(he->thread), he->ms.map->dso->short_name,
  363. he->ms.sym->name, he->stat.period);
  364. i++;
  365. node = rb_next(node);
  366. }
  367. }
  368. int test__hists_link(void)
  369. {
  370. int err = -1;
  371. struct machines machines;
  372. struct machine *machine = NULL;
  373. struct perf_evsel *evsel, *first;
  374. struct perf_evlist *evlist = perf_evlist__new();
  375. if (evlist == NULL)
  376. return -ENOMEM;
  377. err = parse_events(evlist, "cpu-clock");
  378. if (err)
  379. goto out;
  380. err = parse_events(evlist, "task-clock");
  381. if (err)
  382. goto out;
  383. /* default sort order (comm,dso,sym) will be used */
  384. if (setup_sorting() < 0)
  385. goto out;
  386. machines__init(&machines);
  387. /* setup threads/dso/map/symbols also */
  388. machine = setup_fake_machine(&machines);
  389. if (!machine)
  390. goto out;
  391. if (verbose > 1)
  392. machine__fprintf(machine, stderr);
  393. /* process sample events */
  394. err = add_hist_entries(evlist, machine);
  395. if (err < 0)
  396. goto out;
  397. list_for_each_entry(evsel, &evlist->entries, node) {
  398. hists__collapse_resort(&evsel->hists, NULL);
  399. if (verbose > 2)
  400. print_hists(&evsel->hists);
  401. }
  402. first = perf_evlist__first(evlist);
  403. evsel = perf_evlist__last(evlist);
  404. /* match common entries */
  405. hists__match(&first->hists, &evsel->hists);
  406. err = validate_match(&first->hists, &evsel->hists);
  407. if (err)
  408. goto out;
  409. /* link common and/or dummy entries */
  410. hists__link(&first->hists, &evsel->hists);
  411. err = validate_link(&first->hists, &evsel->hists);
  412. if (err)
  413. goto out;
  414. err = 0;
  415. out:
  416. /* tear down everything */
  417. perf_evlist__delete(evlist);
  418. machines__exit(&machines);
  419. return err;
  420. }