6 static unsigned long mmap_window = 32;
7 static char __cwd[PATH_MAX];
9 static int process_event_stub(event_t *event __used,
10 struct perf_session *session __used)
12 dump_printf(": unhandled!\n");
16 static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
18 if (!handler->process_sample_event)
19 handler->process_sample_event = process_event_stub;
20 if (!handler->process_mmap_event)
21 handler->process_mmap_event = process_event_stub;
22 if (!handler->process_comm_event)
23 handler->process_comm_event = process_event_stub;
24 if (!handler->process_fork_event)
25 handler->process_fork_event = process_event_stub;
26 if (!handler->process_exit_event)
27 handler->process_exit_event = process_event_stub;
28 if (!handler->process_lost_event)
29 handler->process_lost_event = process_event_stub;
30 if (!handler->process_read_event)
31 handler->process_read_event = process_event_stub;
32 if (!handler->process_throttle_event)
33 handler->process_throttle_event = process_event_stub;
34 if (!handler->process_unthrottle_event)
35 handler->process_unthrottle_event = process_event_stub;
38 static const char *event__name[] = {
40 [PERF_RECORD_MMAP] = "MMAP",
41 [PERF_RECORD_LOST] = "LOST",
42 [PERF_RECORD_COMM] = "COMM",
43 [PERF_RECORD_EXIT] = "EXIT",
44 [PERF_RECORD_THROTTLE] = "THROTTLE",
45 [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
46 [PERF_RECORD_FORK] = "FORK",
47 [PERF_RECORD_READ] = "READ",
48 [PERF_RECORD_SAMPLE] = "SAMPLE",
51 unsigned long event__total[PERF_RECORD_MAX];
53 void event__print_totals(void)
56 for (i = 0; i < PERF_RECORD_MAX; ++i)
57 pr_info("%10s events: %10ld\n",
58 event__name[i], event__total[i]);
61 static int process_event(event_t *event, struct perf_session *session,
62 struct perf_event_ops *ops,
63 unsigned long offset, unsigned long head)
67 if (event->header.type < PERF_RECORD_MAX) {
68 dump_printf("%p [%p]: PERF_RECORD_%s",
69 (void *)(offset + head),
70 (void *)(long)(event->header.size),
71 event__name[event->header.type]);
73 ++event__total[event->header.type];
76 switch (event->header.type) {
77 case PERF_RECORD_SAMPLE:
78 return ops->process_sample_event(event, session);
79 case PERF_RECORD_MMAP:
80 return ops->process_mmap_event(event, session);
81 case PERF_RECORD_COMM:
82 return ops->process_comm_event(event, session);
83 case PERF_RECORD_FORK:
84 return ops->process_fork_event(event, session);
85 case PERF_RECORD_EXIT:
86 return ops->process_exit_event(event, session);
87 case PERF_RECORD_LOST:
88 return ops->process_lost_event(event, session);
89 case PERF_RECORD_READ:
90 return ops->process_read_event(event, session);
91 case PERF_RECORD_THROTTLE:
92 return ops->process_throttle_event(event, session);
93 case PERF_RECORD_UNTHROTTLE:
94 return ops->process_unthrottle_event(event, session);
101 int perf_header__read_build_ids(int input, u64 offset, u64 size)
103 struct build_id_event bev;
104 char filename[PATH_MAX];
105 u64 limit = offset + size;
108 while (offset < limit) {
112 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
115 len = bev.header.size - sizeof(bev);
116 if (read(input, filename, len) != len)
119 dso = dsos__findnew(filename);
121 dso__set_build_id(dso, &bev.build_id);
123 offset += bev.header.size;
130 int perf_session__process_events(struct perf_session *self,
131 struct perf_event_ops *ops,
132 int full_paths, int *cwdlen, char **cwd)
135 unsigned long head, shift;
136 unsigned long offset = 0;
143 perf_event_ops__fill_defaults(ops);
145 page_size = getpagesize();
147 head = self->header.data_offset;
148 sample_type = perf_header__sample_type(&self->header);
151 if (ops->sample_type_check &&
152 ops->sample_type_check(sample_type) < 0)
156 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
157 pr_err("failed to get the current directory\n");
162 *cwdlen = strlen(*cwd);
168 shift = page_size * (head / page_size);
173 buf = mmap(NULL, page_size * mmap_window, PROT_READ,
174 MAP_SHARED, self->fd, offset);
175 if (buf == MAP_FAILED) {
176 pr_err("failed to mmap file\n");
182 event = (event_t *)(buf + head);
184 size = event->header.size;
188 if (head + event->header.size >= page_size * mmap_window) {
191 shift = page_size * (head / page_size);
193 munmap_ret = munmap(buf, page_size * mmap_window);
194 assert(munmap_ret == 0);
201 size = event->header.size;
203 dump_printf("\n%p [%p]: event: %d\n",
204 (void *)(offset + head),
205 (void *)(long)event->header.size,
208 if (!size || process_event(event, self, ops, offset, head) < 0) {
210 dump_printf("%p [%p]: skipping unknown header type: %d\n",
211 (void *)(offset + head),
212 (void *)(long)(event->header.size),
216 * assume we lost track of the stream, check alignment, and
217 * increment a single u64 in the hope to catch on again 'soon'.
220 if (unlikely(head & 7))
228 if (offset + head >= self->header.data_offset + self->header.data_size)
231 if (offset + head < self->size)