blob: d9abb4ae5f795fbe40db652643652265b959abae [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#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
16static char const *input_name = "perf.data";
17static int input;
18static unsigned long page_size;
19static unsigned long mmap_window = 32;
20
21static unsigned long total = 0;
22static unsigned long total_comm = 0;
23
24static struct rb_root threads;
25static struct thread *last_match;
26
27static struct perf_header *header;
28static u64 sample_type;
29
30
31static int
32process_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 Molnarcdd6c482009-09-21 12:02:48 +020038 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020039 (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 Molnarcdd6c482009-09-21 12:02:48 +020045 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020046 return -1;
47 }
48 total_comm++;
49
50 return 0;
51}
52
53static int
54process_sample_event(event_t *event, unsigned long offset, unsigned long head)
55{
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020056 struct thread *thread;
57 u64 ip = event->ip.ip;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020058 u64 timestamp = -1;
Ingo Molnarcd6feee2009-09-02 20:20:38 +020059 u32 cpu = -1;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020060 u64 period = 1;
61 void *more_data = event->ip.__more_data;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020062
63 thread = threads__findnew(event->ip.pid, &threads, &last_match);
64
Ingo Molnar6ddf2592009-09-03 12:00:22 +020065 if (sample_type & PERF_SAMPLE_TIME) {
66 timestamp = *(u64 *)more_data;
67 more_data += sizeof(u64);
68 }
69
Ingo Molnarcd6feee2009-09-02 20:20:38 +020070 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 Weisbecker5f9c39d2009-08-17 16:18:08 +020076 if (sample_type & PERF_SAMPLE_PERIOD) {
77 period = *(u64 *)more_data;
78 more_data += sizeof(u64);
79 }
80
Ingo Molnarcdd6c482009-09-21 12:02:48 +020081 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020082 (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 Weisbecker5f9c39d2009-08-17 16:18:08 +020097 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 Molnar6ddf2592009-09-03 12:00:22 +0200108 print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200109 }
110 total += period;
111
112 return 0;
113}
114
115static int
116process_event(event_t *event, unsigned long offset, unsigned long head)
117{
118 trace_event(event);
119
120 switch (event->header.type) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200121 case PERF_RECORD_MMAP ... PERF_RECORD_LOST:
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200122 return 0;
123
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200124 case PERF_RECORD_COMM:
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200125 return process_comm_event(event, offset, head);
126
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200127 case PERF_RECORD_EXIT ... PERF_RECORD_READ:
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200128 return 0;
129
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200130 case PERF_RECORD_SAMPLE:
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200131 return process_sample_event(event, offset, head);
132
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200133 case PERF_RECORD_MAX:
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200134 default:
135 return -1;
136 }
137
138 return 0;
139}
140
141static int __cmd_trace(void)
142{
143 int ret, rc = EXIT_FAILURE;
144 unsigned long offset = 0;
145 unsigned long head = 0;
Frederic Weisbeckerb209aa12009-10-06 21:21:26 +0200146 unsigned long shift;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200147 struct stat perf_stat;
148 event_t *event;
149 uint32_t size;
150 char *buf;
151
Frederic Weisbecker3a2684c2009-08-31 06:45:19 +0200152 register_idle_thread(&threads, &last_match);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200153
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 Molnar8886f422009-09-03 16:19:57 +0200171 head = header->data_offset;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200172 sample_type = perf_header__sample_type(header);
173
Frederic Weisbecker4bf23642009-08-17 23:07:49 +0200174 if (!(sample_type & PERF_SAMPLE_RAW))
175 die("No trace sample to read. Did you call perf record "
176 "without -R?");
177
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200178 if (load_kernel() < 0) {
179 perror("failed to load kernel symbols");
180 return EXIT_FAILURE;
181 }
182
Frederic Weisbeckerb209aa12009-10-06 21:21:26 +0200183 shift = page_size * (head / page_size);
184 offset += shift;
185 head -= shift;
186
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200187remap:
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
195more:
196 event = (event_t *)(buf + head);
197
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200198 if (head + event->header.size >= page_size * mmap_window) {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200199 int res;
200
Frederic Weisbeckerb209aa12009-10-06 21:21:26 +0200201 shift = page_size * (head / page_size);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200202 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 Weisbecker5f9c39d2009-08-17 16:18:08 +0200212 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
236static const char * const annotate_usage[] = {
237 "perf trace [<options>] <command>",
238 NULL
239};
240
241static 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 Hiramatsu19096292009-08-21 14:56:03 -0400246 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200247};
248
249int 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 Weisbecker5f9c39d2009-08-17 16:18:08 +0200264 setup_pager();
265
266 return __cmd_trace();
267}