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