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