blob: 21a77e7a171e8aa0664d5caf0737f141bc96e62f [file] [log] [blame]
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02001#include "util.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02004#include <unistd.h>
5#include <stdio.h>
6#include <stdlib.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +01007#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02008#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +01009#include <linux/bitops.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020010#include <sys/utsname.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020011
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020012#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030013#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020014#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020015#include "../perf.h"
16#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020017#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010018#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010019#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020020#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020021#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020022#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090023#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020024#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020025#include "data.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020026
Stephane Eranianfbe96f22011-09-30 15:40:40 +020027static u32 header_argc;
28static const char **header_argv;
29
Stephane Eranian73323f52012-02-02 13:54:44 +010030/*
31 * magic2 = "PERFILE2"
32 * must be a numerical value to let the endianness
33 * determine the memory layout. That way we are able
34 * to detect endianness when reading the perf.data file
35 * back.
36 *
37 * we check for legacy (PERFFILE) format.
38 */
39static const char *__perf_magic1 = "PERFFILE";
40static const u64 __perf_magic2 = 0x32454c4946524550ULL;
41static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020042
Stephane Eranian73323f52012-02-02 13:54:44 +010043#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020044
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020045struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020046 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020047 struct perf_file_section ids;
48};
49
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030050void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020051{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030052 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020053}
54
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030055void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020056{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030057 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020058}
59
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030060bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020061{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030062 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020063}
64
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020065static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020066{
67 while (size) {
68 int ret = write(fd, buf, size);
69
70 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -020071 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020072
73 size -= ret;
74 buf += ret;
75 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020076
77 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020078}
79
Namhyung Kime195fac2014-11-04 10:14:30 +090080int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020081{
82 static const char zero_buf[NAME_ALIGN];
83 int err = do_write(fd, bf, count);
84
85 if (!err)
86 err = do_write(fd, zero_buf, count_aligned - count);
87
88 return err;
89}
90
Stephane Eranianfbe96f22011-09-30 15:40:40 +020091static int do_write_string(int fd, const char *str)
92{
93 u32 len, olen;
94 int ret;
95
96 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +030097 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +020098
99 /* write len, incl. \0 */
100 ret = do_write(fd, &len, sizeof(len));
101 if (ret < 0)
102 return ret;
103
104 return write_padded(fd, str, olen, len);
105}
106
107static char *do_read_string(int fd, struct perf_header *ph)
108{
109 ssize_t sz, ret;
110 u32 len;
111 char *buf;
112
Namhyung Kim5323f602012-12-17 15:38:54 +0900113 sz = readn(fd, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200114 if (sz < (ssize_t)sizeof(len))
115 return NULL;
116
117 if (ph->needs_swap)
118 len = bswap_32(len);
119
120 buf = malloc(len);
121 if (!buf)
122 return NULL;
123
Namhyung Kim5323f602012-12-17 15:38:54 +0900124 ret = readn(fd, buf, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200125 if (ret == (ssize_t)len) {
126 /*
127 * strings are padded by zeroes
128 * thus the actual strlen of buf
129 * may be less than len
130 */
131 return buf;
132 }
133
134 free(buf);
135 return NULL;
136}
137
138int
139perf_header__set_cmdline(int argc, const char **argv)
140{
141 int i;
142
David Ahern56e6f602012-07-29 20:53:51 -0600143 /*
144 * If header_argv has already been set, do not override it.
145 * This allows a command to set the cmdline, parse args and
146 * then call another builtin function that implements a
147 * command -- e.g, cmd_kvm calling cmd_record.
148 */
149 if (header_argv)
150 return 0;
151
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200152 header_argc = (u32)argc;
153
154 /* do not include NULL termination */
155 header_argv = calloc(argc, sizeof(char *));
156 if (!header_argv)
157 return -ENOMEM;
158
159 /*
160 * must copy argv contents because it gets moved
161 * around during option parsing
162 */
163 for (i = 0; i < argc ; i++)
164 header_argv[i] = argv[i];
165
166 return 0;
167}
168
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300169static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200170 struct perf_evlist *evlist)
171{
172 return read_tracing_data(fd, &evlist->entries);
173}
174
175
176static int write_build_id(int fd, struct perf_header *h,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300177 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200178{
179 struct perf_session *session;
180 int err;
181
182 session = container_of(h, struct perf_session, header);
183
Robert Richtere20960c2011-12-07 10:02:55 +0100184 if (!perf_session__read_build_ids(session, true))
185 return -1;
186
Namhyung Kim714c9c42014-11-04 10:14:29 +0900187 err = perf_session__write_buildid_table(session, fd);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200188 if (err < 0) {
189 pr_debug("failed to write buildid table\n");
190 return err;
191 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900192 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200193
194 return 0;
195}
196
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300197static int write_hostname(int fd, struct perf_header *h __maybe_unused,
198 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200199{
200 struct utsname uts;
201 int ret;
202
203 ret = uname(&uts);
204 if (ret < 0)
205 return -1;
206
207 return do_write_string(fd, uts.nodename);
208}
209
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300210static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
211 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200212{
213 struct utsname uts;
214 int ret;
215
216 ret = uname(&uts);
217 if (ret < 0)
218 return -1;
219
220 return do_write_string(fd, uts.release);
221}
222
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300223static int write_arch(int fd, struct perf_header *h __maybe_unused,
224 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200225{
226 struct utsname uts;
227 int ret;
228
229 ret = uname(&uts);
230 if (ret < 0)
231 return -1;
232
233 return do_write_string(fd, uts.machine);
234}
235
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300236static int write_version(int fd, struct perf_header *h __maybe_unused,
237 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200238{
239 return do_write_string(fd, perf_version_string);
240}
241
Wang Nan493c3032014-10-24 09:45:26 +0800242static int __write_cpudesc(int fd, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200243{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200244 FILE *file;
245 char *buf = NULL;
246 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800247 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200248 size_t len = 0;
249 int ret = -1;
250
251 if (!search)
252 return -1;
253
254 file = fopen("/proc/cpuinfo", "r");
255 if (!file)
256 return -1;
257
258 while (getline(&buf, &len, file) > 0) {
259 ret = strncmp(buf, search, strlen(search));
260 if (!ret)
261 break;
262 }
263
Wang Naned307752014-10-16 11:08:29 +0800264 if (ret) {
265 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200266 goto done;
Wang Naned307752014-10-16 11:08:29 +0800267 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200268
269 s = buf;
270
271 p = strchr(buf, ':');
272 if (p && *(p+1) == ' ' && *(p+2))
273 s = p + 2;
274 p = strchr(s, '\n');
275 if (p)
276 *p = '\0';
277
278 /* squash extra space characters (branding string) */
279 p = s;
280 while (*p) {
281 if (isspace(*p)) {
282 char *r = p + 1;
283 char *q = r;
284 *p = ' ';
285 while (*q && isspace(*q))
286 q++;
287 if (q != (p+1))
288 while ((*r++ = *q++));
289 }
290 p++;
291 }
292 ret = do_write_string(fd, s);
293done:
294 free(buf);
295 fclose(file);
296 return ret;
297}
298
Wang Nan493c3032014-10-24 09:45:26 +0800299static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
300 struct perf_evlist *evlist __maybe_unused)
301{
302#ifndef CPUINFO_PROC
303#define CPUINFO_PROC {"model name", }
304#endif
305 const char *cpuinfo_procs[] = CPUINFO_PROC;
306 unsigned int i;
307
308 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
309 int ret;
310 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
311 if (ret >= 0)
312 return ret;
313 }
314 return -1;
315}
316
317
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300318static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
319 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200320{
321 long nr;
322 u32 nrc, nra;
323 int ret;
324
325 nr = sysconf(_SC_NPROCESSORS_CONF);
326 if (nr < 0)
327 return -1;
328
329 nrc = (u32)(nr & UINT_MAX);
330
331 nr = sysconf(_SC_NPROCESSORS_ONLN);
332 if (nr < 0)
333 return -1;
334
335 nra = (u32)(nr & UINT_MAX);
336
337 ret = do_write(fd, &nrc, sizeof(nrc));
338 if (ret < 0)
339 return ret;
340
341 return do_write(fd, &nra, sizeof(nra));
342}
343
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300344static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200345 struct perf_evlist *evlist)
346{
Robert Richter6606f872012-08-16 21:10:19 +0200347 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900348 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200349 int ret;
350
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900351 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200352
353 /*
354 * write number of events
355 */
356 ret = do_write(fd, &nre, sizeof(nre));
357 if (ret < 0)
358 return ret;
359
360 /*
361 * size of perf_event_attr struct
362 */
Robert Richter6606f872012-08-16 21:10:19 +0200363 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200364 ret = do_write(fd, &sz, sizeof(sz));
365 if (ret < 0)
366 return ret;
367
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300368 evlist__for_each(evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +0200369 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200370 if (ret < 0)
371 return ret;
372 /*
373 * write number of unique id per event
374 * there is one id per instance of an event
375 *
376 * copy into an nri to be independent of the
377 * type of ids,
378 */
Robert Richter6606f872012-08-16 21:10:19 +0200379 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200380 ret = do_write(fd, &nri, sizeof(nri));
381 if (ret < 0)
382 return ret;
383
384 /*
385 * write event string as passed on cmdline
386 */
Robert Richter6606f872012-08-16 21:10:19 +0200387 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200388 if (ret < 0)
389 return ret;
390 /*
391 * write unique ids for this event
392 */
Robert Richter6606f872012-08-16 21:10:19 +0200393 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200394 if (ret < 0)
395 return ret;
396 }
397 return 0;
398}
399
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300400static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
401 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200402{
403 char buf[MAXPATHLEN];
404 char proc[32];
405 u32 i, n;
406 int ret;
407
408 /*
409 * actual atual path to perf binary
410 */
411 sprintf(proc, "/proc/%d/exe", getpid());
412 ret = readlink(proc, buf, sizeof(buf));
413 if (ret <= 0)
414 return -1;
415
416 /* readlink() does not add null termination */
417 buf[ret] = '\0';
418
419 /* account for binary path */
420 n = header_argc + 1;
421
422 ret = do_write(fd, &n, sizeof(n));
423 if (ret < 0)
424 return ret;
425
426 ret = do_write_string(fd, buf);
427 if (ret < 0)
428 return ret;
429
430 for (i = 0 ; i < header_argc; i++) {
431 ret = do_write_string(fd, header_argv[i]);
432 if (ret < 0)
433 return ret;
434 }
435 return 0;
436}
437
438#define CORE_SIB_FMT \
439 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
440#define THRD_SIB_FMT \
441 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
442
443struct cpu_topo {
444 u32 core_sib;
445 u32 thread_sib;
446 char **core_siblings;
447 char **thread_siblings;
448};
449
450static int build_cpu_topo(struct cpu_topo *tp, int cpu)
451{
452 FILE *fp;
453 char filename[MAXPATHLEN];
454 char *buf = NULL, *p;
455 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200456 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200457 u32 i = 0;
458 int ret = -1;
459
460 sprintf(filename, CORE_SIB_FMT, cpu);
461 fp = fopen(filename, "r");
462 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200463 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200464
Stephane Eranianc5885742013-08-14 12:04:26 +0200465 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200466 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200467 if (sret <= 0)
468 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200469
470 p = strchr(buf, '\n');
471 if (p)
472 *p = '\0';
473
474 for (i = 0; i < tp->core_sib; i++) {
475 if (!strcmp(buf, tp->core_siblings[i]))
476 break;
477 }
478 if (i == tp->core_sib) {
479 tp->core_siblings[i] = buf;
480 tp->core_sib++;
481 buf = NULL;
482 len = 0;
483 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200484 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200485
Stephane Eranianc5885742013-08-14 12:04:26 +0200486try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200487 sprintf(filename, THRD_SIB_FMT, cpu);
488 fp = fopen(filename, "r");
489 if (!fp)
490 goto done;
491
492 if (getline(&buf, &len, fp) <= 0)
493 goto done;
494
495 p = strchr(buf, '\n');
496 if (p)
497 *p = '\0';
498
499 for (i = 0; i < tp->thread_sib; i++) {
500 if (!strcmp(buf, tp->thread_siblings[i]))
501 break;
502 }
503 if (i == tp->thread_sib) {
504 tp->thread_siblings[i] = buf;
505 tp->thread_sib++;
506 buf = NULL;
507 }
508 ret = 0;
509done:
510 if(fp)
511 fclose(fp);
512 free(buf);
513 return ret;
514}
515
516static void free_cpu_topo(struct cpu_topo *tp)
517{
518 u32 i;
519
520 if (!tp)
521 return;
522
523 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300524 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200525
526 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300527 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200528
529 free(tp);
530}
531
532static struct cpu_topo *build_cpu_topology(void)
533{
534 struct cpu_topo *tp;
535 void *addr;
536 u32 nr, i;
537 size_t sz;
538 long ncpus;
539 int ret = -1;
540
541 ncpus = sysconf(_SC_NPROCESSORS_CONF);
542 if (ncpus < 0)
543 return NULL;
544
545 nr = (u32)(ncpus & UINT_MAX);
546
547 sz = nr * sizeof(char *);
548
549 addr = calloc(1, sizeof(*tp) + 2 * sz);
550 if (!addr)
551 return NULL;
552
553 tp = addr;
554
555 addr += sizeof(*tp);
556 tp->core_siblings = addr;
557 addr += sz;
558 tp->thread_siblings = addr;
559
560 for (i = 0; i < nr; i++) {
561 ret = build_cpu_topo(tp, i);
562 if (ret < 0)
563 break;
564 }
565 if (ret) {
566 free_cpu_topo(tp);
567 tp = NULL;
568 }
569 return tp;
570}
571
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300572static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
573 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200574{
575 struct cpu_topo *tp;
576 u32 i;
577 int ret;
578
579 tp = build_cpu_topology();
580 if (!tp)
581 return -1;
582
583 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
584 if (ret < 0)
585 goto done;
586
587 for (i = 0; i < tp->core_sib; i++) {
588 ret = do_write_string(fd, tp->core_siblings[i]);
589 if (ret < 0)
590 goto done;
591 }
592 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
593 if (ret < 0)
594 goto done;
595
596 for (i = 0; i < tp->thread_sib; i++) {
597 ret = do_write_string(fd, tp->thread_siblings[i]);
598 if (ret < 0)
599 break;
600 }
601done:
602 free_cpu_topo(tp);
603 return ret;
604}
605
606
607
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300608static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
609 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200610{
611 char *buf = NULL;
612 FILE *fp;
613 size_t len = 0;
614 int ret = -1, n;
615 uint64_t mem;
616
617 fp = fopen("/proc/meminfo", "r");
618 if (!fp)
619 return -1;
620
621 while (getline(&buf, &len, fp) > 0) {
622 ret = strncmp(buf, "MemTotal:", 9);
623 if (!ret)
624 break;
625 }
626 if (!ret) {
627 n = sscanf(buf, "%*s %"PRIu64, &mem);
628 if (n == 1)
629 ret = do_write(fd, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800630 } else
631 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200632 free(buf);
633 fclose(fp);
634 return ret;
635}
636
637static int write_topo_node(int fd, int node)
638{
639 char str[MAXPATHLEN];
640 char field[32];
641 char *buf = NULL, *p;
642 size_t len = 0;
643 FILE *fp;
644 u64 mem_total, mem_free, mem;
645 int ret = -1;
646
647 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
648 fp = fopen(str, "r");
649 if (!fp)
650 return -1;
651
652 while (getline(&buf, &len, fp) > 0) {
653 /* skip over invalid lines */
654 if (!strchr(buf, ':'))
655 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100656 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200657 goto done;
658 if (!strcmp(field, "MemTotal:"))
659 mem_total = mem;
660 if (!strcmp(field, "MemFree:"))
661 mem_free = mem;
662 }
663
664 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100665 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200666
667 ret = do_write(fd, &mem_total, sizeof(u64));
668 if (ret)
669 goto done;
670
671 ret = do_write(fd, &mem_free, sizeof(u64));
672 if (ret)
673 goto done;
674
675 ret = -1;
676 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
677
678 fp = fopen(str, "r");
679 if (!fp)
680 goto done;
681
682 if (getline(&buf, &len, fp) <= 0)
683 goto done;
684
685 p = strchr(buf, '\n');
686 if (p)
687 *p = '\0';
688
689 ret = do_write_string(fd, buf);
690done:
691 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100692 if (fp)
693 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200694 return ret;
695}
696
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300697static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
698 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200699{
700 char *buf = NULL;
701 size_t len = 0;
702 FILE *fp;
703 struct cpu_map *node_map = NULL;
704 char *c;
705 u32 nr, i, j;
706 int ret = -1;
707
708 fp = fopen("/sys/devices/system/node/online", "r");
709 if (!fp)
710 return -1;
711
712 if (getline(&buf, &len, fp) <= 0)
713 goto done;
714
715 c = strchr(buf, '\n');
716 if (c)
717 *c = '\0';
718
719 node_map = cpu_map__new(buf);
720 if (!node_map)
721 goto done;
722
723 nr = (u32)node_map->nr;
724
725 ret = do_write(fd, &nr, sizeof(nr));
726 if (ret < 0)
727 goto done;
728
729 for (i = 0; i < nr; i++) {
730 j = (u32)node_map->map[i];
731 ret = do_write(fd, &j, sizeof(j));
732 if (ret < 0)
733 break;
734
735 ret = write_topo_node(fd, i);
736 if (ret < 0)
737 break;
738 }
739done:
740 free(buf);
741 fclose(fp);
742 free(node_map);
743 return ret;
744}
745
746/*
Robert Richter50a96672012-08-16 21:10:24 +0200747 * File format:
748 *
749 * struct pmu_mappings {
750 * u32 pmu_num;
751 * struct pmu_map {
752 * u32 type;
753 * char name[];
754 * }[pmu_num];
755 * };
756 */
757
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300758static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
759 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200760{
761 struct perf_pmu *pmu = NULL;
762 off_t offset = lseek(fd, 0, SEEK_CUR);
763 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900764 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200765
766 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +0900767 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
768 if (ret < 0)
769 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200770
771 while ((pmu = perf_pmu__scan(pmu))) {
772 if (!pmu->name)
773 continue;
774 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +0900775
776 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
777 if (ret < 0)
778 return ret;
779
780 ret = do_write_string(fd, pmu->name);
781 if (ret < 0)
782 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200783 }
784
785 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
786 /* discard all */
787 lseek(fd, offset, SEEK_SET);
788 return -1;
789 }
790
791 return 0;
792}
793
794/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900795 * File format:
796 *
797 * struct group_descs {
798 * u32 nr_groups;
799 * struct group_desc {
800 * char name[];
801 * u32 leader_idx;
802 * u32 nr_members;
803 * }[nr_groups];
804 * };
805 */
806static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
807 struct perf_evlist *evlist)
808{
809 u32 nr_groups = evlist->nr_groups;
810 struct perf_evsel *evsel;
811 int ret;
812
813 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
814 if (ret < 0)
815 return ret;
816
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300817 evlist__for_each(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900818 if (perf_evsel__is_group_leader(evsel) &&
819 evsel->nr_members > 1) {
820 const char *name = evsel->group_name ?: "{anon_group}";
821 u32 leader_idx = evsel->idx;
822 u32 nr_members = evsel->nr_members;
823
824 ret = do_write_string(fd, name);
825 if (ret < 0)
826 return ret;
827
828 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
829 if (ret < 0)
830 return ret;
831
832 ret = do_write(fd, &nr_members, sizeof(nr_members));
833 if (ret < 0)
834 return ret;
835 }
836 }
837 return 0;
838}
839
840/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200841 * default get_cpuid(): nothing gets recorded
842 * actual implementation must be in arch/$(ARCH)/util/header.c
843 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300844int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
845 size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200846{
847 return -1;
848}
849
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300850static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
851 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200852{
853 char buffer[64];
854 int ret;
855
856 ret = get_cpuid(buffer, sizeof(buffer));
857 if (!ret)
858 goto write_it;
859
860 return -1;
861write_it:
862 return do_write_string(fd, buffer);
863}
864
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300865static int write_branch_stack(int fd __maybe_unused,
866 struct perf_header *h __maybe_unused,
867 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100868{
869 return 0;
870}
871
Adrian Hunter99fa2982015-04-30 17:37:25 +0300872static int write_auxtrace(int fd, struct perf_header *h,
Adrian Hunter4025ea42015-04-09 18:53:41 +0300873 struct perf_evlist *evlist __maybe_unused)
874{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300875 struct perf_session *session;
876 int err;
877
878 session = container_of(h, struct perf_session, header);
879
880 err = auxtrace_index__write(fd, &session->auxtrace_index);
881 if (err < 0)
882 pr_err("Failed to write auxtrace index\n");
883 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300884}
885
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900886static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
887 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200888{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900889 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200890}
891
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900892static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
893 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200894{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900895 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200896}
897
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900898static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200899{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900900 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200901}
902
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900903static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
904 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200905{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900906 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200907}
908
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900909static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
910 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200911{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900912 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
913 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200914}
915
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900916static void print_version(struct perf_header *ph, int fd __maybe_unused,
917 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200918{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900919 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200920}
921
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900922static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
923 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200924{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900925 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200926 char *str;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200927
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900928 nr = ph->env.nr_cmdline;
929 str = ph->env.cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200930
931 fprintf(fp, "# cmdline : ");
932
933 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200934 fprintf(fp, "%s ", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900935 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200936 }
937 fputc('\n', fp);
938}
939
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900940static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
941 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200942{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900943 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200944 char *str;
945
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900946 nr = ph->env.nr_sibling_cores;
947 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200948
949 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200950 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900951 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200952 }
953
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900954 nr = ph->env.nr_sibling_threads;
955 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200956
957 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200958 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900959 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200960 }
961}
962
Robert Richter4e1b9c62012-08-16 21:10:22 +0200963static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200964{
Robert Richter4e1b9c62012-08-16 21:10:22 +0200965 struct perf_evsel *evsel;
966
967 if (!events)
968 return;
969
970 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300971 zfree(&evsel->name);
972 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +0200973 }
974
975 free(events);
976}
977
978static struct perf_evsel *
979read_event_desc(struct perf_header *ph, int fd)
980{
981 struct perf_evsel *evsel, *events = NULL;
982 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200983 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +0100984 u32 nre, sz, nr, i, j;
985 ssize_t ret;
986 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200987
988 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +0900989 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200990 if (ret != (ssize_t)sizeof(nre))
991 goto error;
992
993 if (ph->needs_swap)
994 nre = bswap_32(nre);
995
Namhyung Kim5323f602012-12-17 15:38:54 +0900996 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200997 if (ret != (ssize_t)sizeof(sz))
998 goto error;
999
1000 if (ph->needs_swap)
1001 sz = bswap_32(sz);
1002
Stephane Eranian62db9062012-02-09 23:21:07 +01001003 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001004 buf = malloc(sz);
1005 if (!buf)
1006 goto error;
1007
Robert Richter4e1b9c62012-08-16 21:10:22 +02001008 /* the last event terminates with evsel->attr.size == 0: */
1009 events = calloc(nre + 1, sizeof(*events));
1010 if (!events)
1011 goto error;
1012
1013 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001014 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001015 msz = sz;
1016
Robert Richter4e1b9c62012-08-16 21:10:22 +02001017 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1018 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001019
Stephane Eranian62db9062012-02-09 23:21:07 +01001020 /*
1021 * must read entire on-file attr struct to
1022 * sync up with layout.
1023 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001024 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001025 if (ret != (ssize_t)sz)
1026 goto error;
1027
1028 if (ph->needs_swap)
1029 perf_event__attr_swap(buf);
1030
Robert Richter4e1b9c62012-08-16 21:10:22 +02001031 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001032
Namhyung Kim5323f602012-12-17 15:38:54 +09001033 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001034 if (ret != (ssize_t)sizeof(nr))
1035 goto error;
1036
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001037 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001038 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001039 evsel->needs_swap = true;
1040 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001041
Robert Richter4e1b9c62012-08-16 21:10:22 +02001042 evsel->name = do_read_string(fd, ph);
1043
1044 if (!nr)
1045 continue;
1046
1047 id = calloc(nr, sizeof(*id));
1048 if (!id)
1049 goto error;
1050 evsel->ids = nr;
1051 evsel->id = id;
1052
1053 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001054 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001055 if (ret != (ssize_t)sizeof(*id))
1056 goto error;
1057 if (ph->needs_swap)
1058 *id = bswap_64(*id);
1059 id++;
1060 }
1061 }
1062out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001063 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001064 return events;
1065error:
1066 if (events)
1067 free_event_desc(events);
1068 events = NULL;
1069 goto out;
1070}
1071
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001072static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1073 void *priv __attribute__((unused)))
1074{
1075 return fprintf(fp, ", %s = %s", name, val);
1076}
1077
Robert Richter4e1b9c62012-08-16 21:10:22 +02001078static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1079{
1080 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1081 u32 j;
1082 u64 *id;
1083
1084 if (!events) {
1085 fprintf(fp, "# event desc: not available or unable to read\n");
1086 return;
1087 }
1088
1089 for (evsel = events; evsel->attr.size; evsel++) {
1090 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001091
Robert Richter4e1b9c62012-08-16 21:10:22 +02001092 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001093 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001094 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1095 if (j)
1096 fputc(',', fp);
1097 fprintf(fp, " %"PRIu64, *id);
1098 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001099 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001100 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001101
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001102 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001103
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001104 fputc('\n', fp);
1105 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001106
1107 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001108}
1109
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001110static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001111 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001112{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001113 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001114}
1115
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001116static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001117 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001118{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001119 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001120 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001121 uint64_t mem_total, mem_free;
1122
1123 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001124 nr = ph->env.nr_numa_nodes;
1125 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001126
1127 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001128 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001129 c = strtoul(str, &tmp, 0);
1130 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001131 goto error;
1132
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001133 str = tmp + 1;
1134 mem_total = strtoull(str, &tmp, 0);
1135 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001136 goto error;
1137
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001138 str = tmp + 1;
1139 mem_free = strtoull(str, &tmp, 0);
1140 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001141 goto error;
1142
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001143 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1144 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001145 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001146
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001147 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001148 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001149
1150 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001151 }
1152 return;
1153error:
1154 fprintf(fp, "# numa topology : not available\n");
1155}
1156
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001157static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001158{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001159 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001160}
1161
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001162static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001163 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001164{
1165 fprintf(fp, "# contains samples with branch stack\n");
1166}
1167
Adrian Hunter4025ea42015-04-09 18:53:41 +03001168static void print_auxtrace(struct perf_header *ph __maybe_unused,
1169 int fd __maybe_unused, FILE *fp)
1170{
1171 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1172}
1173
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001174static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1175 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001176{
1177 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001178 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001179 u32 pmu_num;
1180 u32 type;
1181
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001182 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001183 if (!pmu_num) {
1184 fprintf(fp, "# pmu mappings: not available\n");
1185 return;
1186 }
1187
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001188 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001189
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001190 while (pmu_num) {
1191 type = strtoul(str, &tmp, 0);
1192 if (*tmp != ':')
1193 goto error;
1194
1195 str = tmp + 1;
1196 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1197
Robert Richter50a96672012-08-16 21:10:24 +02001198 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001199 str += strlen(str) + 1;
1200 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001201 }
1202
1203 fprintf(fp, "\n");
1204
1205 if (!pmu_num)
1206 return;
1207error:
1208 fprintf(fp, "# pmu mappings: unable to read\n");
1209}
1210
Namhyung Kima8bb5592013-01-22 18:09:31 +09001211static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1212 FILE *fp)
1213{
1214 struct perf_session *session;
1215 struct perf_evsel *evsel;
1216 u32 nr = 0;
1217
1218 session = container_of(ph, struct perf_session, header);
1219
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001220 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001221 if (perf_evsel__is_group_leader(evsel) &&
1222 evsel->nr_members > 1) {
1223 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1224 perf_evsel__name(evsel));
1225
1226 nr = evsel->nr_members - 1;
1227 } else if (nr) {
1228 fprintf(fp, ",%s", perf_evsel__name(evsel));
1229
1230 if (--nr == 0)
1231 fprintf(fp, "}\n");
1232 }
1233 }
1234}
1235
Robert Richter08d95bd2012-02-10 15:41:55 +01001236static int __event_process_build_id(struct build_id_event *bev,
1237 char *filename,
1238 struct perf_session *session)
1239{
1240 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001241 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001242 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001243 struct dso *dso;
1244 enum dso_kernel_type dso_type;
1245
1246 machine = perf_session__findnew_machine(session, bev->pid);
1247 if (!machine)
1248 goto out;
1249
Wang Nan1f121b02015-06-03 08:52:21 +00001250 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001251
Wang Nan1f121b02015-06-03 08:52:21 +00001252 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001253 case PERF_RECORD_MISC_KERNEL:
1254 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001255 break;
1256 case PERF_RECORD_MISC_GUEST_KERNEL:
1257 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001258 break;
1259 case PERF_RECORD_MISC_USER:
1260 case PERF_RECORD_MISC_GUEST_USER:
1261 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001262 break;
1263 default:
1264 goto out;
1265 }
1266
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001267 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001268 if (dso != NULL) {
1269 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1270
1271 dso__set_build_id(dso, &bev->build_id);
1272
Wang Nan1f121b02015-06-03 08:52:21 +00001273 if (!is_kernel_module(filename, cpumode))
Robert Richter08d95bd2012-02-10 15:41:55 +01001274 dso->kernel = dso_type;
1275
1276 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1277 sbuild_id);
1278 pr_debug("build id event received for %s: %s\n",
1279 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001280 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001281 }
1282
1283 err = 0;
1284out:
1285 return err;
1286}
1287
1288static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1289 int input, u64 offset, u64 size)
1290{
1291 struct perf_session *session = container_of(header, struct perf_session, header);
1292 struct {
1293 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001294 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001295 char filename[0];
1296 } old_bev;
1297 struct build_id_event bev;
1298 char filename[PATH_MAX];
1299 u64 limit = offset + size;
1300
1301 while (offset < limit) {
1302 ssize_t len;
1303
Namhyung Kim5323f602012-12-17 15:38:54 +09001304 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001305 return -1;
1306
1307 if (header->needs_swap)
1308 perf_event_header__bswap(&old_bev.header);
1309
1310 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001311 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001312 return -1;
1313
1314 bev.header = old_bev.header;
1315
1316 /*
1317 * As the pid is the missing value, we need to fill
1318 * it properly. The header.misc value give us nice hint.
1319 */
1320 bev.pid = HOST_KERNEL_ID;
1321 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1322 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1323 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1324
1325 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1326 __event_process_build_id(&bev, filename, session);
1327
1328 offset += bev.header.size;
1329 }
1330
1331 return 0;
1332}
1333
1334static int perf_header__read_build_ids(struct perf_header *header,
1335 int input, u64 offset, u64 size)
1336{
1337 struct perf_session *session = container_of(header, struct perf_session, header);
1338 struct build_id_event bev;
1339 char filename[PATH_MAX];
1340 u64 limit = offset + size, orig_offset = offset;
1341 int err = -1;
1342
1343 while (offset < limit) {
1344 ssize_t len;
1345
Namhyung Kim5323f602012-12-17 15:38:54 +09001346 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001347 goto out;
1348
1349 if (header->needs_swap)
1350 perf_event_header__bswap(&bev.header);
1351
1352 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001353 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001354 goto out;
1355 /*
1356 * The a1645ce1 changeset:
1357 *
1358 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1359 *
1360 * Added a field to struct build_id_event that broke the file
1361 * format.
1362 *
1363 * Since the kernel build-id is the first entry, process the
1364 * table using the old format if the well known
1365 * '[kernel.kallsyms]' string for the kernel build-id has the
1366 * first 4 characters chopped off (where the pid_t sits).
1367 */
1368 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1369 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1370 return -1;
1371 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1372 }
1373
1374 __event_process_build_id(&bev, filename, session);
1375
1376 offset += bev.header.size;
1377 }
1378 err = 0;
1379out:
1380 return err;
1381}
1382
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001383static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1384 struct perf_header *ph __maybe_unused,
1385 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001386{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001387 ssize_t ret = trace_report(fd, data, false);
1388 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001389}
1390
1391static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001392 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001393 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001394{
1395 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1396 pr_debug("Failed to read buildids, continuing...\n");
1397 return 0;
1398}
1399
Namhyung Kima1ae5652012-09-24 17:14:59 +09001400static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001401 struct perf_header *ph, int fd,
1402 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001403{
1404 ph->env.hostname = do_read_string(fd, ph);
1405 return ph->env.hostname ? 0 : -ENOMEM;
1406}
1407
1408static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001409 struct perf_header *ph, int fd,
1410 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001411{
1412 ph->env.os_release = do_read_string(fd, ph);
1413 return ph->env.os_release ? 0 : -ENOMEM;
1414}
1415
1416static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001417 struct perf_header *ph, int fd,
1418 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001419{
1420 ph->env.version = do_read_string(fd, ph);
1421 return ph->env.version ? 0 : -ENOMEM;
1422}
1423
1424static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001425 struct perf_header *ph, int fd,
1426 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001427{
1428 ph->env.arch = do_read_string(fd, ph);
1429 return ph->env.arch ? 0 : -ENOMEM;
1430}
1431
1432static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001433 struct perf_header *ph, int fd,
1434 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001435{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001436 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001437 u32 nr;
1438
Namhyung Kim5323f602012-12-17 15:38:54 +09001439 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001440 if (ret != sizeof(nr))
1441 return -1;
1442
1443 if (ph->needs_swap)
1444 nr = bswap_32(nr);
1445
1446 ph->env.nr_cpus_online = nr;
1447
Namhyung Kim5323f602012-12-17 15:38:54 +09001448 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001449 if (ret != sizeof(nr))
1450 return -1;
1451
1452 if (ph->needs_swap)
1453 nr = bswap_32(nr);
1454
1455 ph->env.nr_cpus_avail = nr;
1456 return 0;
1457}
1458
1459static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001460 struct perf_header *ph, int fd,
1461 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001462{
1463 ph->env.cpu_desc = do_read_string(fd, ph);
1464 return ph->env.cpu_desc ? 0 : -ENOMEM;
1465}
1466
1467static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001468 struct perf_header *ph, int fd,
1469 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001470{
1471 ph->env.cpuid = do_read_string(fd, ph);
1472 return ph->env.cpuid ? 0 : -ENOMEM;
1473}
1474
1475static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001476 struct perf_header *ph, int fd,
1477 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001478{
1479 uint64_t mem;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001480 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001481
Namhyung Kim5323f602012-12-17 15:38:54 +09001482 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001483 if (ret != sizeof(mem))
1484 return -1;
1485
1486 if (ph->needs_swap)
1487 mem = bswap_64(mem);
1488
1489 ph->env.total_mem = mem;
1490 return 0;
1491}
1492
Robert Richter7c2f7af2012-08-16 21:10:23 +02001493static struct perf_evsel *
1494perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1495{
1496 struct perf_evsel *evsel;
1497
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001498 evlist__for_each(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001499 if (evsel->idx == idx)
1500 return evsel;
1501 }
1502
1503 return NULL;
1504}
1505
1506static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001507perf_evlist__set_event_name(struct perf_evlist *evlist,
1508 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001509{
1510 struct perf_evsel *evsel;
1511
1512 if (!event->name)
1513 return;
1514
1515 evsel = perf_evlist__find_by_index(evlist, event->idx);
1516 if (!evsel)
1517 return;
1518
1519 if (evsel->name)
1520 return;
1521
1522 evsel->name = strdup(event->name);
1523}
1524
1525static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001526process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001527 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001528 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001529{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001530 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001531 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1532
1533 if (!events)
1534 return 0;
1535
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001536 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001537 for (evsel = events; evsel->attr.size; evsel++)
1538 perf_evlist__set_event_name(session->evlist, evsel);
1539
1540 free_event_desc(events);
1541
1542 return 0;
1543}
1544
Namhyung Kima1ae5652012-09-24 17:14:59 +09001545static int process_cmdline(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001546 struct perf_header *ph, int fd,
1547 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001548{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001549 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001550 char *str;
1551 u32 nr, i;
1552 struct strbuf sb;
1553
Namhyung Kim5323f602012-12-17 15:38:54 +09001554 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001555 if (ret != sizeof(nr))
1556 return -1;
1557
1558 if (ph->needs_swap)
1559 nr = bswap_32(nr);
1560
1561 ph->env.nr_cmdline = nr;
1562 strbuf_init(&sb, 128);
1563
1564 for (i = 0; i < nr; i++) {
1565 str = do_read_string(fd, ph);
1566 if (!str)
1567 goto error;
1568
1569 /* include a NULL character at the end */
1570 strbuf_add(&sb, str, strlen(str) + 1);
1571 free(str);
1572 }
1573 ph->env.cmdline = strbuf_detach(&sb, NULL);
1574 return 0;
1575
1576error:
1577 strbuf_release(&sb);
1578 return -1;
1579}
1580
1581static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001582 struct perf_header *ph, int fd,
1583 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001584{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001585 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001586 u32 nr, i;
1587 char *str;
1588 struct strbuf sb;
1589
Namhyung Kim5323f602012-12-17 15:38:54 +09001590 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001591 if (ret != sizeof(nr))
1592 return -1;
1593
1594 if (ph->needs_swap)
1595 nr = bswap_32(nr);
1596
1597 ph->env.nr_sibling_cores = nr;
1598 strbuf_init(&sb, 128);
1599
1600 for (i = 0; i < nr; i++) {
1601 str = do_read_string(fd, ph);
1602 if (!str)
1603 goto error;
1604
1605 /* include a NULL character at the end */
1606 strbuf_add(&sb, str, strlen(str) + 1);
1607 free(str);
1608 }
1609 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1610
Namhyung Kim5323f602012-12-17 15:38:54 +09001611 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001612 if (ret != sizeof(nr))
1613 return -1;
1614
1615 if (ph->needs_swap)
1616 nr = bswap_32(nr);
1617
1618 ph->env.nr_sibling_threads = nr;
1619
1620 for (i = 0; i < nr; i++) {
1621 str = do_read_string(fd, ph);
1622 if (!str)
1623 goto error;
1624
1625 /* include a NULL character at the end */
1626 strbuf_add(&sb, str, strlen(str) + 1);
1627 free(str);
1628 }
1629 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1630 return 0;
1631
1632error:
1633 strbuf_release(&sb);
1634 return -1;
1635}
1636
1637static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001638 struct perf_header *ph, int fd,
1639 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001640{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001641 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001642 u32 nr, node, i;
1643 char *str;
1644 uint64_t mem_total, mem_free;
1645 struct strbuf sb;
1646
1647 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001648 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001649 if (ret != sizeof(nr))
1650 goto error;
1651
1652 if (ph->needs_swap)
1653 nr = bswap_32(nr);
1654
1655 ph->env.nr_numa_nodes = nr;
1656 strbuf_init(&sb, 256);
1657
1658 for (i = 0; i < nr; i++) {
1659 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001660 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001661 if (ret != sizeof(node))
1662 goto error;
1663
Namhyung Kim5323f602012-12-17 15:38:54 +09001664 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001665 if (ret != sizeof(u64))
1666 goto error;
1667
Namhyung Kim5323f602012-12-17 15:38:54 +09001668 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001669 if (ret != sizeof(u64))
1670 goto error;
1671
1672 if (ph->needs_swap) {
1673 node = bswap_32(node);
1674 mem_total = bswap_64(mem_total);
1675 mem_free = bswap_64(mem_free);
1676 }
1677
1678 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1679 node, mem_total, mem_free);
1680
1681 str = do_read_string(fd, ph);
1682 if (!str)
1683 goto error;
1684
1685 /* include a NULL character at the end */
1686 strbuf_add(&sb, str, strlen(str) + 1);
1687 free(str);
1688 }
1689 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1690 return 0;
1691
1692error:
1693 strbuf_release(&sb);
1694 return -1;
1695}
1696
1697static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001698 struct perf_header *ph, int fd,
1699 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001700{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001701 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001702 char *name;
1703 u32 pmu_num;
1704 u32 type;
1705 struct strbuf sb;
1706
Namhyung Kim5323f602012-12-17 15:38:54 +09001707 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001708 if (ret != sizeof(pmu_num))
1709 return -1;
1710
1711 if (ph->needs_swap)
1712 pmu_num = bswap_32(pmu_num);
1713
1714 if (!pmu_num) {
1715 pr_debug("pmu mappings not available\n");
1716 return 0;
1717 }
1718
1719 ph->env.nr_pmu_mappings = pmu_num;
1720 strbuf_init(&sb, 128);
1721
1722 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001723 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001724 goto error;
1725 if (ph->needs_swap)
1726 type = bswap_32(type);
1727
1728 name = do_read_string(fd, ph);
1729 if (!name)
1730 goto error;
1731
1732 strbuf_addf(&sb, "%u:%s", type, name);
1733 /* include a NULL character at the end */
1734 strbuf_add(&sb, "", 1);
1735
1736 free(name);
1737 pmu_num--;
1738 }
1739 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1740 return 0;
1741
1742error:
1743 strbuf_release(&sb);
1744 return -1;
1745}
1746
Namhyung Kima8bb5592013-01-22 18:09:31 +09001747static int process_group_desc(struct perf_file_section *section __maybe_unused,
1748 struct perf_header *ph, int fd,
1749 void *data __maybe_unused)
1750{
1751 size_t ret = -1;
1752 u32 i, nr, nr_groups;
1753 struct perf_session *session;
1754 struct perf_evsel *evsel, *leader = NULL;
1755 struct group_desc {
1756 char *name;
1757 u32 leader_idx;
1758 u32 nr_members;
1759 } *desc;
1760
1761 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
1762 return -1;
1763
1764 if (ph->needs_swap)
1765 nr_groups = bswap_32(nr_groups);
1766
1767 ph->env.nr_groups = nr_groups;
1768 if (!nr_groups) {
1769 pr_debug("group desc not available\n");
1770 return 0;
1771 }
1772
1773 desc = calloc(nr_groups, sizeof(*desc));
1774 if (!desc)
1775 return -1;
1776
1777 for (i = 0; i < nr_groups; i++) {
1778 desc[i].name = do_read_string(fd, ph);
1779 if (!desc[i].name)
1780 goto out_free;
1781
1782 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
1783 goto out_free;
1784
1785 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
1786 goto out_free;
1787
1788 if (ph->needs_swap) {
1789 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
1790 desc[i].nr_members = bswap_32(desc[i].nr_members);
1791 }
1792 }
1793
1794 /*
1795 * Rebuild group relationship based on the group_desc
1796 */
1797 session = container_of(ph, struct perf_session, header);
1798 session->evlist->nr_groups = nr_groups;
1799
1800 i = nr = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001801 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001802 if (evsel->idx == (int) desc[i].leader_idx) {
1803 evsel->leader = evsel;
1804 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09001805 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001806 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09001807 desc[i].name = NULL;
1808 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09001809 evsel->nr_members = desc[i].nr_members;
1810
1811 if (i >= nr_groups || nr > 0) {
1812 pr_debug("invalid group desc\n");
1813 goto out_free;
1814 }
1815
1816 leader = evsel;
1817 nr = evsel->nr_members - 1;
1818 i++;
1819 } else if (nr) {
1820 /* This is a group member */
1821 evsel->leader = leader;
1822
1823 nr--;
1824 }
1825 }
1826
1827 if (i != nr_groups || nr != 0) {
1828 pr_debug("invalid group desc\n");
1829 goto out_free;
1830 }
1831
1832 ret = 0;
1833out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09001834 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001835 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001836 free(desc);
1837
1838 return ret;
1839}
1840
Adrian Hunter99fa2982015-04-30 17:37:25 +03001841static int process_auxtrace(struct perf_file_section *section,
1842 struct perf_header *ph, int fd,
1843 void *data __maybe_unused)
1844{
1845 struct perf_session *session;
1846 int err;
1847
1848 session = container_of(ph, struct perf_session, header);
1849
1850 err = auxtrace_index__process(fd, section->size, session,
1851 ph->needs_swap);
1852 if (err < 0)
1853 pr_err("Failed to process auxtrace index\n");
1854 return err;
1855}
1856
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001857struct feature_ops {
1858 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1859 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01001860 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001861 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001862 const char *name;
1863 bool full_only;
1864};
1865
Robert Richter8cdfa782011-12-07 10:02:56 +01001866#define FEAT_OPA(n, func) \
1867 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01001868#define FEAT_OPP(n, func) \
1869 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1870 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01001871#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01001872 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09001873 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01001874
1875/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001876#define print_tracing_data NULL
1877#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001878
1879static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001880 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01001881 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001882 FEAT_OPP(HEADER_HOSTNAME, hostname),
1883 FEAT_OPP(HEADER_OSRELEASE, osrelease),
1884 FEAT_OPP(HEADER_VERSION, version),
1885 FEAT_OPP(HEADER_ARCH, arch),
1886 FEAT_OPP(HEADER_NRCPUS, nrcpus),
1887 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09001888 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001889 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02001890 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001891 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01001892 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1893 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01001894 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001895 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09001896 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Adrian Hunter99fa2982015-04-30 17:37:25 +03001897 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001898};
1899
1900struct header_print_data {
1901 FILE *fp;
1902 bool full; /* extended list of headers */
1903};
1904
1905static int perf_file_section__fprintf_info(struct perf_file_section *section,
1906 struct perf_header *ph,
1907 int feat, int fd, void *data)
1908{
1909 struct header_print_data *hd = data;
1910
1911 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1912 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1913 "%d, continuing...\n", section->offset, feat);
1914 return 0;
1915 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001916 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001917 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01001918 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001919 }
1920 if (!feat_ops[feat].print)
1921 return 0;
1922
1923 if (!feat_ops[feat].full_only || hd->full)
1924 feat_ops[feat].print(ph, fd, hd->fp);
1925 else
1926 fprintf(hd->fp, "# %s info available, use -I to display\n",
1927 feat_ops[feat].name);
1928
1929 return 0;
1930}
1931
1932int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1933{
1934 struct header_print_data hd;
1935 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001936 int fd = perf_data_file__fd(session->file);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001937 hd.fp = fp;
1938 hd.full = full;
1939
1940 perf_header__process_sections(header, fd, &hd,
1941 perf_file_section__fprintf_info);
1942 return 0;
1943}
1944
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001945static int do_write_feat(int fd, struct perf_header *h, int type,
1946 struct perf_file_section **p,
1947 struct perf_evlist *evlist)
1948{
1949 int err;
1950 int ret = 0;
1951
1952 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001953 if (!feat_ops[type].write)
1954 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001955
1956 (*p)->offset = lseek(fd, 0, SEEK_CUR);
1957
1958 err = feat_ops[type].write(fd, h, evlist);
1959 if (err < 0) {
1960 pr_debug("failed to write feature %d\n", type);
1961
1962 /* undo anything written */
1963 lseek(fd, (*p)->offset, SEEK_SET);
1964
1965 return -1;
1966 }
1967 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
1968 (*p)++;
1969 }
1970 return ret;
1971}
1972
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001973static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001974 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001975{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001976 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001977 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001978 int sec_size;
1979 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001980 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001981 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001982
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001983 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001984 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001985 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001986
Paul Gortmaker91b98802013-01-30 20:05:49 -05001987 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001988 if (feat_sec == NULL)
1989 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001990
1991 sec_size = sizeof(*feat_sec) * nr_sections;
1992
Jiri Olsa8d541e92013-07-17 19:49:44 +02001993 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08001994 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001995
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001996 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
1997 if (do_write_feat(fd, header, feat, &p, evlist))
1998 perf_header__clear_feat(header, feat);
1999 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002000
Xiao Guangrongf887f302010-02-04 16:46:42 +08002001 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002002 /*
2003 * may write more than needed due to dropped feature, but
2004 * this is okay, reader will skip the mising entries
2005 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002006 err = do_write(fd, feat_sec, sec_size);
2007 if (err < 0)
2008 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002009 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002010 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002011}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002012
Tom Zanussi8dc58102010-04-01 23:59:15 -05002013int perf_header__write_pipe(int fd)
2014{
2015 struct perf_pipe_file_header f_header;
2016 int err;
2017
2018 f_header = (struct perf_pipe_file_header){
2019 .magic = PERF_MAGIC,
2020 .size = sizeof(f_header),
2021 };
2022
2023 err = do_write(fd, &f_header, sizeof(f_header));
2024 if (err < 0) {
2025 pr_debug("failed to write perf pipe header\n");
2026 return err;
2027 }
2028
2029 return 0;
2030}
2031
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002032int perf_session__write_header(struct perf_session *session,
2033 struct perf_evlist *evlist,
2034 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002035{
2036 struct perf_file_header f_header;
2037 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002038 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002039 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002040 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002041 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002042
2043 lseek(fd, sizeof(f_header), SEEK_SET);
2044
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002045 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002046 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2047 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002048 if (err < 0) {
2049 pr_debug("failed to write perf header\n");
2050 return err;
2051 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002052 }
2053
Jiri Olsa944d62b2013-07-17 19:49:43 +02002054 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002055
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002056 evlist__for_each(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002057 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002058 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002059 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002060 .offset = evsel->id_offset,
2061 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002062 }
2063 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002064 err = do_write(fd, &f_attr, sizeof(f_attr));
2065 if (err < 0) {
2066 pr_debug("failed to write perf header attribute\n");
2067 return err;
2068 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002069 }
2070
Adrian Hunterd645c442013-12-11 14:36:28 +02002071 if (!header->data_offset)
2072 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002073 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002074
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002075 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002076 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002077 if (err < 0)
2078 return err;
2079 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002080
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002081 f_header = (struct perf_file_header){
2082 .magic = PERF_MAGIC,
2083 .size = sizeof(f_header),
2084 .attr_size = sizeof(f_attr),
2085 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002086 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002087 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002088 },
2089 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002090 .offset = header->data_offset,
2091 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002092 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002093 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002094 };
2095
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002096 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002097
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002098 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002099 err = do_write(fd, &f_header, sizeof(f_header));
2100 if (err < 0) {
2101 pr_debug("failed to write perf header\n");
2102 return err;
2103 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002104 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002105
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002106 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002107}
2108
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002109static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002110 int fd, void *buf, size_t size)
2111{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002112 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002113 return -1;
2114
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002115 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002116 mem_bswap_64(buf, size);
2117
2118 return 0;
2119}
2120
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002121int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002122 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002123 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002124 struct perf_header *ph,
2125 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002126{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002127 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002128 int nr_sections;
2129 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002130 int feat;
2131 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002132
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002133 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002134 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002135 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002136
Paul Gortmaker91b98802013-01-30 20:05:49 -05002137 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002138 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002139 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002140
2141 sec_size = sizeof(*feat_sec) * nr_sections;
2142
Jiri Olsa8d541e92013-07-17 19:49:44 +02002143 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002144
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002145 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2146 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002147 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002148
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002149 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2150 err = process(sec++, header, feat, fd, data);
2151 if (err < 0)
2152 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002153 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002154 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002155out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002156 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002157 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002158}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002159
Stephane Eranian114382a2012-02-09 23:21:08 +01002160static const int attr_file_abi_sizes[] = {
2161 [0] = PERF_ATTR_SIZE_VER0,
2162 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002163 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002164 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002165 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002166 0,
2167};
2168
2169/*
2170 * In the legacy file format, the magic number is not used to encode endianness.
2171 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2172 * on ABI revisions, we need to try all combinations for all endianness to
2173 * detect the endianness.
2174 */
2175static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2176{
2177 uint64_t ref_size, attr_size;
2178 int i;
2179
2180 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2181 ref_size = attr_file_abi_sizes[i]
2182 + sizeof(struct perf_file_section);
2183 if (hdr_sz != ref_size) {
2184 attr_size = bswap_64(hdr_sz);
2185 if (attr_size != ref_size)
2186 continue;
2187
2188 ph->needs_swap = true;
2189 }
2190 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2191 i,
2192 ph->needs_swap);
2193 return 0;
2194 }
2195 /* could not determine endianness */
2196 return -1;
2197}
2198
2199#define PERF_PIPE_HDR_VER0 16
2200
2201static const size_t attr_pipe_abi_sizes[] = {
2202 [0] = PERF_PIPE_HDR_VER0,
2203 0,
2204};
2205
2206/*
2207 * In the legacy pipe format, there is an implicit assumption that endiannesss
2208 * between host recording the samples, and host parsing the samples is the
2209 * same. This is not always the case given that the pipe output may always be
2210 * redirected into a file and analyzed on a different machine with possibly a
2211 * different endianness and perf_event ABI revsions in the perf tool itself.
2212 */
2213static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2214{
2215 u64 attr_size;
2216 int i;
2217
2218 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2219 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2220 attr_size = bswap_64(hdr_sz);
2221 if (attr_size != hdr_sz)
2222 continue;
2223
2224 ph->needs_swap = true;
2225 }
2226 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2227 return 0;
2228 }
2229 return -1;
2230}
2231
Feng Tange84ba4e2012-10-30 11:56:07 +08002232bool is_perf_magic(u64 magic)
2233{
2234 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2235 || magic == __perf_magic2
2236 || magic == __perf_magic2_sw)
2237 return true;
2238
2239 return false;
2240}
2241
Stephane Eranian114382a2012-02-09 23:21:08 +01002242static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2243 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002244{
2245 int ret;
2246
2247 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002248 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002249 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002250 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002251 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002252 if (is_pipe)
2253 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002254
Stephane Eranian114382a2012-02-09 23:21:08 +01002255 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002256 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002257 /*
2258 * the new magic number serves two purposes:
2259 * - unique number to identify actual perf.data files
2260 * - encode endianness of file
2261 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002262 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002263
Stephane Eranian114382a2012-02-09 23:21:08 +01002264 /* check magic number with one endianness */
2265 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002266 return 0;
2267
Stephane Eranian114382a2012-02-09 23:21:08 +01002268 /* check magic number with opposite endianness */
2269 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002270 return -1;
2271
2272 ph->needs_swap = true;
2273
2274 return 0;
2275}
2276
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002277int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002278 struct perf_header *ph, int fd)
2279{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002280 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002281
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002282 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002283
Stephane Eranian73323f52012-02-02 13:54:44 +01002284 ret = readn(fd, header, sizeof(*header));
2285 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002286 return -1;
2287
Stephane Eranian114382a2012-02-09 23:21:08 +01002288 if (check_magic_endian(header->magic,
2289 header->attr_size, false, ph) < 0) {
2290 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002291 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002292 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002293
Stephane Eranian73323f52012-02-02 13:54:44 +01002294 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002295 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002296 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002297 }
2298
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002299 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002300 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002301 if (header->size == offsetof(typeof(*header), adds_features))
2302 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002303 else
2304 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002305 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002306 /*
2307 * feature bitmap is declared as an array of unsigned longs --
2308 * not good since its size can differ between the host that
2309 * generated the data file and the host analyzing the file.
2310 *
2311 * We need to handle endianness, but we don't know the size of
2312 * the unsigned long where the file was generated. Take a best
2313 * guess at determining it: try 64-bit swap first (ie., file
2314 * created on a 64-bit host), and check if the hostname feature
2315 * bit is set (this feature bit is forced on as of fbe96f2).
2316 * If the bit is not, undo the 64-bit swap and try a 32-bit
2317 * swap. If the hostname bit is still not set (e.g., older data
2318 * file), punt and fallback to the original behavior --
2319 * clearing all feature bits and setting buildid.
2320 */
David Ahern80c01202012-06-08 11:47:51 -03002321 mem_bswap_64(&header->adds_features,
2322 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002323
2324 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002325 /* unswap as u64 */
2326 mem_bswap_64(&header->adds_features,
2327 BITS_TO_U64(HEADER_FEAT_BITS));
2328
2329 /* unswap as u32 */
2330 mem_bswap_32(&header->adds_features,
2331 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002332 }
2333
2334 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2335 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2336 set_bit(HEADER_BUILD_ID, header->adds_features);
2337 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002338 }
2339
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002340 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002341 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002342
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002343 ph->data_offset = header->data.offset;
2344 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002345 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002346 return 0;
2347}
2348
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002349static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002350 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002351 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002352{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002353 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002354 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002355 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002356 return 0;
2357 }
2358
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002359 if (feat >= HEADER_LAST_FEATURE) {
2360 pr_debug("unknown feature %d, continuing...\n", feat);
2361 return 0;
2362 }
2363
Robert Richterf1c67db2012-02-10 15:41:56 +01002364 if (!feat_ops[feat].process)
2365 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002366
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002367 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002368}
2369
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002370static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002371 struct perf_header *ph, int fd,
2372 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002373{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002374 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002375
2376 ret = readn(fd, header, sizeof(*header));
2377 if (ret <= 0)
2378 return -1;
2379
Stephane Eranian114382a2012-02-09 23:21:08 +01002380 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2381 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002382 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002383 }
2384
2385 if (ph->needs_swap)
2386 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002387
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002388 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002389 return -1;
2390
Tom Zanussi8dc58102010-04-01 23:59:15 -05002391 return 0;
2392}
2393
Jiri Olsad4339562013-07-17 19:49:41 +02002394static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002395{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002396 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002397 struct perf_pipe_file_header f_header;
2398
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002399 if (perf_file_header__read_pipe(&f_header, header,
2400 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002401 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002402 pr_debug("incompatible file format\n");
2403 return -EINVAL;
2404 }
2405
Tom Zanussi8dc58102010-04-01 23:59:15 -05002406 return 0;
2407}
2408
Stephane Eranian69996df2012-02-09 23:21:06 +01002409static int read_attr(int fd, struct perf_header *ph,
2410 struct perf_file_attr *f_attr)
2411{
2412 struct perf_event_attr *attr = &f_attr->attr;
2413 size_t sz, left;
2414 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002415 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002416
2417 memset(f_attr, 0, sizeof(*f_attr));
2418
2419 /* read minimal guaranteed structure */
2420 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2421 if (ret <= 0) {
2422 pr_debug("cannot read %d bytes of header attr\n",
2423 PERF_ATTR_SIZE_VER0);
2424 return -1;
2425 }
2426
2427 /* on file perf_event_attr size */
2428 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002429
Stephane Eranian69996df2012-02-09 23:21:06 +01002430 if (ph->needs_swap)
2431 sz = bswap_32(sz);
2432
2433 if (sz == 0) {
2434 /* assume ABI0 */
2435 sz = PERF_ATTR_SIZE_VER0;
2436 } else if (sz > our_sz) {
2437 pr_debug("file uses a more recent and unsupported ABI"
2438 " (%zu bytes extra)\n", sz - our_sz);
2439 return -1;
2440 }
2441 /* what we have not yet read and that we know about */
2442 left = sz - PERF_ATTR_SIZE_VER0;
2443 if (left) {
2444 void *ptr = attr;
2445 ptr += PERF_ATTR_SIZE_VER0;
2446
2447 ret = readn(fd, ptr, left);
2448 }
2449 /* read perf_file_section, ids are read in caller */
2450 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2451
2452 return ret <= 0 ? -1 : 0;
2453}
2454
Namhyung Kim831394b2012-09-06 11:10:46 +09002455static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2456 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002457{
Namhyung Kim831394b2012-09-06 11:10:46 +09002458 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002459 char bf[128];
2460
Namhyung Kim831394b2012-09-06 11:10:46 +09002461 /* already prepared */
2462 if (evsel->tp_format)
2463 return 0;
2464
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002465 if (pevent == NULL) {
2466 pr_debug("broken or missing trace data\n");
2467 return -1;
2468 }
2469
Namhyung Kim831394b2012-09-06 11:10:46 +09002470 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002471 if (event == NULL)
2472 return -1;
2473
Namhyung Kim831394b2012-09-06 11:10:46 +09002474 if (!evsel->name) {
2475 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2476 evsel->name = strdup(bf);
2477 if (evsel->name == NULL)
2478 return -1;
2479 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002480
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002481 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002482 return 0;
2483}
2484
Namhyung Kim831394b2012-09-06 11:10:46 +09002485static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2486 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002487{
2488 struct perf_evsel *pos;
2489
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002490 evlist__for_each(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002491 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2492 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002493 return -1;
2494 }
2495
2496 return 0;
2497}
2498
Jiri Olsad4339562013-07-17 19:49:41 +02002499int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002500{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002501 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002502 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002503 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002504 struct perf_file_attr f_attr;
2505 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002506 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002507 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002508
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002509 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002510 if (session->evlist == NULL)
2511 return -ENOMEM;
2512
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002513 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002514 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002515
Stephane Eranian69996df2012-02-09 23:21:06 +01002516 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002517 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002518
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002519 /*
2520 * Sanity check that perf.data was written cleanly; data size is
2521 * initialized to 0 and updated only if the on_exit function is run.
2522 * If data size is still 0 then the file contains only partial
2523 * information. Just warn user and process it as much as it can.
2524 */
2525 if (f_header.data.size == 0) {
2526 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2527 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002528 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002529 }
2530
Stephane Eranian69996df2012-02-09 23:21:06 +01002531 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002532 lseek(fd, f_header.attrs.offset, SEEK_SET);
2533
2534 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002535 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002536 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002537
Stephane Eranian69996df2012-02-09 23:21:06 +01002538 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002539 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002540
David Ahern1060ab82015-04-09 16:15:46 -04002541 if (header->needs_swap) {
2542 f_attr.ids.size = bswap_64(f_attr.ids.size);
2543 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002544 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002545 }
David Aherneda39132011-07-15 12:34:09 -06002546
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002547 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002548 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002549
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002550 if (evsel == NULL)
2551 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002552
2553 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002554 /*
2555 * Do it before so that if perf_evsel__alloc_id fails, this
2556 * entry gets purged too at perf_evlist__delete().
2557 */
2558 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002559
2560 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002561 /*
2562 * We don't have the cpu and thread maps on the header, so
2563 * for allocating the perf_sample_id table we fake 1 cpu and
2564 * hattr->ids threads.
2565 */
2566 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2567 goto out_delete_evlist;
2568
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002569 lseek(fd, f_attr.ids.offset, SEEK_SET);
2570
2571 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002572 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002573 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002574
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002575 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002576 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002577
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002578 lseek(fd, tmp, SEEK_SET);
2579 }
2580
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002581 symbol_conf.nr_events = nr_attrs;
2582
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002583 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002584 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002585
Namhyung Kim831394b2012-09-06 11:10:46 +09002586 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002587 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002588 goto out_delete_evlist;
2589
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002590 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002591out_errno:
2592 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002593
2594out_delete_evlist:
2595 perf_evlist__delete(session->evlist);
2596 session->evlist = NULL;
2597 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002598}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002599
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002600int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002601 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002602 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002603{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002604 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002605 size_t size;
2606 int err;
2607
2608 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002609 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002610 size += sizeof(struct perf_event_header);
2611 size += ids * sizeof(u64);
2612
2613 ev = malloc(size);
2614
Chris Samuelce47dc52010-11-13 13:35:06 +11002615 if (ev == NULL)
2616 return -ENOMEM;
2617
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002618 ev->attr.attr = *attr;
2619 memcpy(ev->attr.id, id, ids * sizeof(u64));
2620
2621 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002622 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002623
Robert Richterf4d83432012-08-16 21:10:17 +02002624 if (ev->attr.header.size == size)
2625 err = process(tool, ev, NULL, NULL);
2626 else
2627 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002628
2629 free(ev);
2630
2631 return err;
2632}
2633
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002634int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002635 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002636 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002637{
Robert Richter6606f872012-08-16 21:10:19 +02002638 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002639 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002640
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002641 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002642 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2643 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002644 if (err) {
2645 pr_debug("failed to create perf header attribute\n");
2646 return err;
2647 }
2648 }
2649
2650 return err;
2651}
2652
Adrian Hunter47c3d102013-07-04 16:20:21 +03002653int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2654 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002655 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002656{
Robert Richterf4d83432012-08-16 21:10:17 +02002657 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002658 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002659 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002660
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002661 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002662 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002663 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002664 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002665 }
2666
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002667 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002668 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002669 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002670
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002671 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002672
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002673 ids = event->header.size;
2674 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002675 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002676 /*
2677 * We don't have the cpu and thread maps on the header, so
2678 * for allocating the perf_sample_id table we fake 1 cpu and
2679 * hattr->ids threads.
2680 */
2681 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2682 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002683
2684 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002685 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002686 }
2687
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03002688 symbol_conf.nr_events = evlist->nr_entries;
2689
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002690 return 0;
2691}
Tom Zanussicd19a032010-04-01 23:59:20 -05002692
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002693int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002694 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002695 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002696{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002697 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002698 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002699 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002700 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002701
Jiri Olsa29208e52011-10-20 15:59:43 +02002702 /*
2703 * We are going to store the size of the data followed
2704 * by the data contents. Since the fd descriptor is a pipe,
2705 * we cannot seek back to store the size of the data once
2706 * we know it. Instead we:
2707 *
2708 * - write the tracing data to the temp file
2709 * - get/write the data size to pipe
2710 * - write the tracing data from the temp file
2711 * to the pipe
2712 */
2713 tdata = tracing_data_get(&evlist->entries, fd, true);
2714 if (!tdata)
2715 return -1;
2716
Tom Zanussi92155452010-04-01 23:59:21 -05002717 memset(&ev, 0, sizeof(ev));
2718
2719 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002720 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002721 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05002722 padding = aligned_size - size;
2723 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2724 ev.tracing_data.size = aligned_size;
2725
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002726 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002727
Jiri Olsa29208e52011-10-20 15:59:43 +02002728 /*
2729 * The put function will copy all the tracing data
2730 * stored in temp file to the pipe.
2731 */
2732 tracing_data_put(tdata);
2733
Tom Zanussi92155452010-04-01 23:59:21 -05002734 write_padded(fd, NULL, 0, padding);
2735
2736 return aligned_size;
2737}
2738
Adrian Hunter47c3d102013-07-04 16:20:21 +03002739int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2740 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002741 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002742{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002743 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002744 int fd = perf_data_file__fd(session->file);
2745 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05002746 char buf[BUFSIZ];
2747
2748 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002749 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05002750 SEEK_SET);
2751
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002752 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002753 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002754 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05002755
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002756 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002757 pr_err("%s: reading input file", __func__);
2758 return -1;
2759 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002760 if (session->repipe) {
2761 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002762 if (retw <= 0 || retw != padding) {
2763 pr_err("%s: repiping tracing data padding", __func__);
2764 return -1;
2765 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002766 }
Tom Zanussi92155452010-04-01 23:59:21 -05002767
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002768 if (size_read + padding != size) {
2769 pr_err("%s: tracing data size mismatch", __func__);
2770 return -1;
2771 }
Tom Zanussi92155452010-04-01 23:59:21 -05002772
Namhyung Kim831394b2012-09-06 11:10:46 +09002773 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002774 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03002775
Tom Zanussi92155452010-04-01 23:59:21 -05002776 return size_read + padding;
2777}
Tom Zanussic7929e42010-04-01 23:59:22 -05002778
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002779int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002780 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002781 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002782 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05002783{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002784 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05002785 size_t len;
2786 int err = 0;
2787
2788 if (!pos->hit)
2789 return err;
2790
2791 memset(&ev, 0, sizeof(ev));
2792
2793 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002794 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05002795 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2796 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2797 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002798 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05002799 ev.build_id.header.size = sizeof(ev.build_id) + len;
2800 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2801
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002802 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05002803
2804 return err;
2805}
2806
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002807int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002808 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002809 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05002810{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002811 __event_process_build_id(&event->build_id,
2812 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002813 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05002814 return 0;
2815}