hists_link.c 8.2 KB

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