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