blob: a326e0d8b5b6b2a48be49091422382e42fbc8541 [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>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020020
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020021#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030022#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020023#include "header.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030024#include "memswap.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020025#include "../perf.h"
26#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020027#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010028#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010029#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020030#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020031#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020032#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090033#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020034#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020035#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010036#include <api/fs/fs.h>
37#include "asm/bug.h"
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -070038#include "tool.h"
Jin Yao60115182017-12-08 21:13:41 +080039#include "time-utils.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020040
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030041#include "sane_ctype.h"
42
Stephane Eranian73323f52012-02-02 13:54:44 +010043/*
44 * magic2 = "PERFILE2"
45 * must be a numerical value to let the endianness
46 * determine the memory layout. That way we are able
47 * to detect endianness when reading the perf.data file
48 * back.
49 *
50 * we check for legacy (PERFFILE) format.
51 */
52static const char *__perf_magic1 = "PERFFILE";
53static const u64 __perf_magic2 = 0x32454c4946524550ULL;
54static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020055
Stephane Eranian73323f52012-02-02 13:54:44 +010056#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020057
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090058const char perf_version_string[] = PERF_VERSION;
59
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020060struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020061 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020062 struct perf_file_section ids;
63};
64
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070065struct feat_fd {
66 struct perf_header *ph;
67 int fd;
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -070068 void *buf; /* Either buf != NULL or fd >= 0 */
David Carrillo-Cisneros62552452017-07-17 21:25:42 -070069 ssize_t offset;
70 size_t size;
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -070071 struct perf_evsel *events;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070072};
73
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030074void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020075{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030076 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020077}
78
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030079void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020080{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030081 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020082}
83
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030084bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020085{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030086 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020087}
88
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -070089static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
90{
91 ssize_t ret = writen(ff->fd, buf, size);
92
93 if (ret != (ssize_t)size)
94 return ret < 0 ? (int)ret : -1;
95 return 0;
96}
97
98static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
99{
100 /* struct perf_event_header::size is u16 */
101 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
102 size_t new_size = ff->size;
103 void *addr;
104
105 if (size + ff->offset > max_size)
106 return -E2BIG;
107
108 while (size > (new_size - ff->offset))
109 new_size <<= 1;
110 new_size = min(max_size, new_size);
111
112 if (ff->size < new_size) {
113 addr = realloc(ff->buf, new_size);
114 if (!addr)
115 return -ENOMEM;
116 ff->buf = addr;
117 ff->size = new_size;
118 }
119
120 memcpy(ff->buf + ff->offset, buf, size);
121 ff->offset += size;
122
123 return 0;
124}
125
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700126/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700127int do_write(struct feat_fd *ff, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200128{
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700129 if (!ff->buf)
130 return __do_write_fd(ff, buf, size);
131 return __do_write_buf(ff, buf, size);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200132}
133
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700134/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700135int write_padded(struct feat_fd *ff, const void *bf,
136 size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200137{
138 static const char zero_buf[NAME_ALIGN];
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700139 int err = do_write(ff, bf, count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200140
141 if (!err)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700142 err = do_write(ff, zero_buf, count_aligned - count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200143
144 return err;
145}
146
Kan Liang2bb00d22015-09-01 09:58:12 -0400147#define string_size(str) \
148 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
149
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700150/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700151static int do_write_string(struct feat_fd *ff, const char *str)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200152{
153 u32 len, olen;
154 int ret;
155
156 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300157 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200158
159 /* write len, incl. \0 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700160 ret = do_write(ff, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200161 if (ret < 0)
162 return ret;
163
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700164 return write_padded(ff, str, olen, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200165}
166
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700167static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700168{
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700169 ssize_t ret = readn(ff->fd, addr, size);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700170
171 if (ret != size)
172 return ret < 0 ? (int)ret : -1;
173 return 0;
174}
175
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700176static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
177{
178 if (size > (ssize_t)ff->size - ff->offset)
179 return -1;
180
181 memcpy(addr, ff->buf + ff->offset, size);
182 ff->offset += size;
183
184 return 0;
185
186}
187
188static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
189{
190 if (!ff->buf)
191 return __do_read_fd(ff, addr, size);
192 return __do_read_buf(ff, addr, size);
193}
194
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700195static int do_read_u32(struct feat_fd *ff, u32 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700196{
197 int ret;
198
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700199 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700200 if (ret)
201 return ret;
202
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700203 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700204 *addr = bswap_32(*addr);
205 return 0;
206}
207
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700208static int do_read_u64(struct feat_fd *ff, u64 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700209{
210 int ret;
211
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700212 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700213 if (ret)
214 return ret;
215
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700216 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700217 *addr = bswap_64(*addr);
218 return 0;
219}
220
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700221static char *do_read_string(struct feat_fd *ff)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200222{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200223 u32 len;
224 char *buf;
225
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700226 if (do_read_u32(ff, &len))
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200227 return NULL;
228
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200229 buf = malloc(len);
230 if (!buf)
231 return NULL;
232
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700233 if (!__do_read(ff, buf, len)) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200234 /*
235 * strings are padded by zeroes
236 * thus the actual strlen of buf
237 * may be less than len
238 */
239 return buf;
240 }
241
242 free(buf);
243 return NULL;
244}
245
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700246static int write_tracing_data(struct feat_fd *ff,
247 struct perf_evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200248{
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700249 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
250 return -1;
251
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700252 return read_tracing_data(ff->fd, &evlist->entries);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200253}
254
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700255static int write_build_id(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300256 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200257{
258 struct perf_session *session;
259 int err;
260
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700261 session = container_of(ff->ph, struct perf_session, header);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200262
Robert Richtere20960c2011-12-07 10:02:55 +0100263 if (!perf_session__read_build_ids(session, true))
264 return -1;
265
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700266 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
267 return -1;
268
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700269 err = perf_session__write_buildid_table(session, ff);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200270 if (err < 0) {
271 pr_debug("failed to write buildid table\n");
272 return err;
273 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900274 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200275
276 return 0;
277}
278
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700279static int write_hostname(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300280 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200281{
282 struct utsname uts;
283 int ret;
284
285 ret = uname(&uts);
286 if (ret < 0)
287 return -1;
288
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700289 return do_write_string(ff, uts.nodename);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200290}
291
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700292static int write_osrelease(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300293 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200294{
295 struct utsname uts;
296 int ret;
297
298 ret = uname(&uts);
299 if (ret < 0)
300 return -1;
301
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700302 return do_write_string(ff, uts.release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200303}
304
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700305static int write_arch(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300306 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200307{
308 struct utsname uts;
309 int ret;
310
311 ret = uname(&uts);
312 if (ret < 0)
313 return -1;
314
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700315 return do_write_string(ff, uts.machine);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200316}
317
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700318static int write_version(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300319 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200320{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700321 return do_write_string(ff, perf_version_string);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200322}
323
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700324static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200325{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200326 FILE *file;
327 char *buf = NULL;
328 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800329 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200330 size_t len = 0;
331 int ret = -1;
332
333 if (!search)
334 return -1;
335
336 file = fopen("/proc/cpuinfo", "r");
337 if (!file)
338 return -1;
339
340 while (getline(&buf, &len, file) > 0) {
341 ret = strncmp(buf, search, strlen(search));
342 if (!ret)
343 break;
344 }
345
Wang Naned307752014-10-16 11:08:29 +0800346 if (ret) {
347 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200348 goto done;
Wang Naned307752014-10-16 11:08:29 +0800349 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200350
351 s = buf;
352
353 p = strchr(buf, ':');
354 if (p && *(p+1) == ' ' && *(p+2))
355 s = p + 2;
356 p = strchr(s, '\n');
357 if (p)
358 *p = '\0';
359
360 /* squash extra space characters (branding string) */
361 p = s;
362 while (*p) {
363 if (isspace(*p)) {
364 char *r = p + 1;
365 char *q = r;
366 *p = ' ';
367 while (*q && isspace(*q))
368 q++;
369 if (q != (p+1))
370 while ((*r++ = *q++));
371 }
372 p++;
373 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700374 ret = do_write_string(ff, s);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200375done:
376 free(buf);
377 fclose(file);
378 return ret;
379}
380
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700381static int write_cpudesc(struct feat_fd *ff,
Wang Nan493c3032014-10-24 09:45:26 +0800382 struct perf_evlist *evlist __maybe_unused)
383{
Wang Nan493c3032014-10-24 09:45:26 +0800384 const char *cpuinfo_procs[] = CPUINFO_PROC;
385 unsigned int i;
386
387 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
388 int ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700389 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
Wang Nan493c3032014-10-24 09:45:26 +0800390 if (ret >= 0)
391 return ret;
392 }
393 return -1;
394}
395
396
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700397static int write_nrcpus(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300398 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200399{
400 long nr;
401 u32 nrc, nra;
402 int ret;
403
Jan Stancekda8a58b2017-02-17 12:10:26 +0100404 nrc = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200405
406 nr = sysconf(_SC_NPROCESSORS_ONLN);
407 if (nr < 0)
408 return -1;
409
410 nra = (u32)(nr & UINT_MAX);
411
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700412 ret = do_write(ff, &nrc, sizeof(nrc));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200413 if (ret < 0)
414 return ret;
415
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700416 return do_write(ff, &nra, sizeof(nra));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200417}
418
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700419static int write_event_desc(struct feat_fd *ff,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200420 struct perf_evlist *evlist)
421{
Robert Richter6606f872012-08-16 21:10:19 +0200422 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900423 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200424 int ret;
425
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900426 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200427
428 /*
429 * write number of events
430 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700431 ret = do_write(ff, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200432 if (ret < 0)
433 return ret;
434
435 /*
436 * size of perf_event_attr struct
437 */
Robert Richter6606f872012-08-16 21:10:19 +0200438 sz = (u32)sizeof(evsel->attr);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700439 ret = do_write(ff, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200440 if (ret < 0)
441 return ret;
442
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300443 evlist__for_each_entry(evlist, evsel) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700444 ret = do_write(ff, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200445 if (ret < 0)
446 return ret;
447 /*
448 * write number of unique id per event
449 * there is one id per instance of an event
450 *
451 * copy into an nri to be independent of the
452 * type of ids,
453 */
Robert Richter6606f872012-08-16 21:10:19 +0200454 nri = evsel->ids;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700455 ret = do_write(ff, &nri, sizeof(nri));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200456 if (ret < 0)
457 return ret;
458
459 /*
460 * write event string as passed on cmdline
461 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700462 ret = do_write_string(ff, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200463 if (ret < 0)
464 return ret;
465 /*
466 * write unique ids for this event
467 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700468 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200469 if (ret < 0)
470 return ret;
471 }
472 return 0;
473}
474
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700475static int write_cmdline(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300476 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200477{
478 char buf[MAXPATHLEN];
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300479 u32 n;
480 int i, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200481
Tommi Rantala55f771282017-03-22 15:06:24 +0200482 /* actual path to perf binary */
483 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200484 if (ret <= 0)
485 return -1;
486
487 /* readlink() does not add null termination */
488 buf[ret] = '\0';
489
490 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300491 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200492
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700493 ret = do_write(ff, &n, sizeof(n));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200494 if (ret < 0)
495 return ret;
496
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700497 ret = do_write_string(ff, buf);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200498 if (ret < 0)
499 return ret;
500
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300501 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700502 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200503 if (ret < 0)
504 return ret;
505 }
506 return 0;
507}
508
509#define CORE_SIB_FMT \
510 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
511#define THRD_SIB_FMT \
512 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
513
514struct cpu_topo {
Kan Liang2bb00d22015-09-01 09:58:12 -0400515 u32 cpu_nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200516 u32 core_sib;
517 u32 thread_sib;
518 char **core_siblings;
519 char **thread_siblings;
520};
521
522static int build_cpu_topo(struct cpu_topo *tp, int cpu)
523{
524 FILE *fp;
525 char filename[MAXPATHLEN];
526 char *buf = NULL, *p;
527 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200528 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200529 u32 i = 0;
530 int ret = -1;
531
532 sprintf(filename, CORE_SIB_FMT, cpu);
533 fp = fopen(filename, "r");
534 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200535 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200536
Stephane Eranianc5885742013-08-14 12:04:26 +0200537 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200538 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200539 if (sret <= 0)
540 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200541
542 p = strchr(buf, '\n');
543 if (p)
544 *p = '\0';
545
546 for (i = 0; i < tp->core_sib; i++) {
547 if (!strcmp(buf, tp->core_siblings[i]))
548 break;
549 }
550 if (i == tp->core_sib) {
551 tp->core_siblings[i] = buf;
552 tp->core_sib++;
553 buf = NULL;
554 len = 0;
555 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200556 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200557
Stephane Eranianc5885742013-08-14 12:04:26 +0200558try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200559 sprintf(filename, THRD_SIB_FMT, cpu);
560 fp = fopen(filename, "r");
561 if (!fp)
562 goto done;
563
564 if (getline(&buf, &len, fp) <= 0)
565 goto done;
566
567 p = strchr(buf, '\n');
568 if (p)
569 *p = '\0';
570
571 for (i = 0; i < tp->thread_sib; i++) {
572 if (!strcmp(buf, tp->thread_siblings[i]))
573 break;
574 }
575 if (i == tp->thread_sib) {
576 tp->thread_siblings[i] = buf;
577 tp->thread_sib++;
578 buf = NULL;
579 }
580 ret = 0;
581done:
582 if(fp)
583 fclose(fp);
584 free(buf);
585 return ret;
586}
587
588static void free_cpu_topo(struct cpu_topo *tp)
589{
590 u32 i;
591
592 if (!tp)
593 return;
594
595 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300596 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200597
598 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300599 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200600
601 free(tp);
602}
603
604static struct cpu_topo *build_cpu_topology(void)
605{
Jan Stancek43db2842017-02-17 12:10:25 +0100606 struct cpu_topo *tp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200607 void *addr;
608 u32 nr, i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300609 size_t sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200610 long ncpus;
611 int ret = -1;
Jan Stancek43db2842017-02-17 12:10:25 +0100612 struct cpu_map *map;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200613
Jan Stancekda8a58b2017-02-17 12:10:26 +0100614 ncpus = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200615
Jan Stancek43db2842017-02-17 12:10:25 +0100616 /* build online CPU map */
617 map = cpu_map__new(NULL);
618 if (map == NULL) {
619 pr_debug("failed to get system cpumap\n");
620 return NULL;
621 }
622
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200623 nr = (u32)(ncpus & UINT_MAX);
624
625 sz = nr * sizeof(char *);
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300626 addr = calloc(1, sizeof(*tp) + 2 * sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200627 if (!addr)
Jan Stancek43db2842017-02-17 12:10:25 +0100628 goto out_free;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200629
630 tp = addr;
Kan Liang2bb00d22015-09-01 09:58:12 -0400631 tp->cpu_nr = nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200632 addr += sizeof(*tp);
633 tp->core_siblings = addr;
634 addr += sz;
635 tp->thread_siblings = addr;
636
637 for (i = 0; i < nr; i++) {
Jan Stancek43db2842017-02-17 12:10:25 +0100638 if (!cpu_map__has(map, i))
639 continue;
640
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200641 ret = build_cpu_topo(tp, i);
642 if (ret < 0)
643 break;
644 }
Jan Stancek43db2842017-02-17 12:10:25 +0100645
646out_free:
647 cpu_map__put(map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200648 if (ret) {
649 free_cpu_topo(tp);
650 tp = NULL;
651 }
652 return tp;
653}
654
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700655static int write_cpu_topology(struct feat_fd *ff,
656 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200657{
658 struct cpu_topo *tp;
659 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300660 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200661
662 tp = build_cpu_topology();
663 if (!tp)
664 return -1;
665
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700666 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200667 if (ret < 0)
668 goto done;
669
670 for (i = 0; i < tp->core_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700671 ret = do_write_string(ff, tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200672 if (ret < 0)
673 goto done;
674 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700675 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200676 if (ret < 0)
677 goto done;
678
679 for (i = 0; i < tp->thread_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700680 ret = do_write_string(ff, tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200681 if (ret < 0)
682 break;
683 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400684
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300685 ret = perf_env__read_cpu_topology_map(&perf_env);
686 if (ret < 0)
687 goto done;
688
689 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700690 ret = do_write(ff, &perf_env.cpu[j].core_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300691 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400692 if (ret < 0)
693 return ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700694 ret = do_write(ff, &perf_env.cpu[j].socket_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300695 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400696 if (ret < 0)
697 return ret;
698 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200699done:
700 free_cpu_topo(tp);
701 return ret;
702}
703
704
705
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700706static int write_total_mem(struct feat_fd *ff,
707 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200708{
709 char *buf = NULL;
710 FILE *fp;
711 size_t len = 0;
712 int ret = -1, n;
713 uint64_t mem;
714
715 fp = fopen("/proc/meminfo", "r");
716 if (!fp)
717 return -1;
718
719 while (getline(&buf, &len, fp) > 0) {
720 ret = strncmp(buf, "MemTotal:", 9);
721 if (!ret)
722 break;
723 }
724 if (!ret) {
725 n = sscanf(buf, "%*s %"PRIu64, &mem);
726 if (n == 1)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700727 ret = do_write(ff, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800728 } else
729 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200730 free(buf);
731 fclose(fp);
732 return ret;
733}
734
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700735static int write_topo_node(struct feat_fd *ff, int node)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200736{
737 char str[MAXPATHLEN];
738 char field[32];
739 char *buf = NULL, *p;
740 size_t len = 0;
741 FILE *fp;
742 u64 mem_total, mem_free, mem;
743 int ret = -1;
744
745 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
746 fp = fopen(str, "r");
747 if (!fp)
748 return -1;
749
750 while (getline(&buf, &len, fp) > 0) {
751 /* skip over invalid lines */
752 if (!strchr(buf, ':'))
753 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100754 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200755 goto done;
756 if (!strcmp(field, "MemTotal:"))
757 mem_total = mem;
758 if (!strcmp(field, "MemFree:"))
759 mem_free = mem;
760 }
761
762 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100763 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200764
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700765 ret = do_write(ff, &mem_total, sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200766 if (ret)
767 goto done;
768
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700769 ret = do_write(ff, &mem_free, sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200770 if (ret)
771 goto done;
772
773 ret = -1;
774 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
775
776 fp = fopen(str, "r");
777 if (!fp)
778 goto done;
779
780 if (getline(&buf, &len, fp) <= 0)
781 goto done;
782
783 p = strchr(buf, '\n');
784 if (p)
785 *p = '\0';
786
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700787 ret = do_write_string(ff, buf);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200788done:
789 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100790 if (fp)
791 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200792 return ret;
793}
794
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700795static int write_numa_topology(struct feat_fd *ff,
796 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200797{
798 char *buf = NULL;
799 size_t len = 0;
800 FILE *fp;
801 struct cpu_map *node_map = NULL;
802 char *c;
803 u32 nr, i, j;
804 int ret = -1;
805
806 fp = fopen("/sys/devices/system/node/online", "r");
807 if (!fp)
808 return -1;
809
810 if (getline(&buf, &len, fp) <= 0)
811 goto done;
812
813 c = strchr(buf, '\n');
814 if (c)
815 *c = '\0';
816
817 node_map = cpu_map__new(buf);
818 if (!node_map)
819 goto done;
820
821 nr = (u32)node_map->nr;
822
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700823 ret = do_write(ff, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200824 if (ret < 0)
825 goto done;
826
827 for (i = 0; i < nr; i++) {
828 j = (u32)node_map->map[i];
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700829 ret = do_write(ff, &j, sizeof(j));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200830 if (ret < 0)
831 break;
832
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700833 ret = write_topo_node(ff, i);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200834 if (ret < 0)
835 break;
836 }
837done:
838 free(buf);
839 fclose(fp);
Masami Hiramatsu5191d8872015-12-09 11:11:35 +0900840 cpu_map__put(node_map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200841 return ret;
842}
843
844/*
Robert Richter50a96672012-08-16 21:10:24 +0200845 * File format:
846 *
847 * struct pmu_mappings {
848 * u32 pmu_num;
849 * struct pmu_map {
850 * u32 type;
851 * char name[];
852 * }[pmu_num];
853 * };
854 */
855
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700856static int write_pmu_mappings(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300857 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200858{
859 struct perf_pmu *pmu = NULL;
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700860 u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900861 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200862
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700863 /*
864 * Do a first pass to count number of pmu to avoid lseek so this
865 * works in pipe mode as well.
866 */
867 while ((pmu = perf_pmu__scan(pmu))) {
868 if (!pmu->name)
869 continue;
870 pmu_num++;
871 }
872
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700873 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
Namhyung Kim5323f602012-12-17 15:38:54 +0900874 if (ret < 0)
875 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200876
877 while ((pmu = perf_pmu__scan(pmu))) {
878 if (!pmu->name)
879 continue;
Namhyung Kim5323f602012-12-17 15:38:54 +0900880
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700881 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
Namhyung Kim5323f602012-12-17 15:38:54 +0900882 if (ret < 0)
883 return ret;
884
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700885 ret = do_write_string(ff, pmu->name);
Namhyung Kim5323f602012-12-17 15:38:54 +0900886 if (ret < 0)
887 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200888 }
889
Robert Richter50a96672012-08-16 21:10:24 +0200890 return 0;
891}
892
893/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900894 * File format:
895 *
896 * struct group_descs {
897 * u32 nr_groups;
898 * struct group_desc {
899 * char name[];
900 * u32 leader_idx;
901 * u32 nr_members;
902 * }[nr_groups];
903 * };
904 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700905static int write_group_desc(struct feat_fd *ff,
Namhyung Kima8bb5592013-01-22 18:09:31 +0900906 struct perf_evlist *evlist)
907{
908 u32 nr_groups = evlist->nr_groups;
909 struct perf_evsel *evsel;
910 int ret;
911
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700912 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900913 if (ret < 0)
914 return ret;
915
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300916 evlist__for_each_entry(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900917 if (perf_evsel__is_group_leader(evsel) &&
918 evsel->nr_members > 1) {
919 const char *name = evsel->group_name ?: "{anon_group}";
920 u32 leader_idx = evsel->idx;
921 u32 nr_members = evsel->nr_members;
922
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700923 ret = do_write_string(ff, name);
Namhyung Kima8bb5592013-01-22 18:09:31 +0900924 if (ret < 0)
925 return ret;
926
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700927 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900928 if (ret < 0)
929 return ret;
930
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700931 ret = do_write(ff, &nr_members, sizeof(nr_members));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900932 if (ret < 0)
933 return ret;
934 }
935 }
936 return 0;
937}
938
939/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200940 * default get_cpuid(): nothing gets recorded
Jiada Wang7a759cd2017-04-09 20:02:37 -0700941 * actual implementation must be in arch/$(SRCARCH)/util/header.c
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200942 */
Rui Teng11d8f872016-07-28 10:05:57 +0800943int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200944{
945 return -1;
946}
947
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700948static int write_cpuid(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300949 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200950{
951 char buffer[64];
952 int ret;
953
954 ret = get_cpuid(buffer, sizeof(buffer));
955 if (!ret)
956 goto write_it;
957
958 return -1;
959write_it:
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700960 return do_write_string(ff, buffer);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200961}
962
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700963static int write_branch_stack(struct feat_fd *ff __maybe_unused,
964 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100965{
966 return 0;
967}
968
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700969static int write_auxtrace(struct feat_fd *ff,
Adrian Hunter4025ea42015-04-09 18:53:41 +0300970 struct perf_evlist *evlist __maybe_unused)
971{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300972 struct perf_session *session;
973 int err;
974
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700975 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
976 return -1;
977
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700978 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300979
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700980 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300981 if (err < 0)
982 pr_err("Failed to write auxtrace index\n");
983 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300984}
985
Jiri Olsa720e98b2016-02-16 16:01:43 +0100986static int cpu_cache_level__sort(const void *a, const void *b)
987{
988 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
989 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
990
991 return cache_a->level - cache_b->level;
992}
993
994static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
995{
996 if (a->level != b->level)
997 return false;
998
999 if (a->line_size != b->line_size)
1000 return false;
1001
1002 if (a->sets != b->sets)
1003 return false;
1004
1005 if (a->ways != b->ways)
1006 return false;
1007
1008 if (strcmp(a->type, b->type))
1009 return false;
1010
1011 if (strcmp(a->size, b->size))
1012 return false;
1013
1014 if (strcmp(a->map, b->map))
1015 return false;
1016
1017 return true;
1018}
1019
1020static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1021{
1022 char path[PATH_MAX], file[PATH_MAX];
1023 struct stat st;
1024 size_t len;
1025
1026 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1027 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1028
1029 if (stat(file, &st))
1030 return 1;
1031
1032 scnprintf(file, PATH_MAX, "%s/level", path);
1033 if (sysfs__read_int(file, (int *) &cache->level))
1034 return -1;
1035
1036 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1037 if (sysfs__read_int(file, (int *) &cache->line_size))
1038 return -1;
1039
1040 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1041 if (sysfs__read_int(file, (int *) &cache->sets))
1042 return -1;
1043
1044 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1045 if (sysfs__read_int(file, (int *) &cache->ways))
1046 return -1;
1047
1048 scnprintf(file, PATH_MAX, "%s/type", path);
1049 if (sysfs__read_str(file, &cache->type, &len))
1050 return -1;
1051
1052 cache->type[len] = 0;
1053 cache->type = rtrim(cache->type);
1054
1055 scnprintf(file, PATH_MAX, "%s/size", path);
1056 if (sysfs__read_str(file, &cache->size, &len)) {
1057 free(cache->type);
1058 return -1;
1059 }
1060
1061 cache->size[len] = 0;
1062 cache->size = rtrim(cache->size);
1063
1064 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1065 if (sysfs__read_str(file, &cache->map, &len)) {
1066 free(cache->map);
1067 free(cache->type);
1068 return -1;
1069 }
1070
1071 cache->map[len] = 0;
1072 cache->map = rtrim(cache->map);
1073 return 0;
1074}
1075
1076static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1077{
1078 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1079}
1080
1081static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1082{
1083 u32 i, cnt = 0;
1084 long ncpus;
1085 u32 nr, cpu;
1086 u16 level;
1087
1088 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1089 if (ncpus < 0)
1090 return -1;
1091
1092 nr = (u32)(ncpus & UINT_MAX);
1093
1094 for (cpu = 0; cpu < nr; cpu++) {
1095 for (level = 0; level < 10; level++) {
1096 struct cpu_cache_level c;
1097 int err;
1098
1099 err = cpu_cache_level__read(&c, cpu, level);
1100 if (err < 0)
1101 return err;
1102
1103 if (err == 1)
1104 break;
1105
1106 for (i = 0; i < cnt; i++) {
1107 if (cpu_cache_level__cmp(&c, &caches[i]))
1108 break;
1109 }
1110
1111 if (i == cnt)
1112 caches[cnt++] = c;
1113 else
1114 cpu_cache_level__free(&c);
1115
1116 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1117 goto out;
1118 }
1119 }
1120 out:
1121 *cntp = cnt;
1122 return 0;
1123}
1124
1125#define MAX_CACHES 2000
1126
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001127static int write_cache(struct feat_fd *ff,
1128 struct perf_evlist *evlist __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001129{
1130 struct cpu_cache_level caches[MAX_CACHES];
1131 u32 cnt = 0, i, version = 1;
1132 int ret;
1133
1134 ret = build_caches(caches, MAX_CACHES, &cnt);
1135 if (ret)
1136 goto out;
1137
1138 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1139
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001140 ret = do_write(ff, &version, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001141 if (ret < 0)
1142 goto out;
1143
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001144 ret = do_write(ff, &cnt, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001145 if (ret < 0)
1146 goto out;
1147
1148 for (i = 0; i < cnt; i++) {
1149 struct cpu_cache_level *c = &caches[i];
1150
1151 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001152 ret = do_write(ff, &c->v, sizeof(u32)); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001153 if (ret < 0) \
1154 goto out;
1155
1156 _W(level)
1157 _W(line_size)
1158 _W(sets)
1159 _W(ways)
1160 #undef _W
1161
1162 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001163 ret = do_write_string(ff, (const char *) c->v); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001164 if (ret < 0) \
1165 goto out;
1166
1167 _W(type)
1168 _W(size)
1169 _W(map)
1170 #undef _W
1171 }
1172
1173out:
1174 for (i = 0; i < cnt; i++)
1175 cpu_cache_level__free(&caches[i]);
1176 return ret;
1177}
1178
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001179static int write_stat(struct feat_fd *ff __maybe_unused,
Jiri Olsaffa517a2015-10-25 15:51:43 +01001180 struct perf_evlist *evlist __maybe_unused)
1181{
1182 return 0;
1183}
1184
Jin Yao60115182017-12-08 21:13:41 +08001185static int write_sample_time(struct feat_fd *ff,
1186 struct perf_evlist *evlist)
1187{
1188 int ret;
1189
1190 ret = do_write(ff, &evlist->first_sample_time,
1191 sizeof(evlist->first_sample_time));
1192 if (ret < 0)
1193 return ret;
1194
1195 return do_write(ff, &evlist->last_sample_time,
1196 sizeof(evlist->last_sample_time));
1197}
1198
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001199static void print_hostname(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001200{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001201 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001202}
1203
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001204static void print_osrelease(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001205{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001206 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001207}
1208
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001209static void print_arch(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001210{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001211 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001212}
1213
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001214static void print_cpudesc(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001215{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001216 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001217}
1218
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001219static void print_nrcpus(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001220{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001221 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1222 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001223}
1224
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001225static void print_version(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001226{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001227 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001228}
1229
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001230static void print_cmdline(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001231{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001232 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001233
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001234 nr = ff->ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001235
1236 fprintf(fp, "# cmdline : ");
1237
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001238 for (i = 0; i < nr; i++)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001239 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001240 fputc('\n', fp);
1241}
1242
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001243static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001244{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001245 struct perf_header *ph = ff->ph;
1246 int cpu_nr = ph->env.nr_cpus_avail;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001247 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001248 char *str;
1249
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001250 nr = ph->env.nr_sibling_cores;
1251 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001252
1253 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001254 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001255 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001256 }
1257
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001258 nr = ph->env.nr_sibling_threads;
1259 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001260
1261 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001262 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001263 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001264 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001265
1266 if (ph->env.cpu != NULL) {
1267 for (i = 0; i < cpu_nr; i++)
1268 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1269 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1270 } else
1271 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001272}
1273
Robert Richter4e1b9c62012-08-16 21:10:22 +02001274static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001275{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001276 struct perf_evsel *evsel;
1277
1278 if (!events)
1279 return;
1280
1281 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001282 zfree(&evsel->name);
1283 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001284 }
1285
1286 free(events);
1287}
1288
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001289static struct perf_evsel *read_event_desc(struct feat_fd *ff)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001290{
1291 struct perf_evsel *evsel, *events = NULL;
1292 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001293 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001294 u32 nre, sz, nr, i, j;
Stephane Eranian62db9062012-02-09 23:21:07 +01001295 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001296
1297 /* number of events */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001298 if (do_read_u32(ff, &nre))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001299 goto error;
1300
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001301 if (do_read_u32(ff, &sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001302 goto error;
1303
Stephane Eranian62db9062012-02-09 23:21:07 +01001304 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001305 buf = malloc(sz);
1306 if (!buf)
1307 goto error;
1308
Robert Richter4e1b9c62012-08-16 21:10:22 +02001309 /* the last event terminates with evsel->attr.size == 0: */
1310 events = calloc(nre + 1, sizeof(*events));
1311 if (!events)
1312 goto error;
1313
1314 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001315 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001316 msz = sz;
1317
Robert Richter4e1b9c62012-08-16 21:10:22 +02001318 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1319 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001320
Stephane Eranian62db9062012-02-09 23:21:07 +01001321 /*
1322 * must read entire on-file attr struct to
1323 * sync up with layout.
1324 */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001325 if (__do_read(ff, buf, sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001326 goto error;
1327
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001328 if (ff->ph->needs_swap)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001329 perf_event__attr_swap(buf);
1330
Robert Richter4e1b9c62012-08-16 21:10:22 +02001331 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001332
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001333 if (do_read_u32(ff, &nr))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001334 goto error;
1335
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001336 if (ff->ph->needs_swap)
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001337 evsel->needs_swap = true;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001338
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001339 evsel->name = do_read_string(ff);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001340 if (!evsel->name)
1341 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001342
1343 if (!nr)
1344 continue;
1345
1346 id = calloc(nr, sizeof(*id));
1347 if (!id)
1348 goto error;
1349 evsel->ids = nr;
1350 evsel->id = id;
1351
1352 for (j = 0 ; j < nr; j++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001353 if (do_read_u64(ff, id))
Robert Richter4e1b9c62012-08-16 21:10:22 +02001354 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001355 id++;
1356 }
1357 }
1358out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001359 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001360 return events;
1361error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001362 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001363 events = NULL;
1364 goto out;
1365}
1366
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001367static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001368 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001369{
1370 return fprintf(fp, ", %s = %s", name, val);
1371}
1372
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001373static void print_event_desc(struct feat_fd *ff, FILE *fp)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001374{
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001375 struct perf_evsel *evsel, *events;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001376 u32 j;
1377 u64 *id;
1378
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001379 if (ff->events)
1380 events = ff->events;
1381 else
1382 events = read_event_desc(ff);
1383
Robert Richter4e1b9c62012-08-16 21:10:22 +02001384 if (!events) {
1385 fprintf(fp, "# event desc: not available or unable to read\n");
1386 return;
1387 }
1388
1389 for (evsel = events; evsel->attr.size; evsel++) {
1390 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001391
Robert Richter4e1b9c62012-08-16 21:10:22 +02001392 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001393 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001394 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1395 if (j)
1396 fputc(',', fp);
1397 fprintf(fp, " %"PRIu64, *id);
1398 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001399 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001400 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001401
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001402 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001403
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001404 fputc('\n', fp);
1405 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001406
1407 free_event_desc(events);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001408 ff->events = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001409}
1410
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001411static void print_total_mem(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001412{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001413 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001414}
1415
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001416static void print_numa_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001417{
Jiri Olsac60da222016-07-04 14:16:20 +02001418 int i;
1419 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001420
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001421 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1422 n = &ff->ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001423
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001424 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1425 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001426 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001427
Jiri Olsac60da222016-07-04 14:16:20 +02001428 fprintf(fp, "# node%u cpu list : ", n->node);
1429 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001430 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001431}
1432
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001433static void print_cpuid(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001434{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001435 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001436}
1437
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001438static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001439{
1440 fprintf(fp, "# contains samples with branch stack\n");
1441}
1442
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001443static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
Adrian Hunter4025ea42015-04-09 18:53:41 +03001444{
1445 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1446}
1447
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001448static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001449{
1450 fprintf(fp, "# contains stat data\n");
1451}
1452
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001453static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001454{
1455 int i;
1456
1457 fprintf(fp, "# CPU cache info:\n");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001458 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
Jiri Olsa720e98b2016-02-16 16:01:43 +01001459 fprintf(fp, "# ");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001460 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001461 }
1462}
1463
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001464static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001465{
1466 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001467 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001468 u32 pmu_num;
1469 u32 type;
1470
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001471 pmu_num = ff->ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001472 if (!pmu_num) {
1473 fprintf(fp, "# pmu mappings: not available\n");
1474 return;
1475 }
1476
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001477 str = ff->ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001478
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001479 while (pmu_num) {
1480 type = strtoul(str, &tmp, 0);
1481 if (*tmp != ':')
1482 goto error;
1483
1484 str = tmp + 1;
1485 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1486
Robert Richter50a96672012-08-16 21:10:24 +02001487 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001488 str += strlen(str) + 1;
1489 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001490 }
1491
1492 fprintf(fp, "\n");
1493
1494 if (!pmu_num)
1495 return;
1496error:
1497 fprintf(fp, "# pmu mappings: unable to read\n");
1498}
1499
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001500static void print_group_desc(struct feat_fd *ff, FILE *fp)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001501{
1502 struct perf_session *session;
1503 struct perf_evsel *evsel;
1504 u32 nr = 0;
1505
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001506 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001507
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001508 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001509 if (perf_evsel__is_group_leader(evsel) &&
1510 evsel->nr_members > 1) {
1511 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1512 perf_evsel__name(evsel));
1513
1514 nr = evsel->nr_members - 1;
1515 } else if (nr) {
1516 fprintf(fp, ",%s", perf_evsel__name(evsel));
1517
1518 if (--nr == 0)
1519 fprintf(fp, "}\n");
1520 }
1521 }
1522}
1523
Jin Yao60115182017-12-08 21:13:41 +08001524static void print_sample_time(struct feat_fd *ff, FILE *fp)
1525{
1526 struct perf_session *session;
1527 char time_buf[32];
1528 double d;
1529
1530 session = container_of(ff->ph, struct perf_session, header);
1531
1532 timestamp__scnprintf_usec(session->evlist->first_sample_time,
1533 time_buf, sizeof(time_buf));
1534 fprintf(fp, "# time of first sample : %s\n", time_buf);
1535
1536 timestamp__scnprintf_usec(session->evlist->last_sample_time,
1537 time_buf, sizeof(time_buf));
1538 fprintf(fp, "# time of last sample : %s\n", time_buf);
1539
1540 d = (double)(session->evlist->last_sample_time -
1541 session->evlist->first_sample_time) / NSEC_PER_MSEC;
1542
1543 fprintf(fp, "# sample duration : %10.3f ms\n", d);
1544}
1545
Robert Richter08d95bd2012-02-10 15:41:55 +01001546static int __event_process_build_id(struct build_id_event *bev,
1547 char *filename,
1548 struct perf_session *session)
1549{
1550 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001551 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001552 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001553 struct dso *dso;
1554 enum dso_kernel_type dso_type;
1555
1556 machine = perf_session__findnew_machine(session, bev->pid);
1557 if (!machine)
1558 goto out;
1559
Wang Nan1f121b02015-06-03 08:52:21 +00001560 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001561
Wang Nan1f121b02015-06-03 08:52:21 +00001562 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001563 case PERF_RECORD_MISC_KERNEL:
1564 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001565 break;
1566 case PERF_RECORD_MISC_GUEST_KERNEL:
1567 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001568 break;
1569 case PERF_RECORD_MISC_USER:
1570 case PERF_RECORD_MISC_GUEST_USER:
1571 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001572 break;
1573 default:
1574 goto out;
1575 }
1576
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001577 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001578 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001579 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001580
1581 dso__set_build_id(dso, &bev->build_id);
1582
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001583 if (dso_type != DSO_TYPE_USER) {
1584 struct kmod_path m = { .name = NULL, };
1585
1586 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09001587 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001588 else
1589 dso->kernel = dso_type;
1590
1591 free(m.name);
1592 }
Robert Richter08d95bd2012-02-10 15:41:55 +01001593
1594 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1595 sbuild_id);
1596 pr_debug("build id event received for %s: %s\n",
1597 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001598 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001599 }
1600
1601 err = 0;
1602out:
1603 return err;
1604}
1605
1606static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1607 int input, u64 offset, u64 size)
1608{
1609 struct perf_session *session = container_of(header, struct perf_session, header);
1610 struct {
1611 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001612 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001613 char filename[0];
1614 } old_bev;
1615 struct build_id_event bev;
1616 char filename[PATH_MAX];
1617 u64 limit = offset + size;
1618
1619 while (offset < limit) {
1620 ssize_t len;
1621
Namhyung Kim5323f602012-12-17 15:38:54 +09001622 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001623 return -1;
1624
1625 if (header->needs_swap)
1626 perf_event_header__bswap(&old_bev.header);
1627
1628 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001629 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001630 return -1;
1631
1632 bev.header = old_bev.header;
1633
1634 /*
1635 * As the pid is the missing value, we need to fill
1636 * it properly. The header.misc value give us nice hint.
1637 */
1638 bev.pid = HOST_KERNEL_ID;
1639 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1640 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1641 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1642
1643 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1644 __event_process_build_id(&bev, filename, session);
1645
1646 offset += bev.header.size;
1647 }
1648
1649 return 0;
1650}
1651
1652static int perf_header__read_build_ids(struct perf_header *header,
1653 int input, u64 offset, u64 size)
1654{
1655 struct perf_session *session = container_of(header, struct perf_session, header);
1656 struct build_id_event bev;
1657 char filename[PATH_MAX];
1658 u64 limit = offset + size, orig_offset = offset;
1659 int err = -1;
1660
1661 while (offset < limit) {
1662 ssize_t len;
1663
Namhyung Kim5323f602012-12-17 15:38:54 +09001664 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001665 goto out;
1666
1667 if (header->needs_swap)
1668 perf_event_header__bswap(&bev.header);
1669
1670 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001671 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001672 goto out;
1673 /*
1674 * The a1645ce1 changeset:
1675 *
1676 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1677 *
1678 * Added a field to struct build_id_event that broke the file
1679 * format.
1680 *
1681 * Since the kernel build-id is the first entry, process the
1682 * table using the old format if the well known
1683 * '[kernel.kallsyms]' string for the kernel build-id has the
1684 * first 4 characters chopped off (where the pid_t sits).
1685 */
1686 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1687 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1688 return -1;
1689 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1690 }
1691
1692 __event_process_build_id(&bev, filename, session);
1693
1694 offset += bev.header.size;
1695 }
1696 err = 0;
1697out:
1698 return err;
1699}
1700
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001701/* Macro for features that simply need to read and store a string. */
1702#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001703static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001704{\
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001705 ff->ph->env.__feat_env = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001706 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001707}
1708
1709FEAT_PROCESS_STR_FUN(hostname, hostname);
1710FEAT_PROCESS_STR_FUN(osrelease, os_release);
1711FEAT_PROCESS_STR_FUN(version, version);
1712FEAT_PROCESS_STR_FUN(arch, arch);
1713FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1714FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1715
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001716static int process_tracing_data(struct feat_fd *ff, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001717{
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001718 ssize_t ret = trace_report(ff->fd, data, false);
1719
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001720 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001721}
1722
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001723static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001724{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001725 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
Robert Richterf1c67db2012-02-10 15:41:56 +01001726 pr_debug("Failed to read buildids, continuing...\n");
1727 return 0;
1728}
1729
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001730static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001731{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001732 int ret;
1733 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001734
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001735 ret = do_read_u32(ff, &nr_cpus_avail);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001736 if (ret)
1737 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001738
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001739 ret = do_read_u32(ff, &nr_cpus_online);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001740 if (ret)
1741 return ret;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001742 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
1743 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001744 return 0;
1745}
1746
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001747static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001748{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001749 u64 total_mem;
1750 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001751
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001752 ret = do_read_u64(ff, &total_mem);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001753 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001754 return -1;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001755 ff->ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001756 return 0;
1757}
1758
Robert Richter7c2f7af2012-08-16 21:10:23 +02001759static struct perf_evsel *
1760perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1761{
1762 struct perf_evsel *evsel;
1763
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001764 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001765 if (evsel->idx == idx)
1766 return evsel;
1767 }
1768
1769 return NULL;
1770}
1771
1772static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001773perf_evlist__set_event_name(struct perf_evlist *evlist,
1774 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001775{
1776 struct perf_evsel *evsel;
1777
1778 if (!event->name)
1779 return;
1780
1781 evsel = perf_evlist__find_by_index(evlist, event->idx);
1782 if (!evsel)
1783 return;
1784
1785 if (evsel->name)
1786 return;
1787
1788 evsel->name = strdup(event->name);
1789}
1790
1791static int
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001792process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001793{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001794 struct perf_session *session;
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001795 struct perf_evsel *evsel, *events = read_event_desc(ff);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001796
1797 if (!events)
1798 return 0;
1799
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001800 session = container_of(ff->ph, struct perf_session, header);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001801
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001802 if (session->data->is_pipe) {
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001803 /* Save events for reading later by print_event_desc,
1804 * since they can't be read again in pipe mode. */
1805 ff->events = events;
1806 }
1807
Robert Richter7c2f7af2012-08-16 21:10:23 +02001808 for (evsel = events; evsel->attr.size; evsel++)
1809 perf_evlist__set_event_name(session->evlist, evsel);
1810
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001811 if (!session->data->is_pipe)
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001812 free_event_desc(events);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001813
1814 return 0;
1815}
1816
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001817static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001818{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001819 char *str, *cmdline = NULL, **argv = NULL;
1820 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001821
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001822 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001823 return -1;
1824
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001825 ff->ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001826
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001827 cmdline = zalloc(ff->size + nr + 1);
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001828 if (!cmdline)
1829 return -1;
1830
1831 argv = zalloc(sizeof(char *) * (nr + 1));
1832 if (!argv)
1833 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001834
1835 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001836 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001837 if (!str)
1838 goto error;
1839
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001840 argv[i] = cmdline + len;
1841 memcpy(argv[i], str, strlen(str) + 1);
1842 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001843 free(str);
1844 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001845 ff->ph->env.cmdline = cmdline;
1846 ff->ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001847 return 0;
1848
1849error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001850 free(argv);
1851 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001852 return -1;
1853}
1854
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001855static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001856{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001857 u32 nr, i;
1858 char *str;
1859 struct strbuf sb;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001860 int cpu_nr = ff->ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04001861 u64 size = 0;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001862 struct perf_header *ph = ff->ph;
Kan Liang2bb00d22015-09-01 09:58:12 -04001863
1864 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1865 if (!ph->env.cpu)
1866 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001867
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001868 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001869 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001870
Namhyung Kima1ae5652012-09-24 17:14:59 +09001871 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001872 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001873 if (strbuf_init(&sb, 128) < 0)
1874 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001875
1876 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001877 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001878 if (!str)
1879 goto error;
1880
1881 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001882 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1883 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001884 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001885 free(str);
1886 }
1887 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1888
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001889 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001890 return -1;
1891
Namhyung Kima1ae5652012-09-24 17:14:59 +09001892 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001893 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001894
1895 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001896 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001897 if (!str)
1898 goto error;
1899
1900 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001901 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1902 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001903 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001904 free(str);
1905 }
1906 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04001907
1908 /*
1909 * The header may be from old perf,
1910 * which doesn't include core id and socket id information.
1911 */
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001912 if (ff->size <= size) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001913 zfree(&ph->env.cpu);
1914 return 0;
1915 }
1916
1917 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001918 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001919 goto free_cpu;
1920
Kan Liang2bb00d22015-09-01 09:58:12 -04001921 ph->env.cpu[i].core_id = nr;
1922
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001923 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001924 goto free_cpu;
1925
Jan Stancekda8a58b2017-02-17 12:10:26 +01001926 if (nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001927 pr_debug("socket_id number is too big."
1928 "You may need to upgrade the perf tool.\n");
1929 goto free_cpu;
1930 }
1931
1932 ph->env.cpu[i].socket_id = nr;
1933 }
1934
Namhyung Kima1ae5652012-09-24 17:14:59 +09001935 return 0;
1936
1937error:
1938 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04001939free_cpu:
1940 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001941 return -1;
1942}
1943
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001944static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001945{
Jiri Olsac60da222016-07-04 14:16:20 +02001946 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02001947 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001948 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001949
1950 /* nr nodes */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001951 if (do_read_u32(ff, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001952 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001953
Jiri Olsac60da222016-07-04 14:16:20 +02001954 nodes = zalloc(sizeof(*nodes) * nr);
1955 if (!nodes)
1956 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001957
1958 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02001959 n = &nodes[i];
1960
Namhyung Kima1ae5652012-09-24 17:14:59 +09001961 /* node number */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001962 if (do_read_u32(ff, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001963 goto error;
1964
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001965 if (do_read_u64(ff, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001966 goto error;
1967
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001968 if (do_read_u64(ff, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001969 goto error;
1970
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001971 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001972 if (!str)
1973 goto error;
1974
Jiri Olsac60da222016-07-04 14:16:20 +02001975 n->map = cpu_map__new(str);
1976 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001977 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02001978
Namhyung Kima1ae5652012-09-24 17:14:59 +09001979 free(str);
1980 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001981 ff->ph->env.nr_numa_nodes = nr;
1982 ff->ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001983 return 0;
1984
1985error:
Jiri Olsac60da222016-07-04 14:16:20 +02001986 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001987 return -1;
1988}
1989
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001990static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001991{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001992 char *name;
1993 u32 pmu_num;
1994 u32 type;
1995 struct strbuf sb;
1996
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001997 if (do_read_u32(ff, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001998 return -1;
1999
Namhyung Kima1ae5652012-09-24 17:14:59 +09002000 if (!pmu_num) {
2001 pr_debug("pmu mappings not available\n");
2002 return 0;
2003 }
2004
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002005 ff->ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002006 if (strbuf_init(&sb, 128) < 0)
2007 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002008
2009 while (pmu_num) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002010 if (do_read_u32(ff, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002011 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002012
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002013 name = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002014 if (!name)
2015 goto error;
2016
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002017 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2018 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002019 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002020 if (strbuf_add(&sb, "", 1) < 0)
2021 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002022
Kan Liange0838e02015-09-10 11:03:05 -03002023 if (!strcmp(name, "msr"))
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002024 ff->ph->env.msr_pmu_type = type;
Kan Liange0838e02015-09-10 11:03:05 -03002025
Namhyung Kima1ae5652012-09-24 17:14:59 +09002026 free(name);
2027 pmu_num--;
2028 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002029 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002030 return 0;
2031
2032error:
2033 strbuf_release(&sb);
2034 return -1;
2035}
2036
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002037static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima8bb5592013-01-22 18:09:31 +09002038{
2039 size_t ret = -1;
2040 u32 i, nr, nr_groups;
2041 struct perf_session *session;
2042 struct perf_evsel *evsel, *leader = NULL;
2043 struct group_desc {
2044 char *name;
2045 u32 leader_idx;
2046 u32 nr_members;
2047 } *desc;
2048
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002049 if (do_read_u32(ff, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002050 return -1;
2051
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002052 ff->ph->env.nr_groups = nr_groups;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002053 if (!nr_groups) {
2054 pr_debug("group desc not available\n");
2055 return 0;
2056 }
2057
2058 desc = calloc(nr_groups, sizeof(*desc));
2059 if (!desc)
2060 return -1;
2061
2062 for (i = 0; i < nr_groups; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002063 desc[i].name = do_read_string(ff);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002064 if (!desc[i].name)
2065 goto out_free;
2066
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002067 if (do_read_u32(ff, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002068 goto out_free;
2069
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002070 if (do_read_u32(ff, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002071 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002072 }
2073
2074 /*
2075 * Rebuild group relationship based on the group_desc
2076 */
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002077 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002078 session->evlist->nr_groups = nr_groups;
2079
2080 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002081 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002082 if (evsel->idx == (int) desc[i].leader_idx) {
2083 evsel->leader = evsel;
2084 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002085 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002086 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002087 desc[i].name = NULL;
2088 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09002089 evsel->nr_members = desc[i].nr_members;
2090
2091 if (i >= nr_groups || nr > 0) {
2092 pr_debug("invalid group desc\n");
2093 goto out_free;
2094 }
2095
2096 leader = evsel;
2097 nr = evsel->nr_members - 1;
2098 i++;
2099 } else if (nr) {
2100 /* This is a group member */
2101 evsel->leader = leader;
2102
2103 nr--;
2104 }
2105 }
2106
2107 if (i != nr_groups || nr != 0) {
2108 pr_debug("invalid group desc\n");
2109 goto out_free;
2110 }
2111
2112 ret = 0;
2113out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002114 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002115 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002116 free(desc);
2117
2118 return ret;
2119}
2120
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002121static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
Adrian Hunter99fa2982015-04-30 17:37:25 +03002122{
2123 struct perf_session *session;
2124 int err;
2125
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002126 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002127
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002128 err = auxtrace_index__process(ff->fd, ff->size, session,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002129 ff->ph->needs_swap);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002130 if (err < 0)
2131 pr_err("Failed to process auxtrace index\n");
2132 return err;
2133}
2134
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002135static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01002136{
2137 struct cpu_cache_level *caches;
2138 u32 cnt, i, version;
2139
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002140 if (do_read_u32(ff, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002141 return -1;
2142
Jiri Olsa720e98b2016-02-16 16:01:43 +01002143 if (version != 1)
2144 return -1;
2145
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002146 if (do_read_u32(ff, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002147 return -1;
2148
Jiri Olsa720e98b2016-02-16 16:01:43 +01002149 caches = zalloc(sizeof(*caches) * cnt);
2150 if (!caches)
2151 return -1;
2152
2153 for (i = 0; i < cnt; i++) {
2154 struct cpu_cache_level c;
2155
2156 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002157 if (do_read_u32(ff, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002158 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002159
2160 _R(level)
2161 _R(line_size)
2162 _R(sets)
2163 _R(ways)
2164 #undef _R
2165
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002166 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002167 c.v = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002168 if (!c.v) \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002169 goto out_free_caches;
2170
2171 _R(type)
2172 _R(size)
2173 _R(map)
2174 #undef _R
2175
2176 caches[i] = c;
2177 }
2178
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002179 ff->ph->env.caches = caches;
2180 ff->ph->env.caches_cnt = cnt;
Jiri Olsa720e98b2016-02-16 16:01:43 +01002181 return 0;
2182out_free_caches:
2183 free(caches);
2184 return -1;
2185}
2186
Jin Yao60115182017-12-08 21:13:41 +08002187static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2188{
2189 struct perf_session *session;
2190 u64 first_sample_time, last_sample_time;
2191 int ret;
2192
2193 session = container_of(ff->ph, struct perf_session, header);
2194
2195 ret = do_read_u64(ff, &first_sample_time);
2196 if (ret)
2197 return -1;
2198
2199 ret = do_read_u64(ff, &last_sample_time);
2200 if (ret)
2201 return -1;
2202
2203 session->evlist->first_sample_time = first_sample_time;
2204 session->evlist->last_sample_time = last_sample_time;
2205 return 0;
2206}
2207
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002208struct feature_ops {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002209 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002210 void (*print)(struct feat_fd *ff, FILE *fp);
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002211 int (*process)(struct feat_fd *ff, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002212 const char *name;
2213 bool full_only;
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002214 bool synthesize;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002215};
2216
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002217#define FEAT_OPR(n, func, __full_only) \
2218 [HEADER_##n] = { \
2219 .name = __stringify(n), \
2220 .write = write_##func, \
2221 .print = print_##func, \
2222 .full_only = __full_only, \
2223 .process = process_##func, \
2224 .synthesize = true \
2225 }
2226
2227#define FEAT_OPN(n, func, __full_only) \
2228 [HEADER_##n] = { \
2229 .name = __stringify(n), \
2230 .write = write_##func, \
2231 .print = print_##func, \
2232 .full_only = __full_only, \
2233 .process = process_##func \
2234 }
Robert Richter8cdfa782011-12-07 10:02:56 +01002235
2236/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002237#define print_tracing_data NULL
2238#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002239
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002240#define process_branch_stack NULL
2241#define process_stat NULL
2242
2243
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002244static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002245 FEAT_OPN(TRACING_DATA, tracing_data, false),
2246 FEAT_OPN(BUILD_ID, build_id, false),
2247 FEAT_OPR(HOSTNAME, hostname, false),
2248 FEAT_OPR(OSRELEASE, osrelease, false),
2249 FEAT_OPR(VERSION, version, false),
2250 FEAT_OPR(ARCH, arch, false),
2251 FEAT_OPR(NRCPUS, nrcpus, false),
2252 FEAT_OPR(CPUDESC, cpudesc, false),
2253 FEAT_OPR(CPUID, cpuid, false),
2254 FEAT_OPR(TOTAL_MEM, total_mem, false),
2255 FEAT_OPR(EVENT_DESC, event_desc, false),
2256 FEAT_OPR(CMDLINE, cmdline, false),
2257 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2258 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2259 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2260 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
2261 FEAT_OPN(GROUP_DESC, group_desc, false),
2262 FEAT_OPN(AUXTRACE, auxtrace, false),
2263 FEAT_OPN(STAT, stat, false),
2264 FEAT_OPN(CACHE, cache, true),
Jin Yao60115182017-12-08 21:13:41 +08002265 FEAT_OPR(SAMPLE_TIME, sample_time, false),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002266};
2267
2268struct header_print_data {
2269 FILE *fp;
2270 bool full; /* extended list of headers */
2271};
2272
2273static int perf_file_section__fprintf_info(struct perf_file_section *section,
2274 struct perf_header *ph,
2275 int feat, int fd, void *data)
2276{
2277 struct header_print_data *hd = data;
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002278 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002279
2280 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2281 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2282 "%d, continuing...\n", section->offset, feat);
2283 return 0;
2284 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002285 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002286 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002287 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002288 }
2289 if (!feat_ops[feat].print)
2290 return 0;
2291
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002292 ff = (struct feat_fd) {
2293 .fd = fd,
2294 .ph = ph,
2295 };
2296
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002297 if (!feat_ops[feat].full_only || hd->full)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002298 feat_ops[feat].print(&ff, hd->fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002299 else
2300 fprintf(hd->fp, "# %s info available, use -I to display\n",
2301 feat_ops[feat].name);
2302
2303 return 0;
2304}
2305
2306int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2307{
2308 struct header_print_data hd;
2309 struct perf_header *header = &session->header;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002310 int fd = perf_data__fd(session->data);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002311 struct stat st;
Jiri Olsaaabae162016-10-10 09:35:50 +02002312 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002313
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002314 hd.fp = fp;
2315 hd.full = full;
2316
Jiri Olsaf45f5612016-10-10 09:03:07 +02002317 ret = fstat(fd, &st);
2318 if (ret == -1)
2319 return -1;
2320
2321 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2322
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002323 perf_header__process_sections(header, fd, &hd,
2324 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002325
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002326 if (session->data->is_pipe)
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002327 return 0;
2328
Jiri Olsaaabae162016-10-10 09:35:50 +02002329 fprintf(fp, "# missing features: ");
2330 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2331 if (bit)
2332 fprintf(fp, "%s ", feat_ops[bit].name);
2333 }
2334
2335 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002336 return 0;
2337}
2338
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002339static int do_write_feat(struct feat_fd *ff, int type,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002340 struct perf_file_section **p,
2341 struct perf_evlist *evlist)
2342{
2343 int err;
2344 int ret = 0;
2345
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002346 if (perf_header__has_feat(ff->ph, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002347 if (!feat_ops[type].write)
2348 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002349
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -07002350 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2351 return -1;
2352
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002353 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002354
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002355 err = feat_ops[type].write(ff, evlist);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002356 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002357 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002358
2359 /* undo anything written */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002360 lseek(ff->fd, (*p)->offset, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002361
2362 return -1;
2363 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002364 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002365 (*p)++;
2366 }
2367 return ret;
2368}
2369
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002370static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002371 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002372{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002373 int nr_sections;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002374 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002375 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002376 int sec_size;
2377 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002378 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002379 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002380
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002381 ff = (struct feat_fd){
2382 .fd = fd,
2383 .ph = header,
2384 };
2385
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002386 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002387 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002388 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002389
Paul Gortmaker91b98802013-01-30 20:05:49 -05002390 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002391 if (feat_sec == NULL)
2392 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002393
2394 sec_size = sizeof(*feat_sec) * nr_sections;
2395
Jiri Olsa8d541e92013-07-17 19:49:44 +02002396 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002397 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002398
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002399 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002400 if (do_write_feat(&ff, feat, &p, evlist))
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002401 perf_header__clear_feat(header, feat);
2402 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002403
Xiao Guangrongf887f302010-02-04 16:46:42 +08002404 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002405 /*
2406 * may write more than needed due to dropped feature, but
2407 * this is okay, reader will skip the mising entries
2408 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002409 err = do_write(&ff, feat_sec, sec_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002410 if (err < 0)
2411 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002412 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002413 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002414}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002415
Tom Zanussi8dc58102010-04-01 23:59:15 -05002416int perf_header__write_pipe(int fd)
2417{
2418 struct perf_pipe_file_header f_header;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002419 struct feat_fd ff;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002420 int err;
2421
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002422 ff = (struct feat_fd){ .fd = fd };
2423
Tom Zanussi8dc58102010-04-01 23:59:15 -05002424 f_header = (struct perf_pipe_file_header){
2425 .magic = PERF_MAGIC,
2426 .size = sizeof(f_header),
2427 };
2428
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002429 err = do_write(&ff, &f_header, sizeof(f_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05002430 if (err < 0) {
2431 pr_debug("failed to write perf pipe header\n");
2432 return err;
2433 }
2434
2435 return 0;
2436}
2437
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002438int perf_session__write_header(struct perf_session *session,
2439 struct perf_evlist *evlist,
2440 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002441{
2442 struct perf_file_header f_header;
2443 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002444 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002445 struct perf_evsel *evsel;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002446 struct feat_fd ff;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002447 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002448 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002449
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002450 ff = (struct feat_fd){ .fd = fd};
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002451 lseek(fd, sizeof(f_header), SEEK_SET);
2452
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002453 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002454 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002455 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002456 if (err < 0) {
2457 pr_debug("failed to write perf header\n");
2458 return err;
2459 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002460 }
2461
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002462 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002463
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002464 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002465 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002466 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002467 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002468 .offset = evsel->id_offset,
2469 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002470 }
2471 };
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002472 err = do_write(&ff, &f_attr, sizeof(f_attr));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002473 if (err < 0) {
2474 pr_debug("failed to write perf header attribute\n");
2475 return err;
2476 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002477 }
2478
Adrian Hunterd645c442013-12-11 14:36:28 +02002479 if (!header->data_offset)
2480 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002481 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002482
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002483 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002484 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002485 if (err < 0)
2486 return err;
2487 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002488
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002489 f_header = (struct perf_file_header){
2490 .magic = PERF_MAGIC,
2491 .size = sizeof(f_header),
2492 .attr_size = sizeof(f_attr),
2493 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002494 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002495 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002496 },
2497 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002498 .offset = header->data_offset,
2499 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002500 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002501 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002502 };
2503
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002504 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002505
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002506 lseek(fd, 0, SEEK_SET);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002507 err = do_write(&ff, &f_header, sizeof(f_header));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002508 if (err < 0) {
2509 pr_debug("failed to write perf header\n");
2510 return err;
2511 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002512 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002513
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002514 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002515}
2516
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002517static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002518 int fd, void *buf, size_t size)
2519{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002520 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002521 return -1;
2522
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002523 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002524 mem_bswap_64(buf, size);
2525
2526 return 0;
2527}
2528
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002529int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002530 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002531 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002532 struct perf_header *ph,
2533 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002534{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002535 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002536 int nr_sections;
2537 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002538 int feat;
2539 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002540
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002541 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002542 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002543 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002544
Paul Gortmaker91b98802013-01-30 20:05:49 -05002545 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002546 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002547 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002548
2549 sec_size = sizeof(*feat_sec) * nr_sections;
2550
Jiri Olsa8d541e92013-07-17 19:49:44 +02002551 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002552
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002553 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2554 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002555 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002556
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002557 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2558 err = process(sec++, header, feat, fd, data);
2559 if (err < 0)
2560 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002561 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002562 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002563out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002564 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002565 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002566}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002567
Stephane Eranian114382a2012-02-09 23:21:08 +01002568static const int attr_file_abi_sizes[] = {
2569 [0] = PERF_ATTR_SIZE_VER0,
2570 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002571 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002572 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002573 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002574 0,
2575};
2576
2577/*
2578 * In the legacy file format, the magic number is not used to encode endianness.
2579 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2580 * on ABI revisions, we need to try all combinations for all endianness to
2581 * detect the endianness.
2582 */
2583static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2584{
2585 uint64_t ref_size, attr_size;
2586 int i;
2587
2588 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2589 ref_size = attr_file_abi_sizes[i]
2590 + sizeof(struct perf_file_section);
2591 if (hdr_sz != ref_size) {
2592 attr_size = bswap_64(hdr_sz);
2593 if (attr_size != ref_size)
2594 continue;
2595
2596 ph->needs_swap = true;
2597 }
2598 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2599 i,
2600 ph->needs_swap);
2601 return 0;
2602 }
2603 /* could not determine endianness */
2604 return -1;
2605}
2606
2607#define PERF_PIPE_HDR_VER0 16
2608
2609static const size_t attr_pipe_abi_sizes[] = {
2610 [0] = PERF_PIPE_HDR_VER0,
2611 0,
2612};
2613
2614/*
2615 * In the legacy pipe format, there is an implicit assumption that endiannesss
2616 * between host recording the samples, and host parsing the samples is the
2617 * same. This is not always the case given that the pipe output may always be
2618 * redirected into a file and analyzed on a different machine with possibly a
2619 * different endianness and perf_event ABI revsions in the perf tool itself.
2620 */
2621static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2622{
2623 u64 attr_size;
2624 int i;
2625
2626 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2627 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2628 attr_size = bswap_64(hdr_sz);
2629 if (attr_size != hdr_sz)
2630 continue;
2631
2632 ph->needs_swap = true;
2633 }
2634 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2635 return 0;
2636 }
2637 return -1;
2638}
2639
Feng Tange84ba4e2012-10-30 11:56:07 +08002640bool is_perf_magic(u64 magic)
2641{
2642 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2643 || magic == __perf_magic2
2644 || magic == __perf_magic2_sw)
2645 return true;
2646
2647 return false;
2648}
2649
Stephane Eranian114382a2012-02-09 23:21:08 +01002650static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2651 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002652{
2653 int ret;
2654
2655 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002656 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002657 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002658 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002659 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002660 if (is_pipe)
2661 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002662
Stephane Eranian114382a2012-02-09 23:21:08 +01002663 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002664 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002665 /*
2666 * the new magic number serves two purposes:
2667 * - unique number to identify actual perf.data files
2668 * - encode endianness of file
2669 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002670 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002671
Stephane Eranian114382a2012-02-09 23:21:08 +01002672 /* check magic number with one endianness */
2673 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002674 return 0;
2675
Stephane Eranian114382a2012-02-09 23:21:08 +01002676 /* check magic number with opposite endianness */
2677 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002678 return -1;
2679
2680 ph->needs_swap = true;
2681
2682 return 0;
2683}
2684
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002685int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002686 struct perf_header *ph, int fd)
2687{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002688 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002689
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002690 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002691
Stephane Eranian73323f52012-02-02 13:54:44 +01002692 ret = readn(fd, header, sizeof(*header));
2693 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002694 return -1;
2695
Stephane Eranian114382a2012-02-09 23:21:08 +01002696 if (check_magic_endian(header->magic,
2697 header->attr_size, false, ph) < 0) {
2698 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002699 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002700 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002701
Stephane Eranian73323f52012-02-02 13:54:44 +01002702 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002703 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002704 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002705 }
2706
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002707 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002708 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002709 if (header->size == offsetof(typeof(*header), adds_features))
2710 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002711 else
2712 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002713 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002714 /*
2715 * feature bitmap is declared as an array of unsigned longs --
2716 * not good since its size can differ between the host that
2717 * generated the data file and the host analyzing the file.
2718 *
2719 * We need to handle endianness, but we don't know the size of
2720 * the unsigned long where the file was generated. Take a best
2721 * guess at determining it: try 64-bit swap first (ie., file
2722 * created on a 64-bit host), and check if the hostname feature
2723 * bit is set (this feature bit is forced on as of fbe96f2).
2724 * If the bit is not, undo the 64-bit swap and try a 32-bit
2725 * swap. If the hostname bit is still not set (e.g., older data
2726 * file), punt and fallback to the original behavior --
2727 * clearing all feature bits and setting buildid.
2728 */
David Ahern80c01202012-06-08 11:47:51 -03002729 mem_bswap_64(&header->adds_features,
2730 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002731
2732 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002733 /* unswap as u64 */
2734 mem_bswap_64(&header->adds_features,
2735 BITS_TO_U64(HEADER_FEAT_BITS));
2736
2737 /* unswap as u32 */
2738 mem_bswap_32(&header->adds_features,
2739 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002740 }
2741
2742 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2743 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2744 set_bit(HEADER_BUILD_ID, header->adds_features);
2745 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002746 }
2747
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002748 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002749 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002750
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002751 ph->data_offset = header->data.offset;
2752 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002753 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002754 return 0;
2755}
2756
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002757static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002758 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002759 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002760{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002761 struct feat_fd fdd = {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002762 .fd = fd,
2763 .ph = ph,
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002764 .size = section->size,
2765 .offset = section->offset,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002766 };
2767
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002768 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002769 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002770 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002771 return 0;
2772 }
2773
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002774 if (feat >= HEADER_LAST_FEATURE) {
2775 pr_debug("unknown feature %d, continuing...\n", feat);
2776 return 0;
2777 }
2778
Robert Richterf1c67db2012-02-10 15:41:56 +01002779 if (!feat_ops[feat].process)
2780 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002781
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002782 return feat_ops[feat].process(&fdd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002783}
2784
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002785static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002786 struct perf_header *ph, int fd,
2787 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002788{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002789 struct feat_fd ff = {
2790 .fd = STDOUT_FILENO,
2791 .ph = ph,
2792 };
Jiri Olsa727ebd52013-11-28 11:30:14 +01002793 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002794
2795 ret = readn(fd, header, sizeof(*header));
2796 if (ret <= 0)
2797 return -1;
2798
Stephane Eranian114382a2012-02-09 23:21:08 +01002799 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2800 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002801 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002802 }
2803
2804 if (ph->needs_swap)
2805 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002806
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002807 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002808 return -1;
2809
Tom Zanussi8dc58102010-04-01 23:59:15 -05002810 return 0;
2811}
2812
Jiri Olsad4339562013-07-17 19:49:41 +02002813static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002814{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002815 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002816 struct perf_pipe_file_header f_header;
2817
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002818 if (perf_file_header__read_pipe(&f_header, header,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002819 perf_data__fd(session->data),
Tom Zanussi454c4072010-05-01 01:41:20 -05002820 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002821 pr_debug("incompatible file format\n");
2822 return -EINVAL;
2823 }
2824
Tom Zanussi8dc58102010-04-01 23:59:15 -05002825 return 0;
2826}
2827
Stephane Eranian69996df2012-02-09 23:21:06 +01002828static int read_attr(int fd, struct perf_header *ph,
2829 struct perf_file_attr *f_attr)
2830{
2831 struct perf_event_attr *attr = &f_attr->attr;
2832 size_t sz, left;
2833 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002834 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002835
2836 memset(f_attr, 0, sizeof(*f_attr));
2837
2838 /* read minimal guaranteed structure */
2839 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2840 if (ret <= 0) {
2841 pr_debug("cannot read %d bytes of header attr\n",
2842 PERF_ATTR_SIZE_VER0);
2843 return -1;
2844 }
2845
2846 /* on file perf_event_attr size */
2847 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002848
Stephane Eranian69996df2012-02-09 23:21:06 +01002849 if (ph->needs_swap)
2850 sz = bswap_32(sz);
2851
2852 if (sz == 0) {
2853 /* assume ABI0 */
2854 sz = PERF_ATTR_SIZE_VER0;
2855 } else if (sz > our_sz) {
2856 pr_debug("file uses a more recent and unsupported ABI"
2857 " (%zu bytes extra)\n", sz - our_sz);
2858 return -1;
2859 }
2860 /* what we have not yet read and that we know about */
2861 left = sz - PERF_ATTR_SIZE_VER0;
2862 if (left) {
2863 void *ptr = attr;
2864 ptr += PERF_ATTR_SIZE_VER0;
2865
2866 ret = readn(fd, ptr, left);
2867 }
2868 /* read perf_file_section, ids are read in caller */
2869 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2870
2871 return ret <= 0 ? -1 : 0;
2872}
2873
Namhyung Kim831394b2012-09-06 11:10:46 +09002874static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2875 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002876{
Namhyung Kim831394b2012-09-06 11:10:46 +09002877 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002878 char bf[128];
2879
Namhyung Kim831394b2012-09-06 11:10:46 +09002880 /* already prepared */
2881 if (evsel->tp_format)
2882 return 0;
2883
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002884 if (pevent == NULL) {
2885 pr_debug("broken or missing trace data\n");
2886 return -1;
2887 }
2888
Namhyung Kim831394b2012-09-06 11:10:46 +09002889 event = pevent_find_event(pevent, evsel->attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09002890 if (event == NULL) {
2891 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002892 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09002893 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002894
Namhyung Kim831394b2012-09-06 11:10:46 +09002895 if (!evsel->name) {
2896 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2897 evsel->name = strdup(bf);
2898 if (evsel->name == NULL)
2899 return -1;
2900 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002901
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002902 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002903 return 0;
2904}
2905
Namhyung Kim831394b2012-09-06 11:10:46 +09002906static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2907 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002908{
2909 struct perf_evsel *pos;
2910
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002911 evlist__for_each_entry(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002912 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2913 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002914 return -1;
2915 }
2916
2917 return 0;
2918}
2919
Jiri Olsad4339562013-07-17 19:49:41 +02002920int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002921{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002922 struct perf_data *data = session->data;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002923 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002924 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002925 struct perf_file_attr f_attr;
2926 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002927 int nr_attrs, nr_ids, i, j;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002928 int fd = perf_data__fd(data);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002929
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002930 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002931 if (session->evlist == NULL)
2932 return -ENOMEM;
2933
Kan Liang2c071442015-08-28 05:48:05 -04002934 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03002935 session->machines.host.env = &header->env;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002936 if (perf_data__is_pipe(data))
Jiri Olsad4339562013-07-17 19:49:41 +02002937 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002938
Stephane Eranian69996df2012-02-09 23:21:06 +01002939 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002940 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002941
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002942 /*
2943 * Sanity check that perf.data was written cleanly; data size is
2944 * initialized to 0 and updated only if the on_exit function is run.
2945 * If data size is still 0 then the file contains only partial
2946 * information. Just warn user and process it as much as it can.
2947 */
2948 if (f_header.data.size == 0) {
2949 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2950 "Was the 'perf record' command properly terminated?\n",
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002951 data->file.path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002952 }
2953
Stephane Eranian69996df2012-02-09 23:21:06 +01002954 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002955 lseek(fd, f_header.attrs.offset, SEEK_SET);
2956
2957 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002958 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002959 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002960
Stephane Eranian69996df2012-02-09 23:21:06 +01002961 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002962 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002963
David Ahern1060ab82015-04-09 16:15:46 -04002964 if (header->needs_swap) {
2965 f_attr.ids.size = bswap_64(f_attr.ids.size);
2966 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002967 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002968 }
David Aherneda39132011-07-15 12:34:09 -06002969
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002970 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002971 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002972
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002973 if (evsel == NULL)
2974 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002975
2976 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002977 /*
2978 * Do it before so that if perf_evsel__alloc_id fails, this
2979 * entry gets purged too at perf_evlist__delete().
2980 */
2981 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002982
2983 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002984 /*
2985 * We don't have the cpu and thread maps on the header, so
2986 * for allocating the perf_sample_id table we fake 1 cpu and
2987 * hattr->ids threads.
2988 */
2989 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2990 goto out_delete_evlist;
2991
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002992 lseek(fd, f_attr.ids.offset, SEEK_SET);
2993
2994 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002995 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002996 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002997
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002998 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002999 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02003000
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003001 lseek(fd, tmp, SEEK_SET);
3002 }
3003
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02003004 symbol_conf.nr_events = nr_attrs;
3005
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003006 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003007 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01003008
Namhyung Kim831394b2012-09-06 11:10:46 +09003009 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003010 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003011 goto out_delete_evlist;
3012
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003013 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003014out_errno:
3015 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003016
3017out_delete_evlist:
3018 perf_evlist__delete(session->evlist);
3019 session->evlist = NULL;
3020 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003021}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02003022
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003023int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02003024 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003025 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02003026{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003027 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003028 size_t size;
3029 int err;
3030
3031 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003032 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003033 size += sizeof(struct perf_event_header);
3034 size += ids * sizeof(u64);
3035
3036 ev = malloc(size);
3037
Chris Samuelce47dc52010-11-13 13:35:06 +11003038 if (ev == NULL)
3039 return -ENOMEM;
3040
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003041 ev->attr.attr = *attr;
3042 memcpy(ev->attr.id, id, ids * sizeof(u64));
3043
3044 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02003045 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003046
Robert Richterf4d83432012-08-16 21:10:17 +02003047 if (ev->attr.header.size == size)
3048 err = process(tool, ev, NULL, NULL);
3049 else
3050 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003051
3052 free(ev);
3053
3054 return err;
3055}
3056
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003057int perf_event__synthesize_features(struct perf_tool *tool,
3058 struct perf_session *session,
3059 struct perf_evlist *evlist,
3060 perf_event__handler_t process)
3061{
3062 struct perf_header *header = &session->header;
3063 struct feat_fd ff;
3064 struct feature_event *fe;
3065 size_t sz, sz_hdr;
3066 int feat, ret;
3067
3068 sz_hdr = sizeof(fe->header);
3069 sz = sizeof(union perf_event);
3070 /* get a nice alignment */
3071 sz = PERF_ALIGN(sz, page_size);
3072
3073 memset(&ff, 0, sizeof(ff));
3074
3075 ff.buf = malloc(sz);
3076 if (!ff.buf)
3077 return -ENOMEM;
3078
3079 ff.size = sz - sz_hdr;
3080
3081 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3082 if (!feat_ops[feat].synthesize) {
3083 pr_debug("No record header feature for header :%d\n", feat);
3084 continue;
3085 }
3086
3087 ff.offset = sizeof(*fe);
3088
3089 ret = feat_ops[feat].write(&ff, evlist);
3090 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3091 pr_debug("Error writing feature\n");
3092 continue;
3093 }
3094 /* ff.buf may have changed due to realloc in do_write() */
3095 fe = ff.buf;
3096 memset(fe, 0, sizeof(*fe));
3097
3098 fe->feat_id = feat;
3099 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3100 fe->header.size = ff.offset;
3101
3102 ret = process(tool, ff.buf, NULL, NULL);
3103 if (ret) {
3104 free(ff.buf);
3105 return ret;
3106 }
3107 }
3108 free(ff.buf);
3109 return 0;
3110}
3111
3112int perf_event__process_feature(struct perf_tool *tool,
3113 union perf_event *event,
3114 struct perf_session *session __maybe_unused)
3115{
3116 struct feat_fd ff = { .fd = 0 };
3117 struct feature_event *fe = (struct feature_event *)event;
3118 int type = fe->header.type;
3119 u64 feat = fe->feat_id;
3120
3121 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3122 pr_warning("invalid record type %d in pipe-mode\n", type);
3123 return 0;
3124 }
3125 if (feat == HEADER_RESERVED || feat > HEADER_LAST_FEATURE) {
3126 pr_warning("invalid record type %d in pipe-mode\n", type);
3127 return -1;
3128 }
3129
3130 if (!feat_ops[feat].process)
3131 return 0;
3132
3133 ff.buf = (void *)fe->data;
3134 ff.size = event->header.size - sizeof(event->header);
3135 ff.ph = &session->header;
3136
3137 if (feat_ops[feat].process(&ff, NULL))
3138 return -1;
3139
3140 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3141 return 0;
3142
3143 if (!feat_ops[feat].full_only ||
3144 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3145 feat_ops[feat].print(&ff, stdout);
3146 } else {
3147 fprintf(stdout, "# %s info available, use -I to display\n",
3148 feat_ops[feat].name);
3149 }
3150
3151 return 0;
3152}
3153
Jiri Olsaa6e52812015-10-25 15:51:37 +01003154static struct event_update_event *
3155event_update_event__new(size_t size, u64 type, u64 id)
3156{
3157 struct event_update_event *ev;
3158
3159 size += sizeof(*ev);
3160 size = PERF_ALIGN(size, sizeof(u64));
3161
3162 ev = zalloc(size);
3163 if (ev) {
3164 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3165 ev->header.size = (u16)size;
3166 ev->type = type;
3167 ev->id = id;
3168 }
3169 return ev;
3170}
3171
3172int
3173perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3174 struct perf_evsel *evsel,
3175 perf_event__handler_t process)
3176{
3177 struct event_update_event *ev;
3178 size_t size = strlen(evsel->unit);
3179 int err;
3180
3181 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3182 if (ev == NULL)
3183 return -ENOMEM;
3184
3185 strncpy(ev->data, evsel->unit, size);
3186 err = process(tool, (union perf_event *)ev, NULL, NULL);
3187 free(ev);
3188 return err;
3189}
3190
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003191int
3192perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3193 struct perf_evsel *evsel,
3194 perf_event__handler_t process)
3195{
3196 struct event_update_event *ev;
3197 struct event_update_event_scale *ev_data;
3198 int err;
3199
3200 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3201 if (ev == NULL)
3202 return -ENOMEM;
3203
3204 ev_data = (struct event_update_event_scale *) ev->data;
3205 ev_data->scale = evsel->scale;
3206 err = process(tool, (union perf_event*) ev, NULL, NULL);
3207 free(ev);
3208 return err;
3209}
3210
Jiri Olsa802c9042015-10-25 15:51:39 +01003211int
3212perf_event__synthesize_event_update_name(struct perf_tool *tool,
3213 struct perf_evsel *evsel,
3214 perf_event__handler_t process)
3215{
3216 struct event_update_event *ev;
3217 size_t len = strlen(evsel->name);
3218 int err;
3219
3220 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3221 if (ev == NULL)
3222 return -ENOMEM;
3223
3224 strncpy(ev->data, evsel->name, len);
3225 err = process(tool, (union perf_event*) ev, NULL, NULL);
3226 free(ev);
3227 return err;
3228}
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003229
Jiri Olsa86ebb092015-10-25 15:51:40 +01003230int
3231perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3232 struct perf_evsel *evsel,
3233 perf_event__handler_t process)
3234{
3235 size_t size = sizeof(struct event_update_event);
3236 struct event_update_event *ev;
3237 int max, err;
3238 u16 type;
3239
3240 if (!evsel->own_cpus)
3241 return 0;
3242
3243 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3244 if (!ev)
3245 return -ENOMEM;
3246
3247 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3248 ev->header.size = (u16)size;
3249 ev->type = PERF_EVENT_UPDATE__CPUS;
3250 ev->id = evsel->id[0];
3251
3252 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3253 evsel->own_cpus,
3254 type, max);
3255
3256 err = process(tool, (union perf_event*) ev, NULL, NULL);
3257 free(ev);
3258 return err;
3259}
3260
Jiri Olsac853f932015-10-25 15:51:41 +01003261size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3262{
3263 struct event_update_event *ev = &event->event_update;
3264 struct event_update_event_scale *ev_scale;
3265 struct event_update_event_cpus *ev_cpus;
3266 struct cpu_map *map;
3267 size_t ret;
3268
3269 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3270
3271 switch (ev->type) {
3272 case PERF_EVENT_UPDATE__SCALE:
3273 ev_scale = (struct event_update_event_scale *) ev->data;
3274 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3275 break;
3276 case PERF_EVENT_UPDATE__UNIT:
3277 ret += fprintf(fp, "... unit: %s\n", ev->data);
3278 break;
3279 case PERF_EVENT_UPDATE__NAME:
3280 ret += fprintf(fp, "... name: %s\n", ev->data);
3281 break;
3282 case PERF_EVENT_UPDATE__CPUS:
3283 ev_cpus = (struct event_update_event_cpus *) ev->data;
3284 ret += fprintf(fp, "... ");
3285
3286 map = cpu_map__new_data(&ev_cpus->cpus);
3287 if (map)
3288 ret += cpu_map__fprintf(map, fp);
3289 else
3290 ret += fprintf(fp, "failed to get cpus\n");
3291 break;
3292 default:
3293 ret += fprintf(fp, "... unknown type\n");
3294 break;
3295 }
3296
3297 return ret;
3298}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003299
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003300int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003301 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003302 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003303{
Robert Richter6606f872012-08-16 21:10:19 +02003304 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003305 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003306
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003307 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003308 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3309 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003310 if (err) {
3311 pr_debug("failed to create perf header attribute\n");
3312 return err;
3313 }
3314 }
3315
3316 return err;
3317}
3318
Andi Kleenbfd8f722017-11-17 13:42:58 -08003319static bool has_unit(struct perf_evsel *counter)
3320{
3321 return counter->unit && *counter->unit;
3322}
3323
3324static bool has_scale(struct perf_evsel *counter)
3325{
3326 return counter->scale != 1;
3327}
3328
3329int perf_event__synthesize_extra_attr(struct perf_tool *tool,
3330 struct perf_evlist *evsel_list,
3331 perf_event__handler_t process,
3332 bool is_pipe)
3333{
3334 struct perf_evsel *counter;
3335 int err;
3336
3337 /*
3338 * Synthesize other events stuff not carried within
3339 * attr event - unit, scale, name
3340 */
3341 evlist__for_each_entry(evsel_list, counter) {
3342 if (!counter->supported)
3343 continue;
3344
3345 /*
3346 * Synthesize unit and scale only if it's defined.
3347 */
3348 if (has_unit(counter)) {
3349 err = perf_event__synthesize_event_update_unit(tool, counter, process);
3350 if (err < 0) {
3351 pr_err("Couldn't synthesize evsel unit.\n");
3352 return err;
3353 }
3354 }
3355
3356 if (has_scale(counter)) {
3357 err = perf_event__synthesize_event_update_scale(tool, counter, process);
3358 if (err < 0) {
3359 pr_err("Couldn't synthesize evsel counter.\n");
3360 return err;
3361 }
3362 }
3363
3364 if (counter->own_cpus) {
3365 err = perf_event__synthesize_event_update_cpus(tool, counter, process);
3366 if (err < 0) {
3367 pr_err("Couldn't synthesize evsel cpus.\n");
3368 return err;
3369 }
3370 }
3371
3372 /*
3373 * Name is needed only for pipe output,
3374 * perf.data carries event names.
3375 */
3376 if (is_pipe) {
3377 err = perf_event__synthesize_event_update_name(tool, counter, process);
3378 if (err < 0) {
3379 pr_err("Couldn't synthesize evsel name.\n");
3380 return err;
3381 }
3382 }
3383 }
3384 return 0;
3385}
3386
Adrian Hunter47c3d102013-07-04 16:20:21 +03003387int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3388 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003389 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003390{
Robert Richterf4d83432012-08-16 21:10:17 +02003391 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003392 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003393 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003394
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003395 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003396 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003397 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003398 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003399 }
3400
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003401 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003402 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003403 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003404
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003405 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003406
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003407 ids = event->header.size;
3408 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003409 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003410 /*
3411 * We don't have the cpu and thread maps on the header, so
3412 * for allocating the perf_sample_id table we fake 1 cpu and
3413 * hattr->ids threads.
3414 */
3415 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3416 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003417
3418 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003419 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003420 }
3421
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03003422 symbol_conf.nr_events = evlist->nr_entries;
3423
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003424 return 0;
3425}
Tom Zanussicd19a032010-04-01 23:59:20 -05003426
Jiri Olsaffe777252015-10-25 15:51:36 +01003427int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3428 union perf_event *event,
3429 struct perf_evlist **pevlist)
3430{
3431 struct event_update_event *ev = &event->event_update;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003432 struct event_update_event_scale *ev_scale;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003433 struct event_update_event_cpus *ev_cpus;
Jiri Olsaffe777252015-10-25 15:51:36 +01003434 struct perf_evlist *evlist;
3435 struct perf_evsel *evsel;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003436 struct cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003437
3438 if (!pevlist || *pevlist == NULL)
3439 return -EINVAL;
3440
3441 evlist = *pevlist;
3442
3443 evsel = perf_evlist__id2evsel(evlist, ev->id);
3444 if (evsel == NULL)
3445 return -EINVAL;
3446
Jiri Olsaa6e52812015-10-25 15:51:37 +01003447 switch (ev->type) {
3448 case PERF_EVENT_UPDATE__UNIT:
3449 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003450 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003451 case PERF_EVENT_UPDATE__NAME:
3452 evsel->name = strdup(ev->data);
3453 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003454 case PERF_EVENT_UPDATE__SCALE:
3455 ev_scale = (struct event_update_event_scale *) ev->data;
3456 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003457 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003458 case PERF_EVENT_UPDATE__CPUS:
3459 ev_cpus = (struct event_update_event_cpus *) ev->data;
3460
3461 map = cpu_map__new_data(&ev_cpus->cpus);
3462 if (map)
3463 evsel->own_cpus = map;
3464 else
3465 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003466 default:
3467 break;
3468 }
3469
Jiri Olsaffe777252015-10-25 15:51:36 +01003470 return 0;
3471}
3472
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003473int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003474 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003475 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003476{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003477 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003478 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003479 ssize_t size = 0, aligned_size = 0, padding;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003480 struct feat_fd ff;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003481 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003482
Jiri Olsa29208e52011-10-20 15:59:43 +02003483 /*
3484 * We are going to store the size of the data followed
3485 * by the data contents. Since the fd descriptor is a pipe,
3486 * we cannot seek back to store the size of the data once
3487 * we know it. Instead we:
3488 *
3489 * - write the tracing data to the temp file
3490 * - get/write the data size to pipe
3491 * - write the tracing data from the temp file
3492 * to the pipe
3493 */
3494 tdata = tracing_data_get(&evlist->entries, fd, true);
3495 if (!tdata)
3496 return -1;
3497
Tom Zanussi92155452010-04-01 23:59:21 -05003498 memset(&ev, 0, sizeof(ev));
3499
3500 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003501 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003502 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003503 padding = aligned_size - size;
3504 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3505 ev.tracing_data.size = aligned_size;
3506
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003507 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003508
Jiri Olsa29208e52011-10-20 15:59:43 +02003509 /*
3510 * The put function will copy all the tracing data
3511 * stored in temp file to the pipe.
3512 */
3513 tracing_data_put(tdata);
3514
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003515 ff = (struct feat_fd){ .fd = fd };
3516 if (write_padded(&ff, NULL, 0, padding))
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -07003517 return -1;
Tom Zanussi92155452010-04-01 23:59:21 -05003518
3519 return aligned_size;
3520}
3521
Adrian Hunter47c3d102013-07-04 16:20:21 +03003522int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3523 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003524 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05003525{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003526 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003527 int fd = perf_data__fd(session->data);
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003528 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003529 char buf[BUFSIZ];
3530
3531 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003532 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003533 SEEK_SET);
3534
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003535 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003536 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003537 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003538
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003539 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003540 pr_err("%s: reading input file", __func__);
3541 return -1;
3542 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003543 if (session->repipe) {
3544 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003545 if (retw <= 0 || retw != padding) {
3546 pr_err("%s: repiping tracing data padding", __func__);
3547 return -1;
3548 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003549 }
Tom Zanussi92155452010-04-01 23:59:21 -05003550
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003551 if (size_read + padding != size) {
3552 pr_err("%s: tracing data size mismatch", __func__);
3553 return -1;
3554 }
Tom Zanussi92155452010-04-01 23:59:21 -05003555
Namhyung Kim831394b2012-09-06 11:10:46 +09003556 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003557 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003558
Tom Zanussi92155452010-04-01 23:59:21 -05003559 return size_read + padding;
3560}
Tom Zanussic7929e42010-04-01 23:59:22 -05003561
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003562int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003563 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003564 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003565 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003566{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003567 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003568 size_t len;
3569 int err = 0;
3570
3571 if (!pos->hit)
3572 return err;
3573
3574 memset(&ev, 0, sizeof(ev));
3575
3576 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003577 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003578 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3579 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3580 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003581 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003582 ev.build_id.header.size = sizeof(ev.build_id) + len;
3583 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3584
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003585 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003586
3587 return err;
3588}
3589
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003590int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003591 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003592 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003593{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003594 __event_process_build_id(&event->build_id,
3595 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003596 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003597 return 0;
3598}