blob: 86d9396cb131cdca6731da256f3a10460827b6cf [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -03004#include "string2.h"
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -03005#include <sys/param.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02006#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02007#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02008#include <unistd.h>
9#include <stdio.h>
10#include <stdlib.h>
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -030011#include <linux/compiler.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010012#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -020013#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +010014#include <linux/bitops.h>
Arnaldo Carvalho de Melofc6a1722019-06-25 21:33:14 -030015#include <linux/string.h>
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -070016#include <linux/stringify.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030017#include <linux/zalloc.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030018#include <sys/stat.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020019#include <sys/utsname.h>
Jin Yao60115182017-12-08 21:13:41 +080020#include <linux/time64.h>
Jiri Olsae2091ce2018-03-07 16:50:08 +010021#include <dirent.h>
Song Liu606f9722019-03-11 22:30:43 -070022#include <bpf/libbpf.h>
Jiri Olsa9c3516d2019-07-21 13:24:30 +020023#include <perf/cpumap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020024
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -030025#include "dso.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020026#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030027#include "evsel.h"
Arnaldo Carvalho de Meloca125272019-09-24 15:41:51 -030028#include "util/evsel_fprintf.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020029#include "header.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030030#include "memswap.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020031#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020032#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010033#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010034#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020035#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020036#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020037#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090038#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020039#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020040#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010041#include <api/fs/fs.h>
42#include "asm/bug.h"
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -070043#include "tool.h"
Jin Yao60115182017-12-08 21:13:41 +080044#include "time-utils.h"
Jiri Olsae2091ce2018-03-07 16:50:08 +010045#include "units.h"
Jiri Olsa20f2be12019-08-06 15:25:25 +020046#include "util/util.h" // perf_exe()
Jiri Olsa5135d5e2019-02-19 10:58:13 +010047#include "cputopo.h"
Song Liu606f9722019-03-11 22:30:43 -070048#include "bpf-event.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020049
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030050#include <linux/ctype.h>
Arnaldo Carvalho de Melofb71c86c2019-09-03 10:56:06 -030051#include <internal/lib.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030052
Stephane Eranian73323f52012-02-02 13:54:44 +010053/*
54 * magic2 = "PERFILE2"
55 * must be a numerical value to let the endianness
56 * determine the memory layout. That way we are able
57 * to detect endianness when reading the perf.data file
58 * back.
59 *
60 * we check for legacy (PERFFILE) format.
61 */
62static const char *__perf_magic1 = "PERFFILE";
63static const u64 __perf_magic2 = 0x32454c4946524550ULL;
64static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020065
Stephane Eranian73323f52012-02-02 13:54:44 +010066#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020067
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090068const char perf_version_string[] = PERF_VERSION;
69
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020070struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020071 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020072 struct perf_file_section ids;
73};
74
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030075void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020076{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030077 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020078}
79
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030080void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020081{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030082 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020083}
84
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030085bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020086{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030087 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020088}
89
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -070090static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
91{
92 ssize_t ret = writen(ff->fd, buf, size);
93
94 if (ret != (ssize_t)size)
95 return ret < 0 ? (int)ret : -1;
96 return 0;
97}
98
99static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
100{
101 /* struct perf_event_header::size is u16 */
102 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
103 size_t new_size = ff->size;
104 void *addr;
105
106 if (size + ff->offset > max_size)
107 return -E2BIG;
108
109 while (size > (new_size - ff->offset))
110 new_size <<= 1;
111 new_size = min(max_size, new_size);
112
113 if (ff->size < new_size) {
114 addr = realloc(ff->buf, new_size);
115 if (!addr)
116 return -ENOMEM;
117 ff->buf = addr;
118 ff->size = new_size;
119 }
120
121 memcpy(ff->buf + ff->offset, buf, size);
122 ff->offset += size;
123
124 return 0;
125}
126
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700127/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700128int do_write(struct feat_fd *ff, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200129{
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700130 if (!ff->buf)
131 return __do_write_fd(ff, buf, size);
132 return __do_write_buf(ff, buf, size);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200133}
134
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700135/* Return: 0 if succeded, -ERR if failed. */
Jiri Olsae2091ce2018-03-07 16:50:08 +0100136static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
137{
138 u64 *p = (u64 *) set;
139 int i, ret;
140
141 ret = do_write(ff, &size, sizeof(size));
142 if (ret < 0)
143 return ret;
144
145 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
146 ret = do_write(ff, p + i, sizeof(*p));
147 if (ret < 0)
148 return ret;
149 }
150
151 return 0;
152}
153
154/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700155int write_padded(struct feat_fd *ff, const void *bf,
156 size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200157{
158 static const char zero_buf[NAME_ALIGN];
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700159 int err = do_write(ff, bf, count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200160
161 if (!err)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700162 err = do_write(ff, zero_buf, count_aligned - count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200163
164 return err;
165}
166
Kan Liang2bb00d22015-09-01 09:58:12 -0400167#define string_size(str) \
168 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
169
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700170/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700171static int do_write_string(struct feat_fd *ff, const char *str)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200172{
173 u32 len, olen;
174 int ret;
175
176 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300177 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200178
179 /* write len, incl. \0 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700180 ret = do_write(ff, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200181 if (ret < 0)
182 return ret;
183
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700184 return write_padded(ff, str, olen, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200185}
186
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700187static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700188{
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700189 ssize_t ret = readn(ff->fd, addr, size);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700190
191 if (ret != size)
192 return ret < 0 ? (int)ret : -1;
193 return 0;
194}
195
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700196static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
197{
198 if (size > (ssize_t)ff->size - ff->offset)
199 return -1;
200
201 memcpy(addr, ff->buf + ff->offset, size);
202 ff->offset += size;
203
204 return 0;
205
206}
207
208static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
209{
210 if (!ff->buf)
211 return __do_read_fd(ff, addr, size);
212 return __do_read_buf(ff, addr, size);
213}
214
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700215static int do_read_u32(struct feat_fd *ff, u32 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700216{
217 int ret;
218
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700219 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700220 if (ret)
221 return ret;
222
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700223 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700224 *addr = bswap_32(*addr);
225 return 0;
226}
227
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700228static int do_read_u64(struct feat_fd *ff, u64 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700229{
230 int ret;
231
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700232 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700233 if (ret)
234 return ret;
235
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700236 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700237 *addr = bswap_64(*addr);
238 return 0;
239}
240
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700241static char *do_read_string(struct feat_fd *ff)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200242{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200243 u32 len;
244 char *buf;
245
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700246 if (do_read_u32(ff, &len))
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200247 return NULL;
248
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200249 buf = malloc(len);
250 if (!buf)
251 return NULL;
252
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700253 if (!__do_read(ff, buf, len)) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200254 /*
255 * strings are padded by zeroes
256 * thus the actual strlen of buf
257 * may be less than len
258 */
259 return buf;
260 }
261
262 free(buf);
263 return NULL;
264}
265
Jiri Olsae2091ce2018-03-07 16:50:08 +0100266/* Return: 0 if succeded, -ERR if failed. */
267static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
268{
269 unsigned long *set;
270 u64 size, *p;
271 int i, ret;
272
273 ret = do_read_u64(ff, &size);
274 if (ret)
275 return ret;
276
277 set = bitmap_alloc(size);
278 if (!set)
279 return -ENOMEM;
280
Jiri Olsae2091ce2018-03-07 16:50:08 +0100281 p = (u64 *) set;
282
283 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
284 ret = do_read_u64(ff, p + i);
285 if (ret < 0) {
286 free(set);
287 return ret;
288 }
289 }
290
291 *pset = set;
292 *psize = size;
293 return 0;
294}
295
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700296static int write_tracing_data(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200297 struct evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200298{
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700299 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
300 return -1;
301
Jiri Olsace9036a2019-07-21 13:24:23 +0200302 return read_tracing_data(ff->fd, &evlist->core.entries);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200303}
304
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700305static int write_build_id(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200306 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200307{
308 struct perf_session *session;
309 int err;
310
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700311 session = container_of(ff->ph, struct perf_session, header);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200312
Robert Richtere20960c2011-12-07 10:02:55 +0100313 if (!perf_session__read_build_ids(session, true))
314 return -1;
315
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700316 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
317 return -1;
318
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700319 err = perf_session__write_buildid_table(session, ff);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200320 if (err < 0) {
321 pr_debug("failed to write buildid table\n");
322 return err;
323 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900324 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200325
326 return 0;
327}
328
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700329static int write_hostname(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200330 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200331{
332 struct utsname uts;
333 int ret;
334
335 ret = uname(&uts);
336 if (ret < 0)
337 return -1;
338
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700339 return do_write_string(ff, uts.nodename);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200340}
341
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700342static int write_osrelease(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200343 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200344{
345 struct utsname uts;
346 int ret;
347
348 ret = uname(&uts);
349 if (ret < 0)
350 return -1;
351
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700352 return do_write_string(ff, uts.release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200353}
354
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700355static int write_arch(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200356 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200357{
358 struct utsname uts;
359 int ret;
360
361 ret = uname(&uts);
362 if (ret < 0)
363 return -1;
364
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700365 return do_write_string(ff, uts.machine);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200366}
367
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700368static int write_version(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200369 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200370{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700371 return do_write_string(ff, perf_version_string);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200372}
373
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700374static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200375{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200376 FILE *file;
377 char *buf = NULL;
378 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800379 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200380 size_t len = 0;
381 int ret = -1;
382
383 if (!search)
384 return -1;
385
386 file = fopen("/proc/cpuinfo", "r");
387 if (!file)
388 return -1;
389
390 while (getline(&buf, &len, file) > 0) {
391 ret = strncmp(buf, search, strlen(search));
392 if (!ret)
393 break;
394 }
395
Wang Naned307752014-10-16 11:08:29 +0800396 if (ret) {
397 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200398 goto done;
Wang Naned307752014-10-16 11:08:29 +0800399 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200400
401 s = buf;
402
403 p = strchr(buf, ':');
404 if (p && *(p+1) == ' ' && *(p+2))
405 s = p + 2;
406 p = strchr(s, '\n');
407 if (p)
408 *p = '\0';
409
410 /* squash extra space characters (branding string) */
411 p = s;
412 while (*p) {
413 if (isspace(*p)) {
414 char *r = p + 1;
Arnaldo Carvalho de Melofc6a1722019-06-25 21:33:14 -0300415 char *q = skip_spaces(r);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200416 *p = ' ';
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200417 if (q != (p+1))
418 while ((*r++ = *q++));
419 }
420 p++;
421 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700422 ret = do_write_string(ff, s);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200423done:
424 free(buf);
425 fclose(file);
426 return ret;
427}
428
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700429static int write_cpudesc(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200430 struct evlist *evlist __maybe_unused)
Wang Nan493c3032014-10-24 09:45:26 +0800431{
Arnaldo Carvalho de Meloa7749402019-08-29 14:40:28 -0300432#if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__)
433#define CPUINFO_PROC { "cpu", }
434#elif defined(__s390__)
435#define CPUINFO_PROC { "vendor_id", }
436#elif defined(__sh__)
437#define CPUINFO_PROC { "cpu type", }
438#elif defined(__alpha__) || defined(__mips__)
439#define CPUINFO_PROC { "cpu model", }
440#elif defined(__arm__)
441#define CPUINFO_PROC { "model name", "Processor", }
442#elif defined(__arc__)
443#define CPUINFO_PROC { "Processor", }
444#elif defined(__xtensa__)
445#define CPUINFO_PROC { "core ID", }
446#else
447#define CPUINFO_PROC { "model name", }
448#endif
Wang Nan493c3032014-10-24 09:45:26 +0800449 const char *cpuinfo_procs[] = CPUINFO_PROC;
Arnaldo Carvalho de Meloa7749402019-08-29 14:40:28 -0300450#undef CPUINFO_PROC
Wang Nan493c3032014-10-24 09:45:26 +0800451 unsigned int i;
452
453 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
454 int ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700455 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
Wang Nan493c3032014-10-24 09:45:26 +0800456 if (ret >= 0)
457 return ret;
458 }
459 return -1;
460}
461
462
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700463static int write_nrcpus(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200464 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200465{
466 long nr;
467 u32 nrc, nra;
468 int ret;
469
Jan Stancekda8a58b2017-02-17 12:10:26 +0100470 nrc = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200471
472 nr = sysconf(_SC_NPROCESSORS_ONLN);
473 if (nr < 0)
474 return -1;
475
476 nra = (u32)(nr & UINT_MAX);
477
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700478 ret = do_write(ff, &nrc, sizeof(nrc));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200479 if (ret < 0)
480 return ret;
481
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700482 return do_write(ff, &nra, sizeof(nra));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200483}
484
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700485static int write_event_desc(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200486 struct evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200487{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200488 struct evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900489 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200490 int ret;
491
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200492 nre = evlist->core.nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200493
494 /*
495 * write number of events
496 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700497 ret = do_write(ff, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200498 if (ret < 0)
499 return ret;
500
501 /*
502 * size of perf_event_attr struct
503 */
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200504 sz = (u32)sizeof(evsel->core.attr);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700505 ret = do_write(ff, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200506 if (ret < 0)
507 return ret;
508
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300509 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200510 ret = do_write(ff, &evsel->core.attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200511 if (ret < 0)
512 return ret;
513 /*
514 * write number of unique id per event
515 * there is one id per instance of an event
516 *
517 * copy into an nri to be independent of the
518 * type of ids,
519 */
Jiri Olsae7eb9002019-09-02 22:15:47 +0200520 nri = evsel->core.ids;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700521 ret = do_write(ff, &nri, sizeof(nri));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200522 if (ret < 0)
523 return ret;
524
525 /*
526 * write event string as passed on cmdline
527 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700528 ret = do_write_string(ff, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200529 if (ret < 0)
530 return ret;
531 /*
532 * write unique ids for this event
533 */
Jiri Olsae7eb9002019-09-02 22:15:47 +0200534 ret = do_write(ff, evsel->core.id, evsel->core.ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200535 if (ret < 0)
536 return ret;
537 }
538 return 0;
539}
540
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700541static int write_cmdline(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200542 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200543{
Andi Kleen94816ad2019-02-24 07:37:19 -0800544 char pbuf[MAXPATHLEN], *buf;
545 int i, ret, n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200546
Tommi Rantala55f771282017-03-22 15:06:24 +0200547 /* actual path to perf binary */
Andi Kleen94816ad2019-02-24 07:37:19 -0800548 buf = perf_exe(pbuf, MAXPATHLEN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200549
550 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300551 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200552
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700553 ret = do_write(ff, &n, sizeof(n));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200554 if (ret < 0)
555 return ret;
556
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700557 ret = do_write_string(ff, buf);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200558 if (ret < 0)
559 return ret;
560
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300561 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700562 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200563 if (ret < 0)
564 return ret;
565 }
566 return 0;
567}
568
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200569
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700570static int write_cpu_topology(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200571 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200572{
Jiri Olsa5135d5e2019-02-19 10:58:13 +0100573 struct cpu_topology *tp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200574 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300575 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200576
Jiri Olsa5135d5e2019-02-19 10:58:13 +0100577 tp = cpu_topology__new();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200578 if (!tp)
579 return -1;
580
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700581 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200582 if (ret < 0)
583 goto done;
584
585 for (i = 0; i < tp->core_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700586 ret = do_write_string(ff, tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200587 if (ret < 0)
588 goto done;
589 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700590 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200591 if (ret < 0)
592 goto done;
593
594 for (i = 0; i < tp->thread_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700595 ret = do_write_string(ff, tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200596 if (ret < 0)
597 break;
598 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400599
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300600 ret = perf_env__read_cpu_topology_map(&perf_env);
601 if (ret < 0)
602 goto done;
603
604 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700605 ret = do_write(ff, &perf_env.cpu[j].core_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300606 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400607 if (ret < 0)
608 return ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700609 ret = do_write(ff, &perf_env.cpu[j].socket_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300610 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400611 if (ret < 0)
612 return ret;
613 }
Kan Liangacae8b32019-06-04 15:50:41 -0700614
615 if (!tp->die_sib)
616 goto done;
617
618 ret = do_write(ff, &tp->die_sib, sizeof(tp->die_sib));
619 if (ret < 0)
620 goto done;
621
622 for (i = 0; i < tp->die_sib; i++) {
623 ret = do_write_string(ff, tp->die_siblings[i]);
624 if (ret < 0)
625 goto done;
626 }
627
628 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
629 ret = do_write(ff, &perf_env.cpu[j].die_id,
630 sizeof(perf_env.cpu[j].die_id));
631 if (ret < 0)
632 return ret;
633 }
634
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200635done:
Jiri Olsa5135d5e2019-02-19 10:58:13 +0100636 cpu_topology__delete(tp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200637 return ret;
638}
639
640
641
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700642static int write_total_mem(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200643 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200644{
645 char *buf = NULL;
646 FILE *fp;
647 size_t len = 0;
648 int ret = -1, n;
649 uint64_t mem;
650
651 fp = fopen("/proc/meminfo", "r");
652 if (!fp)
653 return -1;
654
655 while (getline(&buf, &len, fp) > 0) {
656 ret = strncmp(buf, "MemTotal:", 9);
657 if (!ret)
658 break;
659 }
660 if (!ret) {
661 n = sscanf(buf, "%*s %"PRIu64, &mem);
662 if (n == 1)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700663 ret = do_write(ff, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800664 } else
665 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200666 free(buf);
667 fclose(fp);
668 return ret;
669}
670
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700671static int write_numa_topology(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200672 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200673{
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100674 struct numa_topology *tp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200675 int ret = -1;
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100676 u32 i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200677
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100678 tp = numa_topology__new();
679 if (!tp)
680 return -ENOMEM;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200681
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100682 ret = do_write(ff, &tp->nr, sizeof(u32));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200683 if (ret < 0)
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100684 goto err;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200685
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100686 for (i = 0; i < tp->nr; i++) {
687 struct numa_topology_node *n = &tp->nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200688
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100689 ret = do_write(ff, &n->node, sizeof(u32));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200690 if (ret < 0)
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100691 goto err;
692
693 ret = do_write(ff, &n->mem_total, sizeof(u64));
694 if (ret)
695 goto err;
696
697 ret = do_write(ff, &n->mem_free, sizeof(u64));
698 if (ret)
699 goto err;
700
701 ret = do_write_string(ff, n->cpus);
702 if (ret < 0)
703 goto err;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200704 }
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100705
706 ret = 0;
707
708err:
709 numa_topology__delete(tp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200710 return ret;
711}
712
713/*
Robert Richter50a96672012-08-16 21:10:24 +0200714 * File format:
715 *
716 * struct pmu_mappings {
717 * u32 pmu_num;
718 * struct pmu_map {
719 * u32 type;
720 * char name[];
721 * }[pmu_num];
722 * };
723 */
724
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700725static int write_pmu_mappings(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200726 struct evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200727{
728 struct perf_pmu *pmu = NULL;
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700729 u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900730 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200731
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700732 /*
733 * Do a first pass to count number of pmu to avoid lseek so this
734 * works in pipe mode as well.
735 */
736 while ((pmu = perf_pmu__scan(pmu))) {
737 if (!pmu->name)
738 continue;
739 pmu_num++;
740 }
741
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700742 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
Namhyung Kim5323f602012-12-17 15:38:54 +0900743 if (ret < 0)
744 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200745
746 while ((pmu = perf_pmu__scan(pmu))) {
747 if (!pmu->name)
748 continue;
Namhyung Kim5323f602012-12-17 15:38:54 +0900749
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700750 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
Namhyung Kim5323f602012-12-17 15:38:54 +0900751 if (ret < 0)
752 return ret;
753
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700754 ret = do_write_string(ff, pmu->name);
Namhyung Kim5323f602012-12-17 15:38:54 +0900755 if (ret < 0)
756 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200757 }
758
Robert Richter50a96672012-08-16 21:10:24 +0200759 return 0;
760}
761
762/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900763 * File format:
764 *
765 * struct group_descs {
766 * u32 nr_groups;
767 * struct group_desc {
768 * char name[];
769 * u32 leader_idx;
770 * u32 nr_members;
771 * }[nr_groups];
772 * };
773 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700774static int write_group_desc(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200775 struct evlist *evlist)
Namhyung Kima8bb5592013-01-22 18:09:31 +0900776{
777 u32 nr_groups = evlist->nr_groups;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200778 struct evsel *evsel;
Namhyung Kima8bb5592013-01-22 18:09:31 +0900779 int ret;
780
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700781 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900782 if (ret < 0)
783 return ret;
784
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300785 evlist__for_each_entry(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900786 if (perf_evsel__is_group_leader(evsel) &&
Jiri Olsa5643b1a2019-07-21 13:24:46 +0200787 evsel->core.nr_members > 1) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900788 const char *name = evsel->group_name ?: "{anon_group}";
789 u32 leader_idx = evsel->idx;
Jiri Olsa5643b1a2019-07-21 13:24:46 +0200790 u32 nr_members = evsel->core.nr_members;
Namhyung Kima8bb5592013-01-22 18:09:31 +0900791
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700792 ret = do_write_string(ff, name);
Namhyung Kima8bb5592013-01-22 18:09:31 +0900793 if (ret < 0)
794 return ret;
795
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700796 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900797 if (ret < 0)
798 return ret;
799
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700800 ret = do_write(ff, &nr_members, sizeof(nr_members));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900801 if (ret < 0)
802 return ret;
803 }
804 }
805 return 0;
806}
807
808/*
Kan Liangf4a07422018-11-21 08:49:39 -0800809 * Return the CPU id as a raw string.
810 *
811 * Each architecture should provide a more precise id string that
812 * can be use to match the architecture's "mapfile".
813 */
814char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
815{
816 return NULL;
817}
818
819/* Return zero when the cpuid from the mapfile.csv matches the
820 * cpuid string generated on this platform.
821 * Otherwise return non-zero.
822 */
823int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
824{
825 regex_t re;
826 regmatch_t pmatch[1];
827 int match;
828
829 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
830 /* Warn unable to generate match particular string. */
831 pr_info("Invalid regular expression %s\n", mapcpuid);
832 return 1;
833 }
834
835 match = !regexec(&re, cpuid, 1, pmatch, 0);
836 regfree(&re);
837 if (match) {
838 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
839
840 /* Verify the entire string matched. */
841 if (match_len == strlen(cpuid))
842 return 0;
843 }
844 return 1;
845}
846
847/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200848 * default get_cpuid(): nothing gets recorded
Jiada Wang7a759cd2017-04-09 20:02:37 -0700849 * actual implementation must be in arch/$(SRCARCH)/util/header.c
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200850 */
Rui Teng11d8f872016-07-28 10:05:57 +0800851int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200852{
853 return -1;
854}
855
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700856static int write_cpuid(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200857 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200858{
859 char buffer[64];
860 int ret;
861
862 ret = get_cpuid(buffer, sizeof(buffer));
Jiri Olsaa9aeb872019-02-13 13:32:43 +0100863 if (ret)
864 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200865
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700866 return do_write_string(ff, buffer);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200867}
868
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700869static int write_branch_stack(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +0200870 struct evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100871{
872 return 0;
873}
874
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700875static int write_auxtrace(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200876 struct evlist *evlist __maybe_unused)
Adrian Hunter4025ea42015-04-09 18:53:41 +0300877{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300878 struct perf_session *session;
879 int err;
880
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700881 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
882 return -1;
883
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700884 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300885
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700886 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300887 if (err < 0)
888 pr_err("Failed to write auxtrace index\n");
889 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300890}
891
Alexey Budankovcf790512018-10-09 17:36:24 +0300892static int write_clockid(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200893 struct evlist *evlist __maybe_unused)
Alexey Budankovcf790512018-10-09 17:36:24 +0300894{
895 return do_write(ff, &ff->ph->env.clockid_res_ns,
896 sizeof(ff->ph->env.clockid_res_ns));
897}
898
Jiri Olsa258031c2019-03-08 14:47:39 +0100899static int write_dir_format(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200900 struct evlist *evlist __maybe_unused)
Jiri Olsa258031c2019-03-08 14:47:39 +0100901{
902 struct perf_session *session;
903 struct perf_data *data;
904
905 session = container_of(ff->ph, struct perf_session, header);
906 data = session->data;
907
908 if (WARN_ON(!perf_data__is_dir(data)))
909 return -1;
910
911 return do_write(ff, &data->dir.version, sizeof(data->dir.version));
912}
913
Song Liu606f9722019-03-11 22:30:43 -0700914#ifdef HAVE_LIBBPF_SUPPORT
915static int write_bpf_prog_info(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200916 struct evlist *evlist __maybe_unused)
Song Liu606f9722019-03-11 22:30:43 -0700917{
918 struct perf_env *env = &ff->ph->env;
919 struct rb_root *root;
920 struct rb_node *next;
921 int ret;
922
923 down_read(&env->bpf_progs.lock);
924
925 ret = do_write(ff, &env->bpf_progs.infos_cnt,
926 sizeof(env->bpf_progs.infos_cnt));
927 if (ret < 0)
928 goto out;
929
930 root = &env->bpf_progs.infos;
931 next = rb_first(root);
932 while (next) {
933 struct bpf_prog_info_node *node;
934 size_t len;
935
936 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
937 next = rb_next(&node->rb_node);
938 len = sizeof(struct bpf_prog_info_linear) +
939 node->info_linear->data_len;
940
941 /* before writing to file, translate address to offset */
942 bpf_program__bpil_addr_to_offs(node->info_linear);
943 ret = do_write(ff, node->info_linear, len);
944 /*
945 * translate back to address even when do_write() fails,
946 * so that this function never changes the data.
947 */
948 bpf_program__bpil_offs_to_addr(node->info_linear);
949 if (ret < 0)
950 goto out;
951 }
952out:
953 up_read(&env->bpf_progs.lock);
954 return ret;
955}
956#else // HAVE_LIBBPF_SUPPORT
957static int write_bpf_prog_info(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +0200958 struct evlist *evlist __maybe_unused)
Song Liu606f9722019-03-11 22:30:43 -0700959{
960 return 0;
961}
962#endif // HAVE_LIBBPF_SUPPORT
963
Song Liua70a112312019-03-11 22:30:45 -0700964static int write_bpf_btf(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200965 struct evlist *evlist __maybe_unused)
Song Liua70a112312019-03-11 22:30:45 -0700966{
967 struct perf_env *env = &ff->ph->env;
968 struct rb_root *root;
969 struct rb_node *next;
970 int ret;
971
972 down_read(&env->bpf_progs.lock);
973
974 ret = do_write(ff, &env->bpf_progs.btfs_cnt,
975 sizeof(env->bpf_progs.btfs_cnt));
976
977 if (ret < 0)
978 goto out;
979
980 root = &env->bpf_progs.btfs;
981 next = rb_first(root);
982 while (next) {
983 struct btf_node *node;
984
985 node = rb_entry(next, struct btf_node, rb_node);
986 next = rb_next(&node->rb_node);
987 ret = do_write(ff, &node->id,
988 sizeof(u32) * 2 + node->data_size);
989 if (ret < 0)
990 goto out;
991 }
992out:
993 up_read(&env->bpf_progs.lock);
994 return ret;
995}
996
Jiri Olsa720e98b2016-02-16 16:01:43 +0100997static int cpu_cache_level__sort(const void *a, const void *b)
998{
999 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
1000 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1001
1002 return cache_a->level - cache_b->level;
1003}
1004
1005static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1006{
1007 if (a->level != b->level)
1008 return false;
1009
1010 if (a->line_size != b->line_size)
1011 return false;
1012
1013 if (a->sets != b->sets)
1014 return false;
1015
1016 if (a->ways != b->ways)
1017 return false;
1018
1019 if (strcmp(a->type, b->type))
1020 return false;
1021
1022 if (strcmp(a->size, b->size))
1023 return false;
1024
1025 if (strcmp(a->map, b->map))
1026 return false;
1027
1028 return true;
1029}
1030
1031static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1032{
1033 char path[PATH_MAX], file[PATH_MAX];
1034 struct stat st;
1035 size_t len;
1036
1037 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1038 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1039
1040 if (stat(file, &st))
1041 return 1;
1042
1043 scnprintf(file, PATH_MAX, "%s/level", path);
1044 if (sysfs__read_int(file, (int *) &cache->level))
1045 return -1;
1046
1047 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1048 if (sysfs__read_int(file, (int *) &cache->line_size))
1049 return -1;
1050
1051 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1052 if (sysfs__read_int(file, (int *) &cache->sets))
1053 return -1;
1054
1055 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1056 if (sysfs__read_int(file, (int *) &cache->ways))
1057 return -1;
1058
1059 scnprintf(file, PATH_MAX, "%s/type", path);
1060 if (sysfs__read_str(file, &cache->type, &len))
1061 return -1;
1062
1063 cache->type[len] = 0;
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -03001064 cache->type = strim(cache->type);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001065
1066 scnprintf(file, PATH_MAX, "%s/size", path);
1067 if (sysfs__read_str(file, &cache->size, &len)) {
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001068 zfree(&cache->type);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001069 return -1;
1070 }
1071
1072 cache->size[len] = 0;
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -03001073 cache->size = strim(cache->size);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001074
1075 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1076 if (sysfs__read_str(file, &cache->map, &len)) {
Jiri Olsa02162342019-09-12 12:52:35 +02001077 zfree(&cache->size);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001078 zfree(&cache->type);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001079 return -1;
1080 }
1081
1082 cache->map[len] = 0;
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -03001083 cache->map = strim(cache->map);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001084 return 0;
1085}
1086
1087static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1088{
1089 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1090}
1091
1092static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1093{
1094 u32 i, cnt = 0;
1095 long ncpus;
1096 u32 nr, cpu;
1097 u16 level;
1098
1099 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1100 if (ncpus < 0)
1101 return -1;
1102
1103 nr = (u32)(ncpus & UINT_MAX);
1104
1105 for (cpu = 0; cpu < nr; cpu++) {
1106 for (level = 0; level < 10; level++) {
1107 struct cpu_cache_level c;
1108 int err;
1109
1110 err = cpu_cache_level__read(&c, cpu, level);
1111 if (err < 0)
1112 return err;
1113
1114 if (err == 1)
1115 break;
1116
1117 for (i = 0; i < cnt; i++) {
1118 if (cpu_cache_level__cmp(&c, &caches[i]))
1119 break;
1120 }
1121
1122 if (i == cnt)
1123 caches[cnt++] = c;
1124 else
1125 cpu_cache_level__free(&c);
1126
1127 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1128 goto out;
1129 }
1130 }
1131 out:
1132 *cntp = cnt;
1133 return 0;
1134}
1135
Kyle Meyerdc841872019-08-27 16:43:51 -05001136#define MAX_CACHE_LVL 4
Jiri Olsa720e98b2016-02-16 16:01:43 +01001137
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001138static int write_cache(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +02001139 struct evlist *evlist __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001140{
Kyle Meyerdc841872019-08-27 16:43:51 -05001141 u32 max_caches = cpu__max_cpu() * MAX_CACHE_LVL;
1142 struct cpu_cache_level caches[max_caches];
Jiri Olsa720e98b2016-02-16 16:01:43 +01001143 u32 cnt = 0, i, version = 1;
1144 int ret;
1145
Kyle Meyerdc841872019-08-27 16:43:51 -05001146 ret = build_caches(caches, max_caches, &cnt);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001147 if (ret)
1148 goto out;
1149
1150 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1151
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001152 ret = do_write(ff, &version, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001153 if (ret < 0)
1154 goto out;
1155
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001156 ret = do_write(ff, &cnt, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001157 if (ret < 0)
1158 goto out;
1159
1160 for (i = 0; i < cnt; i++) {
1161 struct cpu_cache_level *c = &caches[i];
1162
1163 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001164 ret = do_write(ff, &c->v, sizeof(u32)); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001165 if (ret < 0) \
1166 goto out;
1167
1168 _W(level)
1169 _W(line_size)
1170 _W(sets)
1171 _W(ways)
1172 #undef _W
1173
1174 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001175 ret = do_write_string(ff, (const char *) c->v); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001176 if (ret < 0) \
1177 goto out;
1178
1179 _W(type)
1180 _W(size)
1181 _W(map)
1182 #undef _W
1183 }
1184
1185out:
1186 for (i = 0; i < cnt; i++)
1187 cpu_cache_level__free(&caches[i]);
1188 return ret;
1189}
1190
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001191static int write_stat(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +02001192 struct evlist *evlist __maybe_unused)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001193{
1194 return 0;
1195}
1196
Jin Yao60115182017-12-08 21:13:41 +08001197static int write_sample_time(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +02001198 struct evlist *evlist)
Jin Yao60115182017-12-08 21:13:41 +08001199{
1200 int ret;
1201
1202 ret = do_write(ff, &evlist->first_sample_time,
1203 sizeof(evlist->first_sample_time));
1204 if (ret < 0)
1205 return ret;
1206
1207 return do_write(ff, &evlist->last_sample_time,
1208 sizeof(evlist->last_sample_time));
1209}
1210
Jiri Olsae2091ce2018-03-07 16:50:08 +01001211
1212static int memory_node__read(struct memory_node *n, unsigned long idx)
1213{
1214 unsigned int phys, size = 0;
1215 char path[PATH_MAX];
1216 struct dirent *ent;
1217 DIR *dir;
1218
1219#define for_each_memory(mem, dir) \
1220 while ((ent = readdir(dir))) \
1221 if (strcmp(ent->d_name, ".") && \
1222 strcmp(ent->d_name, "..") && \
1223 sscanf(ent->d_name, "memory%u", &mem) == 1)
1224
1225 scnprintf(path, PATH_MAX,
1226 "%s/devices/system/node/node%lu",
1227 sysfs__mountpoint(), idx);
1228
1229 dir = opendir(path);
1230 if (!dir) {
1231 pr_warning("failed: cant' open memory sysfs data\n");
1232 return -1;
1233 }
1234
1235 for_each_memory(phys, dir) {
1236 size = max(phys, size);
1237 }
1238
1239 size++;
1240
1241 n->set = bitmap_alloc(size);
1242 if (!n->set) {
1243 closedir(dir);
1244 return -ENOMEM;
1245 }
1246
Jiri Olsae2091ce2018-03-07 16:50:08 +01001247 n->node = idx;
1248 n->size = size;
1249
1250 rewinddir(dir);
1251
1252 for_each_memory(phys, dir) {
1253 set_bit(phys, n->set);
1254 }
1255
1256 closedir(dir);
1257 return 0;
1258}
1259
1260static int memory_node__sort(const void *a, const void *b)
1261{
1262 const struct memory_node *na = a;
1263 const struct memory_node *nb = b;
1264
1265 return na->node - nb->node;
1266}
1267
1268static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1269{
1270 char path[PATH_MAX];
1271 struct dirent *ent;
1272 DIR *dir;
1273 u64 cnt = 0;
1274 int ret = 0;
1275
1276 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1277 sysfs__mountpoint());
1278
1279 dir = opendir(path);
1280 if (!dir) {
Thomas Richter4f75f1cb2018-04-12 15:32:46 +02001281 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1282 __func__, path);
Jiri Olsae2091ce2018-03-07 16:50:08 +01001283 return -1;
1284 }
1285
1286 while (!ret && (ent = readdir(dir))) {
1287 unsigned int idx;
1288 int r;
1289
1290 if (!strcmp(ent->d_name, ".") ||
1291 !strcmp(ent->d_name, ".."))
1292 continue;
1293
1294 r = sscanf(ent->d_name, "node%u", &idx);
1295 if (r != 1)
1296 continue;
1297
1298 if (WARN_ONCE(cnt >= size,
1299 "failed to write MEM_TOPOLOGY, way too many nodes\n"))
1300 return -1;
1301
1302 ret = memory_node__read(&nodes[cnt++], idx);
1303 }
1304
1305 *cntp = cnt;
1306 closedir(dir);
1307
1308 if (!ret)
1309 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1310
1311 return ret;
1312}
1313
1314#define MAX_MEMORY_NODES 2000
1315
1316/*
1317 * The MEM_TOPOLOGY holds physical memory map for every
1318 * node in system. The format of data is as follows:
1319 *
1320 * 0 - version | for future changes
1321 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1322 * 16 - count | number of nodes
1323 *
1324 * For each node we store map of physical indexes for
1325 * each node:
1326 *
1327 * 32 - node id | node index
1328 * 40 - size | size of bitmap
1329 * 48 - bitmap | bitmap of memory indexes that belongs to node
1330 */
1331static int write_mem_topology(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +02001332 struct evlist *evlist __maybe_unused)
Jiri Olsae2091ce2018-03-07 16:50:08 +01001333{
1334 static struct memory_node nodes[MAX_MEMORY_NODES];
1335 u64 bsize, version = 1, i, nr;
1336 int ret;
1337
1338 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1339 (unsigned long long *) &bsize);
1340 if (ret)
1341 return ret;
1342
1343 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1344 if (ret)
1345 return ret;
1346
1347 ret = do_write(ff, &version, sizeof(version));
1348 if (ret < 0)
1349 goto out;
1350
1351 ret = do_write(ff, &bsize, sizeof(bsize));
1352 if (ret < 0)
1353 goto out;
1354
1355 ret = do_write(ff, &nr, sizeof(nr));
1356 if (ret < 0)
1357 goto out;
1358
1359 for (i = 0; i < nr; i++) {
1360 struct memory_node *n = &nodes[i];
1361
1362 #define _W(v) \
1363 ret = do_write(ff, &n->v, sizeof(n->v)); \
1364 if (ret < 0) \
1365 goto out;
1366
1367 _W(node)
1368 _W(size)
1369
1370 #undef _W
1371
1372 ret = do_write_bitmap(ff, n->set, n->size);
1373 if (ret < 0)
1374 goto out;
1375 }
1376
1377out:
1378 return ret;
1379}
1380
Alexey Budankov42e1fd82019-03-18 20:41:33 +03001381static int write_compressed(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +02001382 struct evlist *evlist __maybe_unused)
Alexey Budankov42e1fd82019-03-18 20:41:33 +03001383{
1384 int ret;
1385
1386 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver));
1387 if (ret)
1388 return ret;
1389
1390 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type));
1391 if (ret)
1392 return ret;
1393
1394 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level));
1395 if (ret)
1396 return ret;
1397
1398 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio));
1399 if (ret)
1400 return ret;
1401
1402 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len));
1403}
1404
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001405static void print_hostname(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001406{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001407 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001408}
1409
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001410static void print_osrelease(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001411{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001412 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001413}
1414
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001415static void print_arch(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001416{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001417 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001418}
1419
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001420static void print_cpudesc(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001421{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001422 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001423}
1424
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001425static void print_nrcpus(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001426{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001427 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1428 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001429}
1430
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001431static void print_version(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001432{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001433 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001434}
1435
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001436static void print_cmdline(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001437{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001438 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001439
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001440 nr = ff->ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001441
1442 fprintf(fp, "# cmdline : ");
1443
Alexey Budankovf92da712018-06-04 09:50:56 +03001444 for (i = 0; i < nr; i++) {
1445 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1446 if (!argv_i) {
1447 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1448 } else {
1449 char *mem = argv_i;
1450 do {
1451 char *quote = strchr(argv_i, '\'');
1452 if (!quote)
1453 break;
1454 *quote++ = '\0';
1455 fprintf(fp, "%s\\\'", argv_i);
1456 argv_i = quote;
1457 } while (1);
1458 fprintf(fp, "%s ", argv_i);
1459 free(mem);
1460 }
1461 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001462 fputc('\n', fp);
1463}
1464
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001465static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001466{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001467 struct perf_header *ph = ff->ph;
1468 int cpu_nr = ph->env.nr_cpus_avail;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001469 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001470 char *str;
1471
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001472 nr = ph->env.nr_sibling_cores;
1473 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001474
1475 for (i = 0; i < nr; i++) {
Kan Liange05a8992019-06-04 15:50:43 -07001476 fprintf(fp, "# sibling sockets : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001477 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001478 }
1479
Kan Liangacae8b32019-06-04 15:50:41 -07001480 if (ph->env.nr_sibling_dies) {
1481 nr = ph->env.nr_sibling_dies;
1482 str = ph->env.sibling_dies;
1483
1484 for (i = 0; i < nr; i++) {
1485 fprintf(fp, "# sibling dies : %s\n", str);
1486 str += strlen(str) + 1;
1487 }
1488 }
1489
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001490 nr = ph->env.nr_sibling_threads;
1491 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001492
1493 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001494 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001495 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001496 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001497
Kan Liangacae8b32019-06-04 15:50:41 -07001498 if (ph->env.nr_sibling_dies) {
1499 if (ph->env.cpu != NULL) {
1500 for (i = 0; i < cpu_nr; i++)
1501 fprintf(fp, "# CPU %d: Core ID %d, "
1502 "Die ID %d, Socket ID %d\n",
1503 i, ph->env.cpu[i].core_id,
1504 ph->env.cpu[i].die_id,
1505 ph->env.cpu[i].socket_id);
1506 } else
1507 fprintf(fp, "# Core ID, Die ID and Socket ID "
1508 "information is not available\n");
1509 } else {
1510 if (ph->env.cpu != NULL) {
1511 for (i = 0; i < cpu_nr; i++)
1512 fprintf(fp, "# CPU %d: Core ID %d, "
1513 "Socket ID %d\n",
1514 i, ph->env.cpu[i].core_id,
1515 ph->env.cpu[i].socket_id);
1516 } else
1517 fprintf(fp, "# Core ID and Socket ID "
1518 "information is not available\n");
1519 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001520}
1521
Alexey Budankovcf790512018-10-09 17:36:24 +03001522static void print_clockid(struct feat_fd *ff, FILE *fp)
1523{
1524 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1525 ff->ph->env.clockid_res_ns * 1000);
1526}
1527
Jiri Olsa258031c2019-03-08 14:47:39 +01001528static void print_dir_format(struct feat_fd *ff, FILE *fp)
1529{
1530 struct perf_session *session;
1531 struct perf_data *data;
1532
1533 session = container_of(ff->ph, struct perf_session, header);
1534 data = session->data;
1535
1536 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version);
1537}
1538
Song Liu606f9722019-03-11 22:30:43 -07001539static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp)
1540{
1541 struct perf_env *env = &ff->ph->env;
1542 struct rb_root *root;
1543 struct rb_node *next;
1544
1545 down_read(&env->bpf_progs.lock);
1546
1547 root = &env->bpf_progs.infos;
1548 next = rb_first(root);
1549
1550 while (next) {
1551 struct bpf_prog_info_node *node;
1552
1553 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1554 next = rb_next(&node->rb_node);
Song Liuf8dfeae2019-03-19 09:54:54 -07001555
1556 bpf_event__print_bpf_prog_info(&node->info_linear->info,
1557 env, fp);
Song Liu606f9722019-03-11 22:30:43 -07001558 }
1559
1560 up_read(&env->bpf_progs.lock);
1561}
1562
Song Liua70a112312019-03-11 22:30:45 -07001563static void print_bpf_btf(struct feat_fd *ff, FILE *fp)
1564{
1565 struct perf_env *env = &ff->ph->env;
1566 struct rb_root *root;
1567 struct rb_node *next;
1568
1569 down_read(&env->bpf_progs.lock);
1570
1571 root = &env->bpf_progs.btfs;
1572 next = rb_first(root);
1573
1574 while (next) {
1575 struct btf_node *node;
1576
1577 node = rb_entry(next, struct btf_node, rb_node);
1578 next = rb_next(&node->rb_node);
1579 fprintf(fp, "# btf info of id %u\n", node->id);
1580 }
1581
1582 up_read(&env->bpf_progs.lock);
1583}
1584
Jiri Olsa32dcd022019-07-21 13:23:51 +02001585static void free_event_desc(struct evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001586{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001587 struct evsel *evsel;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001588
1589 if (!events)
1590 return;
1591
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001592 for (evsel = events; evsel->core.attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001593 zfree(&evsel->name);
Jiri Olsadeaf3212019-09-02 22:12:26 +02001594 zfree(&evsel->core.id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001595 }
1596
1597 free(events);
1598}
1599
Jiri Olsa32dcd022019-07-21 13:23:51 +02001600static struct evsel *read_event_desc(struct feat_fd *ff)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001601{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001602 struct evsel *evsel, *events = NULL;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001603 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001604 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001605 u32 nre, sz, nr, i, j;
Stephane Eranian62db9062012-02-09 23:21:07 +01001606 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001607
1608 /* number of events */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001609 if (do_read_u32(ff, &nre))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001610 goto error;
1611
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001612 if (do_read_u32(ff, &sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001613 goto error;
1614
Stephane Eranian62db9062012-02-09 23:21:07 +01001615 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001616 buf = malloc(sz);
1617 if (!buf)
1618 goto error;
1619
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001620 /* the last event terminates with evsel->core.attr.size == 0: */
Robert Richter4e1b9c62012-08-16 21:10:22 +02001621 events = calloc(nre + 1, sizeof(*events));
1622 if (!events)
1623 goto error;
1624
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001625 msz = sizeof(evsel->core.attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001626 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001627 msz = sz;
1628
Robert Richter4e1b9c62012-08-16 21:10:22 +02001629 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1630 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001631
Stephane Eranian62db9062012-02-09 23:21:07 +01001632 /*
1633 * must read entire on-file attr struct to
1634 * sync up with layout.
1635 */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001636 if (__do_read(ff, buf, sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001637 goto error;
1638
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001639 if (ff->ph->needs_swap)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001640 perf_event__attr_swap(buf);
1641
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001642 memcpy(&evsel->core.attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001643
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001644 if (do_read_u32(ff, &nr))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001645 goto error;
1646
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001647 if (ff->ph->needs_swap)
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001648 evsel->needs_swap = true;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001649
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001650 evsel->name = do_read_string(ff);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001651 if (!evsel->name)
1652 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001653
1654 if (!nr)
1655 continue;
1656
1657 id = calloc(nr, sizeof(*id));
1658 if (!id)
1659 goto error;
Jiri Olsae7eb9002019-09-02 22:15:47 +02001660 evsel->core.ids = nr;
Jiri Olsadeaf3212019-09-02 22:12:26 +02001661 evsel->core.id = id;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001662
1663 for (j = 0 ; j < nr; j++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001664 if (do_read_u64(ff, id))
Robert Richter4e1b9c62012-08-16 21:10:22 +02001665 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001666 id++;
1667 }
1668 }
1669out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001670 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001671 return events;
1672error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001673 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001674 events = NULL;
1675 goto out;
1676}
1677
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001678static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001679 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001680{
1681 return fprintf(fp, ", %s = %s", name, val);
1682}
1683
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001684static void print_event_desc(struct feat_fd *ff, FILE *fp)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001685{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001686 struct evsel *evsel, *events;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001687 u32 j;
1688 u64 *id;
1689
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001690 if (ff->events)
1691 events = ff->events;
1692 else
1693 events = read_event_desc(ff);
1694
Robert Richter4e1b9c62012-08-16 21:10:22 +02001695 if (!events) {
1696 fprintf(fp, "# event desc: not available or unable to read\n");
1697 return;
1698 }
1699
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001700 for (evsel = events; evsel->core.attr.size; evsel++) {
Robert Richter4e1b9c62012-08-16 21:10:22 +02001701 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001702
Jiri Olsae7eb9002019-09-02 22:15:47 +02001703 if (evsel->core.ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001704 fprintf(fp, ", id = {");
Jiri Olsae7eb9002019-09-02 22:15:47 +02001705 for (j = 0, id = evsel->core.id; j < evsel->core.ids; j++, id++) {
Robert Richter4e1b9c62012-08-16 21:10:22 +02001706 if (j)
1707 fputc(',', fp);
1708 fprintf(fp, " %"PRIu64, *id);
1709 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001710 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001711 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001712
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001713 perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001714
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001715 fputc('\n', fp);
1716 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001717
1718 free_event_desc(events);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001719 ff->events = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001720}
1721
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001722static void print_total_mem(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001723{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001724 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001725}
1726
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001727static void print_numa_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001728{
Jiri Olsac60da222016-07-04 14:16:20 +02001729 int i;
1730 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001731
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001732 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1733 n = &ff->ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001734
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001735 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1736 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001737 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001738
Jiri Olsac60da222016-07-04 14:16:20 +02001739 fprintf(fp, "# node%u cpu list : ", n->node);
1740 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001741 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001742}
1743
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001744static void print_cpuid(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001745{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001746 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001747}
1748
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001749static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001750{
1751 fprintf(fp, "# contains samples with branch stack\n");
1752}
1753
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001754static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
Adrian Hunter4025ea42015-04-09 18:53:41 +03001755{
1756 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1757}
1758
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001759static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001760{
1761 fprintf(fp, "# contains stat data\n");
1762}
1763
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001764static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001765{
1766 int i;
1767
1768 fprintf(fp, "# CPU cache info:\n");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001769 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
Jiri Olsa720e98b2016-02-16 16:01:43 +01001770 fprintf(fp, "# ");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001771 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001772 }
1773}
1774
Alexey Budankov42e1fd82019-03-18 20:41:33 +03001775static void print_compressed(struct feat_fd *ff, FILE *fp)
1776{
1777 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n",
1778 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown",
1779 ff->ph->env.comp_level, ff->ph->env.comp_ratio);
1780}
1781
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001782static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001783{
1784 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001785 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001786 u32 pmu_num;
1787 u32 type;
1788
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001789 pmu_num = ff->ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001790 if (!pmu_num) {
1791 fprintf(fp, "# pmu mappings: not available\n");
1792 return;
1793 }
1794
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001795 str = ff->ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001796
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001797 while (pmu_num) {
1798 type = strtoul(str, &tmp, 0);
1799 if (*tmp != ':')
1800 goto error;
1801
1802 str = tmp + 1;
1803 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1804
Robert Richter50a96672012-08-16 21:10:24 +02001805 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001806 str += strlen(str) + 1;
1807 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001808 }
1809
1810 fprintf(fp, "\n");
1811
1812 if (!pmu_num)
1813 return;
1814error:
1815 fprintf(fp, "# pmu mappings: unable to read\n");
1816}
1817
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001818static void print_group_desc(struct feat_fd *ff, FILE *fp)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001819{
1820 struct perf_session *session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001821 struct evsel *evsel;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001822 u32 nr = 0;
1823
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001824 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001825
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001826 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001827 if (perf_evsel__is_group_leader(evsel) &&
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001828 evsel->core.nr_members > 1) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001829 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1830 perf_evsel__name(evsel));
1831
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001832 nr = evsel->core.nr_members - 1;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001833 } else if (nr) {
1834 fprintf(fp, ",%s", perf_evsel__name(evsel));
1835
1836 if (--nr == 0)
1837 fprintf(fp, "}\n");
1838 }
1839 }
1840}
1841
Jin Yao60115182017-12-08 21:13:41 +08001842static void print_sample_time(struct feat_fd *ff, FILE *fp)
1843{
1844 struct perf_session *session;
1845 char time_buf[32];
1846 double d;
1847
1848 session = container_of(ff->ph, struct perf_session, header);
1849
1850 timestamp__scnprintf_usec(session->evlist->first_sample_time,
1851 time_buf, sizeof(time_buf));
1852 fprintf(fp, "# time of first sample : %s\n", time_buf);
1853
1854 timestamp__scnprintf_usec(session->evlist->last_sample_time,
1855 time_buf, sizeof(time_buf));
1856 fprintf(fp, "# time of last sample : %s\n", time_buf);
1857
1858 d = (double)(session->evlist->last_sample_time -
1859 session->evlist->first_sample_time) / NSEC_PER_MSEC;
1860
1861 fprintf(fp, "# sample duration : %10.3f ms\n", d);
1862}
1863
Jiri Olsae2091ce2018-03-07 16:50:08 +01001864static void memory_node__fprintf(struct memory_node *n,
1865 unsigned long long bsize, FILE *fp)
1866{
1867 char buf_map[100], buf_size[50];
1868 unsigned long long size;
1869
1870 size = bsize * bitmap_weight(n->set, n->size);
1871 unit_number__scnprintf(buf_size, 50, size);
1872
1873 bitmap_scnprintf(n->set, n->size, buf_map, 100);
1874 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
1875}
1876
1877static void print_mem_topology(struct feat_fd *ff, FILE *fp)
1878{
1879 struct memory_node *nodes;
1880 int i, nr;
1881
1882 nodes = ff->ph->env.memory_nodes;
1883 nr = ff->ph->env.nr_memory_nodes;
1884
1885 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
1886 nr, ff->ph->env.memory_bsize);
1887
1888 for (i = 0; i < nr; i++) {
1889 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
1890 }
1891}
1892
Jiri Olsa72932372019-08-28 15:57:16 +02001893static int __event_process_build_id(struct perf_record_header_build_id *bev,
Robert Richter08d95bd2012-02-10 15:41:55 +01001894 char *filename,
1895 struct perf_session *session)
1896{
1897 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001898 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001899 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001900 struct dso *dso;
1901 enum dso_kernel_type dso_type;
1902
1903 machine = perf_session__findnew_machine(session, bev->pid);
1904 if (!machine)
1905 goto out;
1906
Wang Nan1f121b02015-06-03 08:52:21 +00001907 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001908
Wang Nan1f121b02015-06-03 08:52:21 +00001909 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001910 case PERF_RECORD_MISC_KERNEL:
1911 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001912 break;
1913 case PERF_RECORD_MISC_GUEST_KERNEL:
1914 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001915 break;
1916 case PERF_RECORD_MISC_USER:
1917 case PERF_RECORD_MISC_GUEST_USER:
1918 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001919 break;
1920 default:
1921 goto out;
1922 }
1923
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001924 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001925 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001926 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001927
1928 dso__set_build_id(dso, &bev->build_id);
1929
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001930 if (dso_type != DSO_TYPE_USER) {
1931 struct kmod_path m = { .name = NULL, };
1932
1933 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09001934 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001935 else
1936 dso->kernel = dso_type;
1937
1938 free(m.name);
1939 }
Robert Richter08d95bd2012-02-10 15:41:55 +01001940
1941 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1942 sbuild_id);
1943 pr_debug("build id event received for %s: %s\n",
1944 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001945 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001946 }
1947
1948 err = 0;
1949out:
1950 return err;
1951}
1952
1953static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1954 int input, u64 offset, u64 size)
1955{
1956 struct perf_session *session = container_of(header, struct perf_session, header);
1957 struct {
1958 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001959 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001960 char filename[0];
1961 } old_bev;
Jiri Olsa72932372019-08-28 15:57:16 +02001962 struct perf_record_header_build_id bev;
Robert Richter08d95bd2012-02-10 15:41:55 +01001963 char filename[PATH_MAX];
1964 u64 limit = offset + size;
1965
1966 while (offset < limit) {
1967 ssize_t len;
1968
Namhyung Kim5323f602012-12-17 15:38:54 +09001969 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001970 return -1;
1971
1972 if (header->needs_swap)
1973 perf_event_header__bswap(&old_bev.header);
1974
1975 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001976 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001977 return -1;
1978
1979 bev.header = old_bev.header;
1980
1981 /*
1982 * As the pid is the missing value, we need to fill
1983 * it properly. The header.misc value give us nice hint.
1984 */
1985 bev.pid = HOST_KERNEL_ID;
1986 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1987 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1988 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1989
1990 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1991 __event_process_build_id(&bev, filename, session);
1992
1993 offset += bev.header.size;
1994 }
1995
1996 return 0;
1997}
1998
1999static int perf_header__read_build_ids(struct perf_header *header,
2000 int input, u64 offset, u64 size)
2001{
2002 struct perf_session *session = container_of(header, struct perf_session, header);
Jiri Olsa72932372019-08-28 15:57:16 +02002003 struct perf_record_header_build_id bev;
Robert Richter08d95bd2012-02-10 15:41:55 +01002004 char filename[PATH_MAX];
2005 u64 limit = offset + size, orig_offset = offset;
2006 int err = -1;
2007
2008 while (offset < limit) {
2009 ssize_t len;
2010
Namhyung Kim5323f602012-12-17 15:38:54 +09002011 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01002012 goto out;
2013
2014 if (header->needs_swap)
2015 perf_event_header__bswap(&bev.header);
2016
2017 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09002018 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01002019 goto out;
2020 /*
2021 * The a1645ce1 changeset:
2022 *
2023 * "perf: 'perf kvm' tool for monitoring guest performance from host"
2024 *
Jiri Olsa72932372019-08-28 15:57:16 +02002025 * Added a field to struct perf_record_header_build_id that broke the file
Robert Richter08d95bd2012-02-10 15:41:55 +01002026 * format.
2027 *
2028 * Since the kernel build-id is the first entry, process the
2029 * table using the old format if the well known
2030 * '[kernel.kallsyms]' string for the kernel build-id has the
2031 * first 4 characters chopped off (where the pid_t sits).
2032 */
2033 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
2034 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
2035 return -1;
2036 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
2037 }
2038
2039 __event_process_build_id(&bev, filename, session);
2040
2041 offset += bev.header.size;
2042 }
2043 err = 0;
2044out:
2045 return err;
2046}
2047
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07002048/* Macro for features that simply need to read and store a string. */
2049#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002050static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07002051{\
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002052 ff->ph->env.__feat_env = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002053 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07002054}
2055
2056FEAT_PROCESS_STR_FUN(hostname, hostname);
2057FEAT_PROCESS_STR_FUN(osrelease, os_release);
2058FEAT_PROCESS_STR_FUN(version, version);
2059FEAT_PROCESS_STR_FUN(arch, arch);
2060FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
2061FEAT_PROCESS_STR_FUN(cpuid, cpuid);
2062
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002063static int process_tracing_data(struct feat_fd *ff, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01002064{
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002065 ssize_t ret = trace_report(ff->fd, data, false);
2066
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002067 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01002068}
2069
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002070static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01002071{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002072 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
Robert Richterf1c67db2012-02-10 15:41:56 +01002073 pr_debug("Failed to read buildids, continuing...\n");
2074 return 0;
2075}
2076
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002077static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002078{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002079 int ret;
2080 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002081
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002082 ret = do_read_u32(ff, &nr_cpus_avail);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002083 if (ret)
2084 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002085
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002086 ret = do_read_u32(ff, &nr_cpus_online);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002087 if (ret)
2088 return ret;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002089 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2090 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002091 return 0;
2092}
2093
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002094static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002095{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002096 u64 total_mem;
2097 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002098
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002099 ret = do_read_u64(ff, &total_mem);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002100 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002101 return -1;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002102 ff->ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002103 return 0;
2104}
2105
Jiri Olsa32dcd022019-07-21 13:23:51 +02002106static struct evsel *
Jiri Olsa63503db2019-07-21 13:23:52 +02002107perf_evlist__find_by_index(struct evlist *evlist, int idx)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002108{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002109 struct evsel *evsel;
Robert Richter7c2f7af2012-08-16 21:10:23 +02002110
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002111 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02002112 if (evsel->idx == idx)
2113 return evsel;
2114 }
2115
2116 return NULL;
2117}
2118
2119static void
Jiri Olsa63503db2019-07-21 13:23:52 +02002120perf_evlist__set_event_name(struct evlist *evlist,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002121 struct evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002122{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002123 struct evsel *evsel;
Robert Richter7c2f7af2012-08-16 21:10:23 +02002124
2125 if (!event->name)
2126 return;
2127
2128 evsel = perf_evlist__find_by_index(evlist, event->idx);
2129 if (!evsel)
2130 return;
2131
2132 if (evsel->name)
2133 return;
2134
2135 evsel->name = strdup(event->name);
2136}
2137
2138static int
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002139process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002140{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002141 struct perf_session *session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002142 struct evsel *evsel, *events = read_event_desc(ff);
Robert Richter7c2f7af2012-08-16 21:10:23 +02002143
2144 if (!events)
2145 return 0;
2146
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002147 session = container_of(ff->ph, struct perf_session, header);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002148
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002149 if (session->data->is_pipe) {
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002150 /* Save events for reading later by print_event_desc,
2151 * since they can't be read again in pipe mode. */
2152 ff->events = events;
2153 }
2154
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002155 for (evsel = events; evsel->core.attr.size; evsel++)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002156 perf_evlist__set_event_name(session->evlist, evsel);
2157
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002158 if (!session->data->is_pipe)
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002159 free_event_desc(events);
Robert Richter7c2f7af2012-08-16 21:10:23 +02002160
2161 return 0;
2162}
2163
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002164static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002165{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002166 char *str, *cmdline = NULL, **argv = NULL;
2167 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002168
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002169 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002170 return -1;
2171
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002172 ff->ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002173
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002174 cmdline = zalloc(ff->size + nr + 1);
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002175 if (!cmdline)
2176 return -1;
2177
2178 argv = zalloc(sizeof(char *) * (nr + 1));
2179 if (!argv)
2180 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002181
2182 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002183 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002184 if (!str)
2185 goto error;
2186
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002187 argv[i] = cmdline + len;
2188 memcpy(argv[i], str, strlen(str) + 1);
2189 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002190 free(str);
2191 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002192 ff->ph->env.cmdline = cmdline;
2193 ff->ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002194 return 0;
2195
2196error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002197 free(argv);
2198 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002199 return -1;
2200}
2201
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002202static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002203{
Namhyung Kima1ae5652012-09-24 17:14:59 +09002204 u32 nr, i;
2205 char *str;
2206 struct strbuf sb;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002207 int cpu_nr = ff->ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04002208 u64 size = 0;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002209 struct perf_header *ph = ff->ph;
Thomas Richter01766222018-06-11 09:31:52 +02002210 bool do_core_id_test = true;
Kan Liang2bb00d22015-09-01 09:58:12 -04002211
2212 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2213 if (!ph->env.cpu)
2214 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002215
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002216 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002217 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002218
Namhyung Kima1ae5652012-09-24 17:14:59 +09002219 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002220 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002221 if (strbuf_init(&sb, 128) < 0)
2222 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002223
2224 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002225 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002226 if (!str)
2227 goto error;
2228
2229 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002230 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2231 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04002232 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002233 free(str);
2234 }
2235 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2236
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002237 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002238 return -1;
2239
Namhyung Kima1ae5652012-09-24 17:14:59 +09002240 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002241 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002242
2243 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002244 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002245 if (!str)
2246 goto error;
2247
2248 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002249 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2250 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04002251 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002252 free(str);
2253 }
2254 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04002255
2256 /*
2257 * The header may be from old perf,
2258 * which doesn't include core id and socket id information.
2259 */
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002260 if (ff->size <= size) {
Kan Liang2bb00d22015-09-01 09:58:12 -04002261 zfree(&ph->env.cpu);
2262 return 0;
2263 }
2264
Thomas Richter01766222018-06-11 09:31:52 +02002265 /* On s390 the socket_id number is not related to the numbers of cpus.
2266 * The socket_id number might be higher than the numbers of cpus.
2267 * This depends on the configuration.
Tan Xiaojun0a4d8fb2019-08-02 11:48:57 +08002268 * AArch64 is the same.
Thomas Richter01766222018-06-11 09:31:52 +02002269 */
Tan Xiaojun0a4d8fb2019-08-02 11:48:57 +08002270 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
2271 || !strncmp(ph->env.arch, "aarch64", 7)))
Thomas Richter01766222018-06-11 09:31:52 +02002272 do_core_id_test = false;
2273
Kan Liang2bb00d22015-09-01 09:58:12 -04002274 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002275 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002276 goto free_cpu;
2277
Kan Liang2bb00d22015-09-01 09:58:12 -04002278 ph->env.cpu[i].core_id = nr;
Kan Liangacae8b32019-06-04 15:50:41 -07002279 size += sizeof(u32);
Kan Liang2bb00d22015-09-01 09:58:12 -04002280
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002281 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002282 goto free_cpu;
2283
Thomas Richter01766222018-06-11 09:31:52 +02002284 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04002285 pr_debug("socket_id number is too big."
2286 "You may need to upgrade the perf tool.\n");
2287 goto free_cpu;
2288 }
2289
2290 ph->env.cpu[i].socket_id = nr;
Kan Liangacae8b32019-06-04 15:50:41 -07002291 size += sizeof(u32);
2292 }
2293
2294 /*
2295 * The header may be from old perf,
2296 * which doesn't include die information.
2297 */
2298 if (ff->size <= size)
2299 return 0;
2300
2301 if (do_read_u32(ff, &nr))
2302 return -1;
2303
2304 ph->env.nr_sibling_dies = nr;
2305 size += sizeof(u32);
2306
2307 for (i = 0; i < nr; i++) {
2308 str = do_read_string(ff);
2309 if (!str)
2310 goto error;
2311
2312 /* include a NULL character at the end */
2313 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2314 goto error;
2315 size += string_size(str);
2316 free(str);
2317 }
2318 ph->env.sibling_dies = strbuf_detach(&sb, NULL);
2319
2320 for (i = 0; i < (u32)cpu_nr; i++) {
2321 if (do_read_u32(ff, &nr))
2322 goto free_cpu;
2323
2324 ph->env.cpu[i].die_id = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002325 }
2326
Namhyung Kima1ae5652012-09-24 17:14:59 +09002327 return 0;
2328
2329error:
2330 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04002331free_cpu:
2332 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002333 return -1;
2334}
2335
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002336static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002337{
Jiri Olsac60da222016-07-04 14:16:20 +02002338 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02002339 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002340 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002341
2342 /* nr nodes */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002343 if (do_read_u32(ff, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002344 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002345
Jiri Olsac60da222016-07-04 14:16:20 +02002346 nodes = zalloc(sizeof(*nodes) * nr);
2347 if (!nodes)
2348 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002349
2350 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02002351 n = &nodes[i];
2352
Namhyung Kima1ae5652012-09-24 17:14:59 +09002353 /* node number */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002354 if (do_read_u32(ff, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002355 goto error;
2356
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002357 if (do_read_u64(ff, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002358 goto error;
2359
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002360 if (do_read_u64(ff, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002361 goto error;
2362
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002363 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002364 if (!str)
2365 goto error;
2366
Jiri Olsa9c3516d2019-07-21 13:24:30 +02002367 n->map = perf_cpu_map__new(str);
Jiri Olsac60da222016-07-04 14:16:20 +02002368 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002369 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02002370
Namhyung Kima1ae5652012-09-24 17:14:59 +09002371 free(str);
2372 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002373 ff->ph->env.nr_numa_nodes = nr;
2374 ff->ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002375 return 0;
2376
2377error:
Jiri Olsac60da222016-07-04 14:16:20 +02002378 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002379 return -1;
2380}
2381
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002382static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002383{
Namhyung Kima1ae5652012-09-24 17:14:59 +09002384 char *name;
2385 u32 pmu_num;
2386 u32 type;
2387 struct strbuf sb;
2388
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002389 if (do_read_u32(ff, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002390 return -1;
2391
Namhyung Kima1ae5652012-09-24 17:14:59 +09002392 if (!pmu_num) {
2393 pr_debug("pmu mappings not available\n");
2394 return 0;
2395 }
2396
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002397 ff->ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002398 if (strbuf_init(&sb, 128) < 0)
2399 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002400
2401 while (pmu_num) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002402 if (do_read_u32(ff, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002403 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002404
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002405 name = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002406 if (!name)
2407 goto error;
2408
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002409 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2410 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002411 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002412 if (strbuf_add(&sb, "", 1) < 0)
2413 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002414
Kan Liange0838e02015-09-10 11:03:05 -03002415 if (!strcmp(name, "msr"))
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002416 ff->ph->env.msr_pmu_type = type;
Kan Liange0838e02015-09-10 11:03:05 -03002417
Namhyung Kima1ae5652012-09-24 17:14:59 +09002418 free(name);
2419 pmu_num--;
2420 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002421 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002422 return 0;
2423
2424error:
2425 strbuf_release(&sb);
2426 return -1;
2427}
2428
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002429static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima8bb5592013-01-22 18:09:31 +09002430{
2431 size_t ret = -1;
2432 u32 i, nr, nr_groups;
2433 struct perf_session *session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002434 struct evsel *evsel, *leader = NULL;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002435 struct group_desc {
2436 char *name;
2437 u32 leader_idx;
2438 u32 nr_members;
2439 } *desc;
2440
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002441 if (do_read_u32(ff, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002442 return -1;
2443
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002444 ff->ph->env.nr_groups = nr_groups;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002445 if (!nr_groups) {
2446 pr_debug("group desc not available\n");
2447 return 0;
2448 }
2449
2450 desc = calloc(nr_groups, sizeof(*desc));
2451 if (!desc)
2452 return -1;
2453
2454 for (i = 0; i < nr_groups; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002455 desc[i].name = do_read_string(ff);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002456 if (!desc[i].name)
2457 goto out_free;
2458
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002459 if (do_read_u32(ff, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002460 goto out_free;
2461
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002462 if (do_read_u32(ff, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002463 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002464 }
2465
2466 /*
2467 * Rebuild group relationship based on the group_desc
2468 */
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002469 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002470 session->evlist->nr_groups = nr_groups;
2471
2472 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002473 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002474 if (evsel->idx == (int) desc[i].leader_idx) {
2475 evsel->leader = evsel;
2476 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002477 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002478 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002479 desc[i].name = NULL;
2480 }
Jiri Olsa5643b1a2019-07-21 13:24:46 +02002481 evsel->core.nr_members = desc[i].nr_members;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002482
2483 if (i >= nr_groups || nr > 0) {
2484 pr_debug("invalid group desc\n");
2485 goto out_free;
2486 }
2487
2488 leader = evsel;
Jiri Olsa5643b1a2019-07-21 13:24:46 +02002489 nr = evsel->core.nr_members - 1;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002490 i++;
2491 } else if (nr) {
2492 /* This is a group member */
2493 evsel->leader = leader;
2494
2495 nr--;
2496 }
2497 }
2498
2499 if (i != nr_groups || nr != 0) {
2500 pr_debug("invalid group desc\n");
2501 goto out_free;
2502 }
2503
2504 ret = 0;
2505out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002506 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002507 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002508 free(desc);
2509
2510 return ret;
2511}
2512
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002513static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
Adrian Hunter99fa2982015-04-30 17:37:25 +03002514{
2515 struct perf_session *session;
2516 int err;
2517
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002518 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002519
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002520 err = auxtrace_index__process(ff->fd, ff->size, session,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002521 ff->ph->needs_swap);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002522 if (err < 0)
2523 pr_err("Failed to process auxtrace index\n");
2524 return err;
2525}
2526
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002527static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01002528{
2529 struct cpu_cache_level *caches;
2530 u32 cnt, i, version;
2531
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002532 if (do_read_u32(ff, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002533 return -1;
2534
Jiri Olsa720e98b2016-02-16 16:01:43 +01002535 if (version != 1)
2536 return -1;
2537
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002538 if (do_read_u32(ff, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002539 return -1;
2540
Jiri Olsa720e98b2016-02-16 16:01:43 +01002541 caches = zalloc(sizeof(*caches) * cnt);
2542 if (!caches)
2543 return -1;
2544
2545 for (i = 0; i < cnt; i++) {
2546 struct cpu_cache_level c;
2547
2548 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002549 if (do_read_u32(ff, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002550 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002551
2552 _R(level)
2553 _R(line_size)
2554 _R(sets)
2555 _R(ways)
2556 #undef _R
2557
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002558 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002559 c.v = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002560 if (!c.v) \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002561 goto out_free_caches;
2562
2563 _R(type)
2564 _R(size)
2565 _R(map)
2566 #undef _R
2567
2568 caches[i] = c;
2569 }
2570
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002571 ff->ph->env.caches = caches;
2572 ff->ph->env.caches_cnt = cnt;
Jiri Olsa720e98b2016-02-16 16:01:43 +01002573 return 0;
2574out_free_caches:
2575 free(caches);
2576 return -1;
2577}
2578
Jin Yao60115182017-12-08 21:13:41 +08002579static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2580{
2581 struct perf_session *session;
2582 u64 first_sample_time, last_sample_time;
2583 int ret;
2584
2585 session = container_of(ff->ph, struct perf_session, header);
2586
2587 ret = do_read_u64(ff, &first_sample_time);
2588 if (ret)
2589 return -1;
2590
2591 ret = do_read_u64(ff, &last_sample_time);
2592 if (ret)
2593 return -1;
2594
2595 session->evlist->first_sample_time = first_sample_time;
2596 session->evlist->last_sample_time = last_sample_time;
2597 return 0;
2598}
2599
Jiri Olsae2091ce2018-03-07 16:50:08 +01002600static int process_mem_topology(struct feat_fd *ff,
2601 void *data __maybe_unused)
2602{
2603 struct memory_node *nodes;
2604 u64 version, i, nr, bsize;
2605 int ret = -1;
2606
2607 if (do_read_u64(ff, &version))
2608 return -1;
2609
2610 if (version != 1)
2611 return -1;
2612
2613 if (do_read_u64(ff, &bsize))
2614 return -1;
2615
2616 if (do_read_u64(ff, &nr))
2617 return -1;
2618
2619 nodes = zalloc(sizeof(*nodes) * nr);
2620 if (!nodes)
2621 return -1;
2622
2623 for (i = 0; i < nr; i++) {
2624 struct memory_node n;
2625
2626 #define _R(v) \
2627 if (do_read_u64(ff, &n.v)) \
2628 goto out; \
2629
2630 _R(node)
2631 _R(size)
2632
2633 #undef _R
2634
2635 if (do_read_bitmap(ff, &n.set, &n.size))
2636 goto out;
2637
2638 nodes[i] = n;
2639 }
2640
2641 ff->ph->env.memory_bsize = bsize;
2642 ff->ph->env.memory_nodes = nodes;
2643 ff->ph->env.nr_memory_nodes = nr;
2644 ret = 0;
2645
2646out:
2647 if (ret)
2648 free(nodes);
2649 return ret;
2650}
2651
Alexey Budankovcf790512018-10-09 17:36:24 +03002652static int process_clockid(struct feat_fd *ff,
2653 void *data __maybe_unused)
2654{
2655 if (do_read_u64(ff, &ff->ph->env.clockid_res_ns))
2656 return -1;
2657
2658 return 0;
2659}
2660
Jiri Olsa258031c2019-03-08 14:47:39 +01002661static int process_dir_format(struct feat_fd *ff,
2662 void *_data __maybe_unused)
2663{
2664 struct perf_session *session;
2665 struct perf_data *data;
2666
2667 session = container_of(ff->ph, struct perf_session, header);
2668 data = session->data;
2669
2670 if (WARN_ON(!perf_data__is_dir(data)))
2671 return -1;
2672
2673 return do_read_u64(ff, &data->dir.version);
2674}
2675
Song Liu606f9722019-03-11 22:30:43 -07002676#ifdef HAVE_LIBBPF_SUPPORT
2677static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
2678{
2679 struct bpf_prog_info_linear *info_linear;
2680 struct bpf_prog_info_node *info_node;
2681 struct perf_env *env = &ff->ph->env;
2682 u32 count, i;
2683 int err = -1;
2684
2685 if (ff->ph->needs_swap) {
2686 pr_warning("interpreting bpf_prog_info from systems with endianity is not yet supported\n");
2687 return 0;
2688 }
2689
2690 if (do_read_u32(ff, &count))
2691 return -1;
2692
2693 down_write(&env->bpf_progs.lock);
2694
2695 for (i = 0; i < count; ++i) {
2696 u32 info_len, data_len;
2697
2698 info_linear = NULL;
2699 info_node = NULL;
2700 if (do_read_u32(ff, &info_len))
2701 goto out;
2702 if (do_read_u32(ff, &data_len))
2703 goto out;
2704
2705 if (info_len > sizeof(struct bpf_prog_info)) {
2706 pr_warning("detected invalid bpf_prog_info\n");
2707 goto out;
2708 }
2709
2710 info_linear = malloc(sizeof(struct bpf_prog_info_linear) +
2711 data_len);
2712 if (!info_linear)
2713 goto out;
2714 info_linear->info_len = sizeof(struct bpf_prog_info);
2715 info_linear->data_len = data_len;
2716 if (do_read_u64(ff, (u64 *)(&info_linear->arrays)))
2717 goto out;
2718 if (__do_read(ff, &info_linear->info, info_len))
2719 goto out;
2720 if (info_len < sizeof(struct bpf_prog_info))
2721 memset(((void *)(&info_linear->info)) + info_len, 0,
2722 sizeof(struct bpf_prog_info) - info_len);
2723
2724 if (__do_read(ff, info_linear->data, data_len))
2725 goto out;
2726
2727 info_node = malloc(sizeof(struct bpf_prog_info_node));
2728 if (!info_node)
2729 goto out;
2730
2731 /* after reading from file, translate offset to address */
2732 bpf_program__bpil_offs_to_addr(info_linear);
2733 info_node->info_linear = info_linear;
2734 perf_env__insert_bpf_prog_info(env, info_node);
2735 }
2736
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002737 up_write(&env->bpf_progs.lock);
Song Liu606f9722019-03-11 22:30:43 -07002738 return 0;
2739out:
2740 free(info_linear);
2741 free(info_node);
2742 up_write(&env->bpf_progs.lock);
2743 return err;
2744}
2745#else // HAVE_LIBBPF_SUPPORT
2746static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data __maybe_unused)
2747{
2748 return 0;
2749}
2750#endif // HAVE_LIBBPF_SUPPORT
2751
Song Liua70a112312019-03-11 22:30:45 -07002752static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)
2753{
2754 struct perf_env *env = &ff->ph->env;
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002755 struct btf_node *node = NULL;
Song Liua70a112312019-03-11 22:30:45 -07002756 u32 count, i;
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002757 int err = -1;
Song Liua70a112312019-03-11 22:30:45 -07002758
2759 if (ff->ph->needs_swap) {
2760 pr_warning("interpreting btf from systems with endianity is not yet supported\n");
2761 return 0;
2762 }
2763
2764 if (do_read_u32(ff, &count))
2765 return -1;
2766
2767 down_write(&env->bpf_progs.lock);
2768
2769 for (i = 0; i < count; ++i) {
Song Liua70a112312019-03-11 22:30:45 -07002770 u32 id, data_size;
2771
2772 if (do_read_u32(ff, &id))
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002773 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002774 if (do_read_u32(ff, &data_size))
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002775 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002776
2777 node = malloc(sizeof(struct btf_node) + data_size);
2778 if (!node)
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002779 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002780
2781 node->id = id;
2782 node->data_size = data_size;
2783
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002784 if (__do_read(ff, node->data, data_size))
2785 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002786
2787 perf_env__insert_btf(env, node);
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002788 node = NULL;
Song Liua70a112312019-03-11 22:30:45 -07002789 }
2790
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002791 err = 0;
2792out:
Song Liua70a112312019-03-11 22:30:45 -07002793 up_write(&env->bpf_progs.lock);
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002794 free(node);
2795 return err;
Song Liua70a112312019-03-11 22:30:45 -07002796}
2797
Alexey Budankov42e1fd82019-03-18 20:41:33 +03002798static int process_compressed(struct feat_fd *ff,
2799 void *data __maybe_unused)
2800{
2801 if (do_read_u32(ff, &(ff->ph->env.comp_ver)))
2802 return -1;
2803
2804 if (do_read_u32(ff, &(ff->ph->env.comp_type)))
2805 return -1;
2806
2807 if (do_read_u32(ff, &(ff->ph->env.comp_level)))
2808 return -1;
2809
2810 if (do_read_u32(ff, &(ff->ph->env.comp_ratio)))
2811 return -1;
2812
2813 if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len)))
2814 return -1;
2815
2816 return 0;
2817}
2818
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002819#define FEAT_OPR(n, func, __full_only) \
2820 [HEADER_##n] = { \
2821 .name = __stringify(n), \
2822 .write = write_##func, \
2823 .print = print_##func, \
2824 .full_only = __full_only, \
2825 .process = process_##func, \
2826 .synthesize = true \
2827 }
2828
2829#define FEAT_OPN(n, func, __full_only) \
2830 [HEADER_##n] = { \
2831 .name = __stringify(n), \
2832 .write = write_##func, \
2833 .print = print_##func, \
2834 .full_only = __full_only, \
2835 .process = process_##func \
2836 }
Robert Richter8cdfa782011-12-07 10:02:56 +01002837
2838/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002839#define print_tracing_data NULL
2840#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002841
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002842#define process_branch_stack NULL
2843#define process_stat NULL
2844
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03002845// Only used in util/synthetic-events.c
2846const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002847
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03002848const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002849 FEAT_OPN(TRACING_DATA, tracing_data, false),
2850 FEAT_OPN(BUILD_ID, build_id, false),
2851 FEAT_OPR(HOSTNAME, hostname, false),
2852 FEAT_OPR(OSRELEASE, osrelease, false),
2853 FEAT_OPR(VERSION, version, false),
2854 FEAT_OPR(ARCH, arch, false),
2855 FEAT_OPR(NRCPUS, nrcpus, false),
2856 FEAT_OPR(CPUDESC, cpudesc, false),
2857 FEAT_OPR(CPUID, cpuid, false),
2858 FEAT_OPR(TOTAL_MEM, total_mem, false),
2859 FEAT_OPR(EVENT_DESC, event_desc, false),
2860 FEAT_OPR(CMDLINE, cmdline, false),
2861 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2862 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2863 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2864 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
Jiri Olsae8fedff2018-07-12 15:52:02 +02002865 FEAT_OPR(GROUP_DESC, group_desc, false),
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002866 FEAT_OPN(AUXTRACE, auxtrace, false),
2867 FEAT_OPN(STAT, stat, false),
2868 FEAT_OPN(CACHE, cache, true),
Jin Yao60115182017-12-08 21:13:41 +08002869 FEAT_OPR(SAMPLE_TIME, sample_time, false),
Jiri Olsae2091ce2018-03-07 16:50:08 +01002870 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
Jiri Olsa258031c2019-03-08 14:47:39 +01002871 FEAT_OPR(CLOCKID, clockid, false),
Song Liu606f9722019-03-11 22:30:43 -07002872 FEAT_OPN(DIR_FORMAT, dir_format, false),
Song Liua70a112312019-03-11 22:30:45 -07002873 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false),
2874 FEAT_OPR(BPF_BTF, bpf_btf, false),
Alexey Budankov42e1fd82019-03-18 20:41:33 +03002875 FEAT_OPR(COMPRESSED, compressed, false),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002876};
2877
2878struct header_print_data {
2879 FILE *fp;
2880 bool full; /* extended list of headers */
2881};
2882
2883static int perf_file_section__fprintf_info(struct perf_file_section *section,
2884 struct perf_header *ph,
2885 int feat, int fd, void *data)
2886{
2887 struct header_print_data *hd = data;
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002888 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002889
2890 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2891 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2892 "%d, continuing...\n", section->offset, feat);
2893 return 0;
2894 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002895 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002896 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002897 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002898 }
2899 if (!feat_ops[feat].print)
2900 return 0;
2901
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002902 ff = (struct feat_fd) {
2903 .fd = fd,
2904 .ph = ph,
2905 };
2906
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002907 if (!feat_ops[feat].full_only || hd->full)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002908 feat_ops[feat].print(&ff, hd->fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002909 else
2910 fprintf(hd->fp, "# %s info available, use -I to display\n",
2911 feat_ops[feat].name);
2912
2913 return 0;
2914}
2915
2916int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2917{
2918 struct header_print_data hd;
2919 struct perf_header *header = &session->header;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002920 int fd = perf_data__fd(session->data);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002921 struct stat st;
Arnaldo Carvalho de Melo0afcf292018-12-11 16:11:54 -03002922 time_t stctime;
Jiri Olsaaabae162016-10-10 09:35:50 +02002923 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002924
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002925 hd.fp = fp;
2926 hd.full = full;
2927
Jiri Olsaf45f5612016-10-10 09:03:07 +02002928 ret = fstat(fd, &st);
2929 if (ret == -1)
2930 return -1;
2931
Arnaldo Carvalho de Melo0afcf292018-12-11 16:11:54 -03002932 stctime = st.st_ctime;
2933 fprintf(fp, "# captured on : %s", ctime(&stctime));
Jiri Olsae971a5a2018-03-07 16:50:03 +01002934
2935 fprintf(fp, "# header version : %u\n", header->version);
2936 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
2937 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
2938 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002939
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002940 perf_header__process_sections(header, fd, &hd,
2941 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002942
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002943 if (session->data->is_pipe)
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002944 return 0;
2945
Jiri Olsaaabae162016-10-10 09:35:50 +02002946 fprintf(fp, "# missing features: ");
2947 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2948 if (bit)
2949 fprintf(fp, "%s ", feat_ops[bit].name);
2950 }
2951
2952 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002953 return 0;
2954}
2955
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002956static int do_write_feat(struct feat_fd *ff, int type,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002957 struct perf_file_section **p,
Jiri Olsa63503db2019-07-21 13:23:52 +02002958 struct evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002959{
2960 int err;
2961 int ret = 0;
2962
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002963 if (perf_header__has_feat(ff->ph, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002964 if (!feat_ops[type].write)
2965 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002966
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -07002967 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2968 return -1;
2969
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002970 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002971
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002972 err = feat_ops[type].write(ff, evlist);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002973 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002974 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002975
2976 /* undo anything written */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002977 lseek(ff->fd, (*p)->offset, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002978
2979 return -1;
2980 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002981 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002982 (*p)++;
2983 }
2984 return ret;
2985}
2986
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002987static int perf_header__adds_write(struct perf_header *header,
Jiri Olsa63503db2019-07-21 13:23:52 +02002988 struct evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002989{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002990 int nr_sections;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002991 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002992 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002993 int sec_size;
2994 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002995 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002996 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002997
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002998 ff = (struct feat_fd){
2999 .fd = fd,
3000 .ph = header,
3001 };
3002
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003003 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003004 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003005 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003006
Paul Gortmaker91b98802013-01-30 20:05:49 -05003007 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003008 if (feat_sec == NULL)
3009 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003010
3011 sec_size = sizeof(*feat_sec) * nr_sections;
3012
Jiri Olsa8d541e92013-07-17 19:49:44 +02003013 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08003014 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003015
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003016 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003017 if (do_write_feat(&ff, feat, &p, evlist))
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003018 perf_header__clear_feat(header, feat);
3019 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003020
Xiao Guangrongf887f302010-02-04 16:46:42 +08003021 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003022 /*
3023 * may write more than needed due to dropped feature, but
Ingo Molnaradba1632018-12-03 11:22:00 +01003024 * this is okay, reader will skip the missing entries
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003025 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003026 err = do_write(&ff, feat_sec, sec_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003027 if (err < 0)
3028 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003029 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003030 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003031}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003032
Tom Zanussi8dc58102010-04-01 23:59:15 -05003033int perf_header__write_pipe(int fd)
3034{
3035 struct perf_pipe_file_header f_header;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003036 struct feat_fd ff;
Tom Zanussi8dc58102010-04-01 23:59:15 -05003037 int err;
3038
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003039 ff = (struct feat_fd){ .fd = fd };
3040
Tom Zanussi8dc58102010-04-01 23:59:15 -05003041 f_header = (struct perf_pipe_file_header){
3042 .magic = PERF_MAGIC,
3043 .size = sizeof(f_header),
3044 };
3045
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003046 err = do_write(&ff, &f_header, sizeof(f_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05003047 if (err < 0) {
3048 pr_debug("failed to write perf pipe header\n");
3049 return err;
3050 }
3051
3052 return 0;
3053}
3054
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003055int perf_session__write_header(struct perf_session *session,
Jiri Olsa63503db2019-07-21 13:23:52 +02003056 struct evlist *evlist,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003057 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003058{
3059 struct perf_file_header f_header;
3060 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003061 struct perf_header *header = &session->header;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003062 struct evsel *evsel;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003063 struct feat_fd ff;
Jiri Olsa944d62b2013-07-17 19:49:43 +02003064 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003065 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003066
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003067 ff = (struct feat_fd){ .fd = fd};
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003068 lseek(fd, sizeof(f_header), SEEK_SET);
3069
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003070 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003071 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsae7eb9002019-09-02 22:15:47 +02003072 err = do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003073 if (err < 0) {
3074 pr_debug("failed to write perf header\n");
3075 return err;
3076 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003077 }
3078
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003079 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003080
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003081 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003082 f_attr = (struct perf_file_attr){
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003083 .attr = evsel->core.attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003084 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02003085 .offset = evsel->id_offset,
Jiri Olsae7eb9002019-09-02 22:15:47 +02003086 .size = evsel->core.ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003087 }
3088 };
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003089 err = do_write(&ff, &f_attr, sizeof(f_attr));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003090 if (err < 0) {
3091 pr_debug("failed to write perf header attribute\n");
3092 return err;
3093 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003094 }
3095
Adrian Hunterd645c442013-12-11 14:36:28 +02003096 if (!header->data_offset)
3097 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02003098 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003099
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003100 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003101 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003102 if (err < 0)
3103 return err;
3104 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003105
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003106 f_header = (struct perf_file_header){
3107 .magic = PERF_MAGIC,
3108 .size = sizeof(f_header),
3109 .attr_size = sizeof(f_attr),
3110 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02003111 .offset = attr_offset,
Jiri Olsa6484d2f2019-07-21 13:24:28 +02003112 .size = evlist->core.nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003113 },
3114 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003115 .offset = header->data_offset,
3116 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003117 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02003118 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003119 };
3120
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003121 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003122
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003123 lseek(fd, 0, SEEK_SET);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003124 err = do_write(&ff, &f_header, sizeof(f_header));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003125 if (err < 0) {
3126 pr_debug("failed to write perf header\n");
3127 return err;
3128 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003129 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003130
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003131 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003132}
3133
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003134static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003135 int fd, void *buf, size_t size)
3136{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02003137 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003138 return -1;
3139
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003140 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003141 mem_bswap_64(buf, size);
3142
3143 return 0;
3144}
3145
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003146int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003147 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003148 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003149 struct perf_header *ph,
3150 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003151{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003152 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003153 int nr_sections;
3154 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003155 int feat;
3156 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003157
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003158 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003159 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003160 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003161
Paul Gortmaker91b98802013-01-30 20:05:49 -05003162 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003163 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003164 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003165
3166 sec_size = sizeof(*feat_sec) * nr_sections;
3167
Jiri Olsa8d541e92013-07-17 19:49:44 +02003168 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003169
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003170 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
3171 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003172 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003173
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003174 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
3175 err = process(sec++, header, feat, fd, data);
3176 if (err < 0)
3177 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01003178 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003179 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003180out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003181 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003182 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003183}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003184
Stephane Eranian114382a2012-02-09 23:21:08 +01003185static const int attr_file_abi_sizes[] = {
3186 [0] = PERF_ATTR_SIZE_VER0,
3187 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02003188 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02003189 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02003190 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01003191 0,
3192};
3193
3194/*
3195 * In the legacy file format, the magic number is not used to encode endianness.
3196 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
3197 * on ABI revisions, we need to try all combinations for all endianness to
3198 * detect the endianness.
3199 */
3200static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
3201{
3202 uint64_t ref_size, attr_size;
3203 int i;
3204
3205 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
3206 ref_size = attr_file_abi_sizes[i]
3207 + sizeof(struct perf_file_section);
3208 if (hdr_sz != ref_size) {
3209 attr_size = bswap_64(hdr_sz);
3210 if (attr_size != ref_size)
3211 continue;
3212
3213 ph->needs_swap = true;
3214 }
3215 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
3216 i,
3217 ph->needs_swap);
3218 return 0;
3219 }
3220 /* could not determine endianness */
3221 return -1;
3222}
3223
3224#define PERF_PIPE_HDR_VER0 16
3225
3226static const size_t attr_pipe_abi_sizes[] = {
3227 [0] = PERF_PIPE_HDR_VER0,
3228 0,
3229};
3230
3231/*
3232 * In the legacy pipe format, there is an implicit assumption that endiannesss
3233 * between host recording the samples, and host parsing the samples is the
3234 * same. This is not always the case given that the pipe output may always be
3235 * redirected into a file and analyzed on a different machine with possibly a
3236 * different endianness and perf_event ABI revsions in the perf tool itself.
3237 */
3238static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
3239{
3240 u64 attr_size;
3241 int i;
3242
3243 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
3244 if (hdr_sz != attr_pipe_abi_sizes[i]) {
3245 attr_size = bswap_64(hdr_sz);
3246 if (attr_size != hdr_sz)
3247 continue;
3248
3249 ph->needs_swap = true;
3250 }
3251 pr_debug("Pipe ABI%d perf.data file detected\n", i);
3252 return 0;
3253 }
3254 return -1;
3255}
3256
Feng Tange84ba4e2012-10-30 11:56:07 +08003257bool is_perf_magic(u64 magic)
3258{
3259 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
3260 || magic == __perf_magic2
3261 || magic == __perf_magic2_sw)
3262 return true;
3263
3264 return false;
3265}
3266
Stephane Eranian114382a2012-02-09 23:21:08 +01003267static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3268 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01003269{
3270 int ret;
3271
3272 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01003273 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01003274 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02003275 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01003276 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01003277 if (is_pipe)
3278 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01003279
Stephane Eranian114382a2012-02-09 23:21:08 +01003280 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01003281 }
Stephane Eranian114382a2012-02-09 23:21:08 +01003282 /*
3283 * the new magic number serves two purposes:
3284 * - unique number to identify actual perf.data files
3285 * - encode endianness of file
3286 */
Namhyung Kimf7913972015-01-29 17:06:45 +09003287 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01003288
Stephane Eranian114382a2012-02-09 23:21:08 +01003289 /* check magic number with one endianness */
3290 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01003291 return 0;
3292
Stephane Eranian114382a2012-02-09 23:21:08 +01003293 /* check magic number with opposite endianness */
3294 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01003295 return -1;
3296
3297 ph->needs_swap = true;
3298
3299 return 0;
3300}
3301
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003302int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003303 struct perf_header *ph, int fd)
3304{
Jiri Olsa727ebd52013-11-28 11:30:14 +01003305 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01003306
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003307 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003308
Stephane Eranian73323f52012-02-02 13:54:44 +01003309 ret = readn(fd, header, sizeof(*header));
3310 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003311 return -1;
3312
Stephane Eranian114382a2012-02-09 23:21:08 +01003313 if (check_magic_endian(header->magic,
3314 header->attr_size, false, ph) < 0) {
3315 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01003316 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01003317 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003318
Stephane Eranian73323f52012-02-02 13:54:44 +01003319 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003320 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01003321 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003322 }
3323
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003324 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003325 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003326 if (header->size == offsetof(typeof(*header), adds_features))
3327 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003328 else
3329 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06003330 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06003331 /*
3332 * feature bitmap is declared as an array of unsigned longs --
3333 * not good since its size can differ between the host that
3334 * generated the data file and the host analyzing the file.
3335 *
3336 * We need to handle endianness, but we don't know the size of
3337 * the unsigned long where the file was generated. Take a best
3338 * guess at determining it: try 64-bit swap first (ie., file
3339 * created on a 64-bit host), and check if the hostname feature
3340 * bit is set (this feature bit is forced on as of fbe96f2).
3341 * If the bit is not, undo the 64-bit swap and try a 32-bit
3342 * swap. If the hostname bit is still not set (e.g., older data
3343 * file), punt and fallback to the original behavior --
3344 * clearing all feature bits and setting buildid.
3345 */
David Ahern80c01202012-06-08 11:47:51 -03003346 mem_bswap_64(&header->adds_features,
3347 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06003348
3349 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03003350 /* unswap as u64 */
3351 mem_bswap_64(&header->adds_features,
3352 BITS_TO_U64(HEADER_FEAT_BITS));
3353
3354 /* unswap as u32 */
3355 mem_bswap_32(&header->adds_features,
3356 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06003357 }
3358
3359 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3360 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3361 set_bit(HEADER_BUILD_ID, header->adds_features);
3362 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003363 }
3364
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003365 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003366 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003367
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003368 ph->data_offset = header->data.offset;
3369 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02003370 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003371 return 0;
3372}
3373
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003374static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003375 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003376 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003377{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003378 struct feat_fd fdd = {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07003379 .fd = fd,
3380 .ph = ph,
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003381 .size = section->size,
3382 .offset = section->offset,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07003383 };
3384
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003385 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02003386 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003387 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003388 return 0;
3389 }
3390
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003391 if (feat >= HEADER_LAST_FEATURE) {
3392 pr_debug("unknown feature %d, continuing...\n", feat);
3393 return 0;
3394 }
3395
Robert Richterf1c67db2012-02-10 15:41:56 +01003396 if (!feat_ops[feat].process)
3397 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003398
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003399 return feat_ops[feat].process(&fdd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003400}
3401
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003402static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05003403 struct perf_header *ph, int fd,
3404 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003405{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003406 struct feat_fd ff = {
3407 .fd = STDOUT_FILENO,
3408 .ph = ph,
3409 };
Jiri Olsa727ebd52013-11-28 11:30:14 +01003410 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01003411
3412 ret = readn(fd, header, sizeof(*header));
3413 if (ret <= 0)
3414 return -1;
3415
Stephane Eranian114382a2012-02-09 23:21:08 +01003416 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
3417 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05003418 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01003419 }
3420
3421 if (ph->needs_swap)
3422 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05003423
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003424 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05003425 return -1;
3426
Tom Zanussi8dc58102010-04-01 23:59:15 -05003427 return 0;
3428}
3429
Jiri Olsad4339562013-07-17 19:49:41 +02003430static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05003431{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003432 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05003433 struct perf_pipe_file_header f_header;
3434
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003435 if (perf_file_header__read_pipe(&f_header, header,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003436 perf_data__fd(session->data),
Tom Zanussi454c4072010-05-01 01:41:20 -05003437 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05003438 pr_debug("incompatible file format\n");
3439 return -EINVAL;
3440 }
3441
Tom Zanussi8dc58102010-04-01 23:59:15 -05003442 return 0;
3443}
3444
Stephane Eranian69996df2012-02-09 23:21:06 +01003445static int read_attr(int fd, struct perf_header *ph,
3446 struct perf_file_attr *f_attr)
3447{
3448 struct perf_event_attr *attr = &f_attr->attr;
3449 size_t sz, left;
3450 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01003451 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01003452
3453 memset(f_attr, 0, sizeof(*f_attr));
3454
3455 /* read minimal guaranteed structure */
3456 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3457 if (ret <= 0) {
3458 pr_debug("cannot read %d bytes of header attr\n",
3459 PERF_ATTR_SIZE_VER0);
3460 return -1;
3461 }
3462
3463 /* on file perf_event_attr size */
3464 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01003465
Stephane Eranian69996df2012-02-09 23:21:06 +01003466 if (ph->needs_swap)
3467 sz = bswap_32(sz);
3468
3469 if (sz == 0) {
3470 /* assume ABI0 */
3471 sz = PERF_ATTR_SIZE_VER0;
3472 } else if (sz > our_sz) {
3473 pr_debug("file uses a more recent and unsupported ABI"
3474 " (%zu bytes extra)\n", sz - our_sz);
3475 return -1;
3476 }
3477 /* what we have not yet read and that we know about */
3478 left = sz - PERF_ATTR_SIZE_VER0;
3479 if (left) {
3480 void *ptr = attr;
3481 ptr += PERF_ATTR_SIZE_VER0;
3482
3483 ret = readn(fd, ptr, left);
3484 }
3485 /* read perf_file_section, ids are read in caller */
3486 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3487
3488 return ret <= 0 ? -1 : 0;
3489}
3490
Jiri Olsa32dcd022019-07-21 13:23:51 +02003491static int perf_evsel__prepare_tracepoint_event(struct evsel *evsel,
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003492 struct tep_handle *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003493{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003494 struct tep_event *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003495 char bf[128];
3496
Namhyung Kim831394b2012-09-06 11:10:46 +09003497 /* already prepared */
3498 if (evsel->tp_format)
3499 return 0;
3500
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09003501 if (pevent == NULL) {
3502 pr_debug("broken or missing trace data\n");
3503 return -1;
3504 }
3505
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003506 event = tep_find_event(pevent, evsel->core.attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09003507 if (event == NULL) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003508 pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003509 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09003510 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003511
Namhyung Kim831394b2012-09-06 11:10:46 +09003512 if (!evsel->name) {
3513 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3514 evsel->name = strdup(bf);
3515 if (evsel->name == NULL)
3516 return -1;
3517 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003518
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03003519 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003520 return 0;
3521}
3522
Jiri Olsa63503db2019-07-21 13:23:52 +02003523static int perf_evlist__prepare_tracepoint_events(struct evlist *evlist,
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003524 struct tep_handle *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003525{
Jiri Olsa32dcd022019-07-21 13:23:51 +02003526 struct evsel *pos;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003527
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003528 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003529 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT &&
Namhyung Kim831394b2012-09-06 11:10:46 +09003530 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003531 return -1;
3532 }
3533
3534 return 0;
3535}
3536
Jiri Olsad4339562013-07-17 19:49:41 +02003537int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05003538{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003539 struct perf_data *data = session->data;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003540 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003541 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003542 struct perf_file_attr f_attr;
3543 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003544 int nr_attrs, nr_ids, i, j;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003545 int fd = perf_data__fd(data);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003546
Jiri Olsa0f98b112019-07-21 13:23:55 +02003547 session->evlist = evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003548 if (session->evlist == NULL)
3549 return -ENOMEM;
3550
Kan Liang2c071442015-08-28 05:48:05 -04003551 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03003552 session->machines.host.env = &header->env;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003553 if (perf_data__is_pipe(data))
Jiri Olsad4339562013-07-17 19:49:41 +02003554 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05003555
Stephane Eranian69996df2012-02-09 23:21:06 +01003556 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003557 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003558
Namhyung Kimb314e5c2013-09-30 17:19:48 +09003559 /*
3560 * Sanity check that perf.data was written cleanly; data size is
3561 * initialized to 0 and updated only if the on_exit function is run.
3562 * If data size is still 0 then the file contains only partial
3563 * information. Just warn user and process it as much as it can.
3564 */
3565 if (f_header.data.size == 0) {
3566 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
3567 "Was the 'perf record' command properly terminated?\n",
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003568 data->file.path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09003569 }
3570
Vince Weaver76222362019-07-23 11:06:01 -04003571 if (f_header.attr_size == 0) {
3572 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
3573 "Was the 'perf record' command properly terminated?\n",
3574 data->file.path);
3575 return -EINVAL;
3576 }
3577
Stephane Eranian69996df2012-02-09 23:21:06 +01003578 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003579 lseek(fd, f_header.attrs.offset, SEEK_SET);
3580
3581 for (i = 0; i < nr_attrs; i++) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02003582 struct evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02003583 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003584
Stephane Eranian69996df2012-02-09 23:21:06 +01003585 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003586 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003587
David Ahern1060ab82015-04-09 16:15:46 -04003588 if (header->needs_swap) {
3589 f_attr.ids.size = bswap_64(f_attr.ids.size);
3590 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06003591 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04003592 }
David Aherneda39132011-07-15 12:34:09 -06003593
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02003594 tmp = lseek(fd, 0, SEEK_CUR);
Jiri Olsa365c3ae2019-07-21 13:23:58 +02003595 evsel = evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003596
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003597 if (evsel == NULL)
3598 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03003599
3600 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003601 /*
3602 * Do it before so that if perf_evsel__alloc_id fails, this
Jiri Olsac12995a2019-07-21 13:23:56 +02003603 * entry gets purged too at evlist__delete().
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003604 */
Jiri Olsaa1cf3a72019-07-21 13:23:59 +02003605 evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003606
3607 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003608 /*
3609 * We don't have the cpu and thread maps on the header, so
3610 * for allocating the perf_sample_id table we fake 1 cpu and
3611 * hattr->ids threads.
3612 */
Jiri Olsa70c20362019-09-03 10:34:29 +02003613 if (perf_evsel__alloc_id(&evsel->core, 1, nr_ids))
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003614 goto out_delete_evlist;
3615
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003616 lseek(fd, f_attr.ids.offset, SEEK_SET);
3617
3618 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003619 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003620 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003621
Jiri Olsab0031c22019-09-03 11:01:04 +02003622 perf_evlist__id_add(&session->evlist->core, &evsel->core, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003623 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02003624
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003625 lseek(fd, tmp, SEEK_SET);
3626 }
3627
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003628 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003629 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01003630
Namhyung Kim831394b2012-09-06 11:10:46 +09003631 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003632 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003633 goto out_delete_evlist;
3634
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003635 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003636out_errno:
3637 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003638
3639out_delete_evlist:
Jiri Olsac12995a2019-07-21 13:23:56 +02003640 evlist__delete(session->evlist);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003641 session->evlist = NULL;
3642 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003643}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02003644
Jiri Olsa89f16882018-09-13 14:54:03 +02003645int perf_event__process_feature(struct perf_session *session,
3646 union perf_event *event)
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003647{
Jiri Olsa89f16882018-09-13 14:54:03 +02003648 struct perf_tool *tool = session->tool;
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003649 struct feat_fd ff = { .fd = 0 };
Jiri Olsa72932372019-08-28 15:57:16 +02003650 struct perf_record_header_feature *fe = (struct perf_record_header_feature *)event;
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003651 int type = fe->header.type;
3652 u64 feat = fe->feat_id;
3653
3654 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3655 pr_warning("invalid record type %d in pipe-mode\n", type);
3656 return 0;
3657 }
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303658 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003659 pr_warning("invalid record type %d in pipe-mode\n", type);
3660 return -1;
3661 }
3662
3663 if (!feat_ops[feat].process)
3664 return 0;
3665
3666 ff.buf = (void *)fe->data;
Jiri Olsa79b2fe52019-07-15 16:04:26 +02003667 ff.size = event->header.size - sizeof(*fe);
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003668 ff.ph = &session->header;
3669
3670 if (feat_ops[feat].process(&ff, NULL))
3671 return -1;
3672
3673 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3674 return 0;
3675
3676 if (!feat_ops[feat].full_only ||
3677 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3678 feat_ops[feat].print(&ff, stdout);
3679 } else {
3680 fprintf(stdout, "# %s info available, use -I to display\n",
3681 feat_ops[feat].name);
3682 }
3683
3684 return 0;
3685}
3686
Jiri Olsac853f932015-10-25 15:51:41 +01003687size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3688{
Jiri Olsa72932372019-08-28 15:57:16 +02003689 struct perf_record_event_update *ev = &event->event_update;
3690 struct perf_record_event_update_scale *ev_scale;
3691 struct perf_record_event_update_cpus *ev_cpus;
Jiri Olsaf8548392019-07-21 13:23:49 +02003692 struct perf_cpu_map *map;
Jiri Olsac853f932015-10-25 15:51:41 +01003693 size_t ret;
3694
Jiri Olsa5ded0682019-08-28 15:56:57 +02003695 ret = fprintf(fp, "\n... id: %" PRI_lu64 "\n", ev->id);
Jiri Olsac853f932015-10-25 15:51:41 +01003696
3697 switch (ev->type) {
3698 case PERF_EVENT_UPDATE__SCALE:
Jiri Olsa72932372019-08-28 15:57:16 +02003699 ev_scale = (struct perf_record_event_update_scale *)ev->data;
Jiri Olsac853f932015-10-25 15:51:41 +01003700 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3701 break;
3702 case PERF_EVENT_UPDATE__UNIT:
3703 ret += fprintf(fp, "... unit: %s\n", ev->data);
3704 break;
3705 case PERF_EVENT_UPDATE__NAME:
3706 ret += fprintf(fp, "... name: %s\n", ev->data);
3707 break;
3708 case PERF_EVENT_UPDATE__CPUS:
Jiri Olsa72932372019-08-28 15:57:16 +02003709 ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
Jiri Olsac853f932015-10-25 15:51:41 +01003710 ret += fprintf(fp, "... ");
3711
3712 map = cpu_map__new_data(&ev_cpus->cpus);
3713 if (map)
3714 ret += cpu_map__fprintf(map, fp);
3715 else
3716 ret += fprintf(fp, "failed to get cpus\n");
3717 break;
3718 default:
3719 ret += fprintf(fp, "... unknown type\n");
3720 break;
3721 }
3722
3723 return ret;
3724}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003725
Adrian Hunter47c3d102013-07-04 16:20:21 +03003726int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3727 union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02003728 struct evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003729{
Robert Richterf4d83432012-08-16 21:10:17 +02003730 u32 i, ids, n_ids;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003731 struct evsel *evsel;
Jiri Olsa63503db2019-07-21 13:23:52 +02003732 struct evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003733
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003734 if (evlist == NULL) {
Jiri Olsa0f98b112019-07-21 13:23:55 +02003735 *pevlist = evlist = evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003736 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003737 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003738 }
3739
Jiri Olsa365c3ae2019-07-21 13:23:58 +02003740 evsel = evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003741 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003742 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003743
Jiri Olsaa1cf3a72019-07-21 13:23:59 +02003744 evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003745
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003746 ids = event->header.size;
3747 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003748 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003749 /*
3750 * We don't have the cpu and thread maps on the header, so
3751 * for allocating the perf_sample_id table we fake 1 cpu and
3752 * hattr->ids threads.
3753 */
Jiri Olsa70c20362019-09-03 10:34:29 +02003754 if (perf_evsel__alloc_id(&evsel->core, 1, n_ids))
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003755 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003756
3757 for (i = 0; i < n_ids; i++) {
Jiri Olsab0031c22019-09-03 11:01:04 +02003758 perf_evlist__id_add(&evlist->core, &evsel->core, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003759 }
3760
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003761 return 0;
3762}
Tom Zanussicd19a032010-04-01 23:59:20 -05003763
Jiri Olsaffe777252015-10-25 15:51:36 +01003764int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3765 union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02003766 struct evlist **pevlist)
Jiri Olsaffe777252015-10-25 15:51:36 +01003767{
Jiri Olsa72932372019-08-28 15:57:16 +02003768 struct perf_record_event_update *ev = &event->event_update;
3769 struct perf_record_event_update_scale *ev_scale;
3770 struct perf_record_event_update_cpus *ev_cpus;
Jiri Olsa63503db2019-07-21 13:23:52 +02003771 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003772 struct evsel *evsel;
Jiri Olsaf8548392019-07-21 13:23:49 +02003773 struct perf_cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003774
3775 if (!pevlist || *pevlist == NULL)
3776 return -EINVAL;
3777
3778 evlist = *pevlist;
3779
3780 evsel = perf_evlist__id2evsel(evlist, ev->id);
3781 if (evsel == NULL)
3782 return -EINVAL;
3783
Jiri Olsaa6e52812015-10-25 15:51:37 +01003784 switch (ev->type) {
3785 case PERF_EVENT_UPDATE__UNIT:
3786 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003787 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003788 case PERF_EVENT_UPDATE__NAME:
3789 evsel->name = strdup(ev->data);
3790 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003791 case PERF_EVENT_UPDATE__SCALE:
Jiri Olsa72932372019-08-28 15:57:16 +02003792 ev_scale = (struct perf_record_event_update_scale *)ev->data;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003793 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003794 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003795 case PERF_EVENT_UPDATE__CPUS:
Jiri Olsa72932372019-08-28 15:57:16 +02003796 ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003797
3798 map = cpu_map__new_data(&ev_cpus->cpus);
3799 if (map)
Jiri Olsafe1f61b2019-07-21 13:24:38 +02003800 evsel->core.own_cpus = map;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003801 else
3802 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003803 default:
3804 break;
3805 }
3806
Jiri Olsaffe777252015-10-25 15:51:36 +01003807 return 0;
3808}
3809
Jiri Olsa89f16882018-09-13 14:54:03 +02003810int perf_event__process_tracing_data(struct perf_session *session,
3811 union perf_event *event)
Tom Zanussi92155452010-04-01 23:59:21 -05003812{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003813 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003814 int fd = perf_data__fd(session->data);
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003815 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003816 char buf[BUFSIZ];
3817
3818 /* setup for reading amidst mmap */
Jiri Olsa72932372019-08-28 15:57:16 +02003819 lseek(fd, offset + sizeof(struct perf_record_header_tracing_data),
Tom Zanussi92155452010-04-01 23:59:21 -05003820 SEEK_SET);
3821
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003822 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003823 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003824 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003825
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003826 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003827 pr_err("%s: reading input file", __func__);
3828 return -1;
3829 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003830 if (session->repipe) {
3831 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003832 if (retw <= 0 || retw != padding) {
3833 pr_err("%s: repiping tracing data padding", __func__);
3834 return -1;
3835 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003836 }
Tom Zanussi92155452010-04-01 23:59:21 -05003837
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003838 if (size_read + padding != size) {
3839 pr_err("%s: tracing data size mismatch", __func__);
3840 return -1;
3841 }
Tom Zanussi92155452010-04-01 23:59:21 -05003842
Namhyung Kim831394b2012-09-06 11:10:46 +09003843 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003844 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003845
Tom Zanussi92155452010-04-01 23:59:21 -05003846 return size_read + padding;
3847}
Tom Zanussic7929e42010-04-01 23:59:22 -05003848
Jiri Olsa89f16882018-09-13 14:54:03 +02003849int perf_event__process_build_id(struct perf_session *session,
3850 union perf_event *event)
Tom Zanussic7929e42010-04-01 23:59:22 -05003851{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003852 __event_process_build_id(&event->build_id,
3853 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003854 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003855 return 0;
3856}