blob: 0167f9697172b0ddb441ac8f5510602b0fd9fc7c [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"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020028#include "header.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030029#include "memswap.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020030#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020031#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010032#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010033#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020034#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020035#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020036#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090037#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020038#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020039#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010040#include <api/fs/fs.h>
41#include "asm/bug.h"
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -070042#include "tool.h"
Jin Yao60115182017-12-08 21:13:41 +080043#include "time-utils.h"
Jiri Olsae2091ce2018-03-07 16:50:08 +010044#include "units.h"
Arnaldo Carvalho de Melofb71c86c2019-09-03 10:56:06 -030045#include "util.h" // page_size, perf_exe()
Jiri Olsa5135d5e2019-02-19 10:58:13 +010046#include "cputopo.h"
Song Liu606f9722019-03-11 22:30:43 -070047#include "bpf-event.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020048
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030049#include <linux/ctype.h>
Arnaldo Carvalho de Melofb71c86c2019-09-03 10:56:06 -030050#include <internal/lib.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030051
Stephane Eranian73323f52012-02-02 13:54:44 +010052/*
53 * magic2 = "PERFILE2"
54 * must be a numerical value to let the endianness
55 * determine the memory layout. That way we are able
56 * to detect endianness when reading the perf.data file
57 * back.
58 *
59 * we check for legacy (PERFFILE) format.
60 */
61static const char *__perf_magic1 = "PERFFILE";
62static const u64 __perf_magic2 = 0x32454c4946524550ULL;
63static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020064
Stephane Eranian73323f52012-02-02 13:54:44 +010065#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020066
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090067const char perf_version_string[] = PERF_VERSION;
68
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020069struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020070 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020071 struct perf_file_section ids;
72};
73
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030074void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020075{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030076 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020077}
78
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030079void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020080{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030081 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020082}
83
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030084bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020085{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030086 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020087}
88
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -070089static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
90{
91 ssize_t ret = writen(ff->fd, buf, size);
92
93 if (ret != (ssize_t)size)
94 return ret < 0 ? (int)ret : -1;
95 return 0;
96}
97
98static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
99{
100 /* struct perf_event_header::size is u16 */
101 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
102 size_t new_size = ff->size;
103 void *addr;
104
105 if (size + ff->offset > max_size)
106 return -E2BIG;
107
108 while (size > (new_size - ff->offset))
109 new_size <<= 1;
110 new_size = min(max_size, new_size);
111
112 if (ff->size < new_size) {
113 addr = realloc(ff->buf, new_size);
114 if (!addr)
115 return -ENOMEM;
116 ff->buf = addr;
117 ff->size = new_size;
118 }
119
120 memcpy(ff->buf + ff->offset, buf, size);
121 ff->offset += size;
122
123 return 0;
124}
125
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700126/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700127int do_write(struct feat_fd *ff, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200128{
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700129 if (!ff->buf)
130 return __do_write_fd(ff, buf, size);
131 return __do_write_buf(ff, buf, size);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200132}
133
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700134/* Return: 0 if succeded, -ERR if failed. */
Jiri Olsae2091ce2018-03-07 16:50:08 +0100135static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
136{
137 u64 *p = (u64 *) set;
138 int i, ret;
139
140 ret = do_write(ff, &size, sizeof(size));
141 if (ret < 0)
142 return ret;
143
144 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
145 ret = do_write(ff, p + i, sizeof(*p));
146 if (ret < 0)
147 return ret;
148 }
149
150 return 0;
151}
152
153/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700154int write_padded(struct feat_fd *ff, const void *bf,
155 size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200156{
157 static const char zero_buf[NAME_ALIGN];
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700158 int err = do_write(ff, bf, count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200159
160 if (!err)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700161 err = do_write(ff, zero_buf, count_aligned - count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200162
163 return err;
164}
165
Kan Liang2bb00d22015-09-01 09:58:12 -0400166#define string_size(str) \
167 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
168
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700169/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700170static int do_write_string(struct feat_fd *ff, const char *str)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200171{
172 u32 len, olen;
173 int ret;
174
175 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300176 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200177
178 /* write len, incl. \0 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700179 ret = do_write(ff, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200180 if (ret < 0)
181 return ret;
182
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700183 return write_padded(ff, str, olen, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200184}
185
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700186static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700187{
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700188 ssize_t ret = readn(ff->fd, addr, size);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700189
190 if (ret != size)
191 return ret < 0 ? (int)ret : -1;
192 return 0;
193}
194
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700195static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
196{
197 if (size > (ssize_t)ff->size - ff->offset)
198 return -1;
199
200 memcpy(addr, ff->buf + ff->offset, size);
201 ff->offset += size;
202
203 return 0;
204
205}
206
207static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
208{
209 if (!ff->buf)
210 return __do_read_fd(ff, addr, size);
211 return __do_read_buf(ff, addr, size);
212}
213
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700214static int do_read_u32(struct feat_fd *ff, u32 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700215{
216 int ret;
217
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700218 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700219 if (ret)
220 return ret;
221
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700222 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700223 *addr = bswap_32(*addr);
224 return 0;
225}
226
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700227static int do_read_u64(struct feat_fd *ff, u64 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700228{
229 int ret;
230
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700231 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700232 if (ret)
233 return ret;
234
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700235 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700236 *addr = bswap_64(*addr);
237 return 0;
238}
239
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700240static char *do_read_string(struct feat_fd *ff)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200241{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200242 u32 len;
243 char *buf;
244
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700245 if (do_read_u32(ff, &len))
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200246 return NULL;
247
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200248 buf = malloc(len);
249 if (!buf)
250 return NULL;
251
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700252 if (!__do_read(ff, buf, len)) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200253 /*
254 * strings are padded by zeroes
255 * thus the actual strlen of buf
256 * may be less than len
257 */
258 return buf;
259 }
260
261 free(buf);
262 return NULL;
263}
264
Jiri Olsae2091ce2018-03-07 16:50:08 +0100265/* Return: 0 if succeded, -ERR if failed. */
266static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
267{
268 unsigned long *set;
269 u64 size, *p;
270 int i, ret;
271
272 ret = do_read_u64(ff, &size);
273 if (ret)
274 return ret;
275
276 set = bitmap_alloc(size);
277 if (!set)
278 return -ENOMEM;
279
Jiri Olsae2091ce2018-03-07 16:50:08 +0100280 p = (u64 *) set;
281
282 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
283 ret = do_read_u64(ff, p + i);
284 if (ret < 0) {
285 free(set);
286 return ret;
287 }
288 }
289
290 *pset = set;
291 *psize = size;
292 return 0;
293}
294
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700295static int write_tracing_data(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200296 struct evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200297{
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700298 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
299 return -1;
300
Jiri Olsace9036a2019-07-21 13:24:23 +0200301 return read_tracing_data(ff->fd, &evlist->core.entries);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200302}
303
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700304static int write_build_id(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200305 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200306{
307 struct perf_session *session;
308 int err;
309
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700310 session = container_of(ff->ph, struct perf_session, header);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200311
Robert Richtere20960c2011-12-07 10:02:55 +0100312 if (!perf_session__read_build_ids(session, true))
313 return -1;
314
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700315 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
316 return -1;
317
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700318 err = perf_session__write_buildid_table(session, ff);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200319 if (err < 0) {
320 pr_debug("failed to write buildid table\n");
321 return err;
322 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900323 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200324
325 return 0;
326}
327
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700328static int write_hostname(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200329 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200330{
331 struct utsname uts;
332 int ret;
333
334 ret = uname(&uts);
335 if (ret < 0)
336 return -1;
337
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700338 return do_write_string(ff, uts.nodename);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200339}
340
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700341static int write_osrelease(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200342 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200343{
344 struct utsname uts;
345 int ret;
346
347 ret = uname(&uts);
348 if (ret < 0)
349 return -1;
350
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700351 return do_write_string(ff, uts.release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200352}
353
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700354static int write_arch(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200355 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200356{
357 struct utsname uts;
358 int ret;
359
360 ret = uname(&uts);
361 if (ret < 0)
362 return -1;
363
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700364 return do_write_string(ff, uts.machine);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200365}
366
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700367static int write_version(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200368 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200369{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700370 return do_write_string(ff, perf_version_string);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200371}
372
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700373static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200374{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200375 FILE *file;
376 char *buf = NULL;
377 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800378 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200379 size_t len = 0;
380 int ret = -1;
381
382 if (!search)
383 return -1;
384
385 file = fopen("/proc/cpuinfo", "r");
386 if (!file)
387 return -1;
388
389 while (getline(&buf, &len, file) > 0) {
390 ret = strncmp(buf, search, strlen(search));
391 if (!ret)
392 break;
393 }
394
Wang Naned307752014-10-16 11:08:29 +0800395 if (ret) {
396 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200397 goto done;
Wang Naned307752014-10-16 11:08:29 +0800398 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200399
400 s = buf;
401
402 p = strchr(buf, ':');
403 if (p && *(p+1) == ' ' && *(p+2))
404 s = p + 2;
405 p = strchr(s, '\n');
406 if (p)
407 *p = '\0';
408
409 /* squash extra space characters (branding string) */
410 p = s;
411 while (*p) {
412 if (isspace(*p)) {
413 char *r = p + 1;
Arnaldo Carvalho de Melofc6a1722019-06-25 21:33:14 -0300414 char *q = skip_spaces(r);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200415 *p = ' ';
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200416 if (q != (p+1))
417 while ((*r++ = *q++));
418 }
419 p++;
420 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700421 ret = do_write_string(ff, s);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200422done:
423 free(buf);
424 fclose(file);
425 return ret;
426}
427
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700428static int write_cpudesc(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200429 struct evlist *evlist __maybe_unused)
Wang Nan493c3032014-10-24 09:45:26 +0800430{
Arnaldo Carvalho de Meloa7749402019-08-29 14:40:28 -0300431#if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__)
432#define CPUINFO_PROC { "cpu", }
433#elif defined(__s390__)
434#define CPUINFO_PROC { "vendor_id", }
435#elif defined(__sh__)
436#define CPUINFO_PROC { "cpu type", }
437#elif defined(__alpha__) || defined(__mips__)
438#define CPUINFO_PROC { "cpu model", }
439#elif defined(__arm__)
440#define CPUINFO_PROC { "model name", "Processor", }
441#elif defined(__arc__)
442#define CPUINFO_PROC { "Processor", }
443#elif defined(__xtensa__)
444#define CPUINFO_PROC { "core ID", }
445#else
446#define CPUINFO_PROC { "model name", }
447#endif
Wang Nan493c3032014-10-24 09:45:26 +0800448 const char *cpuinfo_procs[] = CPUINFO_PROC;
Arnaldo Carvalho de Meloa7749402019-08-29 14:40:28 -0300449#undef CPUINFO_PROC
Wang Nan493c3032014-10-24 09:45:26 +0800450 unsigned int i;
451
452 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
453 int ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700454 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
Wang Nan493c3032014-10-24 09:45:26 +0800455 if (ret >= 0)
456 return ret;
457 }
458 return -1;
459}
460
461
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700462static int write_nrcpus(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200463 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200464{
465 long nr;
466 u32 nrc, nra;
467 int ret;
468
Jan Stancekda8a58b2017-02-17 12:10:26 +0100469 nrc = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200470
471 nr = sysconf(_SC_NPROCESSORS_ONLN);
472 if (nr < 0)
473 return -1;
474
475 nra = (u32)(nr & UINT_MAX);
476
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700477 ret = do_write(ff, &nrc, sizeof(nrc));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200478 if (ret < 0)
479 return ret;
480
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700481 return do_write(ff, &nra, sizeof(nra));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200482}
483
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700484static int write_event_desc(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200485 struct evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200486{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200487 struct evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900488 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200489 int ret;
490
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200491 nre = evlist->core.nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200492
493 /*
494 * write number of events
495 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700496 ret = do_write(ff, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200497 if (ret < 0)
498 return ret;
499
500 /*
501 * size of perf_event_attr struct
502 */
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200503 sz = (u32)sizeof(evsel->core.attr);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700504 ret = do_write(ff, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200505 if (ret < 0)
506 return ret;
507
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300508 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200509 ret = do_write(ff, &evsel->core.attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200510 if (ret < 0)
511 return ret;
512 /*
513 * write number of unique id per event
514 * there is one id per instance of an event
515 *
516 * copy into an nri to be independent of the
517 * type of ids,
518 */
Robert Richter6606f872012-08-16 21:10:19 +0200519 nri = evsel->ids;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700520 ret = do_write(ff, &nri, sizeof(nri));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200521 if (ret < 0)
522 return ret;
523
524 /*
525 * write event string as passed on cmdline
526 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700527 ret = do_write_string(ff, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200528 if (ret < 0)
529 return ret;
530 /*
531 * write unique ids for this event
532 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700533 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200534 if (ret < 0)
535 return ret;
536 }
537 return 0;
538}
539
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700540static int write_cmdline(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200541 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200542{
Andi Kleen94816ad2019-02-24 07:37:19 -0800543 char pbuf[MAXPATHLEN], *buf;
544 int i, ret, n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200545
Tommi Rantala55f771282017-03-22 15:06:24 +0200546 /* actual path to perf binary */
Andi Kleen94816ad2019-02-24 07:37:19 -0800547 buf = perf_exe(pbuf, MAXPATHLEN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200548
549 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300550 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200551
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700552 ret = do_write(ff, &n, sizeof(n));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200553 if (ret < 0)
554 return ret;
555
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700556 ret = do_write_string(ff, buf);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200557 if (ret < 0)
558 return ret;
559
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300560 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700561 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200562 if (ret < 0)
563 return ret;
564 }
565 return 0;
566}
567
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200568
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700569static int write_cpu_topology(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200570 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200571{
Jiri Olsa5135d5e2019-02-19 10:58:13 +0100572 struct cpu_topology *tp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200573 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300574 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200575
Jiri Olsa5135d5e2019-02-19 10:58:13 +0100576 tp = cpu_topology__new();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200577 if (!tp)
578 return -1;
579
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700580 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200581 if (ret < 0)
582 goto done;
583
584 for (i = 0; i < tp->core_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700585 ret = do_write_string(ff, tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200586 if (ret < 0)
587 goto done;
588 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700589 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200590 if (ret < 0)
591 goto done;
592
593 for (i = 0; i < tp->thread_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700594 ret = do_write_string(ff, tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200595 if (ret < 0)
596 break;
597 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400598
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300599 ret = perf_env__read_cpu_topology_map(&perf_env);
600 if (ret < 0)
601 goto done;
602
603 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700604 ret = do_write(ff, &perf_env.cpu[j].core_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300605 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400606 if (ret < 0)
607 return ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700608 ret = do_write(ff, &perf_env.cpu[j].socket_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300609 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400610 if (ret < 0)
611 return ret;
612 }
Kan Liangacae8b32019-06-04 15:50:41 -0700613
614 if (!tp->die_sib)
615 goto done;
616
617 ret = do_write(ff, &tp->die_sib, sizeof(tp->die_sib));
618 if (ret < 0)
619 goto done;
620
621 for (i = 0; i < tp->die_sib; i++) {
622 ret = do_write_string(ff, tp->die_siblings[i]);
623 if (ret < 0)
624 goto done;
625 }
626
627 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
628 ret = do_write(ff, &perf_env.cpu[j].die_id,
629 sizeof(perf_env.cpu[j].die_id));
630 if (ret < 0)
631 return ret;
632 }
633
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200634done:
Jiri Olsa5135d5e2019-02-19 10:58:13 +0100635 cpu_topology__delete(tp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200636 return ret;
637}
638
639
640
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700641static int write_total_mem(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200642 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200643{
644 char *buf = NULL;
645 FILE *fp;
646 size_t len = 0;
647 int ret = -1, n;
648 uint64_t mem;
649
650 fp = fopen("/proc/meminfo", "r");
651 if (!fp)
652 return -1;
653
654 while (getline(&buf, &len, fp) > 0) {
655 ret = strncmp(buf, "MemTotal:", 9);
656 if (!ret)
657 break;
658 }
659 if (!ret) {
660 n = sscanf(buf, "%*s %"PRIu64, &mem);
661 if (n == 1)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700662 ret = do_write(ff, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800663 } else
664 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200665 free(buf);
666 fclose(fp);
667 return ret;
668}
669
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700670static int write_numa_topology(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200671 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200672{
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100673 struct numa_topology *tp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200674 int ret = -1;
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100675 u32 i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200676
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100677 tp = numa_topology__new();
678 if (!tp)
679 return -ENOMEM;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200680
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100681 ret = do_write(ff, &tp->nr, sizeof(u32));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200682 if (ret < 0)
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100683 goto err;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200684
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100685 for (i = 0; i < tp->nr; i++) {
686 struct numa_topology_node *n = &tp->nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200687
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100688 ret = do_write(ff, &n->node, sizeof(u32));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200689 if (ret < 0)
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100690 goto err;
691
692 ret = do_write(ff, &n->mem_total, sizeof(u64));
693 if (ret)
694 goto err;
695
696 ret = do_write(ff, &n->mem_free, sizeof(u64));
697 if (ret)
698 goto err;
699
700 ret = do_write_string(ff, n->cpus);
701 if (ret < 0)
702 goto err;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200703 }
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100704
705 ret = 0;
706
707err:
708 numa_topology__delete(tp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200709 return ret;
710}
711
712/*
Robert Richter50a96672012-08-16 21:10:24 +0200713 * File format:
714 *
715 * struct pmu_mappings {
716 * u32 pmu_num;
717 * struct pmu_map {
718 * u32 type;
719 * char name[];
720 * }[pmu_num];
721 * };
722 */
723
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700724static int write_pmu_mappings(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200725 struct evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200726{
727 struct perf_pmu *pmu = NULL;
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700728 u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900729 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200730
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700731 /*
732 * Do a first pass to count number of pmu to avoid lseek so this
733 * works in pipe mode as well.
734 */
735 while ((pmu = perf_pmu__scan(pmu))) {
736 if (!pmu->name)
737 continue;
738 pmu_num++;
739 }
740
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700741 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
Namhyung Kim5323f602012-12-17 15:38:54 +0900742 if (ret < 0)
743 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200744
745 while ((pmu = perf_pmu__scan(pmu))) {
746 if (!pmu->name)
747 continue;
Namhyung Kim5323f602012-12-17 15:38:54 +0900748
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700749 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
Namhyung Kim5323f602012-12-17 15:38:54 +0900750 if (ret < 0)
751 return ret;
752
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700753 ret = do_write_string(ff, pmu->name);
Namhyung Kim5323f602012-12-17 15:38:54 +0900754 if (ret < 0)
755 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200756 }
757
Robert Richter50a96672012-08-16 21:10:24 +0200758 return 0;
759}
760
761/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900762 * File format:
763 *
764 * struct group_descs {
765 * u32 nr_groups;
766 * struct group_desc {
767 * char name[];
768 * u32 leader_idx;
769 * u32 nr_members;
770 * }[nr_groups];
771 * };
772 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700773static int write_group_desc(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200774 struct evlist *evlist)
Namhyung Kima8bb5592013-01-22 18:09:31 +0900775{
776 u32 nr_groups = evlist->nr_groups;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200777 struct evsel *evsel;
Namhyung Kima8bb5592013-01-22 18:09:31 +0900778 int ret;
779
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700780 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900781 if (ret < 0)
782 return ret;
783
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300784 evlist__for_each_entry(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900785 if (perf_evsel__is_group_leader(evsel) &&
Jiri Olsa5643b1a2019-07-21 13:24:46 +0200786 evsel->core.nr_members > 1) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900787 const char *name = evsel->group_name ?: "{anon_group}";
788 u32 leader_idx = evsel->idx;
Jiri Olsa5643b1a2019-07-21 13:24:46 +0200789 u32 nr_members = evsel->core.nr_members;
Namhyung Kima8bb5592013-01-22 18:09:31 +0900790
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700791 ret = do_write_string(ff, name);
Namhyung Kima8bb5592013-01-22 18:09:31 +0900792 if (ret < 0)
793 return ret;
794
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700795 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900796 if (ret < 0)
797 return ret;
798
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700799 ret = do_write(ff, &nr_members, sizeof(nr_members));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900800 if (ret < 0)
801 return ret;
802 }
803 }
804 return 0;
805}
806
807/*
Kan Liangf4a07422018-11-21 08:49:39 -0800808 * Return the CPU id as a raw string.
809 *
810 * Each architecture should provide a more precise id string that
811 * can be use to match the architecture's "mapfile".
812 */
813char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
814{
815 return NULL;
816}
817
818/* Return zero when the cpuid from the mapfile.csv matches the
819 * cpuid string generated on this platform.
820 * Otherwise return non-zero.
821 */
822int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
823{
824 regex_t re;
825 regmatch_t pmatch[1];
826 int match;
827
828 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
829 /* Warn unable to generate match particular string. */
830 pr_info("Invalid regular expression %s\n", mapcpuid);
831 return 1;
832 }
833
834 match = !regexec(&re, cpuid, 1, pmatch, 0);
835 regfree(&re);
836 if (match) {
837 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
838
839 /* Verify the entire string matched. */
840 if (match_len == strlen(cpuid))
841 return 0;
842 }
843 return 1;
844}
845
846/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200847 * default get_cpuid(): nothing gets recorded
Jiada Wang7a759cd2017-04-09 20:02:37 -0700848 * actual implementation must be in arch/$(SRCARCH)/util/header.c
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200849 */
Rui Teng11d8f872016-07-28 10:05:57 +0800850int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200851{
852 return -1;
853}
854
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700855static int write_cpuid(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200856 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200857{
858 char buffer[64];
859 int ret;
860
861 ret = get_cpuid(buffer, sizeof(buffer));
Jiri Olsaa9aeb872019-02-13 13:32:43 +0100862 if (ret)
863 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200864
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700865 return do_write_string(ff, buffer);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200866}
867
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700868static int write_branch_stack(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +0200869 struct evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100870{
871 return 0;
872}
873
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700874static int write_auxtrace(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200875 struct evlist *evlist __maybe_unused)
Adrian Hunter4025ea42015-04-09 18:53:41 +0300876{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300877 struct perf_session *session;
878 int err;
879
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700880 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
881 return -1;
882
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700883 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300884
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700885 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300886 if (err < 0)
887 pr_err("Failed to write auxtrace index\n");
888 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300889}
890
Alexey Budankovcf790512018-10-09 17:36:24 +0300891static int write_clockid(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200892 struct evlist *evlist __maybe_unused)
Alexey Budankovcf790512018-10-09 17:36:24 +0300893{
894 return do_write(ff, &ff->ph->env.clockid_res_ns,
895 sizeof(ff->ph->env.clockid_res_ns));
896}
897
Jiri Olsa258031c2019-03-08 14:47:39 +0100898static int write_dir_format(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200899 struct evlist *evlist __maybe_unused)
Jiri Olsa258031c2019-03-08 14:47:39 +0100900{
901 struct perf_session *session;
902 struct perf_data *data;
903
904 session = container_of(ff->ph, struct perf_session, header);
905 data = session->data;
906
907 if (WARN_ON(!perf_data__is_dir(data)))
908 return -1;
909
910 return do_write(ff, &data->dir.version, sizeof(data->dir.version));
911}
912
Song Liu606f9722019-03-11 22:30:43 -0700913#ifdef HAVE_LIBBPF_SUPPORT
914static int write_bpf_prog_info(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200915 struct evlist *evlist __maybe_unused)
Song Liu606f9722019-03-11 22:30:43 -0700916{
917 struct perf_env *env = &ff->ph->env;
918 struct rb_root *root;
919 struct rb_node *next;
920 int ret;
921
922 down_read(&env->bpf_progs.lock);
923
924 ret = do_write(ff, &env->bpf_progs.infos_cnt,
925 sizeof(env->bpf_progs.infos_cnt));
926 if (ret < 0)
927 goto out;
928
929 root = &env->bpf_progs.infos;
930 next = rb_first(root);
931 while (next) {
932 struct bpf_prog_info_node *node;
933 size_t len;
934
935 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
936 next = rb_next(&node->rb_node);
937 len = sizeof(struct bpf_prog_info_linear) +
938 node->info_linear->data_len;
939
940 /* before writing to file, translate address to offset */
941 bpf_program__bpil_addr_to_offs(node->info_linear);
942 ret = do_write(ff, node->info_linear, len);
943 /*
944 * translate back to address even when do_write() fails,
945 * so that this function never changes the data.
946 */
947 bpf_program__bpil_offs_to_addr(node->info_linear);
948 if (ret < 0)
949 goto out;
950 }
951out:
952 up_read(&env->bpf_progs.lock);
953 return ret;
954}
955#else // HAVE_LIBBPF_SUPPORT
956static int write_bpf_prog_info(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +0200957 struct evlist *evlist __maybe_unused)
Song Liu606f9722019-03-11 22:30:43 -0700958{
959 return 0;
960}
961#endif // HAVE_LIBBPF_SUPPORT
962
Song Liua70a112312019-03-11 22:30:45 -0700963static int write_bpf_btf(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200964 struct evlist *evlist __maybe_unused)
Song Liua70a112312019-03-11 22:30:45 -0700965{
966 struct perf_env *env = &ff->ph->env;
967 struct rb_root *root;
968 struct rb_node *next;
969 int ret;
970
971 down_read(&env->bpf_progs.lock);
972
973 ret = do_write(ff, &env->bpf_progs.btfs_cnt,
974 sizeof(env->bpf_progs.btfs_cnt));
975
976 if (ret < 0)
977 goto out;
978
979 root = &env->bpf_progs.btfs;
980 next = rb_first(root);
981 while (next) {
982 struct btf_node *node;
983
984 node = rb_entry(next, struct btf_node, rb_node);
985 next = rb_next(&node->rb_node);
986 ret = do_write(ff, &node->id,
987 sizeof(u32) * 2 + node->data_size);
988 if (ret < 0)
989 goto out;
990 }
991out:
992 up_read(&env->bpf_progs.lock);
993 return ret;
994}
995
Jiri Olsa720e98b2016-02-16 16:01:43 +0100996static int cpu_cache_level__sort(const void *a, const void *b)
997{
998 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
999 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1000
1001 return cache_a->level - cache_b->level;
1002}
1003
1004static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1005{
1006 if (a->level != b->level)
1007 return false;
1008
1009 if (a->line_size != b->line_size)
1010 return false;
1011
1012 if (a->sets != b->sets)
1013 return false;
1014
1015 if (a->ways != b->ways)
1016 return false;
1017
1018 if (strcmp(a->type, b->type))
1019 return false;
1020
1021 if (strcmp(a->size, b->size))
1022 return false;
1023
1024 if (strcmp(a->map, b->map))
1025 return false;
1026
1027 return true;
1028}
1029
1030static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1031{
1032 char path[PATH_MAX], file[PATH_MAX];
1033 struct stat st;
1034 size_t len;
1035
1036 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1037 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1038
1039 if (stat(file, &st))
1040 return 1;
1041
1042 scnprintf(file, PATH_MAX, "%s/level", path);
1043 if (sysfs__read_int(file, (int *) &cache->level))
1044 return -1;
1045
1046 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1047 if (sysfs__read_int(file, (int *) &cache->line_size))
1048 return -1;
1049
1050 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1051 if (sysfs__read_int(file, (int *) &cache->sets))
1052 return -1;
1053
1054 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1055 if (sysfs__read_int(file, (int *) &cache->ways))
1056 return -1;
1057
1058 scnprintf(file, PATH_MAX, "%s/type", path);
1059 if (sysfs__read_str(file, &cache->type, &len))
1060 return -1;
1061
1062 cache->type[len] = 0;
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -03001063 cache->type = strim(cache->type);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001064
1065 scnprintf(file, PATH_MAX, "%s/size", path);
1066 if (sysfs__read_str(file, &cache->size, &len)) {
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001067 zfree(&cache->type);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001068 return -1;
1069 }
1070
1071 cache->size[len] = 0;
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -03001072 cache->size = strim(cache->size);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001073
1074 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1075 if (sysfs__read_str(file, &cache->map, &len)) {
Jiri Olsa02162342019-09-12 12:52:35 +02001076 zfree(&cache->size);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001077 zfree(&cache->type);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001078 return -1;
1079 }
1080
1081 cache->map[len] = 0;
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -03001082 cache->map = strim(cache->map);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001083 return 0;
1084}
1085
1086static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1087{
1088 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1089}
1090
1091static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1092{
1093 u32 i, cnt = 0;
1094 long ncpus;
1095 u32 nr, cpu;
1096 u16 level;
1097
1098 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1099 if (ncpus < 0)
1100 return -1;
1101
1102 nr = (u32)(ncpus & UINT_MAX);
1103
1104 for (cpu = 0; cpu < nr; cpu++) {
1105 for (level = 0; level < 10; level++) {
1106 struct cpu_cache_level c;
1107 int err;
1108
1109 err = cpu_cache_level__read(&c, cpu, level);
1110 if (err < 0)
1111 return err;
1112
1113 if (err == 1)
1114 break;
1115
1116 for (i = 0; i < cnt; i++) {
1117 if (cpu_cache_level__cmp(&c, &caches[i]))
1118 break;
1119 }
1120
1121 if (i == cnt)
1122 caches[cnt++] = c;
1123 else
1124 cpu_cache_level__free(&c);
1125
1126 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1127 goto out;
1128 }
1129 }
1130 out:
1131 *cntp = cnt;
1132 return 0;
1133}
1134
Kyle Meyerdc841872019-08-27 16:43:51 -05001135#define MAX_CACHE_LVL 4
Jiri Olsa720e98b2016-02-16 16:01:43 +01001136
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001137static int write_cache(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +02001138 struct evlist *evlist __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001139{
Kyle Meyerdc841872019-08-27 16:43:51 -05001140 u32 max_caches = cpu__max_cpu() * MAX_CACHE_LVL;
1141 struct cpu_cache_level caches[max_caches];
Jiri Olsa720e98b2016-02-16 16:01:43 +01001142 u32 cnt = 0, i, version = 1;
1143 int ret;
1144
Kyle Meyerdc841872019-08-27 16:43:51 -05001145 ret = build_caches(caches, max_caches, &cnt);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001146 if (ret)
1147 goto out;
1148
1149 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1150
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001151 ret = do_write(ff, &version, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001152 if (ret < 0)
1153 goto out;
1154
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001155 ret = do_write(ff, &cnt, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001156 if (ret < 0)
1157 goto out;
1158
1159 for (i = 0; i < cnt; i++) {
1160 struct cpu_cache_level *c = &caches[i];
1161
1162 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001163 ret = do_write(ff, &c->v, sizeof(u32)); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001164 if (ret < 0) \
1165 goto out;
1166
1167 _W(level)
1168 _W(line_size)
1169 _W(sets)
1170 _W(ways)
1171 #undef _W
1172
1173 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001174 ret = do_write_string(ff, (const char *) c->v); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001175 if (ret < 0) \
1176 goto out;
1177
1178 _W(type)
1179 _W(size)
1180 _W(map)
1181 #undef _W
1182 }
1183
1184out:
1185 for (i = 0; i < cnt; i++)
1186 cpu_cache_level__free(&caches[i]);
1187 return ret;
1188}
1189
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001190static int write_stat(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +02001191 struct evlist *evlist __maybe_unused)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001192{
1193 return 0;
1194}
1195
Jin Yao60115182017-12-08 21:13:41 +08001196static int write_sample_time(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +02001197 struct evlist *evlist)
Jin Yao60115182017-12-08 21:13:41 +08001198{
1199 int ret;
1200
1201 ret = do_write(ff, &evlist->first_sample_time,
1202 sizeof(evlist->first_sample_time));
1203 if (ret < 0)
1204 return ret;
1205
1206 return do_write(ff, &evlist->last_sample_time,
1207 sizeof(evlist->last_sample_time));
1208}
1209
Jiri Olsae2091ce2018-03-07 16:50:08 +01001210
1211static int memory_node__read(struct memory_node *n, unsigned long idx)
1212{
1213 unsigned int phys, size = 0;
1214 char path[PATH_MAX];
1215 struct dirent *ent;
1216 DIR *dir;
1217
1218#define for_each_memory(mem, dir) \
1219 while ((ent = readdir(dir))) \
1220 if (strcmp(ent->d_name, ".") && \
1221 strcmp(ent->d_name, "..") && \
1222 sscanf(ent->d_name, "memory%u", &mem) == 1)
1223
1224 scnprintf(path, PATH_MAX,
1225 "%s/devices/system/node/node%lu",
1226 sysfs__mountpoint(), idx);
1227
1228 dir = opendir(path);
1229 if (!dir) {
1230 pr_warning("failed: cant' open memory sysfs data\n");
1231 return -1;
1232 }
1233
1234 for_each_memory(phys, dir) {
1235 size = max(phys, size);
1236 }
1237
1238 size++;
1239
1240 n->set = bitmap_alloc(size);
1241 if (!n->set) {
1242 closedir(dir);
1243 return -ENOMEM;
1244 }
1245
Jiri Olsae2091ce2018-03-07 16:50:08 +01001246 n->node = idx;
1247 n->size = size;
1248
1249 rewinddir(dir);
1250
1251 for_each_memory(phys, dir) {
1252 set_bit(phys, n->set);
1253 }
1254
1255 closedir(dir);
1256 return 0;
1257}
1258
1259static int memory_node__sort(const void *a, const void *b)
1260{
1261 const struct memory_node *na = a;
1262 const struct memory_node *nb = b;
1263
1264 return na->node - nb->node;
1265}
1266
1267static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1268{
1269 char path[PATH_MAX];
1270 struct dirent *ent;
1271 DIR *dir;
1272 u64 cnt = 0;
1273 int ret = 0;
1274
1275 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1276 sysfs__mountpoint());
1277
1278 dir = opendir(path);
1279 if (!dir) {
Thomas Richter4f75f1cb2018-04-12 15:32:46 +02001280 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1281 __func__, path);
Jiri Olsae2091ce2018-03-07 16:50:08 +01001282 return -1;
1283 }
1284
1285 while (!ret && (ent = readdir(dir))) {
1286 unsigned int idx;
1287 int r;
1288
1289 if (!strcmp(ent->d_name, ".") ||
1290 !strcmp(ent->d_name, ".."))
1291 continue;
1292
1293 r = sscanf(ent->d_name, "node%u", &idx);
1294 if (r != 1)
1295 continue;
1296
1297 if (WARN_ONCE(cnt >= size,
1298 "failed to write MEM_TOPOLOGY, way too many nodes\n"))
1299 return -1;
1300
1301 ret = memory_node__read(&nodes[cnt++], idx);
1302 }
1303
1304 *cntp = cnt;
1305 closedir(dir);
1306
1307 if (!ret)
1308 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1309
1310 return ret;
1311}
1312
1313#define MAX_MEMORY_NODES 2000
1314
1315/*
1316 * The MEM_TOPOLOGY holds physical memory map for every
1317 * node in system. The format of data is as follows:
1318 *
1319 * 0 - version | for future changes
1320 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1321 * 16 - count | number of nodes
1322 *
1323 * For each node we store map of physical indexes for
1324 * each node:
1325 *
1326 * 32 - node id | node index
1327 * 40 - size | size of bitmap
1328 * 48 - bitmap | bitmap of memory indexes that belongs to node
1329 */
1330static int write_mem_topology(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +02001331 struct evlist *evlist __maybe_unused)
Jiri Olsae2091ce2018-03-07 16:50:08 +01001332{
1333 static struct memory_node nodes[MAX_MEMORY_NODES];
1334 u64 bsize, version = 1, i, nr;
1335 int ret;
1336
1337 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1338 (unsigned long long *) &bsize);
1339 if (ret)
1340 return ret;
1341
1342 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1343 if (ret)
1344 return ret;
1345
1346 ret = do_write(ff, &version, sizeof(version));
1347 if (ret < 0)
1348 goto out;
1349
1350 ret = do_write(ff, &bsize, sizeof(bsize));
1351 if (ret < 0)
1352 goto out;
1353
1354 ret = do_write(ff, &nr, sizeof(nr));
1355 if (ret < 0)
1356 goto out;
1357
1358 for (i = 0; i < nr; i++) {
1359 struct memory_node *n = &nodes[i];
1360
1361 #define _W(v) \
1362 ret = do_write(ff, &n->v, sizeof(n->v)); \
1363 if (ret < 0) \
1364 goto out;
1365
1366 _W(node)
1367 _W(size)
1368
1369 #undef _W
1370
1371 ret = do_write_bitmap(ff, n->set, n->size);
1372 if (ret < 0)
1373 goto out;
1374 }
1375
1376out:
1377 return ret;
1378}
1379
Alexey Budankov42e1fd82019-03-18 20:41:33 +03001380static int write_compressed(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +02001381 struct evlist *evlist __maybe_unused)
Alexey Budankov42e1fd82019-03-18 20:41:33 +03001382{
1383 int ret;
1384
1385 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver));
1386 if (ret)
1387 return ret;
1388
1389 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type));
1390 if (ret)
1391 return ret;
1392
1393 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level));
1394 if (ret)
1395 return ret;
1396
1397 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio));
1398 if (ret)
1399 return ret;
1400
1401 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len));
1402}
1403
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001404static void print_hostname(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001405{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001406 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001407}
1408
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001409static void print_osrelease(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001410{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001411 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001412}
1413
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001414static void print_arch(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001415{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001416 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001417}
1418
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001419static void print_cpudesc(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001420{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001421 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001422}
1423
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001424static void print_nrcpus(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001425{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001426 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1427 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001428}
1429
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001430static void print_version(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001431{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001432 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001433}
1434
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001435static void print_cmdline(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001436{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001437 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001438
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001439 nr = ff->ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001440
1441 fprintf(fp, "# cmdline : ");
1442
Alexey Budankovf92da712018-06-04 09:50:56 +03001443 for (i = 0; i < nr; i++) {
1444 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1445 if (!argv_i) {
1446 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1447 } else {
1448 char *mem = argv_i;
1449 do {
1450 char *quote = strchr(argv_i, '\'');
1451 if (!quote)
1452 break;
1453 *quote++ = '\0';
1454 fprintf(fp, "%s\\\'", argv_i);
1455 argv_i = quote;
1456 } while (1);
1457 fprintf(fp, "%s ", argv_i);
1458 free(mem);
1459 }
1460 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001461 fputc('\n', fp);
1462}
1463
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001464static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001465{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001466 struct perf_header *ph = ff->ph;
1467 int cpu_nr = ph->env.nr_cpus_avail;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001468 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001469 char *str;
1470
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001471 nr = ph->env.nr_sibling_cores;
1472 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001473
1474 for (i = 0; i < nr; i++) {
Kan Liange05a8992019-06-04 15:50:43 -07001475 fprintf(fp, "# sibling sockets : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001476 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001477 }
1478
Kan Liangacae8b32019-06-04 15:50:41 -07001479 if (ph->env.nr_sibling_dies) {
1480 nr = ph->env.nr_sibling_dies;
1481 str = ph->env.sibling_dies;
1482
1483 for (i = 0; i < nr; i++) {
1484 fprintf(fp, "# sibling dies : %s\n", str);
1485 str += strlen(str) + 1;
1486 }
1487 }
1488
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001489 nr = ph->env.nr_sibling_threads;
1490 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001491
1492 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001493 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001494 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001495 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001496
Kan Liangacae8b32019-06-04 15:50:41 -07001497 if (ph->env.nr_sibling_dies) {
1498 if (ph->env.cpu != NULL) {
1499 for (i = 0; i < cpu_nr; i++)
1500 fprintf(fp, "# CPU %d: Core ID %d, "
1501 "Die ID %d, Socket ID %d\n",
1502 i, ph->env.cpu[i].core_id,
1503 ph->env.cpu[i].die_id,
1504 ph->env.cpu[i].socket_id);
1505 } else
1506 fprintf(fp, "# Core ID, Die ID and Socket ID "
1507 "information is not available\n");
1508 } else {
1509 if (ph->env.cpu != NULL) {
1510 for (i = 0; i < cpu_nr; i++)
1511 fprintf(fp, "# CPU %d: Core ID %d, "
1512 "Socket ID %d\n",
1513 i, ph->env.cpu[i].core_id,
1514 ph->env.cpu[i].socket_id);
1515 } else
1516 fprintf(fp, "# Core ID and Socket ID "
1517 "information is not available\n");
1518 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001519}
1520
Alexey Budankovcf790512018-10-09 17:36:24 +03001521static void print_clockid(struct feat_fd *ff, FILE *fp)
1522{
1523 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1524 ff->ph->env.clockid_res_ns * 1000);
1525}
1526
Jiri Olsa258031c2019-03-08 14:47:39 +01001527static void print_dir_format(struct feat_fd *ff, FILE *fp)
1528{
1529 struct perf_session *session;
1530 struct perf_data *data;
1531
1532 session = container_of(ff->ph, struct perf_session, header);
1533 data = session->data;
1534
1535 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version);
1536}
1537
Song Liu606f9722019-03-11 22:30:43 -07001538static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp)
1539{
1540 struct perf_env *env = &ff->ph->env;
1541 struct rb_root *root;
1542 struct rb_node *next;
1543
1544 down_read(&env->bpf_progs.lock);
1545
1546 root = &env->bpf_progs.infos;
1547 next = rb_first(root);
1548
1549 while (next) {
1550 struct bpf_prog_info_node *node;
1551
1552 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1553 next = rb_next(&node->rb_node);
Song Liuf8dfeae2019-03-19 09:54:54 -07001554
1555 bpf_event__print_bpf_prog_info(&node->info_linear->info,
1556 env, fp);
Song Liu606f9722019-03-11 22:30:43 -07001557 }
1558
1559 up_read(&env->bpf_progs.lock);
1560}
1561
Song Liua70a112312019-03-11 22:30:45 -07001562static void print_bpf_btf(struct feat_fd *ff, FILE *fp)
1563{
1564 struct perf_env *env = &ff->ph->env;
1565 struct rb_root *root;
1566 struct rb_node *next;
1567
1568 down_read(&env->bpf_progs.lock);
1569
1570 root = &env->bpf_progs.btfs;
1571 next = rb_first(root);
1572
1573 while (next) {
1574 struct btf_node *node;
1575
1576 node = rb_entry(next, struct btf_node, rb_node);
1577 next = rb_next(&node->rb_node);
1578 fprintf(fp, "# btf info of id %u\n", node->id);
1579 }
1580
1581 up_read(&env->bpf_progs.lock);
1582}
1583
Jiri Olsa32dcd022019-07-21 13:23:51 +02001584static void free_event_desc(struct evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001585{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001586 struct evsel *evsel;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001587
1588 if (!events)
1589 return;
1590
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001591 for (evsel = events; evsel->core.attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001592 zfree(&evsel->name);
1593 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001594 }
1595
1596 free(events);
1597}
1598
Jiri Olsa32dcd022019-07-21 13:23:51 +02001599static struct evsel *read_event_desc(struct feat_fd *ff)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001600{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001601 struct evsel *evsel, *events = NULL;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001602 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001603 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001604 u32 nre, sz, nr, i, j;
Stephane Eranian62db9062012-02-09 23:21:07 +01001605 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001606
1607 /* number of events */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001608 if (do_read_u32(ff, &nre))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001609 goto error;
1610
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001611 if (do_read_u32(ff, &sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001612 goto error;
1613
Stephane Eranian62db9062012-02-09 23:21:07 +01001614 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001615 buf = malloc(sz);
1616 if (!buf)
1617 goto error;
1618
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001619 /* the last event terminates with evsel->core.attr.size == 0: */
Robert Richter4e1b9c62012-08-16 21:10:22 +02001620 events = calloc(nre + 1, sizeof(*events));
1621 if (!events)
1622 goto error;
1623
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001624 msz = sizeof(evsel->core.attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001625 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001626 msz = sz;
1627
Robert Richter4e1b9c62012-08-16 21:10:22 +02001628 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1629 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001630
Stephane Eranian62db9062012-02-09 23:21:07 +01001631 /*
1632 * must read entire on-file attr struct to
1633 * sync up with layout.
1634 */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001635 if (__do_read(ff, buf, sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001636 goto error;
1637
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001638 if (ff->ph->needs_swap)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001639 perf_event__attr_swap(buf);
1640
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001641 memcpy(&evsel->core.attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001642
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001643 if (do_read_u32(ff, &nr))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001644 goto error;
1645
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001646 if (ff->ph->needs_swap)
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001647 evsel->needs_swap = true;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001648
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001649 evsel->name = do_read_string(ff);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001650 if (!evsel->name)
1651 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001652
1653 if (!nr)
1654 continue;
1655
1656 id = calloc(nr, sizeof(*id));
1657 if (!id)
1658 goto error;
1659 evsel->ids = nr;
1660 evsel->id = id;
1661
1662 for (j = 0 ; j < nr; j++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001663 if (do_read_u64(ff, id))
Robert Richter4e1b9c62012-08-16 21:10:22 +02001664 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001665 id++;
1666 }
1667 }
1668out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001669 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001670 return events;
1671error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001672 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001673 events = NULL;
1674 goto out;
1675}
1676
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001677static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001678 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001679{
1680 return fprintf(fp, ", %s = %s", name, val);
1681}
1682
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001683static void print_event_desc(struct feat_fd *ff, FILE *fp)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001684{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001685 struct evsel *evsel, *events;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001686 u32 j;
1687 u64 *id;
1688
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001689 if (ff->events)
1690 events = ff->events;
1691 else
1692 events = read_event_desc(ff);
1693
Robert Richter4e1b9c62012-08-16 21:10:22 +02001694 if (!events) {
1695 fprintf(fp, "# event desc: not available or unable to read\n");
1696 return;
1697 }
1698
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001699 for (evsel = events; evsel->core.attr.size; evsel++) {
Robert Richter4e1b9c62012-08-16 21:10:22 +02001700 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001701
Robert Richter4e1b9c62012-08-16 21:10:22 +02001702 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001703 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001704 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1705 if (j)
1706 fputc(',', fp);
1707 fprintf(fp, " %"PRIu64, *id);
1708 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001709 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001710 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001711
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001712 perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001713
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001714 fputc('\n', fp);
1715 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001716
1717 free_event_desc(events);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001718 ff->events = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001719}
1720
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001721static void print_total_mem(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001722{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001723 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001724}
1725
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001726static void print_numa_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001727{
Jiri Olsac60da222016-07-04 14:16:20 +02001728 int i;
1729 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001730
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001731 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1732 n = &ff->ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001733
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001734 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1735 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001736 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001737
Jiri Olsac60da222016-07-04 14:16:20 +02001738 fprintf(fp, "# node%u cpu list : ", n->node);
1739 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001740 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001741}
1742
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001743static void print_cpuid(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001744{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001745 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001746}
1747
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001748static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001749{
1750 fprintf(fp, "# contains samples with branch stack\n");
1751}
1752
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001753static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
Adrian Hunter4025ea42015-04-09 18:53:41 +03001754{
1755 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1756}
1757
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001758static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001759{
1760 fprintf(fp, "# contains stat data\n");
1761}
1762
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001763static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001764{
1765 int i;
1766
1767 fprintf(fp, "# CPU cache info:\n");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001768 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
Jiri Olsa720e98b2016-02-16 16:01:43 +01001769 fprintf(fp, "# ");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001770 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001771 }
1772}
1773
Alexey Budankov42e1fd82019-03-18 20:41:33 +03001774static void print_compressed(struct feat_fd *ff, FILE *fp)
1775{
1776 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n",
1777 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown",
1778 ff->ph->env.comp_level, ff->ph->env.comp_ratio);
1779}
1780
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001781static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001782{
1783 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001784 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001785 u32 pmu_num;
1786 u32 type;
1787
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001788 pmu_num = ff->ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001789 if (!pmu_num) {
1790 fprintf(fp, "# pmu mappings: not available\n");
1791 return;
1792 }
1793
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001794 str = ff->ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001795
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001796 while (pmu_num) {
1797 type = strtoul(str, &tmp, 0);
1798 if (*tmp != ':')
1799 goto error;
1800
1801 str = tmp + 1;
1802 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1803
Robert Richter50a96672012-08-16 21:10:24 +02001804 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001805 str += strlen(str) + 1;
1806 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001807 }
1808
1809 fprintf(fp, "\n");
1810
1811 if (!pmu_num)
1812 return;
1813error:
1814 fprintf(fp, "# pmu mappings: unable to read\n");
1815}
1816
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001817static void print_group_desc(struct feat_fd *ff, FILE *fp)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001818{
1819 struct perf_session *session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001820 struct evsel *evsel;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001821 u32 nr = 0;
1822
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001823 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001824
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001825 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001826 if (perf_evsel__is_group_leader(evsel) &&
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001827 evsel->core.nr_members > 1) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001828 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1829 perf_evsel__name(evsel));
1830
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001831 nr = evsel->core.nr_members - 1;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001832 } else if (nr) {
1833 fprintf(fp, ",%s", perf_evsel__name(evsel));
1834
1835 if (--nr == 0)
1836 fprintf(fp, "}\n");
1837 }
1838 }
1839}
1840
Jin Yao60115182017-12-08 21:13:41 +08001841static void print_sample_time(struct feat_fd *ff, FILE *fp)
1842{
1843 struct perf_session *session;
1844 char time_buf[32];
1845 double d;
1846
1847 session = container_of(ff->ph, struct perf_session, header);
1848
1849 timestamp__scnprintf_usec(session->evlist->first_sample_time,
1850 time_buf, sizeof(time_buf));
1851 fprintf(fp, "# time of first sample : %s\n", time_buf);
1852
1853 timestamp__scnprintf_usec(session->evlist->last_sample_time,
1854 time_buf, sizeof(time_buf));
1855 fprintf(fp, "# time of last sample : %s\n", time_buf);
1856
1857 d = (double)(session->evlist->last_sample_time -
1858 session->evlist->first_sample_time) / NSEC_PER_MSEC;
1859
1860 fprintf(fp, "# sample duration : %10.3f ms\n", d);
1861}
1862
Jiri Olsae2091ce2018-03-07 16:50:08 +01001863static void memory_node__fprintf(struct memory_node *n,
1864 unsigned long long bsize, FILE *fp)
1865{
1866 char buf_map[100], buf_size[50];
1867 unsigned long long size;
1868
1869 size = bsize * bitmap_weight(n->set, n->size);
1870 unit_number__scnprintf(buf_size, 50, size);
1871
1872 bitmap_scnprintf(n->set, n->size, buf_map, 100);
1873 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
1874}
1875
1876static void print_mem_topology(struct feat_fd *ff, FILE *fp)
1877{
1878 struct memory_node *nodes;
1879 int i, nr;
1880
1881 nodes = ff->ph->env.memory_nodes;
1882 nr = ff->ph->env.nr_memory_nodes;
1883
1884 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
1885 nr, ff->ph->env.memory_bsize);
1886
1887 for (i = 0; i < nr; i++) {
1888 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
1889 }
1890}
1891
Jiri Olsa72932372019-08-28 15:57:16 +02001892static int __event_process_build_id(struct perf_record_header_build_id *bev,
Robert Richter08d95bd2012-02-10 15:41:55 +01001893 char *filename,
1894 struct perf_session *session)
1895{
1896 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001897 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001898 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001899 struct dso *dso;
1900 enum dso_kernel_type dso_type;
1901
1902 machine = perf_session__findnew_machine(session, bev->pid);
1903 if (!machine)
1904 goto out;
1905
Wang Nan1f121b02015-06-03 08:52:21 +00001906 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001907
Wang Nan1f121b02015-06-03 08:52:21 +00001908 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001909 case PERF_RECORD_MISC_KERNEL:
1910 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001911 break;
1912 case PERF_RECORD_MISC_GUEST_KERNEL:
1913 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001914 break;
1915 case PERF_RECORD_MISC_USER:
1916 case PERF_RECORD_MISC_GUEST_USER:
1917 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001918 break;
1919 default:
1920 goto out;
1921 }
1922
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001923 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001924 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001925 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001926
1927 dso__set_build_id(dso, &bev->build_id);
1928
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001929 if (dso_type != DSO_TYPE_USER) {
1930 struct kmod_path m = { .name = NULL, };
1931
1932 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09001933 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001934 else
1935 dso->kernel = dso_type;
1936
1937 free(m.name);
1938 }
Robert Richter08d95bd2012-02-10 15:41:55 +01001939
1940 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1941 sbuild_id);
1942 pr_debug("build id event received for %s: %s\n",
1943 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001944 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001945 }
1946
1947 err = 0;
1948out:
1949 return err;
1950}
1951
1952static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1953 int input, u64 offset, u64 size)
1954{
1955 struct perf_session *session = container_of(header, struct perf_session, header);
1956 struct {
1957 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001958 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001959 char filename[0];
1960 } old_bev;
Jiri Olsa72932372019-08-28 15:57:16 +02001961 struct perf_record_header_build_id bev;
Robert Richter08d95bd2012-02-10 15:41:55 +01001962 char filename[PATH_MAX];
1963 u64 limit = offset + size;
1964
1965 while (offset < limit) {
1966 ssize_t len;
1967
Namhyung Kim5323f602012-12-17 15:38:54 +09001968 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001969 return -1;
1970
1971 if (header->needs_swap)
1972 perf_event_header__bswap(&old_bev.header);
1973
1974 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001975 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001976 return -1;
1977
1978 bev.header = old_bev.header;
1979
1980 /*
1981 * As the pid is the missing value, we need to fill
1982 * it properly. The header.misc value give us nice hint.
1983 */
1984 bev.pid = HOST_KERNEL_ID;
1985 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1986 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1987 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1988
1989 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1990 __event_process_build_id(&bev, filename, session);
1991
1992 offset += bev.header.size;
1993 }
1994
1995 return 0;
1996}
1997
1998static int perf_header__read_build_ids(struct perf_header *header,
1999 int input, u64 offset, u64 size)
2000{
2001 struct perf_session *session = container_of(header, struct perf_session, header);
Jiri Olsa72932372019-08-28 15:57:16 +02002002 struct perf_record_header_build_id bev;
Robert Richter08d95bd2012-02-10 15:41:55 +01002003 char filename[PATH_MAX];
2004 u64 limit = offset + size, orig_offset = offset;
2005 int err = -1;
2006
2007 while (offset < limit) {
2008 ssize_t len;
2009
Namhyung Kim5323f602012-12-17 15:38:54 +09002010 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01002011 goto out;
2012
2013 if (header->needs_swap)
2014 perf_event_header__bswap(&bev.header);
2015
2016 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09002017 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01002018 goto out;
2019 /*
2020 * The a1645ce1 changeset:
2021 *
2022 * "perf: 'perf kvm' tool for monitoring guest performance from host"
2023 *
Jiri Olsa72932372019-08-28 15:57:16 +02002024 * Added a field to struct perf_record_header_build_id that broke the file
Robert Richter08d95bd2012-02-10 15:41:55 +01002025 * format.
2026 *
2027 * Since the kernel build-id is the first entry, process the
2028 * table using the old format if the well known
2029 * '[kernel.kallsyms]' string for the kernel build-id has the
2030 * first 4 characters chopped off (where the pid_t sits).
2031 */
2032 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
2033 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
2034 return -1;
2035 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
2036 }
2037
2038 __event_process_build_id(&bev, filename, session);
2039
2040 offset += bev.header.size;
2041 }
2042 err = 0;
2043out:
2044 return err;
2045}
2046
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07002047/* Macro for features that simply need to read and store a string. */
2048#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002049static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07002050{\
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002051 ff->ph->env.__feat_env = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002052 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07002053}
2054
2055FEAT_PROCESS_STR_FUN(hostname, hostname);
2056FEAT_PROCESS_STR_FUN(osrelease, os_release);
2057FEAT_PROCESS_STR_FUN(version, version);
2058FEAT_PROCESS_STR_FUN(arch, arch);
2059FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
2060FEAT_PROCESS_STR_FUN(cpuid, cpuid);
2061
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002062static int process_tracing_data(struct feat_fd *ff, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01002063{
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002064 ssize_t ret = trace_report(ff->fd, data, false);
2065
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002066 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01002067}
2068
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002069static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01002070{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002071 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
Robert Richterf1c67db2012-02-10 15:41:56 +01002072 pr_debug("Failed to read buildids, continuing...\n");
2073 return 0;
2074}
2075
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002076static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002077{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002078 int ret;
2079 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002080
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002081 ret = do_read_u32(ff, &nr_cpus_avail);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002082 if (ret)
2083 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002084
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002085 ret = do_read_u32(ff, &nr_cpus_online);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002086 if (ret)
2087 return ret;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002088 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2089 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002090 return 0;
2091}
2092
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002093static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002094{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002095 u64 total_mem;
2096 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002097
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002098 ret = do_read_u64(ff, &total_mem);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002099 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002100 return -1;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002101 ff->ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002102 return 0;
2103}
2104
Jiri Olsa32dcd022019-07-21 13:23:51 +02002105static struct evsel *
Jiri Olsa63503db2019-07-21 13:23:52 +02002106perf_evlist__find_by_index(struct evlist *evlist, int idx)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002107{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002108 struct evsel *evsel;
Robert Richter7c2f7af2012-08-16 21:10:23 +02002109
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002110 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02002111 if (evsel->idx == idx)
2112 return evsel;
2113 }
2114
2115 return NULL;
2116}
2117
2118static void
Jiri Olsa63503db2019-07-21 13:23:52 +02002119perf_evlist__set_event_name(struct evlist *evlist,
Jiri Olsa32dcd022019-07-21 13:23:51 +02002120 struct evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002121{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002122 struct evsel *evsel;
Robert Richter7c2f7af2012-08-16 21:10:23 +02002123
2124 if (!event->name)
2125 return;
2126
2127 evsel = perf_evlist__find_by_index(evlist, event->idx);
2128 if (!evsel)
2129 return;
2130
2131 if (evsel->name)
2132 return;
2133
2134 evsel->name = strdup(event->name);
2135}
2136
2137static int
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002138process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002139{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002140 struct perf_session *session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002141 struct evsel *evsel, *events = read_event_desc(ff);
Robert Richter7c2f7af2012-08-16 21:10:23 +02002142
2143 if (!events)
2144 return 0;
2145
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002146 session = container_of(ff->ph, struct perf_session, header);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002147
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002148 if (session->data->is_pipe) {
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002149 /* Save events for reading later by print_event_desc,
2150 * since they can't be read again in pipe mode. */
2151 ff->events = events;
2152 }
2153
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002154 for (evsel = events; evsel->core.attr.size; evsel++)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002155 perf_evlist__set_event_name(session->evlist, evsel);
2156
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002157 if (!session->data->is_pipe)
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002158 free_event_desc(events);
Robert Richter7c2f7af2012-08-16 21:10:23 +02002159
2160 return 0;
2161}
2162
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002163static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002164{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002165 char *str, *cmdline = NULL, **argv = NULL;
2166 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002167
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002168 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002169 return -1;
2170
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002171 ff->ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002172
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002173 cmdline = zalloc(ff->size + nr + 1);
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002174 if (!cmdline)
2175 return -1;
2176
2177 argv = zalloc(sizeof(char *) * (nr + 1));
2178 if (!argv)
2179 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002180
2181 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002182 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002183 if (!str)
2184 goto error;
2185
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002186 argv[i] = cmdline + len;
2187 memcpy(argv[i], str, strlen(str) + 1);
2188 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002189 free(str);
2190 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002191 ff->ph->env.cmdline = cmdline;
2192 ff->ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002193 return 0;
2194
2195error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002196 free(argv);
2197 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002198 return -1;
2199}
2200
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002201static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002202{
Namhyung Kima1ae5652012-09-24 17:14:59 +09002203 u32 nr, i;
2204 char *str;
2205 struct strbuf sb;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002206 int cpu_nr = ff->ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04002207 u64 size = 0;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002208 struct perf_header *ph = ff->ph;
Thomas Richter01766222018-06-11 09:31:52 +02002209 bool do_core_id_test = true;
Kan Liang2bb00d22015-09-01 09:58:12 -04002210
2211 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2212 if (!ph->env.cpu)
2213 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002214
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002215 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002216 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002217
Namhyung Kima1ae5652012-09-24 17:14:59 +09002218 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002219 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002220 if (strbuf_init(&sb, 128) < 0)
2221 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002222
2223 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002224 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002225 if (!str)
2226 goto error;
2227
2228 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002229 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2230 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04002231 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002232 free(str);
2233 }
2234 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2235
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002236 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002237 return -1;
2238
Namhyung Kima1ae5652012-09-24 17:14:59 +09002239 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002240 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002241
2242 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002243 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002244 if (!str)
2245 goto error;
2246
2247 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002248 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2249 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04002250 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002251 free(str);
2252 }
2253 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04002254
2255 /*
2256 * The header may be from old perf,
2257 * which doesn't include core id and socket id information.
2258 */
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002259 if (ff->size <= size) {
Kan Liang2bb00d22015-09-01 09:58:12 -04002260 zfree(&ph->env.cpu);
2261 return 0;
2262 }
2263
Thomas Richter01766222018-06-11 09:31:52 +02002264 /* On s390 the socket_id number is not related to the numbers of cpus.
2265 * The socket_id number might be higher than the numbers of cpus.
2266 * This depends on the configuration.
Tan Xiaojun0a4d8fb2019-08-02 11:48:57 +08002267 * AArch64 is the same.
Thomas Richter01766222018-06-11 09:31:52 +02002268 */
Tan Xiaojun0a4d8fb2019-08-02 11:48:57 +08002269 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
2270 || !strncmp(ph->env.arch, "aarch64", 7)))
Thomas Richter01766222018-06-11 09:31:52 +02002271 do_core_id_test = false;
2272
Kan Liang2bb00d22015-09-01 09:58:12 -04002273 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002274 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002275 goto free_cpu;
2276
Kan Liang2bb00d22015-09-01 09:58:12 -04002277 ph->env.cpu[i].core_id = nr;
Kan Liangacae8b32019-06-04 15:50:41 -07002278 size += sizeof(u32);
Kan Liang2bb00d22015-09-01 09:58:12 -04002279
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002280 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002281 goto free_cpu;
2282
Thomas Richter01766222018-06-11 09:31:52 +02002283 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04002284 pr_debug("socket_id number is too big."
2285 "You may need to upgrade the perf tool.\n");
2286 goto free_cpu;
2287 }
2288
2289 ph->env.cpu[i].socket_id = nr;
Kan Liangacae8b32019-06-04 15:50:41 -07002290 size += sizeof(u32);
2291 }
2292
2293 /*
2294 * The header may be from old perf,
2295 * which doesn't include die information.
2296 */
2297 if (ff->size <= size)
2298 return 0;
2299
2300 if (do_read_u32(ff, &nr))
2301 return -1;
2302
2303 ph->env.nr_sibling_dies = nr;
2304 size += sizeof(u32);
2305
2306 for (i = 0; i < nr; i++) {
2307 str = do_read_string(ff);
2308 if (!str)
2309 goto error;
2310
2311 /* include a NULL character at the end */
2312 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2313 goto error;
2314 size += string_size(str);
2315 free(str);
2316 }
2317 ph->env.sibling_dies = strbuf_detach(&sb, NULL);
2318
2319 for (i = 0; i < (u32)cpu_nr; i++) {
2320 if (do_read_u32(ff, &nr))
2321 goto free_cpu;
2322
2323 ph->env.cpu[i].die_id = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002324 }
2325
Namhyung Kima1ae5652012-09-24 17:14:59 +09002326 return 0;
2327
2328error:
2329 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04002330free_cpu:
2331 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002332 return -1;
2333}
2334
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002335static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002336{
Jiri Olsac60da222016-07-04 14:16:20 +02002337 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02002338 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002339 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002340
2341 /* nr nodes */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002342 if (do_read_u32(ff, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002343 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002344
Jiri Olsac60da222016-07-04 14:16:20 +02002345 nodes = zalloc(sizeof(*nodes) * nr);
2346 if (!nodes)
2347 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002348
2349 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02002350 n = &nodes[i];
2351
Namhyung Kima1ae5652012-09-24 17:14:59 +09002352 /* node number */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002353 if (do_read_u32(ff, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002354 goto error;
2355
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002356 if (do_read_u64(ff, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002357 goto error;
2358
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002359 if (do_read_u64(ff, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002360 goto error;
2361
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002362 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002363 if (!str)
2364 goto error;
2365
Jiri Olsa9c3516d2019-07-21 13:24:30 +02002366 n->map = perf_cpu_map__new(str);
Jiri Olsac60da222016-07-04 14:16:20 +02002367 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002368 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02002369
Namhyung Kima1ae5652012-09-24 17:14:59 +09002370 free(str);
2371 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002372 ff->ph->env.nr_numa_nodes = nr;
2373 ff->ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002374 return 0;
2375
2376error:
Jiri Olsac60da222016-07-04 14:16:20 +02002377 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002378 return -1;
2379}
2380
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002381static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002382{
Namhyung Kima1ae5652012-09-24 17:14:59 +09002383 char *name;
2384 u32 pmu_num;
2385 u32 type;
2386 struct strbuf sb;
2387
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002388 if (do_read_u32(ff, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002389 return -1;
2390
Namhyung Kima1ae5652012-09-24 17:14:59 +09002391 if (!pmu_num) {
2392 pr_debug("pmu mappings not available\n");
2393 return 0;
2394 }
2395
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002396 ff->ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002397 if (strbuf_init(&sb, 128) < 0)
2398 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002399
2400 while (pmu_num) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002401 if (do_read_u32(ff, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002402 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002403
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002404 name = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002405 if (!name)
2406 goto error;
2407
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002408 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2409 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002410 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002411 if (strbuf_add(&sb, "", 1) < 0)
2412 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002413
Kan Liange0838e02015-09-10 11:03:05 -03002414 if (!strcmp(name, "msr"))
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002415 ff->ph->env.msr_pmu_type = type;
Kan Liange0838e02015-09-10 11:03:05 -03002416
Namhyung Kima1ae5652012-09-24 17:14:59 +09002417 free(name);
2418 pmu_num--;
2419 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002420 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002421 return 0;
2422
2423error:
2424 strbuf_release(&sb);
2425 return -1;
2426}
2427
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002428static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima8bb5592013-01-22 18:09:31 +09002429{
2430 size_t ret = -1;
2431 u32 i, nr, nr_groups;
2432 struct perf_session *session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002433 struct evsel *evsel, *leader = NULL;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002434 struct group_desc {
2435 char *name;
2436 u32 leader_idx;
2437 u32 nr_members;
2438 } *desc;
2439
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002440 if (do_read_u32(ff, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002441 return -1;
2442
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002443 ff->ph->env.nr_groups = nr_groups;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002444 if (!nr_groups) {
2445 pr_debug("group desc not available\n");
2446 return 0;
2447 }
2448
2449 desc = calloc(nr_groups, sizeof(*desc));
2450 if (!desc)
2451 return -1;
2452
2453 for (i = 0; i < nr_groups; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002454 desc[i].name = do_read_string(ff);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002455 if (!desc[i].name)
2456 goto out_free;
2457
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002458 if (do_read_u32(ff, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002459 goto out_free;
2460
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002461 if (do_read_u32(ff, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002462 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002463 }
2464
2465 /*
2466 * Rebuild group relationship based on the group_desc
2467 */
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002468 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002469 session->evlist->nr_groups = nr_groups;
2470
2471 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002472 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002473 if (evsel->idx == (int) desc[i].leader_idx) {
2474 evsel->leader = evsel;
2475 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002476 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002477 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002478 desc[i].name = NULL;
2479 }
Jiri Olsa5643b1a2019-07-21 13:24:46 +02002480 evsel->core.nr_members = desc[i].nr_members;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002481
2482 if (i >= nr_groups || nr > 0) {
2483 pr_debug("invalid group desc\n");
2484 goto out_free;
2485 }
2486
2487 leader = evsel;
Jiri Olsa5643b1a2019-07-21 13:24:46 +02002488 nr = evsel->core.nr_members - 1;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002489 i++;
2490 } else if (nr) {
2491 /* This is a group member */
2492 evsel->leader = leader;
2493
2494 nr--;
2495 }
2496 }
2497
2498 if (i != nr_groups || nr != 0) {
2499 pr_debug("invalid group desc\n");
2500 goto out_free;
2501 }
2502
2503 ret = 0;
2504out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002505 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002506 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002507 free(desc);
2508
2509 return ret;
2510}
2511
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002512static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
Adrian Hunter99fa2982015-04-30 17:37:25 +03002513{
2514 struct perf_session *session;
2515 int err;
2516
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002517 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002518
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002519 err = auxtrace_index__process(ff->fd, ff->size, session,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002520 ff->ph->needs_swap);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002521 if (err < 0)
2522 pr_err("Failed to process auxtrace index\n");
2523 return err;
2524}
2525
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002526static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01002527{
2528 struct cpu_cache_level *caches;
2529 u32 cnt, i, version;
2530
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002531 if (do_read_u32(ff, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002532 return -1;
2533
Jiri Olsa720e98b2016-02-16 16:01:43 +01002534 if (version != 1)
2535 return -1;
2536
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002537 if (do_read_u32(ff, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002538 return -1;
2539
Jiri Olsa720e98b2016-02-16 16:01:43 +01002540 caches = zalloc(sizeof(*caches) * cnt);
2541 if (!caches)
2542 return -1;
2543
2544 for (i = 0; i < cnt; i++) {
2545 struct cpu_cache_level c;
2546
2547 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002548 if (do_read_u32(ff, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002549 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002550
2551 _R(level)
2552 _R(line_size)
2553 _R(sets)
2554 _R(ways)
2555 #undef _R
2556
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002557 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002558 c.v = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002559 if (!c.v) \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002560 goto out_free_caches;
2561
2562 _R(type)
2563 _R(size)
2564 _R(map)
2565 #undef _R
2566
2567 caches[i] = c;
2568 }
2569
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002570 ff->ph->env.caches = caches;
2571 ff->ph->env.caches_cnt = cnt;
Jiri Olsa720e98b2016-02-16 16:01:43 +01002572 return 0;
2573out_free_caches:
2574 free(caches);
2575 return -1;
2576}
2577
Jin Yao60115182017-12-08 21:13:41 +08002578static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2579{
2580 struct perf_session *session;
2581 u64 first_sample_time, last_sample_time;
2582 int ret;
2583
2584 session = container_of(ff->ph, struct perf_session, header);
2585
2586 ret = do_read_u64(ff, &first_sample_time);
2587 if (ret)
2588 return -1;
2589
2590 ret = do_read_u64(ff, &last_sample_time);
2591 if (ret)
2592 return -1;
2593
2594 session->evlist->first_sample_time = first_sample_time;
2595 session->evlist->last_sample_time = last_sample_time;
2596 return 0;
2597}
2598
Jiri Olsae2091ce2018-03-07 16:50:08 +01002599static int process_mem_topology(struct feat_fd *ff,
2600 void *data __maybe_unused)
2601{
2602 struct memory_node *nodes;
2603 u64 version, i, nr, bsize;
2604 int ret = -1;
2605
2606 if (do_read_u64(ff, &version))
2607 return -1;
2608
2609 if (version != 1)
2610 return -1;
2611
2612 if (do_read_u64(ff, &bsize))
2613 return -1;
2614
2615 if (do_read_u64(ff, &nr))
2616 return -1;
2617
2618 nodes = zalloc(sizeof(*nodes) * nr);
2619 if (!nodes)
2620 return -1;
2621
2622 for (i = 0; i < nr; i++) {
2623 struct memory_node n;
2624
2625 #define _R(v) \
2626 if (do_read_u64(ff, &n.v)) \
2627 goto out; \
2628
2629 _R(node)
2630 _R(size)
2631
2632 #undef _R
2633
2634 if (do_read_bitmap(ff, &n.set, &n.size))
2635 goto out;
2636
2637 nodes[i] = n;
2638 }
2639
2640 ff->ph->env.memory_bsize = bsize;
2641 ff->ph->env.memory_nodes = nodes;
2642 ff->ph->env.nr_memory_nodes = nr;
2643 ret = 0;
2644
2645out:
2646 if (ret)
2647 free(nodes);
2648 return ret;
2649}
2650
Alexey Budankovcf790512018-10-09 17:36:24 +03002651static int process_clockid(struct feat_fd *ff,
2652 void *data __maybe_unused)
2653{
2654 if (do_read_u64(ff, &ff->ph->env.clockid_res_ns))
2655 return -1;
2656
2657 return 0;
2658}
2659
Jiri Olsa258031c2019-03-08 14:47:39 +01002660static int process_dir_format(struct feat_fd *ff,
2661 void *_data __maybe_unused)
2662{
2663 struct perf_session *session;
2664 struct perf_data *data;
2665
2666 session = container_of(ff->ph, struct perf_session, header);
2667 data = session->data;
2668
2669 if (WARN_ON(!perf_data__is_dir(data)))
2670 return -1;
2671
2672 return do_read_u64(ff, &data->dir.version);
2673}
2674
Song Liu606f9722019-03-11 22:30:43 -07002675#ifdef HAVE_LIBBPF_SUPPORT
2676static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
2677{
2678 struct bpf_prog_info_linear *info_linear;
2679 struct bpf_prog_info_node *info_node;
2680 struct perf_env *env = &ff->ph->env;
2681 u32 count, i;
2682 int err = -1;
2683
2684 if (ff->ph->needs_swap) {
2685 pr_warning("interpreting bpf_prog_info from systems with endianity is not yet supported\n");
2686 return 0;
2687 }
2688
2689 if (do_read_u32(ff, &count))
2690 return -1;
2691
2692 down_write(&env->bpf_progs.lock);
2693
2694 for (i = 0; i < count; ++i) {
2695 u32 info_len, data_len;
2696
2697 info_linear = NULL;
2698 info_node = NULL;
2699 if (do_read_u32(ff, &info_len))
2700 goto out;
2701 if (do_read_u32(ff, &data_len))
2702 goto out;
2703
2704 if (info_len > sizeof(struct bpf_prog_info)) {
2705 pr_warning("detected invalid bpf_prog_info\n");
2706 goto out;
2707 }
2708
2709 info_linear = malloc(sizeof(struct bpf_prog_info_linear) +
2710 data_len);
2711 if (!info_linear)
2712 goto out;
2713 info_linear->info_len = sizeof(struct bpf_prog_info);
2714 info_linear->data_len = data_len;
2715 if (do_read_u64(ff, (u64 *)(&info_linear->arrays)))
2716 goto out;
2717 if (__do_read(ff, &info_linear->info, info_len))
2718 goto out;
2719 if (info_len < sizeof(struct bpf_prog_info))
2720 memset(((void *)(&info_linear->info)) + info_len, 0,
2721 sizeof(struct bpf_prog_info) - info_len);
2722
2723 if (__do_read(ff, info_linear->data, data_len))
2724 goto out;
2725
2726 info_node = malloc(sizeof(struct bpf_prog_info_node));
2727 if (!info_node)
2728 goto out;
2729
2730 /* after reading from file, translate offset to address */
2731 bpf_program__bpil_offs_to_addr(info_linear);
2732 info_node->info_linear = info_linear;
2733 perf_env__insert_bpf_prog_info(env, info_node);
2734 }
2735
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002736 up_write(&env->bpf_progs.lock);
Song Liu606f9722019-03-11 22:30:43 -07002737 return 0;
2738out:
2739 free(info_linear);
2740 free(info_node);
2741 up_write(&env->bpf_progs.lock);
2742 return err;
2743}
2744#else // HAVE_LIBBPF_SUPPORT
2745static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data __maybe_unused)
2746{
2747 return 0;
2748}
2749#endif // HAVE_LIBBPF_SUPPORT
2750
Song Liua70a112312019-03-11 22:30:45 -07002751static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)
2752{
2753 struct perf_env *env = &ff->ph->env;
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002754 struct btf_node *node = NULL;
Song Liua70a112312019-03-11 22:30:45 -07002755 u32 count, i;
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002756 int err = -1;
Song Liua70a112312019-03-11 22:30:45 -07002757
2758 if (ff->ph->needs_swap) {
2759 pr_warning("interpreting btf from systems with endianity is not yet supported\n");
2760 return 0;
2761 }
2762
2763 if (do_read_u32(ff, &count))
2764 return -1;
2765
2766 down_write(&env->bpf_progs.lock);
2767
2768 for (i = 0; i < count; ++i) {
Song Liua70a112312019-03-11 22:30:45 -07002769 u32 id, data_size;
2770
2771 if (do_read_u32(ff, &id))
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002772 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002773 if (do_read_u32(ff, &data_size))
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002774 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002775
2776 node = malloc(sizeof(struct btf_node) + data_size);
2777 if (!node)
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002778 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002779
2780 node->id = id;
2781 node->data_size = data_size;
2782
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002783 if (__do_read(ff, node->data, data_size))
2784 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002785
2786 perf_env__insert_btf(env, node);
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002787 node = NULL;
Song Liua70a112312019-03-11 22:30:45 -07002788 }
2789
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002790 err = 0;
2791out:
Song Liua70a112312019-03-11 22:30:45 -07002792 up_write(&env->bpf_progs.lock);
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002793 free(node);
2794 return err;
Song Liua70a112312019-03-11 22:30:45 -07002795}
2796
Alexey Budankov42e1fd82019-03-18 20:41:33 +03002797static int process_compressed(struct feat_fd *ff,
2798 void *data __maybe_unused)
2799{
2800 if (do_read_u32(ff, &(ff->ph->env.comp_ver)))
2801 return -1;
2802
2803 if (do_read_u32(ff, &(ff->ph->env.comp_type)))
2804 return -1;
2805
2806 if (do_read_u32(ff, &(ff->ph->env.comp_level)))
2807 return -1;
2808
2809 if (do_read_u32(ff, &(ff->ph->env.comp_ratio)))
2810 return -1;
2811
2812 if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len)))
2813 return -1;
2814
2815 return 0;
2816}
2817
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002818#define FEAT_OPR(n, func, __full_only) \
2819 [HEADER_##n] = { \
2820 .name = __stringify(n), \
2821 .write = write_##func, \
2822 .print = print_##func, \
2823 .full_only = __full_only, \
2824 .process = process_##func, \
2825 .synthesize = true \
2826 }
2827
2828#define FEAT_OPN(n, func, __full_only) \
2829 [HEADER_##n] = { \
2830 .name = __stringify(n), \
2831 .write = write_##func, \
2832 .print = print_##func, \
2833 .full_only = __full_only, \
2834 .process = process_##func \
2835 }
Robert Richter8cdfa782011-12-07 10:02:56 +01002836
2837/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002838#define print_tracing_data NULL
2839#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002840
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002841#define process_branch_stack NULL
2842#define process_stat NULL
2843
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03002844// Only used in util/synthetic-events.c
2845const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002846
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03002847const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002848 FEAT_OPN(TRACING_DATA, tracing_data, false),
2849 FEAT_OPN(BUILD_ID, build_id, false),
2850 FEAT_OPR(HOSTNAME, hostname, false),
2851 FEAT_OPR(OSRELEASE, osrelease, false),
2852 FEAT_OPR(VERSION, version, false),
2853 FEAT_OPR(ARCH, arch, false),
2854 FEAT_OPR(NRCPUS, nrcpus, false),
2855 FEAT_OPR(CPUDESC, cpudesc, false),
2856 FEAT_OPR(CPUID, cpuid, false),
2857 FEAT_OPR(TOTAL_MEM, total_mem, false),
2858 FEAT_OPR(EVENT_DESC, event_desc, false),
2859 FEAT_OPR(CMDLINE, cmdline, false),
2860 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2861 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2862 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2863 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
Jiri Olsae8fedff2018-07-12 15:52:02 +02002864 FEAT_OPR(GROUP_DESC, group_desc, false),
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002865 FEAT_OPN(AUXTRACE, auxtrace, false),
2866 FEAT_OPN(STAT, stat, false),
2867 FEAT_OPN(CACHE, cache, true),
Jin Yao60115182017-12-08 21:13:41 +08002868 FEAT_OPR(SAMPLE_TIME, sample_time, false),
Jiri Olsae2091ce2018-03-07 16:50:08 +01002869 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
Jiri Olsa258031c2019-03-08 14:47:39 +01002870 FEAT_OPR(CLOCKID, clockid, false),
Song Liu606f9722019-03-11 22:30:43 -07002871 FEAT_OPN(DIR_FORMAT, dir_format, false),
Song Liua70a112312019-03-11 22:30:45 -07002872 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false),
2873 FEAT_OPR(BPF_BTF, bpf_btf, false),
Alexey Budankov42e1fd82019-03-18 20:41:33 +03002874 FEAT_OPR(COMPRESSED, compressed, false),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002875};
2876
2877struct header_print_data {
2878 FILE *fp;
2879 bool full; /* extended list of headers */
2880};
2881
2882static int perf_file_section__fprintf_info(struct perf_file_section *section,
2883 struct perf_header *ph,
2884 int feat, int fd, void *data)
2885{
2886 struct header_print_data *hd = data;
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002887 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002888
2889 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2890 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2891 "%d, continuing...\n", section->offset, feat);
2892 return 0;
2893 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002894 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002895 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002896 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002897 }
2898 if (!feat_ops[feat].print)
2899 return 0;
2900
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002901 ff = (struct feat_fd) {
2902 .fd = fd,
2903 .ph = ph,
2904 };
2905
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002906 if (!feat_ops[feat].full_only || hd->full)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002907 feat_ops[feat].print(&ff, hd->fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002908 else
2909 fprintf(hd->fp, "# %s info available, use -I to display\n",
2910 feat_ops[feat].name);
2911
2912 return 0;
2913}
2914
2915int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2916{
2917 struct header_print_data hd;
2918 struct perf_header *header = &session->header;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002919 int fd = perf_data__fd(session->data);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002920 struct stat st;
Arnaldo Carvalho de Melo0afcf292018-12-11 16:11:54 -03002921 time_t stctime;
Jiri Olsaaabae162016-10-10 09:35:50 +02002922 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002923
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002924 hd.fp = fp;
2925 hd.full = full;
2926
Jiri Olsaf45f5612016-10-10 09:03:07 +02002927 ret = fstat(fd, &st);
2928 if (ret == -1)
2929 return -1;
2930
Arnaldo Carvalho de Melo0afcf292018-12-11 16:11:54 -03002931 stctime = st.st_ctime;
2932 fprintf(fp, "# captured on : %s", ctime(&stctime));
Jiri Olsae971a5a2018-03-07 16:50:03 +01002933
2934 fprintf(fp, "# header version : %u\n", header->version);
2935 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
2936 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
2937 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002938
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002939 perf_header__process_sections(header, fd, &hd,
2940 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002941
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002942 if (session->data->is_pipe)
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002943 return 0;
2944
Jiri Olsaaabae162016-10-10 09:35:50 +02002945 fprintf(fp, "# missing features: ");
2946 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2947 if (bit)
2948 fprintf(fp, "%s ", feat_ops[bit].name);
2949 }
2950
2951 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002952 return 0;
2953}
2954
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002955static int do_write_feat(struct feat_fd *ff, int type,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002956 struct perf_file_section **p,
Jiri Olsa63503db2019-07-21 13:23:52 +02002957 struct evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002958{
2959 int err;
2960 int ret = 0;
2961
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002962 if (perf_header__has_feat(ff->ph, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002963 if (!feat_ops[type].write)
2964 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002965
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -07002966 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2967 return -1;
2968
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002969 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002970
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002971 err = feat_ops[type].write(ff, evlist);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002972 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002973 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002974
2975 /* undo anything written */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002976 lseek(ff->fd, (*p)->offset, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002977
2978 return -1;
2979 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002980 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002981 (*p)++;
2982 }
2983 return ret;
2984}
2985
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002986static int perf_header__adds_write(struct perf_header *header,
Jiri Olsa63503db2019-07-21 13:23:52 +02002987 struct evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002988{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002989 int nr_sections;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002990 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002991 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002992 int sec_size;
2993 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002994 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002995 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002996
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002997 ff = (struct feat_fd){
2998 .fd = fd,
2999 .ph = header,
3000 };
3001
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003002 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003003 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003004 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003005
Paul Gortmaker91b98802013-01-30 20:05:49 -05003006 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003007 if (feat_sec == NULL)
3008 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003009
3010 sec_size = sizeof(*feat_sec) * nr_sections;
3011
Jiri Olsa8d541e92013-07-17 19:49:44 +02003012 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08003013 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003014
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003015 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003016 if (do_write_feat(&ff, feat, &p, evlist))
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003017 perf_header__clear_feat(header, feat);
3018 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003019
Xiao Guangrongf887f302010-02-04 16:46:42 +08003020 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003021 /*
3022 * may write more than needed due to dropped feature, but
Ingo Molnaradba1632018-12-03 11:22:00 +01003023 * this is okay, reader will skip the missing entries
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003024 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003025 err = do_write(&ff, feat_sec, sec_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003026 if (err < 0)
3027 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003028 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003029 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003030}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003031
Tom Zanussi8dc58102010-04-01 23:59:15 -05003032int perf_header__write_pipe(int fd)
3033{
3034 struct perf_pipe_file_header f_header;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003035 struct feat_fd ff;
Tom Zanussi8dc58102010-04-01 23:59:15 -05003036 int err;
3037
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003038 ff = (struct feat_fd){ .fd = fd };
3039
Tom Zanussi8dc58102010-04-01 23:59:15 -05003040 f_header = (struct perf_pipe_file_header){
3041 .magic = PERF_MAGIC,
3042 .size = sizeof(f_header),
3043 };
3044
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003045 err = do_write(&ff, &f_header, sizeof(f_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05003046 if (err < 0) {
3047 pr_debug("failed to write perf pipe header\n");
3048 return err;
3049 }
3050
3051 return 0;
3052}
3053
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003054int perf_session__write_header(struct perf_session *session,
Jiri Olsa63503db2019-07-21 13:23:52 +02003055 struct evlist *evlist,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003056 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003057{
3058 struct perf_file_header f_header;
3059 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003060 struct perf_header *header = &session->header;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003061 struct evsel *evsel;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003062 struct feat_fd ff;
Jiri Olsa944d62b2013-07-17 19:49:43 +02003063 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003064 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003065
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003066 ff = (struct feat_fd){ .fd = fd};
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003067 lseek(fd, sizeof(f_header), SEEK_SET);
3068
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003069 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003070 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003071 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003072 if (err < 0) {
3073 pr_debug("failed to write perf header\n");
3074 return err;
3075 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003076 }
3077
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003078 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003079
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003080 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003081 f_attr = (struct perf_file_attr){
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003082 .attr = evsel->core.attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003083 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02003084 .offset = evsel->id_offset,
3085 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003086 }
3087 };
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003088 err = do_write(&ff, &f_attr, sizeof(f_attr));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003089 if (err < 0) {
3090 pr_debug("failed to write perf header attribute\n");
3091 return err;
3092 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003093 }
3094
Adrian Hunterd645c442013-12-11 14:36:28 +02003095 if (!header->data_offset)
3096 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02003097 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003098
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003099 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003100 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003101 if (err < 0)
3102 return err;
3103 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003104
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003105 f_header = (struct perf_file_header){
3106 .magic = PERF_MAGIC,
3107 .size = sizeof(f_header),
3108 .attr_size = sizeof(f_attr),
3109 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02003110 .offset = attr_offset,
Jiri Olsa6484d2f2019-07-21 13:24:28 +02003111 .size = evlist->core.nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003112 },
3113 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003114 .offset = header->data_offset,
3115 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003116 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02003117 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003118 };
3119
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003120 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003121
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003122 lseek(fd, 0, SEEK_SET);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003123 err = do_write(&ff, &f_header, sizeof(f_header));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003124 if (err < 0) {
3125 pr_debug("failed to write perf header\n");
3126 return err;
3127 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003128 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003129
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003130 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003131}
3132
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003133static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003134 int fd, void *buf, size_t size)
3135{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02003136 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003137 return -1;
3138
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003139 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003140 mem_bswap_64(buf, size);
3141
3142 return 0;
3143}
3144
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003145int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003146 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003147 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003148 struct perf_header *ph,
3149 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003150{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003151 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003152 int nr_sections;
3153 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003154 int feat;
3155 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003156
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003157 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003158 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003159 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003160
Paul Gortmaker91b98802013-01-30 20:05:49 -05003161 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003162 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003163 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003164
3165 sec_size = sizeof(*feat_sec) * nr_sections;
3166
Jiri Olsa8d541e92013-07-17 19:49:44 +02003167 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003168
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003169 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
3170 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003171 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003172
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003173 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
3174 err = process(sec++, header, feat, fd, data);
3175 if (err < 0)
3176 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01003177 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003178 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003179out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003180 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003181 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003182}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003183
Stephane Eranian114382a2012-02-09 23:21:08 +01003184static const int attr_file_abi_sizes[] = {
3185 [0] = PERF_ATTR_SIZE_VER0,
3186 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02003187 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02003188 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02003189 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01003190 0,
3191};
3192
3193/*
3194 * In the legacy file format, the magic number is not used to encode endianness.
3195 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
3196 * on ABI revisions, we need to try all combinations for all endianness to
3197 * detect the endianness.
3198 */
3199static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
3200{
3201 uint64_t ref_size, attr_size;
3202 int i;
3203
3204 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
3205 ref_size = attr_file_abi_sizes[i]
3206 + sizeof(struct perf_file_section);
3207 if (hdr_sz != ref_size) {
3208 attr_size = bswap_64(hdr_sz);
3209 if (attr_size != ref_size)
3210 continue;
3211
3212 ph->needs_swap = true;
3213 }
3214 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
3215 i,
3216 ph->needs_swap);
3217 return 0;
3218 }
3219 /* could not determine endianness */
3220 return -1;
3221}
3222
3223#define PERF_PIPE_HDR_VER0 16
3224
3225static const size_t attr_pipe_abi_sizes[] = {
3226 [0] = PERF_PIPE_HDR_VER0,
3227 0,
3228};
3229
3230/*
3231 * In the legacy pipe format, there is an implicit assumption that endiannesss
3232 * between host recording the samples, and host parsing the samples is the
3233 * same. This is not always the case given that the pipe output may always be
3234 * redirected into a file and analyzed on a different machine with possibly a
3235 * different endianness and perf_event ABI revsions in the perf tool itself.
3236 */
3237static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
3238{
3239 u64 attr_size;
3240 int i;
3241
3242 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
3243 if (hdr_sz != attr_pipe_abi_sizes[i]) {
3244 attr_size = bswap_64(hdr_sz);
3245 if (attr_size != hdr_sz)
3246 continue;
3247
3248 ph->needs_swap = true;
3249 }
3250 pr_debug("Pipe ABI%d perf.data file detected\n", i);
3251 return 0;
3252 }
3253 return -1;
3254}
3255
Feng Tange84ba4e2012-10-30 11:56:07 +08003256bool is_perf_magic(u64 magic)
3257{
3258 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
3259 || magic == __perf_magic2
3260 || magic == __perf_magic2_sw)
3261 return true;
3262
3263 return false;
3264}
3265
Stephane Eranian114382a2012-02-09 23:21:08 +01003266static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3267 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01003268{
3269 int ret;
3270
3271 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01003272 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01003273 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02003274 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01003275 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01003276 if (is_pipe)
3277 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01003278
Stephane Eranian114382a2012-02-09 23:21:08 +01003279 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01003280 }
Stephane Eranian114382a2012-02-09 23:21:08 +01003281 /*
3282 * the new magic number serves two purposes:
3283 * - unique number to identify actual perf.data files
3284 * - encode endianness of file
3285 */
Namhyung Kimf7913972015-01-29 17:06:45 +09003286 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01003287
Stephane Eranian114382a2012-02-09 23:21:08 +01003288 /* check magic number with one endianness */
3289 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01003290 return 0;
3291
Stephane Eranian114382a2012-02-09 23:21:08 +01003292 /* check magic number with opposite endianness */
3293 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01003294 return -1;
3295
3296 ph->needs_swap = true;
3297
3298 return 0;
3299}
3300
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003301int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003302 struct perf_header *ph, int fd)
3303{
Jiri Olsa727ebd52013-11-28 11:30:14 +01003304 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01003305
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003306 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003307
Stephane Eranian73323f52012-02-02 13:54:44 +01003308 ret = readn(fd, header, sizeof(*header));
3309 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003310 return -1;
3311
Stephane Eranian114382a2012-02-09 23:21:08 +01003312 if (check_magic_endian(header->magic,
3313 header->attr_size, false, ph) < 0) {
3314 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01003315 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01003316 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003317
Stephane Eranian73323f52012-02-02 13:54:44 +01003318 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003319 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01003320 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003321 }
3322
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003323 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003324 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003325 if (header->size == offsetof(typeof(*header), adds_features))
3326 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003327 else
3328 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06003329 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06003330 /*
3331 * feature bitmap is declared as an array of unsigned longs --
3332 * not good since its size can differ between the host that
3333 * generated the data file and the host analyzing the file.
3334 *
3335 * We need to handle endianness, but we don't know the size of
3336 * the unsigned long where the file was generated. Take a best
3337 * guess at determining it: try 64-bit swap first (ie., file
3338 * created on a 64-bit host), and check if the hostname feature
3339 * bit is set (this feature bit is forced on as of fbe96f2).
3340 * If the bit is not, undo the 64-bit swap and try a 32-bit
3341 * swap. If the hostname bit is still not set (e.g., older data
3342 * file), punt and fallback to the original behavior --
3343 * clearing all feature bits and setting buildid.
3344 */
David Ahern80c01202012-06-08 11:47:51 -03003345 mem_bswap_64(&header->adds_features,
3346 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06003347
3348 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03003349 /* unswap as u64 */
3350 mem_bswap_64(&header->adds_features,
3351 BITS_TO_U64(HEADER_FEAT_BITS));
3352
3353 /* unswap as u32 */
3354 mem_bswap_32(&header->adds_features,
3355 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06003356 }
3357
3358 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3359 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3360 set_bit(HEADER_BUILD_ID, header->adds_features);
3361 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003362 }
3363
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003364 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003365 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003366
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003367 ph->data_offset = header->data.offset;
3368 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02003369 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003370 return 0;
3371}
3372
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003373static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003374 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003375 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003376{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003377 struct feat_fd fdd = {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07003378 .fd = fd,
3379 .ph = ph,
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003380 .size = section->size,
3381 .offset = section->offset,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07003382 };
3383
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003384 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02003385 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003386 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003387 return 0;
3388 }
3389
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003390 if (feat >= HEADER_LAST_FEATURE) {
3391 pr_debug("unknown feature %d, continuing...\n", feat);
3392 return 0;
3393 }
3394
Robert Richterf1c67db2012-02-10 15:41:56 +01003395 if (!feat_ops[feat].process)
3396 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003397
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003398 return feat_ops[feat].process(&fdd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003399}
3400
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003401static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05003402 struct perf_header *ph, int fd,
3403 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003404{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003405 struct feat_fd ff = {
3406 .fd = STDOUT_FILENO,
3407 .ph = ph,
3408 };
Jiri Olsa727ebd52013-11-28 11:30:14 +01003409 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01003410
3411 ret = readn(fd, header, sizeof(*header));
3412 if (ret <= 0)
3413 return -1;
3414
Stephane Eranian114382a2012-02-09 23:21:08 +01003415 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
3416 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05003417 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01003418 }
3419
3420 if (ph->needs_swap)
3421 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05003422
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003423 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05003424 return -1;
3425
Tom Zanussi8dc58102010-04-01 23:59:15 -05003426 return 0;
3427}
3428
Jiri Olsad4339562013-07-17 19:49:41 +02003429static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05003430{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003431 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05003432 struct perf_pipe_file_header f_header;
3433
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003434 if (perf_file_header__read_pipe(&f_header, header,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003435 perf_data__fd(session->data),
Tom Zanussi454c4072010-05-01 01:41:20 -05003436 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05003437 pr_debug("incompatible file format\n");
3438 return -EINVAL;
3439 }
3440
Tom Zanussi8dc58102010-04-01 23:59:15 -05003441 return 0;
3442}
3443
Stephane Eranian69996df2012-02-09 23:21:06 +01003444static int read_attr(int fd, struct perf_header *ph,
3445 struct perf_file_attr *f_attr)
3446{
3447 struct perf_event_attr *attr = &f_attr->attr;
3448 size_t sz, left;
3449 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01003450 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01003451
3452 memset(f_attr, 0, sizeof(*f_attr));
3453
3454 /* read minimal guaranteed structure */
3455 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3456 if (ret <= 0) {
3457 pr_debug("cannot read %d bytes of header attr\n",
3458 PERF_ATTR_SIZE_VER0);
3459 return -1;
3460 }
3461
3462 /* on file perf_event_attr size */
3463 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01003464
Stephane Eranian69996df2012-02-09 23:21:06 +01003465 if (ph->needs_swap)
3466 sz = bswap_32(sz);
3467
3468 if (sz == 0) {
3469 /* assume ABI0 */
3470 sz = PERF_ATTR_SIZE_VER0;
3471 } else if (sz > our_sz) {
3472 pr_debug("file uses a more recent and unsupported ABI"
3473 " (%zu bytes extra)\n", sz - our_sz);
3474 return -1;
3475 }
3476 /* what we have not yet read and that we know about */
3477 left = sz - PERF_ATTR_SIZE_VER0;
3478 if (left) {
3479 void *ptr = attr;
3480 ptr += PERF_ATTR_SIZE_VER0;
3481
3482 ret = readn(fd, ptr, left);
3483 }
3484 /* read perf_file_section, ids are read in caller */
3485 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3486
3487 return ret <= 0 ? -1 : 0;
3488}
3489
Jiri Olsa32dcd022019-07-21 13:23:51 +02003490static int perf_evsel__prepare_tracepoint_event(struct evsel *evsel,
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003491 struct tep_handle *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003492{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003493 struct tep_event *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003494 char bf[128];
3495
Namhyung Kim831394b2012-09-06 11:10:46 +09003496 /* already prepared */
3497 if (evsel->tp_format)
3498 return 0;
3499
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09003500 if (pevent == NULL) {
3501 pr_debug("broken or missing trace data\n");
3502 return -1;
3503 }
3504
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003505 event = tep_find_event(pevent, evsel->core.attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09003506 if (event == NULL) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003507 pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003508 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09003509 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003510
Namhyung Kim831394b2012-09-06 11:10:46 +09003511 if (!evsel->name) {
3512 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3513 evsel->name = strdup(bf);
3514 if (evsel->name == NULL)
3515 return -1;
3516 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003517
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03003518 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003519 return 0;
3520}
3521
Jiri Olsa63503db2019-07-21 13:23:52 +02003522static int perf_evlist__prepare_tracepoint_events(struct evlist *evlist,
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003523 struct tep_handle *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003524{
Jiri Olsa32dcd022019-07-21 13:23:51 +02003525 struct evsel *pos;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003526
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003527 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003528 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT &&
Namhyung Kim831394b2012-09-06 11:10:46 +09003529 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003530 return -1;
3531 }
3532
3533 return 0;
3534}
3535
Jiri Olsad4339562013-07-17 19:49:41 +02003536int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05003537{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003538 struct perf_data *data = session->data;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003539 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003540 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003541 struct perf_file_attr f_attr;
3542 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003543 int nr_attrs, nr_ids, i, j;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003544 int fd = perf_data__fd(data);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003545
Jiri Olsa0f98b112019-07-21 13:23:55 +02003546 session->evlist = evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003547 if (session->evlist == NULL)
3548 return -ENOMEM;
3549
Kan Liang2c071442015-08-28 05:48:05 -04003550 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03003551 session->machines.host.env = &header->env;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003552 if (perf_data__is_pipe(data))
Jiri Olsad4339562013-07-17 19:49:41 +02003553 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05003554
Stephane Eranian69996df2012-02-09 23:21:06 +01003555 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003556 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003557
Namhyung Kimb314e5c2013-09-30 17:19:48 +09003558 /*
3559 * Sanity check that perf.data was written cleanly; data size is
3560 * initialized to 0 and updated only if the on_exit function is run.
3561 * If data size is still 0 then the file contains only partial
3562 * information. Just warn user and process it as much as it can.
3563 */
3564 if (f_header.data.size == 0) {
3565 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
3566 "Was the 'perf record' command properly terminated?\n",
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003567 data->file.path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09003568 }
3569
Vince Weaver76222362019-07-23 11:06:01 -04003570 if (f_header.attr_size == 0) {
3571 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
3572 "Was the 'perf record' command properly terminated?\n",
3573 data->file.path);
3574 return -EINVAL;
3575 }
3576
Stephane Eranian69996df2012-02-09 23:21:06 +01003577 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003578 lseek(fd, f_header.attrs.offset, SEEK_SET);
3579
3580 for (i = 0; i < nr_attrs; i++) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02003581 struct evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02003582 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003583
Stephane Eranian69996df2012-02-09 23:21:06 +01003584 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003585 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003586
David Ahern1060ab82015-04-09 16:15:46 -04003587 if (header->needs_swap) {
3588 f_attr.ids.size = bswap_64(f_attr.ids.size);
3589 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06003590 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04003591 }
David Aherneda39132011-07-15 12:34:09 -06003592
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02003593 tmp = lseek(fd, 0, SEEK_CUR);
Jiri Olsa365c3ae2019-07-21 13:23:58 +02003594 evsel = evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003595
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003596 if (evsel == NULL)
3597 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03003598
3599 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003600 /*
3601 * Do it before so that if perf_evsel__alloc_id fails, this
Jiri Olsac12995a2019-07-21 13:23:56 +02003602 * entry gets purged too at evlist__delete().
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003603 */
Jiri Olsaa1cf3a72019-07-21 13:23:59 +02003604 evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003605
3606 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003607 /*
3608 * We don't have the cpu and thread maps on the header, so
3609 * for allocating the perf_sample_id table we fake 1 cpu and
3610 * hattr->ids threads.
3611 */
3612 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
3613 goto out_delete_evlist;
3614
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003615 lseek(fd, f_attr.ids.offset, SEEK_SET);
3616
3617 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003618 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003619 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003620
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003621 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003622 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02003623
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003624 lseek(fd, tmp, SEEK_SET);
3625 }
3626
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003627 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003628 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01003629
Namhyung Kim831394b2012-09-06 11:10:46 +09003630 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003631 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003632 goto out_delete_evlist;
3633
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003634 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003635out_errno:
3636 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003637
3638out_delete_evlist:
Jiri Olsac12995a2019-07-21 13:23:56 +02003639 evlist__delete(session->evlist);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003640 session->evlist = NULL;
3641 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003642}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02003643
Jiri Olsa89f16882018-09-13 14:54:03 +02003644int perf_event__process_feature(struct perf_session *session,
3645 union perf_event *event)
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003646{
Jiri Olsa89f16882018-09-13 14:54:03 +02003647 struct perf_tool *tool = session->tool;
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003648 struct feat_fd ff = { .fd = 0 };
Jiri Olsa72932372019-08-28 15:57:16 +02003649 struct perf_record_header_feature *fe = (struct perf_record_header_feature *)event;
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003650 int type = fe->header.type;
3651 u64 feat = fe->feat_id;
3652
3653 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3654 pr_warning("invalid record type %d in pipe-mode\n", type);
3655 return 0;
3656 }
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303657 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003658 pr_warning("invalid record type %d in pipe-mode\n", type);
3659 return -1;
3660 }
3661
3662 if (!feat_ops[feat].process)
3663 return 0;
3664
3665 ff.buf = (void *)fe->data;
Jiri Olsa79b2fe52019-07-15 16:04:26 +02003666 ff.size = event->header.size - sizeof(*fe);
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003667 ff.ph = &session->header;
3668
3669 if (feat_ops[feat].process(&ff, NULL))
3670 return -1;
3671
3672 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3673 return 0;
3674
3675 if (!feat_ops[feat].full_only ||
3676 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3677 feat_ops[feat].print(&ff, stdout);
3678 } else {
3679 fprintf(stdout, "# %s info available, use -I to display\n",
3680 feat_ops[feat].name);
3681 }
3682
3683 return 0;
3684}
3685
Jiri Olsac853f932015-10-25 15:51:41 +01003686size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3687{
Jiri Olsa72932372019-08-28 15:57:16 +02003688 struct perf_record_event_update *ev = &event->event_update;
3689 struct perf_record_event_update_scale *ev_scale;
3690 struct perf_record_event_update_cpus *ev_cpus;
Jiri Olsaf8548392019-07-21 13:23:49 +02003691 struct perf_cpu_map *map;
Jiri Olsac853f932015-10-25 15:51:41 +01003692 size_t ret;
3693
Jiri Olsa5ded0682019-08-28 15:56:57 +02003694 ret = fprintf(fp, "\n... id: %" PRI_lu64 "\n", ev->id);
Jiri Olsac853f932015-10-25 15:51:41 +01003695
3696 switch (ev->type) {
3697 case PERF_EVENT_UPDATE__SCALE:
Jiri Olsa72932372019-08-28 15:57:16 +02003698 ev_scale = (struct perf_record_event_update_scale *)ev->data;
Jiri Olsac853f932015-10-25 15:51:41 +01003699 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3700 break;
3701 case PERF_EVENT_UPDATE__UNIT:
3702 ret += fprintf(fp, "... unit: %s\n", ev->data);
3703 break;
3704 case PERF_EVENT_UPDATE__NAME:
3705 ret += fprintf(fp, "... name: %s\n", ev->data);
3706 break;
3707 case PERF_EVENT_UPDATE__CPUS:
Jiri Olsa72932372019-08-28 15:57:16 +02003708 ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
Jiri Olsac853f932015-10-25 15:51:41 +01003709 ret += fprintf(fp, "... ");
3710
3711 map = cpu_map__new_data(&ev_cpus->cpus);
3712 if (map)
3713 ret += cpu_map__fprintf(map, fp);
3714 else
3715 ret += fprintf(fp, "failed to get cpus\n");
3716 break;
3717 default:
3718 ret += fprintf(fp, "... unknown type\n");
3719 break;
3720 }
3721
3722 return ret;
3723}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003724
Adrian Hunter47c3d102013-07-04 16:20:21 +03003725int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3726 union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02003727 struct evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003728{
Robert Richterf4d83432012-08-16 21:10:17 +02003729 u32 i, ids, n_ids;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003730 struct evsel *evsel;
Jiri Olsa63503db2019-07-21 13:23:52 +02003731 struct evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003732
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003733 if (evlist == NULL) {
Jiri Olsa0f98b112019-07-21 13:23:55 +02003734 *pevlist = evlist = evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003735 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003736 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003737 }
3738
Jiri Olsa365c3ae2019-07-21 13:23:58 +02003739 evsel = evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003740 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003741 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003742
Jiri Olsaa1cf3a72019-07-21 13:23:59 +02003743 evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003744
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003745 ids = event->header.size;
3746 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003747 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003748 /*
3749 * We don't have the cpu and thread maps on the header, so
3750 * for allocating the perf_sample_id table we fake 1 cpu and
3751 * hattr->ids threads.
3752 */
3753 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3754 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003755
3756 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003757 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003758 }
3759
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003760 return 0;
3761}
Tom Zanussicd19a032010-04-01 23:59:20 -05003762
Jiri Olsaffe777252015-10-25 15:51:36 +01003763int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3764 union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02003765 struct evlist **pevlist)
Jiri Olsaffe777252015-10-25 15:51:36 +01003766{
Jiri Olsa72932372019-08-28 15:57:16 +02003767 struct perf_record_event_update *ev = &event->event_update;
3768 struct perf_record_event_update_scale *ev_scale;
3769 struct perf_record_event_update_cpus *ev_cpus;
Jiri Olsa63503db2019-07-21 13:23:52 +02003770 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003771 struct evsel *evsel;
Jiri Olsaf8548392019-07-21 13:23:49 +02003772 struct perf_cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003773
3774 if (!pevlist || *pevlist == NULL)
3775 return -EINVAL;
3776
3777 evlist = *pevlist;
3778
3779 evsel = perf_evlist__id2evsel(evlist, ev->id);
3780 if (evsel == NULL)
3781 return -EINVAL;
3782
Jiri Olsaa6e52812015-10-25 15:51:37 +01003783 switch (ev->type) {
3784 case PERF_EVENT_UPDATE__UNIT:
3785 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003786 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003787 case PERF_EVENT_UPDATE__NAME:
3788 evsel->name = strdup(ev->data);
3789 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003790 case PERF_EVENT_UPDATE__SCALE:
Jiri Olsa72932372019-08-28 15:57:16 +02003791 ev_scale = (struct perf_record_event_update_scale *)ev->data;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003792 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003793 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003794 case PERF_EVENT_UPDATE__CPUS:
Jiri Olsa72932372019-08-28 15:57:16 +02003795 ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003796
3797 map = cpu_map__new_data(&ev_cpus->cpus);
3798 if (map)
Jiri Olsafe1f61b2019-07-21 13:24:38 +02003799 evsel->core.own_cpus = map;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003800 else
3801 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003802 default:
3803 break;
3804 }
3805
Jiri Olsaffe777252015-10-25 15:51:36 +01003806 return 0;
3807}
3808
Jiri Olsa89f16882018-09-13 14:54:03 +02003809int perf_event__process_tracing_data(struct perf_session *session,
3810 union perf_event *event)
Tom Zanussi92155452010-04-01 23:59:21 -05003811{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003812 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003813 int fd = perf_data__fd(session->data);
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003814 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003815 char buf[BUFSIZ];
3816
3817 /* setup for reading amidst mmap */
Jiri Olsa72932372019-08-28 15:57:16 +02003818 lseek(fd, offset + sizeof(struct perf_record_header_tracing_data),
Tom Zanussi92155452010-04-01 23:59:21 -05003819 SEEK_SET);
3820
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003821 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003822 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003823 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003824
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003825 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003826 pr_err("%s: reading input file", __func__);
3827 return -1;
3828 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003829 if (session->repipe) {
3830 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003831 if (retw <= 0 || retw != padding) {
3832 pr_err("%s: repiping tracing data padding", __func__);
3833 return -1;
3834 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003835 }
Tom Zanussi92155452010-04-01 23:59:21 -05003836
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003837 if (size_read + padding != size) {
3838 pr_err("%s: tracing data size mismatch", __func__);
3839 return -1;
3840 }
Tom Zanussi92155452010-04-01 23:59:21 -05003841
Namhyung Kim831394b2012-09-06 11:10:46 +09003842 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003843 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003844
Tom Zanussi92155452010-04-01 23:59:21 -05003845 return size_read + padding;
3846}
Tom Zanussic7929e42010-04-01 23:59:22 -05003847
Jiri Olsa89f16882018-09-13 14:54:03 +02003848int perf_event__process_build_id(struct perf_session *session,
3849 union perf_event *event)
Tom Zanussic7929e42010-04-01 23:59:22 -05003850{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003851 __event_process_build_id(&event->build_id,
3852 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003853 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003854 return 0;
3855}