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