Xiao Guangrong | b8f46c5 | 2010-02-03 11:53:14 +0800 | [diff] [blame] | 1 | #define _FILE_OFFSET_BITS 64 |
| 2 | |
Arnaldo Carvalho de Melo | a9072bc | 2011-10-26 12:41:38 -0200 | [diff] [blame] | 3 | #include "util.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 4 | #include <sys/types.h> |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 5 | #include <byteswap.h> |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 9 | #include <linux/list.h> |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 12 | #include <sys/utsname.h> |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 13 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 14 | #include "evlist.h" |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 15 | #include "evsel.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 16 | #include "header.h" |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 17 | #include "../perf.h" |
| 18 | #include "trace-event.h" |
Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 19 | #include "session.h" |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 20 | #include "symbol.h" |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 21 | #include "debug.h" |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 22 | #include "cpumap.h" |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 23 | #include "pmu.h" |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 24 | #include "vdso.h" |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 25 | #include "strbuf.h" |
Jiri Olsa | ebb296c | 2012-10-27 23:18:28 +0200 | [diff] [blame] | 26 | #include "build-id.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 27 | |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 28 | static bool no_buildid_cache = false; |
| 29 | |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 30 | static int trace_event_count; |
| 31 | static struct perf_trace_event_type *trace_events; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 32 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 33 | static u32 header_argc; |
| 34 | static const char **header_argv; |
| 35 | |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 36 | int perf_header__push_event(u64 id, const char *name) |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 37 | { |
Ulrich Drepper | f3054c778 | 2012-04-23 09:04:29 -0400 | [diff] [blame] | 38 | struct perf_trace_event_type *nevents; |
| 39 | |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 40 | if (strlen(name) > MAX_EVENT_NAME) |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 41 | pr_warning("Event %s will be truncated\n", name); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 42 | |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 43 | nevents = realloc(trace_events, (trace_event_count + 1) * sizeof(*trace_events)); |
Ulrich Drepper | f3054c778 | 2012-04-23 09:04:29 -0400 | [diff] [blame] | 44 | if (nevents == NULL) |
| 45 | return -ENOMEM; |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 46 | trace_events = nevents; |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 47 | |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 48 | memset(&trace_events[trace_event_count], 0, sizeof(struct perf_trace_event_type)); |
| 49 | trace_events[trace_event_count].event_id = id; |
| 50 | strncpy(trace_events[trace_event_count].name, name, MAX_EVENT_NAME - 1); |
| 51 | trace_event_count++; |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 52 | return 0; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | char *perf_header__find_event(u64 id) |
| 56 | { |
| 57 | int i; |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 58 | for (i = 0 ; i < trace_event_count; i++) { |
| 59 | if (trace_events[i].event_id == id) |
| 60 | return trace_events[i].name; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 61 | } |
| 62 | return NULL; |
| 63 | } |
| 64 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 65 | /* |
| 66 | * magic2 = "PERFILE2" |
| 67 | * must be a numerical value to let the endianness |
| 68 | * determine the memory layout. That way we are able |
| 69 | * to detect endianness when reading the perf.data file |
| 70 | * back. |
| 71 | * |
| 72 | * we check for legacy (PERFFILE) format. |
| 73 | */ |
| 74 | static const char *__perf_magic1 = "PERFFILE"; |
| 75 | static const u64 __perf_magic2 = 0x32454c4946524550ULL; |
| 76 | static const u64 __perf_magic2_sw = 0x50455246494c4532ULL; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 77 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 78 | #define PERF_MAGIC __perf_magic2 |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 79 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 80 | struct perf_file_attr { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 81 | struct perf_event_attr attr; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 82 | struct perf_file_section ids; |
| 83 | }; |
| 84 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 85 | void perf_header__set_feat(struct perf_header *header, int feat) |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 86 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 87 | set_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 88 | } |
| 89 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 90 | void perf_header__clear_feat(struct perf_header *header, int feat) |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 91 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 92 | clear_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 93 | } |
| 94 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 95 | bool perf_header__has_feat(const struct perf_header *header, int feat) |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 96 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 97 | return test_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 98 | } |
| 99 | |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 100 | static int do_write(int fd, const void *buf, size_t size) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 101 | { |
| 102 | while (size) { |
| 103 | int ret = write(fd, buf, size); |
| 104 | |
| 105 | if (ret < 0) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 106 | return -errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 107 | |
| 108 | size -= ret; |
| 109 | buf += ret; |
| 110 | } |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 111 | |
| 112 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Arnaldo Carvalho de Melo | f92cb24 | 2010-01-04 16:19:28 -0200 | [diff] [blame] | 115 | #define NAME_ALIGN 64 |
| 116 | |
| 117 | static int write_padded(int fd, const void *bf, size_t count, |
| 118 | size_t count_aligned) |
| 119 | { |
| 120 | static const char zero_buf[NAME_ALIGN]; |
| 121 | int err = do_write(fd, bf, count); |
| 122 | |
| 123 | if (!err) |
| 124 | err = do_write(fd, zero_buf, count_aligned - count); |
| 125 | |
| 126 | return err; |
| 127 | } |
| 128 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 129 | static int do_write_string(int fd, const char *str) |
| 130 | { |
| 131 | u32 len, olen; |
| 132 | int ret; |
| 133 | |
| 134 | olen = strlen(str) + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 135 | len = PERF_ALIGN(olen, NAME_ALIGN); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 136 | |
| 137 | /* write len, incl. \0 */ |
| 138 | ret = do_write(fd, &len, sizeof(len)); |
| 139 | if (ret < 0) |
| 140 | return ret; |
| 141 | |
| 142 | return write_padded(fd, str, olen, len); |
| 143 | } |
| 144 | |
| 145 | static char *do_read_string(int fd, struct perf_header *ph) |
| 146 | { |
| 147 | ssize_t sz, ret; |
| 148 | u32 len; |
| 149 | char *buf; |
| 150 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 151 | sz = readn(fd, &len, sizeof(len)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 152 | if (sz < (ssize_t)sizeof(len)) |
| 153 | return NULL; |
| 154 | |
| 155 | if (ph->needs_swap) |
| 156 | len = bswap_32(len); |
| 157 | |
| 158 | buf = malloc(len); |
| 159 | if (!buf) |
| 160 | return NULL; |
| 161 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 162 | ret = readn(fd, buf, len); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 163 | if (ret == (ssize_t)len) { |
| 164 | /* |
| 165 | * strings are padded by zeroes |
| 166 | * thus the actual strlen of buf |
| 167 | * may be less than len |
| 168 | */ |
| 169 | return buf; |
| 170 | } |
| 171 | |
| 172 | free(buf); |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | int |
| 177 | perf_header__set_cmdline(int argc, const char **argv) |
| 178 | { |
| 179 | int i; |
| 180 | |
David Ahern | 56e6f60 | 2012-07-29 20:53:51 -0600 | [diff] [blame] | 181 | /* |
| 182 | * If header_argv has already been set, do not override it. |
| 183 | * This allows a command to set the cmdline, parse args and |
| 184 | * then call another builtin function that implements a |
| 185 | * command -- e.g, cmd_kvm calling cmd_record. |
| 186 | */ |
| 187 | if (header_argv) |
| 188 | return 0; |
| 189 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 190 | header_argc = (u32)argc; |
| 191 | |
| 192 | /* do not include NULL termination */ |
| 193 | header_argv = calloc(argc, sizeof(char *)); |
| 194 | if (!header_argv) |
| 195 | return -ENOMEM; |
| 196 | |
| 197 | /* |
| 198 | * must copy argv contents because it gets moved |
| 199 | * around during option parsing |
| 200 | */ |
| 201 | for (i = 0; i < argc ; i++) |
| 202 | header_argv[i] = argv[i]; |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 207 | #define dsos__for_each_with_build_id(pos, head) \ |
| 208 | list_for_each_entry(pos, head, node) \ |
| 209 | if (!pos->has_build_id) \ |
| 210 | continue; \ |
| 211 | else |
| 212 | |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 213 | static int write_buildid(char *name, size_t name_len, u8 *build_id, |
| 214 | pid_t pid, u16 misc, int fd) |
| 215 | { |
| 216 | int err; |
| 217 | struct build_id_event b; |
| 218 | size_t len; |
| 219 | |
| 220 | len = name_len + 1; |
| 221 | len = PERF_ALIGN(len, NAME_ALIGN); |
| 222 | |
| 223 | memset(&b, 0, sizeof(b)); |
| 224 | memcpy(&b.build_id, build_id, BUILD_ID_SIZE); |
| 225 | b.pid = pid; |
| 226 | b.header.misc = misc; |
| 227 | b.header.size = sizeof(b) + len; |
| 228 | |
| 229 | err = do_write(fd, &b, sizeof(b)); |
| 230 | if (err < 0) |
| 231 | return err; |
| 232 | |
| 233 | return write_padded(fd, name, name_len + 1, len); |
| 234 | } |
| 235 | |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 236 | static int __dsos__write_buildid_table(struct list_head *head, pid_t pid, |
| 237 | u16 misc, int fd) |
| 238 | { |
| 239 | struct dso *pos; |
| 240 | |
| 241 | dsos__for_each_with_build_id(pos, head) { |
| 242 | int err; |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 243 | char *name; |
| 244 | size_t name_len; |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 245 | |
| 246 | if (!pos->hit) |
| 247 | continue; |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 248 | |
| 249 | if (is_vdso_map(pos->short_name)) { |
| 250 | name = (char *) VDSO__MAP_NAME; |
| 251 | name_len = sizeof(VDSO__MAP_NAME) + 1; |
| 252 | } else { |
| 253 | name = pos->long_name; |
| 254 | name_len = pos->long_name_len + 1; |
| 255 | } |
| 256 | |
| 257 | err = write_buildid(name, name_len, pos->build_id, |
| 258 | pid, misc, fd); |
| 259 | if (err) |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 260 | return err; |
| 261 | } |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static int machine__write_buildid_table(struct machine *machine, int fd) |
| 267 | { |
| 268 | int err; |
| 269 | u16 kmisc = PERF_RECORD_MISC_KERNEL, |
| 270 | umisc = PERF_RECORD_MISC_USER; |
| 271 | |
| 272 | if (!machine__is_host(machine)) { |
| 273 | kmisc = PERF_RECORD_MISC_GUEST_KERNEL; |
| 274 | umisc = PERF_RECORD_MISC_GUEST_USER; |
| 275 | } |
| 276 | |
| 277 | err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid, |
| 278 | kmisc, fd); |
| 279 | if (err == 0) |
| 280 | err = __dsos__write_buildid_table(&machine->user_dsos, |
| 281 | machine->pid, umisc, fd); |
| 282 | return err; |
| 283 | } |
| 284 | |
| 285 | static int dsos__write_buildid_table(struct perf_header *header, int fd) |
| 286 | { |
| 287 | struct perf_session *session = container_of(header, |
| 288 | struct perf_session, header); |
| 289 | struct rb_node *nd; |
| 290 | int err = machine__write_buildid_table(&session->host_machine, fd); |
| 291 | |
| 292 | if (err) |
| 293 | return err; |
| 294 | |
| 295 | for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { |
| 296 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 297 | err = machine__write_buildid_table(pos, fd); |
| 298 | if (err) |
| 299 | break; |
| 300 | } |
| 301 | return err; |
| 302 | } |
| 303 | |
| 304 | int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 305 | const char *name, bool is_kallsyms, bool is_vdso) |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 306 | { |
| 307 | const size_t size = PATH_MAX; |
| 308 | char *realname, *filename = zalloc(size), |
| 309 | *linkname = zalloc(size), *targetname; |
| 310 | int len, err = -1; |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 311 | bool slash = is_kallsyms || is_vdso; |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 312 | |
| 313 | if (is_kallsyms) { |
| 314 | if (symbol_conf.kptr_restrict) { |
| 315 | pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n"); |
| 316 | return 0; |
| 317 | } |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 318 | realname = (char *) name; |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 319 | } else |
| 320 | realname = realpath(name, NULL); |
| 321 | |
| 322 | if (realname == NULL || filename == NULL || linkname == NULL) |
| 323 | goto out_free; |
| 324 | |
Arnaldo Carvalho de Melo | e7f01d1 | 2012-03-14 12:29:29 -0300 | [diff] [blame] | 325 | len = scnprintf(filename, size, "%s%s%s", |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 326 | debugdir, slash ? "/" : "", |
| 327 | is_vdso ? VDSO__MAP_NAME : realname); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 328 | if (mkdir_p(filename, 0755)) |
| 329 | goto out_free; |
| 330 | |
Namhyung Kim | afda0f9 | 2012-05-01 23:19:36 +0900 | [diff] [blame] | 331 | snprintf(filename + len, size - len, "/%s", sbuild_id); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 332 | |
| 333 | if (access(filename, F_OK)) { |
| 334 | if (is_kallsyms) { |
| 335 | if (copyfile("/proc/kallsyms", filename)) |
| 336 | goto out_free; |
| 337 | } else if (link(realname, filename) && copyfile(name, filename)) |
| 338 | goto out_free; |
| 339 | } |
| 340 | |
Arnaldo Carvalho de Melo | e7f01d1 | 2012-03-14 12:29:29 -0300 | [diff] [blame] | 341 | len = scnprintf(linkname, size, "%s/.build-id/%.2s", |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 342 | debugdir, sbuild_id); |
| 343 | |
| 344 | if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) |
| 345 | goto out_free; |
| 346 | |
| 347 | snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); |
| 348 | targetname = filename + strlen(debugdir) - 5; |
| 349 | memcpy(targetname, "../..", 5); |
| 350 | |
| 351 | if (symlink(targetname, linkname) == 0) |
| 352 | err = 0; |
| 353 | out_free: |
| 354 | if (!is_kallsyms) |
| 355 | free(realname); |
| 356 | free(filename); |
| 357 | free(linkname); |
| 358 | return err; |
| 359 | } |
| 360 | |
| 361 | static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, |
| 362 | const char *name, const char *debugdir, |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 363 | bool is_kallsyms, bool is_vdso) |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 364 | { |
| 365 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 366 | |
| 367 | build_id__sprintf(build_id, build_id_size, sbuild_id); |
| 368 | |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 369 | return build_id_cache__add_s(sbuild_id, debugdir, name, |
| 370 | is_kallsyms, is_vdso); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) |
| 374 | { |
| 375 | const size_t size = PATH_MAX; |
| 376 | char *filename = zalloc(size), |
| 377 | *linkname = zalloc(size); |
| 378 | int err = -1; |
| 379 | |
| 380 | if (filename == NULL || linkname == NULL) |
| 381 | goto out_free; |
| 382 | |
| 383 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", |
| 384 | debugdir, sbuild_id, sbuild_id + 2); |
| 385 | |
| 386 | if (access(linkname, F_OK)) |
| 387 | goto out_free; |
| 388 | |
| 389 | if (readlink(linkname, filename, size - 1) < 0) |
| 390 | goto out_free; |
| 391 | |
| 392 | if (unlink(linkname)) |
| 393 | goto out_free; |
| 394 | |
| 395 | /* |
| 396 | * Since the link is relative, we must make it absolute: |
| 397 | */ |
| 398 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", |
| 399 | debugdir, sbuild_id, filename); |
| 400 | |
| 401 | if (unlink(linkname)) |
| 402 | goto out_free; |
| 403 | |
| 404 | err = 0; |
| 405 | out_free: |
| 406 | free(filename); |
| 407 | free(linkname); |
| 408 | return err; |
| 409 | } |
| 410 | |
| 411 | static int dso__cache_build_id(struct dso *dso, const char *debugdir) |
| 412 | { |
| 413 | bool is_kallsyms = dso->kernel && dso->long_name[0] != '/'; |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 414 | bool is_vdso = is_vdso_map(dso->short_name); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 415 | |
| 416 | return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 417 | dso->long_name, debugdir, |
| 418 | is_kallsyms, is_vdso); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir) |
| 422 | { |
| 423 | struct dso *pos; |
| 424 | int err = 0; |
| 425 | |
| 426 | dsos__for_each_with_build_id(pos, head) |
| 427 | if (dso__cache_build_id(pos, debugdir)) |
| 428 | err = -1; |
| 429 | |
| 430 | return err; |
| 431 | } |
| 432 | |
| 433 | static int machine__cache_build_ids(struct machine *machine, const char *debugdir) |
| 434 | { |
| 435 | int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir); |
| 436 | ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir); |
| 437 | return ret; |
| 438 | } |
| 439 | |
| 440 | static int perf_session__cache_build_ids(struct perf_session *session) |
| 441 | { |
| 442 | struct rb_node *nd; |
| 443 | int ret; |
| 444 | char debugdir[PATH_MAX]; |
| 445 | |
| 446 | snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir); |
| 447 | |
| 448 | if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) |
| 449 | return -1; |
| 450 | |
| 451 | ret = machine__cache_build_ids(&session->host_machine, debugdir); |
| 452 | |
| 453 | for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { |
| 454 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 455 | ret |= machine__cache_build_ids(pos, debugdir); |
| 456 | } |
| 457 | return ret ? -1 : 0; |
| 458 | } |
| 459 | |
| 460 | static bool machine__read_build_ids(struct machine *machine, bool with_hits) |
| 461 | { |
| 462 | bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits); |
| 463 | ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits); |
| 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) |
| 468 | { |
| 469 | struct rb_node *nd; |
| 470 | bool ret = machine__read_build_ids(&session->host_machine, with_hits); |
| 471 | |
| 472 | for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { |
| 473 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 474 | ret |= machine__read_build_ids(pos, with_hits); |
| 475 | } |
| 476 | |
| 477 | return ret; |
| 478 | } |
| 479 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 480 | static int write_tracing_data(int fd, struct perf_header *h __maybe_unused, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 481 | struct perf_evlist *evlist) |
| 482 | { |
| 483 | return read_tracing_data(fd, &evlist->entries); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | static int write_build_id(int fd, struct perf_header *h, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 488 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 489 | { |
| 490 | struct perf_session *session; |
| 491 | int err; |
| 492 | |
| 493 | session = container_of(h, struct perf_session, header); |
| 494 | |
Robert Richter | e20960c | 2011-12-07 10:02:55 +0100 | [diff] [blame] | 495 | if (!perf_session__read_build_ids(session, true)) |
| 496 | return -1; |
| 497 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 498 | err = dsos__write_buildid_table(h, fd); |
| 499 | if (err < 0) { |
| 500 | pr_debug("failed to write buildid table\n"); |
| 501 | return err; |
| 502 | } |
| 503 | if (!no_buildid_cache) |
| 504 | perf_session__cache_build_ids(session); |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 509 | static int write_hostname(int fd, struct perf_header *h __maybe_unused, |
| 510 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 511 | { |
| 512 | struct utsname uts; |
| 513 | int ret; |
| 514 | |
| 515 | ret = uname(&uts); |
| 516 | if (ret < 0) |
| 517 | return -1; |
| 518 | |
| 519 | return do_write_string(fd, uts.nodename); |
| 520 | } |
| 521 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 522 | static int write_osrelease(int fd, struct perf_header *h __maybe_unused, |
| 523 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 524 | { |
| 525 | struct utsname uts; |
| 526 | int ret; |
| 527 | |
| 528 | ret = uname(&uts); |
| 529 | if (ret < 0) |
| 530 | return -1; |
| 531 | |
| 532 | return do_write_string(fd, uts.release); |
| 533 | } |
| 534 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 535 | static int write_arch(int fd, struct perf_header *h __maybe_unused, |
| 536 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 537 | { |
| 538 | struct utsname uts; |
| 539 | int ret; |
| 540 | |
| 541 | ret = uname(&uts); |
| 542 | if (ret < 0) |
| 543 | return -1; |
| 544 | |
| 545 | return do_write_string(fd, uts.machine); |
| 546 | } |
| 547 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 548 | static int write_version(int fd, struct perf_header *h __maybe_unused, |
| 549 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 550 | { |
| 551 | return do_write_string(fd, perf_version_string); |
| 552 | } |
| 553 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 554 | static int write_cpudesc(int fd, struct perf_header *h __maybe_unused, |
| 555 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 556 | { |
| 557 | #ifndef CPUINFO_PROC |
| 558 | #define CPUINFO_PROC NULL |
| 559 | #endif |
| 560 | FILE *file; |
| 561 | char *buf = NULL; |
| 562 | char *s, *p; |
| 563 | const char *search = CPUINFO_PROC; |
| 564 | size_t len = 0; |
| 565 | int ret = -1; |
| 566 | |
| 567 | if (!search) |
| 568 | return -1; |
| 569 | |
| 570 | file = fopen("/proc/cpuinfo", "r"); |
| 571 | if (!file) |
| 572 | return -1; |
| 573 | |
| 574 | while (getline(&buf, &len, file) > 0) { |
| 575 | ret = strncmp(buf, search, strlen(search)); |
| 576 | if (!ret) |
| 577 | break; |
| 578 | } |
| 579 | |
| 580 | if (ret) |
| 581 | goto done; |
| 582 | |
| 583 | s = buf; |
| 584 | |
| 585 | p = strchr(buf, ':'); |
| 586 | if (p && *(p+1) == ' ' && *(p+2)) |
| 587 | s = p + 2; |
| 588 | p = strchr(s, '\n'); |
| 589 | if (p) |
| 590 | *p = '\0'; |
| 591 | |
| 592 | /* squash extra space characters (branding string) */ |
| 593 | p = s; |
| 594 | while (*p) { |
| 595 | if (isspace(*p)) { |
| 596 | char *r = p + 1; |
| 597 | char *q = r; |
| 598 | *p = ' '; |
| 599 | while (*q && isspace(*q)) |
| 600 | q++; |
| 601 | if (q != (p+1)) |
| 602 | while ((*r++ = *q++)); |
| 603 | } |
| 604 | p++; |
| 605 | } |
| 606 | ret = do_write_string(fd, s); |
| 607 | done: |
| 608 | free(buf); |
| 609 | fclose(file); |
| 610 | return ret; |
| 611 | } |
| 612 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 613 | static int write_nrcpus(int fd, struct perf_header *h __maybe_unused, |
| 614 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 615 | { |
| 616 | long nr; |
| 617 | u32 nrc, nra; |
| 618 | int ret; |
| 619 | |
| 620 | nr = sysconf(_SC_NPROCESSORS_CONF); |
| 621 | if (nr < 0) |
| 622 | return -1; |
| 623 | |
| 624 | nrc = (u32)(nr & UINT_MAX); |
| 625 | |
| 626 | nr = sysconf(_SC_NPROCESSORS_ONLN); |
| 627 | if (nr < 0) |
| 628 | return -1; |
| 629 | |
| 630 | nra = (u32)(nr & UINT_MAX); |
| 631 | |
| 632 | ret = do_write(fd, &nrc, sizeof(nrc)); |
| 633 | if (ret < 0) |
| 634 | return ret; |
| 635 | |
| 636 | return do_write(fd, &nra, sizeof(nra)); |
| 637 | } |
| 638 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 639 | static int write_event_desc(int fd, struct perf_header *h __maybe_unused, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 640 | struct perf_evlist *evlist) |
| 641 | { |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 642 | struct perf_evsel *evsel; |
Namhyung Kim | 74ba9e1 | 2012-09-05 14:02:47 +0900 | [diff] [blame] | 643 | u32 nre, nri, sz; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 644 | int ret; |
| 645 | |
Namhyung Kim | 74ba9e1 | 2012-09-05 14:02:47 +0900 | [diff] [blame] | 646 | nre = evlist->nr_entries; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 647 | |
| 648 | /* |
| 649 | * write number of events |
| 650 | */ |
| 651 | ret = do_write(fd, &nre, sizeof(nre)); |
| 652 | if (ret < 0) |
| 653 | return ret; |
| 654 | |
| 655 | /* |
| 656 | * size of perf_event_attr struct |
| 657 | */ |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 658 | sz = (u32)sizeof(evsel->attr); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 659 | ret = do_write(fd, &sz, sizeof(sz)); |
| 660 | if (ret < 0) |
| 661 | return ret; |
| 662 | |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 663 | list_for_each_entry(evsel, &evlist->entries, node) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 664 | |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 665 | ret = do_write(fd, &evsel->attr, sz); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 666 | if (ret < 0) |
| 667 | return ret; |
| 668 | /* |
| 669 | * write number of unique id per event |
| 670 | * there is one id per instance of an event |
| 671 | * |
| 672 | * copy into an nri to be independent of the |
| 673 | * type of ids, |
| 674 | */ |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 675 | nri = evsel->ids; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 676 | ret = do_write(fd, &nri, sizeof(nri)); |
| 677 | if (ret < 0) |
| 678 | return ret; |
| 679 | |
| 680 | /* |
| 681 | * write event string as passed on cmdline |
| 682 | */ |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 683 | ret = do_write_string(fd, perf_evsel__name(evsel)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 684 | if (ret < 0) |
| 685 | return ret; |
| 686 | /* |
| 687 | * write unique ids for this event |
| 688 | */ |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 689 | ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 690 | if (ret < 0) |
| 691 | return ret; |
| 692 | } |
| 693 | return 0; |
| 694 | } |
| 695 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 696 | static int write_cmdline(int fd, struct perf_header *h __maybe_unused, |
| 697 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 698 | { |
| 699 | char buf[MAXPATHLEN]; |
| 700 | char proc[32]; |
| 701 | u32 i, n; |
| 702 | int ret; |
| 703 | |
| 704 | /* |
| 705 | * actual atual path to perf binary |
| 706 | */ |
| 707 | sprintf(proc, "/proc/%d/exe", getpid()); |
| 708 | ret = readlink(proc, buf, sizeof(buf)); |
| 709 | if (ret <= 0) |
| 710 | return -1; |
| 711 | |
| 712 | /* readlink() does not add null termination */ |
| 713 | buf[ret] = '\0'; |
| 714 | |
| 715 | /* account for binary path */ |
| 716 | n = header_argc + 1; |
| 717 | |
| 718 | ret = do_write(fd, &n, sizeof(n)); |
| 719 | if (ret < 0) |
| 720 | return ret; |
| 721 | |
| 722 | ret = do_write_string(fd, buf); |
| 723 | if (ret < 0) |
| 724 | return ret; |
| 725 | |
| 726 | for (i = 0 ; i < header_argc; i++) { |
| 727 | ret = do_write_string(fd, header_argv[i]); |
| 728 | if (ret < 0) |
| 729 | return ret; |
| 730 | } |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | #define CORE_SIB_FMT \ |
| 735 | "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list" |
| 736 | #define THRD_SIB_FMT \ |
| 737 | "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list" |
| 738 | |
| 739 | struct cpu_topo { |
| 740 | u32 core_sib; |
| 741 | u32 thread_sib; |
| 742 | char **core_siblings; |
| 743 | char **thread_siblings; |
| 744 | }; |
| 745 | |
| 746 | static int build_cpu_topo(struct cpu_topo *tp, int cpu) |
| 747 | { |
| 748 | FILE *fp; |
| 749 | char filename[MAXPATHLEN]; |
| 750 | char *buf = NULL, *p; |
| 751 | size_t len = 0; |
| 752 | u32 i = 0; |
| 753 | int ret = -1; |
| 754 | |
| 755 | sprintf(filename, CORE_SIB_FMT, cpu); |
| 756 | fp = fopen(filename, "r"); |
| 757 | if (!fp) |
| 758 | return -1; |
| 759 | |
| 760 | if (getline(&buf, &len, fp) <= 0) |
| 761 | goto done; |
| 762 | |
| 763 | fclose(fp); |
| 764 | |
| 765 | p = strchr(buf, '\n'); |
| 766 | if (p) |
| 767 | *p = '\0'; |
| 768 | |
| 769 | for (i = 0; i < tp->core_sib; i++) { |
| 770 | if (!strcmp(buf, tp->core_siblings[i])) |
| 771 | break; |
| 772 | } |
| 773 | if (i == tp->core_sib) { |
| 774 | tp->core_siblings[i] = buf; |
| 775 | tp->core_sib++; |
| 776 | buf = NULL; |
| 777 | len = 0; |
| 778 | } |
| 779 | |
| 780 | sprintf(filename, THRD_SIB_FMT, cpu); |
| 781 | fp = fopen(filename, "r"); |
| 782 | if (!fp) |
| 783 | goto done; |
| 784 | |
| 785 | if (getline(&buf, &len, fp) <= 0) |
| 786 | goto done; |
| 787 | |
| 788 | p = strchr(buf, '\n'); |
| 789 | if (p) |
| 790 | *p = '\0'; |
| 791 | |
| 792 | for (i = 0; i < tp->thread_sib; i++) { |
| 793 | if (!strcmp(buf, tp->thread_siblings[i])) |
| 794 | break; |
| 795 | } |
| 796 | if (i == tp->thread_sib) { |
| 797 | tp->thread_siblings[i] = buf; |
| 798 | tp->thread_sib++; |
| 799 | buf = NULL; |
| 800 | } |
| 801 | ret = 0; |
| 802 | done: |
| 803 | if(fp) |
| 804 | fclose(fp); |
| 805 | free(buf); |
| 806 | return ret; |
| 807 | } |
| 808 | |
| 809 | static void free_cpu_topo(struct cpu_topo *tp) |
| 810 | { |
| 811 | u32 i; |
| 812 | |
| 813 | if (!tp) |
| 814 | return; |
| 815 | |
| 816 | for (i = 0 ; i < tp->core_sib; i++) |
| 817 | free(tp->core_siblings[i]); |
| 818 | |
| 819 | for (i = 0 ; i < tp->thread_sib; i++) |
| 820 | free(tp->thread_siblings[i]); |
| 821 | |
| 822 | free(tp); |
| 823 | } |
| 824 | |
| 825 | static struct cpu_topo *build_cpu_topology(void) |
| 826 | { |
| 827 | struct cpu_topo *tp; |
| 828 | void *addr; |
| 829 | u32 nr, i; |
| 830 | size_t sz; |
| 831 | long ncpus; |
| 832 | int ret = -1; |
| 833 | |
| 834 | ncpus = sysconf(_SC_NPROCESSORS_CONF); |
| 835 | if (ncpus < 0) |
| 836 | return NULL; |
| 837 | |
| 838 | nr = (u32)(ncpus & UINT_MAX); |
| 839 | |
| 840 | sz = nr * sizeof(char *); |
| 841 | |
| 842 | addr = calloc(1, sizeof(*tp) + 2 * sz); |
| 843 | if (!addr) |
| 844 | return NULL; |
| 845 | |
| 846 | tp = addr; |
| 847 | |
| 848 | addr += sizeof(*tp); |
| 849 | tp->core_siblings = addr; |
| 850 | addr += sz; |
| 851 | tp->thread_siblings = addr; |
| 852 | |
| 853 | for (i = 0; i < nr; i++) { |
| 854 | ret = build_cpu_topo(tp, i); |
| 855 | if (ret < 0) |
| 856 | break; |
| 857 | } |
| 858 | if (ret) { |
| 859 | free_cpu_topo(tp); |
| 860 | tp = NULL; |
| 861 | } |
| 862 | return tp; |
| 863 | } |
| 864 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 865 | static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused, |
| 866 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 867 | { |
| 868 | struct cpu_topo *tp; |
| 869 | u32 i; |
| 870 | int ret; |
| 871 | |
| 872 | tp = build_cpu_topology(); |
| 873 | if (!tp) |
| 874 | return -1; |
| 875 | |
| 876 | ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib)); |
| 877 | if (ret < 0) |
| 878 | goto done; |
| 879 | |
| 880 | for (i = 0; i < tp->core_sib; i++) { |
| 881 | ret = do_write_string(fd, tp->core_siblings[i]); |
| 882 | if (ret < 0) |
| 883 | goto done; |
| 884 | } |
| 885 | ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib)); |
| 886 | if (ret < 0) |
| 887 | goto done; |
| 888 | |
| 889 | for (i = 0; i < tp->thread_sib; i++) { |
| 890 | ret = do_write_string(fd, tp->thread_siblings[i]); |
| 891 | if (ret < 0) |
| 892 | break; |
| 893 | } |
| 894 | done: |
| 895 | free_cpu_topo(tp); |
| 896 | return ret; |
| 897 | } |
| 898 | |
| 899 | |
| 900 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 901 | static int write_total_mem(int fd, struct perf_header *h __maybe_unused, |
| 902 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 903 | { |
| 904 | char *buf = NULL; |
| 905 | FILE *fp; |
| 906 | size_t len = 0; |
| 907 | int ret = -1, n; |
| 908 | uint64_t mem; |
| 909 | |
| 910 | fp = fopen("/proc/meminfo", "r"); |
| 911 | if (!fp) |
| 912 | return -1; |
| 913 | |
| 914 | while (getline(&buf, &len, fp) > 0) { |
| 915 | ret = strncmp(buf, "MemTotal:", 9); |
| 916 | if (!ret) |
| 917 | break; |
| 918 | } |
| 919 | if (!ret) { |
| 920 | n = sscanf(buf, "%*s %"PRIu64, &mem); |
| 921 | if (n == 1) |
| 922 | ret = do_write(fd, &mem, sizeof(mem)); |
| 923 | } |
| 924 | free(buf); |
| 925 | fclose(fp); |
| 926 | return ret; |
| 927 | } |
| 928 | |
| 929 | static int write_topo_node(int fd, int node) |
| 930 | { |
| 931 | char str[MAXPATHLEN]; |
| 932 | char field[32]; |
| 933 | char *buf = NULL, *p; |
| 934 | size_t len = 0; |
| 935 | FILE *fp; |
| 936 | u64 mem_total, mem_free, mem; |
| 937 | int ret = -1; |
| 938 | |
| 939 | sprintf(str, "/sys/devices/system/node/node%d/meminfo", node); |
| 940 | fp = fopen(str, "r"); |
| 941 | if (!fp) |
| 942 | return -1; |
| 943 | |
| 944 | while (getline(&buf, &len, fp) > 0) { |
| 945 | /* skip over invalid lines */ |
| 946 | if (!strchr(buf, ':')) |
| 947 | continue; |
| 948 | if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2) |
| 949 | goto done; |
| 950 | if (!strcmp(field, "MemTotal:")) |
| 951 | mem_total = mem; |
| 952 | if (!strcmp(field, "MemFree:")) |
| 953 | mem_free = mem; |
| 954 | } |
| 955 | |
| 956 | fclose(fp); |
| 957 | |
| 958 | ret = do_write(fd, &mem_total, sizeof(u64)); |
| 959 | if (ret) |
| 960 | goto done; |
| 961 | |
| 962 | ret = do_write(fd, &mem_free, sizeof(u64)); |
| 963 | if (ret) |
| 964 | goto done; |
| 965 | |
| 966 | ret = -1; |
| 967 | sprintf(str, "/sys/devices/system/node/node%d/cpulist", node); |
| 968 | |
| 969 | fp = fopen(str, "r"); |
| 970 | if (!fp) |
| 971 | goto done; |
| 972 | |
| 973 | if (getline(&buf, &len, fp) <= 0) |
| 974 | goto done; |
| 975 | |
| 976 | p = strchr(buf, '\n'); |
| 977 | if (p) |
| 978 | *p = '\0'; |
| 979 | |
| 980 | ret = do_write_string(fd, buf); |
| 981 | done: |
| 982 | free(buf); |
| 983 | fclose(fp); |
| 984 | return ret; |
| 985 | } |
| 986 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 987 | static int write_numa_topology(int fd, struct perf_header *h __maybe_unused, |
| 988 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 989 | { |
| 990 | char *buf = NULL; |
| 991 | size_t len = 0; |
| 992 | FILE *fp; |
| 993 | struct cpu_map *node_map = NULL; |
| 994 | char *c; |
| 995 | u32 nr, i, j; |
| 996 | int ret = -1; |
| 997 | |
| 998 | fp = fopen("/sys/devices/system/node/online", "r"); |
| 999 | if (!fp) |
| 1000 | return -1; |
| 1001 | |
| 1002 | if (getline(&buf, &len, fp) <= 0) |
| 1003 | goto done; |
| 1004 | |
| 1005 | c = strchr(buf, '\n'); |
| 1006 | if (c) |
| 1007 | *c = '\0'; |
| 1008 | |
| 1009 | node_map = cpu_map__new(buf); |
| 1010 | if (!node_map) |
| 1011 | goto done; |
| 1012 | |
| 1013 | nr = (u32)node_map->nr; |
| 1014 | |
| 1015 | ret = do_write(fd, &nr, sizeof(nr)); |
| 1016 | if (ret < 0) |
| 1017 | goto done; |
| 1018 | |
| 1019 | for (i = 0; i < nr; i++) { |
| 1020 | j = (u32)node_map->map[i]; |
| 1021 | ret = do_write(fd, &j, sizeof(j)); |
| 1022 | if (ret < 0) |
| 1023 | break; |
| 1024 | |
| 1025 | ret = write_topo_node(fd, i); |
| 1026 | if (ret < 0) |
| 1027 | break; |
| 1028 | } |
| 1029 | done: |
| 1030 | free(buf); |
| 1031 | fclose(fp); |
| 1032 | free(node_map); |
| 1033 | return ret; |
| 1034 | } |
| 1035 | |
| 1036 | /* |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1037 | * File format: |
| 1038 | * |
| 1039 | * struct pmu_mappings { |
| 1040 | * u32 pmu_num; |
| 1041 | * struct pmu_map { |
| 1042 | * u32 type; |
| 1043 | * char name[]; |
| 1044 | * }[pmu_num]; |
| 1045 | * }; |
| 1046 | */ |
| 1047 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1048 | static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused, |
| 1049 | struct perf_evlist *evlist __maybe_unused) |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1050 | { |
| 1051 | struct perf_pmu *pmu = NULL; |
| 1052 | off_t offset = lseek(fd, 0, SEEK_CUR); |
| 1053 | __u32 pmu_num = 0; |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1054 | int ret; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1055 | |
| 1056 | /* write real pmu_num later */ |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1057 | ret = do_write(fd, &pmu_num, sizeof(pmu_num)); |
| 1058 | if (ret < 0) |
| 1059 | return ret; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1060 | |
| 1061 | while ((pmu = perf_pmu__scan(pmu))) { |
| 1062 | if (!pmu->name) |
| 1063 | continue; |
| 1064 | pmu_num++; |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1065 | |
| 1066 | ret = do_write(fd, &pmu->type, sizeof(pmu->type)); |
| 1067 | if (ret < 0) |
| 1068 | return ret; |
| 1069 | |
| 1070 | ret = do_write_string(fd, pmu->name); |
| 1071 | if (ret < 0) |
| 1072 | return ret; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) { |
| 1076 | /* discard all */ |
| 1077 | lseek(fd, offset, SEEK_SET); |
| 1078 | return -1; |
| 1079 | } |
| 1080 | |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | /* |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1085 | * default get_cpuid(): nothing gets recorded |
| 1086 | * actual implementation must be in arch/$(ARCH)/util/header.c |
| 1087 | */ |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1088 | int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused, |
| 1089 | size_t sz __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1090 | { |
| 1091 | return -1; |
| 1092 | } |
| 1093 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1094 | static int write_cpuid(int fd, struct perf_header *h __maybe_unused, |
| 1095 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1096 | { |
| 1097 | char buffer[64]; |
| 1098 | int ret; |
| 1099 | |
| 1100 | ret = get_cpuid(buffer, sizeof(buffer)); |
| 1101 | if (!ret) |
| 1102 | goto write_it; |
| 1103 | |
| 1104 | return -1; |
| 1105 | write_it: |
| 1106 | return do_write_string(fd, buffer); |
| 1107 | } |
| 1108 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1109 | static int write_branch_stack(int fd __maybe_unused, |
| 1110 | struct perf_header *h __maybe_unused, |
| 1111 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | 330aa67 | 2012-03-08 23:47:46 +0100 | [diff] [blame] | 1112 | { |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1116 | static void print_hostname(struct perf_header *ph, int fd __maybe_unused, |
| 1117 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1118 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1119 | fprintf(fp, "# hostname : %s\n", ph->env.hostname); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1120 | } |
| 1121 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1122 | static void print_osrelease(struct perf_header *ph, int fd __maybe_unused, |
| 1123 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1124 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1125 | fprintf(fp, "# os release : %s\n", ph->env.os_release); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1126 | } |
| 1127 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1128 | static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1129 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1130 | fprintf(fp, "# arch : %s\n", ph->env.arch); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1133 | static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused, |
| 1134 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1135 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1136 | fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1137 | } |
| 1138 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1139 | static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused, |
| 1140 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1141 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1142 | fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online); |
| 1143 | fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1144 | } |
| 1145 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1146 | static void print_version(struct perf_header *ph, int fd __maybe_unused, |
| 1147 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1148 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1149 | fprintf(fp, "# perf version : %s\n", ph->env.version); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1152 | static void print_cmdline(struct perf_header *ph, int fd __maybe_unused, |
| 1153 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1154 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1155 | int nr, i; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1156 | char *str; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1157 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1158 | nr = ph->env.nr_cmdline; |
| 1159 | str = ph->env.cmdline; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1160 | |
| 1161 | fprintf(fp, "# cmdline : "); |
| 1162 | |
| 1163 | for (i = 0; i < nr; i++) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1164 | fprintf(fp, "%s ", str); |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1165 | str += strlen(str) + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1166 | } |
| 1167 | fputc('\n', fp); |
| 1168 | } |
| 1169 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1170 | static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused, |
| 1171 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1172 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1173 | int nr, i; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1174 | char *str; |
| 1175 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1176 | nr = ph->env.nr_sibling_cores; |
| 1177 | str = ph->env.sibling_cores; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1178 | |
| 1179 | for (i = 0; i < nr; i++) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1180 | fprintf(fp, "# sibling cores : %s\n", str); |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1181 | str += strlen(str) + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1184 | nr = ph->env.nr_sibling_threads; |
| 1185 | str = ph->env.sibling_threads; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1186 | |
| 1187 | for (i = 0; i < nr; i++) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1188 | fprintf(fp, "# sibling threads : %s\n", str); |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1189 | str += strlen(str) + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1193 | static void free_event_desc(struct perf_evsel *events) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1194 | { |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1195 | struct perf_evsel *evsel; |
| 1196 | |
| 1197 | if (!events) |
| 1198 | return; |
| 1199 | |
| 1200 | for (evsel = events; evsel->attr.size; evsel++) { |
| 1201 | if (evsel->name) |
| 1202 | free(evsel->name); |
| 1203 | if (evsel->id) |
| 1204 | free(evsel->id); |
| 1205 | } |
| 1206 | |
| 1207 | free(events); |
| 1208 | } |
| 1209 | |
| 1210 | static struct perf_evsel * |
| 1211 | read_event_desc(struct perf_header *ph, int fd) |
| 1212 | { |
| 1213 | struct perf_evsel *evsel, *events = NULL; |
| 1214 | u64 *id; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1215 | void *buf = NULL; |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1216 | u32 nre, sz, nr, i, j; |
| 1217 | ssize_t ret; |
| 1218 | size_t msz; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1219 | |
| 1220 | /* number of events */ |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1221 | ret = readn(fd, &nre, sizeof(nre)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1222 | if (ret != (ssize_t)sizeof(nre)) |
| 1223 | goto error; |
| 1224 | |
| 1225 | if (ph->needs_swap) |
| 1226 | nre = bswap_32(nre); |
| 1227 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1228 | ret = readn(fd, &sz, sizeof(sz)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1229 | if (ret != (ssize_t)sizeof(sz)) |
| 1230 | goto error; |
| 1231 | |
| 1232 | if (ph->needs_swap) |
| 1233 | sz = bswap_32(sz); |
| 1234 | |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1235 | /* buffer to hold on file attr struct */ |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1236 | buf = malloc(sz); |
| 1237 | if (!buf) |
| 1238 | goto error; |
| 1239 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1240 | /* the last event terminates with evsel->attr.size == 0: */ |
| 1241 | events = calloc(nre + 1, sizeof(*events)); |
| 1242 | if (!events) |
| 1243 | goto error; |
| 1244 | |
| 1245 | msz = sizeof(evsel->attr); |
Jiri Olsa | 9fafd98 | 2012-03-20 19:15:39 +0100 | [diff] [blame] | 1246 | if (sz < msz) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1247 | msz = sz; |
| 1248 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1249 | for (i = 0, evsel = events; i < nre; evsel++, i++) { |
| 1250 | evsel->idx = i; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1251 | |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1252 | /* |
| 1253 | * must read entire on-file attr struct to |
| 1254 | * sync up with layout. |
| 1255 | */ |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1256 | ret = readn(fd, buf, sz); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1257 | if (ret != (ssize_t)sz) |
| 1258 | goto error; |
| 1259 | |
| 1260 | if (ph->needs_swap) |
| 1261 | perf_event__attr_swap(buf); |
| 1262 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1263 | memcpy(&evsel->attr, buf, msz); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1264 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1265 | ret = readn(fd, &nr, sizeof(nr)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1266 | if (ret != (ssize_t)sizeof(nr)) |
| 1267 | goto error; |
| 1268 | |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1269 | if (ph->needs_swap) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1270 | nr = bswap_32(nr); |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1271 | evsel->needs_swap = true; |
| 1272 | } |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1273 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1274 | evsel->name = do_read_string(fd, ph); |
| 1275 | |
| 1276 | if (!nr) |
| 1277 | continue; |
| 1278 | |
| 1279 | id = calloc(nr, sizeof(*id)); |
| 1280 | if (!id) |
| 1281 | goto error; |
| 1282 | evsel->ids = nr; |
| 1283 | evsel->id = id; |
| 1284 | |
| 1285 | for (j = 0 ; j < nr; j++) { |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1286 | ret = readn(fd, id, sizeof(*id)); |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1287 | if (ret != (ssize_t)sizeof(*id)) |
| 1288 | goto error; |
| 1289 | if (ph->needs_swap) |
| 1290 | *id = bswap_64(*id); |
| 1291 | id++; |
| 1292 | } |
| 1293 | } |
| 1294 | out: |
| 1295 | if (buf) |
| 1296 | free(buf); |
| 1297 | return events; |
| 1298 | error: |
| 1299 | if (events) |
| 1300 | free_event_desc(events); |
| 1301 | events = NULL; |
| 1302 | goto out; |
| 1303 | } |
| 1304 | |
| 1305 | static void print_event_desc(struct perf_header *ph, int fd, FILE *fp) |
| 1306 | { |
| 1307 | struct perf_evsel *evsel, *events = read_event_desc(ph, fd); |
| 1308 | u32 j; |
| 1309 | u64 *id; |
| 1310 | |
| 1311 | if (!events) { |
| 1312 | fprintf(fp, "# event desc: not available or unable to read\n"); |
| 1313 | return; |
| 1314 | } |
| 1315 | |
| 1316 | for (evsel = events; evsel->attr.size; evsel++) { |
| 1317 | fprintf(fp, "# event : name = %s, ", evsel->name); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1318 | |
| 1319 | fprintf(fp, "type = %d, config = 0x%"PRIx64 |
| 1320 | ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64, |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1321 | evsel->attr.type, |
| 1322 | (u64)evsel->attr.config, |
| 1323 | (u64)evsel->attr.config1, |
| 1324 | (u64)evsel->attr.config2); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1325 | |
| 1326 | fprintf(fp, ", excl_usr = %d, excl_kern = %d", |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1327 | evsel->attr.exclude_user, |
| 1328 | evsel->attr.exclude_kernel); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1329 | |
David Ahern | 78b961f | 2012-07-20 17:25:52 -0600 | [diff] [blame] | 1330 | fprintf(fp, ", excl_host = %d, excl_guest = %d", |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1331 | evsel->attr.exclude_host, |
| 1332 | evsel->attr.exclude_guest); |
David Ahern | 78b961f | 2012-07-20 17:25:52 -0600 | [diff] [blame] | 1333 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1334 | fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip); |
David Ahern | 78b961f | 2012-07-20 17:25:52 -0600 | [diff] [blame] | 1335 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1336 | if (evsel->ids) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1337 | fprintf(fp, ", id = {"); |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1338 | for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) { |
| 1339 | if (j) |
| 1340 | fputc(',', fp); |
| 1341 | fprintf(fp, " %"PRIu64, *id); |
| 1342 | } |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1343 | fprintf(fp, " }"); |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1344 | } |
| 1345 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1346 | fputc('\n', fp); |
| 1347 | } |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1348 | |
| 1349 | free_event_desc(events); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1350 | } |
| 1351 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1352 | static void print_total_mem(struct perf_header *ph, int fd __maybe_unused, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1353 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1354 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1355 | fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1356 | } |
| 1357 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1358 | static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1359 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1360 | { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1361 | u32 nr, c, i; |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1362 | char *str, *tmp; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1363 | uint64_t mem_total, mem_free; |
| 1364 | |
| 1365 | /* nr nodes */ |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1366 | nr = ph->env.nr_numa_nodes; |
| 1367 | str = ph->env.numa_nodes; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1368 | |
| 1369 | for (i = 0; i < nr; i++) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1370 | /* node number */ |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1371 | c = strtoul(str, &tmp, 0); |
| 1372 | if (*tmp != ':') |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1373 | goto error; |
| 1374 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1375 | str = tmp + 1; |
| 1376 | mem_total = strtoull(str, &tmp, 0); |
| 1377 | if (*tmp != ':') |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1378 | goto error; |
| 1379 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1380 | str = tmp + 1; |
| 1381 | mem_free = strtoull(str, &tmp, 0); |
| 1382 | if (*tmp != ':') |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1383 | goto error; |
| 1384 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1385 | fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," |
| 1386 | " free = %"PRIu64" kB\n", |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1387 | c, mem_total, mem_free); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1388 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1389 | str = tmp + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1390 | fprintf(fp, "# node%u cpu list : %s\n", c, str); |
Namhyung Kim | 1234471 | 2012-10-23 22:44:49 +0900 | [diff] [blame] | 1391 | |
| 1392 | str += strlen(str) + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1393 | } |
| 1394 | return; |
| 1395 | error: |
| 1396 | fprintf(fp, "# numa topology : not available\n"); |
| 1397 | } |
| 1398 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1399 | static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1400 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1401 | fprintf(fp, "# cpuid : %s\n", ph->env.cpuid); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1402 | } |
| 1403 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1404 | static void print_branch_stack(struct perf_header *ph __maybe_unused, |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1405 | int fd __maybe_unused, FILE *fp) |
Stephane Eranian | 330aa67 | 2012-03-08 23:47:46 +0100 | [diff] [blame] | 1406 | { |
| 1407 | fprintf(fp, "# contains samples with branch stack\n"); |
| 1408 | } |
| 1409 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1410 | static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused, |
| 1411 | FILE *fp) |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1412 | { |
| 1413 | const char *delimiter = "# pmu mappings: "; |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1414 | char *str, *tmp; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1415 | u32 pmu_num; |
| 1416 | u32 type; |
| 1417 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1418 | pmu_num = ph->env.nr_pmu_mappings; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1419 | if (!pmu_num) { |
| 1420 | fprintf(fp, "# pmu mappings: not available\n"); |
| 1421 | return; |
| 1422 | } |
| 1423 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1424 | str = ph->env.pmu_mappings; |
Namhyung Kim | be4a2de | 2012-09-05 14:02:49 +0900 | [diff] [blame] | 1425 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1426 | while (pmu_num) { |
| 1427 | type = strtoul(str, &tmp, 0); |
| 1428 | if (*tmp != ':') |
| 1429 | goto error; |
| 1430 | |
| 1431 | str = tmp + 1; |
| 1432 | fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type); |
| 1433 | |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1434 | delimiter = ", "; |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1435 | str += strlen(str) + 1; |
| 1436 | pmu_num--; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | fprintf(fp, "\n"); |
| 1440 | |
| 1441 | if (!pmu_num) |
| 1442 | return; |
| 1443 | error: |
| 1444 | fprintf(fp, "# pmu mappings: unable to read\n"); |
| 1445 | } |
| 1446 | |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1447 | static int __event_process_build_id(struct build_id_event *bev, |
| 1448 | char *filename, |
| 1449 | struct perf_session *session) |
| 1450 | { |
| 1451 | int err = -1; |
| 1452 | struct list_head *head; |
| 1453 | struct machine *machine; |
| 1454 | u16 misc; |
| 1455 | struct dso *dso; |
| 1456 | enum dso_kernel_type dso_type; |
| 1457 | |
| 1458 | machine = perf_session__findnew_machine(session, bev->pid); |
| 1459 | if (!machine) |
| 1460 | goto out; |
| 1461 | |
| 1462 | misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 1463 | |
| 1464 | switch (misc) { |
| 1465 | case PERF_RECORD_MISC_KERNEL: |
| 1466 | dso_type = DSO_TYPE_KERNEL; |
| 1467 | head = &machine->kernel_dsos; |
| 1468 | break; |
| 1469 | case PERF_RECORD_MISC_GUEST_KERNEL: |
| 1470 | dso_type = DSO_TYPE_GUEST_KERNEL; |
| 1471 | head = &machine->kernel_dsos; |
| 1472 | break; |
| 1473 | case PERF_RECORD_MISC_USER: |
| 1474 | case PERF_RECORD_MISC_GUEST_USER: |
| 1475 | dso_type = DSO_TYPE_USER; |
| 1476 | head = &machine->user_dsos; |
| 1477 | break; |
| 1478 | default: |
| 1479 | goto out; |
| 1480 | } |
| 1481 | |
| 1482 | dso = __dsos__findnew(head, filename); |
| 1483 | if (dso != NULL) { |
| 1484 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 1485 | |
| 1486 | dso__set_build_id(dso, &bev->build_id); |
| 1487 | |
| 1488 | if (filename[0] == '[') |
| 1489 | dso->kernel = dso_type; |
| 1490 | |
| 1491 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), |
| 1492 | sbuild_id); |
| 1493 | pr_debug("build id event received for %s: %s\n", |
| 1494 | dso->long_name, sbuild_id); |
| 1495 | } |
| 1496 | |
| 1497 | err = 0; |
| 1498 | out: |
| 1499 | return err; |
| 1500 | } |
| 1501 | |
| 1502 | static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, |
| 1503 | int input, u64 offset, u64 size) |
| 1504 | { |
| 1505 | struct perf_session *session = container_of(header, struct perf_session, header); |
| 1506 | struct { |
| 1507 | struct perf_event_header header; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 1508 | u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))]; |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1509 | char filename[0]; |
| 1510 | } old_bev; |
| 1511 | struct build_id_event bev; |
| 1512 | char filename[PATH_MAX]; |
| 1513 | u64 limit = offset + size; |
| 1514 | |
| 1515 | while (offset < limit) { |
| 1516 | ssize_t len; |
| 1517 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1518 | if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1519 | return -1; |
| 1520 | |
| 1521 | if (header->needs_swap) |
| 1522 | perf_event_header__bswap(&old_bev.header); |
| 1523 | |
| 1524 | len = old_bev.header.size - sizeof(old_bev); |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1525 | if (readn(input, filename, len) != len) |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1526 | return -1; |
| 1527 | |
| 1528 | bev.header = old_bev.header; |
| 1529 | |
| 1530 | /* |
| 1531 | * As the pid is the missing value, we need to fill |
| 1532 | * it properly. The header.misc value give us nice hint. |
| 1533 | */ |
| 1534 | bev.pid = HOST_KERNEL_ID; |
| 1535 | if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || |
| 1536 | bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) |
| 1537 | bev.pid = DEFAULT_GUEST_KERNEL_ID; |
| 1538 | |
| 1539 | memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); |
| 1540 | __event_process_build_id(&bev, filename, session); |
| 1541 | |
| 1542 | offset += bev.header.size; |
| 1543 | } |
| 1544 | |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
| 1548 | static int perf_header__read_build_ids(struct perf_header *header, |
| 1549 | int input, u64 offset, u64 size) |
| 1550 | { |
| 1551 | struct perf_session *session = container_of(header, struct perf_session, header); |
| 1552 | struct build_id_event bev; |
| 1553 | char filename[PATH_MAX]; |
| 1554 | u64 limit = offset + size, orig_offset = offset; |
| 1555 | int err = -1; |
| 1556 | |
| 1557 | while (offset < limit) { |
| 1558 | ssize_t len; |
| 1559 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1560 | if (readn(input, &bev, sizeof(bev)) != sizeof(bev)) |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1561 | goto out; |
| 1562 | |
| 1563 | if (header->needs_swap) |
| 1564 | perf_event_header__bswap(&bev.header); |
| 1565 | |
| 1566 | len = bev.header.size - sizeof(bev); |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1567 | if (readn(input, filename, len) != len) |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1568 | goto out; |
| 1569 | /* |
| 1570 | * The a1645ce1 changeset: |
| 1571 | * |
| 1572 | * "perf: 'perf kvm' tool for monitoring guest performance from host" |
| 1573 | * |
| 1574 | * Added a field to struct build_id_event that broke the file |
| 1575 | * format. |
| 1576 | * |
| 1577 | * Since the kernel build-id is the first entry, process the |
| 1578 | * table using the old format if the well known |
| 1579 | * '[kernel.kallsyms]' string for the kernel build-id has the |
| 1580 | * first 4 characters chopped off (where the pid_t sits). |
| 1581 | */ |
| 1582 | if (memcmp(filename, "nel.kallsyms]", 13) == 0) { |
| 1583 | if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) |
| 1584 | return -1; |
| 1585 | return perf_header__read_build_ids_abi_quirk(header, input, offset, size); |
| 1586 | } |
| 1587 | |
| 1588 | __event_process_build_id(&bev, filename, session); |
| 1589 | |
| 1590 | offset += bev.header.size; |
| 1591 | } |
| 1592 | err = 0; |
| 1593 | out: |
| 1594 | return err; |
| 1595 | } |
| 1596 | |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1597 | static int process_tracing_data(struct perf_file_section *section __maybe_unused, |
| 1598 | struct perf_header *ph __maybe_unused, |
| 1599 | int fd, void *data) |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1600 | { |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 1601 | trace_report(fd, data, false); |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1602 | return 0; |
| 1603 | } |
| 1604 | |
| 1605 | static int process_build_id(struct perf_file_section *section, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1606 | struct perf_header *ph, int fd, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1607 | void *data __maybe_unused) |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1608 | { |
| 1609 | if (perf_header__read_build_ids(ph, fd, section->offset, section->size)) |
| 1610 | pr_debug("Failed to read buildids, continuing...\n"); |
| 1611 | return 0; |
| 1612 | } |
| 1613 | |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1614 | static int process_hostname(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1615 | struct perf_header *ph, int fd, |
| 1616 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1617 | { |
| 1618 | ph->env.hostname = do_read_string(fd, ph); |
| 1619 | return ph->env.hostname ? 0 : -ENOMEM; |
| 1620 | } |
| 1621 | |
| 1622 | static int process_osrelease(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1623 | struct perf_header *ph, int fd, |
| 1624 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1625 | { |
| 1626 | ph->env.os_release = do_read_string(fd, ph); |
| 1627 | return ph->env.os_release ? 0 : -ENOMEM; |
| 1628 | } |
| 1629 | |
| 1630 | static int process_version(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1631 | struct perf_header *ph, int fd, |
| 1632 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1633 | { |
| 1634 | ph->env.version = do_read_string(fd, ph); |
| 1635 | return ph->env.version ? 0 : -ENOMEM; |
| 1636 | } |
| 1637 | |
| 1638 | static int process_arch(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1639 | struct perf_header *ph, int fd, |
| 1640 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1641 | { |
| 1642 | ph->env.arch = do_read_string(fd, ph); |
| 1643 | return ph->env.arch ? 0 : -ENOMEM; |
| 1644 | } |
| 1645 | |
| 1646 | static int process_nrcpus(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1647 | struct perf_header *ph, int fd, |
| 1648 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1649 | { |
| 1650 | size_t ret; |
| 1651 | u32 nr; |
| 1652 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1653 | ret = readn(fd, &nr, sizeof(nr)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1654 | if (ret != sizeof(nr)) |
| 1655 | return -1; |
| 1656 | |
| 1657 | if (ph->needs_swap) |
| 1658 | nr = bswap_32(nr); |
| 1659 | |
| 1660 | ph->env.nr_cpus_online = nr; |
| 1661 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1662 | ret = readn(fd, &nr, sizeof(nr)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1663 | if (ret != sizeof(nr)) |
| 1664 | return -1; |
| 1665 | |
| 1666 | if (ph->needs_swap) |
| 1667 | nr = bswap_32(nr); |
| 1668 | |
| 1669 | ph->env.nr_cpus_avail = nr; |
| 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | static int process_cpudesc(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1674 | struct perf_header *ph, int fd, |
| 1675 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1676 | { |
| 1677 | ph->env.cpu_desc = do_read_string(fd, ph); |
| 1678 | return ph->env.cpu_desc ? 0 : -ENOMEM; |
| 1679 | } |
| 1680 | |
| 1681 | static int process_cpuid(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1682 | struct perf_header *ph, int fd, |
| 1683 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1684 | { |
| 1685 | ph->env.cpuid = do_read_string(fd, ph); |
| 1686 | return ph->env.cpuid ? 0 : -ENOMEM; |
| 1687 | } |
| 1688 | |
| 1689 | static int process_total_mem(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1690 | struct perf_header *ph, int fd, |
| 1691 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1692 | { |
| 1693 | uint64_t mem; |
| 1694 | size_t ret; |
| 1695 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1696 | ret = readn(fd, &mem, sizeof(mem)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1697 | if (ret != sizeof(mem)) |
| 1698 | return -1; |
| 1699 | |
| 1700 | if (ph->needs_swap) |
| 1701 | mem = bswap_64(mem); |
| 1702 | |
| 1703 | ph->env.total_mem = mem; |
| 1704 | return 0; |
| 1705 | } |
| 1706 | |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 1707 | static struct perf_evsel * |
| 1708 | perf_evlist__find_by_index(struct perf_evlist *evlist, int idx) |
| 1709 | { |
| 1710 | struct perf_evsel *evsel; |
| 1711 | |
| 1712 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 1713 | if (evsel->idx == idx) |
| 1714 | return evsel; |
| 1715 | } |
| 1716 | |
| 1717 | return NULL; |
| 1718 | } |
| 1719 | |
| 1720 | static void |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1721 | perf_evlist__set_event_name(struct perf_evlist *evlist, |
| 1722 | struct perf_evsel *event) |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 1723 | { |
| 1724 | struct perf_evsel *evsel; |
| 1725 | |
| 1726 | if (!event->name) |
| 1727 | return; |
| 1728 | |
| 1729 | evsel = perf_evlist__find_by_index(evlist, event->idx); |
| 1730 | if (!evsel) |
| 1731 | return; |
| 1732 | |
| 1733 | if (evsel->name) |
| 1734 | return; |
| 1735 | |
| 1736 | evsel->name = strdup(event->name); |
| 1737 | } |
| 1738 | |
| 1739 | static int |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1740 | process_event_desc(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1741 | struct perf_header *header, int fd, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1742 | void *data __maybe_unused) |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 1743 | { |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1744 | struct perf_session *session; |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 1745 | struct perf_evsel *evsel, *events = read_event_desc(header, fd); |
| 1746 | |
| 1747 | if (!events) |
| 1748 | return 0; |
| 1749 | |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1750 | session = container_of(header, struct perf_session, header); |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 1751 | for (evsel = events; evsel->attr.size; evsel++) |
| 1752 | perf_evlist__set_event_name(session->evlist, evsel); |
| 1753 | |
| 1754 | free_event_desc(events); |
| 1755 | |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1759 | static int process_cmdline(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1760 | struct perf_header *ph, int fd, |
| 1761 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1762 | { |
| 1763 | size_t ret; |
| 1764 | char *str; |
| 1765 | u32 nr, i; |
| 1766 | struct strbuf sb; |
| 1767 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1768 | ret = readn(fd, &nr, sizeof(nr)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1769 | if (ret != sizeof(nr)) |
| 1770 | return -1; |
| 1771 | |
| 1772 | if (ph->needs_swap) |
| 1773 | nr = bswap_32(nr); |
| 1774 | |
| 1775 | ph->env.nr_cmdline = nr; |
| 1776 | strbuf_init(&sb, 128); |
| 1777 | |
| 1778 | for (i = 0; i < nr; i++) { |
| 1779 | str = do_read_string(fd, ph); |
| 1780 | if (!str) |
| 1781 | goto error; |
| 1782 | |
| 1783 | /* include a NULL character at the end */ |
| 1784 | strbuf_add(&sb, str, strlen(str) + 1); |
| 1785 | free(str); |
| 1786 | } |
| 1787 | ph->env.cmdline = strbuf_detach(&sb, NULL); |
| 1788 | return 0; |
| 1789 | |
| 1790 | error: |
| 1791 | strbuf_release(&sb); |
| 1792 | return -1; |
| 1793 | } |
| 1794 | |
| 1795 | static int process_cpu_topology(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1796 | struct perf_header *ph, int fd, |
| 1797 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1798 | { |
| 1799 | size_t ret; |
| 1800 | u32 nr, i; |
| 1801 | char *str; |
| 1802 | struct strbuf sb; |
| 1803 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1804 | ret = readn(fd, &nr, sizeof(nr)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1805 | if (ret != sizeof(nr)) |
| 1806 | return -1; |
| 1807 | |
| 1808 | if (ph->needs_swap) |
| 1809 | nr = bswap_32(nr); |
| 1810 | |
| 1811 | ph->env.nr_sibling_cores = nr; |
| 1812 | strbuf_init(&sb, 128); |
| 1813 | |
| 1814 | for (i = 0; i < nr; i++) { |
| 1815 | str = do_read_string(fd, ph); |
| 1816 | if (!str) |
| 1817 | goto error; |
| 1818 | |
| 1819 | /* include a NULL character at the end */ |
| 1820 | strbuf_add(&sb, str, strlen(str) + 1); |
| 1821 | free(str); |
| 1822 | } |
| 1823 | ph->env.sibling_cores = strbuf_detach(&sb, NULL); |
| 1824 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1825 | ret = readn(fd, &nr, sizeof(nr)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1826 | if (ret != sizeof(nr)) |
| 1827 | return -1; |
| 1828 | |
| 1829 | if (ph->needs_swap) |
| 1830 | nr = bswap_32(nr); |
| 1831 | |
| 1832 | ph->env.nr_sibling_threads = nr; |
| 1833 | |
| 1834 | for (i = 0; i < nr; i++) { |
| 1835 | str = do_read_string(fd, ph); |
| 1836 | if (!str) |
| 1837 | goto error; |
| 1838 | |
| 1839 | /* include a NULL character at the end */ |
| 1840 | strbuf_add(&sb, str, strlen(str) + 1); |
| 1841 | free(str); |
| 1842 | } |
| 1843 | ph->env.sibling_threads = strbuf_detach(&sb, NULL); |
| 1844 | return 0; |
| 1845 | |
| 1846 | error: |
| 1847 | strbuf_release(&sb); |
| 1848 | return -1; |
| 1849 | } |
| 1850 | |
| 1851 | static int process_numa_topology(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1852 | struct perf_header *ph, int fd, |
| 1853 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1854 | { |
| 1855 | size_t ret; |
| 1856 | u32 nr, node, i; |
| 1857 | char *str; |
| 1858 | uint64_t mem_total, mem_free; |
| 1859 | struct strbuf sb; |
| 1860 | |
| 1861 | /* nr nodes */ |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1862 | ret = readn(fd, &nr, sizeof(nr)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1863 | if (ret != sizeof(nr)) |
| 1864 | goto error; |
| 1865 | |
| 1866 | if (ph->needs_swap) |
| 1867 | nr = bswap_32(nr); |
| 1868 | |
| 1869 | ph->env.nr_numa_nodes = nr; |
| 1870 | strbuf_init(&sb, 256); |
| 1871 | |
| 1872 | for (i = 0; i < nr; i++) { |
| 1873 | /* node number */ |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1874 | ret = readn(fd, &node, sizeof(node)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1875 | if (ret != sizeof(node)) |
| 1876 | goto error; |
| 1877 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1878 | ret = readn(fd, &mem_total, sizeof(u64)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1879 | if (ret != sizeof(u64)) |
| 1880 | goto error; |
| 1881 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1882 | ret = readn(fd, &mem_free, sizeof(u64)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1883 | if (ret != sizeof(u64)) |
| 1884 | goto error; |
| 1885 | |
| 1886 | if (ph->needs_swap) { |
| 1887 | node = bswap_32(node); |
| 1888 | mem_total = bswap_64(mem_total); |
| 1889 | mem_free = bswap_64(mem_free); |
| 1890 | } |
| 1891 | |
| 1892 | strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":", |
| 1893 | node, mem_total, mem_free); |
| 1894 | |
| 1895 | str = do_read_string(fd, ph); |
| 1896 | if (!str) |
| 1897 | goto error; |
| 1898 | |
| 1899 | /* include a NULL character at the end */ |
| 1900 | strbuf_add(&sb, str, strlen(str) + 1); |
| 1901 | free(str); |
| 1902 | } |
| 1903 | ph->env.numa_nodes = strbuf_detach(&sb, NULL); |
| 1904 | return 0; |
| 1905 | |
| 1906 | error: |
| 1907 | strbuf_release(&sb); |
| 1908 | return -1; |
| 1909 | } |
| 1910 | |
| 1911 | static int process_pmu_mappings(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1912 | struct perf_header *ph, int fd, |
| 1913 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1914 | { |
| 1915 | size_t ret; |
| 1916 | char *name; |
| 1917 | u32 pmu_num; |
| 1918 | u32 type; |
| 1919 | struct strbuf sb; |
| 1920 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1921 | ret = readn(fd, &pmu_num, sizeof(pmu_num)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1922 | if (ret != sizeof(pmu_num)) |
| 1923 | return -1; |
| 1924 | |
| 1925 | if (ph->needs_swap) |
| 1926 | pmu_num = bswap_32(pmu_num); |
| 1927 | |
| 1928 | if (!pmu_num) { |
| 1929 | pr_debug("pmu mappings not available\n"); |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | ph->env.nr_pmu_mappings = pmu_num; |
| 1934 | strbuf_init(&sb, 128); |
| 1935 | |
| 1936 | while (pmu_num) { |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 1937 | if (readn(fd, &type, sizeof(type)) != sizeof(type)) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1938 | goto error; |
| 1939 | if (ph->needs_swap) |
| 1940 | type = bswap_32(type); |
| 1941 | |
| 1942 | name = do_read_string(fd, ph); |
| 1943 | if (!name) |
| 1944 | goto error; |
| 1945 | |
| 1946 | strbuf_addf(&sb, "%u:%s", type, name); |
| 1947 | /* include a NULL character at the end */ |
| 1948 | strbuf_add(&sb, "", 1); |
| 1949 | |
| 1950 | free(name); |
| 1951 | pmu_num--; |
| 1952 | } |
| 1953 | ph->env.pmu_mappings = strbuf_detach(&sb, NULL); |
| 1954 | return 0; |
| 1955 | |
| 1956 | error: |
| 1957 | strbuf_release(&sb); |
| 1958 | return -1; |
| 1959 | } |
| 1960 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1961 | struct feature_ops { |
| 1962 | int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist); |
| 1963 | void (*print)(struct perf_header *h, int fd, FILE *fp); |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1964 | int (*process)(struct perf_file_section *section, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1965 | struct perf_header *h, int fd, void *data); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1966 | const char *name; |
| 1967 | bool full_only; |
| 1968 | }; |
| 1969 | |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1970 | #define FEAT_OPA(n, func) \ |
| 1971 | [n] = { .name = #n, .write = write_##func, .print = print_##func } |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1972 | #define FEAT_OPP(n, func) \ |
| 1973 | [n] = { .name = #n, .write = write_##func, .print = print_##func, \ |
| 1974 | .process = process_##func } |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1975 | #define FEAT_OPF(n, func) \ |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1976 | [n] = { .name = #n, .write = write_##func, .print = print_##func, \ |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1977 | .process = process_##func, .full_only = true } |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1978 | |
| 1979 | /* feature_ops not implemented: */ |
Stephane Eranian | 2eeaaa0 | 2012-05-15 13:28:13 +0200 | [diff] [blame] | 1980 | #define print_tracing_data NULL |
| 1981 | #define print_build_id NULL |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1982 | |
| 1983 | static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = { |
Stephane Eranian | 2eeaaa0 | 2012-05-15 13:28:13 +0200 | [diff] [blame] | 1984 | FEAT_OPP(HEADER_TRACING_DATA, tracing_data), |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1985 | FEAT_OPP(HEADER_BUILD_ID, build_id), |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1986 | FEAT_OPP(HEADER_HOSTNAME, hostname), |
| 1987 | FEAT_OPP(HEADER_OSRELEASE, osrelease), |
| 1988 | FEAT_OPP(HEADER_VERSION, version), |
| 1989 | FEAT_OPP(HEADER_ARCH, arch), |
| 1990 | FEAT_OPP(HEADER_NRCPUS, nrcpus), |
| 1991 | FEAT_OPP(HEADER_CPUDESC, cpudesc), |
Namhyung Kim | 37e9d75 | 2012-09-24 17:15:03 +0900 | [diff] [blame] | 1992 | FEAT_OPP(HEADER_CPUID, cpuid), |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1993 | FEAT_OPP(HEADER_TOTAL_MEM, total_mem), |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 1994 | FEAT_OPP(HEADER_EVENT_DESC, event_desc), |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1995 | FEAT_OPP(HEADER_CMDLINE, cmdline), |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1996 | FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology), |
| 1997 | FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology), |
Stephane Eranian | 330aa67 | 2012-03-08 23:47:46 +0100 | [diff] [blame] | 1998 | FEAT_OPA(HEADER_BRANCH_STACK, branch_stack), |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1999 | FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings), |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2000 | }; |
| 2001 | |
| 2002 | struct header_print_data { |
| 2003 | FILE *fp; |
| 2004 | bool full; /* extended list of headers */ |
| 2005 | }; |
| 2006 | |
| 2007 | static int perf_file_section__fprintf_info(struct perf_file_section *section, |
| 2008 | struct perf_header *ph, |
| 2009 | int feat, int fd, void *data) |
| 2010 | { |
| 2011 | struct header_print_data *hd = data; |
| 2012 | |
| 2013 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { |
| 2014 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " |
| 2015 | "%d, continuing...\n", section->offset, feat); |
| 2016 | return 0; |
| 2017 | } |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2018 | if (feat >= HEADER_LAST_FEATURE) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2019 | pr_warning("unknown feature %d\n", feat); |
Robert Richter | f7a8a13 | 2011-12-07 10:02:51 +0100 | [diff] [blame] | 2020 | return 0; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2021 | } |
| 2022 | if (!feat_ops[feat].print) |
| 2023 | return 0; |
| 2024 | |
| 2025 | if (!feat_ops[feat].full_only || hd->full) |
| 2026 | feat_ops[feat].print(ph, fd, hd->fp); |
| 2027 | else |
| 2028 | fprintf(hd->fp, "# %s info available, use -I to display\n", |
| 2029 | feat_ops[feat].name); |
| 2030 | |
| 2031 | return 0; |
| 2032 | } |
| 2033 | |
| 2034 | int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) |
| 2035 | { |
| 2036 | struct header_print_data hd; |
| 2037 | struct perf_header *header = &session->header; |
| 2038 | int fd = session->fd; |
| 2039 | hd.fp = fp; |
| 2040 | hd.full = full; |
| 2041 | |
| 2042 | perf_header__process_sections(header, fd, &hd, |
| 2043 | perf_file_section__fprintf_info); |
| 2044 | return 0; |
| 2045 | } |
| 2046 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2047 | static int do_write_feat(int fd, struct perf_header *h, int type, |
| 2048 | struct perf_file_section **p, |
| 2049 | struct perf_evlist *evlist) |
| 2050 | { |
| 2051 | int err; |
| 2052 | int ret = 0; |
| 2053 | |
| 2054 | if (perf_header__has_feat(h, type)) { |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2055 | if (!feat_ops[type].write) |
| 2056 | return -1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2057 | |
| 2058 | (*p)->offset = lseek(fd, 0, SEEK_CUR); |
| 2059 | |
| 2060 | err = feat_ops[type].write(fd, h, evlist); |
| 2061 | if (err < 0) { |
| 2062 | pr_debug("failed to write feature %d\n", type); |
| 2063 | |
| 2064 | /* undo anything written */ |
| 2065 | lseek(fd, (*p)->offset, SEEK_SET); |
| 2066 | |
| 2067 | return -1; |
| 2068 | } |
| 2069 | (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset; |
| 2070 | (*p)++; |
| 2071 | } |
| 2072 | return ret; |
| 2073 | } |
| 2074 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2075 | static int perf_header__adds_write(struct perf_header *header, |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 2076 | struct perf_evlist *evlist, int fd) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2077 | { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2078 | int nr_sections; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2079 | struct perf_file_section *feat_sec, *p; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2080 | int sec_size; |
| 2081 | u64 sec_start; |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2082 | int feat; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2083 | int err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2084 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2085 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2086 | if (!nr_sections) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2087 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2088 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2089 | feat_sec = p = calloc(sizeof(*feat_sec), nr_sections); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2090 | if (feat_sec == NULL) |
| 2091 | return -ENOMEM; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2092 | |
| 2093 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 2094 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2095 | sec_start = header->data_offset + header->data_size; |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 2096 | lseek(fd, sec_start + sec_size, SEEK_SET); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2097 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2098 | for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { |
| 2099 | if (do_write_feat(fd, header, feat, &p, evlist)) |
| 2100 | perf_header__clear_feat(header, feat); |
| 2101 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2102 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 2103 | lseek(fd, sec_start, SEEK_SET); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2104 | /* |
| 2105 | * may write more than needed due to dropped feature, but |
| 2106 | * this is okay, reader will skip the mising entries |
| 2107 | */ |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2108 | err = do_write(fd, feat_sec, sec_size); |
| 2109 | if (err < 0) |
| 2110 | pr_debug("failed to write feature section\n"); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2111 | free(feat_sec); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2112 | return err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2113 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2114 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2115 | int perf_header__write_pipe(int fd) |
| 2116 | { |
| 2117 | struct perf_pipe_file_header f_header; |
| 2118 | int err; |
| 2119 | |
| 2120 | f_header = (struct perf_pipe_file_header){ |
| 2121 | .magic = PERF_MAGIC, |
| 2122 | .size = sizeof(f_header), |
| 2123 | }; |
| 2124 | |
| 2125 | err = do_write(fd, &f_header, sizeof(f_header)); |
| 2126 | if (err < 0) { |
| 2127 | pr_debug("failed to write perf pipe header\n"); |
| 2128 | return err; |
| 2129 | } |
| 2130 | |
| 2131 | return 0; |
| 2132 | } |
| 2133 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2134 | int perf_session__write_header(struct perf_session *session, |
| 2135 | struct perf_evlist *evlist, |
| 2136 | int fd, bool at_exit) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2137 | { |
| 2138 | struct perf_file_header f_header; |
| 2139 | struct perf_file_attr f_attr; |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2140 | struct perf_header *header = &session->header; |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2141 | struct perf_evsel *evsel, *pair = NULL; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2142 | int err; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2143 | |
| 2144 | lseek(fd, sizeof(f_header), SEEK_SET); |
| 2145 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2146 | if (session->evlist != evlist) |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 2147 | pair = perf_evlist__first(session->evlist); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2148 | |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2149 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 2150 | evsel->id_offset = lseek(fd, 0, SEEK_CUR); |
| 2151 | err = do_write(fd, evsel->id, evsel->ids * sizeof(u64)); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2152 | if (err < 0) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2153 | out_err_write: |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2154 | pr_debug("failed to write perf header\n"); |
| 2155 | return err; |
| 2156 | } |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2157 | if (session->evlist != evlist) { |
| 2158 | err = do_write(fd, pair->id, pair->ids * sizeof(u64)); |
| 2159 | if (err < 0) |
| 2160 | goto out_err_write; |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2161 | evsel->ids += pair->ids; |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 2162 | pair = perf_evsel__next(pair); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2163 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2164 | } |
| 2165 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2166 | header->attr_offset = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2167 | |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2168 | list_for_each_entry(evsel, &evlist->entries, node) { |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2169 | f_attr = (struct perf_file_attr){ |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2170 | .attr = evsel->attr, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2171 | .ids = { |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2172 | .offset = evsel->id_offset, |
| 2173 | .size = evsel->ids * sizeof(u64), |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2174 | } |
| 2175 | }; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2176 | err = do_write(fd, &f_attr, sizeof(f_attr)); |
| 2177 | if (err < 0) { |
| 2178 | pr_debug("failed to write perf header attribute\n"); |
| 2179 | return err; |
| 2180 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2181 | } |
| 2182 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2183 | header->event_offset = lseek(fd, 0, SEEK_CUR); |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 2184 | header->event_size = trace_event_count * sizeof(struct perf_trace_event_type); |
| 2185 | if (trace_events) { |
| 2186 | err = do_write(fd, trace_events, header->event_size); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2187 | if (err < 0) { |
| 2188 | pr_debug("failed to write perf header events\n"); |
| 2189 | return err; |
| 2190 | } |
| 2191 | } |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2192 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2193 | header->data_offset = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2194 | |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2195 | if (at_exit) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2196 | err = perf_header__adds_write(header, evlist, fd); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2197 | if (err < 0) |
| 2198 | return err; |
| 2199 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2200 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2201 | f_header = (struct perf_file_header){ |
| 2202 | .magic = PERF_MAGIC, |
| 2203 | .size = sizeof(f_header), |
| 2204 | .attr_size = sizeof(f_attr), |
| 2205 | .attrs = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2206 | .offset = header->attr_offset, |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2207 | .size = evlist->nr_entries * sizeof(f_attr), |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2208 | }, |
| 2209 | .data = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2210 | .offset = header->data_offset, |
| 2211 | .size = header->data_size, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2212 | }, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2213 | .event_types = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2214 | .offset = header->event_offset, |
| 2215 | .size = header->event_size, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2216 | }, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2217 | }; |
| 2218 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2219 | memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2220 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2221 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2222 | err = do_write(fd, &f_header, sizeof(f_header)); |
| 2223 | if (err < 0) { |
| 2224 | pr_debug("failed to write perf header\n"); |
| 2225 | return err; |
| 2226 | } |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2227 | lseek(fd, header->data_offset + header->data_size, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2228 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2229 | header->frozen = 1; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2230 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2231 | } |
| 2232 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2233 | static int perf_header__getbuffer64(struct perf_header *header, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2234 | int fd, void *buf, size_t size) |
| 2235 | { |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 2236 | if (readn(fd, buf, size) <= 0) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2237 | return -1; |
| 2238 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2239 | if (header->needs_swap) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2240 | mem_bswap_64(buf, size); |
| 2241 | |
| 2242 | return 0; |
| 2243 | } |
| 2244 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2245 | int perf_header__process_sections(struct perf_header *header, int fd, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2246 | void *data, |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2247 | int (*process)(struct perf_file_section *section, |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2248 | struct perf_header *ph, |
| 2249 | int feat, int fd, void *data)) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2250 | { |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2251 | struct perf_file_section *feat_sec, *sec; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2252 | int nr_sections; |
| 2253 | int sec_size; |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2254 | int feat; |
| 2255 | int err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2256 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2257 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2258 | if (!nr_sections) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2259 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2260 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2261 | feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2262 | if (!feat_sec) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2263 | return -1; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2264 | |
| 2265 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 2266 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2267 | lseek(fd, header->data_offset + header->data_size, SEEK_SET); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2268 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2269 | err = perf_header__getbuffer64(header, fd, feat_sec, sec_size); |
| 2270 | if (err < 0) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2271 | goto out_free; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2272 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2273 | for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) { |
| 2274 | err = process(sec++, header, feat, fd, data); |
| 2275 | if (err < 0) |
| 2276 | goto out_free; |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 2277 | } |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2278 | err = 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2279 | out_free: |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2280 | free(feat_sec); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2281 | return err; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2282 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2283 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2284 | static const int attr_file_abi_sizes[] = { |
| 2285 | [0] = PERF_ATTR_SIZE_VER0, |
| 2286 | [1] = PERF_ATTR_SIZE_VER1, |
Jiri Olsa | 239cc47 | 2012-08-07 15:20:42 +0200 | [diff] [blame] | 2287 | [2] = PERF_ATTR_SIZE_VER2, |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 2288 | [3] = PERF_ATTR_SIZE_VER3, |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2289 | 0, |
| 2290 | }; |
| 2291 | |
| 2292 | /* |
| 2293 | * In the legacy file format, the magic number is not used to encode endianness. |
| 2294 | * hdr_sz was used to encode endianness. But given that hdr_sz can vary based |
| 2295 | * on ABI revisions, we need to try all combinations for all endianness to |
| 2296 | * detect the endianness. |
| 2297 | */ |
| 2298 | static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph) |
| 2299 | { |
| 2300 | uint64_t ref_size, attr_size; |
| 2301 | int i; |
| 2302 | |
| 2303 | for (i = 0 ; attr_file_abi_sizes[i]; i++) { |
| 2304 | ref_size = attr_file_abi_sizes[i] |
| 2305 | + sizeof(struct perf_file_section); |
| 2306 | if (hdr_sz != ref_size) { |
| 2307 | attr_size = bswap_64(hdr_sz); |
| 2308 | if (attr_size != ref_size) |
| 2309 | continue; |
| 2310 | |
| 2311 | ph->needs_swap = true; |
| 2312 | } |
| 2313 | pr_debug("ABI%d perf.data file detected, need_swap=%d\n", |
| 2314 | i, |
| 2315 | ph->needs_swap); |
| 2316 | return 0; |
| 2317 | } |
| 2318 | /* could not determine endianness */ |
| 2319 | return -1; |
| 2320 | } |
| 2321 | |
| 2322 | #define PERF_PIPE_HDR_VER0 16 |
| 2323 | |
| 2324 | static const size_t attr_pipe_abi_sizes[] = { |
| 2325 | [0] = PERF_PIPE_HDR_VER0, |
| 2326 | 0, |
| 2327 | }; |
| 2328 | |
| 2329 | /* |
| 2330 | * In the legacy pipe format, there is an implicit assumption that endiannesss |
| 2331 | * between host recording the samples, and host parsing the samples is the |
| 2332 | * same. This is not always the case given that the pipe output may always be |
| 2333 | * redirected into a file and analyzed on a different machine with possibly a |
| 2334 | * different endianness and perf_event ABI revsions in the perf tool itself. |
| 2335 | */ |
| 2336 | static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph) |
| 2337 | { |
| 2338 | u64 attr_size; |
| 2339 | int i; |
| 2340 | |
| 2341 | for (i = 0 ; attr_pipe_abi_sizes[i]; i++) { |
| 2342 | if (hdr_sz != attr_pipe_abi_sizes[i]) { |
| 2343 | attr_size = bswap_64(hdr_sz); |
| 2344 | if (attr_size != hdr_sz) |
| 2345 | continue; |
| 2346 | |
| 2347 | ph->needs_swap = true; |
| 2348 | } |
| 2349 | pr_debug("Pipe ABI%d perf.data file detected\n", i); |
| 2350 | return 0; |
| 2351 | } |
| 2352 | return -1; |
| 2353 | } |
| 2354 | |
Feng Tang | e84ba4e | 2012-10-30 11:56:07 +0800 | [diff] [blame] | 2355 | bool is_perf_magic(u64 magic) |
| 2356 | { |
| 2357 | if (!memcmp(&magic, __perf_magic1, sizeof(magic)) |
| 2358 | || magic == __perf_magic2 |
| 2359 | || magic == __perf_magic2_sw) |
| 2360 | return true; |
| 2361 | |
| 2362 | return false; |
| 2363 | } |
| 2364 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2365 | static int check_magic_endian(u64 magic, uint64_t hdr_sz, |
| 2366 | bool is_pipe, struct perf_header *ph) |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2367 | { |
| 2368 | int ret; |
| 2369 | |
| 2370 | /* check for legacy format */ |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2371 | ret = memcmp(&magic, __perf_magic1, sizeof(magic)); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2372 | if (ret == 0) { |
| 2373 | pr_debug("legacy perf.data format\n"); |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2374 | if (is_pipe) |
| 2375 | return try_all_pipe_abis(hdr_sz, ph); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2376 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2377 | return try_all_file_abis(hdr_sz, ph); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2378 | } |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2379 | /* |
| 2380 | * the new magic number serves two purposes: |
| 2381 | * - unique number to identify actual perf.data files |
| 2382 | * - encode endianness of file |
| 2383 | */ |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2384 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2385 | /* check magic number with one endianness */ |
| 2386 | if (magic == __perf_magic2) |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2387 | return 0; |
| 2388 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2389 | /* check magic number with opposite endianness */ |
| 2390 | if (magic != __perf_magic2_sw) |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2391 | return -1; |
| 2392 | |
| 2393 | ph->needs_swap = true; |
| 2394 | |
| 2395 | return 0; |
| 2396 | } |
| 2397 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2398 | int perf_file_header__read(struct perf_file_header *header, |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2399 | struct perf_header *ph, int fd) |
| 2400 | { |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2401 | int ret; |
| 2402 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2403 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2404 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2405 | ret = readn(fd, header, sizeof(*header)); |
| 2406 | if (ret <= 0) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2407 | return -1; |
| 2408 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2409 | if (check_magic_endian(header->magic, |
| 2410 | header->attr_size, false, ph) < 0) { |
| 2411 | pr_debug("magic/endian check failed\n"); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2412 | return -1; |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2413 | } |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2414 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2415 | if (ph->needs_swap) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2416 | mem_bswap_64(header, offsetof(struct perf_file_header, |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2417 | adds_features)); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2418 | } |
| 2419 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2420 | if (header->size != sizeof(*header)) { |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2421 | /* Support the previous format */ |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2422 | if (header->size == offsetof(typeof(*header), adds_features)) |
| 2423 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2424 | else |
| 2425 | return -1; |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 2426 | } else if (ph->needs_swap) { |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 2427 | /* |
| 2428 | * feature bitmap is declared as an array of unsigned longs -- |
| 2429 | * not good since its size can differ between the host that |
| 2430 | * generated the data file and the host analyzing the file. |
| 2431 | * |
| 2432 | * We need to handle endianness, but we don't know the size of |
| 2433 | * the unsigned long where the file was generated. Take a best |
| 2434 | * guess at determining it: try 64-bit swap first (ie., file |
| 2435 | * created on a 64-bit host), and check if the hostname feature |
| 2436 | * bit is set (this feature bit is forced on as of fbe96f2). |
| 2437 | * If the bit is not, undo the 64-bit swap and try a 32-bit |
| 2438 | * swap. If the hostname bit is still not set (e.g., older data |
| 2439 | * file), punt and fallback to the original behavior -- |
| 2440 | * clearing all feature bits and setting buildid. |
| 2441 | */ |
David Ahern | 80c0120 | 2012-06-08 11:47:51 -0300 | [diff] [blame] | 2442 | mem_bswap_64(&header->adds_features, |
| 2443 | BITS_TO_U64(HEADER_FEAT_BITS)); |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 2444 | |
| 2445 | if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { |
David Ahern | 80c0120 | 2012-06-08 11:47:51 -0300 | [diff] [blame] | 2446 | /* unswap as u64 */ |
| 2447 | mem_bswap_64(&header->adds_features, |
| 2448 | BITS_TO_U64(HEADER_FEAT_BITS)); |
| 2449 | |
| 2450 | /* unswap as u32 */ |
| 2451 | mem_bswap_32(&header->adds_features, |
| 2452 | BITS_TO_U32(HEADER_FEAT_BITS)); |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { |
| 2456 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); |
| 2457 | set_bit(HEADER_BUILD_ID, header->adds_features); |
| 2458 | } |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2459 | } |
| 2460 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2461 | memcpy(&ph->adds_features, &header->adds_features, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2462 | sizeof(ph->adds_features)); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2463 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2464 | ph->event_offset = header->event_types.offset; |
| 2465 | ph->event_size = header->event_types.size; |
| 2466 | ph->data_offset = header->data.offset; |
| 2467 | ph->data_size = header->data.size; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2468 | return 0; |
| 2469 | } |
| 2470 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2471 | static int perf_file_section__process(struct perf_file_section *section, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2472 | struct perf_header *ph, |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 2473 | int feat, int fd, void *data) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2474 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2475 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 2476 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2477 | "%d, continuing...\n", section->offset, feat); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2478 | return 0; |
| 2479 | } |
| 2480 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2481 | if (feat >= HEADER_LAST_FEATURE) { |
| 2482 | pr_debug("unknown feature %d, continuing...\n", feat); |
| 2483 | return 0; |
| 2484 | } |
| 2485 | |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 2486 | if (!feat_ops[feat].process) |
| 2487 | return 0; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2488 | |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 2489 | return feat_ops[feat].process(section, ph, fd, data); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2490 | } |
| 2491 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2492 | static int perf_file_header__read_pipe(struct perf_pipe_file_header *header, |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2493 | struct perf_header *ph, int fd, |
| 2494 | bool repipe) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2495 | { |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2496 | int ret; |
| 2497 | |
| 2498 | ret = readn(fd, header, sizeof(*header)); |
| 2499 | if (ret <= 0) |
| 2500 | return -1; |
| 2501 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2502 | if (check_magic_endian(header->magic, header->size, true, ph) < 0) { |
| 2503 | pr_debug("endian/magic failed\n"); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2504 | return -1; |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | if (ph->needs_swap) |
| 2508 | header->size = bswap_64(header->size); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2509 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2510 | if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2511 | return -1; |
| 2512 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2513 | return 0; |
| 2514 | } |
| 2515 | |
| 2516 | static int perf_header__read_pipe(struct perf_session *session, int fd) |
| 2517 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2518 | struct perf_header *header = &session->header; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2519 | struct perf_pipe_file_header f_header; |
| 2520 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2521 | if (perf_file_header__read_pipe(&f_header, header, fd, |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2522 | session->repipe) < 0) { |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2523 | pr_debug("incompatible file format\n"); |
| 2524 | return -EINVAL; |
| 2525 | } |
| 2526 | |
| 2527 | session->fd = fd; |
| 2528 | |
| 2529 | return 0; |
| 2530 | } |
| 2531 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2532 | static int read_attr(int fd, struct perf_header *ph, |
| 2533 | struct perf_file_attr *f_attr) |
| 2534 | { |
| 2535 | struct perf_event_attr *attr = &f_attr->attr; |
| 2536 | size_t sz, left; |
| 2537 | size_t our_sz = sizeof(f_attr->attr); |
| 2538 | int ret; |
| 2539 | |
| 2540 | memset(f_attr, 0, sizeof(*f_attr)); |
| 2541 | |
| 2542 | /* read minimal guaranteed structure */ |
| 2543 | ret = readn(fd, attr, PERF_ATTR_SIZE_VER0); |
| 2544 | if (ret <= 0) { |
| 2545 | pr_debug("cannot read %d bytes of header attr\n", |
| 2546 | PERF_ATTR_SIZE_VER0); |
| 2547 | return -1; |
| 2548 | } |
| 2549 | |
| 2550 | /* on file perf_event_attr size */ |
| 2551 | sz = attr->size; |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2552 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2553 | if (ph->needs_swap) |
| 2554 | sz = bswap_32(sz); |
| 2555 | |
| 2556 | if (sz == 0) { |
| 2557 | /* assume ABI0 */ |
| 2558 | sz = PERF_ATTR_SIZE_VER0; |
| 2559 | } else if (sz > our_sz) { |
| 2560 | pr_debug("file uses a more recent and unsupported ABI" |
| 2561 | " (%zu bytes extra)\n", sz - our_sz); |
| 2562 | return -1; |
| 2563 | } |
| 2564 | /* what we have not yet read and that we know about */ |
| 2565 | left = sz - PERF_ATTR_SIZE_VER0; |
| 2566 | if (left) { |
| 2567 | void *ptr = attr; |
| 2568 | ptr += PERF_ATTR_SIZE_VER0; |
| 2569 | |
| 2570 | ret = readn(fd, ptr, left); |
| 2571 | } |
| 2572 | /* read perf_file_section, ids are read in caller */ |
| 2573 | ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); |
| 2574 | |
| 2575 | return ret <= 0 ? -1 : 0; |
| 2576 | } |
| 2577 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2578 | static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel, |
| 2579 | struct pevent *pevent) |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2580 | { |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2581 | struct event_format *event; |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2582 | char bf[128]; |
| 2583 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2584 | /* already prepared */ |
| 2585 | if (evsel->tp_format) |
| 2586 | return 0; |
| 2587 | |
| 2588 | event = pevent_find_event(pevent, evsel->attr.config); |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2589 | if (event == NULL) |
| 2590 | return -1; |
| 2591 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2592 | if (!evsel->name) { |
| 2593 | snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name); |
| 2594 | evsel->name = strdup(bf); |
| 2595 | if (evsel->name == NULL) |
| 2596 | return -1; |
| 2597 | } |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2598 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 2599 | evsel->tp_format = event; |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2600 | return 0; |
| 2601 | } |
| 2602 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2603 | static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist, |
| 2604 | struct pevent *pevent) |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2605 | { |
| 2606 | struct perf_evsel *pos; |
| 2607 | |
| 2608 | list_for_each_entry(pos, &evlist->entries, node) { |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2609 | if (pos->attr.type == PERF_TYPE_TRACEPOINT && |
| 2610 | perf_evsel__prepare_tracepoint_event(pos, pevent)) |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2611 | return -1; |
| 2612 | } |
| 2613 | |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2617 | int perf_session__read_header(struct perf_session *session, int fd) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2618 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2619 | struct perf_header *header = &session->header; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2620 | struct perf_file_header f_header; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2621 | struct perf_file_attr f_attr; |
| 2622 | u64 f_id; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2623 | int nr_attrs, nr_ids, i, j; |
| 2624 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2625 | session->evlist = perf_evlist__new(NULL, NULL); |
| 2626 | if (session->evlist == NULL) |
| 2627 | return -ENOMEM; |
| 2628 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2629 | if (session->fd_pipe) |
| 2630 | return perf_header__read_pipe(session, fd); |
| 2631 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2632 | if (perf_file_header__read(&f_header, header, fd) < 0) |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2633 | return -EINVAL; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2634 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2635 | nr_attrs = f_header.attrs.size / f_header.attr_size; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2636 | lseek(fd, f_header.attrs.offset, SEEK_SET); |
| 2637 | |
| 2638 | for (i = 0; i < nr_attrs; i++) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2639 | struct perf_evsel *evsel; |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 2640 | off_t tmp; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2641 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2642 | if (read_attr(fd, header, &f_attr) < 0) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2643 | goto out_errno; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2644 | |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 2645 | if (header->needs_swap) |
| 2646 | perf_event__attr_swap(&f_attr.attr); |
| 2647 | |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 2648 | tmp = lseek(fd, 0, SEEK_CUR); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2649 | evsel = perf_evsel__new(&f_attr.attr, i); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2650 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2651 | if (evsel == NULL) |
| 2652 | goto out_delete_evlist; |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 2653 | |
| 2654 | evsel->needs_swap = header->needs_swap; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2655 | /* |
| 2656 | * Do it before so that if perf_evsel__alloc_id fails, this |
| 2657 | * entry gets purged too at perf_evlist__delete(). |
| 2658 | */ |
| 2659 | perf_evlist__add(session->evlist, evsel); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2660 | |
| 2661 | nr_ids = f_attr.ids.size / sizeof(u64); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2662 | /* |
| 2663 | * We don't have the cpu and thread maps on the header, so |
| 2664 | * for allocating the perf_sample_id table we fake 1 cpu and |
| 2665 | * hattr->ids threads. |
| 2666 | */ |
| 2667 | if (perf_evsel__alloc_id(evsel, 1, nr_ids)) |
| 2668 | goto out_delete_evlist; |
| 2669 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2670 | lseek(fd, f_attr.ids.offset, SEEK_SET); |
| 2671 | |
| 2672 | for (j = 0; j < nr_ids; j++) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2673 | if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id))) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2674 | goto out_errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2675 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2676 | perf_evlist__id_add(session->evlist, evsel, 0, j, f_id); |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2677 | } |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 2678 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2679 | lseek(fd, tmp, SEEK_SET); |
| 2680 | } |
| 2681 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 2682 | symbol_conf.nr_events = nr_attrs; |
| 2683 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2684 | if (f_header.event_types.size) { |
| 2685 | lseek(fd, f_header.event_types.offset, SEEK_SET); |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 2686 | trace_events = malloc(f_header.event_types.size); |
| 2687 | if (trace_events == NULL) |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2688 | return -ENOMEM; |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 2689 | if (perf_header__getbuffer64(header, fd, trace_events, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2690 | f_header.event_types.size)) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2691 | goto out_errno; |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 2692 | trace_event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2693 | } |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 2694 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 2695 | perf_header__process_sections(header, fd, &session->pevent, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2696 | perf_file_section__process); |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 2697 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2698 | lseek(fd, header->data_offset, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2699 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2700 | if (perf_evlist__prepare_tracepoint_events(session->evlist, |
| 2701 | session->pevent)) |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2702 | goto out_delete_evlist; |
| 2703 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2704 | header->frozen = 1; |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2705 | return 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2706 | out_errno: |
| 2707 | return -errno; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2708 | |
| 2709 | out_delete_evlist: |
| 2710 | perf_evlist__delete(session->evlist); |
| 2711 | session->evlist = NULL; |
| 2712 | return -ENOMEM; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2713 | } |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 2714 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2715 | int perf_event__synthesize_attr(struct perf_tool *tool, |
Robert Richter | f4d8343 | 2012-08-16 21:10:17 +0200 | [diff] [blame] | 2716 | struct perf_event_attr *attr, u32 ids, u64 *id, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2717 | perf_event__handler_t process) |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 2718 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2719 | union perf_event *ev; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2720 | size_t size; |
| 2721 | int err; |
| 2722 | |
| 2723 | size = sizeof(struct perf_event_attr); |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 2724 | size = PERF_ALIGN(size, sizeof(u64)); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2725 | size += sizeof(struct perf_event_header); |
| 2726 | size += ids * sizeof(u64); |
| 2727 | |
| 2728 | ev = malloc(size); |
| 2729 | |
Chris Samuel | ce47dc5 | 2010-11-13 13:35:06 +1100 | [diff] [blame] | 2730 | if (ev == NULL) |
| 2731 | return -ENOMEM; |
| 2732 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2733 | ev->attr.attr = *attr; |
| 2734 | memcpy(ev->attr.id, id, ids * sizeof(u64)); |
| 2735 | |
| 2736 | ev->attr.header.type = PERF_RECORD_HEADER_ATTR; |
Robert Richter | f4d8343 | 2012-08-16 21:10:17 +0200 | [diff] [blame] | 2737 | ev->attr.header.size = (u16)size; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2738 | |
Robert Richter | f4d8343 | 2012-08-16 21:10:17 +0200 | [diff] [blame] | 2739 | if (ev->attr.header.size == size) |
| 2740 | err = process(tool, ev, NULL, NULL); |
| 2741 | else |
| 2742 | err = -E2BIG; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2743 | |
| 2744 | free(ev); |
| 2745 | |
| 2746 | return err; |
| 2747 | } |
| 2748 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2749 | int perf_event__synthesize_attrs(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2750 | struct perf_session *session, |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2751 | perf_event__handler_t process) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2752 | { |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2753 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2754 | int err = 0; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2755 | |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2756 | list_for_each_entry(evsel, &session->evlist->entries, node) { |
| 2757 | err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids, |
| 2758 | evsel->id, process); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2759 | if (err) { |
| 2760 | pr_debug("failed to create perf header attribute\n"); |
| 2761 | return err; |
| 2762 | } |
| 2763 | } |
| 2764 | |
| 2765 | return err; |
| 2766 | } |
| 2767 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2768 | int perf_event__process_attr(union perf_event *event, |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2769 | struct perf_evlist **pevlist) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2770 | { |
Robert Richter | f4d8343 | 2012-08-16 21:10:17 +0200 | [diff] [blame] | 2771 | u32 i, ids, n_ids; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2772 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2773 | struct perf_evlist *evlist = *pevlist; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2774 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2775 | if (evlist == NULL) { |
| 2776 | *pevlist = evlist = perf_evlist__new(NULL, NULL); |
| 2777 | if (evlist == NULL) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2778 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2779 | } |
| 2780 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2781 | evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2782 | if (evsel == NULL) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2783 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2784 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2785 | perf_evlist__add(evlist, evsel); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2786 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2787 | ids = event->header.size; |
| 2788 | ids -= (void *)&event->attr.id - (void *)event; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2789 | n_ids = ids / sizeof(u64); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2790 | /* |
| 2791 | * We don't have the cpu and thread maps on the header, so |
| 2792 | * for allocating the perf_sample_id table we fake 1 cpu and |
| 2793 | * hattr->ids threads. |
| 2794 | */ |
| 2795 | if (perf_evsel__alloc_id(evsel, 1, n_ids)) |
| 2796 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2797 | |
| 2798 | for (i = 0; i < n_ids; i++) { |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2799 | perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2800 | } |
| 2801 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2802 | return 0; |
| 2803 | } |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2804 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2805 | int perf_event__synthesize_event_type(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2806 | u64 event_id, char *name, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2807 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2808 | struct machine *machine) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2809 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2810 | union perf_event ev; |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2811 | size_t size = 0; |
| 2812 | int err = 0; |
| 2813 | |
| 2814 | memset(&ev, 0, sizeof(ev)); |
| 2815 | |
| 2816 | ev.event_type.event_type.event_id = event_id; |
| 2817 | memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME); |
| 2818 | strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1); |
| 2819 | |
| 2820 | ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE; |
Stephane Eranian | f8f4b28 | 2012-01-20 10:49:12 +0100 | [diff] [blame] | 2821 | size = strlen(ev.event_type.event_type.name); |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 2822 | size = PERF_ALIGN(size, sizeof(u64)); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2823 | ev.event_type.header.size = sizeof(ev.event_type) - |
| 2824 | (sizeof(ev.event_type.event_type.name) - size); |
| 2825 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2826 | err = process(tool, &ev, NULL, machine); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2827 | |
| 2828 | return err; |
| 2829 | } |
| 2830 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2831 | int perf_event__synthesize_event_types(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2832 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2833 | struct machine *machine) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2834 | { |
| 2835 | struct perf_trace_event_type *type; |
| 2836 | int i, err = 0; |
| 2837 | |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 2838 | for (i = 0; i < trace_event_count; i++) { |
| 2839 | type = &trace_events[i]; |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2840 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2841 | err = perf_event__synthesize_event_type(tool, type->event_id, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2842 | type->name, process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2843 | machine); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2844 | if (err) { |
| 2845 | pr_debug("failed to create perf header event type\n"); |
| 2846 | return err; |
| 2847 | } |
| 2848 | } |
| 2849 | |
| 2850 | return err; |
| 2851 | } |
| 2852 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 2853 | int perf_event__process_event_type(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2854 | union perf_event *event) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2855 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2856 | if (perf_header__push_event(event->event_type.event_type.event_id, |
| 2857 | event->event_type.event_type.name) < 0) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2858 | return -ENOMEM; |
| 2859 | |
| 2860 | return 0; |
| 2861 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2862 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2863 | int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2864 | struct perf_evlist *evlist, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2865 | perf_event__handler_t process) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2866 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2867 | union perf_event ev; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2868 | struct tracing_data *tdata; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2869 | ssize_t size = 0, aligned_size = 0, padding; |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 2870 | int err __maybe_unused = 0; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2871 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2872 | /* |
| 2873 | * We are going to store the size of the data followed |
| 2874 | * by the data contents. Since the fd descriptor is a pipe, |
| 2875 | * we cannot seek back to store the size of the data once |
| 2876 | * we know it. Instead we: |
| 2877 | * |
| 2878 | * - write the tracing data to the temp file |
| 2879 | * - get/write the data size to pipe |
| 2880 | * - write the tracing data from the temp file |
| 2881 | * to the pipe |
| 2882 | */ |
| 2883 | tdata = tracing_data_get(&evlist->entries, fd, true); |
| 2884 | if (!tdata) |
| 2885 | return -1; |
| 2886 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2887 | memset(&ev, 0, sizeof(ev)); |
| 2888 | |
| 2889 | ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2890 | size = tdata->size; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 2891 | aligned_size = PERF_ALIGN(size, sizeof(u64)); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2892 | padding = aligned_size - size; |
| 2893 | ev.tracing_data.header.size = sizeof(ev.tracing_data); |
| 2894 | ev.tracing_data.size = aligned_size; |
| 2895 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2896 | process(tool, &ev, NULL, NULL); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2897 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2898 | /* |
| 2899 | * The put function will copy all the tracing data |
| 2900 | * stored in temp file to the pipe. |
| 2901 | */ |
| 2902 | tracing_data_put(tdata); |
| 2903 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2904 | write_padded(fd, NULL, 0, padding); |
| 2905 | |
| 2906 | return aligned_size; |
| 2907 | } |
| 2908 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2909 | int perf_event__process_tracing_data(union perf_event *event, |
| 2910 | struct perf_session *session) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2911 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2912 | ssize_t size_read, padding, size = event->tracing_data.size; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2913 | off_t offset = lseek(session->fd, 0, SEEK_CUR); |
| 2914 | char buf[BUFSIZ]; |
| 2915 | |
| 2916 | /* setup for reading amidst mmap */ |
| 2917 | lseek(session->fd, offset + sizeof(struct tracing_data_event), |
| 2918 | SEEK_SET); |
| 2919 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 2920 | size_read = trace_report(session->fd, &session->pevent, |
| 2921 | session->repipe); |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 2922 | padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2923 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame^] | 2924 | if (readn(session->fd, buf, padding) < 0) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2925 | die("reading input file"); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2926 | if (session->repipe) { |
| 2927 | int retw = write(STDOUT_FILENO, buf, padding); |
| 2928 | if (retw <= 0 || retw != padding) |
| 2929 | die("repiping tracing data padding"); |
| 2930 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2931 | |
| 2932 | if (size_read + padding != size) |
| 2933 | die("tracing data size mismatch"); |
| 2934 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2935 | perf_evlist__prepare_tracepoint_events(session->evlist, |
| 2936 | session->pevent); |
Arnaldo Carvalho de Melo | 8b6ee4c | 2012-08-07 23:36:16 -0300 | [diff] [blame] | 2937 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2938 | return size_read + padding; |
| 2939 | } |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2940 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2941 | int perf_event__synthesize_build_id(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2942 | struct dso *pos, u16 misc, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2943 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2944 | struct machine *machine) |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2945 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2946 | union perf_event ev; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2947 | size_t len; |
| 2948 | int err = 0; |
| 2949 | |
| 2950 | if (!pos->hit) |
| 2951 | return err; |
| 2952 | |
| 2953 | memset(&ev, 0, sizeof(ev)); |
| 2954 | |
| 2955 | len = pos->long_name_len + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 2956 | len = PERF_ALIGN(len, NAME_ALIGN); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2957 | memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id)); |
| 2958 | ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID; |
| 2959 | ev.build_id.header.misc = misc; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 2960 | ev.build_id.pid = machine->pid; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2961 | ev.build_id.header.size = sizeof(ev.build_id) + len; |
| 2962 | memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len); |
| 2963 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2964 | err = process(tool, &ev, NULL, machine); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2965 | |
| 2966 | return err; |
| 2967 | } |
| 2968 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 2969 | int perf_event__process_build_id(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2970 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2971 | struct perf_session *session) |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2972 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2973 | __event_process_build_id(&event->build_id, |
| 2974 | event->build_id.filename, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2975 | session); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2976 | return 0; |
| 2977 | } |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 2978 | |
| 2979 | void disable_buildid_cache(void) |
| 2980 | { |
| 2981 | no_buildid_cache = true; |
| 2982 | } |