perf session: Move the hist_entries rb tree to perf_session
[pandora-kernel.git] / tools / perf / util / event.c
1 #include <linux/types.h>
2 #include "event.h"
3 #include "debug.h"
4 #include "session.h"
5 #include "string.h"
6 #include "thread.h"
7
8 static pid_t event__synthesize_comm(pid_t pid, int full,
9                                     int (*process)(event_t *event,
10                                                    struct perf_session *session),
11                                     struct perf_session *session)
12 {
13         event_t ev;
14         char filename[PATH_MAX];
15         char bf[BUFSIZ];
16         FILE *fp;
17         size_t size = 0;
18         DIR *tasks;
19         struct dirent dirent, *next;
20         pid_t tgid = 0;
21
22         snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
23
24         fp = fopen(filename, "r");
25         if (fp == NULL) {
26 out_race:
27                 /*
28                  * We raced with a task exiting - just return:
29                  */
30                 pr_debug("couldn't open %s\n", filename);
31                 return 0;
32         }
33
34         memset(&ev.comm, 0, sizeof(ev.comm));
35         while (!ev.comm.comm[0] || !ev.comm.pid) {
36                 if (fgets(bf, sizeof(bf), fp) == NULL)
37                         goto out_failure;
38
39                 if (memcmp(bf, "Name:", 5) == 0) {
40                         char *name = bf + 5;
41                         while (*name && isspace(*name))
42                                 ++name;
43                         size = strlen(name) - 1;
44                         memcpy(ev.comm.comm, name, size++);
45                 } else if (memcmp(bf, "Tgid:", 5) == 0) {
46                         char *tgids = bf + 5;
47                         while (*tgids && isspace(*tgids))
48                                 ++tgids;
49                         tgid = ev.comm.pid = atoi(tgids);
50                 }
51         }
52
53         ev.comm.header.type = PERF_RECORD_COMM;
54         size = ALIGN(size, sizeof(u64));
55         ev.comm.header.size = sizeof(ev.comm) - (sizeof(ev.comm.comm) - size);
56
57         if (!full) {
58                 ev.comm.tid = pid;
59
60                 process(&ev, session);
61                 goto out_fclose;
62         }
63
64         snprintf(filename, sizeof(filename), "/proc/%d/task", pid);
65
66         tasks = opendir(filename);
67         if (tasks == NULL)
68                 goto out_race;
69
70         while (!readdir_r(tasks, &dirent, &next) && next) {
71                 char *end;
72                 pid = strtol(dirent.d_name, &end, 10);
73                 if (*end)
74                         continue;
75
76                 ev.comm.tid = pid;
77
78                 process(&ev, session);
79         }
80         closedir(tasks);
81
82 out_fclose:
83         fclose(fp);
84         return tgid;
85
86 out_failure:
87         pr_warning("couldn't get COMM and pgid, malformed %s\n", filename);
88         return -1;
89 }
90
91 static int event__synthesize_mmap_events(pid_t pid, pid_t tgid,
92                                          int (*process)(event_t *event,
93                                                         struct perf_session *session),
94                                          struct perf_session *session)
95 {
96         char filename[PATH_MAX];
97         FILE *fp;
98
99         snprintf(filename, sizeof(filename), "/proc/%d/maps", pid);
100
101         fp = fopen(filename, "r");
102         if (fp == NULL) {
103                 /*
104                  * We raced with a task exiting - just return:
105                  */
106                 pr_debug("couldn't open %s\n", filename);
107                 return -1;
108         }
109
110         while (1) {
111                 char bf[BUFSIZ], *pbf = bf;
112                 event_t ev = {
113                         .header = { .type = PERF_RECORD_MMAP },
114                 };
115                 int n;
116                 size_t size;
117                 if (fgets(bf, sizeof(bf), fp) == NULL)
118                         break;
119
120                 /* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
121                 n = hex2u64(pbf, &ev.mmap.start);
122                 if (n < 0)
123                         continue;
124                 pbf += n + 1;
125                 n = hex2u64(pbf, &ev.mmap.len);
126                 if (n < 0)
127                         continue;
128                 pbf += n + 3;
129                 if (*pbf == 'x') { /* vm_exec */
130                         char *execname = strchr(bf, '/');
131
132                         /* Catch VDSO */
133                         if (execname == NULL)
134                                 execname = strstr(bf, "[vdso]");
135
136                         if (execname == NULL)
137                                 continue;
138
139                         size = strlen(execname);
140                         execname[size - 1] = '\0'; /* Remove \n */
141                         memcpy(ev.mmap.filename, execname, size);
142                         size = ALIGN(size, sizeof(u64));
143                         ev.mmap.len -= ev.mmap.start;
144                         ev.mmap.header.size = (sizeof(ev.mmap) -
145                                                (sizeof(ev.mmap.filename) - size));
146                         ev.mmap.pid = tgid;
147                         ev.mmap.tid = pid;
148
149                         process(&ev, session);
150                 }
151         }
152
153         fclose(fp);
154         return 0;
155 }
156
157 int event__synthesize_thread(pid_t pid,
158                              int (*process)(event_t *event,
159                                             struct perf_session *session),
160                              struct perf_session *session)
161 {
162         pid_t tgid = event__synthesize_comm(pid, 1, process, session);
163         if (tgid == -1)
164                 return -1;
165         return event__synthesize_mmap_events(pid, tgid, process, session);
166 }
167
168 void event__synthesize_threads(int (*process)(event_t *event,
169                                               struct perf_session *session),
170                                struct perf_session *session)
171 {
172         DIR *proc;
173         struct dirent dirent, *next;
174
175         proc = opendir("/proc");
176
177         while (!readdir_r(proc, &dirent, &next) && next) {
178                 char *end;
179                 pid_t pid = strtol(dirent.d_name, &end, 10);
180
181                 if (*end) /* only interested in proper numerical dirents */
182                         continue;
183
184                 event__synthesize_thread(pid, process, session);
185         }
186
187         closedir(proc);
188 }
189
190 struct events_stats event__stats;
191
192 int event__process_comm(event_t *self, struct perf_session *session)
193 {
194         struct thread *thread = perf_session__findnew(session, self->comm.pid);
195
196         dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
197
198         if (thread == NULL || thread__set_comm(thread, self->comm.comm)) {
199                 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
200                 return -1;
201         }
202
203         return 0;
204 }
205
206 int event__process_lost(event_t *self, struct perf_session *session __used)
207 {
208         dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
209         event__stats.lost += self->lost.lost;
210         return 0;
211 }
212
213 int event__process_mmap(event_t *self, struct perf_session *session)
214 {
215         struct thread *thread = perf_session__findnew(session, self->mmap.pid);
216         struct map *map = map__new(&self->mmap, MAP__FUNCTION,
217                                    session->cwd, session->cwdlen);
218
219         dump_printf(" %d/%d: [%p(%p) @ %p]: %s\n",
220                     self->mmap.pid, self->mmap.tid,
221                     (void *)(long)self->mmap.start,
222                     (void *)(long)self->mmap.len,
223                     (void *)(long)self->mmap.pgoff,
224                     self->mmap.filename);
225
226         if (thread == NULL || map == NULL)
227                 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
228         else
229                 thread__insert_map(thread, map);
230
231         return 0;
232 }
233
234 int event__process_task(event_t *self, struct perf_session *session)
235 {
236         struct thread *thread = perf_session__findnew(session, self->fork.pid);
237         struct thread *parent = perf_session__findnew(session, self->fork.ppid);
238
239         dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
240                     self->fork.ppid, self->fork.ptid);
241         /*
242          * A thread clone will have the same PID for both parent and child.
243          */
244         if (thread == parent)
245                 return 0;
246
247         if (self->header.type == PERF_RECORD_EXIT)
248                 return 0;
249
250         if (thread == NULL || parent == NULL ||
251             thread__fork(thread, parent) < 0) {
252                 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
253                 return -1;
254         }
255
256         return 0;
257 }
258
259 void thread__find_addr_location(struct thread *self,
260                                 struct perf_session *session, u8 cpumode,
261                                 enum map_type type, u64 addr,
262                                 struct addr_location *al,
263                                 symbol_filter_t filter)
264 {
265         struct map_groups *mg = &self->mg;
266
267         al->thread = self;
268         al->addr = addr;
269
270         if (cpumode & PERF_RECORD_MISC_KERNEL) {
271                 al->level = 'k';
272                 mg = &session->kmaps;
273         } else if (cpumode & PERF_RECORD_MISC_USER)
274                 al->level = '.';
275         else {
276                 al->level = 'H';
277                 al->map = NULL;
278                 al->sym = NULL;
279                 return;
280         }
281 try_again:
282         al->map = map_groups__find(mg, type, al->addr);
283         if (al->map == NULL) {
284                 /*
285                  * If this is outside of all known maps, and is a negative
286                  * address, try to look it up in the kernel dso, as it might be
287                  * a vsyscall or vdso (which executes in user-mode).
288                  *
289                  * XXX This is nasty, we should have a symbol list in the
290                  * "[vdso]" dso, but for now lets use the old trick of looking
291                  * in the whole kernel symbol list.
292                  */
293                 if ((long long)al->addr < 0 && mg != &session->kmaps) {
294                         mg = &session->kmaps;
295                         goto try_again;
296                 }
297                 al->sym = NULL;
298         } else {
299                 al->addr = al->map->map_ip(al->map, al->addr);
300                 al->sym = map__find_symbol(al->map, session, al->addr, filter);
301         }
302 }
303
304 int event__preprocess_sample(const event_t *self, struct perf_session *session,
305                              struct addr_location *al, symbol_filter_t filter)
306 {
307         u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
308         struct thread *thread = perf_session__findnew(session, self->ip.pid);
309
310         if (thread == NULL)
311                 return -1;
312
313         dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
314
315         thread__find_addr_location(thread, session, cpumode, MAP__FUNCTION,
316                                    self->ip.ip, al, filter);
317         dump_printf(" ...... dso: %s\n",
318                     al->map ? al->map->dso->long_name :
319                         al->level == 'H' ? "[hypervisor]" : "<not found>");
320         return 0;
321 }
322
323 int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
324 {
325         u64 *array = event->sample.array;
326
327         if (type & PERF_SAMPLE_IP) {
328                 data->ip = event->ip.ip;
329                 array++;
330         }
331
332         if (type & PERF_SAMPLE_TID) {
333                 u32 *p = (u32 *)array;
334                 data->pid = p[0];
335                 data->tid = p[1];
336                 array++;
337         }
338
339         if (type & PERF_SAMPLE_TIME) {
340                 data->time = *array;
341                 array++;
342         }
343
344         if (type & PERF_SAMPLE_ADDR) {
345                 data->addr = *array;
346                 array++;
347         }
348
349         if (type & PERF_SAMPLE_ID) {
350                 data->id = *array;
351                 array++;
352         }
353
354         if (type & PERF_SAMPLE_STREAM_ID) {
355                 data->stream_id = *array;
356                 array++;
357         }
358
359         if (type & PERF_SAMPLE_CPU) {
360                 u32 *p = (u32 *)array;
361                 data->cpu = *p;
362                 array++;
363         }
364
365         if (type & PERF_SAMPLE_PERIOD) {
366                 data->period = *array;
367                 array++;
368         }
369
370         if (type & PERF_SAMPLE_READ) {
371                 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
372                 return -1;
373         }
374
375         if (type & PERF_SAMPLE_CALLCHAIN) {
376                 data->callchain = (struct ip_callchain *)array;
377                 array += 1 + data->callchain->nr;
378         }
379
380         if (type & PERF_SAMPLE_RAW) {
381                 u32 *p = (u32 *)array;
382                 data->raw_size = *p;
383                 p++;
384                 data->raw_data = p;
385         }
386
387         return 0;
388 }