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