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