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