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