blob: dec6d218c31c326ceac4d4464c8c9ce012a1be61 [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 Meloa9072bc2011-10-26 12:41:38 -02004#include "util.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -03005#include "string2.h"
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -03006#include <sys/param.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02007#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02008#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02009#include <unistd.h>
10#include <stdio.h>
11#include <stdlib.h>
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -030012#include <linux/compiler.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010013#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -020014#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +010015#include <linux/bitops.h>
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -070016#include <linux/stringify.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030017#include <sys/stat.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020018#include <sys/utsname.h>
Jin Yao60115182017-12-08 21:13:41 +080019#include <linux/time64.h>
Jiri Olsae2091ce2018-03-07 16:50:08 +010020#include <dirent.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020021
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020022#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030023#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020024#include "header.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030025#include "memswap.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020026#include "../perf.h"
27#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020028#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010029#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010030#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020031#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020032#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020033#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090034#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020035#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020036#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010037#include <api/fs/fs.h>
38#include "asm/bug.h"
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -070039#include "tool.h"
Jin Yao60115182017-12-08 21:13:41 +080040#include "time-utils.h"
Jiri Olsae2091ce2018-03-07 16:50:08 +010041#include "units.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020042
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030043#include "sane_ctype.h"
44
Stephane Eranian73323f52012-02-02 13:54:44 +010045/*
46 * magic2 = "PERFILE2"
47 * must be a numerical value to let the endianness
48 * determine the memory layout. That way we are able
49 * to detect endianness when reading the perf.data file
50 * back.
51 *
52 * we check for legacy (PERFFILE) format.
53 */
54static const char *__perf_magic1 = "PERFFILE";
55static const u64 __perf_magic2 = 0x32454c4946524550ULL;
56static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020057
Stephane Eranian73323f52012-02-02 13:54:44 +010058#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020059
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090060const char perf_version_string[] = PERF_VERSION;
61
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020062struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020063 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020064 struct perf_file_section ids;
65};
66
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070067struct feat_fd {
68 struct perf_header *ph;
69 int fd;
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -070070 void *buf; /* Either buf != NULL or fd >= 0 */
David Carrillo-Cisneros62552452017-07-17 21:25:42 -070071 ssize_t offset;
72 size_t size;
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -070073 struct perf_evsel *events;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070074};
75
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030076void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020077{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030078 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020079}
80
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030081void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020082{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030083 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020084}
85
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030086bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020087{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030088 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020089}
90
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -070091static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
92{
93 ssize_t ret = writen(ff->fd, buf, size);
94
95 if (ret != (ssize_t)size)
96 return ret < 0 ? (int)ret : -1;
97 return 0;
98}
99
100static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
101{
102 /* struct perf_event_header::size is u16 */
103 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
104 size_t new_size = ff->size;
105 void *addr;
106
107 if (size + ff->offset > max_size)
108 return -E2BIG;
109
110 while (size > (new_size - ff->offset))
111 new_size <<= 1;
112 new_size = min(max_size, new_size);
113
114 if (ff->size < new_size) {
115 addr = realloc(ff->buf, new_size);
116 if (!addr)
117 return -ENOMEM;
118 ff->buf = addr;
119 ff->size = new_size;
120 }
121
122 memcpy(ff->buf + ff->offset, buf, size);
123 ff->offset += size;
124
125 return 0;
126}
127
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700128/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700129int do_write(struct feat_fd *ff, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200130{
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700131 if (!ff->buf)
132 return __do_write_fd(ff, buf, size);
133 return __do_write_buf(ff, buf, size);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200134}
135
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700136/* Return: 0 if succeded, -ERR if failed. */
Jiri Olsae2091ce2018-03-07 16:50:08 +0100137static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
138{
139 u64 *p = (u64 *) set;
140 int i, ret;
141
142 ret = do_write(ff, &size, sizeof(size));
143 if (ret < 0)
144 return ret;
145
146 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
147 ret = do_write(ff, p + i, sizeof(*p));
148 if (ret < 0)
149 return ret;
150 }
151
152 return 0;
153}
154
155/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700156int write_padded(struct feat_fd *ff, const void *bf,
157 size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200158{
159 static const char zero_buf[NAME_ALIGN];
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700160 int err = do_write(ff, bf, count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200161
162 if (!err)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700163 err = do_write(ff, zero_buf, count_aligned - count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200164
165 return err;
166}
167
Kan Liang2bb00d22015-09-01 09:58:12 -0400168#define string_size(str) \
169 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
170
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700171/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700172static int do_write_string(struct feat_fd *ff, const char *str)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200173{
174 u32 len, olen;
175 int ret;
176
177 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300178 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200179
180 /* write len, incl. \0 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700181 ret = do_write(ff, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200182 if (ret < 0)
183 return ret;
184
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700185 return write_padded(ff, str, olen, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200186}
187
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700188static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700189{
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700190 ssize_t ret = readn(ff->fd, addr, size);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700191
192 if (ret != size)
193 return ret < 0 ? (int)ret : -1;
194 return 0;
195}
196
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700197static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
198{
199 if (size > (ssize_t)ff->size - ff->offset)
200 return -1;
201
202 memcpy(addr, ff->buf + ff->offset, size);
203 ff->offset += size;
204
205 return 0;
206
207}
208
209static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
210{
211 if (!ff->buf)
212 return __do_read_fd(ff, addr, size);
213 return __do_read_buf(ff, addr, size);
214}
215
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700216static int do_read_u32(struct feat_fd *ff, u32 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700217{
218 int ret;
219
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700220 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700221 if (ret)
222 return ret;
223
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700224 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700225 *addr = bswap_32(*addr);
226 return 0;
227}
228
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700229static int do_read_u64(struct feat_fd *ff, u64 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700230{
231 int ret;
232
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700233 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700234 if (ret)
235 return ret;
236
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700237 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700238 *addr = bswap_64(*addr);
239 return 0;
240}
241
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700242static char *do_read_string(struct feat_fd *ff)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200243{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200244 u32 len;
245 char *buf;
246
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700247 if (do_read_u32(ff, &len))
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200248 return NULL;
249
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200250 buf = malloc(len);
251 if (!buf)
252 return NULL;
253
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700254 if (!__do_read(ff, buf, len)) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200255 /*
256 * strings are padded by zeroes
257 * thus the actual strlen of buf
258 * may be less than len
259 */
260 return buf;
261 }
262
263 free(buf);
264 return NULL;
265}
266
Jiri Olsae2091ce2018-03-07 16:50:08 +0100267/* Return: 0 if succeded, -ERR if failed. */
268static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
269{
270 unsigned long *set;
271 u64 size, *p;
272 int i, ret;
273
274 ret = do_read_u64(ff, &size);
275 if (ret)
276 return ret;
277
278 set = bitmap_alloc(size);
279 if (!set)
280 return -ENOMEM;
281
Jiri Olsae2091ce2018-03-07 16:50:08 +0100282 p = (u64 *) set;
283
284 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
285 ret = do_read_u64(ff, p + i);
286 if (ret < 0) {
287 free(set);
288 return ret;
289 }
290 }
291
292 *pset = set;
293 *psize = size;
294 return 0;
295}
296
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700297static int write_tracing_data(struct feat_fd *ff,
298 struct perf_evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200299{
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700300 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
301 return -1;
302
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700303 return read_tracing_data(ff->fd, &evlist->entries);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200304}
305
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700306static int write_build_id(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300307 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200308{
309 struct perf_session *session;
310 int err;
311
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700312 session = container_of(ff->ph, struct perf_session, header);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200313
Robert Richtere20960c2011-12-07 10:02:55 +0100314 if (!perf_session__read_build_ids(session, true))
315 return -1;
316
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700317 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
318 return -1;
319
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700320 err = perf_session__write_buildid_table(session, ff);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200321 if (err < 0) {
322 pr_debug("failed to write buildid table\n");
323 return err;
324 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900325 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200326
327 return 0;
328}
329
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700330static int write_hostname(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300331 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200332{
333 struct utsname uts;
334 int ret;
335
336 ret = uname(&uts);
337 if (ret < 0)
338 return -1;
339
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700340 return do_write_string(ff, uts.nodename);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200341}
342
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700343static int write_osrelease(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300344 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200345{
346 struct utsname uts;
347 int ret;
348
349 ret = uname(&uts);
350 if (ret < 0)
351 return -1;
352
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700353 return do_write_string(ff, uts.release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200354}
355
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700356static int write_arch(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300357 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200358{
359 struct utsname uts;
360 int ret;
361
362 ret = uname(&uts);
363 if (ret < 0)
364 return -1;
365
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700366 return do_write_string(ff, uts.machine);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200367}
368
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700369static int write_version(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300370 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200371{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700372 return do_write_string(ff, perf_version_string);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200373}
374
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700375static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200376{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200377 FILE *file;
378 char *buf = NULL;
379 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800380 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200381 size_t len = 0;
382 int ret = -1;
383
384 if (!search)
385 return -1;
386
387 file = fopen("/proc/cpuinfo", "r");
388 if (!file)
389 return -1;
390
391 while (getline(&buf, &len, file) > 0) {
392 ret = strncmp(buf, search, strlen(search));
393 if (!ret)
394 break;
395 }
396
Wang Naned307752014-10-16 11:08:29 +0800397 if (ret) {
398 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200399 goto done;
Wang Naned307752014-10-16 11:08:29 +0800400 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200401
402 s = buf;
403
404 p = strchr(buf, ':');
405 if (p && *(p+1) == ' ' && *(p+2))
406 s = p + 2;
407 p = strchr(s, '\n');
408 if (p)
409 *p = '\0';
410
411 /* squash extra space characters (branding string) */
412 p = s;
413 while (*p) {
414 if (isspace(*p)) {
415 char *r = p + 1;
416 char *q = r;
417 *p = ' ';
418 while (*q && isspace(*q))
419 q++;
420 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,
Wang Nan493c3032014-10-24 09:45:26 +0800433 struct perf_evlist *evlist __maybe_unused)
434{
Wang Nan493c3032014-10-24 09:45:26 +0800435 const char *cpuinfo_procs[] = CPUINFO_PROC;
436 unsigned int i;
437
438 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
439 int ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700440 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
Wang Nan493c3032014-10-24 09:45:26 +0800441 if (ret >= 0)
442 return ret;
443 }
444 return -1;
445}
446
447
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700448static int write_nrcpus(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300449 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200450{
451 long nr;
452 u32 nrc, nra;
453 int ret;
454
Jan Stancekda8a58b2017-02-17 12:10:26 +0100455 nrc = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200456
457 nr = sysconf(_SC_NPROCESSORS_ONLN);
458 if (nr < 0)
459 return -1;
460
461 nra = (u32)(nr & UINT_MAX);
462
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700463 ret = do_write(ff, &nrc, sizeof(nrc));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200464 if (ret < 0)
465 return ret;
466
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700467 return do_write(ff, &nra, sizeof(nra));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200468}
469
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700470static int write_event_desc(struct feat_fd *ff,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200471 struct perf_evlist *evlist)
472{
Robert Richter6606f872012-08-16 21:10:19 +0200473 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900474 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200475 int ret;
476
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900477 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200478
479 /*
480 * write number of events
481 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700482 ret = do_write(ff, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200483 if (ret < 0)
484 return ret;
485
486 /*
487 * size of perf_event_attr struct
488 */
Robert Richter6606f872012-08-16 21:10:19 +0200489 sz = (u32)sizeof(evsel->attr);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700490 ret = do_write(ff, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200491 if (ret < 0)
492 return ret;
493
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300494 evlist__for_each_entry(evlist, evsel) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700495 ret = do_write(ff, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200496 if (ret < 0)
497 return ret;
498 /*
499 * write number of unique id per event
500 * there is one id per instance of an event
501 *
502 * copy into an nri to be independent of the
503 * type of ids,
504 */
Robert Richter6606f872012-08-16 21:10:19 +0200505 nri = evsel->ids;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700506 ret = do_write(ff, &nri, sizeof(nri));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200507 if (ret < 0)
508 return ret;
509
510 /*
511 * write event string as passed on cmdline
512 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700513 ret = do_write_string(ff, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200514 if (ret < 0)
515 return ret;
516 /*
517 * write unique ids for this event
518 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700519 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200520 if (ret < 0)
521 return ret;
522 }
523 return 0;
524}
525
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700526static int write_cmdline(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300527 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200528{
529 char buf[MAXPATHLEN];
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300530 u32 n;
531 int i, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200532
Tommi Rantala55f771282017-03-22 15:06:24 +0200533 /* actual path to perf binary */
534 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200535 if (ret <= 0)
536 return -1;
537
538 /* readlink() does not add null termination */
539 buf[ret] = '\0';
540
541 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300542 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200543
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700544 ret = do_write(ff, &n, sizeof(n));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200545 if (ret < 0)
546 return ret;
547
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700548 ret = do_write_string(ff, buf);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200549 if (ret < 0)
550 return ret;
551
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300552 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700553 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200554 if (ret < 0)
555 return ret;
556 }
557 return 0;
558}
559
560#define CORE_SIB_FMT \
561 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
562#define THRD_SIB_FMT \
563 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
564
565struct cpu_topo {
Kan Liang2bb00d22015-09-01 09:58:12 -0400566 u32 cpu_nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200567 u32 core_sib;
568 u32 thread_sib;
569 char **core_siblings;
570 char **thread_siblings;
571};
572
573static int build_cpu_topo(struct cpu_topo *tp, int cpu)
574{
575 FILE *fp;
576 char filename[MAXPATHLEN];
577 char *buf = NULL, *p;
578 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200579 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200580 u32 i = 0;
581 int ret = -1;
582
583 sprintf(filename, CORE_SIB_FMT, cpu);
584 fp = fopen(filename, "r");
585 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200586 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200587
Stephane Eranianc5885742013-08-14 12:04:26 +0200588 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200589 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200590 if (sret <= 0)
591 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200592
593 p = strchr(buf, '\n');
594 if (p)
595 *p = '\0';
596
597 for (i = 0; i < tp->core_sib; i++) {
598 if (!strcmp(buf, tp->core_siblings[i]))
599 break;
600 }
601 if (i == tp->core_sib) {
602 tp->core_siblings[i] = buf;
603 tp->core_sib++;
604 buf = NULL;
605 len = 0;
606 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200607 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200608
Stephane Eranianc5885742013-08-14 12:04:26 +0200609try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200610 sprintf(filename, THRD_SIB_FMT, cpu);
611 fp = fopen(filename, "r");
612 if (!fp)
613 goto done;
614
615 if (getline(&buf, &len, fp) <= 0)
616 goto done;
617
618 p = strchr(buf, '\n');
619 if (p)
620 *p = '\0';
621
622 for (i = 0; i < tp->thread_sib; i++) {
623 if (!strcmp(buf, tp->thread_siblings[i]))
624 break;
625 }
626 if (i == tp->thread_sib) {
627 tp->thread_siblings[i] = buf;
628 tp->thread_sib++;
629 buf = NULL;
630 }
631 ret = 0;
632done:
633 if(fp)
634 fclose(fp);
635 free(buf);
636 return ret;
637}
638
639static void free_cpu_topo(struct cpu_topo *tp)
640{
641 u32 i;
642
643 if (!tp)
644 return;
645
646 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300647 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200648
649 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300650 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200651
652 free(tp);
653}
654
655static struct cpu_topo *build_cpu_topology(void)
656{
Jan Stancek43db2842017-02-17 12:10:25 +0100657 struct cpu_topo *tp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200658 void *addr;
659 u32 nr, i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300660 size_t sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200661 long ncpus;
662 int ret = -1;
Jan Stancek43db2842017-02-17 12:10:25 +0100663 struct cpu_map *map;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200664
Jan Stancekda8a58b2017-02-17 12:10:26 +0100665 ncpus = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200666
Jan Stancek43db2842017-02-17 12:10:25 +0100667 /* build online CPU map */
668 map = cpu_map__new(NULL);
669 if (map == NULL) {
670 pr_debug("failed to get system cpumap\n");
671 return NULL;
672 }
673
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200674 nr = (u32)(ncpus & UINT_MAX);
675
676 sz = nr * sizeof(char *);
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300677 addr = calloc(1, sizeof(*tp) + 2 * sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200678 if (!addr)
Jan Stancek43db2842017-02-17 12:10:25 +0100679 goto out_free;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200680
681 tp = addr;
Kan Liang2bb00d22015-09-01 09:58:12 -0400682 tp->cpu_nr = nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200683 addr += sizeof(*tp);
684 tp->core_siblings = addr;
685 addr += sz;
686 tp->thread_siblings = addr;
687
688 for (i = 0; i < nr; i++) {
Jan Stancek43db2842017-02-17 12:10:25 +0100689 if (!cpu_map__has(map, i))
690 continue;
691
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200692 ret = build_cpu_topo(tp, i);
693 if (ret < 0)
694 break;
695 }
Jan Stancek43db2842017-02-17 12:10:25 +0100696
697out_free:
698 cpu_map__put(map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200699 if (ret) {
700 free_cpu_topo(tp);
701 tp = NULL;
702 }
703 return tp;
704}
705
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700706static int write_cpu_topology(struct feat_fd *ff,
707 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200708{
709 struct cpu_topo *tp;
710 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300711 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200712
713 tp = build_cpu_topology();
714 if (!tp)
715 return -1;
716
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700717 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200718 if (ret < 0)
719 goto done;
720
721 for (i = 0; i < tp->core_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700722 ret = do_write_string(ff, tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200723 if (ret < 0)
724 goto done;
725 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700726 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200727 if (ret < 0)
728 goto done;
729
730 for (i = 0; i < tp->thread_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700731 ret = do_write_string(ff, tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200732 if (ret < 0)
733 break;
734 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400735
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300736 ret = perf_env__read_cpu_topology_map(&perf_env);
737 if (ret < 0)
738 goto done;
739
740 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700741 ret = do_write(ff, &perf_env.cpu[j].core_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300742 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400743 if (ret < 0)
744 return ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700745 ret = do_write(ff, &perf_env.cpu[j].socket_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300746 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400747 if (ret < 0)
748 return ret;
749 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200750done:
751 free_cpu_topo(tp);
752 return ret;
753}
754
755
756
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700757static int write_total_mem(struct feat_fd *ff,
758 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200759{
760 char *buf = NULL;
761 FILE *fp;
762 size_t len = 0;
763 int ret = -1, n;
764 uint64_t mem;
765
766 fp = fopen("/proc/meminfo", "r");
767 if (!fp)
768 return -1;
769
770 while (getline(&buf, &len, fp) > 0) {
771 ret = strncmp(buf, "MemTotal:", 9);
772 if (!ret)
773 break;
774 }
775 if (!ret) {
776 n = sscanf(buf, "%*s %"PRIu64, &mem);
777 if (n == 1)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700778 ret = do_write(ff, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800779 } else
780 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200781 free(buf);
782 fclose(fp);
783 return ret;
784}
785
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700786static int write_topo_node(struct feat_fd *ff, int node)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200787{
788 char str[MAXPATHLEN];
789 char field[32];
790 char *buf = NULL, *p;
791 size_t len = 0;
792 FILE *fp;
793 u64 mem_total, mem_free, mem;
794 int ret = -1;
795
796 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
797 fp = fopen(str, "r");
798 if (!fp)
799 return -1;
800
801 while (getline(&buf, &len, fp) > 0) {
802 /* skip over invalid lines */
803 if (!strchr(buf, ':'))
804 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100805 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200806 goto done;
807 if (!strcmp(field, "MemTotal:"))
808 mem_total = mem;
809 if (!strcmp(field, "MemFree:"))
810 mem_free = mem;
811 }
812
813 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100814 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200815
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700816 ret = do_write(ff, &mem_total, sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200817 if (ret)
818 goto done;
819
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700820 ret = do_write(ff, &mem_free, sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200821 if (ret)
822 goto done;
823
824 ret = -1;
825 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
826
827 fp = fopen(str, "r");
828 if (!fp)
829 goto done;
830
831 if (getline(&buf, &len, fp) <= 0)
832 goto done;
833
834 p = strchr(buf, '\n');
835 if (p)
836 *p = '\0';
837
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700838 ret = do_write_string(ff, buf);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200839done:
840 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100841 if (fp)
842 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200843 return ret;
844}
845
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700846static int write_numa_topology(struct feat_fd *ff,
847 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200848{
849 char *buf = NULL;
850 size_t len = 0;
851 FILE *fp;
852 struct cpu_map *node_map = NULL;
853 char *c;
854 u32 nr, i, j;
855 int ret = -1;
856
857 fp = fopen("/sys/devices/system/node/online", "r");
858 if (!fp)
859 return -1;
860
861 if (getline(&buf, &len, fp) <= 0)
862 goto done;
863
864 c = strchr(buf, '\n');
865 if (c)
866 *c = '\0';
867
868 node_map = cpu_map__new(buf);
869 if (!node_map)
870 goto done;
871
872 nr = (u32)node_map->nr;
873
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700874 ret = do_write(ff, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200875 if (ret < 0)
876 goto done;
877
878 for (i = 0; i < nr; i++) {
879 j = (u32)node_map->map[i];
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700880 ret = do_write(ff, &j, sizeof(j));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200881 if (ret < 0)
882 break;
883
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700884 ret = write_topo_node(ff, i);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200885 if (ret < 0)
886 break;
887 }
888done:
889 free(buf);
890 fclose(fp);
Masami Hiramatsu5191d8872015-12-09 11:11:35 +0900891 cpu_map__put(node_map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200892 return ret;
893}
894
895/*
Robert Richter50a96672012-08-16 21:10:24 +0200896 * File format:
897 *
898 * struct pmu_mappings {
899 * u32 pmu_num;
900 * struct pmu_map {
901 * u32 type;
902 * char name[];
903 * }[pmu_num];
904 * };
905 */
906
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700907static int write_pmu_mappings(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300908 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200909{
910 struct perf_pmu *pmu = NULL;
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700911 u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900912 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200913
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700914 /*
915 * Do a first pass to count number of pmu to avoid lseek so this
916 * works in pipe mode as well.
917 */
918 while ((pmu = perf_pmu__scan(pmu))) {
919 if (!pmu->name)
920 continue;
921 pmu_num++;
922 }
923
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700924 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
Namhyung Kim5323f602012-12-17 15:38:54 +0900925 if (ret < 0)
926 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200927
928 while ((pmu = perf_pmu__scan(pmu))) {
929 if (!pmu->name)
930 continue;
Namhyung Kim5323f602012-12-17 15:38:54 +0900931
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700932 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
Namhyung Kim5323f602012-12-17 15:38:54 +0900933 if (ret < 0)
934 return ret;
935
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700936 ret = do_write_string(ff, pmu->name);
Namhyung Kim5323f602012-12-17 15:38:54 +0900937 if (ret < 0)
938 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200939 }
940
Robert Richter50a96672012-08-16 21:10:24 +0200941 return 0;
942}
943
944/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900945 * File format:
946 *
947 * struct group_descs {
948 * u32 nr_groups;
949 * struct group_desc {
950 * char name[];
951 * u32 leader_idx;
952 * u32 nr_members;
953 * }[nr_groups];
954 * };
955 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700956static int write_group_desc(struct feat_fd *ff,
Namhyung Kima8bb5592013-01-22 18:09:31 +0900957 struct perf_evlist *evlist)
958{
959 u32 nr_groups = evlist->nr_groups;
960 struct perf_evsel *evsel;
961 int ret;
962
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700963 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900964 if (ret < 0)
965 return ret;
966
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300967 evlist__for_each_entry(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900968 if (perf_evsel__is_group_leader(evsel) &&
969 evsel->nr_members > 1) {
970 const char *name = evsel->group_name ?: "{anon_group}";
971 u32 leader_idx = evsel->idx;
972 u32 nr_members = evsel->nr_members;
973
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700974 ret = do_write_string(ff, name);
Namhyung Kima8bb5592013-01-22 18:09:31 +0900975 if (ret < 0)
976 return ret;
977
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700978 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900979 if (ret < 0)
980 return ret;
981
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700982 ret = do_write(ff, &nr_members, sizeof(nr_members));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900983 if (ret < 0)
984 return ret;
985 }
986 }
987 return 0;
988}
989
990/*
Kan Liangf4a07422018-11-21 08:49:39 -0800991 * Return the CPU id as a raw string.
992 *
993 * Each architecture should provide a more precise id string that
994 * can be use to match the architecture's "mapfile".
995 */
996char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
997{
998 return NULL;
999}
1000
1001/* Return zero when the cpuid from the mapfile.csv matches the
1002 * cpuid string generated on this platform.
1003 * Otherwise return non-zero.
1004 */
1005int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
1006{
1007 regex_t re;
1008 regmatch_t pmatch[1];
1009 int match;
1010
1011 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
1012 /* Warn unable to generate match particular string. */
1013 pr_info("Invalid regular expression %s\n", mapcpuid);
1014 return 1;
1015 }
1016
1017 match = !regexec(&re, cpuid, 1, pmatch, 0);
1018 regfree(&re);
1019 if (match) {
1020 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
1021
1022 /* Verify the entire string matched. */
1023 if (match_len == strlen(cpuid))
1024 return 0;
1025 }
1026 return 1;
1027}
1028
1029/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001030 * default get_cpuid(): nothing gets recorded
Jiada Wang7a759cd2017-04-09 20:02:37 -07001031 * actual implementation must be in arch/$(SRCARCH)/util/header.c
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001032 */
Rui Teng11d8f872016-07-28 10:05:57 +08001033int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001034{
1035 return -1;
1036}
1037
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001038static int write_cpuid(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001039 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001040{
1041 char buffer[64];
1042 int ret;
1043
1044 ret = get_cpuid(buffer, sizeof(buffer));
1045 if (!ret)
1046 goto write_it;
1047
1048 return -1;
1049write_it:
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001050 return do_write_string(ff, buffer);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001051}
1052
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001053static int write_branch_stack(struct feat_fd *ff __maybe_unused,
1054 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +01001055{
1056 return 0;
1057}
1058
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001059static int write_auxtrace(struct feat_fd *ff,
Adrian Hunter4025ea42015-04-09 18:53:41 +03001060 struct perf_evlist *evlist __maybe_unused)
1061{
Adrian Hunter99fa2982015-04-30 17:37:25 +03001062 struct perf_session *session;
1063 int err;
1064
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -07001065 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
1066 return -1;
1067
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001068 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +03001069
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001070 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
Adrian Hunter99fa2982015-04-30 17:37:25 +03001071 if (err < 0)
1072 pr_err("Failed to write auxtrace index\n");
1073 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +03001074}
1075
Alexey Budankovcf790512018-10-09 17:36:24 +03001076static int write_clockid(struct feat_fd *ff,
1077 struct perf_evlist *evlist __maybe_unused)
1078{
1079 return do_write(ff, &ff->ph->env.clockid_res_ns,
1080 sizeof(ff->ph->env.clockid_res_ns));
1081}
1082
Jiri Olsa720e98b2016-02-16 16:01:43 +01001083static int cpu_cache_level__sort(const void *a, const void *b)
1084{
1085 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
1086 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1087
1088 return cache_a->level - cache_b->level;
1089}
1090
1091static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1092{
1093 if (a->level != b->level)
1094 return false;
1095
1096 if (a->line_size != b->line_size)
1097 return false;
1098
1099 if (a->sets != b->sets)
1100 return false;
1101
1102 if (a->ways != b->ways)
1103 return false;
1104
1105 if (strcmp(a->type, b->type))
1106 return false;
1107
1108 if (strcmp(a->size, b->size))
1109 return false;
1110
1111 if (strcmp(a->map, b->map))
1112 return false;
1113
1114 return true;
1115}
1116
1117static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1118{
1119 char path[PATH_MAX], file[PATH_MAX];
1120 struct stat st;
1121 size_t len;
1122
1123 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1124 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1125
1126 if (stat(file, &st))
1127 return 1;
1128
1129 scnprintf(file, PATH_MAX, "%s/level", path);
1130 if (sysfs__read_int(file, (int *) &cache->level))
1131 return -1;
1132
1133 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1134 if (sysfs__read_int(file, (int *) &cache->line_size))
1135 return -1;
1136
1137 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1138 if (sysfs__read_int(file, (int *) &cache->sets))
1139 return -1;
1140
1141 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1142 if (sysfs__read_int(file, (int *) &cache->ways))
1143 return -1;
1144
1145 scnprintf(file, PATH_MAX, "%s/type", path);
1146 if (sysfs__read_str(file, &cache->type, &len))
1147 return -1;
1148
1149 cache->type[len] = 0;
1150 cache->type = rtrim(cache->type);
1151
1152 scnprintf(file, PATH_MAX, "%s/size", path);
1153 if (sysfs__read_str(file, &cache->size, &len)) {
1154 free(cache->type);
1155 return -1;
1156 }
1157
1158 cache->size[len] = 0;
1159 cache->size = rtrim(cache->size);
1160
1161 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1162 if (sysfs__read_str(file, &cache->map, &len)) {
1163 free(cache->map);
1164 free(cache->type);
1165 return -1;
1166 }
1167
1168 cache->map[len] = 0;
1169 cache->map = rtrim(cache->map);
1170 return 0;
1171}
1172
1173static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1174{
1175 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1176}
1177
1178static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1179{
1180 u32 i, cnt = 0;
1181 long ncpus;
1182 u32 nr, cpu;
1183 u16 level;
1184
1185 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1186 if (ncpus < 0)
1187 return -1;
1188
1189 nr = (u32)(ncpus & UINT_MAX);
1190
1191 for (cpu = 0; cpu < nr; cpu++) {
1192 for (level = 0; level < 10; level++) {
1193 struct cpu_cache_level c;
1194 int err;
1195
1196 err = cpu_cache_level__read(&c, cpu, level);
1197 if (err < 0)
1198 return err;
1199
1200 if (err == 1)
1201 break;
1202
1203 for (i = 0; i < cnt; i++) {
1204 if (cpu_cache_level__cmp(&c, &caches[i]))
1205 break;
1206 }
1207
1208 if (i == cnt)
1209 caches[cnt++] = c;
1210 else
1211 cpu_cache_level__free(&c);
1212
1213 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1214 goto out;
1215 }
1216 }
1217 out:
1218 *cntp = cnt;
1219 return 0;
1220}
1221
1222#define MAX_CACHES 2000
1223
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001224static int write_cache(struct feat_fd *ff,
1225 struct perf_evlist *evlist __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001226{
1227 struct cpu_cache_level caches[MAX_CACHES];
1228 u32 cnt = 0, i, version = 1;
1229 int ret;
1230
1231 ret = build_caches(caches, MAX_CACHES, &cnt);
1232 if (ret)
1233 goto out;
1234
1235 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1236
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001237 ret = do_write(ff, &version, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001238 if (ret < 0)
1239 goto out;
1240
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001241 ret = do_write(ff, &cnt, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001242 if (ret < 0)
1243 goto out;
1244
1245 for (i = 0; i < cnt; i++) {
1246 struct cpu_cache_level *c = &caches[i];
1247
1248 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001249 ret = do_write(ff, &c->v, sizeof(u32)); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001250 if (ret < 0) \
1251 goto out;
1252
1253 _W(level)
1254 _W(line_size)
1255 _W(sets)
1256 _W(ways)
1257 #undef _W
1258
1259 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001260 ret = do_write_string(ff, (const char *) c->v); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001261 if (ret < 0) \
1262 goto out;
1263
1264 _W(type)
1265 _W(size)
1266 _W(map)
1267 #undef _W
1268 }
1269
1270out:
1271 for (i = 0; i < cnt; i++)
1272 cpu_cache_level__free(&caches[i]);
1273 return ret;
1274}
1275
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001276static int write_stat(struct feat_fd *ff __maybe_unused,
Jiri Olsaffa517a2015-10-25 15:51:43 +01001277 struct perf_evlist *evlist __maybe_unused)
1278{
1279 return 0;
1280}
1281
Jin Yao60115182017-12-08 21:13:41 +08001282static int write_sample_time(struct feat_fd *ff,
1283 struct perf_evlist *evlist)
1284{
1285 int ret;
1286
1287 ret = do_write(ff, &evlist->first_sample_time,
1288 sizeof(evlist->first_sample_time));
1289 if (ret < 0)
1290 return ret;
1291
1292 return do_write(ff, &evlist->last_sample_time,
1293 sizeof(evlist->last_sample_time));
1294}
1295
Jiri Olsae2091ce2018-03-07 16:50:08 +01001296
1297static int memory_node__read(struct memory_node *n, unsigned long idx)
1298{
1299 unsigned int phys, size = 0;
1300 char path[PATH_MAX];
1301 struct dirent *ent;
1302 DIR *dir;
1303
1304#define for_each_memory(mem, dir) \
1305 while ((ent = readdir(dir))) \
1306 if (strcmp(ent->d_name, ".") && \
1307 strcmp(ent->d_name, "..") && \
1308 sscanf(ent->d_name, "memory%u", &mem) == 1)
1309
1310 scnprintf(path, PATH_MAX,
1311 "%s/devices/system/node/node%lu",
1312 sysfs__mountpoint(), idx);
1313
1314 dir = opendir(path);
1315 if (!dir) {
1316 pr_warning("failed: cant' open memory sysfs data\n");
1317 return -1;
1318 }
1319
1320 for_each_memory(phys, dir) {
1321 size = max(phys, size);
1322 }
1323
1324 size++;
1325
1326 n->set = bitmap_alloc(size);
1327 if (!n->set) {
1328 closedir(dir);
1329 return -ENOMEM;
1330 }
1331
Jiri Olsae2091ce2018-03-07 16:50:08 +01001332 n->node = idx;
1333 n->size = size;
1334
1335 rewinddir(dir);
1336
1337 for_each_memory(phys, dir) {
1338 set_bit(phys, n->set);
1339 }
1340
1341 closedir(dir);
1342 return 0;
1343}
1344
1345static int memory_node__sort(const void *a, const void *b)
1346{
1347 const struct memory_node *na = a;
1348 const struct memory_node *nb = b;
1349
1350 return na->node - nb->node;
1351}
1352
1353static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1354{
1355 char path[PATH_MAX];
1356 struct dirent *ent;
1357 DIR *dir;
1358 u64 cnt = 0;
1359 int ret = 0;
1360
1361 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1362 sysfs__mountpoint());
1363
1364 dir = opendir(path);
1365 if (!dir) {
Thomas Richter4f75f1cb2018-04-12 15:32:46 +02001366 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1367 __func__, path);
Jiri Olsae2091ce2018-03-07 16:50:08 +01001368 return -1;
1369 }
1370
1371 while (!ret && (ent = readdir(dir))) {
1372 unsigned int idx;
1373 int r;
1374
1375 if (!strcmp(ent->d_name, ".") ||
1376 !strcmp(ent->d_name, ".."))
1377 continue;
1378
1379 r = sscanf(ent->d_name, "node%u", &idx);
1380 if (r != 1)
1381 continue;
1382
1383 if (WARN_ONCE(cnt >= size,
1384 "failed to write MEM_TOPOLOGY, way too many nodes\n"))
1385 return -1;
1386
1387 ret = memory_node__read(&nodes[cnt++], idx);
1388 }
1389
1390 *cntp = cnt;
1391 closedir(dir);
1392
1393 if (!ret)
1394 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1395
1396 return ret;
1397}
1398
1399#define MAX_MEMORY_NODES 2000
1400
1401/*
1402 * The MEM_TOPOLOGY holds physical memory map for every
1403 * node in system. The format of data is as follows:
1404 *
1405 * 0 - version | for future changes
1406 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1407 * 16 - count | number of nodes
1408 *
1409 * For each node we store map of physical indexes for
1410 * each node:
1411 *
1412 * 32 - node id | node index
1413 * 40 - size | size of bitmap
1414 * 48 - bitmap | bitmap of memory indexes that belongs to node
1415 */
1416static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1417 struct perf_evlist *evlist __maybe_unused)
1418{
1419 static struct memory_node nodes[MAX_MEMORY_NODES];
1420 u64 bsize, version = 1, i, nr;
1421 int ret;
1422
1423 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1424 (unsigned long long *) &bsize);
1425 if (ret)
1426 return ret;
1427
1428 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1429 if (ret)
1430 return ret;
1431
1432 ret = do_write(ff, &version, sizeof(version));
1433 if (ret < 0)
1434 goto out;
1435
1436 ret = do_write(ff, &bsize, sizeof(bsize));
1437 if (ret < 0)
1438 goto out;
1439
1440 ret = do_write(ff, &nr, sizeof(nr));
1441 if (ret < 0)
1442 goto out;
1443
1444 for (i = 0; i < nr; i++) {
1445 struct memory_node *n = &nodes[i];
1446
1447 #define _W(v) \
1448 ret = do_write(ff, &n->v, sizeof(n->v)); \
1449 if (ret < 0) \
1450 goto out;
1451
1452 _W(node)
1453 _W(size)
1454
1455 #undef _W
1456
1457 ret = do_write_bitmap(ff, n->set, n->size);
1458 if (ret < 0)
1459 goto out;
1460 }
1461
1462out:
1463 return ret;
1464}
1465
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001466static void print_hostname(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001467{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001468 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001469}
1470
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001471static void print_osrelease(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001472{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001473 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001474}
1475
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001476static void print_arch(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001477{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001478 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001479}
1480
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001481static void print_cpudesc(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001482{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001483 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001484}
1485
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001486static void print_nrcpus(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, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1489 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001490}
1491
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001492static void print_version(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001493{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001494 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001495}
1496
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001497static void print_cmdline(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001498{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001499 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001500
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001501 nr = ff->ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001502
1503 fprintf(fp, "# cmdline : ");
1504
Alexey Budankovf92da712018-06-04 09:50:56 +03001505 for (i = 0; i < nr; i++) {
1506 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1507 if (!argv_i) {
1508 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1509 } else {
1510 char *mem = argv_i;
1511 do {
1512 char *quote = strchr(argv_i, '\'');
1513 if (!quote)
1514 break;
1515 *quote++ = '\0';
1516 fprintf(fp, "%s\\\'", argv_i);
1517 argv_i = quote;
1518 } while (1);
1519 fprintf(fp, "%s ", argv_i);
1520 free(mem);
1521 }
1522 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001523 fputc('\n', fp);
1524}
1525
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001526static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001527{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001528 struct perf_header *ph = ff->ph;
1529 int cpu_nr = ph->env.nr_cpus_avail;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001530 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001531 char *str;
1532
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001533 nr = ph->env.nr_sibling_cores;
1534 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001535
1536 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001537 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001538 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001539 }
1540
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001541 nr = ph->env.nr_sibling_threads;
1542 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001543
1544 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001545 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001546 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001547 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001548
1549 if (ph->env.cpu != NULL) {
1550 for (i = 0; i < cpu_nr; i++)
1551 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1552 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1553 } else
1554 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001555}
1556
Alexey Budankovcf790512018-10-09 17:36:24 +03001557static void print_clockid(struct feat_fd *ff, FILE *fp)
1558{
1559 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1560 ff->ph->env.clockid_res_ns * 1000);
1561}
1562
Robert Richter4e1b9c62012-08-16 21:10:22 +02001563static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001564{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001565 struct perf_evsel *evsel;
1566
1567 if (!events)
1568 return;
1569
1570 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001571 zfree(&evsel->name);
1572 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001573 }
1574
1575 free(events);
1576}
1577
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001578static struct perf_evsel *read_event_desc(struct feat_fd *ff)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001579{
1580 struct perf_evsel *evsel, *events = NULL;
1581 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001582 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001583 u32 nre, sz, nr, i, j;
Stephane Eranian62db9062012-02-09 23:21:07 +01001584 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001585
1586 /* number of events */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001587 if (do_read_u32(ff, &nre))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001588 goto error;
1589
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001590 if (do_read_u32(ff, &sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001591 goto error;
1592
Stephane Eranian62db9062012-02-09 23:21:07 +01001593 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001594 buf = malloc(sz);
1595 if (!buf)
1596 goto error;
1597
Robert Richter4e1b9c62012-08-16 21:10:22 +02001598 /* the last event terminates with evsel->attr.size == 0: */
1599 events = calloc(nre + 1, sizeof(*events));
1600 if (!events)
1601 goto error;
1602
1603 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001604 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001605 msz = sz;
1606
Robert Richter4e1b9c62012-08-16 21:10:22 +02001607 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1608 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001609
Stephane Eranian62db9062012-02-09 23:21:07 +01001610 /*
1611 * must read entire on-file attr struct to
1612 * sync up with layout.
1613 */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001614 if (__do_read(ff, buf, sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001615 goto error;
1616
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001617 if (ff->ph->needs_swap)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001618 perf_event__attr_swap(buf);
1619
Robert Richter4e1b9c62012-08-16 21:10:22 +02001620 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001621
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001622 if (do_read_u32(ff, &nr))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001623 goto error;
1624
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001625 if (ff->ph->needs_swap)
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001626 evsel->needs_swap = true;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001627
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001628 evsel->name = do_read_string(ff);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001629 if (!evsel->name)
1630 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001631
1632 if (!nr)
1633 continue;
1634
1635 id = calloc(nr, sizeof(*id));
1636 if (!id)
1637 goto error;
1638 evsel->ids = nr;
1639 evsel->id = id;
1640
1641 for (j = 0 ; j < nr; j++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001642 if (do_read_u64(ff, id))
Robert Richter4e1b9c62012-08-16 21:10:22 +02001643 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001644 id++;
1645 }
1646 }
1647out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001648 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001649 return events;
1650error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001651 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001652 events = NULL;
1653 goto out;
1654}
1655
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001656static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001657 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001658{
1659 return fprintf(fp, ", %s = %s", name, val);
1660}
1661
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001662static void print_event_desc(struct feat_fd *ff, FILE *fp)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001663{
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001664 struct perf_evsel *evsel, *events;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001665 u32 j;
1666 u64 *id;
1667
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001668 if (ff->events)
1669 events = ff->events;
1670 else
1671 events = read_event_desc(ff);
1672
Robert Richter4e1b9c62012-08-16 21:10:22 +02001673 if (!events) {
1674 fprintf(fp, "# event desc: not available or unable to read\n");
1675 return;
1676 }
1677
1678 for (evsel = events; evsel->attr.size; evsel++) {
1679 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001680
Robert Richter4e1b9c62012-08-16 21:10:22 +02001681 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001682 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001683 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1684 if (j)
1685 fputc(',', fp);
1686 fprintf(fp, " %"PRIu64, *id);
1687 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001688 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001689 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001690
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001691 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001692
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001693 fputc('\n', fp);
1694 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001695
1696 free_event_desc(events);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001697 ff->events = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001698}
1699
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001700static void print_total_mem(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001701{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001702 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001703}
1704
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001705static void print_numa_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001706{
Jiri Olsac60da222016-07-04 14:16:20 +02001707 int i;
1708 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001709
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001710 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1711 n = &ff->ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001712
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001713 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1714 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001715 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001716
Jiri Olsac60da222016-07-04 14:16:20 +02001717 fprintf(fp, "# node%u cpu list : ", n->node);
1718 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001719 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001720}
1721
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001722static void print_cpuid(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001723{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001724 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001725}
1726
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001727static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001728{
1729 fprintf(fp, "# contains samples with branch stack\n");
1730}
1731
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001732static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
Adrian Hunter4025ea42015-04-09 18:53:41 +03001733{
1734 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1735}
1736
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001737static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001738{
1739 fprintf(fp, "# contains stat data\n");
1740}
1741
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001742static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001743{
1744 int i;
1745
1746 fprintf(fp, "# CPU cache info:\n");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001747 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
Jiri Olsa720e98b2016-02-16 16:01:43 +01001748 fprintf(fp, "# ");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001749 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001750 }
1751}
1752
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001753static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001754{
1755 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001756 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001757 u32 pmu_num;
1758 u32 type;
1759
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001760 pmu_num = ff->ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001761 if (!pmu_num) {
1762 fprintf(fp, "# pmu mappings: not available\n");
1763 return;
1764 }
1765
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001766 str = ff->ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001767
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001768 while (pmu_num) {
1769 type = strtoul(str, &tmp, 0);
1770 if (*tmp != ':')
1771 goto error;
1772
1773 str = tmp + 1;
1774 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1775
Robert Richter50a96672012-08-16 21:10:24 +02001776 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001777 str += strlen(str) + 1;
1778 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001779 }
1780
1781 fprintf(fp, "\n");
1782
1783 if (!pmu_num)
1784 return;
1785error:
1786 fprintf(fp, "# pmu mappings: unable to read\n");
1787}
1788
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001789static void print_group_desc(struct feat_fd *ff, FILE *fp)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001790{
1791 struct perf_session *session;
1792 struct perf_evsel *evsel;
1793 u32 nr = 0;
1794
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001795 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001796
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001797 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001798 if (perf_evsel__is_group_leader(evsel) &&
1799 evsel->nr_members > 1) {
1800 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1801 perf_evsel__name(evsel));
1802
1803 nr = evsel->nr_members - 1;
1804 } else if (nr) {
1805 fprintf(fp, ",%s", perf_evsel__name(evsel));
1806
1807 if (--nr == 0)
1808 fprintf(fp, "}\n");
1809 }
1810 }
1811}
1812
Jin Yao60115182017-12-08 21:13:41 +08001813static void print_sample_time(struct feat_fd *ff, FILE *fp)
1814{
1815 struct perf_session *session;
1816 char time_buf[32];
1817 double d;
1818
1819 session = container_of(ff->ph, struct perf_session, header);
1820
1821 timestamp__scnprintf_usec(session->evlist->first_sample_time,
1822 time_buf, sizeof(time_buf));
1823 fprintf(fp, "# time of first sample : %s\n", time_buf);
1824
1825 timestamp__scnprintf_usec(session->evlist->last_sample_time,
1826 time_buf, sizeof(time_buf));
1827 fprintf(fp, "# time of last sample : %s\n", time_buf);
1828
1829 d = (double)(session->evlist->last_sample_time -
1830 session->evlist->first_sample_time) / NSEC_PER_MSEC;
1831
1832 fprintf(fp, "# sample duration : %10.3f ms\n", d);
1833}
1834
Jiri Olsae2091ce2018-03-07 16:50:08 +01001835static void memory_node__fprintf(struct memory_node *n,
1836 unsigned long long bsize, FILE *fp)
1837{
1838 char buf_map[100], buf_size[50];
1839 unsigned long long size;
1840
1841 size = bsize * bitmap_weight(n->set, n->size);
1842 unit_number__scnprintf(buf_size, 50, size);
1843
1844 bitmap_scnprintf(n->set, n->size, buf_map, 100);
1845 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
1846}
1847
1848static void print_mem_topology(struct feat_fd *ff, FILE *fp)
1849{
1850 struct memory_node *nodes;
1851 int i, nr;
1852
1853 nodes = ff->ph->env.memory_nodes;
1854 nr = ff->ph->env.nr_memory_nodes;
1855
1856 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
1857 nr, ff->ph->env.memory_bsize);
1858
1859 for (i = 0; i < nr; i++) {
1860 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
1861 }
1862}
1863
Robert Richter08d95bd2012-02-10 15:41:55 +01001864static int __event_process_build_id(struct build_id_event *bev,
1865 char *filename,
1866 struct perf_session *session)
1867{
1868 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001869 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001870 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001871 struct dso *dso;
1872 enum dso_kernel_type dso_type;
1873
1874 machine = perf_session__findnew_machine(session, bev->pid);
1875 if (!machine)
1876 goto out;
1877
Wang Nan1f121b02015-06-03 08:52:21 +00001878 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001879
Wang Nan1f121b02015-06-03 08:52:21 +00001880 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001881 case PERF_RECORD_MISC_KERNEL:
1882 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001883 break;
1884 case PERF_RECORD_MISC_GUEST_KERNEL:
1885 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001886 break;
1887 case PERF_RECORD_MISC_USER:
1888 case PERF_RECORD_MISC_GUEST_USER:
1889 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001890 break;
1891 default:
1892 goto out;
1893 }
1894
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001895 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001896 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001897 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001898
1899 dso__set_build_id(dso, &bev->build_id);
1900
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001901 if (dso_type != DSO_TYPE_USER) {
1902 struct kmod_path m = { .name = NULL, };
1903
1904 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09001905 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001906 else
1907 dso->kernel = dso_type;
1908
1909 free(m.name);
1910 }
Robert Richter08d95bd2012-02-10 15:41:55 +01001911
1912 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1913 sbuild_id);
1914 pr_debug("build id event received for %s: %s\n",
1915 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001916 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001917 }
1918
1919 err = 0;
1920out:
1921 return err;
1922}
1923
1924static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1925 int input, u64 offset, u64 size)
1926{
1927 struct perf_session *session = container_of(header, struct perf_session, header);
1928 struct {
1929 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001930 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001931 char filename[0];
1932 } old_bev;
1933 struct build_id_event bev;
1934 char filename[PATH_MAX];
1935 u64 limit = offset + size;
1936
1937 while (offset < limit) {
1938 ssize_t len;
1939
Namhyung Kim5323f602012-12-17 15:38:54 +09001940 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001941 return -1;
1942
1943 if (header->needs_swap)
1944 perf_event_header__bswap(&old_bev.header);
1945
1946 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001947 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001948 return -1;
1949
1950 bev.header = old_bev.header;
1951
1952 /*
1953 * As the pid is the missing value, we need to fill
1954 * it properly. The header.misc value give us nice hint.
1955 */
1956 bev.pid = HOST_KERNEL_ID;
1957 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1958 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1959 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1960
1961 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1962 __event_process_build_id(&bev, filename, session);
1963
1964 offset += bev.header.size;
1965 }
1966
1967 return 0;
1968}
1969
1970static int perf_header__read_build_ids(struct perf_header *header,
1971 int input, u64 offset, u64 size)
1972{
1973 struct perf_session *session = container_of(header, struct perf_session, header);
1974 struct build_id_event bev;
1975 char filename[PATH_MAX];
1976 u64 limit = offset + size, orig_offset = offset;
1977 int err = -1;
1978
1979 while (offset < limit) {
1980 ssize_t len;
1981
Namhyung Kim5323f602012-12-17 15:38:54 +09001982 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001983 goto out;
1984
1985 if (header->needs_swap)
1986 perf_event_header__bswap(&bev.header);
1987
1988 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001989 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001990 goto out;
1991 /*
1992 * The a1645ce1 changeset:
1993 *
1994 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1995 *
1996 * Added a field to struct build_id_event that broke the file
1997 * format.
1998 *
1999 * Since the kernel build-id is the first entry, process the
2000 * table using the old format if the well known
2001 * '[kernel.kallsyms]' string for the kernel build-id has the
2002 * first 4 characters chopped off (where the pid_t sits).
2003 */
2004 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
2005 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
2006 return -1;
2007 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
2008 }
2009
2010 __event_process_build_id(&bev, filename, session);
2011
2012 offset += bev.header.size;
2013 }
2014 err = 0;
2015out:
2016 return err;
2017}
2018
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07002019/* Macro for features that simply need to read and store a string. */
2020#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002021static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07002022{\
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002023 ff->ph->env.__feat_env = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002024 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07002025}
2026
2027FEAT_PROCESS_STR_FUN(hostname, hostname);
2028FEAT_PROCESS_STR_FUN(osrelease, os_release);
2029FEAT_PROCESS_STR_FUN(version, version);
2030FEAT_PROCESS_STR_FUN(arch, arch);
2031FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
2032FEAT_PROCESS_STR_FUN(cpuid, cpuid);
2033
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002034static int process_tracing_data(struct feat_fd *ff, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01002035{
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002036 ssize_t ret = trace_report(ff->fd, data, false);
2037
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002038 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01002039}
2040
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002041static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01002042{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002043 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
Robert Richterf1c67db2012-02-10 15:41:56 +01002044 pr_debug("Failed to read buildids, continuing...\n");
2045 return 0;
2046}
2047
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002048static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002049{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002050 int ret;
2051 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002052
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002053 ret = do_read_u32(ff, &nr_cpus_avail);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002054 if (ret)
2055 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002056
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002057 ret = do_read_u32(ff, &nr_cpus_online);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002058 if (ret)
2059 return ret;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002060 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2061 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002062 return 0;
2063}
2064
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002065static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002066{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002067 u64 total_mem;
2068 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002069
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002070 ret = do_read_u64(ff, &total_mem);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002071 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002072 return -1;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002073 ff->ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002074 return 0;
2075}
2076
Robert Richter7c2f7af2012-08-16 21:10:23 +02002077static struct perf_evsel *
2078perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
2079{
2080 struct perf_evsel *evsel;
2081
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002082 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02002083 if (evsel->idx == idx)
2084 return evsel;
2085 }
2086
2087 return NULL;
2088}
2089
2090static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002091perf_evlist__set_event_name(struct perf_evlist *evlist,
2092 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002093{
2094 struct perf_evsel *evsel;
2095
2096 if (!event->name)
2097 return;
2098
2099 evsel = perf_evlist__find_by_index(evlist, event->idx);
2100 if (!evsel)
2101 return;
2102
2103 if (evsel->name)
2104 return;
2105
2106 evsel->name = strdup(event->name);
2107}
2108
2109static int
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002110process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002111{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002112 struct perf_session *session;
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002113 struct perf_evsel *evsel, *events = read_event_desc(ff);
Robert Richter7c2f7af2012-08-16 21:10:23 +02002114
2115 if (!events)
2116 return 0;
2117
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002118 session = container_of(ff->ph, struct perf_session, header);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002119
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002120 if (session->data->is_pipe) {
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002121 /* Save events for reading later by print_event_desc,
2122 * since they can't be read again in pipe mode. */
2123 ff->events = events;
2124 }
2125
Robert Richter7c2f7af2012-08-16 21:10:23 +02002126 for (evsel = events; evsel->attr.size; evsel++)
2127 perf_evlist__set_event_name(session->evlist, evsel);
2128
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002129 if (!session->data->is_pipe)
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002130 free_event_desc(events);
Robert Richter7c2f7af2012-08-16 21:10:23 +02002131
2132 return 0;
2133}
2134
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002135static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002136{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002137 char *str, *cmdline = NULL, **argv = NULL;
2138 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002139
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002140 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002141 return -1;
2142
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002143 ff->ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002144
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002145 cmdline = zalloc(ff->size + nr + 1);
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002146 if (!cmdline)
2147 return -1;
2148
2149 argv = zalloc(sizeof(char *) * (nr + 1));
2150 if (!argv)
2151 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002152
2153 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002154 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002155 if (!str)
2156 goto error;
2157
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002158 argv[i] = cmdline + len;
2159 memcpy(argv[i], str, strlen(str) + 1);
2160 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002161 free(str);
2162 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002163 ff->ph->env.cmdline = cmdline;
2164 ff->ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002165 return 0;
2166
2167error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002168 free(argv);
2169 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002170 return -1;
2171}
2172
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002173static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002174{
Namhyung Kima1ae5652012-09-24 17:14:59 +09002175 u32 nr, i;
2176 char *str;
2177 struct strbuf sb;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002178 int cpu_nr = ff->ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04002179 u64 size = 0;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002180 struct perf_header *ph = ff->ph;
Thomas Richter01766222018-06-11 09:31:52 +02002181 bool do_core_id_test = true;
Kan Liang2bb00d22015-09-01 09:58:12 -04002182
2183 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2184 if (!ph->env.cpu)
2185 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002186
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002187 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002188 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002189
Namhyung Kima1ae5652012-09-24 17:14:59 +09002190 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002191 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002192 if (strbuf_init(&sb, 128) < 0)
2193 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002194
2195 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002196 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002197 if (!str)
2198 goto error;
2199
2200 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002201 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2202 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04002203 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002204 free(str);
2205 }
2206 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2207
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002208 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002209 return -1;
2210
Namhyung Kima1ae5652012-09-24 17:14:59 +09002211 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002212 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002213
2214 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002215 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002216 if (!str)
2217 goto error;
2218
2219 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002220 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2221 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04002222 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002223 free(str);
2224 }
2225 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04002226
2227 /*
2228 * The header may be from old perf,
2229 * which doesn't include core id and socket id information.
2230 */
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002231 if (ff->size <= size) {
Kan Liang2bb00d22015-09-01 09:58:12 -04002232 zfree(&ph->env.cpu);
2233 return 0;
2234 }
2235
Thomas Richter01766222018-06-11 09:31:52 +02002236 /* On s390 the socket_id number is not related to the numbers of cpus.
2237 * The socket_id number might be higher than the numbers of cpus.
2238 * This depends on the configuration.
2239 */
2240 if (ph->env.arch && !strncmp(ph->env.arch, "s390", 4))
2241 do_core_id_test = false;
2242
Kan Liang2bb00d22015-09-01 09:58:12 -04002243 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002244 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002245 goto free_cpu;
2246
Kan Liang2bb00d22015-09-01 09:58:12 -04002247 ph->env.cpu[i].core_id = nr;
2248
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002249 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002250 goto free_cpu;
2251
Thomas Richter01766222018-06-11 09:31:52 +02002252 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04002253 pr_debug("socket_id number is too big."
2254 "You may need to upgrade the perf tool.\n");
2255 goto free_cpu;
2256 }
2257
2258 ph->env.cpu[i].socket_id = nr;
2259 }
2260
Namhyung Kima1ae5652012-09-24 17:14:59 +09002261 return 0;
2262
2263error:
2264 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04002265free_cpu:
2266 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002267 return -1;
2268}
2269
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002270static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002271{
Jiri Olsac60da222016-07-04 14:16:20 +02002272 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02002273 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002274 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002275
2276 /* nr nodes */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002277 if (do_read_u32(ff, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002278 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002279
Jiri Olsac60da222016-07-04 14:16:20 +02002280 nodes = zalloc(sizeof(*nodes) * nr);
2281 if (!nodes)
2282 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002283
2284 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02002285 n = &nodes[i];
2286
Namhyung Kima1ae5652012-09-24 17:14:59 +09002287 /* node number */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002288 if (do_read_u32(ff, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002289 goto error;
2290
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002291 if (do_read_u64(ff, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002292 goto error;
2293
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002294 if (do_read_u64(ff, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002295 goto error;
2296
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002297 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002298 if (!str)
2299 goto error;
2300
Jiri Olsac60da222016-07-04 14:16:20 +02002301 n->map = cpu_map__new(str);
2302 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002303 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02002304
Namhyung Kima1ae5652012-09-24 17:14:59 +09002305 free(str);
2306 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002307 ff->ph->env.nr_numa_nodes = nr;
2308 ff->ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002309 return 0;
2310
2311error:
Jiri Olsac60da222016-07-04 14:16:20 +02002312 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002313 return -1;
2314}
2315
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002316static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002317{
Namhyung Kima1ae5652012-09-24 17:14:59 +09002318 char *name;
2319 u32 pmu_num;
2320 u32 type;
2321 struct strbuf sb;
2322
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002323 if (do_read_u32(ff, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002324 return -1;
2325
Namhyung Kima1ae5652012-09-24 17:14:59 +09002326 if (!pmu_num) {
2327 pr_debug("pmu mappings not available\n");
2328 return 0;
2329 }
2330
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002331 ff->ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002332 if (strbuf_init(&sb, 128) < 0)
2333 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002334
2335 while (pmu_num) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002336 if (do_read_u32(ff, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002337 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002338
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002339 name = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002340 if (!name)
2341 goto error;
2342
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002343 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2344 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002345 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002346 if (strbuf_add(&sb, "", 1) < 0)
2347 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002348
Kan Liange0838e02015-09-10 11:03:05 -03002349 if (!strcmp(name, "msr"))
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002350 ff->ph->env.msr_pmu_type = type;
Kan Liange0838e02015-09-10 11:03:05 -03002351
Namhyung Kima1ae5652012-09-24 17:14:59 +09002352 free(name);
2353 pmu_num--;
2354 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002355 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002356 return 0;
2357
2358error:
2359 strbuf_release(&sb);
2360 return -1;
2361}
2362
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002363static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima8bb5592013-01-22 18:09:31 +09002364{
2365 size_t ret = -1;
2366 u32 i, nr, nr_groups;
2367 struct perf_session *session;
2368 struct perf_evsel *evsel, *leader = NULL;
2369 struct group_desc {
2370 char *name;
2371 u32 leader_idx;
2372 u32 nr_members;
2373 } *desc;
2374
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002375 if (do_read_u32(ff, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002376 return -1;
2377
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002378 ff->ph->env.nr_groups = nr_groups;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002379 if (!nr_groups) {
2380 pr_debug("group desc not available\n");
2381 return 0;
2382 }
2383
2384 desc = calloc(nr_groups, sizeof(*desc));
2385 if (!desc)
2386 return -1;
2387
2388 for (i = 0; i < nr_groups; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002389 desc[i].name = do_read_string(ff);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002390 if (!desc[i].name)
2391 goto out_free;
2392
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002393 if (do_read_u32(ff, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002394 goto out_free;
2395
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002396 if (do_read_u32(ff, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002397 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002398 }
2399
2400 /*
2401 * Rebuild group relationship based on the group_desc
2402 */
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002403 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002404 session->evlist->nr_groups = nr_groups;
2405
2406 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002407 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002408 if (evsel->idx == (int) desc[i].leader_idx) {
2409 evsel->leader = evsel;
2410 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002411 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002412 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002413 desc[i].name = NULL;
2414 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09002415 evsel->nr_members = desc[i].nr_members;
2416
2417 if (i >= nr_groups || nr > 0) {
2418 pr_debug("invalid group desc\n");
2419 goto out_free;
2420 }
2421
2422 leader = evsel;
2423 nr = evsel->nr_members - 1;
2424 i++;
2425 } else if (nr) {
2426 /* This is a group member */
2427 evsel->leader = leader;
2428
2429 nr--;
2430 }
2431 }
2432
2433 if (i != nr_groups || nr != 0) {
2434 pr_debug("invalid group desc\n");
2435 goto out_free;
2436 }
2437
2438 ret = 0;
2439out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002440 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002441 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002442 free(desc);
2443
2444 return ret;
2445}
2446
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002447static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
Adrian Hunter99fa2982015-04-30 17:37:25 +03002448{
2449 struct perf_session *session;
2450 int err;
2451
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002452 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002453
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002454 err = auxtrace_index__process(ff->fd, ff->size, session,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002455 ff->ph->needs_swap);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002456 if (err < 0)
2457 pr_err("Failed to process auxtrace index\n");
2458 return err;
2459}
2460
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002461static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01002462{
2463 struct cpu_cache_level *caches;
2464 u32 cnt, i, version;
2465
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002466 if (do_read_u32(ff, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002467 return -1;
2468
Jiri Olsa720e98b2016-02-16 16:01:43 +01002469 if (version != 1)
2470 return -1;
2471
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002472 if (do_read_u32(ff, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002473 return -1;
2474
Jiri Olsa720e98b2016-02-16 16:01:43 +01002475 caches = zalloc(sizeof(*caches) * cnt);
2476 if (!caches)
2477 return -1;
2478
2479 for (i = 0; i < cnt; i++) {
2480 struct cpu_cache_level c;
2481
2482 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002483 if (do_read_u32(ff, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002484 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002485
2486 _R(level)
2487 _R(line_size)
2488 _R(sets)
2489 _R(ways)
2490 #undef _R
2491
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002492 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002493 c.v = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002494 if (!c.v) \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002495 goto out_free_caches;
2496
2497 _R(type)
2498 _R(size)
2499 _R(map)
2500 #undef _R
2501
2502 caches[i] = c;
2503 }
2504
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002505 ff->ph->env.caches = caches;
2506 ff->ph->env.caches_cnt = cnt;
Jiri Olsa720e98b2016-02-16 16:01:43 +01002507 return 0;
2508out_free_caches:
2509 free(caches);
2510 return -1;
2511}
2512
Jin Yao60115182017-12-08 21:13:41 +08002513static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2514{
2515 struct perf_session *session;
2516 u64 first_sample_time, last_sample_time;
2517 int ret;
2518
2519 session = container_of(ff->ph, struct perf_session, header);
2520
2521 ret = do_read_u64(ff, &first_sample_time);
2522 if (ret)
2523 return -1;
2524
2525 ret = do_read_u64(ff, &last_sample_time);
2526 if (ret)
2527 return -1;
2528
2529 session->evlist->first_sample_time = first_sample_time;
2530 session->evlist->last_sample_time = last_sample_time;
2531 return 0;
2532}
2533
Jiri Olsae2091ce2018-03-07 16:50:08 +01002534static int process_mem_topology(struct feat_fd *ff,
2535 void *data __maybe_unused)
2536{
2537 struct memory_node *nodes;
2538 u64 version, i, nr, bsize;
2539 int ret = -1;
2540
2541 if (do_read_u64(ff, &version))
2542 return -1;
2543
2544 if (version != 1)
2545 return -1;
2546
2547 if (do_read_u64(ff, &bsize))
2548 return -1;
2549
2550 if (do_read_u64(ff, &nr))
2551 return -1;
2552
2553 nodes = zalloc(sizeof(*nodes) * nr);
2554 if (!nodes)
2555 return -1;
2556
2557 for (i = 0; i < nr; i++) {
2558 struct memory_node n;
2559
2560 #define _R(v) \
2561 if (do_read_u64(ff, &n.v)) \
2562 goto out; \
2563
2564 _R(node)
2565 _R(size)
2566
2567 #undef _R
2568
2569 if (do_read_bitmap(ff, &n.set, &n.size))
2570 goto out;
2571
2572 nodes[i] = n;
2573 }
2574
2575 ff->ph->env.memory_bsize = bsize;
2576 ff->ph->env.memory_nodes = nodes;
2577 ff->ph->env.nr_memory_nodes = nr;
2578 ret = 0;
2579
2580out:
2581 if (ret)
2582 free(nodes);
2583 return ret;
2584}
2585
Alexey Budankovcf790512018-10-09 17:36:24 +03002586static int process_clockid(struct feat_fd *ff,
2587 void *data __maybe_unused)
2588{
2589 if (do_read_u64(ff, &ff->ph->env.clockid_res_ns))
2590 return -1;
2591
2592 return 0;
2593}
2594
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002595struct feature_ops {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002596 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002597 void (*print)(struct feat_fd *ff, FILE *fp);
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002598 int (*process)(struct feat_fd *ff, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002599 const char *name;
2600 bool full_only;
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002601 bool synthesize;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002602};
2603
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002604#define FEAT_OPR(n, func, __full_only) \
2605 [HEADER_##n] = { \
2606 .name = __stringify(n), \
2607 .write = write_##func, \
2608 .print = print_##func, \
2609 .full_only = __full_only, \
2610 .process = process_##func, \
2611 .synthesize = true \
2612 }
2613
2614#define FEAT_OPN(n, func, __full_only) \
2615 [HEADER_##n] = { \
2616 .name = __stringify(n), \
2617 .write = write_##func, \
2618 .print = print_##func, \
2619 .full_only = __full_only, \
2620 .process = process_##func \
2621 }
Robert Richter8cdfa782011-12-07 10:02:56 +01002622
2623/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002624#define print_tracing_data NULL
2625#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002626
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002627#define process_branch_stack NULL
2628#define process_stat NULL
2629
2630
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002631static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002632 FEAT_OPN(TRACING_DATA, tracing_data, false),
2633 FEAT_OPN(BUILD_ID, build_id, false),
2634 FEAT_OPR(HOSTNAME, hostname, false),
2635 FEAT_OPR(OSRELEASE, osrelease, false),
2636 FEAT_OPR(VERSION, version, false),
2637 FEAT_OPR(ARCH, arch, false),
2638 FEAT_OPR(NRCPUS, nrcpus, false),
2639 FEAT_OPR(CPUDESC, cpudesc, false),
2640 FEAT_OPR(CPUID, cpuid, false),
2641 FEAT_OPR(TOTAL_MEM, total_mem, false),
2642 FEAT_OPR(EVENT_DESC, event_desc, false),
2643 FEAT_OPR(CMDLINE, cmdline, false),
2644 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2645 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2646 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2647 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
Jiri Olsae8fedff2018-07-12 15:52:02 +02002648 FEAT_OPR(GROUP_DESC, group_desc, false),
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002649 FEAT_OPN(AUXTRACE, auxtrace, false),
2650 FEAT_OPN(STAT, stat, false),
2651 FEAT_OPN(CACHE, cache, true),
Jin Yao60115182017-12-08 21:13:41 +08002652 FEAT_OPR(SAMPLE_TIME, sample_time, false),
Jiri Olsae2091ce2018-03-07 16:50:08 +01002653 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
Alexey Budankovcf790512018-10-09 17:36:24 +03002654 FEAT_OPR(CLOCKID, clockid, false)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002655};
2656
2657struct header_print_data {
2658 FILE *fp;
2659 bool full; /* extended list of headers */
2660};
2661
2662static int perf_file_section__fprintf_info(struct perf_file_section *section,
2663 struct perf_header *ph,
2664 int feat, int fd, void *data)
2665{
2666 struct header_print_data *hd = data;
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002667 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002668
2669 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2670 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2671 "%d, continuing...\n", section->offset, feat);
2672 return 0;
2673 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002674 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002675 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002676 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002677 }
2678 if (!feat_ops[feat].print)
2679 return 0;
2680
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002681 ff = (struct feat_fd) {
2682 .fd = fd,
2683 .ph = ph,
2684 };
2685
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002686 if (!feat_ops[feat].full_only || hd->full)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002687 feat_ops[feat].print(&ff, hd->fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002688 else
2689 fprintf(hd->fp, "# %s info available, use -I to display\n",
2690 feat_ops[feat].name);
2691
2692 return 0;
2693}
2694
2695int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2696{
2697 struct header_print_data hd;
2698 struct perf_header *header = &session->header;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002699 int fd = perf_data__fd(session->data);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002700 struct stat st;
Arnaldo Carvalho de Melo0afcf292018-12-11 16:11:54 -03002701 time_t stctime;
Jiri Olsaaabae162016-10-10 09:35:50 +02002702 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002703
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002704 hd.fp = fp;
2705 hd.full = full;
2706
Jiri Olsaf45f5612016-10-10 09:03:07 +02002707 ret = fstat(fd, &st);
2708 if (ret == -1)
2709 return -1;
2710
Arnaldo Carvalho de Melo0afcf292018-12-11 16:11:54 -03002711 stctime = st.st_ctime;
2712 fprintf(fp, "# captured on : %s", ctime(&stctime));
Jiri Olsae971a5a2018-03-07 16:50:03 +01002713
2714 fprintf(fp, "# header version : %u\n", header->version);
2715 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
2716 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
2717 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002718
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002719 perf_header__process_sections(header, fd, &hd,
2720 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002721
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002722 if (session->data->is_pipe)
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002723 return 0;
2724
Jiri Olsaaabae162016-10-10 09:35:50 +02002725 fprintf(fp, "# missing features: ");
2726 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2727 if (bit)
2728 fprintf(fp, "%s ", feat_ops[bit].name);
2729 }
2730
2731 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002732 return 0;
2733}
2734
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002735static int do_write_feat(struct feat_fd *ff, int type,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002736 struct perf_file_section **p,
2737 struct perf_evlist *evlist)
2738{
2739 int err;
2740 int ret = 0;
2741
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002742 if (perf_header__has_feat(ff->ph, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002743 if (!feat_ops[type].write)
2744 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002745
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -07002746 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2747 return -1;
2748
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002749 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002750
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002751 err = feat_ops[type].write(ff, evlist);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002752 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002753 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002754
2755 /* undo anything written */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002756 lseek(ff->fd, (*p)->offset, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002757
2758 return -1;
2759 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002760 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002761 (*p)++;
2762 }
2763 return ret;
2764}
2765
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002766static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002767 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002768{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002769 int nr_sections;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002770 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002771 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002772 int sec_size;
2773 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002774 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002775 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002776
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002777 ff = (struct feat_fd){
2778 .fd = fd,
2779 .ph = header,
2780 };
2781
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002782 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002783 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002784 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002785
Paul Gortmaker91b98802013-01-30 20:05:49 -05002786 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002787 if (feat_sec == NULL)
2788 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002789
2790 sec_size = sizeof(*feat_sec) * nr_sections;
2791
Jiri Olsa8d541e92013-07-17 19:49:44 +02002792 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002793 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002794
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002795 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002796 if (do_write_feat(&ff, feat, &p, evlist))
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002797 perf_header__clear_feat(header, feat);
2798 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002799
Xiao Guangrongf887f302010-02-04 16:46:42 +08002800 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002801 /*
2802 * may write more than needed due to dropped feature, but
Ingo Molnaradba1632018-12-03 11:22:00 +01002803 * this is okay, reader will skip the missing entries
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002804 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002805 err = do_write(&ff, feat_sec, sec_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002806 if (err < 0)
2807 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002808 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002809 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002810}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002811
Tom Zanussi8dc58102010-04-01 23:59:15 -05002812int perf_header__write_pipe(int fd)
2813{
2814 struct perf_pipe_file_header f_header;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002815 struct feat_fd ff;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002816 int err;
2817
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002818 ff = (struct feat_fd){ .fd = fd };
2819
Tom Zanussi8dc58102010-04-01 23:59:15 -05002820 f_header = (struct perf_pipe_file_header){
2821 .magic = PERF_MAGIC,
2822 .size = sizeof(f_header),
2823 };
2824
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002825 err = do_write(&ff, &f_header, sizeof(f_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05002826 if (err < 0) {
2827 pr_debug("failed to write perf pipe header\n");
2828 return err;
2829 }
2830
2831 return 0;
2832}
2833
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002834int perf_session__write_header(struct perf_session *session,
2835 struct perf_evlist *evlist,
2836 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002837{
2838 struct perf_file_header f_header;
2839 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002840 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002841 struct perf_evsel *evsel;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002842 struct feat_fd ff;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002843 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002844 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002845
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002846 ff = (struct feat_fd){ .fd = fd};
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002847 lseek(fd, sizeof(f_header), SEEK_SET);
2848
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002849 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002850 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002851 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002852 if (err < 0) {
2853 pr_debug("failed to write perf header\n");
2854 return err;
2855 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002856 }
2857
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002858 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002859
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002860 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002861 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002862 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002863 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002864 .offset = evsel->id_offset,
2865 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002866 }
2867 };
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002868 err = do_write(&ff, &f_attr, sizeof(f_attr));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002869 if (err < 0) {
2870 pr_debug("failed to write perf header attribute\n");
2871 return err;
2872 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002873 }
2874
Adrian Hunterd645c442013-12-11 14:36:28 +02002875 if (!header->data_offset)
2876 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002877 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002878
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002879 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002880 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002881 if (err < 0)
2882 return err;
2883 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002884
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002885 f_header = (struct perf_file_header){
2886 .magic = PERF_MAGIC,
2887 .size = sizeof(f_header),
2888 .attr_size = sizeof(f_attr),
2889 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002890 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002891 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002892 },
2893 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002894 .offset = header->data_offset,
2895 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002896 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002897 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002898 };
2899
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002900 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002901
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002902 lseek(fd, 0, SEEK_SET);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002903 err = do_write(&ff, &f_header, sizeof(f_header));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002904 if (err < 0) {
2905 pr_debug("failed to write perf header\n");
2906 return err;
2907 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002908 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002909
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002910 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002911}
2912
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002913static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002914 int fd, void *buf, size_t size)
2915{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002916 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002917 return -1;
2918
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002919 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002920 mem_bswap_64(buf, size);
2921
2922 return 0;
2923}
2924
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002925int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002926 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002927 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002928 struct perf_header *ph,
2929 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002930{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002931 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002932 int nr_sections;
2933 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002934 int feat;
2935 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002936
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002937 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002938 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002939 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002940
Paul Gortmaker91b98802013-01-30 20:05:49 -05002941 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002942 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002943 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002944
2945 sec_size = sizeof(*feat_sec) * nr_sections;
2946
Jiri Olsa8d541e92013-07-17 19:49:44 +02002947 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002948
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002949 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2950 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002951 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002952
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002953 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2954 err = process(sec++, header, feat, fd, data);
2955 if (err < 0)
2956 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002957 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002958 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002959out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002960 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002961 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002962}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002963
Stephane Eranian114382a2012-02-09 23:21:08 +01002964static const int attr_file_abi_sizes[] = {
2965 [0] = PERF_ATTR_SIZE_VER0,
2966 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002967 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002968 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002969 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002970 0,
2971};
2972
2973/*
2974 * In the legacy file format, the magic number is not used to encode endianness.
2975 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2976 * on ABI revisions, we need to try all combinations for all endianness to
2977 * detect the endianness.
2978 */
2979static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2980{
2981 uint64_t ref_size, attr_size;
2982 int i;
2983
2984 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2985 ref_size = attr_file_abi_sizes[i]
2986 + sizeof(struct perf_file_section);
2987 if (hdr_sz != ref_size) {
2988 attr_size = bswap_64(hdr_sz);
2989 if (attr_size != ref_size)
2990 continue;
2991
2992 ph->needs_swap = true;
2993 }
2994 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2995 i,
2996 ph->needs_swap);
2997 return 0;
2998 }
2999 /* could not determine endianness */
3000 return -1;
3001}
3002
3003#define PERF_PIPE_HDR_VER0 16
3004
3005static const size_t attr_pipe_abi_sizes[] = {
3006 [0] = PERF_PIPE_HDR_VER0,
3007 0,
3008};
3009
3010/*
3011 * In the legacy pipe format, there is an implicit assumption that endiannesss
3012 * between host recording the samples, and host parsing the samples is the
3013 * same. This is not always the case given that the pipe output may always be
3014 * redirected into a file and analyzed on a different machine with possibly a
3015 * different endianness and perf_event ABI revsions in the perf tool itself.
3016 */
3017static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
3018{
3019 u64 attr_size;
3020 int i;
3021
3022 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
3023 if (hdr_sz != attr_pipe_abi_sizes[i]) {
3024 attr_size = bswap_64(hdr_sz);
3025 if (attr_size != hdr_sz)
3026 continue;
3027
3028 ph->needs_swap = true;
3029 }
3030 pr_debug("Pipe ABI%d perf.data file detected\n", i);
3031 return 0;
3032 }
3033 return -1;
3034}
3035
Feng Tange84ba4e2012-10-30 11:56:07 +08003036bool is_perf_magic(u64 magic)
3037{
3038 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
3039 || magic == __perf_magic2
3040 || magic == __perf_magic2_sw)
3041 return true;
3042
3043 return false;
3044}
3045
Stephane Eranian114382a2012-02-09 23:21:08 +01003046static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3047 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01003048{
3049 int ret;
3050
3051 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01003052 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01003053 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02003054 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01003055 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01003056 if (is_pipe)
3057 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01003058
Stephane Eranian114382a2012-02-09 23:21:08 +01003059 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01003060 }
Stephane Eranian114382a2012-02-09 23:21:08 +01003061 /*
3062 * the new magic number serves two purposes:
3063 * - unique number to identify actual perf.data files
3064 * - encode endianness of file
3065 */
Namhyung Kimf7913972015-01-29 17:06:45 +09003066 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01003067
Stephane Eranian114382a2012-02-09 23:21:08 +01003068 /* check magic number with one endianness */
3069 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01003070 return 0;
3071
Stephane Eranian114382a2012-02-09 23:21:08 +01003072 /* check magic number with opposite endianness */
3073 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01003074 return -1;
3075
3076 ph->needs_swap = true;
3077
3078 return 0;
3079}
3080
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003081int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003082 struct perf_header *ph, int fd)
3083{
Jiri Olsa727ebd52013-11-28 11:30:14 +01003084 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01003085
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003086 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003087
Stephane Eranian73323f52012-02-02 13:54:44 +01003088 ret = readn(fd, header, sizeof(*header));
3089 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003090 return -1;
3091
Stephane Eranian114382a2012-02-09 23:21:08 +01003092 if (check_magic_endian(header->magic,
3093 header->attr_size, false, ph) < 0) {
3094 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01003095 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01003096 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003097
Stephane Eranian73323f52012-02-02 13:54:44 +01003098 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003099 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01003100 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003101 }
3102
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003103 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003104 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003105 if (header->size == offsetof(typeof(*header), adds_features))
3106 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003107 else
3108 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06003109 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06003110 /*
3111 * feature bitmap is declared as an array of unsigned longs --
3112 * not good since its size can differ between the host that
3113 * generated the data file and the host analyzing the file.
3114 *
3115 * We need to handle endianness, but we don't know the size of
3116 * the unsigned long where the file was generated. Take a best
3117 * guess at determining it: try 64-bit swap first (ie., file
3118 * created on a 64-bit host), and check if the hostname feature
3119 * bit is set (this feature bit is forced on as of fbe96f2).
3120 * If the bit is not, undo the 64-bit swap and try a 32-bit
3121 * swap. If the hostname bit is still not set (e.g., older data
3122 * file), punt and fallback to the original behavior --
3123 * clearing all feature bits and setting buildid.
3124 */
David Ahern80c01202012-06-08 11:47:51 -03003125 mem_bswap_64(&header->adds_features,
3126 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06003127
3128 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03003129 /* unswap as u64 */
3130 mem_bswap_64(&header->adds_features,
3131 BITS_TO_U64(HEADER_FEAT_BITS));
3132
3133 /* unswap as u32 */
3134 mem_bswap_32(&header->adds_features,
3135 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06003136 }
3137
3138 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3139 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3140 set_bit(HEADER_BUILD_ID, header->adds_features);
3141 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003142 }
3143
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003144 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003145 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003146
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003147 ph->data_offset = header->data.offset;
3148 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02003149 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003150 return 0;
3151}
3152
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003153static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003154 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003155 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003156{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003157 struct feat_fd fdd = {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07003158 .fd = fd,
3159 .ph = ph,
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003160 .size = section->size,
3161 .offset = section->offset,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07003162 };
3163
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003164 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02003165 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003166 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003167 return 0;
3168 }
3169
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003170 if (feat >= HEADER_LAST_FEATURE) {
3171 pr_debug("unknown feature %d, continuing...\n", feat);
3172 return 0;
3173 }
3174
Robert Richterf1c67db2012-02-10 15:41:56 +01003175 if (!feat_ops[feat].process)
3176 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003177
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003178 return feat_ops[feat].process(&fdd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003179}
3180
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003181static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05003182 struct perf_header *ph, int fd,
3183 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003184{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003185 struct feat_fd ff = {
3186 .fd = STDOUT_FILENO,
3187 .ph = ph,
3188 };
Jiri Olsa727ebd52013-11-28 11:30:14 +01003189 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01003190
3191 ret = readn(fd, header, sizeof(*header));
3192 if (ret <= 0)
3193 return -1;
3194
Stephane Eranian114382a2012-02-09 23:21:08 +01003195 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
3196 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05003197 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01003198 }
3199
3200 if (ph->needs_swap)
3201 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05003202
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003203 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05003204 return -1;
3205
Tom Zanussi8dc58102010-04-01 23:59:15 -05003206 return 0;
3207}
3208
Jiri Olsad4339562013-07-17 19:49:41 +02003209static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05003210{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003211 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05003212 struct perf_pipe_file_header f_header;
3213
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003214 if (perf_file_header__read_pipe(&f_header, header,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003215 perf_data__fd(session->data),
Tom Zanussi454c4072010-05-01 01:41:20 -05003216 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05003217 pr_debug("incompatible file format\n");
3218 return -EINVAL;
3219 }
3220
Tom Zanussi8dc58102010-04-01 23:59:15 -05003221 return 0;
3222}
3223
Stephane Eranian69996df2012-02-09 23:21:06 +01003224static int read_attr(int fd, struct perf_header *ph,
3225 struct perf_file_attr *f_attr)
3226{
3227 struct perf_event_attr *attr = &f_attr->attr;
3228 size_t sz, left;
3229 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01003230 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01003231
3232 memset(f_attr, 0, sizeof(*f_attr));
3233
3234 /* read minimal guaranteed structure */
3235 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3236 if (ret <= 0) {
3237 pr_debug("cannot read %d bytes of header attr\n",
3238 PERF_ATTR_SIZE_VER0);
3239 return -1;
3240 }
3241
3242 /* on file perf_event_attr size */
3243 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01003244
Stephane Eranian69996df2012-02-09 23:21:06 +01003245 if (ph->needs_swap)
3246 sz = bswap_32(sz);
3247
3248 if (sz == 0) {
3249 /* assume ABI0 */
3250 sz = PERF_ATTR_SIZE_VER0;
3251 } else if (sz > our_sz) {
3252 pr_debug("file uses a more recent and unsupported ABI"
3253 " (%zu bytes extra)\n", sz - our_sz);
3254 return -1;
3255 }
3256 /* what we have not yet read and that we know about */
3257 left = sz - PERF_ATTR_SIZE_VER0;
3258 if (left) {
3259 void *ptr = attr;
3260 ptr += PERF_ATTR_SIZE_VER0;
3261
3262 ret = readn(fd, ptr, left);
3263 }
3264 /* read perf_file_section, ids are read in caller */
3265 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3266
3267 return ret <= 0 ? -1 : 0;
3268}
3269
Namhyung Kim831394b2012-09-06 11:10:46 +09003270static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003271 struct tep_handle *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003272{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003273 struct tep_event *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003274 char bf[128];
3275
Namhyung Kim831394b2012-09-06 11:10:46 +09003276 /* already prepared */
3277 if (evsel->tp_format)
3278 return 0;
3279
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09003280 if (pevent == NULL) {
3281 pr_debug("broken or missing trace data\n");
3282 return -1;
3283 }
3284
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04003285 event = tep_find_event(pevent, evsel->attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09003286 if (event == NULL) {
3287 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003288 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09003289 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003290
Namhyung Kim831394b2012-09-06 11:10:46 +09003291 if (!evsel->name) {
3292 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3293 evsel->name = strdup(bf);
3294 if (evsel->name == NULL)
3295 return -1;
3296 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003297
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03003298 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003299 return 0;
3300}
3301
Namhyung Kim831394b2012-09-06 11:10:46 +09003302static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003303 struct tep_handle *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003304{
3305 struct perf_evsel *pos;
3306
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003307 evlist__for_each_entry(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09003308 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
3309 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003310 return -1;
3311 }
3312
3313 return 0;
3314}
3315
Jiri Olsad4339562013-07-17 19:49:41 +02003316int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05003317{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003318 struct perf_data *data = session->data;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003319 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003320 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003321 struct perf_file_attr f_attr;
3322 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003323 int nr_attrs, nr_ids, i, j;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003324 int fd = perf_data__fd(data);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003325
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003326 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003327 if (session->evlist == NULL)
3328 return -ENOMEM;
3329
Kan Liang2c071442015-08-28 05:48:05 -04003330 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03003331 session->machines.host.env = &header->env;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003332 if (perf_data__is_pipe(data))
Jiri Olsad4339562013-07-17 19:49:41 +02003333 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05003334
Stephane Eranian69996df2012-02-09 23:21:06 +01003335 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003336 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003337
Namhyung Kimb314e5c2013-09-30 17:19:48 +09003338 /*
3339 * Sanity check that perf.data was written cleanly; data size is
3340 * initialized to 0 and updated only if the on_exit function is run.
3341 * If data size is still 0 then the file contains only partial
3342 * information. Just warn user and process it as much as it can.
3343 */
3344 if (f_header.data.size == 0) {
3345 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
3346 "Was the 'perf record' command properly terminated?\n",
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003347 data->file.path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09003348 }
3349
Stephane Eranian69996df2012-02-09 23:21:06 +01003350 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003351 lseek(fd, f_header.attrs.offset, SEEK_SET);
3352
3353 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003354 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02003355 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003356
Stephane Eranian69996df2012-02-09 23:21:06 +01003357 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003358 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003359
David Ahern1060ab82015-04-09 16:15:46 -04003360 if (header->needs_swap) {
3361 f_attr.ids.size = bswap_64(f_attr.ids.size);
3362 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06003363 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04003364 }
David Aherneda39132011-07-15 12:34:09 -06003365
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02003366 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003367 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003368
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003369 if (evsel == NULL)
3370 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03003371
3372 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003373 /*
3374 * Do it before so that if perf_evsel__alloc_id fails, this
3375 * entry gets purged too at perf_evlist__delete().
3376 */
3377 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003378
3379 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003380 /*
3381 * We don't have the cpu and thread maps on the header, so
3382 * for allocating the perf_sample_id table we fake 1 cpu and
3383 * hattr->ids threads.
3384 */
3385 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
3386 goto out_delete_evlist;
3387
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003388 lseek(fd, f_attr.ids.offset, SEEK_SET);
3389
3390 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003391 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003392 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003393
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003394 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003395 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02003396
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003397 lseek(fd, tmp, SEEK_SET);
3398 }
3399
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003400 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003401 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01003402
Namhyung Kim831394b2012-09-06 11:10:46 +09003403 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003404 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003405 goto out_delete_evlist;
3406
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003407 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003408out_errno:
3409 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003410
3411out_delete_evlist:
3412 perf_evlist__delete(session->evlist);
3413 session->evlist = NULL;
3414 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003415}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02003416
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003417int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02003418 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003419 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02003420{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003421 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003422 size_t size;
3423 int err;
3424
3425 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003426 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003427 size += sizeof(struct perf_event_header);
3428 size += ids * sizeof(u64);
3429
3430 ev = malloc(size);
3431
Chris Samuelce47dc52010-11-13 13:35:06 +11003432 if (ev == NULL)
3433 return -ENOMEM;
3434
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003435 ev->attr.attr = *attr;
3436 memcpy(ev->attr.id, id, ids * sizeof(u64));
3437
3438 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02003439 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003440
Robert Richterf4d83432012-08-16 21:10:17 +02003441 if (ev->attr.header.size == size)
3442 err = process(tool, ev, NULL, NULL);
3443 else
3444 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003445
3446 free(ev);
3447
3448 return err;
3449}
3450
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003451int perf_event__synthesize_features(struct perf_tool *tool,
3452 struct perf_session *session,
3453 struct perf_evlist *evlist,
3454 perf_event__handler_t process)
3455{
3456 struct perf_header *header = &session->header;
3457 struct feat_fd ff;
3458 struct feature_event *fe;
3459 size_t sz, sz_hdr;
3460 int feat, ret;
3461
3462 sz_hdr = sizeof(fe->header);
3463 sz = sizeof(union perf_event);
3464 /* get a nice alignment */
3465 sz = PERF_ALIGN(sz, page_size);
3466
3467 memset(&ff, 0, sizeof(ff));
3468
3469 ff.buf = malloc(sz);
3470 if (!ff.buf)
3471 return -ENOMEM;
3472
3473 ff.size = sz - sz_hdr;
3474
3475 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3476 if (!feat_ops[feat].synthesize) {
3477 pr_debug("No record header feature for header :%d\n", feat);
3478 continue;
3479 }
3480
3481 ff.offset = sizeof(*fe);
3482
3483 ret = feat_ops[feat].write(&ff, evlist);
3484 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3485 pr_debug("Error writing feature\n");
3486 continue;
3487 }
3488 /* ff.buf may have changed due to realloc in do_write() */
3489 fe = ff.buf;
3490 memset(fe, 0, sizeof(*fe));
3491
3492 fe->feat_id = feat;
3493 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3494 fe->header.size = ff.offset;
3495
3496 ret = process(tool, ff.buf, NULL, NULL);
3497 if (ret) {
3498 free(ff.buf);
3499 return ret;
3500 }
3501 }
Jiri Olsa57b5de42018-03-14 10:22:05 +01003502
3503 /* Send HEADER_LAST_FEATURE mark. */
3504 fe = ff.buf;
3505 fe->feat_id = HEADER_LAST_FEATURE;
3506 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3507 fe->header.size = sizeof(*fe);
3508
3509 ret = process(tool, ff.buf, NULL, NULL);
3510
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003511 free(ff.buf);
Jiri Olsa57b5de42018-03-14 10:22:05 +01003512 return ret;
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003513}
3514
Jiri Olsa89f16882018-09-13 14:54:03 +02003515int perf_event__process_feature(struct perf_session *session,
3516 union perf_event *event)
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003517{
Jiri Olsa89f16882018-09-13 14:54:03 +02003518 struct perf_tool *tool = session->tool;
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003519 struct feat_fd ff = { .fd = 0 };
3520 struct feature_event *fe = (struct feature_event *)event;
3521 int type = fe->header.type;
3522 u64 feat = fe->feat_id;
3523
3524 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3525 pr_warning("invalid record type %d in pipe-mode\n", type);
3526 return 0;
3527 }
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303528 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003529 pr_warning("invalid record type %d in pipe-mode\n", type);
3530 return -1;
3531 }
3532
3533 if (!feat_ops[feat].process)
3534 return 0;
3535
3536 ff.buf = (void *)fe->data;
3537 ff.size = event->header.size - sizeof(event->header);
3538 ff.ph = &session->header;
3539
3540 if (feat_ops[feat].process(&ff, NULL))
3541 return -1;
3542
3543 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3544 return 0;
3545
3546 if (!feat_ops[feat].full_only ||
3547 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3548 feat_ops[feat].print(&ff, stdout);
3549 } else {
3550 fprintf(stdout, "# %s info available, use -I to display\n",
3551 feat_ops[feat].name);
3552 }
3553
3554 return 0;
3555}
3556
Jiri Olsaa6e52812015-10-25 15:51:37 +01003557static struct event_update_event *
3558event_update_event__new(size_t size, u64 type, u64 id)
3559{
3560 struct event_update_event *ev;
3561
3562 size += sizeof(*ev);
3563 size = PERF_ALIGN(size, sizeof(u64));
3564
3565 ev = zalloc(size);
3566 if (ev) {
3567 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3568 ev->header.size = (u16)size;
3569 ev->type = type;
3570 ev->id = id;
3571 }
3572 return ev;
3573}
3574
3575int
3576perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3577 struct perf_evsel *evsel,
3578 perf_event__handler_t process)
3579{
3580 struct event_update_event *ev;
3581 size_t size = strlen(evsel->unit);
3582 int err;
3583
3584 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3585 if (ev == NULL)
3586 return -ENOMEM;
3587
Arnaldo Carvalho de Melo75725882018-12-06 11:02:57 -03003588 strlcpy(ev->data, evsel->unit, size + 1);
Jiri Olsaa6e52812015-10-25 15:51:37 +01003589 err = process(tool, (union perf_event *)ev, NULL, NULL);
3590 free(ev);
3591 return err;
3592}
3593
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003594int
3595perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3596 struct perf_evsel *evsel,
3597 perf_event__handler_t process)
3598{
3599 struct event_update_event *ev;
3600 struct event_update_event_scale *ev_data;
3601 int err;
3602
3603 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3604 if (ev == NULL)
3605 return -ENOMEM;
3606
3607 ev_data = (struct event_update_event_scale *) ev->data;
3608 ev_data->scale = evsel->scale;
3609 err = process(tool, (union perf_event*) ev, NULL, NULL);
3610 free(ev);
3611 return err;
3612}
3613
Jiri Olsa802c9042015-10-25 15:51:39 +01003614int
3615perf_event__synthesize_event_update_name(struct perf_tool *tool,
3616 struct perf_evsel *evsel,
3617 perf_event__handler_t process)
3618{
3619 struct event_update_event *ev;
3620 size_t len = strlen(evsel->name);
3621 int err;
3622
3623 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3624 if (ev == NULL)
3625 return -ENOMEM;
3626
Arnaldo Carvalho de Melo5192bde2018-12-06 11:09:46 -03003627 strlcpy(ev->data, evsel->name, len + 1);
Jiri Olsa802c9042015-10-25 15:51:39 +01003628 err = process(tool, (union perf_event*) ev, NULL, NULL);
3629 free(ev);
3630 return err;
3631}
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003632
Jiri Olsa86ebb092015-10-25 15:51:40 +01003633int
3634perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3635 struct perf_evsel *evsel,
3636 perf_event__handler_t process)
3637{
3638 size_t size = sizeof(struct event_update_event);
3639 struct event_update_event *ev;
3640 int max, err;
3641 u16 type;
3642
3643 if (!evsel->own_cpus)
3644 return 0;
3645
3646 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3647 if (!ev)
3648 return -ENOMEM;
3649
3650 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3651 ev->header.size = (u16)size;
3652 ev->type = PERF_EVENT_UPDATE__CPUS;
3653 ev->id = evsel->id[0];
3654
3655 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3656 evsel->own_cpus,
3657 type, max);
3658
3659 err = process(tool, (union perf_event*) ev, NULL, NULL);
3660 free(ev);
3661 return err;
3662}
3663
Jiri Olsac853f932015-10-25 15:51:41 +01003664size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3665{
3666 struct event_update_event *ev = &event->event_update;
3667 struct event_update_event_scale *ev_scale;
3668 struct event_update_event_cpus *ev_cpus;
3669 struct cpu_map *map;
3670 size_t ret;
3671
3672 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3673
3674 switch (ev->type) {
3675 case PERF_EVENT_UPDATE__SCALE:
3676 ev_scale = (struct event_update_event_scale *) ev->data;
3677 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3678 break;
3679 case PERF_EVENT_UPDATE__UNIT:
3680 ret += fprintf(fp, "... unit: %s\n", ev->data);
3681 break;
3682 case PERF_EVENT_UPDATE__NAME:
3683 ret += fprintf(fp, "... name: %s\n", ev->data);
3684 break;
3685 case PERF_EVENT_UPDATE__CPUS:
3686 ev_cpus = (struct event_update_event_cpus *) ev->data;
3687 ret += fprintf(fp, "... ");
3688
3689 map = cpu_map__new_data(&ev_cpus->cpus);
3690 if (map)
3691 ret += cpu_map__fprintf(map, fp);
3692 else
3693 ret += fprintf(fp, "failed to get cpus\n");
3694 break;
3695 default:
3696 ret += fprintf(fp, "... unknown type\n");
3697 break;
3698 }
3699
3700 return ret;
3701}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003702
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003703int perf_event__synthesize_attrs(struct perf_tool *tool,
Jiri Olsa318ec182018-08-30 08:32:15 +02003704 struct perf_evlist *evlist,
3705 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003706{
Robert Richter6606f872012-08-16 21:10:19 +02003707 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003708 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003709
Jiri Olsa318ec182018-08-30 08:32:15 +02003710 evlist__for_each_entry(evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003711 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3712 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003713 if (err) {
3714 pr_debug("failed to create perf header attribute\n");
3715 return err;
3716 }
3717 }
3718
3719 return err;
3720}
3721
Andi Kleenbfd8f722017-11-17 13:42:58 -08003722static bool has_unit(struct perf_evsel *counter)
3723{
3724 return counter->unit && *counter->unit;
3725}
3726
3727static bool has_scale(struct perf_evsel *counter)
3728{
3729 return counter->scale != 1;
3730}
3731
3732int perf_event__synthesize_extra_attr(struct perf_tool *tool,
3733 struct perf_evlist *evsel_list,
3734 perf_event__handler_t process,
3735 bool is_pipe)
3736{
3737 struct perf_evsel *counter;
3738 int err;
3739
3740 /*
3741 * Synthesize other events stuff not carried within
3742 * attr event - unit, scale, name
3743 */
3744 evlist__for_each_entry(evsel_list, counter) {
3745 if (!counter->supported)
3746 continue;
3747
3748 /*
3749 * Synthesize unit and scale only if it's defined.
3750 */
3751 if (has_unit(counter)) {
3752 err = perf_event__synthesize_event_update_unit(tool, counter, process);
3753 if (err < 0) {
3754 pr_err("Couldn't synthesize evsel unit.\n");
3755 return err;
3756 }
3757 }
3758
3759 if (has_scale(counter)) {
3760 err = perf_event__synthesize_event_update_scale(tool, counter, process);
3761 if (err < 0) {
3762 pr_err("Couldn't synthesize evsel counter.\n");
3763 return err;
3764 }
3765 }
3766
3767 if (counter->own_cpus) {
3768 err = perf_event__synthesize_event_update_cpus(tool, counter, process);
3769 if (err < 0) {
3770 pr_err("Couldn't synthesize evsel cpus.\n");
3771 return err;
3772 }
3773 }
3774
3775 /*
3776 * Name is needed only for pipe output,
3777 * perf.data carries event names.
3778 */
3779 if (is_pipe) {
3780 err = perf_event__synthesize_event_update_name(tool, counter, process);
3781 if (err < 0) {
3782 pr_err("Couldn't synthesize evsel name.\n");
3783 return err;
3784 }
3785 }
3786 }
3787 return 0;
3788}
3789
Adrian Hunter47c3d102013-07-04 16:20:21 +03003790int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3791 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003792 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003793{
Robert Richterf4d83432012-08-16 21:10:17 +02003794 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003795 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003796 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003797
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003798 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003799 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003800 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003801 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003802 }
3803
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003804 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003805 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003806 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003807
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003808 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003809
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003810 ids = event->header.size;
3811 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003812 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003813 /*
3814 * We don't have the cpu and thread maps on the header, so
3815 * for allocating the perf_sample_id table we fake 1 cpu and
3816 * hattr->ids threads.
3817 */
3818 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3819 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003820
3821 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003822 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003823 }
3824
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003825 return 0;
3826}
Tom Zanussicd19a032010-04-01 23:59:20 -05003827
Jiri Olsaffe777252015-10-25 15:51:36 +01003828int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3829 union perf_event *event,
3830 struct perf_evlist **pevlist)
3831{
3832 struct event_update_event *ev = &event->event_update;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003833 struct event_update_event_scale *ev_scale;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003834 struct event_update_event_cpus *ev_cpus;
Jiri Olsaffe777252015-10-25 15:51:36 +01003835 struct perf_evlist *evlist;
3836 struct perf_evsel *evsel;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003837 struct cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003838
3839 if (!pevlist || *pevlist == NULL)
3840 return -EINVAL;
3841
3842 evlist = *pevlist;
3843
3844 evsel = perf_evlist__id2evsel(evlist, ev->id);
3845 if (evsel == NULL)
3846 return -EINVAL;
3847
Jiri Olsaa6e52812015-10-25 15:51:37 +01003848 switch (ev->type) {
3849 case PERF_EVENT_UPDATE__UNIT:
3850 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003851 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003852 case PERF_EVENT_UPDATE__NAME:
3853 evsel->name = strdup(ev->data);
3854 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003855 case PERF_EVENT_UPDATE__SCALE:
3856 ev_scale = (struct event_update_event_scale *) ev->data;
3857 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003858 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003859 case PERF_EVENT_UPDATE__CPUS:
3860 ev_cpus = (struct event_update_event_cpus *) ev->data;
3861
3862 map = cpu_map__new_data(&ev_cpus->cpus);
3863 if (map)
3864 evsel->own_cpus = map;
3865 else
3866 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003867 default:
3868 break;
3869 }
3870
Jiri Olsaffe777252015-10-25 15:51:36 +01003871 return 0;
3872}
3873
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003874int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003875 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003876 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003877{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003878 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003879 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003880 ssize_t size = 0, aligned_size = 0, padding;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003881 struct feat_fd ff;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003882 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003883
Jiri Olsa29208e52011-10-20 15:59:43 +02003884 /*
3885 * We are going to store the size of the data followed
3886 * by the data contents. Since the fd descriptor is a pipe,
3887 * we cannot seek back to store the size of the data once
3888 * we know it. Instead we:
3889 *
3890 * - write the tracing data to the temp file
3891 * - get/write the data size to pipe
3892 * - write the tracing data from the temp file
3893 * to the pipe
3894 */
3895 tdata = tracing_data_get(&evlist->entries, fd, true);
3896 if (!tdata)
3897 return -1;
3898
Tom Zanussi92155452010-04-01 23:59:21 -05003899 memset(&ev, 0, sizeof(ev));
3900
3901 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003902 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003903 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003904 padding = aligned_size - size;
3905 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3906 ev.tracing_data.size = aligned_size;
3907
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003908 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003909
Jiri Olsa29208e52011-10-20 15:59:43 +02003910 /*
3911 * The put function will copy all the tracing data
3912 * stored in temp file to the pipe.
3913 */
3914 tracing_data_put(tdata);
3915
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003916 ff = (struct feat_fd){ .fd = fd };
3917 if (write_padded(&ff, NULL, 0, padding))
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -07003918 return -1;
Tom Zanussi92155452010-04-01 23:59:21 -05003919
3920 return aligned_size;
3921}
3922
Jiri Olsa89f16882018-09-13 14:54:03 +02003923int perf_event__process_tracing_data(struct perf_session *session,
3924 union perf_event *event)
Tom Zanussi92155452010-04-01 23:59:21 -05003925{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003926 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003927 int fd = perf_data__fd(session->data);
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003928 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003929 char buf[BUFSIZ];
3930
3931 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003932 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003933 SEEK_SET);
3934
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003935 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003936 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003937 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003938
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003939 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003940 pr_err("%s: reading input file", __func__);
3941 return -1;
3942 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003943 if (session->repipe) {
3944 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003945 if (retw <= 0 || retw != padding) {
3946 pr_err("%s: repiping tracing data padding", __func__);
3947 return -1;
3948 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003949 }
Tom Zanussi92155452010-04-01 23:59:21 -05003950
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003951 if (size_read + padding != size) {
3952 pr_err("%s: tracing data size mismatch", __func__);
3953 return -1;
3954 }
Tom Zanussi92155452010-04-01 23:59:21 -05003955
Namhyung Kim831394b2012-09-06 11:10:46 +09003956 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003957 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003958
Tom Zanussi92155452010-04-01 23:59:21 -05003959 return size_read + padding;
3960}
Tom Zanussic7929e42010-04-01 23:59:22 -05003961
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003962int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003963 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003964 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003965 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003966{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003967 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003968 size_t len;
3969 int err = 0;
3970
3971 if (!pos->hit)
3972 return err;
3973
3974 memset(&ev, 0, sizeof(ev));
3975
3976 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003977 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003978 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3979 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3980 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003981 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003982 ev.build_id.header.size = sizeof(ev.build_id) + len;
3983 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3984
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003985 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003986
3987 return err;
3988}
3989
Jiri Olsa89f16882018-09-13 14:54:03 +02003990int perf_event__process_build_id(struct perf_session *session,
3991 union perf_event *event)
Tom Zanussic7929e42010-04-01 23:59:22 -05003992{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003993 __event_process_build_id(&event->build_id,
3994 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003995 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003996 return 0;
3997}