blob: 4fe9e2a54346e51fca8ad6b4f0b4396b6af966cf [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>
Arnaldo Carvalho de Meloef0580e2020-10-20 15:57:21 -030022#ifdef HAVE_LIBBPF_SUPPORT
Song Liu606f9722019-03-11 22:30:43 -070023#include <bpf/libbpf.h>
Arnaldo Carvalho de Meloef0580e2020-10-20 15:57:21 -030024#endif
Jiri Olsa9c3516d2019-07-21 13:24:30 +020025#include <perf/cpumap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020026
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -030027#include "dso.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020028#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030029#include "evsel.h"
Arnaldo Carvalho de Meloca125272019-09-24 15:41:51 -030030#include "util/evsel_fprintf.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020031#include "header.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030032#include "memswap.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020033#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020034#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010035#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010036#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020037#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020038#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020039#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090040#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020041#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020042#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010043#include <api/fs/fs.h>
44#include "asm/bug.h"
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -070045#include "tool.h"
Jin Yao60115182017-12-08 21:13:41 +080046#include "time-utils.h"
Jiri Olsae2091ce2018-03-07 16:50:08 +010047#include "units.h"
Jiri Olsa20f2be12019-08-06 15:25:25 +020048#include "util/util.h" // perf_exe()
Jiri Olsa5135d5e2019-02-19 10:58:13 +010049#include "cputopo.h"
Song Liu606f9722019-03-11 22:30:43 -070050#include "bpf-event.h"
Jiri Olsad1e325c2020-08-05 11:34:40 +020051#include "clockid.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020052
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030053#include <linux/ctype.h>
Arnaldo Carvalho de Melofb71c86c2019-09-03 10:56:06 -030054#include <internal/lib.h>
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030055
Stephane Eranian73323f52012-02-02 13:54:44 +010056/*
57 * magic2 = "PERFILE2"
58 * must be a numerical value to let the endianness
59 * determine the memory layout. That way we are able
60 * to detect endianness when reading the perf.data file
61 * back.
62 *
63 * we check for legacy (PERFFILE) format.
64 */
65static const char *__perf_magic1 = "PERFFILE";
66static const u64 __perf_magic2 = 0x32454c4946524550ULL;
67static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020068
Stephane Eranian73323f52012-02-02 13:54:44 +010069#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020070
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090071const char perf_version_string[] = PERF_VERSION;
72
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020073struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020074 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020075 struct perf_file_section ids;
76};
77
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030078void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020079{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030080 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020081}
82
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030083void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020084{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030085 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020086}
87
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030088bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020089{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030090 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020091}
92
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -070093static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
94{
95 ssize_t ret = writen(ff->fd, buf, size);
96
97 if (ret != (ssize_t)size)
98 return ret < 0 ? (int)ret : -1;
99 return 0;
100}
101
102static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
103{
104 /* struct perf_event_header::size is u16 */
105 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
106 size_t new_size = ff->size;
107 void *addr;
108
109 if (size + ff->offset > max_size)
110 return -E2BIG;
111
112 while (size > (new_size - ff->offset))
113 new_size <<= 1;
114 new_size = min(max_size, new_size);
115
116 if (ff->size < new_size) {
117 addr = realloc(ff->buf, new_size);
118 if (!addr)
119 return -ENOMEM;
120 ff->buf = addr;
121 ff->size = new_size;
122 }
123
124 memcpy(ff->buf + ff->offset, buf, size);
125 ff->offset += size;
126
127 return 0;
128}
129
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700130/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700131int do_write(struct feat_fd *ff, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200132{
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700133 if (!ff->buf)
134 return __do_write_fd(ff, buf, size);
135 return __do_write_buf(ff, buf, size);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200136}
137
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700138/* Return: 0 if succeded, -ERR if failed. */
Jiri Olsae2091ce2018-03-07 16:50:08 +0100139static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
140{
141 u64 *p = (u64 *) set;
142 int i, ret;
143
144 ret = do_write(ff, &size, sizeof(size));
145 if (ret < 0)
146 return ret;
147
148 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
149 ret = do_write(ff, p + i, sizeof(*p));
150 if (ret < 0)
151 return ret;
152 }
153
154 return 0;
155}
156
157/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700158int write_padded(struct feat_fd *ff, const void *bf,
159 size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200160{
161 static const char zero_buf[NAME_ALIGN];
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700162 int err = do_write(ff, bf, count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200163
164 if (!err)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700165 err = do_write(ff, zero_buf, count_aligned - count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200166
167 return err;
168}
169
Kan Liang2bb00d22015-09-01 09:58:12 -0400170#define string_size(str) \
171 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
172
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700173/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700174static int do_write_string(struct feat_fd *ff, const char *str)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200175{
176 u32 len, olen;
177 int ret;
178
179 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300180 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200181
182 /* write len, incl. \0 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700183 ret = do_write(ff, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200184 if (ret < 0)
185 return ret;
186
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700187 return write_padded(ff, str, olen, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200188}
189
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700190static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700191{
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700192 ssize_t ret = readn(ff->fd, addr, size);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700193
194 if (ret != size)
195 return ret < 0 ? (int)ret : -1;
196 return 0;
197}
198
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700199static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
200{
201 if (size > (ssize_t)ff->size - ff->offset)
202 return -1;
203
204 memcpy(addr, ff->buf + ff->offset, size);
205 ff->offset += size;
206
207 return 0;
208
209}
210
211static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
212{
213 if (!ff->buf)
214 return __do_read_fd(ff, addr, size);
215 return __do_read_buf(ff, addr, size);
216}
217
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700218static int do_read_u32(struct feat_fd *ff, u32 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700219{
220 int ret;
221
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700222 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700223 if (ret)
224 return ret;
225
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700226 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700227 *addr = bswap_32(*addr);
228 return 0;
229}
230
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700231static int do_read_u64(struct feat_fd *ff, u64 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700232{
233 int ret;
234
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700235 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700236 if (ret)
237 return ret;
238
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700239 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700240 *addr = bswap_64(*addr);
241 return 0;
242}
243
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700244static char *do_read_string(struct feat_fd *ff)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200245{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200246 u32 len;
247 char *buf;
248
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700249 if (do_read_u32(ff, &len))
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200250 return NULL;
251
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200252 buf = malloc(len);
253 if (!buf)
254 return NULL;
255
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700256 if (!__do_read(ff, buf, len)) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200257 /*
258 * strings are padded by zeroes
259 * thus the actual strlen of buf
260 * may be less than len
261 */
262 return buf;
263 }
264
265 free(buf);
266 return NULL;
267}
268
Jiri Olsae2091ce2018-03-07 16:50:08 +0100269/* Return: 0 if succeded, -ERR if failed. */
270static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
271{
272 unsigned long *set;
273 u64 size, *p;
274 int i, ret;
275
276 ret = do_read_u64(ff, &size);
277 if (ret)
278 return ret;
279
280 set = bitmap_alloc(size);
281 if (!set)
282 return -ENOMEM;
283
Jiri Olsae2091ce2018-03-07 16:50:08 +0100284 p = (u64 *) set;
285
286 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
287 ret = do_read_u64(ff, p + i);
288 if (ret < 0) {
289 free(set);
290 return ret;
291 }
292 }
293
294 *pset = set;
295 *psize = size;
296 return 0;
297}
298
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700299static int write_tracing_data(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200300 struct evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200301{
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700302 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
303 return -1;
304
Jiri Olsace9036a2019-07-21 13:24:23 +0200305 return read_tracing_data(ff->fd, &evlist->core.entries);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200306}
307
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700308static int write_build_id(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200309 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200310{
311 struct perf_session *session;
312 int err;
313
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700314 session = container_of(ff->ph, struct perf_session, header);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200315
Robert Richtere20960c2011-12-07 10:02:55 +0100316 if (!perf_session__read_build_ids(session, true))
317 return -1;
318
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700319 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
320 return -1;
321
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700322 err = perf_session__write_buildid_table(session, ff);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200323 if (err < 0) {
324 pr_debug("failed to write buildid table\n");
325 return err;
326 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900327 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200328
329 return 0;
330}
331
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700332static int write_hostname(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200333 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200334{
335 struct utsname uts;
336 int ret;
337
338 ret = uname(&uts);
339 if (ret < 0)
340 return -1;
341
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700342 return do_write_string(ff, uts.nodename);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200343}
344
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700345static int write_osrelease(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200346 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200347{
348 struct utsname uts;
349 int ret;
350
351 ret = uname(&uts);
352 if (ret < 0)
353 return -1;
354
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700355 return do_write_string(ff, uts.release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200356}
357
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700358static int write_arch(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200359 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200360{
361 struct utsname uts;
362 int ret;
363
364 ret = uname(&uts);
365 if (ret < 0)
366 return -1;
367
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700368 return do_write_string(ff, uts.machine);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200369}
370
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700371static int write_version(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200372 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200373{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700374 return do_write_string(ff, perf_version_string);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200375}
376
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700377static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200378{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200379 FILE *file;
380 char *buf = NULL;
381 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800382 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200383 size_t len = 0;
384 int ret = -1;
385
386 if (!search)
387 return -1;
388
389 file = fopen("/proc/cpuinfo", "r");
390 if (!file)
391 return -1;
392
393 while (getline(&buf, &len, file) > 0) {
394 ret = strncmp(buf, search, strlen(search));
395 if (!ret)
396 break;
397 }
398
Wang Naned307752014-10-16 11:08:29 +0800399 if (ret) {
400 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200401 goto done;
Wang Naned307752014-10-16 11:08:29 +0800402 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200403
404 s = buf;
405
406 p = strchr(buf, ':');
407 if (p && *(p+1) == ' ' && *(p+2))
408 s = p + 2;
409 p = strchr(s, '\n');
410 if (p)
411 *p = '\0';
412
413 /* squash extra space characters (branding string) */
414 p = s;
415 while (*p) {
416 if (isspace(*p)) {
417 char *r = p + 1;
Arnaldo Carvalho de Melofc6a1722019-06-25 21:33:14 -0300418 char *q = skip_spaces(r);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200419 *p = ' ';
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200420 if (q != (p+1))
421 while ((*r++ = *q++));
422 }
423 p++;
424 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700425 ret = do_write_string(ff, s);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200426done:
427 free(buf);
428 fclose(file);
429 return ret;
430}
431
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700432static int write_cpudesc(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200433 struct evlist *evlist __maybe_unused)
Wang Nan493c3032014-10-24 09:45:26 +0800434{
Arnaldo Carvalho de Meloa7749402019-08-29 14:40:28 -0300435#if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__)
436#define CPUINFO_PROC { "cpu", }
437#elif defined(__s390__)
438#define CPUINFO_PROC { "vendor_id", }
439#elif defined(__sh__)
440#define CPUINFO_PROC { "cpu type", }
441#elif defined(__alpha__) || defined(__mips__)
442#define CPUINFO_PROC { "cpu model", }
443#elif defined(__arm__)
444#define CPUINFO_PROC { "model name", "Processor", }
445#elif defined(__arc__)
446#define CPUINFO_PROC { "Processor", }
447#elif defined(__xtensa__)
448#define CPUINFO_PROC { "core ID", }
449#else
450#define CPUINFO_PROC { "model name", }
451#endif
Wang Nan493c3032014-10-24 09:45:26 +0800452 const char *cpuinfo_procs[] = CPUINFO_PROC;
Arnaldo Carvalho de Meloa7749402019-08-29 14:40:28 -0300453#undef CPUINFO_PROC
Wang Nan493c3032014-10-24 09:45:26 +0800454 unsigned int i;
455
456 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
457 int ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700458 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
Wang Nan493c3032014-10-24 09:45:26 +0800459 if (ret >= 0)
460 return ret;
461 }
462 return -1;
463}
464
465
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700466static int write_nrcpus(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200467 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200468{
469 long nr;
470 u32 nrc, nra;
471 int ret;
472
Jan Stancekda8a58b2017-02-17 12:10:26 +0100473 nrc = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200474
475 nr = sysconf(_SC_NPROCESSORS_ONLN);
476 if (nr < 0)
477 return -1;
478
479 nra = (u32)(nr & UINT_MAX);
480
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700481 ret = do_write(ff, &nrc, sizeof(nrc));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200482 if (ret < 0)
483 return ret;
484
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700485 return do_write(ff, &nra, sizeof(nra));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200486}
487
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700488static int write_event_desc(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200489 struct evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200490{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200491 struct evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900492 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200493 int ret;
494
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200495 nre = evlist->core.nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200496
497 /*
498 * write number of events
499 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700500 ret = do_write(ff, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200501 if (ret < 0)
502 return ret;
503
504 /*
505 * size of perf_event_attr struct
506 */
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200507 sz = (u32)sizeof(evsel->core.attr);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700508 ret = do_write(ff, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200509 if (ret < 0)
510 return ret;
511
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300512 evlist__for_each_entry(evlist, evsel) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200513 ret = do_write(ff, &evsel->core.attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200514 if (ret < 0)
515 return ret;
516 /*
517 * write number of unique id per event
518 * there is one id per instance of an event
519 *
520 * copy into an nri to be independent of the
521 * type of ids,
522 */
Jiri Olsae7eb9002019-09-02 22:15:47 +0200523 nri = evsel->core.ids;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700524 ret = do_write(ff, &nri, sizeof(nri));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200525 if (ret < 0)
526 return ret;
527
528 /*
529 * write event string as passed on cmdline
530 */
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -0300531 ret = do_write_string(ff, evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200532 if (ret < 0)
533 return ret;
534 /*
535 * write unique ids for this event
536 */
Jiri Olsae7eb9002019-09-02 22:15:47 +0200537 ret = do_write(ff, evsel->core.id, evsel->core.ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200538 if (ret < 0)
539 return ret;
540 }
541 return 0;
542}
543
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700544static int write_cmdline(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200545 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200546{
Andi Kleen94816ad2019-02-24 07:37:19 -0800547 char pbuf[MAXPATHLEN], *buf;
548 int i, ret, n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200549
Tommi Rantala55f771282017-03-22 15:06:24 +0200550 /* actual path to perf binary */
Andi Kleen94816ad2019-02-24 07:37:19 -0800551 buf = perf_exe(pbuf, MAXPATHLEN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200552
553 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300554 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200555
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700556 ret = do_write(ff, &n, sizeof(n));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200557 if (ret < 0)
558 return ret;
559
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700560 ret = do_write_string(ff, buf);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200561 if (ret < 0)
562 return ret;
563
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300564 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700565 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200566 if (ret < 0)
567 return ret;
568 }
569 return 0;
570}
571
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200572
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700573static int write_cpu_topology(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200574 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200575{
Jiri Olsa5135d5e2019-02-19 10:58:13 +0100576 struct cpu_topology *tp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200577 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300578 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200579
Jiri Olsa5135d5e2019-02-19 10:58:13 +0100580 tp = cpu_topology__new();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200581 if (!tp)
582 return -1;
583
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700584 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200585 if (ret < 0)
586 goto done;
587
588 for (i = 0; i < tp->core_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700589 ret = do_write_string(ff, tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200590 if (ret < 0)
591 goto done;
592 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700593 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200594 if (ret < 0)
595 goto done;
596
597 for (i = 0; i < tp->thread_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700598 ret = do_write_string(ff, tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200599 if (ret < 0)
600 break;
601 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400602
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300603 ret = perf_env__read_cpu_topology_map(&perf_env);
604 if (ret < 0)
605 goto done;
606
607 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700608 ret = do_write(ff, &perf_env.cpu[j].core_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300609 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400610 if (ret < 0)
611 return ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700612 ret = do_write(ff, &perf_env.cpu[j].socket_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300613 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400614 if (ret < 0)
615 return ret;
616 }
Kan Liangacae8b32019-06-04 15:50:41 -0700617
618 if (!tp->die_sib)
619 goto done;
620
621 ret = do_write(ff, &tp->die_sib, sizeof(tp->die_sib));
622 if (ret < 0)
623 goto done;
624
625 for (i = 0; i < tp->die_sib; i++) {
626 ret = do_write_string(ff, tp->die_siblings[i]);
627 if (ret < 0)
628 goto done;
629 }
630
631 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
632 ret = do_write(ff, &perf_env.cpu[j].die_id,
633 sizeof(perf_env.cpu[j].die_id));
634 if (ret < 0)
635 return ret;
636 }
637
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200638done:
Jiri Olsa5135d5e2019-02-19 10:58:13 +0100639 cpu_topology__delete(tp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200640 return ret;
641}
642
643
644
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700645static int write_total_mem(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200646 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200647{
648 char *buf = NULL;
649 FILE *fp;
650 size_t len = 0;
651 int ret = -1, n;
652 uint64_t mem;
653
654 fp = fopen("/proc/meminfo", "r");
655 if (!fp)
656 return -1;
657
658 while (getline(&buf, &len, fp) > 0) {
659 ret = strncmp(buf, "MemTotal:", 9);
660 if (!ret)
661 break;
662 }
663 if (!ret) {
664 n = sscanf(buf, "%*s %"PRIu64, &mem);
665 if (n == 1)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700666 ret = do_write(ff, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800667 } else
668 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200669 free(buf);
670 fclose(fp);
671 return ret;
672}
673
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700674static int write_numa_topology(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200675 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200676{
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100677 struct numa_topology *tp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200678 int ret = -1;
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100679 u32 i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200680
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100681 tp = numa_topology__new();
682 if (!tp)
683 return -ENOMEM;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200684
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100685 ret = do_write(ff, &tp->nr, sizeof(u32));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200686 if (ret < 0)
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100687 goto err;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200688
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100689 for (i = 0; i < tp->nr; i++) {
690 struct numa_topology_node *n = &tp->nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200691
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100692 ret = do_write(ff, &n->node, sizeof(u32));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200693 if (ret < 0)
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100694 goto err;
695
696 ret = do_write(ff, &n->mem_total, sizeof(u64));
697 if (ret)
698 goto err;
699
700 ret = do_write(ff, &n->mem_free, sizeof(u64));
701 if (ret)
702 goto err;
703
704 ret = do_write_string(ff, n->cpus);
705 if (ret < 0)
706 goto err;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200707 }
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100708
709 ret = 0;
710
711err:
712 numa_topology__delete(tp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200713 return ret;
714}
715
716/*
Robert Richter50a96672012-08-16 21:10:24 +0200717 * File format:
718 *
719 * struct pmu_mappings {
720 * u32 pmu_num;
721 * struct pmu_map {
722 * u32 type;
723 * char name[];
724 * }[pmu_num];
725 * };
726 */
727
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700728static int write_pmu_mappings(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200729 struct evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200730{
731 struct perf_pmu *pmu = NULL;
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700732 u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900733 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200734
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700735 /*
736 * Do a first pass to count number of pmu to avoid lseek so this
737 * works in pipe mode as well.
738 */
739 while ((pmu = perf_pmu__scan(pmu))) {
740 if (!pmu->name)
741 continue;
742 pmu_num++;
743 }
744
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700745 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
Namhyung Kim5323f602012-12-17 15:38:54 +0900746 if (ret < 0)
747 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200748
749 while ((pmu = perf_pmu__scan(pmu))) {
750 if (!pmu->name)
751 continue;
Namhyung Kim5323f602012-12-17 15:38:54 +0900752
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700753 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
Namhyung Kim5323f602012-12-17 15:38:54 +0900754 if (ret < 0)
755 return ret;
756
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700757 ret = do_write_string(ff, pmu->name);
Namhyung Kim5323f602012-12-17 15:38:54 +0900758 if (ret < 0)
759 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200760 }
761
Robert Richter50a96672012-08-16 21:10:24 +0200762 return 0;
763}
764
765/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900766 * File format:
767 *
768 * struct group_descs {
769 * u32 nr_groups;
770 * struct group_desc {
771 * char name[];
772 * u32 leader_idx;
773 * u32 nr_members;
774 * }[nr_groups];
775 * };
776 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700777static int write_group_desc(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200778 struct evlist *evlist)
Namhyung Kima8bb5592013-01-22 18:09:31 +0900779{
780 u32 nr_groups = evlist->nr_groups;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200781 struct evsel *evsel;
Namhyung Kima8bb5592013-01-22 18:09:31 +0900782 int ret;
783
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700784 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900785 if (ret < 0)
786 return ret;
787
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300788 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -0300789 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900790 const char *name = evsel->group_name ?: "{anon_group}";
791 u32 leader_idx = evsel->idx;
Jiri Olsa5643b1a2019-07-21 13:24:46 +0200792 u32 nr_members = evsel->core.nr_members;
Namhyung Kima8bb5592013-01-22 18:09:31 +0900793
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700794 ret = do_write_string(ff, name);
Namhyung Kima8bb5592013-01-22 18:09:31 +0900795 if (ret < 0)
796 return ret;
797
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700798 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900799 if (ret < 0)
800 return ret;
801
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700802 ret = do_write(ff, &nr_members, sizeof(nr_members));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900803 if (ret < 0)
804 return ret;
805 }
806 }
807 return 0;
808}
809
810/*
Kan Liangf4a07422018-11-21 08:49:39 -0800811 * Return the CPU id as a raw string.
812 *
813 * Each architecture should provide a more precise id string that
814 * can be use to match the architecture's "mapfile".
815 */
816char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
817{
818 return NULL;
819}
820
821/* Return zero when the cpuid from the mapfile.csv matches the
822 * cpuid string generated on this platform.
823 * Otherwise return non-zero.
824 */
825int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
826{
827 regex_t re;
828 regmatch_t pmatch[1];
829 int match;
830
831 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
832 /* Warn unable to generate match particular string. */
833 pr_info("Invalid regular expression %s\n", mapcpuid);
834 return 1;
835 }
836
837 match = !regexec(&re, cpuid, 1, pmatch, 0);
838 regfree(&re);
839 if (match) {
840 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
841
842 /* Verify the entire string matched. */
843 if (match_len == strlen(cpuid))
844 return 0;
845 }
846 return 1;
847}
848
849/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200850 * default get_cpuid(): nothing gets recorded
Jiada Wang7a759cd2017-04-09 20:02:37 -0700851 * actual implementation must be in arch/$(SRCARCH)/util/header.c
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200852 */
Rui Teng11d8f872016-07-28 10:05:57 +0800853int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200854{
Arnaldo Carvalho de Melo05267c72019-12-11 10:09:24 -0300855 return ENOSYS; /* Not implemented */
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200856}
857
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700858static int write_cpuid(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200859 struct evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200860{
861 char buffer[64];
862 int ret;
863
864 ret = get_cpuid(buffer, sizeof(buffer));
Jiri Olsaa9aeb872019-02-13 13:32:43 +0100865 if (ret)
866 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200867
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700868 return do_write_string(ff, buffer);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200869}
870
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700871static int write_branch_stack(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +0200872 struct evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100873{
874 return 0;
875}
876
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700877static int write_auxtrace(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200878 struct evlist *evlist __maybe_unused)
Adrian Hunter4025ea42015-04-09 18:53:41 +0300879{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300880 struct perf_session *session;
881 int err;
882
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700883 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
884 return -1;
885
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700886 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300887
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700888 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300889 if (err < 0)
890 pr_err("Failed to write auxtrace index\n");
891 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300892}
893
Alexey Budankovcf790512018-10-09 17:36:24 +0300894static int write_clockid(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200895 struct evlist *evlist __maybe_unused)
Alexey Budankovcf790512018-10-09 17:36:24 +0300896{
Jiri Olsa9d88a1a12020-08-05 11:34:41 +0200897 return do_write(ff, &ff->ph->env.clock.clockid_res_ns,
898 sizeof(ff->ph->env.clock.clockid_res_ns));
Alexey Budankovcf790512018-10-09 17:36:24 +0300899}
900
Jiri Olsad1e325c2020-08-05 11:34:40 +0200901static int write_clock_data(struct feat_fd *ff,
902 struct evlist *evlist __maybe_unused)
903{
904 u64 *data64;
905 u32 data32;
906 int ret;
907
908 /* version */
909 data32 = 1;
910
911 ret = do_write(ff, &data32, sizeof(data32));
912 if (ret < 0)
913 return ret;
914
915 /* clockid */
916 data32 = ff->ph->env.clock.clockid;
917
918 ret = do_write(ff, &data32, sizeof(data32));
919 if (ret < 0)
920 return ret;
921
922 /* TOD ref time */
923 data64 = &ff->ph->env.clock.tod_ns;
924
925 ret = do_write(ff, data64, sizeof(*data64));
926 if (ret < 0)
927 return ret;
928
929 /* clockid ref time */
930 data64 = &ff->ph->env.clock.clockid_ns;
931
932 return do_write(ff, data64, sizeof(*data64));
933}
934
Jiri Olsa258031c2019-03-08 14:47:39 +0100935static int write_dir_format(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200936 struct evlist *evlist __maybe_unused)
Jiri Olsa258031c2019-03-08 14:47:39 +0100937{
938 struct perf_session *session;
939 struct perf_data *data;
940
941 session = container_of(ff->ph, struct perf_session, header);
942 data = session->data;
943
944 if (WARN_ON(!perf_data__is_dir(data)))
945 return -1;
946
947 return do_write(ff, &data->dir.version, sizeof(data->dir.version));
948}
949
Song Liu606f9722019-03-11 22:30:43 -0700950#ifdef HAVE_LIBBPF_SUPPORT
951static int write_bpf_prog_info(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200952 struct evlist *evlist __maybe_unused)
Song Liu606f9722019-03-11 22:30:43 -0700953{
954 struct perf_env *env = &ff->ph->env;
955 struct rb_root *root;
956 struct rb_node *next;
957 int ret;
958
959 down_read(&env->bpf_progs.lock);
960
961 ret = do_write(ff, &env->bpf_progs.infos_cnt,
962 sizeof(env->bpf_progs.infos_cnt));
963 if (ret < 0)
964 goto out;
965
966 root = &env->bpf_progs.infos;
967 next = rb_first(root);
968 while (next) {
969 struct bpf_prog_info_node *node;
970 size_t len;
971
972 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
973 next = rb_next(&node->rb_node);
974 len = sizeof(struct bpf_prog_info_linear) +
975 node->info_linear->data_len;
976
977 /* before writing to file, translate address to offset */
978 bpf_program__bpil_addr_to_offs(node->info_linear);
979 ret = do_write(ff, node->info_linear, len);
980 /*
981 * translate back to address even when do_write() fails,
982 * so that this function never changes the data.
983 */
984 bpf_program__bpil_offs_to_addr(node->info_linear);
985 if (ret < 0)
986 goto out;
987 }
988out:
989 up_read(&env->bpf_progs.lock);
990 return ret;
991}
Song Liu606f9722019-03-11 22:30:43 -0700992
Song Liua70a112312019-03-11 22:30:45 -0700993static int write_bpf_btf(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +0200994 struct evlist *evlist __maybe_unused)
Song Liua70a112312019-03-11 22:30:45 -0700995{
996 struct perf_env *env = &ff->ph->env;
997 struct rb_root *root;
998 struct rb_node *next;
999 int ret;
1000
1001 down_read(&env->bpf_progs.lock);
1002
1003 ret = do_write(ff, &env->bpf_progs.btfs_cnt,
1004 sizeof(env->bpf_progs.btfs_cnt));
1005
1006 if (ret < 0)
1007 goto out;
1008
1009 root = &env->bpf_progs.btfs;
1010 next = rb_first(root);
1011 while (next) {
1012 struct btf_node *node;
1013
1014 node = rb_entry(next, struct btf_node, rb_node);
1015 next = rb_next(&node->rb_node);
1016 ret = do_write(ff, &node->id,
1017 sizeof(u32) * 2 + node->data_size);
1018 if (ret < 0)
1019 goto out;
1020 }
1021out:
1022 up_read(&env->bpf_progs.lock);
1023 return ret;
1024}
Arnaldo Carvalho de Meloef0580e2020-10-20 15:57:21 -03001025#endif // HAVE_LIBBPF_SUPPORT
Song Liua70a112312019-03-11 22:30:45 -07001026
Jiri Olsa720e98b2016-02-16 16:01:43 +01001027static int cpu_cache_level__sort(const void *a, const void *b)
1028{
1029 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
1030 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1031
1032 return cache_a->level - cache_b->level;
1033}
1034
1035static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1036{
1037 if (a->level != b->level)
1038 return false;
1039
1040 if (a->line_size != b->line_size)
1041 return false;
1042
1043 if (a->sets != b->sets)
1044 return false;
1045
1046 if (a->ways != b->ways)
1047 return false;
1048
1049 if (strcmp(a->type, b->type))
1050 return false;
1051
1052 if (strcmp(a->size, b->size))
1053 return false;
1054
1055 if (strcmp(a->map, b->map))
1056 return false;
1057
1058 return true;
1059}
1060
1061static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1062{
1063 char path[PATH_MAX], file[PATH_MAX];
1064 struct stat st;
1065 size_t len;
1066
1067 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1068 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1069
1070 if (stat(file, &st))
1071 return 1;
1072
1073 scnprintf(file, PATH_MAX, "%s/level", path);
1074 if (sysfs__read_int(file, (int *) &cache->level))
1075 return -1;
1076
1077 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1078 if (sysfs__read_int(file, (int *) &cache->line_size))
1079 return -1;
1080
1081 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1082 if (sysfs__read_int(file, (int *) &cache->sets))
1083 return -1;
1084
1085 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1086 if (sysfs__read_int(file, (int *) &cache->ways))
1087 return -1;
1088
1089 scnprintf(file, PATH_MAX, "%s/type", path);
1090 if (sysfs__read_str(file, &cache->type, &len))
1091 return -1;
1092
1093 cache->type[len] = 0;
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -03001094 cache->type = strim(cache->type);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001095
1096 scnprintf(file, PATH_MAX, "%s/size", path);
1097 if (sysfs__read_str(file, &cache->size, &len)) {
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001098 zfree(&cache->type);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001099 return -1;
1100 }
1101
1102 cache->size[len] = 0;
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -03001103 cache->size = strim(cache->size);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001104
1105 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1106 if (sysfs__read_str(file, &cache->map, &len)) {
Jiri Olsa02162342019-09-12 12:52:35 +02001107 zfree(&cache->size);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001108 zfree(&cache->type);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001109 return -1;
1110 }
1111
1112 cache->map[len] = 0;
Arnaldo Carvalho de Melo13c230a2019-06-26 12:13:13 -03001113 cache->map = strim(cache->map);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001114 return 0;
1115}
1116
1117static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1118{
1119 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1120}
1121
Michael Petlan28707822019-12-08 17:20:56 +01001122#define MAX_CACHE_LVL 4
1123
1124static int build_caches(struct cpu_cache_level caches[], u32 *cntp)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001125{
1126 u32 i, cnt = 0;
Jiri Olsa720e98b2016-02-16 16:01:43 +01001127 u32 nr, cpu;
1128 u16 level;
1129
Michael Petlan28707822019-12-08 17:20:56 +01001130 nr = cpu__max_cpu();
Jiri Olsa720e98b2016-02-16 16:01:43 +01001131
1132 for (cpu = 0; cpu < nr; cpu++) {
Michael Petlan28707822019-12-08 17:20:56 +01001133 for (level = 0; level < MAX_CACHE_LVL; level++) {
Jiri Olsa720e98b2016-02-16 16:01:43 +01001134 struct cpu_cache_level c;
1135 int err;
1136
1137 err = cpu_cache_level__read(&c, cpu, level);
1138 if (err < 0)
1139 return err;
1140
1141 if (err == 1)
1142 break;
1143
1144 for (i = 0; i < cnt; i++) {
1145 if (cpu_cache_level__cmp(&c, &caches[i]))
1146 break;
1147 }
1148
1149 if (i == cnt)
1150 caches[cnt++] = c;
1151 else
1152 cpu_cache_level__free(&c);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001153 }
1154 }
Jiri Olsa720e98b2016-02-16 16:01:43 +01001155 *cntp = cnt;
1156 return 0;
1157}
1158
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001159static int write_cache(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +02001160 struct evlist *evlist __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001161{
Kyle Meyerdc841872019-08-27 16:43:51 -05001162 u32 max_caches = cpu__max_cpu() * MAX_CACHE_LVL;
1163 struct cpu_cache_level caches[max_caches];
Jiri Olsa720e98b2016-02-16 16:01:43 +01001164 u32 cnt = 0, i, version = 1;
1165 int ret;
1166
Michael Petlan28707822019-12-08 17:20:56 +01001167 ret = build_caches(caches, &cnt);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001168 if (ret)
1169 goto out;
1170
1171 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1172
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001173 ret = do_write(ff, &version, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001174 if (ret < 0)
1175 goto out;
1176
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001177 ret = do_write(ff, &cnt, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001178 if (ret < 0)
1179 goto out;
1180
1181 for (i = 0; i < cnt; i++) {
1182 struct cpu_cache_level *c = &caches[i];
1183
1184 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001185 ret = do_write(ff, &c->v, sizeof(u32)); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001186 if (ret < 0) \
1187 goto out;
1188
1189 _W(level)
1190 _W(line_size)
1191 _W(sets)
1192 _W(ways)
1193 #undef _W
1194
1195 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001196 ret = do_write_string(ff, (const char *) c->v); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001197 if (ret < 0) \
1198 goto out;
1199
1200 _W(type)
1201 _W(size)
1202 _W(map)
1203 #undef _W
1204 }
1205
1206out:
1207 for (i = 0; i < cnt; i++)
1208 cpu_cache_level__free(&caches[i]);
1209 return ret;
1210}
1211
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001212static int write_stat(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +02001213 struct evlist *evlist __maybe_unused)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001214{
1215 return 0;
1216}
1217
Jin Yao60115182017-12-08 21:13:41 +08001218static int write_sample_time(struct feat_fd *ff,
Jiri Olsa63503db2019-07-21 13:23:52 +02001219 struct evlist *evlist)
Jin Yao60115182017-12-08 21:13:41 +08001220{
1221 int ret;
1222
1223 ret = do_write(ff, &evlist->first_sample_time,
1224 sizeof(evlist->first_sample_time));
1225 if (ret < 0)
1226 return ret;
1227
1228 return do_write(ff, &evlist->last_sample_time,
1229 sizeof(evlist->last_sample_time));
1230}
1231
Jiri Olsae2091ce2018-03-07 16:50:08 +01001232
1233static int memory_node__read(struct memory_node *n, unsigned long idx)
1234{
1235 unsigned int phys, size = 0;
1236 char path[PATH_MAX];
1237 struct dirent *ent;
1238 DIR *dir;
1239
1240#define for_each_memory(mem, dir) \
1241 while ((ent = readdir(dir))) \
1242 if (strcmp(ent->d_name, ".") && \
1243 strcmp(ent->d_name, "..") && \
1244 sscanf(ent->d_name, "memory%u", &mem) == 1)
1245
1246 scnprintf(path, PATH_MAX,
1247 "%s/devices/system/node/node%lu",
1248 sysfs__mountpoint(), idx);
1249
1250 dir = opendir(path);
1251 if (!dir) {
1252 pr_warning("failed: cant' open memory sysfs data\n");
1253 return -1;
1254 }
1255
1256 for_each_memory(phys, dir) {
1257 size = max(phys, size);
1258 }
1259
1260 size++;
1261
1262 n->set = bitmap_alloc(size);
1263 if (!n->set) {
1264 closedir(dir);
1265 return -ENOMEM;
1266 }
1267
Jiri Olsae2091ce2018-03-07 16:50:08 +01001268 n->node = idx;
1269 n->size = size;
1270
1271 rewinddir(dir);
1272
1273 for_each_memory(phys, dir) {
1274 set_bit(phys, n->set);
1275 }
1276
1277 closedir(dir);
1278 return 0;
1279}
1280
1281static int memory_node__sort(const void *a, const void *b)
1282{
1283 const struct memory_node *na = a;
1284 const struct memory_node *nb = b;
1285
1286 return na->node - nb->node;
1287}
1288
1289static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1290{
1291 char path[PATH_MAX];
1292 struct dirent *ent;
1293 DIR *dir;
1294 u64 cnt = 0;
1295 int ret = 0;
1296
1297 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1298 sysfs__mountpoint());
1299
1300 dir = opendir(path);
1301 if (!dir) {
Thomas Richter4f75f1cb2018-04-12 15:32:46 +02001302 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1303 __func__, path);
Jiri Olsae2091ce2018-03-07 16:50:08 +01001304 return -1;
1305 }
1306
1307 while (!ret && (ent = readdir(dir))) {
1308 unsigned int idx;
1309 int r;
1310
1311 if (!strcmp(ent->d_name, ".") ||
1312 !strcmp(ent->d_name, ".."))
1313 continue;
1314
1315 r = sscanf(ent->d_name, "node%u", &idx);
1316 if (r != 1)
1317 continue;
1318
1319 if (WARN_ONCE(cnt >= size,
Yunfeng Ye60807282019-10-15 16:30:08 +08001320 "failed to write MEM_TOPOLOGY, way too many nodes\n")) {
1321 closedir(dir);
Jiri Olsae2091ce2018-03-07 16:50:08 +01001322 return -1;
Yunfeng Ye60807282019-10-15 16:30:08 +08001323 }
Jiri Olsae2091ce2018-03-07 16:50:08 +01001324
1325 ret = memory_node__read(&nodes[cnt++], idx);
1326 }
1327
1328 *cntp = cnt;
1329 closedir(dir);
1330
1331 if (!ret)
1332 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1333
1334 return ret;
1335}
1336
1337#define MAX_MEMORY_NODES 2000
1338
1339/*
1340 * The MEM_TOPOLOGY holds physical memory map for every
1341 * node in system. The format of data is as follows:
1342 *
1343 * 0 - version | for future changes
1344 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1345 * 16 - count | number of nodes
1346 *
1347 * For each node we store map of physical indexes for
1348 * each node:
1349 *
1350 * 32 - node id | node index
1351 * 40 - size | size of bitmap
1352 * 48 - bitmap | bitmap of memory indexes that belongs to node
1353 */
1354static int write_mem_topology(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +02001355 struct evlist *evlist __maybe_unused)
Jiri Olsae2091ce2018-03-07 16:50:08 +01001356{
1357 static struct memory_node nodes[MAX_MEMORY_NODES];
1358 u64 bsize, version = 1, i, nr;
1359 int ret;
1360
1361 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1362 (unsigned long long *) &bsize);
1363 if (ret)
1364 return ret;
1365
1366 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1367 if (ret)
1368 return ret;
1369
1370 ret = do_write(ff, &version, sizeof(version));
1371 if (ret < 0)
1372 goto out;
1373
1374 ret = do_write(ff, &bsize, sizeof(bsize));
1375 if (ret < 0)
1376 goto out;
1377
1378 ret = do_write(ff, &nr, sizeof(nr));
1379 if (ret < 0)
1380 goto out;
1381
1382 for (i = 0; i < nr; i++) {
1383 struct memory_node *n = &nodes[i];
1384
1385 #define _W(v) \
1386 ret = do_write(ff, &n->v, sizeof(n->v)); \
1387 if (ret < 0) \
1388 goto out;
1389
1390 _W(node)
1391 _W(size)
1392
1393 #undef _W
1394
1395 ret = do_write_bitmap(ff, n->set, n->size);
1396 if (ret < 0)
1397 goto out;
1398 }
1399
1400out:
1401 return ret;
1402}
1403
Alexey Budankov42e1fd82019-03-18 20:41:33 +03001404static int write_compressed(struct feat_fd *ff __maybe_unused,
Jiri Olsa63503db2019-07-21 13:23:52 +02001405 struct evlist *evlist __maybe_unused)
Alexey Budankov42e1fd82019-03-18 20:41:33 +03001406{
1407 int ret;
1408
1409 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver));
1410 if (ret)
1411 return ret;
1412
1413 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type));
1414 if (ret)
1415 return ret;
1416
1417 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level));
1418 if (ret)
1419 return ret;
1420
1421 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio));
1422 if (ret)
1423 return ret;
1424
1425 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len));
1426}
1427
Kan Liang6f91ea22020-03-19 13:25:02 -07001428static int write_cpu_pmu_caps(struct feat_fd *ff,
1429 struct evlist *evlist __maybe_unused)
1430{
1431 struct perf_pmu *cpu_pmu = perf_pmu__find("cpu");
1432 struct perf_pmu_caps *caps = NULL;
1433 int nr_caps;
1434 int ret;
1435
1436 if (!cpu_pmu)
1437 return -ENOENT;
1438
1439 nr_caps = perf_pmu__caps_parse(cpu_pmu);
1440 if (nr_caps < 0)
1441 return nr_caps;
1442
1443 ret = do_write(ff, &nr_caps, sizeof(nr_caps));
1444 if (ret < 0)
1445 return ret;
1446
1447 list_for_each_entry(caps, &cpu_pmu->caps, list) {
1448 ret = do_write_string(ff, caps->name);
1449 if (ret < 0)
1450 return ret;
1451
1452 ret = do_write_string(ff, caps->value);
1453 if (ret < 0)
1454 return ret;
1455 }
1456
1457 return ret;
1458}
1459
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001460static void print_hostname(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001461{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001462 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001463}
1464
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001465static void print_osrelease(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001466{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001467 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001468}
1469
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001470static void print_arch(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001471{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001472 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001473}
1474
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001475static void print_cpudesc(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001476{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001477 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001478}
1479
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001480static void print_nrcpus(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001481{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001482 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1483 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001484}
1485
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001486static void print_version(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001487{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001488 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001489}
1490
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001491static void print_cmdline(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001492{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001493 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001494
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001495 nr = ff->ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001496
1497 fprintf(fp, "# cmdline : ");
1498
Alexey Budankovf92da712018-06-04 09:50:56 +03001499 for (i = 0; i < nr; i++) {
1500 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1501 if (!argv_i) {
1502 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1503 } else {
1504 char *mem = argv_i;
1505 do {
1506 char *quote = strchr(argv_i, '\'');
1507 if (!quote)
1508 break;
1509 *quote++ = '\0';
1510 fprintf(fp, "%s\\\'", argv_i);
1511 argv_i = quote;
1512 } while (1);
1513 fprintf(fp, "%s ", argv_i);
1514 free(mem);
1515 }
1516 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001517 fputc('\n', fp);
1518}
1519
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001520static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001521{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001522 struct perf_header *ph = ff->ph;
1523 int cpu_nr = ph->env.nr_cpus_avail;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001524 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001525 char *str;
1526
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001527 nr = ph->env.nr_sibling_cores;
1528 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001529
1530 for (i = 0; i < nr; i++) {
Kan Liange05a8992019-06-04 15:50:43 -07001531 fprintf(fp, "# sibling sockets : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001532 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001533 }
1534
Kan Liangacae8b32019-06-04 15:50:41 -07001535 if (ph->env.nr_sibling_dies) {
1536 nr = ph->env.nr_sibling_dies;
1537 str = ph->env.sibling_dies;
1538
1539 for (i = 0; i < nr; i++) {
1540 fprintf(fp, "# sibling dies : %s\n", str);
1541 str += strlen(str) + 1;
1542 }
1543 }
1544
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001545 nr = ph->env.nr_sibling_threads;
1546 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001547
1548 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001549 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001550 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001551 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001552
Kan Liangacae8b32019-06-04 15:50:41 -07001553 if (ph->env.nr_sibling_dies) {
1554 if (ph->env.cpu != NULL) {
1555 for (i = 0; i < cpu_nr; i++)
1556 fprintf(fp, "# CPU %d: Core ID %d, "
1557 "Die ID %d, Socket ID %d\n",
1558 i, ph->env.cpu[i].core_id,
1559 ph->env.cpu[i].die_id,
1560 ph->env.cpu[i].socket_id);
1561 } else
1562 fprintf(fp, "# Core ID, Die ID and Socket ID "
1563 "information is not available\n");
1564 } else {
1565 if (ph->env.cpu != NULL) {
1566 for (i = 0; i < cpu_nr; i++)
1567 fprintf(fp, "# CPU %d: Core ID %d, "
1568 "Socket ID %d\n",
1569 i, ph->env.cpu[i].core_id,
1570 ph->env.cpu[i].socket_id);
1571 } else
1572 fprintf(fp, "# Core ID and Socket ID "
1573 "information is not available\n");
1574 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001575}
1576
Alexey Budankovcf790512018-10-09 17:36:24 +03001577static void print_clockid(struct feat_fd *ff, FILE *fp)
1578{
1579 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
Jiri Olsa9d88a1a12020-08-05 11:34:41 +02001580 ff->ph->env.clock.clockid_res_ns * 1000);
Alexey Budankovcf790512018-10-09 17:36:24 +03001581}
1582
Jiri Olsad1e325c2020-08-05 11:34:40 +02001583static void print_clock_data(struct feat_fd *ff, FILE *fp)
1584{
1585 struct timespec clockid_ns;
1586 char tstr[64], date[64];
1587 struct timeval tod_ns;
1588 clockid_t clockid;
1589 struct tm ltime;
1590 u64 ref;
1591
1592 if (!ff->ph->env.clock.enabled) {
1593 fprintf(fp, "# reference time disabled\n");
1594 return;
1595 }
1596
1597 /* Compute TOD time. */
1598 ref = ff->ph->env.clock.tod_ns;
1599 tod_ns.tv_sec = ref / NSEC_PER_SEC;
1600 ref -= tod_ns.tv_sec * NSEC_PER_SEC;
1601 tod_ns.tv_usec = ref / NSEC_PER_USEC;
1602
1603 /* Compute clockid time. */
1604 ref = ff->ph->env.clock.clockid_ns;
1605 clockid_ns.tv_sec = ref / NSEC_PER_SEC;
1606 ref -= clockid_ns.tv_sec * NSEC_PER_SEC;
1607 clockid_ns.tv_nsec = ref;
1608
1609 clockid = ff->ph->env.clock.clockid;
1610
1611 if (localtime_r(&tod_ns.tv_sec, &ltime) == NULL)
1612 snprintf(tstr, sizeof(tstr), "<error>");
1613 else {
1614 strftime(date, sizeof(date), "%F %T", &ltime);
1615 scnprintf(tstr, sizeof(tstr), "%s.%06d",
1616 date, (int) tod_ns.tv_usec);
1617 }
1618
1619 fprintf(fp, "# clockid: %s (%u)\n", clockid_name(clockid), clockid);
1620 fprintf(fp, "# reference time: %s = %ld.%06d (TOD) = %ld.%09ld (%s)\n",
1621 tstr, tod_ns.tv_sec, (int) tod_ns.tv_usec,
1622 clockid_ns.tv_sec, clockid_ns.tv_nsec,
1623 clockid_name(clockid));
1624}
1625
Jiri Olsa258031c2019-03-08 14:47:39 +01001626static void print_dir_format(struct feat_fd *ff, FILE *fp)
1627{
1628 struct perf_session *session;
1629 struct perf_data *data;
1630
1631 session = container_of(ff->ph, struct perf_session, header);
1632 data = session->data;
1633
1634 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version);
1635}
1636
Arnaldo Carvalho de Meloef0580e2020-10-20 15:57:21 -03001637#ifdef HAVE_LIBBPF_SUPPORT
Song Liu606f9722019-03-11 22:30:43 -07001638static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp)
1639{
1640 struct perf_env *env = &ff->ph->env;
1641 struct rb_root *root;
1642 struct rb_node *next;
1643
1644 down_read(&env->bpf_progs.lock);
1645
1646 root = &env->bpf_progs.infos;
1647 next = rb_first(root);
1648
1649 while (next) {
1650 struct bpf_prog_info_node *node;
1651
1652 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1653 next = rb_next(&node->rb_node);
Song Liuf8dfeae2019-03-19 09:54:54 -07001654
1655 bpf_event__print_bpf_prog_info(&node->info_linear->info,
1656 env, fp);
Song Liu606f9722019-03-11 22:30:43 -07001657 }
1658
1659 up_read(&env->bpf_progs.lock);
1660}
1661
Song Liua70a112312019-03-11 22:30:45 -07001662static void print_bpf_btf(struct feat_fd *ff, FILE *fp)
1663{
1664 struct perf_env *env = &ff->ph->env;
1665 struct rb_root *root;
1666 struct rb_node *next;
1667
1668 down_read(&env->bpf_progs.lock);
1669
1670 root = &env->bpf_progs.btfs;
1671 next = rb_first(root);
1672
1673 while (next) {
1674 struct btf_node *node;
1675
1676 node = rb_entry(next, struct btf_node, rb_node);
1677 next = rb_next(&node->rb_node);
1678 fprintf(fp, "# btf info of id %u\n", node->id);
1679 }
1680
1681 up_read(&env->bpf_progs.lock);
1682}
Arnaldo Carvalho de Meloef0580e2020-10-20 15:57:21 -03001683#endif // HAVE_LIBBPF_SUPPORT
Song Liua70a112312019-03-11 22:30:45 -07001684
Jiri Olsa32dcd022019-07-21 13:23:51 +02001685static void free_event_desc(struct evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001686{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001687 struct evsel *evsel;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001688
1689 if (!events)
1690 return;
1691
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001692 for (evsel = events; evsel->core.attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001693 zfree(&evsel->name);
Jiri Olsadeaf3212019-09-02 22:12:26 +02001694 zfree(&evsel->core.id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001695 }
1696
1697 free(events);
1698}
1699
Kan Liang277ce1e2020-02-28 08:30:02 -08001700static bool perf_attr_check(struct perf_event_attr *attr)
1701{
1702 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) {
1703 pr_warning("Reserved bits are set unexpectedly. "
1704 "Please update perf tool.\n");
1705 return false;
1706 }
1707
1708 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) {
1709 pr_warning("Unknown sample type (0x%llx) is detected. "
1710 "Please update perf tool.\n",
1711 attr->sample_type);
1712 return false;
1713 }
1714
1715 if (attr->read_format & ~(PERF_FORMAT_MAX-1)) {
1716 pr_warning("Unknown read format (0x%llx) is detected. "
1717 "Please update perf tool.\n",
1718 attr->read_format);
1719 return false;
1720 }
1721
1722 if ((attr->sample_type & PERF_SAMPLE_BRANCH_STACK) &&
1723 (attr->branch_sample_type & ~(PERF_SAMPLE_BRANCH_MAX-1))) {
1724 pr_warning("Unknown branch sample type (0x%llx) is detected. "
1725 "Please update perf tool.\n",
1726 attr->branch_sample_type);
1727
1728 return false;
1729 }
1730
1731 return true;
1732}
1733
Jiri Olsa32dcd022019-07-21 13:23:51 +02001734static struct evsel *read_event_desc(struct feat_fd *ff)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001735{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001736 struct evsel *evsel, *events = NULL;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001737 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001738 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001739 u32 nre, sz, nr, i, j;
Stephane Eranian62db9062012-02-09 23:21:07 +01001740 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001741
1742 /* number of events */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001743 if (do_read_u32(ff, &nre))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001744 goto error;
1745
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001746 if (do_read_u32(ff, &sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001747 goto error;
1748
Stephane Eranian62db9062012-02-09 23:21:07 +01001749 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001750 buf = malloc(sz);
1751 if (!buf)
1752 goto error;
1753
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001754 /* the last event terminates with evsel->core.attr.size == 0: */
Robert Richter4e1b9c62012-08-16 21:10:22 +02001755 events = calloc(nre + 1, sizeof(*events));
1756 if (!events)
1757 goto error;
1758
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001759 msz = sizeof(evsel->core.attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001760 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001761 msz = sz;
1762
Robert Richter4e1b9c62012-08-16 21:10:22 +02001763 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1764 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001765
Stephane Eranian62db9062012-02-09 23:21:07 +01001766 /*
1767 * must read entire on-file attr struct to
1768 * sync up with layout.
1769 */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001770 if (__do_read(ff, buf, sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001771 goto error;
1772
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001773 if (ff->ph->needs_swap)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001774 perf_event__attr_swap(buf);
1775
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001776 memcpy(&evsel->core.attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001777
Kan Liang277ce1e2020-02-28 08:30:02 -08001778 if (!perf_attr_check(&evsel->core.attr))
1779 goto error;
1780
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001781 if (do_read_u32(ff, &nr))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001782 goto error;
1783
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001784 if (ff->ph->needs_swap)
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001785 evsel->needs_swap = true;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001786
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001787 evsel->name = do_read_string(ff);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001788 if (!evsel->name)
1789 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001790
1791 if (!nr)
1792 continue;
1793
1794 id = calloc(nr, sizeof(*id));
1795 if (!id)
1796 goto error;
Jiri Olsae7eb9002019-09-02 22:15:47 +02001797 evsel->core.ids = nr;
Jiri Olsadeaf3212019-09-02 22:12:26 +02001798 evsel->core.id = id;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001799
1800 for (j = 0 ; j < nr; j++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001801 if (do_read_u64(ff, id))
Robert Richter4e1b9c62012-08-16 21:10:22 +02001802 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001803 id++;
1804 }
1805 }
1806out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001807 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001808 return events;
1809error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001810 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001811 events = NULL;
1812 goto out;
1813}
1814
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001815static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001816 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001817{
1818 return fprintf(fp, ", %s = %s", name, val);
1819}
1820
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001821static void print_event_desc(struct feat_fd *ff, FILE *fp)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001822{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001823 struct evsel *evsel, *events;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001824 u32 j;
1825 u64 *id;
1826
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001827 if (ff->events)
1828 events = ff->events;
1829 else
1830 events = read_event_desc(ff);
1831
Robert Richter4e1b9c62012-08-16 21:10:22 +02001832 if (!events) {
1833 fprintf(fp, "# event desc: not available or unable to read\n");
1834 return;
1835 }
1836
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001837 for (evsel = events; evsel->core.attr.size; evsel++) {
Robert Richter4e1b9c62012-08-16 21:10:22 +02001838 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001839
Jiri Olsae7eb9002019-09-02 22:15:47 +02001840 if (evsel->core.ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001841 fprintf(fp, ", id = {");
Jiri Olsae7eb9002019-09-02 22:15:47 +02001842 for (j = 0, id = evsel->core.id; j < evsel->core.ids; j++, id++) {
Robert Richter4e1b9c62012-08-16 21:10:22 +02001843 if (j)
1844 fputc(',', fp);
1845 fprintf(fp, " %"PRIu64, *id);
1846 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001847 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001848 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001849
Jiri Olsa1fc632c2019-07-21 13:24:29 +02001850 perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001851
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001852 fputc('\n', fp);
1853 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001854
1855 free_event_desc(events);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001856 ff->events = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001857}
1858
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001859static void print_total_mem(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001860{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001861 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001862}
1863
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001864static void print_numa_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001865{
Jiri Olsac60da222016-07-04 14:16:20 +02001866 int i;
1867 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001868
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001869 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1870 n = &ff->ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001871
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001872 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1873 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001874 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001875
Jiri Olsac60da222016-07-04 14:16:20 +02001876 fprintf(fp, "# node%u cpu list : ", n->node);
1877 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001878 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001879}
1880
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001881static void print_cpuid(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001882{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001883 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001884}
1885
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001886static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001887{
1888 fprintf(fp, "# contains samples with branch stack\n");
1889}
1890
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001891static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
Adrian Hunter4025ea42015-04-09 18:53:41 +03001892{
1893 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1894}
1895
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001896static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001897{
1898 fprintf(fp, "# contains stat data\n");
1899}
1900
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001901static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001902{
1903 int i;
1904
1905 fprintf(fp, "# CPU cache info:\n");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001906 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
Jiri Olsa720e98b2016-02-16 16:01:43 +01001907 fprintf(fp, "# ");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001908 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001909 }
1910}
1911
Alexey Budankov42e1fd82019-03-18 20:41:33 +03001912static void print_compressed(struct feat_fd *ff, FILE *fp)
1913{
1914 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n",
1915 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown",
1916 ff->ph->env.comp_level, ff->ph->env.comp_ratio);
1917}
1918
Kan Liang6f91ea22020-03-19 13:25:02 -07001919static void print_cpu_pmu_caps(struct feat_fd *ff, FILE *fp)
1920{
1921 const char *delimiter = "# cpu pmu capabilities: ";
1922 u32 nr_caps = ff->ph->env.nr_cpu_pmu_caps;
1923 char *str;
1924
1925 if (!nr_caps) {
1926 fprintf(fp, "# cpu pmu capabilities: not available\n");
1927 return;
1928 }
1929
1930 str = ff->ph->env.cpu_pmu_caps;
1931 while (nr_caps--) {
1932 fprintf(fp, "%s%s", delimiter, str);
1933 delimiter = ", ";
1934 str += strlen(str) + 1;
1935 }
1936
1937 fprintf(fp, "\n");
1938}
1939
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001940static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001941{
1942 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001943 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001944 u32 pmu_num;
1945 u32 type;
1946
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001947 pmu_num = ff->ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001948 if (!pmu_num) {
1949 fprintf(fp, "# pmu mappings: not available\n");
1950 return;
1951 }
1952
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001953 str = ff->ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001954
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001955 while (pmu_num) {
1956 type = strtoul(str, &tmp, 0);
1957 if (*tmp != ':')
1958 goto error;
1959
1960 str = tmp + 1;
1961 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1962
Robert Richter50a96672012-08-16 21:10:24 +02001963 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001964 str += strlen(str) + 1;
1965 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001966 }
1967
1968 fprintf(fp, "\n");
1969
1970 if (!pmu_num)
1971 return;
1972error:
1973 fprintf(fp, "# pmu mappings: unable to read\n");
1974}
1975
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001976static void print_group_desc(struct feat_fd *ff, FILE *fp)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001977{
1978 struct perf_session *session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001979 struct evsel *evsel;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001980 u32 nr = 0;
1981
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001982 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001983
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001984 evlist__for_each_entry(session->evlist, evsel) {
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03001985 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03001986 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", evsel__name(evsel));
Namhyung Kima8bb5592013-01-22 18:09:31 +09001987
Jiri Olsa5643b1a2019-07-21 13:24:46 +02001988 nr = evsel->core.nr_members - 1;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001989 } else if (nr) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03001990 fprintf(fp, ",%s", evsel__name(evsel));
Namhyung Kima8bb5592013-01-22 18:09:31 +09001991
1992 if (--nr == 0)
1993 fprintf(fp, "}\n");
1994 }
1995 }
1996}
1997
Jin Yao60115182017-12-08 21:13:41 +08001998static void print_sample_time(struct feat_fd *ff, FILE *fp)
1999{
2000 struct perf_session *session;
2001 char time_buf[32];
2002 double d;
2003
2004 session = container_of(ff->ph, struct perf_session, header);
2005
2006 timestamp__scnprintf_usec(session->evlist->first_sample_time,
2007 time_buf, sizeof(time_buf));
2008 fprintf(fp, "# time of first sample : %s\n", time_buf);
2009
2010 timestamp__scnprintf_usec(session->evlist->last_sample_time,
2011 time_buf, sizeof(time_buf));
2012 fprintf(fp, "# time of last sample : %s\n", time_buf);
2013
2014 d = (double)(session->evlist->last_sample_time -
2015 session->evlist->first_sample_time) / NSEC_PER_MSEC;
2016
2017 fprintf(fp, "# sample duration : %10.3f ms\n", d);
2018}
2019
Jiri Olsae2091ce2018-03-07 16:50:08 +01002020static void memory_node__fprintf(struct memory_node *n,
2021 unsigned long long bsize, FILE *fp)
2022{
2023 char buf_map[100], buf_size[50];
2024 unsigned long long size;
2025
2026 size = bsize * bitmap_weight(n->set, n->size);
2027 unit_number__scnprintf(buf_size, 50, size);
2028
2029 bitmap_scnprintf(n->set, n->size, buf_map, 100);
2030 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
2031}
2032
2033static void print_mem_topology(struct feat_fd *ff, FILE *fp)
2034{
2035 struct memory_node *nodes;
2036 int i, nr;
2037
2038 nodes = ff->ph->env.memory_nodes;
2039 nr = ff->ph->env.nr_memory_nodes;
2040
2041 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
2042 nr, ff->ph->env.memory_bsize);
2043
2044 for (i = 0; i < nr; i++) {
2045 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
2046 }
2047}
2048
Jiri Olsa72932372019-08-28 15:57:16 +02002049static int __event_process_build_id(struct perf_record_header_build_id *bev,
Robert Richter08d95bd2012-02-10 15:41:55 +01002050 char *filename,
2051 struct perf_session *session)
2052{
2053 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01002054 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00002055 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01002056 struct dso *dso;
Jiri Olsa1c695c82020-08-08 14:21:54 +02002057 enum dso_space_type dso_space;
Robert Richter08d95bd2012-02-10 15:41:55 +01002058
2059 machine = perf_session__findnew_machine(session, bev->pid);
2060 if (!machine)
2061 goto out;
2062
Wang Nan1f121b02015-06-03 08:52:21 +00002063 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01002064
Wang Nan1f121b02015-06-03 08:52:21 +00002065 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01002066 case PERF_RECORD_MISC_KERNEL:
Jiri Olsa1c695c82020-08-08 14:21:54 +02002067 dso_space = DSO_SPACE__KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01002068 break;
2069 case PERF_RECORD_MISC_GUEST_KERNEL:
Jiri Olsa1c695c82020-08-08 14:21:54 +02002070 dso_space = DSO_SPACE__KERNEL_GUEST;
Robert Richter08d95bd2012-02-10 15:41:55 +01002071 break;
2072 case PERF_RECORD_MISC_USER:
2073 case PERF_RECORD_MISC_GUEST_USER:
Jiri Olsa1c695c82020-08-08 14:21:54 +02002074 dso_space = DSO_SPACE__USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01002075 break;
2076 default:
2077 goto out;
2078 }
2079
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03002080 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01002081 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09002082 char sbuild_id[SBUILD_ID_SIZE];
Jiri Olsa8dfdf442020-10-13 21:24:37 +02002083 struct build_id bid;
Jiri Olsab0a323c2020-10-13 21:24:39 +02002084 size_t size = BUILD_ID_SIZE;
Robert Richter08d95bd2012-02-10 15:41:55 +01002085
Jiri Olsab0a323c2020-10-13 21:24:39 +02002086 if (bev->header.misc & PERF_RECORD_MISC_BUILD_ID_SIZE)
2087 size = bev->size;
2088
2089 build_id__init(&bid, bev->data, size);
Jiri Olsa8dfdf442020-10-13 21:24:37 +02002090 dso__set_build_id(dso, &bid);
Robert Richter08d95bd2012-02-10 15:41:55 +01002091
Jiri Olsa1c695c82020-08-08 14:21:54 +02002092 if (dso_space != DSO_SPACE__USER) {
Namhyung Kim1deec1b2017-05-31 21:01:03 +09002093 struct kmod_path m = { .name = NULL, };
2094
2095 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09002096 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09002097
Jiri Olsab2fe96a2020-08-08 14:26:56 +02002098 dso->kernel = dso_space;
Namhyung Kim1deec1b2017-05-31 21:01:03 +09002099 free(m.name);
2100 }
Robert Richter08d95bd2012-02-10 15:41:55 +01002101
Jiri Olsabf541162020-10-13 21:24:36 +02002102 build_id__sprintf(&dso->bid, sbuild_id);
Jiri Olsab0a323c2020-10-13 21:24:39 +02002103 pr_debug("build id event received for %s: %s [%zu]\n",
2104 dso->long_name, sbuild_id, size);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03002105 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01002106 }
2107
2108 err = 0;
2109out:
2110 return err;
2111}
2112
2113static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
2114 int input, u64 offset, u64 size)
2115{
2116 struct perf_session *session = container_of(header, struct perf_session, header);
2117 struct {
2118 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002119 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01002120 char filename[0];
2121 } old_bev;
Jiri Olsa72932372019-08-28 15:57:16 +02002122 struct perf_record_header_build_id bev;
Robert Richter08d95bd2012-02-10 15:41:55 +01002123 char filename[PATH_MAX];
2124 u64 limit = offset + size;
2125
2126 while (offset < limit) {
2127 ssize_t len;
2128
Namhyung Kim5323f602012-12-17 15:38:54 +09002129 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01002130 return -1;
2131
2132 if (header->needs_swap)
2133 perf_event_header__bswap(&old_bev.header);
2134
2135 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09002136 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01002137 return -1;
2138
2139 bev.header = old_bev.header;
2140
2141 /*
2142 * As the pid is the missing value, we need to fill
2143 * it properly. The header.misc value give us nice hint.
2144 */
2145 bev.pid = HOST_KERNEL_ID;
2146 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
2147 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
2148 bev.pid = DEFAULT_GUEST_KERNEL_ID;
2149
2150 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
2151 __event_process_build_id(&bev, filename, session);
2152
2153 offset += bev.header.size;
2154 }
2155
2156 return 0;
2157}
2158
2159static int perf_header__read_build_ids(struct perf_header *header,
2160 int input, u64 offset, u64 size)
2161{
2162 struct perf_session *session = container_of(header, struct perf_session, header);
Jiri Olsa72932372019-08-28 15:57:16 +02002163 struct perf_record_header_build_id bev;
Robert Richter08d95bd2012-02-10 15:41:55 +01002164 char filename[PATH_MAX];
2165 u64 limit = offset + size, orig_offset = offset;
2166 int err = -1;
2167
2168 while (offset < limit) {
2169 ssize_t len;
2170
Namhyung Kim5323f602012-12-17 15:38:54 +09002171 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01002172 goto out;
2173
2174 if (header->needs_swap)
2175 perf_event_header__bswap(&bev.header);
2176
2177 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09002178 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01002179 goto out;
2180 /*
2181 * The a1645ce1 changeset:
2182 *
2183 * "perf: 'perf kvm' tool for monitoring guest performance from host"
2184 *
Jiri Olsa72932372019-08-28 15:57:16 +02002185 * Added a field to struct perf_record_header_build_id that broke the file
Robert Richter08d95bd2012-02-10 15:41:55 +01002186 * format.
2187 *
2188 * Since the kernel build-id is the first entry, process the
2189 * table using the old format if the well known
2190 * '[kernel.kallsyms]' string for the kernel build-id has the
2191 * first 4 characters chopped off (where the pid_t sits).
2192 */
2193 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
2194 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
2195 return -1;
2196 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
2197 }
2198
2199 __event_process_build_id(&bev, filename, session);
2200
2201 offset += bev.header.size;
2202 }
2203 err = 0;
2204out:
2205 return err;
2206}
2207
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07002208/* Macro for features that simply need to read and store a string. */
2209#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002210static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07002211{\
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002212 ff->ph->env.__feat_env = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002213 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07002214}
2215
2216FEAT_PROCESS_STR_FUN(hostname, hostname);
2217FEAT_PROCESS_STR_FUN(osrelease, os_release);
2218FEAT_PROCESS_STR_FUN(version, version);
2219FEAT_PROCESS_STR_FUN(arch, arch);
2220FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
2221FEAT_PROCESS_STR_FUN(cpuid, cpuid);
2222
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002223static int process_tracing_data(struct feat_fd *ff, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01002224{
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002225 ssize_t ret = trace_report(ff->fd, data, false);
2226
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002227 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01002228}
2229
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002230static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01002231{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002232 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
Robert Richterf1c67db2012-02-10 15:41:56 +01002233 pr_debug("Failed to read buildids, continuing...\n");
2234 return 0;
2235}
2236
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002237static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002238{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002239 int ret;
2240 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002241
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002242 ret = do_read_u32(ff, &nr_cpus_avail);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002243 if (ret)
2244 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002245
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002246 ret = do_read_u32(ff, &nr_cpus_online);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002247 if (ret)
2248 return ret;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002249 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2250 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002251 return 0;
2252}
2253
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002254static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002255{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002256 u64 total_mem;
2257 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002258
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002259 ret = do_read_u64(ff, &total_mem);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002260 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002261 return -1;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002262 ff->ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002263 return 0;
2264}
2265
Arnaldo Carvalho de Melob02736f2020-11-30 09:48:07 -03002266static struct evsel *evlist__find_by_index(struct evlist *evlist, int idx)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002267{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002268 struct evsel *evsel;
Robert Richter7c2f7af2012-08-16 21:10:23 +02002269
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002270 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02002271 if (evsel->idx == idx)
2272 return evsel;
2273 }
2274
2275 return NULL;
2276}
2277
Arnaldo Carvalho de Melo1420ba22020-11-30 15:13:12 -03002278static void evlist__set_event_name(struct evlist *evlist, struct evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002279{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002280 struct evsel *evsel;
Robert Richter7c2f7af2012-08-16 21:10:23 +02002281
2282 if (!event->name)
2283 return;
2284
Arnaldo Carvalho de Melob02736f2020-11-30 09:48:07 -03002285 evsel = evlist__find_by_index(evlist, event->idx);
Robert Richter7c2f7af2012-08-16 21:10:23 +02002286 if (!evsel)
2287 return;
2288
2289 if (evsel->name)
2290 return;
2291
2292 evsel->name = strdup(event->name);
2293}
2294
2295static int
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002296process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002297{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002298 struct perf_session *session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002299 struct evsel *evsel, *events = read_event_desc(ff);
Robert Richter7c2f7af2012-08-16 21:10:23 +02002300
2301 if (!events)
2302 return 0;
2303
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002304 session = container_of(ff->ph, struct perf_session, header);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002305
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002306 if (session->data->is_pipe) {
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002307 /* Save events for reading later by print_event_desc,
2308 * since they can't be read again in pipe mode. */
2309 ff->events = events;
2310 }
2311
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002312 for (evsel = events; evsel->core.attr.size; evsel++)
Arnaldo Carvalho de Melo1420ba22020-11-30 15:13:12 -03002313 evlist__set_event_name(session->evlist, evsel);
Robert Richter7c2f7af2012-08-16 21:10:23 +02002314
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002315 if (!session->data->is_pipe)
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002316 free_event_desc(events);
Robert Richter7c2f7af2012-08-16 21:10:23 +02002317
2318 return 0;
2319}
2320
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002321static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002322{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002323 char *str, *cmdline = NULL, **argv = NULL;
2324 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002325
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002326 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002327 return -1;
2328
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002329 ff->ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002330
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002331 cmdline = zalloc(ff->size + nr + 1);
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002332 if (!cmdline)
2333 return -1;
2334
2335 argv = zalloc(sizeof(char *) * (nr + 1));
2336 if (!argv)
2337 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002338
2339 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002340 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002341 if (!str)
2342 goto error;
2343
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002344 argv[i] = cmdline + len;
2345 memcpy(argv[i], str, strlen(str) + 1);
2346 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002347 free(str);
2348 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002349 ff->ph->env.cmdline = cmdline;
2350 ff->ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002351 return 0;
2352
2353error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002354 free(argv);
2355 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002356 return -1;
2357}
2358
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002359static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002360{
Namhyung Kima1ae5652012-09-24 17:14:59 +09002361 u32 nr, i;
2362 char *str;
2363 struct strbuf sb;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002364 int cpu_nr = ff->ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04002365 u64 size = 0;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002366 struct perf_header *ph = ff->ph;
Thomas Richter01766222018-06-11 09:31:52 +02002367 bool do_core_id_test = true;
Kan Liang2bb00d22015-09-01 09:58:12 -04002368
2369 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2370 if (!ph->env.cpu)
2371 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002372
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002373 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002374 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002375
Namhyung Kima1ae5652012-09-24 17:14:59 +09002376 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002377 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002378 if (strbuf_init(&sb, 128) < 0)
2379 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002380
2381 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002382 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002383 if (!str)
2384 goto error;
2385
2386 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002387 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2388 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04002389 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002390 free(str);
2391 }
2392 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2393
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002394 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002395 return -1;
2396
Namhyung Kima1ae5652012-09-24 17:14:59 +09002397 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002398 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002399
2400 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002401 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002402 if (!str)
2403 goto error;
2404
2405 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002406 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2407 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04002408 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002409 free(str);
2410 }
2411 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04002412
2413 /*
2414 * The header may be from old perf,
2415 * which doesn't include core id and socket id information.
2416 */
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002417 if (ff->size <= size) {
Kan Liang2bb00d22015-09-01 09:58:12 -04002418 zfree(&ph->env.cpu);
2419 return 0;
2420 }
2421
Thomas Richter01766222018-06-11 09:31:52 +02002422 /* On s390 the socket_id number is not related to the numbers of cpus.
2423 * The socket_id number might be higher than the numbers of cpus.
2424 * This depends on the configuration.
Tan Xiaojun0a4d8fb2019-08-02 11:48:57 +08002425 * AArch64 is the same.
Thomas Richter01766222018-06-11 09:31:52 +02002426 */
Tan Xiaojun0a4d8fb2019-08-02 11:48:57 +08002427 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
2428 || !strncmp(ph->env.arch, "aarch64", 7)))
Thomas Richter01766222018-06-11 09:31:52 +02002429 do_core_id_test = false;
2430
Kan Liang2bb00d22015-09-01 09:58:12 -04002431 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002432 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002433 goto free_cpu;
2434
Kan Liang2bb00d22015-09-01 09:58:12 -04002435 ph->env.cpu[i].core_id = nr;
Kan Liangacae8b32019-06-04 15:50:41 -07002436 size += sizeof(u32);
Kan Liang2bb00d22015-09-01 09:58:12 -04002437
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002438 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002439 goto free_cpu;
2440
Thomas Richter01766222018-06-11 09:31:52 +02002441 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04002442 pr_debug("socket_id number is too big."
2443 "You may need to upgrade the perf tool.\n");
2444 goto free_cpu;
2445 }
2446
2447 ph->env.cpu[i].socket_id = nr;
Kan Liangacae8b32019-06-04 15:50:41 -07002448 size += sizeof(u32);
2449 }
2450
2451 /*
2452 * The header may be from old perf,
2453 * which doesn't include die information.
2454 */
2455 if (ff->size <= size)
2456 return 0;
2457
2458 if (do_read_u32(ff, &nr))
2459 return -1;
2460
2461 ph->env.nr_sibling_dies = nr;
2462 size += sizeof(u32);
2463
2464 for (i = 0; i < nr; i++) {
2465 str = do_read_string(ff);
2466 if (!str)
2467 goto error;
2468
2469 /* include a NULL character at the end */
2470 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2471 goto error;
2472 size += string_size(str);
2473 free(str);
2474 }
2475 ph->env.sibling_dies = strbuf_detach(&sb, NULL);
2476
2477 for (i = 0; i < (u32)cpu_nr; i++) {
2478 if (do_read_u32(ff, &nr))
2479 goto free_cpu;
2480
2481 ph->env.cpu[i].die_id = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002482 }
2483
Namhyung Kima1ae5652012-09-24 17:14:59 +09002484 return 0;
2485
2486error:
2487 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04002488free_cpu:
2489 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002490 return -1;
2491}
2492
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002493static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002494{
Jiri Olsac60da222016-07-04 14:16:20 +02002495 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02002496 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002497 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002498
2499 /* nr nodes */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002500 if (do_read_u32(ff, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002501 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002502
Jiri Olsac60da222016-07-04 14:16:20 +02002503 nodes = zalloc(sizeof(*nodes) * nr);
2504 if (!nodes)
2505 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002506
2507 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02002508 n = &nodes[i];
2509
Namhyung Kima1ae5652012-09-24 17:14:59 +09002510 /* node number */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002511 if (do_read_u32(ff, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002512 goto error;
2513
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002514 if (do_read_u64(ff, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002515 goto error;
2516
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002517 if (do_read_u64(ff, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002518 goto error;
2519
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002520 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002521 if (!str)
2522 goto error;
2523
Jiri Olsa9c3516d2019-07-21 13:24:30 +02002524 n->map = perf_cpu_map__new(str);
Jiri Olsac60da222016-07-04 14:16:20 +02002525 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002526 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02002527
Namhyung Kima1ae5652012-09-24 17:14:59 +09002528 free(str);
2529 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002530 ff->ph->env.nr_numa_nodes = nr;
2531 ff->ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002532 return 0;
2533
2534error:
Jiri Olsac60da222016-07-04 14:16:20 +02002535 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002536 return -1;
2537}
2538
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002539static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002540{
Namhyung Kima1ae5652012-09-24 17:14:59 +09002541 char *name;
2542 u32 pmu_num;
2543 u32 type;
2544 struct strbuf sb;
2545
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002546 if (do_read_u32(ff, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002547 return -1;
2548
Namhyung Kima1ae5652012-09-24 17:14:59 +09002549 if (!pmu_num) {
2550 pr_debug("pmu mappings not available\n");
2551 return 0;
2552 }
2553
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002554 ff->ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002555 if (strbuf_init(&sb, 128) < 0)
2556 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002557
2558 while (pmu_num) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002559 if (do_read_u32(ff, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002560 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002561
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002562 name = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002563 if (!name)
2564 goto error;
2565
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002566 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2567 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002568 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002569 if (strbuf_add(&sb, "", 1) < 0)
2570 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002571
Kan Liange0838e02015-09-10 11:03:05 -03002572 if (!strcmp(name, "msr"))
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002573 ff->ph->env.msr_pmu_type = type;
Kan Liange0838e02015-09-10 11:03:05 -03002574
Namhyung Kima1ae5652012-09-24 17:14:59 +09002575 free(name);
2576 pmu_num--;
2577 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002578 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002579 return 0;
2580
2581error:
2582 strbuf_release(&sb);
2583 return -1;
2584}
2585
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002586static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima8bb5592013-01-22 18:09:31 +09002587{
2588 size_t ret = -1;
2589 u32 i, nr, nr_groups;
2590 struct perf_session *session;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002591 struct evsel *evsel, *leader = NULL;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002592 struct group_desc {
2593 char *name;
2594 u32 leader_idx;
2595 u32 nr_members;
2596 } *desc;
2597
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002598 if (do_read_u32(ff, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002599 return -1;
2600
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002601 ff->ph->env.nr_groups = nr_groups;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002602 if (!nr_groups) {
2603 pr_debug("group desc not available\n");
2604 return 0;
2605 }
2606
2607 desc = calloc(nr_groups, sizeof(*desc));
2608 if (!desc)
2609 return -1;
2610
2611 for (i = 0; i < nr_groups; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002612 desc[i].name = do_read_string(ff);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002613 if (!desc[i].name)
2614 goto out_free;
2615
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002616 if (do_read_u32(ff, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002617 goto out_free;
2618
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002619 if (do_read_u32(ff, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002620 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002621 }
2622
2623 /*
2624 * Rebuild group relationship based on the group_desc
2625 */
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002626 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002627 session->evlist->nr_groups = nr_groups;
2628
2629 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002630 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002631 if (evsel->idx == (int) desc[i].leader_idx) {
2632 evsel->leader = evsel;
2633 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002634 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002635 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002636 desc[i].name = NULL;
2637 }
Jiri Olsa5643b1a2019-07-21 13:24:46 +02002638 evsel->core.nr_members = desc[i].nr_members;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002639
2640 if (i >= nr_groups || nr > 0) {
2641 pr_debug("invalid group desc\n");
2642 goto out_free;
2643 }
2644
2645 leader = evsel;
Jiri Olsa5643b1a2019-07-21 13:24:46 +02002646 nr = evsel->core.nr_members - 1;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002647 i++;
2648 } else if (nr) {
2649 /* This is a group member */
2650 evsel->leader = leader;
2651
2652 nr--;
2653 }
2654 }
2655
2656 if (i != nr_groups || nr != 0) {
2657 pr_debug("invalid group desc\n");
2658 goto out_free;
2659 }
2660
2661 ret = 0;
2662out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002663 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002664 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002665 free(desc);
2666
2667 return ret;
2668}
2669
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002670static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
Adrian Hunter99fa2982015-04-30 17:37:25 +03002671{
2672 struct perf_session *session;
2673 int err;
2674
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002675 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002676
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002677 err = auxtrace_index__process(ff->fd, ff->size, session,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002678 ff->ph->needs_swap);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002679 if (err < 0)
2680 pr_err("Failed to process auxtrace index\n");
2681 return err;
2682}
2683
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002684static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01002685{
2686 struct cpu_cache_level *caches;
2687 u32 cnt, i, version;
2688
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002689 if (do_read_u32(ff, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002690 return -1;
2691
Jiri Olsa720e98b2016-02-16 16:01:43 +01002692 if (version != 1)
2693 return -1;
2694
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002695 if (do_read_u32(ff, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002696 return -1;
2697
Jiri Olsa720e98b2016-02-16 16:01:43 +01002698 caches = zalloc(sizeof(*caches) * cnt);
2699 if (!caches)
2700 return -1;
2701
2702 for (i = 0; i < cnt; i++) {
2703 struct cpu_cache_level c;
2704
2705 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002706 if (do_read_u32(ff, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002707 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002708
2709 _R(level)
2710 _R(line_size)
2711 _R(sets)
2712 _R(ways)
2713 #undef _R
2714
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002715 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002716 c.v = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002717 if (!c.v) \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002718 goto out_free_caches;
2719
2720 _R(type)
2721 _R(size)
2722 _R(map)
2723 #undef _R
2724
2725 caches[i] = c;
2726 }
2727
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002728 ff->ph->env.caches = caches;
2729 ff->ph->env.caches_cnt = cnt;
Jiri Olsa720e98b2016-02-16 16:01:43 +01002730 return 0;
2731out_free_caches:
2732 free(caches);
2733 return -1;
2734}
2735
Jin Yao60115182017-12-08 21:13:41 +08002736static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2737{
2738 struct perf_session *session;
2739 u64 first_sample_time, last_sample_time;
2740 int ret;
2741
2742 session = container_of(ff->ph, struct perf_session, header);
2743
2744 ret = do_read_u64(ff, &first_sample_time);
2745 if (ret)
2746 return -1;
2747
2748 ret = do_read_u64(ff, &last_sample_time);
2749 if (ret)
2750 return -1;
2751
2752 session->evlist->first_sample_time = first_sample_time;
2753 session->evlist->last_sample_time = last_sample_time;
2754 return 0;
2755}
2756
Jiri Olsae2091ce2018-03-07 16:50:08 +01002757static int process_mem_topology(struct feat_fd *ff,
2758 void *data __maybe_unused)
2759{
2760 struct memory_node *nodes;
2761 u64 version, i, nr, bsize;
2762 int ret = -1;
2763
2764 if (do_read_u64(ff, &version))
2765 return -1;
2766
2767 if (version != 1)
2768 return -1;
2769
2770 if (do_read_u64(ff, &bsize))
2771 return -1;
2772
2773 if (do_read_u64(ff, &nr))
2774 return -1;
2775
2776 nodes = zalloc(sizeof(*nodes) * nr);
2777 if (!nodes)
2778 return -1;
2779
2780 for (i = 0; i < nr; i++) {
2781 struct memory_node n;
2782
2783 #define _R(v) \
2784 if (do_read_u64(ff, &n.v)) \
2785 goto out; \
2786
2787 _R(node)
2788 _R(size)
2789
2790 #undef _R
2791
2792 if (do_read_bitmap(ff, &n.set, &n.size))
2793 goto out;
2794
2795 nodes[i] = n;
2796 }
2797
2798 ff->ph->env.memory_bsize = bsize;
2799 ff->ph->env.memory_nodes = nodes;
2800 ff->ph->env.nr_memory_nodes = nr;
2801 ret = 0;
2802
2803out:
2804 if (ret)
2805 free(nodes);
2806 return ret;
2807}
2808
Alexey Budankovcf790512018-10-09 17:36:24 +03002809static int process_clockid(struct feat_fd *ff,
2810 void *data __maybe_unused)
2811{
Jiri Olsa9d88a1a12020-08-05 11:34:41 +02002812 if (do_read_u64(ff, &ff->ph->env.clock.clockid_res_ns))
Alexey Budankovcf790512018-10-09 17:36:24 +03002813 return -1;
2814
2815 return 0;
2816}
2817
Jiri Olsad1e325c2020-08-05 11:34:40 +02002818static int process_clock_data(struct feat_fd *ff,
2819 void *_data __maybe_unused)
2820{
2821 u32 data32;
2822 u64 data64;
2823
2824 /* version */
2825 if (do_read_u32(ff, &data32))
2826 return -1;
2827
2828 if (data32 != 1)
2829 return -1;
2830
2831 /* clockid */
2832 if (do_read_u32(ff, &data32))
2833 return -1;
2834
2835 ff->ph->env.clock.clockid = data32;
2836
2837 /* TOD ref time */
2838 if (do_read_u64(ff, &data64))
2839 return -1;
2840
2841 ff->ph->env.clock.tod_ns = data64;
2842
2843 /* clockid ref time */
2844 if (do_read_u64(ff, &data64))
2845 return -1;
2846
2847 ff->ph->env.clock.clockid_ns = data64;
2848 ff->ph->env.clock.enabled = true;
2849 return 0;
2850}
2851
Jiri Olsa258031c2019-03-08 14:47:39 +01002852static int process_dir_format(struct feat_fd *ff,
2853 void *_data __maybe_unused)
2854{
2855 struct perf_session *session;
2856 struct perf_data *data;
2857
2858 session = container_of(ff->ph, struct perf_session, header);
2859 data = session->data;
2860
2861 if (WARN_ON(!perf_data__is_dir(data)))
2862 return -1;
2863
2864 return do_read_u64(ff, &data->dir.version);
2865}
2866
Song Liu606f9722019-03-11 22:30:43 -07002867#ifdef HAVE_LIBBPF_SUPPORT
2868static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
2869{
2870 struct bpf_prog_info_linear *info_linear;
2871 struct bpf_prog_info_node *info_node;
2872 struct perf_env *env = &ff->ph->env;
2873 u32 count, i;
2874 int err = -1;
2875
2876 if (ff->ph->needs_swap) {
2877 pr_warning("interpreting bpf_prog_info from systems with endianity is not yet supported\n");
2878 return 0;
2879 }
2880
2881 if (do_read_u32(ff, &count))
2882 return -1;
2883
2884 down_write(&env->bpf_progs.lock);
2885
2886 for (i = 0; i < count; ++i) {
2887 u32 info_len, data_len;
2888
2889 info_linear = NULL;
2890 info_node = NULL;
2891 if (do_read_u32(ff, &info_len))
2892 goto out;
2893 if (do_read_u32(ff, &data_len))
2894 goto out;
2895
2896 if (info_len > sizeof(struct bpf_prog_info)) {
2897 pr_warning("detected invalid bpf_prog_info\n");
2898 goto out;
2899 }
2900
2901 info_linear = malloc(sizeof(struct bpf_prog_info_linear) +
2902 data_len);
2903 if (!info_linear)
2904 goto out;
2905 info_linear->info_len = sizeof(struct bpf_prog_info);
2906 info_linear->data_len = data_len;
2907 if (do_read_u64(ff, (u64 *)(&info_linear->arrays)))
2908 goto out;
2909 if (__do_read(ff, &info_linear->info, info_len))
2910 goto out;
2911 if (info_len < sizeof(struct bpf_prog_info))
2912 memset(((void *)(&info_linear->info)) + info_len, 0,
2913 sizeof(struct bpf_prog_info) - info_len);
2914
2915 if (__do_read(ff, info_linear->data, data_len))
2916 goto out;
2917
2918 info_node = malloc(sizeof(struct bpf_prog_info_node));
2919 if (!info_node)
2920 goto out;
2921
2922 /* after reading from file, translate offset to address */
2923 bpf_program__bpil_offs_to_addr(info_linear);
2924 info_node->info_linear = info_linear;
2925 perf_env__insert_bpf_prog_info(env, info_node);
2926 }
2927
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002928 up_write(&env->bpf_progs.lock);
Song Liu606f9722019-03-11 22:30:43 -07002929 return 0;
2930out:
2931 free(info_linear);
2932 free(info_node);
2933 up_write(&env->bpf_progs.lock);
2934 return err;
2935}
Song Liu606f9722019-03-11 22:30:43 -07002936
Song Liua70a112312019-03-11 22:30:45 -07002937static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)
2938{
2939 struct perf_env *env = &ff->ph->env;
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002940 struct btf_node *node = NULL;
Song Liua70a112312019-03-11 22:30:45 -07002941 u32 count, i;
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002942 int err = -1;
Song Liua70a112312019-03-11 22:30:45 -07002943
2944 if (ff->ph->needs_swap) {
2945 pr_warning("interpreting btf from systems with endianity is not yet supported\n");
2946 return 0;
2947 }
2948
2949 if (do_read_u32(ff, &count))
2950 return -1;
2951
2952 down_write(&env->bpf_progs.lock);
2953
2954 for (i = 0; i < count; ++i) {
Song Liua70a112312019-03-11 22:30:45 -07002955 u32 id, data_size;
2956
2957 if (do_read_u32(ff, &id))
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002958 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002959 if (do_read_u32(ff, &data_size))
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002960 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002961
2962 node = malloc(sizeof(struct btf_node) + data_size);
2963 if (!node)
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002964 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002965
2966 node->id = id;
2967 node->data_size = data_size;
2968
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002969 if (__do_read(ff, node->data, data_size))
2970 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002971
2972 perf_env__insert_btf(env, node);
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002973 node = NULL;
Song Liua70a112312019-03-11 22:30:45 -07002974 }
2975
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002976 err = 0;
2977out:
Song Liua70a112312019-03-11 22:30:45 -07002978 up_write(&env->bpf_progs.lock);
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002979 free(node);
2980 return err;
Song Liua70a112312019-03-11 22:30:45 -07002981}
Arnaldo Carvalho de Meloef0580e2020-10-20 15:57:21 -03002982#endif // HAVE_LIBBPF_SUPPORT
Song Liua70a112312019-03-11 22:30:45 -07002983
Alexey Budankov42e1fd82019-03-18 20:41:33 +03002984static int process_compressed(struct feat_fd *ff,
2985 void *data __maybe_unused)
2986{
2987 if (do_read_u32(ff, &(ff->ph->env.comp_ver)))
2988 return -1;
2989
2990 if (do_read_u32(ff, &(ff->ph->env.comp_type)))
2991 return -1;
2992
2993 if (do_read_u32(ff, &(ff->ph->env.comp_level)))
2994 return -1;
2995
2996 if (do_read_u32(ff, &(ff->ph->env.comp_ratio)))
2997 return -1;
2998
2999 if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len)))
3000 return -1;
3001
3002 return 0;
3003}
3004
Kan Liang6f91ea22020-03-19 13:25:02 -07003005static int process_cpu_pmu_caps(struct feat_fd *ff,
3006 void *data __maybe_unused)
3007{
3008 char *name, *value;
3009 struct strbuf sb;
3010 u32 nr_caps;
3011
3012 if (do_read_u32(ff, &nr_caps))
3013 return -1;
3014
3015 if (!nr_caps) {
3016 pr_debug("cpu pmu capabilities not available\n");
3017 return 0;
3018 }
3019
3020 ff->ph->env.nr_cpu_pmu_caps = nr_caps;
3021
3022 if (strbuf_init(&sb, 128) < 0)
3023 return -1;
3024
3025 while (nr_caps--) {
3026 name = do_read_string(ff);
3027 if (!name)
3028 goto error;
3029
3030 value = do_read_string(ff);
3031 if (!value)
3032 goto free_name;
3033
3034 if (strbuf_addf(&sb, "%s=%s", name, value) < 0)
3035 goto free_value;
3036
3037 /* include a NULL character at the end */
3038 if (strbuf_add(&sb, "", 1) < 0)
3039 goto free_value;
3040
3041 if (!strcmp(name, "branches"))
3042 ff->ph->env.max_branches = atoi(value);
3043
3044 free(value);
3045 free(name);
3046 }
3047 ff->ph->env.cpu_pmu_caps = strbuf_detach(&sb, NULL);
3048 return 0;
3049
3050free_value:
3051 free(value);
3052free_name:
3053 free(name);
3054error:
3055 strbuf_release(&sb);
3056 return -1;
3057}
3058
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07003059#define FEAT_OPR(n, func, __full_only) \
3060 [HEADER_##n] = { \
3061 .name = __stringify(n), \
3062 .write = write_##func, \
3063 .print = print_##func, \
3064 .full_only = __full_only, \
3065 .process = process_##func, \
3066 .synthesize = true \
3067 }
3068
3069#define FEAT_OPN(n, func, __full_only) \
3070 [HEADER_##n] = { \
3071 .name = __stringify(n), \
3072 .write = write_##func, \
3073 .print = print_##func, \
3074 .full_only = __full_only, \
3075 .process = process_##func \
3076 }
Robert Richter8cdfa782011-12-07 10:02:56 +01003077
3078/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02003079#define print_tracing_data NULL
3080#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003081
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07003082#define process_branch_stack NULL
3083#define process_stat NULL
3084
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03003085// Only used in util/synthetic-events.c
3086const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07003087
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03003088const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07003089 FEAT_OPN(TRACING_DATA, tracing_data, false),
3090 FEAT_OPN(BUILD_ID, build_id, false),
3091 FEAT_OPR(HOSTNAME, hostname, false),
3092 FEAT_OPR(OSRELEASE, osrelease, false),
3093 FEAT_OPR(VERSION, version, false),
3094 FEAT_OPR(ARCH, arch, false),
3095 FEAT_OPR(NRCPUS, nrcpus, false),
3096 FEAT_OPR(CPUDESC, cpudesc, false),
3097 FEAT_OPR(CPUID, cpuid, false),
3098 FEAT_OPR(TOTAL_MEM, total_mem, false),
3099 FEAT_OPR(EVENT_DESC, event_desc, false),
3100 FEAT_OPR(CMDLINE, cmdline, false),
3101 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
3102 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
3103 FEAT_OPN(BRANCH_STACK, branch_stack, false),
3104 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
Jiri Olsae8fedff2018-07-12 15:52:02 +02003105 FEAT_OPR(GROUP_DESC, group_desc, false),
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07003106 FEAT_OPN(AUXTRACE, auxtrace, false),
3107 FEAT_OPN(STAT, stat, false),
3108 FEAT_OPN(CACHE, cache, true),
Jin Yao60115182017-12-08 21:13:41 +08003109 FEAT_OPR(SAMPLE_TIME, sample_time, false),
Jiri Olsae2091ce2018-03-07 16:50:08 +01003110 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
Jiri Olsa258031c2019-03-08 14:47:39 +01003111 FEAT_OPR(CLOCKID, clockid, false),
Song Liu606f9722019-03-11 22:30:43 -07003112 FEAT_OPN(DIR_FORMAT, dir_format, false),
Arnaldo Carvalho de Meloef0580e2020-10-20 15:57:21 -03003113#ifdef HAVE_LIBBPF_SUPPORT
Song Liua70a112312019-03-11 22:30:45 -07003114 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false),
3115 FEAT_OPR(BPF_BTF, bpf_btf, false),
Arnaldo Carvalho de Meloef0580e2020-10-20 15:57:21 -03003116#endif
Alexey Budankov42e1fd82019-03-18 20:41:33 +03003117 FEAT_OPR(COMPRESSED, compressed, false),
Kan Liang6f91ea22020-03-19 13:25:02 -07003118 FEAT_OPR(CPU_PMU_CAPS, cpu_pmu_caps, false),
Jiri Olsad1e325c2020-08-05 11:34:40 +02003119 FEAT_OPR(CLOCK_DATA, clock_data, false),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003120};
3121
3122struct header_print_data {
3123 FILE *fp;
3124 bool full; /* extended list of headers */
3125};
3126
3127static int perf_file_section__fprintf_info(struct perf_file_section *section,
3128 struct perf_header *ph,
3129 int feat, int fd, void *data)
3130{
3131 struct header_print_data *hd = data;
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07003132 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003133
3134 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3135 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3136 "%d, continuing...\n", section->offset, feat);
3137 return 0;
3138 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003139 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003140 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01003141 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003142 }
3143 if (!feat_ops[feat].print)
3144 return 0;
3145
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07003146 ff = (struct feat_fd) {
3147 .fd = fd,
3148 .ph = ph,
3149 };
3150
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003151 if (!feat_ops[feat].full_only || hd->full)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07003152 feat_ops[feat].print(&ff, hd->fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003153 else
3154 fprintf(hd->fp, "# %s info available, use -I to display\n",
3155 feat_ops[feat].name);
3156
3157 return 0;
3158}
3159
3160int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
3161{
3162 struct header_print_data hd;
3163 struct perf_header *header = &session->header;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003164 int fd = perf_data__fd(session->data);
Jiri Olsaf45f5612016-10-10 09:03:07 +02003165 struct stat st;
Arnaldo Carvalho de Melo0afcf292018-12-11 16:11:54 -03003166 time_t stctime;
Jiri Olsaaabae162016-10-10 09:35:50 +02003167 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02003168
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003169 hd.fp = fp;
3170 hd.full = full;
3171
Jiri Olsaf45f5612016-10-10 09:03:07 +02003172 ret = fstat(fd, &st);
3173 if (ret == -1)
3174 return -1;
3175
Michael Petlan8af19d62020-01-14 11:42:36 +01003176 stctime = st.st_mtime;
Arnaldo Carvalho de Melo0afcf292018-12-11 16:11:54 -03003177 fprintf(fp, "# captured on : %s", ctime(&stctime));
Jiri Olsae971a5a2018-03-07 16:50:03 +01003178
3179 fprintf(fp, "# header version : %u\n", header->version);
3180 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
3181 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
3182 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
Jiri Olsaf45f5612016-10-10 09:03:07 +02003183
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003184 perf_header__process_sections(header, fd, &hd,
3185 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02003186
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003187 if (session->data->is_pipe)
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07003188 return 0;
3189
Jiri Olsaaabae162016-10-10 09:35:50 +02003190 fprintf(fp, "# missing features: ");
3191 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
3192 if (bit)
3193 fprintf(fp, "%s ", feat_ops[bit].name);
3194 }
3195
3196 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003197 return 0;
3198}
3199
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003200static int do_write_feat(struct feat_fd *ff, int type,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003201 struct perf_file_section **p,
Jiri Olsa63503db2019-07-21 13:23:52 +02003202 struct evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003203{
3204 int err;
3205 int ret = 0;
3206
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003207 if (perf_header__has_feat(ff->ph, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003208 if (!feat_ops[type].write)
3209 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003210
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -07003211 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
3212 return -1;
3213
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003214 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003215
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003216 err = feat_ops[type].write(ff, evlist);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003217 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02003218 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003219
3220 /* undo anything written */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003221 lseek(ff->fd, (*p)->offset, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003222
3223 return -1;
3224 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003225 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003226 (*p)++;
3227 }
3228 return ret;
3229}
3230
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003231static int perf_header__adds_write(struct perf_header *header,
Jiri Olsa63503db2019-07-21 13:23:52 +02003232 struct evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003233{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003234 int nr_sections;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003235 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003236 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003237 int sec_size;
3238 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003239 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003240 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003241
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003242 ff = (struct feat_fd){
3243 .fd = fd,
3244 .ph = header,
3245 };
3246
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003247 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003248 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003249 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003250
Paul Gortmaker91b98802013-01-30 20:05:49 -05003251 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003252 if (feat_sec == NULL)
3253 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003254
3255 sec_size = sizeof(*feat_sec) * nr_sections;
3256
Jiri Olsa8d541e92013-07-17 19:49:44 +02003257 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08003258 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003259
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003260 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003261 if (do_write_feat(&ff, feat, &p, evlist))
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003262 perf_header__clear_feat(header, feat);
3263 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003264
Xiao Guangrongf887f302010-02-04 16:46:42 +08003265 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003266 /*
3267 * may write more than needed due to dropped feature, but
Ingo Molnaradba1632018-12-03 11:22:00 +01003268 * this is okay, reader will skip the missing entries
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003269 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003270 err = do_write(&ff, feat_sec, sec_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003271 if (err < 0)
3272 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003273 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003274 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003275}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003276
Tom Zanussi8dc58102010-04-01 23:59:15 -05003277int perf_header__write_pipe(int fd)
3278{
3279 struct perf_pipe_file_header f_header;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003280 struct feat_fd ff;
Tom Zanussi8dc58102010-04-01 23:59:15 -05003281 int err;
3282
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003283 ff = (struct feat_fd){ .fd = fd };
3284
Tom Zanussi8dc58102010-04-01 23:59:15 -05003285 f_header = (struct perf_pipe_file_header){
3286 .magic = PERF_MAGIC,
3287 .size = sizeof(f_header),
3288 };
3289
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003290 err = do_write(&ff, &f_header, sizeof(f_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05003291 if (err < 0) {
3292 pr_debug("failed to write perf pipe header\n");
3293 return err;
3294 }
3295
3296 return 0;
3297}
3298
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003299int perf_session__write_header(struct perf_session *session,
Jiri Olsa63503db2019-07-21 13:23:52 +02003300 struct evlist *evlist,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003301 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003302{
3303 struct perf_file_header f_header;
3304 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003305 struct perf_header *header = &session->header;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003306 struct evsel *evsel;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003307 struct feat_fd ff;
Jiri Olsa944d62b2013-07-17 19:49:43 +02003308 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003309 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003310
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003311 ff = (struct feat_fd){ .fd = fd};
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003312 lseek(fd, sizeof(f_header), SEEK_SET);
3313
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003314 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003315 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsae7eb9002019-09-02 22:15:47 +02003316 err = do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003317 if (err < 0) {
3318 pr_debug("failed to write perf header\n");
3319 return err;
3320 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003321 }
3322
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003323 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003324
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003325 evlist__for_each_entry(evlist, evsel) {
Al Grant648b0542020-11-24 19:58:17 +00003326 if (evsel->core.attr.size < sizeof(evsel->core.attr)) {
3327 /*
3328 * We are likely in "perf inject" and have read
3329 * from an older file. Update attr size so that
3330 * reader gets the right offset to the ids.
3331 */
3332 evsel->core.attr.size = sizeof(evsel->core.attr);
3333 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003334 f_attr = (struct perf_file_attr){
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003335 .attr = evsel->core.attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003336 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02003337 .offset = evsel->id_offset,
Jiri Olsae7eb9002019-09-02 22:15:47 +02003338 .size = evsel->core.ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003339 }
3340 };
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003341 err = do_write(&ff, &f_attr, sizeof(f_attr));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003342 if (err < 0) {
3343 pr_debug("failed to write perf header attribute\n");
3344 return err;
3345 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003346 }
3347
Adrian Hunterd645c442013-12-11 14:36:28 +02003348 if (!header->data_offset)
3349 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02003350 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003351
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003352 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003353 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003354 if (err < 0)
3355 return err;
3356 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003357
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003358 f_header = (struct perf_file_header){
3359 .magic = PERF_MAGIC,
3360 .size = sizeof(f_header),
3361 .attr_size = sizeof(f_attr),
3362 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02003363 .offset = attr_offset,
Jiri Olsa6484d2f2019-07-21 13:24:28 +02003364 .size = evlist->core.nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003365 },
3366 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003367 .offset = header->data_offset,
3368 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003369 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02003370 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003371 };
3372
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003373 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003374
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003375 lseek(fd, 0, SEEK_SET);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003376 err = do_write(&ff, &f_header, sizeof(f_header));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003377 if (err < 0) {
3378 pr_debug("failed to write perf header\n");
3379 return err;
3380 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003381 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003382
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003383 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003384}
3385
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003386static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003387 int fd, void *buf, size_t size)
3388{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02003389 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003390 return -1;
3391
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003392 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003393 mem_bswap_64(buf, size);
3394
3395 return 0;
3396}
3397
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003398int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003399 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003400 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003401 struct perf_header *ph,
3402 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003403{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003404 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003405 int nr_sections;
3406 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003407 int feat;
3408 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003409
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003410 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003411 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003412 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003413
Paul Gortmaker91b98802013-01-30 20:05:49 -05003414 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003415 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003416 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003417
3418 sec_size = sizeof(*feat_sec) * nr_sections;
3419
Jiri Olsa8d541e92013-07-17 19:49:44 +02003420 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003421
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003422 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
3423 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003424 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003425
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003426 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
3427 err = process(sec++, header, feat, fd, data);
3428 if (err < 0)
3429 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01003430 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003431 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003432out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003433 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003434 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003435}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003436
Stephane Eranian114382a2012-02-09 23:21:08 +01003437static const int attr_file_abi_sizes[] = {
3438 [0] = PERF_ATTR_SIZE_VER0,
3439 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02003440 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02003441 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02003442 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01003443 0,
3444};
3445
3446/*
3447 * In the legacy file format, the magic number is not used to encode endianness.
3448 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
3449 * on ABI revisions, we need to try all combinations for all endianness to
3450 * detect the endianness.
3451 */
3452static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
3453{
3454 uint64_t ref_size, attr_size;
3455 int i;
3456
3457 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
3458 ref_size = attr_file_abi_sizes[i]
3459 + sizeof(struct perf_file_section);
3460 if (hdr_sz != ref_size) {
3461 attr_size = bswap_64(hdr_sz);
3462 if (attr_size != ref_size)
3463 continue;
3464
3465 ph->needs_swap = true;
3466 }
3467 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
3468 i,
3469 ph->needs_swap);
3470 return 0;
3471 }
3472 /* could not determine endianness */
3473 return -1;
3474}
3475
3476#define PERF_PIPE_HDR_VER0 16
3477
3478static const size_t attr_pipe_abi_sizes[] = {
3479 [0] = PERF_PIPE_HDR_VER0,
3480 0,
3481};
3482
3483/*
3484 * In the legacy pipe format, there is an implicit assumption that endiannesss
3485 * between host recording the samples, and host parsing the samples is the
3486 * same. This is not always the case given that the pipe output may always be
3487 * redirected into a file and analyzed on a different machine with possibly a
3488 * different endianness and perf_event ABI revsions in the perf tool itself.
3489 */
3490static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
3491{
3492 u64 attr_size;
3493 int i;
3494
3495 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
3496 if (hdr_sz != attr_pipe_abi_sizes[i]) {
3497 attr_size = bswap_64(hdr_sz);
3498 if (attr_size != hdr_sz)
3499 continue;
3500
3501 ph->needs_swap = true;
3502 }
3503 pr_debug("Pipe ABI%d perf.data file detected\n", i);
3504 return 0;
3505 }
3506 return -1;
3507}
3508
Feng Tange84ba4e2012-10-30 11:56:07 +08003509bool is_perf_magic(u64 magic)
3510{
3511 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
3512 || magic == __perf_magic2
3513 || magic == __perf_magic2_sw)
3514 return true;
3515
3516 return false;
3517}
3518
Stephane Eranian114382a2012-02-09 23:21:08 +01003519static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3520 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01003521{
3522 int ret;
3523
3524 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01003525 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01003526 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02003527 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01003528 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01003529 if (is_pipe)
3530 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01003531
Stephane Eranian114382a2012-02-09 23:21:08 +01003532 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01003533 }
Stephane Eranian114382a2012-02-09 23:21:08 +01003534 /*
3535 * the new magic number serves two purposes:
3536 * - unique number to identify actual perf.data files
3537 * - encode endianness of file
3538 */
Namhyung Kimf7913972015-01-29 17:06:45 +09003539 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01003540
Stephane Eranian114382a2012-02-09 23:21:08 +01003541 /* check magic number with one endianness */
3542 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01003543 return 0;
3544
Stephane Eranian114382a2012-02-09 23:21:08 +01003545 /* check magic number with opposite endianness */
3546 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01003547 return -1;
3548
3549 ph->needs_swap = true;
3550
3551 return 0;
3552}
3553
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003554int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003555 struct perf_header *ph, int fd)
3556{
Jiri Olsa727ebd52013-11-28 11:30:14 +01003557 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01003558
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003559 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003560
Stephane Eranian73323f52012-02-02 13:54:44 +01003561 ret = readn(fd, header, sizeof(*header));
3562 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003563 return -1;
3564
Stephane Eranian114382a2012-02-09 23:21:08 +01003565 if (check_magic_endian(header->magic,
3566 header->attr_size, false, ph) < 0) {
3567 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01003568 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01003569 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003570
Stephane Eranian73323f52012-02-02 13:54:44 +01003571 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003572 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01003573 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003574 }
3575
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003576 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003577 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003578 if (header->size == offsetof(typeof(*header), adds_features))
3579 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003580 else
3581 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06003582 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06003583 /*
3584 * feature bitmap is declared as an array of unsigned longs --
3585 * not good since its size can differ between the host that
3586 * generated the data file and the host analyzing the file.
3587 *
3588 * We need to handle endianness, but we don't know the size of
3589 * the unsigned long where the file was generated. Take a best
3590 * guess at determining it: try 64-bit swap first (ie., file
3591 * created on a 64-bit host), and check if the hostname feature
3592 * bit is set (this feature bit is forced on as of fbe96f2).
3593 * If the bit is not, undo the 64-bit swap and try a 32-bit
3594 * swap. If the hostname bit is still not set (e.g., older data
3595 * file), punt and fallback to the original behavior --
3596 * clearing all feature bits and setting buildid.
3597 */
David Ahern80c01202012-06-08 11:47:51 -03003598 mem_bswap_64(&header->adds_features,
3599 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06003600
3601 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03003602 /* unswap as u64 */
3603 mem_bswap_64(&header->adds_features,
3604 BITS_TO_U64(HEADER_FEAT_BITS));
3605
3606 /* unswap as u32 */
3607 mem_bswap_32(&header->adds_features,
3608 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06003609 }
3610
3611 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3612 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3613 set_bit(HEADER_BUILD_ID, header->adds_features);
3614 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003615 }
3616
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003617 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003618 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003619
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003620 ph->data_offset = header->data.offset;
3621 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02003622 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003623 return 0;
3624}
3625
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003626static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003627 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003628 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003629{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003630 struct feat_fd fdd = {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07003631 .fd = fd,
3632 .ph = ph,
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003633 .size = section->size,
3634 .offset = section->offset,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07003635 };
3636
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003637 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02003638 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003639 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003640 return 0;
3641 }
3642
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003643 if (feat >= HEADER_LAST_FEATURE) {
3644 pr_debug("unknown feature %d, continuing...\n", feat);
3645 return 0;
3646 }
3647
Robert Richterf1c67db2012-02-10 15:41:56 +01003648 if (!feat_ops[feat].process)
3649 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003650
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003651 return feat_ops[feat].process(&fdd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003652}
3653
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003654static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Namhyung Kim60136662020-10-30 14:47:42 +09003655 struct perf_header *ph,
3656 struct perf_data* data,
Tom Zanussi454c4072010-05-01 01:41:20 -05003657 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003658{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003659 struct feat_fd ff = {
3660 .fd = STDOUT_FILENO,
3661 .ph = ph,
3662 };
Jiri Olsa727ebd52013-11-28 11:30:14 +01003663 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01003664
Namhyung Kim60136662020-10-30 14:47:42 +09003665 ret = perf_data__read(data, header, sizeof(*header));
Stephane Eranian73323f52012-02-02 13:54:44 +01003666 if (ret <= 0)
3667 return -1;
3668
Stephane Eranian114382a2012-02-09 23:21:08 +01003669 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
3670 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05003671 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01003672 }
3673
3674 if (ph->needs_swap)
3675 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05003676
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003677 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05003678 return -1;
3679
Tom Zanussi8dc58102010-04-01 23:59:15 -05003680 return 0;
3681}
3682
Jiri Olsad4339562013-07-17 19:49:41 +02003683static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05003684{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003685 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05003686 struct perf_pipe_file_header f_header;
3687
Namhyung Kim60136662020-10-30 14:47:42 +09003688 if (perf_file_header__read_pipe(&f_header, header, session->data,
Tom Zanussi454c4072010-05-01 01:41:20 -05003689 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05003690 pr_debug("incompatible file format\n");
3691 return -EINVAL;
3692 }
3693
Jiri Olsa14d3d542020-05-07 11:50:22 +02003694 return f_header.size == sizeof(f_header) ? 0 : -1;
Tom Zanussi8dc58102010-04-01 23:59:15 -05003695}
3696
Stephane Eranian69996df2012-02-09 23:21:06 +01003697static int read_attr(int fd, struct perf_header *ph,
3698 struct perf_file_attr *f_attr)
3699{
3700 struct perf_event_attr *attr = &f_attr->attr;
3701 size_t sz, left;
3702 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01003703 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01003704
3705 memset(f_attr, 0, sizeof(*f_attr));
3706
3707 /* read minimal guaranteed structure */
3708 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3709 if (ret <= 0) {
3710 pr_debug("cannot read %d bytes of header attr\n",
3711 PERF_ATTR_SIZE_VER0);
3712 return -1;
3713 }
3714
3715 /* on file perf_event_attr size */
3716 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01003717
Stephane Eranian69996df2012-02-09 23:21:06 +01003718 if (ph->needs_swap)
3719 sz = bswap_32(sz);
3720
3721 if (sz == 0) {
3722 /* assume ABI0 */
3723 sz = PERF_ATTR_SIZE_VER0;
3724 } else if (sz > our_sz) {
3725 pr_debug("file uses a more recent and unsupported ABI"
3726 " (%zu bytes extra)\n", sz - our_sz);
3727 return -1;
3728 }
3729 /* what we have not yet read and that we know about */
3730 left = sz - PERF_ATTR_SIZE_VER0;
3731 if (left) {
3732 void *ptr = attr;
3733 ptr += PERF_ATTR_SIZE_VER0;
3734
3735 ret = readn(fd, ptr, left);
3736 }
3737 /* read perf_file_section, ids are read in caller */
3738 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3739
3740 return ret <= 0 ? -1 : 0;
3741}
3742
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -03003743static int evsel__prepare_tracepoint_event(struct evsel *evsel, struct tep_handle *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003744{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003745 struct tep_event *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003746 char bf[128];
3747
Namhyung Kim831394b2012-09-06 11:10:46 +09003748 /* already prepared */
3749 if (evsel->tp_format)
3750 return 0;
3751
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09003752 if (pevent == NULL) {
3753 pr_debug("broken or missing trace data\n");
3754 return -1;
3755 }
3756
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003757 event = tep_find_event(pevent, evsel->core.attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09003758 if (event == NULL) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003759 pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003760 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09003761 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003762
Namhyung Kim831394b2012-09-06 11:10:46 +09003763 if (!evsel->name) {
3764 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3765 evsel->name = strdup(bf);
3766 if (evsel->name == NULL)
3767 return -1;
3768 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003769
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03003770 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003771 return 0;
3772}
3773
Arnaldo Carvalho de Melo1420ba22020-11-30 15:13:12 -03003774static int evlist__prepare_tracepoint_events(struct evlist *evlist, struct tep_handle *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003775{
Jiri Olsa32dcd022019-07-21 13:23:51 +02003776 struct evsel *pos;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003777
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003778 evlist__for_each_entry(evlist, pos) {
Jiri Olsa1fc632c2019-07-21 13:24:29 +02003779 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT &&
Arnaldo Carvalho de Melo56933022020-11-30 09:08:24 -03003780 evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003781 return -1;
3782 }
3783
3784 return 0;
3785}
3786
Jiri Olsad4339562013-07-17 19:49:41 +02003787int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05003788{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003789 struct perf_data *data = session->data;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003790 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003791 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003792 struct perf_file_attr f_attr;
3793 u64 f_id;
Jiri Olsa14d3d542020-05-07 11:50:22 +02003794 int nr_attrs, nr_ids, i, j, err;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003795 int fd = perf_data__fd(data);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003796
Jiri Olsa0f98b112019-07-21 13:23:55 +02003797 session->evlist = evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003798 if (session->evlist == NULL)
3799 return -ENOMEM;
3800
Kan Liang2c071442015-08-28 05:48:05 -04003801 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03003802 session->machines.host.env = &header->env;
Jiri Olsa14d3d542020-05-07 11:50:22 +02003803
3804 /*
3805 * We can read 'pipe' data event from regular file,
3806 * check for the pipe header regardless of source.
3807 */
3808 err = perf_header__read_pipe(session);
Jiapeng Chong52bcc602021-02-05 11:54:15 +08003809 if (!err || perf_data__is_pipe(data)) {
Jiri Olsa14d3d542020-05-07 11:50:22 +02003810 data->is_pipe = true;
3811 return err;
3812 }
Tom Zanussi8dc58102010-04-01 23:59:15 -05003813
Stephane Eranian69996df2012-02-09 23:21:06 +01003814 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003815 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003816
Namhyung Kimb314e5c2013-09-30 17:19:48 +09003817 /*
3818 * Sanity check that perf.data was written cleanly; data size is
3819 * initialized to 0 and updated only if the on_exit function is run.
3820 * If data size is still 0 then the file contains only partial
3821 * information. Just warn user and process it as much as it can.
3822 */
3823 if (f_header.data.size == 0) {
3824 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
3825 "Was the 'perf record' command properly terminated?\n",
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003826 data->file.path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09003827 }
3828
Vince Weaver76222362019-07-23 11:06:01 -04003829 if (f_header.attr_size == 0) {
3830 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
3831 "Was the 'perf record' command properly terminated?\n",
3832 data->file.path);
3833 return -EINVAL;
3834 }
3835
Stephane Eranian69996df2012-02-09 23:21:06 +01003836 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003837 lseek(fd, f_header.attrs.offset, SEEK_SET);
3838
3839 for (i = 0; i < nr_attrs; i++) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02003840 struct evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02003841 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003842
Stephane Eranian69996df2012-02-09 23:21:06 +01003843 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003844 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003845
David Ahern1060ab82015-04-09 16:15:46 -04003846 if (header->needs_swap) {
3847 f_attr.ids.size = bswap_64(f_attr.ids.size);
3848 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06003849 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04003850 }
David Aherneda39132011-07-15 12:34:09 -06003851
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02003852 tmp = lseek(fd, 0, SEEK_CUR);
Jiri Olsa365c3ae2019-07-21 13:23:58 +02003853 evsel = evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003854
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003855 if (evsel == NULL)
3856 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03003857
3858 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003859 /*
3860 * Do it before so that if perf_evsel__alloc_id fails, this
Jiri Olsac12995a2019-07-21 13:23:56 +02003861 * entry gets purged too at evlist__delete().
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003862 */
Jiri Olsaa1cf3a72019-07-21 13:23:59 +02003863 evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003864
3865 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003866 /*
3867 * We don't have the cpu and thread maps on the header, so
3868 * for allocating the perf_sample_id table we fake 1 cpu and
3869 * hattr->ids threads.
3870 */
Jiri Olsa70c20362019-09-03 10:34:29 +02003871 if (perf_evsel__alloc_id(&evsel->core, 1, nr_ids))
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003872 goto out_delete_evlist;
3873
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003874 lseek(fd, f_attr.ids.offset, SEEK_SET);
3875
3876 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003877 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003878 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003879
Jiri Olsab0031c22019-09-03 11:01:04 +02003880 perf_evlist__id_add(&session->evlist->core, &evsel->core, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003881 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02003882
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003883 lseek(fd, tmp, SEEK_SET);
3884 }
3885
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003886 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003887 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01003888
Arnaldo Carvalho de Melo1420ba22020-11-30 15:13:12 -03003889 if (evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003890 goto out_delete_evlist;
3891
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003892 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003893out_errno:
3894 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003895
3896out_delete_evlist:
Jiri Olsac12995a2019-07-21 13:23:56 +02003897 evlist__delete(session->evlist);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003898 session->evlist = NULL;
3899 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003900}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02003901
Jiri Olsa89f16882018-09-13 14:54:03 +02003902int perf_event__process_feature(struct perf_session *session,
3903 union perf_event *event)
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003904{
Jiri Olsa89f16882018-09-13 14:54:03 +02003905 struct perf_tool *tool = session->tool;
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003906 struct feat_fd ff = { .fd = 0 };
Jiri Olsa72932372019-08-28 15:57:16 +02003907 struct perf_record_header_feature *fe = (struct perf_record_header_feature *)event;
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003908 int type = fe->header.type;
3909 u64 feat = fe->feat_id;
3910
3911 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3912 pr_warning("invalid record type %d in pipe-mode\n", type);
3913 return 0;
3914 }
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303915 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003916 pr_warning("invalid record type %d in pipe-mode\n", type);
3917 return -1;
3918 }
3919
3920 if (!feat_ops[feat].process)
3921 return 0;
3922
3923 ff.buf = (void *)fe->data;
Jiri Olsa79b2fe52019-07-15 16:04:26 +02003924 ff.size = event->header.size - sizeof(*fe);
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003925 ff.ph = &session->header;
3926
3927 if (feat_ops[feat].process(&ff, NULL))
3928 return -1;
3929
3930 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3931 return 0;
3932
3933 if (!feat_ops[feat].full_only ||
3934 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3935 feat_ops[feat].print(&ff, stdout);
3936 } else {
3937 fprintf(stdout, "# %s info available, use -I to display\n",
3938 feat_ops[feat].name);
3939 }
3940
3941 return 0;
3942}
3943
Jiri Olsac853f932015-10-25 15:51:41 +01003944size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3945{
Jiri Olsa72932372019-08-28 15:57:16 +02003946 struct perf_record_event_update *ev = &event->event_update;
3947 struct perf_record_event_update_scale *ev_scale;
3948 struct perf_record_event_update_cpus *ev_cpus;
Jiri Olsaf8548392019-07-21 13:23:49 +02003949 struct perf_cpu_map *map;
Jiri Olsac853f932015-10-25 15:51:41 +01003950 size_t ret;
3951
Jiri Olsa5ded0682019-08-28 15:56:57 +02003952 ret = fprintf(fp, "\n... id: %" PRI_lu64 "\n", ev->id);
Jiri Olsac853f932015-10-25 15:51:41 +01003953
3954 switch (ev->type) {
3955 case PERF_EVENT_UPDATE__SCALE:
Jiri Olsa72932372019-08-28 15:57:16 +02003956 ev_scale = (struct perf_record_event_update_scale *)ev->data;
Jiri Olsac853f932015-10-25 15:51:41 +01003957 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3958 break;
3959 case PERF_EVENT_UPDATE__UNIT:
3960 ret += fprintf(fp, "... unit: %s\n", ev->data);
3961 break;
3962 case PERF_EVENT_UPDATE__NAME:
3963 ret += fprintf(fp, "... name: %s\n", ev->data);
3964 break;
3965 case PERF_EVENT_UPDATE__CPUS:
Jiri Olsa72932372019-08-28 15:57:16 +02003966 ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
Jiri Olsac853f932015-10-25 15:51:41 +01003967 ret += fprintf(fp, "... ");
3968
3969 map = cpu_map__new_data(&ev_cpus->cpus);
3970 if (map)
3971 ret += cpu_map__fprintf(map, fp);
3972 else
3973 ret += fprintf(fp, "failed to get cpus\n");
3974 break;
3975 default:
3976 ret += fprintf(fp, "... unknown type\n");
3977 break;
3978 }
3979
3980 return ret;
3981}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003982
Adrian Hunter47c3d102013-07-04 16:20:21 +03003983int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3984 union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02003985 struct evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003986{
Robert Richterf4d83432012-08-16 21:10:17 +02003987 u32 i, ids, n_ids;
Jiri Olsa32dcd022019-07-21 13:23:51 +02003988 struct evsel *evsel;
Jiri Olsa63503db2019-07-21 13:23:52 +02003989 struct evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003990
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003991 if (evlist == NULL) {
Jiri Olsa0f98b112019-07-21 13:23:55 +02003992 *pevlist = evlist = evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003993 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003994 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003995 }
3996
Jiri Olsa365c3ae2019-07-21 13:23:58 +02003997 evsel = evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003998 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003999 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05004000
Jiri Olsaa1cf3a72019-07-21 13:23:59 +02004001 evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03004002
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02004003 ids = event->header.size;
4004 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05004005 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03004006 /*
4007 * We don't have the cpu and thread maps on the header, so
4008 * for allocating the perf_sample_id table we fake 1 cpu and
4009 * hattr->ids threads.
4010 */
Jiri Olsa70c20362019-09-03 10:34:29 +02004011 if (perf_evsel__alloc_id(&evsel->core, 1, n_ids))
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03004012 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05004013
4014 for (i = 0; i < n_ids; i++) {
Jiri Olsab0031c22019-09-03 11:01:04 +02004015 perf_evlist__id_add(&evlist->core, &evsel->core, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05004016 }
4017
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05004018 return 0;
4019}
Tom Zanussicd19a032010-04-01 23:59:20 -05004020
Jiri Olsaffe777252015-10-25 15:51:36 +01004021int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
4022 union perf_event *event,
Jiri Olsa63503db2019-07-21 13:23:52 +02004023 struct evlist **pevlist)
Jiri Olsaffe777252015-10-25 15:51:36 +01004024{
Jiri Olsa72932372019-08-28 15:57:16 +02004025 struct perf_record_event_update *ev = &event->event_update;
4026 struct perf_record_event_update_scale *ev_scale;
4027 struct perf_record_event_update_cpus *ev_cpus;
Jiri Olsa63503db2019-07-21 13:23:52 +02004028 struct evlist *evlist;
Jiri Olsa32dcd022019-07-21 13:23:51 +02004029 struct evsel *evsel;
Jiri Olsaf8548392019-07-21 13:23:49 +02004030 struct perf_cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01004031
4032 if (!pevlist || *pevlist == NULL)
4033 return -EINVAL;
4034
4035 evlist = *pevlist;
4036
Arnaldo Carvalho de Melo3ccf8a72020-11-30 14:17:57 -03004037 evsel = evlist__id2evsel(evlist, ev->id);
Jiri Olsaffe777252015-10-25 15:51:36 +01004038 if (evsel == NULL)
4039 return -EINVAL;
4040
Jiri Olsaa6e52812015-10-25 15:51:37 +01004041 switch (ev->type) {
4042 case PERF_EVENT_UPDATE__UNIT:
4043 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01004044 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01004045 case PERF_EVENT_UPDATE__NAME:
4046 evsel->name = strdup(ev->data);
4047 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01004048 case PERF_EVENT_UPDATE__SCALE:
Jiri Olsa72932372019-08-28 15:57:16 +02004049 ev_scale = (struct perf_record_event_update_scale *)ev->data;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01004050 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03004051 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01004052 case PERF_EVENT_UPDATE__CPUS:
Jiri Olsa72932372019-08-28 15:57:16 +02004053 ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
Jiri Olsa86ebb092015-10-25 15:51:40 +01004054
4055 map = cpu_map__new_data(&ev_cpus->cpus);
4056 if (map)
Jiri Olsafe1f61b2019-07-21 13:24:38 +02004057 evsel->core.own_cpus = map;
Jiri Olsa86ebb092015-10-25 15:51:40 +01004058 else
4059 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01004060 default:
4061 break;
4062 }
4063
Jiri Olsaffe777252015-10-25 15:51:36 +01004064 return 0;
4065}
4066
Jiri Olsa89f16882018-09-13 14:54:03 +02004067int perf_event__process_tracing_data(struct perf_session *session,
4068 union perf_event *event)
Tom Zanussi92155452010-04-01 23:59:21 -05004069{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02004070 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01004071 int fd = perf_data__fd(session->data);
Tom Zanussi92155452010-04-01 23:59:21 -05004072 char buf[BUFSIZ];
4073
Jiri Olsab4911982020-05-07 11:50:21 +02004074 /*
4075 * The pipe fd is already in proper place and in any case
4076 * we can't move it, and we'd screw the case where we read
4077 * 'pipe' data from regular file. The trace_report reads
4078 * data from 'fd' so we need to set it directly behind the
4079 * event, where the tracing data starts.
4080 */
4081 if (!perf_data__is_pipe(session->data)) {
4082 off_t offset = lseek(fd, 0, SEEK_CUR);
4083
4084 /* setup for reading amidst mmap */
4085 lseek(fd, offset + sizeof(struct perf_record_header_tracing_data),
4086 SEEK_SET);
4087 }
Tom Zanussi92155452010-04-01 23:59:21 -05004088
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01004089 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03004090 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03004091 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05004092
Jiri Olsacc9784bd2013-10-15 16:27:34 +02004093 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03004094 pr_err("%s: reading input file", __func__);
4095 return -1;
4096 }
Tom Zanussi454c4072010-05-01 01:41:20 -05004097 if (session->repipe) {
4098 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03004099 if (retw <= 0 || retw != padding) {
4100 pr_err("%s: repiping tracing data padding", __func__);
4101 return -1;
4102 }
Tom Zanussi454c4072010-05-01 01:41:20 -05004103 }
Tom Zanussi92155452010-04-01 23:59:21 -05004104
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03004105 if (size_read + padding != size) {
4106 pr_err("%s: tracing data size mismatch", __func__);
4107 return -1;
4108 }
Tom Zanussi92155452010-04-01 23:59:21 -05004109
Arnaldo Carvalho de Melo1420ba22020-11-30 15:13:12 -03004110 evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03004111
Tom Zanussi92155452010-04-01 23:59:21 -05004112 return size_read + padding;
4113}
Tom Zanussic7929e42010-04-01 23:59:22 -05004114
Jiri Olsa89f16882018-09-13 14:54:03 +02004115int perf_event__process_build_id(struct perf_session *session,
4116 union perf_event *event)
Tom Zanussic7929e42010-04-01 23:59:22 -05004117{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02004118 __event_process_build_id(&event->build_id,
4119 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08004120 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05004121 return 0;
4122}