hists_common.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. union perf_event fake_mmap_event = {
  90. .mmap = {
  91. .header = { .misc = PERF_RECORD_MISC_USER, },
  92. .pid = fake_mmap_info[i].pid,
  93. .tid = fake_mmap_info[i].pid,
  94. .start = fake_mmap_info[i].start,
  95. .len = FAKE_MAP_LENGTH,
  96. .pgoff = 0ULL,
  97. },
  98. };
  99. strcpy(fake_mmap_event.mmap.filename,
  100. fake_mmap_info[i].filename);
  101. machine__process_mmap_event(machine, &fake_mmap_event, NULL);
  102. }
  103. for (i = 0; i < ARRAY_SIZE(fake_symbols); i++) {
  104. size_t k;
  105. struct dso *dso;
  106. dso = machine__findnew_dso(machine, fake_symbols[i].dso_name);
  107. if (dso == NULL)
  108. goto out;
  109. /* emulate dso__load() */
  110. dso__set_loaded(dso, MAP__FUNCTION);
  111. for (k = 0; k < fake_symbols[i].nr_syms; k++) {
  112. struct symbol *sym;
  113. struct fake_sym *fsym = &fake_symbols[i].syms[k];
  114. sym = symbol__new(fsym->start, fsym->length,
  115. STB_GLOBAL, fsym->name);
  116. if (sym == NULL) {
  117. dso__put(dso);
  118. goto out;
  119. }
  120. symbols__insert(&dso->symbols[MAP__FUNCTION], sym);
  121. }
  122. dso__put(dso);
  123. }
  124. return machine;
  125. out:
  126. pr_debug("Not enough memory for machine setup\n");
  127. machine__delete_threads(machine);
  128. machine__delete(machine);
  129. return NULL;
  130. }
  131. void print_hists_in(struct hists *hists)
  132. {
  133. int i = 0;
  134. struct rb_root *root;
  135. struct rb_node *node;
  136. if (sort__need_collapse)
  137. root = &hists->entries_collapsed;
  138. else
  139. root = hists->entries_in;
  140. pr_info("----- %s --------\n", __func__);
  141. node = rb_first(root);
  142. while (node) {
  143. struct hist_entry *he;
  144. he = rb_entry(node, struct hist_entry, rb_node_in);
  145. if (!he->filtered) {
  146. pr_info("%2d: entry: %-8s [%-8s] %20s: period = %"PRIu64"\n",
  147. i, thread__comm_str(he->thread),
  148. he->ms.map->dso->short_name,
  149. he->ms.sym->name, he->stat.period);
  150. }
  151. i++;
  152. node = rb_next(node);
  153. }
  154. }
  155. void print_hists_out(struct hists *hists)
  156. {
  157. int i = 0;
  158. struct rb_root *root;
  159. struct rb_node *node;
  160. root = &hists->entries;
  161. pr_info("----- %s --------\n", __func__);
  162. node = rb_first(root);
  163. while (node) {
  164. struct hist_entry *he;
  165. he = rb_entry(node, struct hist_entry, rb_node);
  166. if (!he->filtered) {
  167. pr_info("%2d: entry: %8s:%5d [%-8s] %20s: period = %"PRIu64"/%"PRIu64"\n",
  168. i, thread__comm_str(he->thread), he->thread->tid,
  169. he->ms.map->dso->short_name,
  170. he->ms.sym->name, he->stat.period,
  171. he->stat_acc ? he->stat_acc->period : 0);
  172. }
  173. i++;
  174. node = rb_next(node);
  175. }
  176. }