builtin-trace.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include "builtin.h"
  2. #include "util/util.h"
  3. #include "util/cache.h"
  4. #include "util/symbol.h"
  5. #include "util/thread.h"
  6. #include "util/header.h"
  7. #include "util/parse-options.h"
  8. #include "perf.h"
  9. #include "util/debug.h"
  10. #include "util/trace-event.h"
  11. static char const *input_name = "perf.data";
  12. static int input;
  13. static unsigned long page_size;
  14. static unsigned long mmap_window = 32;
  15. static unsigned long total = 0;
  16. static unsigned long total_comm = 0;
  17. static struct rb_root threads;
  18. static struct thread *last_match;
  19. static struct perf_header *header;
  20. static u64 sample_type;
  21. static int
  22. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  23. {
  24. struct thread *thread;
  25. thread = threads__findnew(event->comm.pid, &threads, &last_match);
  26. dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  27. (void *)(offset + head),
  28. (void *)(long)(event->header.size),
  29. event->comm.comm, event->comm.pid);
  30. if (thread == NULL ||
  31. thread__set_comm(thread, event->comm.comm)) {
  32. dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
  33. return -1;
  34. }
  35. total_comm++;
  36. return 0;
  37. }
  38. static int
  39. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  40. {
  41. char level;
  42. int show = 0;
  43. struct dso *dso = NULL;
  44. struct thread *thread;
  45. u64 ip = event->ip.ip;
  46. u64 period = 1;
  47. void *more_data = event->ip.__more_data;
  48. int cpumode;
  49. thread = threads__findnew(event->ip.pid, &threads, &last_match);
  50. if (sample_type & PERF_SAMPLE_PERIOD) {
  51. period = *(u64 *)more_data;
  52. more_data += sizeof(u64);
  53. }
  54. dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
  55. (void *)(offset + head),
  56. (void *)(long)(event->header.size),
  57. event->header.misc,
  58. event->ip.pid, event->ip.tid,
  59. (void *)(long)ip,
  60. (long long)period);
  61. dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  62. if (thread == NULL) {
  63. eprintf("problem processing %d event, skipping it.\n",
  64. event->header.type);
  65. return -1;
  66. }
  67. cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
  68. if (cpumode == PERF_EVENT_MISC_KERNEL) {
  69. show = SHOW_KERNEL;
  70. level = 'k';
  71. dso = kernel_dso;
  72. dump_printf(" ...... dso: %s\n", dso->name);
  73. } else if (cpumode == PERF_EVENT_MISC_USER) {
  74. show = SHOW_USER;
  75. level = '.';
  76. } else {
  77. show = SHOW_HV;
  78. level = 'H';
  79. dso = hypervisor_dso;
  80. dump_printf(" ...... dso: [hypervisor]\n");
  81. }
  82. if (sample_type & PERF_SAMPLE_RAW) {
  83. struct {
  84. u32 size;
  85. char data[0];
  86. } *raw = more_data;
  87. /*
  88. * FIXME: better resolve from pid from the struct trace_entry
  89. * field, although it should be the same than this perf
  90. * event pid
  91. */
  92. print_event(0, raw->data, raw->size, 0, thread->comm);
  93. }
  94. total += period;
  95. return 0;
  96. }
  97. static int
  98. process_event(event_t *event, unsigned long offset, unsigned long head)
  99. {
  100. trace_event(event);
  101. switch (event->header.type) {
  102. case PERF_EVENT_MMAP ... PERF_EVENT_LOST:
  103. return 0;
  104. case PERF_EVENT_COMM:
  105. return process_comm_event(event, offset, head);
  106. case PERF_EVENT_EXIT ... PERF_EVENT_READ:
  107. return 0;
  108. case PERF_EVENT_SAMPLE:
  109. return process_sample_event(event, offset, head);
  110. case PERF_EVENT_MAX:
  111. default:
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. static int __cmd_trace(void)
  117. {
  118. int ret, rc = EXIT_FAILURE;
  119. unsigned long offset = 0;
  120. unsigned long head = 0;
  121. struct stat perf_stat;
  122. event_t *event;
  123. uint32_t size;
  124. char *buf;
  125. trace_report();
  126. input = open(input_name, O_RDONLY);
  127. if (input < 0) {
  128. perror("failed to open file");
  129. exit(-1);
  130. }
  131. ret = fstat(input, &perf_stat);
  132. if (ret < 0) {
  133. perror("failed to stat file");
  134. exit(-1);
  135. }
  136. if (!perf_stat.st_size) {
  137. fprintf(stderr, "zero-sized file, nothing to do!\n");
  138. exit(0);
  139. }
  140. header = perf_header__read(input);
  141. sample_type = perf_header__sample_type(header);
  142. if (!(sample_type & PERF_SAMPLE_RAW))
  143. die("No trace sample to read. Did you call perf record "
  144. "without -R?");
  145. if (load_kernel() < 0) {
  146. perror("failed to load kernel symbols");
  147. return EXIT_FAILURE;
  148. }
  149. remap:
  150. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  151. MAP_SHARED, input, offset);
  152. if (buf == MAP_FAILED) {
  153. perror("failed to mmap file");
  154. exit(-1);
  155. }
  156. more:
  157. event = (event_t *)(buf + head);
  158. size = event->header.size;
  159. if (!size)
  160. size = 8;
  161. if (head + event->header.size >= page_size * mmap_window) {
  162. unsigned long shift = page_size * (head / page_size);
  163. int res;
  164. res = munmap(buf, page_size * mmap_window);
  165. assert(res == 0);
  166. offset += shift;
  167. head -= shift;
  168. goto remap;
  169. }
  170. size = event->header.size;
  171. if (!size || process_event(event, offset, head) < 0) {
  172. /*
  173. * assume we lost track of the stream, check alignment, and
  174. * increment a single u64 in the hope to catch on again 'soon'.
  175. */
  176. if (unlikely(head & 7))
  177. head &= ~7ULL;
  178. size = 8;
  179. }
  180. head += size;
  181. if (offset + head < (unsigned long)perf_stat.st_size)
  182. goto more;
  183. rc = EXIT_SUCCESS;
  184. close(input);
  185. return rc;
  186. }
  187. static const char * const annotate_usage[] = {
  188. "perf trace [<options>] <command>",
  189. NULL
  190. };
  191. static const struct option options[] = {
  192. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  193. "dump raw trace in ASCII"),
  194. OPT_BOOLEAN('v', "verbose", &verbose,
  195. "be more verbose (show symbol address, etc)"),
  196. OPT_END()
  197. };
  198. int cmd_trace(int argc, const char **argv, const char *prefix __used)
  199. {
  200. symbol__init();
  201. page_size = getpagesize();
  202. argc = parse_options(argc, argv, options, annotate_usage, 0);
  203. if (argc) {
  204. /*
  205. * Special case: if there's an argument left then assume tha
  206. * it's a symbol filter:
  207. */
  208. if (argc > 1)
  209. usage_with_options(annotate_usage, options);
  210. }
  211. setup_pager();
  212. return __cmd_trace();
  213. }