builtin-trace.c 5.9 KB

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