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