hists_common.c 5.0 KB

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