hists_link.c 9.0 KB

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