blob: d5359e3384e33eb28159fc3c11245d45edf2a803 [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-Cisnerosa02c3952017-07-17 21:25:44 -0700800 u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900801 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200802
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700803 /*
804 * Do a first pass to count number of pmu to avoid lseek so this
805 * works in pipe mode as well.
806 */
807 while ((pmu = perf_pmu__scan(pmu))) {
808 if (!pmu->name)
809 continue;
810 pmu_num++;
811 }
812
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700813 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
Namhyung Kim5323f602012-12-17 15:38:54 +0900814 if (ret < 0)
815 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200816
817 while ((pmu = perf_pmu__scan(pmu))) {
818 if (!pmu->name)
819 continue;
Namhyung Kim5323f602012-12-17 15:38:54 +0900820
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700821 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
Namhyung Kim5323f602012-12-17 15:38:54 +0900822 if (ret < 0)
823 return ret;
824
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700825 ret = do_write_string(ff, pmu->name);
Namhyung Kim5323f602012-12-17 15:38:54 +0900826 if (ret < 0)
827 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200828 }
829
Robert Richter50a96672012-08-16 21:10:24 +0200830 return 0;
831}
832
833/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900834 * File format:
835 *
836 * struct group_descs {
837 * u32 nr_groups;
838 * struct group_desc {
839 * char name[];
840 * u32 leader_idx;
841 * u32 nr_members;
842 * }[nr_groups];
843 * };
844 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700845static int write_group_desc(struct feat_fd *ff,
Namhyung Kima8bb5592013-01-22 18:09:31 +0900846 struct perf_evlist *evlist)
847{
848 u32 nr_groups = evlist->nr_groups;
849 struct perf_evsel *evsel;
850 int ret;
851
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700852 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900853 if (ret < 0)
854 return ret;
855
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300856 evlist__for_each_entry(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900857 if (perf_evsel__is_group_leader(evsel) &&
858 evsel->nr_members > 1) {
859 const char *name = evsel->group_name ?: "{anon_group}";
860 u32 leader_idx = evsel->idx;
861 u32 nr_members = evsel->nr_members;
862
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700863 ret = do_write_string(ff, name);
Namhyung Kima8bb5592013-01-22 18:09:31 +0900864 if (ret < 0)
865 return ret;
866
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700867 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900868 if (ret < 0)
869 return ret;
870
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700871 ret = do_write(ff, &nr_members, sizeof(nr_members));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900872 if (ret < 0)
873 return ret;
874 }
875 }
876 return 0;
877}
878
879/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200880 * default get_cpuid(): nothing gets recorded
Jiada Wang7a759cd2017-04-09 20:02:37 -0700881 * actual implementation must be in arch/$(SRCARCH)/util/header.c
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200882 */
Rui Teng11d8f872016-07-28 10:05:57 +0800883int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200884{
885 return -1;
886}
887
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700888static int write_cpuid(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300889 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200890{
891 char buffer[64];
892 int ret;
893
894 ret = get_cpuid(buffer, sizeof(buffer));
895 if (!ret)
896 goto write_it;
897
898 return -1;
899write_it:
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700900 return do_write_string(ff, buffer);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200901}
902
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700903static int write_branch_stack(struct feat_fd *ff __maybe_unused,
904 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100905{
906 return 0;
907}
908
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700909static int write_auxtrace(struct feat_fd *ff,
Adrian Hunter4025ea42015-04-09 18:53:41 +0300910 struct perf_evlist *evlist __maybe_unused)
911{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300912 struct perf_session *session;
913 int err;
914
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700915 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300916
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700917 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300918 if (err < 0)
919 pr_err("Failed to write auxtrace index\n");
920 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300921}
922
Jiri Olsa720e98b2016-02-16 16:01:43 +0100923static int cpu_cache_level__sort(const void *a, const void *b)
924{
925 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
926 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
927
928 return cache_a->level - cache_b->level;
929}
930
931static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
932{
933 if (a->level != b->level)
934 return false;
935
936 if (a->line_size != b->line_size)
937 return false;
938
939 if (a->sets != b->sets)
940 return false;
941
942 if (a->ways != b->ways)
943 return false;
944
945 if (strcmp(a->type, b->type))
946 return false;
947
948 if (strcmp(a->size, b->size))
949 return false;
950
951 if (strcmp(a->map, b->map))
952 return false;
953
954 return true;
955}
956
957static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
958{
959 char path[PATH_MAX], file[PATH_MAX];
960 struct stat st;
961 size_t len;
962
963 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
964 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
965
966 if (stat(file, &st))
967 return 1;
968
969 scnprintf(file, PATH_MAX, "%s/level", path);
970 if (sysfs__read_int(file, (int *) &cache->level))
971 return -1;
972
973 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
974 if (sysfs__read_int(file, (int *) &cache->line_size))
975 return -1;
976
977 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
978 if (sysfs__read_int(file, (int *) &cache->sets))
979 return -1;
980
981 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
982 if (sysfs__read_int(file, (int *) &cache->ways))
983 return -1;
984
985 scnprintf(file, PATH_MAX, "%s/type", path);
986 if (sysfs__read_str(file, &cache->type, &len))
987 return -1;
988
989 cache->type[len] = 0;
990 cache->type = rtrim(cache->type);
991
992 scnprintf(file, PATH_MAX, "%s/size", path);
993 if (sysfs__read_str(file, &cache->size, &len)) {
994 free(cache->type);
995 return -1;
996 }
997
998 cache->size[len] = 0;
999 cache->size = rtrim(cache->size);
1000
1001 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1002 if (sysfs__read_str(file, &cache->map, &len)) {
1003 free(cache->map);
1004 free(cache->type);
1005 return -1;
1006 }
1007
1008 cache->map[len] = 0;
1009 cache->map = rtrim(cache->map);
1010 return 0;
1011}
1012
1013static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1014{
1015 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1016}
1017
1018static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1019{
1020 u32 i, cnt = 0;
1021 long ncpus;
1022 u32 nr, cpu;
1023 u16 level;
1024
1025 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1026 if (ncpus < 0)
1027 return -1;
1028
1029 nr = (u32)(ncpus & UINT_MAX);
1030
1031 for (cpu = 0; cpu < nr; cpu++) {
1032 for (level = 0; level < 10; level++) {
1033 struct cpu_cache_level c;
1034 int err;
1035
1036 err = cpu_cache_level__read(&c, cpu, level);
1037 if (err < 0)
1038 return err;
1039
1040 if (err == 1)
1041 break;
1042
1043 for (i = 0; i < cnt; i++) {
1044 if (cpu_cache_level__cmp(&c, &caches[i]))
1045 break;
1046 }
1047
1048 if (i == cnt)
1049 caches[cnt++] = c;
1050 else
1051 cpu_cache_level__free(&c);
1052
1053 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1054 goto out;
1055 }
1056 }
1057 out:
1058 *cntp = cnt;
1059 return 0;
1060}
1061
1062#define MAX_CACHES 2000
1063
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001064static int write_cache(struct feat_fd *ff,
1065 struct perf_evlist *evlist __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001066{
1067 struct cpu_cache_level caches[MAX_CACHES];
1068 u32 cnt = 0, i, version = 1;
1069 int ret;
1070
1071 ret = build_caches(caches, MAX_CACHES, &cnt);
1072 if (ret)
1073 goto out;
1074
1075 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1076
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001077 ret = do_write(ff, &version, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001078 if (ret < 0)
1079 goto out;
1080
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001081 ret = do_write(ff, &cnt, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001082 if (ret < 0)
1083 goto out;
1084
1085 for (i = 0; i < cnt; i++) {
1086 struct cpu_cache_level *c = &caches[i];
1087
1088 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001089 ret = do_write(ff, &c->v, sizeof(u32)); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001090 if (ret < 0) \
1091 goto out;
1092
1093 _W(level)
1094 _W(line_size)
1095 _W(sets)
1096 _W(ways)
1097 #undef _W
1098
1099 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001100 ret = do_write_string(ff, (const char *) c->v); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001101 if (ret < 0) \
1102 goto out;
1103
1104 _W(type)
1105 _W(size)
1106 _W(map)
1107 #undef _W
1108 }
1109
1110out:
1111 for (i = 0; i < cnt; i++)
1112 cpu_cache_level__free(&caches[i]);
1113 return ret;
1114}
1115
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001116static int write_stat(struct feat_fd *ff __maybe_unused,
Jiri Olsaffa517a2015-10-25 15:51:43 +01001117 struct perf_evlist *evlist __maybe_unused)
1118{
1119 return 0;
1120}
1121
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001122static void print_hostname(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001123{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001124 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001125}
1126
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001127static void print_osrelease(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001128{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001129 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001130}
1131
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001132static void print_arch(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001133{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001134 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001135}
1136
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001137static void print_cpudesc(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001138{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001139 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001140}
1141
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001142static void print_nrcpus(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001143{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001144 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1145 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001146}
1147
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001148static void print_version(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001149{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001150 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001151}
1152
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001153static void print_cmdline(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001154{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001155 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001156
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001157 nr = ff->ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001158
1159 fprintf(fp, "# cmdline : ");
1160
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001161 for (i = 0; i < nr; i++)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001162 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001163 fputc('\n', fp);
1164}
1165
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001166static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001167{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001168 struct perf_header *ph = ff->ph;
1169 int cpu_nr = ph->env.nr_cpus_avail;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001170 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001171 char *str;
1172
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001173 nr = ph->env.nr_sibling_cores;
1174 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001175
1176 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001177 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001178 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001179 }
1180
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001181 nr = ph->env.nr_sibling_threads;
1182 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001183
1184 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001185 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001186 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001187 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001188
1189 if (ph->env.cpu != NULL) {
1190 for (i = 0; i < cpu_nr; i++)
1191 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1192 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1193 } else
1194 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001195}
1196
Robert Richter4e1b9c62012-08-16 21:10:22 +02001197static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001198{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001199 struct perf_evsel *evsel;
1200
1201 if (!events)
1202 return;
1203
1204 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001205 zfree(&evsel->name);
1206 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001207 }
1208
1209 free(events);
1210}
1211
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001212static struct perf_evsel *read_event_desc(struct feat_fd *ff)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001213{
1214 struct perf_evsel *evsel, *events = NULL;
1215 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001216 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001217 u32 nre, sz, nr, i, j;
Stephane Eranian62db9062012-02-09 23:21:07 +01001218 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001219
1220 /* number of events */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001221 if (do_read_u32(ff, &nre))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001222 goto error;
1223
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001224 if (do_read_u32(ff, &sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001225 goto error;
1226
Stephane Eranian62db9062012-02-09 23:21:07 +01001227 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001228 buf = malloc(sz);
1229 if (!buf)
1230 goto error;
1231
Robert Richter4e1b9c62012-08-16 21:10:22 +02001232 /* the last event terminates with evsel->attr.size == 0: */
1233 events = calloc(nre + 1, sizeof(*events));
1234 if (!events)
1235 goto error;
1236
1237 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001238 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001239 msz = sz;
1240
Robert Richter4e1b9c62012-08-16 21:10:22 +02001241 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1242 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001243
Stephane Eranian62db9062012-02-09 23:21:07 +01001244 /*
1245 * must read entire on-file attr struct to
1246 * sync up with layout.
1247 */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001248 if (__do_read(ff, buf, sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001249 goto error;
1250
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001251 if (ff->ph->needs_swap)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001252 perf_event__attr_swap(buf);
1253
Robert Richter4e1b9c62012-08-16 21:10:22 +02001254 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001255
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001256 if (do_read_u32(ff, &nr))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001257 goto error;
1258
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001259 if (ff->ph->needs_swap)
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001260 evsel->needs_swap = true;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001261
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001262 evsel->name = do_read_string(ff);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001263 if (!evsel->name)
1264 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001265
1266 if (!nr)
1267 continue;
1268
1269 id = calloc(nr, sizeof(*id));
1270 if (!id)
1271 goto error;
1272 evsel->ids = nr;
1273 evsel->id = id;
1274
1275 for (j = 0 ; j < nr; j++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001276 if (do_read_u64(ff, id))
Robert Richter4e1b9c62012-08-16 21:10:22 +02001277 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001278 id++;
1279 }
1280 }
1281out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001282 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001283 return events;
1284error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001285 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001286 events = NULL;
1287 goto out;
1288}
1289
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001290static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001291 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001292{
1293 return fprintf(fp, ", %s = %s", name, val);
1294}
1295
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001296static void print_event_desc(struct feat_fd *ff, FILE *fp)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001297{
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001298 struct perf_evsel *evsel, *events = read_event_desc(ff);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001299 u32 j;
1300 u64 *id;
1301
1302 if (!events) {
1303 fprintf(fp, "# event desc: not available or unable to read\n");
1304 return;
1305 }
1306
1307 for (evsel = events; evsel->attr.size; evsel++) {
1308 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001309
Robert Richter4e1b9c62012-08-16 21:10:22 +02001310 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001311 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001312 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1313 if (j)
1314 fputc(',', fp);
1315 fprintf(fp, " %"PRIu64, *id);
1316 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001317 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001318 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001319
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001320 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001321
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001322 fputc('\n', fp);
1323 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001324
1325 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001326}
1327
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001328static void print_total_mem(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001329{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001330 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001331}
1332
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001333static void print_numa_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001334{
Jiri Olsac60da222016-07-04 14:16:20 +02001335 int i;
1336 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001337
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001338 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1339 n = &ff->ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001340
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001341 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1342 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001343 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001344
Jiri Olsac60da222016-07-04 14:16:20 +02001345 fprintf(fp, "# node%u cpu list : ", n->node);
1346 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001347 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001348}
1349
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001350static void print_cpuid(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001351{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001352 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001353}
1354
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001355static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001356{
1357 fprintf(fp, "# contains samples with branch stack\n");
1358}
1359
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001360static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
Adrian Hunter4025ea42015-04-09 18:53:41 +03001361{
1362 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1363}
1364
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001365static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001366{
1367 fprintf(fp, "# contains stat data\n");
1368}
1369
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001370static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001371{
1372 int i;
1373
1374 fprintf(fp, "# CPU cache info:\n");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001375 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
Jiri Olsa720e98b2016-02-16 16:01:43 +01001376 fprintf(fp, "# ");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001377 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001378 }
1379}
1380
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001381static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001382{
1383 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001384 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001385 u32 pmu_num;
1386 u32 type;
1387
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001388 pmu_num = ff->ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001389 if (!pmu_num) {
1390 fprintf(fp, "# pmu mappings: not available\n");
1391 return;
1392 }
1393
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001394 str = ff->ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001395
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001396 while (pmu_num) {
1397 type = strtoul(str, &tmp, 0);
1398 if (*tmp != ':')
1399 goto error;
1400
1401 str = tmp + 1;
1402 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1403
Robert Richter50a96672012-08-16 21:10:24 +02001404 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001405 str += strlen(str) + 1;
1406 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001407 }
1408
1409 fprintf(fp, "\n");
1410
1411 if (!pmu_num)
1412 return;
1413error:
1414 fprintf(fp, "# pmu mappings: unable to read\n");
1415}
1416
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001417static void print_group_desc(struct feat_fd *ff, FILE *fp)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001418{
1419 struct perf_session *session;
1420 struct perf_evsel *evsel;
1421 u32 nr = 0;
1422
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001423 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001424
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001425 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001426 if (perf_evsel__is_group_leader(evsel) &&
1427 evsel->nr_members > 1) {
1428 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1429 perf_evsel__name(evsel));
1430
1431 nr = evsel->nr_members - 1;
1432 } else if (nr) {
1433 fprintf(fp, ",%s", perf_evsel__name(evsel));
1434
1435 if (--nr == 0)
1436 fprintf(fp, "}\n");
1437 }
1438 }
1439}
1440
Robert Richter08d95bd2012-02-10 15:41:55 +01001441static int __event_process_build_id(struct build_id_event *bev,
1442 char *filename,
1443 struct perf_session *session)
1444{
1445 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001446 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001447 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001448 struct dso *dso;
1449 enum dso_kernel_type dso_type;
1450
1451 machine = perf_session__findnew_machine(session, bev->pid);
1452 if (!machine)
1453 goto out;
1454
Wang Nan1f121b02015-06-03 08:52:21 +00001455 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001456
Wang Nan1f121b02015-06-03 08:52:21 +00001457 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001458 case PERF_RECORD_MISC_KERNEL:
1459 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001460 break;
1461 case PERF_RECORD_MISC_GUEST_KERNEL:
1462 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001463 break;
1464 case PERF_RECORD_MISC_USER:
1465 case PERF_RECORD_MISC_GUEST_USER:
1466 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001467 break;
1468 default:
1469 goto out;
1470 }
1471
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001472 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001473 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001474 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001475
1476 dso__set_build_id(dso, &bev->build_id);
1477
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001478 if (dso_type != DSO_TYPE_USER) {
1479 struct kmod_path m = { .name = NULL, };
1480
1481 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09001482 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001483 else
1484 dso->kernel = dso_type;
1485
1486 free(m.name);
1487 }
Robert Richter08d95bd2012-02-10 15:41:55 +01001488
1489 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1490 sbuild_id);
1491 pr_debug("build id event received for %s: %s\n",
1492 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001493 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001494 }
1495
1496 err = 0;
1497out:
1498 return err;
1499}
1500
1501static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1502 int input, u64 offset, u64 size)
1503{
1504 struct perf_session *session = container_of(header, struct perf_session, header);
1505 struct {
1506 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001507 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001508 char filename[0];
1509 } old_bev;
1510 struct build_id_event bev;
1511 char filename[PATH_MAX];
1512 u64 limit = offset + size;
1513
1514 while (offset < limit) {
1515 ssize_t len;
1516
Namhyung Kim5323f602012-12-17 15:38:54 +09001517 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001518 return -1;
1519
1520 if (header->needs_swap)
1521 perf_event_header__bswap(&old_bev.header);
1522
1523 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001524 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001525 return -1;
1526
1527 bev.header = old_bev.header;
1528
1529 /*
1530 * As the pid is the missing value, we need to fill
1531 * it properly. The header.misc value give us nice hint.
1532 */
1533 bev.pid = HOST_KERNEL_ID;
1534 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1535 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1536 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1537
1538 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1539 __event_process_build_id(&bev, filename, session);
1540
1541 offset += bev.header.size;
1542 }
1543
1544 return 0;
1545}
1546
1547static int perf_header__read_build_ids(struct perf_header *header,
1548 int input, u64 offset, u64 size)
1549{
1550 struct perf_session *session = container_of(header, struct perf_session, header);
1551 struct build_id_event bev;
1552 char filename[PATH_MAX];
1553 u64 limit = offset + size, orig_offset = offset;
1554 int err = -1;
1555
1556 while (offset < limit) {
1557 ssize_t len;
1558
Namhyung Kim5323f602012-12-17 15:38:54 +09001559 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001560 goto out;
1561
1562 if (header->needs_swap)
1563 perf_event_header__bswap(&bev.header);
1564
1565 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001566 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001567 goto out;
1568 /*
1569 * The a1645ce1 changeset:
1570 *
1571 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1572 *
1573 * Added a field to struct build_id_event that broke the file
1574 * format.
1575 *
1576 * Since the kernel build-id is the first entry, process the
1577 * table using the old format if the well known
1578 * '[kernel.kallsyms]' string for the kernel build-id has the
1579 * first 4 characters chopped off (where the pid_t sits).
1580 */
1581 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1582 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1583 return -1;
1584 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1585 }
1586
1587 __event_process_build_id(&bev, filename, session);
1588
1589 offset += bev.header.size;
1590 }
1591 err = 0;
1592out:
1593 return err;
1594}
1595
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001596/* Macro for features that simply need to read and store a string. */
1597#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001598static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001599{\
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001600 ff->ph->env.__feat_env = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001601 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001602}
1603
1604FEAT_PROCESS_STR_FUN(hostname, hostname);
1605FEAT_PROCESS_STR_FUN(osrelease, os_release);
1606FEAT_PROCESS_STR_FUN(version, version);
1607FEAT_PROCESS_STR_FUN(arch, arch);
1608FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1609FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1610
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001611static int process_tracing_data(struct feat_fd *ff, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001612{
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001613 ssize_t ret = trace_report(ff->fd, data, false);
1614
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001615 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001616}
1617
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001618static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001619{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001620 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
Robert Richterf1c67db2012-02-10 15:41:56 +01001621 pr_debug("Failed to read buildids, continuing...\n");
1622 return 0;
1623}
1624
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001625static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001626{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001627 int ret;
1628 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001629
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001630 ret = do_read_u32(ff, &nr_cpus_avail);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001631 if (ret)
1632 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001633
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001634 ret = do_read_u32(ff, &nr_cpus_online);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001635 if (ret)
1636 return ret;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001637 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
1638 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001639 return 0;
1640}
1641
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001642static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001643{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001644 u64 total_mem;
1645 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001646
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001647 ret = do_read_u64(ff, &total_mem);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001648 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001649 return -1;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001650 ff->ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001651 return 0;
1652}
1653
Robert Richter7c2f7af2012-08-16 21:10:23 +02001654static struct perf_evsel *
1655perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1656{
1657 struct perf_evsel *evsel;
1658
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001659 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001660 if (evsel->idx == idx)
1661 return evsel;
1662 }
1663
1664 return NULL;
1665}
1666
1667static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001668perf_evlist__set_event_name(struct perf_evlist *evlist,
1669 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001670{
1671 struct perf_evsel *evsel;
1672
1673 if (!event->name)
1674 return;
1675
1676 evsel = perf_evlist__find_by_index(evlist, event->idx);
1677 if (!evsel)
1678 return;
1679
1680 if (evsel->name)
1681 return;
1682
1683 evsel->name = strdup(event->name);
1684}
1685
1686static int
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001687process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001688{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001689 struct perf_session *session;
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001690 struct perf_evsel *evsel, *events = read_event_desc(ff);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001691
1692 if (!events)
1693 return 0;
1694
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001695 session = container_of(ff->ph, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001696 for (evsel = events; evsel->attr.size; evsel++)
1697 perf_evlist__set_event_name(session->evlist, evsel);
1698
1699 free_event_desc(events);
1700
1701 return 0;
1702}
1703
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001704static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001705{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001706 char *str, *cmdline = NULL, **argv = NULL;
1707 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001708
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001709 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001710 return -1;
1711
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001712 ff->ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001713
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001714 cmdline = zalloc(ff->size + nr + 1);
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001715 if (!cmdline)
1716 return -1;
1717
1718 argv = zalloc(sizeof(char *) * (nr + 1));
1719 if (!argv)
1720 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001721
1722 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001723 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001724 if (!str)
1725 goto error;
1726
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001727 argv[i] = cmdline + len;
1728 memcpy(argv[i], str, strlen(str) + 1);
1729 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001730 free(str);
1731 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001732 ff->ph->env.cmdline = cmdline;
1733 ff->ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001734 return 0;
1735
1736error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001737 free(argv);
1738 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001739 return -1;
1740}
1741
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001742static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001743{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001744 u32 nr, i;
1745 char *str;
1746 struct strbuf sb;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001747 int cpu_nr = ff->ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04001748 u64 size = 0;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001749 struct perf_header *ph = ff->ph;
Kan Liang2bb00d22015-09-01 09:58:12 -04001750
1751 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1752 if (!ph->env.cpu)
1753 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001754
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001755 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001756 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001757
Namhyung Kima1ae5652012-09-24 17:14:59 +09001758 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001759 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001760 if (strbuf_init(&sb, 128) < 0)
1761 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001762
1763 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001764 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001765 if (!str)
1766 goto error;
1767
1768 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001769 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1770 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001771 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001772 free(str);
1773 }
1774 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1775
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001776 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001777 return -1;
1778
Namhyung Kima1ae5652012-09-24 17:14:59 +09001779 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001780 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001781
1782 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001783 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001784 if (!str)
1785 goto error;
1786
1787 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001788 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1789 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001790 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001791 free(str);
1792 }
1793 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04001794
1795 /*
1796 * The header may be from old perf,
1797 * which doesn't include core id and socket id information.
1798 */
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001799 if (ff->size <= size) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001800 zfree(&ph->env.cpu);
1801 return 0;
1802 }
1803
1804 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001805 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001806 goto free_cpu;
1807
Kan Liang2bb00d22015-09-01 09:58:12 -04001808 ph->env.cpu[i].core_id = nr;
1809
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001810 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001811 goto free_cpu;
1812
Jan Stancekda8a58b2017-02-17 12:10:26 +01001813 if (nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001814 pr_debug("socket_id number is too big."
1815 "You may need to upgrade the perf tool.\n");
1816 goto free_cpu;
1817 }
1818
1819 ph->env.cpu[i].socket_id = nr;
1820 }
1821
Namhyung Kima1ae5652012-09-24 17:14:59 +09001822 return 0;
1823
1824error:
1825 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04001826free_cpu:
1827 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001828 return -1;
1829}
1830
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001831static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001832{
Jiri Olsac60da222016-07-04 14:16:20 +02001833 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02001834 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001835 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001836
1837 /* nr nodes */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001838 if (do_read_u32(ff, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001839 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001840
Jiri Olsac60da222016-07-04 14:16:20 +02001841 nodes = zalloc(sizeof(*nodes) * nr);
1842 if (!nodes)
1843 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001844
1845 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02001846 n = &nodes[i];
1847
Namhyung Kima1ae5652012-09-24 17:14:59 +09001848 /* node number */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001849 if (do_read_u32(ff, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001850 goto error;
1851
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001852 if (do_read_u64(ff, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001853 goto error;
1854
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001855 if (do_read_u64(ff, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001856 goto error;
1857
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001858 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001859 if (!str)
1860 goto error;
1861
Jiri Olsac60da222016-07-04 14:16:20 +02001862 n->map = cpu_map__new(str);
1863 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001864 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02001865
Namhyung Kima1ae5652012-09-24 17:14:59 +09001866 free(str);
1867 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001868 ff->ph->env.nr_numa_nodes = nr;
1869 ff->ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001870 return 0;
1871
1872error:
Jiri Olsac60da222016-07-04 14:16:20 +02001873 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001874 return -1;
1875}
1876
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001877static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001878{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001879 char *name;
1880 u32 pmu_num;
1881 u32 type;
1882 struct strbuf sb;
1883
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001884 if (do_read_u32(ff, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001885 return -1;
1886
Namhyung Kima1ae5652012-09-24 17:14:59 +09001887 if (!pmu_num) {
1888 pr_debug("pmu mappings not available\n");
1889 return 0;
1890 }
1891
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001892 ff->ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001893 if (strbuf_init(&sb, 128) < 0)
1894 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001895
1896 while (pmu_num) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001897 if (do_read_u32(ff, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001898 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001899
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001900 name = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001901 if (!name)
1902 goto error;
1903
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001904 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
1905 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001906 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001907 if (strbuf_add(&sb, "", 1) < 0)
1908 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001909
Kan Liange0838e02015-09-10 11:03:05 -03001910 if (!strcmp(name, "msr"))
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001911 ff->ph->env.msr_pmu_type = type;
Kan Liange0838e02015-09-10 11:03:05 -03001912
Namhyung Kima1ae5652012-09-24 17:14:59 +09001913 free(name);
1914 pmu_num--;
1915 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001916 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001917 return 0;
1918
1919error:
1920 strbuf_release(&sb);
1921 return -1;
1922}
1923
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001924static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001925{
1926 size_t ret = -1;
1927 u32 i, nr, nr_groups;
1928 struct perf_session *session;
1929 struct perf_evsel *evsel, *leader = NULL;
1930 struct group_desc {
1931 char *name;
1932 u32 leader_idx;
1933 u32 nr_members;
1934 } *desc;
1935
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001936 if (do_read_u32(ff, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001937 return -1;
1938
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001939 ff->ph->env.nr_groups = nr_groups;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001940 if (!nr_groups) {
1941 pr_debug("group desc not available\n");
1942 return 0;
1943 }
1944
1945 desc = calloc(nr_groups, sizeof(*desc));
1946 if (!desc)
1947 return -1;
1948
1949 for (i = 0; i < nr_groups; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001950 desc[i].name = do_read_string(ff);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001951 if (!desc[i].name)
1952 goto out_free;
1953
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001954 if (do_read_u32(ff, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001955 goto out_free;
1956
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001957 if (do_read_u32(ff, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001958 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001959 }
1960
1961 /*
1962 * Rebuild group relationship based on the group_desc
1963 */
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001964 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001965 session->evlist->nr_groups = nr_groups;
1966
1967 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001968 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001969 if (evsel->idx == (int) desc[i].leader_idx) {
1970 evsel->leader = evsel;
1971 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09001972 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001973 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09001974 desc[i].name = NULL;
1975 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09001976 evsel->nr_members = desc[i].nr_members;
1977
1978 if (i >= nr_groups || nr > 0) {
1979 pr_debug("invalid group desc\n");
1980 goto out_free;
1981 }
1982
1983 leader = evsel;
1984 nr = evsel->nr_members - 1;
1985 i++;
1986 } else if (nr) {
1987 /* This is a group member */
1988 evsel->leader = leader;
1989
1990 nr--;
1991 }
1992 }
1993
1994 if (i != nr_groups || nr != 0) {
1995 pr_debug("invalid group desc\n");
1996 goto out_free;
1997 }
1998
1999 ret = 0;
2000out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002001 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002002 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002003 free(desc);
2004
2005 return ret;
2006}
2007
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002008static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
Adrian Hunter99fa2982015-04-30 17:37:25 +03002009{
2010 struct perf_session *session;
2011 int err;
2012
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002013 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002014
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002015 err = auxtrace_index__process(ff->fd, ff->size, session,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002016 ff->ph->needs_swap);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002017 if (err < 0)
2018 pr_err("Failed to process auxtrace index\n");
2019 return err;
2020}
2021
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002022static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01002023{
2024 struct cpu_cache_level *caches;
2025 u32 cnt, i, version;
2026
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002027 if (do_read_u32(ff, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002028 return -1;
2029
Jiri Olsa720e98b2016-02-16 16:01:43 +01002030 if (version != 1)
2031 return -1;
2032
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002033 if (do_read_u32(ff, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002034 return -1;
2035
Jiri Olsa720e98b2016-02-16 16:01:43 +01002036 caches = zalloc(sizeof(*caches) * cnt);
2037 if (!caches)
2038 return -1;
2039
2040 for (i = 0; i < cnt; i++) {
2041 struct cpu_cache_level c;
2042
2043 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002044 if (do_read_u32(ff, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002045 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002046
2047 _R(level)
2048 _R(line_size)
2049 _R(sets)
2050 _R(ways)
2051 #undef _R
2052
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002053 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002054 c.v = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002055 if (!c.v) \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002056 goto out_free_caches;
2057
2058 _R(type)
2059 _R(size)
2060 _R(map)
2061 #undef _R
2062
2063 caches[i] = c;
2064 }
2065
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002066 ff->ph->env.caches = caches;
2067 ff->ph->env.caches_cnt = cnt;
Jiri Olsa720e98b2016-02-16 16:01:43 +01002068 return 0;
2069out_free_caches:
2070 free(caches);
2071 return -1;
2072}
2073
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002074struct feature_ops {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002075 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002076 void (*print)(struct feat_fd *ff, FILE *fp);
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002077 int (*process)(struct feat_fd *ff, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002078 const char *name;
2079 bool full_only;
2080};
2081
Robert Richter8cdfa782011-12-07 10:02:56 +01002082#define FEAT_OPA(n, func) \
2083 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002084#define FEAT_OPP(n, func) \
2085 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2086 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002087#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002088 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002089 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002090
2091/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002092#define print_tracing_data NULL
2093#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002094
2095static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002096 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002097 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002098 FEAT_OPP(HEADER_HOSTNAME, hostname),
2099 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2100 FEAT_OPP(HEADER_VERSION, version),
2101 FEAT_OPP(HEADER_ARCH, arch),
2102 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2103 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002104 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002105 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002106 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002107 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002108 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2109 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002110 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002111 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002112 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Adrian Hunter99fa2982015-04-30 17:37:25 +03002113 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
Jiri Olsaffa517a2015-10-25 15:51:43 +01002114 FEAT_OPA(HEADER_STAT, stat),
Jiri Olsa720e98b2016-02-16 16:01:43 +01002115 FEAT_OPF(HEADER_CACHE, cache),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002116};
2117
2118struct header_print_data {
2119 FILE *fp;
2120 bool full; /* extended list of headers */
2121};
2122
2123static int perf_file_section__fprintf_info(struct perf_file_section *section,
2124 struct perf_header *ph,
2125 int feat, int fd, void *data)
2126{
2127 struct header_print_data *hd = data;
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002128 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002129
2130 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2131 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2132 "%d, continuing...\n", section->offset, feat);
2133 return 0;
2134 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002135 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002136 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002137 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002138 }
2139 if (!feat_ops[feat].print)
2140 return 0;
2141
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002142 ff = (struct feat_fd) {
2143 .fd = fd,
2144 .ph = ph,
2145 };
2146
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002147 if (!feat_ops[feat].full_only || hd->full)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002148 feat_ops[feat].print(&ff, hd->fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002149 else
2150 fprintf(hd->fp, "# %s info available, use -I to display\n",
2151 feat_ops[feat].name);
2152
2153 return 0;
2154}
2155
2156int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2157{
2158 struct header_print_data hd;
2159 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002160 int fd = perf_data_file__fd(session->file);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002161 struct stat st;
Jiri Olsaaabae162016-10-10 09:35:50 +02002162 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002163
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002164 hd.fp = fp;
2165 hd.full = full;
2166
Jiri Olsaf45f5612016-10-10 09:03:07 +02002167 ret = fstat(fd, &st);
2168 if (ret == -1)
2169 return -1;
2170
2171 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2172
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002173 perf_header__process_sections(header, fd, &hd,
2174 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002175
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002176 if (session->file->is_pipe)
2177 return 0;
2178
Jiri Olsaaabae162016-10-10 09:35:50 +02002179 fprintf(fp, "# missing features: ");
2180 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2181 if (bit)
2182 fprintf(fp, "%s ", feat_ops[bit].name);
2183 }
2184
2185 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002186 return 0;
2187}
2188
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002189static int do_write_feat(struct feat_fd *ff, int type,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002190 struct perf_file_section **p,
2191 struct perf_evlist *evlist)
2192{
2193 int err;
2194 int ret = 0;
2195
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002196 if (perf_header__has_feat(ff->ph, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002197 if (!feat_ops[type].write)
2198 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002199
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002200 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002201
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002202 err = feat_ops[type].write(ff, evlist);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002203 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002204 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002205
2206 /* undo anything written */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002207 lseek(ff->fd, (*p)->offset, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002208
2209 return -1;
2210 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002211 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002212 (*p)++;
2213 }
2214 return ret;
2215}
2216
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002217static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002218 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002219{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002220 int nr_sections;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002221 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002222 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002223 int sec_size;
2224 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002225 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002226 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002227
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002228 ff = (struct feat_fd){
2229 .fd = fd,
2230 .ph = header,
2231 };
2232
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002233 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002234 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002235 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002236
Paul Gortmaker91b98802013-01-30 20:05:49 -05002237 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002238 if (feat_sec == NULL)
2239 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002240
2241 sec_size = sizeof(*feat_sec) * nr_sections;
2242
Jiri Olsa8d541e92013-07-17 19:49:44 +02002243 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002244 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002245
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002246 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002247 if (do_write_feat(&ff, feat, &p, evlist))
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002248 perf_header__clear_feat(header, feat);
2249 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002250
Xiao Guangrongf887f302010-02-04 16:46:42 +08002251 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002252 /*
2253 * may write more than needed due to dropped feature, but
2254 * this is okay, reader will skip the mising entries
2255 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002256 err = do_write(&ff, feat_sec, sec_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002257 if (err < 0)
2258 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002259 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002260 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002261}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002262
Tom Zanussi8dc58102010-04-01 23:59:15 -05002263int perf_header__write_pipe(int fd)
2264{
2265 struct perf_pipe_file_header f_header;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002266 struct feat_fd ff;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002267 int err;
2268
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002269 ff = (struct feat_fd){ .fd = fd };
2270
Tom Zanussi8dc58102010-04-01 23:59:15 -05002271 f_header = (struct perf_pipe_file_header){
2272 .magic = PERF_MAGIC,
2273 .size = sizeof(f_header),
2274 };
2275
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002276 err = do_write(&ff, &f_header, sizeof(f_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05002277 if (err < 0) {
2278 pr_debug("failed to write perf pipe header\n");
2279 return err;
2280 }
2281
2282 return 0;
2283}
2284
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002285int perf_session__write_header(struct perf_session *session,
2286 struct perf_evlist *evlist,
2287 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002288{
2289 struct perf_file_header f_header;
2290 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002291 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002292 struct perf_evsel *evsel;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002293 struct feat_fd ff;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002294 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002295 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002296
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002297 ff = (struct feat_fd){ .fd = fd};
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002298 lseek(fd, sizeof(f_header), SEEK_SET);
2299
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002300 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002301 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002302 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002303 if (err < 0) {
2304 pr_debug("failed to write perf header\n");
2305 return err;
2306 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002307 }
2308
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002309 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002310
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002311 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002312 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002313 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002314 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002315 .offset = evsel->id_offset,
2316 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002317 }
2318 };
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002319 err = do_write(&ff, &f_attr, sizeof(f_attr));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002320 if (err < 0) {
2321 pr_debug("failed to write perf header attribute\n");
2322 return err;
2323 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002324 }
2325
Adrian Hunterd645c442013-12-11 14:36:28 +02002326 if (!header->data_offset)
2327 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002328 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002329
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002330 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002331 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002332 if (err < 0)
2333 return err;
2334 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002335
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002336 f_header = (struct perf_file_header){
2337 .magic = PERF_MAGIC,
2338 .size = sizeof(f_header),
2339 .attr_size = sizeof(f_attr),
2340 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002341 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002342 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002343 },
2344 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002345 .offset = header->data_offset,
2346 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002347 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002348 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002349 };
2350
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002351 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002352
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002353 lseek(fd, 0, SEEK_SET);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002354 err = do_write(&ff, &f_header, sizeof(f_header));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002355 if (err < 0) {
2356 pr_debug("failed to write perf header\n");
2357 return err;
2358 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002359 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002360
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002361 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002362}
2363
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002364static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002365 int fd, void *buf, size_t size)
2366{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002367 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002368 return -1;
2369
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002370 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002371 mem_bswap_64(buf, size);
2372
2373 return 0;
2374}
2375
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002376int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002377 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002378 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002379 struct perf_header *ph,
2380 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002381{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002382 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002383 int nr_sections;
2384 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002385 int feat;
2386 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002387
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002388 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002389 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002390 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002391
Paul Gortmaker91b98802013-01-30 20:05:49 -05002392 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002393 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002394 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002395
2396 sec_size = sizeof(*feat_sec) * nr_sections;
2397
Jiri Olsa8d541e92013-07-17 19:49:44 +02002398 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002399
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002400 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2401 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002402 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002403
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002404 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2405 err = process(sec++, header, feat, fd, data);
2406 if (err < 0)
2407 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002408 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002409 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002410out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002411 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002412 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002413}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002414
Stephane Eranian114382a2012-02-09 23:21:08 +01002415static const int attr_file_abi_sizes[] = {
2416 [0] = PERF_ATTR_SIZE_VER0,
2417 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002418 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002419 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002420 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002421 0,
2422};
2423
2424/*
2425 * In the legacy file format, the magic number is not used to encode endianness.
2426 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2427 * on ABI revisions, we need to try all combinations for all endianness to
2428 * detect the endianness.
2429 */
2430static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2431{
2432 uint64_t ref_size, attr_size;
2433 int i;
2434
2435 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2436 ref_size = attr_file_abi_sizes[i]
2437 + sizeof(struct perf_file_section);
2438 if (hdr_sz != ref_size) {
2439 attr_size = bswap_64(hdr_sz);
2440 if (attr_size != ref_size)
2441 continue;
2442
2443 ph->needs_swap = true;
2444 }
2445 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2446 i,
2447 ph->needs_swap);
2448 return 0;
2449 }
2450 /* could not determine endianness */
2451 return -1;
2452}
2453
2454#define PERF_PIPE_HDR_VER0 16
2455
2456static const size_t attr_pipe_abi_sizes[] = {
2457 [0] = PERF_PIPE_HDR_VER0,
2458 0,
2459};
2460
2461/*
2462 * In the legacy pipe format, there is an implicit assumption that endiannesss
2463 * between host recording the samples, and host parsing the samples is the
2464 * same. This is not always the case given that the pipe output may always be
2465 * redirected into a file and analyzed on a different machine with possibly a
2466 * different endianness and perf_event ABI revsions in the perf tool itself.
2467 */
2468static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2469{
2470 u64 attr_size;
2471 int i;
2472
2473 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2474 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2475 attr_size = bswap_64(hdr_sz);
2476 if (attr_size != hdr_sz)
2477 continue;
2478
2479 ph->needs_swap = true;
2480 }
2481 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2482 return 0;
2483 }
2484 return -1;
2485}
2486
Feng Tange84ba4e2012-10-30 11:56:07 +08002487bool is_perf_magic(u64 magic)
2488{
2489 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2490 || magic == __perf_magic2
2491 || magic == __perf_magic2_sw)
2492 return true;
2493
2494 return false;
2495}
2496
Stephane Eranian114382a2012-02-09 23:21:08 +01002497static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2498 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002499{
2500 int ret;
2501
2502 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002503 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002504 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002505 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002506 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002507 if (is_pipe)
2508 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002509
Stephane Eranian114382a2012-02-09 23:21:08 +01002510 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002511 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002512 /*
2513 * the new magic number serves two purposes:
2514 * - unique number to identify actual perf.data files
2515 * - encode endianness of file
2516 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002517 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002518
Stephane Eranian114382a2012-02-09 23:21:08 +01002519 /* check magic number with one endianness */
2520 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002521 return 0;
2522
Stephane Eranian114382a2012-02-09 23:21:08 +01002523 /* check magic number with opposite endianness */
2524 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002525 return -1;
2526
2527 ph->needs_swap = true;
2528
2529 return 0;
2530}
2531
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002532int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002533 struct perf_header *ph, int fd)
2534{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002535 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002536
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002537 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002538
Stephane Eranian73323f52012-02-02 13:54:44 +01002539 ret = readn(fd, header, sizeof(*header));
2540 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002541 return -1;
2542
Stephane Eranian114382a2012-02-09 23:21:08 +01002543 if (check_magic_endian(header->magic,
2544 header->attr_size, false, ph) < 0) {
2545 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002546 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002547 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002548
Stephane Eranian73323f52012-02-02 13:54:44 +01002549 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002550 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002551 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002552 }
2553
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002554 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002555 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002556 if (header->size == offsetof(typeof(*header), adds_features))
2557 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002558 else
2559 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002560 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002561 /*
2562 * feature bitmap is declared as an array of unsigned longs --
2563 * not good since its size can differ between the host that
2564 * generated the data file and the host analyzing the file.
2565 *
2566 * We need to handle endianness, but we don't know the size of
2567 * the unsigned long where the file was generated. Take a best
2568 * guess at determining it: try 64-bit swap first (ie., file
2569 * created on a 64-bit host), and check if the hostname feature
2570 * bit is set (this feature bit is forced on as of fbe96f2).
2571 * If the bit is not, undo the 64-bit swap and try a 32-bit
2572 * swap. If the hostname bit is still not set (e.g., older data
2573 * file), punt and fallback to the original behavior --
2574 * clearing all feature bits and setting buildid.
2575 */
David Ahern80c01202012-06-08 11:47:51 -03002576 mem_bswap_64(&header->adds_features,
2577 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002578
2579 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002580 /* unswap as u64 */
2581 mem_bswap_64(&header->adds_features,
2582 BITS_TO_U64(HEADER_FEAT_BITS));
2583
2584 /* unswap as u32 */
2585 mem_bswap_32(&header->adds_features,
2586 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002587 }
2588
2589 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2590 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2591 set_bit(HEADER_BUILD_ID, header->adds_features);
2592 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002593 }
2594
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002595 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002596 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002597
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002598 ph->data_offset = header->data.offset;
2599 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002600 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002601 return 0;
2602}
2603
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002604static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002605 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002606 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002607{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002608 struct feat_fd fdd = {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002609 .fd = fd,
2610 .ph = ph,
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002611 .size = section->size,
2612 .offset = section->offset,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002613 };
2614
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002615 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002616 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002617 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002618 return 0;
2619 }
2620
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002621 if (feat >= HEADER_LAST_FEATURE) {
2622 pr_debug("unknown feature %d, continuing...\n", feat);
2623 return 0;
2624 }
2625
Robert Richterf1c67db2012-02-10 15:41:56 +01002626 if (!feat_ops[feat].process)
2627 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002628
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002629 return feat_ops[feat].process(&fdd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002630}
2631
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002632static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002633 struct perf_header *ph, int fd,
2634 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002635{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002636 struct feat_fd ff = {
2637 .fd = STDOUT_FILENO,
2638 .ph = ph,
2639 };
Jiri Olsa727ebd52013-11-28 11:30:14 +01002640 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002641
2642 ret = readn(fd, header, sizeof(*header));
2643 if (ret <= 0)
2644 return -1;
2645
Stephane Eranian114382a2012-02-09 23:21:08 +01002646 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2647 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002648 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002649 }
2650
2651 if (ph->needs_swap)
2652 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002653
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002654 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002655 return -1;
2656
Tom Zanussi8dc58102010-04-01 23:59:15 -05002657 return 0;
2658}
2659
Jiri Olsad4339562013-07-17 19:49:41 +02002660static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002661{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002662 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002663 struct perf_pipe_file_header f_header;
2664
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002665 if (perf_file_header__read_pipe(&f_header, header,
2666 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002667 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002668 pr_debug("incompatible file format\n");
2669 return -EINVAL;
2670 }
2671
Tom Zanussi8dc58102010-04-01 23:59:15 -05002672 return 0;
2673}
2674
Stephane Eranian69996df2012-02-09 23:21:06 +01002675static int read_attr(int fd, struct perf_header *ph,
2676 struct perf_file_attr *f_attr)
2677{
2678 struct perf_event_attr *attr = &f_attr->attr;
2679 size_t sz, left;
2680 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002681 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002682
2683 memset(f_attr, 0, sizeof(*f_attr));
2684
2685 /* read minimal guaranteed structure */
2686 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2687 if (ret <= 0) {
2688 pr_debug("cannot read %d bytes of header attr\n",
2689 PERF_ATTR_SIZE_VER0);
2690 return -1;
2691 }
2692
2693 /* on file perf_event_attr size */
2694 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002695
Stephane Eranian69996df2012-02-09 23:21:06 +01002696 if (ph->needs_swap)
2697 sz = bswap_32(sz);
2698
2699 if (sz == 0) {
2700 /* assume ABI0 */
2701 sz = PERF_ATTR_SIZE_VER0;
2702 } else if (sz > our_sz) {
2703 pr_debug("file uses a more recent and unsupported ABI"
2704 " (%zu bytes extra)\n", sz - our_sz);
2705 return -1;
2706 }
2707 /* what we have not yet read and that we know about */
2708 left = sz - PERF_ATTR_SIZE_VER0;
2709 if (left) {
2710 void *ptr = attr;
2711 ptr += PERF_ATTR_SIZE_VER0;
2712
2713 ret = readn(fd, ptr, left);
2714 }
2715 /* read perf_file_section, ids are read in caller */
2716 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2717
2718 return ret <= 0 ? -1 : 0;
2719}
2720
Namhyung Kim831394b2012-09-06 11:10:46 +09002721static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2722 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002723{
Namhyung Kim831394b2012-09-06 11:10:46 +09002724 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002725 char bf[128];
2726
Namhyung Kim831394b2012-09-06 11:10:46 +09002727 /* already prepared */
2728 if (evsel->tp_format)
2729 return 0;
2730
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002731 if (pevent == NULL) {
2732 pr_debug("broken or missing trace data\n");
2733 return -1;
2734 }
2735
Namhyung Kim831394b2012-09-06 11:10:46 +09002736 event = pevent_find_event(pevent, evsel->attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09002737 if (event == NULL) {
2738 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002739 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09002740 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002741
Namhyung Kim831394b2012-09-06 11:10:46 +09002742 if (!evsel->name) {
2743 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2744 evsel->name = strdup(bf);
2745 if (evsel->name == NULL)
2746 return -1;
2747 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002748
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002749 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002750 return 0;
2751}
2752
Namhyung Kim831394b2012-09-06 11:10:46 +09002753static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2754 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002755{
2756 struct perf_evsel *pos;
2757
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002758 evlist__for_each_entry(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002759 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2760 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002761 return -1;
2762 }
2763
2764 return 0;
2765}
2766
Jiri Olsad4339562013-07-17 19:49:41 +02002767int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002768{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002769 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002770 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002771 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002772 struct perf_file_attr f_attr;
2773 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002774 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002775 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002776
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002777 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002778 if (session->evlist == NULL)
2779 return -ENOMEM;
2780
Kan Liang2c071442015-08-28 05:48:05 -04002781 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03002782 session->machines.host.env = &header->env;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002783 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002784 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002785
Stephane Eranian69996df2012-02-09 23:21:06 +01002786 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002787 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002788
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002789 /*
2790 * Sanity check that perf.data was written cleanly; data size is
2791 * initialized to 0 and updated only if the on_exit function is run.
2792 * If data size is still 0 then the file contains only partial
2793 * information. Just warn user and process it as much as it can.
2794 */
2795 if (f_header.data.size == 0) {
2796 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2797 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002798 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002799 }
2800
Stephane Eranian69996df2012-02-09 23:21:06 +01002801 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002802 lseek(fd, f_header.attrs.offset, SEEK_SET);
2803
2804 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002805 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002806 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002807
Stephane Eranian69996df2012-02-09 23:21:06 +01002808 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002809 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002810
David Ahern1060ab82015-04-09 16:15:46 -04002811 if (header->needs_swap) {
2812 f_attr.ids.size = bswap_64(f_attr.ids.size);
2813 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002814 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002815 }
David Aherneda39132011-07-15 12:34:09 -06002816
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002817 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002818 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002819
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002820 if (evsel == NULL)
2821 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002822
2823 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002824 /*
2825 * Do it before so that if perf_evsel__alloc_id fails, this
2826 * entry gets purged too at perf_evlist__delete().
2827 */
2828 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002829
2830 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002831 /*
2832 * We don't have the cpu and thread maps on the header, so
2833 * for allocating the perf_sample_id table we fake 1 cpu and
2834 * hattr->ids threads.
2835 */
2836 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2837 goto out_delete_evlist;
2838
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002839 lseek(fd, f_attr.ids.offset, SEEK_SET);
2840
2841 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002842 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002843 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002844
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002845 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002846 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002847
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002848 lseek(fd, tmp, SEEK_SET);
2849 }
2850
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002851 symbol_conf.nr_events = nr_attrs;
2852
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002853 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002854 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002855
Namhyung Kim831394b2012-09-06 11:10:46 +09002856 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002857 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002858 goto out_delete_evlist;
2859
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002860 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002861out_errno:
2862 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002863
2864out_delete_evlist:
2865 perf_evlist__delete(session->evlist);
2866 session->evlist = NULL;
2867 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002868}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002869
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002870int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002871 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002872 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002873{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002874 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002875 size_t size;
2876 int err;
2877
2878 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002879 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002880 size += sizeof(struct perf_event_header);
2881 size += ids * sizeof(u64);
2882
2883 ev = malloc(size);
2884
Chris Samuelce47dc52010-11-13 13:35:06 +11002885 if (ev == NULL)
2886 return -ENOMEM;
2887
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002888 ev->attr.attr = *attr;
2889 memcpy(ev->attr.id, id, ids * sizeof(u64));
2890
2891 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002892 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002893
Robert Richterf4d83432012-08-16 21:10:17 +02002894 if (ev->attr.header.size == size)
2895 err = process(tool, ev, NULL, NULL);
2896 else
2897 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002898
2899 free(ev);
2900
2901 return err;
2902}
2903
Jiri Olsaa6e52812015-10-25 15:51:37 +01002904static struct event_update_event *
2905event_update_event__new(size_t size, u64 type, u64 id)
2906{
2907 struct event_update_event *ev;
2908
2909 size += sizeof(*ev);
2910 size = PERF_ALIGN(size, sizeof(u64));
2911
2912 ev = zalloc(size);
2913 if (ev) {
2914 ev->header.type = PERF_RECORD_EVENT_UPDATE;
2915 ev->header.size = (u16)size;
2916 ev->type = type;
2917 ev->id = id;
2918 }
2919 return ev;
2920}
2921
2922int
2923perf_event__synthesize_event_update_unit(struct perf_tool *tool,
2924 struct perf_evsel *evsel,
2925 perf_event__handler_t process)
2926{
2927 struct event_update_event *ev;
2928 size_t size = strlen(evsel->unit);
2929 int err;
2930
2931 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
2932 if (ev == NULL)
2933 return -ENOMEM;
2934
2935 strncpy(ev->data, evsel->unit, size);
2936 err = process(tool, (union perf_event *)ev, NULL, NULL);
2937 free(ev);
2938 return err;
2939}
2940
Jiri Olsadaeecbc2015-10-25 15:51:38 +01002941int
2942perf_event__synthesize_event_update_scale(struct perf_tool *tool,
2943 struct perf_evsel *evsel,
2944 perf_event__handler_t process)
2945{
2946 struct event_update_event *ev;
2947 struct event_update_event_scale *ev_data;
2948 int err;
2949
2950 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
2951 if (ev == NULL)
2952 return -ENOMEM;
2953
2954 ev_data = (struct event_update_event_scale *) ev->data;
2955 ev_data->scale = evsel->scale;
2956 err = process(tool, (union perf_event*) ev, NULL, NULL);
2957 free(ev);
2958 return err;
2959}
2960
Jiri Olsa802c9042015-10-25 15:51:39 +01002961int
2962perf_event__synthesize_event_update_name(struct perf_tool *tool,
2963 struct perf_evsel *evsel,
2964 perf_event__handler_t process)
2965{
2966 struct event_update_event *ev;
2967 size_t len = strlen(evsel->name);
2968 int err;
2969
2970 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
2971 if (ev == NULL)
2972 return -ENOMEM;
2973
2974 strncpy(ev->data, evsel->name, len);
2975 err = process(tool, (union perf_event*) ev, NULL, NULL);
2976 free(ev);
2977 return err;
2978}
Jiri Olsadaeecbc2015-10-25 15:51:38 +01002979
Jiri Olsa86ebb092015-10-25 15:51:40 +01002980int
2981perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
2982 struct perf_evsel *evsel,
2983 perf_event__handler_t process)
2984{
2985 size_t size = sizeof(struct event_update_event);
2986 struct event_update_event *ev;
2987 int max, err;
2988 u16 type;
2989
2990 if (!evsel->own_cpus)
2991 return 0;
2992
2993 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
2994 if (!ev)
2995 return -ENOMEM;
2996
2997 ev->header.type = PERF_RECORD_EVENT_UPDATE;
2998 ev->header.size = (u16)size;
2999 ev->type = PERF_EVENT_UPDATE__CPUS;
3000 ev->id = evsel->id[0];
3001
3002 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3003 evsel->own_cpus,
3004 type, max);
3005
3006 err = process(tool, (union perf_event*) ev, NULL, NULL);
3007 free(ev);
3008 return err;
3009}
3010
Jiri Olsac853f932015-10-25 15:51:41 +01003011size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3012{
3013 struct event_update_event *ev = &event->event_update;
3014 struct event_update_event_scale *ev_scale;
3015 struct event_update_event_cpus *ev_cpus;
3016 struct cpu_map *map;
3017 size_t ret;
3018
3019 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3020
3021 switch (ev->type) {
3022 case PERF_EVENT_UPDATE__SCALE:
3023 ev_scale = (struct event_update_event_scale *) ev->data;
3024 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3025 break;
3026 case PERF_EVENT_UPDATE__UNIT:
3027 ret += fprintf(fp, "... unit: %s\n", ev->data);
3028 break;
3029 case PERF_EVENT_UPDATE__NAME:
3030 ret += fprintf(fp, "... name: %s\n", ev->data);
3031 break;
3032 case PERF_EVENT_UPDATE__CPUS:
3033 ev_cpus = (struct event_update_event_cpus *) ev->data;
3034 ret += fprintf(fp, "... ");
3035
3036 map = cpu_map__new_data(&ev_cpus->cpus);
3037 if (map)
3038 ret += cpu_map__fprintf(map, fp);
3039 else
3040 ret += fprintf(fp, "failed to get cpus\n");
3041 break;
3042 default:
3043 ret += fprintf(fp, "... unknown type\n");
3044 break;
3045 }
3046
3047 return ret;
3048}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003049
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003050int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003051 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003052 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003053{
Robert Richter6606f872012-08-16 21:10:19 +02003054 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003055 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003056
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003057 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003058 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3059 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003060 if (err) {
3061 pr_debug("failed to create perf header attribute\n");
3062 return err;
3063 }
3064 }
3065
3066 return err;
3067}
3068
Adrian Hunter47c3d102013-07-04 16:20:21 +03003069int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3070 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003071 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003072{
Robert Richterf4d83432012-08-16 21:10:17 +02003073 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003074 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003075 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003076
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003077 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003078 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003079 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003080 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003081 }
3082
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003083 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003084 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003085 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003086
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003087 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003088
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003089 ids = event->header.size;
3090 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003091 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003092 /*
3093 * We don't have the cpu and thread maps on the header, so
3094 * for allocating the perf_sample_id table we fake 1 cpu and
3095 * hattr->ids threads.
3096 */
3097 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3098 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003099
3100 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003101 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003102 }
3103
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03003104 symbol_conf.nr_events = evlist->nr_entries;
3105
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003106 return 0;
3107}
Tom Zanussicd19a032010-04-01 23:59:20 -05003108
Jiri Olsaffe777252015-10-25 15:51:36 +01003109int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3110 union perf_event *event,
3111 struct perf_evlist **pevlist)
3112{
3113 struct event_update_event *ev = &event->event_update;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003114 struct event_update_event_scale *ev_scale;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003115 struct event_update_event_cpus *ev_cpus;
Jiri Olsaffe777252015-10-25 15:51:36 +01003116 struct perf_evlist *evlist;
3117 struct perf_evsel *evsel;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003118 struct cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003119
3120 if (!pevlist || *pevlist == NULL)
3121 return -EINVAL;
3122
3123 evlist = *pevlist;
3124
3125 evsel = perf_evlist__id2evsel(evlist, ev->id);
3126 if (evsel == NULL)
3127 return -EINVAL;
3128
Jiri Olsaa6e52812015-10-25 15:51:37 +01003129 switch (ev->type) {
3130 case PERF_EVENT_UPDATE__UNIT:
3131 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003132 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003133 case PERF_EVENT_UPDATE__NAME:
3134 evsel->name = strdup(ev->data);
3135 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003136 case PERF_EVENT_UPDATE__SCALE:
3137 ev_scale = (struct event_update_event_scale *) ev->data;
3138 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003139 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003140 case PERF_EVENT_UPDATE__CPUS:
3141 ev_cpus = (struct event_update_event_cpus *) ev->data;
3142
3143 map = cpu_map__new_data(&ev_cpus->cpus);
3144 if (map)
3145 evsel->own_cpus = map;
3146 else
3147 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003148 default:
3149 break;
3150 }
3151
Jiri Olsaffe777252015-10-25 15:51:36 +01003152 return 0;
3153}
3154
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003155int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003156 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003157 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003158{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003159 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003160 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003161 ssize_t size = 0, aligned_size = 0, padding;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003162 struct feat_fd ff;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003163 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003164
Jiri Olsa29208e52011-10-20 15:59:43 +02003165 /*
3166 * We are going to store the size of the data followed
3167 * by the data contents. Since the fd descriptor is a pipe,
3168 * we cannot seek back to store the size of the data once
3169 * we know it. Instead we:
3170 *
3171 * - write the tracing data to the temp file
3172 * - get/write the data size to pipe
3173 * - write the tracing data from the temp file
3174 * to the pipe
3175 */
3176 tdata = tracing_data_get(&evlist->entries, fd, true);
3177 if (!tdata)
3178 return -1;
3179
Tom Zanussi92155452010-04-01 23:59:21 -05003180 memset(&ev, 0, sizeof(ev));
3181
3182 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003183 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003184 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003185 padding = aligned_size - size;
3186 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3187 ev.tracing_data.size = aligned_size;
3188
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003189 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003190
Jiri Olsa29208e52011-10-20 15:59:43 +02003191 /*
3192 * The put function will copy all the tracing data
3193 * stored in temp file to the pipe.
3194 */
3195 tracing_data_put(tdata);
3196
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003197 ff = (struct feat_fd){ .fd = fd };
3198 if (write_padded(&ff, NULL, 0, padding))
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -07003199 return -1;
Tom Zanussi92155452010-04-01 23:59:21 -05003200
3201 return aligned_size;
3202}
3203
Adrian Hunter47c3d102013-07-04 16:20:21 +03003204int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3205 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003206 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05003207{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003208 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003209 int fd = perf_data_file__fd(session->file);
3210 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003211 char buf[BUFSIZ];
3212
3213 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003214 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003215 SEEK_SET);
3216
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003217 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003218 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003219 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003220
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003221 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003222 pr_err("%s: reading input file", __func__);
3223 return -1;
3224 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003225 if (session->repipe) {
3226 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003227 if (retw <= 0 || retw != padding) {
3228 pr_err("%s: repiping tracing data padding", __func__);
3229 return -1;
3230 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003231 }
Tom Zanussi92155452010-04-01 23:59:21 -05003232
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003233 if (size_read + padding != size) {
3234 pr_err("%s: tracing data size mismatch", __func__);
3235 return -1;
3236 }
Tom Zanussi92155452010-04-01 23:59:21 -05003237
Namhyung Kim831394b2012-09-06 11:10:46 +09003238 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003239 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003240
Tom Zanussi92155452010-04-01 23:59:21 -05003241 return size_read + padding;
3242}
Tom Zanussic7929e42010-04-01 23:59:22 -05003243
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003244int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003245 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003246 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003247 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003248{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003249 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003250 size_t len;
3251 int err = 0;
3252
3253 if (!pos->hit)
3254 return err;
3255
3256 memset(&ev, 0, sizeof(ev));
3257
3258 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003259 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003260 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3261 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3262 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003263 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003264 ev.build_id.header.size = sizeof(ev.build_id) + len;
3265 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3266
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003267 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003268
3269 return err;
3270}
3271
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003272int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003273 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003274 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003275{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003276 __event_process_build_id(&event->build_id,
3277 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003278 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003279 return 0;
3280}