blob: 14db9f204b0ace56167fa47a628cead17b1b94da [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03002#include <inttypes.h>
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02003#include "util.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -03004#include "string2.h"
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -03005#include <sys/param.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02006#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02007#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02008#include <unistd.h>
9#include <stdio.h>
10#include <stdlib.h>
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -030011#include <linux/compiler.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010012#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -020013#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +010014#include <linux/bitops.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030015#include <sys/stat.h>
16#include <sys/types.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020017#include <sys/utsname.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030018#include <unistd.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020019
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020020#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030021#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020022#include "header.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030023#include "memswap.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020024#include "../perf.h"
25#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020026#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010027#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010028#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020029#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020030#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020031#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090032#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020033#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020034#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010035#include <api/fs/fs.h>
36#include "asm/bug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020037
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030038#include "sane_ctype.h"
39
Stephane Eranian73323f52012-02-02 13:54:44 +010040/*
41 * magic2 = "PERFILE2"
42 * must be a numerical value to let the endianness
43 * determine the memory layout. That way we are able
44 * to detect endianness when reading the perf.data file
45 * back.
46 *
47 * we check for legacy (PERFFILE) format.
48 */
49static const char *__perf_magic1 = "PERFFILE";
50static const u64 __perf_magic2 = 0x32454c4946524550ULL;
51static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020052
Stephane Eranian73323f52012-02-02 13:54:44 +010053#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020054
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090055const char perf_version_string[] = PERF_VERSION;
56
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020057struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020058 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020059 struct perf_file_section ids;
60};
61
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070062struct feat_fd {
63 struct perf_header *ph;
64 int fd;
David Carrillo-Cisneros62552452017-07-17 21:25:42 -070065 ssize_t offset;
66 size_t size;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070067};
68
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030069void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020070{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030071 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020072}
73
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030074void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020075{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030076 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020077}
78
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030079bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020080{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030081 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020082}
83
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -070084/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070085int do_write(struct feat_fd *ff, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020086{
David Carrillo-Cisneros3b8f51a2017-07-17 21:25:38 -070087 ssize_t ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020088
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070089 ret = writen(ff->fd, buf, size);
David Carrillo-Cisneros3b8f51a2017-07-17 21:25:38 -070090 if (ret != (ssize_t)size)
91 return ret < 0 ? (int)ret : -1;
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020092
93 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020094}
95
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -070096/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070097int write_padded(struct feat_fd *ff, const void *bf,
98 size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020099{
100 static const char zero_buf[NAME_ALIGN];
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700101 int err = do_write(ff, bf, count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200102
103 if (!err)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700104 err = do_write(ff, zero_buf, count_aligned - count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200105
106 return err;
107}
108
Kan Liang2bb00d22015-09-01 09:58:12 -0400109#define string_size(str) \
110 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
111
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700112/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700113static int do_write_string(struct feat_fd *ff, const char *str)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200114{
115 u32 len, olen;
116 int ret;
117
118 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300119 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200120
121 /* write len, incl. \0 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700122 ret = do_write(ff, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200123 if (ret < 0)
124 return ret;
125
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700126 return write_padded(ff, str, olen, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200127}
128
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700129static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700130{
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700131 ssize_t ret = readn(ff->fd, addr, size);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700132
133 if (ret != size)
134 return ret < 0 ? (int)ret : -1;
135 return 0;
136}
137
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700138static int do_read_u32(struct feat_fd *ff, u32 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700139{
140 int ret;
141
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700142 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700143 if (ret)
144 return ret;
145
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700146 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700147 *addr = bswap_32(*addr);
148 return 0;
149}
150
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700151static int do_read_u64(struct feat_fd *ff, u64 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700152{
153 int ret;
154
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700155 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700156 if (ret)
157 return ret;
158
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700159 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700160 *addr = bswap_64(*addr);
161 return 0;
162}
163
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700164static char *do_read_string(struct feat_fd *ff)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200165{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200166 u32 len;
167 char *buf;
168
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700169 if (do_read_u32(ff, &len))
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200170 return NULL;
171
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200172 buf = malloc(len);
173 if (!buf)
174 return NULL;
175
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700176 if (!__do_read(ff, buf, len)) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200177 /*
178 * strings are padded by zeroes
179 * thus the actual strlen of buf
180 * may be less than len
181 */
182 return buf;
183 }
184
185 free(buf);
186 return NULL;
187}
188
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700189static int write_tracing_data(struct feat_fd *ff,
190 struct perf_evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200191{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700192 return read_tracing_data(ff->fd, &evlist->entries);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200193}
194
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700195static int write_build_id(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300196 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200197{
198 struct perf_session *session;
199 int err;
200
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700201 session = container_of(ff->ph, struct perf_session, header);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200202
Robert Richtere20960c2011-12-07 10:02:55 +0100203 if (!perf_session__read_build_ids(session, true))
204 return -1;
205
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700206 err = perf_session__write_buildid_table(session, ff);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200207 if (err < 0) {
208 pr_debug("failed to write buildid table\n");
209 return err;
210 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900211 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200212
213 return 0;
214}
215
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700216static int write_hostname(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300217 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200218{
219 struct utsname uts;
220 int ret;
221
222 ret = uname(&uts);
223 if (ret < 0)
224 return -1;
225
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700226 return do_write_string(ff, uts.nodename);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200227}
228
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700229static int write_osrelease(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300230 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200231{
232 struct utsname uts;
233 int ret;
234
235 ret = uname(&uts);
236 if (ret < 0)
237 return -1;
238
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700239 return do_write_string(ff, uts.release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200240}
241
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700242static int write_arch(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300243 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200244{
245 struct utsname uts;
246 int ret;
247
248 ret = uname(&uts);
249 if (ret < 0)
250 return -1;
251
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700252 return do_write_string(ff, uts.machine);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200253}
254
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700255static int write_version(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{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700258 return do_write_string(ff, perf_version_string);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200259}
260
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700261static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200262{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200263 FILE *file;
264 char *buf = NULL;
265 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800266 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200267 size_t len = 0;
268 int ret = -1;
269
270 if (!search)
271 return -1;
272
273 file = fopen("/proc/cpuinfo", "r");
274 if (!file)
275 return -1;
276
277 while (getline(&buf, &len, file) > 0) {
278 ret = strncmp(buf, search, strlen(search));
279 if (!ret)
280 break;
281 }
282
Wang Naned307752014-10-16 11:08:29 +0800283 if (ret) {
284 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200285 goto done;
Wang Naned307752014-10-16 11:08:29 +0800286 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200287
288 s = buf;
289
290 p = strchr(buf, ':');
291 if (p && *(p+1) == ' ' && *(p+2))
292 s = p + 2;
293 p = strchr(s, '\n');
294 if (p)
295 *p = '\0';
296
297 /* squash extra space characters (branding string) */
298 p = s;
299 while (*p) {
300 if (isspace(*p)) {
301 char *r = p + 1;
302 char *q = r;
303 *p = ' ';
304 while (*q && isspace(*q))
305 q++;
306 if (q != (p+1))
307 while ((*r++ = *q++));
308 }
309 p++;
310 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700311 ret = do_write_string(ff, s);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200312done:
313 free(buf);
314 fclose(file);
315 return ret;
316}
317
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700318static int write_cpudesc(struct feat_fd *ff,
Wang Nan493c3032014-10-24 09:45:26 +0800319 struct perf_evlist *evlist __maybe_unused)
320{
321#ifndef CPUINFO_PROC
322#define CPUINFO_PROC {"model name", }
323#endif
324 const char *cpuinfo_procs[] = CPUINFO_PROC;
325 unsigned int i;
326
327 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
328 int ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700329 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
Wang Nan493c3032014-10-24 09:45:26 +0800330 if (ret >= 0)
331 return ret;
332 }
333 return -1;
334}
335
336
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700337static int write_nrcpus(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300338 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200339{
340 long nr;
341 u32 nrc, nra;
342 int ret;
343
Jan Stancekda8a58b2017-02-17 12:10:26 +0100344 nrc = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200345
346 nr = sysconf(_SC_NPROCESSORS_ONLN);
347 if (nr < 0)
348 return -1;
349
350 nra = (u32)(nr & UINT_MAX);
351
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700352 ret = do_write(ff, &nrc, sizeof(nrc));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200353 if (ret < 0)
354 return ret;
355
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700356 return do_write(ff, &nra, sizeof(nra));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200357}
358
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700359static int write_event_desc(struct feat_fd *ff,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200360 struct perf_evlist *evlist)
361{
Robert Richter6606f872012-08-16 21:10:19 +0200362 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900363 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200364 int ret;
365
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900366 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200367
368 /*
369 * write number of events
370 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700371 ret = do_write(ff, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200372 if (ret < 0)
373 return ret;
374
375 /*
376 * size of perf_event_attr struct
377 */
Robert Richter6606f872012-08-16 21:10:19 +0200378 sz = (u32)sizeof(evsel->attr);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700379 ret = do_write(ff, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200380 if (ret < 0)
381 return ret;
382
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300383 evlist__for_each_entry(evlist, evsel) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700384 ret = do_write(ff, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200385 if (ret < 0)
386 return ret;
387 /*
388 * write number of unique id per event
389 * there is one id per instance of an event
390 *
391 * copy into an nri to be independent of the
392 * type of ids,
393 */
Robert Richter6606f872012-08-16 21:10:19 +0200394 nri = evsel->ids;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700395 ret = do_write(ff, &nri, sizeof(nri));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200396 if (ret < 0)
397 return ret;
398
399 /*
400 * write event string as passed on cmdline
401 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700402 ret = do_write_string(ff, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200403 if (ret < 0)
404 return ret;
405 /*
406 * write unique ids for this event
407 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700408 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200409 if (ret < 0)
410 return ret;
411 }
412 return 0;
413}
414
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700415static int write_cmdline(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300416 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200417{
418 char buf[MAXPATHLEN];
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300419 u32 n;
420 int i, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200421
Tommi Rantala55f771282017-03-22 15:06:24 +0200422 /* actual path to perf binary */
423 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200424 if (ret <= 0)
425 return -1;
426
427 /* readlink() does not add null termination */
428 buf[ret] = '\0';
429
430 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300431 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200432
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700433 ret = do_write(ff, &n, sizeof(n));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200434 if (ret < 0)
435 return ret;
436
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700437 ret = do_write_string(ff, buf);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200438 if (ret < 0)
439 return ret;
440
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300441 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700442 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200443 if (ret < 0)
444 return ret;
445 }
446 return 0;
447}
448
449#define CORE_SIB_FMT \
450 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
451#define THRD_SIB_FMT \
452 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
453
454struct cpu_topo {
Kan Liang2bb00d22015-09-01 09:58:12 -0400455 u32 cpu_nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200456 u32 core_sib;
457 u32 thread_sib;
458 char **core_siblings;
459 char **thread_siblings;
460};
461
462static int build_cpu_topo(struct cpu_topo *tp, int cpu)
463{
464 FILE *fp;
465 char filename[MAXPATHLEN];
466 char *buf = NULL, *p;
467 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200468 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200469 u32 i = 0;
470 int ret = -1;
471
472 sprintf(filename, CORE_SIB_FMT, cpu);
473 fp = fopen(filename, "r");
474 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200475 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200476
Stephane Eranianc5885742013-08-14 12:04:26 +0200477 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200478 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200479 if (sret <= 0)
480 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200481
482 p = strchr(buf, '\n');
483 if (p)
484 *p = '\0';
485
486 for (i = 0; i < tp->core_sib; i++) {
487 if (!strcmp(buf, tp->core_siblings[i]))
488 break;
489 }
490 if (i == tp->core_sib) {
491 tp->core_siblings[i] = buf;
492 tp->core_sib++;
493 buf = NULL;
494 len = 0;
495 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200496 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200497
Stephane Eranianc5885742013-08-14 12:04:26 +0200498try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200499 sprintf(filename, THRD_SIB_FMT, cpu);
500 fp = fopen(filename, "r");
501 if (!fp)
502 goto done;
503
504 if (getline(&buf, &len, fp) <= 0)
505 goto done;
506
507 p = strchr(buf, '\n');
508 if (p)
509 *p = '\0';
510
511 for (i = 0; i < tp->thread_sib; i++) {
512 if (!strcmp(buf, tp->thread_siblings[i]))
513 break;
514 }
515 if (i == tp->thread_sib) {
516 tp->thread_siblings[i] = buf;
517 tp->thread_sib++;
518 buf = NULL;
519 }
520 ret = 0;
521done:
522 if(fp)
523 fclose(fp);
524 free(buf);
525 return ret;
526}
527
528static void free_cpu_topo(struct cpu_topo *tp)
529{
530 u32 i;
531
532 if (!tp)
533 return;
534
535 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300536 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200537
538 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300539 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200540
541 free(tp);
542}
543
544static struct cpu_topo *build_cpu_topology(void)
545{
Jan Stancek43db2842017-02-17 12:10:25 +0100546 struct cpu_topo *tp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200547 void *addr;
548 u32 nr, i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300549 size_t sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200550 long ncpus;
551 int ret = -1;
Jan Stancek43db2842017-02-17 12:10:25 +0100552 struct cpu_map *map;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200553
Jan Stancekda8a58b2017-02-17 12:10:26 +0100554 ncpus = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200555
Jan Stancek43db2842017-02-17 12:10:25 +0100556 /* build online CPU map */
557 map = cpu_map__new(NULL);
558 if (map == NULL) {
559 pr_debug("failed to get system cpumap\n");
560 return NULL;
561 }
562
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200563 nr = (u32)(ncpus & UINT_MAX);
564
565 sz = nr * sizeof(char *);
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300566 addr = calloc(1, sizeof(*tp) + 2 * sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200567 if (!addr)
Jan Stancek43db2842017-02-17 12:10:25 +0100568 goto out_free;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200569
570 tp = addr;
Kan Liang2bb00d22015-09-01 09:58:12 -0400571 tp->cpu_nr = nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200572 addr += sizeof(*tp);
573 tp->core_siblings = addr;
574 addr += sz;
575 tp->thread_siblings = addr;
576
577 for (i = 0; i < nr; i++) {
Jan Stancek43db2842017-02-17 12:10:25 +0100578 if (!cpu_map__has(map, i))
579 continue;
580
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200581 ret = build_cpu_topo(tp, i);
582 if (ret < 0)
583 break;
584 }
Jan Stancek43db2842017-02-17 12:10:25 +0100585
586out_free:
587 cpu_map__put(map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200588 if (ret) {
589 free_cpu_topo(tp);
590 tp = NULL;
591 }
592 return tp;
593}
594
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700595static int write_cpu_topology(struct feat_fd *ff,
596 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200597{
598 struct cpu_topo *tp;
599 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300600 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200601
602 tp = build_cpu_topology();
603 if (!tp)
604 return -1;
605
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700606 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200607 if (ret < 0)
608 goto done;
609
610 for (i = 0; i < tp->core_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700611 ret = do_write_string(ff, tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200612 if (ret < 0)
613 goto done;
614 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700615 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200616 if (ret < 0)
617 goto done;
618
619 for (i = 0; i < tp->thread_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700620 ret = do_write_string(ff, tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200621 if (ret < 0)
622 break;
623 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400624
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300625 ret = perf_env__read_cpu_topology_map(&perf_env);
626 if (ret < 0)
627 goto done;
628
629 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700630 ret = do_write(ff, &perf_env.cpu[j].core_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300631 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400632 if (ret < 0)
633 return ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700634 ret = do_write(ff, &perf_env.cpu[j].socket_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300635 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400636 if (ret < 0)
637 return ret;
638 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200639done:
640 free_cpu_topo(tp);
641 return ret;
642}
643
644
645
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700646static int write_total_mem(struct feat_fd *ff,
647 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200648{
649 char *buf = NULL;
650 FILE *fp;
651 size_t len = 0;
652 int ret = -1, n;
653 uint64_t mem;
654
655 fp = fopen("/proc/meminfo", "r");
656 if (!fp)
657 return -1;
658
659 while (getline(&buf, &len, fp) > 0) {
660 ret = strncmp(buf, "MemTotal:", 9);
661 if (!ret)
662 break;
663 }
664 if (!ret) {
665 n = sscanf(buf, "%*s %"PRIu64, &mem);
666 if (n == 1)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700667 ret = do_write(ff, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800668 } else
669 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200670 free(buf);
671 fclose(fp);
672 return ret;
673}
674
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700675static int write_topo_node(struct feat_fd *ff, int node)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200676{
677 char str[MAXPATHLEN];
678 char field[32];
679 char *buf = NULL, *p;
680 size_t len = 0;
681 FILE *fp;
682 u64 mem_total, mem_free, mem;
683 int ret = -1;
684
685 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
686 fp = fopen(str, "r");
687 if (!fp)
688 return -1;
689
690 while (getline(&buf, &len, fp) > 0) {
691 /* skip over invalid lines */
692 if (!strchr(buf, ':'))
693 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100694 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200695 goto done;
696 if (!strcmp(field, "MemTotal:"))
697 mem_total = mem;
698 if (!strcmp(field, "MemFree:"))
699 mem_free = mem;
700 }
701
702 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100703 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200704
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700705 ret = do_write(ff, &mem_total, sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200706 if (ret)
707 goto done;
708
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700709 ret = do_write(ff, &mem_free, sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200710 if (ret)
711 goto done;
712
713 ret = -1;
714 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
715
716 fp = fopen(str, "r");
717 if (!fp)
718 goto done;
719
720 if (getline(&buf, &len, fp) <= 0)
721 goto done;
722
723 p = strchr(buf, '\n');
724 if (p)
725 *p = '\0';
726
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700727 ret = do_write_string(ff, buf);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200728done:
729 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100730 if (fp)
731 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200732 return ret;
733}
734
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700735static int write_numa_topology(struct feat_fd *ff,
736 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200737{
738 char *buf = NULL;
739 size_t len = 0;
740 FILE *fp;
741 struct cpu_map *node_map = NULL;
742 char *c;
743 u32 nr, i, j;
744 int ret = -1;
745
746 fp = fopen("/sys/devices/system/node/online", "r");
747 if (!fp)
748 return -1;
749
750 if (getline(&buf, &len, fp) <= 0)
751 goto done;
752
753 c = strchr(buf, '\n');
754 if (c)
755 *c = '\0';
756
757 node_map = cpu_map__new(buf);
758 if (!node_map)
759 goto done;
760
761 nr = (u32)node_map->nr;
762
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700763 ret = do_write(ff, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200764 if (ret < 0)
765 goto done;
766
767 for (i = 0; i < nr; i++) {
768 j = (u32)node_map->map[i];
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700769 ret = do_write(ff, &j, sizeof(j));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200770 if (ret < 0)
771 break;
772
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700773 ret = write_topo_node(ff, i);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200774 if (ret < 0)
775 break;
776 }
777done:
778 free(buf);
779 fclose(fp);
Masami Hiramatsu5191d8872015-12-09 11:11:35 +0900780 cpu_map__put(node_map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200781 return ret;
782}
783
784/*
Robert Richter50a96672012-08-16 21:10:24 +0200785 * File format:
786 *
787 * struct pmu_mappings {
788 * u32 pmu_num;
789 * struct pmu_map {
790 * u32 type;
791 * char name[];
792 * }[pmu_num];
793 * };
794 */
795
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700796static int write_pmu_mappings(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300797 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200798{
799 struct perf_pmu *pmu = NULL;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700800 off_t offset = lseek(ff->fd, 0, SEEK_CUR);
Robert Richter50a96672012-08-16 21:10:24 +0200801 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900802 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200803
804 /* write real pmu_num later */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700805 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
Namhyung Kim5323f602012-12-17 15:38:54 +0900806 if (ret < 0)
807 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200808
809 while ((pmu = perf_pmu__scan(pmu))) {
810 if (!pmu->name)
811 continue;
812 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +0900813
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700814 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
Namhyung Kim5323f602012-12-17 15:38:54 +0900815 if (ret < 0)
816 return ret;
817
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700818 ret = do_write_string(ff, pmu->name);
Namhyung Kim5323f602012-12-17 15:38:54 +0900819 if (ret < 0)
820 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200821 }
822
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700823 if (pwrite(ff->fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
Robert Richter50a96672012-08-16 21:10:24 +0200824 /* discard all */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700825 lseek(ff->fd, offset, SEEK_SET);
Robert Richter50a96672012-08-16 21:10:24 +0200826 return -1;
827 }
828
829 return 0;
830}
831
832/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900833 * File format:
834 *
835 * struct group_descs {
836 * u32 nr_groups;
837 * struct group_desc {
838 * char name[];
839 * u32 leader_idx;
840 * u32 nr_members;
841 * }[nr_groups];
842 * };
843 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700844static int write_group_desc(struct feat_fd *ff,
Namhyung Kima8bb5592013-01-22 18:09:31 +0900845 struct perf_evlist *evlist)
846{
847 u32 nr_groups = evlist->nr_groups;
848 struct perf_evsel *evsel;
849 int ret;
850
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700851 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900852 if (ret < 0)
853 return ret;
854
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300855 evlist__for_each_entry(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900856 if (perf_evsel__is_group_leader(evsel) &&
857 evsel->nr_members > 1) {
858 const char *name = evsel->group_name ?: "{anon_group}";
859 u32 leader_idx = evsel->idx;
860 u32 nr_members = evsel->nr_members;
861
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700862 ret = do_write_string(ff, name);
Namhyung Kima8bb5592013-01-22 18:09:31 +0900863 if (ret < 0)
864 return ret;
865
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700866 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900867 if (ret < 0)
868 return ret;
869
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700870 ret = do_write(ff, &nr_members, sizeof(nr_members));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900871 if (ret < 0)
872 return ret;
873 }
874 }
875 return 0;
876}
877
878/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200879 * default get_cpuid(): nothing gets recorded
Jiada Wang7a759cd2017-04-09 20:02:37 -0700880 * actual implementation must be in arch/$(SRCARCH)/util/header.c
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200881 */
Rui Teng11d8f872016-07-28 10:05:57 +0800882int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200883{
884 return -1;
885}
886
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700887static int write_cpuid(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300888 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200889{
890 char buffer[64];
891 int ret;
892
893 ret = get_cpuid(buffer, sizeof(buffer));
894 if (!ret)
895 goto write_it;
896
897 return -1;
898write_it:
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700899 return do_write_string(ff, buffer);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200900}
901
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700902static int write_branch_stack(struct feat_fd *ff __maybe_unused,
903 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100904{
905 return 0;
906}
907
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700908static int write_auxtrace(struct feat_fd *ff,
Adrian Hunter4025ea42015-04-09 18:53:41 +0300909 struct perf_evlist *evlist __maybe_unused)
910{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300911 struct perf_session *session;
912 int err;
913
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700914 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300915
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700916 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300917 if (err < 0)
918 pr_err("Failed to write auxtrace index\n");
919 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300920}
921
Jiri Olsa720e98b2016-02-16 16:01:43 +0100922static int cpu_cache_level__sort(const void *a, const void *b)
923{
924 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
925 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
926
927 return cache_a->level - cache_b->level;
928}
929
930static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
931{
932 if (a->level != b->level)
933 return false;
934
935 if (a->line_size != b->line_size)
936 return false;
937
938 if (a->sets != b->sets)
939 return false;
940
941 if (a->ways != b->ways)
942 return false;
943
944 if (strcmp(a->type, b->type))
945 return false;
946
947 if (strcmp(a->size, b->size))
948 return false;
949
950 if (strcmp(a->map, b->map))
951 return false;
952
953 return true;
954}
955
956static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
957{
958 char path[PATH_MAX], file[PATH_MAX];
959 struct stat st;
960 size_t len;
961
962 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
963 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
964
965 if (stat(file, &st))
966 return 1;
967
968 scnprintf(file, PATH_MAX, "%s/level", path);
969 if (sysfs__read_int(file, (int *) &cache->level))
970 return -1;
971
972 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
973 if (sysfs__read_int(file, (int *) &cache->line_size))
974 return -1;
975
976 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
977 if (sysfs__read_int(file, (int *) &cache->sets))
978 return -1;
979
980 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
981 if (sysfs__read_int(file, (int *) &cache->ways))
982 return -1;
983
984 scnprintf(file, PATH_MAX, "%s/type", path);
985 if (sysfs__read_str(file, &cache->type, &len))
986 return -1;
987
988 cache->type[len] = 0;
989 cache->type = rtrim(cache->type);
990
991 scnprintf(file, PATH_MAX, "%s/size", path);
992 if (sysfs__read_str(file, &cache->size, &len)) {
993 free(cache->type);
994 return -1;
995 }
996
997 cache->size[len] = 0;
998 cache->size = rtrim(cache->size);
999
1000 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1001 if (sysfs__read_str(file, &cache->map, &len)) {
1002 free(cache->map);
1003 free(cache->type);
1004 return -1;
1005 }
1006
1007 cache->map[len] = 0;
1008 cache->map = rtrim(cache->map);
1009 return 0;
1010}
1011
1012static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1013{
1014 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1015}
1016
1017static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1018{
1019 u32 i, cnt = 0;
1020 long ncpus;
1021 u32 nr, cpu;
1022 u16 level;
1023
1024 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1025 if (ncpus < 0)
1026 return -1;
1027
1028 nr = (u32)(ncpus & UINT_MAX);
1029
1030 for (cpu = 0; cpu < nr; cpu++) {
1031 for (level = 0; level < 10; level++) {
1032 struct cpu_cache_level c;
1033 int err;
1034
1035 err = cpu_cache_level__read(&c, cpu, level);
1036 if (err < 0)
1037 return err;
1038
1039 if (err == 1)
1040 break;
1041
1042 for (i = 0; i < cnt; i++) {
1043 if (cpu_cache_level__cmp(&c, &caches[i]))
1044 break;
1045 }
1046
1047 if (i == cnt)
1048 caches[cnt++] = c;
1049 else
1050 cpu_cache_level__free(&c);
1051
1052 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1053 goto out;
1054 }
1055 }
1056 out:
1057 *cntp = cnt;
1058 return 0;
1059}
1060
1061#define MAX_CACHES 2000
1062
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001063static int write_cache(struct feat_fd *ff,
1064 struct perf_evlist *evlist __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001065{
1066 struct cpu_cache_level caches[MAX_CACHES];
1067 u32 cnt = 0, i, version = 1;
1068 int ret;
1069
1070 ret = build_caches(caches, MAX_CACHES, &cnt);
1071 if (ret)
1072 goto out;
1073
1074 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1075
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001076 ret = do_write(ff, &version, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001077 if (ret < 0)
1078 goto out;
1079
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001080 ret = do_write(ff, &cnt, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001081 if (ret < 0)
1082 goto out;
1083
1084 for (i = 0; i < cnt; i++) {
1085 struct cpu_cache_level *c = &caches[i];
1086
1087 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001088 ret = do_write(ff, &c->v, sizeof(u32)); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001089 if (ret < 0) \
1090 goto out;
1091
1092 _W(level)
1093 _W(line_size)
1094 _W(sets)
1095 _W(ways)
1096 #undef _W
1097
1098 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001099 ret = do_write_string(ff, (const char *) c->v); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001100 if (ret < 0) \
1101 goto out;
1102
1103 _W(type)
1104 _W(size)
1105 _W(map)
1106 #undef _W
1107 }
1108
1109out:
1110 for (i = 0; i < cnt; i++)
1111 cpu_cache_level__free(&caches[i]);
1112 return ret;
1113}
1114
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001115static int write_stat(struct feat_fd *ff __maybe_unused,
Jiri Olsaffa517a2015-10-25 15:51:43 +01001116 struct perf_evlist *evlist __maybe_unused)
1117{
1118 return 0;
1119}
1120
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001121static void print_hostname(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001122{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001123 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001124}
1125
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001126static void print_osrelease(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001127{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001128 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001129}
1130
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001131static void print_arch(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001132{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001133 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001134}
1135
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001136static void print_cpudesc(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001137{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001138 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001139}
1140
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001141static void print_nrcpus(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001142{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001143 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1144 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001145}
1146
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001147static void print_version(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001148{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001149 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001150}
1151
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001152static void print_cmdline(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001153{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001154 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001155
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001156 nr = ff->ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001157
1158 fprintf(fp, "# cmdline : ");
1159
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001160 for (i = 0; i < nr; i++)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001161 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001162 fputc('\n', fp);
1163}
1164
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001165static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001166{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001167 struct perf_header *ph = ff->ph;
1168 int cpu_nr = ph->env.nr_cpus_avail;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001169 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001170 char *str;
1171
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001172 nr = ph->env.nr_sibling_cores;
1173 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001174
1175 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001176 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001177 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001178 }
1179
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001180 nr = ph->env.nr_sibling_threads;
1181 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001182
1183 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001184 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001185 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001186 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001187
1188 if (ph->env.cpu != NULL) {
1189 for (i = 0; i < cpu_nr; i++)
1190 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1191 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1192 } else
1193 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001194}
1195
Robert Richter4e1b9c62012-08-16 21:10:22 +02001196static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001197{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001198 struct perf_evsel *evsel;
1199
1200 if (!events)
1201 return;
1202
1203 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001204 zfree(&evsel->name);
1205 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001206 }
1207
1208 free(events);
1209}
1210
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001211static struct perf_evsel *read_event_desc(struct feat_fd *ff)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001212{
1213 struct perf_evsel *evsel, *events = NULL;
1214 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001215 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001216 u32 nre, sz, nr, i, j;
Stephane Eranian62db9062012-02-09 23:21:07 +01001217 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001218
1219 /* number of events */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001220 if (do_read_u32(ff, &nre))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001221 goto error;
1222
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001223 if (do_read_u32(ff, &sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001224 goto error;
1225
Stephane Eranian62db9062012-02-09 23:21:07 +01001226 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001227 buf = malloc(sz);
1228 if (!buf)
1229 goto error;
1230
Robert Richter4e1b9c62012-08-16 21:10:22 +02001231 /* the last event terminates with evsel->attr.size == 0: */
1232 events = calloc(nre + 1, sizeof(*events));
1233 if (!events)
1234 goto error;
1235
1236 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001237 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001238 msz = sz;
1239
Robert Richter4e1b9c62012-08-16 21:10:22 +02001240 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1241 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001242
Stephane Eranian62db9062012-02-09 23:21:07 +01001243 /*
1244 * must read entire on-file attr struct to
1245 * sync up with layout.
1246 */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001247 if (__do_read(ff, buf, sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001248 goto error;
1249
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001250 if (ff->ph->needs_swap)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001251 perf_event__attr_swap(buf);
1252
Robert Richter4e1b9c62012-08-16 21:10:22 +02001253 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001254
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001255 if (do_read_u32(ff, &nr))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001256 goto error;
1257
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001258 if (ff->ph->needs_swap)
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001259 evsel->needs_swap = true;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001260
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001261 evsel->name = do_read_string(ff);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001262 if (!evsel->name)
1263 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001264
1265 if (!nr)
1266 continue;
1267
1268 id = calloc(nr, sizeof(*id));
1269 if (!id)
1270 goto error;
1271 evsel->ids = nr;
1272 evsel->id = id;
1273
1274 for (j = 0 ; j < nr; j++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001275 if (do_read_u64(ff, id))
Robert Richter4e1b9c62012-08-16 21:10:22 +02001276 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001277 id++;
1278 }
1279 }
1280out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001281 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001282 return events;
1283error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001284 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001285 events = NULL;
1286 goto out;
1287}
1288
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001289static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001290 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001291{
1292 return fprintf(fp, ", %s = %s", name, val);
1293}
1294
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001295static void print_event_desc(struct feat_fd *ff, FILE *fp)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001296{
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001297 struct perf_evsel *evsel, *events = read_event_desc(ff);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001298 u32 j;
1299 u64 *id;
1300
1301 if (!events) {
1302 fprintf(fp, "# event desc: not available or unable to read\n");
1303 return;
1304 }
1305
1306 for (evsel = events; evsel->attr.size; evsel++) {
1307 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001308
Robert Richter4e1b9c62012-08-16 21:10:22 +02001309 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001310 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001311 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1312 if (j)
1313 fputc(',', fp);
1314 fprintf(fp, " %"PRIu64, *id);
1315 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001316 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001317 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001318
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001319 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001320
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001321 fputc('\n', fp);
1322 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001323
1324 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001325}
1326
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001327static void print_total_mem(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001328{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001329 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001330}
1331
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001332static void print_numa_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001333{
Jiri Olsac60da222016-07-04 14:16:20 +02001334 int i;
1335 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001336
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001337 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1338 n = &ff->ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001339
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001340 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1341 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001342 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001343
Jiri Olsac60da222016-07-04 14:16:20 +02001344 fprintf(fp, "# node%u cpu list : ", n->node);
1345 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001346 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001347}
1348
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001349static void print_cpuid(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001350{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001351 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001352}
1353
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001354static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001355{
1356 fprintf(fp, "# contains samples with branch stack\n");
1357}
1358
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001359static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
Adrian Hunter4025ea42015-04-09 18:53:41 +03001360{
1361 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1362}
1363
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001364static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001365{
1366 fprintf(fp, "# contains stat data\n");
1367}
1368
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001369static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001370{
1371 int i;
1372
1373 fprintf(fp, "# CPU cache info:\n");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001374 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
Jiri Olsa720e98b2016-02-16 16:01:43 +01001375 fprintf(fp, "# ");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001376 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001377 }
1378}
1379
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001380static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001381{
1382 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001383 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001384 u32 pmu_num;
1385 u32 type;
1386
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001387 pmu_num = ff->ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001388 if (!pmu_num) {
1389 fprintf(fp, "# pmu mappings: not available\n");
1390 return;
1391 }
1392
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001393 str = ff->ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001394
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001395 while (pmu_num) {
1396 type = strtoul(str, &tmp, 0);
1397 if (*tmp != ':')
1398 goto error;
1399
1400 str = tmp + 1;
1401 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1402
Robert Richter50a96672012-08-16 21:10:24 +02001403 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001404 str += strlen(str) + 1;
1405 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001406 }
1407
1408 fprintf(fp, "\n");
1409
1410 if (!pmu_num)
1411 return;
1412error:
1413 fprintf(fp, "# pmu mappings: unable to read\n");
1414}
1415
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001416static void print_group_desc(struct feat_fd *ff, FILE *fp)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001417{
1418 struct perf_session *session;
1419 struct perf_evsel *evsel;
1420 u32 nr = 0;
1421
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001422 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001423
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001424 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001425 if (perf_evsel__is_group_leader(evsel) &&
1426 evsel->nr_members > 1) {
1427 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1428 perf_evsel__name(evsel));
1429
1430 nr = evsel->nr_members - 1;
1431 } else if (nr) {
1432 fprintf(fp, ",%s", perf_evsel__name(evsel));
1433
1434 if (--nr == 0)
1435 fprintf(fp, "}\n");
1436 }
1437 }
1438}
1439
Robert Richter08d95bd2012-02-10 15:41:55 +01001440static int __event_process_build_id(struct build_id_event *bev,
1441 char *filename,
1442 struct perf_session *session)
1443{
1444 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001445 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001446 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001447 struct dso *dso;
1448 enum dso_kernel_type dso_type;
1449
1450 machine = perf_session__findnew_machine(session, bev->pid);
1451 if (!machine)
1452 goto out;
1453
Wang Nan1f121b02015-06-03 08:52:21 +00001454 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001455
Wang Nan1f121b02015-06-03 08:52:21 +00001456 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001457 case PERF_RECORD_MISC_KERNEL:
1458 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001459 break;
1460 case PERF_RECORD_MISC_GUEST_KERNEL:
1461 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001462 break;
1463 case PERF_RECORD_MISC_USER:
1464 case PERF_RECORD_MISC_GUEST_USER:
1465 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001466 break;
1467 default:
1468 goto out;
1469 }
1470
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001471 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001472 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001473 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001474
1475 dso__set_build_id(dso, &bev->build_id);
1476
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001477 if (dso_type != DSO_TYPE_USER) {
1478 struct kmod_path m = { .name = NULL, };
1479
1480 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09001481 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001482 else
1483 dso->kernel = dso_type;
1484
1485 free(m.name);
1486 }
Robert Richter08d95bd2012-02-10 15:41:55 +01001487
1488 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1489 sbuild_id);
1490 pr_debug("build id event received for %s: %s\n",
1491 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001492 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001493 }
1494
1495 err = 0;
1496out:
1497 return err;
1498}
1499
1500static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1501 int input, u64 offset, u64 size)
1502{
1503 struct perf_session *session = container_of(header, struct perf_session, header);
1504 struct {
1505 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001506 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001507 char filename[0];
1508 } old_bev;
1509 struct build_id_event bev;
1510 char filename[PATH_MAX];
1511 u64 limit = offset + size;
1512
1513 while (offset < limit) {
1514 ssize_t len;
1515
Namhyung Kim5323f602012-12-17 15:38:54 +09001516 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001517 return -1;
1518
1519 if (header->needs_swap)
1520 perf_event_header__bswap(&old_bev.header);
1521
1522 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001523 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001524 return -1;
1525
1526 bev.header = old_bev.header;
1527
1528 /*
1529 * As the pid is the missing value, we need to fill
1530 * it properly. The header.misc value give us nice hint.
1531 */
1532 bev.pid = HOST_KERNEL_ID;
1533 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1534 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1535 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1536
1537 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1538 __event_process_build_id(&bev, filename, session);
1539
1540 offset += bev.header.size;
1541 }
1542
1543 return 0;
1544}
1545
1546static int perf_header__read_build_ids(struct perf_header *header,
1547 int input, u64 offset, u64 size)
1548{
1549 struct perf_session *session = container_of(header, struct perf_session, header);
1550 struct build_id_event bev;
1551 char filename[PATH_MAX];
1552 u64 limit = offset + size, orig_offset = offset;
1553 int err = -1;
1554
1555 while (offset < limit) {
1556 ssize_t len;
1557
Namhyung Kim5323f602012-12-17 15:38:54 +09001558 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001559 goto out;
1560
1561 if (header->needs_swap)
1562 perf_event_header__bswap(&bev.header);
1563
1564 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001565 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001566 goto out;
1567 /*
1568 * The a1645ce1 changeset:
1569 *
1570 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1571 *
1572 * Added a field to struct build_id_event that broke the file
1573 * format.
1574 *
1575 * Since the kernel build-id is the first entry, process the
1576 * table using the old format if the well known
1577 * '[kernel.kallsyms]' string for the kernel build-id has the
1578 * first 4 characters chopped off (where the pid_t sits).
1579 */
1580 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1581 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1582 return -1;
1583 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1584 }
1585
1586 __event_process_build_id(&bev, filename, session);
1587
1588 offset += bev.header.size;
1589 }
1590 err = 0;
1591out:
1592 return err;
1593}
1594
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001595/* Macro for features that simply need to read and store a string. */
1596#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001597static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001598{\
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001599 ff->ph->env.__feat_env = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001600 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001601}
1602
1603FEAT_PROCESS_STR_FUN(hostname, hostname);
1604FEAT_PROCESS_STR_FUN(osrelease, os_release);
1605FEAT_PROCESS_STR_FUN(version, version);
1606FEAT_PROCESS_STR_FUN(arch, arch);
1607FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1608FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1609
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001610static int process_tracing_data(struct feat_fd *ff, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001611{
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001612 ssize_t ret = trace_report(ff->fd, data, false);
1613
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001614 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001615}
1616
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001617static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001618{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001619 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
Robert Richterf1c67db2012-02-10 15:41:56 +01001620 pr_debug("Failed to read buildids, continuing...\n");
1621 return 0;
1622}
1623
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001624static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001625{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001626 int ret;
1627 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001628
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001629 ret = do_read_u32(ff, &nr_cpus_avail);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001630 if (ret)
1631 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001632
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001633 ret = do_read_u32(ff, &nr_cpus_online);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001634 if (ret)
1635 return ret;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001636 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
1637 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001638 return 0;
1639}
1640
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001641static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001642{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001643 u64 total_mem;
1644 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001645
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001646 ret = do_read_u64(ff, &total_mem);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001647 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001648 return -1;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001649 ff->ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001650 return 0;
1651}
1652
Robert Richter7c2f7af2012-08-16 21:10:23 +02001653static struct perf_evsel *
1654perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1655{
1656 struct perf_evsel *evsel;
1657
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001658 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001659 if (evsel->idx == idx)
1660 return evsel;
1661 }
1662
1663 return NULL;
1664}
1665
1666static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001667perf_evlist__set_event_name(struct perf_evlist *evlist,
1668 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001669{
1670 struct perf_evsel *evsel;
1671
1672 if (!event->name)
1673 return;
1674
1675 evsel = perf_evlist__find_by_index(evlist, event->idx);
1676 if (!evsel)
1677 return;
1678
1679 if (evsel->name)
1680 return;
1681
1682 evsel->name = strdup(event->name);
1683}
1684
1685static int
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001686process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001687{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001688 struct perf_session *session;
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001689 struct perf_evsel *evsel, *events = read_event_desc(ff);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001690
1691 if (!events)
1692 return 0;
1693
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001694 session = container_of(ff->ph, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001695 for (evsel = events; evsel->attr.size; evsel++)
1696 perf_evlist__set_event_name(session->evlist, evsel);
1697
1698 free_event_desc(events);
1699
1700 return 0;
1701}
1702
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001703static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001704{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001705 char *str, *cmdline = NULL, **argv = NULL;
1706 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001707
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001708 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001709 return -1;
1710
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001711 ff->ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001712
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001713 cmdline = zalloc(ff->size + nr + 1);
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001714 if (!cmdline)
1715 return -1;
1716
1717 argv = zalloc(sizeof(char *) * (nr + 1));
1718 if (!argv)
1719 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001720
1721 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001722 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001723 if (!str)
1724 goto error;
1725
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001726 argv[i] = cmdline + len;
1727 memcpy(argv[i], str, strlen(str) + 1);
1728 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001729 free(str);
1730 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001731 ff->ph->env.cmdline = cmdline;
1732 ff->ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001733 return 0;
1734
1735error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001736 free(argv);
1737 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001738 return -1;
1739}
1740
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001741static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001742{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001743 u32 nr, i;
1744 char *str;
1745 struct strbuf sb;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001746 int cpu_nr = ff->ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04001747 u64 size = 0;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001748 struct perf_header *ph = ff->ph;
Kan Liang2bb00d22015-09-01 09:58:12 -04001749
1750 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1751 if (!ph->env.cpu)
1752 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001753
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001754 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001755 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001756
Namhyung Kima1ae5652012-09-24 17:14:59 +09001757 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001758 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001759 if (strbuf_init(&sb, 128) < 0)
1760 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001761
1762 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001763 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001764 if (!str)
1765 goto error;
1766
1767 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001768 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1769 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001770 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001771 free(str);
1772 }
1773 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1774
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001775 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001776 return -1;
1777
Namhyung Kima1ae5652012-09-24 17:14:59 +09001778 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001779 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001780
1781 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001782 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001783 if (!str)
1784 goto error;
1785
1786 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001787 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1788 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001789 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001790 free(str);
1791 }
1792 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04001793
1794 /*
1795 * The header may be from old perf,
1796 * which doesn't include core id and socket id information.
1797 */
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001798 if (ff->size <= size) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001799 zfree(&ph->env.cpu);
1800 return 0;
1801 }
1802
1803 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001804 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001805 goto free_cpu;
1806
Kan Liang2bb00d22015-09-01 09:58:12 -04001807 ph->env.cpu[i].core_id = nr;
1808
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001809 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001810 goto free_cpu;
1811
Jan Stancekda8a58b2017-02-17 12:10:26 +01001812 if (nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001813 pr_debug("socket_id number is too big."
1814 "You may need to upgrade the perf tool.\n");
1815 goto free_cpu;
1816 }
1817
1818 ph->env.cpu[i].socket_id = nr;
1819 }
1820
Namhyung Kima1ae5652012-09-24 17:14:59 +09001821 return 0;
1822
1823error:
1824 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04001825free_cpu:
1826 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001827 return -1;
1828}
1829
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001830static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001831{
Jiri Olsac60da222016-07-04 14:16:20 +02001832 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02001833 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001834 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001835
1836 /* nr nodes */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001837 if (do_read_u32(ff, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001838 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001839
Jiri Olsac60da222016-07-04 14:16:20 +02001840 nodes = zalloc(sizeof(*nodes) * nr);
1841 if (!nodes)
1842 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001843
1844 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02001845 n = &nodes[i];
1846
Namhyung Kima1ae5652012-09-24 17:14:59 +09001847 /* node number */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001848 if (do_read_u32(ff, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001849 goto error;
1850
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001851 if (do_read_u64(ff, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001852 goto error;
1853
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001854 if (do_read_u64(ff, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001855 goto error;
1856
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001857 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001858 if (!str)
1859 goto error;
1860
Jiri Olsac60da222016-07-04 14:16:20 +02001861 n->map = cpu_map__new(str);
1862 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001863 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02001864
Namhyung Kima1ae5652012-09-24 17:14:59 +09001865 free(str);
1866 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001867 ff->ph->env.nr_numa_nodes = nr;
1868 ff->ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001869 return 0;
1870
1871error:
Jiri Olsac60da222016-07-04 14:16:20 +02001872 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001873 return -1;
1874}
1875
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001876static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001877{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001878 char *name;
1879 u32 pmu_num;
1880 u32 type;
1881 struct strbuf sb;
1882
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001883 if (do_read_u32(ff, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001884 return -1;
1885
Namhyung Kima1ae5652012-09-24 17:14:59 +09001886 if (!pmu_num) {
1887 pr_debug("pmu mappings not available\n");
1888 return 0;
1889 }
1890
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001891 ff->ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001892 if (strbuf_init(&sb, 128) < 0)
1893 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001894
1895 while (pmu_num) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001896 if (do_read_u32(ff, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001897 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001898
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001899 name = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001900 if (!name)
1901 goto error;
1902
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001903 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
1904 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001905 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001906 if (strbuf_add(&sb, "", 1) < 0)
1907 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001908
Kan Liange0838e02015-09-10 11:03:05 -03001909 if (!strcmp(name, "msr"))
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001910 ff->ph->env.msr_pmu_type = type;
Kan Liange0838e02015-09-10 11:03:05 -03001911
Namhyung Kima1ae5652012-09-24 17:14:59 +09001912 free(name);
1913 pmu_num--;
1914 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001915 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001916 return 0;
1917
1918error:
1919 strbuf_release(&sb);
1920 return -1;
1921}
1922
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001923static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001924{
1925 size_t ret = -1;
1926 u32 i, nr, nr_groups;
1927 struct perf_session *session;
1928 struct perf_evsel *evsel, *leader = NULL;
1929 struct group_desc {
1930 char *name;
1931 u32 leader_idx;
1932 u32 nr_members;
1933 } *desc;
1934
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001935 if (do_read_u32(ff, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001936 return -1;
1937
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001938 ff->ph->env.nr_groups = nr_groups;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001939 if (!nr_groups) {
1940 pr_debug("group desc not available\n");
1941 return 0;
1942 }
1943
1944 desc = calloc(nr_groups, sizeof(*desc));
1945 if (!desc)
1946 return -1;
1947
1948 for (i = 0; i < nr_groups; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001949 desc[i].name = do_read_string(ff);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001950 if (!desc[i].name)
1951 goto out_free;
1952
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001953 if (do_read_u32(ff, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001954 goto out_free;
1955
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001956 if (do_read_u32(ff, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001957 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001958 }
1959
1960 /*
1961 * Rebuild group relationship based on the group_desc
1962 */
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001963 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001964 session->evlist->nr_groups = nr_groups;
1965
1966 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001967 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001968 if (evsel->idx == (int) desc[i].leader_idx) {
1969 evsel->leader = evsel;
1970 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09001971 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001972 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09001973 desc[i].name = NULL;
1974 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09001975 evsel->nr_members = desc[i].nr_members;
1976
1977 if (i >= nr_groups || nr > 0) {
1978 pr_debug("invalid group desc\n");
1979 goto out_free;
1980 }
1981
1982 leader = evsel;
1983 nr = evsel->nr_members - 1;
1984 i++;
1985 } else if (nr) {
1986 /* This is a group member */
1987 evsel->leader = leader;
1988
1989 nr--;
1990 }
1991 }
1992
1993 if (i != nr_groups || nr != 0) {
1994 pr_debug("invalid group desc\n");
1995 goto out_free;
1996 }
1997
1998 ret = 0;
1999out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002000 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002001 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002002 free(desc);
2003
2004 return ret;
2005}
2006
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002007static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
Adrian Hunter99fa2982015-04-30 17:37:25 +03002008{
2009 struct perf_session *session;
2010 int err;
2011
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002012 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002013
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002014 err = auxtrace_index__process(ff->fd, ff->size, session,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002015 ff->ph->needs_swap);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002016 if (err < 0)
2017 pr_err("Failed to process auxtrace index\n");
2018 return err;
2019}
2020
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002021static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01002022{
2023 struct cpu_cache_level *caches;
2024 u32 cnt, i, version;
2025
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002026 if (do_read_u32(ff, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002027 return -1;
2028
Jiri Olsa720e98b2016-02-16 16:01:43 +01002029 if (version != 1)
2030 return -1;
2031
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002032 if (do_read_u32(ff, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002033 return -1;
2034
Jiri Olsa720e98b2016-02-16 16:01:43 +01002035 caches = zalloc(sizeof(*caches) * cnt);
2036 if (!caches)
2037 return -1;
2038
2039 for (i = 0; i < cnt; i++) {
2040 struct cpu_cache_level c;
2041
2042 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002043 if (do_read_u32(ff, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002044 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002045
2046 _R(level)
2047 _R(line_size)
2048 _R(sets)
2049 _R(ways)
2050 #undef _R
2051
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002052 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002053 c.v = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002054 if (!c.v) \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002055 goto out_free_caches;
2056
2057 _R(type)
2058 _R(size)
2059 _R(map)
2060 #undef _R
2061
2062 caches[i] = c;
2063 }
2064
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002065 ff->ph->env.caches = caches;
2066 ff->ph->env.caches_cnt = cnt;
Jiri Olsa720e98b2016-02-16 16:01:43 +01002067 return 0;
2068out_free_caches:
2069 free(caches);
2070 return -1;
2071}
2072
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002073struct feature_ops {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002074 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002075 void (*print)(struct feat_fd *ff, FILE *fp);
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002076 int (*process)(struct feat_fd *ff, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002077 const char *name;
2078 bool full_only;
2079};
2080
Robert Richter8cdfa782011-12-07 10:02:56 +01002081#define FEAT_OPA(n, func) \
2082 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002083#define FEAT_OPP(n, func) \
2084 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2085 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002086#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002087 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002088 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002089
2090/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002091#define print_tracing_data NULL
2092#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002093
2094static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002095 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002096 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002097 FEAT_OPP(HEADER_HOSTNAME, hostname),
2098 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2099 FEAT_OPP(HEADER_VERSION, version),
2100 FEAT_OPP(HEADER_ARCH, arch),
2101 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2102 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002103 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002104 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002105 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002106 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002107 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2108 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002109 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002110 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002111 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Adrian Hunter99fa2982015-04-30 17:37:25 +03002112 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
Jiri Olsaffa517a2015-10-25 15:51:43 +01002113 FEAT_OPA(HEADER_STAT, stat),
Jiri Olsa720e98b2016-02-16 16:01:43 +01002114 FEAT_OPF(HEADER_CACHE, cache),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002115};
2116
2117struct header_print_data {
2118 FILE *fp;
2119 bool full; /* extended list of headers */
2120};
2121
2122static int perf_file_section__fprintf_info(struct perf_file_section *section,
2123 struct perf_header *ph,
2124 int feat, int fd, void *data)
2125{
2126 struct header_print_data *hd = data;
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002127 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002128
2129 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2130 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2131 "%d, continuing...\n", section->offset, feat);
2132 return 0;
2133 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002134 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002135 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002136 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002137 }
2138 if (!feat_ops[feat].print)
2139 return 0;
2140
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002141 ff = (struct feat_fd) {
2142 .fd = fd,
2143 .ph = ph,
2144 };
2145
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002146 if (!feat_ops[feat].full_only || hd->full)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002147 feat_ops[feat].print(&ff, hd->fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002148 else
2149 fprintf(hd->fp, "# %s info available, use -I to display\n",
2150 feat_ops[feat].name);
2151
2152 return 0;
2153}
2154
2155int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2156{
2157 struct header_print_data hd;
2158 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002159 int fd = perf_data_file__fd(session->file);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002160 struct stat st;
Jiri Olsaaabae162016-10-10 09:35:50 +02002161 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002162
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002163 hd.fp = fp;
2164 hd.full = full;
2165
Jiri Olsaf45f5612016-10-10 09:03:07 +02002166 ret = fstat(fd, &st);
2167 if (ret == -1)
2168 return -1;
2169
2170 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2171
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002172 perf_header__process_sections(header, fd, &hd,
2173 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002174
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002175 if (session->file->is_pipe)
2176 return 0;
2177
Jiri Olsaaabae162016-10-10 09:35:50 +02002178 fprintf(fp, "# missing features: ");
2179 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2180 if (bit)
2181 fprintf(fp, "%s ", feat_ops[bit].name);
2182 }
2183
2184 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002185 return 0;
2186}
2187
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002188static int do_write_feat(struct feat_fd *ff, int type,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002189 struct perf_file_section **p,
2190 struct perf_evlist *evlist)
2191{
2192 int err;
2193 int ret = 0;
2194
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002195 if (perf_header__has_feat(ff->ph, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002196 if (!feat_ops[type].write)
2197 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002198
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002199 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002200
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002201 err = feat_ops[type].write(ff, evlist);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002202 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002203 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002204
2205 /* undo anything written */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002206 lseek(ff->fd, (*p)->offset, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002207
2208 return -1;
2209 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002210 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002211 (*p)++;
2212 }
2213 return ret;
2214}
2215
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002216static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002217 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002218{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002219 int nr_sections;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002220 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002221 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002222 int sec_size;
2223 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002224 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002225 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002226
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002227 ff = (struct feat_fd){
2228 .fd = fd,
2229 .ph = header,
2230 };
2231
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002232 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002233 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002234 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002235
Paul Gortmaker91b98802013-01-30 20:05:49 -05002236 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002237 if (feat_sec == NULL)
2238 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002239
2240 sec_size = sizeof(*feat_sec) * nr_sections;
2241
Jiri Olsa8d541e92013-07-17 19:49:44 +02002242 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002243 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002244
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002245 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002246 if (do_write_feat(&ff, feat, &p, evlist))
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002247 perf_header__clear_feat(header, feat);
2248 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002249
Xiao Guangrongf887f302010-02-04 16:46:42 +08002250 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002251 /*
2252 * may write more than needed due to dropped feature, but
2253 * this is okay, reader will skip the mising entries
2254 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002255 err = do_write(&ff, feat_sec, sec_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002256 if (err < 0)
2257 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002258 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002259 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002260}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002261
Tom Zanussi8dc58102010-04-01 23:59:15 -05002262int perf_header__write_pipe(int fd)
2263{
2264 struct perf_pipe_file_header f_header;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002265 struct feat_fd ff;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002266 int err;
2267
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002268 ff = (struct feat_fd){ .fd = fd };
2269
Tom Zanussi8dc58102010-04-01 23:59:15 -05002270 f_header = (struct perf_pipe_file_header){
2271 .magic = PERF_MAGIC,
2272 .size = sizeof(f_header),
2273 };
2274
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002275 err = do_write(&ff, &f_header, sizeof(f_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05002276 if (err < 0) {
2277 pr_debug("failed to write perf pipe header\n");
2278 return err;
2279 }
2280
2281 return 0;
2282}
2283
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002284int perf_session__write_header(struct perf_session *session,
2285 struct perf_evlist *evlist,
2286 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002287{
2288 struct perf_file_header f_header;
2289 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002290 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002291 struct perf_evsel *evsel;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002292 struct feat_fd ff;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002293 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002294 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002295
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002296 ff = (struct feat_fd){ .fd = fd};
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002297 lseek(fd, sizeof(f_header), SEEK_SET);
2298
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002299 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002300 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002301 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002302 if (err < 0) {
2303 pr_debug("failed to write perf header\n");
2304 return err;
2305 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002306 }
2307
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002308 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002309
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002310 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002311 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002312 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002313 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002314 .offset = evsel->id_offset,
2315 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002316 }
2317 };
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002318 err = do_write(&ff, &f_attr, sizeof(f_attr));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002319 if (err < 0) {
2320 pr_debug("failed to write perf header attribute\n");
2321 return err;
2322 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002323 }
2324
Adrian Hunterd645c442013-12-11 14:36:28 +02002325 if (!header->data_offset)
2326 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002327 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002328
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002329 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002330 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002331 if (err < 0)
2332 return err;
2333 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002334
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002335 f_header = (struct perf_file_header){
2336 .magic = PERF_MAGIC,
2337 .size = sizeof(f_header),
2338 .attr_size = sizeof(f_attr),
2339 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002340 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002341 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002342 },
2343 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002344 .offset = header->data_offset,
2345 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002346 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002347 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002348 };
2349
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002350 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002351
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002352 lseek(fd, 0, SEEK_SET);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002353 err = do_write(&ff, &f_header, sizeof(f_header));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002354 if (err < 0) {
2355 pr_debug("failed to write perf header\n");
2356 return err;
2357 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002358 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002359
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002360 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002361}
2362
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002363static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002364 int fd, void *buf, size_t size)
2365{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002366 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002367 return -1;
2368
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002369 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002370 mem_bswap_64(buf, size);
2371
2372 return 0;
2373}
2374
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002375int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002376 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002377 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002378 struct perf_header *ph,
2379 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002380{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002381 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002382 int nr_sections;
2383 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002384 int feat;
2385 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002386
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002387 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002388 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002389 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002390
Paul Gortmaker91b98802013-01-30 20:05:49 -05002391 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002392 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002393 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002394
2395 sec_size = sizeof(*feat_sec) * nr_sections;
2396
Jiri Olsa8d541e92013-07-17 19:49:44 +02002397 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002398
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002399 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2400 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002401 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002402
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002403 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2404 err = process(sec++, header, feat, fd, data);
2405 if (err < 0)
2406 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002407 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002408 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002409out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002410 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002411 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002412}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002413
Stephane Eranian114382a2012-02-09 23:21:08 +01002414static const int attr_file_abi_sizes[] = {
2415 [0] = PERF_ATTR_SIZE_VER0,
2416 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002417 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002418 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002419 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002420 0,
2421};
2422
2423/*
2424 * In the legacy file format, the magic number is not used to encode endianness.
2425 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2426 * on ABI revisions, we need to try all combinations for all endianness to
2427 * detect the endianness.
2428 */
2429static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2430{
2431 uint64_t ref_size, attr_size;
2432 int i;
2433
2434 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2435 ref_size = attr_file_abi_sizes[i]
2436 + sizeof(struct perf_file_section);
2437 if (hdr_sz != ref_size) {
2438 attr_size = bswap_64(hdr_sz);
2439 if (attr_size != ref_size)
2440 continue;
2441
2442 ph->needs_swap = true;
2443 }
2444 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2445 i,
2446 ph->needs_swap);
2447 return 0;
2448 }
2449 /* could not determine endianness */
2450 return -1;
2451}
2452
2453#define PERF_PIPE_HDR_VER0 16
2454
2455static const size_t attr_pipe_abi_sizes[] = {
2456 [0] = PERF_PIPE_HDR_VER0,
2457 0,
2458};
2459
2460/*
2461 * In the legacy pipe format, there is an implicit assumption that endiannesss
2462 * between host recording the samples, and host parsing the samples is the
2463 * same. This is not always the case given that the pipe output may always be
2464 * redirected into a file and analyzed on a different machine with possibly a
2465 * different endianness and perf_event ABI revsions in the perf tool itself.
2466 */
2467static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2468{
2469 u64 attr_size;
2470 int i;
2471
2472 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2473 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2474 attr_size = bswap_64(hdr_sz);
2475 if (attr_size != hdr_sz)
2476 continue;
2477
2478 ph->needs_swap = true;
2479 }
2480 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2481 return 0;
2482 }
2483 return -1;
2484}
2485
Feng Tange84ba4e2012-10-30 11:56:07 +08002486bool is_perf_magic(u64 magic)
2487{
2488 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2489 || magic == __perf_magic2
2490 || magic == __perf_magic2_sw)
2491 return true;
2492
2493 return false;
2494}
2495
Stephane Eranian114382a2012-02-09 23:21:08 +01002496static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2497 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002498{
2499 int ret;
2500
2501 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002502 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002503 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002504 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002505 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002506 if (is_pipe)
2507 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002508
Stephane Eranian114382a2012-02-09 23:21:08 +01002509 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002510 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002511 /*
2512 * the new magic number serves two purposes:
2513 * - unique number to identify actual perf.data files
2514 * - encode endianness of file
2515 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002516 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002517
Stephane Eranian114382a2012-02-09 23:21:08 +01002518 /* check magic number with one endianness */
2519 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002520 return 0;
2521
Stephane Eranian114382a2012-02-09 23:21:08 +01002522 /* check magic number with opposite endianness */
2523 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002524 return -1;
2525
2526 ph->needs_swap = true;
2527
2528 return 0;
2529}
2530
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002531int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002532 struct perf_header *ph, int fd)
2533{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002534 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002535
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002536 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002537
Stephane Eranian73323f52012-02-02 13:54:44 +01002538 ret = readn(fd, header, sizeof(*header));
2539 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002540 return -1;
2541
Stephane Eranian114382a2012-02-09 23:21:08 +01002542 if (check_magic_endian(header->magic,
2543 header->attr_size, false, ph) < 0) {
2544 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002545 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002546 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002547
Stephane Eranian73323f52012-02-02 13:54:44 +01002548 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002549 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002550 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002551 }
2552
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002553 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002554 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002555 if (header->size == offsetof(typeof(*header), adds_features))
2556 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002557 else
2558 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002559 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002560 /*
2561 * feature bitmap is declared as an array of unsigned longs --
2562 * not good since its size can differ between the host that
2563 * generated the data file and the host analyzing the file.
2564 *
2565 * We need to handle endianness, but we don't know the size of
2566 * the unsigned long where the file was generated. Take a best
2567 * guess at determining it: try 64-bit swap first (ie., file
2568 * created on a 64-bit host), and check if the hostname feature
2569 * bit is set (this feature bit is forced on as of fbe96f2).
2570 * If the bit is not, undo the 64-bit swap and try a 32-bit
2571 * swap. If the hostname bit is still not set (e.g., older data
2572 * file), punt and fallback to the original behavior --
2573 * clearing all feature bits and setting buildid.
2574 */
David Ahern80c01202012-06-08 11:47:51 -03002575 mem_bswap_64(&header->adds_features,
2576 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002577
2578 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002579 /* unswap as u64 */
2580 mem_bswap_64(&header->adds_features,
2581 BITS_TO_U64(HEADER_FEAT_BITS));
2582
2583 /* unswap as u32 */
2584 mem_bswap_32(&header->adds_features,
2585 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002586 }
2587
2588 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2589 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2590 set_bit(HEADER_BUILD_ID, header->adds_features);
2591 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002592 }
2593
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002594 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002595 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002596
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002597 ph->data_offset = header->data.offset;
2598 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002599 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002600 return 0;
2601}
2602
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002603static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002604 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002605 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002606{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002607 struct feat_fd fdd = {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002608 .fd = fd,
2609 .ph = ph,
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002610 .size = section->size,
2611 .offset = section->offset,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002612 };
2613
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002614 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002615 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002616 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002617 return 0;
2618 }
2619
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002620 if (feat >= HEADER_LAST_FEATURE) {
2621 pr_debug("unknown feature %d, continuing...\n", feat);
2622 return 0;
2623 }
2624
Robert Richterf1c67db2012-02-10 15:41:56 +01002625 if (!feat_ops[feat].process)
2626 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002627
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002628 return feat_ops[feat].process(&fdd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002629}
2630
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002631static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002632 struct perf_header *ph, int fd,
2633 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002634{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002635 struct feat_fd ff = {
2636 .fd = STDOUT_FILENO,
2637 .ph = ph,
2638 };
Jiri Olsa727ebd52013-11-28 11:30:14 +01002639 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002640
2641 ret = readn(fd, header, sizeof(*header));
2642 if (ret <= 0)
2643 return -1;
2644
Stephane Eranian114382a2012-02-09 23:21:08 +01002645 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2646 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002647 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002648 }
2649
2650 if (ph->needs_swap)
2651 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002652
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002653 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002654 return -1;
2655
Tom Zanussi8dc58102010-04-01 23:59:15 -05002656 return 0;
2657}
2658
Jiri Olsad4339562013-07-17 19:49:41 +02002659static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002660{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002661 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002662 struct perf_pipe_file_header f_header;
2663
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002664 if (perf_file_header__read_pipe(&f_header, header,
2665 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002666 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002667 pr_debug("incompatible file format\n");
2668 return -EINVAL;
2669 }
2670
Tom Zanussi8dc58102010-04-01 23:59:15 -05002671 return 0;
2672}
2673
Stephane Eranian69996df2012-02-09 23:21:06 +01002674static int read_attr(int fd, struct perf_header *ph,
2675 struct perf_file_attr *f_attr)
2676{
2677 struct perf_event_attr *attr = &f_attr->attr;
2678 size_t sz, left;
2679 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002680 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002681
2682 memset(f_attr, 0, sizeof(*f_attr));
2683
2684 /* read minimal guaranteed structure */
2685 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2686 if (ret <= 0) {
2687 pr_debug("cannot read %d bytes of header attr\n",
2688 PERF_ATTR_SIZE_VER0);
2689 return -1;
2690 }
2691
2692 /* on file perf_event_attr size */
2693 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002694
Stephane Eranian69996df2012-02-09 23:21:06 +01002695 if (ph->needs_swap)
2696 sz = bswap_32(sz);
2697
2698 if (sz == 0) {
2699 /* assume ABI0 */
2700 sz = PERF_ATTR_SIZE_VER0;
2701 } else if (sz > our_sz) {
2702 pr_debug("file uses a more recent and unsupported ABI"
2703 " (%zu bytes extra)\n", sz - our_sz);
2704 return -1;
2705 }
2706 /* what we have not yet read and that we know about */
2707 left = sz - PERF_ATTR_SIZE_VER0;
2708 if (left) {
2709 void *ptr = attr;
2710 ptr += PERF_ATTR_SIZE_VER0;
2711
2712 ret = readn(fd, ptr, left);
2713 }
2714 /* read perf_file_section, ids are read in caller */
2715 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2716
2717 return ret <= 0 ? -1 : 0;
2718}
2719
Namhyung Kim831394b2012-09-06 11:10:46 +09002720static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2721 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002722{
Namhyung Kim831394b2012-09-06 11:10:46 +09002723 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002724 char bf[128];
2725
Namhyung Kim831394b2012-09-06 11:10:46 +09002726 /* already prepared */
2727 if (evsel->tp_format)
2728 return 0;
2729
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002730 if (pevent == NULL) {
2731 pr_debug("broken or missing trace data\n");
2732 return -1;
2733 }
2734
Namhyung Kim831394b2012-09-06 11:10:46 +09002735 event = pevent_find_event(pevent, evsel->attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09002736 if (event == NULL) {
2737 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002738 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09002739 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002740
Namhyung Kim831394b2012-09-06 11:10:46 +09002741 if (!evsel->name) {
2742 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2743 evsel->name = strdup(bf);
2744 if (evsel->name == NULL)
2745 return -1;
2746 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002747
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002748 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002749 return 0;
2750}
2751
Namhyung Kim831394b2012-09-06 11:10:46 +09002752static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2753 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002754{
2755 struct perf_evsel *pos;
2756
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002757 evlist__for_each_entry(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002758 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2759 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002760 return -1;
2761 }
2762
2763 return 0;
2764}
2765
Jiri Olsad4339562013-07-17 19:49:41 +02002766int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002767{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002768 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002769 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002770 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002771 struct perf_file_attr f_attr;
2772 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002773 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002774 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002775
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002776 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002777 if (session->evlist == NULL)
2778 return -ENOMEM;
2779
Kan Liang2c071442015-08-28 05:48:05 -04002780 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03002781 session->machines.host.env = &header->env;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002782 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002783 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002784
Stephane Eranian69996df2012-02-09 23:21:06 +01002785 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002786 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002787
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002788 /*
2789 * Sanity check that perf.data was written cleanly; data size is
2790 * initialized to 0 and updated only if the on_exit function is run.
2791 * If data size is still 0 then the file contains only partial
2792 * information. Just warn user and process it as much as it can.
2793 */
2794 if (f_header.data.size == 0) {
2795 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2796 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002797 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002798 }
2799
Stephane Eranian69996df2012-02-09 23:21:06 +01002800 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002801 lseek(fd, f_header.attrs.offset, SEEK_SET);
2802
2803 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002804 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002805 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002806
Stephane Eranian69996df2012-02-09 23:21:06 +01002807 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002808 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002809
David Ahern1060ab82015-04-09 16:15:46 -04002810 if (header->needs_swap) {
2811 f_attr.ids.size = bswap_64(f_attr.ids.size);
2812 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002813 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002814 }
David Aherneda39132011-07-15 12:34:09 -06002815
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002816 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002817 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002818
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002819 if (evsel == NULL)
2820 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002821
2822 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002823 /*
2824 * Do it before so that if perf_evsel__alloc_id fails, this
2825 * entry gets purged too at perf_evlist__delete().
2826 */
2827 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002828
2829 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002830 /*
2831 * We don't have the cpu and thread maps on the header, so
2832 * for allocating the perf_sample_id table we fake 1 cpu and
2833 * hattr->ids threads.
2834 */
2835 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2836 goto out_delete_evlist;
2837
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002838 lseek(fd, f_attr.ids.offset, SEEK_SET);
2839
2840 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002841 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002842 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002843
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002844 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002845 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002846
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002847 lseek(fd, tmp, SEEK_SET);
2848 }
2849
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002850 symbol_conf.nr_events = nr_attrs;
2851
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002852 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002853 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002854
Namhyung Kim831394b2012-09-06 11:10:46 +09002855 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002856 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002857 goto out_delete_evlist;
2858
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002859 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002860out_errno:
2861 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002862
2863out_delete_evlist:
2864 perf_evlist__delete(session->evlist);
2865 session->evlist = NULL;
2866 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002867}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002868
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002869int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002870 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002871 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002872{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002873 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002874 size_t size;
2875 int err;
2876
2877 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002878 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002879 size += sizeof(struct perf_event_header);
2880 size += ids * sizeof(u64);
2881
2882 ev = malloc(size);
2883
Chris Samuelce47dc52010-11-13 13:35:06 +11002884 if (ev == NULL)
2885 return -ENOMEM;
2886
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002887 ev->attr.attr = *attr;
2888 memcpy(ev->attr.id, id, ids * sizeof(u64));
2889
2890 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002891 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002892
Robert Richterf4d83432012-08-16 21:10:17 +02002893 if (ev->attr.header.size == size)
2894 err = process(tool, ev, NULL, NULL);
2895 else
2896 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002897
2898 free(ev);
2899
2900 return err;
2901}
2902
Jiri Olsaa6e52812015-10-25 15:51:37 +01002903static struct event_update_event *
2904event_update_event__new(size_t size, u64 type, u64 id)
2905{
2906 struct event_update_event *ev;
2907
2908 size += sizeof(*ev);
2909 size = PERF_ALIGN(size, sizeof(u64));
2910
2911 ev = zalloc(size);
2912 if (ev) {
2913 ev->header.type = PERF_RECORD_EVENT_UPDATE;
2914 ev->header.size = (u16)size;
2915 ev->type = type;
2916 ev->id = id;
2917 }
2918 return ev;
2919}
2920
2921int
2922perf_event__synthesize_event_update_unit(struct perf_tool *tool,
2923 struct perf_evsel *evsel,
2924 perf_event__handler_t process)
2925{
2926 struct event_update_event *ev;
2927 size_t size = strlen(evsel->unit);
2928 int err;
2929
2930 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
2931 if (ev == NULL)
2932 return -ENOMEM;
2933
2934 strncpy(ev->data, evsel->unit, size);
2935 err = process(tool, (union perf_event *)ev, NULL, NULL);
2936 free(ev);
2937 return err;
2938}
2939
Jiri Olsadaeecbc2015-10-25 15:51:38 +01002940int
2941perf_event__synthesize_event_update_scale(struct perf_tool *tool,
2942 struct perf_evsel *evsel,
2943 perf_event__handler_t process)
2944{
2945 struct event_update_event *ev;
2946 struct event_update_event_scale *ev_data;
2947 int err;
2948
2949 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
2950 if (ev == NULL)
2951 return -ENOMEM;
2952
2953 ev_data = (struct event_update_event_scale *) ev->data;
2954 ev_data->scale = evsel->scale;
2955 err = process(tool, (union perf_event*) ev, NULL, NULL);
2956 free(ev);
2957 return err;
2958}
2959
Jiri Olsa802c9042015-10-25 15:51:39 +01002960int
2961perf_event__synthesize_event_update_name(struct perf_tool *tool,
2962 struct perf_evsel *evsel,
2963 perf_event__handler_t process)
2964{
2965 struct event_update_event *ev;
2966 size_t len = strlen(evsel->name);
2967 int err;
2968
2969 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
2970 if (ev == NULL)
2971 return -ENOMEM;
2972
2973 strncpy(ev->data, evsel->name, len);
2974 err = process(tool, (union perf_event*) ev, NULL, NULL);
2975 free(ev);
2976 return err;
2977}
Jiri Olsadaeecbc2015-10-25 15:51:38 +01002978
Jiri Olsa86ebb092015-10-25 15:51:40 +01002979int
2980perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
2981 struct perf_evsel *evsel,
2982 perf_event__handler_t process)
2983{
2984 size_t size = sizeof(struct event_update_event);
2985 struct event_update_event *ev;
2986 int max, err;
2987 u16 type;
2988
2989 if (!evsel->own_cpus)
2990 return 0;
2991
2992 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
2993 if (!ev)
2994 return -ENOMEM;
2995
2996 ev->header.type = PERF_RECORD_EVENT_UPDATE;
2997 ev->header.size = (u16)size;
2998 ev->type = PERF_EVENT_UPDATE__CPUS;
2999 ev->id = evsel->id[0];
3000
3001 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3002 evsel->own_cpus,
3003 type, max);
3004
3005 err = process(tool, (union perf_event*) ev, NULL, NULL);
3006 free(ev);
3007 return err;
3008}
3009
Jiri Olsac853f932015-10-25 15:51:41 +01003010size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3011{
3012 struct event_update_event *ev = &event->event_update;
3013 struct event_update_event_scale *ev_scale;
3014 struct event_update_event_cpus *ev_cpus;
3015 struct cpu_map *map;
3016 size_t ret;
3017
3018 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3019
3020 switch (ev->type) {
3021 case PERF_EVENT_UPDATE__SCALE:
3022 ev_scale = (struct event_update_event_scale *) ev->data;
3023 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3024 break;
3025 case PERF_EVENT_UPDATE__UNIT:
3026 ret += fprintf(fp, "... unit: %s\n", ev->data);
3027 break;
3028 case PERF_EVENT_UPDATE__NAME:
3029 ret += fprintf(fp, "... name: %s\n", ev->data);
3030 break;
3031 case PERF_EVENT_UPDATE__CPUS:
3032 ev_cpus = (struct event_update_event_cpus *) ev->data;
3033 ret += fprintf(fp, "... ");
3034
3035 map = cpu_map__new_data(&ev_cpus->cpus);
3036 if (map)
3037 ret += cpu_map__fprintf(map, fp);
3038 else
3039 ret += fprintf(fp, "failed to get cpus\n");
3040 break;
3041 default:
3042 ret += fprintf(fp, "... unknown type\n");
3043 break;
3044 }
3045
3046 return ret;
3047}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003048
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003049int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003050 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003051 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003052{
Robert Richter6606f872012-08-16 21:10:19 +02003053 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003054 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003055
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003056 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003057 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3058 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003059 if (err) {
3060 pr_debug("failed to create perf header attribute\n");
3061 return err;
3062 }
3063 }
3064
3065 return err;
3066}
3067
Adrian Hunter47c3d102013-07-04 16:20:21 +03003068int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3069 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003070 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003071{
Robert Richterf4d83432012-08-16 21:10:17 +02003072 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003073 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003074 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003075
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003076 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003077 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003078 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003079 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003080 }
3081
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003082 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003083 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003084 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003085
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003086 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003087
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003088 ids = event->header.size;
3089 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003090 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003091 /*
3092 * We don't have the cpu and thread maps on the header, so
3093 * for allocating the perf_sample_id table we fake 1 cpu and
3094 * hattr->ids threads.
3095 */
3096 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3097 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003098
3099 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003100 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003101 }
3102
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03003103 symbol_conf.nr_events = evlist->nr_entries;
3104
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003105 return 0;
3106}
Tom Zanussicd19a032010-04-01 23:59:20 -05003107
Jiri Olsaffe777252015-10-25 15:51:36 +01003108int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3109 union perf_event *event,
3110 struct perf_evlist **pevlist)
3111{
3112 struct event_update_event *ev = &event->event_update;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003113 struct event_update_event_scale *ev_scale;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003114 struct event_update_event_cpus *ev_cpus;
Jiri Olsaffe777252015-10-25 15:51:36 +01003115 struct perf_evlist *evlist;
3116 struct perf_evsel *evsel;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003117 struct cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003118
3119 if (!pevlist || *pevlist == NULL)
3120 return -EINVAL;
3121
3122 evlist = *pevlist;
3123
3124 evsel = perf_evlist__id2evsel(evlist, ev->id);
3125 if (evsel == NULL)
3126 return -EINVAL;
3127
Jiri Olsaa6e52812015-10-25 15:51:37 +01003128 switch (ev->type) {
3129 case PERF_EVENT_UPDATE__UNIT:
3130 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003131 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003132 case PERF_EVENT_UPDATE__NAME:
3133 evsel->name = strdup(ev->data);
3134 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003135 case PERF_EVENT_UPDATE__SCALE:
3136 ev_scale = (struct event_update_event_scale *) ev->data;
3137 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003138 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003139 case PERF_EVENT_UPDATE__CPUS:
3140 ev_cpus = (struct event_update_event_cpus *) ev->data;
3141
3142 map = cpu_map__new_data(&ev_cpus->cpus);
3143 if (map)
3144 evsel->own_cpus = map;
3145 else
3146 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003147 default:
3148 break;
3149 }
3150
Jiri Olsaffe777252015-10-25 15:51:36 +01003151 return 0;
3152}
3153
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003154int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003155 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003156 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003157{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003158 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003159 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003160 ssize_t size = 0, aligned_size = 0, padding;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003161 struct feat_fd ff;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003162 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003163
Jiri Olsa29208e52011-10-20 15:59:43 +02003164 /*
3165 * We are going to store the size of the data followed
3166 * by the data contents. Since the fd descriptor is a pipe,
3167 * we cannot seek back to store the size of the data once
3168 * we know it. Instead we:
3169 *
3170 * - write the tracing data to the temp file
3171 * - get/write the data size to pipe
3172 * - write the tracing data from the temp file
3173 * to the pipe
3174 */
3175 tdata = tracing_data_get(&evlist->entries, fd, true);
3176 if (!tdata)
3177 return -1;
3178
Tom Zanussi92155452010-04-01 23:59:21 -05003179 memset(&ev, 0, sizeof(ev));
3180
3181 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003182 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003183 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003184 padding = aligned_size - size;
3185 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3186 ev.tracing_data.size = aligned_size;
3187
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003188 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003189
Jiri Olsa29208e52011-10-20 15:59:43 +02003190 /*
3191 * The put function will copy all the tracing data
3192 * stored in temp file to the pipe.
3193 */
3194 tracing_data_put(tdata);
3195
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003196 ff = (struct feat_fd){ .fd = fd };
3197 if (write_padded(&ff, NULL, 0, padding))
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -07003198 return -1;
Tom Zanussi92155452010-04-01 23:59:21 -05003199
3200 return aligned_size;
3201}
3202
Adrian Hunter47c3d102013-07-04 16:20:21 +03003203int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3204 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003205 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05003206{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003207 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003208 int fd = perf_data_file__fd(session->file);
3209 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003210 char buf[BUFSIZ];
3211
3212 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003213 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003214 SEEK_SET);
3215
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003216 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003217 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003218 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003219
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003220 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003221 pr_err("%s: reading input file", __func__);
3222 return -1;
3223 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003224 if (session->repipe) {
3225 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003226 if (retw <= 0 || retw != padding) {
3227 pr_err("%s: repiping tracing data padding", __func__);
3228 return -1;
3229 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003230 }
Tom Zanussi92155452010-04-01 23:59:21 -05003231
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003232 if (size_read + padding != size) {
3233 pr_err("%s: tracing data size mismatch", __func__);
3234 return -1;
3235 }
Tom Zanussi92155452010-04-01 23:59:21 -05003236
Namhyung Kim831394b2012-09-06 11:10:46 +09003237 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003238 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003239
Tom Zanussi92155452010-04-01 23:59:21 -05003240 return size_read + padding;
3241}
Tom Zanussic7929e42010-04-01 23:59:22 -05003242
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003243int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003244 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003245 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003246 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003247{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003248 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003249 size_t len;
3250 int err = 0;
3251
3252 if (!pos->hit)
3253 return err;
3254
3255 memset(&ev, 0, sizeof(ev));
3256
3257 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003258 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003259 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3260 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3261 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003262 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003263 ev.build_id.header.size = sizeof(ev.build_id) + len;
3264 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3265
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003266 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003267
3268 return err;
3269}
3270
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003271int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003272 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003273 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003274{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003275 __event_process_build_id(&event->build_id,
3276 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003277 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003278 return 0;
3279}