hists_common.c 5.0 KB

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