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 | |
Arnaldo Carvalho de Melo | e248de3 | 2011-03-05 21:40:06 -0300 | [diff] [blame] | 10 | #include "evlist.h" |
| 11 | #include "evsel.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 12 | #include "session.h" |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 13 | #include "sort.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 14 | #include "util.h" |
Anton Blanchard | 5d67be9 | 2011-07-04 21:57:50 +1000 | [diff] [blame] | 15 | #include "cpumap.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 16 | |
| 17 | static int perf_session__open(struct perf_session *self, bool force) |
| 18 | { |
| 19 | struct stat input_stat; |
| 20 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 21 | if (!strcmp(self->filename, "-")) { |
| 22 | self->fd_pipe = true; |
| 23 | self->fd = STDIN_FILENO; |
| 24 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 25 | if (perf_session__read_header(self, self->fd) < 0) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 26 | pr_err("incompatible file format"); |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 31 | self->fd = open(self->filename, O_RDONLY); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 32 | if (self->fd < 0) { |
Andy Isaacson | 0f2c3de | 2010-06-11 20:36:15 -0700 | [diff] [blame] | 33 | int err = errno; |
| 34 | |
| 35 | pr_err("failed to open %s: %s", self->filename, strerror(err)); |
| 36 | if (err == ENOENT && !strcmp(self->filename, "perf.data")) |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 37 | pr_err(" (try 'perf record' first)"); |
| 38 | pr_err("\n"); |
| 39 | return -errno; |
| 40 | } |
| 41 | |
| 42 | if (fstat(self->fd, &input_stat) < 0) |
| 43 | goto out_close; |
| 44 | |
| 45 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { |
| 46 | pr_err("file %s not owned by current user or root\n", |
| 47 | self->filename); |
| 48 | goto out_close; |
| 49 | } |
| 50 | |
| 51 | if (!input_stat.st_size) { |
| 52 | pr_info("zero-sized file (%s), nothing to do!\n", |
| 53 | self->filename); |
| 54 | goto out_close; |
| 55 | } |
| 56 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 57 | if (perf_session__read_header(self, self->fd) < 0) { |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 58 | pr_err("incompatible file format"); |
| 59 | goto out_close; |
| 60 | } |
| 61 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 62 | if (!perf_evlist__valid_sample_type(self->evlist)) { |
| 63 | pr_err("non matching sample_type"); |
| 64 | goto out_close; |
| 65 | } |
| 66 | |
| 67 | if (!perf_evlist__valid_sample_id_all(self->evlist)) { |
| 68 | pr_err("non matching sample_id_all"); |
| 69 | goto out_close; |
| 70 | } |
| 71 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 72 | self->size = input_stat.st_size; |
| 73 | return 0; |
| 74 | |
| 75 | out_close: |
| 76 | close(self->fd); |
| 77 | self->fd = -1; |
| 78 | return -1; |
| 79 | } |
| 80 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 81 | static void perf_session__id_header_size(struct perf_session *session) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 82 | { |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 83 | struct perf_sample *data; |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 84 | u64 sample_type = session->sample_type; |
| 85 | u16 size = 0; |
| 86 | |
| 87 | if (!session->sample_id_all) |
| 88 | goto out; |
| 89 | |
| 90 | if (sample_type & PERF_SAMPLE_TID) |
| 91 | size += sizeof(data->tid) * 2; |
| 92 | |
| 93 | if (sample_type & PERF_SAMPLE_TIME) |
| 94 | size += sizeof(data->time); |
| 95 | |
| 96 | if (sample_type & PERF_SAMPLE_ID) |
| 97 | size += sizeof(data->id); |
| 98 | |
| 99 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 100 | size += sizeof(data->stream_id); |
| 101 | |
| 102 | if (sample_type & PERF_SAMPLE_CPU) |
| 103 | size += sizeof(data->cpu) * 2; |
| 104 | out: |
| 105 | session->id_hdr_size = size; |
| 106 | } |
| 107 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 108 | void perf_session__update_sample_type(struct perf_session *self) |
| 109 | { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 110 | self->sample_type = perf_evlist__sample_type(self->evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 111 | self->sample_size = __perf_evsel__sample_size(self->sample_type); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 112 | self->sample_id_all = perf_evlist__sample_id_all(self->evlist); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 113 | perf_session__id_header_size(self); |
| 114 | } |
| 115 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 116 | int perf_session__create_kernel_maps(struct perf_session *self) |
| 117 | { |
Arnaldo Carvalho de Melo | d118f8b | 2010-05-10 12:51:05 -0300 | [diff] [blame] | 118 | int ret = machine__create_kernel_maps(&self->host_machine); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 119 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 120 | if (ret >= 0) |
Arnaldo Carvalho de Melo | d118f8b | 2010-05-10 12:51:05 -0300 | [diff] [blame] | 121 | ret = machines__create_guest_kernel_maps(&self->machines); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 122 | return ret; |
| 123 | } |
| 124 | |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 125 | static void perf_session__destroy_kernel_maps(struct perf_session *self) |
| 126 | { |
| 127 | machine__destroy_kernel_maps(&self->host_machine); |
| 128 | machines__destroy_guest_kernel_maps(&self->machines); |
| 129 | } |
| 130 | |
Ian Munsie | 21ef97f | 2010-12-10 14:09:16 +1100 | [diff] [blame] | 131 | struct perf_session *perf_session__new(const char *filename, int mode, |
| 132 | bool force, bool repipe, |
| 133 | struct perf_event_ops *ops) |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 134 | { |
Arnaldo Carvalho de Melo | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 135 | size_t len = filename ? strlen(filename) + 1 : 0; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 136 | struct perf_session *self = zalloc(sizeof(*self) + len); |
| 137 | |
| 138 | if (self == NULL) |
| 139 | goto out; |
| 140 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 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 | b3165f4 | 2009-12-13 19:50:28 -0200 | [diff] [blame] | 144 | self->last_match = NULL; |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 145 | /* |
| 146 | * On 64bit we can mmap the data file in one go. No need for tiny mmap |
| 147 | * slices. On 32bit we use 32MB. |
| 148 | */ |
| 149 | #if BITS_PER_LONG == 64 |
| 150 | self->mmap_window = ULLONG_MAX; |
| 151 | #else |
| 152 | self->mmap_window = 32 * 1024 * 1024ULL; |
| 153 | #endif |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 154 | self->machines = RB_ROOT; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 155 | self->repipe = repipe; |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 156 | INIT_LIST_HEAD(&self->ordered_samples.samples); |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 157 | INIT_LIST_HEAD(&self->ordered_samples.sample_cache); |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 158 | INIT_LIST_HEAD(&self->ordered_samples.to_free); |
Arnaldo Carvalho de Melo | 1f626bc | 2010-05-09 19:57:08 -0300 | [diff] [blame] | 159 | machine__init(&self->host_machine, "", HOST_KERNEL_ID); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 160 | |
Arnaldo Carvalho de Melo | 64abebf7 | 2010-01-27 21:05:52 -0200 | [diff] [blame] | 161 | if (mode == O_RDONLY) { |
| 162 | if (perf_session__open(self, force) < 0) |
| 163 | goto out_delete; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 164 | perf_session__update_sample_type(self); |
Arnaldo Carvalho de Melo | 64abebf7 | 2010-01-27 21:05:52 -0200 | [diff] [blame] | 165 | } else if (mode == O_WRONLY) { |
| 166 | /* |
| 167 | * In O_RDONLY mode this will be performed when reading the |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 168 | * kernel MMAP event, in perf_event__process_mmap(). |
Arnaldo Carvalho de Melo | 64abebf7 | 2010-01-27 21:05:52 -0200 | [diff] [blame] | 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 | |
Ian Munsie | 21ef97f | 2010-12-10 14:09:16 +1100 | [diff] [blame] | 174 | if (ops && ops->ordering_requires_timestamps && |
| 175 | ops->ordered_samples && !self->sample_id_all) { |
| 176 | dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n"); |
| 177 | ops->ordered_samples = false; |
| 178 | } |
| 179 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 180 | out: |
| 181 | return self; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 182 | out_delete: |
| 183 | perf_session__delete(self); |
| 184 | return NULL; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 185 | } |
| 186 | |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 187 | static void perf_session__delete_dead_threads(struct perf_session *self) |
| 188 | { |
| 189 | struct thread *n, *t; |
| 190 | |
| 191 | list_for_each_entry_safe(t, n, &self->dead_threads, node) { |
| 192 | list_del(&t->node); |
| 193 | thread__delete(t); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static void perf_session__delete_threads(struct perf_session *self) |
| 198 | { |
| 199 | struct rb_node *nd = rb_first(&self->threads); |
| 200 | |
| 201 | while (nd) { |
| 202 | struct thread *t = rb_entry(nd, struct thread, rb_node); |
| 203 | |
| 204 | rb_erase(&t->rb_node, &self->threads); |
| 205 | nd = rb_next(nd); |
| 206 | thread__delete(t); |
| 207 | } |
| 208 | } |
| 209 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 210 | void perf_session__delete(struct perf_session *self) |
| 211 | { |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 212 | perf_session__destroy_kernel_maps(self); |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 213 | perf_session__delete_dead_threads(self); |
| 214 | perf_session__delete_threads(self); |
| 215 | machine__exit(&self->host_machine); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 216 | close(self->fd); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 217 | free(self); |
| 218 | } |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 219 | |
Arnaldo Carvalho de Melo | 720a3ae | 2010-06-17 08:37:44 -0300 | [diff] [blame] | 220 | void perf_session__remove_thread(struct perf_session *self, struct thread *th) |
| 221 | { |
Arnaldo Carvalho de Melo | 70597f2 | 2010-08-02 18:59:28 -0300 | [diff] [blame] | 222 | self->last_match = NULL; |
Arnaldo Carvalho de Melo | 720a3ae | 2010-06-17 08:37:44 -0300 | [diff] [blame] | 223 | rb_erase(&th->rb_node, &self->threads); |
| 224 | /* |
| 225 | * We may have references to this thread, for instance in some hist_entry |
| 226 | * instances, so just move them to a separate list. |
| 227 | */ |
| 228 | list_add_tail(&th->node, &self->dead_threads); |
| 229 | } |
| 230 | |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 231 | static bool symbol__match_parent_regex(struct symbol *sym) |
| 232 | { |
| 233 | if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) |
| 234 | return 1; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 239 | int perf_session__resolve_callchain(struct perf_session *self, |
| 240 | struct thread *thread, |
| 241 | struct ip_callchain *chain, |
| 242 | struct symbol **parent) |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 243 | { |
| 244 | u8 cpumode = PERF_RECORD_MISC_USER; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 245 | unsigned int i; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 246 | int err; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 247 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 248 | callchain_cursor_reset(&self->callchain_cursor); |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 249 | |
| 250 | for (i = 0; i < chain->nr; i++) { |
Sam Liao | d797fdc | 2011-06-07 23:49:46 +0800 | [diff] [blame] | 251 | u64 ip; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 252 | struct addr_location al; |
| 253 | |
Sam Liao | d797fdc | 2011-06-07 23:49:46 +0800 | [diff] [blame] | 254 | if (callchain_param.order == ORDER_CALLEE) |
| 255 | ip = chain->ips[i]; |
| 256 | else |
| 257 | ip = chain->ips[chain->nr - i - 1]; |
| 258 | |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 259 | if (ip >= PERF_CONTEXT_MAX) { |
| 260 | switch (ip) { |
| 261 | case PERF_CONTEXT_HV: |
| 262 | cpumode = PERF_RECORD_MISC_HYPERVISOR; break; |
| 263 | case PERF_CONTEXT_KERNEL: |
| 264 | cpumode = PERF_RECORD_MISC_KERNEL; break; |
| 265 | case PERF_CONTEXT_USER: |
| 266 | cpumode = PERF_RECORD_MISC_USER; break; |
| 267 | default: |
| 268 | break; |
| 269 | } |
| 270 | continue; |
| 271 | } |
| 272 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 273 | al.filtered = false; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 274 | thread__find_addr_location(thread, self, cpumode, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 275 | MAP__FUNCTION, thread->pid, ip, &al, NULL); |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 276 | if (al.sym != NULL) { |
| 277 | if (sort__has_parent && !*parent && |
| 278 | symbol__match_parent_regex(al.sym)) |
| 279 | *parent = al.sym; |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 280 | if (!symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 281 | break; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 282 | } |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 283 | |
| 284 | err = callchain_cursor_append(&self->callchain_cursor, |
| 285 | ip, al.map, al.sym); |
| 286 | if (err) |
| 287 | return err; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 288 | } |
| 289 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 290 | return 0; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 291 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 292 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 293 | static int process_event_synth_stub(union perf_event *event __used, |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 294 | struct perf_session *session __used) |
| 295 | { |
| 296 | dump_printf(": unhandled!\n"); |
| 297 | return 0; |
| 298 | } |
| 299 | |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 300 | static int process_event_sample_stub(union perf_event *event __used, |
| 301 | struct perf_sample *sample __used, |
| 302 | struct perf_evsel *evsel __used, |
| 303 | struct perf_session *session __used) |
| 304 | { |
| 305 | dump_printf(": unhandled!\n"); |
| 306 | return 0; |
| 307 | } |
| 308 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 309 | static int process_event_stub(union perf_event *event __used, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 310 | struct perf_sample *sample __used, |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 311 | struct perf_session *session __used) |
| 312 | { |
| 313 | dump_printf(": unhandled!\n"); |
| 314 | return 0; |
| 315 | } |
| 316 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 317 | static int process_finished_round_stub(union perf_event *event __used, |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 318 | struct perf_session *session __used, |
| 319 | struct perf_event_ops *ops __used) |
| 320 | { |
| 321 | dump_printf(": unhandled!\n"); |
| 322 | return 0; |
| 323 | } |
| 324 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 325 | static int process_finished_round(union perf_event *event, |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 326 | struct perf_session *session, |
| 327 | struct perf_event_ops *ops); |
| 328 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 329 | static void perf_event_ops__fill_defaults(struct perf_event_ops *handler) |
| 330 | { |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 331 | if (handler->sample == NULL) |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 332 | handler->sample = process_event_sample_stub; |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 333 | if (handler->mmap == NULL) |
| 334 | handler->mmap = process_event_stub; |
| 335 | if (handler->comm == NULL) |
| 336 | handler->comm = process_event_stub; |
| 337 | if (handler->fork == NULL) |
| 338 | handler->fork = process_event_stub; |
| 339 | if (handler->exit == NULL) |
| 340 | handler->exit = process_event_stub; |
| 341 | if (handler->lost == NULL) |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 342 | handler->lost = perf_event__process_lost; |
Arnaldo Carvalho de Melo | 55aa640 | 2009-12-27 21:37:05 -0200 | [diff] [blame] | 343 | if (handler->read == NULL) |
| 344 | handler->read = process_event_stub; |
| 345 | if (handler->throttle == NULL) |
| 346 | handler->throttle = process_event_stub; |
| 347 | if (handler->unthrottle == NULL) |
| 348 | handler->unthrottle = process_event_stub; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 349 | if (handler->attr == NULL) |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 350 | handler->attr = process_event_synth_stub; |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 351 | if (handler->event_type == NULL) |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 352 | handler->event_type = process_event_synth_stub; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 353 | if (handler->tracing_data == NULL) |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 354 | handler->tracing_data = process_event_synth_stub; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 355 | if (handler->build_id == NULL) |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 356 | handler->build_id = process_event_synth_stub; |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 357 | if (handler->finished_round == NULL) { |
| 358 | if (handler->ordered_samples) |
| 359 | handler->finished_round = process_finished_round; |
| 360 | else |
| 361 | handler->finished_round = process_finished_round_stub; |
| 362 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 363 | } |
| 364 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 365 | void mem_bswap_64(void *src, int byte_size) |
| 366 | { |
| 367 | u64 *m = src; |
| 368 | |
| 369 | while (byte_size > 0) { |
| 370 | *m = bswap_64(*m); |
| 371 | byte_size -= sizeof(u64); |
| 372 | ++m; |
| 373 | } |
| 374 | } |
| 375 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 376 | static void perf_event__all64_swap(union perf_event *event) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 377 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 378 | struct perf_event_header *hdr = &event->header; |
| 379 | mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr)); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 380 | } |
| 381 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 382 | static void perf_event__comm_swap(union perf_event *event) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 383 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 384 | event->comm.pid = bswap_32(event->comm.pid); |
| 385 | event->comm.tid = bswap_32(event->comm.tid); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 386 | } |
| 387 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 388 | static void perf_event__mmap_swap(union perf_event *event) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 389 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 390 | event->mmap.pid = bswap_32(event->mmap.pid); |
| 391 | event->mmap.tid = bswap_32(event->mmap.tid); |
| 392 | event->mmap.start = bswap_64(event->mmap.start); |
| 393 | event->mmap.len = bswap_64(event->mmap.len); |
| 394 | event->mmap.pgoff = bswap_64(event->mmap.pgoff); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 395 | } |
| 396 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 397 | static void perf_event__task_swap(union perf_event *event) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 398 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 399 | event->fork.pid = bswap_32(event->fork.pid); |
| 400 | event->fork.tid = bswap_32(event->fork.tid); |
| 401 | event->fork.ppid = bswap_32(event->fork.ppid); |
| 402 | event->fork.ptid = bswap_32(event->fork.ptid); |
| 403 | event->fork.time = bswap_64(event->fork.time); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 404 | } |
| 405 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 406 | static void perf_event__read_swap(union perf_event *event) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 407 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 408 | event->read.pid = bswap_32(event->read.pid); |
| 409 | event->read.tid = bswap_32(event->read.tid); |
| 410 | event->read.value = bswap_64(event->read.value); |
| 411 | event->read.time_enabled = bswap_64(event->read.time_enabled); |
| 412 | event->read.time_running = bswap_64(event->read.time_running); |
| 413 | event->read.id = bswap_64(event->read.id); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 414 | } |
| 415 | |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 416 | /* exported for swapping attributes in file header */ |
| 417 | void perf_event__attr_swap(struct perf_event_attr *attr) |
| 418 | { |
| 419 | attr->type = bswap_32(attr->type); |
| 420 | attr->size = bswap_32(attr->size); |
| 421 | attr->config = bswap_64(attr->config); |
| 422 | attr->sample_period = bswap_64(attr->sample_period); |
| 423 | attr->sample_type = bswap_64(attr->sample_type); |
| 424 | attr->read_format = bswap_64(attr->read_format); |
| 425 | attr->wakeup_events = bswap_32(attr->wakeup_events); |
| 426 | attr->bp_type = bswap_32(attr->bp_type); |
| 427 | attr->bp_addr = bswap_64(attr->bp_addr); |
| 428 | attr->bp_len = bswap_64(attr->bp_len); |
| 429 | } |
| 430 | |
| 431 | static void perf_event__hdr_attr_swap(union perf_event *event) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 432 | { |
| 433 | size_t size; |
| 434 | |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 435 | perf_event__attr_swap(&event->attr.attr); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 436 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 437 | size = event->header.size; |
| 438 | size -= (void *)&event->attr.id - (void *)event; |
| 439 | mem_bswap_64(event->attr.id, size); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 440 | } |
| 441 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 442 | static void perf_event__event_type_swap(union perf_event *event) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 443 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 444 | event->event_type.event_type.event_id = |
| 445 | bswap_64(event->event_type.event_type.event_id); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 446 | } |
| 447 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 448 | static void perf_event__tracing_data_swap(union perf_event *event) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 449 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 450 | event->tracing_data.size = bswap_32(event->tracing_data.size); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 451 | } |
| 452 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 453 | typedef void (*perf_event__swap_op)(union perf_event *event); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 454 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 455 | static perf_event__swap_op perf_event__swap_ops[] = { |
| 456 | [PERF_RECORD_MMAP] = perf_event__mmap_swap, |
| 457 | [PERF_RECORD_COMM] = perf_event__comm_swap, |
| 458 | [PERF_RECORD_FORK] = perf_event__task_swap, |
| 459 | [PERF_RECORD_EXIT] = perf_event__task_swap, |
| 460 | [PERF_RECORD_LOST] = perf_event__all64_swap, |
| 461 | [PERF_RECORD_READ] = perf_event__read_swap, |
| 462 | [PERF_RECORD_SAMPLE] = perf_event__all64_swap, |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 463 | [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 464 | [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap, |
| 465 | [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap, |
| 466 | [PERF_RECORD_HEADER_BUILD_ID] = NULL, |
| 467 | [PERF_RECORD_HEADER_MAX] = NULL, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 468 | }; |
| 469 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 470 | struct sample_queue { |
| 471 | u64 timestamp; |
Thomas Gleixner | e4c2df1 | 2010-12-07 12:48:50 +0000 | [diff] [blame] | 472 | u64 file_offset; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 473 | union perf_event *event; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 474 | struct list_head list; |
| 475 | }; |
| 476 | |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 477 | static void perf_session_free_sample_buffers(struct perf_session *session) |
| 478 | { |
| 479 | struct ordered_samples *os = &session->ordered_samples; |
| 480 | |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 481 | while (!list_empty(&os->to_free)) { |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 482 | struct sample_queue *sq; |
| 483 | |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 484 | sq = list_entry(os->to_free.next, struct sample_queue, list); |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 485 | list_del(&sq->list); |
| 486 | free(sq); |
| 487 | } |
| 488 | } |
| 489 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 490 | static int perf_session_deliver_event(struct perf_session *session, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 491 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 492 | struct perf_sample *sample, |
Thomas Gleixner | f74725d | 2010-12-07 12:48:53 +0000 | [diff] [blame] | 493 | struct perf_event_ops *ops, |
| 494 | u64 file_offset); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 495 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 496 | static void flush_sample_queue(struct perf_session *s, |
| 497 | struct perf_event_ops *ops) |
| 498 | { |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 499 | struct ordered_samples *os = &s->ordered_samples; |
| 500 | struct list_head *head = &os->samples; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 501 | struct sample_queue *tmp, *iter; |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 502 | struct perf_sample sample; |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 503 | u64 limit = os->next_flush; |
| 504 | u64 last_ts = os->last_sample ? os->last_sample->timestamp : 0ULL; |
Frederic Weisbecker | 5538bec | 2011-05-22 02:17:22 +0200 | [diff] [blame] | 505 | int ret; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 506 | |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 507 | if (!ops->ordered_samples || !limit) |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 508 | return; |
| 509 | |
| 510 | list_for_each_entry_safe(iter, tmp, head, list) { |
| 511 | if (iter->timestamp > limit) |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 512 | break; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 513 | |
Frederic Weisbecker | 5538bec | 2011-05-22 02:17:22 +0200 | [diff] [blame] | 514 | ret = perf_session__parse_sample(s, iter->event, &sample); |
| 515 | if (ret) |
| 516 | pr_err("Can't parse sample, err = %d\n", ret); |
| 517 | else |
| 518 | perf_session_deliver_event(s, iter->event, &sample, ops, |
| 519 | iter->file_offset); |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 520 | |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 521 | os->last_flush = iter->timestamp; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 522 | list_del(&iter->list); |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 523 | list_add(&iter->list, &os->sample_cache); |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 524 | } |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 525 | |
| 526 | if (list_empty(head)) { |
| 527 | os->last_sample = NULL; |
| 528 | } else if (last_ts <= limit) { |
| 529 | os->last_sample = |
| 530 | list_entry(head->prev, struct sample_queue, list); |
| 531 | } |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 532 | } |
| 533 | |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 534 | /* |
| 535 | * When perf record finishes a pass on every buffers, it records this pseudo |
| 536 | * event. |
| 537 | * We record the max timestamp t found in the pass n. |
| 538 | * Assuming these timestamps are monotonic across cpus, we know that if |
| 539 | * a buffer still has events with timestamps below t, they will be all |
| 540 | * available and then read in the pass n + 1. |
| 541 | * Hence when we start to read the pass n + 2, we can safely flush every |
| 542 | * events with timestamps below t. |
| 543 | * |
| 544 | * ============ PASS n ================= |
| 545 | * CPU 0 | CPU 1 |
| 546 | * | |
| 547 | * cnt1 timestamps | cnt2 timestamps |
| 548 | * 1 | 2 |
| 549 | * 2 | 3 |
| 550 | * - | 4 <--- max recorded |
| 551 | * |
| 552 | * ============ PASS n + 1 ============== |
| 553 | * CPU 0 | CPU 1 |
| 554 | * | |
| 555 | * cnt1 timestamps | cnt2 timestamps |
| 556 | * 3 | 5 |
| 557 | * 4 | 6 |
| 558 | * 5 | 7 <---- max recorded |
| 559 | * |
| 560 | * Flush every events below timestamp 4 |
| 561 | * |
| 562 | * ============ PASS n + 2 ============== |
| 563 | * CPU 0 | CPU 1 |
| 564 | * | |
| 565 | * cnt1 timestamps | cnt2 timestamps |
| 566 | * 6 | 8 |
| 567 | * 7 | 9 |
| 568 | * - | 10 |
| 569 | * |
| 570 | * Flush every events below timestamp 7 |
| 571 | * etc... |
| 572 | */ |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 573 | static int process_finished_round(union perf_event *event __used, |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 574 | struct perf_session *session, |
| 575 | struct perf_event_ops *ops) |
| 576 | { |
| 577 | flush_sample_queue(session, ops); |
| 578 | session->ordered_samples.next_flush = session->ordered_samples.max_timestamp; |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 583 | /* The queue is ordered by time */ |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 584 | static void __queue_event(struct sample_queue *new, struct perf_session *s) |
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 | struct ordered_samples *os = &s->ordered_samples; |
| 587 | struct sample_queue *sample = os->last_sample; |
| 588 | u64 timestamp = new->timestamp; |
| 589 | struct list_head *p; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 590 | |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 591 | os->last_sample = new; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 592 | |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 593 | if (!sample) { |
| 594 | list_add(&new->list, &os->samples); |
| 595 | os->max_timestamp = timestamp; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 596 | return; |
| 597 | } |
| 598 | |
| 599 | /* |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 600 | * last_sample might point to some random place in the list as it's |
| 601 | * the last queued event. We expect that the new event is close to |
| 602 | * this. |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 603 | */ |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 604 | if (sample->timestamp <= timestamp) { |
| 605 | while (sample->timestamp <= timestamp) { |
| 606 | p = sample->list.next; |
| 607 | if (p == &os->samples) { |
| 608 | list_add_tail(&new->list, &os->samples); |
| 609 | os->max_timestamp = timestamp; |
| 610 | return; |
| 611 | } |
| 612 | sample = list_entry(p, struct sample_queue, list); |
| 613 | } |
| 614 | list_add_tail(&new->list, &sample->list); |
| 615 | } else { |
| 616 | while (sample->timestamp > timestamp) { |
| 617 | p = sample->list.prev; |
| 618 | if (p == &os->samples) { |
| 619 | list_add(&new->list, &os->samples); |
| 620 | return; |
| 621 | } |
| 622 | sample = list_entry(p, struct sample_queue, list); |
| 623 | } |
| 624 | list_add(&new->list, &sample->list); |
| 625 | } |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 626 | } |
| 627 | |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 628 | #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue)) |
| 629 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 630 | static int perf_session_queue_event(struct perf_session *s, union perf_event *event, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 631 | struct perf_sample *sample, u64 file_offset) |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 632 | { |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 633 | struct ordered_samples *os = &s->ordered_samples; |
| 634 | struct list_head *sc = &os->sample_cache; |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 635 | u64 timestamp = sample->time; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 636 | struct sample_queue *new; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 637 | |
Thomas Gleixner | 79a14c1 | 2010-12-07 12:48:44 +0000 | [diff] [blame] | 638 | if (!timestamp || timestamp == ~0ULL) |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 639 | return -ETIME; |
| 640 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 641 | if (timestamp < s->ordered_samples.last_flush) { |
| 642 | printf("Warning: Timestamp below last timeslice flush\n"); |
| 643 | return -EINVAL; |
| 644 | } |
| 645 | |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 646 | if (!list_empty(sc)) { |
| 647 | new = list_entry(sc->next, struct sample_queue, list); |
| 648 | list_del(&new->list); |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 649 | } else if (os->sample_buffer) { |
| 650 | new = os->sample_buffer + os->sample_buffer_idx; |
| 651 | if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER) |
| 652 | os->sample_buffer = NULL; |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 653 | } else { |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 654 | os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new)); |
| 655 | if (!os->sample_buffer) |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 656 | return -ENOMEM; |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 657 | list_add(&os->sample_buffer->list, &os->to_free); |
| 658 | os->sample_buffer_idx = 2; |
| 659 | new = os->sample_buffer + 1; |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 660 | } |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 661 | |
| 662 | new->timestamp = timestamp; |
Thomas Gleixner | e4c2df1 | 2010-12-07 12:48:50 +0000 | [diff] [blame] | 663 | new->file_offset = file_offset; |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 664 | new->event = event; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 665 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 666 | __queue_event(new, s); |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 671 | static void callchain__printf(struct perf_sample *sample) |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 672 | { |
| 673 | unsigned int i; |
| 674 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 675 | printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 676 | |
| 677 | for (i = 0; i < sample->callchain->nr; i++) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 678 | printf("..... %2d: %016" PRIx64 "\n", |
| 679 | i, sample->callchain->ips[i]); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 680 | } |
| 681 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 682 | static void perf_session__print_tstamp(struct perf_session *session, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 683 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 684 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 685 | { |
| 686 | if (event->header.type != PERF_RECORD_SAMPLE && |
| 687 | !session->sample_id_all) { |
| 688 | fputs("-1 -1 ", stdout); |
| 689 | return; |
| 690 | } |
| 691 | |
| 692 | if ((session->sample_type & PERF_SAMPLE_CPU)) |
| 693 | printf("%u ", sample->cpu); |
| 694 | |
| 695 | if (session->sample_type & PERF_SAMPLE_TIME) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 696 | printf("%" PRIu64 " ", sample->time); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 697 | } |
| 698 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 699 | static void dump_event(struct perf_session *session, union perf_event *event, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 700 | u64 file_offset, struct perf_sample *sample) |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 701 | { |
| 702 | if (!dump_trace) |
| 703 | return; |
| 704 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 705 | printf("\n%#" PRIx64 " [%#x]: event: %d\n", |
| 706 | file_offset, event->header.size, event->header.type); |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 707 | |
| 708 | trace_event(event); |
| 709 | |
| 710 | if (sample) |
| 711 | perf_session__print_tstamp(session, event, sample); |
| 712 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 713 | printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 714 | event->header.size, perf_event__name(event->header.type)); |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 717 | static void dump_sample(struct perf_session *session, union perf_event *event, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 718 | struct perf_sample *sample) |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 719 | { |
Arnaldo Carvalho de Melo | ddbc24b | 2010-12-09 12:20:20 -0200 | [diff] [blame] | 720 | if (!dump_trace) |
| 721 | return; |
| 722 | |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 723 | printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n", |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 724 | event->header.misc, sample->pid, sample->tid, sample->ip, |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 725 | sample->period, sample->addr); |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 726 | |
| 727 | if (session->sample_type & PERF_SAMPLE_CALLCHAIN) |
Arnaldo Carvalho de Melo | ddbc24b | 2010-12-09 12:20:20 -0200 | [diff] [blame] | 728 | callchain__printf(sample); |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 731 | static int perf_session_deliver_event(struct perf_session *session, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 732 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 733 | struct perf_sample *sample, |
Thomas Gleixner | f74725d | 2010-12-07 12:48:53 +0000 | [diff] [blame] | 734 | struct perf_event_ops *ops, |
Thomas Gleixner | 532e726 | 2010-12-07 12:48:55 +0000 | [diff] [blame] | 735 | u64 file_offset) |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 736 | { |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 737 | struct perf_evsel *evsel; |
| 738 | |
Thomas Gleixner | 532e726 | 2010-12-07 12:48:55 +0000 | [diff] [blame] | 739 | dump_event(session, event, file_offset, sample); |
| 740 | |
Arnaldo Carvalho de Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame^] | 741 | evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
| 742 | if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) { |
| 743 | /* |
| 744 | * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here |
| 745 | * because the tools right now may apply filters, discarding |
| 746 | * some of the samples. For consistency, in the future we |
| 747 | * should have something like nr_filtered_samples and remove |
| 748 | * the sample->period from total_sample_period, etc, KISS for |
| 749 | * now tho. |
| 750 | * |
| 751 | * Also testing against NULL allows us to handle files without |
| 752 | * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the |
| 753 | * future probably it'll be a good idea to restrict event |
| 754 | * processing via perf_session to files with both set. |
| 755 | */ |
| 756 | hists__inc_nr_events(&evsel->hists, event->header.type); |
| 757 | } |
| 758 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 759 | switch (event->header.type) { |
| 760 | case PERF_RECORD_SAMPLE: |
Thomas Gleixner | 532e726 | 2010-12-07 12:48:55 +0000 | [diff] [blame] | 761 | dump_sample(session, event, sample); |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 762 | if (evsel == NULL) { |
| 763 | ++session->hists.stats.nr_unknown_id; |
| 764 | return -1; |
| 765 | } |
| 766 | return ops->sample(event, sample, evsel, session); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 767 | case PERF_RECORD_MMAP: |
| 768 | return ops->mmap(event, sample, session); |
| 769 | case PERF_RECORD_COMM: |
| 770 | return ops->comm(event, sample, session); |
| 771 | case PERF_RECORD_FORK: |
| 772 | return ops->fork(event, sample, session); |
| 773 | case PERF_RECORD_EXIT: |
| 774 | return ops->exit(event, sample, session); |
| 775 | case PERF_RECORD_LOST: |
| 776 | return ops->lost(event, sample, session); |
| 777 | case PERF_RECORD_READ: |
| 778 | return ops->read(event, sample, session); |
| 779 | case PERF_RECORD_THROTTLE: |
| 780 | return ops->throttle(event, sample, session); |
| 781 | case PERF_RECORD_UNTHROTTLE: |
| 782 | return ops->unthrottle(event, sample, session); |
| 783 | default: |
| 784 | ++session->hists.stats.nr_unknown_events; |
| 785 | return -1; |
| 786 | } |
| 787 | } |
| 788 | |
Thomas Gleixner | 3dfc2c0 | 2010-12-07 12:48:58 +0000 | [diff] [blame] | 789 | static int perf_session__preprocess_sample(struct perf_session *session, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 790 | union perf_event *event, struct perf_sample *sample) |
Thomas Gleixner | 3dfc2c0 | 2010-12-07 12:48:58 +0000 | [diff] [blame] | 791 | { |
| 792 | if (event->header.type != PERF_RECORD_SAMPLE || |
| 793 | !(session->sample_type & PERF_SAMPLE_CALLCHAIN)) |
| 794 | return 0; |
| 795 | |
| 796 | if (!ip_callchain__valid(sample->callchain, event)) { |
| 797 | pr_debug("call-chain problem with event, skipping it.\n"); |
| 798 | ++session->hists.stats.nr_invalid_chains; |
| 799 | session->hists.stats.total_invalid_chains += sample->period; |
| 800 | return -EINVAL; |
| 801 | } |
| 802 | return 0; |
| 803 | } |
| 804 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 805 | static int perf_session__process_user_event(struct perf_session *session, union perf_event *event, |
Thomas Gleixner | ba74f06 | 2010-12-07 12:49:01 +0000 | [diff] [blame] | 806 | struct perf_event_ops *ops, u64 file_offset) |
| 807 | { |
| 808 | dump_event(session, event, file_offset, NULL); |
| 809 | |
| 810 | /* These events are processed right away */ |
| 811 | switch (event->header.type) { |
| 812 | case PERF_RECORD_HEADER_ATTR: |
| 813 | return ops->attr(event, session); |
| 814 | case PERF_RECORD_HEADER_EVENT_TYPE: |
| 815 | return ops->event_type(event, session); |
| 816 | case PERF_RECORD_HEADER_TRACING_DATA: |
| 817 | /* setup for reading amidst mmap */ |
| 818 | lseek(session->fd, file_offset, SEEK_SET); |
| 819 | return ops->tracing_data(event, session); |
| 820 | case PERF_RECORD_HEADER_BUILD_ID: |
| 821 | return ops->build_id(event, session); |
| 822 | case PERF_RECORD_FINISHED_ROUND: |
| 823 | return ops->finished_round(event, session, ops); |
| 824 | default: |
| 825 | return -EINVAL; |
| 826 | } |
| 827 | } |
| 828 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 829 | static int perf_session__process_event(struct perf_session *session, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 830 | union perf_event *event, |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 831 | struct perf_event_ops *ops, |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 832 | u64 file_offset) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 833 | { |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 834 | struct perf_sample sample; |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 835 | int ret; |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 836 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 837 | if (session->header.needs_swap && |
| 838 | perf_event__swap_ops[event->header.type]) |
| 839 | perf_event__swap_ops[event->header.type](event); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 840 | |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 841 | if (event->header.type >= PERF_RECORD_HEADER_MAX) |
| 842 | return -EINVAL; |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 843 | |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 844 | hists__inc_nr_events(&session->hists, event->header.type); |
| 845 | |
| 846 | if (event->header.type >= PERF_RECORD_USER_TYPE_START) |
Thomas Gleixner | ba74f06 | 2010-12-07 12:49:01 +0000 | [diff] [blame] | 847 | return perf_session__process_user_event(session, event, ops, file_offset); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 848 | |
Thomas Gleixner | 3dfc2c0 | 2010-12-07 12:48:58 +0000 | [diff] [blame] | 849 | /* |
| 850 | * For all kernel events we get the sample data |
| 851 | */ |
Frederic Weisbecker | 5538bec | 2011-05-22 02:17:22 +0200 | [diff] [blame] | 852 | ret = perf_session__parse_sample(session, event, &sample); |
| 853 | if (ret) |
| 854 | return ret; |
Thomas Gleixner | 3dfc2c0 | 2010-12-07 12:48:58 +0000 | [diff] [blame] | 855 | |
| 856 | /* Preprocess sample records - precheck callchains */ |
| 857 | if (perf_session__preprocess_sample(session, event, &sample)) |
| 858 | return 0; |
| 859 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 860 | if (ops->ordered_samples) { |
Thomas Gleixner | e4c2df1 | 2010-12-07 12:48:50 +0000 | [diff] [blame] | 861 | ret = perf_session_queue_event(session, event, &sample, |
| 862 | file_offset); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 863 | if (ret != -ETIME) |
| 864 | return ret; |
| 865 | } |
| 866 | |
Thomas Gleixner | f74725d | 2010-12-07 12:48:53 +0000 | [diff] [blame] | 867 | return perf_session_deliver_event(session, event, &sample, ops, |
| 868 | file_offset); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 869 | } |
| 870 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 871 | void perf_event_header__bswap(struct perf_event_header *self) |
| 872 | { |
| 873 | self->type = bswap_32(self->type); |
| 874 | self->misc = bswap_16(self->misc); |
| 875 | self->size = bswap_16(self->size); |
| 876 | } |
| 877 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 878 | static struct thread *perf_session__register_idle_thread(struct perf_session *self) |
| 879 | { |
| 880 | struct thread *thread = perf_session__findnew(self, 0); |
| 881 | |
| 882 | if (thread == NULL || thread__set_comm(thread, "swapper")) { |
| 883 | pr_err("problem inserting idle task.\n"); |
| 884 | thread = NULL; |
| 885 | } |
| 886 | |
| 887 | return thread; |
| 888 | } |
| 889 | |
Arnaldo Carvalho de Melo | 1109599 | 2011-01-04 16:25:15 -0200 | [diff] [blame] | 890 | static void perf_session__warn_about_errors(const struct perf_session *session, |
| 891 | const struct perf_event_ops *ops) |
| 892 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 893 | if (ops->lost == perf_event__process_lost && |
Arnaldo Carvalho de Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame^] | 894 | session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) { |
| 895 | ui__warning("Processed %d events and lost %d chunks!\n\n" |
| 896 | "Check IO/CPU overload!\n\n", |
| 897 | session->hists.stats.nr_events[0], |
| 898 | session->hists.stats.nr_events[PERF_RECORD_LOST]); |
Arnaldo Carvalho de Melo | 1109599 | 2011-01-04 16:25:15 -0200 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | if (session->hists.stats.nr_unknown_events != 0) { |
| 902 | ui__warning("Found %u unknown events!\n\n" |
| 903 | "Is this an older tool processing a perf.data " |
| 904 | "file generated by a more recent tool?\n\n" |
| 905 | "If that is not the case, consider " |
| 906 | "reporting to linux-kernel@vger.kernel.org.\n\n", |
| 907 | session->hists.stats.nr_unknown_events); |
| 908 | } |
| 909 | |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 910 | if (session->hists.stats.nr_unknown_id != 0) { |
| 911 | ui__warning("%u samples with id not present in the header\n", |
| 912 | session->hists.stats.nr_unknown_id); |
| 913 | } |
| 914 | |
Arnaldo Carvalho de Melo | 1109599 | 2011-01-04 16:25:15 -0200 | [diff] [blame] | 915 | if (session->hists.stats.nr_invalid_chains != 0) { |
| 916 | ui__warning("Found invalid callchains!\n\n" |
| 917 | "%u out of %u events were discarded for this reason.\n\n" |
| 918 | "Consider reporting to linux-kernel@vger.kernel.org.\n\n", |
| 919 | session->hists.stats.nr_invalid_chains, |
| 920 | session->hists.stats.nr_events[PERF_RECORD_SAMPLE]); |
| 921 | } |
| 922 | } |
| 923 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 924 | #define session_done() (*(volatile int *)(&session_done)) |
| 925 | volatile int session_done; |
| 926 | |
| 927 | static int __perf_session__process_pipe_events(struct perf_session *self, |
| 928 | struct perf_event_ops *ops) |
| 929 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 930 | union perf_event event; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 931 | uint32_t size; |
| 932 | int skip = 0; |
| 933 | u64 head; |
| 934 | int err; |
| 935 | void *p; |
| 936 | |
| 937 | perf_event_ops__fill_defaults(ops); |
| 938 | |
| 939 | head = 0; |
| 940 | more: |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 941 | err = readn(self->fd, &event, sizeof(struct perf_event_header)); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 942 | if (err <= 0) { |
| 943 | if (err == 0) |
| 944 | goto done; |
| 945 | |
| 946 | pr_err("failed to read event header\n"); |
| 947 | goto out_err; |
| 948 | } |
| 949 | |
| 950 | if (self->header.needs_swap) |
| 951 | perf_event_header__bswap(&event.header); |
| 952 | |
| 953 | size = event.header.size; |
| 954 | if (size == 0) |
| 955 | size = 8; |
| 956 | |
| 957 | p = &event; |
| 958 | p += sizeof(struct perf_event_header); |
| 959 | |
Tom Zanussi | 794e43b | 2010-05-05 00:27:40 -0500 | [diff] [blame] | 960 | if (size - sizeof(struct perf_event_header)) { |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 961 | err = readn(self->fd, p, size - sizeof(struct perf_event_header)); |
Tom Zanussi | 794e43b | 2010-05-05 00:27:40 -0500 | [diff] [blame] | 962 | if (err <= 0) { |
| 963 | if (err == 0) { |
| 964 | pr_err("unexpected end of event stream\n"); |
| 965 | goto done; |
| 966 | } |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 967 | |
Tom Zanussi | 794e43b | 2010-05-05 00:27:40 -0500 | [diff] [blame] | 968 | pr_err("failed to read event data\n"); |
| 969 | goto out_err; |
| 970 | } |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | if (size == 0 || |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 974 | (skip = perf_session__process_event(self, &event, ops, head)) < 0) { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 975 | dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n", |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 976 | head, event.header.size, event.header.type); |
| 977 | /* |
| 978 | * assume we lost track of the stream, check alignment, and |
| 979 | * increment a single u64 in the hope to catch on again 'soon'. |
| 980 | */ |
| 981 | if (unlikely(head & 7)) |
| 982 | head &= ~7ULL; |
| 983 | |
| 984 | size = 8; |
| 985 | } |
| 986 | |
| 987 | head += size; |
| 988 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 989 | if (skip > 0) |
| 990 | head += skip; |
| 991 | |
| 992 | if (!session_done()) |
| 993 | goto more; |
| 994 | done: |
| 995 | err = 0; |
| 996 | out_err: |
Arnaldo Carvalho de Melo | 1109599 | 2011-01-04 16:25:15 -0200 | [diff] [blame] | 997 | perf_session__warn_about_errors(self, ops); |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 998 | perf_session_free_sample_buffers(self); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 999 | return err; |
| 1000 | } |
| 1001 | |
Frederic Weisbecker | 998bedc | 2011-05-23 13:06:28 +0200 | [diff] [blame] | 1002 | static union perf_event * |
| 1003 | fetch_mmaped_event(struct perf_session *session, |
| 1004 | u64 head, size_t mmap_size, char *buf) |
| 1005 | { |
| 1006 | union perf_event *event; |
| 1007 | |
| 1008 | /* |
| 1009 | * Ensure we have enough space remaining to read |
| 1010 | * the size of the event in the headers. |
| 1011 | */ |
| 1012 | if (head + sizeof(event->header) > mmap_size) |
| 1013 | return NULL; |
| 1014 | |
| 1015 | event = (union perf_event *)(buf + head); |
| 1016 | |
| 1017 | if (session->header.needs_swap) |
| 1018 | perf_event_header__bswap(&event->header); |
| 1019 | |
| 1020 | if (head + event->header.size > mmap_size) |
| 1021 | return NULL; |
| 1022 | |
| 1023 | return event; |
| 1024 | } |
| 1025 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1026 | int __perf_session__process_events(struct perf_session *session, |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1027 | u64 data_offset, u64 data_size, |
| 1028 | u64 file_size, struct perf_event_ops *ops) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1029 | { |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1030 | u64 head, page_offset, file_offset, file_pos, progress_next; |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 1031 | int err, mmap_prot, mmap_flags, map_idx = 0; |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1032 | size_t page_size, mmap_size; |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 1033 | char *buf, *mmaps[8]; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1034 | union perf_event *event; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1035 | uint32_t size; |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1036 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1037 | perf_event_ops__fill_defaults(ops); |
| 1038 | |
Arnaldo Carvalho de Melo | 1b75962 | 2010-01-14 18:30:04 -0200 | [diff] [blame] | 1039 | page_size = sysconf(_SC_PAGESIZE); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1040 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1041 | page_offset = page_size * (data_offset / page_size); |
| 1042 | file_offset = page_offset; |
| 1043 | head = data_offset - page_offset; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1044 | |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1045 | if (data_offset + data_size < file_size) |
| 1046 | file_size = data_offset + data_size; |
| 1047 | |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1048 | progress_next = file_size / 16; |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1049 | |
| 1050 | mmap_size = session->mmap_window; |
| 1051 | if (mmap_size > file_size) |
| 1052 | mmap_size = file_size; |
| 1053 | |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 1054 | memset(mmaps, 0, sizeof(mmaps)); |
| 1055 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1056 | mmap_prot = PROT_READ; |
| 1057 | mmap_flags = MAP_SHARED; |
| 1058 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1059 | if (session->header.needs_swap) { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1060 | mmap_prot |= PROT_WRITE; |
| 1061 | mmap_flags = MAP_PRIVATE; |
| 1062 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1063 | remap: |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1064 | buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd, |
| 1065 | file_offset); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1066 | if (buf == MAP_FAILED) { |
| 1067 | pr_err("failed to mmap file\n"); |
| 1068 | err = -errno; |
| 1069 | goto out_err; |
| 1070 | } |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 1071 | mmaps[map_idx] = buf; |
| 1072 | map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1); |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1073 | file_pos = file_offset + head; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1074 | |
| 1075 | more: |
Frederic Weisbecker | 998bedc | 2011-05-23 13:06:28 +0200 | [diff] [blame] | 1076 | event = fetch_mmaped_event(session, head, mmap_size, buf); |
| 1077 | if (!event) { |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 1078 | if (mmaps[map_idx]) { |
| 1079 | munmap(mmaps[map_idx], mmap_size); |
| 1080 | mmaps[map_idx] = NULL; |
| 1081 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1082 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1083 | page_offset = page_size * (head / page_size); |
| 1084 | file_offset += page_offset; |
| 1085 | head -= page_offset; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1086 | goto remap; |
| 1087 | } |
| 1088 | |
| 1089 | size = event->header.size; |
| 1090 | |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1091 | if (size == 0 || |
| 1092 | perf_session__process_event(session, event, ops, file_pos) < 0) { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 1093 | dump_printf("%#" PRIx64 " [%#x]: skipping unknown header type: %d\n", |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1094 | file_offset + head, event->header.size, |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1095 | event->header.type); |
| 1096 | /* |
| 1097 | * assume we lost track of the stream, check alignment, and |
| 1098 | * increment a single u64 in the hope to catch on again 'soon'. |
| 1099 | */ |
| 1100 | if (unlikely(head & 7)) |
| 1101 | head &= ~7ULL; |
| 1102 | |
| 1103 | size = 8; |
| 1104 | } |
| 1105 | |
| 1106 | head += size; |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1107 | file_pos += size; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1108 | |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1109 | if (file_pos >= progress_next) { |
| 1110 | progress_next += file_size / 16; |
Arnaldo Carvalho de Melo | ca59bcb | 2011-10-25 13:29:11 -0200 | [diff] [blame] | 1111 | ui_progress__update(file_pos, file_size, |
| 1112 | "Processing events..."); |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1115 | if (file_pos < file_size) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1116 | goto more; |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1117 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1118 | err = 0; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 1119 | /* do the final flush for ordered samples */ |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1120 | session->ordered_samples.next_flush = ULLONG_MAX; |
| 1121 | flush_sample_queue(session, ops); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1122 | out_err: |
Arnaldo Carvalho de Melo | 1109599 | 2011-01-04 16:25:15 -0200 | [diff] [blame] | 1123 | perf_session__warn_about_errors(session, ops); |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 1124 | perf_session_free_sample_buffers(session); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1125 | return err; |
| 1126 | } |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 1127 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1128 | int perf_session__process_events(struct perf_session *self, |
| 1129 | struct perf_event_ops *ops) |
| 1130 | { |
| 1131 | int err; |
| 1132 | |
| 1133 | if (perf_session__register_idle_thread(self) == NULL) |
| 1134 | return -ENOMEM; |
| 1135 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1136 | if (!self->fd_pipe) |
| 1137 | err = __perf_session__process_events(self, |
| 1138 | self->header.data_offset, |
| 1139 | self->header.data_size, |
| 1140 | self->size, ops); |
| 1141 | else |
| 1142 | err = __perf_session__process_pipe_events(self, ops); |
Dave Martin | 88ca895 | 2010-07-27 11:46:12 -0300 | [diff] [blame] | 1143 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1144 | return err; |
| 1145 | } |
| 1146 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1147 | 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] | 1148 | { |
| 1149 | if (!(self->sample_type & PERF_SAMPLE_RAW)) { |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1150 | pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); |
| 1151 | return false; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 1152 | } |
| 1153 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1154 | return true; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 1155 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1156 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1157 | 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] | 1158 | const char *symbol_name, |
| 1159 | u64 addr) |
| 1160 | { |
| 1161 | char *bracket; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1162 | enum map_type i; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1163 | struct ref_reloc_sym *ref; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1164 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1165 | ref = zalloc(sizeof(struct ref_reloc_sym)); |
| 1166 | if (ref == NULL) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1167 | return -ENOMEM; |
| 1168 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1169 | ref->name = strdup(symbol_name); |
| 1170 | if (ref->name == NULL) { |
| 1171 | free(ref); |
| 1172 | return -ENOMEM; |
| 1173 | } |
| 1174 | |
| 1175 | bracket = strchr(ref->name, ']'); |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1176 | if (bracket) |
| 1177 | *bracket = '\0'; |
| 1178 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1179 | ref->addr = addr; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1180 | |
| 1181 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1182 | struct kmap *kmap = map__kmap(maps[i]); |
| 1183 | kmap->ref_reloc_sym = ref; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1184 | } |
| 1185 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1186 | return 0; |
| 1187 | } |
Arnaldo Carvalho de Melo | 1f626bc | 2010-05-09 19:57:08 -0300 | [diff] [blame] | 1188 | |
| 1189 | size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp) |
| 1190 | { |
| 1191 | return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) + |
| 1192 | __dsos__fprintf(&self->host_machine.user_dsos, fp) + |
| 1193 | machines__fprintf_dsos(&self->machines, fp); |
| 1194 | } |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1195 | |
| 1196 | size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp, |
| 1197 | bool with_hits) |
| 1198 | { |
| 1199 | size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits); |
| 1200 | return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits); |
| 1201 | } |
Arnaldo Carvalho de Melo | e248de3 | 2011-03-05 21:40:06 -0300 | [diff] [blame] | 1202 | |
| 1203 | size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp) |
| 1204 | { |
| 1205 | struct perf_evsel *pos; |
| 1206 | size_t ret = fprintf(fp, "Aggregated stats:\n"); |
| 1207 | |
| 1208 | ret += hists__fprintf_nr_events(&session->hists, fp); |
| 1209 | |
| 1210 | list_for_each_entry(pos, &session->evlist->entries, node) { |
| 1211 | ret += fprintf(fp, "%s stats:\n", event_name(pos)); |
| 1212 | ret += hists__fprintf_nr_events(&pos->hists, fp); |
| 1213 | } |
| 1214 | |
| 1215 | return ret; |
| 1216 | } |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1217 | |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 1218 | struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session, |
| 1219 | unsigned int type) |
| 1220 | { |
| 1221 | struct perf_evsel *pos; |
| 1222 | |
| 1223 | list_for_each_entry(pos, &session->evlist->entries, node) { |
| 1224 | if (pos->attr.type == type) |
| 1225 | return pos; |
| 1226 | } |
| 1227 | return NULL; |
| 1228 | } |
| 1229 | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 1230 | void perf_session__print_ip(union perf_event *event, |
| 1231 | struct perf_sample *sample, |
| 1232 | struct perf_session *session, |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame] | 1233 | int print_sym, int print_dso) |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1234 | { |
| 1235 | struct addr_location al; |
| 1236 | const char *symname, *dsoname; |
| 1237 | struct callchain_cursor *cursor = &session->callchain_cursor; |
| 1238 | struct callchain_cursor_node *node; |
| 1239 | |
| 1240 | if (perf_event__preprocess_sample(event, session, &al, sample, |
| 1241 | NULL) < 0) { |
| 1242 | error("problem processing %d event, skipping it.\n", |
| 1243 | event->header.type); |
| 1244 | return; |
| 1245 | } |
| 1246 | |
| 1247 | if (symbol_conf.use_callchain && sample->callchain) { |
| 1248 | |
| 1249 | if (perf_session__resolve_callchain(session, al.thread, |
| 1250 | sample->callchain, NULL) != 0) { |
| 1251 | if (verbose) |
| 1252 | error("Failed to resolve callchain. Skipping\n"); |
| 1253 | return; |
| 1254 | } |
| 1255 | callchain_cursor_commit(cursor); |
| 1256 | |
| 1257 | while (1) { |
| 1258 | node = callchain_cursor_current(cursor); |
| 1259 | if (!node) |
| 1260 | break; |
| 1261 | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 1262 | printf("\t%16" PRIx64, node->ip); |
| 1263 | if (print_sym) { |
| 1264 | if (node->sym && node->sym->name) |
| 1265 | symname = node->sym->name; |
| 1266 | else |
| 1267 | symname = ""; |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1268 | |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame] | 1269 | printf(" %s", symname); |
| 1270 | } |
| 1271 | if (print_dso) { |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 1272 | if (node->map && node->map->dso && node->map->dso->name) |
| 1273 | dsoname = node->map->dso->name; |
| 1274 | else |
| 1275 | dsoname = ""; |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1276 | |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame] | 1277 | printf(" (%s)", dsoname); |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 1278 | } |
| 1279 | printf("\n"); |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1280 | |
| 1281 | callchain_cursor_advance(cursor); |
| 1282 | } |
| 1283 | |
| 1284 | } else { |
David Ahern | adc4bf9 | 2011-05-30 09:16:27 -0600 | [diff] [blame] | 1285 | printf("%16" PRIx64, sample->ip); |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 1286 | if (print_sym) { |
| 1287 | if (al.sym && al.sym->name) |
| 1288 | symname = al.sym->name; |
| 1289 | else |
| 1290 | symname = ""; |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1291 | |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame] | 1292 | printf(" %s", symname); |
| 1293 | } |
| 1294 | |
| 1295 | if (print_dso) { |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 1296 | if (al.map && al.map->dso && al.map->dso->name) |
| 1297 | dsoname = al.map->dso->name; |
| 1298 | else |
| 1299 | dsoname = ""; |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1300 | |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame] | 1301 | printf(" (%s)", dsoname); |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 1302 | } |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1303 | } |
| 1304 | } |
Anton Blanchard | 5d67be9 | 2011-07-04 21:57:50 +1000 | [diff] [blame] | 1305 | |
| 1306 | int perf_session__cpu_bitmap(struct perf_session *session, |
| 1307 | const char *cpu_list, unsigned long *cpu_bitmap) |
| 1308 | { |
| 1309 | int i; |
| 1310 | struct cpu_map *map; |
| 1311 | |
| 1312 | for (i = 0; i < PERF_TYPE_MAX; ++i) { |
| 1313 | struct perf_evsel *evsel; |
| 1314 | |
| 1315 | evsel = perf_session__find_first_evtype(session, i); |
| 1316 | if (!evsel) |
| 1317 | continue; |
| 1318 | |
| 1319 | if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) { |
| 1320 | pr_err("File does not contain CPU events. " |
| 1321 | "Remove -c option to proceed.\n"); |
| 1322 | return -1; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | map = cpu_map__new(cpu_list); |
| 1327 | |
| 1328 | for (i = 0; i < map->nr; i++) { |
| 1329 | int cpu = map->map[i]; |
| 1330 | |
| 1331 | if (cpu >= MAX_NR_CPUS) { |
| 1332 | pr_err("Requested CPU %d too large. " |
| 1333 | "Consider raising MAX_NR_CPUS\n", cpu); |
| 1334 | return -1; |
| 1335 | } |
| 1336 | |
| 1337 | set_bit(cpu, cpu_bitmap); |
| 1338 | } |
| 1339 | |
| 1340 | return 0; |
| 1341 | } |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1342 | |
| 1343 | void perf_session__fprintf_info(struct perf_session *session, FILE *fp, |
| 1344 | bool full) |
| 1345 | { |
| 1346 | struct stat st; |
| 1347 | int ret; |
| 1348 | |
| 1349 | if (session == NULL || fp == NULL) |
| 1350 | return; |
| 1351 | |
| 1352 | ret = fstat(session->fd, &st); |
| 1353 | if (ret == -1) |
| 1354 | return; |
| 1355 | |
| 1356 | fprintf(fp, "# ========\n"); |
| 1357 | fprintf(fp, "# captured on: %s", ctime(&st.st_ctime)); |
| 1358 | perf_header__fprintf_info(session, fp, full); |
| 1359 | fprintf(fp, "# ========\n#\n"); |
| 1360 | } |