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