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 | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 17 | #include "event-parse.h" |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 18 | #include "perf_regs.h" |
Jiri Olsa | 71ad0f5 | 2012-08-07 15:20:46 +0200 | [diff] [blame] | 19 | #include "unwind.h" |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 20 | |
| 21 | static int perf_session__open(struct perf_session *self, bool force) |
| 22 | { |
| 23 | struct stat input_stat; |
| 24 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 25 | if (!strcmp(self->filename, "-")) { |
| 26 | self->fd_pipe = true; |
| 27 | self->fd = STDIN_FILENO; |
| 28 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 29 | if (perf_session__read_header(self, self->fd) < 0) |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 30 | pr_err("incompatible file format (rerun with -v to learn more)"); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 35 | self->fd = open(self->filename, O_RDONLY); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 36 | if (self->fd < 0) { |
Andy Isaacson | 0f2c3de | 2010-06-11 20:36:15 -0700 | [diff] [blame] | 37 | int err = errno; |
| 38 | |
| 39 | pr_err("failed to open %s: %s", self->filename, strerror(err)); |
| 40 | if (err == ENOENT && !strcmp(self->filename, "perf.data")) |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 41 | pr_err(" (try 'perf record' first)"); |
| 42 | pr_err("\n"); |
| 43 | return -errno; |
| 44 | } |
| 45 | |
| 46 | if (fstat(self->fd, &input_stat) < 0) |
| 47 | goto out_close; |
| 48 | |
| 49 | if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) { |
| 50 | pr_err("file %s not owned by current user or root\n", |
| 51 | self->filename); |
| 52 | goto out_close; |
| 53 | } |
| 54 | |
| 55 | if (!input_stat.st_size) { |
| 56 | pr_info("zero-sized file (%s), nothing to do!\n", |
| 57 | self->filename); |
| 58 | goto out_close; |
| 59 | } |
| 60 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 61 | if (perf_session__read_header(self, self->fd) < 0) { |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 62 | pr_err("incompatible file format (rerun with -v to learn more)"); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 63 | goto out_close; |
| 64 | } |
| 65 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 66 | if (!perf_evlist__valid_sample_type(self->evlist)) { |
| 67 | pr_err("non matching sample_type"); |
| 68 | goto out_close; |
| 69 | } |
| 70 | |
| 71 | if (!perf_evlist__valid_sample_id_all(self->evlist)) { |
| 72 | pr_err("non matching sample_id_all"); |
| 73 | goto out_close; |
| 74 | } |
| 75 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 76 | self->size = input_stat.st_size; |
| 77 | return 0; |
| 78 | |
| 79 | out_close: |
| 80 | close(self->fd); |
| 81 | self->fd = -1; |
| 82 | return -1; |
| 83 | } |
| 84 | |
Arnaldo Carvalho de Melo | 7b56cce | 2012-08-01 19:31:00 -0300 | [diff] [blame] | 85 | void perf_session__set_id_hdr_size(struct perf_session *session) |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 86 | { |
Arnaldo Carvalho de Melo | 7b56cce | 2012-08-01 19:31:00 -0300 | [diff] [blame] | 87 | u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist); |
| 88 | |
| 89 | session->host_machine.id_hdr_size = id_hdr_size; |
| 90 | machines__set_id_hdr_size(&session->machines, id_hdr_size); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 91 | } |
| 92 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 93 | int perf_session__create_kernel_maps(struct perf_session *self) |
| 94 | { |
Arnaldo Carvalho de Melo | d118f8b | 2010-05-10 12:51:05 -0300 | [diff] [blame] | 95 | int ret = machine__create_kernel_maps(&self->host_machine); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 96 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 97 | if (ret >= 0) |
Arnaldo Carvalho de Melo | d118f8b | 2010-05-10 12:51:05 -0300 | [diff] [blame] | 98 | ret = machines__create_guest_kernel_maps(&self->machines); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 99 | return ret; |
| 100 | } |
| 101 | |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 102 | static void perf_session__destroy_kernel_maps(struct perf_session *self) |
| 103 | { |
| 104 | machine__destroy_kernel_maps(&self->host_machine); |
| 105 | machines__destroy_guest_kernel_maps(&self->machines); |
| 106 | } |
| 107 | |
Ian Munsie | 21ef97f | 2010-12-10 14:09:16 +1100 | [diff] [blame] | 108 | struct perf_session *perf_session__new(const char *filename, int mode, |
| 109 | bool force, bool repipe, |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 110 | struct perf_tool *tool) |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 111 | { |
Robert Richter | efad141 | 2011-12-07 10:02:54 +0100 | [diff] [blame] | 112 | struct perf_session *self; |
| 113 | struct stat st; |
| 114 | size_t len; |
| 115 | |
| 116 | if (!filename || !strlen(filename)) { |
| 117 | if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) |
| 118 | filename = "-"; |
| 119 | else |
| 120 | filename = "perf.data"; |
| 121 | } |
| 122 | |
| 123 | len = strlen(filename); |
| 124 | self = zalloc(sizeof(*self) + len); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 125 | |
| 126 | if (self == NULL) |
| 127 | goto out; |
| 128 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 129 | memcpy(self->filename, filename, len); |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 130 | /* |
| 131 | * On 64bit we can mmap the data file in one go. No need for tiny mmap |
| 132 | * slices. On 32bit we use 32MB. |
| 133 | */ |
| 134 | #if BITS_PER_LONG == 64 |
| 135 | self->mmap_window = ULLONG_MAX; |
| 136 | #else |
| 137 | self->mmap_window = 32 * 1024 * 1024ULL; |
| 138 | #endif |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 139 | self->machines = RB_ROOT; |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 140 | self->repipe = repipe; |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 141 | INIT_LIST_HEAD(&self->ordered_samples.samples); |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 142 | INIT_LIST_HEAD(&self->ordered_samples.sample_cache); |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 143 | INIT_LIST_HEAD(&self->ordered_samples.to_free); |
Arnaldo Carvalho de Melo | 1f626bc | 2010-05-09 19:57:08 -0300 | [diff] [blame] | 144 | machine__init(&self->host_machine, "", HOST_KERNEL_ID); |
Jiri Olsa | 4bf9ce1 | 2012-03-22 14:37:26 +0100 | [diff] [blame] | 145 | hists__init(&self->hists); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 146 | |
Arnaldo Carvalho de Melo | 64abebf7 | 2010-01-27 21:05:52 -0200 | [diff] [blame] | 147 | if (mode == O_RDONLY) { |
| 148 | if (perf_session__open(self, force) < 0) |
| 149 | goto out_delete; |
Arnaldo Carvalho de Melo | 7b56cce | 2012-08-01 19:31:00 -0300 | [diff] [blame] | 150 | perf_session__set_id_hdr_size(self); |
Arnaldo Carvalho de Melo | 64abebf7 | 2010-01-27 21:05:52 -0200 | [diff] [blame] | 151 | } else if (mode == O_WRONLY) { |
| 152 | /* |
| 153 | * 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] | 154 | * kernel MMAP event, in perf_event__process_mmap(). |
Arnaldo Carvalho de Melo | 64abebf7 | 2010-01-27 21:05:52 -0200 | [diff] [blame] | 155 | */ |
| 156 | if (perf_session__create_kernel_maps(self) < 0) |
| 157 | goto out_delete; |
| 158 | } |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 159 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 160 | if (tool && tool->ordering_requires_timestamps && |
Arnaldo Carvalho de Melo | 5e56247 | 2012-08-01 19:25:26 -0300 | [diff] [blame] | 161 | tool->ordered_samples && !perf_evlist__sample_id_all(self->evlist)) { |
Ian Munsie | 21ef97f | 2010-12-10 14:09:16 +1100 | [diff] [blame] | 162 | 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] | 163 | tool->ordered_samples = false; |
Ian Munsie | 21ef97f | 2010-12-10 14:09:16 +1100 | [diff] [blame] | 164 | } |
| 165 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 166 | out: |
| 167 | return self; |
Arnaldo Carvalho de Melo | 4aa6563 | 2009-12-13 19:50:29 -0200 | [diff] [blame] | 168 | out_delete: |
| 169 | perf_session__delete(self); |
| 170 | return NULL; |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 171 | } |
| 172 | |
Arnaldo Carvalho de Melo | b424eba | 2011-11-09 13:24:25 -0200 | [diff] [blame] | 173 | static void machine__delete_dead_threads(struct machine *machine) |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 174 | { |
| 175 | struct thread *n, *t; |
| 176 | |
Arnaldo Carvalho de Melo | b424eba | 2011-11-09 13:24:25 -0200 | [diff] [blame] | 177 | 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] | 178 | list_del(&t->node); |
| 179 | thread__delete(t); |
| 180 | } |
| 181 | } |
| 182 | |
Arnaldo Carvalho de Melo | b424eba | 2011-11-09 13:24:25 -0200 | [diff] [blame] | 183 | static void perf_session__delete_dead_threads(struct perf_session *session) |
| 184 | { |
| 185 | machine__delete_dead_threads(&session->host_machine); |
| 186 | } |
| 187 | |
| 188 | static void machine__delete_threads(struct machine *self) |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 189 | { |
| 190 | struct rb_node *nd = rb_first(&self->threads); |
| 191 | |
| 192 | while (nd) { |
| 193 | struct thread *t = rb_entry(nd, struct thread, rb_node); |
| 194 | |
| 195 | rb_erase(&t->rb_node, &self->threads); |
| 196 | nd = rb_next(nd); |
| 197 | thread__delete(t); |
| 198 | } |
| 199 | } |
| 200 | |
Arnaldo Carvalho de Melo | b424eba | 2011-11-09 13:24:25 -0200 | [diff] [blame] | 201 | static void perf_session__delete_threads(struct perf_session *session) |
| 202 | { |
| 203 | machine__delete_threads(&session->host_machine); |
| 204 | } |
| 205 | |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 206 | void perf_session__delete(struct perf_session *self) |
| 207 | { |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 208 | perf_session__destroy_kernel_maps(self); |
Arnaldo Carvalho de Melo | d65a458 | 2010-07-30 18:31:28 -0300 | [diff] [blame] | 209 | perf_session__delete_dead_threads(self); |
| 210 | perf_session__delete_threads(self); |
| 211 | machine__exit(&self->host_machine); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 212 | close(self->fd); |
Arnaldo Carvalho de Melo | 94c744b | 2009-12-11 21:24:02 -0200 | [diff] [blame] | 213 | free(self); |
| 214 | } |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 215 | |
Arnaldo Carvalho de Melo | b424eba | 2011-11-09 13:24:25 -0200 | [diff] [blame] | 216 | void machine__remove_thread(struct machine *self, struct thread *th) |
Arnaldo Carvalho de Melo | 720a3ae | 2010-06-17 08:37:44 -0300 | [diff] [blame] | 217 | { |
Arnaldo Carvalho de Melo | 70597f2 | 2010-08-02 18:59:28 -0300 | [diff] [blame] | 218 | self->last_match = NULL; |
Arnaldo Carvalho de Melo | 720a3ae | 2010-06-17 08:37:44 -0300 | [diff] [blame] | 219 | rb_erase(&th->rb_node, &self->threads); |
| 220 | /* |
| 221 | * We may have references to this thread, for instance in some hist_entry |
| 222 | * instances, so just move them to a separate list. |
| 223 | */ |
| 224 | list_add_tail(&th->node, &self->dead_threads); |
| 225 | } |
| 226 | |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 227 | static bool symbol__match_parent_regex(struct symbol *sym) |
| 228 | { |
| 229 | if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) |
| 230 | return 1; |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 235 | static const u8 cpumodes[] = { |
| 236 | PERF_RECORD_MISC_USER, |
| 237 | PERF_RECORD_MISC_KERNEL, |
| 238 | PERF_RECORD_MISC_GUEST_USER, |
| 239 | PERF_RECORD_MISC_GUEST_KERNEL |
| 240 | }; |
| 241 | #define NCPUMODES (sizeof(cpumodes)/sizeof(u8)) |
| 242 | |
| 243 | static void ip__resolve_ams(struct machine *self, struct thread *thread, |
| 244 | struct addr_map_symbol *ams, |
| 245 | u64 ip) |
| 246 | { |
| 247 | struct addr_location al; |
| 248 | size_t i; |
| 249 | u8 m; |
| 250 | |
| 251 | memset(&al, 0, sizeof(al)); |
| 252 | |
| 253 | for (i = 0; i < NCPUMODES; i++) { |
| 254 | m = cpumodes[i]; |
| 255 | /* |
| 256 | * We cannot use the header.misc hint to determine whether a |
| 257 | * branch stack address is user, kernel, guest, hypervisor. |
| 258 | * Branches may straddle the kernel/user/hypervisor boundaries. |
| 259 | * Thus, we have to try consecutively until we find a match |
| 260 | * or else, the symbol is unknown |
| 261 | */ |
| 262 | thread__find_addr_location(thread, self, m, MAP__FUNCTION, |
| 263 | ip, &al, NULL); |
| 264 | if (al.sym) |
| 265 | goto found; |
| 266 | } |
| 267 | found: |
| 268 | ams->addr = ip; |
Stephane Eranian | a68c2c5 | 2012-03-08 23:47:48 +0100 | [diff] [blame] | 269 | ams->al_addr = al.addr; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 270 | ams->sym = al.sym; |
| 271 | ams->map = al.map; |
| 272 | } |
| 273 | |
| 274 | struct branch_info *machine__resolve_bstack(struct machine *self, |
| 275 | struct thread *thr, |
| 276 | struct branch_stack *bs) |
| 277 | { |
| 278 | struct branch_info *bi; |
| 279 | unsigned int i; |
| 280 | |
| 281 | bi = calloc(bs->nr, sizeof(struct branch_info)); |
| 282 | if (!bi) |
| 283 | return NULL; |
| 284 | |
| 285 | for (i = 0; i < bs->nr; i++) { |
| 286 | ip__resolve_ams(self, thr, &bi[i].to, bs->entries[i].to); |
| 287 | ip__resolve_ams(self, thr, &bi[i].from, bs->entries[i].from); |
| 288 | bi[i].flags = bs->entries[i].flags; |
| 289 | } |
| 290 | return bi; |
| 291 | } |
| 292 | |
Jiri Olsa | 71ad0f5 | 2012-08-07 15:20:46 +0200 | [diff] [blame] | 293 | static int machine__resolve_callchain_sample(struct machine *machine, |
| 294 | struct thread *thread, |
| 295 | struct ip_callchain *chain, |
| 296 | struct symbol **parent) |
| 297 | |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 298 | { |
| 299 | u8 cpumode = PERF_RECORD_MISC_USER; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 300 | unsigned int i; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 301 | int err; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 302 | |
Namhyung Kim | 4726064 | 2012-05-31 14:43:26 +0900 | [diff] [blame] | 303 | callchain_cursor_reset(&callchain_cursor); |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 304 | |
Namhyung Kim | 114067b | 2012-05-31 14:43:27 +0900 | [diff] [blame] | 305 | if (chain->nr > PERF_MAX_STACK_DEPTH) { |
| 306 | pr_warning("corrupted callchain. skipping...\n"); |
| 307 | return 0; |
| 308 | } |
| 309 | |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 310 | for (i = 0; i < chain->nr; i++) { |
Sam Liao | d797fdc | 2011-06-07 23:49:46 +0800 | [diff] [blame] | 311 | u64 ip; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 312 | struct addr_location al; |
| 313 | |
Sam Liao | d797fdc | 2011-06-07 23:49:46 +0800 | [diff] [blame] | 314 | if (callchain_param.order == ORDER_CALLEE) |
| 315 | ip = chain->ips[i]; |
| 316 | else |
| 317 | ip = chain->ips[chain->nr - i - 1]; |
| 318 | |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 319 | if (ip >= PERF_CONTEXT_MAX) { |
| 320 | switch (ip) { |
| 321 | case PERF_CONTEXT_HV: |
Jiri Olsa | 71ad0f5 | 2012-08-07 15:20:46 +0200 | [diff] [blame] | 322 | cpumode = PERF_RECORD_MISC_HYPERVISOR; |
| 323 | break; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 324 | case PERF_CONTEXT_KERNEL: |
Jiri Olsa | 71ad0f5 | 2012-08-07 15:20:46 +0200 | [diff] [blame] | 325 | cpumode = PERF_RECORD_MISC_KERNEL; |
| 326 | break; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 327 | case PERF_CONTEXT_USER: |
Jiri Olsa | 71ad0f5 | 2012-08-07 15:20:46 +0200 | [diff] [blame] | 328 | cpumode = PERF_RECORD_MISC_USER; |
| 329 | break; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 330 | default: |
Namhyung Kim | 114067b | 2012-05-31 14:43:27 +0900 | [diff] [blame] | 331 | pr_debug("invalid callchain context: " |
| 332 | "%"PRId64"\n", (s64) ip); |
| 333 | /* |
| 334 | * It seems the callchain is corrupted. |
| 335 | * Discard all. |
| 336 | */ |
| 337 | callchain_cursor_reset(&callchain_cursor); |
| 338 | return 0; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 339 | } |
| 340 | continue; |
| 341 | } |
| 342 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 343 | al.filtered = false; |
Jiri Olsa | 71ad0f5 | 2012-08-07 15:20:46 +0200 | [diff] [blame] | 344 | thread__find_addr_location(thread, machine, cpumode, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 345 | MAP__FUNCTION, ip, &al, NULL); |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 346 | if (al.sym != NULL) { |
| 347 | if (sort__has_parent && !*parent && |
| 348 | symbol__match_parent_regex(al.sym)) |
| 349 | *parent = al.sym; |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 350 | if (!symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 351 | break; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 352 | } |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 353 | |
Namhyung Kim | 4726064 | 2012-05-31 14:43:26 +0900 | [diff] [blame] | 354 | err = callchain_cursor_append(&callchain_cursor, |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 355 | ip, al.map, al.sym); |
| 356 | if (err) |
| 357 | return err; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 358 | } |
| 359 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 360 | return 0; |
Arnaldo Carvalho de Melo | a328626 | 2009-12-14 14:22:59 -0200 | [diff] [blame] | 361 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 362 | |
Jiri Olsa | 71ad0f5 | 2012-08-07 15:20:46 +0200 | [diff] [blame] | 363 | static int unwind_entry(struct unwind_entry *entry, void *arg) |
| 364 | { |
| 365 | struct callchain_cursor *cursor = arg; |
| 366 | return callchain_cursor_append(cursor, entry->ip, |
| 367 | entry->map, entry->sym); |
| 368 | } |
| 369 | |
| 370 | int machine__resolve_callchain(struct machine *machine, |
| 371 | struct perf_evsel *evsel, |
| 372 | struct thread *thread, |
| 373 | struct perf_sample *sample, |
| 374 | struct symbol **parent) |
| 375 | |
| 376 | { |
| 377 | int ret; |
| 378 | |
| 379 | callchain_cursor_reset(&callchain_cursor); |
| 380 | |
| 381 | ret = machine__resolve_callchain_sample(machine, thread, |
| 382 | sample->callchain, parent); |
| 383 | if (ret) |
| 384 | return ret; |
| 385 | |
| 386 | /* Can we do dwarf post unwind? */ |
| 387 | if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) && |
| 388 | (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER))) |
| 389 | return 0; |
| 390 | |
| 391 | return unwind__get_entries(unwind_entry, &callchain_cursor, machine, |
| 392 | thread, evsel->attr.sample_regs_user, |
| 393 | sample); |
| 394 | |
| 395 | } |
| 396 | |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 397 | static int process_event_synth_tracing_data_stub(union perf_event *event __used, |
| 398 | struct perf_session *session __used) |
| 399 | { |
| 400 | dump_printf(": unhandled!\n"); |
| 401 | return 0; |
| 402 | } |
| 403 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 404 | static int process_event_synth_attr_stub(union perf_event *event __used, |
| 405 | struct perf_evlist **pevlist __used) |
| 406 | { |
| 407 | dump_printf(": unhandled!\n"); |
| 408 | return 0; |
| 409 | } |
| 410 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 411 | 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] | 412 | union perf_event *event __used, |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 413 | struct perf_sample *sample __used, |
| 414 | struct perf_evsel *evsel __used, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 415 | struct machine *machine __used) |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 416 | { |
| 417 | dump_printf(": unhandled!\n"); |
| 418 | return 0; |
| 419 | } |
| 420 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 421 | static int process_event_stub(struct perf_tool *tool __used, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 422 | union perf_event *event __used, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 423 | struct perf_sample *sample __used, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 424 | struct machine *machine __used) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 425 | { |
| 426 | dump_printf(": unhandled!\n"); |
| 427 | return 0; |
| 428 | } |
| 429 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 430 | 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] | 431 | union perf_event *event __used, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 432 | struct perf_session *perf_session __used) |
| 433 | { |
| 434 | dump_printf(": unhandled!\n"); |
| 435 | return 0; |
| 436 | } |
| 437 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 438 | 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] | 439 | union perf_event *event __used) |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 440 | { |
| 441 | dump_printf(": unhandled!\n"); |
| 442 | return 0; |
| 443 | } |
| 444 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 445 | static int process_finished_round(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 446 | union perf_event *event, |
| 447 | struct perf_session *session); |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 448 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 449 | static void perf_tool__fill_defaults(struct perf_tool *tool) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 450 | { |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 451 | if (tool->sample == NULL) |
| 452 | tool->sample = process_event_sample_stub; |
| 453 | if (tool->mmap == NULL) |
| 454 | tool->mmap = process_event_stub; |
| 455 | if (tool->comm == NULL) |
| 456 | tool->comm = process_event_stub; |
| 457 | if (tool->fork == NULL) |
| 458 | tool->fork = process_event_stub; |
| 459 | if (tool->exit == NULL) |
| 460 | tool->exit = process_event_stub; |
| 461 | if (tool->lost == NULL) |
| 462 | tool->lost = perf_event__process_lost; |
| 463 | if (tool->read == NULL) |
| 464 | tool->read = process_event_sample_stub; |
| 465 | if (tool->throttle == NULL) |
| 466 | tool->throttle = process_event_stub; |
| 467 | if (tool->unthrottle == NULL) |
| 468 | tool->unthrottle = process_event_stub; |
| 469 | if (tool->attr == NULL) |
| 470 | tool->attr = process_event_synth_attr_stub; |
| 471 | if (tool->event_type == NULL) |
| 472 | tool->event_type = process_event_type_stub; |
| 473 | if (tool->tracing_data == NULL) |
| 474 | tool->tracing_data = process_event_synth_tracing_data_stub; |
| 475 | if (tool->build_id == NULL) |
| 476 | tool->build_id = process_finished_round_stub; |
| 477 | if (tool->finished_round == NULL) { |
| 478 | if (tool->ordered_samples) |
| 479 | tool->finished_round = process_finished_round; |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 480 | else |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 481 | tool->finished_round = process_finished_round_stub; |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 482 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 483 | } |
David Ahern | 80c0120 | 2012-06-08 11:47:51 -0300 | [diff] [blame] | 484 | |
| 485 | void mem_bswap_32(void *src, int byte_size) |
| 486 | { |
| 487 | u32 *m = src; |
| 488 | while (byte_size > 0) { |
| 489 | *m = bswap_32(*m); |
| 490 | byte_size -= sizeof(u32); |
| 491 | ++m; |
| 492 | } |
| 493 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 494 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 495 | void mem_bswap_64(void *src, int byte_size) |
| 496 | { |
| 497 | u64 *m = src; |
| 498 | |
| 499 | while (byte_size > 0) { |
| 500 | *m = bswap_64(*m); |
| 501 | byte_size -= sizeof(u64); |
| 502 | ++m; |
| 503 | } |
| 504 | } |
| 505 | |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 506 | static void swap_sample_id_all(union perf_event *event, void *data) |
| 507 | { |
| 508 | void *end = (void *) event + event->header.size; |
| 509 | int size = end - data; |
| 510 | |
| 511 | BUG_ON(size % sizeof(u64)); |
| 512 | mem_bswap_64(data, size); |
| 513 | } |
| 514 | |
| 515 | static void perf_event__all64_swap(union perf_event *event, |
| 516 | bool sample_id_all __used) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 517 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 518 | struct perf_event_header *hdr = &event->header; |
| 519 | mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr)); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 520 | } |
| 521 | |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 522 | static void perf_event__comm_swap(union perf_event *event, bool sample_id_all) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 523 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 524 | event->comm.pid = bswap_32(event->comm.pid); |
| 525 | event->comm.tid = bswap_32(event->comm.tid); |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 526 | |
| 527 | if (sample_id_all) { |
| 528 | void *data = &event->comm.comm; |
| 529 | |
| 530 | data += ALIGN(strlen(data) + 1, sizeof(u64)); |
| 531 | swap_sample_id_all(event, data); |
| 532 | } |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 533 | } |
| 534 | |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 535 | static void perf_event__mmap_swap(union perf_event *event, |
| 536 | bool sample_id_all) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 537 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 538 | event->mmap.pid = bswap_32(event->mmap.pid); |
| 539 | event->mmap.tid = bswap_32(event->mmap.tid); |
| 540 | event->mmap.start = bswap_64(event->mmap.start); |
| 541 | event->mmap.len = bswap_64(event->mmap.len); |
| 542 | event->mmap.pgoff = bswap_64(event->mmap.pgoff); |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 543 | |
| 544 | if (sample_id_all) { |
| 545 | void *data = &event->mmap.filename; |
| 546 | |
| 547 | data += ALIGN(strlen(data) + 1, sizeof(u64)); |
| 548 | swap_sample_id_all(event, data); |
| 549 | } |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 550 | } |
| 551 | |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 552 | static void perf_event__task_swap(union perf_event *event, bool sample_id_all) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 553 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 554 | event->fork.pid = bswap_32(event->fork.pid); |
| 555 | event->fork.tid = bswap_32(event->fork.tid); |
| 556 | event->fork.ppid = bswap_32(event->fork.ppid); |
| 557 | event->fork.ptid = bswap_32(event->fork.ptid); |
| 558 | event->fork.time = bswap_64(event->fork.time); |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 559 | |
| 560 | if (sample_id_all) |
| 561 | swap_sample_id_all(event, &event->fork + 1); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 562 | } |
| 563 | |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 564 | static void perf_event__read_swap(union perf_event *event, bool sample_id_all) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 565 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 566 | event->read.pid = bswap_32(event->read.pid); |
| 567 | event->read.tid = bswap_32(event->read.tid); |
| 568 | event->read.value = bswap_64(event->read.value); |
| 569 | event->read.time_enabled = bswap_64(event->read.time_enabled); |
| 570 | event->read.time_running = bswap_64(event->read.time_running); |
| 571 | event->read.id = bswap_64(event->read.id); |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 572 | |
| 573 | if (sample_id_all) |
| 574 | swap_sample_id_all(event, &event->read + 1); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 575 | } |
| 576 | |
Jiri Olsa | e108c66 | 2012-05-16 08:59:03 +0200 | [diff] [blame] | 577 | static u8 revbyte(u8 b) |
| 578 | { |
| 579 | int rev = (b >> 4) | ((b & 0xf) << 4); |
| 580 | rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2); |
| 581 | rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1); |
| 582 | return (u8) rev; |
| 583 | } |
| 584 | |
| 585 | /* |
| 586 | * XXX this is hack in attempt to carry flags bitfield |
| 587 | * throught endian village. ABI says: |
| 588 | * |
| 589 | * Bit-fields are allocated from right to left (least to most significant) |
| 590 | * on little-endian implementations and from left to right (most to least |
| 591 | * significant) on big-endian implementations. |
| 592 | * |
| 593 | * The above seems to be byte specific, so we need to reverse each |
| 594 | * byte of the bitfield. 'Internet' also says this might be implementation |
| 595 | * specific and we probably need proper fix and carry perf_event_attr |
| 596 | * bitfield flags in separate data file FEAT_ section. Thought this seems |
| 597 | * to work for now. |
| 598 | */ |
| 599 | static void swap_bitfield(u8 *p, unsigned len) |
| 600 | { |
| 601 | unsigned i; |
| 602 | |
| 603 | for (i = 0; i < len; i++) { |
| 604 | *p = revbyte(*p); |
| 605 | p++; |
| 606 | } |
| 607 | } |
| 608 | |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 609 | /* exported for swapping attributes in file header */ |
| 610 | void perf_event__attr_swap(struct perf_event_attr *attr) |
| 611 | { |
| 612 | attr->type = bswap_32(attr->type); |
| 613 | attr->size = bswap_32(attr->size); |
| 614 | attr->config = bswap_64(attr->config); |
| 615 | attr->sample_period = bswap_64(attr->sample_period); |
| 616 | attr->sample_type = bswap_64(attr->sample_type); |
| 617 | attr->read_format = bswap_64(attr->read_format); |
| 618 | attr->wakeup_events = bswap_32(attr->wakeup_events); |
| 619 | attr->bp_type = bswap_32(attr->bp_type); |
| 620 | attr->bp_addr = bswap_64(attr->bp_addr); |
| 621 | attr->bp_len = bswap_64(attr->bp_len); |
Jiri Olsa | e108c66 | 2012-05-16 08:59:03 +0200 | [diff] [blame] | 622 | |
| 623 | swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64)); |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 624 | } |
| 625 | |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 626 | static void perf_event__hdr_attr_swap(union perf_event *event, |
| 627 | bool sample_id_all __used) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 628 | { |
| 629 | size_t size; |
| 630 | |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 631 | perf_event__attr_swap(&event->attr.attr); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 632 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 633 | size = event->header.size; |
| 634 | size -= (void *)&event->attr.id - (void *)event; |
| 635 | mem_bswap_64(event->attr.id, size); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 636 | } |
| 637 | |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 638 | static void perf_event__event_type_swap(union perf_event *event, |
| 639 | bool sample_id_all __used) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 640 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 641 | event->event_type.event_type.event_id = |
| 642 | bswap_64(event->event_type.event_type.event_id); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 643 | } |
| 644 | |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 645 | static void perf_event__tracing_data_swap(union perf_event *event, |
| 646 | bool sample_id_all __used) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 647 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 648 | event->tracing_data.size = bswap_32(event->tracing_data.size); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 649 | } |
| 650 | |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 651 | typedef void (*perf_event__swap_op)(union perf_event *event, |
| 652 | bool sample_id_all); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 653 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 654 | static perf_event__swap_op perf_event__swap_ops[] = { |
| 655 | [PERF_RECORD_MMAP] = perf_event__mmap_swap, |
| 656 | [PERF_RECORD_COMM] = perf_event__comm_swap, |
| 657 | [PERF_RECORD_FORK] = perf_event__task_swap, |
| 658 | [PERF_RECORD_EXIT] = perf_event__task_swap, |
| 659 | [PERF_RECORD_LOST] = perf_event__all64_swap, |
| 660 | [PERF_RECORD_READ] = perf_event__read_swap, |
| 661 | [PERF_RECORD_SAMPLE] = perf_event__all64_swap, |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 662 | [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 663 | [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap, |
| 664 | [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap, |
| 665 | [PERF_RECORD_HEADER_BUILD_ID] = NULL, |
| 666 | [PERF_RECORD_HEADER_MAX] = NULL, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 667 | }; |
| 668 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 669 | struct sample_queue { |
| 670 | u64 timestamp; |
Thomas Gleixner | e4c2df1 | 2010-12-07 12:48:50 +0000 | [diff] [blame] | 671 | u64 file_offset; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 672 | union perf_event *event; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 673 | struct list_head list; |
| 674 | }; |
| 675 | |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 676 | static void perf_session_free_sample_buffers(struct perf_session *session) |
| 677 | { |
| 678 | struct ordered_samples *os = &session->ordered_samples; |
| 679 | |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 680 | while (!list_empty(&os->to_free)) { |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 681 | struct sample_queue *sq; |
| 682 | |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 683 | sq = list_entry(os->to_free.next, struct sample_queue, list); |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 684 | list_del(&sq->list); |
| 685 | free(sq); |
| 686 | } |
| 687 | } |
| 688 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 689 | static int perf_session_deliver_event(struct perf_session *session, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 690 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 691 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 692 | struct perf_tool *tool, |
Thomas Gleixner | f74725d | 2010-12-07 12:48:53 +0000 | [diff] [blame] | 693 | u64 file_offset); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 694 | |
David Ahern | d25380cd | 2012-08-26 12:24:41 -0600 | [diff] [blame^] | 695 | static int flush_sample_queue(struct perf_session *s, |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 696 | struct perf_tool *tool) |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 697 | { |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 698 | struct ordered_samples *os = &s->ordered_samples; |
| 699 | struct list_head *head = &os->samples; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 700 | struct sample_queue *tmp, *iter; |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 701 | struct perf_sample sample; |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 702 | u64 limit = os->next_flush; |
| 703 | 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] | 704 | unsigned idx = 0, progress_next = os->nr_samples / 16; |
Frederic Weisbecker | 5538bec | 2011-05-22 02:17:22 +0200 | [diff] [blame] | 705 | int ret; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 706 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 707 | if (!tool->ordered_samples || !limit) |
David Ahern | d25380cd | 2012-08-26 12:24:41 -0600 | [diff] [blame^] | 708 | return 0; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 709 | |
| 710 | list_for_each_entry_safe(iter, tmp, head, list) { |
| 711 | if (iter->timestamp > limit) |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 712 | break; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 713 | |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 714 | ret = perf_evlist__parse_sample(s->evlist, iter->event, &sample, |
| 715 | s->header.needs_swap); |
Frederic Weisbecker | 5538bec | 2011-05-22 02:17:22 +0200 | [diff] [blame] | 716 | if (ret) |
| 717 | pr_err("Can't parse sample, err = %d\n", ret); |
David Ahern | d25380cd | 2012-08-26 12:24:41 -0600 | [diff] [blame^] | 718 | else { |
| 719 | ret = perf_session_deliver_event(s, iter->event, &sample, tool, |
| 720 | iter->file_offset); |
| 721 | if (ret) |
| 722 | return ret; |
| 723 | } |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 724 | |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 725 | os->last_flush = iter->timestamp; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 726 | list_del(&iter->list); |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 727 | list_add(&iter->list, &os->sample_cache); |
Arnaldo Carvalho de Melo | 8866056 | 2011-10-29 12:41:45 -0200 | [diff] [blame] | 728 | if (++idx >= progress_next) { |
| 729 | progress_next += os->nr_samples / 16; |
| 730 | ui_progress__update(idx, os->nr_samples, |
| 731 | "Processing time ordered events..."); |
| 732 | } |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 733 | } |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 734 | |
| 735 | if (list_empty(head)) { |
| 736 | os->last_sample = NULL; |
| 737 | } else if (last_ts <= limit) { |
| 738 | os->last_sample = |
| 739 | list_entry(head->prev, struct sample_queue, list); |
| 740 | } |
Arnaldo Carvalho de Melo | 8866056 | 2011-10-29 12:41:45 -0200 | [diff] [blame] | 741 | |
| 742 | os->nr_samples = 0; |
David Ahern | d25380cd | 2012-08-26 12:24:41 -0600 | [diff] [blame^] | 743 | |
| 744 | return 0; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 745 | } |
| 746 | |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 747 | /* |
| 748 | * When perf record finishes a pass on every buffers, it records this pseudo |
| 749 | * event. |
| 750 | * We record the max timestamp t found in the pass n. |
| 751 | * Assuming these timestamps are monotonic across cpus, we know that if |
| 752 | * a buffer still has events with timestamps below t, they will be all |
| 753 | * available and then read in the pass n + 1. |
| 754 | * Hence when we start to read the pass n + 2, we can safely flush every |
| 755 | * events with timestamps below t. |
| 756 | * |
| 757 | * ============ PASS n ================= |
| 758 | * CPU 0 | CPU 1 |
| 759 | * | |
| 760 | * cnt1 timestamps | cnt2 timestamps |
| 761 | * 1 | 2 |
| 762 | * 2 | 3 |
| 763 | * - | 4 <--- max recorded |
| 764 | * |
| 765 | * ============ PASS n + 1 ============== |
| 766 | * CPU 0 | CPU 1 |
| 767 | * | |
| 768 | * cnt1 timestamps | cnt2 timestamps |
| 769 | * 3 | 5 |
| 770 | * 4 | 6 |
| 771 | * 5 | 7 <---- max recorded |
| 772 | * |
| 773 | * Flush every events below timestamp 4 |
| 774 | * |
| 775 | * ============ PASS n + 2 ============== |
| 776 | * CPU 0 | CPU 1 |
| 777 | * | |
| 778 | * cnt1 timestamps | cnt2 timestamps |
| 779 | * 6 | 8 |
| 780 | * 7 | 9 |
| 781 | * - | 10 |
| 782 | * |
| 783 | * Flush every events below timestamp 7 |
| 784 | * etc... |
| 785 | */ |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 786 | static int process_finished_round(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 787 | union perf_event *event __used, |
| 788 | struct perf_session *session) |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 789 | { |
David Ahern | d25380cd | 2012-08-26 12:24:41 -0600 | [diff] [blame^] | 790 | int ret = flush_sample_queue(session, tool); |
| 791 | if (!ret) |
| 792 | session->ordered_samples.next_flush = session->ordered_samples.max_timestamp; |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 793 | |
David Ahern | d25380cd | 2012-08-26 12:24:41 -0600 | [diff] [blame^] | 794 | return ret; |
Frederic Weisbecker | d6b17be | 2010-05-03 15:14:33 +0200 | [diff] [blame] | 795 | } |
| 796 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 797 | /* The queue is ordered by time */ |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 798 | static void __queue_event(struct sample_queue *new, struct perf_session *s) |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 799 | { |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 800 | struct ordered_samples *os = &s->ordered_samples; |
| 801 | struct sample_queue *sample = os->last_sample; |
| 802 | u64 timestamp = new->timestamp; |
| 803 | struct list_head *p; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 804 | |
Arnaldo Carvalho de Melo | 8866056 | 2011-10-29 12:41:45 -0200 | [diff] [blame] | 805 | ++os->nr_samples; |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 806 | os->last_sample = new; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 807 | |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 808 | if (!sample) { |
| 809 | list_add(&new->list, &os->samples); |
| 810 | os->max_timestamp = timestamp; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 811 | return; |
| 812 | } |
| 813 | |
| 814 | /* |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 815 | * last_sample might point to some random place in the list as it's |
| 816 | * the last queued event. We expect that the new event is close to |
| 817 | * this. |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 818 | */ |
Thomas Gleixner | a1225de | 2010-11-30 17:49:33 +0000 | [diff] [blame] | 819 | if (sample->timestamp <= timestamp) { |
| 820 | while (sample->timestamp <= timestamp) { |
| 821 | p = sample->list.next; |
| 822 | if (p == &os->samples) { |
| 823 | list_add_tail(&new->list, &os->samples); |
| 824 | os->max_timestamp = timestamp; |
| 825 | return; |
| 826 | } |
| 827 | sample = list_entry(p, struct sample_queue, list); |
| 828 | } |
| 829 | list_add_tail(&new->list, &sample->list); |
| 830 | } else { |
| 831 | while (sample->timestamp > timestamp) { |
| 832 | p = sample->list.prev; |
| 833 | if (p == &os->samples) { |
| 834 | list_add(&new->list, &os->samples); |
| 835 | return; |
| 836 | } |
| 837 | sample = list_entry(p, struct sample_queue, list); |
| 838 | } |
| 839 | list_add(&new->list, &sample->list); |
| 840 | } |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 841 | } |
| 842 | |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 843 | #define MAX_SAMPLE_BUFFER (64 * 1024 / sizeof(struct sample_queue)) |
| 844 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 845 | 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] | 846 | struct perf_sample *sample, u64 file_offset) |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 847 | { |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 848 | struct ordered_samples *os = &s->ordered_samples; |
| 849 | struct list_head *sc = &os->sample_cache; |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 850 | u64 timestamp = sample->time; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 851 | struct sample_queue *new; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 852 | |
Thomas Gleixner | 79a14c1 | 2010-12-07 12:48:44 +0000 | [diff] [blame] | 853 | if (!timestamp || timestamp == ~0ULL) |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 854 | return -ETIME; |
| 855 | |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 856 | if (timestamp < s->ordered_samples.last_flush) { |
| 857 | printf("Warning: Timestamp below last timeslice flush\n"); |
| 858 | return -EINVAL; |
| 859 | } |
| 860 | |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 861 | if (!list_empty(sc)) { |
| 862 | new = list_entry(sc->next, struct sample_queue, list); |
| 863 | list_del(&new->list); |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 864 | } else if (os->sample_buffer) { |
| 865 | new = os->sample_buffer + os->sample_buffer_idx; |
| 866 | if (++os->sample_buffer_idx == MAX_SAMPLE_BUFFER) |
| 867 | os->sample_buffer = NULL; |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 868 | } else { |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 869 | os->sample_buffer = malloc(MAX_SAMPLE_BUFFER * sizeof(*new)); |
| 870 | if (!os->sample_buffer) |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 871 | return -ENOMEM; |
Thomas Gleixner | 5c891f3 | 2010-11-30 17:49:55 +0000 | [diff] [blame] | 872 | list_add(&os->sample_buffer->list, &os->to_free); |
| 873 | os->sample_buffer_idx = 2; |
| 874 | new = os->sample_buffer + 1; |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 875 | } |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 876 | |
| 877 | new->timestamp = timestamp; |
Thomas Gleixner | e4c2df1 | 2010-12-07 12:48:50 +0000 | [diff] [blame] | 878 | new->file_offset = file_offset; |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 879 | new->event = event; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 880 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 881 | __queue_event(new, s); |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 882 | |
| 883 | return 0; |
| 884 | } |
| 885 | |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 886 | static void callchain__printf(struct perf_sample *sample) |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 887 | { |
| 888 | unsigned int i; |
| 889 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 890 | printf("... chain: nr:%" PRIu64 "\n", sample->callchain->nr); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 891 | |
| 892 | for (i = 0; i < sample->callchain->nr; i++) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 893 | printf("..... %2d: %016" PRIx64 "\n", |
| 894 | i, sample->callchain->ips[i]); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 895 | } |
| 896 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 897 | static void branch_stack__printf(struct perf_sample *sample) |
| 898 | { |
| 899 | uint64_t i; |
| 900 | |
| 901 | printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr); |
| 902 | |
| 903 | for (i = 0; i < sample->branch_stack->nr; i++) |
| 904 | printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 "\n", |
| 905 | i, sample->branch_stack->entries[i].from, |
| 906 | sample->branch_stack->entries[i].to); |
| 907 | } |
| 908 | |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 909 | static void regs_dump__printf(u64 mask, u64 *regs) |
| 910 | { |
| 911 | unsigned rid, i = 0; |
| 912 | |
| 913 | for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) { |
| 914 | u64 val = regs[i++]; |
| 915 | |
| 916 | printf(".... %-5s 0x%" PRIx64 "\n", |
| 917 | perf_reg_name(rid), val); |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | static void regs_user__printf(struct perf_sample *sample, u64 mask) |
| 922 | { |
| 923 | struct regs_dump *user_regs = &sample->user_regs; |
| 924 | |
| 925 | if (user_regs->regs) { |
| 926 | printf("... user regs: mask 0x%" PRIx64 "\n", mask); |
| 927 | regs_dump__printf(mask, user_regs->regs); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | static void stack_user__printf(struct stack_dump *dump) |
| 932 | { |
| 933 | printf("... ustack: size %" PRIu64 ", offset 0x%x\n", |
| 934 | dump->size, dump->offset); |
| 935 | } |
| 936 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 937 | static void perf_session__print_tstamp(struct perf_session *session, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 938 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 939 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 940 | { |
Arnaldo Carvalho de Melo | 7f3be65 | 2012-08-01 19:15:52 -0300 | [diff] [blame] | 941 | u64 sample_type = perf_evlist__sample_type(session->evlist); |
| 942 | |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 943 | if (event->header.type != PERF_RECORD_SAMPLE && |
Arnaldo Carvalho de Melo | 5e56247 | 2012-08-01 19:25:26 -0300 | [diff] [blame] | 944 | !perf_evlist__sample_id_all(session->evlist)) { |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 945 | fputs("-1 -1 ", stdout); |
| 946 | return; |
| 947 | } |
| 948 | |
Arnaldo Carvalho de Melo | 7f3be65 | 2012-08-01 19:15:52 -0300 | [diff] [blame] | 949 | if ((sample_type & PERF_SAMPLE_CPU)) |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 950 | printf("%u ", sample->cpu); |
| 951 | |
Arnaldo Carvalho de Melo | 7f3be65 | 2012-08-01 19:15:52 -0300 | [diff] [blame] | 952 | if (sample_type & PERF_SAMPLE_TIME) |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 953 | printf("%" PRIu64 " ", sample->time); |
Arnaldo Carvalho de Melo | 9c90a61 | 2010-12-02 10:25:28 -0200 | [diff] [blame] | 954 | } |
| 955 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 956 | 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] | 957 | u64 file_offset, struct perf_sample *sample) |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 958 | { |
| 959 | if (!dump_trace) |
| 960 | return; |
| 961 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 962 | printf("\n%#" PRIx64 " [%#x]: event: %d\n", |
| 963 | file_offset, event->header.size, event->header.type); |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 964 | |
| 965 | trace_event(event); |
| 966 | |
| 967 | if (sample) |
| 968 | perf_session__print_tstamp(session, event, sample); |
| 969 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 970 | printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 971 | event->header.size, perf_event__name(event->header.type)); |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 974 | static void dump_sample(struct perf_evsel *evsel, union perf_event *event, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 975 | struct perf_sample *sample) |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 976 | { |
Arnaldo Carvalho de Melo | 7f3be65 | 2012-08-01 19:15:52 -0300 | [diff] [blame] | 977 | u64 sample_type; |
| 978 | |
Arnaldo Carvalho de Melo | ddbc24b | 2010-12-09 12:20:20 -0200 | [diff] [blame] | 979 | if (!dump_trace) |
| 980 | return; |
| 981 | |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 982 | 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] | 983 | event->header.misc, sample->pid, sample->tid, sample->ip, |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 984 | sample->period, sample->addr); |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 985 | |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 986 | sample_type = evsel->attr.sample_type; |
Arnaldo Carvalho de Melo | 7f3be65 | 2012-08-01 19:15:52 -0300 | [diff] [blame] | 987 | |
| 988 | if (sample_type & PERF_SAMPLE_CALLCHAIN) |
Arnaldo Carvalho de Melo | ddbc24b | 2010-12-09 12:20:20 -0200 | [diff] [blame] | 989 | callchain__printf(sample); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 990 | |
Arnaldo Carvalho de Melo | 7f3be65 | 2012-08-01 19:15:52 -0300 | [diff] [blame] | 991 | if (sample_type & PERF_SAMPLE_BRANCH_STACK) |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 992 | branch_stack__printf(sample); |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 993 | |
| 994 | if (sample_type & PERF_SAMPLE_REGS_USER) |
| 995 | regs_user__printf(sample, evsel->attr.sample_regs_user); |
| 996 | |
| 997 | if (sample_type & PERF_SAMPLE_STACK_USER) |
| 998 | stack_user__printf(&sample->user_stack); |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1001 | static struct machine * |
| 1002 | perf_session__find_machine_for_cpumode(struct perf_session *session, |
| 1003 | union perf_event *event) |
| 1004 | { |
| 1005 | const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 1006 | |
David Ahern | 7c0f4a4 | 2012-07-20 17:25:48 -0600 | [diff] [blame] | 1007 | if (perf_guest && |
| 1008 | ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) || |
| 1009 | (cpumode == PERF_RECORD_MISC_GUEST_USER))) { |
Nikunj A. Dadhania | 7fb0a5e | 2012-04-09 13:52:23 +0530 | [diff] [blame] | 1010 | u32 pid; |
| 1011 | |
| 1012 | if (event->header.type == PERF_RECORD_MMAP) |
| 1013 | pid = event->mmap.pid; |
| 1014 | else |
| 1015 | pid = event->ip.pid; |
| 1016 | |
David Ahern | 207b579 | 2012-07-01 16:11:37 -0600 | [diff] [blame] | 1017 | return perf_session__findnew_machine(session, pid); |
Nikunj A. Dadhania | 7fb0a5e | 2012-04-09 13:52:23 +0530 | [diff] [blame] | 1018 | } |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1019 | |
| 1020 | return perf_session__find_host_machine(session); |
| 1021 | } |
| 1022 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1023 | static int perf_session_deliver_event(struct perf_session *session, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1024 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 1025 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1026 | struct perf_tool *tool, |
Thomas Gleixner | 532e726 | 2010-12-07 12:48:55 +0000 | [diff] [blame] | 1027 | u64 file_offset) |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1028 | { |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 1029 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1030 | struct machine *machine; |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 1031 | |
Thomas Gleixner | 532e726 | 2010-12-07 12:48:55 +0000 | [diff] [blame] | 1032 | dump_event(session, event, file_offset, sample); |
| 1033 | |
Arnaldo Carvalho de Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame] | 1034 | evsel = perf_evlist__id2evsel(session->evlist, sample->id); |
| 1035 | if (evsel != NULL && event->header.type != PERF_RECORD_SAMPLE) { |
| 1036 | /* |
| 1037 | * XXX We're leaving PERF_RECORD_SAMPLE unnacounted here |
| 1038 | * because the tools right now may apply filters, discarding |
| 1039 | * some of the samples. For consistency, in the future we |
| 1040 | * should have something like nr_filtered_samples and remove |
| 1041 | * the sample->period from total_sample_period, etc, KISS for |
| 1042 | * now tho. |
| 1043 | * |
| 1044 | * Also testing against NULL allows us to handle files without |
| 1045 | * attr.sample_id_all and/or without PERF_SAMPLE_ID. In the |
| 1046 | * future probably it'll be a good idea to restrict event |
| 1047 | * processing via perf_session to files with both set. |
| 1048 | */ |
| 1049 | hists__inc_nr_events(&evsel->hists, event->header.type); |
| 1050 | } |
| 1051 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1052 | machine = perf_session__find_machine_for_cpumode(session, event); |
| 1053 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1054 | switch (event->header.type) { |
| 1055 | case PERF_RECORD_SAMPLE: |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 1056 | dump_sample(evsel, event, sample); |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 1057 | if (evsel == NULL) { |
| 1058 | ++session->hists.stats.nr_unknown_id; |
Jiri Olsa | 6782206 | 2012-04-12 14:21:01 +0200 | [diff] [blame] | 1059 | return 0; |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 1060 | } |
Joerg Roedel | 0c09571 | 2012-02-10 18:05:04 +0100 | [diff] [blame] | 1061 | if (machine == NULL) { |
| 1062 | ++session->hists.stats.nr_unprocessable_samples; |
Jiri Olsa | 6782206 | 2012-04-12 14:21:01 +0200 | [diff] [blame] | 1063 | return 0; |
Joerg Roedel | 0c09571 | 2012-02-10 18:05:04 +0100 | [diff] [blame] | 1064 | } |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1065 | return tool->sample(tool, event, sample, evsel, machine); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1066 | case PERF_RECORD_MMAP: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1067 | return tool->mmap(tool, event, sample, machine); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1068 | case PERF_RECORD_COMM: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1069 | return tool->comm(tool, event, sample, machine); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1070 | case PERF_RECORD_FORK: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1071 | return tool->fork(tool, event, sample, machine); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1072 | case PERF_RECORD_EXIT: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1073 | return tool->exit(tool, event, sample, machine); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1074 | case PERF_RECORD_LOST: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1075 | if (tool->lost == perf_event__process_lost) |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1076 | session->hists.stats.total_lost += event->lost.lost; |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1077 | return tool->lost(tool, event, sample, machine); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1078 | case PERF_RECORD_READ: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1079 | return tool->read(tool, event, sample, evsel, machine); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1080 | case PERF_RECORD_THROTTLE: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1081 | return tool->throttle(tool, event, sample, machine); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1082 | case PERF_RECORD_UNTHROTTLE: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1083 | return tool->unthrottle(tool, event, sample, machine); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1084 | default: |
| 1085 | ++session->hists.stats.nr_unknown_events; |
| 1086 | return -1; |
| 1087 | } |
| 1088 | } |
| 1089 | |
Thomas Gleixner | 3dfc2c0 | 2010-12-07 12:48:58 +0000 | [diff] [blame] | 1090 | static int perf_session__preprocess_sample(struct perf_session *session, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1091 | union perf_event *event, struct perf_sample *sample) |
Thomas Gleixner | 3dfc2c0 | 2010-12-07 12:48:58 +0000 | [diff] [blame] | 1092 | { |
| 1093 | if (event->header.type != PERF_RECORD_SAMPLE || |
Arnaldo Carvalho de Melo | 7f3be65 | 2012-08-01 19:15:52 -0300 | [diff] [blame] | 1094 | !(perf_evlist__sample_type(session->evlist) & PERF_SAMPLE_CALLCHAIN)) |
Thomas Gleixner | 3dfc2c0 | 2010-12-07 12:48:58 +0000 | [diff] [blame] | 1095 | return 0; |
| 1096 | |
| 1097 | if (!ip_callchain__valid(sample->callchain, event)) { |
| 1098 | pr_debug("call-chain problem with event, skipping it.\n"); |
| 1099 | ++session->hists.stats.nr_invalid_chains; |
| 1100 | session->hists.stats.total_invalid_chains += sample->period; |
| 1101 | return -EINVAL; |
| 1102 | } |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1106 | 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] | 1107 | struct perf_tool *tool, u64 file_offset) |
Thomas Gleixner | ba74f06 | 2010-12-07 12:49:01 +0000 | [diff] [blame] | 1108 | { |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 1109 | int err; |
| 1110 | |
Thomas Gleixner | ba74f06 | 2010-12-07 12:49:01 +0000 | [diff] [blame] | 1111 | dump_event(session, event, file_offset, NULL); |
| 1112 | |
| 1113 | /* These events are processed right away */ |
| 1114 | switch (event->header.type) { |
| 1115 | case PERF_RECORD_HEADER_ATTR: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1116 | err = tool->attr(event, &session->evlist); |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 1117 | if (err == 0) |
Arnaldo Carvalho de Melo | 7b56cce | 2012-08-01 19:31:00 -0300 | [diff] [blame] | 1118 | perf_session__set_id_hdr_size(session); |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 1119 | return err; |
Thomas Gleixner | ba74f06 | 2010-12-07 12:49:01 +0000 | [diff] [blame] | 1120 | case PERF_RECORD_HEADER_EVENT_TYPE: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1121 | return tool->event_type(tool, event); |
Thomas Gleixner | ba74f06 | 2010-12-07 12:49:01 +0000 | [diff] [blame] | 1122 | case PERF_RECORD_HEADER_TRACING_DATA: |
| 1123 | /* setup for reading amidst mmap */ |
| 1124 | lseek(session->fd, file_offset, SEEK_SET); |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1125 | return tool->tracing_data(event, session); |
Thomas Gleixner | ba74f06 | 2010-12-07 12:49:01 +0000 | [diff] [blame] | 1126 | case PERF_RECORD_HEADER_BUILD_ID: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1127 | return tool->build_id(tool, event, session); |
Thomas Gleixner | ba74f06 | 2010-12-07 12:49:01 +0000 | [diff] [blame] | 1128 | case PERF_RECORD_FINISHED_ROUND: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1129 | return tool->finished_round(tool, event, session); |
Thomas Gleixner | ba74f06 | 2010-12-07 12:49:01 +0000 | [diff] [blame] | 1130 | default: |
| 1131 | return -EINVAL; |
| 1132 | } |
| 1133 | } |
| 1134 | |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 1135 | static void event_swap(union perf_event *event, bool sample_id_all) |
| 1136 | { |
| 1137 | perf_event__swap_op swap; |
| 1138 | |
| 1139 | swap = perf_event__swap_ops[event->header.type]; |
| 1140 | if (swap) |
| 1141 | swap(event, sample_id_all); |
| 1142 | } |
| 1143 | |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1144 | static int perf_session__process_event(struct perf_session *session, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1145 | union perf_event *event, |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1146 | struct perf_tool *tool, |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1147 | u64 file_offset) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1148 | { |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 1149 | struct perf_sample sample; |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1150 | int ret; |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 1151 | |
Jiri Olsa | 268fb20 | 2012-05-30 14:23:43 +0200 | [diff] [blame] | 1152 | if (session->header.needs_swap) |
Arnaldo Carvalho de Melo | 5e56247 | 2012-08-01 19:25:26 -0300 | [diff] [blame] | 1153 | event_swap(event, perf_evlist__sample_id_all(session->evlist)); |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 1154 | |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 1155 | if (event->header.type >= PERF_RECORD_HEADER_MAX) |
| 1156 | return -EINVAL; |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 1157 | |
Thomas Gleixner | 9aefcab0 | 2010-12-07 12:48:47 +0000 | [diff] [blame] | 1158 | hists__inc_nr_events(&session->hists, event->header.type); |
| 1159 | |
| 1160 | if (event->header.type >= PERF_RECORD_USER_TYPE_START) |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1161 | return perf_session__process_user_event(session, event, tool, file_offset); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1162 | |
Thomas Gleixner | 3dfc2c0 | 2010-12-07 12:48:58 +0000 | [diff] [blame] | 1163 | /* |
| 1164 | * For all kernel events we get the sample data |
| 1165 | */ |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1166 | ret = perf_evlist__parse_sample(session->evlist, event, &sample, |
| 1167 | session->header.needs_swap); |
Frederic Weisbecker | 5538bec | 2011-05-22 02:17:22 +0200 | [diff] [blame] | 1168 | if (ret) |
| 1169 | return ret; |
Thomas Gleixner | 3dfc2c0 | 2010-12-07 12:48:58 +0000 | [diff] [blame] | 1170 | |
| 1171 | /* Preprocess sample records - precheck callchains */ |
| 1172 | if (perf_session__preprocess_sample(session, event, &sample)) |
| 1173 | return 0; |
| 1174 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1175 | if (tool->ordered_samples) { |
Thomas Gleixner | e4c2df1 | 2010-12-07 12:48:50 +0000 | [diff] [blame] | 1176 | ret = perf_session_queue_event(session, event, &sample, |
| 1177 | file_offset); |
Thomas Gleixner | cbf4164 | 2010-12-05 14:32:55 +0100 | [diff] [blame] | 1178 | if (ret != -ETIME) |
| 1179 | return ret; |
| 1180 | } |
| 1181 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1182 | return perf_session_deliver_event(session, event, &sample, tool, |
Thomas Gleixner | f74725d | 2010-12-07 12:48:53 +0000 | [diff] [blame] | 1183 | file_offset); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1184 | } |
| 1185 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1186 | void perf_event_header__bswap(struct perf_event_header *self) |
| 1187 | { |
| 1188 | self->type = bswap_32(self->type); |
| 1189 | self->misc = bswap_16(self->misc); |
| 1190 | self->size = bswap_16(self->size); |
| 1191 | } |
| 1192 | |
Arnaldo Carvalho de Melo | b424eba | 2011-11-09 13:24:25 -0200 | [diff] [blame] | 1193 | struct thread *perf_session__findnew(struct perf_session *session, pid_t pid) |
| 1194 | { |
| 1195 | return machine__findnew_thread(&session->host_machine, pid); |
| 1196 | } |
| 1197 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1198 | static struct thread *perf_session__register_idle_thread(struct perf_session *self) |
| 1199 | { |
| 1200 | struct thread *thread = perf_session__findnew(self, 0); |
| 1201 | |
| 1202 | if (thread == NULL || thread__set_comm(thread, "swapper")) { |
| 1203 | pr_err("problem inserting idle task.\n"); |
| 1204 | thread = NULL; |
| 1205 | } |
| 1206 | |
| 1207 | return thread; |
| 1208 | } |
| 1209 | |
Arnaldo Carvalho de Melo | 1109599 | 2011-01-04 16:25:15 -0200 | [diff] [blame] | 1210 | 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] | 1211 | const struct perf_tool *tool) |
Arnaldo Carvalho de Melo | 1109599 | 2011-01-04 16:25:15 -0200 | [diff] [blame] | 1212 | { |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1213 | if (tool->lost == perf_event__process_lost && |
Arnaldo Carvalho de Melo | 7b27509 | 2011-10-29 12:15:04 -0200 | [diff] [blame] | 1214 | session->hists.stats.nr_events[PERF_RECORD_LOST] != 0) { |
| 1215 | ui__warning("Processed %d events and lost %d chunks!\n\n" |
| 1216 | "Check IO/CPU overload!\n\n", |
| 1217 | session->hists.stats.nr_events[0], |
| 1218 | session->hists.stats.nr_events[PERF_RECORD_LOST]); |
Arnaldo Carvalho de Melo | 1109599 | 2011-01-04 16:25:15 -0200 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | if (session->hists.stats.nr_unknown_events != 0) { |
| 1222 | ui__warning("Found %u unknown events!\n\n" |
| 1223 | "Is this an older tool processing a perf.data " |
| 1224 | "file generated by a more recent tool?\n\n" |
| 1225 | "If that is not the case, consider " |
| 1226 | "reporting to linux-kernel@vger.kernel.org.\n\n", |
| 1227 | session->hists.stats.nr_unknown_events); |
| 1228 | } |
| 1229 | |
Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 1230 | if (session->hists.stats.nr_unknown_id != 0) { |
| 1231 | ui__warning("%u samples with id not present in the header\n", |
| 1232 | session->hists.stats.nr_unknown_id); |
| 1233 | } |
| 1234 | |
Arnaldo Carvalho de Melo | 1109599 | 2011-01-04 16:25:15 -0200 | [diff] [blame] | 1235 | if (session->hists.stats.nr_invalid_chains != 0) { |
| 1236 | ui__warning("Found invalid callchains!\n\n" |
| 1237 | "%u out of %u events were discarded for this reason.\n\n" |
| 1238 | "Consider reporting to linux-kernel@vger.kernel.org.\n\n", |
| 1239 | session->hists.stats.nr_invalid_chains, |
| 1240 | session->hists.stats.nr_events[PERF_RECORD_SAMPLE]); |
| 1241 | } |
Joerg Roedel | 0c09571 | 2012-02-10 18:05:04 +0100 | [diff] [blame] | 1242 | |
| 1243 | if (session->hists.stats.nr_unprocessable_samples != 0) { |
| 1244 | ui__warning("%u unprocessable samples recorded.\n" |
| 1245 | "Do you have a KVM guest running and not using 'perf kvm'?\n", |
| 1246 | session->hists.stats.nr_unprocessable_samples); |
| 1247 | } |
Arnaldo Carvalho de Melo | 1109599 | 2011-01-04 16:25:15 -0200 | [diff] [blame] | 1248 | } |
| 1249 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1250 | #define session_done() (*(volatile int *)(&session_done)) |
| 1251 | volatile int session_done; |
| 1252 | |
| 1253 | 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] | 1254 | struct perf_tool *tool) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1255 | { |
Stephane Eranian | 444d286 | 2012-05-15 13:28:12 +0200 | [diff] [blame] | 1256 | union perf_event *event; |
| 1257 | uint32_t size, cur_size = 0; |
| 1258 | void *buf = NULL; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1259 | int skip = 0; |
| 1260 | u64 head; |
| 1261 | int err; |
| 1262 | void *p; |
| 1263 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1264 | perf_tool__fill_defaults(tool); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1265 | |
| 1266 | head = 0; |
Stephane Eranian | 444d286 | 2012-05-15 13:28:12 +0200 | [diff] [blame] | 1267 | cur_size = sizeof(union perf_event); |
| 1268 | |
| 1269 | buf = malloc(cur_size); |
| 1270 | if (!buf) |
| 1271 | return -errno; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1272 | more: |
Stephane Eranian | 444d286 | 2012-05-15 13:28:12 +0200 | [diff] [blame] | 1273 | event = buf; |
| 1274 | err = readn(self->fd, event, sizeof(struct perf_event_header)); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1275 | if (err <= 0) { |
| 1276 | if (err == 0) |
| 1277 | goto done; |
| 1278 | |
| 1279 | pr_err("failed to read event header\n"); |
| 1280 | goto out_err; |
| 1281 | } |
| 1282 | |
| 1283 | if (self->header.needs_swap) |
Stephane Eranian | 444d286 | 2012-05-15 13:28:12 +0200 | [diff] [blame] | 1284 | perf_event_header__bswap(&event->header); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1285 | |
Stephane Eranian | 444d286 | 2012-05-15 13:28:12 +0200 | [diff] [blame] | 1286 | size = event->header.size; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1287 | if (size == 0) |
| 1288 | size = 8; |
| 1289 | |
Stephane Eranian | 444d286 | 2012-05-15 13:28:12 +0200 | [diff] [blame] | 1290 | if (size > cur_size) { |
| 1291 | void *new = realloc(buf, size); |
| 1292 | if (!new) { |
| 1293 | pr_err("failed to allocate memory to read event\n"); |
| 1294 | goto out_err; |
| 1295 | } |
| 1296 | buf = new; |
| 1297 | cur_size = size; |
| 1298 | event = buf; |
| 1299 | } |
| 1300 | p = event; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1301 | p += sizeof(struct perf_event_header); |
| 1302 | |
Tom Zanussi | 794e43b | 2010-05-05 00:27:40 -0500 | [diff] [blame] | 1303 | if (size - sizeof(struct perf_event_header)) { |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 1304 | err = readn(self->fd, p, size - sizeof(struct perf_event_header)); |
Tom Zanussi | 794e43b | 2010-05-05 00:27:40 -0500 | [diff] [blame] | 1305 | if (err <= 0) { |
| 1306 | if (err == 0) { |
| 1307 | pr_err("unexpected end of event stream\n"); |
| 1308 | goto done; |
| 1309 | } |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1310 | |
Tom Zanussi | 794e43b | 2010-05-05 00:27:40 -0500 | [diff] [blame] | 1311 | pr_err("failed to read event data\n"); |
| 1312 | goto out_err; |
| 1313 | } |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1314 | } |
| 1315 | |
Stephane Eranian | 444d286 | 2012-05-15 13:28:12 +0200 | [diff] [blame] | 1316 | if ((skip = perf_session__process_event(self, event, tool, head)) < 0) { |
Jiri Olsa | 9389a46 | 2012-04-16 20:42:51 +0200 | [diff] [blame] | 1317 | pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", |
Stephane Eranian | 444d286 | 2012-05-15 13:28:12 +0200 | [diff] [blame] | 1318 | head, event->header.size, event->header.type); |
Jiri Olsa | 9389a46 | 2012-04-16 20:42:51 +0200 | [diff] [blame] | 1319 | err = -EINVAL; |
| 1320 | goto out_err; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | head += size; |
| 1324 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1325 | if (skip > 0) |
| 1326 | head += skip; |
| 1327 | |
| 1328 | if (!session_done()) |
| 1329 | goto more; |
| 1330 | done: |
| 1331 | err = 0; |
| 1332 | out_err: |
Stephane Eranian | 444d286 | 2012-05-15 13:28:12 +0200 | [diff] [blame] | 1333 | free(buf); |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1334 | perf_session__warn_about_errors(self, tool); |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 1335 | perf_session_free_sample_buffers(self); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1336 | return err; |
| 1337 | } |
| 1338 | |
Frederic Weisbecker | 998bedc | 2011-05-23 13:06:28 +0200 | [diff] [blame] | 1339 | static union perf_event * |
| 1340 | fetch_mmaped_event(struct perf_session *session, |
| 1341 | u64 head, size_t mmap_size, char *buf) |
| 1342 | { |
| 1343 | union perf_event *event; |
| 1344 | |
| 1345 | /* |
| 1346 | * Ensure we have enough space remaining to read |
| 1347 | * the size of the event in the headers. |
| 1348 | */ |
| 1349 | if (head + sizeof(event->header) > mmap_size) |
| 1350 | return NULL; |
| 1351 | |
| 1352 | event = (union perf_event *)(buf + head); |
| 1353 | |
| 1354 | if (session->header.needs_swap) |
| 1355 | perf_event_header__bswap(&event->header); |
| 1356 | |
| 1357 | if (head + event->header.size > mmap_size) |
| 1358 | return NULL; |
| 1359 | |
| 1360 | return event; |
| 1361 | } |
| 1362 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1363 | int __perf_session__process_events(struct perf_session *session, |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1364 | u64 data_offset, u64 data_size, |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1365 | u64 file_size, struct perf_tool *tool) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1366 | { |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1367 | u64 head, page_offset, file_offset, file_pos, progress_next; |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 1368 | int err, mmap_prot, mmap_flags, map_idx = 0; |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1369 | size_t page_size, mmap_size; |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 1370 | char *buf, *mmaps[8]; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1371 | union perf_event *event; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1372 | uint32_t size; |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1373 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1374 | perf_tool__fill_defaults(tool); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1375 | |
Arnaldo Carvalho de Melo | 1b75962 | 2010-01-14 18:30:04 -0200 | [diff] [blame] | 1376 | page_size = sysconf(_SC_PAGESIZE); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1377 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1378 | page_offset = page_size * (data_offset / page_size); |
| 1379 | file_offset = page_offset; |
| 1380 | head = data_offset - page_offset; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1381 | |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1382 | if (data_offset + data_size < file_size) |
| 1383 | file_size = data_offset + data_size; |
| 1384 | |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1385 | progress_next = file_size / 16; |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1386 | |
| 1387 | mmap_size = session->mmap_window; |
| 1388 | if (mmap_size > file_size) |
| 1389 | mmap_size = file_size; |
| 1390 | |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 1391 | memset(mmaps, 0, sizeof(mmaps)); |
| 1392 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1393 | mmap_prot = PROT_READ; |
| 1394 | mmap_flags = MAP_SHARED; |
| 1395 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1396 | if (session->header.needs_swap) { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1397 | mmap_prot |= PROT_WRITE; |
| 1398 | mmap_flags = MAP_PRIVATE; |
| 1399 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1400 | remap: |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1401 | buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, session->fd, |
| 1402 | file_offset); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1403 | if (buf == MAP_FAILED) { |
| 1404 | pr_err("failed to mmap file\n"); |
| 1405 | err = -errno; |
| 1406 | goto out_err; |
| 1407 | } |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 1408 | mmaps[map_idx] = buf; |
| 1409 | map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1); |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1410 | file_pos = file_offset + head; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1411 | |
| 1412 | more: |
Frederic Weisbecker | 998bedc | 2011-05-23 13:06:28 +0200 | [diff] [blame] | 1413 | event = fetch_mmaped_event(session, head, mmap_size, buf); |
| 1414 | if (!event) { |
Thomas Gleixner | fe17420 | 2010-11-30 17:49:49 +0000 | [diff] [blame] | 1415 | if (mmaps[map_idx]) { |
| 1416 | munmap(mmaps[map_idx], mmap_size); |
| 1417 | mmaps[map_idx] = NULL; |
| 1418 | } |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1419 | |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1420 | page_offset = page_size * (head / page_size); |
| 1421 | file_offset += page_offset; |
| 1422 | head -= page_offset; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1423 | goto remap; |
| 1424 | } |
| 1425 | |
| 1426 | size = event->header.size; |
| 1427 | |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1428 | if (size == 0 || |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1429 | perf_session__process_event(session, event, tool, file_pos) < 0) { |
Jiri Olsa | 9389a46 | 2012-04-16 20:42:51 +0200 | [diff] [blame] | 1430 | pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", |
| 1431 | file_offset + head, event->header.size, |
| 1432 | event->header.type); |
| 1433 | err = -EINVAL; |
| 1434 | goto out_err; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | head += size; |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1438 | file_pos += size; |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1439 | |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1440 | if (file_pos >= progress_next) { |
| 1441 | progress_next += file_size / 16; |
Arnaldo Carvalho de Melo | ca59bcb | 2011-10-25 13:29:11 -0200 | [diff] [blame] | 1442 | ui_progress__update(file_pos, file_size, |
| 1443 | "Processing events..."); |
Thomas Gleixner | 55b44629 | 2010-11-30 17:49:46 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1446 | if (file_pos < file_size) |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1447 | goto more; |
Thomas Gleixner | d651328 | 2010-11-30 17:49:44 +0000 | [diff] [blame] | 1448 | |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1449 | err = 0; |
Frederic Weisbecker | c61e52e | 2010-04-24 00:04:12 +0200 | [diff] [blame] | 1450 | /* do the final flush for ordered samples */ |
Thomas Gleixner | 0331ee0 | 2010-11-30 17:49:38 +0000 | [diff] [blame] | 1451 | session->ordered_samples.next_flush = ULLONG_MAX; |
David Ahern | d25380cd | 2012-08-26 12:24:41 -0600 | [diff] [blame^] | 1452 | err = flush_sample_queue(session, tool); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1453 | out_err: |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1454 | perf_session__warn_about_errors(session, tool); |
Thomas Gleixner | 020bb75 | 2010-11-30 17:49:53 +0000 | [diff] [blame] | 1455 | perf_session_free_sample_buffers(session); |
Arnaldo Carvalho de Melo | 06aae590 | 2009-12-27 21:36:59 -0200 | [diff] [blame] | 1456 | return err; |
| 1457 | } |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 1458 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1459 | int perf_session__process_events(struct perf_session *self, |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1460 | struct perf_tool *tool) |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1461 | { |
| 1462 | int err; |
| 1463 | |
| 1464 | if (perf_session__register_idle_thread(self) == NULL) |
| 1465 | return -ENOMEM; |
| 1466 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1467 | if (!self->fd_pipe) |
| 1468 | err = __perf_session__process_events(self, |
| 1469 | self->header.data_offset, |
| 1470 | self->header.data_size, |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1471 | self->size, tool); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1472 | else |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 1473 | err = __perf_session__process_pipe_events(self, tool); |
Dave Martin | 88ca895 | 2010-07-27 11:46:12 -0300 | [diff] [blame] | 1474 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1475 | return err; |
| 1476 | } |
| 1477 | |
Arnaldo Carvalho de Melo | 7f3be65 | 2012-08-01 19:15:52 -0300 | [diff] [blame] | 1478 | bool perf_session__has_traces(struct perf_session *session, const char *msg) |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 1479 | { |
Arnaldo Carvalho de Melo | 7f3be65 | 2012-08-01 19:15:52 -0300 | [diff] [blame] | 1480 | if (!(perf_evlist__sample_type(session->evlist) & PERF_SAMPLE_RAW)) { |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1481 | pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); |
| 1482 | return false; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 1483 | } |
| 1484 | |
Arnaldo Carvalho de Melo | d549c769 | 2009-12-27 21:37:02 -0200 | [diff] [blame] | 1485 | return true; |
Arnaldo Carvalho de Melo | 2729559 | 2009-12-27 21:37:01 -0200 | [diff] [blame] | 1486 | } |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1487 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1488 | int maps__set_kallsyms_ref_reloc_sym(struct map **maps, |
| 1489 | const char *symbol_name, u64 addr) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1490 | { |
| 1491 | char *bracket; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1492 | enum map_type i; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1493 | struct ref_reloc_sym *ref; |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1494 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1495 | ref = zalloc(sizeof(struct ref_reloc_sym)); |
| 1496 | if (ref == NULL) |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1497 | return -ENOMEM; |
| 1498 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1499 | ref->name = strdup(symbol_name); |
| 1500 | if (ref->name == NULL) { |
| 1501 | free(ref); |
| 1502 | return -ENOMEM; |
| 1503 | } |
| 1504 | |
| 1505 | bracket = strchr(ref->name, ']'); |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1506 | if (bracket) |
| 1507 | *bracket = '\0'; |
| 1508 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1509 | ref->addr = addr; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1510 | |
| 1511 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1512 | struct kmap *kmap = map__kmap(maps[i]); |
| 1513 | kmap->ref_reloc_sym = ref; |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 1514 | } |
| 1515 | |
Arnaldo Carvalho de Melo | 56b03f3 | 2010-01-05 16:50:31 -0200 | [diff] [blame] | 1516 | return 0; |
| 1517 | } |
Arnaldo Carvalho de Melo | 1f626bc | 2010-05-09 19:57:08 -0300 | [diff] [blame] | 1518 | |
| 1519 | size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp) |
| 1520 | { |
| 1521 | return __dsos__fprintf(&self->host_machine.kernel_dsos, fp) + |
| 1522 | __dsos__fprintf(&self->host_machine.user_dsos, fp) + |
| 1523 | machines__fprintf_dsos(&self->machines, fp); |
| 1524 | } |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1525 | |
| 1526 | size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp, |
| 1527 | bool with_hits) |
| 1528 | { |
| 1529 | size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits); |
| 1530 | return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits); |
| 1531 | } |
Arnaldo Carvalho de Melo | e248de3 | 2011-03-05 21:40:06 -0300 | [diff] [blame] | 1532 | |
| 1533 | size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp) |
| 1534 | { |
| 1535 | struct perf_evsel *pos; |
| 1536 | size_t ret = fprintf(fp, "Aggregated stats:\n"); |
| 1537 | |
| 1538 | ret += hists__fprintf_nr_events(&session->hists, fp); |
| 1539 | |
| 1540 | list_for_each_entry(pos, &session->evlist->entries, node) { |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 1541 | ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos)); |
Arnaldo Carvalho de Melo | e248de3 | 2011-03-05 21:40:06 -0300 | [diff] [blame] | 1542 | ret += hists__fprintf_nr_events(&pos->hists, fp); |
| 1543 | } |
| 1544 | |
| 1545 | return ret; |
| 1546 | } |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1547 | |
Arnaldo Carvalho de Melo | b424eba | 2011-11-09 13:24:25 -0200 | [diff] [blame] | 1548 | size_t perf_session__fprintf(struct perf_session *session, FILE *fp) |
| 1549 | { |
| 1550 | /* |
| 1551 | * FIXME: Here we have to actually print all the machines in this |
| 1552 | * session, not just the host... |
| 1553 | */ |
| 1554 | return machine__fprintf(&session->host_machine, fp); |
| 1555 | } |
| 1556 | |
| 1557 | void perf_session__remove_thread(struct perf_session *session, |
| 1558 | struct thread *th) |
| 1559 | { |
| 1560 | /* |
| 1561 | * FIXME: This one makes no sense, we need to remove the thread from |
| 1562 | * the machine it belongs to, perf_session can have many machines, so |
| 1563 | * doing it always on ->host_machine is wrong. Fix when auditing all |
| 1564 | * the 'perf kvm' code. |
| 1565 | */ |
| 1566 | machine__remove_thread(&session->host_machine, th); |
| 1567 | } |
| 1568 | |
David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 1569 | struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session, |
| 1570 | unsigned int type) |
| 1571 | { |
| 1572 | struct perf_evsel *pos; |
| 1573 | |
| 1574 | list_for_each_entry(pos, &session->evlist->entries, node) { |
| 1575 | if (pos->attr.type == type) |
| 1576 | return pos; |
| 1577 | } |
| 1578 | return NULL; |
| 1579 | } |
| 1580 | |
Jiri Olsa | 71ad0f5 | 2012-08-07 15:20:46 +0200 | [diff] [blame] | 1581 | void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event, |
| 1582 | struct perf_sample *sample, struct machine *machine, |
| 1583 | int print_sym, int print_dso, int print_symoffset) |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1584 | { |
| 1585 | struct addr_location al; |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1586 | struct callchain_cursor_node *node; |
| 1587 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 1588 | if (perf_event__preprocess_sample(event, machine, &al, sample, |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1589 | NULL) < 0) { |
| 1590 | error("problem processing %d event, skipping it.\n", |
| 1591 | event->header.type); |
| 1592 | return; |
| 1593 | } |
| 1594 | |
| 1595 | if (symbol_conf.use_callchain && sample->callchain) { |
| 1596 | |
Jiri Olsa | 71ad0f5 | 2012-08-07 15:20:46 +0200 | [diff] [blame] | 1597 | |
| 1598 | if (machine__resolve_callchain(machine, evsel, al.thread, |
| 1599 | sample, NULL) != 0) { |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1600 | if (verbose) |
| 1601 | error("Failed to resolve callchain. Skipping\n"); |
| 1602 | return; |
| 1603 | } |
Namhyung Kim | 4726064 | 2012-05-31 14:43:26 +0900 | [diff] [blame] | 1604 | callchain_cursor_commit(&callchain_cursor); |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1605 | |
| 1606 | while (1) { |
Namhyung Kim | 4726064 | 2012-05-31 14:43:26 +0900 | [diff] [blame] | 1607 | node = callchain_cursor_current(&callchain_cursor); |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1608 | if (!node) |
| 1609 | break; |
| 1610 | |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 1611 | printf("\t%16" PRIx64, node->ip); |
| 1612 | if (print_sym) { |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 1613 | printf(" "); |
| 1614 | symbol__fprintf_symname(node->sym, stdout); |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame] | 1615 | } |
| 1616 | if (print_dso) { |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 1617 | printf(" ("); |
David Ahern | 52deff7 | 2012-05-29 22:58:26 -0600 | [diff] [blame] | 1618 | map__fprintf_dsoname(node->map, stdout); |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 1619 | printf(")"); |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 1620 | } |
| 1621 | printf("\n"); |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1622 | |
Namhyung Kim | 4726064 | 2012-05-31 14:43:26 +0900 | [diff] [blame] | 1623 | callchain_cursor_advance(&callchain_cursor); |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | } else { |
David Ahern | adc4bf9 | 2011-05-30 09:16:27 -0600 | [diff] [blame] | 1627 | printf("%16" PRIx64, sample->ip); |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 1628 | if (print_sym) { |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 1629 | printf(" "); |
Akihiro Nagai | a978f2a | 2012-01-30 13:43:15 +0900 | [diff] [blame] | 1630 | if (print_symoffset) |
| 1631 | symbol__fprintf_symname_offs(al.sym, &al, |
| 1632 | stdout); |
| 1633 | else |
| 1634 | symbol__fprintf_symname(al.sym, stdout); |
David Ahern | 610723f | 2011-05-27 14:28:44 -0600 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | if (print_dso) { |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 1638 | printf(" ("); |
| 1639 | map__fprintf_dsoname(al.map, stdout); |
| 1640 | printf(")"); |
David Ahern | 787bef1 | 2011-05-27 14:28:43 -0600 | [diff] [blame] | 1641 | } |
David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1642 | } |
| 1643 | } |
Anton Blanchard | 5d67be9 | 2011-07-04 21:57:50 +1000 | [diff] [blame] | 1644 | |
| 1645 | int perf_session__cpu_bitmap(struct perf_session *session, |
| 1646 | const char *cpu_list, unsigned long *cpu_bitmap) |
| 1647 | { |
| 1648 | int i; |
| 1649 | struct cpu_map *map; |
| 1650 | |
| 1651 | for (i = 0; i < PERF_TYPE_MAX; ++i) { |
| 1652 | struct perf_evsel *evsel; |
| 1653 | |
| 1654 | evsel = perf_session__find_first_evtype(session, i); |
| 1655 | if (!evsel) |
| 1656 | continue; |
| 1657 | |
| 1658 | if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) { |
| 1659 | pr_err("File does not contain CPU events. " |
| 1660 | "Remove -c option to proceed.\n"); |
| 1661 | return -1; |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | map = cpu_map__new(cpu_list); |
David Ahern | 47fbe53 | 2011-11-13 10:45:27 -0700 | [diff] [blame] | 1666 | if (map == NULL) { |
| 1667 | pr_err("Invalid cpu_list\n"); |
| 1668 | return -1; |
| 1669 | } |
Anton Blanchard | 5d67be9 | 2011-07-04 21:57:50 +1000 | [diff] [blame] | 1670 | |
| 1671 | for (i = 0; i < map->nr; i++) { |
| 1672 | int cpu = map->map[i]; |
| 1673 | |
| 1674 | if (cpu >= MAX_NR_CPUS) { |
| 1675 | pr_err("Requested CPU %d too large. " |
| 1676 | "Consider raising MAX_NR_CPUS\n", cpu); |
| 1677 | return -1; |
| 1678 | } |
| 1679 | |
| 1680 | set_bit(cpu, cpu_bitmap); |
| 1681 | } |
| 1682 | |
| 1683 | return 0; |
| 1684 | } |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1685 | |
| 1686 | void perf_session__fprintf_info(struct perf_session *session, FILE *fp, |
| 1687 | bool full) |
| 1688 | { |
| 1689 | struct stat st; |
| 1690 | int ret; |
| 1691 | |
| 1692 | if (session == NULL || fp == NULL) |
| 1693 | return; |
| 1694 | |
| 1695 | ret = fstat(session->fd, &st); |
| 1696 | if (ret == -1) |
| 1697 | return; |
| 1698 | |
| 1699 | fprintf(fp, "# ========\n"); |
| 1700 | fprintf(fp, "# captured on: %s", ctime(&st.st_ctime)); |
| 1701 | perf_header__fprintf_info(session, fp, full); |
| 1702 | fprintf(fp, "# ========\n#\n"); |
| 1703 | } |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 1704 | |
| 1705 | |
| 1706 | int __perf_session__set_tracepoints_handlers(struct perf_session *session, |
| 1707 | const struct perf_evsel_str_handler *assocs, |
| 1708 | size_t nr_assocs) |
| 1709 | { |
| 1710 | struct perf_evlist *evlist = session->evlist; |
| 1711 | struct event_format *format; |
| 1712 | struct perf_evsel *evsel; |
| 1713 | char *tracepoint, *name; |
| 1714 | size_t i; |
| 1715 | int err; |
| 1716 | |
| 1717 | for (i = 0; i < nr_assocs; i++) { |
| 1718 | err = -ENOMEM; |
| 1719 | tracepoint = strdup(assocs[i].name); |
| 1720 | if (tracepoint == NULL) |
| 1721 | goto out; |
| 1722 | |
| 1723 | err = -ENOENT; |
| 1724 | name = strchr(tracepoint, ':'); |
| 1725 | if (name == NULL) |
| 1726 | goto out_free; |
| 1727 | |
| 1728 | *name++ = '\0'; |
| 1729 | format = pevent_find_event_by_name(session->pevent, |
| 1730 | tracepoint, name); |
| 1731 | if (format == NULL) { |
| 1732 | /* |
| 1733 | * Adding a handler for an event not in the session, |
| 1734 | * just ignore it. |
| 1735 | */ |
| 1736 | goto next; |
| 1737 | } |
| 1738 | |
| 1739 | evsel = perf_evlist__find_tracepoint_by_id(evlist, format->id); |
| 1740 | if (evsel == NULL) |
| 1741 | goto next; |
| 1742 | |
| 1743 | err = -EEXIST; |
| 1744 | if (evsel->handler.func != NULL) |
| 1745 | goto out_free; |
| 1746 | evsel->handler.func = assocs[i].handler; |
| 1747 | next: |
| 1748 | free(tracepoint); |
| 1749 | } |
| 1750 | |
| 1751 | err = 0; |
| 1752 | out: |
| 1753 | return err; |
| 1754 | |
| 1755 | out_free: |
| 1756 | free(tracepoint); |
| 1757 | goto out; |
| 1758 | } |