ba5bcfa1f9087b9129e640e0343838f918d75130
[linux-3.10.git] / tools / perf / util / data_map.c
1 #include "symbol.h"
2 #include "util.h"
3 #include "debug.h"
4 #include "session.h"
5
6 static unsigned long    mmap_window = 32;
7 static char             __cwd[PATH_MAX];
8
9 static int process_event_stub(event_t *event __used,
10                               struct perf_session *session __used)
11 {
12         dump_printf(": unhandled!\n");
13         return 0;
14 }
15
16 static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
17 {
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;
36 }
37
38 static const char *event__name[] = {
39         [0]                      = "TOTAL",
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",
49 };
50
51 unsigned long event__total[PERF_RECORD_MAX];
52
53 void event__print_totals(void)
54 {
55         int i;
56         for (i = 0; i < PERF_RECORD_MAX; ++i)
57                 pr_info("%10s events: %10ld\n",
58                         event__name[i], event__total[i]);
59 }
60
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)
64 {
65         trace_event(event);
66
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]);
72                 ++event__total[0];
73                 ++event__total[event->header.type];
74         }
75
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);
95         default:
96                 ops->total_unknown++;
97                 return -1;
98         }
99 }
100
101 int perf_header__read_build_ids(int input, u64 offset, u64 size)
102 {
103         struct build_id_event bev;
104         char filename[PATH_MAX];
105         u64 limit = offset + size;
106         int err = -1;
107
108         while (offset < limit) {
109                 struct dso *dso;
110                 ssize_t len;
111
112                 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
113                         goto out;
114
115                 len = bev.header.size - sizeof(bev);
116                 if (read(input, filename, len) != len)
117                         goto out;
118
119                 dso = dsos__findnew(filename);
120                 if (dso != NULL)
121                         dso__set_build_id(dso, &bev.build_id);
122
123                 offset += bev.header.size;
124         }
125         err = 0;
126 out:
127         return err;
128 }
129
130 int perf_session__process_events(struct perf_session *self,
131                                  struct perf_event_ops *ops,
132                                  int full_paths, int *cwdlen, char **cwd)
133 {
134         int err;
135         unsigned long head, shift;
136         unsigned long offset = 0;
137         size_t  page_size;
138         u64 sample_type;
139         event_t *event;
140         uint32_t size;
141         char *buf;
142
143         perf_event_ops__fill_defaults(ops);
144
145         page_size = getpagesize();
146
147         head = self->header.data_offset;
148         sample_type = perf_header__sample_type(&self->header);
149
150         err = -EINVAL;
151         if (ops->sample_type_check &&
152             ops->sample_type_check(sample_type) < 0)
153                 goto out_err;
154
155         if (!full_paths) {
156                 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
157                         pr_err("failed to get the current directory\n");
158                         err = -errno;
159                         goto out_err;
160                 }
161                 *cwd = __cwd;
162                 *cwdlen = strlen(*cwd);
163         } else {
164                 *cwd = NULL;
165                 *cwdlen = 0;
166         }
167
168         shift = page_size * (head / page_size);
169         offset += shift;
170         head -= shift;
171
172 remap:
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");
177                 err = -errno;
178                 goto out_err;
179         }
180
181 more:
182         event = (event_t *)(buf + head);
183
184         size = event->header.size;
185         if (!size)
186                 size = 8;
187
188         if (head + event->header.size >= page_size * mmap_window) {
189                 int munmap_ret;
190
191                 shift = page_size * (head / page_size);
192
193                 munmap_ret = munmap(buf, page_size * mmap_window);
194                 assert(munmap_ret == 0);
195
196                 offset += shift;
197                 head -= shift;
198                 goto remap;
199         }
200
201         size = event->header.size;
202
203         dump_printf("\n%p [%p]: event: %d\n",
204                         (void *)(offset + head),
205                         (void *)(long)event->header.size,
206                         event->header.type);
207
208         if (!size || process_event(event, self, ops, offset, head) < 0) {
209
210                 dump_printf("%p [%p]: skipping unknown header type: %d\n",
211                         (void *)(offset + head),
212                         (void *)(long)(event->header.size),
213                         event->header.type);
214
215                 /*
216                  * assume we lost track of the stream, check alignment, and
217                  * increment a single u64 in the hope to catch on again 'soon'.
218                  */
219
220                 if (unlikely(head & 7))
221                         head &= ~7ULL;
222
223                 size = 8;
224         }
225
226         head += size;
227
228         if (offset + head >= self->header.data_offset + self->header.data_size)
229                 goto done;
230
231         if (offset + head < self->size)
232                 goto more;
233
234 done:
235         err = 0;
236 out_err:
237         return err;
238 }