perf session: Register the idle thread in perf_session__process_events
[linux-3.10.git] / tools / perf / util / data_map.c
1 #include "symbol.h"
2 #include "util.h"
3 #include "debug.h"
4 #include "thread.h"
5 #include "session.h"
6
7 static unsigned long    mmap_window = 32;
8 static char             __cwd[PATH_MAX];
9
10 static int process_event_stub(event_t *event __used,
11                               struct perf_session *session __used)
12 {
13         dump_printf(": unhandled!\n");
14         return 0;
15 }
16
17 static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
18 {
19         if (!handler->process_sample_event)
20                 handler->process_sample_event = process_event_stub;
21         if (!handler->process_mmap_event)
22                 handler->process_mmap_event = process_event_stub;
23         if (!handler->process_comm_event)
24                 handler->process_comm_event = process_event_stub;
25         if (!handler->process_fork_event)
26                 handler->process_fork_event = process_event_stub;
27         if (!handler->process_exit_event)
28                 handler->process_exit_event = process_event_stub;
29         if (!handler->process_lost_event)
30                 handler->process_lost_event = process_event_stub;
31         if (!handler->process_read_event)
32                 handler->process_read_event = process_event_stub;
33         if (!handler->process_throttle_event)
34                 handler->process_throttle_event = process_event_stub;
35         if (!handler->process_unthrottle_event)
36                 handler->process_unthrottle_event = process_event_stub;
37 }
38
39 static const char *event__name[] = {
40         [0]                      = "TOTAL",
41         [PERF_RECORD_MMAP]       = "MMAP",
42         [PERF_RECORD_LOST]       = "LOST",
43         [PERF_RECORD_COMM]       = "COMM",
44         [PERF_RECORD_EXIT]       = "EXIT",
45         [PERF_RECORD_THROTTLE]   = "THROTTLE",
46         [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
47         [PERF_RECORD_FORK]       = "FORK",
48         [PERF_RECORD_READ]       = "READ",
49         [PERF_RECORD_SAMPLE]     = "SAMPLE",
50 };
51
52 unsigned long event__total[PERF_RECORD_MAX];
53
54 void event__print_totals(void)
55 {
56         int i;
57         for (i = 0; i < PERF_RECORD_MAX; ++i)
58                 pr_info("%10s events: %10ld\n",
59                         event__name[i], event__total[i]);
60 }
61
62 static int process_event(event_t *event, struct perf_session *session,
63                          struct perf_event_ops *ops,
64                          unsigned long offset, unsigned long head)
65 {
66         trace_event(event);
67
68         if (event->header.type < PERF_RECORD_MAX) {
69                 dump_printf("%p [%p]: PERF_RECORD_%s",
70                             (void *)(offset + head),
71                             (void *)(long)(event->header.size),
72                             event__name[event->header.type]);
73                 ++event__total[0];
74                 ++event__total[event->header.type];
75         }
76
77         switch (event->header.type) {
78         case PERF_RECORD_SAMPLE:
79                 return ops->process_sample_event(event, session);
80         case PERF_RECORD_MMAP:
81                 return ops->process_mmap_event(event, session);
82         case PERF_RECORD_COMM:
83                 return ops->process_comm_event(event, session);
84         case PERF_RECORD_FORK:
85                 return ops->process_fork_event(event, session);
86         case PERF_RECORD_EXIT:
87                 return ops->process_exit_event(event, session);
88         case PERF_RECORD_LOST:
89                 return ops->process_lost_event(event, session);
90         case PERF_RECORD_READ:
91                 return ops->process_read_event(event, session);
92         case PERF_RECORD_THROTTLE:
93                 return ops->process_throttle_event(event, session);
94         case PERF_RECORD_UNTHROTTLE:
95                 return ops->process_unthrottle_event(event, session);
96         default:
97                 ops->total_unknown++;
98                 return -1;
99         }
100 }
101
102 int perf_header__read_build_ids(int input, u64 offset, u64 size)
103 {
104         struct build_id_event bev;
105         char filename[PATH_MAX];
106         u64 limit = offset + size;
107         int err = -1;
108
109         while (offset < limit) {
110                 struct dso *dso;
111                 ssize_t len;
112
113                 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
114                         goto out;
115
116                 len = bev.header.size - sizeof(bev);
117                 if (read(input, filename, len) != len)
118                         goto out;
119
120                 dso = dsos__findnew(filename);
121                 if (dso != NULL)
122                         dso__set_build_id(dso, &bev.build_id);
123
124                 offset += bev.header.size;
125         }
126         err = 0;
127 out:
128         return err;
129 }
130
131 static struct thread *perf_session__register_idle_thread(struct perf_session *self __used)
132 {
133         struct thread *thread = threads__findnew(0);
134
135         if (!thread || thread__set_comm(thread, "swapper")) {
136                 pr_err("problem inserting idle task.\n");
137                 thread = NULL;
138         }
139
140         return thread;
141 }
142
143 int perf_session__process_events(struct perf_session *self,
144                                  struct perf_event_ops *ops,
145                                  int full_paths, int *cwdlen, char **cwd)
146 {
147         int err;
148         unsigned long head, shift;
149         unsigned long offset = 0;
150         size_t  page_size;
151         u64 sample_type;
152         event_t *event;
153         uint32_t size;
154         char *buf;
155
156         if (perf_session__register_idle_thread(self) == NULL)
157                 return -ENOMEM;
158
159         perf_event_ops__fill_defaults(ops);
160
161         page_size = getpagesize();
162
163         head = self->header.data_offset;
164         sample_type = perf_header__sample_type(&self->header);
165
166         err = -EINVAL;
167         if (ops->sample_type_check &&
168             ops->sample_type_check(sample_type) < 0)
169                 goto out_err;
170
171         if (!full_paths) {
172                 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
173                         pr_err("failed to get the current directory\n");
174                         err = -errno;
175                         goto out_err;
176                 }
177                 *cwd = __cwd;
178                 *cwdlen = strlen(*cwd);
179         } else {
180                 *cwd = NULL;
181                 *cwdlen = 0;
182         }
183
184         shift = page_size * (head / page_size);
185         offset += shift;
186         head -= shift;
187
188 remap:
189         buf = mmap(NULL, page_size * mmap_window, PROT_READ,
190                    MAP_SHARED, self->fd, offset);
191         if (buf == MAP_FAILED) {
192                 pr_err("failed to mmap file\n");
193                 err = -errno;
194                 goto out_err;
195         }
196
197 more:
198         event = (event_t *)(buf + head);
199
200         size = event->header.size;
201         if (!size)
202                 size = 8;
203
204         if (head + event->header.size >= page_size * mmap_window) {
205                 int munmap_ret;
206
207                 shift = page_size * (head / page_size);
208
209                 munmap_ret = munmap(buf, page_size * mmap_window);
210                 assert(munmap_ret == 0);
211
212                 offset += shift;
213                 head -= shift;
214                 goto remap;
215         }
216
217         size = event->header.size;
218
219         dump_printf("\n%p [%p]: event: %d\n",
220                         (void *)(offset + head),
221                         (void *)(long)event->header.size,
222                         event->header.type);
223
224         if (!size || process_event(event, self, ops, offset, head) < 0) {
225
226                 dump_printf("%p [%p]: skipping unknown header type: %d\n",
227                         (void *)(offset + head),
228                         (void *)(long)(event->header.size),
229                         event->header.type);
230
231                 /*
232                  * assume we lost track of the stream, check alignment, and
233                  * increment a single u64 in the hope to catch on again 'soon'.
234                  */
235
236                 if (unlikely(head & 7))
237                         head &= ~7ULL;
238
239                 size = 8;
240         }
241
242         head += size;
243
244         if (offset + head >= self->header.data_offset + self->header.data_size)
245                 goto done;
246
247         if (offset + head < self->size)
248                 goto more;
249
250 done:
251         err = 0;
252 out_err:
253         return err;
254 }