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