Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | |
| 3 | #include <unistd.h> |
| 4 | #include <sys/types.h> |
| 5 | |
| 6 | #include "session.h" |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 7 | #include "sort.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 8 | #include "util.h" |
| 9 | |
| 10 | static int perf_session__open(struct perf_session *self, bool force) |
| 11 | { |
| 12 | struct stat input_stat; |
| 13 | |
| 14 | self->fd = open(self->filename, O_RDONLY); |
| 15 | if (self->fd < 0) { |
| 16 | pr_err("failed to open file: %s", self->filename); |
| 17 | if (!strcmp(self->filename, "perf.data")) |
| 18 | pr_err(" (try 'perf record' first)"); |
| 19 | pr_err("\n"); |
| 20 | return -errno; |
| 21 | } |
| 22 | |
| 23 | if (fstat(self->fd, &input_stat) < 0) |
| 24 | goto out_close; |
| 25 | |
| 26 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { |
| 27 | pr_err("file %s not owned by current user or root\n", |
| 28 | self->filename); |
| 29 | goto out_close; |
| 30 | } |
| 31 | |
| 32 | if (!input_stat.st_size) { |
| 33 | pr_info("zero-sized file (%s), nothing to do!\n", |
| 34 | self->filename); |
| 35 | goto out_close; |
| 36 | } |
| 37 | |
| 38 | if (perf_header__read(&self->header, self->fd) < 0) { |
| 39 | pr_err("incompatible file format"); |
| 40 | goto out_close; |
| 41 | } |
| 42 | |
| 43 | self->size = input_stat.st_size; |
| 44 | return 0; |
| 45 | |
| 46 | out_close: |
| 47 | close(self->fd); |
| 48 | self->fd = -1; |
| 49 | return -1; |
| 50 | } |
| 51 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 52 | 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] | 53 | { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 54 | size_t len = filename ? strlen(filename) + 1 : 0; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 55 | struct perf_session *self = zalloc(sizeof(*self) + len); |
| 56 | |
| 57 | if (self == NULL) |
| 58 | goto out; |
| 59 | |
| 60 | if (perf_header__init(&self->header) < 0) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 61 | goto out_free; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 62 | |
| 63 | memcpy(self->filename, filename, len); |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 64 | self->threads = RB_ROOT; |
| 65 | self->last_match = NULL; |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 66 | self->mmap_window = 32; |
| 67 | self->cwd = NULL; |
| 68 | self->cwdlen = 0; |
Arnaldo Carvalho de Melo | 31d337c | 2009-12-27 21:37:03 -0200 | [diff] [blame] | 69 | self->unknown_events = 0; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 70 | map_groups__init(&self->kmaps); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 71 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 72 | if (perf_session__create_kernel_maps(self) < 0) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 73 | goto out_delete; |
| 74 | |
| 75 | if (mode == O_RDONLY && perf_session__open(self, force) < 0) |
| 76 | goto out_delete; |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 77 | |
| 78 | self->sample_type = perf_header__sample_type(&self->header); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 79 | out: |
| 80 | return self; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 81 | out_free: |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 82 | free(self); |
| 83 | return NULL; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 84 | out_delete: |
| 85 | perf_session__delete(self); |
| 86 | return NULL; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void perf_session__delete(struct perf_session *self) |
| 90 | { |
| 91 | perf_header__exit(&self->header); |
| 92 | close(self->fd); |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 93 | free(self->cwd); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 94 | free(self); |
| 95 | } |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 96 | |
| 97 | static bool symbol__match_parent_regex(struct symbol *sym) |
| 98 | { |
| 99 | if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) |
| 100 | return 1; |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | struct symbol **perf_session__resolve_callchain(struct perf_session *self, |
| 106 | struct thread *thread, |
| 107 | struct ip_callchain *chain, |
| 108 | struct symbol **parent) |
| 109 | { |
| 110 | u8 cpumode = PERF_RECORD_MISC_USER; |
| 111 | struct symbol **syms = NULL; |
| 112 | unsigned int i; |
| 113 | |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 114 | if (symbol_conf.use_callchain) { |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 115 | syms = calloc(chain->nr, sizeof(*syms)); |
| 116 | if (!syms) { |
| 117 | fprintf(stderr, "Can't allocate memory for symbols\n"); |
| 118 | exit(-1); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | for (i = 0; i < chain->nr; i++) { |
| 123 | u64 ip = chain->ips[i]; |
| 124 | struct addr_location al; |
| 125 | |
| 126 | if (ip >= PERF_CONTEXT_MAX) { |
| 127 | switch (ip) { |
| 128 | case PERF_CONTEXT_HV: |
| 129 | cpumode = PERF_RECORD_MISC_HYPERVISOR; break; |
| 130 | case PERF_CONTEXT_KERNEL: |
| 131 | cpumode = PERF_RECORD_MISC_KERNEL; break; |
| 132 | case PERF_CONTEXT_USER: |
| 133 | cpumode = PERF_RECORD_MISC_USER; break; |
| 134 | default: |
| 135 | break; |
| 136 | } |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | thread__find_addr_location(thread, self, cpumode, |
| 141 | MAP__FUNCTION, ip, &al, NULL); |
| 142 | if (al.sym != NULL) { |
| 143 | if (sort__has_parent && !*parent && |
| 144 | symbol__match_parent_regex(al.sym)) |
| 145 | *parent = al.sym; |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 146 | if (!symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 147 | break; |
| 148 | syms[i] = al.sym; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return syms; |
| 153 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 154 | |
| 155 | static int process_event_stub(event_t *event __used, |
| 156 | struct perf_session *session __used) |
| 157 | { |
| 158 | dump_printf(": unhandled!\n"); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static void perf_event_ops__fill_defaults(struct perf_event_ops *handler) |
| 163 | { |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame^] | 164 | if (handler->sample == NULL) |
| 165 | handler->sample = process_event_stub; |
| 166 | if (handler->mmap == NULL) |
| 167 | handler->mmap = process_event_stub; |
| 168 | if (handler->comm == NULL) |
| 169 | handler->comm = process_event_stub; |
| 170 | if (handler->fork == NULL) |
| 171 | handler->fork = process_event_stub; |
| 172 | if (handler->exit == NULL) |
| 173 | handler->exit = process_event_stub; |
| 174 | if (handler->lost == NULL) |
| 175 | handler->lost = process_event_stub; |
| 176 | if (handler->read == NULL) |
| 177 | handler->read = process_event_stub; |
| 178 | if (handler->throttle == NULL) |
| 179 | handler->throttle = process_event_stub; |
| 180 | if (handler->unthrottle == NULL) |
| 181 | handler->unthrottle = process_event_stub; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static const char *event__name[] = { |
| 185 | [0] = "TOTAL", |
| 186 | [PERF_RECORD_MMAP] = "MMAP", |
| 187 | [PERF_RECORD_LOST] = "LOST", |
| 188 | [PERF_RECORD_COMM] = "COMM", |
| 189 | [PERF_RECORD_EXIT] = "EXIT", |
| 190 | [PERF_RECORD_THROTTLE] = "THROTTLE", |
| 191 | [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", |
| 192 | [PERF_RECORD_FORK] = "FORK", |
| 193 | [PERF_RECORD_READ] = "READ", |
| 194 | [PERF_RECORD_SAMPLE] = "SAMPLE", |
| 195 | }; |
| 196 | |
| 197 | unsigned long event__total[PERF_RECORD_MAX]; |
| 198 | |
| 199 | void event__print_totals(void) |
| 200 | { |
| 201 | int i; |
| 202 | for (i = 0; i < PERF_RECORD_MAX; ++i) |
| 203 | pr_info("%10s events: %10ld\n", |
| 204 | event__name[i], event__total[i]); |
| 205 | } |
| 206 | |
| 207 | static int perf_session__process_event(struct perf_session *self, |
| 208 | event_t *event, |
| 209 | struct perf_event_ops *ops, |
| 210 | unsigned long offset, unsigned long head) |
| 211 | { |
| 212 | trace_event(event); |
| 213 | |
| 214 | if (event->header.type < PERF_RECORD_MAX) { |
| 215 | dump_printf("%p [%p]: PERF_RECORD_%s", |
| 216 | (void *)(offset + head), |
| 217 | (void *)(long)(event->header.size), |
| 218 | event__name[event->header.type]); |
| 219 | ++event__total[0]; |
| 220 | ++event__total[event->header.type]; |
| 221 | } |
| 222 | |
| 223 | switch (event->header.type) { |
| 224 | case PERF_RECORD_SAMPLE: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame^] | 225 | return ops->sample(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 226 | case PERF_RECORD_MMAP: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame^] | 227 | return ops->mmap(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 228 | case PERF_RECORD_COMM: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame^] | 229 | return ops->comm(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 230 | case PERF_RECORD_FORK: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame^] | 231 | return ops->fork(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 232 | case PERF_RECORD_EXIT: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame^] | 233 | return ops->exit(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 234 | case PERF_RECORD_LOST: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame^] | 235 | return ops->lost(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 236 | case PERF_RECORD_READ: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame^] | 237 | return ops->read(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 238 | case PERF_RECORD_THROTTLE: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame^] | 239 | return ops->throttle(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 240 | case PERF_RECORD_UNTHROTTLE: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame^] | 241 | return ops->unthrottle(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 242 | default: |
Arnaldo Carvalho de Melo | 31d337c | 2009-12-27 21:37:03 -0200 | [diff] [blame] | 243 | self->unknown_events++; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 244 | return -1; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | int perf_header__read_build_ids(int input, u64 offset, u64 size) |
| 249 | { |
| 250 | struct build_id_event bev; |
| 251 | char filename[PATH_MAX]; |
| 252 | u64 limit = offset + size; |
| 253 | int err = -1; |
| 254 | |
| 255 | while (offset < limit) { |
| 256 | struct dso *dso; |
| 257 | ssize_t len; |
| 258 | |
| 259 | if (read(input, &bev, sizeof(bev)) != sizeof(bev)) |
| 260 | goto out; |
| 261 | |
| 262 | len = bev.header.size - sizeof(bev); |
| 263 | if (read(input, filename, len) != len) |
| 264 | goto out; |
| 265 | |
| 266 | dso = dsos__findnew(filename); |
| 267 | if (dso != NULL) |
| 268 | dso__set_build_id(dso, &bev.build_id); |
| 269 | |
| 270 | offset += bev.header.size; |
| 271 | } |
| 272 | err = 0; |
| 273 | out: |
| 274 | return err; |
| 275 | } |
| 276 | |
| 277 | static struct thread *perf_session__register_idle_thread(struct perf_session *self) |
| 278 | { |
| 279 | struct thread *thread = perf_session__findnew(self, 0); |
| 280 | |
| 281 | if (thread == NULL || thread__set_comm(thread, "swapper")) { |
| 282 | pr_err("problem inserting idle task.\n"); |
| 283 | thread = NULL; |
| 284 | } |
| 285 | |
| 286 | return thread; |
| 287 | } |
| 288 | |
| 289 | int perf_session__process_events(struct perf_session *self, |
| 290 | struct perf_event_ops *ops) |
| 291 | { |
| 292 | int err; |
| 293 | unsigned long head, shift; |
| 294 | unsigned long offset = 0; |
| 295 | size_t page_size; |
| 296 | event_t *event; |
| 297 | uint32_t size; |
| 298 | char *buf; |
| 299 | |
| 300 | if (perf_session__register_idle_thread(self) == NULL) |
| 301 | return -ENOMEM; |
| 302 | |
| 303 | perf_event_ops__fill_defaults(ops); |
| 304 | |
| 305 | page_size = getpagesize(); |
| 306 | |
| 307 | head = self->header.data_offset; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 308 | |
Arnaldo Carvalho de Melo | f7d8744 | 2009-12-27 21:37:04 -0200 | [diff] [blame] | 309 | if (!symbol_conf.full_paths) { |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 310 | char bf[PATH_MAX]; |
| 311 | |
| 312 | if (getcwd(bf, sizeof(bf)) == NULL) { |
| 313 | err = -errno; |
| 314 | out_getcwd_err: |
| 315 | pr_err("failed to get the current directory\n"); |
| 316 | goto out_err; |
| 317 | } |
| 318 | self->cwd = strdup(bf); |
| 319 | if (self->cwd == NULL) { |
| 320 | err = -ENOMEM; |
| 321 | goto out_getcwd_err; |
| 322 | } |
| 323 | self->cwdlen = strlen(self->cwd); |
| 324 | } |
| 325 | |
| 326 | shift = page_size * (head / page_size); |
| 327 | offset += shift; |
| 328 | head -= shift; |
| 329 | |
| 330 | remap: |
| 331 | buf = mmap(NULL, page_size * self->mmap_window, PROT_READ, |
| 332 | MAP_SHARED, self->fd, offset); |
| 333 | if (buf == MAP_FAILED) { |
| 334 | pr_err("failed to mmap file\n"); |
| 335 | err = -errno; |
| 336 | goto out_err; |
| 337 | } |
| 338 | |
| 339 | more: |
| 340 | event = (event_t *)(buf + head); |
| 341 | |
| 342 | size = event->header.size; |
| 343 | if (size == 0) |
| 344 | size = 8; |
| 345 | |
| 346 | if (head + event->header.size >= page_size * self->mmap_window) { |
| 347 | int munmap_ret; |
| 348 | |
| 349 | shift = page_size * (head / page_size); |
| 350 | |
| 351 | munmap_ret = munmap(buf, page_size * self->mmap_window); |
| 352 | assert(munmap_ret == 0); |
| 353 | |
| 354 | offset += shift; |
| 355 | head -= shift; |
| 356 | goto remap; |
| 357 | } |
| 358 | |
| 359 | size = event->header.size; |
| 360 | |
| 361 | dump_printf("\n%p [%p]: event: %d\n", |
| 362 | (void *)(offset + head), |
| 363 | (void *)(long)event->header.size, |
| 364 | event->header.type); |
| 365 | |
| 366 | if (size == 0 || |
| 367 | perf_session__process_event(self, event, ops, offset, head) < 0) { |
| 368 | dump_printf("%p [%p]: skipping unknown header type: %d\n", |
| 369 | (void *)(offset + head), |
| 370 | (void *)(long)(event->header.size), |
| 371 | event->header.type); |
| 372 | /* |
| 373 | * assume we lost track of the stream, check alignment, and |
| 374 | * increment a single u64 in the hope to catch on again 'soon'. |
| 375 | */ |
| 376 | if (unlikely(head & 7)) |
| 377 | head &= ~7ULL; |
| 378 | |
| 379 | size = 8; |
| 380 | } |
| 381 | |
| 382 | head += size; |
| 383 | |
| 384 | if (offset + head >= self->header.data_offset + self->header.data_size) |
| 385 | goto done; |
| 386 | |
| 387 | if (offset + head < self->size) |
| 388 | goto more; |
| 389 | done: |
| 390 | err = 0; |
| 391 | out_err: |
| 392 | return err; |
| 393 | } |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 394 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 395 | 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] | 396 | { |
| 397 | if (!(self->sample_type & PERF_SAMPLE_RAW)) { |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 398 | pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); |
| 399 | return false; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 400 | } |
| 401 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 402 | return true; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 403 | } |