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