Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
| 2 | |
| 3 | #include "util/util.h" |
| 4 | #include "util/cache.h" |
| 5 | #include "util/symbol.h" |
| 6 | #include "util/thread.h" |
| 7 | #include "util/header.h" |
| 8 | |
| 9 | #include "util/parse-options.h" |
| 10 | |
| 11 | #include "perf.h" |
| 12 | #include "util/debug.h" |
| 13 | |
| 14 | #include "util/trace-event.h" |
| 15 | |
| 16 | static char const *input_name = "perf.data"; |
| 17 | static int input; |
| 18 | static unsigned long page_size; |
| 19 | static unsigned long mmap_window = 32; |
| 20 | |
| 21 | static unsigned long total = 0; |
| 22 | static unsigned long total_comm = 0; |
| 23 | |
| 24 | static struct rb_root threads; |
| 25 | static struct thread *last_match; |
| 26 | |
| 27 | static struct perf_header *header; |
| 28 | static u64 sample_type; |
| 29 | |
| 30 | |
| 31 | static int |
| 32 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) |
| 33 | { |
| 34 | struct thread *thread; |
| 35 | |
| 36 | thread = threads__findnew(event->comm.pid, &threads, &last_match); |
| 37 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 38 | dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n", |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 39 | (void *)(offset + head), |
| 40 | (void *)(long)(event->header.size), |
| 41 | event->comm.comm, event->comm.pid); |
| 42 | |
| 43 | if (thread == NULL || |
| 44 | thread__set_comm(thread, event->comm.comm)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 45 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 46 | return -1; |
| 47 | } |
| 48 | total_comm++; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static int |
| 54 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) |
| 55 | { |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 56 | struct thread *thread; |
| 57 | u64 ip = event->ip.ip; |
Ingo Molnar | 6ddf259 | 2009-09-03 12:00:22 +0200 | [diff] [blame] | 58 | u64 timestamp = -1; |
Ingo Molnar | cd6feee | 2009-09-02 20:20:38 +0200 | [diff] [blame] | 59 | u32 cpu = -1; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 60 | u64 period = 1; |
| 61 | void *more_data = event->ip.__more_data; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 62 | |
| 63 | thread = threads__findnew(event->ip.pid, &threads, &last_match); |
| 64 | |
Ingo Molnar | 6ddf259 | 2009-09-03 12:00:22 +0200 | [diff] [blame] | 65 | if (sample_type & PERF_SAMPLE_TIME) { |
| 66 | timestamp = *(u64 *)more_data; |
| 67 | more_data += sizeof(u64); |
| 68 | } |
| 69 | |
Ingo Molnar | cd6feee | 2009-09-02 20:20:38 +0200 | [diff] [blame] | 70 | if (sample_type & PERF_SAMPLE_CPU) { |
| 71 | cpu = *(u32 *)more_data; |
| 72 | more_data += sizeof(u32); |
| 73 | more_data += sizeof(u32); /* reserved */ |
| 74 | } |
| 75 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 76 | if (sample_type & PERF_SAMPLE_PERIOD) { |
| 77 | period = *(u64 *)more_data; |
| 78 | more_data += sizeof(u64); |
| 79 | } |
| 80 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 81 | dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n", |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 82 | (void *)(offset + head), |
| 83 | (void *)(long)(event->header.size), |
| 84 | event->header.misc, |
| 85 | event->ip.pid, event->ip.tid, |
| 86 | (void *)(long)ip, |
| 87 | (long long)period); |
| 88 | |
| 89 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
| 90 | |
| 91 | if (thread == NULL) { |
| 92 | eprintf("problem processing %d event, skipping it.\n", |
| 93 | event->header.type); |
| 94 | return -1; |
| 95 | } |
| 96 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 97 | if (sample_type & PERF_SAMPLE_RAW) { |
| 98 | struct { |
| 99 | u32 size; |
| 100 | char data[0]; |
| 101 | } *raw = more_data; |
| 102 | |
| 103 | /* |
| 104 | * FIXME: better resolve from pid from the struct trace_entry |
| 105 | * field, although it should be the same than this perf |
| 106 | * event pid |
| 107 | */ |
Ingo Molnar | 6ddf259 | 2009-09-03 12:00:22 +0200 | [diff] [blame] | 108 | print_event(cpu, raw->data, raw->size, timestamp, thread->comm); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 109 | } |
| 110 | total += period; |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int |
| 116 | process_event(event_t *event, unsigned long offset, unsigned long head) |
| 117 | { |
| 118 | trace_event(event); |
| 119 | |
| 120 | switch (event->header.type) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 121 | case PERF_RECORD_MMAP ... PERF_RECORD_LOST: |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 122 | return 0; |
| 123 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 124 | case PERF_RECORD_COMM: |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 125 | return process_comm_event(event, offset, head); |
| 126 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 127 | case PERF_RECORD_EXIT ... PERF_RECORD_READ: |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 128 | return 0; |
| 129 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 130 | case PERF_RECORD_SAMPLE: |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 131 | return process_sample_event(event, offset, head); |
| 132 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 133 | case PERF_RECORD_MAX: |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 134 | default: |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int __cmd_trace(void) |
| 142 | { |
| 143 | int ret, rc = EXIT_FAILURE; |
| 144 | unsigned long offset = 0; |
| 145 | unsigned long head = 0; |
Frederic Weisbecker | b209aa1 | 2009-10-06 21:21:26 +0200 | [diff] [blame] | 146 | unsigned long shift; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 147 | struct stat perf_stat; |
| 148 | event_t *event; |
| 149 | uint32_t size; |
| 150 | char *buf; |
| 151 | |
Frederic Weisbecker | 3a2684c | 2009-08-31 06:45:19 +0200 | [diff] [blame] | 152 | register_idle_thread(&threads, &last_match); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 153 | |
| 154 | input = open(input_name, O_RDONLY); |
| 155 | if (input < 0) { |
| 156 | perror("failed to open file"); |
| 157 | exit(-1); |
| 158 | } |
| 159 | |
| 160 | ret = fstat(input, &perf_stat); |
| 161 | if (ret < 0) { |
| 162 | perror("failed to stat file"); |
| 163 | exit(-1); |
| 164 | } |
| 165 | |
| 166 | if (!perf_stat.st_size) { |
| 167 | fprintf(stderr, "zero-sized file, nothing to do!\n"); |
| 168 | exit(0); |
| 169 | } |
| 170 | header = perf_header__read(input); |
Ingo Molnar | 8886f42 | 2009-09-03 16:19:57 +0200 | [diff] [blame] | 171 | head = header->data_offset; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 172 | sample_type = perf_header__sample_type(header); |
| 173 | |
Frederic Weisbecker | 4bf2364 | 2009-08-17 23:07:49 +0200 | [diff] [blame] | 174 | if (!(sample_type & PERF_SAMPLE_RAW)) |
| 175 | die("No trace sample to read. Did you call perf record " |
| 176 | "without -R?"); |
| 177 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 178 | if (load_kernel() < 0) { |
| 179 | perror("failed to load kernel symbols"); |
| 180 | return EXIT_FAILURE; |
| 181 | } |
| 182 | |
Frederic Weisbecker | b209aa1 | 2009-10-06 21:21:26 +0200 | [diff] [blame] | 183 | shift = page_size * (head / page_size); |
| 184 | offset += shift; |
| 185 | head -= shift; |
| 186 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 187 | remap: |
| 188 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, |
| 189 | MAP_SHARED, input, offset); |
| 190 | if (buf == MAP_FAILED) { |
| 191 | perror("failed to mmap file"); |
| 192 | exit(-1); |
| 193 | } |
| 194 | |
| 195 | more: |
| 196 | event = (event_t *)(buf + head); |
| 197 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 198 | if (head + event->header.size >= page_size * mmap_window) { |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 199 | int res; |
| 200 | |
Frederic Weisbecker | b209aa1 | 2009-10-06 21:21:26 +0200 | [diff] [blame] | 201 | shift = page_size * (head / page_size); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 202 | res = munmap(buf, page_size * mmap_window); |
| 203 | assert(res == 0); |
| 204 | |
| 205 | offset += shift; |
| 206 | head -= shift; |
| 207 | goto remap; |
| 208 | } |
| 209 | |
| 210 | size = event->header.size; |
| 211 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 212 | if (!size || process_event(event, offset, head) < 0) { |
| 213 | |
| 214 | /* |
| 215 | * assume we lost track of the stream, check alignment, and |
| 216 | * increment a single u64 in the hope to catch on again 'soon'. |
| 217 | */ |
| 218 | |
| 219 | if (unlikely(head & 7)) |
| 220 | head &= ~7ULL; |
| 221 | |
| 222 | size = 8; |
| 223 | } |
| 224 | |
| 225 | head += size; |
| 226 | |
| 227 | if (offset + head < (unsigned long)perf_stat.st_size) |
| 228 | goto more; |
| 229 | |
| 230 | rc = EXIT_SUCCESS; |
| 231 | close(input); |
| 232 | |
| 233 | return rc; |
| 234 | } |
| 235 | |
| 236 | static const char * const annotate_usage[] = { |
| 237 | "perf trace [<options>] <command>", |
| 238 | NULL |
| 239 | }; |
| 240 | |
| 241 | static const struct option options[] = { |
| 242 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 243 | "dump raw trace in ASCII"), |
| 244 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 245 | "be more verbose (show symbol address, etc)"), |
Masami Hiramatsu | 1909629 | 2009-08-21 14:56:03 -0400 | [diff] [blame] | 246 | OPT_END() |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | int cmd_trace(int argc, const char **argv, const char *prefix __used) |
| 250 | { |
| 251 | symbol__init(); |
| 252 | page_size = getpagesize(); |
| 253 | |
| 254 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
| 255 | if (argc) { |
| 256 | /* |
| 257 | * Special case: if there's an argument left then assume tha |
| 258 | * it's a symbol filter: |
| 259 | */ |
| 260 | if (argc > 1) |
| 261 | usage_with_options(annotate_usage, options); |
| 262 | } |
| 263 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 264 | setup_pager(); |
| 265 | |
| 266 | return __cmd_trace(); |
| 267 | } |