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