blob: 58e888ae13d431b64f79ae18f998777fbebcc2e2 [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, \
1597 struct perf_header *ph, int fd, \
1598 void *data __maybe_unused) \
1599{\
1600 ph->env.__feat_env = do_read_string(fd, ph); \
1601 return ph->env.__feat_env ? 0 : -ENOMEM; \
1602}
1603
1604FEAT_PROCESS_STR_FUN(hostname, hostname);
1605FEAT_PROCESS_STR_FUN(osrelease, os_release);
1606FEAT_PROCESS_STR_FUN(version, version);
1607FEAT_PROCESS_STR_FUN(arch, arch);
1608FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1609FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1610
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001611static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1612 struct perf_header *ph __maybe_unused,
1613 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001614{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001615 ssize_t ret = trace_report(fd, data, false);
1616 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001617}
1618
1619static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001620 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001621 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001622{
1623 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1624 pr_debug("Failed to read buildids, continuing...\n");
1625 return 0;
1626}
1627
Namhyung Kima1ae5652012-09-24 17:14:59 +09001628static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001629 struct perf_header *ph, int fd,
1630 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001631{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001632 int ret;
1633 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001634
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001635 ret = do_read_u32(fd, ph, &nr_cpus_avail);
1636 if (ret)
1637 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001638
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001639 ret = do_read_u32(fd, ph, &nr_cpus_online);
1640 if (ret)
1641 return ret;
1642 ph->env.nr_cpus_avail = (int)nr_cpus_avail;
1643 ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001644 return 0;
1645}
1646
Namhyung Kima1ae5652012-09-24 17:14:59 +09001647static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001648 struct perf_header *ph, int fd,
1649 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001650{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001651 u64 total_mem;
1652 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001653
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001654 ret = do_read_u64(fd, ph, &total_mem);
1655 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001656 return -1;
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001657 ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001658 return 0;
1659}
1660
Robert Richter7c2f7af2012-08-16 21:10:23 +02001661static struct perf_evsel *
1662perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1663{
1664 struct perf_evsel *evsel;
1665
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001666 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001667 if (evsel->idx == idx)
1668 return evsel;
1669 }
1670
1671 return NULL;
1672}
1673
1674static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001675perf_evlist__set_event_name(struct perf_evlist *evlist,
1676 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001677{
1678 struct perf_evsel *evsel;
1679
1680 if (!event->name)
1681 return;
1682
1683 evsel = perf_evlist__find_by_index(evlist, event->idx);
1684 if (!evsel)
1685 return;
1686
1687 if (evsel->name)
1688 return;
1689
1690 evsel->name = strdup(event->name);
1691}
1692
1693static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001694process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001695 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001696 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001697{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001698 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001699 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1700
1701 if (!events)
1702 return 0;
1703
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001704 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001705 for (evsel = events; evsel->attr.size; evsel++)
1706 perf_evlist__set_event_name(session->evlist, evsel);
1707
1708 free_event_desc(events);
1709
1710 return 0;
1711}
1712
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001713static int process_cmdline(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001714 struct perf_header *ph, int fd,
1715 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001716{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001717 char *str, *cmdline = NULL, **argv = NULL;
1718 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001719
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001720 if (do_read_u32(fd, ph, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001721 return -1;
1722
Namhyung Kima1ae5652012-09-24 17:14:59 +09001723 ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001724
1725 cmdline = zalloc(section->size + nr + 1);
1726 if (!cmdline)
1727 return -1;
1728
1729 argv = zalloc(sizeof(char *) * (nr + 1));
1730 if (!argv)
1731 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001732
1733 for (i = 0; i < nr; i++) {
1734 str = do_read_string(fd, ph);
1735 if (!str)
1736 goto error;
1737
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001738 argv[i] = cmdline + len;
1739 memcpy(argv[i], str, strlen(str) + 1);
1740 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001741 free(str);
1742 }
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001743 ph->env.cmdline = cmdline;
1744 ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001745 return 0;
1746
1747error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001748 free(argv);
1749 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001750 return -1;
1751}
1752
Kan Liang2bb00d22015-09-01 09:58:12 -04001753static int process_cpu_topology(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001754 struct perf_header *ph, int fd,
1755 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001756{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001757 u32 nr, i;
1758 char *str;
1759 struct strbuf sb;
Jan Stancekda8a58b2017-02-17 12:10:26 +01001760 int cpu_nr = ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04001761 u64 size = 0;
1762
1763 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1764 if (!ph->env.cpu)
1765 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001766
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001767 if (do_read_u32(fd, ph, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001768 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001769
Namhyung Kima1ae5652012-09-24 17:14:59 +09001770 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001771 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001772 if (strbuf_init(&sb, 128) < 0)
1773 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001774
1775 for (i = 0; i < nr; i++) {
1776 str = do_read_string(fd, ph);
1777 if (!str)
1778 goto error;
1779
1780 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001781 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1782 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001783 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001784 free(str);
1785 }
1786 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1787
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001788 if (do_read_u32(fd, ph, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001789 return -1;
1790
Namhyung Kima1ae5652012-09-24 17:14:59 +09001791 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001792 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001793
1794 for (i = 0; i < nr; i++) {
1795 str = do_read_string(fd, ph);
1796 if (!str)
1797 goto error;
1798
1799 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001800 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1801 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001802 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001803 free(str);
1804 }
1805 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04001806
1807 /*
1808 * The header may be from old perf,
1809 * which doesn't include core id and socket id information.
1810 */
1811 if (section->size <= size) {
1812 zfree(&ph->env.cpu);
1813 return 0;
1814 }
1815
1816 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001817 if (do_read_u32(fd, ph, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001818 goto free_cpu;
1819
Kan Liang2bb00d22015-09-01 09:58:12 -04001820 ph->env.cpu[i].core_id = nr;
1821
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001822 if (do_read_u32(fd, ph, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001823 goto free_cpu;
1824
Jan Stancekda8a58b2017-02-17 12:10:26 +01001825 if (nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001826 pr_debug("socket_id number is too big."
1827 "You may need to upgrade the perf tool.\n");
1828 goto free_cpu;
1829 }
1830
1831 ph->env.cpu[i].socket_id = nr;
1832 }
1833
Namhyung Kima1ae5652012-09-24 17:14:59 +09001834 return 0;
1835
1836error:
1837 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04001838free_cpu:
1839 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001840 return -1;
1841}
1842
1843static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001844 struct perf_header *ph, int fd,
1845 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001846{
Jiri Olsac60da222016-07-04 14:16:20 +02001847 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02001848 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001849 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001850
1851 /* nr nodes */
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001852 if (do_read_u32(fd, ph, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001853 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001854
Jiri Olsac60da222016-07-04 14:16:20 +02001855 nodes = zalloc(sizeof(*nodes) * nr);
1856 if (!nodes)
1857 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001858
1859 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02001860 n = &nodes[i];
1861
Namhyung Kima1ae5652012-09-24 17:14:59 +09001862 /* node number */
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001863 if (do_read_u32(fd, ph, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001864 goto error;
1865
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001866 if (do_read_u64(fd, ph, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001867 goto error;
1868
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001869 if (do_read_u64(fd, ph, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001870 goto error;
1871
Namhyung Kima1ae5652012-09-24 17:14:59 +09001872 str = do_read_string(fd, ph);
1873 if (!str)
1874 goto error;
1875
Jiri Olsac60da222016-07-04 14:16:20 +02001876 n->map = cpu_map__new(str);
1877 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001878 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02001879
Namhyung Kima1ae5652012-09-24 17:14:59 +09001880 free(str);
1881 }
Jiri Olsaf957a532016-10-10 09:56:32 +02001882 ph->env.nr_numa_nodes = nr;
Jiri Olsac60da222016-07-04 14:16:20 +02001883 ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001884 return 0;
1885
1886error:
Jiri Olsac60da222016-07-04 14:16:20 +02001887 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001888 return -1;
1889}
1890
1891static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001892 struct perf_header *ph, int fd,
1893 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001894{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001895 char *name;
1896 u32 pmu_num;
1897 u32 type;
1898 struct strbuf sb;
1899
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001900 if (do_read_u32(fd, ph, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001901 return -1;
1902
Namhyung Kima1ae5652012-09-24 17:14:59 +09001903 if (!pmu_num) {
1904 pr_debug("pmu mappings not available\n");
1905 return 0;
1906 }
1907
1908 ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001909 if (strbuf_init(&sb, 128) < 0)
1910 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001911
1912 while (pmu_num) {
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001913 if (do_read_u32(fd, ph, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001914 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001915
1916 name = do_read_string(fd, ph);
1917 if (!name)
1918 goto error;
1919
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001920 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
1921 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001922 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001923 if (strbuf_add(&sb, "", 1) < 0)
1924 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001925
Kan Liange0838e02015-09-10 11:03:05 -03001926 if (!strcmp(name, "msr"))
1927 ph->env.msr_pmu_type = type;
1928
Namhyung Kima1ae5652012-09-24 17:14:59 +09001929 free(name);
1930 pmu_num--;
1931 }
1932 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1933 return 0;
1934
1935error:
1936 strbuf_release(&sb);
1937 return -1;
1938}
1939
Namhyung Kima8bb5592013-01-22 18:09:31 +09001940static int process_group_desc(struct perf_file_section *section __maybe_unused,
1941 struct perf_header *ph, int fd,
1942 void *data __maybe_unused)
1943{
1944 size_t ret = -1;
1945 u32 i, nr, nr_groups;
1946 struct perf_session *session;
1947 struct perf_evsel *evsel, *leader = NULL;
1948 struct group_desc {
1949 char *name;
1950 u32 leader_idx;
1951 u32 nr_members;
1952 } *desc;
1953
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001954 if (do_read_u32(fd, ph, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001955 return -1;
1956
Namhyung Kima8bb5592013-01-22 18:09:31 +09001957 ph->env.nr_groups = nr_groups;
1958 if (!nr_groups) {
1959 pr_debug("group desc not available\n");
1960 return 0;
1961 }
1962
1963 desc = calloc(nr_groups, sizeof(*desc));
1964 if (!desc)
1965 return -1;
1966
1967 for (i = 0; i < nr_groups; i++) {
1968 desc[i].name = do_read_string(fd, ph);
1969 if (!desc[i].name)
1970 goto out_free;
1971
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001972 if (do_read_u32(fd, ph, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001973 goto out_free;
1974
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001975 if (do_read_u32(fd, ph, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001976 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001977 }
1978
1979 /*
1980 * Rebuild group relationship based on the group_desc
1981 */
1982 session = container_of(ph, struct perf_session, header);
1983 session->evlist->nr_groups = nr_groups;
1984
1985 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001986 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001987 if (evsel->idx == (int) desc[i].leader_idx) {
1988 evsel->leader = evsel;
1989 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09001990 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001991 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09001992 desc[i].name = NULL;
1993 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09001994 evsel->nr_members = desc[i].nr_members;
1995
1996 if (i >= nr_groups || nr > 0) {
1997 pr_debug("invalid group desc\n");
1998 goto out_free;
1999 }
2000
2001 leader = evsel;
2002 nr = evsel->nr_members - 1;
2003 i++;
2004 } else if (nr) {
2005 /* This is a group member */
2006 evsel->leader = leader;
2007
2008 nr--;
2009 }
2010 }
2011
2012 if (i != nr_groups || nr != 0) {
2013 pr_debug("invalid group desc\n");
2014 goto out_free;
2015 }
2016
2017 ret = 0;
2018out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002019 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002020 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002021 free(desc);
2022
2023 return ret;
2024}
2025
Adrian Hunter99fa2982015-04-30 17:37:25 +03002026static int process_auxtrace(struct perf_file_section *section,
2027 struct perf_header *ph, int fd,
2028 void *data __maybe_unused)
2029{
2030 struct perf_session *session;
2031 int err;
2032
2033 session = container_of(ph, struct perf_session, header);
2034
2035 err = auxtrace_index__process(fd, section->size, session,
2036 ph->needs_swap);
2037 if (err < 0)
2038 pr_err("Failed to process auxtrace index\n");
2039 return err;
2040}
2041
Jiri Olsa720e98b2016-02-16 16:01:43 +01002042static int process_cache(struct perf_file_section *section __maybe_unused,
2043 struct perf_header *ph __maybe_unused, int fd __maybe_unused,
2044 void *data __maybe_unused)
2045{
2046 struct cpu_cache_level *caches;
2047 u32 cnt, i, version;
2048
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002049 if (do_read_u32(fd, ph, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002050 return -1;
2051
Jiri Olsa720e98b2016-02-16 16:01:43 +01002052 if (version != 1)
2053 return -1;
2054
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002055 if (do_read_u32(fd, ph, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002056 return -1;
2057
Jiri Olsa720e98b2016-02-16 16:01:43 +01002058 caches = zalloc(sizeof(*caches) * cnt);
2059 if (!caches)
2060 return -1;
2061
2062 for (i = 0; i < cnt; i++) {
2063 struct cpu_cache_level c;
2064
2065 #define _R(v) \
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002066 if (do_read_u32(fd, ph, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002067 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002068
2069 _R(level)
2070 _R(line_size)
2071 _R(sets)
2072 _R(ways)
2073 #undef _R
2074
2075 #define _R(v) \
2076 c.v = do_read_string(fd, ph); \
2077 if (!c.v) \
2078 goto out_free_caches;
2079
2080 _R(type)
2081 _R(size)
2082 _R(map)
2083 #undef _R
2084
2085 caches[i] = c;
2086 }
2087
2088 ph->env.caches = caches;
2089 ph->env.caches_cnt = cnt;
2090 return 0;
2091out_free_caches:
2092 free(caches);
2093 return -1;
2094}
2095
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002096struct feature_ops {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002097 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002098 void (*print)(struct feat_fd *ff, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002099 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002100 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002101 const char *name;
2102 bool full_only;
2103};
2104
Robert Richter8cdfa782011-12-07 10:02:56 +01002105#define FEAT_OPA(n, func) \
2106 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002107#define FEAT_OPP(n, func) \
2108 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2109 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002110#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002111 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002112 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002113
2114/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002115#define print_tracing_data NULL
2116#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002117
2118static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002119 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002120 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002121 FEAT_OPP(HEADER_HOSTNAME, hostname),
2122 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2123 FEAT_OPP(HEADER_VERSION, version),
2124 FEAT_OPP(HEADER_ARCH, arch),
2125 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2126 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002127 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002128 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002129 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002130 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002131 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2132 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002133 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002134 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002135 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Adrian Hunter99fa2982015-04-30 17:37:25 +03002136 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
Jiri Olsaffa517a2015-10-25 15:51:43 +01002137 FEAT_OPA(HEADER_STAT, stat),
Jiri Olsa720e98b2016-02-16 16:01:43 +01002138 FEAT_OPF(HEADER_CACHE, cache),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002139};
2140
2141struct header_print_data {
2142 FILE *fp;
2143 bool full; /* extended list of headers */
2144};
2145
2146static int perf_file_section__fprintf_info(struct perf_file_section *section,
2147 struct perf_header *ph,
2148 int feat, int fd, void *data)
2149{
2150 struct header_print_data *hd = data;
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002151 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002152
2153 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2154 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2155 "%d, continuing...\n", section->offset, feat);
2156 return 0;
2157 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002158 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002159 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002160 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002161 }
2162 if (!feat_ops[feat].print)
2163 return 0;
2164
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002165 ff = (struct feat_fd) {
2166 .fd = fd,
2167 .ph = ph,
2168 };
2169
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002170 if (!feat_ops[feat].full_only || hd->full)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002171 feat_ops[feat].print(&ff, hd->fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002172 else
2173 fprintf(hd->fp, "# %s info available, use -I to display\n",
2174 feat_ops[feat].name);
2175
2176 return 0;
2177}
2178
2179int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2180{
2181 struct header_print_data hd;
2182 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002183 int fd = perf_data_file__fd(session->file);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002184 struct stat st;
Jiri Olsaaabae162016-10-10 09:35:50 +02002185 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002186
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002187 hd.fp = fp;
2188 hd.full = full;
2189
Jiri Olsaf45f5612016-10-10 09:03:07 +02002190 ret = fstat(fd, &st);
2191 if (ret == -1)
2192 return -1;
2193
2194 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2195
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002196 perf_header__process_sections(header, fd, &hd,
2197 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002198
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002199 if (session->file->is_pipe)
2200 return 0;
2201
Jiri Olsaaabae162016-10-10 09:35:50 +02002202 fprintf(fp, "# missing features: ");
2203 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2204 if (bit)
2205 fprintf(fp, "%s ", feat_ops[bit].name);
2206 }
2207
2208 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002209 return 0;
2210}
2211
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002212static int do_write_feat(struct feat_fd *ff, int type,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002213 struct perf_file_section **p,
2214 struct perf_evlist *evlist)
2215{
2216 int err;
2217 int ret = 0;
2218
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002219 if (perf_header__has_feat(ff->ph, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002220 if (!feat_ops[type].write)
2221 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002222
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002223 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002224
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002225 err = feat_ops[type].write(ff, evlist);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002226 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002227 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002228
2229 /* undo anything written */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002230 lseek(ff->fd, (*p)->offset, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002231
2232 return -1;
2233 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002234 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002235 (*p)++;
2236 }
2237 return ret;
2238}
2239
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002240static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002241 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002242{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002243 int nr_sections;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002244 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002245 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002246 int sec_size;
2247 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002248 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002249 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002250
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002251 ff = (struct feat_fd){
2252 .fd = fd,
2253 .ph = header,
2254 };
2255
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002256 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002257 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002258 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002259
Paul Gortmaker91b98802013-01-30 20:05:49 -05002260 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002261 if (feat_sec == NULL)
2262 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002263
2264 sec_size = sizeof(*feat_sec) * nr_sections;
2265
Jiri Olsa8d541e92013-07-17 19:49:44 +02002266 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002267 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002268
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002269 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002270 if (do_write_feat(&ff, feat, &p, evlist))
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002271 perf_header__clear_feat(header, feat);
2272 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002273
Xiao Guangrongf887f302010-02-04 16:46:42 +08002274 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002275 /*
2276 * may write more than needed due to dropped feature, but
2277 * this is okay, reader will skip the mising entries
2278 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002279 err = do_write(&ff, feat_sec, sec_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002280 if (err < 0)
2281 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002282 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002283 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002284}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002285
Tom Zanussi8dc58102010-04-01 23:59:15 -05002286int perf_header__write_pipe(int fd)
2287{
2288 struct perf_pipe_file_header f_header;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002289 struct feat_fd ff;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002290 int err;
2291
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002292 ff = (struct feat_fd){ .fd = fd };
2293
Tom Zanussi8dc58102010-04-01 23:59:15 -05002294 f_header = (struct perf_pipe_file_header){
2295 .magic = PERF_MAGIC,
2296 .size = sizeof(f_header),
2297 };
2298
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002299 err = do_write(&ff, &f_header, sizeof(f_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05002300 if (err < 0) {
2301 pr_debug("failed to write perf pipe header\n");
2302 return err;
2303 }
2304
2305 return 0;
2306}
2307
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002308int perf_session__write_header(struct perf_session *session,
2309 struct perf_evlist *evlist,
2310 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002311{
2312 struct perf_file_header f_header;
2313 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002314 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002315 struct perf_evsel *evsel;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002316 struct feat_fd ff;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002317 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002318 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002319
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002320 ff = (struct feat_fd){ .fd = fd};
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002321 lseek(fd, sizeof(f_header), SEEK_SET);
2322
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002323 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002324 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002325 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002326 if (err < 0) {
2327 pr_debug("failed to write perf header\n");
2328 return err;
2329 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002330 }
2331
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002332 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002333
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002334 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002335 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002336 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002337 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002338 .offset = evsel->id_offset,
2339 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002340 }
2341 };
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002342 err = do_write(&ff, &f_attr, sizeof(f_attr));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002343 if (err < 0) {
2344 pr_debug("failed to write perf header attribute\n");
2345 return err;
2346 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002347 }
2348
Adrian Hunterd645c442013-12-11 14:36:28 +02002349 if (!header->data_offset)
2350 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002351 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002352
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002353 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002354 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002355 if (err < 0)
2356 return err;
2357 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002358
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002359 f_header = (struct perf_file_header){
2360 .magic = PERF_MAGIC,
2361 .size = sizeof(f_header),
2362 .attr_size = sizeof(f_attr),
2363 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002364 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002365 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002366 },
2367 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002368 .offset = header->data_offset,
2369 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002370 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002371 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002372 };
2373
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002374 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002375
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002376 lseek(fd, 0, SEEK_SET);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002377 err = do_write(&ff, &f_header, sizeof(f_header));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002378 if (err < 0) {
2379 pr_debug("failed to write perf header\n");
2380 return err;
2381 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002382 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002383
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002384 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002385}
2386
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002387static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002388 int fd, void *buf, size_t size)
2389{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002390 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002391 return -1;
2392
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002393 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002394 mem_bswap_64(buf, size);
2395
2396 return 0;
2397}
2398
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002399int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002400 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002401 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002402 struct perf_header *ph,
2403 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002404{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002405 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002406 int nr_sections;
2407 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002408 int feat;
2409 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002410
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002411 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002412 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002413 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002414
Paul Gortmaker91b98802013-01-30 20:05:49 -05002415 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002416 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002417 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002418
2419 sec_size = sizeof(*feat_sec) * nr_sections;
2420
Jiri Olsa8d541e92013-07-17 19:49:44 +02002421 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002422
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002423 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2424 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002425 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002426
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002427 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2428 err = process(sec++, header, feat, fd, data);
2429 if (err < 0)
2430 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002431 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002432 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002433out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002434 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002435 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002436}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002437
Stephane Eranian114382a2012-02-09 23:21:08 +01002438static const int attr_file_abi_sizes[] = {
2439 [0] = PERF_ATTR_SIZE_VER0,
2440 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002441 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002442 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002443 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002444 0,
2445};
2446
2447/*
2448 * In the legacy file format, the magic number is not used to encode endianness.
2449 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2450 * on ABI revisions, we need to try all combinations for all endianness to
2451 * detect the endianness.
2452 */
2453static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2454{
2455 uint64_t ref_size, attr_size;
2456 int i;
2457
2458 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2459 ref_size = attr_file_abi_sizes[i]
2460 + sizeof(struct perf_file_section);
2461 if (hdr_sz != ref_size) {
2462 attr_size = bswap_64(hdr_sz);
2463 if (attr_size != ref_size)
2464 continue;
2465
2466 ph->needs_swap = true;
2467 }
2468 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2469 i,
2470 ph->needs_swap);
2471 return 0;
2472 }
2473 /* could not determine endianness */
2474 return -1;
2475}
2476
2477#define PERF_PIPE_HDR_VER0 16
2478
2479static const size_t attr_pipe_abi_sizes[] = {
2480 [0] = PERF_PIPE_HDR_VER0,
2481 0,
2482};
2483
2484/*
2485 * In the legacy pipe format, there is an implicit assumption that endiannesss
2486 * between host recording the samples, and host parsing the samples is the
2487 * same. This is not always the case given that the pipe output may always be
2488 * redirected into a file and analyzed on a different machine with possibly a
2489 * different endianness and perf_event ABI revsions in the perf tool itself.
2490 */
2491static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2492{
2493 u64 attr_size;
2494 int i;
2495
2496 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2497 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2498 attr_size = bswap_64(hdr_sz);
2499 if (attr_size != hdr_sz)
2500 continue;
2501
2502 ph->needs_swap = true;
2503 }
2504 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2505 return 0;
2506 }
2507 return -1;
2508}
2509
Feng Tange84ba4e2012-10-30 11:56:07 +08002510bool is_perf_magic(u64 magic)
2511{
2512 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2513 || magic == __perf_magic2
2514 || magic == __perf_magic2_sw)
2515 return true;
2516
2517 return false;
2518}
2519
Stephane Eranian114382a2012-02-09 23:21:08 +01002520static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2521 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002522{
2523 int ret;
2524
2525 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002526 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002527 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002528 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002529 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002530 if (is_pipe)
2531 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002532
Stephane Eranian114382a2012-02-09 23:21:08 +01002533 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002534 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002535 /*
2536 * the new magic number serves two purposes:
2537 * - unique number to identify actual perf.data files
2538 * - encode endianness of file
2539 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002540 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002541
Stephane Eranian114382a2012-02-09 23:21:08 +01002542 /* check magic number with one endianness */
2543 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002544 return 0;
2545
Stephane Eranian114382a2012-02-09 23:21:08 +01002546 /* check magic number with opposite endianness */
2547 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002548 return -1;
2549
2550 ph->needs_swap = true;
2551
2552 return 0;
2553}
2554
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002555int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002556 struct perf_header *ph, int fd)
2557{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002558 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002559
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002560 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002561
Stephane Eranian73323f52012-02-02 13:54:44 +01002562 ret = readn(fd, header, sizeof(*header));
2563 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002564 return -1;
2565
Stephane Eranian114382a2012-02-09 23:21:08 +01002566 if (check_magic_endian(header->magic,
2567 header->attr_size, false, ph) < 0) {
2568 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002569 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002570 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002571
Stephane Eranian73323f52012-02-02 13:54:44 +01002572 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002573 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002574 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002575 }
2576
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002577 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002578 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002579 if (header->size == offsetof(typeof(*header), adds_features))
2580 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002581 else
2582 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002583 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002584 /*
2585 * feature bitmap is declared as an array of unsigned longs --
2586 * not good since its size can differ between the host that
2587 * generated the data file and the host analyzing the file.
2588 *
2589 * We need to handle endianness, but we don't know the size of
2590 * the unsigned long where the file was generated. Take a best
2591 * guess at determining it: try 64-bit swap first (ie., file
2592 * created on a 64-bit host), and check if the hostname feature
2593 * bit is set (this feature bit is forced on as of fbe96f2).
2594 * If the bit is not, undo the 64-bit swap and try a 32-bit
2595 * swap. If the hostname bit is still not set (e.g., older data
2596 * file), punt and fallback to the original behavior --
2597 * clearing all feature bits and setting buildid.
2598 */
David Ahern80c01202012-06-08 11:47:51 -03002599 mem_bswap_64(&header->adds_features,
2600 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002601
2602 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002603 /* unswap as u64 */
2604 mem_bswap_64(&header->adds_features,
2605 BITS_TO_U64(HEADER_FEAT_BITS));
2606
2607 /* unswap as u32 */
2608 mem_bswap_32(&header->adds_features,
2609 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002610 }
2611
2612 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2613 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2614 set_bit(HEADER_BUILD_ID, header->adds_features);
2615 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002616 }
2617
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002618 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002619 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002620
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002621 ph->data_offset = header->data.offset;
2622 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002623 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002624 return 0;
2625}
2626
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002627static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002628 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002629 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002630{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002631 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002632 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002633 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002634 return 0;
2635 }
2636
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002637 if (feat >= HEADER_LAST_FEATURE) {
2638 pr_debug("unknown feature %d, continuing...\n", feat);
2639 return 0;
2640 }
2641
Robert Richterf1c67db2012-02-10 15:41:56 +01002642 if (!feat_ops[feat].process)
2643 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002644
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002645 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002646}
2647
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002648static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002649 struct perf_header *ph, int fd,
2650 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002651{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002652 struct feat_fd ff = {
2653 .fd = STDOUT_FILENO,
2654 .ph = ph,
2655 };
Jiri Olsa727ebd52013-11-28 11:30:14 +01002656 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002657
2658 ret = readn(fd, header, sizeof(*header));
2659 if (ret <= 0)
2660 return -1;
2661
Stephane Eranian114382a2012-02-09 23:21:08 +01002662 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2663 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002664 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002665 }
2666
2667 if (ph->needs_swap)
2668 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002669
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002670 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002671 return -1;
2672
Tom Zanussi8dc58102010-04-01 23:59:15 -05002673 return 0;
2674}
2675
Jiri Olsad4339562013-07-17 19:49:41 +02002676static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002677{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002678 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002679 struct perf_pipe_file_header f_header;
2680
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002681 if (perf_file_header__read_pipe(&f_header, header,
2682 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002683 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002684 pr_debug("incompatible file format\n");
2685 return -EINVAL;
2686 }
2687
Tom Zanussi8dc58102010-04-01 23:59:15 -05002688 return 0;
2689}
2690
Stephane Eranian69996df2012-02-09 23:21:06 +01002691static int read_attr(int fd, struct perf_header *ph,
2692 struct perf_file_attr *f_attr)
2693{
2694 struct perf_event_attr *attr = &f_attr->attr;
2695 size_t sz, left;
2696 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002697 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002698
2699 memset(f_attr, 0, sizeof(*f_attr));
2700
2701 /* read minimal guaranteed structure */
2702 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2703 if (ret <= 0) {
2704 pr_debug("cannot read %d bytes of header attr\n",
2705 PERF_ATTR_SIZE_VER0);
2706 return -1;
2707 }
2708
2709 /* on file perf_event_attr size */
2710 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002711
Stephane Eranian69996df2012-02-09 23:21:06 +01002712 if (ph->needs_swap)
2713 sz = bswap_32(sz);
2714
2715 if (sz == 0) {
2716 /* assume ABI0 */
2717 sz = PERF_ATTR_SIZE_VER0;
2718 } else if (sz > our_sz) {
2719 pr_debug("file uses a more recent and unsupported ABI"
2720 " (%zu bytes extra)\n", sz - our_sz);
2721 return -1;
2722 }
2723 /* what we have not yet read and that we know about */
2724 left = sz - PERF_ATTR_SIZE_VER0;
2725 if (left) {
2726 void *ptr = attr;
2727 ptr += PERF_ATTR_SIZE_VER0;
2728
2729 ret = readn(fd, ptr, left);
2730 }
2731 /* read perf_file_section, ids are read in caller */
2732 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2733
2734 return ret <= 0 ? -1 : 0;
2735}
2736
Namhyung Kim831394b2012-09-06 11:10:46 +09002737static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2738 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002739{
Namhyung Kim831394b2012-09-06 11:10:46 +09002740 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002741 char bf[128];
2742
Namhyung Kim831394b2012-09-06 11:10:46 +09002743 /* already prepared */
2744 if (evsel->tp_format)
2745 return 0;
2746
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002747 if (pevent == NULL) {
2748 pr_debug("broken or missing trace data\n");
2749 return -1;
2750 }
2751
Namhyung Kim831394b2012-09-06 11:10:46 +09002752 event = pevent_find_event(pevent, evsel->attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09002753 if (event == NULL) {
2754 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002755 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09002756 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002757
Namhyung Kim831394b2012-09-06 11:10:46 +09002758 if (!evsel->name) {
2759 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2760 evsel->name = strdup(bf);
2761 if (evsel->name == NULL)
2762 return -1;
2763 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002764
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002765 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002766 return 0;
2767}
2768
Namhyung Kim831394b2012-09-06 11:10:46 +09002769static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2770 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002771{
2772 struct perf_evsel *pos;
2773
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002774 evlist__for_each_entry(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002775 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2776 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002777 return -1;
2778 }
2779
2780 return 0;
2781}
2782
Jiri Olsad4339562013-07-17 19:49:41 +02002783int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002784{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002785 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002786 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002787 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002788 struct perf_file_attr f_attr;
2789 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002790 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002791 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002792
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002793 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002794 if (session->evlist == NULL)
2795 return -ENOMEM;
2796
Kan Liang2c071442015-08-28 05:48:05 -04002797 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03002798 session->machines.host.env = &header->env;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002799 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002800 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002801
Stephane Eranian69996df2012-02-09 23:21:06 +01002802 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002803 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002804
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002805 /*
2806 * Sanity check that perf.data was written cleanly; data size is
2807 * initialized to 0 and updated only if the on_exit function is run.
2808 * If data size is still 0 then the file contains only partial
2809 * information. Just warn user and process it as much as it can.
2810 */
2811 if (f_header.data.size == 0) {
2812 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2813 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002814 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002815 }
2816
Stephane Eranian69996df2012-02-09 23:21:06 +01002817 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002818 lseek(fd, f_header.attrs.offset, SEEK_SET);
2819
2820 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002821 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002822 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002823
Stephane Eranian69996df2012-02-09 23:21:06 +01002824 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002825 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002826
David Ahern1060ab82015-04-09 16:15:46 -04002827 if (header->needs_swap) {
2828 f_attr.ids.size = bswap_64(f_attr.ids.size);
2829 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002830 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002831 }
David Aherneda39132011-07-15 12:34:09 -06002832
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002833 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002834 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002835
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002836 if (evsel == NULL)
2837 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002838
2839 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002840 /*
2841 * Do it before so that if perf_evsel__alloc_id fails, this
2842 * entry gets purged too at perf_evlist__delete().
2843 */
2844 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002845
2846 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002847 /*
2848 * We don't have the cpu and thread maps on the header, so
2849 * for allocating the perf_sample_id table we fake 1 cpu and
2850 * hattr->ids threads.
2851 */
2852 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2853 goto out_delete_evlist;
2854
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002855 lseek(fd, f_attr.ids.offset, SEEK_SET);
2856
2857 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002858 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002859 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002860
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002861 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002862 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002863
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002864 lseek(fd, tmp, SEEK_SET);
2865 }
2866
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002867 symbol_conf.nr_events = nr_attrs;
2868
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002869 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002870 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002871
Namhyung Kim831394b2012-09-06 11:10:46 +09002872 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002873 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002874 goto out_delete_evlist;
2875
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002876 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002877out_errno:
2878 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002879
2880out_delete_evlist:
2881 perf_evlist__delete(session->evlist);
2882 session->evlist = NULL;
2883 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002884}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002885
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002886int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002887 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002888 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002889{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002890 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002891 size_t size;
2892 int err;
2893
2894 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002895 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002896 size += sizeof(struct perf_event_header);
2897 size += ids * sizeof(u64);
2898
2899 ev = malloc(size);
2900
Chris Samuelce47dc52010-11-13 13:35:06 +11002901 if (ev == NULL)
2902 return -ENOMEM;
2903
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002904 ev->attr.attr = *attr;
2905 memcpy(ev->attr.id, id, ids * sizeof(u64));
2906
2907 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002908 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002909
Robert Richterf4d83432012-08-16 21:10:17 +02002910 if (ev->attr.header.size == size)
2911 err = process(tool, ev, NULL, NULL);
2912 else
2913 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002914
2915 free(ev);
2916
2917 return err;
2918}
2919
Jiri Olsaa6e52812015-10-25 15:51:37 +01002920static struct event_update_event *
2921event_update_event__new(size_t size, u64 type, u64 id)
2922{
2923 struct event_update_event *ev;
2924
2925 size += sizeof(*ev);
2926 size = PERF_ALIGN(size, sizeof(u64));
2927
2928 ev = zalloc(size);
2929 if (ev) {
2930 ev->header.type = PERF_RECORD_EVENT_UPDATE;
2931 ev->header.size = (u16)size;
2932 ev->type = type;
2933 ev->id = id;
2934 }
2935 return ev;
2936}
2937
2938int
2939perf_event__synthesize_event_update_unit(struct perf_tool *tool,
2940 struct perf_evsel *evsel,
2941 perf_event__handler_t process)
2942{
2943 struct event_update_event *ev;
2944 size_t size = strlen(evsel->unit);
2945 int err;
2946
2947 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
2948 if (ev == NULL)
2949 return -ENOMEM;
2950
2951 strncpy(ev->data, evsel->unit, size);
2952 err = process(tool, (union perf_event *)ev, NULL, NULL);
2953 free(ev);
2954 return err;
2955}
2956
Jiri Olsadaeecbc2015-10-25 15:51:38 +01002957int
2958perf_event__synthesize_event_update_scale(struct perf_tool *tool,
2959 struct perf_evsel *evsel,
2960 perf_event__handler_t process)
2961{
2962 struct event_update_event *ev;
2963 struct event_update_event_scale *ev_data;
2964 int err;
2965
2966 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
2967 if (ev == NULL)
2968 return -ENOMEM;
2969
2970 ev_data = (struct event_update_event_scale *) ev->data;
2971 ev_data->scale = evsel->scale;
2972 err = process(tool, (union perf_event*) ev, NULL, NULL);
2973 free(ev);
2974 return err;
2975}
2976
Jiri Olsa802c9042015-10-25 15:51:39 +01002977int
2978perf_event__synthesize_event_update_name(struct perf_tool *tool,
2979 struct perf_evsel *evsel,
2980 perf_event__handler_t process)
2981{
2982 struct event_update_event *ev;
2983 size_t len = strlen(evsel->name);
2984 int err;
2985
2986 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
2987 if (ev == NULL)
2988 return -ENOMEM;
2989
2990 strncpy(ev->data, evsel->name, len);
2991 err = process(tool, (union perf_event*) ev, NULL, NULL);
2992 free(ev);
2993 return err;
2994}
Jiri Olsadaeecbc2015-10-25 15:51:38 +01002995
Jiri Olsa86ebb092015-10-25 15:51:40 +01002996int
2997perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
2998 struct perf_evsel *evsel,
2999 perf_event__handler_t process)
3000{
3001 size_t size = sizeof(struct event_update_event);
3002 struct event_update_event *ev;
3003 int max, err;
3004 u16 type;
3005
3006 if (!evsel->own_cpus)
3007 return 0;
3008
3009 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3010 if (!ev)
3011 return -ENOMEM;
3012
3013 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3014 ev->header.size = (u16)size;
3015 ev->type = PERF_EVENT_UPDATE__CPUS;
3016 ev->id = evsel->id[0];
3017
3018 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3019 evsel->own_cpus,
3020 type, max);
3021
3022 err = process(tool, (union perf_event*) ev, NULL, NULL);
3023 free(ev);
3024 return err;
3025}
3026
Jiri Olsac853f932015-10-25 15:51:41 +01003027size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3028{
3029 struct event_update_event *ev = &event->event_update;
3030 struct event_update_event_scale *ev_scale;
3031 struct event_update_event_cpus *ev_cpus;
3032 struct cpu_map *map;
3033 size_t ret;
3034
3035 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3036
3037 switch (ev->type) {
3038 case PERF_EVENT_UPDATE__SCALE:
3039 ev_scale = (struct event_update_event_scale *) ev->data;
3040 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3041 break;
3042 case PERF_EVENT_UPDATE__UNIT:
3043 ret += fprintf(fp, "... unit: %s\n", ev->data);
3044 break;
3045 case PERF_EVENT_UPDATE__NAME:
3046 ret += fprintf(fp, "... name: %s\n", ev->data);
3047 break;
3048 case PERF_EVENT_UPDATE__CPUS:
3049 ev_cpus = (struct event_update_event_cpus *) ev->data;
3050 ret += fprintf(fp, "... ");
3051
3052 map = cpu_map__new_data(&ev_cpus->cpus);
3053 if (map)
3054 ret += cpu_map__fprintf(map, fp);
3055 else
3056 ret += fprintf(fp, "failed to get cpus\n");
3057 break;
3058 default:
3059 ret += fprintf(fp, "... unknown type\n");
3060 break;
3061 }
3062
3063 return ret;
3064}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003065
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003066int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003067 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003068 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003069{
Robert Richter6606f872012-08-16 21:10:19 +02003070 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003071 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003072
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003073 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003074 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3075 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003076 if (err) {
3077 pr_debug("failed to create perf header attribute\n");
3078 return err;
3079 }
3080 }
3081
3082 return err;
3083}
3084
Adrian Hunter47c3d102013-07-04 16:20:21 +03003085int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3086 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003087 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003088{
Robert Richterf4d83432012-08-16 21:10:17 +02003089 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003090 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003091 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003092
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003093 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003094 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003095 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003096 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003097 }
3098
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003099 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003100 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003101 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003102
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003103 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003104
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003105 ids = event->header.size;
3106 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003107 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003108 /*
3109 * We don't have the cpu and thread maps on the header, so
3110 * for allocating the perf_sample_id table we fake 1 cpu and
3111 * hattr->ids threads.
3112 */
3113 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3114 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003115
3116 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003117 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003118 }
3119
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03003120 symbol_conf.nr_events = evlist->nr_entries;
3121
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003122 return 0;
3123}
Tom Zanussicd19a032010-04-01 23:59:20 -05003124
Jiri Olsaffe777252015-10-25 15:51:36 +01003125int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3126 union perf_event *event,
3127 struct perf_evlist **pevlist)
3128{
3129 struct event_update_event *ev = &event->event_update;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003130 struct event_update_event_scale *ev_scale;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003131 struct event_update_event_cpus *ev_cpus;
Jiri Olsaffe777252015-10-25 15:51:36 +01003132 struct perf_evlist *evlist;
3133 struct perf_evsel *evsel;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003134 struct cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003135
3136 if (!pevlist || *pevlist == NULL)
3137 return -EINVAL;
3138
3139 evlist = *pevlist;
3140
3141 evsel = perf_evlist__id2evsel(evlist, ev->id);
3142 if (evsel == NULL)
3143 return -EINVAL;
3144
Jiri Olsaa6e52812015-10-25 15:51:37 +01003145 switch (ev->type) {
3146 case PERF_EVENT_UPDATE__UNIT:
3147 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003148 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003149 case PERF_EVENT_UPDATE__NAME:
3150 evsel->name = strdup(ev->data);
3151 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003152 case PERF_EVENT_UPDATE__SCALE:
3153 ev_scale = (struct event_update_event_scale *) ev->data;
3154 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003155 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003156 case PERF_EVENT_UPDATE__CPUS:
3157 ev_cpus = (struct event_update_event_cpus *) ev->data;
3158
3159 map = cpu_map__new_data(&ev_cpus->cpus);
3160 if (map)
3161 evsel->own_cpus = map;
3162 else
3163 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003164 default:
3165 break;
3166 }
3167
Jiri Olsaffe777252015-10-25 15:51:36 +01003168 return 0;
3169}
3170
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003171int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003172 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003173 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003174{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003175 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003176 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003177 ssize_t size = 0, aligned_size = 0, padding;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003178 struct feat_fd ff;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003179 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003180
Jiri Olsa29208e52011-10-20 15:59:43 +02003181 /*
3182 * We are going to store the size of the data followed
3183 * by the data contents. Since the fd descriptor is a pipe,
3184 * we cannot seek back to store the size of the data once
3185 * we know it. Instead we:
3186 *
3187 * - write the tracing data to the temp file
3188 * - get/write the data size to pipe
3189 * - write the tracing data from the temp file
3190 * to the pipe
3191 */
3192 tdata = tracing_data_get(&evlist->entries, fd, true);
3193 if (!tdata)
3194 return -1;
3195
Tom Zanussi92155452010-04-01 23:59:21 -05003196 memset(&ev, 0, sizeof(ev));
3197
3198 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003199 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003200 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003201 padding = aligned_size - size;
3202 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3203 ev.tracing_data.size = aligned_size;
3204
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003205 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003206
Jiri Olsa29208e52011-10-20 15:59:43 +02003207 /*
3208 * The put function will copy all the tracing data
3209 * stored in temp file to the pipe.
3210 */
3211 tracing_data_put(tdata);
3212
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003213 ff = (struct feat_fd){ .fd = fd };
3214 if (write_padded(&ff, NULL, 0, padding))
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -07003215 return -1;
Tom Zanussi92155452010-04-01 23:59:21 -05003216
3217 return aligned_size;
3218}
3219
Adrian Hunter47c3d102013-07-04 16:20:21 +03003220int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3221 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003222 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05003223{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003224 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003225 int fd = perf_data_file__fd(session->file);
3226 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003227 char buf[BUFSIZ];
3228
3229 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003230 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003231 SEEK_SET);
3232
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003233 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003234 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003235 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003236
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003237 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003238 pr_err("%s: reading input file", __func__);
3239 return -1;
3240 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003241 if (session->repipe) {
3242 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003243 if (retw <= 0 || retw != padding) {
3244 pr_err("%s: repiping tracing data padding", __func__);
3245 return -1;
3246 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003247 }
Tom Zanussi92155452010-04-01 23:59:21 -05003248
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003249 if (size_read + padding != size) {
3250 pr_err("%s: tracing data size mismatch", __func__);
3251 return -1;
3252 }
Tom Zanussi92155452010-04-01 23:59:21 -05003253
Namhyung Kim831394b2012-09-06 11:10:46 +09003254 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003255 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003256
Tom Zanussi92155452010-04-01 23:59:21 -05003257 return size_read + padding;
3258}
Tom Zanussic7929e42010-04-01 23:59:22 -05003259
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003260int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003261 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003262 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003263 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003264{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003265 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003266 size_t len;
3267 int err = 0;
3268
3269 if (!pos->hit)
3270 return err;
3271
3272 memset(&ev, 0, sizeof(ev));
3273
3274 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003275 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003276 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3277 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3278 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003279 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003280 ev.build_id.header.size = sizeof(ev.build_id) + len;
3281 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3282
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003283 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003284
3285 return err;
3286}
3287
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003288int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003289 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003290 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003291{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003292 __event_process_build_id(&event->build_id,
3293 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003294 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003295 return 0;
3296}