hists_common.c 5.0 KB

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