blob: 9aae360c0f284762e04e39e6ab8b144d577597fc [file] [log] [blame]
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001#include <sys/types.h>
2#include <unistd.h>
3#include <stdio.h>
4#include <stdlib.h>
5
6#include "util.h"
7#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +02008#include "../perf.h"
9#include "trace-event.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020010
11/*
Arjan van de Venec60a3f2009-09-19 13:36:30 +020012 * Create new perf.data header attribute:
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020013 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020014struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020015{
16 struct perf_header_attr *self = malloc(sizeof(*self));
17
18 if (!self)
19 die("nomem");
20
21 self->attr = *attr;
22 self->ids = 0;
23 self->size = 1;
24 self->id = malloc(sizeof(u64));
25
26 if (!self->id)
27 die("nomem");
28
29 return self;
30}
31
32void perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
33{
34 int pos = self->ids;
35
36 self->ids++;
37 if (self->ids > self->size) {
38 self->size *= 2;
39 self->id = realloc(self->id, self->size * sizeof(u64));
40 if (!self->id)
41 die("nomem");
42 }
43 self->id[pos] = id;
44}
45
46/*
Arjan van de Venec60a3f2009-09-19 13:36:30 +020047 * Create new perf.data header:
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020048 */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020049struct perf_header *perf_header__new(void)
50{
51 struct perf_header *self = malloc(sizeof(*self));
52
53 if (!self)
54 die("nomem");
55
56 self->frozen = 0;
57
58 self->attrs = 0;
59 self->size = 1;
60 self->attr = malloc(sizeof(void *));
61
62 if (!self->attr)
63 die("nomem");
64
65 self->data_offset = 0;
66 self->data_size = 0;
Frederic Weisbecker03456a12009-10-06 23:36:47 +020067 self->trace_info_offset = 0;
68 self->trace_info_size = 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020069
70 return self;
71}
72
73void perf_header__add_attr(struct perf_header *self,
74 struct perf_header_attr *attr)
75{
76 int pos = self->attrs;
77
78 if (self->frozen)
79 die("frozen");
80
81 self->attrs++;
82 if (self->attrs > self->size) {
83 self->size *= 2;
84 self->attr = realloc(self->attr, self->size * sizeof(void *));
85 if (!self->attr)
86 die("nomem");
87 }
88 self->attr[pos] = attr;
89}
90
Arjan van de Venec60a3f2009-09-19 13:36:30 +020091#define MAX_EVENT_NAME 64
92
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020093struct perf_trace_event_type {
94 u64 event_id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +020095 char name[MAX_EVENT_NAME];
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020096};
97
98static int event_count;
99static struct perf_trace_event_type *events;
100
101void perf_header__push_event(u64 id, const char *name)
102{
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200103 if (strlen(name) > MAX_EVENT_NAME)
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200104 printf("Event %s will be truncated\n", name);
105
106 if (!events) {
107 events = malloc(sizeof(struct perf_trace_event_type));
108 if (!events)
109 die("nomem");
110 } else {
111 events = realloc(events, (event_count + 1) * sizeof(struct perf_trace_event_type));
112 if (!events)
113 die("nomem");
114 }
115 memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
116 events[event_count].event_id = id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200117 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200118 event_count++;
119}
120
121char *perf_header__find_event(u64 id)
122{
123 int i;
124 for (i = 0 ; i < event_count; i++) {
125 if (events[i].event_id == id)
126 return events[i].name;
127 }
128 return NULL;
129}
130
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200131static const char *__perf_magic = "PERFFILE";
132
133#define PERF_MAGIC (*(u64 *)__perf_magic)
134
135struct perf_file_section {
136 u64 offset;
137 u64 size;
138};
139
140struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200141 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200142 struct perf_file_section ids;
143};
144
145struct perf_file_header {
146 u64 magic;
147 u64 size;
148 u64 attr_size;
149 struct perf_file_section attrs;
150 struct perf_file_section data;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200151 struct perf_file_section event_types;
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200152 struct perf_file_section trace_info;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200153};
154
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200155static int trace_info;
156
157void perf_header__set_trace_info(void)
158{
159 trace_info = 1;
160}
161
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200162static void do_write(int fd, void *buf, size_t size)
163{
164 while (size) {
165 int ret = write(fd, buf, size);
166
167 if (ret < 0)
168 die("failed to write");
169
170 size -= ret;
171 buf += ret;
172 }
173}
174
175void perf_header__write(struct perf_header *self, int fd)
176{
177 struct perf_file_header f_header;
178 struct perf_file_attr f_attr;
179 struct perf_header_attr *attr;
180 int i;
181
182 lseek(fd, sizeof(f_header), SEEK_SET);
183
184
185 for (i = 0; i < self->attrs; i++) {
186 attr = self->attr[i];
187
188 attr->id_offset = lseek(fd, 0, SEEK_CUR);
189 do_write(fd, attr->id, attr->ids * sizeof(u64));
190 }
191
192
193 self->attr_offset = lseek(fd, 0, SEEK_CUR);
194
195 for (i = 0; i < self->attrs; i++) {
196 attr = self->attr[i];
197
198 f_attr = (struct perf_file_attr){
199 .attr = attr->attr,
200 .ids = {
201 .offset = attr->id_offset,
202 .size = attr->ids * sizeof(u64),
203 }
204 };
205 do_write(fd, &f_attr, sizeof(f_attr));
206 }
207
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200208 self->event_offset = lseek(fd, 0, SEEK_CUR);
209 self->event_size = event_count * sizeof(struct perf_trace_event_type);
210 if (events)
211 do_write(fd, events, self->event_size);
212
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200213 if (trace_info) {
214 static int trace_info_written;
215
216 /*
217 * Write it only once
218 */
219 if (!trace_info_written) {
220 self->trace_info_offset = lseek(fd, 0, SEEK_CUR);
221 read_tracing_data(fd, attrs, nr_counters);
222 self->trace_info_size = lseek(fd, 0, SEEK_CUR) -
223 self->trace_info_offset;
224 trace_info_written = 1;
225 } else {
226 lseek(fd, self->trace_info_offset +
227 self->trace_info_size, SEEK_SET);
228 }
229 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200230
231 self->data_offset = lseek(fd, 0, SEEK_CUR);
232
233 f_header = (struct perf_file_header){
234 .magic = PERF_MAGIC,
235 .size = sizeof(f_header),
236 .attr_size = sizeof(f_attr),
237 .attrs = {
238 .offset = self->attr_offset,
239 .size = self->attrs * sizeof(f_attr),
240 },
241 .data = {
242 .offset = self->data_offset,
243 .size = self->data_size,
244 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200245 .event_types = {
246 .offset = self->event_offset,
247 .size = self->event_size,
248 },
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200249 .trace_info = {
250 .offset = self->trace_info_offset,
251 .size = self->trace_info_size,
252 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200253 };
254
255 lseek(fd, 0, SEEK_SET);
256 do_write(fd, &f_header, sizeof(f_header));
257 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
258
259 self->frozen = 1;
260}
261
262static void do_read(int fd, void *buf, size_t size)
263{
264 while (size) {
265 int ret = read(fd, buf, size);
266
267 if (ret < 0)
268 die("failed to read");
Pierre Habouzit7eac7e92009-08-07 14:16:00 +0200269 if (ret == 0)
270 die("failed to read: missing data");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200271
272 size -= ret;
273 buf += ret;
274 }
275}
276
277struct perf_header *perf_header__read(int fd)
278{
279 struct perf_header *self = perf_header__new();
280 struct perf_file_header f_header;
281 struct perf_file_attr f_attr;
282 u64 f_id;
283
284 int nr_attrs, nr_ids, i, j;
285
286 lseek(fd, 0, SEEK_SET);
287 do_read(fd, &f_header, sizeof(f_header));
288
289 if (f_header.magic != PERF_MAGIC ||
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200290 f_header.attr_size != sizeof(f_attr))
291 die("incompatible file format");
292
Frederic Weisbecker26dd2cb2009-10-08 22:07:29 +0200293 if (f_header.size != sizeof(f_header)) {
294 /* Support the previous format */
295 if (f_header.size == offsetof(typeof(f_header), trace_info))
296 f_header.trace_info.size = 0;
297 else
298 die("incompatible file format");
299 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200300 nr_attrs = f_header.attrs.size / sizeof(f_attr);
301 lseek(fd, f_header.attrs.offset, SEEK_SET);
302
303 for (i = 0; i < nr_attrs; i++) {
304 struct perf_header_attr *attr;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200305 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200306
307 do_read(fd, &f_attr, sizeof(f_attr));
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200308 tmp = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200309
310 attr = perf_header_attr__new(&f_attr.attr);
311
312 nr_ids = f_attr.ids.size / sizeof(u64);
313 lseek(fd, f_attr.ids.offset, SEEK_SET);
314
315 for (j = 0; j < nr_ids; j++) {
316 do_read(fd, &f_id, sizeof(f_id));
317
318 perf_header_attr__add_id(attr, f_id);
319 }
320 perf_header__add_attr(self, attr);
321 lseek(fd, tmp, SEEK_SET);
322 }
323
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200324 if (f_header.event_types.size) {
325 lseek(fd, f_header.event_types.offset, SEEK_SET);
326 events = malloc(f_header.event_types.size);
327 if (!events)
328 die("nomem");
329 do_read(fd, events, f_header.event_types.size);
330 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
331 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200332
333 self->trace_info_offset = f_header.trace_info.offset;
334 self->trace_info_size = f_header.trace_info.size;
335
336 if (self->trace_info_size) {
337 lseek(fd, self->trace_info_offset, SEEK_SET);
338 trace_report(fd);
339 }
340
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200341 self->event_offset = f_header.event_types.offset;
342 self->event_size = f_header.event_types.size;
343
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200344 self->data_offset = f_header.data.offset;
345 self->data_size = f_header.data.size;
346
Ingo Molnar2e01d172009-09-03 16:21:11 +0200347 lseek(fd, self->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200348
349 self->frozen = 1;
350
351 return self;
352}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200353
354u64 perf_header__sample_type(struct perf_header *header)
355{
356 u64 type = 0;
357 int i;
358
359 for (i = 0; i < header->attrs; i++) {
360 struct perf_header_attr *attr = header->attr[i];
361
362 if (!type)
363 type = attr->attr.sample_type;
364 else if (type != attr->attr.sample_type)
365 die("non matching sample_type");
366 }
367
368 return type;
369}
370
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200371struct perf_event_attr *
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200372perf_header__find_attr(u64 id, struct perf_header *header)
373{
374 int i;
375
376 for (i = 0; i < header->attrs; i++) {
377 struct perf_header_attr *attr = header->attr[i];
378 int j;
379
380 for (j = 0; j < attr->ids; j++) {
381 if (attr->id[j] == id)
382 return &attr->attr;
383 }
384 }
385
386 return NULL;
387}