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