blob: 589c28028379b3b76cd714fcd9d44b28dd7bb220 [file] [log] [blame]
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02001#include "util.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02004#include <unistd.h>
5#include <stdio.h>
6#include <stdlib.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +01007#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02008#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +01009#include <linux/bitops.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020010#include <sys/utsname.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020011
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020012#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030013#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020014#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020015#include "../perf.h"
16#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020017#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010018#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010019#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020020#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020021#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020022#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090023#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020024#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020025#include "data.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020026
Stephane Eranianfbe96f22011-09-30 15:40:40 +020027static u32 header_argc;
28static const char **header_argv;
29
Stephane Eranian73323f52012-02-02 13:54:44 +010030/*
31 * magic2 = "PERFILE2"
32 * must be a numerical value to let the endianness
33 * determine the memory layout. That way we are able
34 * to detect endianness when reading the perf.data file
35 * back.
36 *
37 * we check for legacy (PERFFILE) format.
38 */
39static const char *__perf_magic1 = "PERFFILE";
40static const u64 __perf_magic2 = 0x32454c4946524550ULL;
41static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020042
Stephane Eranian73323f52012-02-02 13:54:44 +010043#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020044
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020045struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020046 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020047 struct perf_file_section ids;
48};
49
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030050void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020051{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030052 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020053}
54
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030055void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020056{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030057 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020058}
59
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030060bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020061{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030062 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020063}
64
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020065static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020066{
67 while (size) {
68 int ret = write(fd, buf, size);
69
70 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -020071 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020072
73 size -= ret;
74 buf += ret;
75 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020076
77 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020078}
79
Namhyung Kime195fac2014-11-04 10:14:30 +090080int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020081{
82 static const char zero_buf[NAME_ALIGN];
83 int err = do_write(fd, bf, count);
84
85 if (!err)
86 err = do_write(fd, zero_buf, count_aligned - count);
87
88 return err;
89}
90
Stephane Eranianfbe96f22011-09-30 15:40:40 +020091static int do_write_string(int fd, const char *str)
92{
93 u32 len, olen;
94 int ret;
95
96 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +030097 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +020098
99 /* write len, incl. \0 */
100 ret = do_write(fd, &len, sizeof(len));
101 if (ret < 0)
102 return ret;
103
104 return write_padded(fd, str, olen, len);
105}
106
107static char *do_read_string(int fd, struct perf_header *ph)
108{
109 ssize_t sz, ret;
110 u32 len;
111 char *buf;
112
Namhyung Kim5323f602012-12-17 15:38:54 +0900113 sz = readn(fd, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200114 if (sz < (ssize_t)sizeof(len))
115 return NULL;
116
117 if (ph->needs_swap)
118 len = bswap_32(len);
119
120 buf = malloc(len);
121 if (!buf)
122 return NULL;
123
Namhyung Kim5323f602012-12-17 15:38:54 +0900124 ret = readn(fd, buf, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200125 if (ret == (ssize_t)len) {
126 /*
127 * strings are padded by zeroes
128 * thus the actual strlen of buf
129 * may be less than len
130 */
131 return buf;
132 }
133
134 free(buf);
135 return NULL;
136}
137
138int
139perf_header__set_cmdline(int argc, const char **argv)
140{
141 int i;
142
David Ahern56e6f602012-07-29 20:53:51 -0600143 /*
144 * If header_argv has already been set, do not override it.
145 * This allows a command to set the cmdline, parse args and
146 * then call another builtin function that implements a
147 * command -- e.g, cmd_kvm calling cmd_record.
148 */
149 if (header_argv)
150 return 0;
151
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200152 header_argc = (u32)argc;
153
154 /* do not include NULL termination */
155 header_argv = calloc(argc, sizeof(char *));
156 if (!header_argv)
157 return -ENOMEM;
158
159 /*
160 * must copy argv contents because it gets moved
161 * around during option parsing
162 */
163 for (i = 0; i < argc ; i++)
164 header_argv[i] = argv[i];
165
166 return 0;
167}
168
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300169static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200170 struct perf_evlist *evlist)
171{
172 return read_tracing_data(fd, &evlist->entries);
173}
174
175
176static int write_build_id(int fd, struct perf_header *h,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300177 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200178{
179 struct perf_session *session;
180 int err;
181
182 session = container_of(h, struct perf_session, header);
183
Robert Richtere20960c2011-12-07 10:02:55 +0100184 if (!perf_session__read_build_ids(session, true))
185 return -1;
186
Namhyung Kim714c9c42014-11-04 10:14:29 +0900187 err = perf_session__write_buildid_table(session, fd);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200188 if (err < 0) {
189 pr_debug("failed to write buildid table\n");
190 return err;
191 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900192 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200193
194 return 0;
195}
196
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300197static int write_hostname(int fd, struct perf_header *h __maybe_unused,
198 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200199{
200 struct utsname uts;
201 int ret;
202
203 ret = uname(&uts);
204 if (ret < 0)
205 return -1;
206
207 return do_write_string(fd, uts.nodename);
208}
209
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300210static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
211 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200212{
213 struct utsname uts;
214 int ret;
215
216 ret = uname(&uts);
217 if (ret < 0)
218 return -1;
219
220 return do_write_string(fd, uts.release);
221}
222
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300223static int write_arch(int fd, struct perf_header *h __maybe_unused,
224 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200225{
226 struct utsname uts;
227 int ret;
228
229 ret = uname(&uts);
230 if (ret < 0)
231 return -1;
232
233 return do_write_string(fd, uts.machine);
234}
235
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300236static int write_version(int fd, struct perf_header *h __maybe_unused,
237 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200238{
239 return do_write_string(fd, perf_version_string);
240}
241
Wang Nan493c3032014-10-24 09:45:26 +0800242static int __write_cpudesc(int fd, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200243{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200244 FILE *file;
245 char *buf = NULL;
246 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800247 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200248 size_t len = 0;
249 int ret = -1;
250
251 if (!search)
252 return -1;
253
254 file = fopen("/proc/cpuinfo", "r");
255 if (!file)
256 return -1;
257
258 while (getline(&buf, &len, file) > 0) {
259 ret = strncmp(buf, search, strlen(search));
260 if (!ret)
261 break;
262 }
263
Wang Naned307752014-10-16 11:08:29 +0800264 if (ret) {
265 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200266 goto done;
Wang Naned307752014-10-16 11:08:29 +0800267 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200268
269 s = buf;
270
271 p = strchr(buf, ':');
272 if (p && *(p+1) == ' ' && *(p+2))
273 s = p + 2;
274 p = strchr(s, '\n');
275 if (p)
276 *p = '\0';
277
278 /* squash extra space characters (branding string) */
279 p = s;
280 while (*p) {
281 if (isspace(*p)) {
282 char *r = p + 1;
283 char *q = r;
284 *p = ' ';
285 while (*q && isspace(*q))
286 q++;
287 if (q != (p+1))
288 while ((*r++ = *q++));
289 }
290 p++;
291 }
292 ret = do_write_string(fd, s);
293done:
294 free(buf);
295 fclose(file);
296 return ret;
297}
298
Wang Nan493c3032014-10-24 09:45:26 +0800299static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
300 struct perf_evlist *evlist __maybe_unused)
301{
302#ifndef CPUINFO_PROC
303#define CPUINFO_PROC {"model name", }
304#endif
305 const char *cpuinfo_procs[] = CPUINFO_PROC;
306 unsigned int i;
307
308 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
309 int ret;
310 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
311 if (ret >= 0)
312 return ret;
313 }
314 return -1;
315}
316
317
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300318static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
319 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200320{
321 long nr;
322 u32 nrc, nra;
323 int ret;
324
325 nr = sysconf(_SC_NPROCESSORS_CONF);
326 if (nr < 0)
327 return -1;
328
329 nrc = (u32)(nr & UINT_MAX);
330
331 nr = sysconf(_SC_NPROCESSORS_ONLN);
332 if (nr < 0)
333 return -1;
334
335 nra = (u32)(nr & UINT_MAX);
336
337 ret = do_write(fd, &nrc, sizeof(nrc));
338 if (ret < 0)
339 return ret;
340
341 return do_write(fd, &nra, sizeof(nra));
342}
343
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300344static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200345 struct perf_evlist *evlist)
346{
Robert Richter6606f872012-08-16 21:10:19 +0200347 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900348 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200349 int ret;
350
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900351 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200352
353 /*
354 * write number of events
355 */
356 ret = do_write(fd, &nre, sizeof(nre));
357 if (ret < 0)
358 return ret;
359
360 /*
361 * size of perf_event_attr struct
362 */
Robert Richter6606f872012-08-16 21:10:19 +0200363 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200364 ret = do_write(fd, &sz, sizeof(sz));
365 if (ret < 0)
366 return ret;
367
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300368 evlist__for_each(evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +0200369 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200370 if (ret < 0)
371 return ret;
372 /*
373 * write number of unique id per event
374 * there is one id per instance of an event
375 *
376 * copy into an nri to be independent of the
377 * type of ids,
378 */
Robert Richter6606f872012-08-16 21:10:19 +0200379 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200380 ret = do_write(fd, &nri, sizeof(nri));
381 if (ret < 0)
382 return ret;
383
384 /*
385 * write event string as passed on cmdline
386 */
Robert Richter6606f872012-08-16 21:10:19 +0200387 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200388 if (ret < 0)
389 return ret;
390 /*
391 * write unique ids for this event
392 */
Robert Richter6606f872012-08-16 21:10:19 +0200393 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200394 if (ret < 0)
395 return ret;
396 }
397 return 0;
398}
399
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300400static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
401 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200402{
403 char buf[MAXPATHLEN];
404 char proc[32];
405 u32 i, n;
406 int ret;
407
408 /*
409 * actual atual path to perf binary
410 */
411 sprintf(proc, "/proc/%d/exe", getpid());
412 ret = readlink(proc, buf, sizeof(buf));
413 if (ret <= 0)
414 return -1;
415
416 /* readlink() does not add null termination */
417 buf[ret] = '\0';
418
419 /* account for binary path */
420 n = header_argc + 1;
421
422 ret = do_write(fd, &n, sizeof(n));
423 if (ret < 0)
424 return ret;
425
426 ret = do_write_string(fd, buf);
427 if (ret < 0)
428 return ret;
429
430 for (i = 0 ; i < header_argc; i++) {
431 ret = do_write_string(fd, header_argv[i]);
432 if (ret < 0)
433 return ret;
434 }
435 return 0;
436}
437
438#define CORE_SIB_FMT \
439 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
440#define THRD_SIB_FMT \
441 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
442
443struct cpu_topo {
444 u32 core_sib;
445 u32 thread_sib;
446 char **core_siblings;
447 char **thread_siblings;
448};
449
450static int build_cpu_topo(struct cpu_topo *tp, int cpu)
451{
452 FILE *fp;
453 char filename[MAXPATHLEN];
454 char *buf = NULL, *p;
455 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200456 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200457 u32 i = 0;
458 int ret = -1;
459
460 sprintf(filename, CORE_SIB_FMT, cpu);
461 fp = fopen(filename, "r");
462 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200463 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200464
Stephane Eranianc5885742013-08-14 12:04:26 +0200465 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200466 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200467 if (sret <= 0)
468 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200469
470 p = strchr(buf, '\n');
471 if (p)
472 *p = '\0';
473
474 for (i = 0; i < tp->core_sib; i++) {
475 if (!strcmp(buf, tp->core_siblings[i]))
476 break;
477 }
478 if (i == tp->core_sib) {
479 tp->core_siblings[i] = buf;
480 tp->core_sib++;
481 buf = NULL;
482 len = 0;
483 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200484 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200485
Stephane Eranianc5885742013-08-14 12:04:26 +0200486try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200487 sprintf(filename, THRD_SIB_FMT, cpu);
488 fp = fopen(filename, "r");
489 if (!fp)
490 goto done;
491
492 if (getline(&buf, &len, fp) <= 0)
493 goto done;
494
495 p = strchr(buf, '\n');
496 if (p)
497 *p = '\0';
498
499 for (i = 0; i < tp->thread_sib; i++) {
500 if (!strcmp(buf, tp->thread_siblings[i]))
501 break;
502 }
503 if (i == tp->thread_sib) {
504 tp->thread_siblings[i] = buf;
505 tp->thread_sib++;
506 buf = NULL;
507 }
508 ret = 0;
509done:
510 if(fp)
511 fclose(fp);
512 free(buf);
513 return ret;
514}
515
516static void free_cpu_topo(struct cpu_topo *tp)
517{
518 u32 i;
519
520 if (!tp)
521 return;
522
523 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300524 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200525
526 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300527 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200528
529 free(tp);
530}
531
532static struct cpu_topo *build_cpu_topology(void)
533{
534 struct cpu_topo *tp;
535 void *addr;
536 u32 nr, i;
537 size_t sz;
538 long ncpus;
539 int ret = -1;
540
541 ncpus = sysconf(_SC_NPROCESSORS_CONF);
542 if (ncpus < 0)
543 return NULL;
544
545 nr = (u32)(ncpus & UINT_MAX);
546
547 sz = nr * sizeof(char *);
548
549 addr = calloc(1, sizeof(*tp) + 2 * sz);
550 if (!addr)
551 return NULL;
552
553 tp = addr;
554
555 addr += sizeof(*tp);
556 tp->core_siblings = addr;
557 addr += sz;
558 tp->thread_siblings = addr;
559
560 for (i = 0; i < nr; i++) {
561 ret = build_cpu_topo(tp, i);
562 if (ret < 0)
563 break;
564 }
565 if (ret) {
566 free_cpu_topo(tp);
567 tp = NULL;
568 }
569 return tp;
570}
571
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300572static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
573 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200574{
575 struct cpu_topo *tp;
576 u32 i;
577 int ret;
578
579 tp = build_cpu_topology();
580 if (!tp)
581 return -1;
582
583 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
584 if (ret < 0)
585 goto done;
586
587 for (i = 0; i < tp->core_sib; i++) {
588 ret = do_write_string(fd, tp->core_siblings[i]);
589 if (ret < 0)
590 goto done;
591 }
592 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
593 if (ret < 0)
594 goto done;
595
596 for (i = 0; i < tp->thread_sib; i++) {
597 ret = do_write_string(fd, tp->thread_siblings[i]);
598 if (ret < 0)
599 break;
600 }
601done:
602 free_cpu_topo(tp);
603 return ret;
604}
605
606
607
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300608static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
609 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200610{
611 char *buf = NULL;
612 FILE *fp;
613 size_t len = 0;
614 int ret = -1, n;
615 uint64_t mem;
616
617 fp = fopen("/proc/meminfo", "r");
618 if (!fp)
619 return -1;
620
621 while (getline(&buf, &len, fp) > 0) {
622 ret = strncmp(buf, "MemTotal:", 9);
623 if (!ret)
624 break;
625 }
626 if (!ret) {
627 n = sscanf(buf, "%*s %"PRIu64, &mem);
628 if (n == 1)
629 ret = do_write(fd, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800630 } else
631 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200632 free(buf);
633 fclose(fp);
634 return ret;
635}
636
637static int write_topo_node(int fd, int node)
638{
639 char str[MAXPATHLEN];
640 char field[32];
641 char *buf = NULL, *p;
642 size_t len = 0;
643 FILE *fp;
644 u64 mem_total, mem_free, mem;
645 int ret = -1;
646
647 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
648 fp = fopen(str, "r");
649 if (!fp)
650 return -1;
651
652 while (getline(&buf, &len, fp) > 0) {
653 /* skip over invalid lines */
654 if (!strchr(buf, ':'))
655 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100656 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200657 goto done;
658 if (!strcmp(field, "MemTotal:"))
659 mem_total = mem;
660 if (!strcmp(field, "MemFree:"))
661 mem_free = mem;
662 }
663
664 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100665 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200666
667 ret = do_write(fd, &mem_total, sizeof(u64));
668 if (ret)
669 goto done;
670
671 ret = do_write(fd, &mem_free, sizeof(u64));
672 if (ret)
673 goto done;
674
675 ret = -1;
676 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
677
678 fp = fopen(str, "r");
679 if (!fp)
680 goto done;
681
682 if (getline(&buf, &len, fp) <= 0)
683 goto done;
684
685 p = strchr(buf, '\n');
686 if (p)
687 *p = '\0';
688
689 ret = do_write_string(fd, buf);
690done:
691 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100692 if (fp)
693 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200694 return ret;
695}
696
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300697static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
698 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200699{
700 char *buf = NULL;
701 size_t len = 0;
702 FILE *fp;
703 struct cpu_map *node_map = NULL;
704 char *c;
705 u32 nr, i, j;
706 int ret = -1;
707
708 fp = fopen("/sys/devices/system/node/online", "r");
709 if (!fp)
710 return -1;
711
712 if (getline(&buf, &len, fp) <= 0)
713 goto done;
714
715 c = strchr(buf, '\n');
716 if (c)
717 *c = '\0';
718
719 node_map = cpu_map__new(buf);
720 if (!node_map)
721 goto done;
722
723 nr = (u32)node_map->nr;
724
725 ret = do_write(fd, &nr, sizeof(nr));
726 if (ret < 0)
727 goto done;
728
729 for (i = 0; i < nr; i++) {
730 j = (u32)node_map->map[i];
731 ret = do_write(fd, &j, sizeof(j));
732 if (ret < 0)
733 break;
734
735 ret = write_topo_node(fd, i);
736 if (ret < 0)
737 break;
738 }
739done:
740 free(buf);
741 fclose(fp);
742 free(node_map);
743 return ret;
744}
745
746/*
Robert Richter50a96672012-08-16 21:10:24 +0200747 * File format:
748 *
749 * struct pmu_mappings {
750 * u32 pmu_num;
751 * struct pmu_map {
752 * u32 type;
753 * char name[];
754 * }[pmu_num];
755 * };
756 */
757
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300758static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
759 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200760{
761 struct perf_pmu *pmu = NULL;
762 off_t offset = lseek(fd, 0, SEEK_CUR);
763 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900764 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200765
766 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +0900767 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
768 if (ret < 0)
769 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200770
771 while ((pmu = perf_pmu__scan(pmu))) {
772 if (!pmu->name)
773 continue;
774 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +0900775
776 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
777 if (ret < 0)
778 return ret;
779
780 ret = do_write_string(fd, pmu->name);
781 if (ret < 0)
782 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200783 }
784
785 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
786 /* discard all */
787 lseek(fd, offset, SEEK_SET);
788 return -1;
789 }
790
791 return 0;
792}
793
794/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900795 * File format:
796 *
797 * struct group_descs {
798 * u32 nr_groups;
799 * struct group_desc {
800 * char name[];
801 * u32 leader_idx;
802 * u32 nr_members;
803 * }[nr_groups];
804 * };
805 */
806static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
807 struct perf_evlist *evlist)
808{
809 u32 nr_groups = evlist->nr_groups;
810 struct perf_evsel *evsel;
811 int ret;
812
813 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
814 if (ret < 0)
815 return ret;
816
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300817 evlist__for_each(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900818 if (perf_evsel__is_group_leader(evsel) &&
819 evsel->nr_members > 1) {
820 const char *name = evsel->group_name ?: "{anon_group}";
821 u32 leader_idx = evsel->idx;
822 u32 nr_members = evsel->nr_members;
823
824 ret = do_write_string(fd, name);
825 if (ret < 0)
826 return ret;
827
828 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
829 if (ret < 0)
830 return ret;
831
832 ret = do_write(fd, &nr_members, sizeof(nr_members));
833 if (ret < 0)
834 return ret;
835 }
836 }
837 return 0;
838}
839
840/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200841 * default get_cpuid(): nothing gets recorded
842 * actual implementation must be in arch/$(ARCH)/util/header.c
843 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300844int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
845 size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200846{
847 return -1;
848}
849
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300850static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
851 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200852{
853 char buffer[64];
854 int ret;
855
856 ret = get_cpuid(buffer, sizeof(buffer));
857 if (!ret)
858 goto write_it;
859
860 return -1;
861write_it:
862 return do_write_string(fd, buffer);
863}
864
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300865static int write_branch_stack(int fd __maybe_unused,
866 struct perf_header *h __maybe_unused,
867 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100868{
869 return 0;
870}
871
Adrian Hunter4025ea42015-04-09 18:53:41 +0300872static int write_auxtrace(int fd __maybe_unused,
873 struct perf_header *h __maybe_unused,
874 struct perf_evlist *evlist __maybe_unused)
875{
876 return 0;
877}
878
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900879static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
880 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200881{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900882 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200883}
884
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900885static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
886 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200887{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900888 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200889}
890
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900891static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200892{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900893 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200894}
895
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900896static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
897 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200898{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900899 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200900}
901
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900902static void print_nrcpus(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, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
906 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200907}
908
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900909static void print_version(struct perf_header *ph, int fd __maybe_unused,
910 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200911{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900912 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200913}
914
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900915static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
916 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200917{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900918 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200919 char *str;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200920
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900921 nr = ph->env.nr_cmdline;
922 str = ph->env.cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200923
924 fprintf(fp, "# cmdline : ");
925
926 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200927 fprintf(fp, "%s ", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900928 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200929 }
930 fputc('\n', fp);
931}
932
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900933static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
934 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200935{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900936 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200937 char *str;
938
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900939 nr = ph->env.nr_sibling_cores;
940 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200941
942 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200943 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900944 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200945 }
946
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900947 nr = ph->env.nr_sibling_threads;
948 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200949
950 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200951 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900952 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200953 }
954}
955
Robert Richter4e1b9c62012-08-16 21:10:22 +0200956static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200957{
Robert Richter4e1b9c62012-08-16 21:10:22 +0200958 struct perf_evsel *evsel;
959
960 if (!events)
961 return;
962
963 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300964 zfree(&evsel->name);
965 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +0200966 }
967
968 free(events);
969}
970
971static struct perf_evsel *
972read_event_desc(struct perf_header *ph, int fd)
973{
974 struct perf_evsel *evsel, *events = NULL;
975 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200976 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +0100977 u32 nre, sz, nr, i, j;
978 ssize_t ret;
979 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200980
981 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +0900982 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200983 if (ret != (ssize_t)sizeof(nre))
984 goto error;
985
986 if (ph->needs_swap)
987 nre = bswap_32(nre);
988
Namhyung Kim5323f602012-12-17 15:38:54 +0900989 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200990 if (ret != (ssize_t)sizeof(sz))
991 goto error;
992
993 if (ph->needs_swap)
994 sz = bswap_32(sz);
995
Stephane Eranian62db9062012-02-09 23:21:07 +0100996 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200997 buf = malloc(sz);
998 if (!buf)
999 goto error;
1000
Robert Richter4e1b9c62012-08-16 21:10:22 +02001001 /* the last event terminates with evsel->attr.size == 0: */
1002 events = calloc(nre + 1, sizeof(*events));
1003 if (!events)
1004 goto error;
1005
1006 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001007 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001008 msz = sz;
1009
Robert Richter4e1b9c62012-08-16 21:10:22 +02001010 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1011 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001012
Stephane Eranian62db9062012-02-09 23:21:07 +01001013 /*
1014 * must read entire on-file attr struct to
1015 * sync up with layout.
1016 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001017 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001018 if (ret != (ssize_t)sz)
1019 goto error;
1020
1021 if (ph->needs_swap)
1022 perf_event__attr_swap(buf);
1023
Robert Richter4e1b9c62012-08-16 21:10:22 +02001024 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001025
Namhyung Kim5323f602012-12-17 15:38:54 +09001026 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001027 if (ret != (ssize_t)sizeof(nr))
1028 goto error;
1029
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001030 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001031 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001032 evsel->needs_swap = true;
1033 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001034
Robert Richter4e1b9c62012-08-16 21:10:22 +02001035 evsel->name = do_read_string(fd, ph);
1036
1037 if (!nr)
1038 continue;
1039
1040 id = calloc(nr, sizeof(*id));
1041 if (!id)
1042 goto error;
1043 evsel->ids = nr;
1044 evsel->id = id;
1045
1046 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001047 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001048 if (ret != (ssize_t)sizeof(*id))
1049 goto error;
1050 if (ph->needs_swap)
1051 *id = bswap_64(*id);
1052 id++;
1053 }
1054 }
1055out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001056 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001057 return events;
1058error:
1059 if (events)
1060 free_event_desc(events);
1061 events = NULL;
1062 goto out;
1063}
1064
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001065static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1066 void *priv __attribute__((unused)))
1067{
1068 return fprintf(fp, ", %s = %s", name, val);
1069}
1070
Robert Richter4e1b9c62012-08-16 21:10:22 +02001071static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1072{
1073 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1074 u32 j;
1075 u64 *id;
1076
1077 if (!events) {
1078 fprintf(fp, "# event desc: not available or unable to read\n");
1079 return;
1080 }
1081
1082 for (evsel = events; evsel->attr.size; evsel++) {
1083 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001084
Robert Richter4e1b9c62012-08-16 21:10:22 +02001085 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001086 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001087 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1088 if (j)
1089 fputc(',', fp);
1090 fprintf(fp, " %"PRIu64, *id);
1091 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001092 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001093 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001094
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001095 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001096
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001097 fputc('\n', fp);
1098 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001099
1100 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001101}
1102
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001103static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001104 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001105{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001106 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001107}
1108
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001109static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001110 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001111{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001112 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001113 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001114 uint64_t mem_total, mem_free;
1115
1116 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001117 nr = ph->env.nr_numa_nodes;
1118 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001119
1120 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001121 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001122 c = strtoul(str, &tmp, 0);
1123 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001124 goto error;
1125
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001126 str = tmp + 1;
1127 mem_total = strtoull(str, &tmp, 0);
1128 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001129 goto error;
1130
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001131 str = tmp + 1;
1132 mem_free = strtoull(str, &tmp, 0);
1133 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001134 goto error;
1135
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001136 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1137 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001138 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001139
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001140 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001141 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001142
1143 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001144 }
1145 return;
1146error:
1147 fprintf(fp, "# numa topology : not available\n");
1148}
1149
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001150static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001151{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001152 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001153}
1154
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001155static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001156 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001157{
1158 fprintf(fp, "# contains samples with branch stack\n");
1159}
1160
Adrian Hunter4025ea42015-04-09 18:53:41 +03001161static void print_auxtrace(struct perf_header *ph __maybe_unused,
1162 int fd __maybe_unused, FILE *fp)
1163{
1164 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1165}
1166
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001167static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1168 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001169{
1170 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001171 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001172 u32 pmu_num;
1173 u32 type;
1174
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001175 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001176 if (!pmu_num) {
1177 fprintf(fp, "# pmu mappings: not available\n");
1178 return;
1179 }
1180
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001181 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001182
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001183 while (pmu_num) {
1184 type = strtoul(str, &tmp, 0);
1185 if (*tmp != ':')
1186 goto error;
1187
1188 str = tmp + 1;
1189 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1190
Robert Richter50a96672012-08-16 21:10:24 +02001191 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001192 str += strlen(str) + 1;
1193 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001194 }
1195
1196 fprintf(fp, "\n");
1197
1198 if (!pmu_num)
1199 return;
1200error:
1201 fprintf(fp, "# pmu mappings: unable to read\n");
1202}
1203
Namhyung Kima8bb5592013-01-22 18:09:31 +09001204static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1205 FILE *fp)
1206{
1207 struct perf_session *session;
1208 struct perf_evsel *evsel;
1209 u32 nr = 0;
1210
1211 session = container_of(ph, struct perf_session, header);
1212
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001213 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001214 if (perf_evsel__is_group_leader(evsel) &&
1215 evsel->nr_members > 1) {
1216 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1217 perf_evsel__name(evsel));
1218
1219 nr = evsel->nr_members - 1;
1220 } else if (nr) {
1221 fprintf(fp, ",%s", perf_evsel__name(evsel));
1222
1223 if (--nr == 0)
1224 fprintf(fp, "}\n");
1225 }
1226 }
1227}
1228
Robert Richter08d95bd2012-02-10 15:41:55 +01001229static int __event_process_build_id(struct build_id_event *bev,
1230 char *filename,
1231 struct perf_session *session)
1232{
1233 int err = -1;
Waiman Long8fa7d872014-09-29 16:07:28 -04001234 struct dsos *dsos;
Robert Richter08d95bd2012-02-10 15:41:55 +01001235 struct machine *machine;
1236 u16 misc;
1237 struct dso *dso;
1238 enum dso_kernel_type dso_type;
1239
1240 machine = perf_session__findnew_machine(session, bev->pid);
1241 if (!machine)
1242 goto out;
1243
1244 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1245
1246 switch (misc) {
1247 case PERF_RECORD_MISC_KERNEL:
1248 dso_type = DSO_TYPE_KERNEL;
Waiman Long8fa7d872014-09-29 16:07:28 -04001249 dsos = &machine->kernel_dsos;
Robert Richter08d95bd2012-02-10 15:41:55 +01001250 break;
1251 case PERF_RECORD_MISC_GUEST_KERNEL:
1252 dso_type = DSO_TYPE_GUEST_KERNEL;
Waiman Long8fa7d872014-09-29 16:07:28 -04001253 dsos = &machine->kernel_dsos;
Robert Richter08d95bd2012-02-10 15:41:55 +01001254 break;
1255 case PERF_RECORD_MISC_USER:
1256 case PERF_RECORD_MISC_GUEST_USER:
1257 dso_type = DSO_TYPE_USER;
Waiman Long8fa7d872014-09-29 16:07:28 -04001258 dsos = &machine->user_dsos;
Robert Richter08d95bd2012-02-10 15:41:55 +01001259 break;
1260 default:
1261 goto out;
1262 }
1263
Waiman Long8fa7d872014-09-29 16:07:28 -04001264 dso = __dsos__findnew(dsos, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001265 if (dso != NULL) {
1266 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1267
1268 dso__set_build_id(dso, &bev->build_id);
1269
Jiri Olsae746b3e2015-02-12 22:16:34 +01001270 if (!is_kernel_module(filename))
Robert Richter08d95bd2012-02-10 15:41:55 +01001271 dso->kernel = dso_type;
1272
1273 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1274 sbuild_id);
1275 pr_debug("build id event received for %s: %s\n",
1276 dso->long_name, sbuild_id);
1277 }
1278
1279 err = 0;
1280out:
1281 return err;
1282}
1283
1284static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1285 int input, u64 offset, u64 size)
1286{
1287 struct perf_session *session = container_of(header, struct perf_session, header);
1288 struct {
1289 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001290 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001291 char filename[0];
1292 } old_bev;
1293 struct build_id_event bev;
1294 char filename[PATH_MAX];
1295 u64 limit = offset + size;
1296
1297 while (offset < limit) {
1298 ssize_t len;
1299
Namhyung Kim5323f602012-12-17 15:38:54 +09001300 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001301 return -1;
1302
1303 if (header->needs_swap)
1304 perf_event_header__bswap(&old_bev.header);
1305
1306 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001307 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001308 return -1;
1309
1310 bev.header = old_bev.header;
1311
1312 /*
1313 * As the pid is the missing value, we need to fill
1314 * it properly. The header.misc value give us nice hint.
1315 */
1316 bev.pid = HOST_KERNEL_ID;
1317 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1318 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1319 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1320
1321 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1322 __event_process_build_id(&bev, filename, session);
1323
1324 offset += bev.header.size;
1325 }
1326
1327 return 0;
1328}
1329
1330static int perf_header__read_build_ids(struct perf_header *header,
1331 int input, u64 offset, u64 size)
1332{
1333 struct perf_session *session = container_of(header, struct perf_session, header);
1334 struct build_id_event bev;
1335 char filename[PATH_MAX];
1336 u64 limit = offset + size, orig_offset = offset;
1337 int err = -1;
1338
1339 while (offset < limit) {
1340 ssize_t len;
1341
Namhyung Kim5323f602012-12-17 15:38:54 +09001342 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001343 goto out;
1344
1345 if (header->needs_swap)
1346 perf_event_header__bswap(&bev.header);
1347
1348 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001349 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001350 goto out;
1351 /*
1352 * The a1645ce1 changeset:
1353 *
1354 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1355 *
1356 * Added a field to struct build_id_event that broke the file
1357 * format.
1358 *
1359 * Since the kernel build-id is the first entry, process the
1360 * table using the old format if the well known
1361 * '[kernel.kallsyms]' string for the kernel build-id has the
1362 * first 4 characters chopped off (where the pid_t sits).
1363 */
1364 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1365 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1366 return -1;
1367 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1368 }
1369
1370 __event_process_build_id(&bev, filename, session);
1371
1372 offset += bev.header.size;
1373 }
1374 err = 0;
1375out:
1376 return err;
1377}
1378
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001379static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1380 struct perf_header *ph __maybe_unused,
1381 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001382{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001383 ssize_t ret = trace_report(fd, data, false);
1384 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001385}
1386
1387static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001388 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001389 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001390{
1391 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1392 pr_debug("Failed to read buildids, continuing...\n");
1393 return 0;
1394}
1395
Namhyung Kima1ae5652012-09-24 17:14:59 +09001396static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001397 struct perf_header *ph, int fd,
1398 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001399{
1400 ph->env.hostname = do_read_string(fd, ph);
1401 return ph->env.hostname ? 0 : -ENOMEM;
1402}
1403
1404static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001405 struct perf_header *ph, int fd,
1406 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001407{
1408 ph->env.os_release = do_read_string(fd, ph);
1409 return ph->env.os_release ? 0 : -ENOMEM;
1410}
1411
1412static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001413 struct perf_header *ph, int fd,
1414 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001415{
1416 ph->env.version = do_read_string(fd, ph);
1417 return ph->env.version ? 0 : -ENOMEM;
1418}
1419
1420static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001421 struct perf_header *ph, int fd,
1422 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001423{
1424 ph->env.arch = do_read_string(fd, ph);
1425 return ph->env.arch ? 0 : -ENOMEM;
1426}
1427
1428static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001429 struct perf_header *ph, int fd,
1430 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001431{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001432 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001433 u32 nr;
1434
Namhyung Kim5323f602012-12-17 15:38:54 +09001435 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001436 if (ret != sizeof(nr))
1437 return -1;
1438
1439 if (ph->needs_swap)
1440 nr = bswap_32(nr);
1441
1442 ph->env.nr_cpus_online = nr;
1443
Namhyung Kim5323f602012-12-17 15:38:54 +09001444 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001445 if (ret != sizeof(nr))
1446 return -1;
1447
1448 if (ph->needs_swap)
1449 nr = bswap_32(nr);
1450
1451 ph->env.nr_cpus_avail = nr;
1452 return 0;
1453}
1454
1455static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001456 struct perf_header *ph, int fd,
1457 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001458{
1459 ph->env.cpu_desc = do_read_string(fd, ph);
1460 return ph->env.cpu_desc ? 0 : -ENOMEM;
1461}
1462
1463static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001464 struct perf_header *ph, int fd,
1465 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001466{
1467 ph->env.cpuid = do_read_string(fd, ph);
1468 return ph->env.cpuid ? 0 : -ENOMEM;
1469}
1470
1471static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001472 struct perf_header *ph, int fd,
1473 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001474{
1475 uint64_t mem;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001476 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001477
Namhyung Kim5323f602012-12-17 15:38:54 +09001478 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001479 if (ret != sizeof(mem))
1480 return -1;
1481
1482 if (ph->needs_swap)
1483 mem = bswap_64(mem);
1484
1485 ph->env.total_mem = mem;
1486 return 0;
1487}
1488
Robert Richter7c2f7af2012-08-16 21:10:23 +02001489static struct perf_evsel *
1490perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1491{
1492 struct perf_evsel *evsel;
1493
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001494 evlist__for_each(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001495 if (evsel->idx == idx)
1496 return evsel;
1497 }
1498
1499 return NULL;
1500}
1501
1502static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001503perf_evlist__set_event_name(struct perf_evlist *evlist,
1504 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001505{
1506 struct perf_evsel *evsel;
1507
1508 if (!event->name)
1509 return;
1510
1511 evsel = perf_evlist__find_by_index(evlist, event->idx);
1512 if (!evsel)
1513 return;
1514
1515 if (evsel->name)
1516 return;
1517
1518 evsel->name = strdup(event->name);
1519}
1520
1521static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001522process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001523 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001524 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001525{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001526 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001527 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1528
1529 if (!events)
1530 return 0;
1531
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001532 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001533 for (evsel = events; evsel->attr.size; evsel++)
1534 perf_evlist__set_event_name(session->evlist, evsel);
1535
1536 free_event_desc(events);
1537
1538 return 0;
1539}
1540
Namhyung Kima1ae5652012-09-24 17:14:59 +09001541static int process_cmdline(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001542 struct perf_header *ph, int fd,
1543 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001544{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001545 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001546 char *str;
1547 u32 nr, i;
1548 struct strbuf sb;
1549
Namhyung Kim5323f602012-12-17 15:38:54 +09001550 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001551 if (ret != sizeof(nr))
1552 return -1;
1553
1554 if (ph->needs_swap)
1555 nr = bswap_32(nr);
1556
1557 ph->env.nr_cmdline = nr;
1558 strbuf_init(&sb, 128);
1559
1560 for (i = 0; i < nr; i++) {
1561 str = do_read_string(fd, ph);
1562 if (!str)
1563 goto error;
1564
1565 /* include a NULL character at the end */
1566 strbuf_add(&sb, str, strlen(str) + 1);
1567 free(str);
1568 }
1569 ph->env.cmdline = strbuf_detach(&sb, NULL);
1570 return 0;
1571
1572error:
1573 strbuf_release(&sb);
1574 return -1;
1575}
1576
1577static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001578 struct perf_header *ph, int fd,
1579 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001580{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001581 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001582 u32 nr, i;
1583 char *str;
1584 struct strbuf sb;
1585
Namhyung Kim5323f602012-12-17 15:38:54 +09001586 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001587 if (ret != sizeof(nr))
1588 return -1;
1589
1590 if (ph->needs_swap)
1591 nr = bswap_32(nr);
1592
1593 ph->env.nr_sibling_cores = nr;
1594 strbuf_init(&sb, 128);
1595
1596 for (i = 0; i < nr; i++) {
1597 str = do_read_string(fd, ph);
1598 if (!str)
1599 goto error;
1600
1601 /* include a NULL character at the end */
1602 strbuf_add(&sb, str, strlen(str) + 1);
1603 free(str);
1604 }
1605 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1606
Namhyung Kim5323f602012-12-17 15:38:54 +09001607 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001608 if (ret != sizeof(nr))
1609 return -1;
1610
1611 if (ph->needs_swap)
1612 nr = bswap_32(nr);
1613
1614 ph->env.nr_sibling_threads = nr;
1615
1616 for (i = 0; i < nr; i++) {
1617 str = do_read_string(fd, ph);
1618 if (!str)
1619 goto error;
1620
1621 /* include a NULL character at the end */
1622 strbuf_add(&sb, str, strlen(str) + 1);
1623 free(str);
1624 }
1625 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1626 return 0;
1627
1628error:
1629 strbuf_release(&sb);
1630 return -1;
1631}
1632
1633static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001634 struct perf_header *ph, int fd,
1635 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001636{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001637 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001638 u32 nr, node, i;
1639 char *str;
1640 uint64_t mem_total, mem_free;
1641 struct strbuf sb;
1642
1643 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001644 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001645 if (ret != sizeof(nr))
1646 goto error;
1647
1648 if (ph->needs_swap)
1649 nr = bswap_32(nr);
1650
1651 ph->env.nr_numa_nodes = nr;
1652 strbuf_init(&sb, 256);
1653
1654 for (i = 0; i < nr; i++) {
1655 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001656 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001657 if (ret != sizeof(node))
1658 goto error;
1659
Namhyung Kim5323f602012-12-17 15:38:54 +09001660 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001661 if (ret != sizeof(u64))
1662 goto error;
1663
Namhyung Kim5323f602012-12-17 15:38:54 +09001664 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001665 if (ret != sizeof(u64))
1666 goto error;
1667
1668 if (ph->needs_swap) {
1669 node = bswap_32(node);
1670 mem_total = bswap_64(mem_total);
1671 mem_free = bswap_64(mem_free);
1672 }
1673
1674 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1675 node, mem_total, mem_free);
1676
1677 str = do_read_string(fd, ph);
1678 if (!str)
1679 goto error;
1680
1681 /* include a NULL character at the end */
1682 strbuf_add(&sb, str, strlen(str) + 1);
1683 free(str);
1684 }
1685 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1686 return 0;
1687
1688error:
1689 strbuf_release(&sb);
1690 return -1;
1691}
1692
1693static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001694 struct perf_header *ph, int fd,
1695 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001696{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001697 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001698 char *name;
1699 u32 pmu_num;
1700 u32 type;
1701 struct strbuf sb;
1702
Namhyung Kim5323f602012-12-17 15:38:54 +09001703 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001704 if (ret != sizeof(pmu_num))
1705 return -1;
1706
1707 if (ph->needs_swap)
1708 pmu_num = bswap_32(pmu_num);
1709
1710 if (!pmu_num) {
1711 pr_debug("pmu mappings not available\n");
1712 return 0;
1713 }
1714
1715 ph->env.nr_pmu_mappings = pmu_num;
1716 strbuf_init(&sb, 128);
1717
1718 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001719 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001720 goto error;
1721 if (ph->needs_swap)
1722 type = bswap_32(type);
1723
1724 name = do_read_string(fd, ph);
1725 if (!name)
1726 goto error;
1727
1728 strbuf_addf(&sb, "%u:%s", type, name);
1729 /* include a NULL character at the end */
1730 strbuf_add(&sb, "", 1);
1731
1732 free(name);
1733 pmu_num--;
1734 }
1735 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1736 return 0;
1737
1738error:
1739 strbuf_release(&sb);
1740 return -1;
1741}
1742
Namhyung Kima8bb5592013-01-22 18:09:31 +09001743static int process_group_desc(struct perf_file_section *section __maybe_unused,
1744 struct perf_header *ph, int fd,
1745 void *data __maybe_unused)
1746{
1747 size_t ret = -1;
1748 u32 i, nr, nr_groups;
1749 struct perf_session *session;
1750 struct perf_evsel *evsel, *leader = NULL;
1751 struct group_desc {
1752 char *name;
1753 u32 leader_idx;
1754 u32 nr_members;
1755 } *desc;
1756
1757 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
1758 return -1;
1759
1760 if (ph->needs_swap)
1761 nr_groups = bswap_32(nr_groups);
1762
1763 ph->env.nr_groups = nr_groups;
1764 if (!nr_groups) {
1765 pr_debug("group desc not available\n");
1766 return 0;
1767 }
1768
1769 desc = calloc(nr_groups, sizeof(*desc));
1770 if (!desc)
1771 return -1;
1772
1773 for (i = 0; i < nr_groups; i++) {
1774 desc[i].name = do_read_string(fd, ph);
1775 if (!desc[i].name)
1776 goto out_free;
1777
1778 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
1779 goto out_free;
1780
1781 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
1782 goto out_free;
1783
1784 if (ph->needs_swap) {
1785 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
1786 desc[i].nr_members = bswap_32(desc[i].nr_members);
1787 }
1788 }
1789
1790 /*
1791 * Rebuild group relationship based on the group_desc
1792 */
1793 session = container_of(ph, struct perf_session, header);
1794 session->evlist->nr_groups = nr_groups;
1795
1796 i = nr = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001797 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001798 if (evsel->idx == (int) desc[i].leader_idx) {
1799 evsel->leader = evsel;
1800 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09001801 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001802 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09001803 desc[i].name = NULL;
1804 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09001805 evsel->nr_members = desc[i].nr_members;
1806
1807 if (i >= nr_groups || nr > 0) {
1808 pr_debug("invalid group desc\n");
1809 goto out_free;
1810 }
1811
1812 leader = evsel;
1813 nr = evsel->nr_members - 1;
1814 i++;
1815 } else if (nr) {
1816 /* This is a group member */
1817 evsel->leader = leader;
1818
1819 nr--;
1820 }
1821 }
1822
1823 if (i != nr_groups || nr != 0) {
1824 pr_debug("invalid group desc\n");
1825 goto out_free;
1826 }
1827
1828 ret = 0;
1829out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09001830 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001831 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001832 free(desc);
1833
1834 return ret;
1835}
1836
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001837struct feature_ops {
1838 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1839 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01001840 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001841 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001842 const char *name;
1843 bool full_only;
1844};
1845
Robert Richter8cdfa782011-12-07 10:02:56 +01001846#define FEAT_OPA(n, func) \
1847 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01001848#define FEAT_OPP(n, func) \
1849 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1850 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01001851#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01001852 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09001853 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01001854
1855/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001856#define print_tracing_data NULL
1857#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001858
1859static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001860 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01001861 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001862 FEAT_OPP(HEADER_HOSTNAME, hostname),
1863 FEAT_OPP(HEADER_OSRELEASE, osrelease),
1864 FEAT_OPP(HEADER_VERSION, version),
1865 FEAT_OPP(HEADER_ARCH, arch),
1866 FEAT_OPP(HEADER_NRCPUS, nrcpus),
1867 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09001868 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001869 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02001870 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001871 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01001872 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1873 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01001874 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001875 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09001876 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Adrian Hunter4025ea42015-04-09 18:53:41 +03001877 FEAT_OPA(HEADER_AUXTRACE, auxtrace),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001878};
1879
1880struct header_print_data {
1881 FILE *fp;
1882 bool full; /* extended list of headers */
1883};
1884
1885static int perf_file_section__fprintf_info(struct perf_file_section *section,
1886 struct perf_header *ph,
1887 int feat, int fd, void *data)
1888{
1889 struct header_print_data *hd = data;
1890
1891 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1892 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1893 "%d, continuing...\n", section->offset, feat);
1894 return 0;
1895 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001896 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001897 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01001898 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001899 }
1900 if (!feat_ops[feat].print)
1901 return 0;
1902
1903 if (!feat_ops[feat].full_only || hd->full)
1904 feat_ops[feat].print(ph, fd, hd->fp);
1905 else
1906 fprintf(hd->fp, "# %s info available, use -I to display\n",
1907 feat_ops[feat].name);
1908
1909 return 0;
1910}
1911
1912int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1913{
1914 struct header_print_data hd;
1915 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001916 int fd = perf_data_file__fd(session->file);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001917 hd.fp = fp;
1918 hd.full = full;
1919
1920 perf_header__process_sections(header, fd, &hd,
1921 perf_file_section__fprintf_info);
1922 return 0;
1923}
1924
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001925static int do_write_feat(int fd, struct perf_header *h, int type,
1926 struct perf_file_section **p,
1927 struct perf_evlist *evlist)
1928{
1929 int err;
1930 int ret = 0;
1931
1932 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001933 if (!feat_ops[type].write)
1934 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001935
1936 (*p)->offset = lseek(fd, 0, SEEK_CUR);
1937
1938 err = feat_ops[type].write(fd, h, evlist);
1939 if (err < 0) {
1940 pr_debug("failed to write feature %d\n", type);
1941
1942 /* undo anything written */
1943 lseek(fd, (*p)->offset, SEEK_SET);
1944
1945 return -1;
1946 }
1947 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
1948 (*p)++;
1949 }
1950 return ret;
1951}
1952
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001953static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001954 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001955{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001956 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001957 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001958 int sec_size;
1959 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001960 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001961 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001962
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001963 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001964 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001965 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001966
Paul Gortmaker91b98802013-01-30 20:05:49 -05001967 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001968 if (feat_sec == NULL)
1969 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001970
1971 sec_size = sizeof(*feat_sec) * nr_sections;
1972
Jiri Olsa8d541e92013-07-17 19:49:44 +02001973 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08001974 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001975
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001976 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
1977 if (do_write_feat(fd, header, feat, &p, evlist))
1978 perf_header__clear_feat(header, feat);
1979 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001980
Xiao Guangrongf887f302010-02-04 16:46:42 +08001981 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001982 /*
1983 * may write more than needed due to dropped feature, but
1984 * this is okay, reader will skip the mising entries
1985 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001986 err = do_write(fd, feat_sec, sec_size);
1987 if (err < 0)
1988 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001989 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001990 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001991}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001992
Tom Zanussi8dc58102010-04-01 23:59:15 -05001993int perf_header__write_pipe(int fd)
1994{
1995 struct perf_pipe_file_header f_header;
1996 int err;
1997
1998 f_header = (struct perf_pipe_file_header){
1999 .magic = PERF_MAGIC,
2000 .size = sizeof(f_header),
2001 };
2002
2003 err = do_write(fd, &f_header, sizeof(f_header));
2004 if (err < 0) {
2005 pr_debug("failed to write perf pipe header\n");
2006 return err;
2007 }
2008
2009 return 0;
2010}
2011
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002012int perf_session__write_header(struct perf_session *session,
2013 struct perf_evlist *evlist,
2014 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002015{
2016 struct perf_file_header f_header;
2017 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002018 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002019 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002020 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002021 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002022
2023 lseek(fd, sizeof(f_header), SEEK_SET);
2024
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002025 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002026 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2027 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002028 if (err < 0) {
2029 pr_debug("failed to write perf header\n");
2030 return err;
2031 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002032 }
2033
Jiri Olsa944d62b2013-07-17 19:49:43 +02002034 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002035
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002036 evlist__for_each(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002037 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002038 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002039 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002040 .offset = evsel->id_offset,
2041 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002042 }
2043 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002044 err = do_write(fd, &f_attr, sizeof(f_attr));
2045 if (err < 0) {
2046 pr_debug("failed to write perf header attribute\n");
2047 return err;
2048 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002049 }
2050
Adrian Hunterd645c442013-12-11 14:36:28 +02002051 if (!header->data_offset)
2052 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002053 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002054
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002055 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002056 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002057 if (err < 0)
2058 return err;
2059 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002060
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002061 f_header = (struct perf_file_header){
2062 .magic = PERF_MAGIC,
2063 .size = sizeof(f_header),
2064 .attr_size = sizeof(f_attr),
2065 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002066 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002067 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002068 },
2069 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002070 .offset = header->data_offset,
2071 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002072 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002073 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002074 };
2075
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002076 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002077
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002078 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002079 err = do_write(fd, &f_header, sizeof(f_header));
2080 if (err < 0) {
2081 pr_debug("failed to write perf header\n");
2082 return err;
2083 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002084 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002085
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002086 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002087}
2088
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002089static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002090 int fd, void *buf, size_t size)
2091{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002092 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002093 return -1;
2094
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002095 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002096 mem_bswap_64(buf, size);
2097
2098 return 0;
2099}
2100
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002101int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002102 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002103 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002104 struct perf_header *ph,
2105 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002106{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002107 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002108 int nr_sections;
2109 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002110 int feat;
2111 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002112
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002113 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002114 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002115 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002116
Paul Gortmaker91b98802013-01-30 20:05:49 -05002117 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002118 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002119 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002120
2121 sec_size = sizeof(*feat_sec) * nr_sections;
2122
Jiri Olsa8d541e92013-07-17 19:49:44 +02002123 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002124
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002125 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2126 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002127 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002128
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002129 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2130 err = process(sec++, header, feat, fd, data);
2131 if (err < 0)
2132 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002133 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002134 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002135out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002136 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002137 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002138}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002139
Stephane Eranian114382a2012-02-09 23:21:08 +01002140static const int attr_file_abi_sizes[] = {
2141 [0] = PERF_ATTR_SIZE_VER0,
2142 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002143 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002144 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002145 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002146 0,
2147};
2148
2149/*
2150 * In the legacy file format, the magic number is not used to encode endianness.
2151 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2152 * on ABI revisions, we need to try all combinations for all endianness to
2153 * detect the endianness.
2154 */
2155static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2156{
2157 uint64_t ref_size, attr_size;
2158 int i;
2159
2160 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2161 ref_size = attr_file_abi_sizes[i]
2162 + sizeof(struct perf_file_section);
2163 if (hdr_sz != ref_size) {
2164 attr_size = bswap_64(hdr_sz);
2165 if (attr_size != ref_size)
2166 continue;
2167
2168 ph->needs_swap = true;
2169 }
2170 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2171 i,
2172 ph->needs_swap);
2173 return 0;
2174 }
2175 /* could not determine endianness */
2176 return -1;
2177}
2178
2179#define PERF_PIPE_HDR_VER0 16
2180
2181static const size_t attr_pipe_abi_sizes[] = {
2182 [0] = PERF_PIPE_HDR_VER0,
2183 0,
2184};
2185
2186/*
2187 * In the legacy pipe format, there is an implicit assumption that endiannesss
2188 * between host recording the samples, and host parsing the samples is the
2189 * same. This is not always the case given that the pipe output may always be
2190 * redirected into a file and analyzed on a different machine with possibly a
2191 * different endianness and perf_event ABI revsions in the perf tool itself.
2192 */
2193static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2194{
2195 u64 attr_size;
2196 int i;
2197
2198 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2199 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2200 attr_size = bswap_64(hdr_sz);
2201 if (attr_size != hdr_sz)
2202 continue;
2203
2204 ph->needs_swap = true;
2205 }
2206 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2207 return 0;
2208 }
2209 return -1;
2210}
2211
Feng Tange84ba4e2012-10-30 11:56:07 +08002212bool is_perf_magic(u64 magic)
2213{
2214 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2215 || magic == __perf_magic2
2216 || magic == __perf_magic2_sw)
2217 return true;
2218
2219 return false;
2220}
2221
Stephane Eranian114382a2012-02-09 23:21:08 +01002222static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2223 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002224{
2225 int ret;
2226
2227 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002228 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002229 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002230 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002231 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002232 if (is_pipe)
2233 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002234
Stephane Eranian114382a2012-02-09 23:21:08 +01002235 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002236 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002237 /*
2238 * the new magic number serves two purposes:
2239 * - unique number to identify actual perf.data files
2240 * - encode endianness of file
2241 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002242 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002243
Stephane Eranian114382a2012-02-09 23:21:08 +01002244 /* check magic number with one endianness */
2245 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002246 return 0;
2247
Stephane Eranian114382a2012-02-09 23:21:08 +01002248 /* check magic number with opposite endianness */
2249 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002250 return -1;
2251
2252 ph->needs_swap = true;
2253
2254 return 0;
2255}
2256
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002257int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002258 struct perf_header *ph, int fd)
2259{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002260 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002261
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002262 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002263
Stephane Eranian73323f52012-02-02 13:54:44 +01002264 ret = readn(fd, header, sizeof(*header));
2265 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002266 return -1;
2267
Stephane Eranian114382a2012-02-09 23:21:08 +01002268 if (check_magic_endian(header->magic,
2269 header->attr_size, false, ph) < 0) {
2270 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002271 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002272 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002273
Stephane Eranian73323f52012-02-02 13:54:44 +01002274 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002275 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002276 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002277 }
2278
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002279 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002280 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002281 if (header->size == offsetof(typeof(*header), adds_features))
2282 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002283 else
2284 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002285 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002286 /*
2287 * feature bitmap is declared as an array of unsigned longs --
2288 * not good since its size can differ between the host that
2289 * generated the data file and the host analyzing the file.
2290 *
2291 * We need to handle endianness, but we don't know the size of
2292 * the unsigned long where the file was generated. Take a best
2293 * guess at determining it: try 64-bit swap first (ie., file
2294 * created on a 64-bit host), and check if the hostname feature
2295 * bit is set (this feature bit is forced on as of fbe96f2).
2296 * If the bit is not, undo the 64-bit swap and try a 32-bit
2297 * swap. If the hostname bit is still not set (e.g., older data
2298 * file), punt and fallback to the original behavior --
2299 * clearing all feature bits and setting buildid.
2300 */
David Ahern80c01202012-06-08 11:47:51 -03002301 mem_bswap_64(&header->adds_features,
2302 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002303
2304 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002305 /* unswap as u64 */
2306 mem_bswap_64(&header->adds_features,
2307 BITS_TO_U64(HEADER_FEAT_BITS));
2308
2309 /* unswap as u32 */
2310 mem_bswap_32(&header->adds_features,
2311 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002312 }
2313
2314 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2315 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2316 set_bit(HEADER_BUILD_ID, header->adds_features);
2317 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002318 }
2319
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002320 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002321 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002322
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002323 ph->data_offset = header->data.offset;
2324 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002325 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002326 return 0;
2327}
2328
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002329static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002330 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002331 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002332{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002333 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002334 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002335 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002336 return 0;
2337 }
2338
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002339 if (feat >= HEADER_LAST_FEATURE) {
2340 pr_debug("unknown feature %d, continuing...\n", feat);
2341 return 0;
2342 }
2343
Robert Richterf1c67db2012-02-10 15:41:56 +01002344 if (!feat_ops[feat].process)
2345 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002346
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002347 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002348}
2349
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002350static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002351 struct perf_header *ph, int fd,
2352 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002353{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002354 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002355
2356 ret = readn(fd, header, sizeof(*header));
2357 if (ret <= 0)
2358 return -1;
2359
Stephane Eranian114382a2012-02-09 23:21:08 +01002360 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2361 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002362 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002363 }
2364
2365 if (ph->needs_swap)
2366 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002367
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002368 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002369 return -1;
2370
Tom Zanussi8dc58102010-04-01 23:59:15 -05002371 return 0;
2372}
2373
Jiri Olsad4339562013-07-17 19:49:41 +02002374static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002375{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002376 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002377 struct perf_pipe_file_header f_header;
2378
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002379 if (perf_file_header__read_pipe(&f_header, header,
2380 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002381 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002382 pr_debug("incompatible file format\n");
2383 return -EINVAL;
2384 }
2385
Tom Zanussi8dc58102010-04-01 23:59:15 -05002386 return 0;
2387}
2388
Stephane Eranian69996df2012-02-09 23:21:06 +01002389static int read_attr(int fd, struct perf_header *ph,
2390 struct perf_file_attr *f_attr)
2391{
2392 struct perf_event_attr *attr = &f_attr->attr;
2393 size_t sz, left;
2394 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002395 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002396
2397 memset(f_attr, 0, sizeof(*f_attr));
2398
2399 /* read minimal guaranteed structure */
2400 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2401 if (ret <= 0) {
2402 pr_debug("cannot read %d bytes of header attr\n",
2403 PERF_ATTR_SIZE_VER0);
2404 return -1;
2405 }
2406
2407 /* on file perf_event_attr size */
2408 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002409
Stephane Eranian69996df2012-02-09 23:21:06 +01002410 if (ph->needs_swap)
2411 sz = bswap_32(sz);
2412
2413 if (sz == 0) {
2414 /* assume ABI0 */
2415 sz = PERF_ATTR_SIZE_VER0;
2416 } else if (sz > our_sz) {
2417 pr_debug("file uses a more recent and unsupported ABI"
2418 " (%zu bytes extra)\n", sz - our_sz);
2419 return -1;
2420 }
2421 /* what we have not yet read and that we know about */
2422 left = sz - PERF_ATTR_SIZE_VER0;
2423 if (left) {
2424 void *ptr = attr;
2425 ptr += PERF_ATTR_SIZE_VER0;
2426
2427 ret = readn(fd, ptr, left);
2428 }
2429 /* read perf_file_section, ids are read in caller */
2430 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2431
2432 return ret <= 0 ? -1 : 0;
2433}
2434
Namhyung Kim831394b2012-09-06 11:10:46 +09002435static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2436 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002437{
Namhyung Kim831394b2012-09-06 11:10:46 +09002438 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002439 char bf[128];
2440
Namhyung Kim831394b2012-09-06 11:10:46 +09002441 /* already prepared */
2442 if (evsel->tp_format)
2443 return 0;
2444
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002445 if (pevent == NULL) {
2446 pr_debug("broken or missing trace data\n");
2447 return -1;
2448 }
2449
Namhyung Kim831394b2012-09-06 11:10:46 +09002450 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002451 if (event == NULL)
2452 return -1;
2453
Namhyung Kim831394b2012-09-06 11:10:46 +09002454 if (!evsel->name) {
2455 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2456 evsel->name = strdup(bf);
2457 if (evsel->name == NULL)
2458 return -1;
2459 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002460
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002461 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002462 return 0;
2463}
2464
Namhyung Kim831394b2012-09-06 11:10:46 +09002465static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2466 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002467{
2468 struct perf_evsel *pos;
2469
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002470 evlist__for_each(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002471 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2472 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002473 return -1;
2474 }
2475
2476 return 0;
2477}
2478
Jiri Olsad4339562013-07-17 19:49:41 +02002479int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002480{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002481 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002482 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002483 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002484 struct perf_file_attr f_attr;
2485 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002486 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002487 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002488
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002489 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002490 if (session->evlist == NULL)
2491 return -ENOMEM;
2492
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002493 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002494 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002495
Stephane Eranian69996df2012-02-09 23:21:06 +01002496 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002497 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002498
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002499 /*
2500 * Sanity check that perf.data was written cleanly; data size is
2501 * initialized to 0 and updated only if the on_exit function is run.
2502 * If data size is still 0 then the file contains only partial
2503 * information. Just warn user and process it as much as it can.
2504 */
2505 if (f_header.data.size == 0) {
2506 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2507 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002508 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002509 }
2510
Stephane Eranian69996df2012-02-09 23:21:06 +01002511 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002512 lseek(fd, f_header.attrs.offset, SEEK_SET);
2513
2514 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002515 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002516 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002517
Stephane Eranian69996df2012-02-09 23:21:06 +01002518 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002519 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002520
David Ahern1060ab82015-04-09 16:15:46 -04002521 if (header->needs_swap) {
2522 f_attr.ids.size = bswap_64(f_attr.ids.size);
2523 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002524 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002525 }
David Aherneda39132011-07-15 12:34:09 -06002526
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002527 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002528 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002529
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002530 if (evsel == NULL)
2531 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002532
2533 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002534 /*
2535 * Do it before so that if perf_evsel__alloc_id fails, this
2536 * entry gets purged too at perf_evlist__delete().
2537 */
2538 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002539
2540 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002541 /*
2542 * We don't have the cpu and thread maps on the header, so
2543 * for allocating the perf_sample_id table we fake 1 cpu and
2544 * hattr->ids threads.
2545 */
2546 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2547 goto out_delete_evlist;
2548
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002549 lseek(fd, f_attr.ids.offset, SEEK_SET);
2550
2551 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002552 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002553 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002554
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002555 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002556 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002557
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002558 lseek(fd, tmp, SEEK_SET);
2559 }
2560
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002561 symbol_conf.nr_events = nr_attrs;
2562
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002563 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002564 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002565
Namhyung Kim831394b2012-09-06 11:10:46 +09002566 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002567 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002568 goto out_delete_evlist;
2569
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002570 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002571out_errno:
2572 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002573
2574out_delete_evlist:
2575 perf_evlist__delete(session->evlist);
2576 session->evlist = NULL;
2577 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002578}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002579
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002580int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002581 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002582 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002583{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002584 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002585 size_t size;
2586 int err;
2587
2588 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002589 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002590 size += sizeof(struct perf_event_header);
2591 size += ids * sizeof(u64);
2592
2593 ev = malloc(size);
2594
Chris Samuelce47dc52010-11-13 13:35:06 +11002595 if (ev == NULL)
2596 return -ENOMEM;
2597
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002598 ev->attr.attr = *attr;
2599 memcpy(ev->attr.id, id, ids * sizeof(u64));
2600
2601 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002602 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002603
Robert Richterf4d83432012-08-16 21:10:17 +02002604 if (ev->attr.header.size == size)
2605 err = process(tool, ev, NULL, NULL);
2606 else
2607 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002608
2609 free(ev);
2610
2611 return err;
2612}
2613
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002614int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002615 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002616 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002617{
Robert Richter6606f872012-08-16 21:10:19 +02002618 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002619 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002620
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002621 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002622 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2623 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002624 if (err) {
2625 pr_debug("failed to create perf header attribute\n");
2626 return err;
2627 }
2628 }
2629
2630 return err;
2631}
2632
Adrian Hunter47c3d102013-07-04 16:20:21 +03002633int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2634 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002635 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002636{
Robert Richterf4d83432012-08-16 21:10:17 +02002637 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002638 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002639 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002640
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002641 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002642 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002643 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002644 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002645 }
2646
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002647 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002648 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002649 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002650
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002651 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002652
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002653 ids = event->header.size;
2654 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002655 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002656 /*
2657 * We don't have the cpu and thread maps on the header, so
2658 * for allocating the perf_sample_id table we fake 1 cpu and
2659 * hattr->ids threads.
2660 */
2661 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2662 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002663
2664 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002665 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002666 }
2667
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03002668 symbol_conf.nr_events = evlist->nr_entries;
2669
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002670 return 0;
2671}
Tom Zanussicd19a032010-04-01 23:59:20 -05002672
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002673int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002674 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002675 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002676{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002677 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002678 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002679 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002680 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002681
Jiri Olsa29208e52011-10-20 15:59:43 +02002682 /*
2683 * We are going to store the size of the data followed
2684 * by the data contents. Since the fd descriptor is a pipe,
2685 * we cannot seek back to store the size of the data once
2686 * we know it. Instead we:
2687 *
2688 * - write the tracing data to the temp file
2689 * - get/write the data size to pipe
2690 * - write the tracing data from the temp file
2691 * to the pipe
2692 */
2693 tdata = tracing_data_get(&evlist->entries, fd, true);
2694 if (!tdata)
2695 return -1;
2696
Tom Zanussi92155452010-04-01 23:59:21 -05002697 memset(&ev, 0, sizeof(ev));
2698
2699 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002700 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002701 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05002702 padding = aligned_size - size;
2703 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2704 ev.tracing_data.size = aligned_size;
2705
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002706 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002707
Jiri Olsa29208e52011-10-20 15:59:43 +02002708 /*
2709 * The put function will copy all the tracing data
2710 * stored in temp file to the pipe.
2711 */
2712 tracing_data_put(tdata);
2713
Tom Zanussi92155452010-04-01 23:59:21 -05002714 write_padded(fd, NULL, 0, padding);
2715
2716 return aligned_size;
2717}
2718
Adrian Hunter47c3d102013-07-04 16:20:21 +03002719int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2720 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002721 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002722{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002723 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002724 int fd = perf_data_file__fd(session->file);
2725 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05002726 char buf[BUFSIZ];
2727
2728 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002729 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05002730 SEEK_SET);
2731
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002732 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002733 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002734 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05002735
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002736 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002737 pr_err("%s: reading input file", __func__);
2738 return -1;
2739 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002740 if (session->repipe) {
2741 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002742 if (retw <= 0 || retw != padding) {
2743 pr_err("%s: repiping tracing data padding", __func__);
2744 return -1;
2745 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002746 }
Tom Zanussi92155452010-04-01 23:59:21 -05002747
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002748 if (size_read + padding != size) {
2749 pr_err("%s: tracing data size mismatch", __func__);
2750 return -1;
2751 }
Tom Zanussi92155452010-04-01 23:59:21 -05002752
Namhyung Kim831394b2012-09-06 11:10:46 +09002753 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002754 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03002755
Tom Zanussi92155452010-04-01 23:59:21 -05002756 return size_read + padding;
2757}
Tom Zanussic7929e42010-04-01 23:59:22 -05002758
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002759int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002760 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002761 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002762 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05002763{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002764 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05002765 size_t len;
2766 int err = 0;
2767
2768 if (!pos->hit)
2769 return err;
2770
2771 memset(&ev, 0, sizeof(ev));
2772
2773 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002774 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05002775 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2776 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2777 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002778 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05002779 ev.build_id.header.size = sizeof(ev.build_id) + len;
2780 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2781
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002782 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05002783
2784 return err;
2785}
2786
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002787int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002788 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002789 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05002790{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002791 __event_process_build_id(&event->build_id,
2792 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002793 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05002794 return 0;
2795}