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 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 70 | int perf_session__create_kernel_maps(struct perf_session *self) |
| 71 | { |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 72 | struct rb_root *machines = &self->machines; |
| 73 | int ret = machines__create_kernel_maps(machines, HOST_KERNEL_ID); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 74 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 75 | if (ret >= 0) |
Arnaldo Carvalho de Melo | d28c622 | 2010-04-27 21:20:43 -0300 | [diff] [blame] | 76 | ret = machines__create_guest_kernel_maps(machines); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 77 | return ret; |
| 78 | } |
| 79 | |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 80 | struct perf_session *perf_session__new(const char *filename, int mode, bool force, bool repipe) |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 81 | { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 82 | size_t len = filename ? strlen(filename) + 1 : 0; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 83 | struct perf_session *self = zalloc(sizeof(*self) + len); |
| 84 | |
| 85 | if (self == NULL) |
| 86 | goto out; |
| 87 | |
| 88 | if (perf_header__init(&self->header) < 0) |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 89 | goto out_free; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 90 | |
| 91 | memcpy(self->filename, filename, len); |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 92 | self->threads = RB_ROOT; |
Eric B Munson | cb8f093 | 2010-03-05 12:51:07 -0300 | [diff] [blame] | 93 | self->stats_by_id = RB_ROOT; |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 94 | self->last_match = NULL; |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 95 | self->mmap_window = 32; |
| 96 | self->cwd = NULL; |
| 97 | self->cwdlen = 0; |
Arnaldo Carvalho de Melo | 31d337c | 2009-12-27 21:37:03 -0200 | [diff] [blame] | 98 | self->unknown_events = 0; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 99 | self->machines = RB_ROOT; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 100 | self->repipe = repipe; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 101 | INIT_LIST_HEAD(&self->ordered_samples.samples_head); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 102 | |
Arnaldo Carvalho de Melo | 64abebf7 | 2010-01-27 21:05:52 -0200 | [diff] [blame] | 103 | if (mode == O_RDONLY) { |
| 104 | if (perf_session__open(self, force) < 0) |
| 105 | goto out_delete; |
| 106 | } else if (mode == O_WRONLY) { |
| 107 | /* |
| 108 | * In O_RDONLY mode this will be performed when reading the |
| 109 | * kernel MMAP event, in event__process_mmap(). |
| 110 | */ |
| 111 | if (perf_session__create_kernel_maps(self) < 0) |
| 112 | goto out_delete; |
| 113 | } |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 114 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 115 | perf_session__update_sample_type(self); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 116 | out: |
| 117 | return self; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 118 | out_free: |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 119 | free(self); |
| 120 | return NULL; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 121 | out_delete: |
| 122 | perf_session__delete(self); |
| 123 | return NULL; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void perf_session__delete(struct perf_session *self) |
| 127 | { |
| 128 | perf_header__exit(&self->header); |
| 129 | close(self->fd); |
Arnaldo Carvalho de Melo | ec91336 | 2009-12-13 19:50:27 -0200 | [diff] [blame] | 130 | free(self->cwd); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 131 | free(self); |
| 132 | } |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 133 | |
| 134 | static bool symbol__match_parent_regex(struct symbol *sym) |
| 135 | { |
| 136 | if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) |
| 137 | return 1; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 142 | struct map_symbol *perf_session__resolve_callchain(struct perf_session *self, |
| 143 | struct thread *thread, |
| 144 | struct ip_callchain *chain, |
| 145 | struct symbol **parent) |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 146 | { |
| 147 | u8 cpumode = PERF_RECORD_MISC_USER; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 148 | unsigned int i; |
Arnaldo Carvalho de Melo | ad5b217 | 2010-04-02 10:04:18 -0300 | [diff] [blame] | 149 | struct map_symbol *syms = calloc(chain->nr, sizeof(*syms)); |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 150 | |
Arnaldo Carvalho de Melo | ad5b217 | 2010-04-02 10:04:18 -0300 | [diff] [blame] | 151 | if (!syms) |
| 152 | return NULL; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 153 | |
| 154 | for (i = 0; i < chain->nr; i++) { |
| 155 | u64 ip = chain->ips[i]; |
| 156 | struct addr_location al; |
| 157 | |
| 158 | if (ip >= PERF_CONTEXT_MAX) { |
| 159 | switch (ip) { |
| 160 | case PERF_CONTEXT_HV: |
| 161 | cpumode = PERF_RECORD_MISC_HYPERVISOR; break; |
| 162 | case PERF_CONTEXT_KERNEL: |
| 163 | cpumode = PERF_RECORD_MISC_KERNEL; break; |
| 164 | case PERF_CONTEXT_USER: |
| 165 | cpumode = PERF_RECORD_MISC_USER; break; |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | continue; |
| 170 | } |
| 171 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 172 | al.filtered = false; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 173 | thread__find_addr_location(thread, self, cpumode, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 174 | MAP__FUNCTION, thread->pid, ip, &al, NULL); |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 175 | if (al.sym != NULL) { |
| 176 | if (sort__has_parent && !*parent && |
| 177 | symbol__match_parent_regex(al.sym)) |
| 178 | *parent = al.sym; |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 179 | if (!symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 180 | break; |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 181 | syms[i].map = al.map; |
| 182 | syms[i].sym = al.sym; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | return syms; |
| 187 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 188 | |
| 189 | static int process_event_stub(event_t *event __used, |
| 190 | struct perf_session *session __used) |
| 191 | { |
| 192 | dump_printf(": unhandled!\n"); |
| 193 | return 0; |
| 194 | } |
| 195 | |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame^] | 196 | static int process_finished_round_stub(event_t *event __used, |
| 197 | struct perf_session *session __used, |
| 198 | struct perf_event_ops *ops __used) |
| 199 | { |
| 200 | dump_printf(": unhandled!\n"); |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int process_finished_round(event_t *event, |
| 205 | struct perf_session *session, |
| 206 | struct perf_event_ops *ops); |
| 207 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 208 | static void perf_event_ops__fill_defaults(struct perf_event_ops *handler) |
| 209 | { |
Arnaldo Carvalho de Melo | 55aa640f | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 210 | if (handler->sample == NULL) |
| 211 | handler->sample = process_event_stub; |
| 212 | if (handler->mmap == NULL) |
| 213 | handler->mmap = process_event_stub; |
| 214 | if (handler->comm == NULL) |
| 215 | handler->comm = process_event_stub; |
| 216 | if (handler->fork == NULL) |
| 217 | handler->fork = process_event_stub; |
| 218 | if (handler->exit == NULL) |
| 219 | handler->exit = process_event_stub; |
| 220 | if (handler->lost == NULL) |
| 221 | handler->lost = process_event_stub; |
| 222 | if (handler->read == NULL) |
| 223 | handler->read = process_event_stub; |
| 224 | if (handler->throttle == NULL) |
| 225 | handler->throttle = process_event_stub; |
| 226 | if (handler->unthrottle == NULL) |
| 227 | handler->unthrottle = process_event_stub; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 228 | if (handler->attr == NULL) |
| 229 | handler->attr = process_event_stub; |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 230 | if (handler->event_type == NULL) |
| 231 | handler->event_type = process_event_stub; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 232 | if (handler->tracing_data == NULL) |
| 233 | handler->tracing_data = process_event_stub; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 234 | if (handler->build_id == NULL) |
| 235 | handler->build_id = process_event_stub; |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame^] | 236 | if (handler->finished_round == NULL) { |
| 237 | if (handler->ordered_samples) |
| 238 | handler->finished_round = process_finished_round; |
| 239 | else |
| 240 | handler->finished_round = process_finished_round_stub; |
| 241 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static const char *event__name[] = { |
| 245 | [0] = "TOTAL", |
| 246 | [PERF_RECORD_MMAP] = "MMAP", |
| 247 | [PERF_RECORD_LOST] = "LOST", |
| 248 | [PERF_RECORD_COMM] = "COMM", |
| 249 | [PERF_RECORD_EXIT] = "EXIT", |
| 250 | [PERF_RECORD_THROTTLE] = "THROTTLE", |
| 251 | [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE", |
| 252 | [PERF_RECORD_FORK] = "FORK", |
| 253 | [PERF_RECORD_READ] = "READ", |
| 254 | [PERF_RECORD_SAMPLE] = "SAMPLE", |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 255 | [PERF_RECORD_HEADER_ATTR] = "ATTR", |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 256 | [PERF_RECORD_HEADER_EVENT_TYPE] = "EVENT_TYPE", |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 257 | [PERF_RECORD_HEADER_TRACING_DATA] = "TRACING_DATA", |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 258 | [PERF_RECORD_HEADER_BUILD_ID] = "BUILD_ID", |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 259 | }; |
| 260 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 261 | unsigned long event__total[PERF_RECORD_HEADER_MAX]; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 262 | |
| 263 | void event__print_totals(void) |
| 264 | { |
| 265 | int i; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 266 | for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) { |
| 267 | if (!event__name[i]) |
| 268 | continue; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 269 | pr_info("%10s events: %10ld\n", |
| 270 | event__name[i], event__total[i]); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 271 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 272 | } |
| 273 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 274 | void mem_bswap_64(void *src, int byte_size) |
| 275 | { |
| 276 | u64 *m = src; |
| 277 | |
| 278 | while (byte_size > 0) { |
| 279 | *m = bswap_64(*m); |
| 280 | byte_size -= sizeof(u64); |
| 281 | ++m; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | static void event__all64_swap(event_t *self) |
| 286 | { |
| 287 | struct perf_event_header *hdr = &self->header; |
| 288 | mem_bswap_64(hdr + 1, self->header.size - sizeof(*hdr)); |
| 289 | } |
| 290 | |
| 291 | static void event__comm_swap(event_t *self) |
| 292 | { |
| 293 | self->comm.pid = bswap_32(self->comm.pid); |
| 294 | self->comm.tid = bswap_32(self->comm.tid); |
| 295 | } |
| 296 | |
| 297 | static void event__mmap_swap(event_t *self) |
| 298 | { |
| 299 | self->mmap.pid = bswap_32(self->mmap.pid); |
| 300 | self->mmap.tid = bswap_32(self->mmap.tid); |
| 301 | self->mmap.start = bswap_64(self->mmap.start); |
| 302 | self->mmap.len = bswap_64(self->mmap.len); |
| 303 | self->mmap.pgoff = bswap_64(self->mmap.pgoff); |
| 304 | } |
| 305 | |
| 306 | static void event__task_swap(event_t *self) |
| 307 | { |
| 308 | self->fork.pid = bswap_32(self->fork.pid); |
| 309 | self->fork.tid = bswap_32(self->fork.tid); |
| 310 | self->fork.ppid = bswap_32(self->fork.ppid); |
| 311 | self->fork.ptid = bswap_32(self->fork.ptid); |
| 312 | self->fork.time = bswap_64(self->fork.time); |
| 313 | } |
| 314 | |
| 315 | static void event__read_swap(event_t *self) |
| 316 | { |
| 317 | self->read.pid = bswap_32(self->read.pid); |
| 318 | self->read.tid = bswap_32(self->read.tid); |
| 319 | self->read.value = bswap_64(self->read.value); |
| 320 | self->read.time_enabled = bswap_64(self->read.time_enabled); |
| 321 | self->read.time_running = bswap_64(self->read.time_running); |
| 322 | self->read.id = bswap_64(self->read.id); |
| 323 | } |
| 324 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 325 | static void event__attr_swap(event_t *self) |
| 326 | { |
| 327 | size_t size; |
| 328 | |
| 329 | self->attr.attr.type = bswap_32(self->attr.attr.type); |
| 330 | self->attr.attr.size = bswap_32(self->attr.attr.size); |
| 331 | self->attr.attr.config = bswap_64(self->attr.attr.config); |
| 332 | self->attr.attr.sample_period = bswap_64(self->attr.attr.sample_period); |
| 333 | self->attr.attr.sample_type = bswap_64(self->attr.attr.sample_type); |
| 334 | self->attr.attr.read_format = bswap_64(self->attr.attr.read_format); |
| 335 | self->attr.attr.wakeup_events = bswap_32(self->attr.attr.wakeup_events); |
| 336 | self->attr.attr.bp_type = bswap_32(self->attr.attr.bp_type); |
| 337 | self->attr.attr.bp_addr = bswap_64(self->attr.attr.bp_addr); |
| 338 | self->attr.attr.bp_len = bswap_64(self->attr.attr.bp_len); |
| 339 | |
| 340 | size = self->header.size; |
| 341 | size -= (void *)&self->attr.id - (void *)self; |
| 342 | mem_bswap_64(self->attr.id, size); |
| 343 | } |
| 344 | |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 345 | static void event__event_type_swap(event_t *self) |
| 346 | { |
| 347 | self->event_type.event_type.event_id = |
| 348 | bswap_64(self->event_type.event_type.event_id); |
| 349 | } |
| 350 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 351 | static void event__tracing_data_swap(event_t *self) |
| 352 | { |
| 353 | self->tracing_data.size = bswap_32(self->tracing_data.size); |
| 354 | } |
| 355 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 356 | typedef void (*event__swap_op)(event_t *self); |
| 357 | |
| 358 | static event__swap_op event__swap_ops[] = { |
| 359 | [PERF_RECORD_MMAP] = event__mmap_swap, |
| 360 | [PERF_RECORD_COMM] = event__comm_swap, |
| 361 | [PERF_RECORD_FORK] = event__task_swap, |
| 362 | [PERF_RECORD_EXIT] = event__task_swap, |
| 363 | [PERF_RECORD_LOST] = event__all64_swap, |
| 364 | [PERF_RECORD_READ] = event__read_swap, |
| 365 | [PERF_RECORD_SAMPLE] = event__all64_swap, |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 366 | [PERF_RECORD_HEADER_ATTR] = event__attr_swap, |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 367 | [PERF_RECORD_HEADER_EVENT_TYPE] = event__event_type_swap, |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 368 | [PERF_RECORD_HEADER_TRACING_DATA] = event__tracing_data_swap, |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 369 | [PERF_RECORD_HEADER_BUILD_ID] = NULL, |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 370 | [PERF_RECORD_HEADER_MAX] = NULL, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 371 | }; |
| 372 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 373 | struct sample_queue { |
| 374 | u64 timestamp; |
| 375 | struct sample_event *event; |
| 376 | struct list_head list; |
| 377 | }; |
| 378 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 379 | static void flush_sample_queue(struct perf_session *s, |
| 380 | struct perf_event_ops *ops) |
| 381 | { |
| 382 | struct list_head *head = &s->ordered_samples.samples_head; |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame^] | 383 | u64 limit = s->ordered_samples.next_flush; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 384 | struct sample_queue *tmp, *iter; |
| 385 | |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame^] | 386 | if (!ops->ordered_samples || !limit) |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 387 | return; |
| 388 | |
| 389 | list_for_each_entry_safe(iter, tmp, head, list) { |
| 390 | if (iter->timestamp > limit) |
| 391 | return; |
| 392 | |
| 393 | if (iter == s->ordered_samples.last_inserted) |
| 394 | s->ordered_samples.last_inserted = NULL; |
| 395 | |
| 396 | ops->sample((event_t *)iter->event, s); |
| 397 | |
| 398 | s->ordered_samples.last_flush = iter->timestamp; |
| 399 | list_del(&iter->list); |
| 400 | free(iter->event); |
| 401 | free(iter); |
| 402 | } |
| 403 | } |
| 404 | |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame^] | 405 | /* |
| 406 | * When perf record finishes a pass on every buffers, it records this pseudo |
| 407 | * event. |
| 408 | * We record the max timestamp t found in the pass n. |
| 409 | * Assuming these timestamps are monotonic across cpus, we know that if |
| 410 | * a buffer still has events with timestamps below t, they will be all |
| 411 | * available and then read in the pass n + 1. |
| 412 | * Hence when we start to read the pass n + 2, we can safely flush every |
| 413 | * events with timestamps below t. |
| 414 | * |
| 415 | * ============ PASS n ================= |
| 416 | * CPU 0 | CPU 1 |
| 417 | * | |
| 418 | * cnt1 timestamps | cnt2 timestamps |
| 419 | * 1 | 2 |
| 420 | * 2 | 3 |
| 421 | * - | 4 <--- max recorded |
| 422 | * |
| 423 | * ============ PASS n + 1 ============== |
| 424 | * CPU 0 | CPU 1 |
| 425 | * | |
| 426 | * cnt1 timestamps | cnt2 timestamps |
| 427 | * 3 | 5 |
| 428 | * 4 | 6 |
| 429 | * 5 | 7 <---- max recorded |
| 430 | * |
| 431 | * Flush every events below timestamp 4 |
| 432 | * |
| 433 | * ============ PASS n + 2 ============== |
| 434 | * CPU 0 | CPU 1 |
| 435 | * | |
| 436 | * cnt1 timestamps | cnt2 timestamps |
| 437 | * 6 | 8 |
| 438 | * 7 | 9 |
| 439 | * - | 10 |
| 440 | * |
| 441 | * Flush every events below timestamp 7 |
| 442 | * etc... |
| 443 | */ |
| 444 | static int process_finished_round(event_t *event __used, |
| 445 | struct perf_session *session, |
| 446 | struct perf_event_ops *ops) |
| 447 | { |
| 448 | flush_sample_queue(session, ops); |
| 449 | session->ordered_samples.next_flush = session->ordered_samples.max_timestamp; |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 454 | static void __queue_sample_end(struct sample_queue *new, struct list_head *head) |
| 455 | { |
| 456 | struct sample_queue *iter; |
| 457 | |
| 458 | list_for_each_entry_reverse(iter, head, list) { |
| 459 | if (iter->timestamp < new->timestamp) { |
| 460 | list_add(&new->list, &iter->list); |
| 461 | return; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | list_add(&new->list, head); |
| 466 | } |
| 467 | |
| 468 | static void __queue_sample_before(struct sample_queue *new, |
| 469 | struct sample_queue *iter, |
| 470 | struct list_head *head) |
| 471 | { |
| 472 | list_for_each_entry_continue_reverse(iter, head, list) { |
| 473 | if (iter->timestamp < new->timestamp) { |
| 474 | list_add(&new->list, &iter->list); |
| 475 | return; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | list_add(&new->list, head); |
| 480 | } |
| 481 | |
| 482 | static void __queue_sample_after(struct sample_queue *new, |
| 483 | struct sample_queue *iter, |
| 484 | struct list_head *head) |
| 485 | { |
| 486 | list_for_each_entry_continue(iter, head, list) { |
| 487 | if (iter->timestamp > new->timestamp) { |
| 488 | list_add_tail(&new->list, &iter->list); |
| 489 | return; |
| 490 | } |
| 491 | } |
| 492 | list_add_tail(&new->list, head); |
| 493 | } |
| 494 | |
| 495 | /* The queue is ordered by time */ |
| 496 | static void __queue_sample_event(struct sample_queue *new, |
| 497 | struct perf_session *s) |
| 498 | { |
| 499 | struct sample_queue *last_inserted = s->ordered_samples.last_inserted; |
| 500 | struct list_head *head = &s->ordered_samples.samples_head; |
| 501 | |
| 502 | |
| 503 | if (!last_inserted) { |
| 504 | __queue_sample_end(new, head); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Most of the time the current event has a timestamp |
| 510 | * very close to the last event inserted, unless we just switched |
| 511 | * to another event buffer. Having a sorting based on a list and |
| 512 | * on the last inserted event that is close to the current one is |
| 513 | * probably more efficient than an rbtree based sorting. |
| 514 | */ |
| 515 | if (last_inserted->timestamp >= new->timestamp) |
| 516 | __queue_sample_before(new, last_inserted, head); |
| 517 | else |
| 518 | __queue_sample_after(new, last_inserted, head); |
| 519 | } |
| 520 | |
| 521 | static int queue_sample_event(event_t *event, struct sample_data *data, |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame^] | 522 | struct perf_session *s) |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 523 | { |
| 524 | u64 timestamp = data->time; |
| 525 | struct sample_queue *new; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 526 | |
| 527 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 528 | if (timestamp < s->ordered_samples.last_flush) { |
| 529 | printf("Warning: Timestamp below last timeslice flush\n"); |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | |
| 533 | new = malloc(sizeof(*new)); |
| 534 | if (!new) |
| 535 | return -ENOMEM; |
| 536 | |
| 537 | new->timestamp = timestamp; |
| 538 | |
| 539 | new->event = malloc(event->header.size); |
| 540 | if (!new->event) { |
| 541 | free(new); |
| 542 | return -ENOMEM; |
| 543 | } |
| 544 | |
| 545 | memcpy(new->event, event, event->header.size); |
| 546 | |
| 547 | __queue_sample_event(new, s); |
| 548 | s->ordered_samples.last_inserted = new; |
| 549 | |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame^] | 550 | if (new->timestamp > s->ordered_samples.max_timestamp) |
| 551 | s->ordered_samples.max_timestamp = new->timestamp; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static int perf_session__process_sample(event_t *event, struct perf_session *s, |
| 557 | struct perf_event_ops *ops) |
| 558 | { |
| 559 | struct sample_data data; |
| 560 | |
| 561 | if (!ops->ordered_samples) |
| 562 | return ops->sample(event, s); |
| 563 | |
| 564 | bzero(&data, sizeof(struct sample_data)); |
| 565 | event__parse_sample(event, s->sample_type, &data); |
| 566 | |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame^] | 567 | queue_sample_event(event, &data, s); |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 572 | static int perf_session__process_event(struct perf_session *self, |
| 573 | event_t *event, |
| 574 | struct perf_event_ops *ops, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 575 | u64 offset, u64 head) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 576 | { |
| 577 | trace_event(event); |
| 578 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 579 | if (event->header.type < PERF_RECORD_HEADER_MAX) { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 580 | dump_printf("%#Lx [%#x]: PERF_RECORD_%s", |
Arnaldo Carvalho de Melo | 0d75503 | 2010-01-14 12:23:09 -0200 | [diff] [blame] | 581 | offset + head, event->header.size, |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 582 | event__name[event->header.type]); |
| 583 | ++event__total[0]; |
| 584 | ++event__total[event->header.type]; |
| 585 | } |
| 586 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 587 | if (self->header.needs_swap && event__swap_ops[event->header.type]) |
| 588 | event__swap_ops[event->header.type](event); |
| 589 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 590 | switch (event->header.type) { |
| 591 | case PERF_RECORD_SAMPLE: |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 592 | return perf_session__process_sample(event, self, ops); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 593 | case PERF_RECORD_MMAP: |
Arnaldo Carvalho de Melo | 55aa640f | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 594 | return ops->mmap(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 595 | case PERF_RECORD_COMM: |
Arnaldo Carvalho de Melo | 55aa640f | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 596 | return ops->comm(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 597 | case PERF_RECORD_FORK: |
Arnaldo Carvalho de Melo | 55aa640f | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 598 | return ops->fork(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 599 | case PERF_RECORD_EXIT: |
Arnaldo Carvalho de Melo | 55aa640f | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 600 | return ops->exit(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 601 | case PERF_RECORD_LOST: |
Arnaldo Carvalho de Melo | 55aa640f | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 602 | return ops->lost(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 603 | case PERF_RECORD_READ: |
Arnaldo Carvalho de Melo | 55aa640f | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 604 | return ops->read(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 605 | case PERF_RECORD_THROTTLE: |
Arnaldo Carvalho de Melo | 55aa640f | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 606 | return ops->throttle(event, self); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 607 | case PERF_RECORD_UNTHROTTLE: |
Arnaldo Carvalho de Melo | 55aa640f | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 608 | return ops->unthrottle(event, self); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 609 | case PERF_RECORD_HEADER_ATTR: |
| 610 | return ops->attr(event, self); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 611 | case PERF_RECORD_HEADER_EVENT_TYPE: |
| 612 | return ops->event_type(event, self); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 613 | case PERF_RECORD_HEADER_TRACING_DATA: |
| 614 | /* setup for reading amidst mmap */ |
| 615 | lseek(self->fd, offset + head, SEEK_SET); |
| 616 | return ops->tracing_data(event, self); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 617 | case PERF_RECORD_HEADER_BUILD_ID: |
| 618 | return ops->build_id(event, self); |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame^] | 619 | case PERF_RECORD_FINISHED_ROUND: |
| 620 | return ops->finished_round(event, self, ops); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 621 | default: |
Arnaldo Carvalho de Melo | 31d337c | 2009-12-27 21:37:03 -0200 | [diff] [blame] | 622 | self->unknown_events++; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 623 | return -1; |
| 624 | } |
| 625 | } |
| 626 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 627 | void perf_event_header__bswap(struct perf_event_header *self) |
| 628 | { |
| 629 | self->type = bswap_32(self->type); |
| 630 | self->misc = bswap_16(self->misc); |
| 631 | self->size = bswap_16(self->size); |
| 632 | } |
| 633 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 634 | static struct thread *perf_session__register_idle_thread(struct perf_session *self) |
| 635 | { |
| 636 | struct thread *thread = perf_session__findnew(self, 0); |
| 637 | |
| 638 | if (thread == NULL || thread__set_comm(thread, "swapper")) { |
| 639 | pr_err("problem inserting idle task.\n"); |
| 640 | thread = NULL; |
| 641 | } |
| 642 | |
| 643 | return thread; |
| 644 | } |
| 645 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 646 | int do_read(int fd, void *buf, size_t size) |
| 647 | { |
| 648 | void *buf_start = buf; |
| 649 | |
| 650 | while (size) { |
| 651 | int ret = read(fd, buf, size); |
| 652 | |
| 653 | if (ret <= 0) |
| 654 | return ret; |
| 655 | |
| 656 | size -= ret; |
| 657 | buf += ret; |
| 658 | } |
| 659 | |
| 660 | return buf - buf_start; |
| 661 | } |
| 662 | |
| 663 | #define session_done() (*(volatile int *)(&session_done)) |
| 664 | volatile int session_done; |
| 665 | |
| 666 | static int __perf_session__process_pipe_events(struct perf_session *self, |
| 667 | struct perf_event_ops *ops) |
| 668 | { |
| 669 | event_t event; |
| 670 | uint32_t size; |
| 671 | int skip = 0; |
| 672 | u64 head; |
| 673 | int err; |
| 674 | void *p; |
| 675 | |
| 676 | perf_event_ops__fill_defaults(ops); |
| 677 | |
| 678 | head = 0; |
| 679 | more: |
| 680 | err = do_read(self->fd, &event, sizeof(struct perf_event_header)); |
| 681 | if (err <= 0) { |
| 682 | if (err == 0) |
| 683 | goto done; |
| 684 | |
| 685 | pr_err("failed to read event header\n"); |
| 686 | goto out_err; |
| 687 | } |
| 688 | |
| 689 | if (self->header.needs_swap) |
| 690 | perf_event_header__bswap(&event.header); |
| 691 | |
| 692 | size = event.header.size; |
| 693 | if (size == 0) |
| 694 | size = 8; |
| 695 | |
| 696 | p = &event; |
| 697 | p += sizeof(struct perf_event_header); |
| 698 | |
| 699 | err = do_read(self->fd, p, size - sizeof(struct perf_event_header)); |
| 700 | if (err <= 0) { |
| 701 | if (err == 0) { |
| 702 | pr_err("unexpected end of event stream\n"); |
| 703 | goto done; |
| 704 | } |
| 705 | |
| 706 | pr_err("failed to read event data\n"); |
| 707 | goto out_err; |
| 708 | } |
| 709 | |
| 710 | if (size == 0 || |
| 711 | (skip = perf_session__process_event(self, &event, ops, |
| 712 | 0, head)) < 0) { |
| 713 | dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n", |
| 714 | head, event.header.size, event.header.type); |
| 715 | /* |
| 716 | * assume we lost track of the stream, check alignment, and |
| 717 | * increment a single u64 in the hope to catch on again 'soon'. |
| 718 | */ |
| 719 | if (unlikely(head & 7)) |
| 720 | head &= ~7ULL; |
| 721 | |
| 722 | size = 8; |
| 723 | } |
| 724 | |
| 725 | head += size; |
| 726 | |
| 727 | dump_printf("\n%#Lx [%#x]: event: %d\n", |
| 728 | head, event.header.size, event.header.type); |
| 729 | |
| 730 | if (skip > 0) |
| 731 | head += skip; |
| 732 | |
| 733 | if (!session_done()) |
| 734 | goto more; |
| 735 | done: |
| 736 | err = 0; |
| 737 | out_err: |
| 738 | return err; |
| 739 | } |
| 740 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 741 | int __perf_session__process_events(struct perf_session *self, |
| 742 | u64 data_offset, u64 data_size, |
| 743 | u64 file_size, struct perf_event_ops *ops) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 744 | { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 745 | int err, mmap_prot, mmap_flags; |
| 746 | u64 head, shift; |
| 747 | u64 offset = 0; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 748 | size_t page_size; |
| 749 | event_t *event; |
| 750 | uint32_t size; |
| 751 | char *buf; |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 752 | struct ui_progress *progress = ui_progress__new("Processing events...", |
| 753 | self->size); |
| 754 | if (progress == NULL) |
| 755 | return -1; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 756 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 757 | perf_event_ops__fill_defaults(ops); |
| 758 | |
Arnaldo Carvalho de Melo | 1b75962 | 2010-01-14 18:30:04 -0200 | [diff] [blame] | 759 | page_size = sysconf(_SC_PAGESIZE); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 760 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 761 | head = data_offset; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 762 | shift = page_size * (head / page_size); |
| 763 | offset += shift; |
| 764 | head -= shift; |
| 765 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 766 | mmap_prot = PROT_READ; |
| 767 | mmap_flags = MAP_SHARED; |
| 768 | |
| 769 | if (self->header.needs_swap) { |
| 770 | mmap_prot |= PROT_WRITE; |
| 771 | mmap_flags = MAP_PRIVATE; |
| 772 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 773 | remap: |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 774 | buf = mmap(NULL, page_size * self->mmap_window, mmap_prot, |
| 775 | mmap_flags, self->fd, offset); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 776 | if (buf == MAP_FAILED) { |
| 777 | pr_err("failed to mmap file\n"); |
| 778 | err = -errno; |
| 779 | goto out_err; |
| 780 | } |
| 781 | |
| 782 | more: |
| 783 | event = (event_t *)(buf + head); |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 784 | ui_progress__update(progress, offset); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 785 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 786 | if (self->header.needs_swap) |
| 787 | perf_event_header__bswap(&event->header); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 788 | size = event->header.size; |
| 789 | if (size == 0) |
| 790 | size = 8; |
| 791 | |
| 792 | if (head + event->header.size >= page_size * self->mmap_window) { |
| 793 | int munmap_ret; |
| 794 | |
| 795 | shift = page_size * (head / page_size); |
| 796 | |
| 797 | munmap_ret = munmap(buf, page_size * self->mmap_window); |
| 798 | assert(munmap_ret == 0); |
| 799 | |
| 800 | offset += shift; |
| 801 | head -= shift; |
| 802 | goto remap; |
| 803 | } |
| 804 | |
| 805 | size = event->header.size; |
| 806 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 807 | dump_printf("\n%#Lx [%#x]: event: %d\n", |
Arnaldo Carvalho de Melo | 0d75503 | 2010-01-14 12:23:09 -0200 | [diff] [blame] | 808 | offset + head, event->header.size, event->header.type); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 809 | |
| 810 | if (size == 0 || |
| 811 | perf_session__process_event(self, event, ops, offset, head) < 0) { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 812 | dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n", |
Arnaldo Carvalho de Melo | 0d75503 | 2010-01-14 12:23:09 -0200 | [diff] [blame] | 813 | offset + head, event->header.size, |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 814 | event->header.type); |
| 815 | /* |
| 816 | * assume we lost track of the stream, check alignment, and |
| 817 | * increment a single u64 in the hope to catch on again 'soon'. |
| 818 | */ |
| 819 | if (unlikely(head & 7)) |
| 820 | head &= ~7ULL; |
| 821 | |
| 822 | size = 8; |
| 823 | } |
| 824 | |
| 825 | head += size; |
| 826 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 827 | if (offset + head >= data_offset + data_size) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 828 | goto done; |
| 829 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 830 | if (offset + head < file_size) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 831 | goto more; |
| 832 | done: |
| 833 | err = 0; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 834 | /* do the final flush for ordered samples */ |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame^] | 835 | self->ordered_samples.next_flush = ULLONG_MAX; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 836 | flush_sample_queue(self, ops); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 837 | out_err: |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 838 | ui_progress__delete(progress); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 839 | return err; |
| 840 | } |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 841 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 842 | int perf_session__process_events(struct perf_session *self, |
| 843 | struct perf_event_ops *ops) |
| 844 | { |
| 845 | int err; |
| 846 | |
| 847 | if (perf_session__register_idle_thread(self) == NULL) |
| 848 | return -ENOMEM; |
| 849 | |
| 850 | if (!symbol_conf.full_paths) { |
| 851 | char bf[PATH_MAX]; |
| 852 | |
| 853 | if (getcwd(bf, sizeof(bf)) == NULL) { |
| 854 | err = -errno; |
| 855 | out_getcwd_err: |
| 856 | pr_err("failed to get the current directory\n"); |
| 857 | goto out_err; |
| 858 | } |
| 859 | self->cwd = strdup(bf); |
| 860 | if (self->cwd == NULL) { |
| 861 | err = -ENOMEM; |
| 862 | goto out_getcwd_err; |
| 863 | } |
| 864 | self->cwdlen = strlen(self->cwd); |
| 865 | } |
| 866 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 867 | if (!self->fd_pipe) |
| 868 | err = __perf_session__process_events(self, |
| 869 | self->header.data_offset, |
| 870 | self->header.data_size, |
| 871 | self->size, ops); |
| 872 | else |
| 873 | err = __perf_session__process_pipe_events(self, ops); |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 874 | out_err: |
| 875 | return err; |
| 876 | } |
| 877 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 878 | 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] | 879 | { |
| 880 | if (!(self->sample_type & PERF_SAMPLE_RAW)) { |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 881 | pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); |
| 882 | return false; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 883 | } |
| 884 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 885 | return true; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 886 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 887 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 888 | int perf_session__set_kallsyms_ref_reloc_sym(struct map **maps, |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 889 | const char *symbol_name, |
| 890 | u64 addr) |
| 891 | { |
| 892 | char *bracket; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 893 | enum map_type i; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 894 | struct ref_reloc_sym *ref; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 895 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 896 | ref = zalloc(sizeof(struct ref_reloc_sym)); |
| 897 | if (ref == NULL) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 898 | return -ENOMEM; |
| 899 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 900 | ref->name = strdup(symbol_name); |
| 901 | if (ref->name == NULL) { |
| 902 | free(ref); |
| 903 | return -ENOMEM; |
| 904 | } |
| 905 | |
| 906 | bracket = strchr(ref->name, ']'); |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 907 | if (bracket) |
| 908 | *bracket = '\0'; |
| 909 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 910 | ref->addr = addr; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 911 | |
| 912 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 913 | struct kmap *kmap = map__kmap(maps[i]); |
| 914 | kmap->ref_reloc_sym = ref; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 915 | } |
| 916 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 917 | return 0; |
| 918 | } |