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