hists_common.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <inttypes.h>
  3. #include "util/debug.h"
  4. #include "util/dso.h"
  5. #include "util/event.h" // struct perf_sample
  6. #include "util/map.h"
  7. #include "util/symbol.h"
  8. #include "util/sort.h"
  9. #include "util/evsel.h"
  10. #include "util/machine.h"
  11. #include "util/thread.h"
  12. #include "tests/hists_common.h"
  13. #include <linux/kernel.h>
  14. #include <linux/perf_event.h>
  15. static struct {
  16. u32 pid;
  17. const char *comm;
  18. } fake_threads[] = {
  19. { FAKE_PID_PERF1, "perf" },
  20. { FAKE_PID_PERF2, "perf" },
  21. { FAKE_PID_BASH, "bash" },
  22. };
  23. static struct {
  24. u32 pid;
  25. u64 start;
  26. const char *filename;
  27. } fake_mmap_info[] = {
  28. { FAKE_PID_PERF1, FAKE_MAP_PERF, "perf" },
  29. { FAKE_PID_PERF1, FAKE_MAP_LIBC, "libc" },
  30. { FAKE_PID_PERF1, FAKE_MAP_KERNEL, "[kernel]" },
  31. { FAKE_PID_PERF2, FAKE_MAP_PERF, "perf" },
  32. { FAKE_PID_PERF2, FAKE_MAP_LIBC, "libc" },
  33. { FAKE_PID_PERF2, FAKE_MAP_KERNEL, "[kernel]" },
  34. { FAKE_PID_BASH, FAKE_MAP_BASH, "bash" },
  35. { FAKE_PID_BASH, FAKE_MAP_LIBC, "libc" },
  36. { FAKE_PID_BASH, FAKE_MAP_KERNEL, "[kernel]" },
  37. };
  38. struct fake_sym {
  39. u64 start;
  40. u64 length;
  41. const char *name;
  42. };
  43. static struct fake_sym perf_syms[] = {
  44. { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
  45. { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "run_command" },
  46. { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "cmd_record" },
  47. };
  48. static struct fake_sym bash_syms[] = {
  49. { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "main" },
  50. { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "xmalloc" },
  51. { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "xfree" },
  52. };
  53. static struct fake_sym libc_syms[] = {
  54. { 700, 100, "malloc" },
  55. { 800, 100, "free" },
  56. { 900, 100, "realloc" },
  57. { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "malloc" },
  58. { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "free" },
  59. { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "realloc" },
  60. };
  61. static struct fake_sym kernel_syms[] = {
  62. { FAKE_SYM_OFFSET1, FAKE_SYM_LENGTH, "schedule" },
  63. { FAKE_SYM_OFFSET2, FAKE_SYM_LENGTH, "page_fault" },
  64. { FAKE_SYM_OFFSET3, FAKE_SYM_LENGTH, "sys_perf_event_open" },
  65. };
  66. static struct {
  67. const char *dso_name;
  68. struct fake_sym *syms;
  69. size_t nr_syms;
  70. } fake_symbols[] = {
  71. { "perf", perf_syms, ARRAY_SIZE(perf_syms) },
  72. { "bash", bash_syms, ARRAY_SIZE(bash_syms) },
  73. { "libc", libc_syms, ARRAY_SIZE(libc_syms) },
  74. { "[kernel]", kernel_syms, ARRAY_SIZE(kernel_syms) },
  75. };
  76. struct machine *setup_fake_machine(struct machines *machines)
  77. {
  78. struct machine *machine = machines__find(machines, HOST_KERNEL_ID);
  79. size_t i;
  80. if (machine == NULL) {
  81. pr_debug("Not enough memory for machine setup\n");
  82. return NULL;
  83. }
  84. for (i = 0; i < ARRAY_SIZE(fake_threads); i++) {
  85. struct thread *thread;
  86. thread = machine__findnew_thread(machine, fake_threads[i].pid,
  87. fake_threads[i].pid);
  88. if (thread == NULL)
  89. goto out;
  90. thread__set_comm(thread, fake_threads[i].comm, 0);
  91. thread__put(thread);
  92. }
  93. for (i = 0; i < ARRAY_SIZE(fake_mmap_info); i++) {
  94. struct perf_sample sample = {
  95. .cpumode = PERF_RECORD_MISC_USER,
  96. };
  97. union perf_event fake_mmap_event = {
  98. .mmap = {
  99. .pid = fake_mmap_info[i].pid,
  100. .tid = fake_mmap_info[i].pid,
  101. .start = fake_mmap_info[i].start,
  102. .len = FAKE_MAP_LENGTH,
  103. .pgoff = 0ULL,
  104. },
  105. };
  106. strcpy(fake_mmap_event.mmap.filename,
  107. fake_mmap_info[i].filename);
  108. machine__process_mmap_event(machine, &fake_mmap_event, &sample);
  109. }
  110. for (i = 0; i < ARRAY_SIZE(fake_symbols); i++) {
  111. size_t k;
  112. struct dso *dso;
  113. dso = machine__findnew_dso(machine, fake_symbols[i].dso_name);
  114. if (dso == NULL)
  115. goto out;
  116. /* emulate dso__load() */
  117. dso__set_loaded(dso);
  118. for (k = 0; k < fake_symbols[i].nr_syms; k++) {
  119. struct symbol *sym;
  120. struct fake_sym *fsym = &fake_symbols[i].syms[k];
  121. sym = symbol__new(fsym->start, fsym->length,
  122. STB_GLOBAL, STT_FUNC, fsym->name);
  123. if (sym == NULL) {
  124. dso__put(dso);
  125. goto out;
  126. }
  127. symbols__insert(&dso->symbols, sym);
  128. }
  129. dso__put(dso);
  130. }
  131. return machine;
  132. out:
  133. pr_debug("Not enough memory for machine setup\n");
  134. machine__delete_threads(machine);
  135. return NULL;
  136. }
  137. void print_hists_in(struct hists *hists)
  138. {
  139. int i = 0;
  140. struct rb_root_cached *root;
  141. struct rb_node *node;
  142. if (hists__has(hists, need_collapse))
  143. root = &hists->entries_collapsed;
  144. else
  145. root = hists->entries_in;
  146. pr_info("----- %s --------\n", __func__);
  147. node = rb_first_cached(root);
  148. while (node) {
  149. struct hist_entry *he;
  150. he = rb_entry(node, struct hist_entry, rb_node_in);
  151. if (!he->filtered) {
  152. pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n",
  153. i, thread__comm_str(he->thread),
  154. he->ms.map->dso->short_name,
  155. he->ms.sym->name, he->stat.period);
  156. }
  157. i++;
  158. node = rb_next(node);
  159. }
  160. }
  161. void print_hists_out(struct hists *hists)
  162. {
  163. int i = 0;
  164. struct rb_root_cached *root;
  165. struct rb_node *node;
  166. root = &hists->entries;
  167. pr_info("----- %s --------\n", __func__);
  168. node = rb_first_cached(root);
  169. while (node) {
  170. struct hist_entry *he;
  171. he = rb_entry(node, struct hist_entry, rb_node);
  172. if (!he->filtered) {
  173. pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period = %"PRIu64"/%"PRIu64"\n",
  174. i, thread__comm_str(he->thread), he->thread->tid,
  175. he->ms.map->dso->short_name,
  176. he->ms.sym->name, he->stat.period,
  177. he->stat_acc ? he->stat_acc->period : 0);
  178. }
  179. i++;
  180. node = rb_next(node);
  181. }
  182. }