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