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> |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 5 | #include <linux/list.h> |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 6 | |
| 7 | #include "util.h" |
| 8 | #include "header.h" |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 9 | #include "../perf.h" |
| 10 | #include "trace-event.h" |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 11 | #include "symbol.h" |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 12 | #include "data_map.h" |
| 13 | #include "debug.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 14 | |
| 15 | /* |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 16 | * Create new perf.data header attribute: |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 17 | */ |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 18 | 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] | 19 | { |
| 20 | struct perf_header_attr *self = malloc(sizeof(*self)); |
| 21 | |
| 22 | if (!self) |
| 23 | die("nomem"); |
| 24 | |
| 25 | self->attr = *attr; |
| 26 | self->ids = 0; |
| 27 | self->size = 1; |
| 28 | self->id = malloc(sizeof(u64)); |
| 29 | |
| 30 | if (!self->id) |
| 31 | die("nomem"); |
| 32 | |
| 33 | return self; |
| 34 | } |
| 35 | |
| 36 | void perf_header_attr__add_id(struct perf_header_attr *self, u64 id) |
| 37 | { |
| 38 | int pos = self->ids; |
| 39 | |
| 40 | self->ids++; |
| 41 | if (self->ids > self->size) { |
| 42 | self->size *= 2; |
| 43 | self->id = realloc(self->id, self->size * sizeof(u64)); |
| 44 | if (!self->id) |
| 45 | die("nomem"); |
| 46 | } |
| 47 | self->id[pos] = id; |
| 48 | } |
| 49 | |
| 50 | /* |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 51 | * Create new perf.data header: |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 52 | */ |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 53 | struct perf_header *perf_header__new(void) |
| 54 | { |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 55 | struct perf_header *self = calloc(sizeof(*self), 1); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 56 | |
| 57 | if (!self) |
| 58 | die("nomem"); |
| 59 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 60 | self->size = 1; |
| 61 | self->attr = malloc(sizeof(void *)); |
| 62 | |
| 63 | if (!self->attr) |
| 64 | die("nomem"); |
| 65 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 66 | return self; |
| 67 | } |
| 68 | |
| 69 | void perf_header__add_attr(struct perf_header *self, |
| 70 | struct perf_header_attr *attr) |
| 71 | { |
| 72 | int pos = self->attrs; |
| 73 | |
| 74 | if (self->frozen) |
| 75 | die("frozen"); |
| 76 | |
| 77 | self->attrs++; |
| 78 | if (self->attrs > self->size) { |
| 79 | self->size *= 2; |
| 80 | self->attr = realloc(self->attr, self->size * sizeof(void *)); |
| 81 | if (!self->attr) |
| 82 | die("nomem"); |
| 83 | } |
| 84 | self->attr[pos] = attr; |
| 85 | } |
| 86 | |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 87 | #define MAX_EVENT_NAME 64 |
| 88 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 89 | struct perf_trace_event_type { |
| 90 | u64 event_id; |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 91 | char name[MAX_EVENT_NAME]; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static int event_count; |
| 95 | static struct perf_trace_event_type *events; |
| 96 | |
| 97 | void perf_header__push_event(u64 id, const char *name) |
| 98 | { |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 99 | if (strlen(name) > MAX_EVENT_NAME) |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 100 | pr_warning("Event %s will be truncated\n", name); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 101 | |
| 102 | if (!events) { |
| 103 | events = malloc(sizeof(struct perf_trace_event_type)); |
| 104 | if (!events) |
| 105 | die("nomem"); |
| 106 | } else { |
| 107 | events = realloc(events, (event_count + 1) * sizeof(struct perf_trace_event_type)); |
| 108 | if (!events) |
| 109 | die("nomem"); |
| 110 | } |
| 111 | memset(&events[event_count], 0, sizeof(struct perf_trace_event_type)); |
| 112 | events[event_count].event_id = id; |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 113 | strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 114 | event_count++; |
| 115 | } |
| 116 | |
| 117 | char *perf_header__find_event(u64 id) |
| 118 | { |
| 119 | int i; |
| 120 | for (i = 0 ; i < event_count; i++) { |
| 121 | if (events[i].event_id == id) |
| 122 | return events[i].name; |
| 123 | } |
| 124 | return NULL; |
| 125 | } |
| 126 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 127 | static const char *__perf_magic = "PERFFILE"; |
| 128 | |
| 129 | #define PERF_MAGIC (*(u64 *)__perf_magic) |
| 130 | |
| 131 | struct perf_file_section { |
| 132 | u64 offset; |
| 133 | u64 size; |
| 134 | }; |
| 135 | |
| 136 | struct perf_file_attr { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 137 | struct perf_event_attr attr; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 138 | struct perf_file_section ids; |
| 139 | }; |
| 140 | |
| 141 | struct perf_file_header { |
| 142 | u64 magic; |
| 143 | u64 size; |
| 144 | u64 attr_size; |
| 145 | struct perf_file_section attrs; |
| 146 | struct perf_file_section data; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 147 | struct perf_file_section event_types; |
Frederic Weisbecker | db9f11e | 2009-10-17 17:57:18 +0200 | [diff] [blame] | 148 | DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 149 | }; |
| 150 | |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 151 | void perf_header__set_feat(struct perf_header *self, int feat) |
| 152 | { |
| 153 | set_bit(feat, self->adds_features); |
| 154 | } |
| 155 | |
| 156 | bool perf_header__has_feat(const struct perf_header *self, int feat) |
| 157 | { |
| 158 | return test_bit(feat, self->adds_features); |
| 159 | } |
| 160 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 161 | static void do_write(int fd, void *buf, size_t size) |
| 162 | { |
| 163 | while (size) { |
| 164 | int ret = write(fd, buf, size); |
| 165 | |
| 166 | if (ret < 0) |
| 167 | die("failed to write"); |
| 168 | |
| 169 | size -= ret; |
| 170 | buf += ret; |
| 171 | } |
| 172 | } |
| 173 | |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 174 | static void write_buildid_table(int fd, struct list_head *id_head) |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 175 | { |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 176 | struct build_id_list *iter, *next; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 177 | |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 178 | list_for_each_entry_safe(iter, next, id_head, list) { |
| 179 | struct build_id_event *b = &iter->event; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 180 | |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 181 | do_write(fd, b, sizeof(*b)); |
| 182 | do_write(fd, (void *)iter->dso_name, iter->len); |
| 183 | list_del(&iter->list); |
| 184 | free(iter); |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 185 | } |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static void |
| 189 | perf_header__adds_write(struct perf_header *self, int fd, bool at_exit) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 190 | { |
| 191 | struct perf_file_section trace_sec; |
| 192 | u64 cur_offset = lseek(fd, 0, SEEK_CUR); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 193 | |
Frederic Weisbecker | 3e13ab2 | 2009-11-11 04:51:06 +0100 | [diff] [blame^] | 194 | if (perf_header__has_feat(self, HEADER_TRACE_INFO)) { |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 195 | /* Write trace info */ |
| 196 | trace_sec.offset = lseek(fd, sizeof(trace_sec), SEEK_CUR); |
| 197 | read_tracing_data(fd, attrs, nr_counters); |
| 198 | trace_sec.size = lseek(fd, 0, SEEK_CUR) - trace_sec.offset; |
| 199 | |
| 200 | /* Write trace info headers */ |
| 201 | lseek(fd, cur_offset, SEEK_SET); |
| 202 | do_write(fd, &trace_sec, sizeof(trace_sec)); |
| 203 | |
| 204 | /* |
| 205 | * Update cur_offset. So that other (future) |
| 206 | * features can set their own infos in this place. But if we are |
| 207 | * the only feature, at least that seeks to the place the data |
| 208 | * should begin. |
| 209 | */ |
| 210 | cur_offset = lseek(fd, trace_sec.offset + trace_sec.size, SEEK_SET); |
| 211 | } |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 212 | |
| 213 | if (at_exit) { |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 214 | LIST_HEAD(id_list); |
| 215 | |
| 216 | if (fetch_build_id_table(&id_list)) { |
| 217 | lseek(fd, self->data_offset + self->data_size, SEEK_SET); |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 218 | perf_header__set_feat(self, HEADER_BUILD_ID); |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 219 | write_buildid_table(fd, &id_list); |
| 220 | lseek(fd, cur_offset, SEEK_SET); |
| 221 | } |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 222 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 223 | }; |
| 224 | |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 225 | void perf_header__write(struct perf_header *self, int fd, bool at_exit) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 226 | { |
| 227 | struct perf_file_header f_header; |
| 228 | struct perf_file_attr f_attr; |
| 229 | struct perf_header_attr *attr; |
| 230 | int i; |
| 231 | |
| 232 | lseek(fd, sizeof(f_header), SEEK_SET); |
| 233 | |
| 234 | |
| 235 | for (i = 0; i < self->attrs; i++) { |
| 236 | attr = self->attr[i]; |
| 237 | |
| 238 | attr->id_offset = lseek(fd, 0, SEEK_CUR); |
| 239 | do_write(fd, attr->id, attr->ids * sizeof(u64)); |
| 240 | } |
| 241 | |
| 242 | |
| 243 | self->attr_offset = lseek(fd, 0, SEEK_CUR); |
| 244 | |
| 245 | for (i = 0; i < self->attrs; i++) { |
| 246 | attr = self->attr[i]; |
| 247 | |
| 248 | f_attr = (struct perf_file_attr){ |
| 249 | .attr = attr->attr, |
| 250 | .ids = { |
| 251 | .offset = attr->id_offset, |
| 252 | .size = attr->ids * sizeof(u64), |
| 253 | } |
| 254 | }; |
| 255 | do_write(fd, &f_attr, sizeof(f_attr)); |
| 256 | } |
| 257 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 258 | self->event_offset = lseek(fd, 0, SEEK_CUR); |
| 259 | self->event_size = event_count * sizeof(struct perf_trace_event_type); |
| 260 | if (events) |
| 261 | do_write(fd, events, self->event_size); |
| 262 | |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 263 | perf_header__adds_write(self, fd, at_exit); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 264 | |
| 265 | self->data_offset = lseek(fd, 0, SEEK_CUR); |
| 266 | |
| 267 | f_header = (struct perf_file_header){ |
| 268 | .magic = PERF_MAGIC, |
| 269 | .size = sizeof(f_header), |
| 270 | .attr_size = sizeof(f_attr), |
| 271 | .attrs = { |
| 272 | .offset = self->attr_offset, |
| 273 | .size = self->attrs * sizeof(f_attr), |
| 274 | }, |
| 275 | .data = { |
| 276 | .offset = self->data_offset, |
| 277 | .size = self->data_size, |
| 278 | }, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 279 | .event_types = { |
| 280 | .offset = self->event_offset, |
| 281 | .size = self->event_size, |
| 282 | }, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 283 | }; |
| 284 | |
Frederic Weisbecker | db9f11e | 2009-10-17 17:57:18 +0200 | [diff] [blame] | 285 | memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features)); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 286 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 287 | lseek(fd, 0, SEEK_SET); |
| 288 | do_write(fd, &f_header, sizeof(f_header)); |
| 289 | lseek(fd, self->data_offset + self->data_size, SEEK_SET); |
| 290 | |
| 291 | self->frozen = 1; |
| 292 | } |
| 293 | |
| 294 | static void do_read(int fd, void *buf, size_t size) |
| 295 | { |
| 296 | while (size) { |
| 297 | int ret = read(fd, buf, size); |
| 298 | |
| 299 | if (ret < 0) |
| 300 | die("failed to read"); |
Pierre Habouzit | 7eac7e9 | 2009-08-07 14:16:00 +0200 | [diff] [blame] | 301 | if (ret == 0) |
| 302 | die("failed to read: missing data"); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 303 | |
| 304 | size -= ret; |
| 305 | buf += ret; |
| 306 | } |
| 307 | } |
| 308 | |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 309 | static void perf_header__adds_read(struct perf_header *self, int fd) |
| 310 | { |
Frederic Weisbecker | 3e13ab2 | 2009-11-11 04:51:06 +0100 | [diff] [blame^] | 311 | if (perf_header__has_feat(self, HEADER_TRACE_INFO)) { |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 312 | struct perf_file_section trace_sec; |
| 313 | |
| 314 | do_read(fd, &trace_sec, sizeof(trace_sec)); |
| 315 | lseek(fd, trace_sec.offset, SEEK_SET); |
| 316 | trace_report(fd); |
| 317 | lseek(fd, trace_sec.offset + trace_sec.size, SEEK_SET); |
| 318 | } |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 319 | |
| 320 | if (perf_header__has_feat(self, HEADER_BUILD_ID)) { |
| 321 | struct stat input_stat; |
| 322 | |
| 323 | fstat(fd, &input_stat); |
| 324 | if (perf_header__read_build_ids(self, fd, input_stat.st_size)) |
| 325 | pr_debug("failed to read buildids, continuing...\n"); |
| 326 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 327 | }; |
| 328 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 329 | struct perf_header *perf_header__read(int fd) |
| 330 | { |
| 331 | struct perf_header *self = perf_header__new(); |
| 332 | struct perf_file_header f_header; |
| 333 | struct perf_file_attr f_attr; |
| 334 | u64 f_id; |
| 335 | |
| 336 | int nr_attrs, nr_ids, i, j; |
| 337 | |
| 338 | lseek(fd, 0, SEEK_SET); |
| 339 | do_read(fd, &f_header, sizeof(f_header)); |
| 340 | |
| 341 | if (f_header.magic != PERF_MAGIC || |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 342 | f_header.attr_size != sizeof(f_attr)) |
| 343 | die("incompatible file format"); |
| 344 | |
Frederic Weisbecker | 26dd2cb | 2009-10-08 22:07:29 +0200 | [diff] [blame] | 345 | if (f_header.size != sizeof(f_header)) { |
| 346 | /* Support the previous format */ |
Frederic Weisbecker | db9f11e | 2009-10-17 17:57:18 +0200 | [diff] [blame] | 347 | if (f_header.size == offsetof(typeof(f_header), adds_features)) |
| 348 | bitmap_zero(f_header.adds_features, HEADER_FEAT_BITS); |
| 349 | else |
Frederic Weisbecker | 26dd2cb | 2009-10-08 22:07:29 +0200 | [diff] [blame] | 350 | die("incompatible file format"); |
| 351 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 352 | nr_attrs = f_header.attrs.size / sizeof(f_attr); |
| 353 | lseek(fd, f_header.attrs.offset, SEEK_SET); |
| 354 | |
| 355 | for (i = 0; i < nr_attrs; i++) { |
| 356 | struct perf_header_attr *attr; |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 357 | off_t tmp; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 358 | |
| 359 | do_read(fd, &f_attr, sizeof(f_attr)); |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 360 | tmp = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 361 | |
| 362 | attr = perf_header_attr__new(&f_attr.attr); |
| 363 | |
| 364 | nr_ids = f_attr.ids.size / sizeof(u64); |
| 365 | lseek(fd, f_attr.ids.offset, SEEK_SET); |
| 366 | |
| 367 | for (j = 0; j < nr_ids; j++) { |
| 368 | do_read(fd, &f_id, sizeof(f_id)); |
| 369 | |
| 370 | perf_header_attr__add_id(attr, f_id); |
| 371 | } |
| 372 | perf_header__add_attr(self, attr); |
| 373 | lseek(fd, tmp, SEEK_SET); |
| 374 | } |
| 375 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 376 | if (f_header.event_types.size) { |
| 377 | lseek(fd, f_header.event_types.offset, SEEK_SET); |
| 378 | events = malloc(f_header.event_types.size); |
| 379 | if (!events) |
| 380 | die("nomem"); |
| 381 | do_read(fd, events, f_header.event_types.size); |
| 382 | event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); |
| 383 | } |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 384 | |
Frederic Weisbecker | db9f11e | 2009-10-17 17:57:18 +0200 | [diff] [blame] | 385 | memcpy(&self->adds_features, &f_header.adds_features, sizeof(f_header.adds_features)); |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 386 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 387 | self->event_offset = f_header.event_types.offset; |
| 388 | self->event_size = f_header.event_types.size; |
| 389 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 390 | self->data_offset = f_header.data.offset; |
| 391 | self->data_size = f_header.data.size; |
| 392 | |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 393 | perf_header__adds_read(self, fd); |
| 394 | |
Ingo Molnar | 2e01d17 | 2009-09-03 16:21:11 +0200 | [diff] [blame] | 395 | lseek(fd, self->data_offset, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 396 | |
| 397 | self->frozen = 1; |
| 398 | |
| 399 | return self; |
| 400 | } |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 401 | |
| 402 | u64 perf_header__sample_type(struct perf_header *header) |
| 403 | { |
| 404 | u64 type = 0; |
| 405 | int i; |
| 406 | |
| 407 | for (i = 0; i < header->attrs; i++) { |
| 408 | struct perf_header_attr *attr = header->attr[i]; |
| 409 | |
| 410 | if (!type) |
| 411 | type = attr->attr.sample_type; |
| 412 | else if (type != attr->attr.sample_type) |
| 413 | die("non matching sample_type"); |
| 414 | } |
| 415 | |
| 416 | return type; |
| 417 | } |
| 418 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 419 | struct perf_event_attr * |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 420 | perf_header__find_attr(u64 id, struct perf_header *header) |
| 421 | { |
| 422 | int i; |
| 423 | |
| 424 | for (i = 0; i < header->attrs; i++) { |
| 425 | struct perf_header_attr *attr = header->attr[i]; |
| 426 | int j; |
| 427 | |
| 428 | for (j = 0; j < attr->ids; j++) { |
| 429 | if (attr->id[j] == id) |
| 430 | return &attr->attr; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | return NULL; |
| 435 | } |