Xiao Guangrong | b8f46c5 | 2010-02-03 11:53:14 +0800 | [diff] [blame] | 1 | #define _FILE_OFFSET_BITS 64 |
| 2 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 3 | #include <linux/kernel.h> |
| 4 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 5 | #include <byteswap.h> |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | #include <sys/types.h> |
| 8 | |
| 9 | #include "session.h" |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 10 | #include "sort.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 11 | #include "util.h" |
| 12 | |
| 13 | static int perf_session__open(struct perf_session *self, bool force) |
| 14 | { |
| 15 | struct stat input_stat; |
| 16 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 17 | if (!strcmp(self->filename, "-")) { |
| 18 | self->fd_pipe = true; |
| 19 | self->fd = STDIN_FILENO; |
| 20 | |
| 21 | if (perf_header__read(self, self->fd) < 0) |
| 22 | pr_err("incompatible file format"); |
| 23 | |
| 24 | return 0; |
| 25 | } |
| 26 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 27 | self->fd = open(self->filename, O_RDONLY); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 28 | if (self->fd < 0) { |
| 29 | pr_err("failed to open file: %s", self->filename); |
| 30 | if (!strcmp(self->filename, "perf.data")) |
| 31 | pr_err(" (try 'perf record' first)"); |
| 32 | pr_err("\n"); |
| 33 | return -errno; |
| 34 | } |
| 35 | |
| 36 | if (fstat(self->fd, &input_stat) < 0) |
| 37 | goto out_close; |
| 38 | |
| 39 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { |
| 40 | pr_err("file %s not owned by current user or root\n", |
| 41 | self->filename); |
| 42 | goto out_close; |
| 43 | } |
| 44 | |
| 45 | if (!input_stat.st_size) { |
| 46 | pr_info("zero-sized file (%s), nothing to do!\n", |
| 47 | self->filename); |
| 48 | goto out_close; |
| 49 | } |
| 50 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 51 | if (perf_header__read(self, self->fd) < 0) { |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 52 | pr_err("incompatible file format"); |
| 53 | goto out_close; |
| 54 | } |
| 55 | |
| 56 | self->size = input_stat.st_size; |
| 57 | return 0; |
| 58 | |
| 59 | out_close: |
| 60 | close(self->fd); |
| 61 | self->fd = -1; |
| 62 | return -1; |
| 63 | } |
| 64 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 65 | void perf_session__update_sample_type(struct perf_session *self) |
| 66 | { |
| 67 | self->sample_type = perf_header__sample_type(&self->header); |
| 68 | } |
| 69 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 70 | struct perf_session *perf_session__new(const char *filename, int mode, bool force) |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 71 | { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 72 | size_t len = filename ? strlen(filename) + 1 : 0; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 73 | struct perf_session *self = zalloc(sizeof(*self) + len); |
| 74 | |
| 75 | if (self == NULL) |
| 76 | goto out; |
| 77 | |
| 78 | if (perf_header__init(&self->header) < 0) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 79 | goto out_free; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 80 | |
| 81 | memcpy(self->filename, filename, len); |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 82 | self->threads = RB_ROOT; |
Eric B Munson | cb8f093 | 2010-03-05 12:51:07 -0300 | [diff] [blame] | 83 | self->stats_by_id = RB_ROOT; |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 84 | self->last_match = NULL; |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 85 | self->mmap_window = 32; |
| 86 | self->cwd = NULL; |
| 87 | self->cwdlen = 0; |
Arnaldo Carvalho de Melo | 31d337c | 2009-12-27 21:37:03 -0200 | [diff] [blame] | 88 | self->unknown_events = 0; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 89 | map_groups__init(&self->kmaps); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 90 | |
Arnaldo Carvalho de Melo | 64abebf | 2010-01-27 21:05:52 -0200 | [diff] [blame] | 91 | if (mode == O_RDONLY) { |
| 92 | if (perf_session__open(self, force) < 0) |
| 93 | goto out_delete; |
| 94 | } else if (mode == O_WRONLY) { |
| 95 | /* |
| 96 | * In O_RDONLY mode this will be performed when reading the |
| 97 | * kernel MMAP event, in event__process_mmap(). |
| 98 | */ |
| 99 | if (perf_session__create_kernel_maps(self) < 0) |
| 100 | goto out_delete; |
| 101 | } |
Arnaldo Carvalho de Melo | d549c76 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 102 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 103 | perf_session__update_sample_type(self); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 104 | out: |
| 105 | return self; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 106 | out_free: |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 107 | free(self); |
| 108 | return NULL; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 109 | out_delete: |
| 110 | perf_session__delete(self); |
| 111 | return NULL; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void perf_session__delete(struct perf_session *self) |
| 115 | { |
| 116 | perf_header__exit(&self->header); |
| 117 | close(self->fd); |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 118 | free(self->cwd); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 119 | free(self); |
| 120 | } |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 121 | |
| 122 | static bool symbol__match_parent_regex(struct symbol *sym) |
| 123 | { |
| 124 | if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) |
| 125 | return 1; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 130 | struct map_symbol *perf_session__resolve_callchain(struct perf_session *self, |
| 131 | struct thread *thread, |
| 132 | struct ip_callchain *chain, |
| 133 | struct symbol **parent) |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 134 | { |
| 135 | u8 cpumode = PERF_RECORD_MISC_USER; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 136 | unsigned int i; |
Arnaldo Carvalho de Melo | ad5b217 | 2010-04-02 10:04:18 -0300 | [diff] [blame] | 137 | struct map_symbol *syms = calloc(chain->nr, sizeof(*syms)); |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 138 | |
Arnaldo Carvalho de Melo | ad5b217 | 2010-04-02 10:04:18 -0300 | [diff] [blame] | 139 | if (!syms) |
| 140 | return NULL; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 141 | |
| 142 | for (i = 0; i < chain->nr; i++) { |
| 143 | u64 ip = chain->ips[i]; |
| 144 | struct addr_location al; |
| 145 | |
| 146 | if (ip >= PERF_CONTEXT_MAX) { |
| 147 | switch (ip) { |
| 148 | case PERF_CONTEXT_HV: |
| 149 | cpumode = PERF_RECORD_MISC_HYPERVISOR; break; |
| 150 | case PERF_CONTEXT_KERNEL: |
| 151 | cpumode = PERF_RECORD_MISC_KERNEL; break; |
| 152 | case PERF_CONTEXT_USER: |
| 153 | cpumode = PERF_RECORD_MISC_USER; break; |
| 154 | default: |
| 155 | break; |
| 156 | } |
| 157 | continue; |
| 158 | } |
| 159 | |
| 160 | thread__find_addr_location(thread, self, cpumode, |
| 161 | MAP__FUNCTION, ip, &al, NULL); |
| 162 | if (al.sym != NULL) { |
| 163 | if (sort__has_parent && !*parent && |
| 164 | symbol__match_parent_regex(al.sym)) |
| 165 | *parent = al.sym; |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 166 | if (!symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 167 | break; |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 168 | syms[i].map = al.map; |
| 169 | syms[i].sym = al.sym; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | return syms; |
| 174 | } |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 175 | |
| 176 | static int process_event_stub(event_t *event __used, |
| 177 | struct perf_session *session __used) |
| 178 | { |
| 179 | dump_printf(": unhandled!\n"); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static void perf_event_ops__fill_defaults(struct perf_event_ops *handler) |
| 184 | { |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 185 | if (handler->sample == NULL) |
| 186 | handler->sample = process_event_stub; |
| 187 | if (handler->mmap == NULL) |
| 188 | handler->mmap = process_event_stub; |
| 189 | if (handler->comm == NULL) |
| 190 | handler->comm = process_event_stub; |
| 191 | if (handler->fork == NULL) |
| 192 | handler->fork = process_event_stub; |
| 193 | if (handler->exit == NULL) |
| 194 | handler->exit = process_event_stub; |
| 195 | if (handler->lost == NULL) |
| 196 | handler->lost = process_event_stub; |
| 197 | if (handler->read == NULL) |
| 198 | handler->read = process_event_stub; |
| 199 | if (handler->throttle == NULL) |
| 200 | handler->throttle = process_event_stub; |
| 201 | if (handler->unthrottle == NULL) |
| 202 | handler->unthrottle = process_event_stub; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 203 | if (handler->attr == NULL) |
| 204 | handler->attr = process_event_stub; |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 205 | if (handler->event_type == NULL) |
| 206 | handler->event_type = process_event_stub; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 207 | if (handler->tracing_data == NULL) |
| 208 | handler->tracing_data = process_event_stub; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame^] | 209 | if (handler->build_id == NULL) |
| 210 | handler->build_id = process_event_stub; |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static const char *event__name[] = { |
| 214 | [0] = "TOTAL", |
| 215 | [PERF_RECORD_MMAP] = "MMAP", |
| 216 | [PERF_RECORD_LOST] = "LOST", |
| 217 | [PERF_RECORD_COMM] = "COMM", |
| 218 | [PERF_RECORD_EXIT] = "EXIT", |
| 219 | [PERF_RECORD_THROTTLE] = "THROTTLE", |
| 220 | [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", |
| 221 | [PERF_RECORD_FORK] = "FORK", |
| 222 | [PERF_RECORD_READ] = "READ", |
| 223 | [PERF_RECORD_SAMPLE] = "SAMPLE", |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 224 | [PERF_RECORD_HEADER_ATTR] = "ATTR", |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 225 | [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 226 | [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA", |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame^] | 227 | [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID", |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 228 | }; |
| 229 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 230 | unsigned long event__total[PERF_RECORD_HEADER_MAX]; |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 231 | |
| 232 | void event__print_totals(void) |
| 233 | { |
| 234 | int i; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 235 | for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) { |
| 236 | if (!event__name[i]) |
| 237 | continue; |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 238 | pr_info("%10s events: %10ld\n", |
| 239 | event__name[i], event__total[i]); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 240 | } |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 241 | } |
| 242 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 243 | void mem_bswap_64(void *src, int byte_size) |
| 244 | { |
| 245 | u64 *m = src; |
| 246 | |
| 247 | while (byte_size > 0) { |
| 248 | *m = bswap_64(*m); |
| 249 | byte_size -= sizeof(u64); |
| 250 | ++m; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static void event__all64_swap(event_t *self) |
| 255 | { |
| 256 | struct perf_event_header *hdr = &self->header; |
| 257 | mem_bswap_64(hdr + 1, self->header.size - sizeof(*hdr)); |
| 258 | } |
| 259 | |
| 260 | static void event__comm_swap(event_t *self) |
| 261 | { |
| 262 | self->comm.pid = bswap_32(self->comm.pid); |
| 263 | self->comm.tid = bswap_32(self->comm.tid); |
| 264 | } |
| 265 | |
| 266 | static void event__mmap_swap(event_t *self) |
| 267 | { |
| 268 | self->mmap.pid = bswap_32(self->mmap.pid); |
| 269 | self->mmap.tid = bswap_32(self->mmap.tid); |
| 270 | self->mmap.start = bswap_64(self->mmap.start); |
| 271 | self->mmap.len = bswap_64(self->mmap.len); |
| 272 | self->mmap.pgoff = bswap_64(self->mmap.pgoff); |
| 273 | } |
| 274 | |
| 275 | static void event__task_swap(event_t *self) |
| 276 | { |
| 277 | self->fork.pid = bswap_32(self->fork.pid); |
| 278 | self->fork.tid = bswap_32(self->fork.tid); |
| 279 | self->fork.ppid = bswap_32(self->fork.ppid); |
| 280 | self->fork.ptid = bswap_32(self->fork.ptid); |
| 281 | self->fork.time = bswap_64(self->fork.time); |
| 282 | } |
| 283 | |
| 284 | static void event__read_swap(event_t *self) |
| 285 | { |
| 286 | self->read.pid = bswap_32(self->read.pid); |
| 287 | self->read.tid = bswap_32(self->read.tid); |
| 288 | self->read.value = bswap_64(self->read.value); |
| 289 | self->read.time_enabled = bswap_64(self->read.time_enabled); |
| 290 | self->read.time_running = bswap_64(self->read.time_running); |
| 291 | self->read.id = bswap_64(self->read.id); |
| 292 | } |
| 293 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 294 | static void event__attr_swap(event_t *self) |
| 295 | { |
| 296 | size_t size; |
| 297 | |
| 298 | self->attr.attr.type = bswap_32(self->attr.attr.type); |
| 299 | self->attr.attr.size = bswap_32(self->attr.attr.size); |
| 300 | self->attr.attr.config = bswap_64(self->attr.attr.config); |
| 301 | self->attr.attr.sample_period = bswap_64(self->attr.attr.sample_period); |
| 302 | self->attr.attr.sample_type = bswap_64(self->attr.attr.sample_type); |
| 303 | self->attr.attr.read_format = bswap_64(self->attr.attr.read_format); |
| 304 | self->attr.attr.wakeup_events = bswap_32(self->attr.attr.wakeup_events); |
| 305 | self->attr.attr.bp_type = bswap_32(self->attr.attr.bp_type); |
| 306 | self->attr.attr.bp_addr = bswap_64(self->attr.attr.bp_addr); |
| 307 | self->attr.attr.bp_len = bswap_64(self->attr.attr.bp_len); |
| 308 | |
| 309 | size = self->header.size; |
| 310 | size -= (void *)&self->attr.id - (void *)self; |
| 311 | mem_bswap_64(self->attr.id, size); |
| 312 | } |
| 313 | |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 314 | static void event__event_type_swap(event_t *self) |
| 315 | { |
| 316 | self->event_type.event_type.event_id = |
| 317 | bswap_64(self->event_type.event_type.event_id); |
| 318 | } |
| 319 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 320 | static void event__tracing_data_swap(event_t *self) |
| 321 | { |
| 322 | self->tracing_data.size = bswap_32(self->tracing_data.size); |
| 323 | } |
| 324 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 325 | typedef void (*event__swap_op)(event_t *self); |
| 326 | |
| 327 | static event__swap_op event__swap_ops[] = { |
| 328 | [PERF_RECORD_MMAP] = event__mmap_swap, |
| 329 | [PERF_RECORD_COMM] = event__comm_swap, |
| 330 | [PERF_RECORD_FORK] = event__task_swap, |
| 331 | [PERF_RECORD_EXIT] = event__task_swap, |
| 332 | [PERF_RECORD_LOST] = event__all64_swap, |
| 333 | [PERF_RECORD_READ] = event__read_swap, |
| 334 | [PERF_RECORD_SAMPLE] = event__all64_swap, |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 335 | [PERF_RECORD_HEADER_ATTR] = event__attr_swap, |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 336 | [PERF_RECORD_HEADER_EVENT_TYPE] = event__event_type_swap, |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 337 | [PERF_RECORD_HEADER_TRACING_DATA] = event__tracing_data_swap, |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame^] | 338 | [PERF_RECORD_HEADER_BUILD_ID] = NULL, |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 339 | [PERF_RECORD_HEADER_MAX] = NULL, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 340 | }; |
| 341 | |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 342 | static int perf_session__process_event(struct perf_session *self, |
| 343 | event_t *event, |
| 344 | struct perf_event_ops *ops, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 345 | u64 offset, u64 head) |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 346 | { |
| 347 | trace_event(event); |
| 348 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 349 | if (event->header.type < PERF_RECORD_HEADER_MAX) { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 350 | dump_printf("%#Lx [%#x]: PERF_RECORD_%s", |
Arnaldo Carvalho de Melo | 0d75503 | 2010-01-14 12:23:09 -0200 | [diff] [blame] | 351 | offset + head, event->header.size, |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 352 | event__name[event->header.type]); |
| 353 | ++event__total[0]; |
| 354 | ++event__total[event->header.type]; |
| 355 | } |
| 356 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 357 | if (self->header.needs_swap && event__swap_ops[event->header.type]) |
| 358 | event__swap_ops[event->header.type](event); |
| 359 | |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 360 | switch (event->header.type) { |
| 361 | case PERF_RECORD_SAMPLE: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 362 | return ops->sample(event, self); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 363 | case PERF_RECORD_MMAP: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 364 | return ops->mmap(event, self); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 365 | case PERF_RECORD_COMM: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 366 | return ops->comm(event, self); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 367 | case PERF_RECORD_FORK: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 368 | return ops->fork(event, self); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 369 | case PERF_RECORD_EXIT: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 370 | return ops->exit(event, self); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 371 | case PERF_RECORD_LOST: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 372 | return ops->lost(event, self); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 373 | case PERF_RECORD_READ: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 374 | return ops->read(event, self); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 375 | case PERF_RECORD_THROTTLE: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 376 | return ops->throttle(event, self); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 377 | case PERF_RECORD_UNTHROTTLE: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 378 | return ops->unthrottle(event, self); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 379 | case PERF_RECORD_HEADER_ATTR: |
| 380 | return ops->attr(event, self); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 381 | case PERF_RECORD_HEADER_EVENT_TYPE: |
| 382 | return ops->event_type(event, self); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 383 | case PERF_RECORD_HEADER_TRACING_DATA: |
| 384 | /* setup for reading amidst mmap */ |
| 385 | lseek(self->fd, offset + head, SEEK_SET); |
| 386 | return ops->tracing_data(event, self); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame^] | 387 | case PERF_RECORD_HEADER_BUILD_ID: |
| 388 | return ops->build_id(event, self); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 389 | default: |
Arnaldo Carvalho de Melo | 31d337c | 2009-12-27 21:37:03 -0200 | [diff] [blame] | 390 | self->unknown_events++; |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 391 | return -1; |
| 392 | } |
| 393 | } |
| 394 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 395 | void perf_event_header__bswap(struct perf_event_header *self) |
| 396 | { |
| 397 | self->type = bswap_32(self->type); |
| 398 | self->misc = bswap_16(self->misc); |
| 399 | self->size = bswap_16(self->size); |
| 400 | } |
| 401 | |
| 402 | int perf_header__read_build_ids(struct perf_header *self, |
| 403 | int input, u64 offset, u64 size) |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 404 | { |
| 405 | struct build_id_event bev; |
| 406 | char filename[PATH_MAX]; |
| 407 | u64 limit = offset + size; |
| 408 | int err = -1; |
| 409 | |
| 410 | while (offset < limit) { |
| 411 | struct dso *dso; |
| 412 | ssize_t len; |
Arnaldo Carvalho de Melo | a89e5ab | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 413 | struct list_head *head = &dsos__user; |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 414 | |
| 415 | if (read(input, &bev, sizeof(bev)) != sizeof(bev)) |
| 416 | goto out; |
| 417 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 418 | if (self->needs_swap) |
| 419 | perf_event_header__bswap(&bev.header); |
| 420 | |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 421 | len = bev.header.size - sizeof(bev); |
| 422 | if (read(input, filename, len) != len) |
| 423 | goto out; |
| 424 | |
Arnaldo Carvalho de Melo | a89e5ab | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 425 | if (bev.header.misc & PERF_RECORD_MISC_KERNEL) |
| 426 | head = &dsos__kernel; |
| 427 | |
| 428 | dso = __dsos__findnew(head, filename); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 429 | if (dso != NULL) { |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 430 | dso__set_build_id(dso, &bev.build_id); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 431 | if (head == &dsos__kernel && filename[0] == '[') |
| 432 | dso->kernel = 1; |
| 433 | } |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 434 | |
| 435 | offset += bev.header.size; |
| 436 | } |
| 437 | err = 0; |
| 438 | out: |
| 439 | return err; |
| 440 | } |
| 441 | |
| 442 | static struct thread *perf_session__register_idle_thread(struct perf_session *self) |
| 443 | { |
| 444 | struct thread *thread = perf_session__findnew(self, 0); |
| 445 | |
| 446 | if (thread == NULL || thread__set_comm(thread, "swapper")) { |
| 447 | pr_err("problem inserting idle task.\n"); |
| 448 | thread = NULL; |
| 449 | } |
| 450 | |
| 451 | return thread; |
| 452 | } |
| 453 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 454 | int do_read(int fd, void *buf, size_t size) |
| 455 | { |
| 456 | void *buf_start = buf; |
| 457 | |
| 458 | while (size) { |
| 459 | int ret = read(fd, buf, size); |
| 460 | |
| 461 | if (ret <= 0) |
| 462 | return ret; |
| 463 | |
| 464 | size -= ret; |
| 465 | buf += ret; |
| 466 | } |
| 467 | |
| 468 | return buf - buf_start; |
| 469 | } |
| 470 | |
| 471 | #define session_done() (*(volatile int *)(&session_done)) |
| 472 | volatile int session_done; |
| 473 | |
| 474 | static int __perf_session__process_pipe_events(struct perf_session *self, |
| 475 | struct perf_event_ops *ops) |
| 476 | { |
| 477 | event_t event; |
| 478 | uint32_t size; |
| 479 | int skip = 0; |
| 480 | u64 head; |
| 481 | int err; |
| 482 | void *p; |
| 483 | |
| 484 | perf_event_ops__fill_defaults(ops); |
| 485 | |
| 486 | head = 0; |
| 487 | more: |
| 488 | err = do_read(self->fd, &event, sizeof(struct perf_event_header)); |
| 489 | if (err <= 0) { |
| 490 | if (err == 0) |
| 491 | goto done; |
| 492 | |
| 493 | pr_err("failed to read event header\n"); |
| 494 | goto out_err; |
| 495 | } |
| 496 | |
| 497 | if (self->header.needs_swap) |
| 498 | perf_event_header__bswap(&event.header); |
| 499 | |
| 500 | size = event.header.size; |
| 501 | if (size == 0) |
| 502 | size = 8; |
| 503 | |
| 504 | p = &event; |
| 505 | p += sizeof(struct perf_event_header); |
| 506 | |
| 507 | err = do_read(self->fd, p, size - sizeof(struct perf_event_header)); |
| 508 | if (err <= 0) { |
| 509 | if (err == 0) { |
| 510 | pr_err("unexpected end of event stream\n"); |
| 511 | goto done; |
| 512 | } |
| 513 | |
| 514 | pr_err("failed to read event data\n"); |
| 515 | goto out_err; |
| 516 | } |
| 517 | |
| 518 | if (size == 0 || |
| 519 | (skip = perf_session__process_event(self, &event, ops, |
| 520 | 0, head)) < 0) { |
| 521 | dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n", |
| 522 | head, event.header.size, event.header.type); |
| 523 | /* |
| 524 | * assume we lost track of the stream, check alignment, and |
| 525 | * increment a single u64 in the hope to catch on again 'soon'. |
| 526 | */ |
| 527 | if (unlikely(head & 7)) |
| 528 | head &= ~7ULL; |
| 529 | |
| 530 | size = 8; |
| 531 | } |
| 532 | |
| 533 | head += size; |
| 534 | |
| 535 | dump_printf("\n%#Lx [%#x]: event: %d\n", |
| 536 | head, event.header.size, event.header.type); |
| 537 | |
| 538 | if (skip > 0) |
| 539 | head += skip; |
| 540 | |
| 541 | if (!session_done()) |
| 542 | goto more; |
| 543 | done: |
| 544 | err = 0; |
| 545 | out_err: |
| 546 | return err; |
| 547 | } |
| 548 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 549 | int __perf_session__process_events(struct perf_session *self, |
| 550 | u64 data_offset, u64 data_size, |
| 551 | u64 file_size, struct perf_event_ops *ops) |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 552 | { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 553 | int err, mmap_prot, mmap_flags; |
| 554 | u64 head, shift; |
| 555 | u64 offset = 0; |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 556 | size_t page_size; |
| 557 | event_t *event; |
| 558 | uint32_t size; |
| 559 | char *buf; |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 560 | struct ui_progress *progress = ui_progress__new("Processing events...", |
| 561 | self->size); |
| 562 | if (progress == NULL) |
| 563 | return -1; |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 564 | |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 565 | perf_event_ops__fill_defaults(ops); |
| 566 | |
Arnaldo Carvalho de Melo | 1b75962 | 2010-01-14 18:30:04 -0200 | [diff] [blame] | 567 | page_size = sysconf(_SC_PAGESIZE); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 568 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 569 | head = data_offset; |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 570 | shift = page_size * (head / page_size); |
| 571 | offset += shift; |
| 572 | head -= shift; |
| 573 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 574 | mmap_prot = PROT_READ; |
| 575 | mmap_flags = MAP_SHARED; |
| 576 | |
| 577 | if (self->header.needs_swap) { |
| 578 | mmap_prot |= PROT_WRITE; |
| 579 | mmap_flags = MAP_PRIVATE; |
| 580 | } |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 581 | remap: |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 582 | buf = mmap(NULL, page_size * self->mmap_window, mmap_prot, |
| 583 | mmap_flags, self->fd, offset); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 584 | if (buf == MAP_FAILED) { |
| 585 | pr_err("failed to mmap file\n"); |
| 586 | err = -errno; |
| 587 | goto out_err; |
| 588 | } |
| 589 | |
| 590 | more: |
| 591 | event = (event_t *)(buf + head); |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 592 | ui_progress__update(progress, offset); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 593 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 594 | if (self->header.needs_swap) |
| 595 | perf_event_header__bswap(&event->header); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 596 | size = event->header.size; |
| 597 | if (size == 0) |
| 598 | size = 8; |
| 599 | |
| 600 | if (head + event->header.size >= page_size * self->mmap_window) { |
| 601 | int munmap_ret; |
| 602 | |
| 603 | shift = page_size * (head / page_size); |
| 604 | |
| 605 | munmap_ret = munmap(buf, page_size * self->mmap_window); |
| 606 | assert(munmap_ret == 0); |
| 607 | |
| 608 | offset += shift; |
| 609 | head -= shift; |
| 610 | goto remap; |
| 611 | } |
| 612 | |
| 613 | size = event->header.size; |
| 614 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 615 | dump_printf("\n%#Lx [%#x]: event: %d\n", |
Arnaldo Carvalho de Melo | 0d75503 | 2010-01-14 12:23:09 -0200 | [diff] [blame] | 616 | offset + head, event->header.size, event->header.type); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 617 | |
| 618 | if (size == 0 || |
| 619 | perf_session__process_event(self, event, ops, offset, head) < 0) { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 620 | dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n", |
Arnaldo Carvalho de Melo | 0d75503 | 2010-01-14 12:23:09 -0200 | [diff] [blame] | 621 | offset + head, event->header.size, |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 622 | event->header.type); |
| 623 | /* |
| 624 | * assume we lost track of the stream, check alignment, and |
| 625 | * increment a single u64 in the hope to catch on again 'soon'. |
| 626 | */ |
| 627 | if (unlikely(head & 7)) |
| 628 | head &= ~7ULL; |
| 629 | |
| 630 | size = 8; |
| 631 | } |
| 632 | |
| 633 | head += size; |
| 634 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 635 | if (offset + head >= data_offset + data_size) |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 636 | goto done; |
| 637 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 638 | if (offset + head < file_size) |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 639 | goto more; |
| 640 | done: |
| 641 | err = 0; |
| 642 | out_err: |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 643 | ui_progress__delete(progress); |
Arnaldo Carvalho de Melo | 06aae59 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 644 | return err; |
| 645 | } |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 646 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 647 | int perf_session__process_events(struct perf_session *self, |
| 648 | struct perf_event_ops *ops) |
| 649 | { |
| 650 | int err; |
| 651 | |
| 652 | if (perf_session__register_idle_thread(self) == NULL) |
| 653 | return -ENOMEM; |
| 654 | |
| 655 | if (!symbol_conf.full_paths) { |
| 656 | char bf[PATH_MAX]; |
| 657 | |
| 658 | if (getcwd(bf, sizeof(bf)) == NULL) { |
| 659 | err = -errno; |
| 660 | out_getcwd_err: |
| 661 | pr_err("failed to get the current directory\n"); |
| 662 | goto out_err; |
| 663 | } |
| 664 | self->cwd = strdup(bf); |
| 665 | if (self->cwd == NULL) { |
| 666 | err = -ENOMEM; |
| 667 | goto out_getcwd_err; |
| 668 | } |
| 669 | self->cwdlen = strlen(self->cwd); |
| 670 | } |
| 671 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 672 | if (!self->fd_pipe) |
| 673 | err = __perf_session__process_events(self, |
| 674 | self->header.data_offset, |
| 675 | self->header.data_size, |
| 676 | self->size, ops); |
| 677 | else |
| 678 | err = __perf_session__process_pipe_events(self, ops); |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 679 | out_err: |
| 680 | return err; |
| 681 | } |
| 682 | |
Arnaldo Carvalho de Melo | d549c76 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 683 | bool perf_session__has_traces(struct perf_session *self, const char *msg) |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 684 | { |
| 685 | if (!(self->sample_type & PERF_SAMPLE_RAW)) { |
Arnaldo Carvalho de Melo | d549c76 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 686 | pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); |
| 687 | return false; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 688 | } |
| 689 | |
Arnaldo Carvalho de Melo | d549c76 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 690 | return true; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 691 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 692 | |
| 693 | int perf_session__set_kallsyms_ref_reloc_sym(struct perf_session *self, |
| 694 | const char *symbol_name, |
| 695 | u64 addr) |
| 696 | { |
| 697 | char *bracket; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 698 | enum map_type i; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 699 | |
| 700 | self->ref_reloc_sym.name = strdup(symbol_name); |
| 701 | if (self->ref_reloc_sym.name == NULL) |
| 702 | return -ENOMEM; |
| 703 | |
| 704 | bracket = strchr(self->ref_reloc_sym.name, ']'); |
| 705 | if (bracket) |
| 706 | *bracket = '\0'; |
| 707 | |
| 708 | self->ref_reloc_sym.addr = addr; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 709 | |
| 710 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
| 711 | struct kmap *kmap = map__kmap(self->vmlinux_maps[i]); |
| 712 | kmap->ref_reloc_sym = &self->ref_reloc_sym; |
| 713 | } |
| 714 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 715 | return 0; |
| 716 | } |