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 | |
Arnaldo Carvalho de Melo | dc79c0f | 2009-11-16 19:30:26 -0200 | [diff] [blame] | 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 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 32 | |
| 33 | return self; |
| 34 | } |
| 35 | |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 36 | void perf_header_attr__delete(struct perf_header_attr *self) |
| 37 | { |
| 38 | free(self->id); |
| 39 | free(self); |
| 40 | } |
| 41 | |
Arnaldo Carvalho de Melo | 5875412 | 2009-11-17 01:18:10 -0200 | [diff] [blame] | 42 | int perf_header_attr__add_id(struct perf_header_attr *self, u64 id) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 43 | { |
| 44 | int pos = self->ids; |
| 45 | |
| 46 | self->ids++; |
| 47 | if (self->ids > self->size) { |
Arnaldo Carvalho de Melo | 5875412 | 2009-11-17 01:18:10 -0200 | [diff] [blame] | 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; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 56 | } |
| 57 | self->id[pos] = id; |
Arnaldo Carvalho de Melo | 5875412 | 2009-11-17 01:18:10 -0200 | [diff] [blame] | 58 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 62 | * Create new perf.data header: |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 63 | */ |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 64 | struct perf_header *perf_header__new(void) |
| 65 | { |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 66 | struct perf_header *self = calloc(sizeof(*self), 1); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 67 | |
Arnaldo Carvalho de Melo | a9a70bb | 2009-11-17 01:18:11 -0200 | [diff] [blame^] | 68 | if (self != NULL) { |
| 69 | self->size = 1; |
| 70 | self->attr = malloc(sizeof(void *)); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 71 | |
Arnaldo Carvalho de Melo | a9a70bb | 2009-11-17 01:18:11 -0200 | [diff] [blame^] | 72 | if (self->attr == NULL) { |
| 73 | free(self); |
| 74 | self = NULL; |
| 75 | } |
| 76 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 77 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 78 | return self; |
| 79 | } |
| 80 | |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 81 | int perf_header__add_attr(struct perf_header *self, |
| 82 | struct perf_header_attr *attr) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 83 | { |
| 84 | int pos = self->attrs; |
| 85 | |
| 86 | if (self->frozen) |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 87 | return -1; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 88 | |
| 89 | self->attrs++; |
| 90 | if (self->attrs > self->size) { |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 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; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 100 | } |
| 101 | self->attr[pos] = attr; |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 102 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 105 | #define MAX_EVENT_NAME 64 |
| 106 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 107 | struct perf_trace_event_type { |
| 108 | u64 event_id; |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 109 | char name[MAX_EVENT_NAME]; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 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 | { |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 117 | if (strlen(name) > MAX_EVENT_NAME) |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 118 | pr_warning("Event %s will be truncated\n", name); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 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; |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 131 | strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 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 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 145 | static const char *__perf_magic = "PERFFILE"; |
| 146 | |
| 147 | #define PERF_MAGIC (*(u64 *)__perf_magic) |
| 148 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 149 | struct perf_file_attr { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 150 | struct perf_event_attr attr; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 151 | struct perf_file_section ids; |
| 152 | }; |
| 153 | |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 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 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 164 | static void do_write(int fd, void *buf, size_t size) |
| 165 | { |
| 166 | while (size) { |
| 167 | int ret = write(fd, buf, size); |
| 168 | |
| 169 | if (ret < 0) |
| 170 | die("failed to write"); |
| 171 | |
| 172 | size -= ret; |
| 173 | buf += ret; |
| 174 | } |
| 175 | } |
| 176 | |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 177 | static void write_buildid_table(int fd, struct list_head *id_head) |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 178 | { |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 179 | struct build_id_list *iter, *next; |
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 | list_for_each_entry_safe(iter, next, id_head, list) { |
| 182 | struct build_id_event *b = &iter->event; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 183 | |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 184 | do_write(fd, b, sizeof(*b)); |
| 185 | do_write(fd, (void *)iter->dso_name, iter->len); |
| 186 | list_del(&iter->list); |
| 187 | free(iter); |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 188 | } |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 192 | perf_header__adds_write(struct perf_header *self, int fd) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 193 | { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 194 | LIST_HEAD(id_list); |
| 195 | int nr_sections; |
| 196 | struct perf_file_section *feat_sec; |
| 197 | int sec_size; |
| 198 | u64 sec_start; |
| 199 | int idx = 0; |
| 200 | |
| 201 | if (fetch_build_id_table(&id_list)) |
| 202 | perf_header__set_feat(self, HEADER_BUILD_ID); |
| 203 | |
| 204 | nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS); |
| 205 | if (!nr_sections) |
| 206 | return; |
| 207 | |
| 208 | feat_sec = calloc(sizeof(*feat_sec), nr_sections); |
| 209 | if (!feat_sec) |
| 210 | die("No memory"); |
| 211 | |
| 212 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 213 | |
| 214 | sec_start = self->data_offset + self->data_size; |
| 215 | lseek(fd, sec_start + sec_size, SEEK_SET); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 216 | |
Frederic Weisbecker | 3e13ab2 | 2009-11-11 04:51:06 +0100 | [diff] [blame] | 217 | if (perf_header__has_feat(self, HEADER_TRACE_INFO)) { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 218 | struct perf_file_section *trace_sec; |
| 219 | |
| 220 | trace_sec = &feat_sec[idx++]; |
| 221 | |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 222 | /* Write trace info */ |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 223 | trace_sec->offset = lseek(fd, 0, SEEK_CUR); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 224 | read_tracing_data(fd, attrs, nr_counters); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 225 | trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset; |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 226 | } |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 227 | |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 228 | |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 229 | if (perf_header__has_feat(self, HEADER_BUILD_ID)) { |
| 230 | struct perf_file_section *buildid_sec; |
| 231 | |
| 232 | buildid_sec = &feat_sec[idx++]; |
| 233 | |
| 234 | /* Write build-ids */ |
| 235 | buildid_sec->offset = lseek(fd, 0, SEEK_CUR); |
| 236 | write_buildid_table(fd, &id_list); |
| 237 | buildid_sec->size = lseek(fd, 0, SEEK_CUR) - buildid_sec->offset; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 238 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 239 | |
| 240 | lseek(fd, sec_start, SEEK_SET); |
| 241 | do_write(fd, feat_sec, sec_size); |
| 242 | free(feat_sec); |
| 243 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 244 | |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 245 | 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] | 246 | { |
| 247 | struct perf_file_header f_header; |
| 248 | struct perf_file_attr f_attr; |
| 249 | struct perf_header_attr *attr; |
| 250 | int i; |
| 251 | |
| 252 | lseek(fd, sizeof(f_header), SEEK_SET); |
| 253 | |
| 254 | |
| 255 | for (i = 0; i < self->attrs; i++) { |
| 256 | attr = self->attr[i]; |
| 257 | |
| 258 | attr->id_offset = lseek(fd, 0, SEEK_CUR); |
| 259 | do_write(fd, attr->id, attr->ids * sizeof(u64)); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | self->attr_offset = lseek(fd, 0, SEEK_CUR); |
| 264 | |
| 265 | for (i = 0; i < self->attrs; i++) { |
| 266 | attr = self->attr[i]; |
| 267 | |
| 268 | f_attr = (struct perf_file_attr){ |
| 269 | .attr = attr->attr, |
| 270 | .ids = { |
| 271 | .offset = attr->id_offset, |
| 272 | .size = attr->ids * sizeof(u64), |
| 273 | } |
| 274 | }; |
| 275 | do_write(fd, &f_attr, sizeof(f_attr)); |
| 276 | } |
| 277 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 278 | self->event_offset = lseek(fd, 0, SEEK_CUR); |
| 279 | self->event_size = event_count * sizeof(struct perf_trace_event_type); |
| 280 | if (events) |
| 281 | do_write(fd, events, self->event_size); |
| 282 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 283 | self->data_offset = lseek(fd, 0, SEEK_CUR); |
| 284 | |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 285 | if (at_exit) |
| 286 | perf_header__adds_write(self, fd); |
| 287 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 288 | f_header = (struct perf_file_header){ |
| 289 | .magic = PERF_MAGIC, |
| 290 | .size = sizeof(f_header), |
| 291 | .attr_size = sizeof(f_attr), |
| 292 | .attrs = { |
| 293 | .offset = self->attr_offset, |
| 294 | .size = self->attrs * sizeof(f_attr), |
| 295 | }, |
| 296 | .data = { |
| 297 | .offset = self->data_offset, |
| 298 | .size = self->data_size, |
| 299 | }, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 300 | .event_types = { |
| 301 | .offset = self->event_offset, |
| 302 | .size = self->event_size, |
| 303 | }, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 304 | }; |
| 305 | |
Frederic Weisbecker | db9f11e | 2009-10-17 17:57:18 +0200 | [diff] [blame] | 306 | memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features)); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 307 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 308 | lseek(fd, 0, SEEK_SET); |
| 309 | do_write(fd, &f_header, sizeof(f_header)); |
| 310 | lseek(fd, self->data_offset + self->data_size, SEEK_SET); |
| 311 | |
| 312 | self->frozen = 1; |
| 313 | } |
| 314 | |
| 315 | static void do_read(int fd, void *buf, size_t size) |
| 316 | { |
| 317 | while (size) { |
| 318 | int ret = read(fd, buf, size); |
| 319 | |
| 320 | if (ret < 0) |
| 321 | die("failed to read"); |
Pierre Habouzit | 7eac7e9 | 2009-08-07 14:16:00 +0200 | [diff] [blame] | 322 | if (ret == 0) |
| 323 | die("failed to read: missing data"); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 324 | |
| 325 | size -= ret; |
| 326 | buf += ret; |
| 327 | } |
| 328 | } |
| 329 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 330 | int perf_header__process_sections(struct perf_header *self, int fd, |
| 331 | int (*process)(struct perf_file_section *self, |
| 332 | int feat, int fd)) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 333 | { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 334 | struct perf_file_section *feat_sec; |
| 335 | int nr_sections; |
| 336 | int sec_size; |
| 337 | int idx = 0; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 338 | int err = 0, feat = 1; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 339 | |
| 340 | nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS); |
| 341 | if (!nr_sections) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 342 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 343 | |
| 344 | feat_sec = calloc(sizeof(*feat_sec), nr_sections); |
| 345 | if (!feat_sec) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 346 | return -1; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 347 | |
| 348 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 349 | |
| 350 | lseek(fd, self->data_offset + self->data_size, SEEK_SET); |
| 351 | |
| 352 | do_read(fd, feat_sec, sec_size); |
| 353 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 354 | while (idx < nr_sections && feat < HEADER_LAST_FEATURE) { |
| 355 | if (perf_header__has_feat(self, feat)) { |
| 356 | struct perf_file_section *sec = &feat_sec[idx++]; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 357 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 358 | err = process(sec, feat, fd); |
| 359 | if (err < 0) |
| 360 | break; |
| 361 | } |
| 362 | ++feat; |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 363 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 364 | |
| 365 | free(feat_sec); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 366 | return err; |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 367 | }; |
| 368 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 369 | int perf_file_header__read(struct perf_file_header *self, |
| 370 | struct perf_header *ph, int fd) |
| 371 | { |
| 372 | lseek(fd, 0, SEEK_SET); |
| 373 | do_read(fd, self, sizeof(*self)); |
| 374 | |
| 375 | if (self->magic != PERF_MAGIC || |
| 376 | self->attr_size != sizeof(struct perf_file_attr)) |
| 377 | return -1; |
| 378 | |
| 379 | if (self->size != sizeof(*self)) { |
| 380 | /* Support the previous format */ |
| 381 | if (self->size == offsetof(typeof(*self), adds_features)) |
| 382 | bitmap_zero(self->adds_features, HEADER_FEAT_BITS); |
| 383 | else |
| 384 | return -1; |
| 385 | } |
| 386 | |
| 387 | memcpy(&ph->adds_features, &self->adds_features, |
| 388 | sizeof(self->adds_features)); |
| 389 | |
| 390 | ph->event_offset = self->event_types.offset; |
| 391 | ph->event_size = self->event_types.size; |
| 392 | ph->data_offset = self->data.offset; |
| 393 | ph->data_size = self->data.size; |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int perf_file_section__process(struct perf_file_section *self, |
| 398 | int feat, int fd) |
| 399 | { |
| 400 | if (lseek(fd, self->offset, SEEK_SET) < 0) { |
| 401 | pr_debug("Failed to lseek to %Ld offset for feature %d, " |
| 402 | "continuing...\n", self->offset, feat); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | switch (feat) { |
| 407 | case HEADER_TRACE_INFO: |
| 408 | trace_report(fd); |
| 409 | break; |
| 410 | |
| 411 | case HEADER_BUILD_ID: |
| 412 | if (perf_header__read_build_ids(fd, self->offset, self->size)) |
| 413 | pr_debug("Failed to read buildids, continuing...\n"); |
| 414 | break; |
| 415 | default: |
| 416 | pr_debug("unknown feature %d, continuing...\n", feat); |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 422 | struct perf_header *perf_header__read(int fd) |
| 423 | { |
| 424 | struct perf_header *self = perf_header__new(); |
| 425 | struct perf_file_header f_header; |
| 426 | struct perf_file_attr f_attr; |
| 427 | u64 f_id; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 428 | int nr_attrs, nr_ids, i, j; |
| 429 | |
Arnaldo Carvalho de Melo | a9a70bb | 2009-11-17 01:18:11 -0200 | [diff] [blame^] | 430 | if (self == NULL) |
| 431 | die("nomem"); |
| 432 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 433 | if (perf_file_header__read(&f_header, self, fd) < 0) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 434 | die("incompatible file format"); |
| 435 | |
| 436 | nr_attrs = f_header.attrs.size / sizeof(f_attr); |
| 437 | lseek(fd, f_header.attrs.offset, SEEK_SET); |
| 438 | |
| 439 | for (i = 0; i < nr_attrs; i++) { |
| 440 | struct perf_header_attr *attr; |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 441 | off_t tmp; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 442 | |
| 443 | do_read(fd, &f_attr, sizeof(f_attr)); |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 444 | tmp = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 445 | |
| 446 | attr = perf_header_attr__new(&f_attr.attr); |
Arnaldo Carvalho de Melo | dc79c0f | 2009-11-16 19:30:26 -0200 | [diff] [blame] | 447 | if (attr == NULL) |
| 448 | die("nomem"); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 449 | |
| 450 | nr_ids = f_attr.ids.size / sizeof(u64); |
| 451 | lseek(fd, f_attr.ids.offset, SEEK_SET); |
| 452 | |
| 453 | for (j = 0; j < nr_ids; j++) { |
| 454 | do_read(fd, &f_id, sizeof(f_id)); |
| 455 | |
Arnaldo Carvalho de Melo | 5875412 | 2009-11-17 01:18:10 -0200 | [diff] [blame] | 456 | if (perf_header_attr__add_id(attr, f_id) < 0) |
| 457 | die("nomem"); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 458 | } |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 459 | if (perf_header__add_attr(self, attr) < 0) |
| 460 | die("nomem"); |
| 461 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 462 | lseek(fd, tmp, SEEK_SET); |
| 463 | } |
| 464 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 465 | if (f_header.event_types.size) { |
| 466 | lseek(fd, f_header.event_types.offset, SEEK_SET); |
| 467 | events = malloc(f_header.event_types.size); |
| 468 | if (!events) |
| 469 | die("nomem"); |
| 470 | do_read(fd, events, f_header.event_types.size); |
| 471 | event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); |
| 472 | } |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 473 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 474 | perf_header__process_sections(self, fd, perf_file_section__process); |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 475 | |
Ingo Molnar | 2e01d17 | 2009-09-03 16:21:11 +0200 | [diff] [blame] | 476 | lseek(fd, self->data_offset, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 477 | |
| 478 | self->frozen = 1; |
| 479 | |
| 480 | return self; |
| 481 | } |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 482 | |
| 483 | u64 perf_header__sample_type(struct perf_header *header) |
| 484 | { |
| 485 | u64 type = 0; |
| 486 | int i; |
| 487 | |
| 488 | for (i = 0; i < header->attrs; i++) { |
| 489 | struct perf_header_attr *attr = header->attr[i]; |
| 490 | |
| 491 | if (!type) |
| 492 | type = attr->attr.sample_type; |
| 493 | else if (type != attr->attr.sample_type) |
| 494 | die("non matching sample_type"); |
| 495 | } |
| 496 | |
| 497 | return type; |
| 498 | } |
| 499 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 500 | struct perf_event_attr * |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 501 | perf_header__find_attr(u64 id, struct perf_header *header) |
| 502 | { |
| 503 | int i; |
| 504 | |
| 505 | for (i = 0; i < header->attrs; i++) { |
| 506 | struct perf_header_attr *attr = header->attr[i]; |
| 507 | int j; |
| 508 | |
| 509 | for (j = 0; j < attr->ids; j++) { |
| 510 | if (attr->id[j] == id) |
| 511 | return &attr->attr; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | return NULL; |
| 516 | } |