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