aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnaldo Carvalho de Melo <acme@redhat.com>2009-12-27 18:36:59 -0500
committerIngo Molnar <mingo@elte.hu>2009-12-28 03:03:32 -0500
commit06aae590033d1ae3c35b2920ef950cfc603e2a2d (patch)
treec896105d0f394c845304a507c72b8193886261d9
parent3912f2abc942a002ef611fc973add5e5eadb3432 (diff)
perf session: Move the event processing routines to session.c
No need for an extra "data_map" file since the routines there operate mainly on a perf_session instance. Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Frédéric Weisbecker <fweisbec@gmail.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Paul Mackerras <paulus@samba.org> LKML-Reference: <1261957026-15580-3-git-send-email-acme@infradead.org> Signed-off-by: Ingo Molnar <mingo@elte.hu>
-rw-r--r--tools/perf/Makefile1
-rw-r--r--tools/perf/util/data_map.c252
-rw-r--r--tools/perf/util/session.c245
3 files changed, 245 insertions, 253 deletions
diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index 652a470b5f74..4172c3b0e4a7 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -423,7 +423,6 @@ LIB_OBJS += util/trace-event-perl.o
423LIB_OBJS += util/svghelper.o 423LIB_OBJS += util/svghelper.o
424LIB_OBJS += util/sort.o 424LIB_OBJS += util/sort.o
425LIB_OBJS += util/hist.o 425LIB_OBJS += util/hist.o
426LIB_OBJS += util/data_map.o
427LIB_OBJS += util/probe-event.o 426LIB_OBJS += util/probe-event.o
428 427
429BUILTIN_OBJS += builtin-annotate.o 428BUILTIN_OBJS += builtin-annotate.o
diff --git a/tools/perf/util/data_map.c b/tools/perf/util/data_map.c
deleted file mode 100644
index b557b836de3d..000000000000
--- a/tools/perf/util/data_map.c
+++ /dev/null
@@ -1,252 +0,0 @@
1#include "symbol.h"
2#include "util.h"
3#include "debug.h"
4#include "thread.h"
5#include "session.h"
6
7static int process_event_stub(event_t *event __used,
8 struct perf_session *session __used)
9{
10 dump_printf(": unhandled!\n");
11 return 0;
12}
13
14static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
15{
16 if (!handler->process_sample_event)
17 handler->process_sample_event = process_event_stub;
18 if (!handler->process_mmap_event)
19 handler->process_mmap_event = process_event_stub;
20 if (!handler->process_comm_event)
21 handler->process_comm_event = process_event_stub;
22 if (!handler->process_fork_event)
23 handler->process_fork_event = process_event_stub;
24 if (!handler->process_exit_event)
25 handler->process_exit_event = process_event_stub;
26 if (!handler->process_lost_event)
27 handler->process_lost_event = process_event_stub;
28 if (!handler->process_read_event)
29 handler->process_read_event = process_event_stub;
30 if (!handler->process_throttle_event)
31 handler->process_throttle_event = process_event_stub;
32 if (!handler->process_unthrottle_event)
33 handler->process_unthrottle_event = process_event_stub;
34}
35
36static const char *event__name[] = {
37 [0] = "TOTAL",
38 [PERF_RECORD_MMAP] = "MMAP",
39 [PERF_RECORD_LOST] = "LOST",
40 [PERF_RECORD_COMM] = "COMM",
41 [PERF_RECORD_EXIT] = "EXIT",
42 [PERF_RECORD_THROTTLE] = "THROTTLE",
43 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
44 [PERF_RECORD_FORK] = "FORK",
45 [PERF_RECORD_READ] = "READ",
46 [PERF_RECORD_SAMPLE] = "SAMPLE",
47};
48
49unsigned long event__total[PERF_RECORD_MAX];
50
51void event__print_totals(void)
52{
53 int i;
54 for (i = 0; i < PERF_RECORD_MAX; ++i)
55 pr_info("%10s events: %10ld\n",
56 event__name[i], event__total[i]);
57}
58
59static int process_event(event_t *event, struct perf_session *session,
60 struct perf_event_ops *ops,
61 unsigned long offset, unsigned long head)
62{
63 trace_event(event);
64
65 if (event->header.type < PERF_RECORD_MAX) {
66 dump_printf("%p [%p]: PERF_RECORD_%s",
67 (void *)(offset + head),
68 (void *)(long)(event->header.size),
69 event__name[event->header.type]);
70 ++event__total[0];
71 ++event__total[event->header.type];
72 }
73
74 switch (event->header.type) {
75 case PERF_RECORD_SAMPLE:
76 return ops->process_sample_event(event, session);
77 case PERF_RECORD_MMAP:
78 return ops->process_mmap_event(event, session);
79 case PERF_RECORD_COMM:
80 return ops->process_comm_event(event, session);
81 case PERF_RECORD_FORK:
82 return ops->process_fork_event(event, session);
83 case PERF_RECORD_EXIT:
84 return ops->process_exit_event(event, session);
85 case PERF_RECORD_LOST:
86 return ops->process_lost_event(event, session);
87 case PERF_RECORD_READ:
88 return ops->process_read_event(event, session);
89 case PERF_RECORD_THROTTLE:
90 return ops->process_throttle_event(event, session);
91 case PERF_RECORD_UNTHROTTLE:
92 return ops->process_unthrottle_event(event, session);
93 default:
94 ops->total_unknown++;
95 return -1;
96 }
97}
98
99int perf_header__read_build_ids(int input, u64 offset, u64 size)
100{
101 struct build_id_event bev;
102 char filename[PATH_MAX];
103 u64 limit = offset + size;
104 int err = -1;
105
106 while (offset < limit) {
107 struct dso *dso;
108 ssize_t len;
109
110 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
111 goto out;
112
113 len = bev.header.size - sizeof(bev);
114 if (read(input, filename, len) != len)
115 goto out;
116
117 dso = dsos__findnew(filename);
118 if (dso != NULL)
119 dso__set_build_id(dso, &bev.build_id);
120
121 offset += bev.header.size;
122 }
123 err = 0;
124out:
125 return err;
126}
127
128static struct thread *perf_session__register_idle_thread(struct perf_session *self)
129{
130 struct thread *thread = perf_session__findnew(self, 0);
131
132 if (!thread || thread__set_comm(thread, "swapper")) {
133 pr_err("problem inserting idle task.\n");
134 thread = NULL;
135 }
136
137 return thread;
138}
139
140int perf_session__process_events(struct perf_session *self,
141 struct perf_event_ops *ops)
142{
143 int err;
144 unsigned long head, shift;
145 unsigned long offset = 0;
146 size_t page_size;
147 event_t *event;
148 uint32_t size;
149 char *buf;
150
151 if (perf_session__register_idle_thread(self) == NULL)
152 return -ENOMEM;
153
154 perf_event_ops__fill_defaults(ops);
155
156 page_size = getpagesize();
157
158 head = self->header.data_offset;
159 self->sample_type = perf_header__sample_type(&self->header);
160
161 err = -EINVAL;
162 if (ops->sample_type_check && ops->sample_type_check(self) < 0)
163 goto out_err;
164
165 if (!ops->full_paths) {
166 char bf[PATH_MAX];
167
168 if (getcwd(bf, sizeof(bf)) == NULL) {
169 err = -errno;
170out_getcwd_err:
171 pr_err("failed to get the current directory\n");
172 goto out_err;
173 }
174 self->cwd = strdup(bf);
175 if (self->cwd == NULL) {
176 err = -ENOMEM;
177 goto out_getcwd_err;
178 }
179 self->cwdlen = strlen(self->cwd);
180 }
181
182 shift = page_size * (head / page_size);
183 offset += shift;
184 head -= shift;
185
186remap:
187 buf = mmap(NULL, page_size * self->mmap_window, PROT_READ,
188 MAP_SHARED, self->fd, offset);
189 if (buf == MAP_FAILED) {
190 pr_err("failed to mmap file\n");
191 err = -errno;
192 goto out_err;
193 }
194
195more:
196 event = (event_t *)(buf + head);
197
198 size = event->header.size;
199 if (!size)
200 size = 8;
201
202 if (head + event->header.size >= page_size * self->mmap_window) {
203 int munmap_ret;
204
205 shift = page_size * (head / page_size);
206
207 munmap_ret = munmap(buf, page_size * self->mmap_window);
208 assert(munmap_ret == 0);
209
210 offset += shift;
211 head -= shift;
212 goto remap;
213 }
214
215 size = event->header.size;
216
217 dump_printf("\n%p [%p]: event: %d\n",
218 (void *)(offset + head),
219 (void *)(long)event->header.size,
220 event->header.type);
221
222 if (!size || process_event(event, self, ops, offset, head) < 0) {
223
224 dump_printf("%p [%p]: skipping unknown header type: %d\n",
225 (void *)(offset + head),
226 (void *)(long)(event->header.size),
227 event->header.type);
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 >= self->header.data_offset + self->header.data_size)
243 goto done;
244
245 if (offset + head < self->size)
246 goto more;
247
248done:
249 err = 0;
250out_err:
251 return err;
252}
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index ce3a6c8abe76..736d4fda9272 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -148,3 +148,248 @@ struct symbol **perf_session__resolve_callchain(struct perf_session *self,
148 148
149 return syms; 149 return syms;
150} 150}
151
152static int process_event_stub(event_t *event __used,
153 struct perf_session *session __used)
154{
155 dump_printf(": unhandled!\n");
156 return 0;
157}
158
159static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
160{
161 if (handler->process_sample_event == NULL)
162 handler->process_sample_event = process_event_stub;
163 if (handler->process_mmap_event == NULL)
164 handler->process_mmap_event = process_event_stub;
165 if (handler->process_comm_event == NULL)
166 handler->process_comm_event = process_event_stub;
167 if (handler->process_fork_event == NULL)
168 handler->process_fork_event = process_event_stub;
169 if (handler->process_exit_event == NULL)
170 handler->process_exit_event = process_event_stub;
171 if (handler->process_lost_event == NULL)
172 handler->process_lost_event = process_event_stub;
173 if (handler->process_read_event == NULL)
174 handler->process_read_event = process_event_stub;
175 if (handler->process_throttle_event == NULL)
176 handler->process_throttle_event = process_event_stub;
177 if (handler->process_unthrottle_event == NULL)
178 handler->process_unthrottle_event = process_event_stub;
179}
180
181static const char *event__name[] = {
182 [0] = "TOTAL",
183 [PERF_RECORD_MMAP] = "MMAP",
184 [PERF_RECORD_LOST] = "LOST",
185 [PERF_RECORD_COMM] = "COMM",
186 [PERF_RECORD_EXIT] = "EXIT",
187 [PERF_RECORD_THROTTLE] = "THROTTLE",
188 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
189 [PERF_RECORD_FORK] = "FORK",
190 [PERF_RECORD_READ] = "READ",
191 [PERF_RECORD_SAMPLE] = "SAMPLE",
192};
193
194unsigned long event__total[PERF_RECORD_MAX];
195
196void event__print_totals(void)
197{
198 int i;
199 for (i = 0; i < PERF_RECORD_MAX; ++i)
200 pr_info("%10s events: %10ld\n",
201 event__name[i], event__total[i]);
202}
203
204static int perf_session__process_event(struct perf_session *self,
205 event_t *event,
206 struct perf_event_ops *ops,
207 unsigned long offset, unsigned long head)
208{
209 trace_event(event);
210
211 if (event->header.type < PERF_RECORD_MAX) {
212 dump_printf("%p [%p]: PERF_RECORD_%s",
213 (void *)(offset + head),
214 (void *)(long)(event->header.size),
215 event__name[event->header.type]);
216 ++event__total[0];
217 ++event__total[event->header.type];
218 }
219
220 switch (event->header.type) {
221 case PERF_RECORD_SAMPLE:
222 return ops->process_sample_event(event, self);
223 case PERF_RECORD_MMAP:
224 return ops->process_mmap_event(event, self);
225 case PERF_RECORD_COMM:
226 return ops->process_comm_event(event, self);
227 case PERF_RECORD_FORK:
228 return ops->process_fork_event(event, self);
229 case PERF_RECORD_EXIT:
230 return ops->process_exit_event(event, self);
231 case PERF_RECORD_LOST:
232 return ops->process_lost_event(event, self);
233 case PERF_RECORD_READ:
234 return ops->process_read_event(event, self);
235 case PERF_RECORD_THROTTLE:
236 return ops->process_throttle_event(event, self);
237 case PERF_RECORD_UNTHROTTLE:
238 return ops->process_unthrottle_event(event, self);
239 default:
240 ops->total_unknown++;
241 return -1;
242 }
243}
244
245int perf_header__read_build_ids(int input, u64 offset, u64 size)
246{
247 struct build_id_event bev;
248 char filename[PATH_MAX];
249 u64 limit = offset + size;
250 int err = -1;
251
252 while (offset < limit) {
253 struct dso *dso;
254 ssize_t len;
255
256 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
257 goto out;
258
259 len = bev.header.size - sizeof(bev);
260 if (read(input, filename, len) != len)
261 goto out;
262
263 dso = dsos__findnew(filename);
264 if (dso != NULL)
265 dso__set_build_id(dso, &bev.build_id);
266
267 offset += bev.header.size;
268 }
269 err = 0;
270out:
271 return err;
272}
273
274static struct thread *perf_session__register_idle_thread(struct perf_session *self)
275{
276 struct thread *thread = perf_session__findnew(self, 0);
277
278 if (thread == NULL || thread__set_comm(thread, "swapper")) {
279 pr_err("problem inserting idle task.\n");
280 thread = NULL;
281 }
282
283 return thread;
284}
285
286int perf_session__process_events(struct perf_session *self,
287 struct perf_event_ops *ops)
288{
289 int err;
290 unsigned long head, shift;
291 unsigned long offset = 0;
292 size_t page_size;
293 event_t *event;
294 uint32_t size;
295 char *buf;
296
297 if (perf_session__register_idle_thread(self) == NULL)
298 return -ENOMEM;
299
300 perf_event_ops__fill_defaults(ops);
301
302 page_size = getpagesize();
303
304 head = self->header.data_offset;
305 self->sample_type = perf_header__sample_type(&self->header);
306
307 err = -EINVAL;
308 if (ops->sample_type_check && ops->sample_type_check(self) < 0)
309 goto out_err;
310
311 if (!ops->full_paths) {
312 char bf[PATH_MAX];
313
314 if (getcwd(bf, sizeof(bf)) == NULL) {
315 err = -errno;
316out_getcwd_err:
317 pr_err("failed to get the current directory\n");
318 goto out_err;
319 }
320 self->cwd = strdup(bf);
321 if (self->cwd == NULL) {
322 err = -ENOMEM;
323 goto out_getcwd_err;
324 }
325 self->cwdlen = strlen(self->cwd);
326 }
327
328 shift = page_size * (head / page_size);
329 offset += shift;
330 head -= shift;
331
332remap:
333 buf = mmap(NULL, page_size * self->mmap_window, PROT_READ,
334 MAP_SHARED, self->fd, offset);
335 if (buf == MAP_FAILED) {
336 pr_err("failed to mmap file\n");
337 err = -errno;
338 goto out_err;
339 }
340
341more:
342 event = (event_t *)(buf + head);
343
344 size = event->header.size;
345 if (size == 0)
346 size = 8;
347
348 if (head + event->header.size >= page_size * self->mmap_window) {
349 int munmap_ret;
350
351 shift = page_size * (head / page_size);
352
353 munmap_ret = munmap(buf, page_size * self->mmap_window);
354 assert(munmap_ret == 0);
355
356 offset += shift;
357 head -= shift;
358 goto remap;
359 }
360
361 size = event->header.size;
362
363 dump_printf("\n%p [%p]: event: %d\n",
364 (void *)(offset + head),
365 (void *)(long)event->header.size,
366 event->header.type);
367
368 if (size == 0 ||
369 perf_session__process_event(self, event, ops, offset, head) < 0) {
370 dump_printf("%p [%p]: skipping unknown header type: %d\n",
371 (void *)(offset + head),
372 (void *)(long)(event->header.size),
373 event->header.type);
374 /*
375 * assume we lost track of the stream, check alignment, and
376 * increment a single u64 in the hope to catch on again 'soon'.
377 */
378 if (unlikely(head & 7))
379 head &= ~7ULL;
380
381 size = 8;
382 }
383
384 head += size;
385
386 if (offset + head >= self->header.data_offset + self->header.data_size)
387 goto done;
388
389 if (offset + head < self->size)
390 goto more;
391done:
392 err = 0;
393out_err:
394 return err;
395}