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