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 | 55b44629 | 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, |
| 468 | struct perf_event_ops *ops); |
| 469 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 470 | static void flush_sample_queue(struct perf_session *s, |
| 471 | struct perf_event_ops *ops) |
| 472 | { |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 473 | struct ordered_samples *os = &s->ordered_samples; |
| 474 | struct list_head *head = &os->samples; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 475 | struct sample_queue *tmp, *iter; |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 476 | struct sample_data sample; |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 477 | u64 limit = os->next_flush; |
| 478 | u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 479 | |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 480 | if (!ops->ordered_samples || !limit) |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 481 | return; |
| 482 | |
| 483 | list_for_each_entry_safe(iter, tmp, head, list) { |
| 484 | if (iter->timestamp > limit) |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 485 | break; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 486 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 487 | event__parse_sample(iter->event, s, &sample); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 488 | perf_session_deliver_event(s, iter->event, &sample, ops); |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 489 | |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 490 | os->last_flush = iter->timestamp; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 491 | list_del(&iter->list); |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 492 | list_add(&iter->list, &os->sample_cache); |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 493 | } |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 494 | |
| 495 | if (list_empty(head)) { |
| 496 | os->last_sample = NULL; |
| 497 | } else if (last_ts <= limit) { |
| 498 | os->last_sample = |
| 499 | list_entry(head->prev, struct sample_queue, list); |
| 500 | } |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 501 | } |
| 502 | |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 503 | /* |
| 504 | * When perf record finishes a pass on every buffers, it records this pseudo |
| 505 | * event. |
| 506 | * We record the max timestamp t found in the pass n. |
| 507 | * Assuming these timestamps are monotonic across cpus, we know that if |
| 508 | * a buffer still has events with timestamps below t, they will be all |
| 509 | * available and then read in the pass n + 1. |
| 510 | * Hence when we start to read the pass n + 2, we can safely flush every |
| 511 | * events with timestamps below t. |
| 512 | * |
| 513 | * ============ PASS n ================= |
| 514 | * CPU 0 | CPU 1 |
| 515 | * | |
| 516 | * cnt1 timestamps | cnt2 timestamps |
| 517 | * 1 | 2 |
| 518 | * 2 | 3 |
| 519 | * - | 4 <--- max recorded |
| 520 | * |
| 521 | * ============ PASS n + 1 ============== |
| 522 | * CPU 0 | CPU 1 |
| 523 | * | |
| 524 | * cnt1 timestamps | cnt2 timestamps |
| 525 | * 3 | 5 |
| 526 | * 4 | 6 |
| 527 | * 5 | 7 <---- max recorded |
| 528 | * |
| 529 | * Flush every events below timestamp 4 |
| 530 | * |
| 531 | * ============ PASS n + 2 ============== |
| 532 | * CPU 0 | CPU 1 |
| 533 | * | |
| 534 | * cnt1 timestamps | cnt2 timestamps |
| 535 | * 6 | 8 |
| 536 | * 7 | 9 |
| 537 | * - | 10 |
| 538 | * |
| 539 | * Flush every events below timestamp 7 |
| 540 | * etc... |
| 541 | */ |
| 542 | static int process_finished_round(event_t *event __used, |
| 543 | struct perf_session *session, |
| 544 | struct perf_event_ops *ops) |
| 545 | { |
| 546 | flush_sample_queue(session, ops); |
| 547 | session->ordered_samples.next_flush = session->ordered_samples.max_timestamp; |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 552 | /* The queue is ordered by time */ |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 553 | static void __queue_event(struct sample_queue *new, struct perf_session *s) |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 554 | { |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 555 | struct ordered_samples *os = &s->ordered_samples; |
| 556 | struct sample_queue *sample = os->last_sample; |
| 557 | u64 timestamp = new->timestamp; |
| 558 | struct list_head *p; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 559 | |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 560 | os->last_sample = new; |
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 | if (!sample) { |
| 563 | list_add(&new->list, &os->samples); |
| 564 | os->max_timestamp = timestamp; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | |
| 568 | /* |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 569 | * last_sample might point to some random place in the list as it's |
| 570 | * the last queued event. We expect that the new event is close to |
| 571 | * this. |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 572 | */ |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 573 | if (sample->timestamp <= timestamp) { |
| 574 | while (sample->timestamp <= timestamp) { |
| 575 | p = sample->list.next; |
| 576 | if (p == &os->samples) { |
| 577 | list_add_tail(&new->list, &os->samples); |
| 578 | os->max_timestamp = timestamp; |
| 579 | return; |
| 580 | } |
| 581 | sample = list_entry(p, struct sample_queue, list); |
| 582 | } |
| 583 | list_add_tail(&new->list, &sample->list); |
| 584 | } else { |
| 585 | while (sample->timestamp > timestamp) { |
| 586 | p = sample->list.prev; |
| 587 | if (p == &os->samples) { |
| 588 | list_add(&new->list, &os->samples); |
| 589 | return; |
| 590 | } |
| 591 | sample = list_entry(p, struct sample_queue, list); |
| 592 | } |
| 593 | list_add(&new->list, &sample->list); |
| 594 | } |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 597 | #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue)) |
| 598 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 599 | 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^] | 600 | struct sample_data *data, u64 file_offset) |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 601 | { |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 602 | struct ordered_samples *os = &s->ordered_samples; |
| 603 | struct list_head *sc = &os->sample_cache; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 604 | u64 timestamp = data->time; |
| 605 | struct sample_queue *new; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 606 | |
Thomas Gleixner | 79a14c1 | 2010-12-07 12:48:44 +0000 | [diff] [blame] | 607 | if (!timestamp || timestamp == ~0ULL) |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 608 | return -ETIME; |
| 609 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 610 | if (timestamp < s->ordered_samples.last_flush) { |
| 611 | printf("Warning: Timestamp below last timeslice flush\n"); |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 615 | if (!list_empty(sc)) { |
| 616 | new = list_entry(sc->next, struct sample_queue, list); |
| 617 | list_del(&new->list); |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 618 | } else if (os->sample_buffer) { |
| 619 | new = os->sample_buffer + os->sample_buffer_idx; |
| 620 | if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER) |
| 621 | os->sample_buffer = NULL; |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 622 | } else { |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 623 | os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new)); |
| 624 | if (!os->sample_buffer) |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 625 | return -ENOMEM; |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 626 | list_add(&os->sample_buffer->list, &os->to_free); |
| 627 | os->sample_buffer_idx = 2; |
| 628 | new = os->sample_buffer + 1; |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 629 | } |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 630 | |
| 631 | new->timestamp = timestamp; |
Thomas Gleixner | e4c2df1 | 2010-12-07 12:48:50 +0000 | [diff] [blame^] | 632 | new->file_offset = file_offset; |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 633 | new->event = event; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 634 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 635 | __queue_event(new, s); |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 640 | static void callchain__dump(struct sample_data *sample) |
| 641 | { |
| 642 | unsigned int i; |
| 643 | |
| 644 | if (!dump_trace) |
| 645 | return; |
| 646 | |
| 647 | printf("... chain: nr:%Lu\n", sample->callchain->nr); |
| 648 | |
| 649 | for (i = 0; i < sample->callchain->nr; i++) |
| 650 | printf("..... %2d: %016Lx\n", i, sample->callchain->ips[i]); |
| 651 | } |
| 652 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 653 | static void perf_session__print_tstamp(struct perf_session *session, |
| 654 | event_t *event, |
| 655 | struct sample_data *sample) |
| 656 | { |
| 657 | if (event->header.type != PERF_RECORD_SAMPLE && |
| 658 | !session->sample_id_all) { |
| 659 | fputs("-1 -1 ", stdout); |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | if ((session->sample_type & PERF_SAMPLE_CPU)) |
| 664 | printf("%u ", sample->cpu); |
| 665 | |
| 666 | if (session->sample_type & PERF_SAMPLE_TIME) |
| 667 | printf("%Lu ", sample->time); |
| 668 | } |
| 669 | |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 670 | static void dump_event(struct perf_session *session, event_t *event, |
| 671 | u64 file_offset, struct sample_data *sample) |
| 672 | { |
| 673 | if (!dump_trace) |
| 674 | return; |
| 675 | |
| 676 | dump_printf("\n%#Lx [%#x]: event: %d\n", file_offset, |
| 677 | event->header.size, event->header.type); |
| 678 | |
| 679 | trace_event(event); |
| 680 | |
| 681 | if (sample) |
| 682 | perf_session__print_tstamp(session, event, sample); |
| 683 | |
| 684 | dump_printf("%#Lx [%#x]: PERF_RECORD_%s", |
| 685 | file_offset, event->header.size, |
| 686 | event__get_event_name(event->header.type)); |
| 687 | } |
| 688 | |
| 689 | static void dump_sample(struct perf_session *session, event_t *event, |
| 690 | struct sample_data *sample) |
| 691 | { |
| 692 | dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc, |
| 693 | sample->pid, sample->tid, sample->ip, sample->period); |
| 694 | |
| 695 | if (session->sample_type & PERF_SAMPLE_CALLCHAIN) |
| 696 | callchain__dump(sample); |
| 697 | } |
| 698 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 699 | static int perf_session_deliver_event(struct perf_session *session, |
| 700 | event_t *event, |
| 701 | struct sample_data *sample, |
| 702 | struct perf_event_ops *ops) |
| 703 | { |
| 704 | switch (event->header.type) { |
| 705 | case PERF_RECORD_SAMPLE: |
| 706 | return ops->sample(event, sample, session); |
| 707 | case PERF_RECORD_MMAP: |
| 708 | return ops->mmap(event, sample, session); |
| 709 | case PERF_RECORD_COMM: |
| 710 | return ops->comm(event, sample, session); |
| 711 | case PERF_RECORD_FORK: |
| 712 | return ops->fork(event, sample, session); |
| 713 | case PERF_RECORD_EXIT: |
| 714 | return ops->exit(event, sample, session); |
| 715 | case PERF_RECORD_LOST: |
| 716 | return ops->lost(event, sample, session); |
| 717 | case PERF_RECORD_READ: |
| 718 | return ops->read(event, sample, session); |
| 719 | case PERF_RECORD_THROTTLE: |
| 720 | return ops->throttle(event, sample, session); |
| 721 | case PERF_RECORD_UNTHROTTLE: |
| 722 | return ops->unthrottle(event, sample, session); |
| 723 | default: |
| 724 | ++session->hists.stats.nr_unknown_events; |
| 725 | return -1; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | static int perf_session__process_event(struct perf_session *session, |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 730 | event_t *event, |
| 731 | struct perf_event_ops *ops, |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 732 | u64 file_offset) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 733 | { |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 734 | struct sample_data sample; |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 735 | int ret; |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 736 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 737 | 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] | 738 | event__swap_ops[event->header.type](event); |
| 739 | |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 740 | if (event->header.type >= PERF_RECORD_HEADER_MAX) |
| 741 | return -EINVAL; |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 742 | |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 743 | hists__inc_nr_events(&session->hists, event->header.type); |
| 744 | |
| 745 | if (event->header.type >= PERF_RECORD_USER_TYPE_START) |
| 746 | dump_event(session, event, file_offset, NULL); |
| 747 | else { |
| 748 | event__parse_sample(event, session, &sample); |
| 749 | dump_event(session, event, file_offset, &sample); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 750 | } |
| 751 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 752 | /* These events are processed right away */ |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 753 | switch (event->header.type) { |
| 754 | case PERF_RECORD_SAMPLE: |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 755 | if (session->sample_type & PERF_SAMPLE_CALLCHAIN) { |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 756 | if (!ip_callchain__valid(sample.callchain, event)) { |
| 757 | pr_debug("call-chain problem with event, " |
| 758 | "skipping it.\n"); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 759 | ++session->hists.stats.nr_invalid_chains; |
| 760 | session->hists.stats.total_invalid_chains += |
| 761 | sample.period; |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 762 | return 0; |
| 763 | } |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 764 | } |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 765 | dump_sample(session, event, &sample); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 766 | break; |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 767 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 768 | case PERF_RECORD_HEADER_ATTR: |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 769 | return ops->attr(event, session); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 770 | case PERF_RECORD_HEADER_EVENT_TYPE: |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 771 | return ops->event_type(event, session); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 772 | case PERF_RECORD_HEADER_TRACING_DATA: |
| 773 | /* setup for reading amidst mmap */ |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 774 | lseek(session->fd, file_offset, SEEK_SET); |
| 775 | return ops->tracing_data(event, session); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 776 | case PERF_RECORD_HEADER_BUILD_ID: |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 777 | return ops->build_id(event, session); |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 778 | case PERF_RECORD_FINISHED_ROUND: |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 779 | return ops->finished_round(event, session, ops); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 780 | default: |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 781 | break; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 782 | } |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 783 | |
| 784 | if (ops->ordered_samples) { |
Thomas Gleixner | e4c2df1 | 2010-12-07 12:48:50 +0000 | [diff] [blame^] | 785 | ret = perf_session_queue_event(session, event, &sample, |
| 786 | file_offset); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 787 | if (ret != -ETIME) |
| 788 | return ret; |
| 789 | } |
| 790 | |
| 791 | return perf_session_deliver_event(session, event, &sample, ops); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 792 | } |
| 793 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 794 | void perf_event_header__bswap(struct perf_event_header *self) |
| 795 | { |
| 796 | self->type = bswap_32(self->type); |
| 797 | self->misc = bswap_16(self->misc); |
| 798 | self->size = bswap_16(self->size); |
| 799 | } |
| 800 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 801 | static struct thread *perf_session__register_idle_thread(struct perf_session *self) |
| 802 | { |
| 803 | struct thread *thread = perf_session__findnew(self, 0); |
| 804 | |
| 805 | if (thread == NULL || thread__set_comm(thread, "swapper")) { |
| 806 | pr_err("problem inserting idle task.\n"); |
| 807 | thread = NULL; |
| 808 | } |
| 809 | |
| 810 | return thread; |
| 811 | } |
| 812 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 813 | int do_read(int fd, void *buf, size_t size) |
| 814 | { |
| 815 | void *buf_start = buf; |
| 816 | |
| 817 | while (size) { |
| 818 | int ret = read(fd, buf, size); |
| 819 | |
| 820 | if (ret <= 0) |
| 821 | return ret; |
| 822 | |
| 823 | size -= ret; |
| 824 | buf += ret; |
| 825 | } |
| 826 | |
| 827 | return buf - buf_start; |
| 828 | } |
| 829 | |
| 830 | #define session_done() (*(volatile int *)(&session_done)) |
| 831 | volatile int session_done; |
| 832 | |
| 833 | static int __perf_session__process_pipe_events(struct perf_session *self, |
| 834 | struct perf_event_ops *ops) |
| 835 | { |
| 836 | event_t event; |
| 837 | uint32_t size; |
| 838 | int skip = 0; |
| 839 | u64 head; |
| 840 | int err; |
| 841 | void *p; |
| 842 | |
| 843 | perf_event_ops__fill_defaults(ops); |
| 844 | |
| 845 | head = 0; |
| 846 | more: |
| 847 | err = do_read(self->fd, &event, sizeof(struct perf_event_header)); |
| 848 | if (err <= 0) { |
| 849 | if (err == 0) |
| 850 | goto done; |
| 851 | |
| 852 | pr_err("failed to read event header\n"); |
| 853 | goto out_err; |
| 854 | } |
| 855 | |
| 856 | if (self->header.needs_swap) |
| 857 | perf_event_header__bswap(&event.header); |
| 858 | |
| 859 | size = event.header.size; |
| 860 | if (size == 0) |
| 861 | size = 8; |
| 862 | |
| 863 | p = &event; |
| 864 | p += sizeof(struct perf_event_header); |
| 865 | |
Tom Zanussi | 794e43b | 2010-05-05 00:27:40 -0500 | [diff] [blame] | 866 | if (size - sizeof(struct perf_event_header)) { |
| 867 | err = do_read(self->fd, p, |
| 868 | size - sizeof(struct perf_event_header)); |
| 869 | if (err <= 0) { |
| 870 | if (err == 0) { |
| 871 | pr_err("unexpected end of event stream\n"); |
| 872 | goto done; |
| 873 | } |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 874 | |
Tom Zanussi | 794e43b | 2010-05-05 00:27:40 -0500 | [diff] [blame] | 875 | pr_err("failed to read event data\n"); |
| 876 | goto out_err; |
| 877 | } |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | if (size == 0 || |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 881 | (skip = perf_session__process_event(self, &event, ops, head)) < 0) { |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 882 | dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n", |
| 883 | head, event.header.size, event.header.type); |
| 884 | /* |
| 885 | * assume we lost track of the stream, check alignment, and |
| 886 | * increment a single u64 in the hope to catch on again 'soon'. |
| 887 | */ |
| 888 | if (unlikely(head & 7)) |
| 889 | head &= ~7ULL; |
| 890 | |
| 891 | size = 8; |
| 892 | } |
| 893 | |
| 894 | head += size; |
| 895 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 896 | if (skip > 0) |
| 897 | head += skip; |
| 898 | |
| 899 | if (!session_done()) |
| 900 | goto more; |
| 901 | done: |
| 902 | err = 0; |
| 903 | out_err: |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 904 | perf_session_free_sample_buffers(self); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 905 | return err; |
| 906 | } |
| 907 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 908 | int __perf_session__process_events(struct perf_session *session, |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 909 | u64 data_offset, u64 data_size, |
| 910 | u64 file_size, struct perf_event_ops *ops) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 911 | { |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 912 | u64 head, page_offset, file_offset, file_pos, progress_next; |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 913 | int err, mmap_prot, mmap_flags, map_idx = 0; |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 914 | struct ui_progress *progress; |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 915 | size_t page_size, mmap_size; |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 916 | char *buf, *mmaps[8]; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 917 | event_t *event; |
| 918 | uint32_t size; |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 919 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 920 | perf_event_ops__fill_defaults(ops); |
| 921 | |
Arnaldo Carvalho de Melo | 1b75962 | 2010-01-14 18:30:04 -0200 | [diff] [blame] | 922 | page_size = sysconf(_SC_PAGESIZE); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 923 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 924 | page_offset = page_size * (data_offset / page_size); |
| 925 | file_offset = page_offset; |
| 926 | head = data_offset - page_offset; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 927 | |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 928 | if (data_offset + data_size < file_size) |
| 929 | file_size = data_offset + data_size; |
| 930 | |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 931 | progress_next = file_size / 16; |
| 932 | progress = ui_progress__new("Processing events...", file_size); |
| 933 | if (progress == NULL) |
| 934 | return -1; |
| 935 | |
| 936 | mmap_size = session->mmap_window; |
| 937 | if (mmap_size > file_size) |
| 938 | mmap_size = file_size; |
| 939 | |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 940 | memset(mmaps, 0, sizeof(mmaps)); |
| 941 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 942 | mmap_prot = PROT_READ; |
| 943 | mmap_flags = MAP_SHARED; |
| 944 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 945 | if (session->header.needs_swap) { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 946 | mmap_prot |= PROT_WRITE; |
| 947 | mmap_flags = MAP_PRIVATE; |
| 948 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 949 | remap: |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 950 | buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd, |
| 951 | file_offset); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 952 | if (buf == MAP_FAILED) { |
| 953 | pr_err("failed to mmap file\n"); |
| 954 | err = -errno; |
| 955 | goto out_err; |
| 956 | } |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 957 | mmaps[map_idx] = buf; |
| 958 | map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1); |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 959 | file_pos = file_offset + head; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 960 | |
| 961 | more: |
| 962 | event = (event_t *)(buf + head); |
| 963 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 964 | if (session->header.needs_swap) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 965 | perf_event_header__bswap(&event->header); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 966 | size = event->header.size; |
| 967 | if (size == 0) |
| 968 | size = 8; |
| 969 | |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 970 | if (head + event->header.size >= mmap_size) { |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 971 | if (mmaps[map_idx]) { |
| 972 | munmap(mmaps[map_idx], mmap_size); |
| 973 | mmaps[map_idx] = NULL; |
| 974 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 975 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 976 | page_offset = page_size * (head / page_size); |
| 977 | file_offset += page_offset; |
| 978 | head -= page_offset; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 979 | goto remap; |
| 980 | } |
| 981 | |
| 982 | size = event->header.size; |
| 983 | |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 984 | if (size == 0 || |
| 985 | perf_session__process_event(session, event, ops, file_pos) < 0) { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 986 | dump_printf("%#Lx [%#x]: skipping unknown header type: %d\n", |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 987 | file_offset + head, event->header.size, |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 988 | event->header.type); |
| 989 | /* |
| 990 | * assume we lost track of the stream, check alignment, and |
| 991 | * increment a single u64 in the hope to catch on again 'soon'. |
| 992 | */ |
| 993 | if (unlikely(head & 7)) |
| 994 | head &= ~7ULL; |
| 995 | |
| 996 | size = 8; |
| 997 | } |
| 998 | |
| 999 | head += size; |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1000 | file_pos += size; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1001 | |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1002 | if (file_pos >= progress_next) { |
| 1003 | progress_next += file_size / 16; |
| 1004 | ui_progress__update(progress, file_pos); |
| 1005 | } |
| 1006 | |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1007 | if (file_pos < file_size) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1008 | goto more; |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1009 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1010 | err = 0; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 1011 | /* do the final flush for ordered samples */ |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1012 | session->ordered_samples.next_flush = ULLONG_MAX; |
| 1013 | flush_sample_queue(session, ops); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1014 | out_err: |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 1015 | ui_progress__delete(progress); |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 1016 | |
| 1017 | if (ops->lost == event__process_lost && |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1018 | session->hists.stats.total_lost != 0) { |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 1019 | ui__warning("Processed %Lu events and LOST %Lu!\n\n" |
| 1020 | "Check IO/CPU overload!\n\n", |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1021 | session->hists.stats.total_period, |
| 1022 | session->hists.stats.total_lost); |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 1023 | } |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1024 | |
| 1025 | if (session->hists.stats.nr_unknown_events != 0) { |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 1026 | ui__warning("Found %u unknown events!\n\n" |
| 1027 | "Is this an older tool processing a perf.data " |
| 1028 | "file generated by a more recent tool?\n\n" |
| 1029 | "If that is not the case, consider " |
| 1030 | "reporting to linux-kernel@vger.kernel.org.\n\n", |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1031 | session->hists.stats.nr_unknown_events); |
Arnaldo Carvalho de Melo | 068ffaa | 2010-11-27 02:41:01 -0200 | [diff] [blame] | 1032 | } |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1033 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 1034 | if (session->hists.stats.nr_invalid_chains != 0) { |
| 1035 | ui__warning("Found invalid callchains!\n\n" |
| 1036 | "%u out of %u events were discarded for this reason.\n\n" |
| 1037 | "Consider reporting to linux-kernel@vger.kernel.org.\n\n", |
| 1038 | session->hists.stats.nr_invalid_chains, |
| 1039 | session->hists.stats.nr_events[PERF_RECORD_SAMPLE]); |
| 1040 | } |
| 1041 | |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 1042 | perf_session_free_sample_buffers(session); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1043 | return err; |
| 1044 | } |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 1045 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1046 | int perf_session__process_events(struct perf_session *self, |
| 1047 | struct perf_event_ops *ops) |
| 1048 | { |
| 1049 | int err; |
| 1050 | |
| 1051 | if (perf_session__register_idle_thread(self) == NULL) |
| 1052 | return -ENOMEM; |
| 1053 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1054 | if (!self->fd_pipe) |
| 1055 | err = __perf_session__process_events(self, |
| 1056 | self->header.data_offset, |
| 1057 | self->header.data_size, |
| 1058 | self->size, ops); |
| 1059 | else |
| 1060 | err = __perf_session__process_pipe_events(self, ops); |
Dave Martin | 88ca895 | 2010-07-27 11:46:12 -0300 | [diff] [blame] | 1061 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1062 | return err; |
| 1063 | } |
| 1064 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1065 | 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] | 1066 | { |
| 1067 | if (!(self->sample_type & PERF_SAMPLE_RAW)) { |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1068 | pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); |
| 1069 | return false; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 1070 | } |
| 1071 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1072 | return true; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 1073 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1074 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1075 | 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] | 1076 | const char *symbol_name, |
| 1077 | u64 addr) |
| 1078 | { |
| 1079 | char *bracket; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1080 | enum map_type i; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1081 | struct ref_reloc_sym *ref; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1082 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1083 | ref = zalloc(sizeof(struct ref_reloc_sym)); |
| 1084 | if (ref == NULL) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1085 | return -ENOMEM; |
| 1086 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1087 | ref->name = strdup(symbol_name); |
| 1088 | if (ref->name == NULL) { |
| 1089 | free(ref); |
| 1090 | return -ENOMEM; |
| 1091 | } |
| 1092 | |
| 1093 | bracket = strchr(ref->name, ']'); |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1094 | if (bracket) |
| 1095 | *bracket = '\0'; |
| 1096 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1097 | ref->addr = addr; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1098 | |
| 1099 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1100 | struct kmap *kmap = map__kmap(maps[i]); |
| 1101 | kmap->ref_reloc_sym = ref; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1102 | } |
| 1103 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1104 | return 0; |
| 1105 | } |
Arnaldo Carvalho de Melo | 1f626bc | 2010-05-09 19:57:08 -0300 | [diff] [blame] | 1106 | |
| 1107 | size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp) |
| 1108 | { |
| 1109 | return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) + |
| 1110 | __dsos__fprintf(&self->host_machine.user_dsos, fp) + |
| 1111 | machines__fprintf_dsos(&self->machines, fp); |
| 1112 | } |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1113 | |
| 1114 | size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp, |
| 1115 | bool with_hits) |
| 1116 | { |
| 1117 | size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits); |
| 1118 | return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits); |
| 1119 | } |