blob: de5f4669ba5f0030768df55e409043960e8f3bd8 [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
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900872static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
873 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200874{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900875 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200876}
877
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900878static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
879 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200880{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900881 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200882}
883
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900884static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200885{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900886 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200887}
888
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900889static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
890 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200891{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900892 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200893}
894
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900895static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
896 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200897{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900898 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
899 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200900}
901
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900902static void print_version(struct perf_header *ph, int fd __maybe_unused,
903 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200904{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900905 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200906}
907
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900908static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
909 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200910{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900911 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200912 char *str;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200913
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900914 nr = ph->env.nr_cmdline;
915 str = ph->env.cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200916
917 fprintf(fp, "# cmdline : ");
918
919 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200920 fprintf(fp, "%s ", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900921 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200922 }
923 fputc('\n', fp);
924}
925
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900926static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
927 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200928{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900929 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200930 char *str;
931
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900932 nr = ph->env.nr_sibling_cores;
933 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200934
935 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200936 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900937 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200938 }
939
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900940 nr = ph->env.nr_sibling_threads;
941 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200942
943 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200944 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900945 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200946 }
947}
948
Robert Richter4e1b9c62012-08-16 21:10:22 +0200949static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200950{
Robert Richter4e1b9c62012-08-16 21:10:22 +0200951 struct perf_evsel *evsel;
952
953 if (!events)
954 return;
955
956 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300957 zfree(&evsel->name);
958 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +0200959 }
960
961 free(events);
962}
963
964static struct perf_evsel *
965read_event_desc(struct perf_header *ph, int fd)
966{
967 struct perf_evsel *evsel, *events = NULL;
968 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200969 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +0100970 u32 nre, sz, nr, i, j;
971 ssize_t ret;
972 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200973
974 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +0900975 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200976 if (ret != (ssize_t)sizeof(nre))
977 goto error;
978
979 if (ph->needs_swap)
980 nre = bswap_32(nre);
981
Namhyung Kim5323f602012-12-17 15:38:54 +0900982 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200983 if (ret != (ssize_t)sizeof(sz))
984 goto error;
985
986 if (ph->needs_swap)
987 sz = bswap_32(sz);
988
Stephane Eranian62db9062012-02-09 23:21:07 +0100989 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200990 buf = malloc(sz);
991 if (!buf)
992 goto error;
993
Robert Richter4e1b9c62012-08-16 21:10:22 +0200994 /* the last event terminates with evsel->attr.size == 0: */
995 events = calloc(nre + 1, sizeof(*events));
996 if (!events)
997 goto error;
998
999 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001000 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001001 msz = sz;
1002
Robert Richter4e1b9c62012-08-16 21:10:22 +02001003 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1004 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001005
Stephane Eranian62db9062012-02-09 23:21:07 +01001006 /*
1007 * must read entire on-file attr struct to
1008 * sync up with layout.
1009 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001010 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001011 if (ret != (ssize_t)sz)
1012 goto error;
1013
1014 if (ph->needs_swap)
1015 perf_event__attr_swap(buf);
1016
Robert Richter4e1b9c62012-08-16 21:10:22 +02001017 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001018
Namhyung Kim5323f602012-12-17 15:38:54 +09001019 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001020 if (ret != (ssize_t)sizeof(nr))
1021 goto error;
1022
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001023 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001024 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001025 evsel->needs_swap = true;
1026 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001027
Robert Richter4e1b9c62012-08-16 21:10:22 +02001028 evsel->name = do_read_string(fd, ph);
1029
1030 if (!nr)
1031 continue;
1032
1033 id = calloc(nr, sizeof(*id));
1034 if (!id)
1035 goto error;
1036 evsel->ids = nr;
1037 evsel->id = id;
1038
1039 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001040 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001041 if (ret != (ssize_t)sizeof(*id))
1042 goto error;
1043 if (ph->needs_swap)
1044 *id = bswap_64(*id);
1045 id++;
1046 }
1047 }
1048out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001049 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001050 return events;
1051error:
1052 if (events)
1053 free_event_desc(events);
1054 events = NULL;
1055 goto out;
1056}
1057
1058static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1059{
1060 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1061 u32 j;
1062 u64 *id;
1063
1064 if (!events) {
1065 fprintf(fp, "# event desc: not available or unable to read\n");
1066 return;
1067 }
1068
1069 for (evsel = events; evsel->attr.size; evsel++) {
1070 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001071
1072 fprintf(fp, "type = %d, config = 0x%"PRIx64
1073 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
Robert Richter4e1b9c62012-08-16 21:10:22 +02001074 evsel->attr.type,
1075 (u64)evsel->attr.config,
1076 (u64)evsel->attr.config1,
1077 (u64)evsel->attr.config2);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001078
1079 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001080 evsel->attr.exclude_user,
1081 evsel->attr.exclude_kernel);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001082
David Ahern78b961f2012-07-20 17:25:52 -06001083 fprintf(fp, ", excl_host = %d, excl_guest = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001084 evsel->attr.exclude_host,
1085 evsel->attr.exclude_guest);
David Ahern78b961f2012-07-20 17:25:52 -06001086
Robert Richter4e1b9c62012-08-16 21:10:22 +02001087 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
David Ahern78b961f2012-07-20 17:25:52 -06001088
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001089 fprintf(fp, ", attr_mmap2 = %d", evsel->attr.mmap2);
1090 fprintf(fp, ", attr_mmap = %d", evsel->attr.mmap);
1091 fprintf(fp, ", attr_mmap_data = %d", evsel->attr.mmap_data);
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 if (evsel->attr.use_clockid)
1102 fprintf(fp, ", clockid = %d", evsel->attr.clockid);
1103
Robert Richter4e1b9c62012-08-16 21:10:22 +02001104
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001105 fputc('\n', fp);
1106 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001107
1108 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001109}
1110
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001111static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001112 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001113{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001114 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001115}
1116
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001117static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001118 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001119{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001120 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001121 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001122 uint64_t mem_total, mem_free;
1123
1124 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001125 nr = ph->env.nr_numa_nodes;
1126 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001127
1128 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001129 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001130 c = strtoul(str, &tmp, 0);
1131 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001132 goto error;
1133
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001134 str = tmp + 1;
1135 mem_total = strtoull(str, &tmp, 0);
1136 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001137 goto error;
1138
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001139 str = tmp + 1;
1140 mem_free = strtoull(str, &tmp, 0);
1141 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001142 goto error;
1143
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001144 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1145 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001146 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001147
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001148 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001149 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001150
1151 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001152 }
1153 return;
1154error:
1155 fprintf(fp, "# numa topology : not available\n");
1156}
1157
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001158static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001159{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001160 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001161}
1162
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001163static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001164 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001165{
1166 fprintf(fp, "# contains samples with branch stack\n");
1167}
1168
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001169static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1170 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001171{
1172 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001173 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001174 u32 pmu_num;
1175 u32 type;
1176
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001177 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001178 if (!pmu_num) {
1179 fprintf(fp, "# pmu mappings: not available\n");
1180 return;
1181 }
1182
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001183 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001184
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001185 while (pmu_num) {
1186 type = strtoul(str, &tmp, 0);
1187 if (*tmp != ':')
1188 goto error;
1189
1190 str = tmp + 1;
1191 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1192
Robert Richter50a96672012-08-16 21:10:24 +02001193 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001194 str += strlen(str) + 1;
1195 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001196 }
1197
1198 fprintf(fp, "\n");
1199
1200 if (!pmu_num)
1201 return;
1202error:
1203 fprintf(fp, "# pmu mappings: unable to read\n");
1204}
1205
Namhyung Kima8bb5592013-01-22 18:09:31 +09001206static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1207 FILE *fp)
1208{
1209 struct perf_session *session;
1210 struct perf_evsel *evsel;
1211 u32 nr = 0;
1212
1213 session = container_of(ph, struct perf_session, header);
1214
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001215 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001216 if (perf_evsel__is_group_leader(evsel) &&
1217 evsel->nr_members > 1) {
1218 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1219 perf_evsel__name(evsel));
1220
1221 nr = evsel->nr_members - 1;
1222 } else if (nr) {
1223 fprintf(fp, ",%s", perf_evsel__name(evsel));
1224
1225 if (--nr == 0)
1226 fprintf(fp, "}\n");
1227 }
1228 }
1229}
1230
Robert Richter08d95bd2012-02-10 15:41:55 +01001231static int __event_process_build_id(struct build_id_event *bev,
1232 char *filename,
1233 struct perf_session *session)
1234{
1235 int err = -1;
Waiman Long8fa7d872014-09-29 16:07:28 -04001236 struct dsos *dsos;
Robert Richter08d95bd2012-02-10 15:41:55 +01001237 struct machine *machine;
1238 u16 misc;
1239 struct dso *dso;
1240 enum dso_kernel_type dso_type;
1241
1242 machine = perf_session__findnew_machine(session, bev->pid);
1243 if (!machine)
1244 goto out;
1245
1246 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1247
1248 switch (misc) {
1249 case PERF_RECORD_MISC_KERNEL:
1250 dso_type = DSO_TYPE_KERNEL;
Waiman Long8fa7d872014-09-29 16:07:28 -04001251 dsos = &machine->kernel_dsos;
Robert Richter08d95bd2012-02-10 15:41:55 +01001252 break;
1253 case PERF_RECORD_MISC_GUEST_KERNEL:
1254 dso_type = DSO_TYPE_GUEST_KERNEL;
Waiman Long8fa7d872014-09-29 16:07:28 -04001255 dsos = &machine->kernel_dsos;
Robert Richter08d95bd2012-02-10 15:41:55 +01001256 break;
1257 case PERF_RECORD_MISC_USER:
1258 case PERF_RECORD_MISC_GUEST_USER:
1259 dso_type = DSO_TYPE_USER;
Waiman Long8fa7d872014-09-29 16:07:28 -04001260 dsos = &machine->user_dsos;
Robert Richter08d95bd2012-02-10 15:41:55 +01001261 break;
1262 default:
1263 goto out;
1264 }
1265
Waiman Long8fa7d872014-09-29 16:07:28 -04001266 dso = __dsos__findnew(dsos, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001267 if (dso != NULL) {
1268 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1269
1270 dso__set_build_id(dso, &bev->build_id);
1271
Jiri Olsae746b3e2015-02-12 22:16:34 +01001272 if (!is_kernel_module(filename))
Robert Richter08d95bd2012-02-10 15:41:55 +01001273 dso->kernel = dso_type;
1274
1275 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1276 sbuild_id);
1277 pr_debug("build id event received for %s: %s\n",
1278 dso->long_name, sbuild_id);
1279 }
1280
1281 err = 0;
1282out:
1283 return err;
1284}
1285
1286static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1287 int input, u64 offset, u64 size)
1288{
1289 struct perf_session *session = container_of(header, struct perf_session, header);
1290 struct {
1291 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001292 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001293 char filename[0];
1294 } old_bev;
1295 struct build_id_event bev;
1296 char filename[PATH_MAX];
1297 u64 limit = offset + size;
1298
1299 while (offset < limit) {
1300 ssize_t len;
1301
Namhyung Kim5323f602012-12-17 15:38:54 +09001302 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001303 return -1;
1304
1305 if (header->needs_swap)
1306 perf_event_header__bswap(&old_bev.header);
1307
1308 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001309 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001310 return -1;
1311
1312 bev.header = old_bev.header;
1313
1314 /*
1315 * As the pid is the missing value, we need to fill
1316 * it properly. The header.misc value give us nice hint.
1317 */
1318 bev.pid = HOST_KERNEL_ID;
1319 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1320 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1321 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1322
1323 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1324 __event_process_build_id(&bev, filename, session);
1325
1326 offset += bev.header.size;
1327 }
1328
1329 return 0;
1330}
1331
1332static int perf_header__read_build_ids(struct perf_header *header,
1333 int input, u64 offset, u64 size)
1334{
1335 struct perf_session *session = container_of(header, struct perf_session, header);
1336 struct build_id_event bev;
1337 char filename[PATH_MAX];
1338 u64 limit = offset + size, orig_offset = offset;
1339 int err = -1;
1340
1341 while (offset < limit) {
1342 ssize_t len;
1343
Namhyung Kim5323f602012-12-17 15:38:54 +09001344 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001345 goto out;
1346
1347 if (header->needs_swap)
1348 perf_event_header__bswap(&bev.header);
1349
1350 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001351 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001352 goto out;
1353 /*
1354 * The a1645ce1 changeset:
1355 *
1356 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1357 *
1358 * Added a field to struct build_id_event that broke the file
1359 * format.
1360 *
1361 * Since the kernel build-id is the first entry, process the
1362 * table using the old format if the well known
1363 * '[kernel.kallsyms]' string for the kernel build-id has the
1364 * first 4 characters chopped off (where the pid_t sits).
1365 */
1366 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1367 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1368 return -1;
1369 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1370 }
1371
1372 __event_process_build_id(&bev, filename, session);
1373
1374 offset += bev.header.size;
1375 }
1376 err = 0;
1377out:
1378 return err;
1379}
1380
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001381static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1382 struct perf_header *ph __maybe_unused,
1383 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001384{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001385 ssize_t ret = trace_report(fd, data, false);
1386 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001387}
1388
1389static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001390 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001391 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001392{
1393 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1394 pr_debug("Failed to read buildids, continuing...\n");
1395 return 0;
1396}
1397
Namhyung Kima1ae5652012-09-24 17:14:59 +09001398static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001399 struct perf_header *ph, int fd,
1400 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001401{
1402 ph->env.hostname = do_read_string(fd, ph);
1403 return ph->env.hostname ? 0 : -ENOMEM;
1404}
1405
1406static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001407 struct perf_header *ph, int fd,
1408 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001409{
1410 ph->env.os_release = do_read_string(fd, ph);
1411 return ph->env.os_release ? 0 : -ENOMEM;
1412}
1413
1414static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001415 struct perf_header *ph, int fd,
1416 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001417{
1418 ph->env.version = do_read_string(fd, ph);
1419 return ph->env.version ? 0 : -ENOMEM;
1420}
1421
1422static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001423 struct perf_header *ph, int fd,
1424 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001425{
1426 ph->env.arch = do_read_string(fd, ph);
1427 return ph->env.arch ? 0 : -ENOMEM;
1428}
1429
1430static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001431 struct perf_header *ph, int fd,
1432 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001433{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001434 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001435 u32 nr;
1436
Namhyung Kim5323f602012-12-17 15:38:54 +09001437 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001438 if (ret != sizeof(nr))
1439 return -1;
1440
1441 if (ph->needs_swap)
1442 nr = bswap_32(nr);
1443
1444 ph->env.nr_cpus_online = nr;
1445
Namhyung Kim5323f602012-12-17 15:38:54 +09001446 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001447 if (ret != sizeof(nr))
1448 return -1;
1449
1450 if (ph->needs_swap)
1451 nr = bswap_32(nr);
1452
1453 ph->env.nr_cpus_avail = nr;
1454 return 0;
1455}
1456
1457static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001458 struct perf_header *ph, int fd,
1459 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001460{
1461 ph->env.cpu_desc = do_read_string(fd, ph);
1462 return ph->env.cpu_desc ? 0 : -ENOMEM;
1463}
1464
1465static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001466 struct perf_header *ph, int fd,
1467 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001468{
1469 ph->env.cpuid = do_read_string(fd, ph);
1470 return ph->env.cpuid ? 0 : -ENOMEM;
1471}
1472
1473static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001474 struct perf_header *ph, int fd,
1475 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001476{
1477 uint64_t mem;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001478 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001479
Namhyung Kim5323f602012-12-17 15:38:54 +09001480 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001481 if (ret != sizeof(mem))
1482 return -1;
1483
1484 if (ph->needs_swap)
1485 mem = bswap_64(mem);
1486
1487 ph->env.total_mem = mem;
1488 return 0;
1489}
1490
Robert Richter7c2f7af2012-08-16 21:10:23 +02001491static struct perf_evsel *
1492perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1493{
1494 struct perf_evsel *evsel;
1495
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001496 evlist__for_each(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001497 if (evsel->idx == idx)
1498 return evsel;
1499 }
1500
1501 return NULL;
1502}
1503
1504static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001505perf_evlist__set_event_name(struct perf_evlist *evlist,
1506 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001507{
1508 struct perf_evsel *evsel;
1509
1510 if (!event->name)
1511 return;
1512
1513 evsel = perf_evlist__find_by_index(evlist, event->idx);
1514 if (!evsel)
1515 return;
1516
1517 if (evsel->name)
1518 return;
1519
1520 evsel->name = strdup(event->name);
1521}
1522
1523static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001524process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001525 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001526 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001527{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001528 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001529 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1530
1531 if (!events)
1532 return 0;
1533
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001534 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001535 for (evsel = events; evsel->attr.size; evsel++)
1536 perf_evlist__set_event_name(session->evlist, evsel);
1537
1538 free_event_desc(events);
1539
1540 return 0;
1541}
1542
Namhyung Kima1ae5652012-09-24 17:14:59 +09001543static int process_cmdline(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001544 struct perf_header *ph, int fd,
1545 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001546{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001547 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001548 char *str;
1549 u32 nr, i;
1550 struct strbuf sb;
1551
Namhyung Kim5323f602012-12-17 15:38:54 +09001552 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001553 if (ret != sizeof(nr))
1554 return -1;
1555
1556 if (ph->needs_swap)
1557 nr = bswap_32(nr);
1558
1559 ph->env.nr_cmdline = nr;
1560 strbuf_init(&sb, 128);
1561
1562 for (i = 0; i < nr; i++) {
1563 str = do_read_string(fd, ph);
1564 if (!str)
1565 goto error;
1566
1567 /* include a NULL character at the end */
1568 strbuf_add(&sb, str, strlen(str) + 1);
1569 free(str);
1570 }
1571 ph->env.cmdline = strbuf_detach(&sb, NULL);
1572 return 0;
1573
1574error:
1575 strbuf_release(&sb);
1576 return -1;
1577}
1578
1579static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001580 struct perf_header *ph, int fd,
1581 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001582{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001583 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001584 u32 nr, i;
1585 char *str;
1586 struct strbuf sb;
1587
Namhyung Kim5323f602012-12-17 15:38:54 +09001588 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001589 if (ret != sizeof(nr))
1590 return -1;
1591
1592 if (ph->needs_swap)
1593 nr = bswap_32(nr);
1594
1595 ph->env.nr_sibling_cores = nr;
1596 strbuf_init(&sb, 128);
1597
1598 for (i = 0; i < nr; i++) {
1599 str = do_read_string(fd, ph);
1600 if (!str)
1601 goto error;
1602
1603 /* include a NULL character at the end */
1604 strbuf_add(&sb, str, strlen(str) + 1);
1605 free(str);
1606 }
1607 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1608
Namhyung Kim5323f602012-12-17 15:38:54 +09001609 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001610 if (ret != sizeof(nr))
1611 return -1;
1612
1613 if (ph->needs_swap)
1614 nr = bswap_32(nr);
1615
1616 ph->env.nr_sibling_threads = nr;
1617
1618 for (i = 0; i < nr; i++) {
1619 str = do_read_string(fd, ph);
1620 if (!str)
1621 goto error;
1622
1623 /* include a NULL character at the end */
1624 strbuf_add(&sb, str, strlen(str) + 1);
1625 free(str);
1626 }
1627 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1628 return 0;
1629
1630error:
1631 strbuf_release(&sb);
1632 return -1;
1633}
1634
1635static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001636 struct perf_header *ph, int fd,
1637 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001638{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001639 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001640 u32 nr, node, i;
1641 char *str;
1642 uint64_t mem_total, mem_free;
1643 struct strbuf sb;
1644
1645 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001646 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001647 if (ret != sizeof(nr))
1648 goto error;
1649
1650 if (ph->needs_swap)
1651 nr = bswap_32(nr);
1652
1653 ph->env.nr_numa_nodes = nr;
1654 strbuf_init(&sb, 256);
1655
1656 for (i = 0; i < nr; i++) {
1657 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001658 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001659 if (ret != sizeof(node))
1660 goto error;
1661
Namhyung Kim5323f602012-12-17 15:38:54 +09001662 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001663 if (ret != sizeof(u64))
1664 goto error;
1665
Namhyung Kim5323f602012-12-17 15:38:54 +09001666 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001667 if (ret != sizeof(u64))
1668 goto error;
1669
1670 if (ph->needs_swap) {
1671 node = bswap_32(node);
1672 mem_total = bswap_64(mem_total);
1673 mem_free = bswap_64(mem_free);
1674 }
1675
1676 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1677 node, mem_total, mem_free);
1678
1679 str = do_read_string(fd, ph);
1680 if (!str)
1681 goto error;
1682
1683 /* include a NULL character at the end */
1684 strbuf_add(&sb, str, strlen(str) + 1);
1685 free(str);
1686 }
1687 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1688 return 0;
1689
1690error:
1691 strbuf_release(&sb);
1692 return -1;
1693}
1694
1695static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001696 struct perf_header *ph, int fd,
1697 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001698{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001699 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001700 char *name;
1701 u32 pmu_num;
1702 u32 type;
1703 struct strbuf sb;
1704
Namhyung Kim5323f602012-12-17 15:38:54 +09001705 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001706 if (ret != sizeof(pmu_num))
1707 return -1;
1708
1709 if (ph->needs_swap)
1710 pmu_num = bswap_32(pmu_num);
1711
1712 if (!pmu_num) {
1713 pr_debug("pmu mappings not available\n");
1714 return 0;
1715 }
1716
1717 ph->env.nr_pmu_mappings = pmu_num;
1718 strbuf_init(&sb, 128);
1719
1720 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001721 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001722 goto error;
1723 if (ph->needs_swap)
1724 type = bswap_32(type);
1725
1726 name = do_read_string(fd, ph);
1727 if (!name)
1728 goto error;
1729
1730 strbuf_addf(&sb, "%u:%s", type, name);
1731 /* include a NULL character at the end */
1732 strbuf_add(&sb, "", 1);
1733
1734 free(name);
1735 pmu_num--;
1736 }
1737 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1738 return 0;
1739
1740error:
1741 strbuf_release(&sb);
1742 return -1;
1743}
1744
Namhyung Kima8bb5592013-01-22 18:09:31 +09001745static int process_group_desc(struct perf_file_section *section __maybe_unused,
1746 struct perf_header *ph, int fd,
1747 void *data __maybe_unused)
1748{
1749 size_t ret = -1;
1750 u32 i, nr, nr_groups;
1751 struct perf_session *session;
1752 struct perf_evsel *evsel, *leader = NULL;
1753 struct group_desc {
1754 char *name;
1755 u32 leader_idx;
1756 u32 nr_members;
1757 } *desc;
1758
1759 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
1760 return -1;
1761
1762 if (ph->needs_swap)
1763 nr_groups = bswap_32(nr_groups);
1764
1765 ph->env.nr_groups = nr_groups;
1766 if (!nr_groups) {
1767 pr_debug("group desc not available\n");
1768 return 0;
1769 }
1770
1771 desc = calloc(nr_groups, sizeof(*desc));
1772 if (!desc)
1773 return -1;
1774
1775 for (i = 0; i < nr_groups; i++) {
1776 desc[i].name = do_read_string(fd, ph);
1777 if (!desc[i].name)
1778 goto out_free;
1779
1780 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
1781 goto out_free;
1782
1783 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
1784 goto out_free;
1785
1786 if (ph->needs_swap) {
1787 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
1788 desc[i].nr_members = bswap_32(desc[i].nr_members);
1789 }
1790 }
1791
1792 /*
1793 * Rebuild group relationship based on the group_desc
1794 */
1795 session = container_of(ph, struct perf_session, header);
1796 session->evlist->nr_groups = nr_groups;
1797
1798 i = nr = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001799 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001800 if (evsel->idx == (int) desc[i].leader_idx) {
1801 evsel->leader = evsel;
1802 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09001803 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001804 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09001805 desc[i].name = NULL;
1806 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09001807 evsel->nr_members = desc[i].nr_members;
1808
1809 if (i >= nr_groups || nr > 0) {
1810 pr_debug("invalid group desc\n");
1811 goto out_free;
1812 }
1813
1814 leader = evsel;
1815 nr = evsel->nr_members - 1;
1816 i++;
1817 } else if (nr) {
1818 /* This is a group member */
1819 evsel->leader = leader;
1820
1821 nr--;
1822 }
1823 }
1824
1825 if (i != nr_groups || nr != 0) {
1826 pr_debug("invalid group desc\n");
1827 goto out_free;
1828 }
1829
1830 ret = 0;
1831out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09001832 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001833 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001834 free(desc);
1835
1836 return ret;
1837}
1838
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001839struct feature_ops {
1840 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1841 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01001842 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001843 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001844 const char *name;
1845 bool full_only;
1846};
1847
Robert Richter8cdfa782011-12-07 10:02:56 +01001848#define FEAT_OPA(n, func) \
1849 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01001850#define FEAT_OPP(n, func) \
1851 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1852 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01001853#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01001854 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09001855 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01001856
1857/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001858#define print_tracing_data NULL
1859#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001860
1861static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001862 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01001863 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001864 FEAT_OPP(HEADER_HOSTNAME, hostname),
1865 FEAT_OPP(HEADER_OSRELEASE, osrelease),
1866 FEAT_OPP(HEADER_VERSION, version),
1867 FEAT_OPP(HEADER_ARCH, arch),
1868 FEAT_OPP(HEADER_NRCPUS, nrcpus),
1869 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09001870 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001871 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02001872 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001873 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01001874 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1875 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01001876 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001877 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09001878 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001879};
1880
1881struct header_print_data {
1882 FILE *fp;
1883 bool full; /* extended list of headers */
1884};
1885
1886static int perf_file_section__fprintf_info(struct perf_file_section *section,
1887 struct perf_header *ph,
1888 int feat, int fd, void *data)
1889{
1890 struct header_print_data *hd = data;
1891
1892 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1893 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1894 "%d, continuing...\n", section->offset, feat);
1895 return 0;
1896 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001897 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001898 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01001899 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001900 }
1901 if (!feat_ops[feat].print)
1902 return 0;
1903
1904 if (!feat_ops[feat].full_only || hd->full)
1905 feat_ops[feat].print(ph, fd, hd->fp);
1906 else
1907 fprintf(hd->fp, "# %s info available, use -I to display\n",
1908 feat_ops[feat].name);
1909
1910 return 0;
1911}
1912
1913int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1914{
1915 struct header_print_data hd;
1916 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001917 int fd = perf_data_file__fd(session->file);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001918 hd.fp = fp;
1919 hd.full = full;
1920
1921 perf_header__process_sections(header, fd, &hd,
1922 perf_file_section__fprintf_info);
1923 return 0;
1924}
1925
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001926static int do_write_feat(int fd, struct perf_header *h, int type,
1927 struct perf_file_section **p,
1928 struct perf_evlist *evlist)
1929{
1930 int err;
1931 int ret = 0;
1932
1933 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001934 if (!feat_ops[type].write)
1935 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001936
1937 (*p)->offset = lseek(fd, 0, SEEK_CUR);
1938
1939 err = feat_ops[type].write(fd, h, evlist);
1940 if (err < 0) {
1941 pr_debug("failed to write feature %d\n", type);
1942
1943 /* undo anything written */
1944 lseek(fd, (*p)->offset, SEEK_SET);
1945
1946 return -1;
1947 }
1948 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
1949 (*p)++;
1950 }
1951 return ret;
1952}
1953
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001954static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001955 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001956{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001957 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001958 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001959 int sec_size;
1960 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001961 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001962 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001963
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001964 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001965 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001966 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001967
Paul Gortmaker91b98802013-01-30 20:05:49 -05001968 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001969 if (feat_sec == NULL)
1970 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001971
1972 sec_size = sizeof(*feat_sec) * nr_sections;
1973
Jiri Olsa8d541e92013-07-17 19:49:44 +02001974 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08001975 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001976
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001977 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
1978 if (do_write_feat(fd, header, feat, &p, evlist))
1979 perf_header__clear_feat(header, feat);
1980 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001981
Xiao Guangrongf887f302010-02-04 16:46:42 +08001982 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001983 /*
1984 * may write more than needed due to dropped feature, but
1985 * this is okay, reader will skip the mising entries
1986 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001987 err = do_write(fd, feat_sec, sec_size);
1988 if (err < 0)
1989 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001990 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001991 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001992}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001993
Tom Zanussi8dc58102010-04-01 23:59:15 -05001994int perf_header__write_pipe(int fd)
1995{
1996 struct perf_pipe_file_header f_header;
1997 int err;
1998
1999 f_header = (struct perf_pipe_file_header){
2000 .magic = PERF_MAGIC,
2001 .size = sizeof(f_header),
2002 };
2003
2004 err = do_write(fd, &f_header, sizeof(f_header));
2005 if (err < 0) {
2006 pr_debug("failed to write perf pipe header\n");
2007 return err;
2008 }
2009
2010 return 0;
2011}
2012
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002013int perf_session__write_header(struct perf_session *session,
2014 struct perf_evlist *evlist,
2015 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002016{
2017 struct perf_file_header f_header;
2018 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002019 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002020 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002021 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002022 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002023
2024 lseek(fd, sizeof(f_header), SEEK_SET);
2025
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002026 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002027 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2028 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002029 if (err < 0) {
2030 pr_debug("failed to write perf header\n");
2031 return err;
2032 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002033 }
2034
Jiri Olsa944d62b2013-07-17 19:49:43 +02002035 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002036
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002037 evlist__for_each(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002038 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002039 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002040 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002041 .offset = evsel->id_offset,
2042 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002043 }
2044 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002045 err = do_write(fd, &f_attr, sizeof(f_attr));
2046 if (err < 0) {
2047 pr_debug("failed to write perf header attribute\n");
2048 return err;
2049 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002050 }
2051
Adrian Hunterd645c442013-12-11 14:36:28 +02002052 if (!header->data_offset)
2053 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002054 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002055
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002056 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002057 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002058 if (err < 0)
2059 return err;
2060 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002061
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002062 f_header = (struct perf_file_header){
2063 .magic = PERF_MAGIC,
2064 .size = sizeof(f_header),
2065 .attr_size = sizeof(f_attr),
2066 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002067 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002068 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002069 },
2070 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002071 .offset = header->data_offset,
2072 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002073 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002074 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002075 };
2076
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002077 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002078
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002079 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002080 err = do_write(fd, &f_header, sizeof(f_header));
2081 if (err < 0) {
2082 pr_debug("failed to write perf header\n");
2083 return err;
2084 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002085 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002086
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002087 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002088}
2089
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002090static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002091 int fd, void *buf, size_t size)
2092{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002093 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002094 return -1;
2095
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002096 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002097 mem_bswap_64(buf, size);
2098
2099 return 0;
2100}
2101
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002102int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002103 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002104 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002105 struct perf_header *ph,
2106 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002107{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002108 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002109 int nr_sections;
2110 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002111 int feat;
2112 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002113
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002114 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002115 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002116 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002117
Paul Gortmaker91b98802013-01-30 20:05:49 -05002118 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002119 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002120 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002121
2122 sec_size = sizeof(*feat_sec) * nr_sections;
2123
Jiri Olsa8d541e92013-07-17 19:49:44 +02002124 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002125
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002126 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2127 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002128 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002129
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002130 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2131 err = process(sec++, header, feat, fd, data);
2132 if (err < 0)
2133 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002134 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002135 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002136out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002137 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002138 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002139}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002140
Stephane Eranian114382a2012-02-09 23:21:08 +01002141static const int attr_file_abi_sizes[] = {
2142 [0] = PERF_ATTR_SIZE_VER0,
2143 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002144 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002145 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002146 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002147 0,
2148};
2149
2150/*
2151 * In the legacy file format, the magic number is not used to encode endianness.
2152 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2153 * on ABI revisions, we need to try all combinations for all endianness to
2154 * detect the endianness.
2155 */
2156static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2157{
2158 uint64_t ref_size, attr_size;
2159 int i;
2160
2161 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2162 ref_size = attr_file_abi_sizes[i]
2163 + sizeof(struct perf_file_section);
2164 if (hdr_sz != ref_size) {
2165 attr_size = bswap_64(hdr_sz);
2166 if (attr_size != ref_size)
2167 continue;
2168
2169 ph->needs_swap = true;
2170 }
2171 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2172 i,
2173 ph->needs_swap);
2174 return 0;
2175 }
2176 /* could not determine endianness */
2177 return -1;
2178}
2179
2180#define PERF_PIPE_HDR_VER0 16
2181
2182static const size_t attr_pipe_abi_sizes[] = {
2183 [0] = PERF_PIPE_HDR_VER0,
2184 0,
2185};
2186
2187/*
2188 * In the legacy pipe format, there is an implicit assumption that endiannesss
2189 * between host recording the samples, and host parsing the samples is the
2190 * same. This is not always the case given that the pipe output may always be
2191 * redirected into a file and analyzed on a different machine with possibly a
2192 * different endianness and perf_event ABI revsions in the perf tool itself.
2193 */
2194static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2195{
2196 u64 attr_size;
2197 int i;
2198
2199 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2200 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2201 attr_size = bswap_64(hdr_sz);
2202 if (attr_size != hdr_sz)
2203 continue;
2204
2205 ph->needs_swap = true;
2206 }
2207 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2208 return 0;
2209 }
2210 return -1;
2211}
2212
Feng Tange84ba4e2012-10-30 11:56:07 +08002213bool is_perf_magic(u64 magic)
2214{
2215 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2216 || magic == __perf_magic2
2217 || magic == __perf_magic2_sw)
2218 return true;
2219
2220 return false;
2221}
2222
Stephane Eranian114382a2012-02-09 23:21:08 +01002223static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2224 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002225{
2226 int ret;
2227
2228 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002229 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002230 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002231 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002232 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002233 if (is_pipe)
2234 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002235
Stephane Eranian114382a2012-02-09 23:21:08 +01002236 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002237 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002238 /*
2239 * the new magic number serves two purposes:
2240 * - unique number to identify actual perf.data files
2241 * - encode endianness of file
2242 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002243 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002244
Stephane Eranian114382a2012-02-09 23:21:08 +01002245 /* check magic number with one endianness */
2246 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002247 return 0;
2248
Stephane Eranian114382a2012-02-09 23:21:08 +01002249 /* check magic number with opposite endianness */
2250 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002251 return -1;
2252
2253 ph->needs_swap = true;
2254
2255 return 0;
2256}
2257
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002258int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002259 struct perf_header *ph, int fd)
2260{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002261 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002262
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002263 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002264
Stephane Eranian73323f52012-02-02 13:54:44 +01002265 ret = readn(fd, header, sizeof(*header));
2266 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002267 return -1;
2268
Stephane Eranian114382a2012-02-09 23:21:08 +01002269 if (check_magic_endian(header->magic,
2270 header->attr_size, false, ph) < 0) {
2271 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002272 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002273 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002274
Stephane Eranian73323f52012-02-02 13:54:44 +01002275 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002276 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002277 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002278 }
2279
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002280 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002281 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002282 if (header->size == offsetof(typeof(*header), adds_features))
2283 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002284 else
2285 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002286 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002287 /*
2288 * feature bitmap is declared as an array of unsigned longs --
2289 * not good since its size can differ between the host that
2290 * generated the data file and the host analyzing the file.
2291 *
2292 * We need to handle endianness, but we don't know the size of
2293 * the unsigned long where the file was generated. Take a best
2294 * guess at determining it: try 64-bit swap first (ie., file
2295 * created on a 64-bit host), and check if the hostname feature
2296 * bit is set (this feature bit is forced on as of fbe96f2).
2297 * If the bit is not, undo the 64-bit swap and try a 32-bit
2298 * swap. If the hostname bit is still not set (e.g., older data
2299 * file), punt and fallback to the original behavior --
2300 * clearing all feature bits and setting buildid.
2301 */
David Ahern80c01202012-06-08 11:47:51 -03002302 mem_bswap_64(&header->adds_features,
2303 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002304
2305 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002306 /* unswap as u64 */
2307 mem_bswap_64(&header->adds_features,
2308 BITS_TO_U64(HEADER_FEAT_BITS));
2309
2310 /* unswap as u32 */
2311 mem_bswap_32(&header->adds_features,
2312 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002313 }
2314
2315 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2316 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2317 set_bit(HEADER_BUILD_ID, header->adds_features);
2318 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002319 }
2320
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002321 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002322 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002323
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002324 ph->data_offset = header->data.offset;
2325 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002326 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002327 return 0;
2328}
2329
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002330static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002331 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002332 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002333{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002334 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002335 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002336 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002337 return 0;
2338 }
2339
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002340 if (feat >= HEADER_LAST_FEATURE) {
2341 pr_debug("unknown feature %d, continuing...\n", feat);
2342 return 0;
2343 }
2344
Robert Richterf1c67db2012-02-10 15:41:56 +01002345 if (!feat_ops[feat].process)
2346 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002347
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002348 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002349}
2350
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002351static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002352 struct perf_header *ph, int fd,
2353 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002354{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002355 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002356
2357 ret = readn(fd, header, sizeof(*header));
2358 if (ret <= 0)
2359 return -1;
2360
Stephane Eranian114382a2012-02-09 23:21:08 +01002361 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2362 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002363 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002364 }
2365
2366 if (ph->needs_swap)
2367 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002368
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002369 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002370 return -1;
2371
Tom Zanussi8dc58102010-04-01 23:59:15 -05002372 return 0;
2373}
2374
Jiri Olsad4339562013-07-17 19:49:41 +02002375static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002376{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002377 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002378 struct perf_pipe_file_header f_header;
2379
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002380 if (perf_file_header__read_pipe(&f_header, header,
2381 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002382 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002383 pr_debug("incompatible file format\n");
2384 return -EINVAL;
2385 }
2386
Tom Zanussi8dc58102010-04-01 23:59:15 -05002387 return 0;
2388}
2389
Stephane Eranian69996df2012-02-09 23:21:06 +01002390static int read_attr(int fd, struct perf_header *ph,
2391 struct perf_file_attr *f_attr)
2392{
2393 struct perf_event_attr *attr = &f_attr->attr;
2394 size_t sz, left;
2395 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002396 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002397
2398 memset(f_attr, 0, sizeof(*f_attr));
2399
2400 /* read minimal guaranteed structure */
2401 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2402 if (ret <= 0) {
2403 pr_debug("cannot read %d bytes of header attr\n",
2404 PERF_ATTR_SIZE_VER0);
2405 return -1;
2406 }
2407
2408 /* on file perf_event_attr size */
2409 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002410
Stephane Eranian69996df2012-02-09 23:21:06 +01002411 if (ph->needs_swap)
2412 sz = bswap_32(sz);
2413
2414 if (sz == 0) {
2415 /* assume ABI0 */
2416 sz = PERF_ATTR_SIZE_VER0;
2417 } else if (sz > our_sz) {
2418 pr_debug("file uses a more recent and unsupported ABI"
2419 " (%zu bytes extra)\n", sz - our_sz);
2420 return -1;
2421 }
2422 /* what we have not yet read and that we know about */
2423 left = sz - PERF_ATTR_SIZE_VER0;
2424 if (left) {
2425 void *ptr = attr;
2426 ptr += PERF_ATTR_SIZE_VER0;
2427
2428 ret = readn(fd, ptr, left);
2429 }
2430 /* read perf_file_section, ids are read in caller */
2431 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2432
2433 return ret <= 0 ? -1 : 0;
2434}
2435
Namhyung Kim831394b2012-09-06 11:10:46 +09002436static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2437 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002438{
Namhyung Kim831394b2012-09-06 11:10:46 +09002439 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002440 char bf[128];
2441
Namhyung Kim831394b2012-09-06 11:10:46 +09002442 /* already prepared */
2443 if (evsel->tp_format)
2444 return 0;
2445
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002446 if (pevent == NULL) {
2447 pr_debug("broken or missing trace data\n");
2448 return -1;
2449 }
2450
Namhyung Kim831394b2012-09-06 11:10:46 +09002451 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002452 if (event == NULL)
2453 return -1;
2454
Namhyung Kim831394b2012-09-06 11:10:46 +09002455 if (!evsel->name) {
2456 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2457 evsel->name = strdup(bf);
2458 if (evsel->name == NULL)
2459 return -1;
2460 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002461
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002462 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002463 return 0;
2464}
2465
Namhyung Kim831394b2012-09-06 11:10:46 +09002466static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2467 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002468{
2469 struct perf_evsel *pos;
2470
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002471 evlist__for_each(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002472 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2473 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002474 return -1;
2475 }
2476
2477 return 0;
2478}
2479
Jiri Olsad4339562013-07-17 19:49:41 +02002480int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002481{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002482 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002483 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002484 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002485 struct perf_file_attr f_attr;
2486 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002487 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002488 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002489
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002490 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002491 if (session->evlist == NULL)
2492 return -ENOMEM;
2493
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002494 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002495 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002496
Stephane Eranian69996df2012-02-09 23:21:06 +01002497 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002498 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002499
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002500 /*
2501 * Sanity check that perf.data was written cleanly; data size is
2502 * initialized to 0 and updated only if the on_exit function is run.
2503 * If data size is still 0 then the file contains only partial
2504 * information. Just warn user and process it as much as it can.
2505 */
2506 if (f_header.data.size == 0) {
2507 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2508 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002509 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002510 }
2511
Stephane Eranian69996df2012-02-09 23:21:06 +01002512 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002513 lseek(fd, f_header.attrs.offset, SEEK_SET);
2514
2515 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002516 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002517 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002518
Stephane Eranian69996df2012-02-09 23:21:06 +01002519 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002520 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002521
David Aherneda39132011-07-15 12:34:09 -06002522 if (header->needs_swap)
2523 perf_event__attr_swap(&f_attr.attr);
2524
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002525 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002526 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002527
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002528 if (evsel == NULL)
2529 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002530
2531 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002532 /*
2533 * Do it before so that if perf_evsel__alloc_id fails, this
2534 * entry gets purged too at perf_evlist__delete().
2535 */
2536 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002537
2538 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002539 /*
2540 * We don't have the cpu and thread maps on the header, so
2541 * for allocating the perf_sample_id table we fake 1 cpu and
2542 * hattr->ids threads.
2543 */
2544 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2545 goto out_delete_evlist;
2546
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002547 lseek(fd, f_attr.ids.offset, SEEK_SET);
2548
2549 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002550 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002551 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002552
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002553 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002554 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002555
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002556 lseek(fd, tmp, SEEK_SET);
2557 }
2558
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002559 symbol_conf.nr_events = nr_attrs;
2560
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002561 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002562 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002563
Namhyung Kim831394b2012-09-06 11:10:46 +09002564 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002565 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002566 goto out_delete_evlist;
2567
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002568 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002569out_errno:
2570 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002571
2572out_delete_evlist:
2573 perf_evlist__delete(session->evlist);
2574 session->evlist = NULL;
2575 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002576}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002577
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002578int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002579 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002580 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002581{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002582 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002583 size_t size;
2584 int err;
2585
2586 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002587 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002588 size += sizeof(struct perf_event_header);
2589 size += ids * sizeof(u64);
2590
2591 ev = malloc(size);
2592
Chris Samuelce47dc52010-11-13 13:35:06 +11002593 if (ev == NULL)
2594 return -ENOMEM;
2595
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002596 ev->attr.attr = *attr;
2597 memcpy(ev->attr.id, id, ids * sizeof(u64));
2598
2599 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002600 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002601
Robert Richterf4d83432012-08-16 21:10:17 +02002602 if (ev->attr.header.size == size)
2603 err = process(tool, ev, NULL, NULL);
2604 else
2605 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002606
2607 free(ev);
2608
2609 return err;
2610}
2611
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002612int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002613 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002614 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002615{
Robert Richter6606f872012-08-16 21:10:19 +02002616 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002617 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002618
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002619 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002620 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2621 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002622 if (err) {
2623 pr_debug("failed to create perf header attribute\n");
2624 return err;
2625 }
2626 }
2627
2628 return err;
2629}
2630
Adrian Hunter47c3d102013-07-04 16:20:21 +03002631int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2632 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002633 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002634{
Robert Richterf4d83432012-08-16 21:10:17 +02002635 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002636 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002637 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002638
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002639 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002640 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002641 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002642 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002643 }
2644
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002645 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002646 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002647 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002648
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002649 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002650
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002651 ids = event->header.size;
2652 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002653 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002654 /*
2655 * We don't have the cpu and thread maps on the header, so
2656 * for allocating the perf_sample_id table we fake 1 cpu and
2657 * hattr->ids threads.
2658 */
2659 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2660 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002661
2662 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002663 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002664 }
2665
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03002666 symbol_conf.nr_events = evlist->nr_entries;
2667
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002668 return 0;
2669}
Tom Zanussicd19a032010-04-01 23:59:20 -05002670
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002671int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002672 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002673 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002674{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002675 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002676 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002677 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002678 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002679
Jiri Olsa29208e52011-10-20 15:59:43 +02002680 /*
2681 * We are going to store the size of the data followed
2682 * by the data contents. Since the fd descriptor is a pipe,
2683 * we cannot seek back to store the size of the data once
2684 * we know it. Instead we:
2685 *
2686 * - write the tracing data to the temp file
2687 * - get/write the data size to pipe
2688 * - write the tracing data from the temp file
2689 * to the pipe
2690 */
2691 tdata = tracing_data_get(&evlist->entries, fd, true);
2692 if (!tdata)
2693 return -1;
2694
Tom Zanussi92155452010-04-01 23:59:21 -05002695 memset(&ev, 0, sizeof(ev));
2696
2697 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002698 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002699 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05002700 padding = aligned_size - size;
2701 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2702 ev.tracing_data.size = aligned_size;
2703
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002704 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002705
Jiri Olsa29208e52011-10-20 15:59:43 +02002706 /*
2707 * The put function will copy all the tracing data
2708 * stored in temp file to the pipe.
2709 */
2710 tracing_data_put(tdata);
2711
Tom Zanussi92155452010-04-01 23:59:21 -05002712 write_padded(fd, NULL, 0, padding);
2713
2714 return aligned_size;
2715}
2716
Adrian Hunter47c3d102013-07-04 16:20:21 +03002717int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2718 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002719 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002720{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002721 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002722 int fd = perf_data_file__fd(session->file);
2723 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05002724 char buf[BUFSIZ];
2725
2726 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002727 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05002728 SEEK_SET);
2729
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002730 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002731 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002732 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05002733
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002734 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002735 pr_err("%s: reading input file", __func__);
2736 return -1;
2737 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002738 if (session->repipe) {
2739 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002740 if (retw <= 0 || retw != padding) {
2741 pr_err("%s: repiping tracing data padding", __func__);
2742 return -1;
2743 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002744 }
Tom Zanussi92155452010-04-01 23:59:21 -05002745
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002746 if (size_read + padding != size) {
2747 pr_err("%s: tracing data size mismatch", __func__);
2748 return -1;
2749 }
Tom Zanussi92155452010-04-01 23:59:21 -05002750
Namhyung Kim831394b2012-09-06 11:10:46 +09002751 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002752 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03002753
Tom Zanussi92155452010-04-01 23:59:21 -05002754 return size_read + padding;
2755}
Tom Zanussic7929e42010-04-01 23:59:22 -05002756
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002757int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002758 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002759 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002760 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05002761{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002762 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05002763 size_t len;
2764 int err = 0;
2765
2766 if (!pos->hit)
2767 return err;
2768
2769 memset(&ev, 0, sizeof(ev));
2770
2771 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002772 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05002773 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2774 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2775 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002776 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05002777 ev.build_id.header.size = sizeof(ev.build_id) + len;
2778 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2779
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002780 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05002781
2782 return err;
2783}
2784
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002785int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002786 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002787 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05002788{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002789 __event_process_build_id(&event->build_id,
2790 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002791 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05002792 return 0;
2793}