blob: 954f0ef8e7126619276076b3e254827372c94040 [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03002#include <inttypes.h>
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02003#include "util.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -03004#include "string2.h"
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -03005#include <sys/param.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02006#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02007#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02008#include <unistd.h>
9#include <stdio.h>
10#include <stdlib.h>
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -030011#include <linux/compiler.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010012#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -020013#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +010014#include <linux/bitops.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030015#include <sys/stat.h>
16#include <sys/types.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020017#include <sys/utsname.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030018#include <unistd.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020019
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020020#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030021#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020022#include "header.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030023#include "memswap.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020024#include "../perf.h"
25#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020026#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010027#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010028#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020029#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020030#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020031#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090032#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020033#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020034#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010035#include <api/fs/fs.h>
36#include "asm/bug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020037
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030038#include "sane_ctype.h"
39
Stephane Eranian73323f52012-02-02 13:54:44 +010040/*
41 * magic2 = "PERFILE2"
42 * must be a numerical value to let the endianness
43 * determine the memory layout. That way we are able
44 * to detect endianness when reading the perf.data file
45 * back.
46 *
47 * we check for legacy (PERFFILE) format.
48 */
49static const char *__perf_magic1 = "PERFFILE";
50static const u64 __perf_magic2 = 0x32454c4946524550ULL;
51static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020052
Stephane Eranian73323f52012-02-02 13:54:44 +010053#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020054
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090055const char perf_version_string[] = PERF_VERSION;
56
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020057struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020058 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020059 struct perf_file_section ids;
60};
61
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030062void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020063{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030064 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020065}
66
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030067void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020068{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030069 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020070}
71
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030072bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020073{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030074 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020075}
76
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -070077/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisneros3b8f51a2017-07-17 21:25:38 -070078int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020079{
David Carrillo-Cisneros3b8f51a2017-07-17 21:25:38 -070080 ssize_t ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020081
David Carrillo-Cisneros3b8f51a2017-07-17 21:25:38 -070082 ret = writen(fd, buf, size);
83 if (ret != (ssize_t)size)
84 return ret < 0 ? (int)ret : -1;
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020085
86 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020087}
88
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -070089/* Return: 0 if succeded, -ERR if failed. */
Namhyung Kime195fac2014-11-04 10:14:30 +090090int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020091{
92 static const char zero_buf[NAME_ALIGN];
93 int err = do_write(fd, bf, count);
94
95 if (!err)
96 err = do_write(fd, zero_buf, count_aligned - count);
97
98 return err;
99}
100
Kan Liang2bb00d22015-09-01 09:58:12 -0400101#define string_size(str) \
102 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
103
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700104/* Return: 0 if succeded, -ERR if failed. */
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200105static int do_write_string(int fd, const char *str)
106{
107 u32 len, olen;
108 int ret;
109
110 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300111 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200112
113 /* write len, incl. \0 */
114 ret = do_write(fd, &len, sizeof(len));
115 if (ret < 0)
116 return ret;
117
118 return write_padded(fd, str, olen, len);
119}
120
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700121static int __do_read(int fd, void *addr, ssize_t size)
122{
123 ssize_t ret = readn(fd, addr, size);
124
125 if (ret != size)
126 return ret < 0 ? (int)ret : -1;
127 return 0;
128}
129
130static int do_read_u32(int fd, struct perf_header *ph, u32 *addr)
131{
132 int ret;
133
134 ret = __do_read(fd, addr, sizeof(*addr));
135 if (ret)
136 return ret;
137
138 if (ph->needs_swap)
139 *addr = bswap_32(*addr);
140 return 0;
141}
142
143static int do_read_u64(int fd, struct perf_header *ph, u64 *addr)
144{
145 int ret;
146
147 ret = __do_read(fd, addr, sizeof(*addr));
148 if (ret)
149 return ret;
150
151 if (ph->needs_swap)
152 *addr = bswap_64(*addr);
153 return 0;
154}
155
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200156static char *do_read_string(int fd, struct perf_header *ph)
157{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200158 u32 len;
159 char *buf;
160
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700161 if (do_read_u32(fd, ph, &len))
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200162 return NULL;
163
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200164 buf = malloc(len);
165 if (!buf)
166 return NULL;
167
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700168 if (!__do_read(fd, buf, len)) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200169 /*
170 * strings are padded by zeroes
171 * thus the actual strlen of buf
172 * may be less than len
173 */
174 return buf;
175 }
176
177 free(buf);
178 return NULL;
179}
180
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300181static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200182 struct perf_evlist *evlist)
183{
184 return read_tracing_data(fd, &evlist->entries);
185}
186
187
188static int write_build_id(int fd, struct perf_header *h,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300189 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200190{
191 struct perf_session *session;
192 int err;
193
194 session = container_of(h, struct perf_session, header);
195
Robert Richtere20960c2011-12-07 10:02:55 +0100196 if (!perf_session__read_build_ids(session, true))
197 return -1;
198
Namhyung Kim714c9c42014-11-04 10:14:29 +0900199 err = perf_session__write_buildid_table(session, fd);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200200 if (err < 0) {
201 pr_debug("failed to write buildid table\n");
202 return err;
203 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900204 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200205
206 return 0;
207}
208
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300209static int write_hostname(int fd, struct perf_header *h __maybe_unused,
210 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200211{
212 struct utsname uts;
213 int ret;
214
215 ret = uname(&uts);
216 if (ret < 0)
217 return -1;
218
219 return do_write_string(fd, uts.nodename);
220}
221
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300222static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
223 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200224{
225 struct utsname uts;
226 int ret;
227
228 ret = uname(&uts);
229 if (ret < 0)
230 return -1;
231
232 return do_write_string(fd, uts.release);
233}
234
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300235static int write_arch(int fd, struct perf_header *h __maybe_unused,
236 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200237{
238 struct utsname uts;
239 int ret;
240
241 ret = uname(&uts);
242 if (ret < 0)
243 return -1;
244
245 return do_write_string(fd, uts.machine);
246}
247
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300248static int write_version(int fd, struct perf_header *h __maybe_unused,
249 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200250{
251 return do_write_string(fd, perf_version_string);
252}
253
Wang Nan493c3032014-10-24 09:45:26 +0800254static int __write_cpudesc(int fd, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200255{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200256 FILE *file;
257 char *buf = NULL;
258 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800259 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200260 size_t len = 0;
261 int ret = -1;
262
263 if (!search)
264 return -1;
265
266 file = fopen("/proc/cpuinfo", "r");
267 if (!file)
268 return -1;
269
270 while (getline(&buf, &len, file) > 0) {
271 ret = strncmp(buf, search, strlen(search));
272 if (!ret)
273 break;
274 }
275
Wang Naned307752014-10-16 11:08:29 +0800276 if (ret) {
277 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200278 goto done;
Wang Naned307752014-10-16 11:08:29 +0800279 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200280
281 s = buf;
282
283 p = strchr(buf, ':');
284 if (p && *(p+1) == ' ' && *(p+2))
285 s = p + 2;
286 p = strchr(s, '\n');
287 if (p)
288 *p = '\0';
289
290 /* squash extra space characters (branding string) */
291 p = s;
292 while (*p) {
293 if (isspace(*p)) {
294 char *r = p + 1;
295 char *q = r;
296 *p = ' ';
297 while (*q && isspace(*q))
298 q++;
299 if (q != (p+1))
300 while ((*r++ = *q++));
301 }
302 p++;
303 }
304 ret = do_write_string(fd, s);
305done:
306 free(buf);
307 fclose(file);
308 return ret;
309}
310
Wang Nan493c3032014-10-24 09:45:26 +0800311static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
312 struct perf_evlist *evlist __maybe_unused)
313{
314#ifndef CPUINFO_PROC
315#define CPUINFO_PROC {"model name", }
316#endif
317 const char *cpuinfo_procs[] = CPUINFO_PROC;
318 unsigned int i;
319
320 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
321 int ret;
322 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
323 if (ret >= 0)
324 return ret;
325 }
326 return -1;
327}
328
329
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300330static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
331 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200332{
333 long nr;
334 u32 nrc, nra;
335 int ret;
336
Jan Stancekda8a58b2017-02-17 12:10:26 +0100337 nrc = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200338
339 nr = sysconf(_SC_NPROCESSORS_ONLN);
340 if (nr < 0)
341 return -1;
342
343 nra = (u32)(nr & UINT_MAX);
344
345 ret = do_write(fd, &nrc, sizeof(nrc));
346 if (ret < 0)
347 return ret;
348
349 return do_write(fd, &nra, sizeof(nra));
350}
351
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300352static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200353 struct perf_evlist *evlist)
354{
Robert Richter6606f872012-08-16 21:10:19 +0200355 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900356 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200357 int ret;
358
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900359 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200360
361 /*
362 * write number of events
363 */
364 ret = do_write(fd, &nre, sizeof(nre));
365 if (ret < 0)
366 return ret;
367
368 /*
369 * size of perf_event_attr struct
370 */
Robert Richter6606f872012-08-16 21:10:19 +0200371 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200372 ret = do_write(fd, &sz, sizeof(sz));
373 if (ret < 0)
374 return ret;
375
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300376 evlist__for_each_entry(evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +0200377 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200378 if (ret < 0)
379 return ret;
380 /*
381 * write number of unique id per event
382 * there is one id per instance of an event
383 *
384 * copy into an nri to be independent of the
385 * type of ids,
386 */
Robert Richter6606f872012-08-16 21:10:19 +0200387 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200388 ret = do_write(fd, &nri, sizeof(nri));
389 if (ret < 0)
390 return ret;
391
392 /*
393 * write event string as passed on cmdline
394 */
Robert Richter6606f872012-08-16 21:10:19 +0200395 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200396 if (ret < 0)
397 return ret;
398 /*
399 * write unique ids for this event
400 */
Robert Richter6606f872012-08-16 21:10:19 +0200401 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200402 if (ret < 0)
403 return ret;
404 }
405 return 0;
406}
407
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300408static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
409 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200410{
411 char buf[MAXPATHLEN];
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300412 u32 n;
413 int i, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200414
Tommi Rantala55f771282017-03-22 15:06:24 +0200415 /* actual path to perf binary */
416 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200417 if (ret <= 0)
418 return -1;
419
420 /* readlink() does not add null termination */
421 buf[ret] = '\0';
422
423 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300424 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200425
426 ret = do_write(fd, &n, sizeof(n));
427 if (ret < 0)
428 return ret;
429
430 ret = do_write_string(fd, buf);
431 if (ret < 0)
432 return ret;
433
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300434 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
435 ret = do_write_string(fd, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200436 if (ret < 0)
437 return ret;
438 }
439 return 0;
440}
441
442#define CORE_SIB_FMT \
443 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
444#define THRD_SIB_FMT \
445 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
446
447struct cpu_topo {
Kan Liang2bb00d22015-09-01 09:58:12 -0400448 u32 cpu_nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200449 u32 core_sib;
450 u32 thread_sib;
451 char **core_siblings;
452 char **thread_siblings;
453};
454
455static int build_cpu_topo(struct cpu_topo *tp, int cpu)
456{
457 FILE *fp;
458 char filename[MAXPATHLEN];
459 char *buf = NULL, *p;
460 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200461 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200462 u32 i = 0;
463 int ret = -1;
464
465 sprintf(filename, CORE_SIB_FMT, cpu);
466 fp = fopen(filename, "r");
467 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200468 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200469
Stephane Eranianc5885742013-08-14 12:04:26 +0200470 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200471 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200472 if (sret <= 0)
473 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200474
475 p = strchr(buf, '\n');
476 if (p)
477 *p = '\0';
478
479 for (i = 0; i < tp->core_sib; i++) {
480 if (!strcmp(buf, tp->core_siblings[i]))
481 break;
482 }
483 if (i == tp->core_sib) {
484 tp->core_siblings[i] = buf;
485 tp->core_sib++;
486 buf = NULL;
487 len = 0;
488 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200489 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200490
Stephane Eranianc5885742013-08-14 12:04:26 +0200491try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200492 sprintf(filename, THRD_SIB_FMT, cpu);
493 fp = fopen(filename, "r");
494 if (!fp)
495 goto done;
496
497 if (getline(&buf, &len, fp) <= 0)
498 goto done;
499
500 p = strchr(buf, '\n');
501 if (p)
502 *p = '\0';
503
504 for (i = 0; i < tp->thread_sib; i++) {
505 if (!strcmp(buf, tp->thread_siblings[i]))
506 break;
507 }
508 if (i == tp->thread_sib) {
509 tp->thread_siblings[i] = buf;
510 tp->thread_sib++;
511 buf = NULL;
512 }
513 ret = 0;
514done:
515 if(fp)
516 fclose(fp);
517 free(buf);
518 return ret;
519}
520
521static void free_cpu_topo(struct cpu_topo *tp)
522{
523 u32 i;
524
525 if (!tp)
526 return;
527
528 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300529 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200530
531 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300532 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200533
534 free(tp);
535}
536
537static struct cpu_topo *build_cpu_topology(void)
538{
Jan Stancek43db2842017-02-17 12:10:25 +0100539 struct cpu_topo *tp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200540 void *addr;
541 u32 nr, i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300542 size_t sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200543 long ncpus;
544 int ret = -1;
Jan Stancek43db2842017-02-17 12:10:25 +0100545 struct cpu_map *map;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200546
Jan Stancekda8a58b2017-02-17 12:10:26 +0100547 ncpus = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200548
Jan Stancek43db2842017-02-17 12:10:25 +0100549 /* build online CPU map */
550 map = cpu_map__new(NULL);
551 if (map == NULL) {
552 pr_debug("failed to get system cpumap\n");
553 return NULL;
554 }
555
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200556 nr = (u32)(ncpus & UINT_MAX);
557
558 sz = nr * sizeof(char *);
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300559 addr = calloc(1, sizeof(*tp) + 2 * sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200560 if (!addr)
Jan Stancek43db2842017-02-17 12:10:25 +0100561 goto out_free;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200562
563 tp = addr;
Kan Liang2bb00d22015-09-01 09:58:12 -0400564 tp->cpu_nr = nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200565 addr += sizeof(*tp);
566 tp->core_siblings = addr;
567 addr += sz;
568 tp->thread_siblings = addr;
569
570 for (i = 0; i < nr; i++) {
Jan Stancek43db2842017-02-17 12:10:25 +0100571 if (!cpu_map__has(map, i))
572 continue;
573
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200574 ret = build_cpu_topo(tp, i);
575 if (ret < 0)
576 break;
577 }
Jan Stancek43db2842017-02-17 12:10:25 +0100578
579out_free:
580 cpu_map__put(map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200581 if (ret) {
582 free_cpu_topo(tp);
583 tp = NULL;
584 }
585 return tp;
586}
587
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300588static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
589 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200590{
591 struct cpu_topo *tp;
592 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300593 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200594
595 tp = build_cpu_topology();
596 if (!tp)
597 return -1;
598
599 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
600 if (ret < 0)
601 goto done;
602
603 for (i = 0; i < tp->core_sib; i++) {
604 ret = do_write_string(fd, tp->core_siblings[i]);
605 if (ret < 0)
606 goto done;
607 }
608 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
609 if (ret < 0)
610 goto done;
611
612 for (i = 0; i < tp->thread_sib; i++) {
613 ret = do_write_string(fd, tp->thread_siblings[i]);
614 if (ret < 0)
615 break;
616 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400617
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300618 ret = perf_env__read_cpu_topology_map(&perf_env);
619 if (ret < 0)
620 goto done;
621
622 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
623 ret = do_write(fd, &perf_env.cpu[j].core_id,
624 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400625 if (ret < 0)
626 return ret;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300627 ret = do_write(fd, &perf_env.cpu[j].socket_id,
628 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400629 if (ret < 0)
630 return ret;
631 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200632done:
633 free_cpu_topo(tp);
634 return ret;
635}
636
637
638
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300639static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
640 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200641{
642 char *buf = NULL;
643 FILE *fp;
644 size_t len = 0;
645 int ret = -1, n;
646 uint64_t mem;
647
648 fp = fopen("/proc/meminfo", "r");
649 if (!fp)
650 return -1;
651
652 while (getline(&buf, &len, fp) > 0) {
653 ret = strncmp(buf, "MemTotal:", 9);
654 if (!ret)
655 break;
656 }
657 if (!ret) {
658 n = sscanf(buf, "%*s %"PRIu64, &mem);
659 if (n == 1)
660 ret = do_write(fd, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800661 } else
662 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200663 free(buf);
664 fclose(fp);
665 return ret;
666}
667
668static int write_topo_node(int fd, int node)
669{
670 char str[MAXPATHLEN];
671 char field[32];
672 char *buf = NULL, *p;
673 size_t len = 0;
674 FILE *fp;
675 u64 mem_total, mem_free, mem;
676 int ret = -1;
677
678 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
679 fp = fopen(str, "r");
680 if (!fp)
681 return -1;
682
683 while (getline(&buf, &len, fp) > 0) {
684 /* skip over invalid lines */
685 if (!strchr(buf, ':'))
686 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100687 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200688 goto done;
689 if (!strcmp(field, "MemTotal:"))
690 mem_total = mem;
691 if (!strcmp(field, "MemFree:"))
692 mem_free = mem;
693 }
694
695 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100696 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200697
698 ret = do_write(fd, &mem_total, sizeof(u64));
699 if (ret)
700 goto done;
701
702 ret = do_write(fd, &mem_free, sizeof(u64));
703 if (ret)
704 goto done;
705
706 ret = -1;
707 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
708
709 fp = fopen(str, "r");
710 if (!fp)
711 goto done;
712
713 if (getline(&buf, &len, fp) <= 0)
714 goto done;
715
716 p = strchr(buf, '\n');
717 if (p)
718 *p = '\0';
719
720 ret = do_write_string(fd, buf);
721done:
722 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100723 if (fp)
724 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200725 return ret;
726}
727
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300728static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
729 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200730{
731 char *buf = NULL;
732 size_t len = 0;
733 FILE *fp;
734 struct cpu_map *node_map = NULL;
735 char *c;
736 u32 nr, i, j;
737 int ret = -1;
738
739 fp = fopen("/sys/devices/system/node/online", "r");
740 if (!fp)
741 return -1;
742
743 if (getline(&buf, &len, fp) <= 0)
744 goto done;
745
746 c = strchr(buf, '\n');
747 if (c)
748 *c = '\0';
749
750 node_map = cpu_map__new(buf);
751 if (!node_map)
752 goto done;
753
754 nr = (u32)node_map->nr;
755
756 ret = do_write(fd, &nr, sizeof(nr));
757 if (ret < 0)
758 goto done;
759
760 for (i = 0; i < nr; i++) {
761 j = (u32)node_map->map[i];
762 ret = do_write(fd, &j, sizeof(j));
763 if (ret < 0)
764 break;
765
766 ret = write_topo_node(fd, i);
767 if (ret < 0)
768 break;
769 }
770done:
771 free(buf);
772 fclose(fp);
Masami Hiramatsu5191d8872015-12-09 11:11:35 +0900773 cpu_map__put(node_map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200774 return ret;
775}
776
777/*
Robert Richter50a96672012-08-16 21:10:24 +0200778 * File format:
779 *
780 * struct pmu_mappings {
781 * u32 pmu_num;
782 * struct pmu_map {
783 * u32 type;
784 * char name[];
785 * }[pmu_num];
786 * };
787 */
788
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300789static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
790 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200791{
792 struct perf_pmu *pmu = NULL;
793 off_t offset = lseek(fd, 0, SEEK_CUR);
794 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900795 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200796
797 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +0900798 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
799 if (ret < 0)
800 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200801
802 while ((pmu = perf_pmu__scan(pmu))) {
803 if (!pmu->name)
804 continue;
805 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +0900806
807 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
808 if (ret < 0)
809 return ret;
810
811 ret = do_write_string(fd, pmu->name);
812 if (ret < 0)
813 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200814 }
815
816 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
817 /* discard all */
818 lseek(fd, offset, SEEK_SET);
819 return -1;
820 }
821
822 return 0;
823}
824
825/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900826 * File format:
827 *
828 * struct group_descs {
829 * u32 nr_groups;
830 * struct group_desc {
831 * char name[];
832 * u32 leader_idx;
833 * u32 nr_members;
834 * }[nr_groups];
835 * };
836 */
837static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
838 struct perf_evlist *evlist)
839{
840 u32 nr_groups = evlist->nr_groups;
841 struct perf_evsel *evsel;
842 int ret;
843
844 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
845 if (ret < 0)
846 return ret;
847
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300848 evlist__for_each_entry(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900849 if (perf_evsel__is_group_leader(evsel) &&
850 evsel->nr_members > 1) {
851 const char *name = evsel->group_name ?: "{anon_group}";
852 u32 leader_idx = evsel->idx;
853 u32 nr_members = evsel->nr_members;
854
855 ret = do_write_string(fd, name);
856 if (ret < 0)
857 return ret;
858
859 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
860 if (ret < 0)
861 return ret;
862
863 ret = do_write(fd, &nr_members, sizeof(nr_members));
864 if (ret < 0)
865 return ret;
866 }
867 }
868 return 0;
869}
870
871/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200872 * default get_cpuid(): nothing gets recorded
Jiada Wang7a759cd2017-04-09 20:02:37 -0700873 * actual implementation must be in arch/$(SRCARCH)/util/header.c
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200874 */
Rui Teng11d8f872016-07-28 10:05:57 +0800875int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200876{
877 return -1;
878}
879
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300880static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
881 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200882{
883 char buffer[64];
884 int ret;
885
886 ret = get_cpuid(buffer, sizeof(buffer));
887 if (!ret)
888 goto write_it;
889
890 return -1;
891write_it:
892 return do_write_string(fd, buffer);
893}
894
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300895static int write_branch_stack(int fd __maybe_unused,
896 struct perf_header *h __maybe_unused,
897 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100898{
899 return 0;
900}
901
Adrian Hunter99fa2982015-04-30 17:37:25 +0300902static int write_auxtrace(int fd, struct perf_header *h,
Adrian Hunter4025ea42015-04-09 18:53:41 +0300903 struct perf_evlist *evlist __maybe_unused)
904{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300905 struct perf_session *session;
906 int err;
907
908 session = container_of(h, struct perf_session, header);
909
910 err = auxtrace_index__write(fd, &session->auxtrace_index);
911 if (err < 0)
912 pr_err("Failed to write auxtrace index\n");
913 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300914}
915
Jiri Olsa720e98b2016-02-16 16:01:43 +0100916static int cpu_cache_level__sort(const void *a, const void *b)
917{
918 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
919 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
920
921 return cache_a->level - cache_b->level;
922}
923
924static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
925{
926 if (a->level != b->level)
927 return false;
928
929 if (a->line_size != b->line_size)
930 return false;
931
932 if (a->sets != b->sets)
933 return false;
934
935 if (a->ways != b->ways)
936 return false;
937
938 if (strcmp(a->type, b->type))
939 return false;
940
941 if (strcmp(a->size, b->size))
942 return false;
943
944 if (strcmp(a->map, b->map))
945 return false;
946
947 return true;
948}
949
950static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
951{
952 char path[PATH_MAX], file[PATH_MAX];
953 struct stat st;
954 size_t len;
955
956 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
957 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
958
959 if (stat(file, &st))
960 return 1;
961
962 scnprintf(file, PATH_MAX, "%s/level", path);
963 if (sysfs__read_int(file, (int *) &cache->level))
964 return -1;
965
966 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
967 if (sysfs__read_int(file, (int *) &cache->line_size))
968 return -1;
969
970 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
971 if (sysfs__read_int(file, (int *) &cache->sets))
972 return -1;
973
974 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
975 if (sysfs__read_int(file, (int *) &cache->ways))
976 return -1;
977
978 scnprintf(file, PATH_MAX, "%s/type", path);
979 if (sysfs__read_str(file, &cache->type, &len))
980 return -1;
981
982 cache->type[len] = 0;
983 cache->type = rtrim(cache->type);
984
985 scnprintf(file, PATH_MAX, "%s/size", path);
986 if (sysfs__read_str(file, &cache->size, &len)) {
987 free(cache->type);
988 return -1;
989 }
990
991 cache->size[len] = 0;
992 cache->size = rtrim(cache->size);
993
994 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
995 if (sysfs__read_str(file, &cache->map, &len)) {
996 free(cache->map);
997 free(cache->type);
998 return -1;
999 }
1000
1001 cache->map[len] = 0;
1002 cache->map = rtrim(cache->map);
1003 return 0;
1004}
1005
1006static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1007{
1008 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1009}
1010
1011static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1012{
1013 u32 i, cnt = 0;
1014 long ncpus;
1015 u32 nr, cpu;
1016 u16 level;
1017
1018 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1019 if (ncpus < 0)
1020 return -1;
1021
1022 nr = (u32)(ncpus & UINT_MAX);
1023
1024 for (cpu = 0; cpu < nr; cpu++) {
1025 for (level = 0; level < 10; level++) {
1026 struct cpu_cache_level c;
1027 int err;
1028
1029 err = cpu_cache_level__read(&c, cpu, level);
1030 if (err < 0)
1031 return err;
1032
1033 if (err == 1)
1034 break;
1035
1036 for (i = 0; i < cnt; i++) {
1037 if (cpu_cache_level__cmp(&c, &caches[i]))
1038 break;
1039 }
1040
1041 if (i == cnt)
1042 caches[cnt++] = c;
1043 else
1044 cpu_cache_level__free(&c);
1045
1046 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1047 goto out;
1048 }
1049 }
1050 out:
1051 *cntp = cnt;
1052 return 0;
1053}
1054
1055#define MAX_CACHES 2000
1056
1057static int write_cache(int fd, struct perf_header *h __maybe_unused,
1058 struct perf_evlist *evlist __maybe_unused)
1059{
1060 struct cpu_cache_level caches[MAX_CACHES];
1061 u32 cnt = 0, i, version = 1;
1062 int ret;
1063
1064 ret = build_caches(caches, MAX_CACHES, &cnt);
1065 if (ret)
1066 goto out;
1067
1068 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1069
1070 ret = do_write(fd, &version, sizeof(u32));
1071 if (ret < 0)
1072 goto out;
1073
1074 ret = do_write(fd, &cnt, sizeof(u32));
1075 if (ret < 0)
1076 goto out;
1077
1078 for (i = 0; i < cnt; i++) {
1079 struct cpu_cache_level *c = &caches[i];
1080
1081 #define _W(v) \
1082 ret = do_write(fd, &c->v, sizeof(u32)); \
1083 if (ret < 0) \
1084 goto out;
1085
1086 _W(level)
1087 _W(line_size)
1088 _W(sets)
1089 _W(ways)
1090 #undef _W
1091
1092 #define _W(v) \
1093 ret = do_write_string(fd, (const char *) c->v); \
1094 if (ret < 0) \
1095 goto out;
1096
1097 _W(type)
1098 _W(size)
1099 _W(map)
1100 #undef _W
1101 }
1102
1103out:
1104 for (i = 0; i < cnt; i++)
1105 cpu_cache_level__free(&caches[i]);
1106 return ret;
1107}
1108
Jiri Olsaffa517a2015-10-25 15:51:43 +01001109static int write_stat(int fd __maybe_unused,
1110 struct perf_header *h __maybe_unused,
1111 struct perf_evlist *evlist __maybe_unused)
1112{
1113 return 0;
1114}
1115
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001116static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1117 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001118{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001119 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001120}
1121
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001122static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1123 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001124{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001125 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001126}
1127
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001128static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001129{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001130 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001131}
1132
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001133static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1134 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001135{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001136 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001137}
1138
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001139static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1140 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001141{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001142 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1143 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001144}
1145
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001146static void print_version(struct perf_header *ph, int fd __maybe_unused,
1147 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001148{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001149 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001150}
1151
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001152static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1153 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001154{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001155 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001156
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001157 nr = ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001158
1159 fprintf(fp, "# cmdline : ");
1160
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001161 for (i = 0; i < nr; i++)
1162 fprintf(fp, "%s ", ph->env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001163 fputc('\n', fp);
1164}
1165
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001166static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1167 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001168{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001169 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001170 char *str;
Jan Stancekda8a58b2017-02-17 12:10:26 +01001171 int cpu_nr = ph->env.nr_cpus_avail;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001172
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001173 nr = ph->env.nr_sibling_cores;
1174 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001175
1176 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001177 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001178 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001179 }
1180
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001181 nr = ph->env.nr_sibling_threads;
1182 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001183
1184 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001185 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001186 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001187 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001188
1189 if (ph->env.cpu != NULL) {
1190 for (i = 0; i < cpu_nr; i++)
1191 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1192 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1193 } else
1194 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001195}
1196
Robert Richter4e1b9c62012-08-16 21:10:22 +02001197static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001198{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001199 struct perf_evsel *evsel;
1200
1201 if (!events)
1202 return;
1203
1204 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001205 zfree(&evsel->name);
1206 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001207 }
1208
1209 free(events);
1210}
1211
1212static struct perf_evsel *
1213read_event_desc(struct perf_header *ph, int fd)
1214{
1215 struct perf_evsel *evsel, *events = NULL;
1216 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001217 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001218 u32 nre, sz, nr, i, j;
Stephane Eranian62db9062012-02-09 23:21:07 +01001219 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001220
1221 /* number of events */
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001222 if (do_read_u32(fd, ph, &nre))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001223 goto error;
1224
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001225 if (do_read_u32(fd, ph, &sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001226 goto error;
1227
Stephane Eranian62db9062012-02-09 23:21:07 +01001228 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001229 buf = malloc(sz);
1230 if (!buf)
1231 goto error;
1232
Robert Richter4e1b9c62012-08-16 21:10:22 +02001233 /* the last event terminates with evsel->attr.size == 0: */
1234 events = calloc(nre + 1, sizeof(*events));
1235 if (!events)
1236 goto error;
1237
1238 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001239 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001240 msz = sz;
1241
Robert Richter4e1b9c62012-08-16 21:10:22 +02001242 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1243 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001244
Stephane Eranian62db9062012-02-09 23:21:07 +01001245 /*
1246 * must read entire on-file attr struct to
1247 * sync up with layout.
1248 */
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001249 if (__do_read(fd, buf, sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001250 goto error;
1251
1252 if (ph->needs_swap)
1253 perf_event__attr_swap(buf);
1254
Robert Richter4e1b9c62012-08-16 21:10:22 +02001255 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001256
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001257 if (do_read_u32(fd, ph, &nr))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001258 goto error;
1259
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001260 if (ph->needs_swap)
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001261 evsel->needs_swap = true;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001262
Robert Richter4e1b9c62012-08-16 21:10:22 +02001263 evsel->name = do_read_string(fd, ph);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001264 if (!evsel->name)
1265 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001266
1267 if (!nr)
1268 continue;
1269
1270 id = calloc(nr, sizeof(*id));
1271 if (!id)
1272 goto error;
1273 evsel->ids = nr;
1274 evsel->id = id;
1275
1276 for (j = 0 ; j < nr; j++) {
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001277 if (do_read_u64(fd, ph, id))
Robert Richter4e1b9c62012-08-16 21:10:22 +02001278 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001279 id++;
1280 }
1281 }
1282out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001283 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001284 return events;
1285error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001286 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001287 events = NULL;
1288 goto out;
1289}
1290
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001291static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001292 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001293{
1294 return fprintf(fp, ", %s = %s", name, val);
1295}
1296
Robert Richter4e1b9c62012-08-16 21:10:22 +02001297static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1298{
1299 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1300 u32 j;
1301 u64 *id;
1302
1303 if (!events) {
1304 fprintf(fp, "# event desc: not available or unable to read\n");
1305 return;
1306 }
1307
1308 for (evsel = events; evsel->attr.size; evsel++) {
1309 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001310
Robert Richter4e1b9c62012-08-16 21:10:22 +02001311 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001312 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001313 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1314 if (j)
1315 fputc(',', fp);
1316 fprintf(fp, " %"PRIu64, *id);
1317 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001318 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001319 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001320
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001321 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001322
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001323 fputc('\n', fp);
1324 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001325
1326 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001327}
1328
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001329static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001330 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001331{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001332 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001333}
1334
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001335static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001336 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001337{
Jiri Olsac60da222016-07-04 14:16:20 +02001338 int i;
1339 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001340
Jiri Olsac60da222016-07-04 14:16:20 +02001341 for (i = 0; i < ph->env.nr_numa_nodes; i++) {
1342 n = &ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001343
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001344 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1345 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001346 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001347
Jiri Olsac60da222016-07-04 14:16:20 +02001348 fprintf(fp, "# node%u cpu list : ", n->node);
1349 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001350 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001351}
1352
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001353static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001354{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001355 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001356}
1357
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001358static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001359 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001360{
1361 fprintf(fp, "# contains samples with branch stack\n");
1362}
1363
Adrian Hunter4025ea42015-04-09 18:53:41 +03001364static void print_auxtrace(struct perf_header *ph __maybe_unused,
1365 int fd __maybe_unused, FILE *fp)
1366{
1367 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1368}
1369
Jiri Olsaffa517a2015-10-25 15:51:43 +01001370static void print_stat(struct perf_header *ph __maybe_unused,
1371 int fd __maybe_unused, FILE *fp)
1372{
1373 fprintf(fp, "# contains stat data\n");
1374}
1375
Jiri Olsa720e98b2016-02-16 16:01:43 +01001376static void print_cache(struct perf_header *ph __maybe_unused,
1377 int fd __maybe_unused, FILE *fp __maybe_unused)
1378{
1379 int i;
1380
1381 fprintf(fp, "# CPU cache info:\n");
1382 for (i = 0; i < ph->env.caches_cnt; i++) {
1383 fprintf(fp, "# ");
1384 cpu_cache_level__fprintf(fp, &ph->env.caches[i]);
1385 }
1386}
1387
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001388static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1389 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001390{
1391 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001392 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001393 u32 pmu_num;
1394 u32 type;
1395
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001396 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001397 if (!pmu_num) {
1398 fprintf(fp, "# pmu mappings: not available\n");
1399 return;
1400 }
1401
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001402 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001403
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001404 while (pmu_num) {
1405 type = strtoul(str, &tmp, 0);
1406 if (*tmp != ':')
1407 goto error;
1408
1409 str = tmp + 1;
1410 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1411
Robert Richter50a96672012-08-16 21:10:24 +02001412 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001413 str += strlen(str) + 1;
1414 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001415 }
1416
1417 fprintf(fp, "\n");
1418
1419 if (!pmu_num)
1420 return;
1421error:
1422 fprintf(fp, "# pmu mappings: unable to read\n");
1423}
1424
Namhyung Kima8bb5592013-01-22 18:09:31 +09001425static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1426 FILE *fp)
1427{
1428 struct perf_session *session;
1429 struct perf_evsel *evsel;
1430 u32 nr = 0;
1431
1432 session = container_of(ph, struct perf_session, header);
1433
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001434 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001435 if (perf_evsel__is_group_leader(evsel) &&
1436 evsel->nr_members > 1) {
1437 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1438 perf_evsel__name(evsel));
1439
1440 nr = evsel->nr_members - 1;
1441 } else if (nr) {
1442 fprintf(fp, ",%s", perf_evsel__name(evsel));
1443
1444 if (--nr == 0)
1445 fprintf(fp, "}\n");
1446 }
1447 }
1448}
1449
Robert Richter08d95bd2012-02-10 15:41:55 +01001450static int __event_process_build_id(struct build_id_event *bev,
1451 char *filename,
1452 struct perf_session *session)
1453{
1454 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001455 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001456 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001457 struct dso *dso;
1458 enum dso_kernel_type dso_type;
1459
1460 machine = perf_session__findnew_machine(session, bev->pid);
1461 if (!machine)
1462 goto out;
1463
Wang Nan1f121b02015-06-03 08:52:21 +00001464 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001465
Wang Nan1f121b02015-06-03 08:52:21 +00001466 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001467 case PERF_RECORD_MISC_KERNEL:
1468 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001469 break;
1470 case PERF_RECORD_MISC_GUEST_KERNEL:
1471 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001472 break;
1473 case PERF_RECORD_MISC_USER:
1474 case PERF_RECORD_MISC_GUEST_USER:
1475 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001476 break;
1477 default:
1478 goto out;
1479 }
1480
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001481 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001482 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001483 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001484
1485 dso__set_build_id(dso, &bev->build_id);
1486
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001487 if (dso_type != DSO_TYPE_USER) {
1488 struct kmod_path m = { .name = NULL, };
1489
1490 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09001491 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001492 else
1493 dso->kernel = dso_type;
1494
1495 free(m.name);
1496 }
Robert Richter08d95bd2012-02-10 15:41:55 +01001497
1498 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1499 sbuild_id);
1500 pr_debug("build id event received for %s: %s\n",
1501 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001502 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001503 }
1504
1505 err = 0;
1506out:
1507 return err;
1508}
1509
1510static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1511 int input, u64 offset, u64 size)
1512{
1513 struct perf_session *session = container_of(header, struct perf_session, header);
1514 struct {
1515 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001516 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001517 char filename[0];
1518 } old_bev;
1519 struct build_id_event bev;
1520 char filename[PATH_MAX];
1521 u64 limit = offset + size;
1522
1523 while (offset < limit) {
1524 ssize_t len;
1525
Namhyung Kim5323f602012-12-17 15:38:54 +09001526 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001527 return -1;
1528
1529 if (header->needs_swap)
1530 perf_event_header__bswap(&old_bev.header);
1531
1532 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001533 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001534 return -1;
1535
1536 bev.header = old_bev.header;
1537
1538 /*
1539 * As the pid is the missing value, we need to fill
1540 * it properly. The header.misc value give us nice hint.
1541 */
1542 bev.pid = HOST_KERNEL_ID;
1543 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1544 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1545 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1546
1547 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1548 __event_process_build_id(&bev, filename, session);
1549
1550 offset += bev.header.size;
1551 }
1552
1553 return 0;
1554}
1555
1556static int perf_header__read_build_ids(struct perf_header *header,
1557 int input, u64 offset, u64 size)
1558{
1559 struct perf_session *session = container_of(header, struct perf_session, header);
1560 struct build_id_event bev;
1561 char filename[PATH_MAX];
1562 u64 limit = offset + size, orig_offset = offset;
1563 int err = -1;
1564
1565 while (offset < limit) {
1566 ssize_t len;
1567
Namhyung Kim5323f602012-12-17 15:38:54 +09001568 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001569 goto out;
1570
1571 if (header->needs_swap)
1572 perf_event_header__bswap(&bev.header);
1573
1574 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001575 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001576 goto out;
1577 /*
1578 * The a1645ce1 changeset:
1579 *
1580 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1581 *
1582 * Added a field to struct build_id_event that broke the file
1583 * format.
1584 *
1585 * Since the kernel build-id is the first entry, process the
1586 * table using the old format if the well known
1587 * '[kernel.kallsyms]' string for the kernel build-id has the
1588 * first 4 characters chopped off (where the pid_t sits).
1589 */
1590 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1591 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1592 return -1;
1593 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1594 }
1595
1596 __event_process_build_id(&bev, filename, session);
1597
1598 offset += bev.header.size;
1599 }
1600 err = 0;
1601out:
1602 return err;
1603}
1604
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001605/* Macro for features that simply need to read and store a string. */
1606#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
1607static int process_##__feat(struct perf_file_section *section __maybe_unused, \
1608 struct perf_header *ph, int fd, \
1609 void *data __maybe_unused) \
1610{\
1611 ph->env.__feat_env = do_read_string(fd, ph); \
1612 return ph->env.__feat_env ? 0 : -ENOMEM; \
1613}
1614
1615FEAT_PROCESS_STR_FUN(hostname, hostname);
1616FEAT_PROCESS_STR_FUN(osrelease, os_release);
1617FEAT_PROCESS_STR_FUN(version, version);
1618FEAT_PROCESS_STR_FUN(arch, arch);
1619FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1620FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1621
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001622static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1623 struct perf_header *ph __maybe_unused,
1624 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001625{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001626 ssize_t ret = trace_report(fd, data, false);
1627 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001628}
1629
1630static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001631 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001632 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001633{
1634 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1635 pr_debug("Failed to read buildids, continuing...\n");
1636 return 0;
1637}
1638
Namhyung Kima1ae5652012-09-24 17:14:59 +09001639static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001640 struct perf_header *ph, int fd,
1641 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001642{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001643 int ret;
1644 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001645
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001646 ret = do_read_u32(fd, ph, &nr_cpus_avail);
1647 if (ret)
1648 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001649
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001650 ret = do_read_u32(fd, ph, &nr_cpus_online);
1651 if (ret)
1652 return ret;
1653 ph->env.nr_cpus_avail = (int)nr_cpus_avail;
1654 ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001655 return 0;
1656}
1657
Namhyung Kima1ae5652012-09-24 17:14:59 +09001658static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001659 struct perf_header *ph, int fd,
1660 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001661{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001662 u64 total_mem;
1663 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001664
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001665 ret = do_read_u64(fd, ph, &total_mem);
1666 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001667 return -1;
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001668 ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001669 return 0;
1670}
1671
Robert Richter7c2f7af2012-08-16 21:10:23 +02001672static struct perf_evsel *
1673perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1674{
1675 struct perf_evsel *evsel;
1676
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001677 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001678 if (evsel->idx == idx)
1679 return evsel;
1680 }
1681
1682 return NULL;
1683}
1684
1685static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001686perf_evlist__set_event_name(struct perf_evlist *evlist,
1687 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001688{
1689 struct perf_evsel *evsel;
1690
1691 if (!event->name)
1692 return;
1693
1694 evsel = perf_evlist__find_by_index(evlist, event->idx);
1695 if (!evsel)
1696 return;
1697
1698 if (evsel->name)
1699 return;
1700
1701 evsel->name = strdup(event->name);
1702}
1703
1704static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001705process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001706 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001707 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001708{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001709 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001710 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1711
1712 if (!events)
1713 return 0;
1714
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001715 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001716 for (evsel = events; evsel->attr.size; evsel++)
1717 perf_evlist__set_event_name(session->evlist, evsel);
1718
1719 free_event_desc(events);
1720
1721 return 0;
1722}
1723
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001724static int process_cmdline(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001725 struct perf_header *ph, int fd,
1726 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001727{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001728 char *str, *cmdline = NULL, **argv = NULL;
1729 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001730
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001731 if (do_read_u32(fd, ph, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001732 return -1;
1733
Namhyung Kima1ae5652012-09-24 17:14:59 +09001734 ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001735
1736 cmdline = zalloc(section->size + nr + 1);
1737 if (!cmdline)
1738 return -1;
1739
1740 argv = zalloc(sizeof(char *) * (nr + 1));
1741 if (!argv)
1742 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001743
1744 for (i = 0; i < nr; i++) {
1745 str = do_read_string(fd, ph);
1746 if (!str)
1747 goto error;
1748
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001749 argv[i] = cmdline + len;
1750 memcpy(argv[i], str, strlen(str) + 1);
1751 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001752 free(str);
1753 }
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001754 ph->env.cmdline = cmdline;
1755 ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001756 return 0;
1757
1758error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001759 free(argv);
1760 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001761 return -1;
1762}
1763
Kan Liang2bb00d22015-09-01 09:58:12 -04001764static int process_cpu_topology(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001765 struct perf_header *ph, int fd,
1766 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001767{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001768 u32 nr, i;
1769 char *str;
1770 struct strbuf sb;
Jan Stancekda8a58b2017-02-17 12:10:26 +01001771 int cpu_nr = ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04001772 u64 size = 0;
1773
1774 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1775 if (!ph->env.cpu)
1776 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001777
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001778 if (do_read_u32(fd, ph, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001779 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001780
Namhyung Kima1ae5652012-09-24 17:14:59 +09001781 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001782 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001783 if (strbuf_init(&sb, 128) < 0)
1784 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001785
1786 for (i = 0; i < nr; i++) {
1787 str = do_read_string(fd, ph);
1788 if (!str)
1789 goto error;
1790
1791 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001792 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1793 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001794 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001795 free(str);
1796 }
1797 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1798
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001799 if (do_read_u32(fd, ph, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001800 return -1;
1801
Namhyung Kima1ae5652012-09-24 17:14:59 +09001802 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001803 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001804
1805 for (i = 0; i < nr; i++) {
1806 str = do_read_string(fd, ph);
1807 if (!str)
1808 goto error;
1809
1810 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001811 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1812 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001813 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001814 free(str);
1815 }
1816 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04001817
1818 /*
1819 * The header may be from old perf,
1820 * which doesn't include core id and socket id information.
1821 */
1822 if (section->size <= size) {
1823 zfree(&ph->env.cpu);
1824 return 0;
1825 }
1826
1827 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001828 if (do_read_u32(fd, ph, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001829 goto free_cpu;
1830
Kan Liang2bb00d22015-09-01 09:58:12 -04001831 ph->env.cpu[i].core_id = nr;
1832
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001833 if (do_read_u32(fd, ph, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001834 goto free_cpu;
1835
Jan Stancekda8a58b2017-02-17 12:10:26 +01001836 if (nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001837 pr_debug("socket_id number is too big."
1838 "You may need to upgrade the perf tool.\n");
1839 goto free_cpu;
1840 }
1841
1842 ph->env.cpu[i].socket_id = nr;
1843 }
1844
Namhyung Kima1ae5652012-09-24 17:14:59 +09001845 return 0;
1846
1847error:
1848 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04001849free_cpu:
1850 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001851 return -1;
1852}
1853
1854static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001855 struct perf_header *ph, int fd,
1856 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001857{
Jiri Olsac60da222016-07-04 14:16:20 +02001858 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02001859 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001860 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001861
1862 /* nr nodes */
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001863 if (do_read_u32(fd, ph, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001864 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001865
Jiri Olsac60da222016-07-04 14:16:20 +02001866 nodes = zalloc(sizeof(*nodes) * nr);
1867 if (!nodes)
1868 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001869
1870 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02001871 n = &nodes[i];
1872
Namhyung Kima1ae5652012-09-24 17:14:59 +09001873 /* node number */
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001874 if (do_read_u32(fd, ph, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001875 goto error;
1876
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001877 if (do_read_u64(fd, ph, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001878 goto error;
1879
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001880 if (do_read_u64(fd, ph, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001881 goto error;
1882
Namhyung Kima1ae5652012-09-24 17:14:59 +09001883 str = do_read_string(fd, ph);
1884 if (!str)
1885 goto error;
1886
Jiri Olsac60da222016-07-04 14:16:20 +02001887 n->map = cpu_map__new(str);
1888 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001889 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02001890
Namhyung Kima1ae5652012-09-24 17:14:59 +09001891 free(str);
1892 }
Jiri Olsaf957a532016-10-10 09:56:32 +02001893 ph->env.nr_numa_nodes = nr;
Jiri Olsac60da222016-07-04 14:16:20 +02001894 ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001895 return 0;
1896
1897error:
Jiri Olsac60da222016-07-04 14:16:20 +02001898 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001899 return -1;
1900}
1901
1902static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001903 struct perf_header *ph, int fd,
1904 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001905{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001906 char *name;
1907 u32 pmu_num;
1908 u32 type;
1909 struct strbuf sb;
1910
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001911 if (do_read_u32(fd, ph, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001912 return -1;
1913
Namhyung Kima1ae5652012-09-24 17:14:59 +09001914 if (!pmu_num) {
1915 pr_debug("pmu mappings not available\n");
1916 return 0;
1917 }
1918
1919 ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001920 if (strbuf_init(&sb, 128) < 0)
1921 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001922
1923 while (pmu_num) {
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001924 if (do_read_u32(fd, ph, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001925 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001926
1927 name = do_read_string(fd, ph);
1928 if (!name)
1929 goto error;
1930
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001931 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
1932 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001933 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001934 if (strbuf_add(&sb, "", 1) < 0)
1935 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001936
Kan Liange0838e02015-09-10 11:03:05 -03001937 if (!strcmp(name, "msr"))
1938 ph->env.msr_pmu_type = type;
1939
Namhyung Kima1ae5652012-09-24 17:14:59 +09001940 free(name);
1941 pmu_num--;
1942 }
1943 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1944 return 0;
1945
1946error:
1947 strbuf_release(&sb);
1948 return -1;
1949}
1950
Namhyung Kima8bb5592013-01-22 18:09:31 +09001951static int process_group_desc(struct perf_file_section *section __maybe_unused,
1952 struct perf_header *ph, int fd,
1953 void *data __maybe_unused)
1954{
1955 size_t ret = -1;
1956 u32 i, nr, nr_groups;
1957 struct perf_session *session;
1958 struct perf_evsel *evsel, *leader = NULL;
1959 struct group_desc {
1960 char *name;
1961 u32 leader_idx;
1962 u32 nr_members;
1963 } *desc;
1964
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001965 if (do_read_u32(fd, ph, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001966 return -1;
1967
Namhyung Kima8bb5592013-01-22 18:09:31 +09001968 ph->env.nr_groups = nr_groups;
1969 if (!nr_groups) {
1970 pr_debug("group desc not available\n");
1971 return 0;
1972 }
1973
1974 desc = calloc(nr_groups, sizeof(*desc));
1975 if (!desc)
1976 return -1;
1977
1978 for (i = 0; i < nr_groups; i++) {
1979 desc[i].name = do_read_string(fd, ph);
1980 if (!desc[i].name)
1981 goto out_free;
1982
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001983 if (do_read_u32(fd, ph, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001984 goto out_free;
1985
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001986 if (do_read_u32(fd, ph, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001987 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001988 }
1989
1990 /*
1991 * Rebuild group relationship based on the group_desc
1992 */
1993 session = container_of(ph, struct perf_session, header);
1994 session->evlist->nr_groups = nr_groups;
1995
1996 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001997 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001998 if (evsel->idx == (int) desc[i].leader_idx) {
1999 evsel->leader = evsel;
2000 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002001 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002002 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002003 desc[i].name = NULL;
2004 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09002005 evsel->nr_members = desc[i].nr_members;
2006
2007 if (i >= nr_groups || nr > 0) {
2008 pr_debug("invalid group desc\n");
2009 goto out_free;
2010 }
2011
2012 leader = evsel;
2013 nr = evsel->nr_members - 1;
2014 i++;
2015 } else if (nr) {
2016 /* This is a group member */
2017 evsel->leader = leader;
2018
2019 nr--;
2020 }
2021 }
2022
2023 if (i != nr_groups || nr != 0) {
2024 pr_debug("invalid group desc\n");
2025 goto out_free;
2026 }
2027
2028 ret = 0;
2029out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002030 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002031 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002032 free(desc);
2033
2034 return ret;
2035}
2036
Adrian Hunter99fa2982015-04-30 17:37:25 +03002037static int process_auxtrace(struct perf_file_section *section,
2038 struct perf_header *ph, int fd,
2039 void *data __maybe_unused)
2040{
2041 struct perf_session *session;
2042 int err;
2043
2044 session = container_of(ph, struct perf_session, header);
2045
2046 err = auxtrace_index__process(fd, section->size, session,
2047 ph->needs_swap);
2048 if (err < 0)
2049 pr_err("Failed to process auxtrace index\n");
2050 return err;
2051}
2052
Jiri Olsa720e98b2016-02-16 16:01:43 +01002053static int process_cache(struct perf_file_section *section __maybe_unused,
2054 struct perf_header *ph __maybe_unused, int fd __maybe_unused,
2055 void *data __maybe_unused)
2056{
2057 struct cpu_cache_level *caches;
2058 u32 cnt, i, version;
2059
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002060 if (do_read_u32(fd, ph, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002061 return -1;
2062
Jiri Olsa720e98b2016-02-16 16:01:43 +01002063 if (version != 1)
2064 return -1;
2065
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002066 if (do_read_u32(fd, ph, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002067 return -1;
2068
Jiri Olsa720e98b2016-02-16 16:01:43 +01002069 caches = zalloc(sizeof(*caches) * cnt);
2070 if (!caches)
2071 return -1;
2072
2073 for (i = 0; i < cnt; i++) {
2074 struct cpu_cache_level c;
2075
2076 #define _R(v) \
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002077 if (do_read_u32(fd, ph, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002078 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002079
2080 _R(level)
2081 _R(line_size)
2082 _R(sets)
2083 _R(ways)
2084 #undef _R
2085
2086 #define _R(v) \
2087 c.v = do_read_string(fd, ph); \
2088 if (!c.v) \
2089 goto out_free_caches;
2090
2091 _R(type)
2092 _R(size)
2093 _R(map)
2094 #undef _R
2095
2096 caches[i] = c;
2097 }
2098
2099 ph->env.caches = caches;
2100 ph->env.caches_cnt = cnt;
2101 return 0;
2102out_free_caches:
2103 free(caches);
2104 return -1;
2105}
2106
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002107struct feature_ops {
2108 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2109 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002110 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002111 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002112 const char *name;
2113 bool full_only;
2114};
2115
Robert Richter8cdfa782011-12-07 10:02:56 +01002116#define FEAT_OPA(n, func) \
2117 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002118#define FEAT_OPP(n, func) \
2119 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2120 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002121#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002122 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002123 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002124
2125/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002126#define print_tracing_data NULL
2127#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002128
2129static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002130 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002131 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002132 FEAT_OPP(HEADER_HOSTNAME, hostname),
2133 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2134 FEAT_OPP(HEADER_VERSION, version),
2135 FEAT_OPP(HEADER_ARCH, arch),
2136 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2137 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002138 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002139 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002140 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002141 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002142 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2143 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002144 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002145 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002146 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Adrian Hunter99fa2982015-04-30 17:37:25 +03002147 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
Jiri Olsaffa517a2015-10-25 15:51:43 +01002148 FEAT_OPA(HEADER_STAT, stat),
Jiri Olsa720e98b2016-02-16 16:01:43 +01002149 FEAT_OPF(HEADER_CACHE, cache),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002150};
2151
2152struct header_print_data {
2153 FILE *fp;
2154 bool full; /* extended list of headers */
2155};
2156
2157static int perf_file_section__fprintf_info(struct perf_file_section *section,
2158 struct perf_header *ph,
2159 int feat, int fd, void *data)
2160{
2161 struct header_print_data *hd = data;
2162
2163 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2164 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2165 "%d, continuing...\n", section->offset, feat);
2166 return 0;
2167 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002168 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002169 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002170 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002171 }
2172 if (!feat_ops[feat].print)
2173 return 0;
2174
2175 if (!feat_ops[feat].full_only || hd->full)
2176 feat_ops[feat].print(ph, fd, hd->fp);
2177 else
2178 fprintf(hd->fp, "# %s info available, use -I to display\n",
2179 feat_ops[feat].name);
2180
2181 return 0;
2182}
2183
2184int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2185{
2186 struct header_print_data hd;
2187 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002188 int fd = perf_data_file__fd(session->file);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002189 struct stat st;
Jiri Olsaaabae162016-10-10 09:35:50 +02002190 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002191
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002192 hd.fp = fp;
2193 hd.full = full;
2194
Jiri Olsaf45f5612016-10-10 09:03:07 +02002195 ret = fstat(fd, &st);
2196 if (ret == -1)
2197 return -1;
2198
2199 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2200
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002201 perf_header__process_sections(header, fd, &hd,
2202 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002203
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002204 if (session->file->is_pipe)
2205 return 0;
2206
Jiri Olsaaabae162016-10-10 09:35:50 +02002207 fprintf(fp, "# missing features: ");
2208 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2209 if (bit)
2210 fprintf(fp, "%s ", feat_ops[bit].name);
2211 }
2212
2213 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002214 return 0;
2215}
2216
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002217static int do_write_feat(int fd, struct perf_header *h, int type,
2218 struct perf_file_section **p,
2219 struct perf_evlist *evlist)
2220{
2221 int err;
2222 int ret = 0;
2223
2224 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002225 if (!feat_ops[type].write)
2226 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002227
2228 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2229
2230 err = feat_ops[type].write(fd, h, evlist);
2231 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002232 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002233
2234 /* undo anything written */
2235 lseek(fd, (*p)->offset, SEEK_SET);
2236
2237 return -1;
2238 }
2239 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2240 (*p)++;
2241 }
2242 return ret;
2243}
2244
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002245static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002246 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002247{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002248 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002249 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002250 int sec_size;
2251 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002252 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002253 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002254
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002255 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002256 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002257 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002258
Paul Gortmaker91b98802013-01-30 20:05:49 -05002259 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002260 if (feat_sec == NULL)
2261 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002262
2263 sec_size = sizeof(*feat_sec) * nr_sections;
2264
Jiri Olsa8d541e92013-07-17 19:49:44 +02002265 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002266 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002267
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002268 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2269 if (do_write_feat(fd, header, feat, &p, evlist))
2270 perf_header__clear_feat(header, feat);
2271 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002272
Xiao Guangrongf887f302010-02-04 16:46:42 +08002273 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002274 /*
2275 * may write more than needed due to dropped feature, but
2276 * this is okay, reader will skip the mising entries
2277 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002278 err = do_write(fd, feat_sec, sec_size);
2279 if (err < 0)
2280 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002281 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002282 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002283}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002284
Tom Zanussi8dc58102010-04-01 23:59:15 -05002285int perf_header__write_pipe(int fd)
2286{
2287 struct perf_pipe_file_header f_header;
2288 int err;
2289
2290 f_header = (struct perf_pipe_file_header){
2291 .magic = PERF_MAGIC,
2292 .size = sizeof(f_header),
2293 };
2294
2295 err = do_write(fd, &f_header, sizeof(f_header));
2296 if (err < 0) {
2297 pr_debug("failed to write perf pipe header\n");
2298 return err;
2299 }
2300
2301 return 0;
2302}
2303
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002304int perf_session__write_header(struct perf_session *session,
2305 struct perf_evlist *evlist,
2306 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002307{
2308 struct perf_file_header f_header;
2309 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002310 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002311 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002312 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002313 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002314
2315 lseek(fd, sizeof(f_header), SEEK_SET);
2316
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002317 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002318 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2319 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002320 if (err < 0) {
2321 pr_debug("failed to write perf header\n");
2322 return err;
2323 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002324 }
2325
Jiri Olsa944d62b2013-07-17 19:49:43 +02002326 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002327
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002328 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002329 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002330 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002331 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002332 .offset = evsel->id_offset,
2333 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002334 }
2335 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002336 err = do_write(fd, &f_attr, sizeof(f_attr));
2337 if (err < 0) {
2338 pr_debug("failed to write perf header attribute\n");
2339 return err;
2340 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002341 }
2342
Adrian Hunterd645c442013-12-11 14:36:28 +02002343 if (!header->data_offset)
2344 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002345 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002346
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002347 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002348 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002349 if (err < 0)
2350 return err;
2351 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002352
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002353 f_header = (struct perf_file_header){
2354 .magic = PERF_MAGIC,
2355 .size = sizeof(f_header),
2356 .attr_size = sizeof(f_attr),
2357 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002358 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002359 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002360 },
2361 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002362 .offset = header->data_offset,
2363 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002364 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002365 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002366 };
2367
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002368 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002369
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002370 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002371 err = do_write(fd, &f_header, sizeof(f_header));
2372 if (err < 0) {
2373 pr_debug("failed to write perf header\n");
2374 return err;
2375 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002376 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002377
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002378 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002379}
2380
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002381static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002382 int fd, void *buf, size_t size)
2383{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002384 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002385 return -1;
2386
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002387 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002388 mem_bswap_64(buf, size);
2389
2390 return 0;
2391}
2392
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002393int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002394 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002395 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002396 struct perf_header *ph,
2397 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002398{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002399 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002400 int nr_sections;
2401 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002402 int feat;
2403 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002404
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002405 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002406 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002407 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002408
Paul Gortmaker91b98802013-01-30 20:05:49 -05002409 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002410 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002411 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002412
2413 sec_size = sizeof(*feat_sec) * nr_sections;
2414
Jiri Olsa8d541e92013-07-17 19:49:44 +02002415 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002416
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002417 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2418 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002419 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002420
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002421 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2422 err = process(sec++, header, feat, fd, data);
2423 if (err < 0)
2424 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002425 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002426 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002427out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002428 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002429 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002430}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002431
Stephane Eranian114382a2012-02-09 23:21:08 +01002432static const int attr_file_abi_sizes[] = {
2433 [0] = PERF_ATTR_SIZE_VER0,
2434 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002435 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002436 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002437 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002438 0,
2439};
2440
2441/*
2442 * In the legacy file format, the magic number is not used to encode endianness.
2443 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2444 * on ABI revisions, we need to try all combinations for all endianness to
2445 * detect the endianness.
2446 */
2447static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2448{
2449 uint64_t ref_size, attr_size;
2450 int i;
2451
2452 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2453 ref_size = attr_file_abi_sizes[i]
2454 + sizeof(struct perf_file_section);
2455 if (hdr_sz != ref_size) {
2456 attr_size = bswap_64(hdr_sz);
2457 if (attr_size != ref_size)
2458 continue;
2459
2460 ph->needs_swap = true;
2461 }
2462 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2463 i,
2464 ph->needs_swap);
2465 return 0;
2466 }
2467 /* could not determine endianness */
2468 return -1;
2469}
2470
2471#define PERF_PIPE_HDR_VER0 16
2472
2473static const size_t attr_pipe_abi_sizes[] = {
2474 [0] = PERF_PIPE_HDR_VER0,
2475 0,
2476};
2477
2478/*
2479 * In the legacy pipe format, there is an implicit assumption that endiannesss
2480 * between host recording the samples, and host parsing the samples is the
2481 * same. This is not always the case given that the pipe output may always be
2482 * redirected into a file and analyzed on a different machine with possibly a
2483 * different endianness and perf_event ABI revsions in the perf tool itself.
2484 */
2485static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2486{
2487 u64 attr_size;
2488 int i;
2489
2490 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2491 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2492 attr_size = bswap_64(hdr_sz);
2493 if (attr_size != hdr_sz)
2494 continue;
2495
2496 ph->needs_swap = true;
2497 }
2498 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2499 return 0;
2500 }
2501 return -1;
2502}
2503
Feng Tange84ba4e2012-10-30 11:56:07 +08002504bool is_perf_magic(u64 magic)
2505{
2506 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2507 || magic == __perf_magic2
2508 || magic == __perf_magic2_sw)
2509 return true;
2510
2511 return false;
2512}
2513
Stephane Eranian114382a2012-02-09 23:21:08 +01002514static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2515 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002516{
2517 int ret;
2518
2519 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002520 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002521 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002522 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002523 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002524 if (is_pipe)
2525 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002526
Stephane Eranian114382a2012-02-09 23:21:08 +01002527 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002528 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002529 /*
2530 * the new magic number serves two purposes:
2531 * - unique number to identify actual perf.data files
2532 * - encode endianness of file
2533 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002534 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002535
Stephane Eranian114382a2012-02-09 23:21:08 +01002536 /* check magic number with one endianness */
2537 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002538 return 0;
2539
Stephane Eranian114382a2012-02-09 23:21:08 +01002540 /* check magic number with opposite endianness */
2541 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002542 return -1;
2543
2544 ph->needs_swap = true;
2545
2546 return 0;
2547}
2548
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002549int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002550 struct perf_header *ph, int fd)
2551{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002552 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002553
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002554 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002555
Stephane Eranian73323f52012-02-02 13:54:44 +01002556 ret = readn(fd, header, sizeof(*header));
2557 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002558 return -1;
2559
Stephane Eranian114382a2012-02-09 23:21:08 +01002560 if (check_magic_endian(header->magic,
2561 header->attr_size, false, ph) < 0) {
2562 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002563 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002564 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002565
Stephane Eranian73323f52012-02-02 13:54:44 +01002566 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002567 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002568 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002569 }
2570
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002571 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002572 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002573 if (header->size == offsetof(typeof(*header), adds_features))
2574 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002575 else
2576 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002577 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002578 /*
2579 * feature bitmap is declared as an array of unsigned longs --
2580 * not good since its size can differ between the host that
2581 * generated the data file and the host analyzing the file.
2582 *
2583 * We need to handle endianness, but we don't know the size of
2584 * the unsigned long where the file was generated. Take a best
2585 * guess at determining it: try 64-bit swap first (ie., file
2586 * created on a 64-bit host), and check if the hostname feature
2587 * bit is set (this feature bit is forced on as of fbe96f2).
2588 * If the bit is not, undo the 64-bit swap and try a 32-bit
2589 * swap. If the hostname bit is still not set (e.g., older data
2590 * file), punt and fallback to the original behavior --
2591 * clearing all feature bits and setting buildid.
2592 */
David Ahern80c01202012-06-08 11:47:51 -03002593 mem_bswap_64(&header->adds_features,
2594 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002595
2596 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002597 /* unswap as u64 */
2598 mem_bswap_64(&header->adds_features,
2599 BITS_TO_U64(HEADER_FEAT_BITS));
2600
2601 /* unswap as u32 */
2602 mem_bswap_32(&header->adds_features,
2603 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002604 }
2605
2606 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2607 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2608 set_bit(HEADER_BUILD_ID, header->adds_features);
2609 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002610 }
2611
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002612 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002613 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002614
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002615 ph->data_offset = header->data.offset;
2616 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002617 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002618 return 0;
2619}
2620
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002621static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002622 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002623 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002624{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002625 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002626 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002627 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002628 return 0;
2629 }
2630
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002631 if (feat >= HEADER_LAST_FEATURE) {
2632 pr_debug("unknown feature %d, continuing...\n", feat);
2633 return 0;
2634 }
2635
Robert Richterf1c67db2012-02-10 15:41:56 +01002636 if (!feat_ops[feat].process)
2637 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002638
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002639 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002640}
2641
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002642static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002643 struct perf_header *ph, int fd,
2644 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002645{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002646 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002647
2648 ret = readn(fd, header, sizeof(*header));
2649 if (ret <= 0)
2650 return -1;
2651
Stephane Eranian114382a2012-02-09 23:21:08 +01002652 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2653 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002654 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002655 }
2656
2657 if (ph->needs_swap)
2658 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002659
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002660 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002661 return -1;
2662
Tom Zanussi8dc58102010-04-01 23:59:15 -05002663 return 0;
2664}
2665
Jiri Olsad4339562013-07-17 19:49:41 +02002666static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002667{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002668 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002669 struct perf_pipe_file_header f_header;
2670
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002671 if (perf_file_header__read_pipe(&f_header, header,
2672 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002673 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002674 pr_debug("incompatible file format\n");
2675 return -EINVAL;
2676 }
2677
Tom Zanussi8dc58102010-04-01 23:59:15 -05002678 return 0;
2679}
2680
Stephane Eranian69996df2012-02-09 23:21:06 +01002681static int read_attr(int fd, struct perf_header *ph,
2682 struct perf_file_attr *f_attr)
2683{
2684 struct perf_event_attr *attr = &f_attr->attr;
2685 size_t sz, left;
2686 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002687 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002688
2689 memset(f_attr, 0, sizeof(*f_attr));
2690
2691 /* read minimal guaranteed structure */
2692 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2693 if (ret <= 0) {
2694 pr_debug("cannot read %d bytes of header attr\n",
2695 PERF_ATTR_SIZE_VER0);
2696 return -1;
2697 }
2698
2699 /* on file perf_event_attr size */
2700 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002701
Stephane Eranian69996df2012-02-09 23:21:06 +01002702 if (ph->needs_swap)
2703 sz = bswap_32(sz);
2704
2705 if (sz == 0) {
2706 /* assume ABI0 */
2707 sz = PERF_ATTR_SIZE_VER0;
2708 } else if (sz > our_sz) {
2709 pr_debug("file uses a more recent and unsupported ABI"
2710 " (%zu bytes extra)\n", sz - our_sz);
2711 return -1;
2712 }
2713 /* what we have not yet read and that we know about */
2714 left = sz - PERF_ATTR_SIZE_VER0;
2715 if (left) {
2716 void *ptr = attr;
2717 ptr += PERF_ATTR_SIZE_VER0;
2718
2719 ret = readn(fd, ptr, left);
2720 }
2721 /* read perf_file_section, ids are read in caller */
2722 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2723
2724 return ret <= 0 ? -1 : 0;
2725}
2726
Namhyung Kim831394b2012-09-06 11:10:46 +09002727static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2728 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002729{
Namhyung Kim831394b2012-09-06 11:10:46 +09002730 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002731 char bf[128];
2732
Namhyung Kim831394b2012-09-06 11:10:46 +09002733 /* already prepared */
2734 if (evsel->tp_format)
2735 return 0;
2736
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002737 if (pevent == NULL) {
2738 pr_debug("broken or missing trace data\n");
2739 return -1;
2740 }
2741
Namhyung Kim831394b2012-09-06 11:10:46 +09002742 event = pevent_find_event(pevent, evsel->attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09002743 if (event == NULL) {
2744 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002745 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09002746 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002747
Namhyung Kim831394b2012-09-06 11:10:46 +09002748 if (!evsel->name) {
2749 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2750 evsel->name = strdup(bf);
2751 if (evsel->name == NULL)
2752 return -1;
2753 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002754
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002755 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002756 return 0;
2757}
2758
Namhyung Kim831394b2012-09-06 11:10:46 +09002759static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2760 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002761{
2762 struct perf_evsel *pos;
2763
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002764 evlist__for_each_entry(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002765 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2766 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002767 return -1;
2768 }
2769
2770 return 0;
2771}
2772
Jiri Olsad4339562013-07-17 19:49:41 +02002773int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002774{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002775 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002776 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002777 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002778 struct perf_file_attr f_attr;
2779 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002780 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002781 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002782
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002783 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002784 if (session->evlist == NULL)
2785 return -ENOMEM;
2786
Kan Liang2c071442015-08-28 05:48:05 -04002787 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03002788 session->machines.host.env = &header->env;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002789 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002790 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002791
Stephane Eranian69996df2012-02-09 23:21:06 +01002792 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002793 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002794
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002795 /*
2796 * Sanity check that perf.data was written cleanly; data size is
2797 * initialized to 0 and updated only if the on_exit function is run.
2798 * If data size is still 0 then the file contains only partial
2799 * information. Just warn user and process it as much as it can.
2800 */
2801 if (f_header.data.size == 0) {
2802 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2803 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002804 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002805 }
2806
Stephane Eranian69996df2012-02-09 23:21:06 +01002807 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002808 lseek(fd, f_header.attrs.offset, SEEK_SET);
2809
2810 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002811 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002812 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002813
Stephane Eranian69996df2012-02-09 23:21:06 +01002814 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002815 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002816
David Ahern1060ab82015-04-09 16:15:46 -04002817 if (header->needs_swap) {
2818 f_attr.ids.size = bswap_64(f_attr.ids.size);
2819 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002820 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002821 }
David Aherneda39132011-07-15 12:34:09 -06002822
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002823 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002824 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002825
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002826 if (evsel == NULL)
2827 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002828
2829 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002830 /*
2831 * Do it before so that if perf_evsel__alloc_id fails, this
2832 * entry gets purged too at perf_evlist__delete().
2833 */
2834 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002835
2836 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002837 /*
2838 * We don't have the cpu and thread maps on the header, so
2839 * for allocating the perf_sample_id table we fake 1 cpu and
2840 * hattr->ids threads.
2841 */
2842 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2843 goto out_delete_evlist;
2844
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002845 lseek(fd, f_attr.ids.offset, SEEK_SET);
2846
2847 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002848 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002849 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002850
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002851 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002852 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002853
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002854 lseek(fd, tmp, SEEK_SET);
2855 }
2856
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002857 symbol_conf.nr_events = nr_attrs;
2858
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002859 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002860 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002861
Namhyung Kim831394b2012-09-06 11:10:46 +09002862 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002863 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002864 goto out_delete_evlist;
2865
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002866 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002867out_errno:
2868 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002869
2870out_delete_evlist:
2871 perf_evlist__delete(session->evlist);
2872 session->evlist = NULL;
2873 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002874}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002875
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002876int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002877 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002878 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002879{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002880 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002881 size_t size;
2882 int err;
2883
2884 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002885 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002886 size += sizeof(struct perf_event_header);
2887 size += ids * sizeof(u64);
2888
2889 ev = malloc(size);
2890
Chris Samuelce47dc52010-11-13 13:35:06 +11002891 if (ev == NULL)
2892 return -ENOMEM;
2893
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002894 ev->attr.attr = *attr;
2895 memcpy(ev->attr.id, id, ids * sizeof(u64));
2896
2897 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002898 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002899
Robert Richterf4d83432012-08-16 21:10:17 +02002900 if (ev->attr.header.size == size)
2901 err = process(tool, ev, NULL, NULL);
2902 else
2903 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002904
2905 free(ev);
2906
2907 return err;
2908}
2909
Jiri Olsaa6e52812015-10-25 15:51:37 +01002910static struct event_update_event *
2911event_update_event__new(size_t size, u64 type, u64 id)
2912{
2913 struct event_update_event *ev;
2914
2915 size += sizeof(*ev);
2916 size = PERF_ALIGN(size, sizeof(u64));
2917
2918 ev = zalloc(size);
2919 if (ev) {
2920 ev->header.type = PERF_RECORD_EVENT_UPDATE;
2921 ev->header.size = (u16)size;
2922 ev->type = type;
2923 ev->id = id;
2924 }
2925 return ev;
2926}
2927
2928int
2929perf_event__synthesize_event_update_unit(struct perf_tool *tool,
2930 struct perf_evsel *evsel,
2931 perf_event__handler_t process)
2932{
2933 struct event_update_event *ev;
2934 size_t size = strlen(evsel->unit);
2935 int err;
2936
2937 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
2938 if (ev == NULL)
2939 return -ENOMEM;
2940
2941 strncpy(ev->data, evsel->unit, size);
2942 err = process(tool, (union perf_event *)ev, NULL, NULL);
2943 free(ev);
2944 return err;
2945}
2946
Jiri Olsadaeecbc2015-10-25 15:51:38 +01002947int
2948perf_event__synthesize_event_update_scale(struct perf_tool *tool,
2949 struct perf_evsel *evsel,
2950 perf_event__handler_t process)
2951{
2952 struct event_update_event *ev;
2953 struct event_update_event_scale *ev_data;
2954 int err;
2955
2956 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
2957 if (ev == NULL)
2958 return -ENOMEM;
2959
2960 ev_data = (struct event_update_event_scale *) ev->data;
2961 ev_data->scale = evsel->scale;
2962 err = process(tool, (union perf_event*) ev, NULL, NULL);
2963 free(ev);
2964 return err;
2965}
2966
Jiri Olsa802c9042015-10-25 15:51:39 +01002967int
2968perf_event__synthesize_event_update_name(struct perf_tool *tool,
2969 struct perf_evsel *evsel,
2970 perf_event__handler_t process)
2971{
2972 struct event_update_event *ev;
2973 size_t len = strlen(evsel->name);
2974 int err;
2975
2976 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
2977 if (ev == NULL)
2978 return -ENOMEM;
2979
2980 strncpy(ev->data, evsel->name, len);
2981 err = process(tool, (union perf_event*) ev, NULL, NULL);
2982 free(ev);
2983 return err;
2984}
Jiri Olsadaeecbc2015-10-25 15:51:38 +01002985
Jiri Olsa86ebb092015-10-25 15:51:40 +01002986int
2987perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
2988 struct perf_evsel *evsel,
2989 perf_event__handler_t process)
2990{
2991 size_t size = sizeof(struct event_update_event);
2992 struct event_update_event *ev;
2993 int max, err;
2994 u16 type;
2995
2996 if (!evsel->own_cpus)
2997 return 0;
2998
2999 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3000 if (!ev)
3001 return -ENOMEM;
3002
3003 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3004 ev->header.size = (u16)size;
3005 ev->type = PERF_EVENT_UPDATE__CPUS;
3006 ev->id = evsel->id[0];
3007
3008 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3009 evsel->own_cpus,
3010 type, max);
3011
3012 err = process(tool, (union perf_event*) ev, NULL, NULL);
3013 free(ev);
3014 return err;
3015}
3016
Jiri Olsac853f932015-10-25 15:51:41 +01003017size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3018{
3019 struct event_update_event *ev = &event->event_update;
3020 struct event_update_event_scale *ev_scale;
3021 struct event_update_event_cpus *ev_cpus;
3022 struct cpu_map *map;
3023 size_t ret;
3024
3025 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3026
3027 switch (ev->type) {
3028 case PERF_EVENT_UPDATE__SCALE:
3029 ev_scale = (struct event_update_event_scale *) ev->data;
3030 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3031 break;
3032 case PERF_EVENT_UPDATE__UNIT:
3033 ret += fprintf(fp, "... unit: %s\n", ev->data);
3034 break;
3035 case PERF_EVENT_UPDATE__NAME:
3036 ret += fprintf(fp, "... name: %s\n", ev->data);
3037 break;
3038 case PERF_EVENT_UPDATE__CPUS:
3039 ev_cpus = (struct event_update_event_cpus *) ev->data;
3040 ret += fprintf(fp, "... ");
3041
3042 map = cpu_map__new_data(&ev_cpus->cpus);
3043 if (map)
3044 ret += cpu_map__fprintf(map, fp);
3045 else
3046 ret += fprintf(fp, "failed to get cpus\n");
3047 break;
3048 default:
3049 ret += fprintf(fp, "... unknown type\n");
3050 break;
3051 }
3052
3053 return ret;
3054}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003055
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003056int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003057 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003058 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003059{
Robert Richter6606f872012-08-16 21:10:19 +02003060 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003061 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003062
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003063 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003064 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3065 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003066 if (err) {
3067 pr_debug("failed to create perf header attribute\n");
3068 return err;
3069 }
3070 }
3071
3072 return err;
3073}
3074
Adrian Hunter47c3d102013-07-04 16:20:21 +03003075int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3076 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003077 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003078{
Robert Richterf4d83432012-08-16 21:10:17 +02003079 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003080 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003081 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003082
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003083 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003084 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003085 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003086 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003087 }
3088
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003089 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003090 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003091 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003092
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003093 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003094
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003095 ids = event->header.size;
3096 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003097 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003098 /*
3099 * We don't have the cpu and thread maps on the header, so
3100 * for allocating the perf_sample_id table we fake 1 cpu and
3101 * hattr->ids threads.
3102 */
3103 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3104 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003105
3106 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003107 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003108 }
3109
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03003110 symbol_conf.nr_events = evlist->nr_entries;
3111
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003112 return 0;
3113}
Tom Zanussicd19a032010-04-01 23:59:20 -05003114
Jiri Olsaffe777252015-10-25 15:51:36 +01003115int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3116 union perf_event *event,
3117 struct perf_evlist **pevlist)
3118{
3119 struct event_update_event *ev = &event->event_update;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003120 struct event_update_event_scale *ev_scale;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003121 struct event_update_event_cpus *ev_cpus;
Jiri Olsaffe777252015-10-25 15:51:36 +01003122 struct perf_evlist *evlist;
3123 struct perf_evsel *evsel;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003124 struct cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003125
3126 if (!pevlist || *pevlist == NULL)
3127 return -EINVAL;
3128
3129 evlist = *pevlist;
3130
3131 evsel = perf_evlist__id2evsel(evlist, ev->id);
3132 if (evsel == NULL)
3133 return -EINVAL;
3134
Jiri Olsaa6e52812015-10-25 15:51:37 +01003135 switch (ev->type) {
3136 case PERF_EVENT_UPDATE__UNIT:
3137 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003138 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003139 case PERF_EVENT_UPDATE__NAME:
3140 evsel->name = strdup(ev->data);
3141 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003142 case PERF_EVENT_UPDATE__SCALE:
3143 ev_scale = (struct event_update_event_scale *) ev->data;
3144 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003145 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003146 case PERF_EVENT_UPDATE__CPUS:
3147 ev_cpus = (struct event_update_event_cpus *) ev->data;
3148
3149 map = cpu_map__new_data(&ev_cpus->cpus);
3150 if (map)
3151 evsel->own_cpus = map;
3152 else
3153 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003154 default:
3155 break;
3156 }
3157
Jiri Olsaffe777252015-10-25 15:51:36 +01003158 return 0;
3159}
3160
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003161int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003162 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003163 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003164{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003165 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003166 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003167 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003168 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003169
Jiri Olsa29208e52011-10-20 15:59:43 +02003170 /*
3171 * We are going to store the size of the data followed
3172 * by the data contents. Since the fd descriptor is a pipe,
3173 * we cannot seek back to store the size of the data once
3174 * we know it. Instead we:
3175 *
3176 * - write the tracing data to the temp file
3177 * - get/write the data size to pipe
3178 * - write the tracing data from the temp file
3179 * to the pipe
3180 */
3181 tdata = tracing_data_get(&evlist->entries, fd, true);
3182 if (!tdata)
3183 return -1;
3184
Tom Zanussi92155452010-04-01 23:59:21 -05003185 memset(&ev, 0, sizeof(ev));
3186
3187 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003188 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003189 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003190 padding = aligned_size - size;
3191 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3192 ev.tracing_data.size = aligned_size;
3193
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003194 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003195
Jiri Olsa29208e52011-10-20 15:59:43 +02003196 /*
3197 * The put function will copy all the tracing data
3198 * stored in temp file to the pipe.
3199 */
3200 tracing_data_put(tdata);
3201
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -07003202 if (write_padded(fd, NULL, 0, padding))
3203 return -1;
Tom Zanussi92155452010-04-01 23:59:21 -05003204
3205 return aligned_size;
3206}
3207
Adrian Hunter47c3d102013-07-04 16:20:21 +03003208int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3209 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003210 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05003211{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003212 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003213 int fd = perf_data_file__fd(session->file);
3214 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003215 char buf[BUFSIZ];
3216
3217 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003218 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003219 SEEK_SET);
3220
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003221 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003222 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003223 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003224
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003225 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003226 pr_err("%s: reading input file", __func__);
3227 return -1;
3228 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003229 if (session->repipe) {
3230 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003231 if (retw <= 0 || retw != padding) {
3232 pr_err("%s: repiping tracing data padding", __func__);
3233 return -1;
3234 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003235 }
Tom Zanussi92155452010-04-01 23:59:21 -05003236
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003237 if (size_read + padding != size) {
3238 pr_err("%s: tracing data size mismatch", __func__);
3239 return -1;
3240 }
Tom Zanussi92155452010-04-01 23:59:21 -05003241
Namhyung Kim831394b2012-09-06 11:10:46 +09003242 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003243 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003244
Tom Zanussi92155452010-04-01 23:59:21 -05003245 return size_read + padding;
3246}
Tom Zanussic7929e42010-04-01 23:59:22 -05003247
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003248int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003249 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003250 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003251 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003252{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003253 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003254 size_t len;
3255 int err = 0;
3256
3257 if (!pos->hit)
3258 return err;
3259
3260 memset(&ev, 0, sizeof(ev));
3261
3262 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003263 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003264 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3265 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3266 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003267 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003268 ev.build_id.header.size = sizeof(ev.build_id) + len;
3269 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3270
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003271 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003272
3273 return err;
3274}
3275
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003276int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003277 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003278 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003279{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003280 __event_process_build_id(&event->build_id,
3281 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003282 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003283 return 0;
3284}