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