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