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