blob: 22b52e5636a6b5be40b71b5159c8ec0b4e2c0b00 [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03002#include <inttypes.h>
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02003#include "util.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -03004#include "string2.h"
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -03005#include <sys/param.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02006#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02007#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02008#include <unistd.h>
9#include <stdio.h>
10#include <stdlib.h>
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -030011#include <linux/compiler.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010012#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -020013#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +010014#include <linux/bitops.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030015#include <sys/stat.h>
16#include <sys/types.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020017#include <sys/utsname.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030018#include <unistd.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020019
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020020#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030021#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020022#include "header.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030023#include "memswap.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020024#include "../perf.h"
25#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020026#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010027#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010028#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020029#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020030#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020031#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090032#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020033#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020034#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010035#include <api/fs/fs.h>
36#include "asm/bug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020037
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030038#include "sane_ctype.h"
39
Stephane Eranian73323f52012-02-02 13:54:44 +010040/*
41 * magic2 = "PERFILE2"
42 * must be a numerical value to let the endianness
43 * determine the memory layout. That way we are able
44 * to detect endianness when reading the perf.data file
45 * back.
46 *
47 * we check for legacy (PERFFILE) format.
48 */
49static const char *__perf_magic1 = "PERFFILE";
50static const u64 __perf_magic2 = 0x32454c4946524550ULL;
51static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020052
Stephane Eranian73323f52012-02-02 13:54:44 +010053#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020054
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090055const char perf_version_string[] = PERF_VERSION;
56
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020057struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020058 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020059 struct perf_file_section ids;
60};
61
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070062struct feat_fd {
63 struct perf_header *ph;
64 int fd;
65};
66
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030067void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020068{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030069 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020070}
71
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030072void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020073{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030074 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020075}
76
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030077bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020078{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030079 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020080}
81
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -070082/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070083int do_write(struct feat_fd *ff, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020084{
David Carrillo-Cisneros3b8f51a2017-07-17 21:25:38 -070085 ssize_t ret;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020086
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070087 ret = writen(ff->fd, buf, size);
David Carrillo-Cisneros3b8f51a2017-07-17 21:25:38 -070088 if (ret != (ssize_t)size)
89 return ret < 0 ? (int)ret : -1;
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020090
91 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020092}
93
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -070094/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070095int write_padded(struct feat_fd *ff, const void *bf,
96 size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020097{
98 static const char zero_buf[NAME_ALIGN];
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070099 int err = do_write(ff, bf, count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200100
101 if (!err)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700102 err = do_write(ff, zero_buf, count_aligned - count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200103
104 return err;
105}
106
Kan Liang2bb00d22015-09-01 09:58:12 -0400107#define string_size(str) \
108 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
109
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700110/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700111static int do_write_string(struct feat_fd *ff, const char *str)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200112{
113 u32 len, olen;
114 int ret;
115
116 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300117 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200118
119 /* write len, incl. \0 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700120 ret = do_write(ff, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200121 if (ret < 0)
122 return ret;
123
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700124 return write_padded(ff, str, olen, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200125}
126
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700127static int __do_read(int fd, void *addr, ssize_t size)
128{
129 ssize_t ret = readn(fd, addr, size);
130
131 if (ret != size)
132 return ret < 0 ? (int)ret : -1;
133 return 0;
134}
135
136static int do_read_u32(int fd, struct perf_header *ph, u32 *addr)
137{
138 int ret;
139
140 ret = __do_read(fd, addr, sizeof(*addr));
141 if (ret)
142 return ret;
143
144 if (ph->needs_swap)
145 *addr = bswap_32(*addr);
146 return 0;
147}
148
149static int do_read_u64(int fd, struct perf_header *ph, u64 *addr)
150{
151 int ret;
152
153 ret = __do_read(fd, addr, sizeof(*addr));
154 if (ret)
155 return ret;
156
157 if (ph->needs_swap)
158 *addr = bswap_64(*addr);
159 return 0;
160}
161
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200162static char *do_read_string(int fd, struct perf_header *ph)
163{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200164 u32 len;
165 char *buf;
166
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700167 if (do_read_u32(fd, ph, &len))
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200168 return NULL;
169
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200170 buf = malloc(len);
171 if (!buf)
172 return NULL;
173
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700174 if (!__do_read(fd, buf, len)) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200175 /*
176 * strings are padded by zeroes
177 * thus the actual strlen of buf
178 * may be less than len
179 */
180 return buf;
181 }
182
183 free(buf);
184 return NULL;
185}
186
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700187static int write_tracing_data(struct feat_fd *ff,
188 struct perf_evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200189{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700190 return read_tracing_data(ff->fd, &evlist->entries);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200191}
192
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700193static int write_build_id(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300194 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200195{
196 struct perf_session *session;
197 int err;
198
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700199 session = container_of(ff->ph, struct perf_session, header);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200200
Robert Richtere20960c2011-12-07 10:02:55 +0100201 if (!perf_session__read_build_ids(session, true))
202 return -1;
203
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700204 err = perf_session__write_buildid_table(session, ff);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200205 if (err < 0) {
206 pr_debug("failed to write buildid table\n");
207 return err;
208 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900209 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200210
211 return 0;
212}
213
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700214static int write_hostname(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300215 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200216{
217 struct utsname uts;
218 int ret;
219
220 ret = uname(&uts);
221 if (ret < 0)
222 return -1;
223
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700224 return do_write_string(ff, uts.nodename);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200225}
226
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700227static int write_osrelease(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300228 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200229{
230 struct utsname uts;
231 int ret;
232
233 ret = uname(&uts);
234 if (ret < 0)
235 return -1;
236
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700237 return do_write_string(ff, uts.release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200238}
239
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700240static int write_arch(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300241 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200242{
243 struct utsname uts;
244 int ret;
245
246 ret = uname(&uts);
247 if (ret < 0)
248 return -1;
249
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700250 return do_write_string(ff, uts.machine);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200251}
252
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700253static int write_version(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300254 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200255{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700256 return do_write_string(ff, perf_version_string);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200257}
258
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700259static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200260{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200261 FILE *file;
262 char *buf = NULL;
263 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800264 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200265 size_t len = 0;
266 int ret = -1;
267
268 if (!search)
269 return -1;
270
271 file = fopen("/proc/cpuinfo", "r");
272 if (!file)
273 return -1;
274
275 while (getline(&buf, &len, file) > 0) {
276 ret = strncmp(buf, search, strlen(search));
277 if (!ret)
278 break;
279 }
280
Wang Naned307752014-10-16 11:08:29 +0800281 if (ret) {
282 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200283 goto done;
Wang Naned307752014-10-16 11:08:29 +0800284 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200285
286 s = buf;
287
288 p = strchr(buf, ':');
289 if (p && *(p+1) == ' ' && *(p+2))
290 s = p + 2;
291 p = strchr(s, '\n');
292 if (p)
293 *p = '\0';
294
295 /* squash extra space characters (branding string) */
296 p = s;
297 while (*p) {
298 if (isspace(*p)) {
299 char *r = p + 1;
300 char *q = r;
301 *p = ' ';
302 while (*q && isspace(*q))
303 q++;
304 if (q != (p+1))
305 while ((*r++ = *q++));
306 }
307 p++;
308 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700309 ret = do_write_string(ff, s);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200310done:
311 free(buf);
312 fclose(file);
313 return ret;
314}
315
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700316static int write_cpudesc(struct feat_fd *ff,
Wang Nan493c3032014-10-24 09:45:26 +0800317 struct perf_evlist *evlist __maybe_unused)
318{
319#ifndef CPUINFO_PROC
320#define CPUINFO_PROC {"model name", }
321#endif
322 const char *cpuinfo_procs[] = CPUINFO_PROC;
323 unsigned int i;
324
325 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
326 int ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700327 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
Wang Nan493c3032014-10-24 09:45:26 +0800328 if (ret >= 0)
329 return ret;
330 }
331 return -1;
332}
333
334
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700335static int write_nrcpus(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300336 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200337{
338 long nr;
339 u32 nrc, nra;
340 int ret;
341
Jan Stancekda8a58b2017-02-17 12:10:26 +0100342 nrc = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200343
344 nr = sysconf(_SC_NPROCESSORS_ONLN);
345 if (nr < 0)
346 return -1;
347
348 nra = (u32)(nr & UINT_MAX);
349
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700350 ret = do_write(ff, &nrc, sizeof(nrc));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200351 if (ret < 0)
352 return ret;
353
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700354 return do_write(ff, &nra, sizeof(nra));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200355}
356
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700357static int write_event_desc(struct feat_fd *ff,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200358 struct perf_evlist *evlist)
359{
Robert Richter6606f872012-08-16 21:10:19 +0200360 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900361 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200362 int ret;
363
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900364 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200365
366 /*
367 * write number of events
368 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700369 ret = do_write(ff, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200370 if (ret < 0)
371 return ret;
372
373 /*
374 * size of perf_event_attr struct
375 */
Robert Richter6606f872012-08-16 21:10:19 +0200376 sz = (u32)sizeof(evsel->attr);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700377 ret = do_write(ff, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200378 if (ret < 0)
379 return ret;
380
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300381 evlist__for_each_entry(evlist, evsel) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700382 ret = do_write(ff, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200383 if (ret < 0)
384 return ret;
385 /*
386 * write number of unique id per event
387 * there is one id per instance of an event
388 *
389 * copy into an nri to be independent of the
390 * type of ids,
391 */
Robert Richter6606f872012-08-16 21:10:19 +0200392 nri = evsel->ids;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700393 ret = do_write(ff, &nri, sizeof(nri));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200394 if (ret < 0)
395 return ret;
396
397 /*
398 * write event string as passed on cmdline
399 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700400 ret = do_write_string(ff, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200401 if (ret < 0)
402 return ret;
403 /*
404 * write unique ids for this event
405 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700406 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200407 if (ret < 0)
408 return ret;
409 }
410 return 0;
411}
412
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700413static int write_cmdline(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300414 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200415{
416 char buf[MAXPATHLEN];
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300417 u32 n;
418 int i, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200419
Tommi Rantala55f771282017-03-22 15:06:24 +0200420 /* actual path to perf binary */
421 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200422 if (ret <= 0)
423 return -1;
424
425 /* readlink() does not add null termination */
426 buf[ret] = '\0';
427
428 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300429 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200430
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700431 ret = do_write(ff, &n, sizeof(n));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200432 if (ret < 0)
433 return ret;
434
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700435 ret = do_write_string(ff, buf);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200436 if (ret < 0)
437 return ret;
438
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300439 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700440 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200441 if (ret < 0)
442 return ret;
443 }
444 return 0;
445}
446
447#define CORE_SIB_FMT \
448 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
449#define THRD_SIB_FMT \
450 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
451
452struct cpu_topo {
Kan Liang2bb00d22015-09-01 09:58:12 -0400453 u32 cpu_nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200454 u32 core_sib;
455 u32 thread_sib;
456 char **core_siblings;
457 char **thread_siblings;
458};
459
460static int build_cpu_topo(struct cpu_topo *tp, int cpu)
461{
462 FILE *fp;
463 char filename[MAXPATHLEN];
464 char *buf = NULL, *p;
465 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200466 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200467 u32 i = 0;
468 int ret = -1;
469
470 sprintf(filename, CORE_SIB_FMT, cpu);
471 fp = fopen(filename, "r");
472 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200473 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200474
Stephane Eranianc5885742013-08-14 12:04:26 +0200475 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200476 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200477 if (sret <= 0)
478 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200479
480 p = strchr(buf, '\n');
481 if (p)
482 *p = '\0';
483
484 for (i = 0; i < tp->core_sib; i++) {
485 if (!strcmp(buf, tp->core_siblings[i]))
486 break;
487 }
488 if (i == tp->core_sib) {
489 tp->core_siblings[i] = buf;
490 tp->core_sib++;
491 buf = NULL;
492 len = 0;
493 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200494 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200495
Stephane Eranianc5885742013-08-14 12:04:26 +0200496try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200497 sprintf(filename, THRD_SIB_FMT, cpu);
498 fp = fopen(filename, "r");
499 if (!fp)
500 goto done;
501
502 if (getline(&buf, &len, fp) <= 0)
503 goto done;
504
505 p = strchr(buf, '\n');
506 if (p)
507 *p = '\0';
508
509 for (i = 0; i < tp->thread_sib; i++) {
510 if (!strcmp(buf, tp->thread_siblings[i]))
511 break;
512 }
513 if (i == tp->thread_sib) {
514 tp->thread_siblings[i] = buf;
515 tp->thread_sib++;
516 buf = NULL;
517 }
518 ret = 0;
519done:
520 if(fp)
521 fclose(fp);
522 free(buf);
523 return ret;
524}
525
526static void free_cpu_topo(struct cpu_topo *tp)
527{
528 u32 i;
529
530 if (!tp)
531 return;
532
533 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300534 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200535
536 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300537 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200538
539 free(tp);
540}
541
542static struct cpu_topo *build_cpu_topology(void)
543{
Jan Stancek43db2842017-02-17 12:10:25 +0100544 struct cpu_topo *tp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200545 void *addr;
546 u32 nr, i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300547 size_t sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200548 long ncpus;
549 int ret = -1;
Jan Stancek43db2842017-02-17 12:10:25 +0100550 struct cpu_map *map;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200551
Jan Stancekda8a58b2017-02-17 12:10:26 +0100552 ncpus = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200553
Jan Stancek43db2842017-02-17 12:10:25 +0100554 /* build online CPU map */
555 map = cpu_map__new(NULL);
556 if (map == NULL) {
557 pr_debug("failed to get system cpumap\n");
558 return NULL;
559 }
560
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200561 nr = (u32)(ncpus & UINT_MAX);
562
563 sz = nr * sizeof(char *);
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300564 addr = calloc(1, sizeof(*tp) + 2 * sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200565 if (!addr)
Jan Stancek43db2842017-02-17 12:10:25 +0100566 goto out_free;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200567
568 tp = addr;
Kan Liang2bb00d22015-09-01 09:58:12 -0400569 tp->cpu_nr = nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200570 addr += sizeof(*tp);
571 tp->core_siblings = addr;
572 addr += sz;
573 tp->thread_siblings = addr;
574
575 for (i = 0; i < nr; i++) {
Jan Stancek43db2842017-02-17 12:10:25 +0100576 if (!cpu_map__has(map, i))
577 continue;
578
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200579 ret = build_cpu_topo(tp, i);
580 if (ret < 0)
581 break;
582 }
Jan Stancek43db2842017-02-17 12:10:25 +0100583
584out_free:
585 cpu_map__put(map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200586 if (ret) {
587 free_cpu_topo(tp);
588 tp = NULL;
589 }
590 return tp;
591}
592
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700593static int write_cpu_topology(struct feat_fd *ff,
594 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200595{
596 struct cpu_topo *tp;
597 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300598 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200599
600 tp = build_cpu_topology();
601 if (!tp)
602 return -1;
603
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700604 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200605 if (ret < 0)
606 goto done;
607
608 for (i = 0; i < tp->core_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700609 ret = do_write_string(ff, tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200610 if (ret < 0)
611 goto done;
612 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700613 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200614 if (ret < 0)
615 goto done;
616
617 for (i = 0; i < tp->thread_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700618 ret = do_write_string(ff, tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200619 if (ret < 0)
620 break;
621 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400622
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300623 ret = perf_env__read_cpu_topology_map(&perf_env);
624 if (ret < 0)
625 goto done;
626
627 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700628 ret = do_write(ff, &perf_env.cpu[j].core_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300629 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400630 if (ret < 0)
631 return ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700632 ret = do_write(ff, &perf_env.cpu[j].socket_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300633 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400634 if (ret < 0)
635 return ret;
636 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200637done:
638 free_cpu_topo(tp);
639 return ret;
640}
641
642
643
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700644static int write_total_mem(struct feat_fd *ff,
645 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200646{
647 char *buf = NULL;
648 FILE *fp;
649 size_t len = 0;
650 int ret = -1, n;
651 uint64_t mem;
652
653 fp = fopen("/proc/meminfo", "r");
654 if (!fp)
655 return -1;
656
657 while (getline(&buf, &len, fp) > 0) {
658 ret = strncmp(buf, "MemTotal:", 9);
659 if (!ret)
660 break;
661 }
662 if (!ret) {
663 n = sscanf(buf, "%*s %"PRIu64, &mem);
664 if (n == 1)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700665 ret = do_write(ff, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800666 } else
667 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200668 free(buf);
669 fclose(fp);
670 return ret;
671}
672
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700673static int write_topo_node(struct feat_fd *ff, int node)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200674{
675 char str[MAXPATHLEN];
676 char field[32];
677 char *buf = NULL, *p;
678 size_t len = 0;
679 FILE *fp;
680 u64 mem_total, mem_free, mem;
681 int ret = -1;
682
683 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
684 fp = fopen(str, "r");
685 if (!fp)
686 return -1;
687
688 while (getline(&buf, &len, fp) > 0) {
689 /* skip over invalid lines */
690 if (!strchr(buf, ':'))
691 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100692 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200693 goto done;
694 if (!strcmp(field, "MemTotal:"))
695 mem_total = mem;
696 if (!strcmp(field, "MemFree:"))
697 mem_free = mem;
698 }
699
700 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100701 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200702
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700703 ret = do_write(ff, &mem_total, sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200704 if (ret)
705 goto done;
706
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700707 ret = do_write(ff, &mem_free, sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200708 if (ret)
709 goto done;
710
711 ret = -1;
712 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
713
714 fp = fopen(str, "r");
715 if (!fp)
716 goto done;
717
718 if (getline(&buf, &len, fp) <= 0)
719 goto done;
720
721 p = strchr(buf, '\n');
722 if (p)
723 *p = '\0';
724
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700725 ret = do_write_string(ff, buf);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200726done:
727 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100728 if (fp)
729 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200730 return ret;
731}
732
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700733static int write_numa_topology(struct feat_fd *ff,
734 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200735{
736 char *buf = NULL;
737 size_t len = 0;
738 FILE *fp;
739 struct cpu_map *node_map = NULL;
740 char *c;
741 u32 nr, i, j;
742 int ret = -1;
743
744 fp = fopen("/sys/devices/system/node/online", "r");
745 if (!fp)
746 return -1;
747
748 if (getline(&buf, &len, fp) <= 0)
749 goto done;
750
751 c = strchr(buf, '\n');
752 if (c)
753 *c = '\0';
754
755 node_map = cpu_map__new(buf);
756 if (!node_map)
757 goto done;
758
759 nr = (u32)node_map->nr;
760
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700761 ret = do_write(ff, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200762 if (ret < 0)
763 goto done;
764
765 for (i = 0; i < nr; i++) {
766 j = (u32)node_map->map[i];
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700767 ret = do_write(ff, &j, sizeof(j));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200768 if (ret < 0)
769 break;
770
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700771 ret = write_topo_node(ff, i);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200772 if (ret < 0)
773 break;
774 }
775done:
776 free(buf);
777 fclose(fp);
Masami Hiramatsu5191d8872015-12-09 11:11:35 +0900778 cpu_map__put(node_map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200779 return ret;
780}
781
782/*
Robert Richter50a96672012-08-16 21:10:24 +0200783 * File format:
784 *
785 * struct pmu_mappings {
786 * u32 pmu_num;
787 * struct pmu_map {
788 * u32 type;
789 * char name[];
790 * }[pmu_num];
791 * };
792 */
793
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700794static int write_pmu_mappings(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300795 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200796{
797 struct perf_pmu *pmu = NULL;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700798 off_t offset = lseek(ff->fd, 0, SEEK_CUR);
Robert Richter50a96672012-08-16 21:10:24 +0200799 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900800 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200801
802 /* write real pmu_num later */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700803 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
Namhyung Kim5323f602012-12-17 15:38:54 +0900804 if (ret < 0)
805 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200806
807 while ((pmu = perf_pmu__scan(pmu))) {
808 if (!pmu->name)
809 continue;
810 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +0900811
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700812 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
Namhyung Kim5323f602012-12-17 15:38:54 +0900813 if (ret < 0)
814 return ret;
815
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700816 ret = do_write_string(ff, pmu->name);
Namhyung Kim5323f602012-12-17 15:38:54 +0900817 if (ret < 0)
818 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200819 }
820
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700821 if (pwrite(ff->fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
Robert Richter50a96672012-08-16 21:10:24 +0200822 /* discard all */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700823 lseek(ff->fd, offset, SEEK_SET);
Robert Richter50a96672012-08-16 21:10:24 +0200824 return -1;
825 }
826
827 return 0;
828}
829
830/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900831 * File format:
832 *
833 * struct group_descs {
834 * u32 nr_groups;
835 * struct group_desc {
836 * char name[];
837 * u32 leader_idx;
838 * u32 nr_members;
839 * }[nr_groups];
840 * };
841 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700842static int write_group_desc(struct feat_fd *ff,
Namhyung Kima8bb5592013-01-22 18:09:31 +0900843 struct perf_evlist *evlist)
844{
845 u32 nr_groups = evlist->nr_groups;
846 struct perf_evsel *evsel;
847 int ret;
848
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700849 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900850 if (ret < 0)
851 return ret;
852
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300853 evlist__for_each_entry(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900854 if (perf_evsel__is_group_leader(evsel) &&
855 evsel->nr_members > 1) {
856 const char *name = evsel->group_name ?: "{anon_group}";
857 u32 leader_idx = evsel->idx;
858 u32 nr_members = evsel->nr_members;
859
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700860 ret = do_write_string(ff, name);
Namhyung Kima8bb5592013-01-22 18:09:31 +0900861 if (ret < 0)
862 return ret;
863
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700864 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900865 if (ret < 0)
866 return ret;
867
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700868 ret = do_write(ff, &nr_members, sizeof(nr_members));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900869 if (ret < 0)
870 return ret;
871 }
872 }
873 return 0;
874}
875
876/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200877 * default get_cpuid(): nothing gets recorded
Jiada Wang7a759cd2017-04-09 20:02:37 -0700878 * actual implementation must be in arch/$(SRCARCH)/util/header.c
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200879 */
Rui Teng11d8f872016-07-28 10:05:57 +0800880int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200881{
882 return -1;
883}
884
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700885static int write_cpuid(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300886 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200887{
888 char buffer[64];
889 int ret;
890
891 ret = get_cpuid(buffer, sizeof(buffer));
892 if (!ret)
893 goto write_it;
894
895 return -1;
896write_it:
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700897 return do_write_string(ff, buffer);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200898}
899
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700900static int write_branch_stack(struct feat_fd *ff __maybe_unused,
901 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100902{
903 return 0;
904}
905
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700906static int write_auxtrace(struct feat_fd *ff,
Adrian Hunter4025ea42015-04-09 18:53:41 +0300907 struct perf_evlist *evlist __maybe_unused)
908{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300909 struct perf_session *session;
910 int err;
911
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700912 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300913
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700914 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300915 if (err < 0)
916 pr_err("Failed to write auxtrace index\n");
917 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300918}
919
Jiri Olsa720e98b2016-02-16 16:01:43 +0100920static int cpu_cache_level__sort(const void *a, const void *b)
921{
922 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
923 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
924
925 return cache_a->level - cache_b->level;
926}
927
928static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
929{
930 if (a->level != b->level)
931 return false;
932
933 if (a->line_size != b->line_size)
934 return false;
935
936 if (a->sets != b->sets)
937 return false;
938
939 if (a->ways != b->ways)
940 return false;
941
942 if (strcmp(a->type, b->type))
943 return false;
944
945 if (strcmp(a->size, b->size))
946 return false;
947
948 if (strcmp(a->map, b->map))
949 return false;
950
951 return true;
952}
953
954static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
955{
956 char path[PATH_MAX], file[PATH_MAX];
957 struct stat st;
958 size_t len;
959
960 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
961 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
962
963 if (stat(file, &st))
964 return 1;
965
966 scnprintf(file, PATH_MAX, "%s/level", path);
967 if (sysfs__read_int(file, (int *) &cache->level))
968 return -1;
969
970 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
971 if (sysfs__read_int(file, (int *) &cache->line_size))
972 return -1;
973
974 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
975 if (sysfs__read_int(file, (int *) &cache->sets))
976 return -1;
977
978 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
979 if (sysfs__read_int(file, (int *) &cache->ways))
980 return -1;
981
982 scnprintf(file, PATH_MAX, "%s/type", path);
983 if (sysfs__read_str(file, &cache->type, &len))
984 return -1;
985
986 cache->type[len] = 0;
987 cache->type = rtrim(cache->type);
988
989 scnprintf(file, PATH_MAX, "%s/size", path);
990 if (sysfs__read_str(file, &cache->size, &len)) {
991 free(cache->type);
992 return -1;
993 }
994
995 cache->size[len] = 0;
996 cache->size = rtrim(cache->size);
997
998 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
999 if (sysfs__read_str(file, &cache->map, &len)) {
1000 free(cache->map);
1001 free(cache->type);
1002 return -1;
1003 }
1004
1005 cache->map[len] = 0;
1006 cache->map = rtrim(cache->map);
1007 return 0;
1008}
1009
1010static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1011{
1012 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1013}
1014
1015static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1016{
1017 u32 i, cnt = 0;
1018 long ncpus;
1019 u32 nr, cpu;
1020 u16 level;
1021
1022 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1023 if (ncpus < 0)
1024 return -1;
1025
1026 nr = (u32)(ncpus & UINT_MAX);
1027
1028 for (cpu = 0; cpu < nr; cpu++) {
1029 for (level = 0; level < 10; level++) {
1030 struct cpu_cache_level c;
1031 int err;
1032
1033 err = cpu_cache_level__read(&c, cpu, level);
1034 if (err < 0)
1035 return err;
1036
1037 if (err == 1)
1038 break;
1039
1040 for (i = 0; i < cnt; i++) {
1041 if (cpu_cache_level__cmp(&c, &caches[i]))
1042 break;
1043 }
1044
1045 if (i == cnt)
1046 caches[cnt++] = c;
1047 else
1048 cpu_cache_level__free(&c);
1049
1050 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1051 goto out;
1052 }
1053 }
1054 out:
1055 *cntp = cnt;
1056 return 0;
1057}
1058
1059#define MAX_CACHES 2000
1060
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001061static int write_cache(struct feat_fd *ff,
1062 struct perf_evlist *evlist __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001063{
1064 struct cpu_cache_level caches[MAX_CACHES];
1065 u32 cnt = 0, i, version = 1;
1066 int ret;
1067
1068 ret = build_caches(caches, MAX_CACHES, &cnt);
1069 if (ret)
1070 goto out;
1071
1072 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1073
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001074 ret = do_write(ff, &version, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001075 if (ret < 0)
1076 goto out;
1077
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001078 ret = do_write(ff, &cnt, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001079 if (ret < 0)
1080 goto out;
1081
1082 for (i = 0; i < cnt; i++) {
1083 struct cpu_cache_level *c = &caches[i];
1084
1085 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001086 ret = do_write(ff, &c->v, sizeof(u32)); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001087 if (ret < 0) \
1088 goto out;
1089
1090 _W(level)
1091 _W(line_size)
1092 _W(sets)
1093 _W(ways)
1094 #undef _W
1095
1096 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001097 ret = do_write_string(ff, (const char *) c->v); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001098 if (ret < 0) \
1099 goto out;
1100
1101 _W(type)
1102 _W(size)
1103 _W(map)
1104 #undef _W
1105 }
1106
1107out:
1108 for (i = 0; i < cnt; i++)
1109 cpu_cache_level__free(&caches[i]);
1110 return ret;
1111}
1112
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001113static int write_stat(struct feat_fd *ff __maybe_unused,
Jiri Olsaffa517a2015-10-25 15:51:43 +01001114 struct perf_evlist *evlist __maybe_unused)
1115{
1116 return 0;
1117}
1118
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001119static void print_hostname(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001120{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001121 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001122}
1123
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001124static void print_osrelease(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001125{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001126 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001127}
1128
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001129static void print_arch(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001130{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001131 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001132}
1133
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001134static void print_cpudesc(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001135{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001136 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001137}
1138
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001139static void print_nrcpus(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001140{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001141 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1142 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001143}
1144
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001145static void print_version(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001146{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001147 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001148}
1149
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001150static void print_cmdline(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001151{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001152 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001153
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001154 nr = ff->ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001155
1156 fprintf(fp, "# cmdline : ");
1157
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001158 for (i = 0; i < nr; i++)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001159 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001160 fputc('\n', fp);
1161}
1162
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001163static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001164{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001165 struct perf_header *ph = ff->ph;
1166 int cpu_nr = ph->env.nr_cpus_avail;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001167 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001168 char *str;
1169
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001170 nr = ph->env.nr_sibling_cores;
1171 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001172
1173 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001174 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001175 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001176 }
1177
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001178 nr = ph->env.nr_sibling_threads;
1179 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001180
1181 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001182 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001183 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001184 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001185
1186 if (ph->env.cpu != NULL) {
1187 for (i = 0; i < cpu_nr; i++)
1188 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1189 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1190 } else
1191 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001192}
1193
Robert Richter4e1b9c62012-08-16 21:10:22 +02001194static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001195{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001196 struct perf_evsel *evsel;
1197
1198 if (!events)
1199 return;
1200
1201 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001202 zfree(&evsel->name);
1203 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001204 }
1205
1206 free(events);
1207}
1208
1209static struct perf_evsel *
1210read_event_desc(struct perf_header *ph, int fd)
1211{
1212 struct perf_evsel *evsel, *events = NULL;
1213 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001214 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001215 u32 nre, sz, nr, i, j;
Stephane Eranian62db9062012-02-09 23:21:07 +01001216 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001217
1218 /* number of events */
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001219 if (do_read_u32(fd, ph, &nre))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001220 goto error;
1221
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001222 if (do_read_u32(fd, ph, &sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001223 goto error;
1224
Stephane Eranian62db9062012-02-09 23:21:07 +01001225 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001226 buf = malloc(sz);
1227 if (!buf)
1228 goto error;
1229
Robert Richter4e1b9c62012-08-16 21:10:22 +02001230 /* the last event terminates with evsel->attr.size == 0: */
1231 events = calloc(nre + 1, sizeof(*events));
1232 if (!events)
1233 goto error;
1234
1235 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001236 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001237 msz = sz;
1238
Robert Richter4e1b9c62012-08-16 21:10:22 +02001239 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1240 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001241
Stephane Eranian62db9062012-02-09 23:21:07 +01001242 /*
1243 * must read entire on-file attr struct to
1244 * sync up with layout.
1245 */
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001246 if (__do_read(fd, buf, sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001247 goto error;
1248
1249 if (ph->needs_swap)
1250 perf_event__attr_swap(buf);
1251
Robert Richter4e1b9c62012-08-16 21:10:22 +02001252 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001253
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001254 if (do_read_u32(fd, ph, &nr))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001255 goto error;
1256
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001257 if (ph->needs_swap)
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001258 evsel->needs_swap = true;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001259
Robert Richter4e1b9c62012-08-16 21:10:22 +02001260 evsel->name = do_read_string(fd, ph);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001261 if (!evsel->name)
1262 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001263
1264 if (!nr)
1265 continue;
1266
1267 id = calloc(nr, sizeof(*id));
1268 if (!id)
1269 goto error;
1270 evsel->ids = nr;
1271 evsel->id = id;
1272
1273 for (j = 0 ; j < nr; j++) {
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001274 if (do_read_u64(fd, ph, id))
Robert Richter4e1b9c62012-08-16 21:10:22 +02001275 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001276 id++;
1277 }
1278 }
1279out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001280 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001281 return events;
1282error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001283 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001284 events = NULL;
1285 goto out;
1286}
1287
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001288static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001289 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001290{
1291 return fprintf(fp, ", %s = %s", name, val);
1292}
1293
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001294static void print_event_desc(struct feat_fd *ff, FILE *fp)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001295{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001296 struct perf_evsel *evsel, *events = read_event_desc(ff->ph, ff->fd);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001297 u32 j;
1298 u64 *id;
1299
1300 if (!events) {
1301 fprintf(fp, "# event desc: not available or unable to read\n");
1302 return;
1303 }
1304
1305 for (evsel = events; evsel->attr.size; evsel++) {
1306 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001307
Robert Richter4e1b9c62012-08-16 21:10:22 +02001308 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001309 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001310 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1311 if (j)
1312 fputc(',', fp);
1313 fprintf(fp, " %"PRIu64, *id);
1314 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001315 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001316 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001317
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001318 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001319
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001320 fputc('\n', fp);
1321 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001322
1323 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001324}
1325
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001326static void print_total_mem(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001327{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001328 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001329}
1330
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001331static void print_numa_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001332{
Jiri Olsac60da222016-07-04 14:16:20 +02001333 int i;
1334 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001335
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001336 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1337 n = &ff->ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001338
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001339 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1340 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001341 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001342
Jiri Olsac60da222016-07-04 14:16:20 +02001343 fprintf(fp, "# node%u cpu list : ", n->node);
1344 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001345 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001346}
1347
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001348static void print_cpuid(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001349{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001350 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001351}
1352
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001353static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001354{
1355 fprintf(fp, "# contains samples with branch stack\n");
1356}
1357
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001358static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
Adrian Hunter4025ea42015-04-09 18:53:41 +03001359{
1360 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1361}
1362
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001363static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001364{
1365 fprintf(fp, "# contains stat data\n");
1366}
1367
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001368static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001369{
1370 int i;
1371
1372 fprintf(fp, "# CPU cache info:\n");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001373 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
Jiri Olsa720e98b2016-02-16 16:01:43 +01001374 fprintf(fp, "# ");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001375 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001376 }
1377}
1378
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001379static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001380{
1381 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001382 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001383 u32 pmu_num;
1384 u32 type;
1385
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001386 pmu_num = ff->ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001387 if (!pmu_num) {
1388 fprintf(fp, "# pmu mappings: not available\n");
1389 return;
1390 }
1391
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001392 str = ff->ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001393
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001394 while (pmu_num) {
1395 type = strtoul(str, &tmp, 0);
1396 if (*tmp != ':')
1397 goto error;
1398
1399 str = tmp + 1;
1400 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1401
Robert Richter50a96672012-08-16 21:10:24 +02001402 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001403 str += strlen(str) + 1;
1404 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001405 }
1406
1407 fprintf(fp, "\n");
1408
1409 if (!pmu_num)
1410 return;
1411error:
1412 fprintf(fp, "# pmu mappings: unable to read\n");
1413}
1414
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001415static void print_group_desc(struct feat_fd *ff, FILE *fp)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001416{
1417 struct perf_session *session;
1418 struct perf_evsel *evsel;
1419 u32 nr = 0;
1420
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001421 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001422
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001423 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001424 if (perf_evsel__is_group_leader(evsel) &&
1425 evsel->nr_members > 1) {
1426 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1427 perf_evsel__name(evsel));
1428
1429 nr = evsel->nr_members - 1;
1430 } else if (nr) {
1431 fprintf(fp, ",%s", perf_evsel__name(evsel));
1432
1433 if (--nr == 0)
1434 fprintf(fp, "}\n");
1435 }
1436 }
1437}
1438
Robert Richter08d95bd2012-02-10 15:41:55 +01001439static int __event_process_build_id(struct build_id_event *bev,
1440 char *filename,
1441 struct perf_session *session)
1442{
1443 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001444 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001445 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001446 struct dso *dso;
1447 enum dso_kernel_type dso_type;
1448
1449 machine = perf_session__findnew_machine(session, bev->pid);
1450 if (!machine)
1451 goto out;
1452
Wang Nan1f121b02015-06-03 08:52:21 +00001453 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001454
Wang Nan1f121b02015-06-03 08:52:21 +00001455 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001456 case PERF_RECORD_MISC_KERNEL:
1457 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001458 break;
1459 case PERF_RECORD_MISC_GUEST_KERNEL:
1460 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001461 break;
1462 case PERF_RECORD_MISC_USER:
1463 case PERF_RECORD_MISC_GUEST_USER:
1464 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001465 break;
1466 default:
1467 goto out;
1468 }
1469
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001470 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001471 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001472 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001473
1474 dso__set_build_id(dso, &bev->build_id);
1475
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001476 if (dso_type != DSO_TYPE_USER) {
1477 struct kmod_path m = { .name = NULL, };
1478
1479 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09001480 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001481 else
1482 dso->kernel = dso_type;
1483
1484 free(m.name);
1485 }
Robert Richter08d95bd2012-02-10 15:41:55 +01001486
1487 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1488 sbuild_id);
1489 pr_debug("build id event received for %s: %s\n",
1490 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001491 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001492 }
1493
1494 err = 0;
1495out:
1496 return err;
1497}
1498
1499static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1500 int input, u64 offset, u64 size)
1501{
1502 struct perf_session *session = container_of(header, struct perf_session, header);
1503 struct {
1504 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001505 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001506 char filename[0];
1507 } old_bev;
1508 struct build_id_event bev;
1509 char filename[PATH_MAX];
1510 u64 limit = offset + size;
1511
1512 while (offset < limit) {
1513 ssize_t len;
1514
Namhyung Kim5323f602012-12-17 15:38:54 +09001515 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001516 return -1;
1517
1518 if (header->needs_swap)
1519 perf_event_header__bswap(&old_bev.header);
1520
1521 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001522 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001523 return -1;
1524
1525 bev.header = old_bev.header;
1526
1527 /*
1528 * As the pid is the missing value, we need to fill
1529 * it properly. The header.misc value give us nice hint.
1530 */
1531 bev.pid = HOST_KERNEL_ID;
1532 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1533 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1534 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1535
1536 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1537 __event_process_build_id(&bev, filename, session);
1538
1539 offset += bev.header.size;
1540 }
1541
1542 return 0;
1543}
1544
1545static int perf_header__read_build_ids(struct perf_header *header,
1546 int input, u64 offset, u64 size)
1547{
1548 struct perf_session *session = container_of(header, struct perf_session, header);
1549 struct build_id_event bev;
1550 char filename[PATH_MAX];
1551 u64 limit = offset + size, orig_offset = offset;
1552 int err = -1;
1553
1554 while (offset < limit) {
1555 ssize_t len;
1556
Namhyung Kim5323f602012-12-17 15:38:54 +09001557 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001558 goto out;
1559
1560 if (header->needs_swap)
1561 perf_event_header__bswap(&bev.header);
1562
1563 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001564 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001565 goto out;
1566 /*
1567 * The a1645ce1 changeset:
1568 *
1569 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1570 *
1571 * Added a field to struct build_id_event that broke the file
1572 * format.
1573 *
1574 * Since the kernel build-id is the first entry, process the
1575 * table using the old format if the well known
1576 * '[kernel.kallsyms]' string for the kernel build-id has the
1577 * first 4 characters chopped off (where the pid_t sits).
1578 */
1579 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1580 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1581 return -1;
1582 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1583 }
1584
1585 __event_process_build_id(&bev, filename, session);
1586
1587 offset += bev.header.size;
1588 }
1589 err = 0;
1590out:
1591 return err;
1592}
1593
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001594/* Macro for features that simply need to read and store a string. */
1595#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
1596static int process_##__feat(struct perf_file_section *section __maybe_unused, \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001597 struct feat_fd *ff, void *data __maybe_unused) \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001598{\
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001599 ff->ph->env.__feat_env = do_read_string(ff->fd, ff->ph); \
1600 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001601}
1602
1603FEAT_PROCESS_STR_FUN(hostname, hostname);
1604FEAT_PROCESS_STR_FUN(osrelease, os_release);
1605FEAT_PROCESS_STR_FUN(version, version);
1606FEAT_PROCESS_STR_FUN(arch, arch);
1607FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1608FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1609
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001610static int process_tracing_data(struct perf_file_section *section __maybe_unused,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001611 struct feat_fd *ff, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001612{
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001613 ssize_t ret = trace_report(ff->fd, data, false);
1614
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001615 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001616}
1617
1618static int process_build_id(struct perf_file_section *section,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001619 struct feat_fd *ff, void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001620{
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001621 if (perf_header__read_build_ids(ff->ph, ff->fd, section->offset, section->size))
Robert Richterf1c67db2012-02-10 15:41:56 +01001622 pr_debug("Failed to read buildids, continuing...\n");
1623 return 0;
1624}
1625
Namhyung Kima1ae5652012-09-24 17:14:59 +09001626static int process_nrcpus(struct perf_file_section *section __maybe_unused,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001627 struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001628{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001629 int ret;
1630 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001631
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001632 ret = do_read_u32(ff->fd, ff->ph, &nr_cpus_avail);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001633 if (ret)
1634 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001635
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001636 ret = do_read_u32(ff->fd, ff->ph, &nr_cpus_online);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001637 if (ret)
1638 return ret;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001639 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
1640 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001641 return 0;
1642}
1643
Namhyung Kima1ae5652012-09-24 17:14:59 +09001644static int process_total_mem(struct perf_file_section *section __maybe_unused,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001645 struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001646{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001647 u64 total_mem;
1648 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001649
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001650 ret = do_read_u64(ff->fd, ff->ph, &total_mem);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001651 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001652 return -1;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001653 ff->ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001654 return 0;
1655}
1656
Robert Richter7c2f7af2012-08-16 21:10:23 +02001657static struct perf_evsel *
1658perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1659{
1660 struct perf_evsel *evsel;
1661
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001662 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001663 if (evsel->idx == idx)
1664 return evsel;
1665 }
1666
1667 return NULL;
1668}
1669
1670static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001671perf_evlist__set_event_name(struct perf_evlist *evlist,
1672 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001673{
1674 struct perf_evsel *evsel;
1675
1676 if (!event->name)
1677 return;
1678
1679 evsel = perf_evlist__find_by_index(evlist, event->idx);
1680 if (!evsel)
1681 return;
1682
1683 if (evsel->name)
1684 return;
1685
1686 evsel->name = strdup(event->name);
1687}
1688
1689static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001690process_event_desc(struct perf_file_section *section __maybe_unused,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001691 struct feat_fd *ff, void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001692{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001693 struct perf_session *session;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001694 struct perf_evsel *evsel, *events = read_event_desc(ff->ph, ff->fd);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001695
1696 if (!events)
1697 return 0;
1698
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001699 session = container_of(ff->ph, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001700 for (evsel = events; evsel->attr.size; evsel++)
1701 perf_evlist__set_event_name(session->evlist, evsel);
1702
1703 free_event_desc(events);
1704
1705 return 0;
1706}
1707
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001708static int process_cmdline(struct perf_file_section *section __maybe_unused,
1709 struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001710{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001711 char *str, *cmdline = NULL, **argv = NULL;
1712 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001713
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001714 if (do_read_u32(ff->fd, ff->ph, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001715 return -1;
1716
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001717 ff->ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001718
1719 cmdline = zalloc(section->size + nr + 1);
1720 if (!cmdline)
1721 return -1;
1722
1723 argv = zalloc(sizeof(char *) * (nr + 1));
1724 if (!argv)
1725 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001726
1727 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001728 str = do_read_string(ff->fd, ff->ph);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001729 if (!str)
1730 goto error;
1731
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001732 argv[i] = cmdline + len;
1733 memcpy(argv[i], str, strlen(str) + 1);
1734 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001735 free(str);
1736 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001737 ff->ph->env.cmdline = cmdline;
1738 ff->ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001739 return 0;
1740
1741error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001742 free(argv);
1743 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001744 return -1;
1745}
1746
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001747static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
1748 struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001749{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001750 u32 nr, i;
1751 char *str;
1752 struct strbuf sb;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001753 int cpu_nr = ff->ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04001754 u64 size = 0;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001755 struct perf_header *ph = ff->ph;
Kan Liang2bb00d22015-09-01 09:58:12 -04001756
1757 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1758 if (!ph->env.cpu)
1759 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001760
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001761 if (do_read_u32(ff->fd, ph, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001762 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001763
Namhyung Kima1ae5652012-09-24 17:14:59 +09001764 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001765 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001766 if (strbuf_init(&sb, 128) < 0)
1767 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001768
1769 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001770 str = do_read_string(ff->fd, ph);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001771 if (!str)
1772 goto error;
1773
1774 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001775 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1776 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001777 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001778 free(str);
1779 }
1780 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1781
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001782 if (do_read_u32(ff->fd, ph, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001783 return -1;
1784
Namhyung Kima1ae5652012-09-24 17:14:59 +09001785 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001786 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001787
1788 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001789 str = do_read_string(ff->fd, ph);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001790 if (!str)
1791 goto error;
1792
1793 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001794 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1795 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001796 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001797 free(str);
1798 }
1799 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04001800
1801 /*
1802 * The header may be from old perf,
1803 * which doesn't include core id and socket id information.
1804 */
1805 if (section->size <= size) {
1806 zfree(&ph->env.cpu);
1807 return 0;
1808 }
1809
1810 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001811 if (do_read_u32(ff->fd, ph, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001812 goto free_cpu;
1813
Kan Liang2bb00d22015-09-01 09:58:12 -04001814 ph->env.cpu[i].core_id = nr;
1815
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001816 if (do_read_u32(ff->fd, ph, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001817 goto free_cpu;
1818
Jan Stancekda8a58b2017-02-17 12:10:26 +01001819 if (nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001820 pr_debug("socket_id number is too big."
1821 "You may need to upgrade the perf tool.\n");
1822 goto free_cpu;
1823 }
1824
1825 ph->env.cpu[i].socket_id = nr;
1826 }
1827
Namhyung Kima1ae5652012-09-24 17:14:59 +09001828 return 0;
1829
1830error:
1831 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04001832free_cpu:
1833 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001834 return -1;
1835}
1836
1837static int process_numa_topology(struct perf_file_section *section __maybe_unused,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001838 struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001839{
Jiri Olsac60da222016-07-04 14:16:20 +02001840 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02001841 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001842 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001843
1844 /* nr nodes */
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001845 if (do_read_u32(ff->fd, ff->ph, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001846 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001847
Jiri Olsac60da222016-07-04 14:16:20 +02001848 nodes = zalloc(sizeof(*nodes) * nr);
1849 if (!nodes)
1850 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001851
1852 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02001853 n = &nodes[i];
1854
Namhyung Kima1ae5652012-09-24 17:14:59 +09001855 /* node number */
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001856 if (do_read_u32(ff->fd, ff->ph, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001857 goto error;
1858
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001859 if (do_read_u64(ff->fd, ff->ph, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001860 goto error;
1861
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001862 if (do_read_u64(ff->fd, ff->ph, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001863 goto error;
1864
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001865 str = do_read_string(ff->fd, ff->ph);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001866 if (!str)
1867 goto error;
1868
Jiri Olsac60da222016-07-04 14:16:20 +02001869 n->map = cpu_map__new(str);
1870 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001871 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02001872
Namhyung Kima1ae5652012-09-24 17:14:59 +09001873 free(str);
1874 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001875 ff->ph->env.nr_numa_nodes = nr;
1876 ff->ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001877 return 0;
1878
1879error:
Jiri Olsac60da222016-07-04 14:16:20 +02001880 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001881 return -1;
1882}
1883
1884static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001885 struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001886{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001887 char *name;
1888 u32 pmu_num;
1889 u32 type;
1890 struct strbuf sb;
1891
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001892 if (do_read_u32(ff->fd, ff->ph, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001893 return -1;
1894
Namhyung Kima1ae5652012-09-24 17:14:59 +09001895 if (!pmu_num) {
1896 pr_debug("pmu mappings not available\n");
1897 return 0;
1898 }
1899
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001900 ff->ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001901 if (strbuf_init(&sb, 128) < 0)
1902 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001903
1904 while (pmu_num) {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001905 if (do_read_u32(ff->fd, ff->ph, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001906 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001907
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001908 name = do_read_string(ff->fd, ff->ph);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001909 if (!name)
1910 goto error;
1911
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001912 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
1913 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001914 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001915 if (strbuf_add(&sb, "", 1) < 0)
1916 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001917
Kan Liange0838e02015-09-10 11:03:05 -03001918 if (!strcmp(name, "msr"))
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001919 ff->ph->env.msr_pmu_type = type;
Kan Liange0838e02015-09-10 11:03:05 -03001920
Namhyung Kima1ae5652012-09-24 17:14:59 +09001921 free(name);
1922 pmu_num--;
1923 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001924 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001925 return 0;
1926
1927error:
1928 strbuf_release(&sb);
1929 return -1;
1930}
1931
Namhyung Kima8bb5592013-01-22 18:09:31 +09001932static int process_group_desc(struct perf_file_section *section __maybe_unused,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001933 struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001934{
1935 size_t ret = -1;
1936 u32 i, nr, nr_groups;
1937 struct perf_session *session;
1938 struct perf_evsel *evsel, *leader = NULL;
1939 struct group_desc {
1940 char *name;
1941 u32 leader_idx;
1942 u32 nr_members;
1943 } *desc;
1944
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001945 if (do_read_u32(ff->fd, ff->ph, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001946 return -1;
1947
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001948 ff->ph->env.nr_groups = nr_groups;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001949 if (!nr_groups) {
1950 pr_debug("group desc not available\n");
1951 return 0;
1952 }
1953
1954 desc = calloc(nr_groups, sizeof(*desc));
1955 if (!desc)
1956 return -1;
1957
1958 for (i = 0; i < nr_groups; i++) {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001959 desc[i].name = do_read_string(ff->fd, ff->ph);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001960 if (!desc[i].name)
1961 goto out_free;
1962
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001963 if (do_read_u32(ff->fd, ff->ph, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001964 goto out_free;
1965
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001966 if (do_read_u32(ff->fd, ff->ph, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001967 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001968 }
1969
1970 /*
1971 * Rebuild group relationship based on the group_desc
1972 */
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001973 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001974 session->evlist->nr_groups = nr_groups;
1975
1976 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001977 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001978 if (evsel->idx == (int) desc[i].leader_idx) {
1979 evsel->leader = evsel;
1980 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09001981 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001982 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09001983 desc[i].name = NULL;
1984 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09001985 evsel->nr_members = desc[i].nr_members;
1986
1987 if (i >= nr_groups || nr > 0) {
1988 pr_debug("invalid group desc\n");
1989 goto out_free;
1990 }
1991
1992 leader = evsel;
1993 nr = evsel->nr_members - 1;
1994 i++;
1995 } else if (nr) {
1996 /* This is a group member */
1997 evsel->leader = leader;
1998
1999 nr--;
2000 }
2001 }
2002
2003 if (i != nr_groups || nr != 0) {
2004 pr_debug("invalid group desc\n");
2005 goto out_free;
2006 }
2007
2008 ret = 0;
2009out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002010 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002011 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002012 free(desc);
2013
2014 return ret;
2015}
2016
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002017static int process_auxtrace(struct perf_file_section *section __maybe_unused,
2018 struct feat_fd *ff, void *data __maybe_unused)
Adrian Hunter99fa2982015-04-30 17:37:25 +03002019{
2020 struct perf_session *session;
2021 int err;
2022
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002023 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002024
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002025 err = auxtrace_index__process(ff->fd, section->size, session,
2026 ff->ph->needs_swap);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002027 if (err < 0)
2028 pr_err("Failed to process auxtrace index\n");
2029 return err;
2030}
2031
Jiri Olsa720e98b2016-02-16 16:01:43 +01002032static int process_cache(struct perf_file_section *section __maybe_unused,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002033 struct feat_fd *ff, void *data __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01002034{
2035 struct cpu_cache_level *caches;
2036 u32 cnt, i, version;
2037
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002038 if (do_read_u32(ff->fd, ff->ph, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002039 return -1;
2040
Jiri Olsa720e98b2016-02-16 16:01:43 +01002041 if (version != 1)
2042 return -1;
2043
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002044 if (do_read_u32(ff->fd, ff->ph, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002045 return -1;
2046
Jiri Olsa720e98b2016-02-16 16:01:43 +01002047 caches = zalloc(sizeof(*caches) * cnt);
2048 if (!caches)
2049 return -1;
2050
2051 for (i = 0; i < cnt; i++) {
2052 struct cpu_cache_level c;
2053
2054 #define _R(v) \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002055 if (do_read_u32(ff->fd, ff->ph, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002056 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002057
2058 _R(level)
2059 _R(line_size)
2060 _R(sets)
2061 _R(ways)
2062 #undef _R
2063
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002064 #define _R(v) \
2065 c.v = do_read_string(ff->fd, ff->ph); \
2066 if (!c.v) \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002067 goto out_free_caches;
2068
2069 _R(type)
2070 _R(size)
2071 _R(map)
2072 #undef _R
2073
2074 caches[i] = c;
2075 }
2076
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002077 ff->ph->env.caches = caches;
2078 ff->ph->env.caches_cnt = cnt;
Jiri Olsa720e98b2016-02-16 16:01:43 +01002079 return 0;
2080out_free_caches:
2081 free(caches);
2082 return -1;
2083}
2084
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002085struct feature_ops {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002086 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002087 void (*print)(struct feat_fd *ff, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002088 int (*process)(struct perf_file_section *section,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002089 struct feat_fd *ff, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002090 const char *name;
2091 bool full_only;
2092};
2093
Robert Richter8cdfa782011-12-07 10:02:56 +01002094#define FEAT_OPA(n, func) \
2095 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002096#define FEAT_OPP(n, func) \
2097 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2098 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002099#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002100 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002101 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002102
2103/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002104#define print_tracing_data NULL
2105#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002106
2107static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002108 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002109 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002110 FEAT_OPP(HEADER_HOSTNAME, hostname),
2111 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2112 FEAT_OPP(HEADER_VERSION, version),
2113 FEAT_OPP(HEADER_ARCH, arch),
2114 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2115 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002116 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002117 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002118 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002119 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002120 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2121 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002122 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002123 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002124 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Adrian Hunter99fa2982015-04-30 17:37:25 +03002125 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
Jiri Olsaffa517a2015-10-25 15:51:43 +01002126 FEAT_OPA(HEADER_STAT, stat),
Jiri Olsa720e98b2016-02-16 16:01:43 +01002127 FEAT_OPF(HEADER_CACHE, cache),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002128};
2129
2130struct header_print_data {
2131 FILE *fp;
2132 bool full; /* extended list of headers */
2133};
2134
2135static int perf_file_section__fprintf_info(struct perf_file_section *section,
2136 struct perf_header *ph,
2137 int feat, int fd, void *data)
2138{
2139 struct header_print_data *hd = data;
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002140 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002141
2142 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2143 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2144 "%d, continuing...\n", section->offset, feat);
2145 return 0;
2146 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002147 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002148 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002149 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002150 }
2151 if (!feat_ops[feat].print)
2152 return 0;
2153
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002154 ff = (struct feat_fd) {
2155 .fd = fd,
2156 .ph = ph,
2157 };
2158
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002159 if (!feat_ops[feat].full_only || hd->full)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002160 feat_ops[feat].print(&ff, hd->fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002161 else
2162 fprintf(hd->fp, "# %s info available, use -I to display\n",
2163 feat_ops[feat].name);
2164
2165 return 0;
2166}
2167
2168int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2169{
2170 struct header_print_data hd;
2171 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002172 int fd = perf_data_file__fd(session->file);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002173 struct stat st;
Jiri Olsaaabae162016-10-10 09:35:50 +02002174 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002175
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002176 hd.fp = fp;
2177 hd.full = full;
2178
Jiri Olsaf45f5612016-10-10 09:03:07 +02002179 ret = fstat(fd, &st);
2180 if (ret == -1)
2181 return -1;
2182
2183 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2184
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002185 perf_header__process_sections(header, fd, &hd,
2186 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002187
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002188 if (session->file->is_pipe)
2189 return 0;
2190
Jiri Olsaaabae162016-10-10 09:35:50 +02002191 fprintf(fp, "# missing features: ");
2192 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2193 if (bit)
2194 fprintf(fp, "%s ", feat_ops[bit].name);
2195 }
2196
2197 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002198 return 0;
2199}
2200
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002201static int do_write_feat(struct feat_fd *ff, int type,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002202 struct perf_file_section **p,
2203 struct perf_evlist *evlist)
2204{
2205 int err;
2206 int ret = 0;
2207
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002208 if (perf_header__has_feat(ff->ph, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002209 if (!feat_ops[type].write)
2210 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002211
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002212 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002213
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002214 err = feat_ops[type].write(ff, evlist);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002215 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002216 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002217
2218 /* undo anything written */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002219 lseek(ff->fd, (*p)->offset, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002220
2221 return -1;
2222 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002223 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002224 (*p)++;
2225 }
2226 return ret;
2227}
2228
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002229static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002230 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002231{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002232 int nr_sections;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002233 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002234 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002235 int sec_size;
2236 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002237 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002238 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002239
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002240 ff = (struct feat_fd){
2241 .fd = fd,
2242 .ph = header,
2243 };
2244
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002245 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002246 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002247 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002248
Paul Gortmaker91b98802013-01-30 20:05:49 -05002249 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002250 if (feat_sec == NULL)
2251 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002252
2253 sec_size = sizeof(*feat_sec) * nr_sections;
2254
Jiri Olsa8d541e92013-07-17 19:49:44 +02002255 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002256 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002257
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002258 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002259 if (do_write_feat(&ff, feat, &p, evlist))
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002260 perf_header__clear_feat(header, feat);
2261 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002262
Xiao Guangrongf887f302010-02-04 16:46:42 +08002263 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002264 /*
2265 * may write more than needed due to dropped feature, but
2266 * this is okay, reader will skip the mising entries
2267 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002268 err = do_write(&ff, feat_sec, sec_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002269 if (err < 0)
2270 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002271 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002272 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002273}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002274
Tom Zanussi8dc58102010-04-01 23:59:15 -05002275int perf_header__write_pipe(int fd)
2276{
2277 struct perf_pipe_file_header f_header;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002278 struct feat_fd ff;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002279 int err;
2280
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002281 ff = (struct feat_fd){ .fd = fd };
2282
Tom Zanussi8dc58102010-04-01 23:59:15 -05002283 f_header = (struct perf_pipe_file_header){
2284 .magic = PERF_MAGIC,
2285 .size = sizeof(f_header),
2286 };
2287
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002288 err = do_write(&ff, &f_header, sizeof(f_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05002289 if (err < 0) {
2290 pr_debug("failed to write perf pipe header\n");
2291 return err;
2292 }
2293
2294 return 0;
2295}
2296
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002297int perf_session__write_header(struct perf_session *session,
2298 struct perf_evlist *evlist,
2299 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002300{
2301 struct perf_file_header f_header;
2302 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002303 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002304 struct perf_evsel *evsel;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002305 struct feat_fd ff;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002306 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002307 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002308
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002309 ff = (struct feat_fd){ .fd = fd};
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002310 lseek(fd, sizeof(f_header), SEEK_SET);
2311
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002312 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002313 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002314 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002315 if (err < 0) {
2316 pr_debug("failed to write perf header\n");
2317 return err;
2318 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002319 }
2320
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002321 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002322
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002323 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002324 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002325 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002326 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002327 .offset = evsel->id_offset,
2328 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002329 }
2330 };
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002331 err = do_write(&ff, &f_attr, sizeof(f_attr));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002332 if (err < 0) {
2333 pr_debug("failed to write perf header attribute\n");
2334 return err;
2335 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002336 }
2337
Adrian Hunterd645c442013-12-11 14:36:28 +02002338 if (!header->data_offset)
2339 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002340 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002341
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002342 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002343 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002344 if (err < 0)
2345 return err;
2346 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002347
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002348 f_header = (struct perf_file_header){
2349 .magic = PERF_MAGIC,
2350 .size = sizeof(f_header),
2351 .attr_size = sizeof(f_attr),
2352 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002353 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002354 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002355 },
2356 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002357 .offset = header->data_offset,
2358 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002359 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002360 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002361 };
2362
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002363 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002364
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002365 lseek(fd, 0, SEEK_SET);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002366 err = do_write(&ff, &f_header, sizeof(f_header));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002367 if (err < 0) {
2368 pr_debug("failed to write perf header\n");
2369 return err;
2370 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002371 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002372
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002373 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002374}
2375
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002376static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002377 int fd, void *buf, size_t size)
2378{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002379 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002380 return -1;
2381
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002382 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002383 mem_bswap_64(buf, size);
2384
2385 return 0;
2386}
2387
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002388int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002389 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002390 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002391 struct perf_header *ph,
2392 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002393{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002394 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002395 int nr_sections;
2396 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002397 int feat;
2398 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002399
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002400 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002401 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002402 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002403
Paul Gortmaker91b98802013-01-30 20:05:49 -05002404 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002405 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002406 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002407
2408 sec_size = sizeof(*feat_sec) * nr_sections;
2409
Jiri Olsa8d541e92013-07-17 19:49:44 +02002410 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002411
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002412 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2413 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002414 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002415
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002416 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2417 err = process(sec++, header, feat, fd, data);
2418 if (err < 0)
2419 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002420 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002421 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002422out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002423 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002424 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002425}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002426
Stephane Eranian114382a2012-02-09 23:21:08 +01002427static const int attr_file_abi_sizes[] = {
2428 [0] = PERF_ATTR_SIZE_VER0,
2429 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002430 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002431 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002432 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002433 0,
2434};
2435
2436/*
2437 * In the legacy file format, the magic number is not used to encode endianness.
2438 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2439 * on ABI revisions, we need to try all combinations for all endianness to
2440 * detect the endianness.
2441 */
2442static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2443{
2444 uint64_t ref_size, attr_size;
2445 int i;
2446
2447 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2448 ref_size = attr_file_abi_sizes[i]
2449 + sizeof(struct perf_file_section);
2450 if (hdr_sz != ref_size) {
2451 attr_size = bswap_64(hdr_sz);
2452 if (attr_size != ref_size)
2453 continue;
2454
2455 ph->needs_swap = true;
2456 }
2457 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2458 i,
2459 ph->needs_swap);
2460 return 0;
2461 }
2462 /* could not determine endianness */
2463 return -1;
2464}
2465
2466#define PERF_PIPE_HDR_VER0 16
2467
2468static const size_t attr_pipe_abi_sizes[] = {
2469 [0] = PERF_PIPE_HDR_VER0,
2470 0,
2471};
2472
2473/*
2474 * In the legacy pipe format, there is an implicit assumption that endiannesss
2475 * between host recording the samples, and host parsing the samples is the
2476 * same. This is not always the case given that the pipe output may always be
2477 * redirected into a file and analyzed on a different machine with possibly a
2478 * different endianness and perf_event ABI revsions in the perf tool itself.
2479 */
2480static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2481{
2482 u64 attr_size;
2483 int i;
2484
2485 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2486 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2487 attr_size = bswap_64(hdr_sz);
2488 if (attr_size != hdr_sz)
2489 continue;
2490
2491 ph->needs_swap = true;
2492 }
2493 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2494 return 0;
2495 }
2496 return -1;
2497}
2498
Feng Tange84ba4e2012-10-30 11:56:07 +08002499bool is_perf_magic(u64 magic)
2500{
2501 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2502 || magic == __perf_magic2
2503 || magic == __perf_magic2_sw)
2504 return true;
2505
2506 return false;
2507}
2508
Stephane Eranian114382a2012-02-09 23:21:08 +01002509static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2510 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002511{
2512 int ret;
2513
2514 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002515 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002516 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002517 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002518 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002519 if (is_pipe)
2520 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002521
Stephane Eranian114382a2012-02-09 23:21:08 +01002522 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002523 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002524 /*
2525 * the new magic number serves two purposes:
2526 * - unique number to identify actual perf.data files
2527 * - encode endianness of file
2528 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002529 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002530
Stephane Eranian114382a2012-02-09 23:21:08 +01002531 /* check magic number with one endianness */
2532 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002533 return 0;
2534
Stephane Eranian114382a2012-02-09 23:21:08 +01002535 /* check magic number with opposite endianness */
2536 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002537 return -1;
2538
2539 ph->needs_swap = true;
2540
2541 return 0;
2542}
2543
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002544int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002545 struct perf_header *ph, int fd)
2546{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002547 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002548
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002549 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002550
Stephane Eranian73323f52012-02-02 13:54:44 +01002551 ret = readn(fd, header, sizeof(*header));
2552 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002553 return -1;
2554
Stephane Eranian114382a2012-02-09 23:21:08 +01002555 if (check_magic_endian(header->magic,
2556 header->attr_size, false, ph) < 0) {
2557 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002558 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002559 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002560
Stephane Eranian73323f52012-02-02 13:54:44 +01002561 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002562 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002563 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002564 }
2565
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002566 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002567 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002568 if (header->size == offsetof(typeof(*header), adds_features))
2569 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002570 else
2571 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002572 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002573 /*
2574 * feature bitmap is declared as an array of unsigned longs --
2575 * not good since its size can differ between the host that
2576 * generated the data file and the host analyzing the file.
2577 *
2578 * We need to handle endianness, but we don't know the size of
2579 * the unsigned long where the file was generated. Take a best
2580 * guess at determining it: try 64-bit swap first (ie., file
2581 * created on a 64-bit host), and check if the hostname feature
2582 * bit is set (this feature bit is forced on as of fbe96f2).
2583 * If the bit is not, undo the 64-bit swap and try a 32-bit
2584 * swap. If the hostname bit is still not set (e.g., older data
2585 * file), punt and fallback to the original behavior --
2586 * clearing all feature bits and setting buildid.
2587 */
David Ahern80c01202012-06-08 11:47:51 -03002588 mem_bswap_64(&header->adds_features,
2589 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002590
2591 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002592 /* unswap as u64 */
2593 mem_bswap_64(&header->adds_features,
2594 BITS_TO_U64(HEADER_FEAT_BITS));
2595
2596 /* unswap as u32 */
2597 mem_bswap_32(&header->adds_features,
2598 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002599 }
2600
2601 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2602 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2603 set_bit(HEADER_BUILD_ID, header->adds_features);
2604 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002605 }
2606
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002607 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002608 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002609
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002610 ph->data_offset = header->data.offset;
2611 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002612 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002613 return 0;
2614}
2615
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002616static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002617 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002618 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002619{
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002620 struct feat_fd ff = {
2621 .fd = fd,
2622 .ph = ph,
2623 };
2624
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
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002639 return feat_ops[feat].process(section, &ff, 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{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002646 struct feat_fd ff = {
2647 .fd = STDOUT_FILENO,
2648 .ph = ph,
2649 };
Jiri Olsa727ebd52013-11-28 11:30:14 +01002650 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002651
2652 ret = readn(fd, header, sizeof(*header));
2653 if (ret <= 0)
2654 return -1;
2655
Stephane Eranian114382a2012-02-09 23:21:08 +01002656 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2657 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002658 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002659 }
2660
2661 if (ph->needs_swap)
2662 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002663
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002664 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002665 return -1;
2666
Tom Zanussi8dc58102010-04-01 23:59:15 -05002667 return 0;
2668}
2669
Jiri Olsad4339562013-07-17 19:49:41 +02002670static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002671{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002672 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002673 struct perf_pipe_file_header f_header;
2674
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002675 if (perf_file_header__read_pipe(&f_header, header,
2676 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002677 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002678 pr_debug("incompatible file format\n");
2679 return -EINVAL;
2680 }
2681
Tom Zanussi8dc58102010-04-01 23:59:15 -05002682 return 0;
2683}
2684
Stephane Eranian69996df2012-02-09 23:21:06 +01002685static int read_attr(int fd, struct perf_header *ph,
2686 struct perf_file_attr *f_attr)
2687{
2688 struct perf_event_attr *attr = &f_attr->attr;
2689 size_t sz, left;
2690 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002691 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002692
2693 memset(f_attr, 0, sizeof(*f_attr));
2694
2695 /* read minimal guaranteed structure */
2696 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2697 if (ret <= 0) {
2698 pr_debug("cannot read %d bytes of header attr\n",
2699 PERF_ATTR_SIZE_VER0);
2700 return -1;
2701 }
2702
2703 /* on file perf_event_attr size */
2704 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002705
Stephane Eranian69996df2012-02-09 23:21:06 +01002706 if (ph->needs_swap)
2707 sz = bswap_32(sz);
2708
2709 if (sz == 0) {
2710 /* assume ABI0 */
2711 sz = PERF_ATTR_SIZE_VER0;
2712 } else if (sz > our_sz) {
2713 pr_debug("file uses a more recent and unsupported ABI"
2714 " (%zu bytes extra)\n", sz - our_sz);
2715 return -1;
2716 }
2717 /* what we have not yet read and that we know about */
2718 left = sz - PERF_ATTR_SIZE_VER0;
2719 if (left) {
2720 void *ptr = attr;
2721 ptr += PERF_ATTR_SIZE_VER0;
2722
2723 ret = readn(fd, ptr, left);
2724 }
2725 /* read perf_file_section, ids are read in caller */
2726 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2727
2728 return ret <= 0 ? -1 : 0;
2729}
2730
Namhyung Kim831394b2012-09-06 11:10:46 +09002731static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2732 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002733{
Namhyung Kim831394b2012-09-06 11:10:46 +09002734 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002735 char bf[128];
2736
Namhyung Kim831394b2012-09-06 11:10:46 +09002737 /* already prepared */
2738 if (evsel->tp_format)
2739 return 0;
2740
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002741 if (pevent == NULL) {
2742 pr_debug("broken or missing trace data\n");
2743 return -1;
2744 }
2745
Namhyung Kim831394b2012-09-06 11:10:46 +09002746 event = pevent_find_event(pevent, evsel->attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09002747 if (event == NULL) {
2748 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002749 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09002750 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002751
Namhyung Kim831394b2012-09-06 11:10:46 +09002752 if (!evsel->name) {
2753 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2754 evsel->name = strdup(bf);
2755 if (evsel->name == NULL)
2756 return -1;
2757 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002758
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002759 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002760 return 0;
2761}
2762
Namhyung Kim831394b2012-09-06 11:10:46 +09002763static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2764 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002765{
2766 struct perf_evsel *pos;
2767
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002768 evlist__for_each_entry(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002769 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2770 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002771 return -1;
2772 }
2773
2774 return 0;
2775}
2776
Jiri Olsad4339562013-07-17 19:49:41 +02002777int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002778{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002779 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002780 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002781 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002782 struct perf_file_attr f_attr;
2783 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002784 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002785 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002786
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002787 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002788 if (session->evlist == NULL)
2789 return -ENOMEM;
2790
Kan Liang2c071442015-08-28 05:48:05 -04002791 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03002792 session->machines.host.env = &header->env;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002793 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002794 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002795
Stephane Eranian69996df2012-02-09 23:21:06 +01002796 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002797 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002798
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002799 /*
2800 * Sanity check that perf.data was written cleanly; data size is
2801 * initialized to 0 and updated only if the on_exit function is run.
2802 * If data size is still 0 then the file contains only partial
2803 * information. Just warn user and process it as much as it can.
2804 */
2805 if (f_header.data.size == 0) {
2806 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2807 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002808 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002809 }
2810
Stephane Eranian69996df2012-02-09 23:21:06 +01002811 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002812 lseek(fd, f_header.attrs.offset, SEEK_SET);
2813
2814 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002815 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002816 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002817
Stephane Eranian69996df2012-02-09 23:21:06 +01002818 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002819 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002820
David Ahern1060ab82015-04-09 16:15:46 -04002821 if (header->needs_swap) {
2822 f_attr.ids.size = bswap_64(f_attr.ids.size);
2823 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002824 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002825 }
David Aherneda39132011-07-15 12:34:09 -06002826
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002827 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002828 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002829
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002830 if (evsel == NULL)
2831 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002832
2833 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002834 /*
2835 * Do it before so that if perf_evsel__alloc_id fails, this
2836 * entry gets purged too at perf_evlist__delete().
2837 */
2838 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002839
2840 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002841 /*
2842 * We don't have the cpu and thread maps on the header, so
2843 * for allocating the perf_sample_id table we fake 1 cpu and
2844 * hattr->ids threads.
2845 */
2846 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2847 goto out_delete_evlist;
2848
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002849 lseek(fd, f_attr.ids.offset, SEEK_SET);
2850
2851 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002852 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002853 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002854
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002855 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002856 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002857
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002858 lseek(fd, tmp, SEEK_SET);
2859 }
2860
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002861 symbol_conf.nr_events = nr_attrs;
2862
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002863 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002864 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002865
Namhyung Kim831394b2012-09-06 11:10:46 +09002866 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002867 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002868 goto out_delete_evlist;
2869
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002870 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002871out_errno:
2872 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002873
2874out_delete_evlist:
2875 perf_evlist__delete(session->evlist);
2876 session->evlist = NULL;
2877 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002878}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002879
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002880int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002881 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002882 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002883{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002884 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002885 size_t size;
2886 int err;
2887
2888 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002889 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002890 size += sizeof(struct perf_event_header);
2891 size += ids * sizeof(u64);
2892
2893 ev = malloc(size);
2894
Chris Samuelce47dc52010-11-13 13:35:06 +11002895 if (ev == NULL)
2896 return -ENOMEM;
2897
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002898 ev->attr.attr = *attr;
2899 memcpy(ev->attr.id, id, ids * sizeof(u64));
2900
2901 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002902 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002903
Robert Richterf4d83432012-08-16 21:10:17 +02002904 if (ev->attr.header.size == size)
2905 err = process(tool, ev, NULL, NULL);
2906 else
2907 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002908
2909 free(ev);
2910
2911 return err;
2912}
2913
Jiri Olsaa6e52812015-10-25 15:51:37 +01002914static struct event_update_event *
2915event_update_event__new(size_t size, u64 type, u64 id)
2916{
2917 struct event_update_event *ev;
2918
2919 size += sizeof(*ev);
2920 size = PERF_ALIGN(size, sizeof(u64));
2921
2922 ev = zalloc(size);
2923 if (ev) {
2924 ev->header.type = PERF_RECORD_EVENT_UPDATE;
2925 ev->header.size = (u16)size;
2926 ev->type = type;
2927 ev->id = id;
2928 }
2929 return ev;
2930}
2931
2932int
2933perf_event__synthesize_event_update_unit(struct perf_tool *tool,
2934 struct perf_evsel *evsel,
2935 perf_event__handler_t process)
2936{
2937 struct event_update_event *ev;
2938 size_t size = strlen(evsel->unit);
2939 int err;
2940
2941 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
2942 if (ev == NULL)
2943 return -ENOMEM;
2944
2945 strncpy(ev->data, evsel->unit, size);
2946 err = process(tool, (union perf_event *)ev, NULL, NULL);
2947 free(ev);
2948 return err;
2949}
2950
Jiri Olsadaeecbc2015-10-25 15:51:38 +01002951int
2952perf_event__synthesize_event_update_scale(struct perf_tool *tool,
2953 struct perf_evsel *evsel,
2954 perf_event__handler_t process)
2955{
2956 struct event_update_event *ev;
2957 struct event_update_event_scale *ev_data;
2958 int err;
2959
2960 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
2961 if (ev == NULL)
2962 return -ENOMEM;
2963
2964 ev_data = (struct event_update_event_scale *) ev->data;
2965 ev_data->scale = evsel->scale;
2966 err = process(tool, (union perf_event*) ev, NULL, NULL);
2967 free(ev);
2968 return err;
2969}
2970
Jiri Olsa802c9042015-10-25 15:51:39 +01002971int
2972perf_event__synthesize_event_update_name(struct perf_tool *tool,
2973 struct perf_evsel *evsel,
2974 perf_event__handler_t process)
2975{
2976 struct event_update_event *ev;
2977 size_t len = strlen(evsel->name);
2978 int err;
2979
2980 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
2981 if (ev == NULL)
2982 return -ENOMEM;
2983
2984 strncpy(ev->data, evsel->name, len);
2985 err = process(tool, (union perf_event*) ev, NULL, NULL);
2986 free(ev);
2987 return err;
2988}
Jiri Olsadaeecbc2015-10-25 15:51:38 +01002989
Jiri Olsa86ebb092015-10-25 15:51:40 +01002990int
2991perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
2992 struct perf_evsel *evsel,
2993 perf_event__handler_t process)
2994{
2995 size_t size = sizeof(struct event_update_event);
2996 struct event_update_event *ev;
2997 int max, err;
2998 u16 type;
2999
3000 if (!evsel->own_cpus)
3001 return 0;
3002
3003 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3004 if (!ev)
3005 return -ENOMEM;
3006
3007 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3008 ev->header.size = (u16)size;
3009 ev->type = PERF_EVENT_UPDATE__CPUS;
3010 ev->id = evsel->id[0];
3011
3012 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3013 evsel->own_cpus,
3014 type, max);
3015
3016 err = process(tool, (union perf_event*) ev, NULL, NULL);
3017 free(ev);
3018 return err;
3019}
3020
Jiri Olsac853f932015-10-25 15:51:41 +01003021size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3022{
3023 struct event_update_event *ev = &event->event_update;
3024 struct event_update_event_scale *ev_scale;
3025 struct event_update_event_cpus *ev_cpus;
3026 struct cpu_map *map;
3027 size_t ret;
3028
3029 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3030
3031 switch (ev->type) {
3032 case PERF_EVENT_UPDATE__SCALE:
3033 ev_scale = (struct event_update_event_scale *) ev->data;
3034 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3035 break;
3036 case PERF_EVENT_UPDATE__UNIT:
3037 ret += fprintf(fp, "... unit: %s\n", ev->data);
3038 break;
3039 case PERF_EVENT_UPDATE__NAME:
3040 ret += fprintf(fp, "... name: %s\n", ev->data);
3041 break;
3042 case PERF_EVENT_UPDATE__CPUS:
3043 ev_cpus = (struct event_update_event_cpus *) ev->data;
3044 ret += fprintf(fp, "... ");
3045
3046 map = cpu_map__new_data(&ev_cpus->cpus);
3047 if (map)
3048 ret += cpu_map__fprintf(map, fp);
3049 else
3050 ret += fprintf(fp, "failed to get cpus\n");
3051 break;
3052 default:
3053 ret += fprintf(fp, "... unknown type\n");
3054 break;
3055 }
3056
3057 return ret;
3058}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003059
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003060int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003061 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003062 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003063{
Robert Richter6606f872012-08-16 21:10:19 +02003064 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003065 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003066
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003067 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003068 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3069 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003070 if (err) {
3071 pr_debug("failed to create perf header attribute\n");
3072 return err;
3073 }
3074 }
3075
3076 return err;
3077}
3078
Adrian Hunter47c3d102013-07-04 16:20:21 +03003079int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3080 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003081 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003082{
Robert Richterf4d83432012-08-16 21:10:17 +02003083 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003084 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003085 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003086
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003087 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003088 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003089 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003090 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003091 }
3092
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003093 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003094 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003095 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003096
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003097 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003098
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003099 ids = event->header.size;
3100 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003101 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003102 /*
3103 * We don't have the cpu and thread maps on the header, so
3104 * for allocating the perf_sample_id table we fake 1 cpu and
3105 * hattr->ids threads.
3106 */
3107 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3108 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003109
3110 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003111 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003112 }
3113
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03003114 symbol_conf.nr_events = evlist->nr_entries;
3115
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003116 return 0;
3117}
Tom Zanussicd19a032010-04-01 23:59:20 -05003118
Jiri Olsaffe777252015-10-25 15:51:36 +01003119int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3120 union perf_event *event,
3121 struct perf_evlist **pevlist)
3122{
3123 struct event_update_event *ev = &event->event_update;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003124 struct event_update_event_scale *ev_scale;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003125 struct event_update_event_cpus *ev_cpus;
Jiri Olsaffe777252015-10-25 15:51:36 +01003126 struct perf_evlist *evlist;
3127 struct perf_evsel *evsel;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003128 struct cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003129
3130 if (!pevlist || *pevlist == NULL)
3131 return -EINVAL;
3132
3133 evlist = *pevlist;
3134
3135 evsel = perf_evlist__id2evsel(evlist, ev->id);
3136 if (evsel == NULL)
3137 return -EINVAL;
3138
Jiri Olsaa6e52812015-10-25 15:51:37 +01003139 switch (ev->type) {
3140 case PERF_EVENT_UPDATE__UNIT:
3141 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003142 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003143 case PERF_EVENT_UPDATE__NAME:
3144 evsel->name = strdup(ev->data);
3145 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003146 case PERF_EVENT_UPDATE__SCALE:
3147 ev_scale = (struct event_update_event_scale *) ev->data;
3148 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003149 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003150 case PERF_EVENT_UPDATE__CPUS:
3151 ev_cpus = (struct event_update_event_cpus *) ev->data;
3152
3153 map = cpu_map__new_data(&ev_cpus->cpus);
3154 if (map)
3155 evsel->own_cpus = map;
3156 else
3157 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003158 default:
3159 break;
3160 }
3161
Jiri Olsaffe777252015-10-25 15:51:36 +01003162 return 0;
3163}
3164
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003165int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003166 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003167 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003168{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003169 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003170 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003171 ssize_t size = 0, aligned_size = 0, padding;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003172 struct feat_fd ff;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003173 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003174
Jiri Olsa29208e52011-10-20 15:59:43 +02003175 /*
3176 * We are going to store the size of the data followed
3177 * by the data contents. Since the fd descriptor is a pipe,
3178 * we cannot seek back to store the size of the data once
3179 * we know it. Instead we:
3180 *
3181 * - write the tracing data to the temp file
3182 * - get/write the data size to pipe
3183 * - write the tracing data from the temp file
3184 * to the pipe
3185 */
3186 tdata = tracing_data_get(&evlist->entries, fd, true);
3187 if (!tdata)
3188 return -1;
3189
Tom Zanussi92155452010-04-01 23:59:21 -05003190 memset(&ev, 0, sizeof(ev));
3191
3192 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003193 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003194 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003195 padding = aligned_size - size;
3196 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3197 ev.tracing_data.size = aligned_size;
3198
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003199 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003200
Jiri Olsa29208e52011-10-20 15:59:43 +02003201 /*
3202 * The put function will copy all the tracing data
3203 * stored in temp file to the pipe.
3204 */
3205 tracing_data_put(tdata);
3206
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003207 ff = (struct feat_fd){ .fd = fd };
3208 if (write_padded(&ff, NULL, 0, padding))
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -07003209 return -1;
Tom Zanussi92155452010-04-01 23:59:21 -05003210
3211 return aligned_size;
3212}
3213
Adrian Hunter47c3d102013-07-04 16:20:21 +03003214int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3215 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003216 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05003217{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003218 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003219 int fd = perf_data_file__fd(session->file);
3220 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003221 char buf[BUFSIZ];
3222
3223 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003224 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003225 SEEK_SET);
3226
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003227 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003228 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003229 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003230
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003231 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003232 pr_err("%s: reading input file", __func__);
3233 return -1;
3234 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003235 if (session->repipe) {
3236 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003237 if (retw <= 0 || retw != padding) {
3238 pr_err("%s: repiping tracing data padding", __func__);
3239 return -1;
3240 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003241 }
Tom Zanussi92155452010-04-01 23:59:21 -05003242
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003243 if (size_read + padding != size) {
3244 pr_err("%s: tracing data size mismatch", __func__);
3245 return -1;
3246 }
Tom Zanussi92155452010-04-01 23:59:21 -05003247
Namhyung Kim831394b2012-09-06 11:10:46 +09003248 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003249 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003250
Tom Zanussi92155452010-04-01 23:59:21 -05003251 return size_read + padding;
3252}
Tom Zanussic7929e42010-04-01 23:59:22 -05003253
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003254int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003255 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003256 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003257 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003258{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003259 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003260 size_t len;
3261 int err = 0;
3262
3263 if (!pos->hit)
3264 return err;
3265
3266 memset(&ev, 0, sizeof(ev));
3267
3268 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003269 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003270 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3271 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3272 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003273 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003274 ev.build_id.header.size = sizeof(ev.build_id) + len;
3275 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3276
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003277 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003278
3279 return err;
3280}
3281
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003282int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003283 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003284 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003285{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003286 __event_process_build_id(&event->build_id,
3287 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003288 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003289 return 0;
3290}