425a29ba01a97ee178a2068877baa4797b1fdb72
[linux-2.6.git] / tools / perf / util / header.c
1 #include <sys/types.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <linux/list.h>
6
7 #include "util.h"
8 #include "header.h"
9 #include "../perf.h"
10 #include "trace-event.h"
11 #include "symbol.h"
12 #include "data_map.h"
13 #include "debug.h"
14
15 /*
16  * Create new perf.data header attribute:
17  */
18 struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
19 {
20         struct perf_header_attr *self = malloc(sizeof(*self));
21
22         if (self != NULL) {
23                 self->attr = *attr;
24                 self->ids  = 0;
25                 self->size = 1;
26                 self->id   = malloc(sizeof(u64));
27                 if (self->id == NULL) {
28                         free(self);
29                         self = NULL;
30                 }
31         }
32
33         return self;
34 }
35
36 void perf_header_attr__delete(struct perf_header_attr *self)
37 {
38         free(self->id);
39         free(self);
40 }
41
42 int perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
43 {
44         int pos = self->ids;
45
46         self->ids++;
47         if (self->ids > self->size) {
48                 int nsize = self->size * 2;
49                 u64 *nid = realloc(self->id, nsize * sizeof(u64));
50
51                 if (nid == NULL)
52                         return -1;
53
54                 self->size = nsize;
55                 self->id = nid;
56         }
57         self->id[pos] = id;
58         return 0;
59 }
60
61 /*
62  * Create new perf.data header:
63  */
64 struct perf_header *perf_header__new(void)
65 {
66         struct perf_header *self = calloc(sizeof(*self), 1);
67
68         if (self != NULL) {
69                 self->size = 1;
70                 self->attr = malloc(sizeof(void *));
71
72                 if (self->attr == NULL) {
73                         free(self);
74                         self = NULL;
75                 }
76         }
77
78         return self;
79 }
80
81 int perf_header__add_attr(struct perf_header *self,
82                           struct perf_header_attr *attr)
83 {
84         int pos = self->attrs;
85
86         if (self->frozen)
87                 return -1;
88
89         self->attrs++;
90         if (self->attrs > self->size) {
91                 int nsize = self->size * 2;
92                 struct perf_header_attr **nattr;
93
94                 nattr = realloc(self->attr, nsize * sizeof(void *));
95                 if (nattr == NULL)
96                         return -1;
97
98                 self->size = nsize;
99                 self->attr = nattr;
100         }
101         self->attr[pos] = attr;
102         return 0;
103 }
104
105 #define MAX_EVENT_NAME 64
106
107 struct perf_trace_event_type {
108         u64     event_id;
109         char    name[MAX_EVENT_NAME];
110 };
111
112 static int event_count;
113 static struct perf_trace_event_type *events;
114
115 void perf_header__push_event(u64 id, const char *name)
116 {
117         if (strlen(name) > MAX_EVENT_NAME)
118                 pr_warning("Event %s will be truncated\n", name);
119
120         if (!events) {
121                 events = malloc(sizeof(struct perf_trace_event_type));
122                 if (!events)
123                         die("nomem");
124         } else {
125                 events = realloc(events, (event_count + 1) * sizeof(struct perf_trace_event_type));
126                 if (!events)
127                         die("nomem");
128         }
129         memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
130         events[event_count].event_id = id;
131         strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
132         event_count++;
133 }
134
135 char *perf_header__find_event(u64 id)
136 {
137         int i;
138         for (i = 0 ; i < event_count; i++) {
139                 if (events[i].event_id == id)
140                         return events[i].name;
141         }
142         return NULL;
143 }
144
145 static const char *__perf_magic = "PERFFILE";
146
147 #define PERF_MAGIC      (*(u64 *)__perf_magic)
148
149 struct perf_file_attr {
150         struct perf_event_attr  attr;
151         struct perf_file_section        ids;
152 };
153
154 void perf_header__set_feat(struct perf_header *self, int feat)
155 {
156         set_bit(feat, self->adds_features);
157 }
158
159 bool perf_header__has_feat(const struct perf_header *self, int feat)
160 {
161         return test_bit(feat, self->adds_features);
162 }
163
164 static int do_write(int fd, const void *buf, size_t size)
165 {
166         while (size) {
167                 int ret = write(fd, buf, size);
168
169                 if (ret < 0)
170                         return -1;
171
172                 size -= ret;
173                 buf += ret;
174         }
175
176         return 0;
177 }
178
179 static int dsos__write_buildid_table(int fd)
180 {
181         struct dso *pos;
182
183         list_for_each_entry(pos, &dsos, node) {
184                 struct build_id_event b;
185                 size_t len;
186
187                 if (!pos->has_build_id)
188                         continue;
189                 len = pos->long_name_len + 1;
190                 len = ALIGN(len, 64);
191                 memset(&b, 0, sizeof(b));
192                 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
193                 b.header.size = sizeof(b) + len;
194                 if (do_write(fd, &b, sizeof(b)) < 0 ||
195                     do_write(fd, pos->long_name, len) < 0)
196                         return -1;
197         }
198
199         return 0;
200 }
201
202 static void
203 perf_header__adds_write(struct perf_header *self, int fd)
204 {
205         int nr_sections;
206         struct perf_file_section *feat_sec;
207         int sec_size;
208         u64 sec_start;
209         int idx = 0;
210
211         if (dsos__read_build_ids())
212                 perf_header__set_feat(self, HEADER_BUILD_ID);
213
214         nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
215         if (!nr_sections)
216                 return;
217
218         feat_sec = calloc(sizeof(*feat_sec), nr_sections);
219         if (!feat_sec)
220                 die("No memory");
221
222         sec_size = sizeof(*feat_sec) * nr_sections;
223
224         sec_start = self->data_offset + self->data_size;
225         lseek(fd, sec_start + sec_size, SEEK_SET);
226
227         if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
228                 struct perf_file_section *trace_sec;
229
230                 trace_sec = &feat_sec[idx++];
231
232                 /* Write trace info */
233                 trace_sec->offset = lseek(fd, 0, SEEK_CUR);
234                 read_tracing_data(fd, attrs, nr_counters);
235                 trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
236         }
237
238
239         if (perf_header__has_feat(self, HEADER_BUILD_ID)) {
240                 struct perf_file_section *buildid_sec;
241
242                 buildid_sec = &feat_sec[idx++];
243
244                 dsos__load_kernel();
245                 /*
246                  * Read the list of loaded modules with its build_ids
247                  */
248                 dsos__load_modules();
249
250                 /* Write build-ids */
251                 buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
252                 if (dsos__write_buildid_table(fd) < 0)
253                         die("failed to write buildid table");
254                 buildid_sec->size = lseek(fd, 0, SEEK_CUR) - buildid_sec->offset;
255         }
256
257         lseek(fd, sec_start, SEEK_SET);
258         if (do_write(fd, feat_sec, sec_size) < 0)
259                 die("failed to write feature section");
260         free(feat_sec);
261 }
262
263 void perf_header__write(struct perf_header *self, int fd, bool at_exit)
264 {
265         struct perf_file_header f_header;
266         struct perf_file_attr   f_attr;
267         struct perf_header_attr *attr;
268         int i;
269
270         lseek(fd, sizeof(f_header), SEEK_SET);
271
272
273         for (i = 0; i < self->attrs; i++) {
274                 attr = self->attr[i];
275
276                 attr->id_offset = lseek(fd, 0, SEEK_CUR);
277                 if (do_write(fd, attr->id, attr->ids * sizeof(u64)) < 0)
278                         die("failed to write perf header");
279         }
280
281
282         self->attr_offset = lseek(fd, 0, SEEK_CUR);
283
284         for (i = 0; i < self->attrs; i++) {
285                 attr = self->attr[i];
286
287                 f_attr = (struct perf_file_attr){
288                         .attr = attr->attr,
289                         .ids  = {
290                                 .offset = attr->id_offset,
291                                 .size   = attr->ids * sizeof(u64),
292                         }
293                 };
294                 if (do_write(fd, &f_attr, sizeof(f_attr)) < 0)
295                         die("failed to write perf header attribute");
296         }
297
298         self->event_offset = lseek(fd, 0, SEEK_CUR);
299         self->event_size = event_count * sizeof(struct perf_trace_event_type);
300         if (events)
301                 if (do_write(fd, events, self->event_size) < 0)
302                         die("failed to write perf header events");
303
304         self->data_offset = lseek(fd, 0, SEEK_CUR);
305
306         if (at_exit)
307                 perf_header__adds_write(self, fd);
308
309         f_header = (struct perf_file_header){
310                 .magic     = PERF_MAGIC,
311                 .size      = sizeof(f_header),
312                 .attr_size = sizeof(f_attr),
313                 .attrs = {
314                         .offset = self->attr_offset,
315                         .size   = self->attrs * sizeof(f_attr),
316                 },
317                 .data = {
318                         .offset = self->data_offset,
319                         .size   = self->data_size,
320                 },
321                 .event_types = {
322                         .offset = self->event_offset,
323                         .size   = self->event_size,
324                 },
325         };
326
327         memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
328
329         lseek(fd, 0, SEEK_SET);
330         if (do_write(fd, &f_header, sizeof(f_header)) < 0)
331                 die("failed to write perf header");
332         lseek(fd, self->data_offset + self->data_size, SEEK_SET);
333
334         self->frozen = 1;
335 }
336
337 static void do_read(int fd, void *buf, size_t size)
338 {
339         while (size) {
340                 int ret = read(fd, buf, size);
341
342                 if (ret < 0)
343                         die("failed to read");
344                 if (ret == 0)
345                         die("failed to read: missing data");
346
347                 size -= ret;
348                 buf += ret;
349         }
350 }
351
352 int perf_header__process_sections(struct perf_header *self, int fd,
353                                   int (*process)(struct perf_file_section *self,
354                                                  int feat, int fd))
355 {
356         struct perf_file_section *feat_sec;
357         int nr_sections;
358         int sec_size;
359         int idx = 0;
360         int err = 0, feat = 1;
361
362         nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
363         if (!nr_sections)
364                 return 0;
365
366         feat_sec = calloc(sizeof(*feat_sec), nr_sections);
367         if (!feat_sec)
368                 return -1;
369
370         sec_size = sizeof(*feat_sec) * nr_sections;
371
372         lseek(fd, self->data_offset + self->data_size, SEEK_SET);
373
374         do_read(fd, feat_sec, sec_size);
375
376         while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
377                 if (perf_header__has_feat(self, feat)) {
378                         struct perf_file_section *sec = &feat_sec[idx++];
379
380                         err = process(sec, feat, fd);
381                         if (err < 0)
382                                 break;
383                 }
384                 ++feat;
385         }
386
387         free(feat_sec);
388         return err;
389 };
390
391 int perf_file_header__read(struct perf_file_header *self,
392                            struct perf_header *ph, int fd)
393 {
394         lseek(fd, 0, SEEK_SET);
395         do_read(fd, self, sizeof(*self));
396
397         if (self->magic     != PERF_MAGIC ||
398             self->attr_size != sizeof(struct perf_file_attr))
399                 return -1;
400
401         if (self->size != sizeof(*self)) {
402                 /* Support the previous format */
403                 if (self->size == offsetof(typeof(*self), adds_features))
404                         bitmap_zero(self->adds_features, HEADER_FEAT_BITS);
405                 else
406                         return -1;
407         }
408
409         memcpy(&ph->adds_features, &self->adds_features,
410                sizeof(self->adds_features));
411
412         ph->event_offset = self->event_types.offset;
413         ph->event_size   = self->event_types.size;
414         ph->data_offset  = self->data.offset;
415         ph->data_size    = self->data.size;
416         return 0;
417 }
418
419 static int perf_file_section__process(struct perf_file_section *self,
420                                       int feat, int fd)
421 {
422         if (lseek(fd, self->offset, SEEK_SET) < 0) {
423                 pr_debug("Failed to lseek to %Ld offset for feature %d, "
424                          "continuing...\n", self->offset, feat);
425                 return 0;
426         }
427
428         switch (feat) {
429         case HEADER_TRACE_INFO:
430                 trace_report(fd);
431                 break;
432
433         case HEADER_BUILD_ID:
434                 if (perf_header__read_build_ids(fd, self->offset, self->size))
435                         pr_debug("Failed to read buildids, continuing...\n");
436                 break;
437         default:
438                 pr_debug("unknown feature %d, continuing...\n", feat);
439         }
440
441         return 0;
442 }
443
444 struct perf_header *perf_header__read(int fd)
445 {
446         struct perf_header      *self = perf_header__new();
447         struct perf_file_header f_header;
448         struct perf_file_attr   f_attr;
449         u64                     f_id;
450         int nr_attrs, nr_ids, i, j;
451
452         if (self == NULL)
453                 die("nomem");
454
455         if (perf_file_header__read(&f_header, self, fd) < 0)
456                 die("incompatible file format");
457
458         nr_attrs = f_header.attrs.size / sizeof(f_attr);
459         lseek(fd, f_header.attrs.offset, SEEK_SET);
460
461         for (i = 0; i < nr_attrs; i++) {
462                 struct perf_header_attr *attr;
463                 off_t tmp;
464
465                 do_read(fd, &f_attr, sizeof(f_attr));
466                 tmp = lseek(fd, 0, SEEK_CUR);
467
468                 attr = perf_header_attr__new(&f_attr.attr);
469                 if (attr == NULL)
470                          die("nomem");
471
472                 nr_ids = f_attr.ids.size / sizeof(u64);
473                 lseek(fd, f_attr.ids.offset, SEEK_SET);
474
475                 for (j = 0; j < nr_ids; j++) {
476                         do_read(fd, &f_id, sizeof(f_id));
477
478                         if (perf_header_attr__add_id(attr, f_id) < 0)
479                                 die("nomem");
480                 }
481                 if (perf_header__add_attr(self, attr) < 0)
482                          die("nomem");
483
484                 lseek(fd, tmp, SEEK_SET);
485         }
486
487         if (f_header.event_types.size) {
488                 lseek(fd, f_header.event_types.offset, SEEK_SET);
489                 events = malloc(f_header.event_types.size);
490                 if (!events)
491                         die("nomem");
492                 do_read(fd, events, f_header.event_types.size);
493                 event_count =  f_header.event_types.size / sizeof(struct perf_trace_event_type);
494         }
495
496         perf_header__process_sections(self, fd, perf_file_section__process);
497
498         lseek(fd, self->data_offset, SEEK_SET);
499
500         self->frozen = 1;
501
502         return self;
503 }
504
505 u64 perf_header__sample_type(struct perf_header *header)
506 {
507         u64 type = 0;
508         int i;
509
510         for (i = 0; i < header->attrs; i++) {
511                 struct perf_header_attr *attr = header->attr[i];
512
513                 if (!type)
514                         type = attr->attr.sample_type;
515                 else if (type != attr->attr.sample_type)
516                         die("non matching sample_type");
517         }
518
519         return type;
520 }
521
522 struct perf_event_attr *
523 perf_header__find_attr(u64 id, struct perf_header *header)
524 {
525         int i;
526
527         for (i = 0; i < header->attrs; i++) {
528                 struct perf_header_attr *attr = header->attr[i];
529                 int j;
530
531                 for (j = 0; j < attr->ids; j++) {
532                         if (attr->id[j] == id)
533                                 return &attr->attr;
534                 }
535         }
536
537         return NULL;
538 }