builtin-trace.c 5.4 KB

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