Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1 | #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 Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 8 | #include "../perf.h" |
| 9 | #include "trace-event.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 10 | |
| 11 | /* |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 12 | * Create new perf.data header attribute: |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 13 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 14 | struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 15 | { |
| 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 | |
| 32 | void 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 Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 47 | * Create new perf.data header: |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 48 | */ |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 49 | struct 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 Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 67 | self->trace_info_offset = 0; |
| 68 | self->trace_info_size = 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 69 | |
| 70 | return self; |
| 71 | } |
| 72 | |
| 73 | void 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 Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 91 | #define MAX_EVENT_NAME 64 |
| 92 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 93 | struct perf_trace_event_type { |
| 94 | u64 event_id; |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 95 | char name[MAX_EVENT_NAME]; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | static int event_count; |
| 99 | static struct perf_trace_event_type *events; |
| 100 | |
| 101 | void perf_header__push_event(u64 id, const char *name) |
| 102 | { |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 103 | if (strlen(name) > MAX_EVENT_NAME) |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 104 | 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 Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 117 | strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 118 | event_count++; |
| 119 | } |
| 120 | |
| 121 | char *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 Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 131 | static const char *__perf_magic = "PERFFILE"; |
| 132 | |
| 133 | #define PERF_MAGIC (*(u64 *)__perf_magic) |
| 134 | |
| 135 | struct perf_file_section { |
| 136 | u64 offset; |
| 137 | u64 size; |
| 138 | }; |
| 139 | |
| 140 | struct perf_file_attr { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 141 | struct perf_event_attr attr; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 142 | struct perf_file_section ids; |
| 143 | }; |
| 144 | |
| 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; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 151 | struct perf_file_section event_types; |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 152 | struct perf_file_section trace_info; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 153 | }; |
| 154 | |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 155 | static int trace_info; |
| 156 | |
| 157 | void perf_header__set_trace_info(void) |
| 158 | { |
| 159 | trace_info = 1; |
| 160 | } |
| 161 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 162 | static 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 | |
| 175 | void 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 Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 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); |
| 212 | |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 213 | 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 Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 230 | |
| 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 Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 245 | .event_types = { |
| 246 | .offset = self->event_offset, |
| 247 | .size = self->event_size, |
| 248 | }, |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 249 | .trace_info = { |
| 250 | .offset = self->trace_info_offset, |
| 251 | .size = self->trace_info_size, |
| 252 | }, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 253 | }; |
| 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 | |
| 262 | static 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 Habouzit | 7eac7e9 | 2009-08-07 14:16:00 +0200 | [diff] [blame] | 269 | if (ret == 0) |
| 270 | die("failed to read: missing data"); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 271 | |
| 272 | size -= ret; |
| 273 | buf += ret; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | struct 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 Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 290 | f_header.attr_size != sizeof(f_attr)) |
| 291 | die("incompatible file format"); |
| 292 | |
Frederic Weisbecker | 26dd2cb | 2009-10-08 22:07:29 +0200 | [diff] [blame] | 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"); |
| 299 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 300 | 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 Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 305 | off_t tmp; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 306 | |
| 307 | do_read(fd, &f_attr, sizeof(f_attr)); |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 308 | tmp = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 309 | |
| 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 Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 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); |
| 331 | } |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 332 | |
| 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 Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 341 | self->event_offset = f_header.event_types.offset; |
| 342 | self->event_size = f_header.event_types.size; |
| 343 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 344 | self->data_offset = f_header.data.offset; |
| 345 | self->data_size = f_header.data.size; |
| 346 | |
Ingo Molnar | 2e01d17 | 2009-09-03 16:21:11 +0200 | [diff] [blame] | 347 | lseek(fd, self->data_offset, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 348 | |
| 349 | self->frozen = 1; |
| 350 | |
| 351 | return self; |
| 352 | } |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 353 | |
| 354 | u64 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 Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 371 | struct perf_event_attr * |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 372 | perf_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 | } |