blob: 5890e08e075439182c0471a65fa20994a19446a2 [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>
18#include <sys/types.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020019#include <sys/utsname.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030020#include <unistd.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020021
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020022#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030023#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020024#include "header.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030025#include "memswap.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020026#include "../perf.h"
27#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020028#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010029#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010030#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020031#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020032#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020033#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090034#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020035#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020036#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010037#include <api/fs/fs.h>
38#include "asm/bug.h"
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -070039#include "tool.h"
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
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001185static void print_hostname(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001186{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001187 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001188}
1189
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001190static void print_osrelease(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001191{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001192 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001193}
1194
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001195static void print_arch(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001196{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001197 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001198}
1199
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001200static void print_cpudesc(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001201{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001202 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001203}
1204
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001205static void print_nrcpus(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001206{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001207 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1208 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001209}
1210
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001211static void print_version(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001212{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001213 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001214}
1215
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001216static void print_cmdline(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001217{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001218 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001219
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001220 nr = ff->ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001221
1222 fprintf(fp, "# cmdline : ");
1223
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001224 for (i = 0; i < nr; i++)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001225 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001226 fputc('\n', fp);
1227}
1228
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001229static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001230{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001231 struct perf_header *ph = ff->ph;
1232 int cpu_nr = ph->env.nr_cpus_avail;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001233 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001234 char *str;
1235
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001236 nr = ph->env.nr_sibling_cores;
1237 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001238
1239 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001240 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001241 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001242 }
1243
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001244 nr = ph->env.nr_sibling_threads;
1245 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001246
1247 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001248 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001249 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001250 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001251
1252 if (ph->env.cpu != NULL) {
1253 for (i = 0; i < cpu_nr; i++)
1254 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1255 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1256 } else
1257 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001258}
1259
Robert Richter4e1b9c62012-08-16 21:10:22 +02001260static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001261{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001262 struct perf_evsel *evsel;
1263
1264 if (!events)
1265 return;
1266
1267 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001268 zfree(&evsel->name);
1269 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001270 }
1271
1272 free(events);
1273}
1274
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001275static struct perf_evsel *read_event_desc(struct feat_fd *ff)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001276{
1277 struct perf_evsel *evsel, *events = NULL;
1278 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001279 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001280 u32 nre, sz, nr, i, j;
Stephane Eranian62db9062012-02-09 23:21:07 +01001281 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001282
1283 /* number of events */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001284 if (do_read_u32(ff, &nre))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001285 goto error;
1286
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001287 if (do_read_u32(ff, &sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001288 goto error;
1289
Stephane Eranian62db9062012-02-09 23:21:07 +01001290 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001291 buf = malloc(sz);
1292 if (!buf)
1293 goto error;
1294
Robert Richter4e1b9c62012-08-16 21:10:22 +02001295 /* the last event terminates with evsel->attr.size == 0: */
1296 events = calloc(nre + 1, sizeof(*events));
1297 if (!events)
1298 goto error;
1299
1300 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001301 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001302 msz = sz;
1303
Robert Richter4e1b9c62012-08-16 21:10:22 +02001304 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1305 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001306
Stephane Eranian62db9062012-02-09 23:21:07 +01001307 /*
1308 * must read entire on-file attr struct to
1309 * sync up with layout.
1310 */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001311 if (__do_read(ff, buf, sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001312 goto error;
1313
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001314 if (ff->ph->needs_swap)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001315 perf_event__attr_swap(buf);
1316
Robert Richter4e1b9c62012-08-16 21:10:22 +02001317 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001318
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001319 if (do_read_u32(ff, &nr))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001320 goto error;
1321
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001322 if (ff->ph->needs_swap)
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001323 evsel->needs_swap = true;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001324
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001325 evsel->name = do_read_string(ff);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001326 if (!evsel->name)
1327 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001328
1329 if (!nr)
1330 continue;
1331
1332 id = calloc(nr, sizeof(*id));
1333 if (!id)
1334 goto error;
1335 evsel->ids = nr;
1336 evsel->id = id;
1337
1338 for (j = 0 ; j < nr; j++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001339 if (do_read_u64(ff, id))
Robert Richter4e1b9c62012-08-16 21:10:22 +02001340 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001341 id++;
1342 }
1343 }
1344out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001345 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001346 return events;
1347error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001348 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001349 events = NULL;
1350 goto out;
1351}
1352
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001353static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001354 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001355{
1356 return fprintf(fp, ", %s = %s", name, val);
1357}
1358
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001359static void print_event_desc(struct feat_fd *ff, FILE *fp)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001360{
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001361 struct perf_evsel *evsel, *events;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001362 u32 j;
1363 u64 *id;
1364
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001365 if (ff->events)
1366 events = ff->events;
1367 else
1368 events = read_event_desc(ff);
1369
Robert Richter4e1b9c62012-08-16 21:10:22 +02001370 if (!events) {
1371 fprintf(fp, "# event desc: not available or unable to read\n");
1372 return;
1373 }
1374
1375 for (evsel = events; evsel->attr.size; evsel++) {
1376 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001377
Robert Richter4e1b9c62012-08-16 21:10:22 +02001378 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001379 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001380 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1381 if (j)
1382 fputc(',', fp);
1383 fprintf(fp, " %"PRIu64, *id);
1384 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001385 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001386 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001387
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001388 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001389
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001390 fputc('\n', fp);
1391 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001392
1393 free_event_desc(events);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001394 ff->events = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001395}
1396
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001397static void print_total_mem(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001398{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001399 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001400}
1401
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001402static void print_numa_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001403{
Jiri Olsac60da222016-07-04 14:16:20 +02001404 int i;
1405 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001406
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001407 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1408 n = &ff->ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001409
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001410 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1411 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001412 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001413
Jiri Olsac60da222016-07-04 14:16:20 +02001414 fprintf(fp, "# node%u cpu list : ", n->node);
1415 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001416 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001417}
1418
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001419static void print_cpuid(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001420{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001421 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001422}
1423
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001424static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001425{
1426 fprintf(fp, "# contains samples with branch stack\n");
1427}
1428
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001429static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
Adrian Hunter4025ea42015-04-09 18:53:41 +03001430{
1431 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1432}
1433
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001434static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001435{
1436 fprintf(fp, "# contains stat data\n");
1437}
1438
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001439static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001440{
1441 int i;
1442
1443 fprintf(fp, "# CPU cache info:\n");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001444 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
Jiri Olsa720e98b2016-02-16 16:01:43 +01001445 fprintf(fp, "# ");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001446 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001447 }
1448}
1449
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001450static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001451{
1452 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001453 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001454 u32 pmu_num;
1455 u32 type;
1456
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001457 pmu_num = ff->ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001458 if (!pmu_num) {
1459 fprintf(fp, "# pmu mappings: not available\n");
1460 return;
1461 }
1462
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001463 str = ff->ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001464
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001465 while (pmu_num) {
1466 type = strtoul(str, &tmp, 0);
1467 if (*tmp != ':')
1468 goto error;
1469
1470 str = tmp + 1;
1471 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1472
Robert Richter50a96672012-08-16 21:10:24 +02001473 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001474 str += strlen(str) + 1;
1475 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001476 }
1477
1478 fprintf(fp, "\n");
1479
1480 if (!pmu_num)
1481 return;
1482error:
1483 fprintf(fp, "# pmu mappings: unable to read\n");
1484}
1485
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001486static void print_group_desc(struct feat_fd *ff, FILE *fp)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001487{
1488 struct perf_session *session;
1489 struct perf_evsel *evsel;
1490 u32 nr = 0;
1491
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001492 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001493
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001494 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001495 if (perf_evsel__is_group_leader(evsel) &&
1496 evsel->nr_members > 1) {
1497 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1498 perf_evsel__name(evsel));
1499
1500 nr = evsel->nr_members - 1;
1501 } else if (nr) {
1502 fprintf(fp, ",%s", perf_evsel__name(evsel));
1503
1504 if (--nr == 0)
1505 fprintf(fp, "}\n");
1506 }
1507 }
1508}
1509
Robert Richter08d95bd2012-02-10 15:41:55 +01001510static int __event_process_build_id(struct build_id_event *bev,
1511 char *filename,
1512 struct perf_session *session)
1513{
1514 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001515 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001516 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001517 struct dso *dso;
1518 enum dso_kernel_type dso_type;
1519
1520 machine = perf_session__findnew_machine(session, bev->pid);
1521 if (!machine)
1522 goto out;
1523
Wang Nan1f121b02015-06-03 08:52:21 +00001524 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001525
Wang Nan1f121b02015-06-03 08:52:21 +00001526 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001527 case PERF_RECORD_MISC_KERNEL:
1528 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001529 break;
1530 case PERF_RECORD_MISC_GUEST_KERNEL:
1531 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001532 break;
1533 case PERF_RECORD_MISC_USER:
1534 case PERF_RECORD_MISC_GUEST_USER:
1535 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001536 break;
1537 default:
1538 goto out;
1539 }
1540
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001541 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001542 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001543 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001544
1545 dso__set_build_id(dso, &bev->build_id);
1546
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001547 if (dso_type != DSO_TYPE_USER) {
1548 struct kmod_path m = { .name = NULL, };
1549
1550 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09001551 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001552 else
1553 dso->kernel = dso_type;
1554
1555 free(m.name);
1556 }
Robert Richter08d95bd2012-02-10 15:41:55 +01001557
1558 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1559 sbuild_id);
1560 pr_debug("build id event received for %s: %s\n",
1561 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001562 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001563 }
1564
1565 err = 0;
1566out:
1567 return err;
1568}
1569
1570static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1571 int input, u64 offset, u64 size)
1572{
1573 struct perf_session *session = container_of(header, struct perf_session, header);
1574 struct {
1575 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001576 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001577 char filename[0];
1578 } old_bev;
1579 struct build_id_event bev;
1580 char filename[PATH_MAX];
1581 u64 limit = offset + size;
1582
1583 while (offset < limit) {
1584 ssize_t len;
1585
Namhyung Kim5323f602012-12-17 15:38:54 +09001586 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001587 return -1;
1588
1589 if (header->needs_swap)
1590 perf_event_header__bswap(&old_bev.header);
1591
1592 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001593 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001594 return -1;
1595
1596 bev.header = old_bev.header;
1597
1598 /*
1599 * As the pid is the missing value, we need to fill
1600 * it properly. The header.misc value give us nice hint.
1601 */
1602 bev.pid = HOST_KERNEL_ID;
1603 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1604 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1605 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1606
1607 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1608 __event_process_build_id(&bev, filename, session);
1609
1610 offset += bev.header.size;
1611 }
1612
1613 return 0;
1614}
1615
1616static int perf_header__read_build_ids(struct perf_header *header,
1617 int input, u64 offset, u64 size)
1618{
1619 struct perf_session *session = container_of(header, struct perf_session, header);
1620 struct build_id_event bev;
1621 char filename[PATH_MAX];
1622 u64 limit = offset + size, orig_offset = offset;
1623 int err = -1;
1624
1625 while (offset < limit) {
1626 ssize_t len;
1627
Namhyung Kim5323f602012-12-17 15:38:54 +09001628 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001629 goto out;
1630
1631 if (header->needs_swap)
1632 perf_event_header__bswap(&bev.header);
1633
1634 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001635 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001636 goto out;
1637 /*
1638 * The a1645ce1 changeset:
1639 *
1640 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1641 *
1642 * Added a field to struct build_id_event that broke the file
1643 * format.
1644 *
1645 * Since the kernel build-id is the first entry, process the
1646 * table using the old format if the well known
1647 * '[kernel.kallsyms]' string for the kernel build-id has the
1648 * first 4 characters chopped off (where the pid_t sits).
1649 */
1650 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1651 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1652 return -1;
1653 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1654 }
1655
1656 __event_process_build_id(&bev, filename, session);
1657
1658 offset += bev.header.size;
1659 }
1660 err = 0;
1661out:
1662 return err;
1663}
1664
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001665/* Macro for features that simply need to read and store a string. */
1666#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001667static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001668{\
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001669 ff->ph->env.__feat_env = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001670 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001671}
1672
1673FEAT_PROCESS_STR_FUN(hostname, hostname);
1674FEAT_PROCESS_STR_FUN(osrelease, os_release);
1675FEAT_PROCESS_STR_FUN(version, version);
1676FEAT_PROCESS_STR_FUN(arch, arch);
1677FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1678FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1679
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001680static int process_tracing_data(struct feat_fd *ff, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001681{
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001682 ssize_t ret = trace_report(ff->fd, data, false);
1683
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001684 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001685}
1686
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001687static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001688{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001689 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
Robert Richterf1c67db2012-02-10 15:41:56 +01001690 pr_debug("Failed to read buildids, continuing...\n");
1691 return 0;
1692}
1693
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001694static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001695{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001696 int ret;
1697 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001698
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001699 ret = do_read_u32(ff, &nr_cpus_avail);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001700 if (ret)
1701 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001702
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001703 ret = do_read_u32(ff, &nr_cpus_online);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001704 if (ret)
1705 return ret;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001706 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
1707 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001708 return 0;
1709}
1710
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001711static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001712{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001713 u64 total_mem;
1714 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001715
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001716 ret = do_read_u64(ff, &total_mem);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001717 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001718 return -1;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001719 ff->ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001720 return 0;
1721}
1722
Robert Richter7c2f7af2012-08-16 21:10:23 +02001723static struct perf_evsel *
1724perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1725{
1726 struct perf_evsel *evsel;
1727
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001728 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001729 if (evsel->idx == idx)
1730 return evsel;
1731 }
1732
1733 return NULL;
1734}
1735
1736static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001737perf_evlist__set_event_name(struct perf_evlist *evlist,
1738 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001739{
1740 struct perf_evsel *evsel;
1741
1742 if (!event->name)
1743 return;
1744
1745 evsel = perf_evlist__find_by_index(evlist, event->idx);
1746 if (!evsel)
1747 return;
1748
1749 if (evsel->name)
1750 return;
1751
1752 evsel->name = strdup(event->name);
1753}
1754
1755static int
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001756process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001757{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001758 struct perf_session *session;
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001759 struct perf_evsel *evsel, *events = read_event_desc(ff);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001760
1761 if (!events)
1762 return 0;
1763
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001764 session = container_of(ff->ph, struct perf_session, header);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001765
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001766 if (session->data->is_pipe) {
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001767 /* Save events for reading later by print_event_desc,
1768 * since they can't be read again in pipe mode. */
1769 ff->events = events;
1770 }
1771
Robert Richter7c2f7af2012-08-16 21:10:23 +02001772 for (evsel = events; evsel->attr.size; evsel++)
1773 perf_evlist__set_event_name(session->evlist, evsel);
1774
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001775 if (!session->data->is_pipe)
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001776 free_event_desc(events);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001777
1778 return 0;
1779}
1780
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001781static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001782{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001783 char *str, *cmdline = NULL, **argv = NULL;
1784 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001785
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001786 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001787 return -1;
1788
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001789 ff->ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001790
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001791 cmdline = zalloc(ff->size + nr + 1);
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001792 if (!cmdline)
1793 return -1;
1794
1795 argv = zalloc(sizeof(char *) * (nr + 1));
1796 if (!argv)
1797 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001798
1799 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001800 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001801 if (!str)
1802 goto error;
1803
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001804 argv[i] = cmdline + len;
1805 memcpy(argv[i], str, strlen(str) + 1);
1806 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001807 free(str);
1808 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001809 ff->ph->env.cmdline = cmdline;
1810 ff->ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001811 return 0;
1812
1813error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001814 free(argv);
1815 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001816 return -1;
1817}
1818
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001819static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001820{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001821 u32 nr, i;
1822 char *str;
1823 struct strbuf sb;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001824 int cpu_nr = ff->ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04001825 u64 size = 0;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001826 struct perf_header *ph = ff->ph;
Kan Liang2bb00d22015-09-01 09:58:12 -04001827
1828 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1829 if (!ph->env.cpu)
1830 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001831
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001832 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001833 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001834
Namhyung Kima1ae5652012-09-24 17:14:59 +09001835 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001836 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001837 if (strbuf_init(&sb, 128) < 0)
1838 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001839
1840 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001841 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001842 if (!str)
1843 goto error;
1844
1845 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001846 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1847 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001848 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001849 free(str);
1850 }
1851 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1852
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001853 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001854 return -1;
1855
Namhyung Kima1ae5652012-09-24 17:14:59 +09001856 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001857 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001858
1859 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001860 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001861 if (!str)
1862 goto error;
1863
1864 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001865 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1866 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001867 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001868 free(str);
1869 }
1870 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04001871
1872 /*
1873 * The header may be from old perf,
1874 * which doesn't include core id and socket id information.
1875 */
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001876 if (ff->size <= size) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001877 zfree(&ph->env.cpu);
1878 return 0;
1879 }
1880
1881 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001882 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001883 goto free_cpu;
1884
Kan Liang2bb00d22015-09-01 09:58:12 -04001885 ph->env.cpu[i].core_id = nr;
1886
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001887 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001888 goto free_cpu;
1889
Jan Stancekda8a58b2017-02-17 12:10:26 +01001890 if (nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001891 pr_debug("socket_id number is too big."
1892 "You may need to upgrade the perf tool.\n");
1893 goto free_cpu;
1894 }
1895
1896 ph->env.cpu[i].socket_id = nr;
1897 }
1898
Namhyung Kima1ae5652012-09-24 17:14:59 +09001899 return 0;
1900
1901error:
1902 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04001903free_cpu:
1904 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001905 return -1;
1906}
1907
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001908static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001909{
Jiri Olsac60da222016-07-04 14:16:20 +02001910 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02001911 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001912 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001913
1914 /* nr nodes */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001915 if (do_read_u32(ff, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001916 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001917
Jiri Olsac60da222016-07-04 14:16:20 +02001918 nodes = zalloc(sizeof(*nodes) * nr);
1919 if (!nodes)
1920 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001921
1922 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02001923 n = &nodes[i];
1924
Namhyung Kima1ae5652012-09-24 17:14:59 +09001925 /* node number */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001926 if (do_read_u32(ff, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001927 goto error;
1928
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001929 if (do_read_u64(ff, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001930 goto error;
1931
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001932 if (do_read_u64(ff, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001933 goto error;
1934
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001935 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001936 if (!str)
1937 goto error;
1938
Jiri Olsac60da222016-07-04 14:16:20 +02001939 n->map = cpu_map__new(str);
1940 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001941 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02001942
Namhyung Kima1ae5652012-09-24 17:14:59 +09001943 free(str);
1944 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001945 ff->ph->env.nr_numa_nodes = nr;
1946 ff->ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001947 return 0;
1948
1949error:
Jiri Olsac60da222016-07-04 14:16:20 +02001950 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001951 return -1;
1952}
1953
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001954static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001955{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001956 char *name;
1957 u32 pmu_num;
1958 u32 type;
1959 struct strbuf sb;
1960
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001961 if (do_read_u32(ff, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001962 return -1;
1963
Namhyung Kima1ae5652012-09-24 17:14:59 +09001964 if (!pmu_num) {
1965 pr_debug("pmu mappings not available\n");
1966 return 0;
1967 }
1968
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001969 ff->ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001970 if (strbuf_init(&sb, 128) < 0)
1971 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001972
1973 while (pmu_num) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001974 if (do_read_u32(ff, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001975 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001976
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001977 name = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001978 if (!name)
1979 goto error;
1980
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001981 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
1982 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001983 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001984 if (strbuf_add(&sb, "", 1) < 0)
1985 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001986
Kan Liange0838e02015-09-10 11:03:05 -03001987 if (!strcmp(name, "msr"))
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001988 ff->ph->env.msr_pmu_type = type;
Kan Liange0838e02015-09-10 11:03:05 -03001989
Namhyung Kima1ae5652012-09-24 17:14:59 +09001990 free(name);
1991 pmu_num--;
1992 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001993 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001994 return 0;
1995
1996error:
1997 strbuf_release(&sb);
1998 return -1;
1999}
2000
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002001static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima8bb5592013-01-22 18:09:31 +09002002{
2003 size_t ret = -1;
2004 u32 i, nr, nr_groups;
2005 struct perf_session *session;
2006 struct perf_evsel *evsel, *leader = NULL;
2007 struct group_desc {
2008 char *name;
2009 u32 leader_idx;
2010 u32 nr_members;
2011 } *desc;
2012
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002013 if (do_read_u32(ff, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002014 return -1;
2015
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002016 ff->ph->env.nr_groups = nr_groups;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002017 if (!nr_groups) {
2018 pr_debug("group desc not available\n");
2019 return 0;
2020 }
2021
2022 desc = calloc(nr_groups, sizeof(*desc));
2023 if (!desc)
2024 return -1;
2025
2026 for (i = 0; i < nr_groups; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002027 desc[i].name = do_read_string(ff);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002028 if (!desc[i].name)
2029 goto out_free;
2030
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002031 if (do_read_u32(ff, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002032 goto out_free;
2033
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002034 if (do_read_u32(ff, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002035 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002036 }
2037
2038 /*
2039 * Rebuild group relationship based on the group_desc
2040 */
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002041 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002042 session->evlist->nr_groups = nr_groups;
2043
2044 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002045 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002046 if (evsel->idx == (int) desc[i].leader_idx) {
2047 evsel->leader = evsel;
2048 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002049 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002050 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002051 desc[i].name = NULL;
2052 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09002053 evsel->nr_members = desc[i].nr_members;
2054
2055 if (i >= nr_groups || nr > 0) {
2056 pr_debug("invalid group desc\n");
2057 goto out_free;
2058 }
2059
2060 leader = evsel;
2061 nr = evsel->nr_members - 1;
2062 i++;
2063 } else if (nr) {
2064 /* This is a group member */
2065 evsel->leader = leader;
2066
2067 nr--;
2068 }
2069 }
2070
2071 if (i != nr_groups || nr != 0) {
2072 pr_debug("invalid group desc\n");
2073 goto out_free;
2074 }
2075
2076 ret = 0;
2077out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002078 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002079 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002080 free(desc);
2081
2082 return ret;
2083}
2084
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002085static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
Adrian Hunter99fa2982015-04-30 17:37:25 +03002086{
2087 struct perf_session *session;
2088 int err;
2089
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002090 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002091
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002092 err = auxtrace_index__process(ff->fd, ff->size, session,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002093 ff->ph->needs_swap);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002094 if (err < 0)
2095 pr_err("Failed to process auxtrace index\n");
2096 return err;
2097}
2098
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002099static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01002100{
2101 struct cpu_cache_level *caches;
2102 u32 cnt, i, version;
2103
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002104 if (do_read_u32(ff, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002105 return -1;
2106
Jiri Olsa720e98b2016-02-16 16:01:43 +01002107 if (version != 1)
2108 return -1;
2109
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002110 if (do_read_u32(ff, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002111 return -1;
2112
Jiri Olsa720e98b2016-02-16 16:01:43 +01002113 caches = zalloc(sizeof(*caches) * cnt);
2114 if (!caches)
2115 return -1;
2116
2117 for (i = 0; i < cnt; i++) {
2118 struct cpu_cache_level c;
2119
2120 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002121 if (do_read_u32(ff, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002122 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002123
2124 _R(level)
2125 _R(line_size)
2126 _R(sets)
2127 _R(ways)
2128 #undef _R
2129
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002130 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002131 c.v = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002132 if (!c.v) \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002133 goto out_free_caches;
2134
2135 _R(type)
2136 _R(size)
2137 _R(map)
2138 #undef _R
2139
2140 caches[i] = c;
2141 }
2142
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002143 ff->ph->env.caches = caches;
2144 ff->ph->env.caches_cnt = cnt;
Jiri Olsa720e98b2016-02-16 16:01:43 +01002145 return 0;
2146out_free_caches:
2147 free(caches);
2148 return -1;
2149}
2150
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002151struct feature_ops {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002152 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002153 void (*print)(struct feat_fd *ff, FILE *fp);
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002154 int (*process)(struct feat_fd *ff, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002155 const char *name;
2156 bool full_only;
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002157 bool synthesize;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002158};
2159
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002160#define FEAT_OPR(n, func, __full_only) \
2161 [HEADER_##n] = { \
2162 .name = __stringify(n), \
2163 .write = write_##func, \
2164 .print = print_##func, \
2165 .full_only = __full_only, \
2166 .process = process_##func, \
2167 .synthesize = true \
2168 }
2169
2170#define FEAT_OPN(n, func, __full_only) \
2171 [HEADER_##n] = { \
2172 .name = __stringify(n), \
2173 .write = write_##func, \
2174 .print = print_##func, \
2175 .full_only = __full_only, \
2176 .process = process_##func \
2177 }
Robert Richter8cdfa782011-12-07 10:02:56 +01002178
2179/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002180#define print_tracing_data NULL
2181#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002182
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002183#define process_branch_stack NULL
2184#define process_stat NULL
2185
2186
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002187static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002188 FEAT_OPN(TRACING_DATA, tracing_data, false),
2189 FEAT_OPN(BUILD_ID, build_id, false),
2190 FEAT_OPR(HOSTNAME, hostname, false),
2191 FEAT_OPR(OSRELEASE, osrelease, false),
2192 FEAT_OPR(VERSION, version, false),
2193 FEAT_OPR(ARCH, arch, false),
2194 FEAT_OPR(NRCPUS, nrcpus, false),
2195 FEAT_OPR(CPUDESC, cpudesc, false),
2196 FEAT_OPR(CPUID, cpuid, false),
2197 FEAT_OPR(TOTAL_MEM, total_mem, false),
2198 FEAT_OPR(EVENT_DESC, event_desc, false),
2199 FEAT_OPR(CMDLINE, cmdline, false),
2200 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2201 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2202 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2203 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
2204 FEAT_OPN(GROUP_DESC, group_desc, false),
2205 FEAT_OPN(AUXTRACE, auxtrace, false),
2206 FEAT_OPN(STAT, stat, false),
2207 FEAT_OPN(CACHE, cache, true),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002208};
2209
2210struct header_print_data {
2211 FILE *fp;
2212 bool full; /* extended list of headers */
2213};
2214
2215static int perf_file_section__fprintf_info(struct perf_file_section *section,
2216 struct perf_header *ph,
2217 int feat, int fd, void *data)
2218{
2219 struct header_print_data *hd = data;
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002220 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002221
2222 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2223 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2224 "%d, continuing...\n", section->offset, feat);
2225 return 0;
2226 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002227 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002228 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002229 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002230 }
2231 if (!feat_ops[feat].print)
2232 return 0;
2233
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002234 ff = (struct feat_fd) {
2235 .fd = fd,
2236 .ph = ph,
2237 };
2238
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002239 if (!feat_ops[feat].full_only || hd->full)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002240 feat_ops[feat].print(&ff, hd->fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002241 else
2242 fprintf(hd->fp, "# %s info available, use -I to display\n",
2243 feat_ops[feat].name);
2244
2245 return 0;
2246}
2247
2248int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2249{
2250 struct header_print_data hd;
2251 struct perf_header *header = &session->header;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002252 int fd = perf_data__fd(session->data);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002253 struct stat st;
Jiri Olsaaabae162016-10-10 09:35:50 +02002254 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002255
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002256 hd.fp = fp;
2257 hd.full = full;
2258
Jiri Olsaf45f5612016-10-10 09:03:07 +02002259 ret = fstat(fd, &st);
2260 if (ret == -1)
2261 return -1;
2262
2263 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2264
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002265 perf_header__process_sections(header, fd, &hd,
2266 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002267
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002268 if (session->data->is_pipe)
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002269 return 0;
2270
Jiri Olsaaabae162016-10-10 09:35:50 +02002271 fprintf(fp, "# missing features: ");
2272 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2273 if (bit)
2274 fprintf(fp, "%s ", feat_ops[bit].name);
2275 }
2276
2277 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002278 return 0;
2279}
2280
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002281static int do_write_feat(struct feat_fd *ff, int type,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002282 struct perf_file_section **p,
2283 struct perf_evlist *evlist)
2284{
2285 int err;
2286 int ret = 0;
2287
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002288 if (perf_header__has_feat(ff->ph, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002289 if (!feat_ops[type].write)
2290 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002291
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -07002292 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2293 return -1;
2294
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002295 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002296
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002297 err = feat_ops[type].write(ff, evlist);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002298 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002299 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002300
2301 /* undo anything written */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002302 lseek(ff->fd, (*p)->offset, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002303
2304 return -1;
2305 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002306 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002307 (*p)++;
2308 }
2309 return ret;
2310}
2311
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002312static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002313 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002314{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002315 int nr_sections;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002316 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002317 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002318 int sec_size;
2319 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002320 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002321 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002322
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002323 ff = (struct feat_fd){
2324 .fd = fd,
2325 .ph = header,
2326 };
2327
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002328 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002329 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002330 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002331
Paul Gortmaker91b98802013-01-30 20:05:49 -05002332 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002333 if (feat_sec == NULL)
2334 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002335
2336 sec_size = sizeof(*feat_sec) * nr_sections;
2337
Jiri Olsa8d541e92013-07-17 19:49:44 +02002338 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002339 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002340
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002341 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002342 if (do_write_feat(&ff, feat, &p, evlist))
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002343 perf_header__clear_feat(header, feat);
2344 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002345
Xiao Guangrongf887f302010-02-04 16:46:42 +08002346 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002347 /*
2348 * may write more than needed due to dropped feature, but
2349 * this is okay, reader will skip the mising entries
2350 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002351 err = do_write(&ff, feat_sec, sec_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002352 if (err < 0)
2353 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002354 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002355 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002356}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002357
Tom Zanussi8dc58102010-04-01 23:59:15 -05002358int perf_header__write_pipe(int fd)
2359{
2360 struct perf_pipe_file_header f_header;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002361 struct feat_fd ff;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002362 int err;
2363
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002364 ff = (struct feat_fd){ .fd = fd };
2365
Tom Zanussi8dc58102010-04-01 23:59:15 -05002366 f_header = (struct perf_pipe_file_header){
2367 .magic = PERF_MAGIC,
2368 .size = sizeof(f_header),
2369 };
2370
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002371 err = do_write(&ff, &f_header, sizeof(f_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05002372 if (err < 0) {
2373 pr_debug("failed to write perf pipe header\n");
2374 return err;
2375 }
2376
2377 return 0;
2378}
2379
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002380int perf_session__write_header(struct perf_session *session,
2381 struct perf_evlist *evlist,
2382 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002383{
2384 struct perf_file_header f_header;
2385 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002386 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002387 struct perf_evsel *evsel;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002388 struct feat_fd ff;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002389 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002390 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002391
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002392 ff = (struct feat_fd){ .fd = fd};
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002393 lseek(fd, sizeof(f_header), SEEK_SET);
2394
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002395 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002396 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002397 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002398 if (err < 0) {
2399 pr_debug("failed to write perf header\n");
2400 return err;
2401 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002402 }
2403
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002404 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002405
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002406 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002407 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002408 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002409 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002410 .offset = evsel->id_offset,
2411 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002412 }
2413 };
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002414 err = do_write(&ff, &f_attr, sizeof(f_attr));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002415 if (err < 0) {
2416 pr_debug("failed to write perf header attribute\n");
2417 return err;
2418 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002419 }
2420
Adrian Hunterd645c442013-12-11 14:36:28 +02002421 if (!header->data_offset)
2422 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002423 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002424
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002425 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002426 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002427 if (err < 0)
2428 return err;
2429 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002430
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002431 f_header = (struct perf_file_header){
2432 .magic = PERF_MAGIC,
2433 .size = sizeof(f_header),
2434 .attr_size = sizeof(f_attr),
2435 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002436 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002437 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002438 },
2439 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002440 .offset = header->data_offset,
2441 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002442 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002443 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002444 };
2445
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002446 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002447
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002448 lseek(fd, 0, SEEK_SET);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002449 err = do_write(&ff, &f_header, sizeof(f_header));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002450 if (err < 0) {
2451 pr_debug("failed to write perf header\n");
2452 return err;
2453 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002454 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002455
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002456 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002457}
2458
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002459static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002460 int fd, void *buf, size_t size)
2461{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002462 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002463 return -1;
2464
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002465 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002466 mem_bswap_64(buf, size);
2467
2468 return 0;
2469}
2470
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002471int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002472 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002473 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002474 struct perf_header *ph,
2475 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002476{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002477 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002478 int nr_sections;
2479 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002480 int feat;
2481 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002482
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002483 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002484 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002485 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002486
Paul Gortmaker91b98802013-01-30 20:05:49 -05002487 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002488 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002489 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002490
2491 sec_size = sizeof(*feat_sec) * nr_sections;
2492
Jiri Olsa8d541e92013-07-17 19:49:44 +02002493 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002494
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002495 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2496 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002497 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002498
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002499 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2500 err = process(sec++, header, feat, fd, data);
2501 if (err < 0)
2502 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002503 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002504 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002505out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002506 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002507 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002508}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002509
Stephane Eranian114382a2012-02-09 23:21:08 +01002510static const int attr_file_abi_sizes[] = {
2511 [0] = PERF_ATTR_SIZE_VER0,
2512 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002513 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002514 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002515 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002516 0,
2517};
2518
2519/*
2520 * In the legacy file format, the magic number is not used to encode endianness.
2521 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2522 * on ABI revisions, we need to try all combinations for all endianness to
2523 * detect the endianness.
2524 */
2525static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2526{
2527 uint64_t ref_size, attr_size;
2528 int i;
2529
2530 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2531 ref_size = attr_file_abi_sizes[i]
2532 + sizeof(struct perf_file_section);
2533 if (hdr_sz != ref_size) {
2534 attr_size = bswap_64(hdr_sz);
2535 if (attr_size != ref_size)
2536 continue;
2537
2538 ph->needs_swap = true;
2539 }
2540 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2541 i,
2542 ph->needs_swap);
2543 return 0;
2544 }
2545 /* could not determine endianness */
2546 return -1;
2547}
2548
2549#define PERF_PIPE_HDR_VER0 16
2550
2551static const size_t attr_pipe_abi_sizes[] = {
2552 [0] = PERF_PIPE_HDR_VER0,
2553 0,
2554};
2555
2556/*
2557 * In the legacy pipe format, there is an implicit assumption that endiannesss
2558 * between host recording the samples, and host parsing the samples is the
2559 * same. This is not always the case given that the pipe output may always be
2560 * redirected into a file and analyzed on a different machine with possibly a
2561 * different endianness and perf_event ABI revsions in the perf tool itself.
2562 */
2563static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2564{
2565 u64 attr_size;
2566 int i;
2567
2568 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2569 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2570 attr_size = bswap_64(hdr_sz);
2571 if (attr_size != hdr_sz)
2572 continue;
2573
2574 ph->needs_swap = true;
2575 }
2576 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2577 return 0;
2578 }
2579 return -1;
2580}
2581
Feng Tange84ba4e2012-10-30 11:56:07 +08002582bool is_perf_magic(u64 magic)
2583{
2584 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2585 || magic == __perf_magic2
2586 || magic == __perf_magic2_sw)
2587 return true;
2588
2589 return false;
2590}
2591
Stephane Eranian114382a2012-02-09 23:21:08 +01002592static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2593 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002594{
2595 int ret;
2596
2597 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002598 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002599 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002600 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002601 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002602 if (is_pipe)
2603 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002604
Stephane Eranian114382a2012-02-09 23:21:08 +01002605 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002606 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002607 /*
2608 * the new magic number serves two purposes:
2609 * - unique number to identify actual perf.data files
2610 * - encode endianness of file
2611 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002612 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002613
Stephane Eranian114382a2012-02-09 23:21:08 +01002614 /* check magic number with one endianness */
2615 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002616 return 0;
2617
Stephane Eranian114382a2012-02-09 23:21:08 +01002618 /* check magic number with opposite endianness */
2619 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002620 return -1;
2621
2622 ph->needs_swap = true;
2623
2624 return 0;
2625}
2626
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002627int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002628 struct perf_header *ph, int fd)
2629{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002630 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002631
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002632 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002633
Stephane Eranian73323f52012-02-02 13:54:44 +01002634 ret = readn(fd, header, sizeof(*header));
2635 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002636 return -1;
2637
Stephane Eranian114382a2012-02-09 23:21:08 +01002638 if (check_magic_endian(header->magic,
2639 header->attr_size, false, ph) < 0) {
2640 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002641 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002642 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002643
Stephane Eranian73323f52012-02-02 13:54:44 +01002644 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002645 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002646 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002647 }
2648
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002649 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002650 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002651 if (header->size == offsetof(typeof(*header), adds_features))
2652 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002653 else
2654 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002655 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002656 /*
2657 * feature bitmap is declared as an array of unsigned longs --
2658 * not good since its size can differ between the host that
2659 * generated the data file and the host analyzing the file.
2660 *
2661 * We need to handle endianness, but we don't know the size of
2662 * the unsigned long where the file was generated. Take a best
2663 * guess at determining it: try 64-bit swap first (ie., file
2664 * created on a 64-bit host), and check if the hostname feature
2665 * bit is set (this feature bit is forced on as of fbe96f2).
2666 * If the bit is not, undo the 64-bit swap and try a 32-bit
2667 * swap. If the hostname bit is still not set (e.g., older data
2668 * file), punt and fallback to the original behavior --
2669 * clearing all feature bits and setting buildid.
2670 */
David Ahern80c01202012-06-08 11:47:51 -03002671 mem_bswap_64(&header->adds_features,
2672 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002673
2674 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002675 /* unswap as u64 */
2676 mem_bswap_64(&header->adds_features,
2677 BITS_TO_U64(HEADER_FEAT_BITS));
2678
2679 /* unswap as u32 */
2680 mem_bswap_32(&header->adds_features,
2681 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002682 }
2683
2684 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2685 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2686 set_bit(HEADER_BUILD_ID, header->adds_features);
2687 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002688 }
2689
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002690 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002691 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002692
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002693 ph->data_offset = header->data.offset;
2694 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002695 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002696 return 0;
2697}
2698
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002699static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002700 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002701 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002702{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002703 struct feat_fd fdd = {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002704 .fd = fd,
2705 .ph = ph,
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002706 .size = section->size,
2707 .offset = section->offset,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002708 };
2709
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002710 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002711 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002712 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002713 return 0;
2714 }
2715
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002716 if (feat >= HEADER_LAST_FEATURE) {
2717 pr_debug("unknown feature %d, continuing...\n", feat);
2718 return 0;
2719 }
2720
Robert Richterf1c67db2012-02-10 15:41:56 +01002721 if (!feat_ops[feat].process)
2722 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002723
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002724 return feat_ops[feat].process(&fdd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002725}
2726
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002727static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002728 struct perf_header *ph, int fd,
2729 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002730{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002731 struct feat_fd ff = {
2732 .fd = STDOUT_FILENO,
2733 .ph = ph,
2734 };
Jiri Olsa727ebd52013-11-28 11:30:14 +01002735 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002736
2737 ret = readn(fd, header, sizeof(*header));
2738 if (ret <= 0)
2739 return -1;
2740
Stephane Eranian114382a2012-02-09 23:21:08 +01002741 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2742 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002743 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002744 }
2745
2746 if (ph->needs_swap)
2747 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002748
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002749 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002750 return -1;
2751
Tom Zanussi8dc58102010-04-01 23:59:15 -05002752 return 0;
2753}
2754
Jiri Olsad4339562013-07-17 19:49:41 +02002755static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002756{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002757 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002758 struct perf_pipe_file_header f_header;
2759
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002760 if (perf_file_header__read_pipe(&f_header, header,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002761 perf_data__fd(session->data),
Tom Zanussi454c4072010-05-01 01:41:20 -05002762 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002763 pr_debug("incompatible file format\n");
2764 return -EINVAL;
2765 }
2766
Tom Zanussi8dc58102010-04-01 23:59:15 -05002767 return 0;
2768}
2769
Stephane Eranian69996df2012-02-09 23:21:06 +01002770static int read_attr(int fd, struct perf_header *ph,
2771 struct perf_file_attr *f_attr)
2772{
2773 struct perf_event_attr *attr = &f_attr->attr;
2774 size_t sz, left;
2775 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002776 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002777
2778 memset(f_attr, 0, sizeof(*f_attr));
2779
2780 /* read minimal guaranteed structure */
2781 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2782 if (ret <= 0) {
2783 pr_debug("cannot read %d bytes of header attr\n",
2784 PERF_ATTR_SIZE_VER0);
2785 return -1;
2786 }
2787
2788 /* on file perf_event_attr size */
2789 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002790
Stephane Eranian69996df2012-02-09 23:21:06 +01002791 if (ph->needs_swap)
2792 sz = bswap_32(sz);
2793
2794 if (sz == 0) {
2795 /* assume ABI0 */
2796 sz = PERF_ATTR_SIZE_VER0;
2797 } else if (sz > our_sz) {
2798 pr_debug("file uses a more recent and unsupported ABI"
2799 " (%zu bytes extra)\n", sz - our_sz);
2800 return -1;
2801 }
2802 /* what we have not yet read and that we know about */
2803 left = sz - PERF_ATTR_SIZE_VER0;
2804 if (left) {
2805 void *ptr = attr;
2806 ptr += PERF_ATTR_SIZE_VER0;
2807
2808 ret = readn(fd, ptr, left);
2809 }
2810 /* read perf_file_section, ids are read in caller */
2811 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2812
2813 return ret <= 0 ? -1 : 0;
2814}
2815
Namhyung Kim831394b2012-09-06 11:10:46 +09002816static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2817 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002818{
Namhyung Kim831394b2012-09-06 11:10:46 +09002819 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002820 char bf[128];
2821
Namhyung Kim831394b2012-09-06 11:10:46 +09002822 /* already prepared */
2823 if (evsel->tp_format)
2824 return 0;
2825
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002826 if (pevent == NULL) {
2827 pr_debug("broken or missing trace data\n");
2828 return -1;
2829 }
2830
Namhyung Kim831394b2012-09-06 11:10:46 +09002831 event = pevent_find_event(pevent, evsel->attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09002832 if (event == NULL) {
2833 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002834 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09002835 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002836
Namhyung Kim831394b2012-09-06 11:10:46 +09002837 if (!evsel->name) {
2838 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2839 evsel->name = strdup(bf);
2840 if (evsel->name == NULL)
2841 return -1;
2842 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002843
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002844 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002845 return 0;
2846}
2847
Namhyung Kim831394b2012-09-06 11:10:46 +09002848static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2849 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002850{
2851 struct perf_evsel *pos;
2852
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002853 evlist__for_each_entry(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002854 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2855 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002856 return -1;
2857 }
2858
2859 return 0;
2860}
2861
Jiri Olsad4339562013-07-17 19:49:41 +02002862int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002863{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002864 struct perf_data *data = session->data;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002865 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002866 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002867 struct perf_file_attr f_attr;
2868 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002869 int nr_attrs, nr_ids, i, j;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002870 int fd = perf_data__fd(data);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002871
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002872 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002873 if (session->evlist == NULL)
2874 return -ENOMEM;
2875
Kan Liang2c071442015-08-28 05:48:05 -04002876 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03002877 session->machines.host.env = &header->env;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002878 if (perf_data__is_pipe(data))
Jiri Olsad4339562013-07-17 19:49:41 +02002879 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002880
Stephane Eranian69996df2012-02-09 23:21:06 +01002881 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002882 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002883
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002884 /*
2885 * Sanity check that perf.data was written cleanly; data size is
2886 * initialized to 0 and updated only if the on_exit function is run.
2887 * If data size is still 0 then the file contains only partial
2888 * information. Just warn user and process it as much as it can.
2889 */
2890 if (f_header.data.size == 0) {
2891 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2892 "Was the 'perf record' command properly terminated?\n",
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002893 data->file.path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002894 }
2895
Stephane Eranian69996df2012-02-09 23:21:06 +01002896 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002897 lseek(fd, f_header.attrs.offset, SEEK_SET);
2898
2899 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002900 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002901 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002902
Stephane Eranian69996df2012-02-09 23:21:06 +01002903 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002904 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002905
David Ahern1060ab82015-04-09 16:15:46 -04002906 if (header->needs_swap) {
2907 f_attr.ids.size = bswap_64(f_attr.ids.size);
2908 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002909 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002910 }
David Aherneda39132011-07-15 12:34:09 -06002911
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002912 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002913 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002914
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002915 if (evsel == NULL)
2916 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002917
2918 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002919 /*
2920 * Do it before so that if perf_evsel__alloc_id fails, this
2921 * entry gets purged too at perf_evlist__delete().
2922 */
2923 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002924
2925 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002926 /*
2927 * We don't have the cpu and thread maps on the header, so
2928 * for allocating the perf_sample_id table we fake 1 cpu and
2929 * hattr->ids threads.
2930 */
2931 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2932 goto out_delete_evlist;
2933
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002934 lseek(fd, f_attr.ids.offset, SEEK_SET);
2935
2936 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002937 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002938 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002939
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002940 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002941 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002942
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002943 lseek(fd, tmp, SEEK_SET);
2944 }
2945
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002946 symbol_conf.nr_events = nr_attrs;
2947
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002948 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002949 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002950
Namhyung Kim831394b2012-09-06 11:10:46 +09002951 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002952 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002953 goto out_delete_evlist;
2954
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002955 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002956out_errno:
2957 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002958
2959out_delete_evlist:
2960 perf_evlist__delete(session->evlist);
2961 session->evlist = NULL;
2962 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002963}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002964
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002965int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002966 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002967 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002968{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002969 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002970 size_t size;
2971 int err;
2972
2973 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002974 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002975 size += sizeof(struct perf_event_header);
2976 size += ids * sizeof(u64);
2977
2978 ev = malloc(size);
2979
Chris Samuelce47dc52010-11-13 13:35:06 +11002980 if (ev == NULL)
2981 return -ENOMEM;
2982
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002983 ev->attr.attr = *attr;
2984 memcpy(ev->attr.id, id, ids * sizeof(u64));
2985
2986 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002987 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002988
Robert Richterf4d83432012-08-16 21:10:17 +02002989 if (ev->attr.header.size == size)
2990 err = process(tool, ev, NULL, NULL);
2991 else
2992 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002993
2994 free(ev);
2995
2996 return err;
2997}
2998
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07002999int perf_event__synthesize_features(struct perf_tool *tool,
3000 struct perf_session *session,
3001 struct perf_evlist *evlist,
3002 perf_event__handler_t process)
3003{
3004 struct perf_header *header = &session->header;
3005 struct feat_fd ff;
3006 struct feature_event *fe;
3007 size_t sz, sz_hdr;
3008 int feat, ret;
3009
3010 sz_hdr = sizeof(fe->header);
3011 sz = sizeof(union perf_event);
3012 /* get a nice alignment */
3013 sz = PERF_ALIGN(sz, page_size);
3014
3015 memset(&ff, 0, sizeof(ff));
3016
3017 ff.buf = malloc(sz);
3018 if (!ff.buf)
3019 return -ENOMEM;
3020
3021 ff.size = sz - sz_hdr;
3022
3023 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3024 if (!feat_ops[feat].synthesize) {
3025 pr_debug("No record header feature for header :%d\n", feat);
3026 continue;
3027 }
3028
3029 ff.offset = sizeof(*fe);
3030
3031 ret = feat_ops[feat].write(&ff, evlist);
3032 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3033 pr_debug("Error writing feature\n");
3034 continue;
3035 }
3036 /* ff.buf may have changed due to realloc in do_write() */
3037 fe = ff.buf;
3038 memset(fe, 0, sizeof(*fe));
3039
3040 fe->feat_id = feat;
3041 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3042 fe->header.size = ff.offset;
3043
3044 ret = process(tool, ff.buf, NULL, NULL);
3045 if (ret) {
3046 free(ff.buf);
3047 return ret;
3048 }
3049 }
3050 free(ff.buf);
3051 return 0;
3052}
3053
3054int perf_event__process_feature(struct perf_tool *tool,
3055 union perf_event *event,
3056 struct perf_session *session __maybe_unused)
3057{
3058 struct feat_fd ff = { .fd = 0 };
3059 struct feature_event *fe = (struct feature_event *)event;
3060 int type = fe->header.type;
3061 u64 feat = fe->feat_id;
3062
3063 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3064 pr_warning("invalid record type %d in pipe-mode\n", type);
3065 return 0;
3066 }
3067 if (feat == HEADER_RESERVED || feat > HEADER_LAST_FEATURE) {
3068 pr_warning("invalid record type %d in pipe-mode\n", type);
3069 return -1;
3070 }
3071
3072 if (!feat_ops[feat].process)
3073 return 0;
3074
3075 ff.buf = (void *)fe->data;
3076 ff.size = event->header.size - sizeof(event->header);
3077 ff.ph = &session->header;
3078
3079 if (feat_ops[feat].process(&ff, NULL))
3080 return -1;
3081
3082 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3083 return 0;
3084
3085 if (!feat_ops[feat].full_only ||
3086 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3087 feat_ops[feat].print(&ff, stdout);
3088 } else {
3089 fprintf(stdout, "# %s info available, use -I to display\n",
3090 feat_ops[feat].name);
3091 }
3092
3093 return 0;
3094}
3095
Jiri Olsaa6e52812015-10-25 15:51:37 +01003096static struct event_update_event *
3097event_update_event__new(size_t size, u64 type, u64 id)
3098{
3099 struct event_update_event *ev;
3100
3101 size += sizeof(*ev);
3102 size = PERF_ALIGN(size, sizeof(u64));
3103
3104 ev = zalloc(size);
3105 if (ev) {
3106 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3107 ev->header.size = (u16)size;
3108 ev->type = type;
3109 ev->id = id;
3110 }
3111 return ev;
3112}
3113
3114int
3115perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3116 struct perf_evsel *evsel,
3117 perf_event__handler_t process)
3118{
3119 struct event_update_event *ev;
3120 size_t size = strlen(evsel->unit);
3121 int err;
3122
3123 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3124 if (ev == NULL)
3125 return -ENOMEM;
3126
3127 strncpy(ev->data, evsel->unit, size);
3128 err = process(tool, (union perf_event *)ev, NULL, NULL);
3129 free(ev);
3130 return err;
3131}
3132
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003133int
3134perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3135 struct perf_evsel *evsel,
3136 perf_event__handler_t process)
3137{
3138 struct event_update_event *ev;
3139 struct event_update_event_scale *ev_data;
3140 int err;
3141
3142 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3143 if (ev == NULL)
3144 return -ENOMEM;
3145
3146 ev_data = (struct event_update_event_scale *) ev->data;
3147 ev_data->scale = evsel->scale;
3148 err = process(tool, (union perf_event*) ev, NULL, NULL);
3149 free(ev);
3150 return err;
3151}
3152
Jiri Olsa802c9042015-10-25 15:51:39 +01003153int
3154perf_event__synthesize_event_update_name(struct perf_tool *tool,
3155 struct perf_evsel *evsel,
3156 perf_event__handler_t process)
3157{
3158 struct event_update_event *ev;
3159 size_t len = strlen(evsel->name);
3160 int err;
3161
3162 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3163 if (ev == NULL)
3164 return -ENOMEM;
3165
3166 strncpy(ev->data, evsel->name, len);
3167 err = process(tool, (union perf_event*) ev, NULL, NULL);
3168 free(ev);
3169 return err;
3170}
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003171
Jiri Olsa86ebb092015-10-25 15:51:40 +01003172int
3173perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3174 struct perf_evsel *evsel,
3175 perf_event__handler_t process)
3176{
3177 size_t size = sizeof(struct event_update_event);
3178 struct event_update_event *ev;
3179 int max, err;
3180 u16 type;
3181
3182 if (!evsel->own_cpus)
3183 return 0;
3184
3185 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3186 if (!ev)
3187 return -ENOMEM;
3188
3189 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3190 ev->header.size = (u16)size;
3191 ev->type = PERF_EVENT_UPDATE__CPUS;
3192 ev->id = evsel->id[0];
3193
3194 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3195 evsel->own_cpus,
3196 type, max);
3197
3198 err = process(tool, (union perf_event*) ev, NULL, NULL);
3199 free(ev);
3200 return err;
3201}
3202
Jiri Olsac853f932015-10-25 15:51:41 +01003203size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3204{
3205 struct event_update_event *ev = &event->event_update;
3206 struct event_update_event_scale *ev_scale;
3207 struct event_update_event_cpus *ev_cpus;
3208 struct cpu_map *map;
3209 size_t ret;
3210
3211 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3212
3213 switch (ev->type) {
3214 case PERF_EVENT_UPDATE__SCALE:
3215 ev_scale = (struct event_update_event_scale *) ev->data;
3216 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3217 break;
3218 case PERF_EVENT_UPDATE__UNIT:
3219 ret += fprintf(fp, "... unit: %s\n", ev->data);
3220 break;
3221 case PERF_EVENT_UPDATE__NAME:
3222 ret += fprintf(fp, "... name: %s\n", ev->data);
3223 break;
3224 case PERF_EVENT_UPDATE__CPUS:
3225 ev_cpus = (struct event_update_event_cpus *) ev->data;
3226 ret += fprintf(fp, "... ");
3227
3228 map = cpu_map__new_data(&ev_cpus->cpus);
3229 if (map)
3230 ret += cpu_map__fprintf(map, fp);
3231 else
3232 ret += fprintf(fp, "failed to get cpus\n");
3233 break;
3234 default:
3235 ret += fprintf(fp, "... unknown type\n");
3236 break;
3237 }
3238
3239 return ret;
3240}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003241
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003242int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003243 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003244 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003245{
Robert Richter6606f872012-08-16 21:10:19 +02003246 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003247 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003248
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003249 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003250 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3251 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003252 if (err) {
3253 pr_debug("failed to create perf header attribute\n");
3254 return err;
3255 }
3256 }
3257
3258 return err;
3259}
3260
Andi Kleenbfd8f722017-11-17 13:42:58 -08003261static bool has_unit(struct perf_evsel *counter)
3262{
3263 return counter->unit && *counter->unit;
3264}
3265
3266static bool has_scale(struct perf_evsel *counter)
3267{
3268 return counter->scale != 1;
3269}
3270
3271int perf_event__synthesize_extra_attr(struct perf_tool *tool,
3272 struct perf_evlist *evsel_list,
3273 perf_event__handler_t process,
3274 bool is_pipe)
3275{
3276 struct perf_evsel *counter;
3277 int err;
3278
3279 /*
3280 * Synthesize other events stuff not carried within
3281 * attr event - unit, scale, name
3282 */
3283 evlist__for_each_entry(evsel_list, counter) {
3284 if (!counter->supported)
3285 continue;
3286
3287 /*
3288 * Synthesize unit and scale only if it's defined.
3289 */
3290 if (has_unit(counter)) {
3291 err = perf_event__synthesize_event_update_unit(tool, counter, process);
3292 if (err < 0) {
3293 pr_err("Couldn't synthesize evsel unit.\n");
3294 return err;
3295 }
3296 }
3297
3298 if (has_scale(counter)) {
3299 err = perf_event__synthesize_event_update_scale(tool, counter, process);
3300 if (err < 0) {
3301 pr_err("Couldn't synthesize evsel counter.\n");
3302 return err;
3303 }
3304 }
3305
3306 if (counter->own_cpus) {
3307 err = perf_event__synthesize_event_update_cpus(tool, counter, process);
3308 if (err < 0) {
3309 pr_err("Couldn't synthesize evsel cpus.\n");
3310 return err;
3311 }
3312 }
3313
3314 /*
3315 * Name is needed only for pipe output,
3316 * perf.data carries event names.
3317 */
3318 if (is_pipe) {
3319 err = perf_event__synthesize_event_update_name(tool, counter, process);
3320 if (err < 0) {
3321 pr_err("Couldn't synthesize evsel name.\n");
3322 return err;
3323 }
3324 }
3325 }
3326 return 0;
3327}
3328
Adrian Hunter47c3d102013-07-04 16:20:21 +03003329int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3330 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003331 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003332{
Robert Richterf4d83432012-08-16 21:10:17 +02003333 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003334 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003335 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003336
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003337 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003338 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003339 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003340 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003341 }
3342
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003343 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003344 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003345 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003346
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003347 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003348
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003349 ids = event->header.size;
3350 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003351 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003352 /*
3353 * We don't have the cpu and thread maps on the header, so
3354 * for allocating the perf_sample_id table we fake 1 cpu and
3355 * hattr->ids threads.
3356 */
3357 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3358 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003359
3360 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003361 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003362 }
3363
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03003364 symbol_conf.nr_events = evlist->nr_entries;
3365
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003366 return 0;
3367}
Tom Zanussicd19a032010-04-01 23:59:20 -05003368
Jiri Olsaffe777252015-10-25 15:51:36 +01003369int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3370 union perf_event *event,
3371 struct perf_evlist **pevlist)
3372{
3373 struct event_update_event *ev = &event->event_update;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003374 struct event_update_event_scale *ev_scale;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003375 struct event_update_event_cpus *ev_cpus;
Jiri Olsaffe777252015-10-25 15:51:36 +01003376 struct perf_evlist *evlist;
3377 struct perf_evsel *evsel;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003378 struct cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003379
3380 if (!pevlist || *pevlist == NULL)
3381 return -EINVAL;
3382
3383 evlist = *pevlist;
3384
3385 evsel = perf_evlist__id2evsel(evlist, ev->id);
3386 if (evsel == NULL)
3387 return -EINVAL;
3388
Jiri Olsaa6e52812015-10-25 15:51:37 +01003389 switch (ev->type) {
3390 case PERF_EVENT_UPDATE__UNIT:
3391 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003392 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003393 case PERF_EVENT_UPDATE__NAME:
3394 evsel->name = strdup(ev->data);
3395 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003396 case PERF_EVENT_UPDATE__SCALE:
3397 ev_scale = (struct event_update_event_scale *) ev->data;
3398 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003399 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003400 case PERF_EVENT_UPDATE__CPUS:
3401 ev_cpus = (struct event_update_event_cpus *) ev->data;
3402
3403 map = cpu_map__new_data(&ev_cpus->cpus);
3404 if (map)
3405 evsel->own_cpus = map;
3406 else
3407 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003408 default:
3409 break;
3410 }
3411
Jiri Olsaffe777252015-10-25 15:51:36 +01003412 return 0;
3413}
3414
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003415int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003416 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003417 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003418{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003419 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003420 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003421 ssize_t size = 0, aligned_size = 0, padding;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003422 struct feat_fd ff;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003423 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003424
Jiri Olsa29208e52011-10-20 15:59:43 +02003425 /*
3426 * We are going to store the size of the data followed
3427 * by the data contents. Since the fd descriptor is a pipe,
3428 * we cannot seek back to store the size of the data once
3429 * we know it. Instead we:
3430 *
3431 * - write the tracing data to the temp file
3432 * - get/write the data size to pipe
3433 * - write the tracing data from the temp file
3434 * to the pipe
3435 */
3436 tdata = tracing_data_get(&evlist->entries, fd, true);
3437 if (!tdata)
3438 return -1;
3439
Tom Zanussi92155452010-04-01 23:59:21 -05003440 memset(&ev, 0, sizeof(ev));
3441
3442 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003443 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003444 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003445 padding = aligned_size - size;
3446 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3447 ev.tracing_data.size = aligned_size;
3448
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003449 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003450
Jiri Olsa29208e52011-10-20 15:59:43 +02003451 /*
3452 * The put function will copy all the tracing data
3453 * stored in temp file to the pipe.
3454 */
3455 tracing_data_put(tdata);
3456
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003457 ff = (struct feat_fd){ .fd = fd };
3458 if (write_padded(&ff, NULL, 0, padding))
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -07003459 return -1;
Tom Zanussi92155452010-04-01 23:59:21 -05003460
3461 return aligned_size;
3462}
3463
Adrian Hunter47c3d102013-07-04 16:20:21 +03003464int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3465 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003466 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05003467{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003468 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003469 int fd = perf_data__fd(session->data);
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003470 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003471 char buf[BUFSIZ];
3472
3473 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003474 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003475 SEEK_SET);
3476
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003477 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003478 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003479 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003480
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003481 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003482 pr_err("%s: reading input file", __func__);
3483 return -1;
3484 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003485 if (session->repipe) {
3486 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003487 if (retw <= 0 || retw != padding) {
3488 pr_err("%s: repiping tracing data padding", __func__);
3489 return -1;
3490 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003491 }
Tom Zanussi92155452010-04-01 23:59:21 -05003492
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003493 if (size_read + padding != size) {
3494 pr_err("%s: tracing data size mismatch", __func__);
3495 return -1;
3496 }
Tom Zanussi92155452010-04-01 23:59:21 -05003497
Namhyung Kim831394b2012-09-06 11:10:46 +09003498 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003499 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003500
Tom Zanussi92155452010-04-01 23:59:21 -05003501 return size_read + padding;
3502}
Tom Zanussic7929e42010-04-01 23:59:22 -05003503
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003504int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003505 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003506 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003507 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003508{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003509 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003510 size_t len;
3511 int err = 0;
3512
3513 if (!pos->hit)
3514 return err;
3515
3516 memset(&ev, 0, sizeof(ev));
3517
3518 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003519 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003520 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3521 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3522 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003523 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003524 ev.build_id.header.size = sizeof(ev.build_id) + len;
3525 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3526
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003527 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003528
3529 return err;
3530}
3531
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003532int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003533 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003534 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003535{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003536 __event_process_build_id(&event->build_id,
3537 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003538 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003539 return 0;
3540}