perf session: Event statistics also are per session
[linux-2.6.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 int event__process_comm(event_t *self, struct perf_session *session)
191 {
192         struct thread *thread = perf_session__findnew(session, self->comm.pid);
193
194         dump_printf(": %s:%d\n", self->comm.comm, self->comm.pid);
195
196         if (thread == NULL || thread__set_comm(thread, self->comm.comm)) {
197                 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
198                 return -1;
199         }
200
201         return 0;
202 }
203
204 int event__process_lost(event_t *self, struct perf_session *session)
205 {
206         dump_printf(": id:%Ld: lost:%Ld\n", self->lost.id, self->lost.lost);
207         session->events_stats.lost += self->lost.lost;
208         return 0;
209 }
210
211 int event__process_mmap(event_t *self, struct perf_session *session)
212 {
213         struct thread *thread = perf_session__findnew(session, self->mmap.pid);
214         struct map *map = map__new(&self->mmap, MAP__FUNCTION,
215                                    session->cwd, session->cwdlen);
216
217         dump_printf(" %d/%d: [%p(%p) @ %p]: %s\n",
218                     self->mmap.pid, self->mmap.tid,
219                     (void *)(long)self->mmap.start,
220                     (void *)(long)self->mmap.len,
221                     (void *)(long)self->mmap.pgoff,
222                     self->mmap.filename);
223
224         if (thread == NULL || map == NULL)
225                 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
226         else
227                 thread__insert_map(thread, map);
228
229         return 0;
230 }
231
232 int event__process_task(event_t *self, struct perf_session *session)
233 {
234         struct thread *thread = perf_session__findnew(session, self->fork.pid);
235         struct thread *parent = perf_session__findnew(session, self->fork.ppid);
236
237         dump_printf("(%d:%d):(%d:%d)\n", self->fork.pid, self->fork.tid,
238                     self->fork.ppid, self->fork.ptid);
239         /*
240          * A thread clone will have the same PID for both parent and child.
241          */
242         if (thread == parent)
243                 return 0;
244
245         if (self->header.type == PERF_RECORD_EXIT)
246                 return 0;
247
248         if (thread == NULL || parent == NULL ||
249             thread__fork(thread, parent) < 0) {
250                 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
251                 return -1;
252         }
253
254         return 0;
255 }
256
257 void thread__find_addr_location(struct thread *self,
258                                 struct perf_session *session, u8 cpumode,
259                                 enum map_type type, u64 addr,
260                                 struct addr_location *al,
261                                 symbol_filter_t filter)
262 {
263         struct map_groups *mg = &self->mg;
264
265         al->thread = self;
266         al->addr = addr;
267
268         if (cpumode & PERF_RECORD_MISC_KERNEL) {
269                 al->level = 'k';
270                 mg = &session->kmaps;
271         } else if (cpumode & PERF_RECORD_MISC_USER)
272                 al->level = '.';
273         else {
274                 al->level = 'H';
275                 al->map = NULL;
276                 al->sym = NULL;
277                 return;
278         }
279 try_again:
280         al->map = map_groups__find(mg, type, al->addr);
281         if (al->map == NULL) {
282                 /*
283                  * If this is outside of all known maps, and is a negative
284                  * address, try to look it up in the kernel dso, as it might be
285                  * a vsyscall or vdso (which executes in user-mode).
286                  *
287                  * XXX This is nasty, we should have a symbol list in the
288                  * "[vdso]" dso, but for now lets use the old trick of looking
289                  * in the whole kernel symbol list.
290                  */
291                 if ((long long)al->addr < 0 && mg != &session->kmaps) {
292                         mg = &session->kmaps;
293                         goto try_again;
294                 }
295                 al->sym = NULL;
296         } else {
297                 al->addr = al->map->map_ip(al->map, al->addr);
298                 al->sym = map__find_symbol(al->map, session, al->addr, filter);
299         }
300 }
301
302 int event__preprocess_sample(const event_t *self, struct perf_session *session,
303                              struct addr_location *al, symbol_filter_t filter)
304 {
305         u8 cpumode = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
306         struct thread *thread = perf_session__findnew(session, self->ip.pid);
307
308         if (thread == NULL)
309                 return -1;
310
311         dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
312
313         thread__find_addr_location(thread, session, cpumode, MAP__FUNCTION,
314                                    self->ip.ip, al, filter);
315         dump_printf(" ...... dso: %s\n",
316                     al->map ? al->map->dso->long_name :
317                         al->level == 'H' ? "[hypervisor]" : "<not found>");
318         return 0;
319 }
320
321 int event__parse_sample(event_t *event, u64 type, struct sample_data *data)
322 {
323         u64 *array = event->sample.array;
324
325         if (type & PERF_SAMPLE_IP) {
326                 data->ip = event->ip.ip;
327                 array++;
328         }
329
330         if (type & PERF_SAMPLE_TID) {
331                 u32 *p = (u32 *)array;
332                 data->pid = p[0];
333                 data->tid = p[1];
334                 array++;
335         }
336
337         if (type & PERF_SAMPLE_TIME) {
338                 data->time = *array;
339                 array++;
340         }
341
342         if (type & PERF_SAMPLE_ADDR) {
343                 data->addr = *array;
344                 array++;
345         }
346
347         if (type & PERF_SAMPLE_ID) {
348                 data->id = *array;
349                 array++;
350         }
351
352         if (type & PERF_SAMPLE_STREAM_ID) {
353                 data->stream_id = *array;
354                 array++;
355         }
356
357         if (type & PERF_SAMPLE_CPU) {
358                 u32 *p = (u32 *)array;
359                 data->cpu = *p;
360                 array++;
361         }
362
363         if (type & PERF_SAMPLE_PERIOD) {
364                 data->period = *array;
365                 array++;
366         }
367
368         if (type & PERF_SAMPLE_READ) {
369                 pr_debug("PERF_SAMPLE_READ is unsuported for now\n");
370                 return -1;
371         }
372
373         if (type & PERF_SAMPLE_CALLCHAIN) {
374                 data->callchain = (struct ip_callchain *)array;
375                 array += 1 + data->callchain->nr;
376         }
377
378         if (type & PERF_SAMPLE_RAW) {
379                 u32 *p = (u32 *)array;
380                 data->raw_size = *p;
381                 p++;
382                 data->raw_data = p;
383         }
384
385         return 0;
386 }