Xiao Guangrong | b8f46c5 | 2010-02-03 11:53:14 +0800 | [diff] [blame^] | 1 | #define _LARGEFILE64_SOURCE |
| 2 | #define _FILE_OFFSET_BITS 64 |
| 3 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 4 | #include <linux/kernel.h> |
| 5 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 6 | #include <byteswap.h> |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 7 | #include <unistd.h> |
| 8 | #include <sys/types.h> |
| 9 | |
| 10 | #include "session.h" |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 11 | #include "sort.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 12 | #include "util.h" |
| 13 | |
| 14 | static int perf_session__open(struct perf_session *self, bool force) |
| 15 | { |
| 16 | struct stat input_stat; |
| 17 | |
Xiao Guangrong | b8f46c5 | 2010-02-03 11:53:14 +0800 | [diff] [blame^] | 18 | self->fd = open(self->filename, O_RDONLY|O_LARGEFILE); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 19 | if (self->fd < 0) { |
| 20 | pr_err("failed to open file: %s", self->filename); |
| 21 | if (!strcmp(self->filename, "perf.data")) |
| 22 | pr_err(" (try 'perf record' first)"); |
| 23 | pr_err("\n"); |
| 24 | return -errno; |
| 25 | } |
| 26 | |
| 27 | if (fstat(self->fd, &input_stat) < 0) |
| 28 | goto out_close; |
| 29 | |
| 30 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { |
| 31 | pr_err("file %s not owned by current user or root\n", |
| 32 | self->filename); |
| 33 | goto out_close; |
| 34 | } |
| 35 | |
| 36 | if (!input_stat.st_size) { |
| 37 | pr_info("zero-sized file (%s), nothing to do!\n", |
| 38 | self->filename); |
| 39 | goto out_close; |
| 40 | } |
| 41 | |
| 42 | if (perf_header__read(&self->header, self->fd) < 0) { |
| 43 | pr_err("incompatible file format"); |
| 44 | goto out_close; |
| 45 | } |
| 46 | |
| 47 | self->size = input_stat.st_size; |
| 48 | return 0; |
| 49 | |
| 50 | out_close: |
| 51 | close(self->fd); |
| 52 | self->fd = -1; |
| 53 | return -1; |
| 54 | } |
| 55 | |
Arnaldo Carvalho de Melo | 75be6cf | 2009-12-15 20:04:39 -0200 | [diff] [blame] | 56 | 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] | 57 | { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 58 | size_t len = filename ? strlen(filename) + 1 : 0; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 59 | struct perf_session *self = zalloc(sizeof(*self) + len); |
| 60 | |
| 61 | if (self == NULL) |
| 62 | goto out; |
| 63 | |
| 64 | if (perf_header__init(&self->header) < 0) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 65 | goto out_free; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 66 | |
| 67 | memcpy(self->filename, filename, len); |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 68 | self->threads = RB_ROOT; |
| 69 | self->last_match = NULL; |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 70 | self->mmap_window = 32; |
| 71 | self->cwd = NULL; |
| 72 | self->cwdlen = 0; |
Arnaldo Carvalho de Melo | 31d337c | 2009-12-27 21:37:03 -0200 | [diff] [blame] | 73 | self->unknown_events = 0; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 74 | map_groups__init(&self->kmaps); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 75 | |
Arnaldo Carvalho de Melo | 64abebf7 | 2010-01-27 21:05:52 -0200 | [diff] [blame] | 76 | if (mode == O_RDONLY) { |
| 77 | if (perf_session__open(self, force) < 0) |
| 78 | goto out_delete; |
| 79 | } else if (mode == O_WRONLY) { |
| 80 | /* |
| 81 | * In O_RDONLY mode this will be performed when reading the |
| 82 | * kernel MMAP event, in event__process_mmap(). |
| 83 | */ |
| 84 | if (perf_session__create_kernel_maps(self) < 0) |
| 85 | goto out_delete; |
| 86 | } |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 87 | |
| 88 | self->sample_type = perf_header__sample_type(&self->header); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 89 | out: |
| 90 | return self; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 91 | out_free: |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 92 | free(self); |
| 93 | return NULL; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 94 | out_delete: |
| 95 | perf_session__delete(self); |
| 96 | return NULL; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void perf_session__delete(struct perf_session *self) |
| 100 | { |
| 101 | perf_header__exit(&self->header); |
| 102 | close(self->fd); |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 103 | free(self->cwd); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 104 | free(self); |
| 105 | } |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 106 | |
| 107 | static bool symbol__match_parent_regex(struct symbol *sym) |
| 108 | { |
| 109 | if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) |
| 110 | return 1; |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | struct symbol **perf_session__resolve_callchain(struct perf_session *self, |
| 116 | struct thread *thread, |
| 117 | struct ip_callchain *chain, |
| 118 | struct symbol **parent) |
| 119 | { |
| 120 | u8 cpumode = PERF_RECORD_MISC_USER; |
| 121 | struct symbol **syms = NULL; |
| 122 | unsigned int i; |
| 123 | |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 124 | if (symbol_conf.use_callchain) { |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 125 | syms = calloc(chain->nr, sizeof(*syms)); |
| 126 | if (!syms) { |
| 127 | fprintf(stderr, "Can't allocate memory for symbols\n"); |
| 128 | exit(-1); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | for (i = 0; i < chain->nr; i++) { |
| 133 | u64 ip = chain->ips[i]; |
| 134 | struct addr_location al; |
| 135 | |
| 136 | if (ip >= PERF_CONTEXT_MAX) { |
| 137 | switch (ip) { |
| 138 | case PERF_CONTEXT_HV: |
| 139 | cpumode = PERF_RECORD_MISC_HYPERVISOR; break; |
| 140 | case PERF_CONTEXT_KERNEL: |
| 141 | cpumode = PERF_RECORD_MISC_KERNEL; break; |
| 142 | case PERF_CONTEXT_USER: |
| 143 | cpumode = PERF_RECORD_MISC_USER; break; |
| 144 | default: |
| 145 | break; |
| 146 | } |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | thread__find_addr_location(thread, self, cpumode, |
| 151 | MAP__FUNCTION, ip, &al, NULL); |
| 152 | if (al.sym != NULL) { |
| 153 | if (sort__has_parent && !*parent && |
| 154 | symbol__match_parent_regex(al.sym)) |
| 155 | *parent = al.sym; |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 156 | if (!symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 157 | break; |
| 158 | syms[i] = al.sym; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return syms; |
| 163 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 164 | |
| 165 | static int process_event_stub(event_t *event __used, |
| 166 | struct perf_session *session __used) |
| 167 | { |
| 168 | dump_printf(": unhandled!\n"); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static void perf_event_ops__fill_defaults(struct perf_event_ops *handler) |
| 173 | { |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 174 | if (handler->sample == NULL) |
| 175 | handler->sample = process_event_stub; |
| 176 | if (handler->mmap == NULL) |
| 177 | handler->mmap = process_event_stub; |
| 178 | if (handler->comm == NULL) |
| 179 | handler->comm = process_event_stub; |
| 180 | if (handler->fork == NULL) |
| 181 | handler->fork = process_event_stub; |
| 182 | if (handler->exit == NULL) |
| 183 | handler->exit = process_event_stub; |
| 184 | if (handler->lost == NULL) |
| 185 | handler->lost = process_event_stub; |
| 186 | if (handler->read == NULL) |
| 187 | handler->read = process_event_stub; |
| 188 | if (handler->throttle == NULL) |
| 189 | handler->throttle = process_event_stub; |
| 190 | if (handler->unthrottle == NULL) |
| 191 | handler->unthrottle = process_event_stub; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static const char *event__name[] = { |
| 195 | [0] = "TOTAL", |
| 196 | [PERF_RECORD_MMAP] = "MMAP", |
| 197 | [PERF_RECORD_LOST] = "LOST", |
| 198 | [PERF_RECORD_COMM] = "COMM", |
| 199 | [PERF_RECORD_EXIT] = "EXIT", |
| 200 | [PERF_RECORD_THROTTLE] = "THROTTLE", |
| 201 | [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", |
| 202 | [PERF_RECORD_FORK] = "FORK", |
| 203 | [PERF_RECORD_READ] = "READ", |
| 204 | [PERF_RECORD_SAMPLE] = "SAMPLE", |
| 205 | }; |
| 206 | |
| 207 | unsigned long event__total[PERF_RECORD_MAX]; |
| 208 | |
| 209 | void event__print_totals(void) |
| 210 | { |
| 211 | int i; |
| 212 | for (i = 0; i < PERF_RECORD_MAX; ++i) |
| 213 | pr_info("%10s events: %10ld\n", |
| 214 | event__name[i], event__total[i]); |
| 215 | } |
| 216 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 217 | void mem_bswap_64(void *src, int byte_size) |
| 218 | { |
| 219 | u64 *m = src; |
| 220 | |
| 221 | while (byte_size > 0) { |
| 222 | *m = bswap_64(*m); |
| 223 | byte_size -= sizeof(u64); |
| 224 | ++m; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static void event__all64_swap(event_t *self) |
| 229 | { |
| 230 | struct perf_event_header *hdr = &self->header; |
| 231 | mem_bswap_64(hdr + 1, self->header.size - sizeof(*hdr)); |
| 232 | } |
| 233 | |
| 234 | static void event__comm_swap(event_t *self) |
| 235 | { |
| 236 | self->comm.pid = bswap_32(self->comm.pid); |
| 237 | self->comm.tid = bswap_32(self->comm.tid); |
| 238 | } |
| 239 | |
| 240 | static void event__mmap_swap(event_t *self) |
| 241 | { |
| 242 | self->mmap.pid = bswap_32(self->mmap.pid); |
| 243 | self->mmap.tid = bswap_32(self->mmap.tid); |
| 244 | self->mmap.start = bswap_64(self->mmap.start); |
| 245 | self->mmap.len = bswap_64(self->mmap.len); |
| 246 | self->mmap.pgoff = bswap_64(self->mmap.pgoff); |
| 247 | } |
| 248 | |
| 249 | static void event__task_swap(event_t *self) |
| 250 | { |
| 251 | self->fork.pid = bswap_32(self->fork.pid); |
| 252 | self->fork.tid = bswap_32(self->fork.tid); |
| 253 | self->fork.ppid = bswap_32(self->fork.ppid); |
| 254 | self->fork.ptid = bswap_32(self->fork.ptid); |
| 255 | self->fork.time = bswap_64(self->fork.time); |
| 256 | } |
| 257 | |
| 258 | static void event__read_swap(event_t *self) |
| 259 | { |
| 260 | self->read.pid = bswap_32(self->read.pid); |
| 261 | self->read.tid = bswap_32(self->read.tid); |
| 262 | self->read.value = bswap_64(self->read.value); |
| 263 | self->read.time_enabled = bswap_64(self->read.time_enabled); |
| 264 | self->read.time_running = bswap_64(self->read.time_running); |
| 265 | self->read.id = bswap_64(self->read.id); |
| 266 | } |
| 267 | |
| 268 | typedef void (*event__swap_op)(event_t *self); |
| 269 | |
| 270 | static event__swap_op event__swap_ops[] = { |
| 271 | [PERF_RECORD_MMAP] = event__mmap_swap, |
| 272 | [PERF_RECORD_COMM] = event__comm_swap, |
| 273 | [PERF_RECORD_FORK] = event__task_swap, |
| 274 | [PERF_RECORD_EXIT] = event__task_swap, |
| 275 | [PERF_RECORD_LOST] = event__all64_swap, |
| 276 | [PERF_RECORD_READ] = event__read_swap, |
| 277 | [PERF_RECORD_SAMPLE] = event__all64_swap, |
| 278 | [PERF_RECORD_MAX] = NULL, |
| 279 | }; |
| 280 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 281 | static int perf_session__process_event(struct perf_session *self, |
| 282 | event_t *event, |
| 283 | struct perf_event_ops *ops, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 284 | u64 offset, u64 head) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 285 | { |
| 286 | trace_event(event); |
| 287 | |
| 288 | if (event->header.type < PERF_RECORD_MAX) { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 289 | dump_printf("%#Lx [%#x]: PERF_RECORD_%s", |
Arnaldo Carvalho de Melo | 0d75503 | 2010-01-14 12:23:09 -0200 | [diff] [blame] | 290 | offset + head, event->header.size, |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 291 | event__name[event->header.type]); |
| 292 | ++event__total[0]; |
| 293 | ++event__total[event->header.type]; |
| 294 | } |
| 295 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 296 | if (self->header.needs_swap && event__swap_ops[event->header.type]) |
| 297 | event__swap_ops[event->header.type](event); |
| 298 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 299 | switch (event->header.type) { |
| 300 | case PERF_RECORD_SAMPLE: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 301 | return ops->sample(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 302 | case PERF_RECORD_MMAP: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 303 | return ops->mmap(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 304 | case PERF_RECORD_COMM: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 305 | return ops->comm(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 306 | case PERF_RECORD_FORK: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 307 | return ops->fork(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 308 | case PERF_RECORD_EXIT: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 309 | return ops->exit(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 310 | case PERF_RECORD_LOST: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 311 | return ops->lost(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 312 | case PERF_RECORD_READ: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 313 | return ops->read(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 314 | case PERF_RECORD_THROTTLE: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 315 | return ops->throttle(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 316 | case PERF_RECORD_UNTHROTTLE: |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 317 | return ops->unthrottle(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 318 | default: |
Arnaldo Carvalho de Melo | 31d337c | 2009-12-27 21:37:03 -0200 | [diff] [blame] | 319 | self->unknown_events++; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 320 | return -1; |
| 321 | } |
| 322 | } |
| 323 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 324 | void perf_event_header__bswap(struct perf_event_header *self) |
| 325 | { |
| 326 | self->type = bswap_32(self->type); |
| 327 | self->misc = bswap_16(self->misc); |
| 328 | self->size = bswap_16(self->size); |
| 329 | } |
| 330 | |
| 331 | int perf_header__read_build_ids(struct perf_header *self, |
| 332 | int input, u64 offset, u64 size) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 333 | { |
| 334 | struct build_id_event bev; |
| 335 | char filename[PATH_MAX]; |
| 336 | u64 limit = offset + size; |
| 337 | int err = -1; |
| 338 | |
| 339 | while (offset < limit) { |
| 340 | struct dso *dso; |
| 341 | ssize_t len; |
Arnaldo Carvalho de Melo | a89e5abe | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 342 | struct list_head *head = &dsos__user; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 343 | |
| 344 | if (read(input, &bev, sizeof(bev)) != sizeof(bev)) |
| 345 | goto out; |
| 346 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 347 | if (self->needs_swap) |
| 348 | perf_event_header__bswap(&bev.header); |
| 349 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 350 | len = bev.header.size - sizeof(bev); |
| 351 | if (read(input, filename, len) != len) |
| 352 | goto out; |
| 353 | |
Arnaldo Carvalho de Melo | a89e5abe | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 354 | if (bev.header.misc & PERF_RECORD_MISC_KERNEL) |
| 355 | head = &dsos__kernel; |
| 356 | |
| 357 | dso = __dsos__findnew(head, filename); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 358 | if (dso != NULL) { |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 359 | dso__set_build_id(dso, &bev.build_id); |
Arnaldo Carvalho de Melo | b7cece7 | 2010-01-13 13:22:17 -0200 | [diff] [blame] | 360 | if (head == &dsos__kernel && filename[0] == '[') |
| 361 | dso->kernel = 1; |
| 362 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 363 | |
| 364 | offset += bev.header.size; |
| 365 | } |
| 366 | err = 0; |
| 367 | out: |
| 368 | return err; |
| 369 | } |
| 370 | |
| 371 | static struct thread *perf_session__register_idle_thread(struct perf_session *self) |
| 372 | { |
| 373 | struct thread *thread = perf_session__findnew(self, 0); |
| 374 | |
| 375 | if (thread == NULL || thread__set_comm(thread, "swapper")) { |
| 376 | pr_err("problem inserting idle task.\n"); |
| 377 | thread = NULL; |
| 378 | } |
| 379 | |
| 380 | return thread; |
| 381 | } |
| 382 | |
| 383 | int perf_session__process_events(struct perf_session *self, |
| 384 | struct perf_event_ops *ops) |
| 385 | { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 386 | int err, mmap_prot, mmap_flags; |
| 387 | u64 head, shift; |
| 388 | u64 offset = 0; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 389 | size_t page_size; |
| 390 | event_t *event; |
| 391 | uint32_t size; |
| 392 | char *buf; |
| 393 | |
| 394 | if (perf_session__register_idle_thread(self) == NULL) |
| 395 | return -ENOMEM; |
| 396 | |
| 397 | perf_event_ops__fill_defaults(ops); |
| 398 | |
Arnaldo Carvalho de Melo | 1b75962 | 2010-01-14 18:30:04 -0200 | [diff] [blame] | 399 | page_size = sysconf(_SC_PAGESIZE); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 400 | |
| 401 | head = self->header.data_offset; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 402 | |
Arnaldo Carvalho de Melo | f7d8744 | 2009-12-27 21:37:04 -0200 | [diff] [blame] | 403 | if (!symbol_conf.full_paths) { |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 404 | char bf[PATH_MAX]; |
| 405 | |
| 406 | if (getcwd(bf, sizeof(bf)) == NULL) { |
| 407 | err = -errno; |
| 408 | out_getcwd_err: |
| 409 | pr_err("failed to get the current directory\n"); |
| 410 | goto out_err; |
| 411 | } |
| 412 | self->cwd = strdup(bf); |
| 413 | if (self->cwd == NULL) { |
| 414 | err = -ENOMEM; |
| 415 | goto out_getcwd_err; |
| 416 | } |
| 417 | self->cwdlen = strlen(self->cwd); |
| 418 | } |
| 419 | |
| 420 | shift = page_size * (head / page_size); |
| 421 | offset += shift; |
| 422 | head -= shift; |
| 423 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 424 | mmap_prot = PROT_READ; |
| 425 | mmap_flags = MAP_SHARED; |
| 426 | |
| 427 | if (self->header.needs_swap) { |
| 428 | mmap_prot |= PROT_WRITE; |
| 429 | mmap_flags = MAP_PRIVATE; |
| 430 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 431 | remap: |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 432 | buf = mmap(NULL, page_size * self->mmap_window, mmap_prot, |
| 433 | mmap_flags, self->fd, offset); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 434 | if (buf == MAP_FAILED) { |
| 435 | pr_err("failed to mmap file\n"); |
| 436 | err = -errno; |
| 437 | goto out_err; |
| 438 | } |
| 439 | |
| 440 | more: |
| 441 | event = (event_t *)(buf + head); |
| 442 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 443 | if (self->header.needs_swap) |
| 444 | perf_event_header__bswap(&event->header); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 445 | size = event->header.size; |
| 446 | if (size == 0) |
| 447 | size = 8; |
| 448 | |
| 449 | if (head + event->header.size >= page_size * self->mmap_window) { |
| 450 | int munmap_ret; |
| 451 | |
| 452 | shift = page_size * (head / page_size); |
| 453 | |
| 454 | munmap_ret = munmap(buf, page_size * self->mmap_window); |
| 455 | assert(munmap_ret == 0); |
| 456 | |
| 457 | offset += shift; |
| 458 | head -= shift; |
| 459 | goto remap; |
| 460 | } |
| 461 | |
| 462 | size = event->header.size; |
| 463 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 464 | dump_printf("\n%#Lx [%#x]: event: %d\n", |
Arnaldo Carvalho de Melo | 0d75503 | 2010-01-14 12:23:09 -0200 | [diff] [blame] | 465 | offset + head, event->header.size, event->header.type); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 466 | |
| 467 | if (size == 0 || |
| 468 | perf_session__process_event(self, event, ops, offset, head) < 0) { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 469 | dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n", |
Arnaldo Carvalho de Melo | 0d75503 | 2010-01-14 12:23:09 -0200 | [diff] [blame] | 470 | offset + head, event->header.size, |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 471 | event->header.type); |
| 472 | /* |
| 473 | * assume we lost track of the stream, check alignment, and |
| 474 | * increment a single u64 in the hope to catch on again 'soon'. |
| 475 | */ |
| 476 | if (unlikely(head & 7)) |
| 477 | head &= ~7ULL; |
| 478 | |
| 479 | size = 8; |
| 480 | } |
| 481 | |
| 482 | head += size; |
| 483 | |
| 484 | if (offset + head >= self->header.data_offset + self->header.data_size) |
| 485 | goto done; |
| 486 | |
| 487 | if (offset + head < self->size) |
| 488 | goto more; |
| 489 | done: |
| 490 | err = 0; |
| 491 | out_err: |
| 492 | return err; |
| 493 | } |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 494 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 495 | 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] | 496 | { |
| 497 | if (!(self->sample_type & PERF_SAMPLE_RAW)) { |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 498 | pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); |
| 499 | return false; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 500 | } |
| 501 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 502 | return true; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 503 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 504 | |
| 505 | int perf_session__set_kallsyms_ref_reloc_sym(struct perf_session *self, |
| 506 | const char *symbol_name, |
| 507 | u64 addr) |
| 508 | { |
| 509 | char *bracket; |
| 510 | |
| 511 | self->ref_reloc_sym.name = strdup(symbol_name); |
| 512 | if (self->ref_reloc_sym.name == NULL) |
| 513 | return -ENOMEM; |
| 514 | |
| 515 | bracket = strchr(self->ref_reloc_sym.name, ']'); |
| 516 | if (bracket) |
| 517 | *bracket = '\0'; |
| 518 | |
| 519 | self->ref_reloc_sym.addr = addr; |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static u64 map__reloc_map_ip(struct map *map, u64 ip) |
| 524 | { |
| 525 | return ip + (s64)map->pgoff; |
| 526 | } |
| 527 | |
| 528 | static u64 map__reloc_unmap_ip(struct map *map, u64 ip) |
| 529 | { |
| 530 | return ip - (s64)map->pgoff; |
| 531 | } |
| 532 | |
| 533 | void perf_session__reloc_vmlinux_maps(struct perf_session *self, |
| 534 | u64 unrelocated_addr) |
| 535 | { |
| 536 | enum map_type type; |
| 537 | s64 reloc = unrelocated_addr - self->ref_reloc_sym.addr; |
| 538 | |
| 539 | if (!reloc) |
| 540 | return; |
| 541 | |
| 542 | for (type = 0; type < MAP__NR_TYPES; ++type) { |
| 543 | struct map *map = self->vmlinux_maps[type]; |
| 544 | |
| 545 | map->map_ip = map__reloc_map_ip; |
| 546 | map->unmap_ip = map__reloc_unmap_ip; |
| 547 | map->pgoff = reloc; |
| 548 | } |
| 549 | } |