hists_link.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "perf.h"
  3. #include "tests.h"
  4. #include "debug.h"
  5. #include "symbol.h"
  6. #include "sort.h"
  7. #include "evsel.h"
  8. #include "evlist.h"
  9. #include "machine.h"
  10. #include "thread.h"
  11. #include "parse-events.h"
  12. #include "hists_common.h"
  13. #include <errno.h>
  14. #include <linux/kernel.h>
  15. struct sample {
  16. u32 pid;
  17. u64 ip;
  18. struct thread *thread;
  19. struct map *map;
  20. struct symbol *sym;
  21. };
  22. /* For the numbers, see hists_common.c */
  23. static struct sample fake_common_samples[] = {
  24. /* perf [kernel] schedule() */
  25. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_SCHEDULE, },
  26. /* perf [perf] main() */
  27. { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_MAIN, },
  28. /* perf [perf] cmd_record() */
  29. { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_PERF_CMD_RECORD, },
  30. /* bash [bash] xmalloc() */
  31. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XMALLOC, },
  32. /* bash [libc] malloc() */
  33. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, },
  34. };
  35. static struct sample fake_samples[][5] = {
  36. {
  37. /* perf [perf] run_command() */
  38. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_PERF_RUN_COMMAND, },
  39. /* perf [libc] malloc() */
  40. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_LIBC_MALLOC, },
  41. /* perf [kernel] page_fault() */
  42. { .pid = FAKE_PID_PERF1, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
  43. /* perf [kernel] sys_perf_event_open() */
  44. { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_KERNEL_SYS_PERF_EVENT_OPEN, },
  45. /* bash [libc] free() */
  46. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_FREE, },
  47. },
  48. {
  49. /* perf [libc] free() */
  50. { .pid = FAKE_PID_PERF2, .ip = FAKE_IP_LIBC_FREE, },
  51. /* bash [libc] malloc() */
  52. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_MALLOC, }, /* will be merged */
  53. /* bash [bash] xfee() */
  54. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_BASH_XFREE, },
  55. /* bash [libc] realloc() */
  56. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_LIBC_REALLOC, },
  57. /* bash [kernel] page_fault() */
  58. { .pid = FAKE_PID_BASH, .ip = FAKE_IP_KERNEL_PAGE_FAULT, },
  59. },
  60. };
  61. static int add_hist_entries(struct perf_evlist *evlist, struct machine *machine)
  62. {
  63. struct perf_evsel *evsel;
  64. struct addr_location al;
  65. struct hist_entry *he;
  66. struct perf_sample sample = { .period = 1, .weight = 1, };
  67. size_t i = 0, k;
  68. /*
  69. * each evsel will have 10 samples - 5 common and 5 distinct.
  70. * However the second evsel also has a collapsed entry for
  71. * "bash [libc] malloc" so total 9 entries will be in the tree.
  72. */
  73. evlist__for_each_entry(evlist, evsel) {
  74. struct hists *hists = evsel__hists(evsel);
  75. for (k = 0; k < ARRAY_SIZE(fake_common_samples); k++) {
  76. sample.cpumode = PERF_RECORD_MISC_USER;
  77. sample.pid = fake_common_samples[k].pid;
  78. sample.tid = fake_common_samples[k].pid;
  79. sample.ip = fake_common_samples[k].ip;
  80. if (machine__resolve(machine, &al, &sample) < 0)
  81. goto out;
  82. he = hists__add_entry(hists, &al, NULL,
  83. NULL, NULL, &sample, true);
  84. if (he == NULL) {
  85. addr_location__put(&al);
  86. goto out;
  87. }
  88. fake_common_samples[k].thread = al.thread;
  89. fake_common_samples[k].map = al.map;
  90. fake_common_samples[k].sym = al.sym;
  91. }
  92. for (k = 0; k < ARRAY_SIZE(fake_samples[i]); k++) {
  93. sample.pid = fake_samples[i][k].pid;
  94. sample.tid = fake_samples[i][k].pid;
  95. sample.ip = fake_samples[i][k].ip;
  96. if (machine__resolve(machine, &al, &sample) < 0)
  97. goto out;
  98. he = hists__add_entry(hists, &al, NULL,
  99. NULL, NULL, &sample, true);
  100. if (he == NULL) {
  101. addr_location__put(&al);
  102. goto out;
  103. }
  104. fake_samples[i][k].thread = al.thread;
  105. fake_samples[i][k].map = al.map;
  106. fake_samples[i][k].sym = al.sym;
  107. }
  108. i++;
  109. }
  110. return 0;
  111. out:
  112. pr_debug("Not enough memory for adding a hist entry\n");
  113. return -1;
  114. }
  115. static int find_sample(struct sample *samples, size_t nr_samples,
  116. struct thread *t, struct map *m, struct symbol *s)
  117. {
  118. while (nr_samples--) {
  119. if (samples->thread == t && samples->map == m &&
  120. samples->sym == s)
  121. return 1;
  122. samples++;
  123. }
  124. return 0;
  125. }
  126. static int __validate_match(struct hists *hists)
  127. {
  128. size_t count = 0;
  129. struct rb_root *root;
  130. struct rb_node *node;
  131. /*
  132. * Only entries from fake_common_samples should have a pair.
  133. */
  134. if (hists__has(hists, need_collapse))
  135. root = &hists->entries_collapsed;
  136. else
  137. root = hists->entries_in;
  138. node = rb_first(root);
  139. while (node) {
  140. struct hist_entry *he;
  141. he = rb_entry(node, struct hist_entry, rb_node_in);
  142. if (hist_entry__has_pairs(he)) {
  143. if (find_sample(fake_common_samples,
  144. ARRAY_SIZE(fake_common_samples),
  145. he->thread, he->ms.map, he->ms.sym)) {
  146. count++;
  147. } else {
  148. pr_debug("Can't find the matched entry\n");
  149. return -1;
  150. }
  151. }
  152. node = rb_next(node);
  153. }
  154. if (count != ARRAY_SIZE(fake_common_samples)) {
  155. pr_debug("Invalid count for matched entries: %zd of %zd\n",
  156. count, ARRAY_SIZE(fake_common_samples));
  157. return -1;
  158. }
  159. return 0;
  160. }
  161. static int validate_match(struct hists *leader, struct hists *other)
  162. {
  163. return __validate_match(leader) || __validate_match(other);
  164. }
  165. static int __validate_link(struct hists *hists, int idx)
  166. {
  167. size_t count = 0;
  168. size_t count_pair = 0;
  169. size_t count_dummy = 0;
  170. struct rb_root *root;
  171. struct rb_node *node;
  172. /*
  173. * Leader hists (idx = 0) will have dummy entries from other,
  174. * and some entries will have no pair. However every entry
  175. * in other hists should have (dummy) pair.
  176. */
  177. if (hists__has(hists, need_collapse))
  178. root = &hists->entries_collapsed;
  179. else
  180. root = hists->entries_in;
  181. node = rb_first(root);
  182. while (node) {
  183. struct hist_entry *he;
  184. he = rb_entry(node, struct hist_entry, rb_node_in);
  185. if (hist_entry__has_pairs(he)) {
  186. if (!find_sample(fake_common_samples,
  187. ARRAY_SIZE(fake_common_samples),
  188. he->thread, he->ms.map, he->ms.sym) &&
  189. !find_sample(fake_samples[idx],
  190. ARRAY_SIZE(fake_samples[idx]),
  191. he->thread, he->ms.map, he->ms.sym)) {
  192. count_dummy++;
  193. }
  194. count_pair++;
  195. } else if (idx) {
  196. pr_debug("A entry from the other hists should have pair\n");
  197. return -1;
  198. }
  199. count++;
  200. node = rb_next(node);
  201. }
  202. /*
  203. * Note that we have a entry collapsed in the other (idx = 1) hists.
  204. */
  205. if (idx == 0) {
  206. if (count_dummy != ARRAY_SIZE(fake_samples[1]) - 1) {
  207. pr_debug("Invalid count of dummy entries: %zd of %zd\n",
  208. count_dummy, ARRAY_SIZE(fake_samples[1]) - 1);
  209. return -1;
  210. }
  211. if (count != count_pair + ARRAY_SIZE(fake_samples[0])) {
  212. pr_debug("Invalid count of total leader entries: %zd of %zd\n",
  213. count, count_pair + ARRAY_SIZE(fake_samples[0]));
  214. return -1;
  215. }
  216. } else {
  217. if (count != count_pair) {
  218. pr_debug("Invalid count of total other entries: %zd of %zd\n",
  219. count, count_pair);
  220. return -1;
  221. }
  222. if (count_dummy > 0) {
  223. pr_debug("Other hists should not have dummy entries: %zd\n",
  224. count_dummy);
  225. return -1;
  226. }
  227. }
  228. return 0;
  229. }
  230. static int validate_link(struct hists *leader, struct hists *other)
  231. {
  232. return __validate_link(leader, 0) || __validate_link(other, 1);
  233. }
  234. int test__hists_link(struct test *test __maybe_unused, int subtest __maybe_unused)
  235. {
  236. int err = -1;
  237. struct hists *hists, *first_hists;
  238. struct machines machines;
  239. struct machine *machine = NULL;
  240. struct perf_evsel *evsel, *first;
  241. struct perf_evlist *evlist = perf_evlist__new();
  242. if (evlist == NULL)
  243. return -ENOMEM;
  244. err = parse_events(evlist, "cpu-clock", NULL);
  245. if (err)
  246. goto out;
  247. err = parse_events(evlist, "task-clock", NULL);
  248. if (err)
  249. goto out;
  250. err = TEST_FAIL;
  251. /* default sort order (comm,dso,sym) will be used */
  252. if (setup_sorting(NULL) < 0)
  253. goto out;
  254. machines__init(&machines);
  255. /* setup threads/dso/map/symbols also */
  256. machine = setup_fake_machine(&machines);
  257. if (!machine)
  258. goto out;
  259. if (verbose > 1)
  260. machine__fprintf(machine, stderr);
  261. /* process sample events */
  262. err = add_hist_entries(evlist, machine);
  263. if (err < 0)
  264. goto out;
  265. evlist__for_each_entry(evlist, evsel) {
  266. hists = evsel__hists(evsel);
  267. hists__collapse_resort(hists, NULL);
  268. if (verbose > 2)
  269. print_hists_in(hists);
  270. }
  271. first = perf_evlist__first(evlist);
  272. evsel = perf_evlist__last(evlist);
  273. first_hists = evsel__hists(first);
  274. hists = evsel__hists(evsel);
  275. /* match common entries */
  276. hists__match(first_hists, hists);
  277. err = validate_match(first_hists, hists);
  278. if (err)
  279. goto out;
  280. /* link common and/or dummy entries */
  281. hists__link(first_hists, hists);
  282. err = validate_link(first_hists, hists);
  283. if (err)
  284. goto out;
  285. err = 0;
  286. out:
  287. /* tear down everything */
  288. perf_evlist__delete(evlist);
  289. reset_output_field();
  290. machines__exit(&machines);
  291. return err;
  292. }