mm-only debug patch...
[mmotm.git] / tools / perf / util / header.c
blob9aae360c0f284762e04e39e6ab8b144d577597fc
1 #include <sys/types.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <stdlib.h>
6 #include "util.h"
7 #include "header.h"
8 #include "../perf.h"
9 #include "trace-event.h"
12 * Create new perf.data header attribute:
14 struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
16 struct perf_header_attr *self = malloc(sizeof(*self));
18 if (!self)
19 die("nomem");
21 self->attr = *attr;
22 self->ids = 0;
23 self->size = 1;
24 self->id = malloc(sizeof(u64));
26 if (!self->id)
27 die("nomem");
29 return self;
32 void perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
34 int pos = self->ids;
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");
43 self->id[pos] = id;
47 * Create new perf.data header:
49 struct perf_header *perf_header__new(void)
51 struct perf_header *self = malloc(sizeof(*self));
53 if (!self)
54 die("nomem");
56 self->frozen = 0;
58 self->attrs = 0;
59 self->size = 1;
60 self->attr = malloc(sizeof(void *));
62 if (!self->attr)
63 die("nomem");
65 self->data_offset = 0;
66 self->data_size = 0;
67 self->trace_info_offset = 0;
68 self->trace_info_size = 0;
70 return self;
73 void perf_header__add_attr(struct perf_header *self,
74 struct perf_header_attr *attr)
76 int pos = self->attrs;
78 if (self->frozen)
79 die("frozen");
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");
88 self->attr[pos] = attr;
91 #define MAX_EVENT_NAME 64
93 struct perf_trace_event_type {
94 u64 event_id;
95 char name[MAX_EVENT_NAME];
98 static int event_count;
99 static struct perf_trace_event_type *events;
101 void perf_header__push_event(u64 id, const char *name)
103 if (strlen(name) > MAX_EVENT_NAME)
104 printf("Event %s will be truncated\n", name);
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");
115 memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
116 events[event_count].event_id = id;
117 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
118 event_count++;
121 char *perf_header__find_event(u64 id)
123 int i;
124 for (i = 0 ; i < event_count; i++) {
125 if (events[i].event_id == id)
126 return events[i].name;
128 return NULL;
131 static const char *__perf_magic = "PERFFILE";
133 #define PERF_MAGIC (*(u64 *)__perf_magic)
135 struct perf_file_section {
136 u64 offset;
137 u64 size;
140 struct perf_file_attr {
141 struct perf_event_attr attr;
142 struct perf_file_section ids;
145 struct 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;
151 struct perf_file_section event_types;
152 struct perf_file_section trace_info;
155 static int trace_info;
157 void perf_header__set_trace_info(void)
159 trace_info = 1;
162 static void do_write(int fd, void *buf, size_t size)
164 while (size) {
165 int ret = write(fd, buf, size);
167 if (ret < 0)
168 die("failed to write");
170 size -= ret;
171 buf += ret;
175 void perf_header__write(struct perf_header *self, int fd)
177 struct perf_file_header f_header;
178 struct perf_file_attr f_attr;
179 struct perf_header_attr *attr;
180 int i;
182 lseek(fd, sizeof(f_header), SEEK_SET);
185 for (i = 0; i < self->attrs; i++) {
186 attr = self->attr[i];
188 attr->id_offset = lseek(fd, 0, SEEK_CUR);
189 do_write(fd, attr->id, attr->ids * sizeof(u64));
193 self->attr_offset = lseek(fd, 0, SEEK_CUR);
195 for (i = 0; i < self->attrs; i++) {
196 attr = self->attr[i];
198 f_attr = (struct perf_file_attr){
199 .attr = attr->attr,
200 .ids = {
201 .offset = attr->id_offset,
202 .size = attr->ids * sizeof(u64),
205 do_write(fd, &f_attr, sizeof(f_attr));
208 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);
213 if (trace_info) {
214 static int trace_info_written;
217 * Write it only once
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);
231 self->data_offset = lseek(fd, 0, SEEK_CUR);
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),
241 .data = {
242 .offset = self->data_offset,
243 .size = self->data_size,
245 .event_types = {
246 .offset = self->event_offset,
247 .size = self->event_size,
249 .trace_info = {
250 .offset = self->trace_info_offset,
251 .size = self->trace_info_size,
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);
259 self->frozen = 1;
262 static void do_read(int fd, void *buf, size_t size)
264 while (size) {
265 int ret = read(fd, buf, size);
267 if (ret < 0)
268 die("failed to read");
269 if (ret == 0)
270 die("failed to read: missing data");
272 size -= ret;
273 buf += ret;
277 struct perf_header *perf_header__read(int fd)
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;
284 int nr_attrs, nr_ids, i, j;
286 lseek(fd, 0, SEEK_SET);
287 do_read(fd, &f_header, sizeof(f_header));
289 if (f_header.magic != PERF_MAGIC ||
290 f_header.attr_size != sizeof(f_attr))
291 die("incompatible file format");
293 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");
300 nr_attrs = f_header.attrs.size / sizeof(f_attr);
301 lseek(fd, f_header.attrs.offset, SEEK_SET);
303 for (i = 0; i < nr_attrs; i++) {
304 struct perf_header_attr *attr;
305 off_t tmp;
307 do_read(fd, &f_attr, sizeof(f_attr));
308 tmp = lseek(fd, 0, SEEK_CUR);
310 attr = perf_header_attr__new(&f_attr.attr);
312 nr_ids = f_attr.ids.size / sizeof(u64);
313 lseek(fd, f_attr.ids.offset, SEEK_SET);
315 for (j = 0; j < nr_ids; j++) {
316 do_read(fd, &f_id, sizeof(f_id));
318 perf_header_attr__add_id(attr, f_id);
320 perf_header__add_attr(self, attr);
321 lseek(fd, tmp, SEEK_SET);
324 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);
333 self->trace_info_offset = f_header.trace_info.offset;
334 self->trace_info_size = f_header.trace_info.size;
336 if (self->trace_info_size) {
337 lseek(fd, self->trace_info_offset, SEEK_SET);
338 trace_report(fd);
341 self->event_offset = f_header.event_types.offset;
342 self->event_size = f_header.event_types.size;
344 self->data_offset = f_header.data.offset;
345 self->data_size = f_header.data.size;
347 lseek(fd, self->data_offset, SEEK_SET);
349 self->frozen = 1;
351 return self;
354 u64 perf_header__sample_type(struct perf_header *header)
356 u64 type = 0;
357 int i;
359 for (i = 0; i < header->attrs; i++) {
360 struct perf_header_attr *attr = header->attr[i];
362 if (!type)
363 type = attr->attr.sample_type;
364 else if (type != attr->attr.sample_type)
365 die("non matching sample_type");
368 return type;
371 struct perf_event_attr *
372 perf_header__find_attr(u64 id, struct perf_header *header)
374 int i;
376 for (i = 0; i < header->attrs; i++) {
377 struct perf_header_attr *attr = header->attr[i];
378 int j;
380 for (j = 0; j < attr->ids; j++) {
381 if (attr->id[j] == id)
382 return &attr->attr;
386 return NULL;