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