blob: 8247fd04745a7f6274414d95654262d4206d44aa [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
38 dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
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)) {
45 dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
46 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{
56 char level;
57 int show = 0;
58 struct dso *dso = NULL;
59 struct thread *thread;
60 u64 ip = event->ip.ip;
61 u64 period = 1;
62 void *more_data = event->ip.__more_data;
63 int cpumode;
64
65 thread = threads__findnew(event->ip.pid, &threads, &last_match);
66
67 if (sample_type & PERF_SAMPLE_PERIOD) {
68 period = *(u64 *)more_data;
69 more_data += sizeof(u64);
70 }
71
72 dump_printf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
73 (void *)(offset + head),
74 (void *)(long)(event->header.size),
75 event->header.misc,
76 event->ip.pid, event->ip.tid,
77 (void *)(long)ip,
78 (long long)period);
79
80 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
81
82 if (thread == NULL) {
83 eprintf("problem processing %d event, skipping it.\n",
84 event->header.type);
85 return -1;
86 }
87
88 cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
89
90 if (cpumode == PERF_EVENT_MISC_KERNEL) {
91 show = SHOW_KERNEL;
92 level = 'k';
93
94 dso = kernel_dso;
95
96 dump_printf(" ...... dso: %s\n", dso->name);
97
98 } else if (cpumode == PERF_EVENT_MISC_USER) {
99
100 show = SHOW_USER;
101 level = '.';
102
103 } else {
104 show = SHOW_HV;
105 level = 'H';
106
107 dso = hypervisor_dso;
108
109 dump_printf(" ...... dso: [hypervisor]\n");
110 }
111
112 if (sample_type & PERF_SAMPLE_RAW) {
113 struct {
114 u32 size;
115 char data[0];
116 } *raw = more_data;
117
118 /*
119 * FIXME: better resolve from pid from the struct trace_entry
120 * field, although it should be the same than this perf
121 * event pid
122 */
123 print_event(0, raw->data, raw->size, 0, thread->comm);
124 }
125 total += period;
126
127 return 0;
128}
129
130static int
131process_event(event_t *event, unsigned long offset, unsigned long head)
132{
133 trace_event(event);
134
135 switch (event->header.type) {
136 case PERF_EVENT_MMAP ... PERF_EVENT_LOST:
137 return 0;
138
139 case PERF_EVENT_COMM:
140 return process_comm_event(event, offset, head);
141
142 case PERF_EVENT_EXIT ... PERF_EVENT_READ:
143 return 0;
144
145 case PERF_EVENT_SAMPLE:
146 return process_sample_event(event, offset, head);
147
148 case PERF_EVENT_MAX:
149 default:
150 return -1;
151 }
152
153 return 0;
154}
155
156static int __cmd_trace(void)
157{
158 int ret, rc = EXIT_FAILURE;
159 unsigned long offset = 0;
160 unsigned long head = 0;
161 struct stat perf_stat;
162 event_t *event;
163 uint32_t size;
164 char *buf;
165
166 trace_report();
Frederic Weisbecker3a2684c2009-08-31 06:45:19 +0200167 register_idle_thread(&threads, &last_match);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200168
169 input = open(input_name, O_RDONLY);
170 if (input < 0) {
171 perror("failed to open file");
172 exit(-1);
173 }
174
175 ret = fstat(input, &perf_stat);
176 if (ret < 0) {
177 perror("failed to stat file");
178 exit(-1);
179 }
180
181 if (!perf_stat.st_size) {
182 fprintf(stderr, "zero-sized file, nothing to do!\n");
183 exit(0);
184 }
185 header = perf_header__read(input);
186 sample_type = perf_header__sample_type(header);
187
Frederic Weisbecker4bf23642009-08-17 23:07:49 +0200188 if (!(sample_type & PERF_SAMPLE_RAW))
189 die("No trace sample to read. Did you call perf record "
190 "without -R?");
191
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200192 if (load_kernel() < 0) {
193 perror("failed to load kernel symbols");
194 return EXIT_FAILURE;
195 }
196
197remap:
198 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
199 MAP_SHARED, input, offset);
200 if (buf == MAP_FAILED) {
201 perror("failed to mmap file");
202 exit(-1);
203 }
204
205more:
206 event = (event_t *)(buf + head);
207
208 size = event->header.size;
209 if (!size)
210 size = 8;
211
212 if (head + event->header.size >= page_size * mmap_window) {
213 unsigned long shift = page_size * (head / page_size);
214 int res;
215
216 res = munmap(buf, page_size * mmap_window);
217 assert(res == 0);
218
219 offset += shift;
220 head -= shift;
221 goto remap;
222 }
223
224 size = event->header.size;
225
226
227 if (!size || process_event(event, offset, head) < 0) {
228
229 /*
230 * assume we lost track of the stream, check alignment, and
231 * increment a single u64 in the hope to catch on again 'soon'.
232 */
233
234 if (unlikely(head & 7))
235 head &= ~7ULL;
236
237 size = 8;
238 }
239
240 head += size;
241
242 if (offset + head < (unsigned long)perf_stat.st_size)
243 goto more;
244
245 rc = EXIT_SUCCESS;
246 close(input);
247
248 return rc;
249}
250
251static const char * const annotate_usage[] = {
252 "perf trace [<options>] <command>",
253 NULL
254};
255
256static const struct option options[] = {
257 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
258 "dump raw trace in ASCII"),
259 OPT_BOOLEAN('v', "verbose", &verbose,
260 "be more verbose (show symbol address, etc)"),
Masami Hiramatsu19096292009-08-21 14:56:03 -0400261 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200262};
263
264int cmd_trace(int argc, const char **argv, const char *prefix __used)
265{
266 symbol__init();
267 page_size = getpagesize();
268
269 argc = parse_options(argc, argv, options, annotate_usage, 0);
270 if (argc) {
271 /*
272 * Special case: if there's an argument left then assume tha
273 * it's a symbol filter:
274 */
275 if (argc > 1)
276 usage_with_options(annotate_usage, options);
277 }
278
279
280 setup_pager();
281
282 return __cmd_trace();
283}