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" |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame^] | 15 | #include "util/data_map.h" |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 16 | |
| 17 | static char const *input_name = "perf.data"; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 18 | |
| 19 | static unsigned long total = 0; |
| 20 | static unsigned long total_comm = 0; |
| 21 | |
| 22 | static struct rb_root threads; |
| 23 | static struct thread *last_match; |
| 24 | |
| 25 | static struct perf_header *header; |
| 26 | static u64 sample_type; |
| 27 | |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame^] | 28 | static char *cwd; |
| 29 | static int cwdlen; |
| 30 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 31 | |
| 32 | static int |
| 33 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) |
| 34 | { |
| 35 | struct thread *thread; |
| 36 | |
| 37 | thread = threads__findnew(event->comm.pid, &threads, &last_match); |
| 38 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 39 | dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n", |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 40 | (void *)(offset + head), |
| 41 | (void *)(long)(event->header.size), |
| 42 | event->comm.comm, event->comm.pid); |
| 43 | |
| 44 | if (thread == NULL || |
| 45 | thread__set_comm(thread, event->comm.comm)) { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 46 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 47 | return -1; |
| 48 | } |
| 49 | total_comm++; |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int |
| 55 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) |
| 56 | { |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 57 | struct thread *thread; |
| 58 | u64 ip = event->ip.ip; |
Ingo Molnar | 6ddf259 | 2009-09-03 12:00:22 +0200 | [diff] [blame] | 59 | u64 timestamp = -1; |
Ingo Molnar | cd6feee | 2009-09-02 20:20:38 +0200 | [diff] [blame] | 60 | u32 cpu = -1; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 61 | u64 period = 1; |
| 62 | void *more_data = event->ip.__more_data; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 63 | |
| 64 | thread = threads__findnew(event->ip.pid, &threads, &last_match); |
| 65 | |
Ingo Molnar | 6ddf259 | 2009-09-03 12:00:22 +0200 | [diff] [blame] | 66 | if (sample_type & PERF_SAMPLE_TIME) { |
| 67 | timestamp = *(u64 *)more_data; |
| 68 | more_data += sizeof(u64); |
| 69 | } |
| 70 | |
Ingo Molnar | cd6feee | 2009-09-02 20:20:38 +0200 | [diff] [blame] | 71 | if (sample_type & PERF_SAMPLE_CPU) { |
| 72 | cpu = *(u32 *)more_data; |
| 73 | more_data += sizeof(u32); |
| 74 | more_data += sizeof(u32); /* reserved */ |
| 75 | } |
| 76 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 77 | if (sample_type & PERF_SAMPLE_PERIOD) { |
| 78 | period = *(u64 *)more_data; |
| 79 | more_data += sizeof(u64); |
| 80 | } |
| 81 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 82 | 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] | 83 | (void *)(offset + head), |
| 84 | (void *)(long)(event->header.size), |
| 85 | event->header.misc, |
| 86 | event->ip.pid, event->ip.tid, |
| 87 | (void *)(long)ip, |
| 88 | (long long)period); |
| 89 | |
| 90 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
| 91 | |
| 92 | if (thread == NULL) { |
| 93 | eprintf("problem processing %d event, skipping it.\n", |
| 94 | event->header.type); |
| 95 | return -1; |
| 96 | } |
| 97 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 98 | if (sample_type & PERF_SAMPLE_RAW) { |
| 99 | struct { |
| 100 | u32 size; |
| 101 | char data[0]; |
| 102 | } *raw = more_data; |
| 103 | |
| 104 | /* |
| 105 | * FIXME: better resolve from pid from the struct trace_entry |
| 106 | * field, although it should be the same than this perf |
| 107 | * event pid |
| 108 | */ |
Ingo Molnar | 6ddf259 | 2009-09-03 12:00:22 +0200 | [diff] [blame] | 109 | print_event(cpu, raw->data, raw->size, timestamp, thread->comm); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 110 | } |
| 111 | total += period; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame^] | 116 | static int sample_type_check(u64 type) |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 117 | { |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame^] | 118 | sample_type = type; |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 119 | |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame^] | 120 | if (!(sample_type & PERF_SAMPLE_RAW)) { |
| 121 | fprintf(stderr, |
| 122 | "No trace sample to read. Did you call perf record " |
| 123 | "without -R?"); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame^] | 130 | static struct perf_file_handler file_handler = { |
| 131 | .process_sample_event = process_sample_event, |
| 132 | .process_comm_event = process_comm_event, |
| 133 | .sample_type_check = sample_type_check, |
| 134 | }; |
| 135 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 136 | static int __cmd_trace(void) |
| 137 | { |
Frederic Weisbecker | 3a2684c | 2009-08-31 06:45:19 +0200 | [diff] [blame] | 138 | register_idle_thread(&threads, &last_match); |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame^] | 139 | register_perf_file_handler(&file_handler); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 140 | |
Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame^] | 141 | return mmap_dispatch_perf_file(&header, input_name, 0, 0, &cwdlen, &cwd); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static const char * const annotate_usage[] = { |
| 145 | "perf trace [<options>] <command>", |
| 146 | NULL |
| 147 | }; |
| 148 | |
| 149 | static const struct option options[] = { |
| 150 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 151 | "dump raw trace in ASCII"), |
| 152 | OPT_BOOLEAN('v', "verbose", &verbose, |
| 153 | "be more verbose (show symbol address, etc)"), |
Masami Hiramatsu | 1909629 | 2009-08-21 14:56:03 -0400 | [diff] [blame] | 154 | OPT_END() |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | int cmd_trace(int argc, const char **argv, const char *prefix __used) |
| 158 | { |
| 159 | symbol__init(); |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 160 | |
| 161 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
| 162 | if (argc) { |
| 163 | /* |
| 164 | * Special case: if there's an argument left then assume tha |
| 165 | * it's a symbol filter: |
| 166 | */ |
| 167 | if (argc > 1) |
| 168 | usage_with_options(annotate_usage, options); |
| 169 | } |
| 170 | |
Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 171 | setup_pager(); |
| 172 | |
| 173 | return __cmd_trace(); |
| 174 | } |