perf tools: Merge trace.info content into perf.data
[linux-2.6-block.git] / tools / perf / builtin-trace.c
CommitLineData
5f9c39dc
FW
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
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
cdd6c482 38 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
5f9c39dc
FW
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)) {
cdd6c482 45 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
5f9c39dc
FW
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{
5f9c39dc
FW
56 struct thread *thread;
57 u64 ip = event->ip.ip;
6ddf259d 58 u64 timestamp = -1;
cd6feeea 59 u32 cpu = -1;
5f9c39dc
FW
60 u64 period = 1;
61 void *more_data = event->ip.__more_data;
5f9c39dc
FW
62
63 thread = threads__findnew(event->ip.pid, &threads, &last_match);
64
6ddf259d
IM
65 if (sample_type & PERF_SAMPLE_TIME) {
66 timestamp = *(u64 *)more_data;
67 more_data += sizeof(u64);
68 }
69
cd6feeea
IM
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
5f9c39dc
FW
76 if (sample_type & PERF_SAMPLE_PERIOD) {
77 period = *(u64 *)more_data;
78 more_data += sizeof(u64);
79 }
80
cdd6c482 81 dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
5f9c39dc
FW
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
5f9c39dc
FW
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 */
6ddf259d 108 print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
5f9c39dc
FW
109 }
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) {
cdd6c482 121 case PERF_RECORD_MMAP ... PERF_RECORD_LOST:
5f9c39dc
FW
122 return 0;
123
cdd6c482 124 case PERF_RECORD_COMM:
5f9c39dc
FW
125 return process_comm_event(event, offset, head);
126
cdd6c482 127 case PERF_RECORD_EXIT ... PERF_RECORD_READ:
5f9c39dc
FW
128 return 0;
129
cdd6c482 130 case PERF_RECORD_SAMPLE:
5f9c39dc
FW
131 return process_sample_event(event, offset, head);
132
cdd6c482 133 case PERF_RECORD_MAX:
5f9c39dc
FW
134 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;
b209aa1f 146 unsigned long shift;
5f9c39dc
FW
147 struct stat perf_stat;
148 event_t *event;
149 uint32_t size;
150 char *buf;
151
3a2684ca 152 register_idle_thread(&threads, &last_match);
5f9c39dc
FW
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);
8886f42d 171 head = header->data_offset;
5f9c39dc
FW
172 sample_type = perf_header__sample_type(header);
173
4bf2364a
FW
174 if (!(sample_type & PERF_SAMPLE_RAW))
175 die("No trace sample to read. Did you call perf record "
176 "without -R?");
177
5f9c39dc
FW
178 if (load_kernel() < 0) {
179 perror("failed to load kernel symbols");
180 return EXIT_FAILURE;
181 }
182
b209aa1f
FW
183 shift = page_size * (head / page_size);
184 offset += shift;
185 head -= shift;
186
5f9c39dc
FW
187remap:
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
5f9c39dc 198 if (head + event->header.size >= page_size * mmap_window) {
5f9c39dc
FW
199 int res;
200
b209aa1f 201 shift = page_size * (head / page_size);
5f9c39dc
FW
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
5f9c39dc
FW
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
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)"),
1909629f 246 OPT_END()
5f9c39dc
FW
247};
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
5f9c39dc
FW
264 setup_pager();
265
266 return __cmd_trace();
267}