Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 2 | #include <errno.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 3 | #include <inttypes.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> |
Arnaldo Carvalho de Melo | fc6a172 | 2019-06-25 21:33:14 -0300 | [diff] [blame] | 15 | #include <linux/string.h> |
David Carrillo-Cisneros | a4d8c98 | 2017-07-17 21:25:46 -0700 | [diff] [blame] | 16 | #include <linux/stringify.h> |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 17 | #include <linux/zalloc.h> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 18 | #include <sys/stat.h> |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 19 | #include <sys/utsname.h> |
Jin Yao | 6011518 | 2017-12-08 21:13:41 +0800 | [diff] [blame] | 20 | #include <linux/time64.h> |
Jiri Olsa | e2091ce | 2018-03-07 16:50:08 +0100 | [diff] [blame] | 21 | #include <dirent.h> |
Song Liu | 606f972 | 2019-03-11 22:30:43 -0700 | [diff] [blame] | 22 | #include <bpf/libbpf.h> |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame^] | 23 | #include <perf/cpumap.h> |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 24 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 25 | #include "evlist.h" |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 26 | #include "evsel.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 27 | #include "header.h" |
Arnaldo Carvalho de Melo | 98521b3 | 2017-04-25 15:45:35 -0300 | [diff] [blame] | 28 | #include "memswap.h" |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 29 | #include "../perf.h" |
| 30 | #include "trace-event.h" |
Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 31 | #include "session.h" |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 32 | #include "symbol.h" |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 33 | #include "debug.h" |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 34 | #include "cpumap.h" |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 35 | #include "pmu.h" |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 36 | #include "vdso.h" |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 37 | #include "strbuf.h" |
Jiri Olsa | ebb296c | 2012-10-27 23:18:28 +0200 | [diff] [blame] | 38 | #include "build-id.h" |
Jiri Olsa | cc9784bd | 2013-10-15 16:27:34 +0200 | [diff] [blame] | 39 | #include "data.h" |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 40 | #include <api/fs/fs.h> |
| 41 | #include "asm/bug.h" |
David Carrillo-Cisneros | e9def1b | 2017-07-17 21:25:48 -0700 | [diff] [blame] | 42 | #include "tool.h" |
Jin Yao | 6011518 | 2017-12-08 21:13:41 +0800 | [diff] [blame] | 43 | #include "time-utils.h" |
Jiri Olsa | e2091ce | 2018-03-07 16:50:08 +0100 | [diff] [blame] | 44 | #include "units.h" |
Jiri Olsa | 5135d5e | 2019-02-19 10:58:13 +0100 | [diff] [blame] | 45 | #include "cputopo.h" |
Song Liu | 606f972 | 2019-03-11 22:30:43 -0700 | [diff] [blame] | 46 | #include "bpf-event.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 47 | |
Arnaldo Carvalho de Melo | 3052ba5 | 2019-06-25 17:27:31 -0300 | [diff] [blame] | 48 | #include <linux/ctype.h> |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 49 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 50 | /* |
| 51 | * magic2 = "PERFILE2" |
| 52 | * must be a numerical value to let the endianness |
| 53 | * determine the memory layout. That way we are able |
| 54 | * to detect endianness when reading the perf.data file |
| 55 | * back. |
| 56 | * |
| 57 | * we check for legacy (PERFFILE) format. |
| 58 | */ |
| 59 | static const char *__perf_magic1 = "PERFFILE"; |
| 60 | static const u64 __perf_magic2 = 0x32454c4946524550ULL; |
| 61 | static const u64 __perf_magic2_sw = 0x50455246494c4532ULL; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 62 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 63 | #define PERF_MAGIC __perf_magic2 |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 64 | |
Soramichi AKIYAMA | d25ed5d | 2017-01-17 00:22:37 +0900 | [diff] [blame] | 65 | const char perf_version_string[] = PERF_VERSION; |
| 66 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 67 | struct perf_file_attr { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 68 | struct perf_event_attr attr; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 69 | struct perf_file_section ids; |
| 70 | }; |
| 71 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 72 | struct feat_fd { |
| 73 | struct perf_header *ph; |
| 74 | int fd; |
David Carrillo-Cisneros | 0b3d341 | 2017-07-17 21:25:45 -0700 | [diff] [blame] | 75 | void *buf; /* Either buf != NULL or fd >= 0 */ |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 76 | ssize_t offset; |
| 77 | size_t size; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 78 | struct evsel *events; |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 81 | 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] | 82 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 83 | set_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 84 | } |
| 85 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 86 | 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] | 87 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 88 | clear_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 89 | } |
| 90 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 91 | 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] | 92 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 93 | return test_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 94 | } |
| 95 | |
David Carrillo-Cisneros | 0b3d341 | 2017-07-17 21:25:45 -0700 | [diff] [blame] | 96 | static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size) |
| 97 | { |
| 98 | ssize_t ret = writen(ff->fd, buf, size); |
| 99 | |
| 100 | if (ret != (ssize_t)size) |
| 101 | return ret < 0 ? (int)ret : -1; |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size) |
| 106 | { |
| 107 | /* struct perf_event_header::size is u16 */ |
| 108 | const size_t max_size = 0xffff - sizeof(struct perf_event_header); |
| 109 | size_t new_size = ff->size; |
| 110 | void *addr; |
| 111 | |
| 112 | if (size + ff->offset > max_size) |
| 113 | return -E2BIG; |
| 114 | |
| 115 | while (size > (new_size - ff->offset)) |
| 116 | new_size <<= 1; |
| 117 | new_size = min(max_size, new_size); |
| 118 | |
| 119 | if (ff->size < new_size) { |
| 120 | addr = realloc(ff->buf, new_size); |
| 121 | if (!addr) |
| 122 | return -ENOMEM; |
| 123 | ff->buf = addr; |
| 124 | ff->size = new_size; |
| 125 | } |
| 126 | |
| 127 | memcpy(ff->buf + ff->offset, buf, size); |
| 128 | ff->offset += size; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
David Carrillo-Cisneros | 2ff5365 | 2017-07-17 21:25:36 -0700 | [diff] [blame] | 133 | /* Return: 0 if succeded, -ERR if failed. */ |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 134 | 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] | 135 | { |
David Carrillo-Cisneros | 0b3d341 | 2017-07-17 21:25:45 -0700 | [diff] [blame] | 136 | if (!ff->buf) |
| 137 | return __do_write_fd(ff, buf, size); |
| 138 | return __do_write_buf(ff, buf, size); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 139 | } |
| 140 | |
David Carrillo-Cisneros | 2ff5365 | 2017-07-17 21:25:36 -0700 | [diff] [blame] | 141 | /* Return: 0 if succeded, -ERR if failed. */ |
Jiri Olsa | e2091ce | 2018-03-07 16:50:08 +0100 | [diff] [blame] | 142 | static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size) |
| 143 | { |
| 144 | u64 *p = (u64 *) set; |
| 145 | int i, ret; |
| 146 | |
| 147 | ret = do_write(ff, &size, sizeof(size)); |
| 148 | if (ret < 0) |
| 149 | return ret; |
| 150 | |
| 151 | for (i = 0; (u64) i < BITS_TO_U64(size); i++) { |
| 152 | ret = do_write(ff, p + i, sizeof(*p)); |
| 153 | if (ret < 0) |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /* Return: 0 if succeded, -ERR if failed. */ |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 161 | int write_padded(struct feat_fd *ff, const void *bf, |
| 162 | size_t count, size_t count_aligned) |
Arnaldo Carvalho de Melo | f92cb24 | 2010-01-04 16:19:28 -0200 | [diff] [blame] | 163 | { |
| 164 | static const char zero_buf[NAME_ALIGN]; |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 165 | int err = do_write(ff, bf, count); |
Arnaldo Carvalho de Melo | f92cb24 | 2010-01-04 16:19:28 -0200 | [diff] [blame] | 166 | |
| 167 | if (!err) |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 168 | err = do_write(ff, zero_buf, count_aligned - count); |
Arnaldo Carvalho de Melo | f92cb24 | 2010-01-04 16:19:28 -0200 | [diff] [blame] | 169 | |
| 170 | return err; |
| 171 | } |
| 172 | |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 173 | #define string_size(str) \ |
| 174 | (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32)) |
| 175 | |
David Carrillo-Cisneros | 2ff5365 | 2017-07-17 21:25:36 -0700 | [diff] [blame] | 176 | /* Return: 0 if succeded, -ERR if failed. */ |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 177 | static int do_write_string(struct feat_fd *ff, const char *str) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 178 | { |
| 179 | u32 len, olen; |
| 180 | int ret; |
| 181 | |
| 182 | olen = strlen(str) + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 183 | len = PERF_ALIGN(olen, NAME_ALIGN); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 184 | |
| 185 | /* write len, incl. \0 */ |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 186 | ret = do_write(ff, &len, sizeof(len)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 187 | if (ret < 0) |
| 188 | return ret; |
| 189 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 190 | return write_padded(ff, str, olen, len); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 191 | } |
| 192 | |
David Carrillo-Cisneros | 0b3d341 | 2017-07-17 21:25:45 -0700 | [diff] [blame] | 193 | 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] | 194 | { |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 195 | ssize_t ret = readn(ff->fd, addr, size); |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 196 | |
| 197 | if (ret != size) |
| 198 | return ret < 0 ? (int)ret : -1; |
| 199 | return 0; |
| 200 | } |
| 201 | |
David Carrillo-Cisneros | 0b3d341 | 2017-07-17 21:25:45 -0700 | [diff] [blame] | 202 | static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size) |
| 203 | { |
| 204 | if (size > (ssize_t)ff->size - ff->offset) |
| 205 | return -1; |
| 206 | |
| 207 | memcpy(addr, ff->buf + ff->offset, size); |
| 208 | ff->offset += size; |
| 209 | |
| 210 | return 0; |
| 211 | |
| 212 | } |
| 213 | |
| 214 | static int __do_read(struct feat_fd *ff, void *addr, ssize_t size) |
| 215 | { |
| 216 | if (!ff->buf) |
| 217 | return __do_read_fd(ff, addr, size); |
| 218 | return __do_read_buf(ff, addr, size); |
| 219 | } |
| 220 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 221 | static int do_read_u32(struct feat_fd *ff, u32 *addr) |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 222 | { |
| 223 | int ret; |
| 224 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 225 | ret = __do_read(ff, addr, sizeof(*addr)); |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 226 | if (ret) |
| 227 | return ret; |
| 228 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 229 | if (ff->ph->needs_swap) |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 230 | *addr = bswap_32(*addr); |
| 231 | return 0; |
| 232 | } |
| 233 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 234 | static int do_read_u64(struct feat_fd *ff, u64 *addr) |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 235 | { |
| 236 | int ret; |
| 237 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 238 | ret = __do_read(ff, addr, sizeof(*addr)); |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 239 | if (ret) |
| 240 | return ret; |
| 241 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 242 | if (ff->ph->needs_swap) |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 243 | *addr = bswap_64(*addr); |
| 244 | return 0; |
| 245 | } |
| 246 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 247 | static char *do_read_string(struct feat_fd *ff) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 248 | { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 249 | u32 len; |
| 250 | char *buf; |
| 251 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 252 | if (do_read_u32(ff, &len)) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 253 | return NULL; |
| 254 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 255 | buf = malloc(len); |
| 256 | if (!buf) |
| 257 | return NULL; |
| 258 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 259 | if (!__do_read(ff, buf, len)) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 260 | /* |
| 261 | * strings are padded by zeroes |
| 262 | * thus the actual strlen of buf |
| 263 | * may be less than len |
| 264 | */ |
| 265 | return buf; |
| 266 | } |
| 267 | |
| 268 | free(buf); |
| 269 | return NULL; |
| 270 | } |
| 271 | |
Jiri Olsa | e2091ce | 2018-03-07 16:50:08 +0100 | [diff] [blame] | 272 | /* Return: 0 if succeded, -ERR if failed. */ |
| 273 | static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize) |
| 274 | { |
| 275 | unsigned long *set; |
| 276 | u64 size, *p; |
| 277 | int i, ret; |
| 278 | |
| 279 | ret = do_read_u64(ff, &size); |
| 280 | if (ret) |
| 281 | return ret; |
| 282 | |
| 283 | set = bitmap_alloc(size); |
| 284 | if (!set) |
| 285 | return -ENOMEM; |
| 286 | |
Jiri Olsa | e2091ce | 2018-03-07 16:50:08 +0100 | [diff] [blame] | 287 | p = (u64 *) set; |
| 288 | |
| 289 | for (i = 0; (u64) i < BITS_TO_U64(size); i++) { |
| 290 | ret = do_read_u64(ff, p + i); |
| 291 | if (ret < 0) { |
| 292 | free(set); |
| 293 | return ret; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | *pset = set; |
| 298 | *psize = size; |
| 299 | return 0; |
| 300 | } |
| 301 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 302 | static int write_tracing_data(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 303 | struct evlist *evlist) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 304 | { |
David Carrillo-Cisneros | 0b3d341 | 2017-07-17 21:25:45 -0700 | [diff] [blame] | 305 | if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) |
| 306 | return -1; |
| 307 | |
Jiri Olsa | ce9036a | 2019-07-21 13:24:23 +0200 | [diff] [blame] | 308 | return read_tracing_data(ff->fd, &evlist->core.entries); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 309 | } |
| 310 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 311 | static int write_build_id(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 312 | struct evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 313 | { |
| 314 | struct perf_session *session; |
| 315 | int err; |
| 316 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 317 | session = container_of(ff->ph, struct perf_session, header); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 318 | |
Robert Richter | e20960c | 2011-12-07 10:02:55 +0100 | [diff] [blame] | 319 | if (!perf_session__read_build_ids(session, true)) |
| 320 | return -1; |
| 321 | |
David Carrillo-Cisneros | 0b3d341 | 2017-07-17 21:25:45 -0700 | [diff] [blame] | 322 | if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) |
| 323 | return -1; |
| 324 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 325 | err = perf_session__write_buildid_table(session, ff); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 326 | if (err < 0) { |
| 327 | pr_debug("failed to write buildid table\n"); |
| 328 | return err; |
| 329 | } |
Namhyung Kim | 73c5d22 | 2014-11-07 22:57:56 +0900 | [diff] [blame] | 330 | perf_session__cache_build_ids(session); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 335 | static int write_hostname(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 336 | struct evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 337 | { |
| 338 | struct utsname uts; |
| 339 | int ret; |
| 340 | |
| 341 | ret = uname(&uts); |
| 342 | if (ret < 0) |
| 343 | return -1; |
| 344 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 345 | return do_write_string(ff, uts.nodename); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 346 | } |
| 347 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 348 | static int write_osrelease(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 349 | struct evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 350 | { |
| 351 | struct utsname uts; |
| 352 | int ret; |
| 353 | |
| 354 | ret = uname(&uts); |
| 355 | if (ret < 0) |
| 356 | return -1; |
| 357 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 358 | return do_write_string(ff, uts.release); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 359 | } |
| 360 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 361 | static int write_arch(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 362 | struct evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 363 | { |
| 364 | struct utsname uts; |
| 365 | int ret; |
| 366 | |
| 367 | ret = uname(&uts); |
| 368 | if (ret < 0) |
| 369 | return -1; |
| 370 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 371 | return do_write_string(ff, uts.machine); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 372 | } |
| 373 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 374 | static int write_version(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 375 | struct evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 376 | { |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 377 | return do_write_string(ff, perf_version_string); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 378 | } |
| 379 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 380 | static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 381 | { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 382 | FILE *file; |
| 383 | char *buf = NULL; |
| 384 | char *s, *p; |
Wang Nan | 493c303 | 2014-10-24 09:45:26 +0800 | [diff] [blame] | 385 | const char *search = cpuinfo_proc; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 386 | size_t len = 0; |
| 387 | int ret = -1; |
| 388 | |
| 389 | if (!search) |
| 390 | return -1; |
| 391 | |
| 392 | file = fopen("/proc/cpuinfo", "r"); |
| 393 | if (!file) |
| 394 | return -1; |
| 395 | |
| 396 | while (getline(&buf, &len, file) > 0) { |
| 397 | ret = strncmp(buf, search, strlen(search)); |
| 398 | if (!ret) |
| 399 | break; |
| 400 | } |
| 401 | |
Wang Nan | ed30775 | 2014-10-16 11:08:29 +0800 | [diff] [blame] | 402 | if (ret) { |
| 403 | ret = -1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 404 | goto done; |
Wang Nan | ed30775 | 2014-10-16 11:08:29 +0800 | [diff] [blame] | 405 | } |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 406 | |
| 407 | s = buf; |
| 408 | |
| 409 | p = strchr(buf, ':'); |
| 410 | if (p && *(p+1) == ' ' && *(p+2)) |
| 411 | s = p + 2; |
| 412 | p = strchr(s, '\n'); |
| 413 | if (p) |
| 414 | *p = '\0'; |
| 415 | |
| 416 | /* squash extra space characters (branding string) */ |
| 417 | p = s; |
| 418 | while (*p) { |
| 419 | if (isspace(*p)) { |
| 420 | char *r = p + 1; |
Arnaldo Carvalho de Melo | fc6a172 | 2019-06-25 21:33:14 -0300 | [diff] [blame] | 421 | char *q = skip_spaces(r); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 422 | *p = ' '; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 423 | if (q != (p+1)) |
| 424 | while ((*r++ = *q++)); |
| 425 | } |
| 426 | p++; |
| 427 | } |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 428 | ret = do_write_string(ff, s); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 429 | done: |
| 430 | free(buf); |
| 431 | fclose(file); |
| 432 | return ret; |
| 433 | } |
| 434 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 435 | static int write_cpudesc(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 436 | struct evlist *evlist __maybe_unused) |
Wang Nan | 493c303 | 2014-10-24 09:45:26 +0800 | [diff] [blame] | 437 | { |
Wang Nan | 493c303 | 2014-10-24 09:45:26 +0800 | [diff] [blame] | 438 | const char *cpuinfo_procs[] = CPUINFO_PROC; |
| 439 | unsigned int i; |
| 440 | |
| 441 | for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) { |
| 442 | int ret; |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 443 | ret = __write_cpudesc(ff, cpuinfo_procs[i]); |
Wang Nan | 493c303 | 2014-10-24 09:45:26 +0800 | [diff] [blame] | 444 | if (ret >= 0) |
| 445 | return ret; |
| 446 | } |
| 447 | return -1; |
| 448 | } |
| 449 | |
| 450 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 451 | static int write_nrcpus(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 452 | struct evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 453 | { |
| 454 | long nr; |
| 455 | u32 nrc, nra; |
| 456 | int ret; |
| 457 | |
Jan Stancek | da8a58b | 2017-02-17 12:10:26 +0100 | [diff] [blame] | 458 | nrc = cpu__max_present_cpu(); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 459 | |
| 460 | nr = sysconf(_SC_NPROCESSORS_ONLN); |
| 461 | if (nr < 0) |
| 462 | return -1; |
| 463 | |
| 464 | nra = (u32)(nr & UINT_MAX); |
| 465 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 466 | ret = do_write(ff, &nrc, sizeof(nrc)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 467 | if (ret < 0) |
| 468 | return ret; |
| 469 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 470 | return do_write(ff, &nra, sizeof(nra)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 471 | } |
| 472 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 473 | static int write_event_desc(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 474 | struct evlist *evlist) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 475 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 476 | struct evsel *evsel; |
Namhyung Kim | 74ba9e1 | 2012-09-05 14:02:47 +0900 | [diff] [blame] | 477 | u32 nre, nri, sz; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 478 | int ret; |
| 479 | |
Jiri Olsa | 6484d2f | 2019-07-21 13:24:28 +0200 | [diff] [blame] | 480 | nre = evlist->core.nr_entries; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 481 | |
| 482 | /* |
| 483 | * write number of events |
| 484 | */ |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 485 | ret = do_write(ff, &nre, sizeof(nre)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 486 | if (ret < 0) |
| 487 | return ret; |
| 488 | |
| 489 | /* |
| 490 | * size of perf_event_attr struct |
| 491 | */ |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 492 | sz = (u32)sizeof(evsel->core.attr); |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 493 | ret = do_write(ff, &sz, sizeof(sz)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 494 | if (ret < 0) |
| 495 | return ret; |
| 496 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 497 | evlist__for_each_entry(evlist, evsel) { |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 498 | ret = do_write(ff, &evsel->core.attr, sz); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 499 | if (ret < 0) |
| 500 | return ret; |
| 501 | /* |
| 502 | * write number of unique id per event |
| 503 | * there is one id per instance of an event |
| 504 | * |
| 505 | * copy into an nri to be independent of the |
| 506 | * type of ids, |
| 507 | */ |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 508 | nri = evsel->ids; |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 509 | ret = do_write(ff, &nri, sizeof(nri)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 510 | if (ret < 0) |
| 511 | return ret; |
| 512 | |
| 513 | /* |
| 514 | * write event string as passed on cmdline |
| 515 | */ |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 516 | ret = do_write_string(ff, perf_evsel__name(evsel)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 517 | if (ret < 0) |
| 518 | return ret; |
| 519 | /* |
| 520 | * write unique ids for this event |
| 521 | */ |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 522 | ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 523 | if (ret < 0) |
| 524 | return ret; |
| 525 | } |
| 526 | return 0; |
| 527 | } |
| 528 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 529 | static int write_cmdline(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 530 | struct evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 531 | { |
Andi Kleen | 94816ad | 2019-02-24 07:37:19 -0800 | [diff] [blame] | 532 | char pbuf[MAXPATHLEN], *buf; |
| 533 | int i, ret, n; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 534 | |
Tommi Rantala | 55f77128 | 2017-03-22 15:06:24 +0200 | [diff] [blame] | 535 | /* actual path to perf binary */ |
Andi Kleen | 94816ad | 2019-02-24 07:37:19 -0800 | [diff] [blame] | 536 | buf = perf_exe(pbuf, MAXPATHLEN); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 537 | |
| 538 | /* account for binary path */ |
Arnaldo Carvalho de Melo | b699869 | 2015-09-08 16:58:20 -0300 | [diff] [blame] | 539 | n = perf_env.nr_cmdline + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 540 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 541 | ret = do_write(ff, &n, sizeof(n)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 542 | if (ret < 0) |
| 543 | return ret; |
| 544 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 545 | ret = do_write_string(ff, buf); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 546 | if (ret < 0) |
| 547 | return ret; |
| 548 | |
Arnaldo Carvalho de Melo | b699869 | 2015-09-08 16:58:20 -0300 | [diff] [blame] | 549 | for (i = 0 ; i < perf_env.nr_cmdline; i++) { |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 550 | ret = do_write_string(ff, perf_env.cmdline_argv[i]); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 551 | if (ret < 0) |
| 552 | return ret; |
| 553 | } |
| 554 | return 0; |
| 555 | } |
| 556 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 557 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 558 | static int write_cpu_topology(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 559 | struct evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 560 | { |
Jiri Olsa | 5135d5e | 2019-02-19 10:58:13 +0100 | [diff] [blame] | 561 | struct cpu_topology *tp; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 562 | u32 i; |
Arnaldo Carvalho de Melo | aa36ddd | 2015-09-09 10:37:01 -0300 | [diff] [blame] | 563 | int ret, j; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 564 | |
Jiri Olsa | 5135d5e | 2019-02-19 10:58:13 +0100 | [diff] [blame] | 565 | tp = cpu_topology__new(); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 566 | if (!tp) |
| 567 | return -1; |
| 568 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 569 | ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 570 | if (ret < 0) |
| 571 | goto done; |
| 572 | |
| 573 | for (i = 0; i < tp->core_sib; i++) { |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 574 | ret = do_write_string(ff, tp->core_siblings[i]); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 575 | if (ret < 0) |
| 576 | goto done; |
| 577 | } |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 578 | ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 579 | if (ret < 0) |
| 580 | goto done; |
| 581 | |
| 582 | for (i = 0; i < tp->thread_sib; i++) { |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 583 | ret = do_write_string(ff, tp->thread_siblings[i]); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 584 | if (ret < 0) |
| 585 | break; |
| 586 | } |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 587 | |
Arnaldo Carvalho de Melo | aa36ddd | 2015-09-09 10:37:01 -0300 | [diff] [blame] | 588 | ret = perf_env__read_cpu_topology_map(&perf_env); |
| 589 | if (ret < 0) |
| 590 | goto done; |
| 591 | |
| 592 | for (j = 0; j < perf_env.nr_cpus_avail; j++) { |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 593 | ret = do_write(ff, &perf_env.cpu[j].core_id, |
Arnaldo Carvalho de Melo | aa36ddd | 2015-09-09 10:37:01 -0300 | [diff] [blame] | 594 | sizeof(perf_env.cpu[j].core_id)); |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 595 | if (ret < 0) |
| 596 | return ret; |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 597 | ret = do_write(ff, &perf_env.cpu[j].socket_id, |
Arnaldo Carvalho de Melo | aa36ddd | 2015-09-09 10:37:01 -0300 | [diff] [blame] | 598 | sizeof(perf_env.cpu[j].socket_id)); |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 599 | if (ret < 0) |
| 600 | return ret; |
| 601 | } |
Kan Liang | acae8b3 | 2019-06-04 15:50:41 -0700 | [diff] [blame] | 602 | |
| 603 | if (!tp->die_sib) |
| 604 | goto done; |
| 605 | |
| 606 | ret = do_write(ff, &tp->die_sib, sizeof(tp->die_sib)); |
| 607 | if (ret < 0) |
| 608 | goto done; |
| 609 | |
| 610 | for (i = 0; i < tp->die_sib; i++) { |
| 611 | ret = do_write_string(ff, tp->die_siblings[i]); |
| 612 | if (ret < 0) |
| 613 | goto done; |
| 614 | } |
| 615 | |
| 616 | for (j = 0; j < perf_env.nr_cpus_avail; j++) { |
| 617 | ret = do_write(ff, &perf_env.cpu[j].die_id, |
| 618 | sizeof(perf_env.cpu[j].die_id)); |
| 619 | if (ret < 0) |
| 620 | return ret; |
| 621 | } |
| 622 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 623 | done: |
Jiri Olsa | 5135d5e | 2019-02-19 10:58:13 +0100 | [diff] [blame] | 624 | cpu_topology__delete(tp); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 625 | return ret; |
| 626 | } |
| 627 | |
| 628 | |
| 629 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 630 | static int write_total_mem(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 631 | struct evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 632 | { |
| 633 | char *buf = NULL; |
| 634 | FILE *fp; |
| 635 | size_t len = 0; |
| 636 | int ret = -1, n; |
| 637 | uint64_t mem; |
| 638 | |
| 639 | fp = fopen("/proc/meminfo", "r"); |
| 640 | if (!fp) |
| 641 | return -1; |
| 642 | |
| 643 | while (getline(&buf, &len, fp) > 0) { |
| 644 | ret = strncmp(buf, "MemTotal:", 9); |
| 645 | if (!ret) |
| 646 | break; |
| 647 | } |
| 648 | if (!ret) { |
| 649 | n = sscanf(buf, "%*s %"PRIu64, &mem); |
| 650 | if (n == 1) |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 651 | ret = do_write(ff, &mem, sizeof(mem)); |
Wang Nan | ed30775 | 2014-10-16 11:08:29 +0800 | [diff] [blame] | 652 | } else |
| 653 | ret = -1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 654 | free(buf); |
| 655 | fclose(fp); |
| 656 | return ret; |
| 657 | } |
| 658 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 659 | static int write_numa_topology(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 660 | struct evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 661 | { |
Jiri Olsa | 48e6c5a | 2019-02-19 10:58:14 +0100 | [diff] [blame] | 662 | struct numa_topology *tp; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 663 | int ret = -1; |
Jiri Olsa | 48e6c5a | 2019-02-19 10:58:14 +0100 | [diff] [blame] | 664 | u32 i; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 665 | |
Jiri Olsa | 48e6c5a | 2019-02-19 10:58:14 +0100 | [diff] [blame] | 666 | tp = numa_topology__new(); |
| 667 | if (!tp) |
| 668 | return -ENOMEM; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 669 | |
Jiri Olsa | 48e6c5a | 2019-02-19 10:58:14 +0100 | [diff] [blame] | 670 | ret = do_write(ff, &tp->nr, sizeof(u32)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 671 | if (ret < 0) |
Jiri Olsa | 48e6c5a | 2019-02-19 10:58:14 +0100 | [diff] [blame] | 672 | goto err; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 673 | |
Jiri Olsa | 48e6c5a | 2019-02-19 10:58:14 +0100 | [diff] [blame] | 674 | for (i = 0; i < tp->nr; i++) { |
| 675 | struct numa_topology_node *n = &tp->nodes[i]; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 676 | |
Jiri Olsa | 48e6c5a | 2019-02-19 10:58:14 +0100 | [diff] [blame] | 677 | ret = do_write(ff, &n->node, sizeof(u32)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 678 | if (ret < 0) |
Jiri Olsa | 48e6c5a | 2019-02-19 10:58:14 +0100 | [diff] [blame] | 679 | goto err; |
| 680 | |
| 681 | ret = do_write(ff, &n->mem_total, sizeof(u64)); |
| 682 | if (ret) |
| 683 | goto err; |
| 684 | |
| 685 | ret = do_write(ff, &n->mem_free, sizeof(u64)); |
| 686 | if (ret) |
| 687 | goto err; |
| 688 | |
| 689 | ret = do_write_string(ff, n->cpus); |
| 690 | if (ret < 0) |
| 691 | goto err; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 692 | } |
Jiri Olsa | 48e6c5a | 2019-02-19 10:58:14 +0100 | [diff] [blame] | 693 | |
| 694 | ret = 0; |
| 695 | |
| 696 | err: |
| 697 | numa_topology__delete(tp); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | /* |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 702 | * File format: |
| 703 | * |
| 704 | * struct pmu_mappings { |
| 705 | * u32 pmu_num; |
| 706 | * struct pmu_map { |
| 707 | * u32 type; |
| 708 | * char name[]; |
| 709 | * }[pmu_num]; |
| 710 | * }; |
| 711 | */ |
| 712 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 713 | static int write_pmu_mappings(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 714 | struct evlist *evlist __maybe_unused) |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 715 | { |
| 716 | struct perf_pmu *pmu = NULL; |
David Carrillo-Cisneros | a02c395 | 2017-07-17 21:25:44 -0700 | [diff] [blame] | 717 | u32 pmu_num = 0; |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 718 | int ret; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 719 | |
David Carrillo-Cisneros | a02c395 | 2017-07-17 21:25:44 -0700 | [diff] [blame] | 720 | /* |
| 721 | * Do a first pass to count number of pmu to avoid lseek so this |
| 722 | * works in pipe mode as well. |
| 723 | */ |
| 724 | while ((pmu = perf_pmu__scan(pmu))) { |
| 725 | if (!pmu->name) |
| 726 | continue; |
| 727 | pmu_num++; |
| 728 | } |
| 729 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 730 | ret = do_write(ff, &pmu_num, sizeof(pmu_num)); |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 731 | if (ret < 0) |
| 732 | return ret; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 733 | |
| 734 | while ((pmu = perf_pmu__scan(pmu))) { |
| 735 | if (!pmu->name) |
| 736 | continue; |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 737 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 738 | ret = do_write(ff, &pmu->type, sizeof(pmu->type)); |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 739 | if (ret < 0) |
| 740 | return ret; |
| 741 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 742 | ret = do_write_string(ff, pmu->name); |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 743 | if (ret < 0) |
| 744 | return ret; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 745 | } |
| 746 | |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 747 | return 0; |
| 748 | } |
| 749 | |
| 750 | /* |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 751 | * File format: |
| 752 | * |
| 753 | * struct group_descs { |
| 754 | * u32 nr_groups; |
| 755 | * struct group_desc { |
| 756 | * char name[]; |
| 757 | * u32 leader_idx; |
| 758 | * u32 nr_members; |
| 759 | * }[nr_groups]; |
| 760 | * }; |
| 761 | */ |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 762 | static int write_group_desc(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 763 | struct evlist *evlist) |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 764 | { |
| 765 | u32 nr_groups = evlist->nr_groups; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 766 | struct evsel *evsel; |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 767 | int ret; |
| 768 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 769 | ret = do_write(ff, &nr_groups, sizeof(nr_groups)); |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 770 | if (ret < 0) |
| 771 | return ret; |
| 772 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 773 | evlist__for_each_entry(evlist, evsel) { |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 774 | if (perf_evsel__is_group_leader(evsel) && |
| 775 | evsel->nr_members > 1) { |
| 776 | const char *name = evsel->group_name ?: "{anon_group}"; |
| 777 | u32 leader_idx = evsel->idx; |
| 778 | u32 nr_members = evsel->nr_members; |
| 779 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 780 | ret = do_write_string(ff, name); |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 781 | if (ret < 0) |
| 782 | return ret; |
| 783 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 784 | ret = do_write(ff, &leader_idx, sizeof(leader_idx)); |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 785 | if (ret < 0) |
| 786 | return ret; |
| 787 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 788 | ret = do_write(ff, &nr_members, sizeof(nr_members)); |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 789 | if (ret < 0) |
| 790 | return ret; |
| 791 | } |
| 792 | } |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | /* |
Kan Liang | f4a0742 | 2018-11-21 08:49:39 -0800 | [diff] [blame] | 797 | * Return the CPU id as a raw string. |
| 798 | * |
| 799 | * Each architecture should provide a more precise id string that |
| 800 | * can be use to match the architecture's "mapfile". |
| 801 | */ |
| 802 | char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused) |
| 803 | { |
| 804 | return NULL; |
| 805 | } |
| 806 | |
| 807 | /* Return zero when the cpuid from the mapfile.csv matches the |
| 808 | * cpuid string generated on this platform. |
| 809 | * Otherwise return non-zero. |
| 810 | */ |
| 811 | int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid) |
| 812 | { |
| 813 | regex_t re; |
| 814 | regmatch_t pmatch[1]; |
| 815 | int match; |
| 816 | |
| 817 | if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) { |
| 818 | /* Warn unable to generate match particular string. */ |
| 819 | pr_info("Invalid regular expression %s\n", mapcpuid); |
| 820 | return 1; |
| 821 | } |
| 822 | |
| 823 | match = !regexec(&re, cpuid, 1, pmatch, 0); |
| 824 | regfree(&re); |
| 825 | if (match) { |
| 826 | size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so); |
| 827 | |
| 828 | /* Verify the entire string matched. */ |
| 829 | if (match_len == strlen(cpuid)) |
| 830 | return 0; |
| 831 | } |
| 832 | return 1; |
| 833 | } |
| 834 | |
| 835 | /* |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 836 | * default get_cpuid(): nothing gets recorded |
Jiada Wang | 7a759cd | 2017-04-09 20:02:37 -0700 | [diff] [blame] | 837 | * actual implementation must be in arch/$(SRCARCH)/util/header.c |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 838 | */ |
Rui Teng | 11d8f87 | 2016-07-28 10:05:57 +0800 | [diff] [blame] | 839 | 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] | 840 | { |
| 841 | return -1; |
| 842 | } |
| 843 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 844 | static int write_cpuid(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 845 | struct evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 846 | { |
| 847 | char buffer[64]; |
| 848 | int ret; |
| 849 | |
| 850 | ret = get_cpuid(buffer, sizeof(buffer)); |
Jiri Olsa | a9aeb87 | 2019-02-13 13:32:43 +0100 | [diff] [blame] | 851 | if (ret) |
| 852 | return -1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 853 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 854 | return do_write_string(ff, buffer); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 855 | } |
| 856 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 857 | static int write_branch_stack(struct feat_fd *ff __maybe_unused, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 858 | struct evlist *evlist __maybe_unused) |
Stephane Eranian | 330aa67 | 2012-03-08 23:47:46 +0100 | [diff] [blame] | 859 | { |
| 860 | return 0; |
| 861 | } |
| 862 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 863 | static int write_auxtrace(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 864 | struct evlist *evlist __maybe_unused) |
Adrian Hunter | 4025ea4 | 2015-04-09 18:53:41 +0300 | [diff] [blame] | 865 | { |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 866 | struct perf_session *session; |
| 867 | int err; |
| 868 | |
David Carrillo-Cisneros | 0b3d341 | 2017-07-17 21:25:45 -0700 | [diff] [blame] | 869 | if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) |
| 870 | return -1; |
| 871 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 872 | session = container_of(ff->ph, struct perf_session, header); |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 873 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 874 | err = auxtrace_index__write(ff->fd, &session->auxtrace_index); |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 875 | if (err < 0) |
| 876 | pr_err("Failed to write auxtrace index\n"); |
| 877 | return err; |
Adrian Hunter | 4025ea4 | 2015-04-09 18:53:41 +0300 | [diff] [blame] | 878 | } |
| 879 | |
Alexey Budankov | cf79051 | 2018-10-09 17:36:24 +0300 | [diff] [blame] | 880 | static int write_clockid(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 881 | struct evlist *evlist __maybe_unused) |
Alexey Budankov | cf79051 | 2018-10-09 17:36:24 +0300 | [diff] [blame] | 882 | { |
| 883 | return do_write(ff, &ff->ph->env.clockid_res_ns, |
| 884 | sizeof(ff->ph->env.clockid_res_ns)); |
| 885 | } |
| 886 | |
Jiri Olsa | 258031c | 2019-03-08 14:47:39 +0100 | [diff] [blame] | 887 | static int write_dir_format(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 888 | struct evlist *evlist __maybe_unused) |
Jiri Olsa | 258031c | 2019-03-08 14:47:39 +0100 | [diff] [blame] | 889 | { |
| 890 | struct perf_session *session; |
| 891 | struct perf_data *data; |
| 892 | |
| 893 | session = container_of(ff->ph, struct perf_session, header); |
| 894 | data = session->data; |
| 895 | |
| 896 | if (WARN_ON(!perf_data__is_dir(data))) |
| 897 | return -1; |
| 898 | |
| 899 | return do_write(ff, &data->dir.version, sizeof(data->dir.version)); |
| 900 | } |
| 901 | |
Song Liu | 606f972 | 2019-03-11 22:30:43 -0700 | [diff] [blame] | 902 | #ifdef HAVE_LIBBPF_SUPPORT |
| 903 | static int write_bpf_prog_info(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 904 | struct evlist *evlist __maybe_unused) |
Song Liu | 606f972 | 2019-03-11 22:30:43 -0700 | [diff] [blame] | 905 | { |
| 906 | struct perf_env *env = &ff->ph->env; |
| 907 | struct rb_root *root; |
| 908 | struct rb_node *next; |
| 909 | int ret; |
| 910 | |
| 911 | down_read(&env->bpf_progs.lock); |
| 912 | |
| 913 | ret = do_write(ff, &env->bpf_progs.infos_cnt, |
| 914 | sizeof(env->bpf_progs.infos_cnt)); |
| 915 | if (ret < 0) |
| 916 | goto out; |
| 917 | |
| 918 | root = &env->bpf_progs.infos; |
| 919 | next = rb_first(root); |
| 920 | while (next) { |
| 921 | struct bpf_prog_info_node *node; |
| 922 | size_t len; |
| 923 | |
| 924 | node = rb_entry(next, struct bpf_prog_info_node, rb_node); |
| 925 | next = rb_next(&node->rb_node); |
| 926 | len = sizeof(struct bpf_prog_info_linear) + |
| 927 | node->info_linear->data_len; |
| 928 | |
| 929 | /* before writing to file, translate address to offset */ |
| 930 | bpf_program__bpil_addr_to_offs(node->info_linear); |
| 931 | ret = do_write(ff, node->info_linear, len); |
| 932 | /* |
| 933 | * translate back to address even when do_write() fails, |
| 934 | * so that this function never changes the data. |
| 935 | */ |
| 936 | bpf_program__bpil_offs_to_addr(node->info_linear); |
| 937 | if (ret < 0) |
| 938 | goto out; |
| 939 | } |
| 940 | out: |
| 941 | up_read(&env->bpf_progs.lock); |
| 942 | return ret; |
| 943 | } |
| 944 | #else // HAVE_LIBBPF_SUPPORT |
| 945 | static int write_bpf_prog_info(struct feat_fd *ff __maybe_unused, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 946 | struct evlist *evlist __maybe_unused) |
Song Liu | 606f972 | 2019-03-11 22:30:43 -0700 | [diff] [blame] | 947 | { |
| 948 | return 0; |
| 949 | } |
| 950 | #endif // HAVE_LIBBPF_SUPPORT |
| 951 | |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 952 | static int write_bpf_btf(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 953 | struct evlist *evlist __maybe_unused) |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 954 | { |
| 955 | struct perf_env *env = &ff->ph->env; |
| 956 | struct rb_root *root; |
| 957 | struct rb_node *next; |
| 958 | int ret; |
| 959 | |
| 960 | down_read(&env->bpf_progs.lock); |
| 961 | |
| 962 | ret = do_write(ff, &env->bpf_progs.btfs_cnt, |
| 963 | sizeof(env->bpf_progs.btfs_cnt)); |
| 964 | |
| 965 | if (ret < 0) |
| 966 | goto out; |
| 967 | |
| 968 | root = &env->bpf_progs.btfs; |
| 969 | next = rb_first(root); |
| 970 | while (next) { |
| 971 | struct btf_node *node; |
| 972 | |
| 973 | node = rb_entry(next, struct btf_node, rb_node); |
| 974 | next = rb_next(&node->rb_node); |
| 975 | ret = do_write(ff, &node->id, |
| 976 | sizeof(u32) * 2 + node->data_size); |
| 977 | if (ret < 0) |
| 978 | goto out; |
| 979 | } |
| 980 | out: |
| 981 | up_read(&env->bpf_progs.lock); |
| 982 | return ret; |
| 983 | } |
| 984 | |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 985 | static int cpu_cache_level__sort(const void *a, const void *b) |
| 986 | { |
| 987 | struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a; |
| 988 | struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b; |
| 989 | |
| 990 | return cache_a->level - cache_b->level; |
| 991 | } |
| 992 | |
| 993 | static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b) |
| 994 | { |
| 995 | if (a->level != b->level) |
| 996 | return false; |
| 997 | |
| 998 | if (a->line_size != b->line_size) |
| 999 | return false; |
| 1000 | |
| 1001 | if (a->sets != b->sets) |
| 1002 | return false; |
| 1003 | |
| 1004 | if (a->ways != b->ways) |
| 1005 | return false; |
| 1006 | |
| 1007 | if (strcmp(a->type, b->type)) |
| 1008 | return false; |
| 1009 | |
| 1010 | if (strcmp(a->size, b->size)) |
| 1011 | return false; |
| 1012 | |
| 1013 | if (strcmp(a->map, b->map)) |
| 1014 | return false; |
| 1015 | |
| 1016 | return true; |
| 1017 | } |
| 1018 | |
| 1019 | static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level) |
| 1020 | { |
| 1021 | char path[PATH_MAX], file[PATH_MAX]; |
| 1022 | struct stat st; |
| 1023 | size_t len; |
| 1024 | |
| 1025 | scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level); |
| 1026 | scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path); |
| 1027 | |
| 1028 | if (stat(file, &st)) |
| 1029 | return 1; |
| 1030 | |
| 1031 | scnprintf(file, PATH_MAX, "%s/level", path); |
| 1032 | if (sysfs__read_int(file, (int *) &cache->level)) |
| 1033 | return -1; |
| 1034 | |
| 1035 | scnprintf(file, PATH_MAX, "%s/coherency_line_size", path); |
| 1036 | if (sysfs__read_int(file, (int *) &cache->line_size)) |
| 1037 | return -1; |
| 1038 | |
| 1039 | scnprintf(file, PATH_MAX, "%s/number_of_sets", path); |
| 1040 | if (sysfs__read_int(file, (int *) &cache->sets)) |
| 1041 | return -1; |
| 1042 | |
| 1043 | scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path); |
| 1044 | if (sysfs__read_int(file, (int *) &cache->ways)) |
| 1045 | return -1; |
| 1046 | |
| 1047 | scnprintf(file, PATH_MAX, "%s/type", path); |
| 1048 | if (sysfs__read_str(file, &cache->type, &len)) |
| 1049 | return -1; |
| 1050 | |
| 1051 | cache->type[len] = 0; |
Arnaldo Carvalho de Melo | 13c230a | 2019-06-26 12:13:13 -0300 | [diff] [blame] | 1052 | cache->type = strim(cache->type); |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1053 | |
| 1054 | scnprintf(file, PATH_MAX, "%s/size", path); |
| 1055 | if (sysfs__read_str(file, &cache->size, &len)) { |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 1056 | zfree(&cache->type); |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1057 | return -1; |
| 1058 | } |
| 1059 | |
| 1060 | cache->size[len] = 0; |
Arnaldo Carvalho de Melo | 13c230a | 2019-06-26 12:13:13 -0300 | [diff] [blame] | 1061 | cache->size = strim(cache->size); |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1062 | |
| 1063 | scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path); |
| 1064 | if (sysfs__read_str(file, &cache->map, &len)) { |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 1065 | zfree(&cache->map); |
| 1066 | zfree(&cache->type); |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1067 | return -1; |
| 1068 | } |
| 1069 | |
| 1070 | cache->map[len] = 0; |
Arnaldo Carvalho de Melo | 13c230a | 2019-06-26 12:13:13 -0300 | [diff] [blame] | 1071 | cache->map = strim(cache->map); |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1072 | return 0; |
| 1073 | } |
| 1074 | |
| 1075 | static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c) |
| 1076 | { |
| 1077 | fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map); |
| 1078 | } |
| 1079 | |
| 1080 | static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp) |
| 1081 | { |
| 1082 | u32 i, cnt = 0; |
| 1083 | long ncpus; |
| 1084 | u32 nr, cpu; |
| 1085 | u16 level; |
| 1086 | |
| 1087 | ncpus = sysconf(_SC_NPROCESSORS_CONF); |
| 1088 | if (ncpus < 0) |
| 1089 | return -1; |
| 1090 | |
| 1091 | nr = (u32)(ncpus & UINT_MAX); |
| 1092 | |
| 1093 | for (cpu = 0; cpu < nr; cpu++) { |
| 1094 | for (level = 0; level < 10; level++) { |
| 1095 | struct cpu_cache_level c; |
| 1096 | int err; |
| 1097 | |
| 1098 | err = cpu_cache_level__read(&c, cpu, level); |
| 1099 | if (err < 0) |
| 1100 | return err; |
| 1101 | |
| 1102 | if (err == 1) |
| 1103 | break; |
| 1104 | |
| 1105 | for (i = 0; i < cnt; i++) { |
| 1106 | if (cpu_cache_level__cmp(&c, &caches[i])) |
| 1107 | break; |
| 1108 | } |
| 1109 | |
| 1110 | if (i == cnt) |
| 1111 | caches[cnt++] = c; |
| 1112 | else |
| 1113 | cpu_cache_level__free(&c); |
| 1114 | |
| 1115 | if (WARN_ONCE(cnt == size, "way too many cpu caches..")) |
| 1116 | goto out; |
| 1117 | } |
| 1118 | } |
| 1119 | out: |
| 1120 | *cntp = cnt; |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
Kyle Meyer | 9f94c7f | 2019-06-20 14:36:30 -0500 | [diff] [blame] | 1124 | #define MAX_CACHES (MAX_NR_CPUS * 4) |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1125 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 1126 | static int write_cache(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 1127 | struct evlist *evlist __maybe_unused) |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1128 | { |
| 1129 | struct cpu_cache_level caches[MAX_CACHES]; |
| 1130 | u32 cnt = 0, i, version = 1; |
| 1131 | int ret; |
| 1132 | |
| 1133 | ret = build_caches(caches, MAX_CACHES, &cnt); |
| 1134 | if (ret) |
| 1135 | goto out; |
| 1136 | |
| 1137 | qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort); |
| 1138 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 1139 | ret = do_write(ff, &version, sizeof(u32)); |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1140 | if (ret < 0) |
| 1141 | goto out; |
| 1142 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 1143 | ret = do_write(ff, &cnt, sizeof(u32)); |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1144 | if (ret < 0) |
| 1145 | goto out; |
| 1146 | |
| 1147 | for (i = 0; i < cnt; i++) { |
| 1148 | struct cpu_cache_level *c = &caches[i]; |
| 1149 | |
| 1150 | #define _W(v) \ |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 1151 | ret = do_write(ff, &c->v, sizeof(u32)); \ |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1152 | if (ret < 0) \ |
| 1153 | goto out; |
| 1154 | |
| 1155 | _W(level) |
| 1156 | _W(line_size) |
| 1157 | _W(sets) |
| 1158 | _W(ways) |
| 1159 | #undef _W |
| 1160 | |
| 1161 | #define _W(v) \ |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 1162 | ret = do_write_string(ff, (const char *) c->v); \ |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1163 | if (ret < 0) \ |
| 1164 | goto out; |
| 1165 | |
| 1166 | _W(type) |
| 1167 | _W(size) |
| 1168 | _W(map) |
| 1169 | #undef _W |
| 1170 | } |
| 1171 | |
| 1172 | out: |
| 1173 | for (i = 0; i < cnt; i++) |
| 1174 | cpu_cache_level__free(&caches[i]); |
| 1175 | return ret; |
| 1176 | } |
| 1177 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 1178 | static int write_stat(struct feat_fd *ff __maybe_unused, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 1179 | struct evlist *evlist __maybe_unused) |
Jiri Olsa | ffa517a | 2015-10-25 15:51:43 +0100 | [diff] [blame] | 1180 | { |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Jin Yao | 6011518 | 2017-12-08 21:13:41 +0800 | [diff] [blame] | 1184 | static int write_sample_time(struct feat_fd *ff, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 1185 | struct evlist *evlist) |
Jin Yao | 6011518 | 2017-12-08 21:13:41 +0800 | [diff] [blame] | 1186 | { |
| 1187 | int ret; |
| 1188 | |
| 1189 | ret = do_write(ff, &evlist->first_sample_time, |
| 1190 | sizeof(evlist->first_sample_time)); |
| 1191 | if (ret < 0) |
| 1192 | return ret; |
| 1193 | |
| 1194 | return do_write(ff, &evlist->last_sample_time, |
| 1195 | sizeof(evlist->last_sample_time)); |
| 1196 | } |
| 1197 | |
Jiri Olsa | e2091ce | 2018-03-07 16:50:08 +0100 | [diff] [blame] | 1198 | |
| 1199 | static int memory_node__read(struct memory_node *n, unsigned long idx) |
| 1200 | { |
| 1201 | unsigned int phys, size = 0; |
| 1202 | char path[PATH_MAX]; |
| 1203 | struct dirent *ent; |
| 1204 | DIR *dir; |
| 1205 | |
| 1206 | #define for_each_memory(mem, dir) \ |
| 1207 | while ((ent = readdir(dir))) \ |
| 1208 | if (strcmp(ent->d_name, ".") && \ |
| 1209 | strcmp(ent->d_name, "..") && \ |
| 1210 | sscanf(ent->d_name, "memory%u", &mem) == 1) |
| 1211 | |
| 1212 | scnprintf(path, PATH_MAX, |
| 1213 | "%s/devices/system/node/node%lu", |
| 1214 | sysfs__mountpoint(), idx); |
| 1215 | |
| 1216 | dir = opendir(path); |
| 1217 | if (!dir) { |
| 1218 | pr_warning("failed: cant' open memory sysfs data\n"); |
| 1219 | return -1; |
| 1220 | } |
| 1221 | |
| 1222 | for_each_memory(phys, dir) { |
| 1223 | size = max(phys, size); |
| 1224 | } |
| 1225 | |
| 1226 | size++; |
| 1227 | |
| 1228 | n->set = bitmap_alloc(size); |
| 1229 | if (!n->set) { |
| 1230 | closedir(dir); |
| 1231 | return -ENOMEM; |
| 1232 | } |
| 1233 | |
Jiri Olsa | e2091ce | 2018-03-07 16:50:08 +0100 | [diff] [blame] | 1234 | n->node = idx; |
| 1235 | n->size = size; |
| 1236 | |
| 1237 | rewinddir(dir); |
| 1238 | |
| 1239 | for_each_memory(phys, dir) { |
| 1240 | set_bit(phys, n->set); |
| 1241 | } |
| 1242 | |
| 1243 | closedir(dir); |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | static int memory_node__sort(const void *a, const void *b) |
| 1248 | { |
| 1249 | const struct memory_node *na = a; |
| 1250 | const struct memory_node *nb = b; |
| 1251 | |
| 1252 | return na->node - nb->node; |
| 1253 | } |
| 1254 | |
| 1255 | static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp) |
| 1256 | { |
| 1257 | char path[PATH_MAX]; |
| 1258 | struct dirent *ent; |
| 1259 | DIR *dir; |
| 1260 | u64 cnt = 0; |
| 1261 | int ret = 0; |
| 1262 | |
| 1263 | scnprintf(path, PATH_MAX, "%s/devices/system/node/", |
| 1264 | sysfs__mountpoint()); |
| 1265 | |
| 1266 | dir = opendir(path); |
| 1267 | if (!dir) { |
Thomas Richter | 4f75f1cb | 2018-04-12 15:32:46 +0200 | [diff] [blame] | 1268 | pr_debug2("%s: could't read %s, does this arch have topology information?\n", |
| 1269 | __func__, path); |
Jiri Olsa | e2091ce | 2018-03-07 16:50:08 +0100 | [diff] [blame] | 1270 | return -1; |
| 1271 | } |
| 1272 | |
| 1273 | while (!ret && (ent = readdir(dir))) { |
| 1274 | unsigned int idx; |
| 1275 | int r; |
| 1276 | |
| 1277 | if (!strcmp(ent->d_name, ".") || |
| 1278 | !strcmp(ent->d_name, "..")) |
| 1279 | continue; |
| 1280 | |
| 1281 | r = sscanf(ent->d_name, "node%u", &idx); |
| 1282 | if (r != 1) |
| 1283 | continue; |
| 1284 | |
| 1285 | if (WARN_ONCE(cnt >= size, |
| 1286 | "failed to write MEM_TOPOLOGY, way too many nodes\n")) |
| 1287 | return -1; |
| 1288 | |
| 1289 | ret = memory_node__read(&nodes[cnt++], idx); |
| 1290 | } |
| 1291 | |
| 1292 | *cntp = cnt; |
| 1293 | closedir(dir); |
| 1294 | |
| 1295 | if (!ret) |
| 1296 | qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort); |
| 1297 | |
| 1298 | return ret; |
| 1299 | } |
| 1300 | |
| 1301 | #define MAX_MEMORY_NODES 2000 |
| 1302 | |
| 1303 | /* |
| 1304 | * The MEM_TOPOLOGY holds physical memory map for every |
| 1305 | * node in system. The format of data is as follows: |
| 1306 | * |
| 1307 | * 0 - version | for future changes |
| 1308 | * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes |
| 1309 | * 16 - count | number of nodes |
| 1310 | * |
| 1311 | * For each node we store map of physical indexes for |
| 1312 | * each node: |
| 1313 | * |
| 1314 | * 32 - node id | node index |
| 1315 | * 40 - size | size of bitmap |
| 1316 | * 48 - bitmap | bitmap of memory indexes that belongs to node |
| 1317 | */ |
| 1318 | static int write_mem_topology(struct feat_fd *ff __maybe_unused, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 1319 | struct evlist *evlist __maybe_unused) |
Jiri Olsa | e2091ce | 2018-03-07 16:50:08 +0100 | [diff] [blame] | 1320 | { |
| 1321 | static struct memory_node nodes[MAX_MEMORY_NODES]; |
| 1322 | u64 bsize, version = 1, i, nr; |
| 1323 | int ret; |
| 1324 | |
| 1325 | ret = sysfs__read_xll("devices/system/memory/block_size_bytes", |
| 1326 | (unsigned long long *) &bsize); |
| 1327 | if (ret) |
| 1328 | return ret; |
| 1329 | |
| 1330 | ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr); |
| 1331 | if (ret) |
| 1332 | return ret; |
| 1333 | |
| 1334 | ret = do_write(ff, &version, sizeof(version)); |
| 1335 | if (ret < 0) |
| 1336 | goto out; |
| 1337 | |
| 1338 | ret = do_write(ff, &bsize, sizeof(bsize)); |
| 1339 | if (ret < 0) |
| 1340 | goto out; |
| 1341 | |
| 1342 | ret = do_write(ff, &nr, sizeof(nr)); |
| 1343 | if (ret < 0) |
| 1344 | goto out; |
| 1345 | |
| 1346 | for (i = 0; i < nr; i++) { |
| 1347 | struct memory_node *n = &nodes[i]; |
| 1348 | |
| 1349 | #define _W(v) \ |
| 1350 | ret = do_write(ff, &n->v, sizeof(n->v)); \ |
| 1351 | if (ret < 0) \ |
| 1352 | goto out; |
| 1353 | |
| 1354 | _W(node) |
| 1355 | _W(size) |
| 1356 | |
| 1357 | #undef _W |
| 1358 | |
| 1359 | ret = do_write_bitmap(ff, n->set, n->size); |
| 1360 | if (ret < 0) |
| 1361 | goto out; |
| 1362 | } |
| 1363 | |
| 1364 | out: |
| 1365 | return ret; |
| 1366 | } |
| 1367 | |
Alexey Budankov | 42e1fd8 | 2019-03-18 20:41:33 +0300 | [diff] [blame] | 1368 | static int write_compressed(struct feat_fd *ff __maybe_unused, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 1369 | struct evlist *evlist __maybe_unused) |
Alexey Budankov | 42e1fd8 | 2019-03-18 20:41:33 +0300 | [diff] [blame] | 1370 | { |
| 1371 | int ret; |
| 1372 | |
| 1373 | ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver)); |
| 1374 | if (ret) |
| 1375 | return ret; |
| 1376 | |
| 1377 | ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type)); |
| 1378 | if (ret) |
| 1379 | return ret; |
| 1380 | |
| 1381 | ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level)); |
| 1382 | if (ret) |
| 1383 | return ret; |
| 1384 | |
| 1385 | ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio)); |
| 1386 | if (ret) |
| 1387 | return ret; |
| 1388 | |
| 1389 | return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len)); |
| 1390 | } |
| 1391 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1392 | static void print_hostname(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, "# hostname : %s\n", ff->ph->env.hostname); |
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_osrelease(struct feat_fd *ff, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1398 | { |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1399 | fprintf(fp, "# os release : %s\n", ff->ph->env.os_release); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1400 | } |
| 1401 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1402 | static void print_arch(struct feat_fd *ff, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1403 | { |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1404 | fprintf(fp, "# arch : %s\n", ff->ph->env.arch); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1405 | } |
| 1406 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1407 | static void print_cpudesc(struct feat_fd *ff, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1408 | { |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1409 | fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1410 | } |
| 1411 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1412 | static void print_nrcpus(struct feat_fd *ff, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1413 | { |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1414 | fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online); |
| 1415 | fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1416 | } |
| 1417 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1418 | static void print_version(struct feat_fd *ff, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1419 | { |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1420 | fprintf(fp, "# perf version : %s\n", ff->ph->env.version); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1421 | } |
| 1422 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1423 | static void print_cmdline(struct feat_fd *ff, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1424 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1425 | int nr, i; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1426 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1427 | nr = ff->ph->env.nr_cmdline; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1428 | |
| 1429 | fprintf(fp, "# cmdline : "); |
| 1430 | |
Alexey Budankov | f92da71 | 2018-06-04 09:50:56 +0300 | [diff] [blame] | 1431 | for (i = 0; i < nr; i++) { |
| 1432 | char *argv_i = strdup(ff->ph->env.cmdline_argv[i]); |
| 1433 | if (!argv_i) { |
| 1434 | fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]); |
| 1435 | } else { |
| 1436 | char *mem = argv_i; |
| 1437 | do { |
| 1438 | char *quote = strchr(argv_i, '\''); |
| 1439 | if (!quote) |
| 1440 | break; |
| 1441 | *quote++ = '\0'; |
| 1442 | fprintf(fp, "%s\\\'", argv_i); |
| 1443 | argv_i = quote; |
| 1444 | } while (1); |
| 1445 | fprintf(fp, "%s ", argv_i); |
| 1446 | free(mem); |
| 1447 | } |
| 1448 | } |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1449 | fputc('\n', fp); |
| 1450 | } |
| 1451 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1452 | static void print_cpu_topology(struct feat_fd *ff, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1453 | { |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1454 | struct perf_header *ph = ff->ph; |
| 1455 | int cpu_nr = ph->env.nr_cpus_avail; |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1456 | int nr, i; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1457 | char *str; |
| 1458 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1459 | nr = ph->env.nr_sibling_cores; |
| 1460 | str = ph->env.sibling_cores; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1461 | |
| 1462 | for (i = 0; i < nr; i++) { |
Kan Liang | e05a899 | 2019-06-04 15:50:43 -0700 | [diff] [blame] | 1463 | fprintf(fp, "# sibling sockets : %s\n", str); |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1464 | str += strlen(str) + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1465 | } |
| 1466 | |
Kan Liang | acae8b3 | 2019-06-04 15:50:41 -0700 | [diff] [blame] | 1467 | if (ph->env.nr_sibling_dies) { |
| 1468 | nr = ph->env.nr_sibling_dies; |
| 1469 | str = ph->env.sibling_dies; |
| 1470 | |
| 1471 | for (i = 0; i < nr; i++) { |
| 1472 | fprintf(fp, "# sibling dies : %s\n", str); |
| 1473 | str += strlen(str) + 1; |
| 1474 | } |
| 1475 | } |
| 1476 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1477 | nr = ph->env.nr_sibling_threads; |
| 1478 | str = ph->env.sibling_threads; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1479 | |
| 1480 | for (i = 0; i < nr; i++) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1481 | fprintf(fp, "# sibling threads : %s\n", str); |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1482 | str += strlen(str) + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1483 | } |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 1484 | |
Kan Liang | acae8b3 | 2019-06-04 15:50:41 -0700 | [diff] [blame] | 1485 | if (ph->env.nr_sibling_dies) { |
| 1486 | if (ph->env.cpu != NULL) { |
| 1487 | for (i = 0; i < cpu_nr; i++) |
| 1488 | fprintf(fp, "# CPU %d: Core ID %d, " |
| 1489 | "Die ID %d, Socket ID %d\n", |
| 1490 | i, ph->env.cpu[i].core_id, |
| 1491 | ph->env.cpu[i].die_id, |
| 1492 | ph->env.cpu[i].socket_id); |
| 1493 | } else |
| 1494 | fprintf(fp, "# Core ID, Die ID and Socket ID " |
| 1495 | "information is not available\n"); |
| 1496 | } else { |
| 1497 | if (ph->env.cpu != NULL) { |
| 1498 | for (i = 0; i < cpu_nr; i++) |
| 1499 | fprintf(fp, "# CPU %d: Core ID %d, " |
| 1500 | "Socket ID %d\n", |
| 1501 | i, ph->env.cpu[i].core_id, |
| 1502 | ph->env.cpu[i].socket_id); |
| 1503 | } else |
| 1504 | fprintf(fp, "# Core ID and Socket ID " |
| 1505 | "information is not available\n"); |
| 1506 | } |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
Alexey Budankov | cf79051 | 2018-10-09 17:36:24 +0300 | [diff] [blame] | 1509 | static void print_clockid(struct feat_fd *ff, FILE *fp) |
| 1510 | { |
| 1511 | fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n", |
| 1512 | ff->ph->env.clockid_res_ns * 1000); |
| 1513 | } |
| 1514 | |
Jiri Olsa | 258031c | 2019-03-08 14:47:39 +0100 | [diff] [blame] | 1515 | static void print_dir_format(struct feat_fd *ff, FILE *fp) |
| 1516 | { |
| 1517 | struct perf_session *session; |
| 1518 | struct perf_data *data; |
| 1519 | |
| 1520 | session = container_of(ff->ph, struct perf_session, header); |
| 1521 | data = session->data; |
| 1522 | |
| 1523 | fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version); |
| 1524 | } |
| 1525 | |
Song Liu | 606f972 | 2019-03-11 22:30:43 -0700 | [diff] [blame] | 1526 | static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp) |
| 1527 | { |
| 1528 | struct perf_env *env = &ff->ph->env; |
| 1529 | struct rb_root *root; |
| 1530 | struct rb_node *next; |
| 1531 | |
| 1532 | down_read(&env->bpf_progs.lock); |
| 1533 | |
| 1534 | root = &env->bpf_progs.infos; |
| 1535 | next = rb_first(root); |
| 1536 | |
| 1537 | while (next) { |
| 1538 | struct bpf_prog_info_node *node; |
| 1539 | |
| 1540 | node = rb_entry(next, struct bpf_prog_info_node, rb_node); |
| 1541 | next = rb_next(&node->rb_node); |
Song Liu | f8dfeae | 2019-03-19 09:54:54 -0700 | [diff] [blame] | 1542 | |
| 1543 | bpf_event__print_bpf_prog_info(&node->info_linear->info, |
| 1544 | env, fp); |
Song Liu | 606f972 | 2019-03-11 22:30:43 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | up_read(&env->bpf_progs.lock); |
| 1548 | } |
| 1549 | |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 1550 | static void print_bpf_btf(struct feat_fd *ff, FILE *fp) |
| 1551 | { |
| 1552 | struct perf_env *env = &ff->ph->env; |
| 1553 | struct rb_root *root; |
| 1554 | struct rb_node *next; |
| 1555 | |
| 1556 | down_read(&env->bpf_progs.lock); |
| 1557 | |
| 1558 | root = &env->bpf_progs.btfs; |
| 1559 | next = rb_first(root); |
| 1560 | |
| 1561 | while (next) { |
| 1562 | struct btf_node *node; |
| 1563 | |
| 1564 | node = rb_entry(next, struct btf_node, rb_node); |
| 1565 | next = rb_next(&node->rb_node); |
| 1566 | fprintf(fp, "# btf info of id %u\n", node->id); |
| 1567 | } |
| 1568 | |
| 1569 | up_read(&env->bpf_progs.lock); |
| 1570 | } |
| 1571 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1572 | static void free_event_desc(struct evsel *events) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1573 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1574 | struct evsel *evsel; |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1575 | |
| 1576 | if (!events) |
| 1577 | return; |
| 1578 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 1579 | for (evsel = events; evsel->core.attr.size; evsel++) { |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 1580 | zfree(&evsel->name); |
| 1581 | zfree(&evsel->id); |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | free(events); |
| 1585 | } |
| 1586 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1587 | static struct evsel *read_event_desc(struct feat_fd *ff) |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1588 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1589 | struct evsel *evsel, *events = NULL; |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1590 | u64 *id; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1591 | void *buf = NULL; |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1592 | u32 nre, sz, nr, i, j; |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1593 | size_t msz; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1594 | |
| 1595 | /* number of events */ |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 1596 | if (do_read_u32(ff, &nre)) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1597 | goto error; |
| 1598 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 1599 | if (do_read_u32(ff, &sz)) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1600 | goto error; |
| 1601 | |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1602 | /* buffer to hold on file attr struct */ |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1603 | buf = malloc(sz); |
| 1604 | if (!buf) |
| 1605 | goto error; |
| 1606 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 1607 | /* the last event terminates with evsel->core.attr.size == 0: */ |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1608 | events = calloc(nre + 1, sizeof(*events)); |
| 1609 | if (!events) |
| 1610 | goto error; |
| 1611 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 1612 | msz = sizeof(evsel->core.attr); |
Jiri Olsa | 9fafd98 | 2012-03-20 19:15:39 +0100 | [diff] [blame] | 1613 | if (sz < msz) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1614 | msz = sz; |
| 1615 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1616 | for (i = 0, evsel = events; i < nre; evsel++, i++) { |
| 1617 | evsel->idx = i; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1618 | |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1619 | /* |
| 1620 | * must read entire on-file attr struct to |
| 1621 | * sync up with layout. |
| 1622 | */ |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 1623 | if (__do_read(ff, buf, sz)) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1624 | goto error; |
| 1625 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 1626 | if (ff->ph->needs_swap) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1627 | perf_event__attr_swap(buf); |
| 1628 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 1629 | memcpy(&evsel->core.attr, buf, msz); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1630 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 1631 | if (do_read_u32(ff, &nr)) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1632 | goto error; |
| 1633 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 1634 | if (ff->ph->needs_swap) |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1635 | evsel->needs_swap = true; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1636 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 1637 | evsel->name = do_read_string(ff); |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 1638 | if (!evsel->name) |
| 1639 | goto error; |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1640 | |
| 1641 | if (!nr) |
| 1642 | continue; |
| 1643 | |
| 1644 | id = calloc(nr, sizeof(*id)); |
| 1645 | if (!id) |
| 1646 | goto error; |
| 1647 | evsel->ids = nr; |
| 1648 | evsel->id = id; |
| 1649 | |
| 1650 | for (j = 0 ; j < nr; j++) { |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 1651 | if (do_read_u64(ff, id)) |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1652 | goto error; |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1653 | id++; |
| 1654 | } |
| 1655 | } |
| 1656 | out: |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1657 | free(buf); |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1658 | return events; |
| 1659 | error: |
Markus Elfring | 4cc9761 | 2015-06-25 17:12:32 +0200 | [diff] [blame] | 1660 | free_event_desc(events); |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1661 | events = NULL; |
| 1662 | goto out; |
| 1663 | } |
| 1664 | |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1665 | 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] | 1666 | void *priv __maybe_unused) |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1667 | { |
| 1668 | return fprintf(fp, ", %s = %s", name, val); |
| 1669 | } |
| 1670 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1671 | static void print_event_desc(struct feat_fd *ff, FILE *fp) |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1672 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1673 | struct evsel *evsel, *events; |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1674 | u32 j; |
| 1675 | u64 *id; |
| 1676 | |
David Carrillo-Cisneros | f9ebdcc | 2017-07-17 21:25:49 -0700 | [diff] [blame] | 1677 | if (ff->events) |
| 1678 | events = ff->events; |
| 1679 | else |
| 1680 | events = read_event_desc(ff); |
| 1681 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1682 | if (!events) { |
| 1683 | fprintf(fp, "# event desc: not available or unable to read\n"); |
| 1684 | return; |
| 1685 | } |
| 1686 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 1687 | for (evsel = events; evsel->core.attr.size; evsel++) { |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1688 | fprintf(fp, "# event : name = %s, ", evsel->name); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1689 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1690 | if (evsel->ids) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1691 | fprintf(fp, ", id = {"); |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1692 | for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) { |
| 1693 | if (j) |
| 1694 | fputc(',', fp); |
| 1695 | fprintf(fp, " %"PRIu64, *id); |
| 1696 | } |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1697 | fprintf(fp, " }"); |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1698 | } |
Peter Zijlstra | 814c8c3 | 2015-03-31 00:19:31 +0200 | [diff] [blame] | 1699 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 1700 | perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL); |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1701 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1702 | fputc('\n', fp); |
| 1703 | } |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1704 | |
| 1705 | free_event_desc(events); |
David Carrillo-Cisneros | f9ebdcc | 2017-07-17 21:25:49 -0700 | [diff] [blame] | 1706 | ff->events = NULL; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1707 | } |
| 1708 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1709 | static void print_total_mem(struct feat_fd *ff, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1710 | { |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1711 | fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1712 | } |
| 1713 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1714 | static void print_numa_topology(struct feat_fd *ff, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1715 | { |
Jiri Olsa | c60da22 | 2016-07-04 14:16:20 +0200 | [diff] [blame] | 1716 | int i; |
| 1717 | struct numa_node *n; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1718 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1719 | for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) { |
| 1720 | n = &ff->ph->env.numa_nodes[i]; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1721 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1722 | fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," |
| 1723 | " free = %"PRIu64" kB\n", |
Jiri Olsa | c60da22 | 2016-07-04 14:16:20 +0200 | [diff] [blame] | 1724 | n->node, n->mem_total, n->mem_free); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1725 | |
Jiri Olsa | c60da22 | 2016-07-04 14:16:20 +0200 | [diff] [blame] | 1726 | fprintf(fp, "# node%u cpu list : ", n->node); |
| 1727 | cpu_map__fprintf(n->map, fp); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1728 | } |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1729 | } |
| 1730 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1731 | static void print_cpuid(struct feat_fd *ff, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1732 | { |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1733 | fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1734 | } |
| 1735 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1736 | 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] | 1737 | { |
| 1738 | fprintf(fp, "# contains samples with branch stack\n"); |
| 1739 | } |
| 1740 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1741 | static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp) |
Adrian Hunter | 4025ea4 | 2015-04-09 18:53:41 +0300 | [diff] [blame] | 1742 | { |
| 1743 | fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n"); |
| 1744 | } |
| 1745 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1746 | static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp) |
Jiri Olsa | ffa517a | 2015-10-25 15:51:43 +0100 | [diff] [blame] | 1747 | { |
| 1748 | fprintf(fp, "# contains stat data\n"); |
| 1749 | } |
| 1750 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1751 | static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused) |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1752 | { |
| 1753 | int i; |
| 1754 | |
| 1755 | fprintf(fp, "# CPU cache info:\n"); |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1756 | for (i = 0; i < ff->ph->env.caches_cnt; i++) { |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1757 | fprintf(fp, "# "); |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1758 | cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]); |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 1759 | } |
| 1760 | } |
| 1761 | |
Alexey Budankov | 42e1fd8 | 2019-03-18 20:41:33 +0300 | [diff] [blame] | 1762 | static void print_compressed(struct feat_fd *ff, FILE *fp) |
| 1763 | { |
| 1764 | fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n", |
| 1765 | ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown", |
| 1766 | ff->ph->env.comp_level, ff->ph->env.comp_ratio); |
| 1767 | } |
| 1768 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1769 | static void print_pmu_mappings(struct feat_fd *ff, FILE *fp) |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1770 | { |
| 1771 | const char *delimiter = "# pmu mappings: "; |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1772 | char *str, *tmp; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1773 | u32 pmu_num; |
| 1774 | u32 type; |
| 1775 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1776 | pmu_num = ff->ph->env.nr_pmu_mappings; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1777 | if (!pmu_num) { |
| 1778 | fprintf(fp, "# pmu mappings: not available\n"); |
| 1779 | return; |
| 1780 | } |
| 1781 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1782 | str = ff->ph->env.pmu_mappings; |
Namhyung Kim | be4a2de | 2012-09-05 14:02:49 +0900 | [diff] [blame] | 1783 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1784 | while (pmu_num) { |
| 1785 | type = strtoul(str, &tmp, 0); |
| 1786 | if (*tmp != ':') |
| 1787 | goto error; |
| 1788 | |
| 1789 | str = tmp + 1; |
| 1790 | fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type); |
| 1791 | |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1792 | delimiter = ", "; |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1793 | str += strlen(str) + 1; |
| 1794 | pmu_num--; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | fprintf(fp, "\n"); |
| 1798 | |
| 1799 | if (!pmu_num) |
| 1800 | return; |
| 1801 | error: |
| 1802 | fprintf(fp, "# pmu mappings: unable to read\n"); |
| 1803 | } |
| 1804 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1805 | static void print_group_desc(struct feat_fd *ff, FILE *fp) |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 1806 | { |
| 1807 | struct perf_session *session; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1808 | struct evsel *evsel; |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 1809 | u32 nr = 0; |
| 1810 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 1811 | session = container_of(ff->ph, struct perf_session, header); |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 1812 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1813 | evlist__for_each_entry(session->evlist, evsel) { |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 1814 | if (perf_evsel__is_group_leader(evsel) && |
| 1815 | evsel->nr_members > 1) { |
| 1816 | fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", |
| 1817 | perf_evsel__name(evsel)); |
| 1818 | |
| 1819 | nr = evsel->nr_members - 1; |
| 1820 | } else if (nr) { |
| 1821 | fprintf(fp, ",%s", perf_evsel__name(evsel)); |
| 1822 | |
| 1823 | if (--nr == 0) |
| 1824 | fprintf(fp, "}\n"); |
| 1825 | } |
| 1826 | } |
| 1827 | } |
| 1828 | |
Jin Yao | 6011518 | 2017-12-08 21:13:41 +0800 | [diff] [blame] | 1829 | static void print_sample_time(struct feat_fd *ff, FILE *fp) |
| 1830 | { |
| 1831 | struct perf_session *session; |
| 1832 | char time_buf[32]; |
| 1833 | double d; |
| 1834 | |
| 1835 | session = container_of(ff->ph, struct perf_session, header); |
| 1836 | |
| 1837 | timestamp__scnprintf_usec(session->evlist->first_sample_time, |
| 1838 | time_buf, sizeof(time_buf)); |
| 1839 | fprintf(fp, "# time of first sample : %s\n", time_buf); |
| 1840 | |
| 1841 | timestamp__scnprintf_usec(session->evlist->last_sample_time, |
| 1842 | time_buf, sizeof(time_buf)); |
| 1843 | fprintf(fp, "# time of last sample : %s\n", time_buf); |
| 1844 | |
| 1845 | d = (double)(session->evlist->last_sample_time - |
| 1846 | session->evlist->first_sample_time) / NSEC_PER_MSEC; |
| 1847 | |
| 1848 | fprintf(fp, "# sample duration : %10.3f ms\n", d); |
| 1849 | } |
| 1850 | |
Jiri Olsa | e2091ce | 2018-03-07 16:50:08 +0100 | [diff] [blame] | 1851 | static void memory_node__fprintf(struct memory_node *n, |
| 1852 | unsigned long long bsize, FILE *fp) |
| 1853 | { |
| 1854 | char buf_map[100], buf_size[50]; |
| 1855 | unsigned long long size; |
| 1856 | |
| 1857 | size = bsize * bitmap_weight(n->set, n->size); |
| 1858 | unit_number__scnprintf(buf_size, 50, size); |
| 1859 | |
| 1860 | bitmap_scnprintf(n->set, n->size, buf_map, 100); |
| 1861 | fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map); |
| 1862 | } |
| 1863 | |
| 1864 | static void print_mem_topology(struct feat_fd *ff, FILE *fp) |
| 1865 | { |
| 1866 | struct memory_node *nodes; |
| 1867 | int i, nr; |
| 1868 | |
| 1869 | nodes = ff->ph->env.memory_nodes; |
| 1870 | nr = ff->ph->env.nr_memory_nodes; |
| 1871 | |
| 1872 | fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n", |
| 1873 | nr, ff->ph->env.memory_bsize); |
| 1874 | |
| 1875 | for (i = 0; i < nr; i++) { |
| 1876 | memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp); |
| 1877 | } |
| 1878 | } |
| 1879 | |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1880 | static int __event_process_build_id(struct build_id_event *bev, |
| 1881 | char *filename, |
| 1882 | struct perf_session *session) |
| 1883 | { |
| 1884 | int err = -1; |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1885 | struct machine *machine; |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 1886 | u16 cpumode; |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1887 | struct dso *dso; |
| 1888 | enum dso_kernel_type dso_type; |
| 1889 | |
| 1890 | machine = perf_session__findnew_machine(session, bev->pid); |
| 1891 | if (!machine) |
| 1892 | goto out; |
| 1893 | |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 1894 | cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1895 | |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 1896 | switch (cpumode) { |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1897 | case PERF_RECORD_MISC_KERNEL: |
| 1898 | dso_type = DSO_TYPE_KERNEL; |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1899 | break; |
| 1900 | case PERF_RECORD_MISC_GUEST_KERNEL: |
| 1901 | dso_type = DSO_TYPE_GUEST_KERNEL; |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1902 | break; |
| 1903 | case PERF_RECORD_MISC_USER: |
| 1904 | case PERF_RECORD_MISC_GUEST_USER: |
| 1905 | dso_type = DSO_TYPE_USER; |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1906 | break; |
| 1907 | default: |
| 1908 | goto out; |
| 1909 | } |
| 1910 | |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 1911 | dso = machine__findnew_dso(machine, filename); |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1912 | if (dso != NULL) { |
Masami Hiramatsu | b5d8bbe | 2016-05-11 22:51:59 +0900 | [diff] [blame] | 1913 | char sbuild_id[SBUILD_ID_SIZE]; |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1914 | |
| 1915 | dso__set_build_id(dso, &bev->build_id); |
| 1916 | |
Namhyung Kim | 1deec1b | 2017-05-31 21:01:03 +0900 | [diff] [blame] | 1917 | if (dso_type != DSO_TYPE_USER) { |
| 1918 | struct kmod_path m = { .name = NULL, }; |
| 1919 | |
| 1920 | if (!kmod_path__parse_name(&m, filename) && m.kmod) |
Namhyung Kim | 6b335e8 | 2017-05-31 21:01:04 +0900 | [diff] [blame] | 1921 | dso__set_module_info(dso, &m, machine); |
Namhyung Kim | 1deec1b | 2017-05-31 21:01:03 +0900 | [diff] [blame] | 1922 | else |
| 1923 | dso->kernel = dso_type; |
| 1924 | |
| 1925 | free(m.name); |
| 1926 | } |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1927 | |
| 1928 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), |
| 1929 | sbuild_id); |
| 1930 | pr_debug("build id event received for %s: %s\n", |
| 1931 | dso->long_name, sbuild_id); |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1932 | dso__put(dso); |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | err = 0; |
| 1936 | out: |
| 1937 | return err; |
| 1938 | } |
| 1939 | |
| 1940 | static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, |
| 1941 | int input, u64 offset, u64 size) |
| 1942 | { |
| 1943 | struct perf_session *session = container_of(header, struct perf_session, header); |
| 1944 | struct { |
| 1945 | struct perf_event_header header; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 1946 | u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))]; |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1947 | char filename[0]; |
| 1948 | } old_bev; |
| 1949 | struct build_id_event bev; |
| 1950 | char filename[PATH_MAX]; |
| 1951 | u64 limit = offset + size; |
| 1952 | |
| 1953 | while (offset < limit) { |
| 1954 | ssize_t len; |
| 1955 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1956 | if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1957 | return -1; |
| 1958 | |
| 1959 | if (header->needs_swap) |
| 1960 | perf_event_header__bswap(&old_bev.header); |
| 1961 | |
| 1962 | len = old_bev.header.size - sizeof(old_bev); |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1963 | if (readn(input, filename, len) != len) |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1964 | return -1; |
| 1965 | |
| 1966 | bev.header = old_bev.header; |
| 1967 | |
| 1968 | /* |
| 1969 | * As the pid is the missing value, we need to fill |
| 1970 | * it properly. The header.misc value give us nice hint. |
| 1971 | */ |
| 1972 | bev.pid = HOST_KERNEL_ID; |
| 1973 | if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || |
| 1974 | bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) |
| 1975 | bev.pid = DEFAULT_GUEST_KERNEL_ID; |
| 1976 | |
| 1977 | memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); |
| 1978 | __event_process_build_id(&bev, filename, session); |
| 1979 | |
| 1980 | offset += bev.header.size; |
| 1981 | } |
| 1982 | |
| 1983 | return 0; |
| 1984 | } |
| 1985 | |
| 1986 | static int perf_header__read_build_ids(struct perf_header *header, |
| 1987 | int input, u64 offset, u64 size) |
| 1988 | { |
| 1989 | struct perf_session *session = container_of(header, struct perf_session, header); |
| 1990 | struct build_id_event bev; |
| 1991 | char filename[PATH_MAX]; |
| 1992 | u64 limit = offset + size, orig_offset = offset; |
| 1993 | int err = -1; |
| 1994 | |
| 1995 | while (offset < limit) { |
| 1996 | ssize_t len; |
| 1997 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1998 | if (readn(input, &bev, sizeof(bev)) != sizeof(bev)) |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1999 | goto out; |
| 2000 | |
| 2001 | if (header->needs_swap) |
| 2002 | perf_event_header__bswap(&bev.header); |
| 2003 | |
| 2004 | len = bev.header.size - sizeof(bev); |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 2005 | if (readn(input, filename, len) != len) |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 2006 | goto out; |
| 2007 | /* |
| 2008 | * The a1645ce1 changeset: |
| 2009 | * |
| 2010 | * "perf: 'perf kvm' tool for monitoring guest performance from host" |
| 2011 | * |
| 2012 | * Added a field to struct build_id_event that broke the file |
| 2013 | * format. |
| 2014 | * |
| 2015 | * Since the kernel build-id is the first entry, process the |
| 2016 | * table using the old format if the well known |
| 2017 | * '[kernel.kallsyms]' string for the kernel build-id has the |
| 2018 | * first 4 characters chopped off (where the pid_t sits). |
| 2019 | */ |
| 2020 | if (memcmp(filename, "nel.kallsyms]", 13) == 0) { |
| 2021 | if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) |
| 2022 | return -1; |
| 2023 | return perf_header__read_build_ids_abi_quirk(header, input, offset, size); |
| 2024 | } |
| 2025 | |
| 2026 | __event_process_build_id(&bev, filename, session); |
| 2027 | |
| 2028 | offset += bev.header.size; |
| 2029 | } |
| 2030 | err = 0; |
| 2031 | out: |
| 2032 | return err; |
| 2033 | } |
| 2034 | |
David Carrillo-Cisneros | dfaa158 | 2017-07-17 21:25:35 -0700 | [diff] [blame] | 2035 | /* Macro for features that simply need to read and store a string. */ |
| 2036 | #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \ |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2037 | 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] | 2038 | {\ |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2039 | ff->ph->env.__feat_env = do_read_string(ff); \ |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2040 | return ff->ph->env.__feat_env ? 0 : -ENOMEM; \ |
David Carrillo-Cisneros | dfaa158 | 2017-07-17 21:25:35 -0700 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | FEAT_PROCESS_STR_FUN(hostname, hostname); |
| 2044 | FEAT_PROCESS_STR_FUN(osrelease, os_release); |
| 2045 | FEAT_PROCESS_STR_FUN(version, version); |
| 2046 | FEAT_PROCESS_STR_FUN(arch, arch); |
| 2047 | FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc); |
| 2048 | FEAT_PROCESS_STR_FUN(cpuid, cpuid); |
| 2049 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2050 | static int process_tracing_data(struct feat_fd *ff, void *data) |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 2051 | { |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2052 | ssize_t ret = trace_report(ff->fd, data, false); |
| 2053 | |
Namhyung Kim | 3dce2ce | 2013-03-21 16:18:48 +0900 | [diff] [blame] | 2054 | return ret < 0 ? -1 : 0; |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 2055 | } |
| 2056 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2057 | 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] | 2058 | { |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2059 | 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] | 2060 | pr_debug("Failed to read buildids, continuing...\n"); |
| 2061 | return 0; |
| 2062 | } |
| 2063 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2064 | static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2065 | { |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 2066 | int ret; |
| 2067 | u32 nr_cpus_avail, nr_cpus_online; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2068 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2069 | ret = do_read_u32(ff, &nr_cpus_avail); |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 2070 | if (ret) |
| 2071 | return ret; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2072 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2073 | ret = do_read_u32(ff, &nr_cpus_online); |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 2074 | if (ret) |
| 2075 | return ret; |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2076 | ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail; |
| 2077 | ff->ph->env.nr_cpus_online = (int)nr_cpus_online; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2078 | return 0; |
| 2079 | } |
| 2080 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2081 | 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] | 2082 | { |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 2083 | u64 total_mem; |
| 2084 | int ret; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2085 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2086 | ret = do_read_u64(ff, &total_mem); |
David Carrillo-Cisneros | 6200e494 | 2017-07-17 21:25:34 -0700 | [diff] [blame] | 2087 | if (ret) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2088 | return -1; |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2089 | ff->ph->env.total_mem = (unsigned long long)total_mem; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2090 | return 0; |
| 2091 | } |
| 2092 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2093 | static struct evsel * |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 2094 | perf_evlist__find_by_index(struct evlist *evlist, int idx) |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 2095 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2096 | struct evsel *evsel; |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 2097 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 2098 | evlist__for_each_entry(evlist, evsel) { |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 2099 | if (evsel->idx == idx) |
| 2100 | return evsel; |
| 2101 | } |
| 2102 | |
| 2103 | return NULL; |
| 2104 | } |
| 2105 | |
| 2106 | static void |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 2107 | perf_evlist__set_event_name(struct evlist *evlist, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2108 | struct evsel *event) |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 2109 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2110 | struct evsel *evsel; |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 2111 | |
| 2112 | if (!event->name) |
| 2113 | return; |
| 2114 | |
| 2115 | evsel = perf_evlist__find_by_index(evlist, event->idx); |
| 2116 | if (!evsel) |
| 2117 | return; |
| 2118 | |
| 2119 | if (evsel->name) |
| 2120 | return; |
| 2121 | |
| 2122 | evsel->name = strdup(event->name); |
| 2123 | } |
| 2124 | |
| 2125 | static int |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2126 | process_event_desc(struct feat_fd *ff, void *data __maybe_unused) |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 2127 | { |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 2128 | struct perf_session *session; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2129 | struct evsel *evsel, *events = read_event_desc(ff); |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 2130 | |
| 2131 | if (!events) |
| 2132 | return 0; |
| 2133 | |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2134 | session = container_of(ff->ph, struct perf_session, header); |
David Carrillo-Cisneros | f9ebdcc | 2017-07-17 21:25:49 -0700 | [diff] [blame] | 2135 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2136 | if (session->data->is_pipe) { |
David Carrillo-Cisneros | f9ebdcc | 2017-07-17 21:25:49 -0700 | [diff] [blame] | 2137 | /* Save events for reading later by print_event_desc, |
| 2138 | * since they can't be read again in pipe mode. */ |
| 2139 | ff->events = events; |
| 2140 | } |
| 2141 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 2142 | for (evsel = events; evsel->core.attr.size; evsel++) |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 2143 | perf_evlist__set_event_name(session->evlist, evsel); |
| 2144 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2145 | if (!session->data->is_pipe) |
David Carrillo-Cisneros | f9ebdcc | 2017-07-17 21:25:49 -0700 | [diff] [blame] | 2146 | free_event_desc(events); |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 2147 | |
| 2148 | return 0; |
| 2149 | } |
| 2150 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2151 | static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2152 | { |
Jiri Olsa | 768dd3f | 2015-07-21 14:31:31 +0200 | [diff] [blame] | 2153 | char *str, *cmdline = NULL, **argv = NULL; |
| 2154 | u32 nr, i, len = 0; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2155 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2156 | if (do_read_u32(ff, &nr)) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2157 | return -1; |
| 2158 | |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2159 | ff->ph->env.nr_cmdline = nr; |
Jiri Olsa | 768dd3f | 2015-07-21 14:31:31 +0200 | [diff] [blame] | 2160 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2161 | cmdline = zalloc(ff->size + nr + 1); |
Jiri Olsa | 768dd3f | 2015-07-21 14:31:31 +0200 | [diff] [blame] | 2162 | if (!cmdline) |
| 2163 | return -1; |
| 2164 | |
| 2165 | argv = zalloc(sizeof(char *) * (nr + 1)); |
| 2166 | if (!argv) |
| 2167 | goto error; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2168 | |
| 2169 | for (i = 0; i < nr; i++) { |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2170 | str = do_read_string(ff); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2171 | if (!str) |
| 2172 | goto error; |
| 2173 | |
Jiri Olsa | 768dd3f | 2015-07-21 14:31:31 +0200 | [diff] [blame] | 2174 | argv[i] = cmdline + len; |
| 2175 | memcpy(argv[i], str, strlen(str) + 1); |
| 2176 | len += strlen(str) + 1; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2177 | free(str); |
| 2178 | } |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2179 | ff->ph->env.cmdline = cmdline; |
| 2180 | ff->ph->env.cmdline_argv = (const char **) argv; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2181 | return 0; |
| 2182 | |
| 2183 | error: |
Jiri Olsa | 768dd3f | 2015-07-21 14:31:31 +0200 | [diff] [blame] | 2184 | free(argv); |
| 2185 | free(cmdline); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2186 | return -1; |
| 2187 | } |
| 2188 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2189 | 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] | 2190 | { |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2191 | u32 nr, i; |
| 2192 | char *str; |
| 2193 | struct strbuf sb; |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2194 | int cpu_nr = ff->ph->env.nr_cpus_avail; |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2195 | u64 size = 0; |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2196 | struct perf_header *ph = ff->ph; |
Thomas Richter | 0176622 | 2018-06-11 09:31:52 +0200 | [diff] [blame] | 2197 | bool do_core_id_test = true; |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2198 | |
| 2199 | ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu)); |
| 2200 | if (!ph->env.cpu) |
| 2201 | return -1; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2202 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2203 | if (do_read_u32(ff, &nr)) |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2204 | goto free_cpu; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2205 | |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2206 | ph->env.nr_sibling_cores = nr; |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2207 | size += sizeof(u32); |
Masami Hiramatsu | 642aada | 2016-05-10 14:47:35 +0900 | [diff] [blame] | 2208 | if (strbuf_init(&sb, 128) < 0) |
| 2209 | goto free_cpu; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2210 | |
| 2211 | for (i = 0; i < nr; i++) { |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2212 | str = do_read_string(ff); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2213 | if (!str) |
| 2214 | goto error; |
| 2215 | |
| 2216 | /* include a NULL character at the end */ |
Masami Hiramatsu | 642aada | 2016-05-10 14:47:35 +0900 | [diff] [blame] | 2217 | if (strbuf_add(&sb, str, strlen(str) + 1) < 0) |
| 2218 | goto error; |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2219 | size += string_size(str); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2220 | free(str); |
| 2221 | } |
| 2222 | ph->env.sibling_cores = strbuf_detach(&sb, NULL); |
| 2223 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2224 | if (do_read_u32(ff, &nr)) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2225 | return -1; |
| 2226 | |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2227 | ph->env.nr_sibling_threads = nr; |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2228 | size += sizeof(u32); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2229 | |
| 2230 | for (i = 0; i < nr; i++) { |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2231 | str = do_read_string(ff); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2232 | if (!str) |
| 2233 | goto error; |
| 2234 | |
| 2235 | /* include a NULL character at the end */ |
Masami Hiramatsu | 642aada | 2016-05-10 14:47:35 +0900 | [diff] [blame] | 2236 | if (strbuf_add(&sb, str, strlen(str) + 1) < 0) |
| 2237 | goto error; |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2238 | size += string_size(str); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2239 | free(str); |
| 2240 | } |
| 2241 | ph->env.sibling_threads = strbuf_detach(&sb, NULL); |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2242 | |
| 2243 | /* |
| 2244 | * The header may be from old perf, |
| 2245 | * which doesn't include core id and socket id information. |
| 2246 | */ |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2247 | if (ff->size <= size) { |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2248 | zfree(&ph->env.cpu); |
| 2249 | return 0; |
| 2250 | } |
| 2251 | |
Thomas Richter | 0176622 | 2018-06-11 09:31:52 +0200 | [diff] [blame] | 2252 | /* On s390 the socket_id number is not related to the numbers of cpus. |
| 2253 | * The socket_id number might be higher than the numbers of cpus. |
| 2254 | * This depends on the configuration. |
| 2255 | */ |
| 2256 | if (ph->env.arch && !strncmp(ph->env.arch, "s390", 4)) |
| 2257 | do_core_id_test = false; |
| 2258 | |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2259 | for (i = 0; i < (u32)cpu_nr; i++) { |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2260 | if (do_read_u32(ff, &nr)) |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2261 | goto free_cpu; |
| 2262 | |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2263 | ph->env.cpu[i].core_id = nr; |
Kan Liang | acae8b3 | 2019-06-04 15:50:41 -0700 | [diff] [blame] | 2264 | size += sizeof(u32); |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2265 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2266 | if (do_read_u32(ff, &nr)) |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2267 | goto free_cpu; |
| 2268 | |
Thomas Richter | 0176622 | 2018-06-11 09:31:52 +0200 | [diff] [blame] | 2269 | if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) { |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2270 | pr_debug("socket_id number is too big." |
| 2271 | "You may need to upgrade the perf tool.\n"); |
| 2272 | goto free_cpu; |
| 2273 | } |
| 2274 | |
| 2275 | ph->env.cpu[i].socket_id = nr; |
Kan Liang | acae8b3 | 2019-06-04 15:50:41 -0700 | [diff] [blame] | 2276 | size += sizeof(u32); |
| 2277 | } |
| 2278 | |
| 2279 | /* |
| 2280 | * The header may be from old perf, |
| 2281 | * which doesn't include die information. |
| 2282 | */ |
| 2283 | if (ff->size <= size) |
| 2284 | return 0; |
| 2285 | |
| 2286 | if (do_read_u32(ff, &nr)) |
| 2287 | return -1; |
| 2288 | |
| 2289 | ph->env.nr_sibling_dies = nr; |
| 2290 | size += sizeof(u32); |
| 2291 | |
| 2292 | for (i = 0; i < nr; i++) { |
| 2293 | str = do_read_string(ff); |
| 2294 | if (!str) |
| 2295 | goto error; |
| 2296 | |
| 2297 | /* include a NULL character at the end */ |
| 2298 | if (strbuf_add(&sb, str, strlen(str) + 1) < 0) |
| 2299 | goto error; |
| 2300 | size += string_size(str); |
| 2301 | free(str); |
| 2302 | } |
| 2303 | ph->env.sibling_dies = strbuf_detach(&sb, NULL); |
| 2304 | |
| 2305 | for (i = 0; i < (u32)cpu_nr; i++) { |
| 2306 | if (do_read_u32(ff, &nr)) |
| 2307 | goto free_cpu; |
| 2308 | |
| 2309 | ph->env.cpu[i].die_id = nr; |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2310 | } |
| 2311 | |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2312 | return 0; |
| 2313 | |
| 2314 | error: |
| 2315 | strbuf_release(&sb); |
Kan Liang | 2bb00d2 | 2015-09-01 09:58:12 -0400 | [diff] [blame] | 2316 | free_cpu: |
| 2317 | zfree(&ph->env.cpu); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2318 | return -1; |
| 2319 | } |
| 2320 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2321 | 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] | 2322 | { |
Jiri Olsa | c60da22 | 2016-07-04 14:16:20 +0200 | [diff] [blame] | 2323 | struct numa_node *nodes, *n; |
Jiri Olsa | c60da22 | 2016-07-04 14:16:20 +0200 | [diff] [blame] | 2324 | u32 nr, i; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2325 | char *str; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2326 | |
| 2327 | /* nr nodes */ |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2328 | if (do_read_u32(ff, &nr)) |
Masami Hiramatsu | 642aada | 2016-05-10 14:47:35 +0900 | [diff] [blame] | 2329 | return -1; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2330 | |
Jiri Olsa | c60da22 | 2016-07-04 14:16:20 +0200 | [diff] [blame] | 2331 | nodes = zalloc(sizeof(*nodes) * nr); |
| 2332 | if (!nodes) |
| 2333 | return -ENOMEM; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2334 | |
| 2335 | for (i = 0; i < nr; i++) { |
Jiri Olsa | c60da22 | 2016-07-04 14:16:20 +0200 | [diff] [blame] | 2336 | n = &nodes[i]; |
| 2337 | |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2338 | /* node number */ |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2339 | if (do_read_u32(ff, &n->node)) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2340 | goto error; |
| 2341 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2342 | if (do_read_u64(ff, &n->mem_total)) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2343 | goto error; |
| 2344 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2345 | if (do_read_u64(ff, &n->mem_free)) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2346 | goto error; |
| 2347 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2348 | str = do_read_string(ff); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2349 | if (!str) |
| 2350 | goto error; |
| 2351 | |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame^] | 2352 | n->map = perf_cpu_map__new(str); |
Jiri Olsa | c60da22 | 2016-07-04 14:16:20 +0200 | [diff] [blame] | 2353 | if (!n->map) |
Masami Hiramatsu | 642aada | 2016-05-10 14:47:35 +0900 | [diff] [blame] | 2354 | goto error; |
Jiri Olsa | c60da22 | 2016-07-04 14:16:20 +0200 | [diff] [blame] | 2355 | |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2356 | free(str); |
| 2357 | } |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2358 | ff->ph->env.nr_numa_nodes = nr; |
| 2359 | ff->ph->env.numa_nodes = nodes; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2360 | return 0; |
| 2361 | |
| 2362 | error: |
Jiri Olsa | c60da22 | 2016-07-04 14:16:20 +0200 | [diff] [blame] | 2363 | free(nodes); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2364 | return -1; |
| 2365 | } |
| 2366 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2367 | 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] | 2368 | { |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2369 | char *name; |
| 2370 | u32 pmu_num; |
| 2371 | u32 type; |
| 2372 | struct strbuf sb; |
| 2373 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2374 | if (do_read_u32(ff, &pmu_num)) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2375 | return -1; |
| 2376 | |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2377 | if (!pmu_num) { |
| 2378 | pr_debug("pmu mappings not available\n"); |
| 2379 | return 0; |
| 2380 | } |
| 2381 | |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2382 | ff->ph->env.nr_pmu_mappings = pmu_num; |
Masami Hiramatsu | 642aada | 2016-05-10 14:47:35 +0900 | [diff] [blame] | 2383 | if (strbuf_init(&sb, 128) < 0) |
| 2384 | return -1; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2385 | |
| 2386 | while (pmu_num) { |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2387 | if (do_read_u32(ff, &type)) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2388 | goto error; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2389 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2390 | name = do_read_string(ff); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2391 | if (!name) |
| 2392 | goto error; |
| 2393 | |
Masami Hiramatsu | 642aada | 2016-05-10 14:47:35 +0900 | [diff] [blame] | 2394 | if (strbuf_addf(&sb, "%u:%s", type, name) < 0) |
| 2395 | goto error; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2396 | /* include a NULL character at the end */ |
Masami Hiramatsu | 642aada | 2016-05-10 14:47:35 +0900 | [diff] [blame] | 2397 | if (strbuf_add(&sb, "", 1) < 0) |
| 2398 | goto error; |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2399 | |
Kan Liang | e0838e0 | 2015-09-10 11:03:05 -0300 | [diff] [blame] | 2400 | if (!strcmp(name, "msr")) |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2401 | ff->ph->env.msr_pmu_type = type; |
Kan Liang | e0838e0 | 2015-09-10 11:03:05 -0300 | [diff] [blame] | 2402 | |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2403 | free(name); |
| 2404 | pmu_num--; |
| 2405 | } |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2406 | ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2407 | return 0; |
| 2408 | |
| 2409 | error: |
| 2410 | strbuf_release(&sb); |
| 2411 | return -1; |
| 2412 | } |
| 2413 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2414 | 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] | 2415 | { |
| 2416 | size_t ret = -1; |
| 2417 | u32 i, nr, nr_groups; |
| 2418 | struct perf_session *session; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2419 | struct evsel *evsel, *leader = NULL; |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 2420 | struct group_desc { |
| 2421 | char *name; |
| 2422 | u32 leader_idx; |
| 2423 | u32 nr_members; |
| 2424 | } *desc; |
| 2425 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2426 | if (do_read_u32(ff, &nr_groups)) |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 2427 | return -1; |
| 2428 | |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2429 | ff->ph->env.nr_groups = nr_groups; |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 2430 | if (!nr_groups) { |
| 2431 | pr_debug("group desc not available\n"); |
| 2432 | return 0; |
| 2433 | } |
| 2434 | |
| 2435 | desc = calloc(nr_groups, sizeof(*desc)); |
| 2436 | if (!desc) |
| 2437 | return -1; |
| 2438 | |
| 2439 | for (i = 0; i < nr_groups; i++) { |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2440 | desc[i].name = do_read_string(ff); |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 2441 | if (!desc[i].name) |
| 2442 | goto out_free; |
| 2443 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2444 | if (do_read_u32(ff, &desc[i].leader_idx)) |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 2445 | goto out_free; |
| 2446 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2447 | if (do_read_u32(ff, &desc[i].nr_members)) |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 2448 | goto out_free; |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | /* |
| 2452 | * Rebuild group relationship based on the group_desc |
| 2453 | */ |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2454 | session = container_of(ff->ph, struct perf_session, header); |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 2455 | session->evlist->nr_groups = nr_groups; |
| 2456 | |
| 2457 | i = nr = 0; |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 2458 | evlist__for_each_entry(session->evlist, evsel) { |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 2459 | if (evsel->idx == (int) desc[i].leader_idx) { |
| 2460 | evsel->leader = evsel; |
| 2461 | /* {anon_group} is a dummy name */ |
Namhyung Kim | 210e812 | 2013-11-18 11:20:43 +0900 | [diff] [blame] | 2462 | if (strcmp(desc[i].name, "{anon_group}")) { |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 2463 | evsel->group_name = desc[i].name; |
Namhyung Kim | 210e812 | 2013-11-18 11:20:43 +0900 | [diff] [blame] | 2464 | desc[i].name = NULL; |
| 2465 | } |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 2466 | evsel->nr_members = desc[i].nr_members; |
| 2467 | |
| 2468 | if (i >= nr_groups || nr > 0) { |
| 2469 | pr_debug("invalid group desc\n"); |
| 2470 | goto out_free; |
| 2471 | } |
| 2472 | |
| 2473 | leader = evsel; |
| 2474 | nr = evsel->nr_members - 1; |
| 2475 | i++; |
| 2476 | } else if (nr) { |
| 2477 | /* This is a group member */ |
| 2478 | evsel->leader = leader; |
| 2479 | |
| 2480 | nr--; |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | if (i != nr_groups || nr != 0) { |
| 2485 | pr_debug("invalid group desc\n"); |
| 2486 | goto out_free; |
| 2487 | } |
| 2488 | |
| 2489 | ret = 0; |
| 2490 | out_free: |
Namhyung Kim | 50a2740 | 2013-11-18 11:20:44 +0900 | [diff] [blame] | 2491 | for (i = 0; i < nr_groups; i++) |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 2492 | zfree(&desc[i].name); |
Namhyung Kim | a8bb559 | 2013-01-22 18:09:31 +0900 | [diff] [blame] | 2493 | free(desc); |
| 2494 | |
| 2495 | return ret; |
| 2496 | } |
| 2497 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2498 | static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused) |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 2499 | { |
| 2500 | struct perf_session *session; |
| 2501 | int err; |
| 2502 | |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2503 | session = container_of(ff->ph, struct perf_session, header); |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 2504 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2505 | err = auxtrace_index__process(ff->fd, ff->size, session, |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2506 | ff->ph->needs_swap); |
Adrian Hunter | 99fa298 | 2015-04-30 17:37:25 +0300 | [diff] [blame] | 2507 | if (err < 0) |
| 2508 | pr_err("Failed to process auxtrace index\n"); |
| 2509 | return err; |
| 2510 | } |
| 2511 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2512 | static int process_cache(struct feat_fd *ff, void *data __maybe_unused) |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 2513 | { |
| 2514 | struct cpu_cache_level *caches; |
| 2515 | u32 cnt, i, version; |
| 2516 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2517 | if (do_read_u32(ff, &version)) |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 2518 | return -1; |
| 2519 | |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 2520 | if (version != 1) |
| 2521 | return -1; |
| 2522 | |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2523 | if (do_read_u32(ff, &cnt)) |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 2524 | return -1; |
| 2525 | |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 2526 | caches = zalloc(sizeof(*caches) * cnt); |
| 2527 | if (!caches) |
| 2528 | return -1; |
| 2529 | |
| 2530 | for (i = 0; i < cnt; i++) { |
| 2531 | struct cpu_cache_level c; |
| 2532 | |
| 2533 | #define _R(v) \ |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2534 | if (do_read_u32(ff, &c.v))\ |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 2535 | goto out_free_caches; \ |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 2536 | |
| 2537 | _R(level) |
| 2538 | _R(line_size) |
| 2539 | _R(sets) |
| 2540 | _R(ways) |
| 2541 | #undef _R |
| 2542 | |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2543 | #define _R(v) \ |
David Carrillo-Cisneros | 48e5fce | 2017-07-17 21:25:43 -0700 | [diff] [blame] | 2544 | c.v = do_read_string(ff); \ |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2545 | if (!c.v) \ |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 2546 | goto out_free_caches; |
| 2547 | |
| 2548 | _R(type) |
| 2549 | _R(size) |
| 2550 | _R(map) |
| 2551 | #undef _R |
| 2552 | |
| 2553 | caches[i] = c; |
| 2554 | } |
| 2555 | |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 2556 | ff->ph->env.caches = caches; |
| 2557 | ff->ph->env.caches_cnt = cnt; |
Jiri Olsa | 720e98b | 2016-02-16 16:01:43 +0100 | [diff] [blame] | 2558 | return 0; |
| 2559 | out_free_caches: |
| 2560 | free(caches); |
| 2561 | return -1; |
| 2562 | } |
| 2563 | |
Jin Yao | 6011518 | 2017-12-08 21:13:41 +0800 | [diff] [blame] | 2564 | static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused) |
| 2565 | { |
| 2566 | struct perf_session *session; |
| 2567 | u64 first_sample_time, last_sample_time; |
| 2568 | int ret; |
| 2569 | |
| 2570 | session = container_of(ff->ph, struct perf_session, header); |
| 2571 | |
| 2572 | ret = do_read_u64(ff, &first_sample_time); |
| 2573 | if (ret) |
| 2574 | return -1; |
| 2575 | |
| 2576 | ret = do_read_u64(ff, &last_sample_time); |
| 2577 | if (ret) |
| 2578 | return -1; |
| 2579 | |
| 2580 | session->evlist->first_sample_time = first_sample_time; |
| 2581 | session->evlist->last_sample_time = last_sample_time; |
| 2582 | return 0; |
| 2583 | } |
| 2584 | |
Jiri Olsa | e2091ce | 2018-03-07 16:50:08 +0100 | [diff] [blame] | 2585 | static int process_mem_topology(struct feat_fd *ff, |
| 2586 | void *data __maybe_unused) |
| 2587 | { |
| 2588 | struct memory_node *nodes; |
| 2589 | u64 version, i, nr, bsize; |
| 2590 | int ret = -1; |
| 2591 | |
| 2592 | if (do_read_u64(ff, &version)) |
| 2593 | return -1; |
| 2594 | |
| 2595 | if (version != 1) |
| 2596 | return -1; |
| 2597 | |
| 2598 | if (do_read_u64(ff, &bsize)) |
| 2599 | return -1; |
| 2600 | |
| 2601 | if (do_read_u64(ff, &nr)) |
| 2602 | return -1; |
| 2603 | |
| 2604 | nodes = zalloc(sizeof(*nodes) * nr); |
| 2605 | if (!nodes) |
| 2606 | return -1; |
| 2607 | |
| 2608 | for (i = 0; i < nr; i++) { |
| 2609 | struct memory_node n; |
| 2610 | |
| 2611 | #define _R(v) \ |
| 2612 | if (do_read_u64(ff, &n.v)) \ |
| 2613 | goto out; \ |
| 2614 | |
| 2615 | _R(node) |
| 2616 | _R(size) |
| 2617 | |
| 2618 | #undef _R |
| 2619 | |
| 2620 | if (do_read_bitmap(ff, &n.set, &n.size)) |
| 2621 | goto out; |
| 2622 | |
| 2623 | nodes[i] = n; |
| 2624 | } |
| 2625 | |
| 2626 | ff->ph->env.memory_bsize = bsize; |
| 2627 | ff->ph->env.memory_nodes = nodes; |
| 2628 | ff->ph->env.nr_memory_nodes = nr; |
| 2629 | ret = 0; |
| 2630 | |
| 2631 | out: |
| 2632 | if (ret) |
| 2633 | free(nodes); |
| 2634 | return ret; |
| 2635 | } |
| 2636 | |
Alexey Budankov | cf79051 | 2018-10-09 17:36:24 +0300 | [diff] [blame] | 2637 | static int process_clockid(struct feat_fd *ff, |
| 2638 | void *data __maybe_unused) |
| 2639 | { |
| 2640 | if (do_read_u64(ff, &ff->ph->env.clockid_res_ns)) |
| 2641 | return -1; |
| 2642 | |
| 2643 | return 0; |
| 2644 | } |
| 2645 | |
Jiri Olsa | 258031c | 2019-03-08 14:47:39 +0100 | [diff] [blame] | 2646 | static int process_dir_format(struct feat_fd *ff, |
| 2647 | void *_data __maybe_unused) |
| 2648 | { |
| 2649 | struct perf_session *session; |
| 2650 | struct perf_data *data; |
| 2651 | |
| 2652 | session = container_of(ff->ph, struct perf_session, header); |
| 2653 | data = session->data; |
| 2654 | |
| 2655 | if (WARN_ON(!perf_data__is_dir(data))) |
| 2656 | return -1; |
| 2657 | |
| 2658 | return do_read_u64(ff, &data->dir.version); |
| 2659 | } |
| 2660 | |
Song Liu | 606f972 | 2019-03-11 22:30:43 -0700 | [diff] [blame] | 2661 | #ifdef HAVE_LIBBPF_SUPPORT |
| 2662 | static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused) |
| 2663 | { |
| 2664 | struct bpf_prog_info_linear *info_linear; |
| 2665 | struct bpf_prog_info_node *info_node; |
| 2666 | struct perf_env *env = &ff->ph->env; |
| 2667 | u32 count, i; |
| 2668 | int err = -1; |
| 2669 | |
| 2670 | if (ff->ph->needs_swap) { |
| 2671 | pr_warning("interpreting bpf_prog_info from systems with endianity is not yet supported\n"); |
| 2672 | return 0; |
| 2673 | } |
| 2674 | |
| 2675 | if (do_read_u32(ff, &count)) |
| 2676 | return -1; |
| 2677 | |
| 2678 | down_write(&env->bpf_progs.lock); |
| 2679 | |
| 2680 | for (i = 0; i < count; ++i) { |
| 2681 | u32 info_len, data_len; |
| 2682 | |
| 2683 | info_linear = NULL; |
| 2684 | info_node = NULL; |
| 2685 | if (do_read_u32(ff, &info_len)) |
| 2686 | goto out; |
| 2687 | if (do_read_u32(ff, &data_len)) |
| 2688 | goto out; |
| 2689 | |
| 2690 | if (info_len > sizeof(struct bpf_prog_info)) { |
| 2691 | pr_warning("detected invalid bpf_prog_info\n"); |
| 2692 | goto out; |
| 2693 | } |
| 2694 | |
| 2695 | info_linear = malloc(sizeof(struct bpf_prog_info_linear) + |
| 2696 | data_len); |
| 2697 | if (!info_linear) |
| 2698 | goto out; |
| 2699 | info_linear->info_len = sizeof(struct bpf_prog_info); |
| 2700 | info_linear->data_len = data_len; |
| 2701 | if (do_read_u64(ff, (u64 *)(&info_linear->arrays))) |
| 2702 | goto out; |
| 2703 | if (__do_read(ff, &info_linear->info, info_len)) |
| 2704 | goto out; |
| 2705 | if (info_len < sizeof(struct bpf_prog_info)) |
| 2706 | memset(((void *)(&info_linear->info)) + info_len, 0, |
| 2707 | sizeof(struct bpf_prog_info) - info_len); |
| 2708 | |
| 2709 | if (__do_read(ff, info_linear->data, data_len)) |
| 2710 | goto out; |
| 2711 | |
| 2712 | info_node = malloc(sizeof(struct bpf_prog_info_node)); |
| 2713 | if (!info_node) |
| 2714 | goto out; |
| 2715 | |
| 2716 | /* after reading from file, translate offset to address */ |
| 2717 | bpf_program__bpil_offs_to_addr(info_linear); |
| 2718 | info_node->info_linear = info_linear; |
| 2719 | perf_env__insert_bpf_prog_info(env, info_node); |
| 2720 | } |
| 2721 | |
Gustavo A. R. Silva | 14c9b31 | 2019-04-08 12:33:55 -0500 | [diff] [blame] | 2722 | up_write(&env->bpf_progs.lock); |
Song Liu | 606f972 | 2019-03-11 22:30:43 -0700 | [diff] [blame] | 2723 | return 0; |
| 2724 | out: |
| 2725 | free(info_linear); |
| 2726 | free(info_node); |
| 2727 | up_write(&env->bpf_progs.lock); |
| 2728 | return err; |
| 2729 | } |
| 2730 | #else // HAVE_LIBBPF_SUPPORT |
| 2731 | static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data __maybe_unused) |
| 2732 | { |
| 2733 | return 0; |
| 2734 | } |
| 2735 | #endif // HAVE_LIBBPF_SUPPORT |
| 2736 | |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 2737 | static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) |
| 2738 | { |
| 2739 | struct perf_env *env = &ff->ph->env; |
Gustavo A. R. Silva | 14c9b31 | 2019-04-08 12:33:55 -0500 | [diff] [blame] | 2740 | struct btf_node *node = NULL; |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 2741 | u32 count, i; |
Gustavo A. R. Silva | 14c9b31 | 2019-04-08 12:33:55 -0500 | [diff] [blame] | 2742 | int err = -1; |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 2743 | |
| 2744 | if (ff->ph->needs_swap) { |
| 2745 | pr_warning("interpreting btf from systems with endianity is not yet supported\n"); |
| 2746 | return 0; |
| 2747 | } |
| 2748 | |
| 2749 | if (do_read_u32(ff, &count)) |
| 2750 | return -1; |
| 2751 | |
| 2752 | down_write(&env->bpf_progs.lock); |
| 2753 | |
| 2754 | for (i = 0; i < count; ++i) { |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 2755 | u32 id, data_size; |
| 2756 | |
| 2757 | if (do_read_u32(ff, &id)) |
Gustavo A. R. Silva | 14c9b31 | 2019-04-08 12:33:55 -0500 | [diff] [blame] | 2758 | goto out; |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 2759 | if (do_read_u32(ff, &data_size)) |
Gustavo A. R. Silva | 14c9b31 | 2019-04-08 12:33:55 -0500 | [diff] [blame] | 2760 | goto out; |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 2761 | |
| 2762 | node = malloc(sizeof(struct btf_node) + data_size); |
| 2763 | if (!node) |
Gustavo A. R. Silva | 14c9b31 | 2019-04-08 12:33:55 -0500 | [diff] [blame] | 2764 | goto out; |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 2765 | |
| 2766 | node->id = id; |
| 2767 | node->data_size = data_size; |
| 2768 | |
Gustavo A. R. Silva | 14c9b31 | 2019-04-08 12:33:55 -0500 | [diff] [blame] | 2769 | if (__do_read(ff, node->data, data_size)) |
| 2770 | goto out; |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 2771 | |
| 2772 | perf_env__insert_btf(env, node); |
Gustavo A. R. Silva | 14c9b31 | 2019-04-08 12:33:55 -0500 | [diff] [blame] | 2773 | node = NULL; |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
Gustavo A. R. Silva | 14c9b31 | 2019-04-08 12:33:55 -0500 | [diff] [blame] | 2776 | err = 0; |
| 2777 | out: |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 2778 | up_write(&env->bpf_progs.lock); |
Gustavo A. R. Silva | 14c9b31 | 2019-04-08 12:33:55 -0500 | [diff] [blame] | 2779 | free(node); |
| 2780 | return err; |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 2781 | } |
| 2782 | |
Alexey Budankov | 42e1fd8 | 2019-03-18 20:41:33 +0300 | [diff] [blame] | 2783 | static int process_compressed(struct feat_fd *ff, |
| 2784 | void *data __maybe_unused) |
| 2785 | { |
| 2786 | if (do_read_u32(ff, &(ff->ph->env.comp_ver))) |
| 2787 | return -1; |
| 2788 | |
| 2789 | if (do_read_u32(ff, &(ff->ph->env.comp_type))) |
| 2790 | return -1; |
| 2791 | |
| 2792 | if (do_read_u32(ff, &(ff->ph->env.comp_level))) |
| 2793 | return -1; |
| 2794 | |
| 2795 | if (do_read_u32(ff, &(ff->ph->env.comp_ratio))) |
| 2796 | return -1; |
| 2797 | |
| 2798 | if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len))) |
| 2799 | return -1; |
| 2800 | |
| 2801 | return 0; |
| 2802 | } |
| 2803 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2804 | struct feature_ops { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 2805 | int (*write)(struct feat_fd *ff, struct evlist *evlist); |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 2806 | void (*print)(struct feat_fd *ff, FILE *fp); |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 2807 | int (*process)(struct feat_fd *ff, void *data); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2808 | const char *name; |
| 2809 | bool full_only; |
David Carrillo-Cisneros | a4d8c98 | 2017-07-17 21:25:46 -0700 | [diff] [blame] | 2810 | bool synthesize; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2811 | }; |
| 2812 | |
David Carrillo-Cisneros | a4d8c98 | 2017-07-17 21:25:46 -0700 | [diff] [blame] | 2813 | #define FEAT_OPR(n, func, __full_only) \ |
| 2814 | [HEADER_##n] = { \ |
| 2815 | .name = __stringify(n), \ |
| 2816 | .write = write_##func, \ |
| 2817 | .print = print_##func, \ |
| 2818 | .full_only = __full_only, \ |
| 2819 | .process = process_##func, \ |
| 2820 | .synthesize = true \ |
| 2821 | } |
| 2822 | |
| 2823 | #define FEAT_OPN(n, func, __full_only) \ |
| 2824 | [HEADER_##n] = { \ |
| 2825 | .name = __stringify(n), \ |
| 2826 | .write = write_##func, \ |
| 2827 | .print = print_##func, \ |
| 2828 | .full_only = __full_only, \ |
| 2829 | .process = process_##func \ |
| 2830 | } |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 2831 | |
| 2832 | /* feature_ops not implemented: */ |
Stephane Eranian | 2eeaaa0 | 2012-05-15 13:28:13 +0200 | [diff] [blame] | 2833 | #define print_tracing_data NULL |
| 2834 | #define print_build_id NULL |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2835 | |
David Carrillo-Cisneros | a4d8c98 | 2017-07-17 21:25:46 -0700 | [diff] [blame] | 2836 | #define process_branch_stack NULL |
| 2837 | #define process_stat NULL |
| 2838 | |
| 2839 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2840 | static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = { |
David Carrillo-Cisneros | a4d8c98 | 2017-07-17 21:25:46 -0700 | [diff] [blame] | 2841 | FEAT_OPN(TRACING_DATA, tracing_data, false), |
| 2842 | FEAT_OPN(BUILD_ID, build_id, false), |
| 2843 | FEAT_OPR(HOSTNAME, hostname, false), |
| 2844 | FEAT_OPR(OSRELEASE, osrelease, false), |
| 2845 | FEAT_OPR(VERSION, version, false), |
| 2846 | FEAT_OPR(ARCH, arch, false), |
| 2847 | FEAT_OPR(NRCPUS, nrcpus, false), |
| 2848 | FEAT_OPR(CPUDESC, cpudesc, false), |
| 2849 | FEAT_OPR(CPUID, cpuid, false), |
| 2850 | FEAT_OPR(TOTAL_MEM, total_mem, false), |
| 2851 | FEAT_OPR(EVENT_DESC, event_desc, false), |
| 2852 | FEAT_OPR(CMDLINE, cmdline, false), |
| 2853 | FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true), |
| 2854 | FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true), |
| 2855 | FEAT_OPN(BRANCH_STACK, branch_stack, false), |
| 2856 | FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false), |
Jiri Olsa | e8fedff | 2018-07-12 15:52:02 +0200 | [diff] [blame] | 2857 | FEAT_OPR(GROUP_DESC, group_desc, false), |
David Carrillo-Cisneros | a4d8c98 | 2017-07-17 21:25:46 -0700 | [diff] [blame] | 2858 | FEAT_OPN(AUXTRACE, auxtrace, false), |
| 2859 | FEAT_OPN(STAT, stat, false), |
| 2860 | FEAT_OPN(CACHE, cache, true), |
Jin Yao | 6011518 | 2017-12-08 21:13:41 +0800 | [diff] [blame] | 2861 | FEAT_OPR(SAMPLE_TIME, sample_time, false), |
Jiri Olsa | e2091ce | 2018-03-07 16:50:08 +0100 | [diff] [blame] | 2862 | FEAT_OPR(MEM_TOPOLOGY, mem_topology, true), |
Jiri Olsa | 258031c | 2019-03-08 14:47:39 +0100 | [diff] [blame] | 2863 | FEAT_OPR(CLOCKID, clockid, false), |
Song Liu | 606f972 | 2019-03-11 22:30:43 -0700 | [diff] [blame] | 2864 | FEAT_OPN(DIR_FORMAT, dir_format, false), |
Song Liu | a70a11231 | 2019-03-11 22:30:45 -0700 | [diff] [blame] | 2865 | FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false), |
| 2866 | FEAT_OPR(BPF_BTF, bpf_btf, false), |
Alexey Budankov | 42e1fd8 | 2019-03-18 20:41:33 +0300 | [diff] [blame] | 2867 | FEAT_OPR(COMPRESSED, compressed, false), |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2868 | }; |
| 2869 | |
| 2870 | struct header_print_data { |
| 2871 | FILE *fp; |
| 2872 | bool full; /* extended list of headers */ |
| 2873 | }; |
| 2874 | |
| 2875 | static int perf_file_section__fprintf_info(struct perf_file_section *section, |
| 2876 | struct perf_header *ph, |
| 2877 | int feat, int fd, void *data) |
| 2878 | { |
| 2879 | struct header_print_data *hd = data; |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 2880 | struct feat_fd ff; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2881 | |
| 2882 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { |
| 2883 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " |
| 2884 | "%d, continuing...\n", section->offset, feat); |
| 2885 | return 0; |
| 2886 | } |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2887 | if (feat >= HEADER_LAST_FEATURE) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2888 | pr_warning("unknown feature %d\n", feat); |
Robert Richter | f7a8a13 | 2011-12-07 10:02:51 +0100 | [diff] [blame] | 2889 | return 0; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2890 | } |
| 2891 | if (!feat_ops[feat].print) |
| 2892 | return 0; |
| 2893 | |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 2894 | ff = (struct feat_fd) { |
| 2895 | .fd = fd, |
| 2896 | .ph = ph, |
| 2897 | }; |
| 2898 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2899 | if (!feat_ops[feat].full_only || hd->full) |
David Carrillo-Cisneros | cfc6542 | 2017-07-17 21:25:40 -0700 | [diff] [blame] | 2900 | feat_ops[feat].print(&ff, hd->fp); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2901 | else |
| 2902 | fprintf(hd->fp, "# %s info available, use -I to display\n", |
| 2903 | feat_ops[feat].name); |
| 2904 | |
| 2905 | return 0; |
| 2906 | } |
| 2907 | |
| 2908 | int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) |
| 2909 | { |
| 2910 | struct header_print_data hd; |
| 2911 | struct perf_header *header = &session->header; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2912 | int fd = perf_data__fd(session->data); |
Jiri Olsa | f45f561 | 2016-10-10 09:03:07 +0200 | [diff] [blame] | 2913 | struct stat st; |
Arnaldo Carvalho de Melo | 0afcf29 | 2018-12-11 16:11:54 -0300 | [diff] [blame] | 2914 | time_t stctime; |
Jiri Olsa | aabae16 | 2016-10-10 09:35:50 +0200 | [diff] [blame] | 2915 | int ret, bit; |
Jiri Olsa | f45f561 | 2016-10-10 09:03:07 +0200 | [diff] [blame] | 2916 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2917 | hd.fp = fp; |
| 2918 | hd.full = full; |
| 2919 | |
Jiri Olsa | f45f561 | 2016-10-10 09:03:07 +0200 | [diff] [blame] | 2920 | ret = fstat(fd, &st); |
| 2921 | if (ret == -1) |
| 2922 | return -1; |
| 2923 | |
Arnaldo Carvalho de Melo | 0afcf29 | 2018-12-11 16:11:54 -0300 | [diff] [blame] | 2924 | stctime = st.st_ctime; |
| 2925 | fprintf(fp, "# captured on : %s", ctime(&stctime)); |
Jiri Olsa | e971a5a | 2018-03-07 16:50:03 +0100 | [diff] [blame] | 2926 | |
| 2927 | fprintf(fp, "# header version : %u\n", header->version); |
| 2928 | fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset); |
| 2929 | fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size); |
| 2930 | fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset); |
Jiri Olsa | f45f561 | 2016-10-10 09:03:07 +0200 | [diff] [blame] | 2931 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2932 | perf_header__process_sections(header, fd, &hd, |
| 2933 | perf_file_section__fprintf_info); |
Jiri Olsa | aabae16 | 2016-10-10 09:35:50 +0200 | [diff] [blame] | 2934 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2935 | if (session->data->is_pipe) |
David Carrillo-Cisneros | c9d1c93 | 2017-04-10 13:14:32 -0700 | [diff] [blame] | 2936 | return 0; |
| 2937 | |
Jiri Olsa | aabae16 | 2016-10-10 09:35:50 +0200 | [diff] [blame] | 2938 | fprintf(fp, "# missing features: "); |
| 2939 | for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) { |
| 2940 | if (bit) |
| 2941 | fprintf(fp, "%s ", feat_ops[bit].name); |
| 2942 | } |
| 2943 | |
| 2944 | fprintf(fp, "\n"); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2945 | return 0; |
| 2946 | } |
| 2947 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 2948 | static int do_write_feat(struct feat_fd *ff, int type, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2949 | struct perf_file_section **p, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 2950 | struct evlist *evlist) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2951 | { |
| 2952 | int err; |
| 2953 | int ret = 0; |
| 2954 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 2955 | if (perf_header__has_feat(ff->ph, type)) { |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2956 | if (!feat_ops[type].write) |
| 2957 | return -1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2958 | |
David Carrillo-Cisneros | 0b3d341 | 2017-07-17 21:25:45 -0700 | [diff] [blame] | 2959 | if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) |
| 2960 | return -1; |
| 2961 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 2962 | (*p)->offset = lseek(ff->fd, 0, SEEK_CUR); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2963 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 2964 | err = feat_ops[type].write(ff, evlist); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2965 | if (err < 0) { |
Jiri Olsa | 0c2aff4 | 2016-10-10 09:38:02 +0200 | [diff] [blame] | 2966 | pr_debug("failed to write feature %s\n", feat_ops[type].name); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2967 | |
| 2968 | /* undo anything written */ |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 2969 | lseek(ff->fd, (*p)->offset, SEEK_SET); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2970 | |
| 2971 | return -1; |
| 2972 | } |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 2973 | (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2974 | (*p)++; |
| 2975 | } |
| 2976 | return ret; |
| 2977 | } |
| 2978 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2979 | static int perf_header__adds_write(struct perf_header *header, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 2980 | struct evlist *evlist, int fd) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2981 | { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2982 | int nr_sections; |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 2983 | struct feat_fd ff; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2984 | struct perf_file_section *feat_sec, *p; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2985 | int sec_size; |
| 2986 | u64 sec_start; |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2987 | int feat; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2988 | int err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2989 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 2990 | ff = (struct feat_fd){ |
| 2991 | .fd = fd, |
| 2992 | .ph = header, |
| 2993 | }; |
| 2994 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2995 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2996 | if (!nr_sections) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2997 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2998 | |
Paul Gortmaker | 91b9880 | 2013-01-30 20:05:49 -0500 | [diff] [blame] | 2999 | feat_sec = p = calloc(nr_sections, sizeof(*feat_sec)); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 3000 | if (feat_sec == NULL) |
| 3001 | return -ENOMEM; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3002 | |
| 3003 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 3004 | |
Jiri Olsa | 8d541e9 | 2013-07-17 19:49:44 +0200 | [diff] [blame] | 3005 | sec_start = header->feat_offset; |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 3006 | lseek(fd, sec_start + sec_size, SEEK_SET); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 3007 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 3008 | for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3009 | if (do_write_feat(&ff, feat, &p, evlist)) |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 3010 | perf_header__clear_feat(header, feat); |
| 3011 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3012 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 3013 | lseek(fd, sec_start, SEEK_SET); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 3014 | /* |
| 3015 | * may write more than needed due to dropped feature, but |
Ingo Molnar | adba163 | 2018-12-03 11:22:00 +0100 | [diff] [blame] | 3016 | * this is okay, reader will skip the missing entries |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 3017 | */ |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3018 | err = do_write(&ff, feat_sec, sec_size); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 3019 | if (err < 0) |
| 3020 | pr_debug("failed to write feature section\n"); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3021 | free(feat_sec); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 3022 | return err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3023 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 3024 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3025 | int perf_header__write_pipe(int fd) |
| 3026 | { |
| 3027 | struct perf_pipe_file_header f_header; |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3028 | struct feat_fd ff; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3029 | int err; |
| 3030 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3031 | ff = (struct feat_fd){ .fd = fd }; |
| 3032 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3033 | f_header = (struct perf_pipe_file_header){ |
| 3034 | .magic = PERF_MAGIC, |
| 3035 | .size = sizeof(f_header), |
| 3036 | }; |
| 3037 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3038 | err = do_write(&ff, &f_header, sizeof(f_header)); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3039 | if (err < 0) { |
| 3040 | pr_debug("failed to write perf pipe header\n"); |
| 3041 | return err; |
| 3042 | } |
| 3043 | |
| 3044 | return 0; |
| 3045 | } |
| 3046 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 3047 | int perf_session__write_header(struct perf_session *session, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 3048 | struct evlist *evlist, |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 3049 | int fd, bool at_exit) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3050 | { |
| 3051 | struct perf_file_header f_header; |
| 3052 | struct perf_file_attr f_attr; |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3053 | struct perf_header *header = &session->header; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 3054 | struct evsel *evsel; |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3055 | struct feat_fd ff; |
Jiri Olsa | 944d62b | 2013-07-17 19:49:43 +0200 | [diff] [blame] | 3056 | u64 attr_offset; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 3057 | int err; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3058 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3059 | ff = (struct feat_fd){ .fd = fd}; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3060 | lseek(fd, sizeof(f_header), SEEK_SET); |
| 3061 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 3062 | evlist__for_each_entry(session->evlist, evsel) { |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 3063 | evsel->id_offset = lseek(fd, 0, SEEK_CUR); |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3064 | err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64)); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 3065 | if (err < 0) { |
| 3066 | pr_debug("failed to write perf header\n"); |
| 3067 | return err; |
| 3068 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3069 | } |
| 3070 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3071 | attr_offset = lseek(ff.fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3072 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 3073 | evlist__for_each_entry(evlist, evsel) { |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3074 | f_attr = (struct perf_file_attr){ |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 3075 | .attr = evsel->core.attr, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3076 | .ids = { |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 3077 | .offset = evsel->id_offset, |
| 3078 | .size = evsel->ids * sizeof(u64), |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3079 | } |
| 3080 | }; |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3081 | err = do_write(&ff, &f_attr, sizeof(f_attr)); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 3082 | if (err < 0) { |
| 3083 | pr_debug("failed to write perf header attribute\n"); |
| 3084 | return err; |
| 3085 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3086 | } |
| 3087 | |
Adrian Hunter | d645c44 | 2013-12-11 14:36:28 +0200 | [diff] [blame] | 3088 | if (!header->data_offset) |
| 3089 | header->data_offset = lseek(fd, 0, SEEK_CUR); |
Jiri Olsa | 8d541e9 | 2013-07-17 19:49:44 +0200 | [diff] [blame] | 3090 | header->feat_offset = header->data_offset + header->data_size; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3091 | |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 3092 | if (at_exit) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3093 | err = perf_header__adds_write(header, evlist, fd); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 3094 | if (err < 0) |
| 3095 | return err; |
| 3096 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3097 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3098 | f_header = (struct perf_file_header){ |
| 3099 | .magic = PERF_MAGIC, |
| 3100 | .size = sizeof(f_header), |
| 3101 | .attr_size = sizeof(f_attr), |
| 3102 | .attrs = { |
Jiri Olsa | 944d62b | 2013-07-17 19:49:43 +0200 | [diff] [blame] | 3103 | .offset = attr_offset, |
Jiri Olsa | 6484d2f | 2019-07-21 13:24:28 +0200 | [diff] [blame] | 3104 | .size = evlist->core.nr_entries * sizeof(f_attr), |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3105 | }, |
| 3106 | .data = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3107 | .offset = header->data_offset, |
| 3108 | .size = header->data_size, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3109 | }, |
Jiri Olsa | 44b3c57 | 2013-07-11 17:28:31 +0200 | [diff] [blame] | 3110 | /* event_types is ignored, store zeros */ |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3111 | }; |
| 3112 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3113 | memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 3114 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3115 | lseek(fd, 0, SEEK_SET); |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3116 | err = do_write(&ff, &f_header, sizeof(f_header)); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 3117 | if (err < 0) { |
| 3118 | pr_debug("failed to write perf header\n"); |
| 3119 | return err; |
| 3120 | } |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3121 | lseek(fd, header->data_offset + header->data_size, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3122 | |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 3123 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3124 | } |
| 3125 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3126 | static int perf_header__getbuffer64(struct perf_header *header, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 3127 | int fd, void *buf, size_t size) |
| 3128 | { |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 3129 | if (readn(fd, buf, size) <= 0) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 3130 | return -1; |
| 3131 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3132 | if (header->needs_swap) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 3133 | mem_bswap_64(buf, size); |
| 3134 | |
| 3135 | return 0; |
| 3136 | } |
| 3137 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3138 | int perf_header__process_sections(struct perf_header *header, int fd, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 3139 | void *data, |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3140 | int (*process)(struct perf_file_section *section, |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 3141 | struct perf_header *ph, |
| 3142 | int feat, int fd, void *data)) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 3143 | { |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 3144 | struct perf_file_section *feat_sec, *sec; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3145 | int nr_sections; |
| 3146 | int sec_size; |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 3147 | int feat; |
| 3148 | int err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3149 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3150 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3151 | if (!nr_sections) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3152 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3153 | |
Paul Gortmaker | 91b9880 | 2013-01-30 20:05:49 -0500 | [diff] [blame] | 3154 | feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec)); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3155 | if (!feat_sec) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3156 | return -1; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3157 | |
| 3158 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 3159 | |
Jiri Olsa | 8d541e9 | 2013-07-17 19:49:44 +0200 | [diff] [blame] | 3160 | lseek(fd, header->feat_offset, SEEK_SET); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3161 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 3162 | err = perf_header__getbuffer64(header, fd, feat_sec, sec_size); |
| 3163 | if (err < 0) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 3164 | goto out_free; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3165 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 3166 | for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) { |
| 3167 | err = process(sec++, header, feat, fd, data); |
| 3168 | if (err < 0) |
| 3169 | goto out_free; |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 3170 | } |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 3171 | err = 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 3172 | out_free: |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 3173 | free(feat_sec); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3174 | return err; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 3175 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 3176 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3177 | static const int attr_file_abi_sizes[] = { |
| 3178 | [0] = PERF_ATTR_SIZE_VER0, |
| 3179 | [1] = PERF_ATTR_SIZE_VER1, |
Jiri Olsa | 239cc47 | 2012-08-07 15:20:42 +0200 | [diff] [blame] | 3180 | [2] = PERF_ATTR_SIZE_VER2, |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 3181 | [3] = PERF_ATTR_SIZE_VER3, |
Stephane Eranian | 6a21c0b | 2014-09-24 13:48:39 +0200 | [diff] [blame] | 3182 | [4] = PERF_ATTR_SIZE_VER4, |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3183 | 0, |
| 3184 | }; |
| 3185 | |
| 3186 | /* |
| 3187 | * In the legacy file format, the magic number is not used to encode endianness. |
| 3188 | * hdr_sz was used to encode endianness. But given that hdr_sz can vary based |
| 3189 | * on ABI revisions, we need to try all combinations for all endianness to |
| 3190 | * detect the endianness. |
| 3191 | */ |
| 3192 | static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph) |
| 3193 | { |
| 3194 | uint64_t ref_size, attr_size; |
| 3195 | int i; |
| 3196 | |
| 3197 | for (i = 0 ; attr_file_abi_sizes[i]; i++) { |
| 3198 | ref_size = attr_file_abi_sizes[i] |
| 3199 | + sizeof(struct perf_file_section); |
| 3200 | if (hdr_sz != ref_size) { |
| 3201 | attr_size = bswap_64(hdr_sz); |
| 3202 | if (attr_size != ref_size) |
| 3203 | continue; |
| 3204 | |
| 3205 | ph->needs_swap = true; |
| 3206 | } |
| 3207 | pr_debug("ABI%d perf.data file detected, need_swap=%d\n", |
| 3208 | i, |
| 3209 | ph->needs_swap); |
| 3210 | return 0; |
| 3211 | } |
| 3212 | /* could not determine endianness */ |
| 3213 | return -1; |
| 3214 | } |
| 3215 | |
| 3216 | #define PERF_PIPE_HDR_VER0 16 |
| 3217 | |
| 3218 | static const size_t attr_pipe_abi_sizes[] = { |
| 3219 | [0] = PERF_PIPE_HDR_VER0, |
| 3220 | 0, |
| 3221 | }; |
| 3222 | |
| 3223 | /* |
| 3224 | * In the legacy pipe format, there is an implicit assumption that endiannesss |
| 3225 | * between host recording the samples, and host parsing the samples is the |
| 3226 | * same. This is not always the case given that the pipe output may always be |
| 3227 | * redirected into a file and analyzed on a different machine with possibly a |
| 3228 | * different endianness and perf_event ABI revsions in the perf tool itself. |
| 3229 | */ |
| 3230 | static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph) |
| 3231 | { |
| 3232 | u64 attr_size; |
| 3233 | int i; |
| 3234 | |
| 3235 | for (i = 0 ; attr_pipe_abi_sizes[i]; i++) { |
| 3236 | if (hdr_sz != attr_pipe_abi_sizes[i]) { |
| 3237 | attr_size = bswap_64(hdr_sz); |
| 3238 | if (attr_size != hdr_sz) |
| 3239 | continue; |
| 3240 | |
| 3241 | ph->needs_swap = true; |
| 3242 | } |
| 3243 | pr_debug("Pipe ABI%d perf.data file detected\n", i); |
| 3244 | return 0; |
| 3245 | } |
| 3246 | return -1; |
| 3247 | } |
| 3248 | |
Feng Tang | e84ba4e | 2012-10-30 11:56:07 +0800 | [diff] [blame] | 3249 | bool is_perf_magic(u64 magic) |
| 3250 | { |
| 3251 | if (!memcmp(&magic, __perf_magic1, sizeof(magic)) |
| 3252 | || magic == __perf_magic2 |
| 3253 | || magic == __perf_magic2_sw) |
| 3254 | return true; |
| 3255 | |
| 3256 | return false; |
| 3257 | } |
| 3258 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3259 | static int check_magic_endian(u64 magic, uint64_t hdr_sz, |
| 3260 | bool is_pipe, struct perf_header *ph) |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3261 | { |
| 3262 | int ret; |
| 3263 | |
| 3264 | /* check for legacy format */ |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3265 | ret = memcmp(&magic, __perf_magic1, sizeof(magic)); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3266 | if (ret == 0) { |
Jiri Olsa | 2a08c3e | 2013-07-17 19:49:47 +0200 | [diff] [blame] | 3267 | ph->version = PERF_HEADER_VERSION_1; |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3268 | pr_debug("legacy perf.data format\n"); |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3269 | if (is_pipe) |
| 3270 | return try_all_pipe_abis(hdr_sz, ph); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3271 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3272 | return try_all_file_abis(hdr_sz, ph); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3273 | } |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3274 | /* |
| 3275 | * the new magic number serves two purposes: |
| 3276 | * - unique number to identify actual perf.data files |
| 3277 | * - encode endianness of file |
| 3278 | */ |
Namhyung Kim | f791397 | 2015-01-29 17:06:45 +0900 | [diff] [blame] | 3279 | ph->version = PERF_HEADER_VERSION_2; |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3280 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3281 | /* check magic number with one endianness */ |
| 3282 | if (magic == __perf_magic2) |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3283 | return 0; |
| 3284 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3285 | /* check magic number with opposite endianness */ |
| 3286 | if (magic != __perf_magic2_sw) |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3287 | return -1; |
| 3288 | |
| 3289 | ph->needs_swap = true; |
| 3290 | |
| 3291 | return 0; |
| 3292 | } |
| 3293 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3294 | int perf_file_header__read(struct perf_file_header *header, |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3295 | struct perf_header *ph, int fd) |
| 3296 | { |
Jiri Olsa | 727ebd5 | 2013-11-28 11:30:14 +0100 | [diff] [blame] | 3297 | ssize_t ret; |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3298 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3299 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3300 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3301 | ret = readn(fd, header, sizeof(*header)); |
| 3302 | if (ret <= 0) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3303 | return -1; |
| 3304 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3305 | if (check_magic_endian(header->magic, |
| 3306 | header->attr_size, false, ph) < 0) { |
| 3307 | pr_debug("magic/endian check failed\n"); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3308 | return -1; |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3309 | } |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 3310 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3311 | if (ph->needs_swap) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3312 | mem_bswap_64(header, offsetof(struct perf_file_header, |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3313 | adds_features)); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 3314 | } |
| 3315 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3316 | if (header->size != sizeof(*header)) { |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3317 | /* Support the previous format */ |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3318 | if (header->size == offsetof(typeof(*header), adds_features)) |
| 3319 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3320 | else |
| 3321 | return -1; |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 3322 | } else if (ph->needs_swap) { |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 3323 | /* |
| 3324 | * feature bitmap is declared as an array of unsigned longs -- |
| 3325 | * not good since its size can differ between the host that |
| 3326 | * generated the data file and the host analyzing the file. |
| 3327 | * |
| 3328 | * We need to handle endianness, but we don't know the size of |
| 3329 | * the unsigned long where the file was generated. Take a best |
| 3330 | * guess at determining it: try 64-bit swap first (ie., file |
| 3331 | * created on a 64-bit host), and check if the hostname feature |
| 3332 | * bit is set (this feature bit is forced on as of fbe96f2). |
| 3333 | * If the bit is not, undo the 64-bit swap and try a 32-bit |
| 3334 | * swap. If the hostname bit is still not set (e.g., older data |
| 3335 | * file), punt and fallback to the original behavior -- |
| 3336 | * clearing all feature bits and setting buildid. |
| 3337 | */ |
David Ahern | 80c0120 | 2012-06-08 11:47:51 -0300 | [diff] [blame] | 3338 | mem_bswap_64(&header->adds_features, |
| 3339 | BITS_TO_U64(HEADER_FEAT_BITS)); |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 3340 | |
| 3341 | if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { |
David Ahern | 80c0120 | 2012-06-08 11:47:51 -0300 | [diff] [blame] | 3342 | /* unswap as u64 */ |
| 3343 | mem_bswap_64(&header->adds_features, |
| 3344 | BITS_TO_U64(HEADER_FEAT_BITS)); |
| 3345 | |
| 3346 | /* unswap as u32 */ |
| 3347 | mem_bswap_32(&header->adds_features, |
| 3348 | BITS_TO_U32(HEADER_FEAT_BITS)); |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { |
| 3352 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); |
| 3353 | set_bit(HEADER_BUILD_ID, header->adds_features); |
| 3354 | } |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3355 | } |
| 3356 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3357 | memcpy(&ph->adds_features, &header->adds_features, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 3358 | sizeof(ph->adds_features)); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3359 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3360 | ph->data_offset = header->data.offset; |
| 3361 | ph->data_size = header->data.size; |
Jiri Olsa | 8d541e9 | 2013-07-17 19:49:44 +0200 | [diff] [blame] | 3362 | ph->feat_offset = header->data.offset + header->data.size; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3363 | return 0; |
| 3364 | } |
| 3365 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3366 | 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] | 3367 | struct perf_header *ph, |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 3368 | int feat, int fd, void *data) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3369 | { |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 3370 | struct feat_fd fdd = { |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 3371 | .fd = fd, |
| 3372 | .ph = ph, |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 3373 | .size = section->size, |
| 3374 | .offset = section->offset, |
David Carrillo-Cisneros | 1a22275 | 2017-07-17 21:25:41 -0700 | [diff] [blame] | 3375 | }; |
| 3376 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3377 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 3378 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3379 | "%d, continuing...\n", section->offset, feat); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3380 | return 0; |
| 3381 | } |
| 3382 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 3383 | if (feat >= HEADER_LAST_FEATURE) { |
| 3384 | pr_debug("unknown feature %d, continuing...\n", feat); |
| 3385 | return 0; |
| 3386 | } |
| 3387 | |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 3388 | if (!feat_ops[feat].process) |
| 3389 | return 0; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3390 | |
David Carrillo-Cisneros | 6255245 | 2017-07-17 21:25:42 -0700 | [diff] [blame] | 3391 | return feat_ops[feat].process(&fdd, data); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 3392 | } |
| 3393 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3394 | 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] | 3395 | struct perf_header *ph, int fd, |
| 3396 | bool repipe) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3397 | { |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3398 | struct feat_fd ff = { |
| 3399 | .fd = STDOUT_FILENO, |
| 3400 | .ph = ph, |
| 3401 | }; |
Jiri Olsa | 727ebd5 | 2013-11-28 11:30:14 +0100 | [diff] [blame] | 3402 | ssize_t ret; |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 3403 | |
| 3404 | ret = readn(fd, header, sizeof(*header)); |
| 3405 | if (ret <= 0) |
| 3406 | return -1; |
| 3407 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3408 | if (check_magic_endian(header->magic, header->size, true, ph) < 0) { |
| 3409 | pr_debug("endian/magic failed\n"); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3410 | return -1; |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | if (ph->needs_swap) |
| 3414 | header->size = bswap_64(header->size); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3415 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 3416 | if (repipe && do_write(&ff, header, sizeof(*header)) < 0) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 3417 | return -1; |
| 3418 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3419 | return 0; |
| 3420 | } |
| 3421 | |
Jiri Olsa | d433956 | 2013-07-17 19:49:41 +0200 | [diff] [blame] | 3422 | static int perf_header__read_pipe(struct perf_session *session) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3423 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3424 | struct perf_header *header = &session->header; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3425 | struct perf_pipe_file_header f_header; |
| 3426 | |
Jiri Olsa | cc9784bd | 2013-10-15 16:27:34 +0200 | [diff] [blame] | 3427 | if (perf_file_header__read_pipe(&f_header, header, |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 3428 | perf_data__fd(session->data), |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 3429 | session->repipe) < 0) { |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3430 | pr_debug("incompatible file format\n"); |
| 3431 | return -EINVAL; |
| 3432 | } |
| 3433 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3434 | return 0; |
| 3435 | } |
| 3436 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 3437 | static int read_attr(int fd, struct perf_header *ph, |
| 3438 | struct perf_file_attr *f_attr) |
| 3439 | { |
| 3440 | struct perf_event_attr *attr = &f_attr->attr; |
| 3441 | size_t sz, left; |
| 3442 | size_t our_sz = sizeof(f_attr->attr); |
Jiri Olsa | 727ebd5 | 2013-11-28 11:30:14 +0100 | [diff] [blame] | 3443 | ssize_t ret; |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 3444 | |
| 3445 | memset(f_attr, 0, sizeof(*f_attr)); |
| 3446 | |
| 3447 | /* read minimal guaranteed structure */ |
| 3448 | ret = readn(fd, attr, PERF_ATTR_SIZE_VER0); |
| 3449 | if (ret <= 0) { |
| 3450 | pr_debug("cannot read %d bytes of header attr\n", |
| 3451 | PERF_ATTR_SIZE_VER0); |
| 3452 | return -1; |
| 3453 | } |
| 3454 | |
| 3455 | /* on file perf_event_attr size */ |
| 3456 | sz = attr->size; |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 3457 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 3458 | if (ph->needs_swap) |
| 3459 | sz = bswap_32(sz); |
| 3460 | |
| 3461 | if (sz == 0) { |
| 3462 | /* assume ABI0 */ |
| 3463 | sz = PERF_ATTR_SIZE_VER0; |
| 3464 | } else if (sz > our_sz) { |
| 3465 | pr_debug("file uses a more recent and unsupported ABI" |
| 3466 | " (%zu bytes extra)\n", sz - our_sz); |
| 3467 | return -1; |
| 3468 | } |
| 3469 | /* what we have not yet read and that we know about */ |
| 3470 | left = sz - PERF_ATTR_SIZE_VER0; |
| 3471 | if (left) { |
| 3472 | void *ptr = attr; |
| 3473 | ptr += PERF_ATTR_SIZE_VER0; |
| 3474 | |
| 3475 | ret = readn(fd, ptr, left); |
| 3476 | } |
| 3477 | /* read perf_file_section, ids are read in caller */ |
| 3478 | ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); |
| 3479 | |
| 3480 | return ret <= 0 ? -1 : 0; |
| 3481 | } |
| 3482 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 3483 | static int perf_evsel__prepare_tracepoint_event(struct evsel *evsel, |
Tzvetomir Stoyanov (VMware) | 096177a | 2018-08-08 14:02:46 -0400 | [diff] [blame] | 3484 | struct tep_handle *pevent) |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 3485 | { |
Tzvetomir Stoyanov | 97fbf3f | 2018-11-30 10:44:07 -0500 | [diff] [blame] | 3486 | struct tep_event *event; |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 3487 | char bf[128]; |
| 3488 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 3489 | /* already prepared */ |
| 3490 | if (evsel->tp_format) |
| 3491 | return 0; |
| 3492 | |
Namhyung Kim | 3dce2ce | 2013-03-21 16:18:48 +0900 | [diff] [blame] | 3493 | if (pevent == NULL) { |
| 3494 | pr_debug("broken or missing trace data\n"); |
| 3495 | return -1; |
| 3496 | } |
| 3497 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 3498 | event = tep_find_event(pevent, evsel->core.attr.config); |
Namhyung Kim | a7619ae | 2013-04-18 21:24:16 +0900 | [diff] [blame] | 3499 | if (event == NULL) { |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 3500 | pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config); |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 3501 | return -1; |
Namhyung Kim | a7619ae | 2013-04-18 21:24:16 +0900 | [diff] [blame] | 3502 | } |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 3503 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 3504 | if (!evsel->name) { |
| 3505 | snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name); |
| 3506 | evsel->name = strdup(bf); |
| 3507 | if (evsel->name == NULL) |
| 3508 | return -1; |
| 3509 | } |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 3510 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 3511 | evsel->tp_format = event; |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 3512 | return 0; |
| 3513 | } |
| 3514 | |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 3515 | static int perf_evlist__prepare_tracepoint_events(struct evlist *evlist, |
Tzvetomir Stoyanov (VMware) | 096177a | 2018-08-08 14:02:46 -0400 | [diff] [blame] | 3516 | struct tep_handle *pevent) |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 3517 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 3518 | struct evsel *pos; |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 3519 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 3520 | evlist__for_each_entry(evlist, pos) { |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 3521 | if (pos->core.attr.type == PERF_TYPE_TRACEPOINT && |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 3522 | perf_evsel__prepare_tracepoint_event(pos, pevent)) |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 3523 | return -1; |
| 3524 | } |
| 3525 | |
| 3526 | return 0; |
| 3527 | } |
| 3528 | |
Jiri Olsa | d433956 | 2013-07-17 19:49:41 +0200 | [diff] [blame] | 3529 | int perf_session__read_header(struct perf_session *session) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3530 | { |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 3531 | struct perf_data *data = session->data; |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3532 | struct perf_header *header = &session->header; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 3533 | struct perf_file_header f_header; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3534 | struct perf_file_attr f_attr; |
| 3535 | u64 f_id; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3536 | int nr_attrs, nr_ids, i, j; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 3537 | int fd = perf_data__fd(data); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3538 | |
Jiri Olsa | 0f98b11 | 2019-07-21 13:23:55 +0200 | [diff] [blame] | 3539 | session->evlist = evlist__new(); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 3540 | if (session->evlist == NULL) |
| 3541 | return -ENOMEM; |
| 3542 | |
Kan Liang | 2c07144 | 2015-08-28 05:48:05 -0400 | [diff] [blame] | 3543 | session->evlist->env = &header->env; |
Arnaldo Carvalho de Melo | 4cde998 | 2015-09-09 12:25:00 -0300 | [diff] [blame] | 3544 | session->machines.host.env = &header->env; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 3545 | if (perf_data__is_pipe(data)) |
Jiri Olsa | d433956 | 2013-07-17 19:49:41 +0200 | [diff] [blame] | 3546 | return perf_header__read_pipe(session); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 3547 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 3548 | if (perf_file_header__read(&f_header, header, fd) < 0) |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 3549 | return -EINVAL; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3550 | |
Namhyung Kim | b314e5c | 2013-09-30 17:19:48 +0900 | [diff] [blame] | 3551 | /* |
| 3552 | * Sanity check that perf.data was written cleanly; data size is |
| 3553 | * initialized to 0 and updated only if the on_exit function is run. |
| 3554 | * If data size is still 0 then the file contains only partial |
| 3555 | * information. Just warn user and process it as much as it can. |
| 3556 | */ |
| 3557 | if (f_header.data.size == 0) { |
| 3558 | pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n" |
| 3559 | "Was the 'perf record' command properly terminated?\n", |
Jiri Olsa | eae8ad8 | 2017-01-23 22:25:41 +0100 | [diff] [blame] | 3560 | data->file.path); |
Namhyung Kim | b314e5c | 2013-09-30 17:19:48 +0900 | [diff] [blame] | 3561 | } |
| 3562 | |
Vince Weaver | 7622236 | 2019-07-23 11:06:01 -0400 | [diff] [blame] | 3563 | if (f_header.attr_size == 0) { |
| 3564 | pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n" |
| 3565 | "Was the 'perf record' command properly terminated?\n", |
| 3566 | data->file.path); |
| 3567 | return -EINVAL; |
| 3568 | } |
| 3569 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 3570 | nr_attrs = f_header.attrs.size / f_header.attr_size; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3571 | lseek(fd, f_header.attrs.offset, SEEK_SET); |
| 3572 | |
| 3573 | for (i = 0; i < nr_attrs; i++) { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 3574 | struct evsel *evsel; |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 3575 | off_t tmp; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3576 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 3577 | if (read_attr(fd, header, &f_attr) < 0) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 3578 | goto out_errno; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 3579 | |
David Ahern | 1060ab8 | 2015-04-09 16:15:46 -0400 | [diff] [blame] | 3580 | if (header->needs_swap) { |
| 3581 | f_attr.ids.size = bswap_64(f_attr.ids.size); |
| 3582 | f_attr.ids.offset = bswap_64(f_attr.ids.offset); |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 3583 | perf_event__attr_swap(&f_attr.attr); |
David Ahern | 1060ab8 | 2015-04-09 16:15:46 -0400 | [diff] [blame] | 3584 | } |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 3585 | |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 3586 | tmp = lseek(fd, 0, SEEK_CUR); |
Jiri Olsa | 365c3ae | 2019-07-21 13:23:58 +0200 | [diff] [blame] | 3587 | evsel = evsel__new(&f_attr.attr); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3588 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 3589 | if (evsel == NULL) |
| 3590 | goto out_delete_evlist; |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 3591 | |
| 3592 | evsel->needs_swap = header->needs_swap; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 3593 | /* |
| 3594 | * Do it before so that if perf_evsel__alloc_id fails, this |
Jiri Olsa | c12995a | 2019-07-21 13:23:56 +0200 | [diff] [blame] | 3595 | * entry gets purged too at evlist__delete(). |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 3596 | */ |
Jiri Olsa | a1cf3a7 | 2019-07-21 13:23:59 +0200 | [diff] [blame] | 3597 | evlist__add(session->evlist, evsel); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3598 | |
| 3599 | nr_ids = f_attr.ids.size / sizeof(u64); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 3600 | /* |
| 3601 | * We don't have the cpu and thread maps on the header, so |
| 3602 | * for allocating the perf_sample_id table we fake 1 cpu and |
| 3603 | * hattr->ids threads. |
| 3604 | */ |
| 3605 | if (perf_evsel__alloc_id(evsel, 1, nr_ids)) |
| 3606 | goto out_delete_evlist; |
| 3607 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3608 | lseek(fd, f_attr.ids.offset, SEEK_SET); |
| 3609 | |
| 3610 | for (j = 0; j < nr_ids; j++) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 3611 | 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] | 3612 | goto out_errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3613 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 3614 | 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] | 3615 | } |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 3616 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3617 | lseek(fd, tmp, SEEK_SET); |
| 3618 | } |
| 3619 | |
Jiri Olsa | 29f5ffd | 2013-12-03 14:09:23 +0100 | [diff] [blame] | 3620 | perf_header__process_sections(header, fd, &session->tevent, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 3621 | perf_file_section__process); |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 3622 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 3623 | if (perf_evlist__prepare_tracepoint_events(session->evlist, |
Jiri Olsa | 29f5ffd | 2013-12-03 14:09:23 +0100 | [diff] [blame] | 3624 | session->tevent.pevent)) |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 3625 | goto out_delete_evlist; |
| 3626 | |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 3627 | return 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 3628 | out_errno: |
| 3629 | return -errno; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 3630 | |
| 3631 | out_delete_evlist: |
Jiri Olsa | c12995a | 2019-07-21 13:23:56 +0200 | [diff] [blame] | 3632 | evlist__delete(session->evlist); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 3633 | session->evlist = NULL; |
| 3634 | return -ENOMEM; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3635 | } |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 3636 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 3637 | int perf_event__synthesize_attr(struct perf_tool *tool, |
Robert Richter | f4d8343 | 2012-08-16 21:10:17 +0200 | [diff] [blame] | 3638 | struct perf_event_attr *attr, u32 ids, u64 *id, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 3639 | perf_event__handler_t process) |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 3640 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 3641 | union perf_event *ev; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 3642 | size_t size; |
| 3643 | int err; |
| 3644 | |
| 3645 | size = sizeof(struct perf_event_attr); |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 3646 | size = PERF_ALIGN(size, sizeof(u64)); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 3647 | size += sizeof(struct perf_event_header); |
| 3648 | size += ids * sizeof(u64); |
| 3649 | |
Numfor Mbiziwo-Tiapo | 20f9781 | 2019-07-24 16:44:58 -0700 | [diff] [blame] | 3650 | ev = zalloc(size); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 3651 | |
Chris Samuel | ce47dc5 | 2010-11-13 13:35:06 +1100 | [diff] [blame] | 3652 | if (ev == NULL) |
| 3653 | return -ENOMEM; |
| 3654 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 3655 | ev->attr.attr = *attr; |
| 3656 | memcpy(ev->attr.id, id, ids * sizeof(u64)); |
| 3657 | |
| 3658 | ev->attr.header.type = PERF_RECORD_HEADER_ATTR; |
Robert Richter | f4d8343 | 2012-08-16 21:10:17 +0200 | [diff] [blame] | 3659 | ev->attr.header.size = (u16)size; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 3660 | |
Robert Richter | f4d8343 | 2012-08-16 21:10:17 +0200 | [diff] [blame] | 3661 | if (ev->attr.header.size == size) |
| 3662 | err = process(tool, ev, NULL, NULL); |
| 3663 | else |
| 3664 | err = -E2BIG; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 3665 | |
| 3666 | free(ev); |
| 3667 | |
| 3668 | return err; |
| 3669 | } |
| 3670 | |
David Carrillo-Cisneros | e9def1b | 2017-07-17 21:25:48 -0700 | [diff] [blame] | 3671 | int perf_event__synthesize_features(struct perf_tool *tool, |
| 3672 | struct perf_session *session, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 3673 | struct evlist *evlist, |
David Carrillo-Cisneros | e9def1b | 2017-07-17 21:25:48 -0700 | [diff] [blame] | 3674 | perf_event__handler_t process) |
| 3675 | { |
| 3676 | struct perf_header *header = &session->header; |
| 3677 | struct feat_fd ff; |
| 3678 | struct feature_event *fe; |
| 3679 | size_t sz, sz_hdr; |
| 3680 | int feat, ret; |
| 3681 | |
| 3682 | sz_hdr = sizeof(fe->header); |
| 3683 | sz = sizeof(union perf_event); |
| 3684 | /* get a nice alignment */ |
| 3685 | sz = PERF_ALIGN(sz, page_size); |
| 3686 | |
| 3687 | memset(&ff, 0, sizeof(ff)); |
| 3688 | |
| 3689 | ff.buf = malloc(sz); |
| 3690 | if (!ff.buf) |
| 3691 | return -ENOMEM; |
| 3692 | |
| 3693 | ff.size = sz - sz_hdr; |
Song Liu | c952b35 | 2019-06-19 18:04:53 -0700 | [diff] [blame] | 3694 | ff.ph = &session->header; |
David Carrillo-Cisneros | e9def1b | 2017-07-17 21:25:48 -0700 | [diff] [blame] | 3695 | |
| 3696 | for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { |
| 3697 | if (!feat_ops[feat].synthesize) { |
| 3698 | pr_debug("No record header feature for header :%d\n", feat); |
| 3699 | continue; |
| 3700 | } |
| 3701 | |
| 3702 | ff.offset = sizeof(*fe); |
| 3703 | |
| 3704 | ret = feat_ops[feat].write(&ff, evlist); |
| 3705 | if (ret || ff.offset <= (ssize_t)sizeof(*fe)) { |
| 3706 | pr_debug("Error writing feature\n"); |
| 3707 | continue; |
| 3708 | } |
| 3709 | /* ff.buf may have changed due to realloc in do_write() */ |
| 3710 | fe = ff.buf; |
| 3711 | memset(fe, 0, sizeof(*fe)); |
| 3712 | |
| 3713 | fe->feat_id = feat; |
| 3714 | fe->header.type = PERF_RECORD_HEADER_FEATURE; |
| 3715 | fe->header.size = ff.offset; |
| 3716 | |
| 3717 | ret = process(tool, ff.buf, NULL, NULL); |
| 3718 | if (ret) { |
| 3719 | free(ff.buf); |
| 3720 | return ret; |
| 3721 | } |
| 3722 | } |
Jiri Olsa | 57b5de4 | 2018-03-14 10:22:05 +0100 | [diff] [blame] | 3723 | |
| 3724 | /* Send HEADER_LAST_FEATURE mark. */ |
| 3725 | fe = ff.buf; |
| 3726 | fe->feat_id = HEADER_LAST_FEATURE; |
| 3727 | fe->header.type = PERF_RECORD_HEADER_FEATURE; |
| 3728 | fe->header.size = sizeof(*fe); |
| 3729 | |
| 3730 | ret = process(tool, ff.buf, NULL, NULL); |
| 3731 | |
David Carrillo-Cisneros | e9def1b | 2017-07-17 21:25:48 -0700 | [diff] [blame] | 3732 | free(ff.buf); |
Jiri Olsa | 57b5de4 | 2018-03-14 10:22:05 +0100 | [diff] [blame] | 3733 | return ret; |
David Carrillo-Cisneros | e9def1b | 2017-07-17 21:25:48 -0700 | [diff] [blame] | 3734 | } |
| 3735 | |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 3736 | int perf_event__process_feature(struct perf_session *session, |
| 3737 | union perf_event *event) |
David Carrillo-Cisneros | e9def1b | 2017-07-17 21:25:48 -0700 | [diff] [blame] | 3738 | { |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 3739 | struct perf_tool *tool = session->tool; |
David Carrillo-Cisneros | e9def1b | 2017-07-17 21:25:48 -0700 | [diff] [blame] | 3740 | struct feat_fd ff = { .fd = 0 }; |
| 3741 | struct feature_event *fe = (struct feature_event *)event; |
| 3742 | int type = fe->header.type; |
| 3743 | u64 feat = fe->feat_id; |
| 3744 | |
| 3745 | if (type < 0 || type >= PERF_RECORD_HEADER_MAX) { |
| 3746 | pr_warning("invalid record type %d in pipe-mode\n", type); |
| 3747 | return 0; |
| 3748 | } |
Ravi Bangoria | 92ead7e | 2018-06-25 18:12:20 +0530 | [diff] [blame] | 3749 | if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) { |
David Carrillo-Cisneros | e9def1b | 2017-07-17 21:25:48 -0700 | [diff] [blame] | 3750 | pr_warning("invalid record type %d in pipe-mode\n", type); |
| 3751 | return -1; |
| 3752 | } |
| 3753 | |
| 3754 | if (!feat_ops[feat].process) |
| 3755 | return 0; |
| 3756 | |
| 3757 | ff.buf = (void *)fe->data; |
Jiri Olsa | 79b2fe5 | 2019-07-15 16:04:26 +0200 | [diff] [blame] | 3758 | ff.size = event->header.size - sizeof(*fe); |
David Carrillo-Cisneros | e9def1b | 2017-07-17 21:25:48 -0700 | [diff] [blame] | 3759 | ff.ph = &session->header; |
| 3760 | |
| 3761 | if (feat_ops[feat].process(&ff, NULL)) |
| 3762 | return -1; |
| 3763 | |
| 3764 | if (!feat_ops[feat].print || !tool->show_feat_hdr) |
| 3765 | return 0; |
| 3766 | |
| 3767 | if (!feat_ops[feat].full_only || |
| 3768 | tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) { |
| 3769 | feat_ops[feat].print(&ff, stdout); |
| 3770 | } else { |
| 3771 | fprintf(stdout, "# %s info available, use -I to display\n", |
| 3772 | feat_ops[feat].name); |
| 3773 | } |
| 3774 | |
| 3775 | return 0; |
| 3776 | } |
| 3777 | |
Jiri Olsa | a6e5281 | 2015-10-25 15:51:37 +0100 | [diff] [blame] | 3778 | static struct event_update_event * |
| 3779 | event_update_event__new(size_t size, u64 type, u64 id) |
| 3780 | { |
| 3781 | struct event_update_event *ev; |
| 3782 | |
| 3783 | size += sizeof(*ev); |
| 3784 | size = PERF_ALIGN(size, sizeof(u64)); |
| 3785 | |
| 3786 | ev = zalloc(size); |
| 3787 | if (ev) { |
| 3788 | ev->header.type = PERF_RECORD_EVENT_UPDATE; |
| 3789 | ev->header.size = (u16)size; |
| 3790 | ev->type = type; |
| 3791 | ev->id = id; |
| 3792 | } |
| 3793 | return ev; |
| 3794 | } |
| 3795 | |
| 3796 | int |
| 3797 | perf_event__synthesize_event_update_unit(struct perf_tool *tool, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 3798 | struct evsel *evsel, |
Jiri Olsa | a6e5281 | 2015-10-25 15:51:37 +0100 | [diff] [blame] | 3799 | perf_event__handler_t process) |
| 3800 | { |
| 3801 | struct event_update_event *ev; |
| 3802 | size_t size = strlen(evsel->unit); |
| 3803 | int err; |
| 3804 | |
| 3805 | ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]); |
| 3806 | if (ev == NULL) |
| 3807 | return -ENOMEM; |
| 3808 | |
Arnaldo Carvalho de Melo | 7572588 | 2018-12-06 11:02:57 -0300 | [diff] [blame] | 3809 | strlcpy(ev->data, evsel->unit, size + 1); |
Jiri Olsa | a6e5281 | 2015-10-25 15:51:37 +0100 | [diff] [blame] | 3810 | err = process(tool, (union perf_event *)ev, NULL, NULL); |
| 3811 | free(ev); |
| 3812 | return err; |
| 3813 | } |
| 3814 | |
Jiri Olsa | daeecbc | 2015-10-25 15:51:38 +0100 | [diff] [blame] | 3815 | int |
| 3816 | perf_event__synthesize_event_update_scale(struct perf_tool *tool, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 3817 | struct evsel *evsel, |
Jiri Olsa | daeecbc | 2015-10-25 15:51:38 +0100 | [diff] [blame] | 3818 | perf_event__handler_t process) |
| 3819 | { |
| 3820 | struct event_update_event *ev; |
| 3821 | struct event_update_event_scale *ev_data; |
| 3822 | int err; |
| 3823 | |
| 3824 | ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]); |
| 3825 | if (ev == NULL) |
| 3826 | return -ENOMEM; |
| 3827 | |
| 3828 | ev_data = (struct event_update_event_scale *) ev->data; |
| 3829 | ev_data->scale = evsel->scale; |
| 3830 | err = process(tool, (union perf_event*) ev, NULL, NULL); |
| 3831 | free(ev); |
| 3832 | return err; |
| 3833 | } |
| 3834 | |
Jiri Olsa | 802c904 | 2015-10-25 15:51:39 +0100 | [diff] [blame] | 3835 | int |
| 3836 | perf_event__synthesize_event_update_name(struct perf_tool *tool, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 3837 | struct evsel *evsel, |
Jiri Olsa | 802c904 | 2015-10-25 15:51:39 +0100 | [diff] [blame] | 3838 | perf_event__handler_t process) |
| 3839 | { |
| 3840 | struct event_update_event *ev; |
| 3841 | size_t len = strlen(evsel->name); |
| 3842 | int err; |
| 3843 | |
| 3844 | ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]); |
| 3845 | if (ev == NULL) |
| 3846 | return -ENOMEM; |
| 3847 | |
Arnaldo Carvalho de Melo | 5192bde | 2018-12-06 11:09:46 -0300 | [diff] [blame] | 3848 | strlcpy(ev->data, evsel->name, len + 1); |
Jiri Olsa | 802c904 | 2015-10-25 15:51:39 +0100 | [diff] [blame] | 3849 | err = process(tool, (union perf_event*) ev, NULL, NULL); |
| 3850 | free(ev); |
| 3851 | return err; |
| 3852 | } |
Jiri Olsa | daeecbc | 2015-10-25 15:51:38 +0100 | [diff] [blame] | 3853 | |
Jiri Olsa | 86ebb09 | 2015-10-25 15:51:40 +0100 | [diff] [blame] | 3854 | int |
| 3855 | perf_event__synthesize_event_update_cpus(struct perf_tool *tool, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 3856 | struct evsel *evsel, |
Jiri Olsa | 86ebb09 | 2015-10-25 15:51:40 +0100 | [diff] [blame] | 3857 | perf_event__handler_t process) |
| 3858 | { |
| 3859 | size_t size = sizeof(struct event_update_event); |
| 3860 | struct event_update_event *ev; |
| 3861 | int max, err; |
| 3862 | u16 type; |
| 3863 | |
| 3864 | if (!evsel->own_cpus) |
| 3865 | return 0; |
| 3866 | |
| 3867 | ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max); |
| 3868 | if (!ev) |
| 3869 | return -ENOMEM; |
| 3870 | |
| 3871 | ev->header.type = PERF_RECORD_EVENT_UPDATE; |
| 3872 | ev->header.size = (u16)size; |
| 3873 | ev->type = PERF_EVENT_UPDATE__CPUS; |
| 3874 | ev->id = evsel->id[0]; |
| 3875 | |
| 3876 | cpu_map_data__synthesize((struct cpu_map_data *) ev->data, |
| 3877 | evsel->own_cpus, |
| 3878 | type, max); |
| 3879 | |
| 3880 | err = process(tool, (union perf_event*) ev, NULL, NULL); |
| 3881 | free(ev); |
| 3882 | return err; |
| 3883 | } |
| 3884 | |
Jiri Olsa | c853f93 | 2015-10-25 15:51:41 +0100 | [diff] [blame] | 3885 | size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp) |
| 3886 | { |
| 3887 | struct event_update_event *ev = &event->event_update; |
| 3888 | struct event_update_event_scale *ev_scale; |
| 3889 | struct event_update_event_cpus *ev_cpus; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 3890 | struct perf_cpu_map *map; |
Jiri Olsa | c853f93 | 2015-10-25 15:51:41 +0100 | [diff] [blame] | 3891 | size_t ret; |
| 3892 | |
| 3893 | ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id); |
| 3894 | |
| 3895 | switch (ev->type) { |
| 3896 | case PERF_EVENT_UPDATE__SCALE: |
| 3897 | ev_scale = (struct event_update_event_scale *) ev->data; |
| 3898 | ret += fprintf(fp, "... scale: %f\n", ev_scale->scale); |
| 3899 | break; |
| 3900 | case PERF_EVENT_UPDATE__UNIT: |
| 3901 | ret += fprintf(fp, "... unit: %s\n", ev->data); |
| 3902 | break; |
| 3903 | case PERF_EVENT_UPDATE__NAME: |
| 3904 | ret += fprintf(fp, "... name: %s\n", ev->data); |
| 3905 | break; |
| 3906 | case PERF_EVENT_UPDATE__CPUS: |
| 3907 | ev_cpus = (struct event_update_event_cpus *) ev->data; |
| 3908 | ret += fprintf(fp, "... "); |
| 3909 | |
| 3910 | map = cpu_map__new_data(&ev_cpus->cpus); |
| 3911 | if (map) |
| 3912 | ret += cpu_map__fprintf(map, fp); |
| 3913 | else |
| 3914 | ret += fprintf(fp, "failed to get cpus\n"); |
| 3915 | break; |
| 3916 | default: |
| 3917 | ret += fprintf(fp, "... unknown type\n"); |
| 3918 | break; |
| 3919 | } |
| 3920 | |
| 3921 | return ret; |
| 3922 | } |
Jiri Olsa | 86ebb09 | 2015-10-25 15:51:40 +0100 | [diff] [blame] | 3923 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 3924 | int perf_event__synthesize_attrs(struct perf_tool *tool, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 3925 | struct evlist *evlist, |
Jiri Olsa | 318ec18 | 2018-08-30 08:32:15 +0200 | [diff] [blame] | 3926 | perf_event__handler_t process) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 3927 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 3928 | struct evsel *evsel; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 3929 | int err = 0; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 3930 | |
Jiri Olsa | 318ec18 | 2018-08-30 08:32:15 +0200 | [diff] [blame] | 3931 | evlist__for_each_entry(evlist, evsel) { |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 3932 | err = perf_event__synthesize_attr(tool, &evsel->core.attr, evsel->ids, |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 3933 | evsel->id, process); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 3934 | if (err) { |
| 3935 | pr_debug("failed to create perf header attribute\n"); |
| 3936 | return err; |
| 3937 | } |
| 3938 | } |
| 3939 | |
| 3940 | return err; |
| 3941 | } |
| 3942 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 3943 | static bool has_unit(struct evsel *counter) |
Andi Kleen | bfd8f72 | 2017-11-17 13:42:58 -0800 | [diff] [blame] | 3944 | { |
| 3945 | return counter->unit && *counter->unit; |
| 3946 | } |
| 3947 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 3948 | static bool has_scale(struct evsel *counter) |
Andi Kleen | bfd8f72 | 2017-11-17 13:42:58 -0800 | [diff] [blame] | 3949 | { |
| 3950 | return counter->scale != 1; |
| 3951 | } |
| 3952 | |
| 3953 | int perf_event__synthesize_extra_attr(struct perf_tool *tool, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 3954 | struct evlist *evsel_list, |
Andi Kleen | bfd8f72 | 2017-11-17 13:42:58 -0800 | [diff] [blame] | 3955 | perf_event__handler_t process, |
| 3956 | bool is_pipe) |
| 3957 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 3958 | struct evsel *counter; |
Andi Kleen | bfd8f72 | 2017-11-17 13:42:58 -0800 | [diff] [blame] | 3959 | int err; |
| 3960 | |
| 3961 | /* |
| 3962 | * Synthesize other events stuff not carried within |
| 3963 | * attr event - unit, scale, name |
| 3964 | */ |
| 3965 | evlist__for_each_entry(evsel_list, counter) { |
| 3966 | if (!counter->supported) |
| 3967 | continue; |
| 3968 | |
| 3969 | /* |
| 3970 | * Synthesize unit and scale only if it's defined. |
| 3971 | */ |
| 3972 | if (has_unit(counter)) { |
| 3973 | err = perf_event__synthesize_event_update_unit(tool, counter, process); |
| 3974 | if (err < 0) { |
| 3975 | pr_err("Couldn't synthesize evsel unit.\n"); |
| 3976 | return err; |
| 3977 | } |
| 3978 | } |
| 3979 | |
| 3980 | if (has_scale(counter)) { |
| 3981 | err = perf_event__synthesize_event_update_scale(tool, counter, process); |
| 3982 | if (err < 0) { |
| 3983 | pr_err("Couldn't synthesize evsel counter.\n"); |
| 3984 | return err; |
| 3985 | } |
| 3986 | } |
| 3987 | |
| 3988 | if (counter->own_cpus) { |
| 3989 | err = perf_event__synthesize_event_update_cpus(tool, counter, process); |
| 3990 | if (err < 0) { |
| 3991 | pr_err("Couldn't synthesize evsel cpus.\n"); |
| 3992 | return err; |
| 3993 | } |
| 3994 | } |
| 3995 | |
| 3996 | /* |
| 3997 | * Name is needed only for pipe output, |
| 3998 | * perf.data carries event names. |
| 3999 | */ |
| 4000 | if (is_pipe) { |
| 4001 | err = perf_event__synthesize_event_update_name(tool, counter, process); |
| 4002 | if (err < 0) { |
| 4003 | pr_err("Couldn't synthesize evsel name.\n"); |
| 4004 | return err; |
| 4005 | } |
| 4006 | } |
| 4007 | } |
| 4008 | return 0; |
| 4009 | } |
| 4010 | |
Adrian Hunter | 47c3d10 | 2013-07-04 16:20:21 +0300 | [diff] [blame] | 4011 | int perf_event__process_attr(struct perf_tool *tool __maybe_unused, |
| 4012 | union perf_event *event, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 4013 | struct evlist **pevlist) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 4014 | { |
Robert Richter | f4d8343 | 2012-08-16 21:10:17 +0200 | [diff] [blame] | 4015 | u32 i, ids, n_ids; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 4016 | struct evsel *evsel; |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 4017 | struct evlist *evlist = *pevlist; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 4018 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 4019 | if (evlist == NULL) { |
Jiri Olsa | 0f98b11 | 2019-07-21 13:23:55 +0200 | [diff] [blame] | 4020 | *pevlist = evlist = evlist__new(); |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 4021 | if (evlist == NULL) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 4022 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 4023 | } |
| 4024 | |
Jiri Olsa | 365c3ae | 2019-07-21 13:23:58 +0200 | [diff] [blame] | 4025 | evsel = evsel__new(&event->attr.attr); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 4026 | if (evsel == NULL) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 4027 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 4028 | |
Jiri Olsa | a1cf3a7 | 2019-07-21 13:23:59 +0200 | [diff] [blame] | 4029 | evlist__add(evlist, evsel); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 4030 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 4031 | ids = event->header.size; |
| 4032 | ids -= (void *)&event->attr.id - (void *)event; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 4033 | n_ids = ids / sizeof(u64); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 4034 | /* |
| 4035 | * We don't have the cpu and thread maps on the header, so |
| 4036 | * for allocating the perf_sample_id table we fake 1 cpu and |
| 4037 | * hattr->ids threads. |
| 4038 | */ |
| 4039 | if (perf_evsel__alloc_id(evsel, 1, n_ids)) |
| 4040 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 4041 | |
| 4042 | for (i = 0; i < n_ids; i++) { |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 4043 | perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 4044 | } |
| 4045 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 4046 | return 0; |
| 4047 | } |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 4048 | |
Jiri Olsa | ffe77725 | 2015-10-25 15:51:36 +0100 | [diff] [blame] | 4049 | int perf_event__process_event_update(struct perf_tool *tool __maybe_unused, |
| 4050 | union perf_event *event, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 4051 | struct evlist **pevlist) |
Jiri Olsa | ffe77725 | 2015-10-25 15:51:36 +0100 | [diff] [blame] | 4052 | { |
| 4053 | struct event_update_event *ev = &event->event_update; |
Jiri Olsa | daeecbc | 2015-10-25 15:51:38 +0100 | [diff] [blame] | 4054 | struct event_update_event_scale *ev_scale; |
Jiri Olsa | 86ebb09 | 2015-10-25 15:51:40 +0100 | [diff] [blame] | 4055 | struct event_update_event_cpus *ev_cpus; |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 4056 | struct evlist *evlist; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 4057 | struct evsel *evsel; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 4058 | struct perf_cpu_map *map; |
Jiri Olsa | ffe77725 | 2015-10-25 15:51:36 +0100 | [diff] [blame] | 4059 | |
| 4060 | if (!pevlist || *pevlist == NULL) |
| 4061 | return -EINVAL; |
| 4062 | |
| 4063 | evlist = *pevlist; |
| 4064 | |
| 4065 | evsel = perf_evlist__id2evsel(evlist, ev->id); |
| 4066 | if (evsel == NULL) |
| 4067 | return -EINVAL; |
| 4068 | |
Jiri Olsa | a6e5281 | 2015-10-25 15:51:37 +0100 | [diff] [blame] | 4069 | switch (ev->type) { |
| 4070 | case PERF_EVENT_UPDATE__UNIT: |
| 4071 | evsel->unit = strdup(ev->data); |
Jiri Olsa | daeecbc | 2015-10-25 15:51:38 +0100 | [diff] [blame] | 4072 | break; |
Jiri Olsa | 802c904 | 2015-10-25 15:51:39 +0100 | [diff] [blame] | 4073 | case PERF_EVENT_UPDATE__NAME: |
| 4074 | evsel->name = strdup(ev->data); |
| 4075 | break; |
Jiri Olsa | daeecbc | 2015-10-25 15:51:38 +0100 | [diff] [blame] | 4076 | case PERF_EVENT_UPDATE__SCALE: |
| 4077 | ev_scale = (struct event_update_event_scale *) ev->data; |
| 4078 | evsel->scale = ev_scale->scale; |
Arnaldo Carvalho de Melo | 8434a2e | 2017-02-08 21:57:22 -0300 | [diff] [blame] | 4079 | break; |
Jiri Olsa | 86ebb09 | 2015-10-25 15:51:40 +0100 | [diff] [blame] | 4080 | case PERF_EVENT_UPDATE__CPUS: |
| 4081 | ev_cpus = (struct event_update_event_cpus *) ev->data; |
| 4082 | |
| 4083 | map = cpu_map__new_data(&ev_cpus->cpus); |
| 4084 | if (map) |
| 4085 | evsel->own_cpus = map; |
| 4086 | else |
| 4087 | pr_err("failed to get event_update cpus\n"); |
Jiri Olsa | a6e5281 | 2015-10-25 15:51:37 +0100 | [diff] [blame] | 4088 | default: |
| 4089 | break; |
| 4090 | } |
| 4091 | |
Jiri Olsa | ffe77725 | 2015-10-25 15:51:36 +0100 | [diff] [blame] | 4092 | return 0; |
| 4093 | } |
| 4094 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 4095 | int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 4096 | struct evlist *evlist, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 4097 | perf_event__handler_t process) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4098 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 4099 | union perf_event ev; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 4100 | struct tracing_data *tdata; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4101 | ssize_t size = 0, aligned_size = 0, padding; |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 4102 | struct feat_fd ff; |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 4103 | int err __maybe_unused = 0; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4104 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 4105 | /* |
| 4106 | * We are going to store the size of the data followed |
| 4107 | * by the data contents. Since the fd descriptor is a pipe, |
| 4108 | * we cannot seek back to store the size of the data once |
| 4109 | * we know it. Instead we: |
| 4110 | * |
| 4111 | * - write the tracing data to the temp file |
| 4112 | * - get/write the data size to pipe |
| 4113 | * - write the tracing data from the temp file |
| 4114 | * to the pipe |
| 4115 | */ |
Jiri Olsa | ce9036a | 2019-07-21 13:24:23 +0200 | [diff] [blame] | 4116 | tdata = tracing_data_get(&evlist->core.entries, fd, true); |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 4117 | if (!tdata) |
| 4118 | return -1; |
| 4119 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4120 | memset(&ev, 0, sizeof(ev)); |
| 4121 | |
| 4122 | ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 4123 | size = tdata->size; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 4124 | aligned_size = PERF_ALIGN(size, sizeof(u64)); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4125 | padding = aligned_size - size; |
| 4126 | ev.tracing_data.header.size = sizeof(ev.tracing_data); |
| 4127 | ev.tracing_data.size = aligned_size; |
| 4128 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 4129 | process(tool, &ev, NULL, NULL); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4130 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 4131 | /* |
| 4132 | * The put function will copy all the tracing data |
| 4133 | * stored in temp file to the pipe. |
| 4134 | */ |
| 4135 | tracing_data_put(tdata); |
| 4136 | |
David Carrillo-Cisneros | ccebbeb | 2017-07-17 21:25:39 -0700 | [diff] [blame] | 4137 | ff = (struct feat_fd){ .fd = fd }; |
| 4138 | if (write_padded(&ff, NULL, 0, padding)) |
David Carrillo-Cisneros | 2ff5365 | 2017-07-17 21:25:36 -0700 | [diff] [blame] | 4139 | return -1; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4140 | |
| 4141 | return aligned_size; |
| 4142 | } |
| 4143 | |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 4144 | int perf_event__process_tracing_data(struct perf_session *session, |
| 4145 | union perf_event *event) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4146 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 4147 | ssize_t size_read, padding, size = event->tracing_data.size; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 4148 | int fd = perf_data__fd(session->data); |
Jiri Olsa | cc9784bd | 2013-10-15 16:27:34 +0200 | [diff] [blame] | 4149 | off_t offset = lseek(fd, 0, SEEK_CUR); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4150 | char buf[BUFSIZ]; |
| 4151 | |
| 4152 | /* setup for reading amidst mmap */ |
Jiri Olsa | cc9784bd | 2013-10-15 16:27:34 +0200 | [diff] [blame] | 4153 | lseek(fd, offset + sizeof(struct tracing_data_event), |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4154 | SEEK_SET); |
| 4155 | |
Jiri Olsa | 29f5ffd | 2013-12-03 14:09:23 +0100 | [diff] [blame] | 4156 | size_read = trace_report(fd, &session->tevent, |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 4157 | session->repipe); |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 4158 | padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4159 | |
Jiri Olsa | cc9784bd | 2013-10-15 16:27:34 +0200 | [diff] [blame] | 4160 | if (readn(fd, buf, padding) < 0) { |
Arnaldo Carvalho de Melo | 2caa48a | 2013-01-24 22:34:33 -0300 | [diff] [blame] | 4161 | pr_err("%s: reading input file", __func__); |
| 4162 | return -1; |
| 4163 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 4164 | if (session->repipe) { |
| 4165 | int retw = write(STDOUT_FILENO, buf, padding); |
Arnaldo Carvalho de Melo | 2caa48a | 2013-01-24 22:34:33 -0300 | [diff] [blame] | 4166 | if (retw <= 0 || retw != padding) { |
| 4167 | pr_err("%s: repiping tracing data padding", __func__); |
| 4168 | return -1; |
| 4169 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 4170 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4171 | |
Arnaldo Carvalho de Melo | 2caa48a | 2013-01-24 22:34:33 -0300 | [diff] [blame] | 4172 | if (size_read + padding != size) { |
| 4173 | pr_err("%s: tracing data size mismatch", __func__); |
| 4174 | return -1; |
| 4175 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4176 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 4177 | perf_evlist__prepare_tracepoint_events(session->evlist, |
Jiri Olsa | 29f5ffd | 2013-12-03 14:09:23 +0100 | [diff] [blame] | 4178 | session->tevent.pevent); |
Arnaldo Carvalho de Melo | 8b6ee4c | 2012-08-07 23:36:16 -0300 | [diff] [blame] | 4179 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 4180 | return size_read + padding; |
| 4181 | } |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 4182 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 4183 | int perf_event__synthesize_build_id(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 4184 | struct dso *pos, u16 misc, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 4185 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 4186 | struct machine *machine) |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 4187 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 4188 | union perf_event ev; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 4189 | size_t len; |
| 4190 | int err = 0; |
| 4191 | |
| 4192 | if (!pos->hit) |
| 4193 | return err; |
| 4194 | |
| 4195 | memset(&ev, 0, sizeof(ev)); |
| 4196 | |
| 4197 | len = pos->long_name_len + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 4198 | len = PERF_ALIGN(len, NAME_ALIGN); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 4199 | memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id)); |
| 4200 | ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID; |
| 4201 | ev.build_id.header.misc = misc; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 4202 | ev.build_id.pid = machine->pid; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 4203 | ev.build_id.header.size = sizeof(ev.build_id) + len; |
| 4204 | memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len); |
| 4205 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 4206 | err = process(tool, &ev, NULL, machine); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 4207 | |
| 4208 | return err; |
| 4209 | } |
| 4210 | |
Jiri Olsa | 89f1688 | 2018-09-13 14:54:03 +0200 | [diff] [blame] | 4211 | int perf_event__process_build_id(struct perf_session *session, |
| 4212 | union perf_event *event) |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 4213 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 4214 | __event_process_build_id(&event->build_id, |
| 4215 | event->build_id.filename, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 4216 | session); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 4217 | return 0; |
| 4218 | } |