Commit | Line | Data |
---|---|---|
a9072bc0 | 1 | #include "util.h" |
7c6a1c65 | 2 | #include <sys/types.h> |
ba21594c | 3 | #include <byteswap.h> |
7c6a1c65 PZ |
4 | #include <unistd.h> |
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
8671dab9 | 7 | #include <linux/list.h> |
ba21594c | 8 | #include <linux/kernel.h> |
b1e5a9be | 9 | #include <linux/bitops.h> |
fbe96f29 | 10 | #include <sys/utsname.h> |
7c6a1c65 | 11 | |
361c99a6 | 12 | #include "evlist.h" |
a91e5431 | 13 | #include "evsel.h" |
7c6a1c65 | 14 | #include "header.h" |
03456a15 FW |
15 | #include "../perf.h" |
16 | #include "trace-event.h" | |
301a0b02 | 17 | #include "session.h" |
8671dab9 | 18 | #include "symbol.h" |
4778d2e4 | 19 | #include "debug.h" |
fbe96f29 | 20 | #include "cpumap.h" |
50a9667c | 21 | #include "pmu.h" |
7dbf4dcf | 22 | #include "vdso.h" |
a1ae5655 | 23 | #include "strbuf.h" |
ebb296c2 | 24 | #include "build-id.h" |
cc9784bd | 25 | #include "data.h" |
7c6a1c65 | 26 | |
73323f54 SE |
27 | /* |
28 | * magic2 = "PERFILE2" | |
29 | * must be a numerical value to let the endianness | |
30 | * determine the memory layout. That way we are able | |
31 | * to detect endianness when reading the perf.data file | |
32 | * back. | |
33 | * | |
34 | * we check for legacy (PERFFILE) format. | |
35 | */ | |
36 | static const char *__perf_magic1 = "PERFFILE"; | |
37 | static const u64 __perf_magic2 = 0x32454c4946524550ULL; | |
38 | static const u64 __perf_magic2_sw = 0x50455246494c4532ULL; | |
7c6a1c65 | 39 | |
73323f54 | 40 | #define PERF_MAGIC __perf_magic2 |
7c6a1c65 | 41 | |
7c6a1c65 | 42 | struct perf_file_attr { |
cdd6c482 | 43 | struct perf_event_attr attr; |
7c6a1c65 PZ |
44 | struct perf_file_section ids; |
45 | }; | |
46 | ||
1c0b04d1 | 47 | void perf_header__set_feat(struct perf_header *header, int feat) |
8d06367f | 48 | { |
1c0b04d1 | 49 | set_bit(feat, header->adds_features); |
8d06367f ACM |
50 | } |
51 | ||
1c0b04d1 | 52 | void perf_header__clear_feat(struct perf_header *header, int feat) |
baa2f6ce | 53 | { |
1c0b04d1 | 54 | clear_bit(feat, header->adds_features); |
baa2f6ce ACM |
55 | } |
56 | ||
1c0b04d1 | 57 | bool perf_header__has_feat(const struct perf_header *header, int feat) |
8d06367f | 58 | { |
1c0b04d1 | 59 | return test_bit(feat, header->adds_features); |
8d06367f ACM |
60 | } |
61 | ||
3726cc75 | 62 | static int do_write(int fd, const void *buf, size_t size) |
7c6a1c65 PZ |
63 | { |
64 | while (size) { | |
65 | int ret = write(fd, buf, size); | |
66 | ||
67 | if (ret < 0) | |
d5eed904 | 68 | return -errno; |
7c6a1c65 PZ |
69 | |
70 | size -= ret; | |
71 | buf += ret; | |
72 | } | |
3726cc75 ACM |
73 | |
74 | return 0; | |
7c6a1c65 PZ |
75 | } |
76 | ||
e195fac8 | 77 | int write_padded(int fd, const void *bf, size_t count, size_t count_aligned) |
f92cb24c ACM |
78 | { |
79 | static const char zero_buf[NAME_ALIGN]; | |
80 | int err = do_write(fd, bf, count); | |
81 | ||
82 | if (!err) | |
83 | err = do_write(fd, zero_buf, count_aligned - count); | |
84 | ||
85 | return err; | |
86 | } | |
87 | ||
2bb00d2f KL |
88 | #define string_size(str) \ |
89 | (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32)) | |
90 | ||
fbe96f29 SE |
91 | static int do_write_string(int fd, const char *str) |
92 | { | |
93 | u32 len, olen; | |
94 | int ret; | |
95 | ||
96 | olen = strlen(str) + 1; | |
9ac3e487 | 97 | len = PERF_ALIGN(olen, NAME_ALIGN); |
fbe96f29 SE |
98 | |
99 | /* write len, incl. \0 */ | |
100 | ret = do_write(fd, &len, sizeof(len)); | |
101 | if (ret < 0) | |
102 | return ret; | |
103 | ||
104 | return write_padded(fd, str, olen, len); | |
105 | } | |
106 | ||
107 | static char *do_read_string(int fd, struct perf_header *ph) | |
108 | { | |
109 | ssize_t sz, ret; | |
110 | u32 len; | |
111 | char *buf; | |
112 | ||
5323f60c | 113 | sz = readn(fd, &len, sizeof(len)); |
fbe96f29 SE |
114 | if (sz < (ssize_t)sizeof(len)) |
115 | return NULL; | |
116 | ||
117 | if (ph->needs_swap) | |
118 | len = bswap_32(len); | |
119 | ||
120 | buf = malloc(len); | |
121 | if (!buf) | |
122 | return NULL; | |
123 | ||
5323f60c | 124 | ret = readn(fd, buf, len); |
fbe96f29 SE |
125 | if (ret == (ssize_t)len) { |
126 | /* | |
127 | * strings are padded by zeroes | |
128 | * thus the actual strlen of buf | |
129 | * may be less than len | |
130 | */ | |
131 | return buf; | |
132 | } | |
133 | ||
134 | free(buf); | |
135 | return NULL; | |
136 | } | |
137 | ||
1d037ca1 | 138 | static int write_tracing_data(int fd, struct perf_header *h __maybe_unused, |
fbe96f29 SE |
139 | struct perf_evlist *evlist) |
140 | { | |
141 | return read_tracing_data(fd, &evlist->entries); | |
142 | } | |
143 | ||
144 | ||
145 | static int write_build_id(int fd, struct perf_header *h, | |
1d037ca1 | 146 | struct perf_evlist *evlist __maybe_unused) |
fbe96f29 SE |
147 | { |
148 | struct perf_session *session; | |
149 | int err; | |
150 | ||
151 | session = container_of(h, struct perf_session, header); | |
152 | ||
e20960c0 RR |
153 | if (!perf_session__read_build_ids(session, true)) |
154 | return -1; | |
155 | ||
714c9c4a | 156 | err = perf_session__write_buildid_table(session, fd); |
fbe96f29 SE |
157 | if (err < 0) { |
158 | pr_debug("failed to write buildid table\n"); | |
159 | return err; | |
160 | } | |
73c5d224 | 161 | perf_session__cache_build_ids(session); |
fbe96f29 SE |
162 | |
163 | return 0; | |
164 | } | |
165 | ||
1d037ca1 IT |
166 | static int write_hostname(int fd, struct perf_header *h __maybe_unused, |
167 | struct perf_evlist *evlist __maybe_unused) | |
fbe96f29 SE |
168 | { |
169 | struct utsname uts; | |
170 | int ret; | |
171 | ||
172 | ret = uname(&uts); | |
173 | if (ret < 0) | |
174 | return -1; | |
175 | ||
176 | return do_write_string(fd, uts.nodename); | |
177 | } | |
178 | ||
1d037ca1 IT |
179 | static int write_osrelease(int fd, struct perf_header *h __maybe_unused, |
180 | struct perf_evlist *evlist __maybe_unused) | |
fbe96f29 SE |
181 | { |
182 | struct utsname uts; | |
183 | int ret; | |
184 | ||
185 | ret = uname(&uts); | |
186 | if (ret < 0) | |
187 | return -1; | |
188 | ||
189 | return do_write_string(fd, uts.release); | |
190 | } | |
191 | ||
1d037ca1 IT |
192 | static int write_arch(int fd, struct perf_header *h __maybe_unused, |
193 | struct perf_evlist *evlist __maybe_unused) | |
fbe96f29 SE |
194 | { |
195 | struct utsname uts; | |
196 | int ret; | |
197 | ||
198 | ret = uname(&uts); | |
199 | if (ret < 0) | |
200 | return -1; | |
201 | ||
202 | return do_write_string(fd, uts.machine); | |
203 | } | |
204 | ||
1d037ca1 IT |
205 | static int write_version(int fd, struct perf_header *h __maybe_unused, |
206 | struct perf_evlist *evlist __maybe_unused) | |
fbe96f29 SE |
207 | { |
208 | return do_write_string(fd, perf_version_string); | |
209 | } | |
210 | ||
493c3031 | 211 | static int __write_cpudesc(int fd, const char *cpuinfo_proc) |
fbe96f29 | 212 | { |
fbe96f29 SE |
213 | FILE *file; |
214 | char *buf = NULL; | |
215 | char *s, *p; | |
493c3031 | 216 | const char *search = cpuinfo_proc; |
fbe96f29 SE |
217 | size_t len = 0; |
218 | int ret = -1; | |
219 | ||
220 | if (!search) | |
221 | return -1; | |
222 | ||
223 | file = fopen("/proc/cpuinfo", "r"); | |
224 | if (!file) | |
225 | return -1; | |
226 | ||
227 | while (getline(&buf, &len, file) > 0) { | |
228 | ret = strncmp(buf, search, strlen(search)); | |
229 | if (!ret) | |
230 | break; | |
231 | } | |
232 | ||
ed307758 WN |
233 | if (ret) { |
234 | ret = -1; | |
fbe96f29 | 235 | goto done; |
ed307758 | 236 | } |
fbe96f29 SE |
237 | |
238 | s = buf; | |
239 | ||
240 | p = strchr(buf, ':'); | |
241 | if (p && *(p+1) == ' ' && *(p+2)) | |
242 | s = p + 2; | |
243 | p = strchr(s, '\n'); | |
244 | if (p) | |
245 | *p = '\0'; | |
246 | ||
247 | /* squash extra space characters (branding string) */ | |
248 | p = s; | |
249 | while (*p) { | |
250 | if (isspace(*p)) { | |
251 | char *r = p + 1; | |
252 | char *q = r; | |
253 | *p = ' '; | |
254 | while (*q && isspace(*q)) | |
255 | q++; | |
256 | if (q != (p+1)) | |
257 | while ((*r++ = *q++)); | |
258 | } | |
259 | p++; | |
260 | } | |
261 | ret = do_write_string(fd, s); | |
262 | done: | |
263 | free(buf); | |
264 | fclose(file); | |
265 | return ret; | |
266 | } | |
267 | ||
493c3031 WN |
268 | static int write_cpudesc(int fd, struct perf_header *h __maybe_unused, |
269 | struct perf_evlist *evlist __maybe_unused) | |
270 | { | |
271 | #ifndef CPUINFO_PROC | |
272 | #define CPUINFO_PROC {"model name", } | |
273 | #endif | |
274 | const char *cpuinfo_procs[] = CPUINFO_PROC; | |
275 | unsigned int i; | |
276 | ||
277 | for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) { | |
278 | int ret; | |
279 | ret = __write_cpudesc(fd, cpuinfo_procs[i]); | |
280 | if (ret >= 0) | |
281 | return ret; | |
282 | } | |
283 | return -1; | |
284 | } | |
285 | ||
286 | ||
1d037ca1 IT |
287 | static int write_nrcpus(int fd, struct perf_header *h __maybe_unused, |
288 | struct perf_evlist *evlist __maybe_unused) | |
fbe96f29 SE |
289 | { |
290 | long nr; | |
291 | u32 nrc, nra; | |
292 | int ret; | |
293 | ||
294 | nr = sysconf(_SC_NPROCESSORS_CONF); | |
295 | if (nr < 0) | |
296 | return -1; | |
297 | ||
298 | nrc = (u32)(nr & UINT_MAX); | |
299 | ||
300 | nr = sysconf(_SC_NPROCESSORS_ONLN); | |
301 | if (nr < 0) | |
302 | return -1; | |
303 | ||
304 | nra = (u32)(nr & UINT_MAX); | |
305 | ||
306 | ret = do_write(fd, &nrc, sizeof(nrc)); | |
307 | if (ret < 0) | |
308 | return ret; | |
309 | ||
310 | return do_write(fd, &nra, sizeof(nra)); | |
311 | } | |
312 | ||
1d037ca1 | 313 | static int write_event_desc(int fd, struct perf_header *h __maybe_unused, |
fbe96f29 SE |
314 | struct perf_evlist *evlist) |
315 | { | |
6606f873 | 316 | struct perf_evsel *evsel; |
74ba9e11 | 317 | u32 nre, nri, sz; |
fbe96f29 SE |
318 | int ret; |
319 | ||
74ba9e11 | 320 | nre = evlist->nr_entries; |
fbe96f29 SE |
321 | |
322 | /* | |
323 | * write number of events | |
324 | */ | |
325 | ret = do_write(fd, &nre, sizeof(nre)); | |
326 | if (ret < 0) | |
327 | return ret; | |
328 | ||
329 | /* | |
330 | * size of perf_event_attr struct | |
331 | */ | |
6606f873 | 332 | sz = (u32)sizeof(evsel->attr); |
fbe96f29 SE |
333 | ret = do_write(fd, &sz, sizeof(sz)); |
334 | if (ret < 0) | |
335 | return ret; | |
336 | ||
0050f7aa | 337 | evlist__for_each(evlist, evsel) { |
6606f873 | 338 | ret = do_write(fd, &evsel->attr, sz); |
fbe96f29 SE |
339 | if (ret < 0) |
340 | return ret; | |
341 | /* | |
342 | * write number of unique id per event | |
343 | * there is one id per instance of an event | |
344 | * | |
345 | * copy into an nri to be independent of the | |
346 | * type of ids, | |
347 | */ | |
6606f873 | 348 | nri = evsel->ids; |
fbe96f29 SE |
349 | ret = do_write(fd, &nri, sizeof(nri)); |
350 | if (ret < 0) | |
351 | return ret; | |
352 | ||
353 | /* | |
354 | * write event string as passed on cmdline | |
355 | */ | |
6606f873 | 356 | ret = do_write_string(fd, perf_evsel__name(evsel)); |
fbe96f29 SE |
357 | if (ret < 0) |
358 | return ret; | |
359 | /* | |
360 | * write unique ids for this event | |
361 | */ | |
6606f873 | 362 | ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64)); |
fbe96f29 SE |
363 | if (ret < 0) |
364 | return ret; | |
365 | } | |
366 | return 0; | |
367 | } | |
368 | ||
1d037ca1 IT |
369 | static int write_cmdline(int fd, struct perf_header *h __maybe_unused, |
370 | struct perf_evlist *evlist __maybe_unused) | |
fbe96f29 SE |
371 | { |
372 | char buf[MAXPATHLEN]; | |
373 | char proc[32]; | |
b6998692 ACM |
374 | u32 n; |
375 | int i, ret; | |
fbe96f29 SE |
376 | |
377 | /* | |
378 | * actual atual path to perf binary | |
379 | */ | |
380 | sprintf(proc, "/proc/%d/exe", getpid()); | |
381 | ret = readlink(proc, buf, sizeof(buf)); | |
382 | if (ret <= 0) | |
383 | return -1; | |
384 | ||
385 | /* readlink() does not add null termination */ | |
386 | buf[ret] = '\0'; | |
387 | ||
388 | /* account for binary path */ | |
b6998692 | 389 | n = perf_env.nr_cmdline + 1; |
fbe96f29 SE |
390 | |
391 | ret = do_write(fd, &n, sizeof(n)); | |
392 | if (ret < 0) | |
393 | return ret; | |
394 | ||
395 | ret = do_write_string(fd, buf); | |
396 | if (ret < 0) | |
397 | return ret; | |
398 | ||
b6998692 ACM |
399 | for (i = 0 ; i < perf_env.nr_cmdline; i++) { |
400 | ret = do_write_string(fd, perf_env.cmdline_argv[i]); | |
fbe96f29 SE |
401 | if (ret < 0) |
402 | return ret; | |
403 | } | |
404 | return 0; | |
405 | } | |
406 | ||
407 | #define CORE_SIB_FMT \ | |
408 | "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list" | |
409 | #define THRD_SIB_FMT \ | |
410 | "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list" | |
411 | ||
412 | struct cpu_topo { | |
2bb00d2f | 413 | u32 cpu_nr; |
fbe96f29 SE |
414 | u32 core_sib; |
415 | u32 thread_sib; | |
416 | char **core_siblings; | |
417 | char **thread_siblings; | |
2bb00d2f KL |
418 | int *core_id; |
419 | int *phy_pkg_id; | |
fbe96f29 SE |
420 | }; |
421 | ||
422 | static int build_cpu_topo(struct cpu_topo *tp, int cpu) | |
423 | { | |
424 | FILE *fp; | |
425 | char filename[MAXPATHLEN]; | |
426 | char *buf = NULL, *p; | |
427 | size_t len = 0; | |
c5885749 | 428 | ssize_t sret; |
fbe96f29 SE |
429 | u32 i = 0; |
430 | int ret = -1; | |
431 | ||
432 | sprintf(filename, CORE_SIB_FMT, cpu); | |
433 | fp = fopen(filename, "r"); | |
434 | if (!fp) | |
c5885749 | 435 | goto try_threads; |
fbe96f29 | 436 | |
c5885749 | 437 | sret = getline(&buf, &len, fp); |
fbe96f29 | 438 | fclose(fp); |
c5885749 SE |
439 | if (sret <= 0) |
440 | goto try_threads; | |
fbe96f29 SE |
441 | |
442 | p = strchr(buf, '\n'); | |
443 | if (p) | |
444 | *p = '\0'; | |
445 | ||
446 | for (i = 0; i < tp->core_sib; i++) { | |
447 | if (!strcmp(buf, tp->core_siblings[i])) | |
448 | break; | |
449 | } | |
450 | if (i == tp->core_sib) { | |
451 | tp->core_siblings[i] = buf; | |
452 | tp->core_sib++; | |
453 | buf = NULL; | |
454 | len = 0; | |
455 | } | |
c5885749 | 456 | ret = 0; |
fbe96f29 | 457 | |
c5885749 | 458 | try_threads: |
fbe96f29 SE |
459 | sprintf(filename, THRD_SIB_FMT, cpu); |
460 | fp = fopen(filename, "r"); | |
461 | if (!fp) | |
462 | goto done; | |
463 | ||
464 | if (getline(&buf, &len, fp) <= 0) | |
465 | goto done; | |
466 | ||
467 | p = strchr(buf, '\n'); | |
468 | if (p) | |
469 | *p = '\0'; | |
470 | ||
471 | for (i = 0; i < tp->thread_sib; i++) { | |
472 | if (!strcmp(buf, tp->thread_siblings[i])) | |
473 | break; | |
474 | } | |
475 | if (i == tp->thread_sib) { | |
476 | tp->thread_siblings[i] = buf; | |
477 | tp->thread_sib++; | |
478 | buf = NULL; | |
479 | } | |
480 | ret = 0; | |
481 | done: | |
2bb00d2f KL |
482 | tp->core_id[cpu] = cpu_map__get_core_id(cpu); |
483 | tp->phy_pkg_id[cpu] = cpu_map__get_socket_id(cpu); | |
484 | ||
fbe96f29 SE |
485 | if(fp) |
486 | fclose(fp); | |
487 | free(buf); | |
488 | return ret; | |
489 | } | |
490 | ||
491 | static void free_cpu_topo(struct cpu_topo *tp) | |
492 | { | |
493 | u32 i; | |
494 | ||
495 | if (!tp) | |
496 | return; | |
497 | ||
498 | for (i = 0 ; i < tp->core_sib; i++) | |
74cf249d | 499 | zfree(&tp->core_siblings[i]); |
fbe96f29 SE |
500 | |
501 | for (i = 0 ; i < tp->thread_sib; i++) | |
74cf249d | 502 | zfree(&tp->thread_siblings[i]); |
fbe96f29 SE |
503 | |
504 | free(tp); | |
505 | } | |
506 | ||
507 | static struct cpu_topo *build_cpu_topology(void) | |
508 | { | |
509 | struct cpu_topo *tp; | |
510 | void *addr; | |
511 | u32 nr, i; | |
2bb00d2f | 512 | size_t sz, sz_id; |
fbe96f29 SE |
513 | long ncpus; |
514 | int ret = -1; | |
515 | ||
516 | ncpus = sysconf(_SC_NPROCESSORS_CONF); | |
517 | if (ncpus < 0) | |
518 | return NULL; | |
519 | ||
520 | nr = (u32)(ncpus & UINT_MAX); | |
521 | ||
522 | sz = nr * sizeof(char *); | |
2bb00d2f | 523 | sz_id = nr * sizeof(int); |
fbe96f29 | 524 | |
2bb00d2f | 525 | addr = calloc(1, sizeof(*tp) + 2 * sz + 2 * sz_id); |
fbe96f29 SE |
526 | if (!addr) |
527 | return NULL; | |
528 | ||
529 | tp = addr; | |
2bb00d2f | 530 | tp->cpu_nr = nr; |
fbe96f29 SE |
531 | addr += sizeof(*tp); |
532 | tp->core_siblings = addr; | |
533 | addr += sz; | |
534 | tp->thread_siblings = addr; | |
2bb00d2f KL |
535 | addr += sz; |
536 | tp->core_id = addr; | |
537 | addr += sz_id; | |
538 | tp->phy_pkg_id = addr; | |
fbe96f29 SE |
539 | |
540 | for (i = 0; i < nr; i++) { | |
541 | ret = build_cpu_topo(tp, i); | |
542 | if (ret < 0) | |
543 | break; | |
544 | } | |
545 | if (ret) { | |
546 | free_cpu_topo(tp); | |
547 | tp = NULL; | |
548 | } | |
549 | return tp; | |
550 | } | |
551 | ||
1d037ca1 IT |
552 | static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused, |
553 | struct perf_evlist *evlist __maybe_unused) | |
fbe96f29 SE |
554 | { |
555 | struct cpu_topo *tp; | |
556 | u32 i; | |
557 | int ret; | |
558 | ||
559 | tp = build_cpu_topology(); | |
560 | if (!tp) | |
561 | return -1; | |
562 | ||
563 | ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib)); | |
564 | if (ret < 0) | |
565 | goto done; | |
566 | ||
567 | for (i = 0; i < tp->core_sib; i++) { | |
568 | ret = do_write_string(fd, tp->core_siblings[i]); | |
569 | if (ret < 0) | |
570 | goto done; | |
571 | } | |
572 | ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib)); | |
573 | if (ret < 0) | |
574 | goto done; | |
575 | ||
576 | for (i = 0; i < tp->thread_sib; i++) { | |
577 | ret = do_write_string(fd, tp->thread_siblings[i]); | |
578 | if (ret < 0) | |
579 | break; | |
580 | } | |
2bb00d2f KL |
581 | |
582 | for (i = 0; i < tp->cpu_nr; i++) { | |
583 | ret = do_write(fd, &tp->core_id[i], sizeof(int)); | |
584 | if (ret < 0) | |
585 | return ret; | |
586 | ret = do_write(fd, &tp->phy_pkg_id[i], sizeof(int)); | |
587 | if (ret < 0) | |
588 | return ret; | |
589 | } | |
fbe96f29 SE |
590 | done: |
591 | free_cpu_topo(tp); | |
592 | return ret; | |
593 | } | |
594 | ||
595 | ||
596 | ||
1d037ca1 IT |
597 | static int write_total_mem(int fd, struct perf_header *h __maybe_unused, |
598 | struct perf_evlist *evlist __maybe_unused) | |
fbe96f29 SE |
599 | { |
600 | char *buf = NULL; | |
601 | FILE *fp; | |
602 | size_t len = 0; | |
603 | int ret = -1, n; | |
604 | uint64_t mem; | |
605 | ||
606 | fp = fopen("/proc/meminfo", "r"); | |
607 | if (!fp) | |
608 | return -1; | |
609 | ||
610 | while (getline(&buf, &len, fp) > 0) { | |
611 | ret = strncmp(buf, "MemTotal:", 9); | |
612 | if (!ret) | |
613 | break; | |
614 | } | |
615 | if (!ret) { | |
616 | n = sscanf(buf, "%*s %"PRIu64, &mem); | |
617 | if (n == 1) | |
618 | ret = do_write(fd, &mem, sizeof(mem)); | |
ed307758 WN |
619 | } else |
620 | ret = -1; | |
fbe96f29 SE |
621 | free(buf); |
622 | fclose(fp); | |
623 | return ret; | |
624 | } | |
625 | ||
626 | static int write_topo_node(int fd, int node) | |
627 | { | |
628 | char str[MAXPATHLEN]; | |
629 | char field[32]; | |
630 | char *buf = NULL, *p; | |
631 | size_t len = 0; | |
632 | FILE *fp; | |
633 | u64 mem_total, mem_free, mem; | |
634 | int ret = -1; | |
635 | ||
636 | sprintf(str, "/sys/devices/system/node/node%d/meminfo", node); | |
637 | fp = fopen(str, "r"); | |
638 | if (!fp) | |
639 | return -1; | |
640 | ||
641 | while (getline(&buf, &len, fp) > 0) { | |
642 | /* skip over invalid lines */ | |
643 | if (!strchr(buf, ':')) | |
644 | continue; | |
a761a2d8 | 645 | if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2) |
fbe96f29 SE |
646 | goto done; |
647 | if (!strcmp(field, "MemTotal:")) | |
648 | mem_total = mem; | |
649 | if (!strcmp(field, "MemFree:")) | |
650 | mem_free = mem; | |
651 | } | |
652 | ||
653 | fclose(fp); | |
5809fde0 | 654 | fp = NULL; |
fbe96f29 SE |
655 | |
656 | ret = do_write(fd, &mem_total, sizeof(u64)); | |
657 | if (ret) | |
658 | goto done; | |
659 | ||
660 | ret = do_write(fd, &mem_free, sizeof(u64)); | |
661 | if (ret) | |
662 | goto done; | |
663 | ||
664 | ret = -1; | |
665 | sprintf(str, "/sys/devices/system/node/node%d/cpulist", node); | |
666 | ||
667 | fp = fopen(str, "r"); | |
668 | if (!fp) | |
669 | goto done; | |
670 | ||
671 | if (getline(&buf, &len, fp) <= 0) | |
672 | goto done; | |
673 | ||
674 | p = strchr(buf, '\n'); | |
675 | if (p) | |
676 | *p = '\0'; | |
677 | ||
678 | ret = do_write_string(fd, buf); | |
679 | done: | |
680 | free(buf); | |
5809fde0 TJ |
681 | if (fp) |
682 | fclose(fp); | |
fbe96f29 SE |
683 | return ret; |
684 | } | |
685 | ||
1d037ca1 IT |
686 | static int write_numa_topology(int fd, struct perf_header *h __maybe_unused, |
687 | struct perf_evlist *evlist __maybe_unused) | |
fbe96f29 SE |
688 | { |
689 | char *buf = NULL; | |
690 | size_t len = 0; | |
691 | FILE *fp; | |
692 | struct cpu_map *node_map = NULL; | |
693 | char *c; | |
694 | u32 nr, i, j; | |
695 | int ret = -1; | |
696 | ||
697 | fp = fopen("/sys/devices/system/node/online", "r"); | |
698 | if (!fp) | |
699 | return -1; | |
700 | ||
701 | if (getline(&buf, &len, fp) <= 0) | |
702 | goto done; | |
703 | ||
704 | c = strchr(buf, '\n'); | |
705 | if (c) | |
706 | *c = '\0'; | |
707 | ||
708 | node_map = cpu_map__new(buf); | |
709 | if (!node_map) | |
710 | goto done; | |
711 | ||
712 | nr = (u32)node_map->nr; | |
713 | ||
714 | ret = do_write(fd, &nr, sizeof(nr)); | |
715 | if (ret < 0) | |
716 | goto done; | |
717 | ||
718 | for (i = 0; i < nr; i++) { | |
719 | j = (u32)node_map->map[i]; | |
720 | ret = do_write(fd, &j, sizeof(j)); | |
721 | if (ret < 0) | |
722 | break; | |
723 | ||
724 | ret = write_topo_node(fd, i); | |
725 | if (ret < 0) | |
726 | break; | |
727 | } | |
728 | done: | |
729 | free(buf); | |
730 | fclose(fp); | |
731 | free(node_map); | |
732 | return ret; | |
733 | } | |
734 | ||
50a9667c RR |
735 | /* |
736 | * File format: | |
737 | * | |
738 | * struct pmu_mappings { | |
739 | * u32 pmu_num; | |
740 | * struct pmu_map { | |
741 | * u32 type; | |
742 | * char name[]; | |
743 | * }[pmu_num]; | |
744 | * }; | |
745 | */ | |
746 | ||
1d037ca1 IT |
747 | static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused, |
748 | struct perf_evlist *evlist __maybe_unused) | |
50a9667c RR |
749 | { |
750 | struct perf_pmu *pmu = NULL; | |
751 | off_t offset = lseek(fd, 0, SEEK_CUR); | |
752 | __u32 pmu_num = 0; | |
5323f60c | 753 | int ret; |
50a9667c RR |
754 | |
755 | /* write real pmu_num later */ | |
5323f60c NK |
756 | ret = do_write(fd, &pmu_num, sizeof(pmu_num)); |
757 | if (ret < 0) | |
758 | return ret; | |
50a9667c RR |
759 | |
760 | while ((pmu = perf_pmu__scan(pmu))) { | |
761 | if (!pmu->name) | |
762 | continue; | |
763 | pmu_num++; | |
5323f60c NK |
764 | |
765 | ret = do_write(fd, &pmu->type, sizeof(pmu->type)); | |
766 | if (ret < 0) | |
767 | return ret; | |
768 | ||
769 | ret = do_write_string(fd, pmu->name); | |
770 | if (ret < 0) | |
771 | return ret; | |
50a9667c RR |
772 | } |
773 | ||
774 | if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) { | |
775 | /* discard all */ | |
776 | lseek(fd, offset, SEEK_SET); | |
777 | return -1; | |
778 | } | |
779 | ||
780 | return 0; | |
781 | } | |
782 | ||
a8bb559b NK |
783 | /* |
784 | * File format: | |
785 | * | |
786 | * struct group_descs { | |
787 | * u32 nr_groups; | |
788 | * struct group_desc { | |
789 | * char name[]; | |
790 | * u32 leader_idx; | |
791 | * u32 nr_members; | |
792 | * }[nr_groups]; | |
793 | * }; | |
794 | */ | |
795 | static int write_group_desc(int fd, struct perf_header *h __maybe_unused, | |
796 | struct perf_evlist *evlist) | |
797 | { | |
798 | u32 nr_groups = evlist->nr_groups; | |
799 | struct perf_evsel *evsel; | |
800 | int ret; | |
801 | ||
802 | ret = do_write(fd, &nr_groups, sizeof(nr_groups)); | |
803 | if (ret < 0) | |
804 | return ret; | |
805 | ||
0050f7aa | 806 | evlist__for_each(evlist, evsel) { |
a8bb559b NK |
807 | if (perf_evsel__is_group_leader(evsel) && |
808 | evsel->nr_members > 1) { | |
809 | const char *name = evsel->group_name ?: "{anon_group}"; | |
810 | u32 leader_idx = evsel->idx; | |
811 | u32 nr_members = evsel->nr_members; | |
812 | ||
813 | ret = do_write_string(fd, name); | |
814 | if (ret < 0) | |
815 | return ret; | |
816 | ||
817 | ret = do_write(fd, &leader_idx, sizeof(leader_idx)); | |
818 | if (ret < 0) | |
819 | return ret; | |
820 | ||
821 | ret = do_write(fd, &nr_members, sizeof(nr_members)); | |
822 | if (ret < 0) | |
823 | return ret; | |
824 | } | |
825 | } | |
826 | return 0; | |
827 | } | |
828 | ||
fbe96f29 SE |
829 | /* |
830 | * default get_cpuid(): nothing gets recorded | |
831 | * actual implementation must be in arch/$(ARCH)/util/header.c | |
832 | */ | |
1d037ca1 IT |
833 | int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused, |
834 | size_t sz __maybe_unused) | |
fbe96f29 SE |
835 | { |
836 | return -1; | |
837 | } | |
838 | ||
1d037ca1 IT |
839 | static int write_cpuid(int fd, struct perf_header *h __maybe_unused, |
840 | struct perf_evlist *evlist __maybe_unused) | |
fbe96f29 SE |
841 | { |
842 | char buffer[64]; | |
843 | int ret; | |
844 | ||
845 | ret = get_cpuid(buffer, sizeof(buffer)); | |
846 | if (!ret) | |
847 | goto write_it; | |
848 | ||
849 | return -1; | |
850 | write_it: | |
851 | return do_write_string(fd, buffer); | |
852 | } | |
853 | ||
1d037ca1 IT |
854 | static int write_branch_stack(int fd __maybe_unused, |
855 | struct perf_header *h __maybe_unused, | |
856 | struct perf_evlist *evlist __maybe_unused) | |
330aa675 SE |
857 | { |
858 | return 0; | |
859 | } | |
860 | ||
99fa2984 | 861 | static int write_auxtrace(int fd, struct perf_header *h, |
4025ea40 AH |
862 | struct perf_evlist *evlist __maybe_unused) |
863 | { | |
99fa2984 AH |
864 | struct perf_session *session; |
865 | int err; | |
866 | ||
867 | session = container_of(h, struct perf_session, header); | |
868 | ||
869 | err = auxtrace_index__write(fd, &session->auxtrace_index); | |
870 | if (err < 0) | |
871 | pr_err("Failed to write auxtrace index\n"); | |
872 | return err; | |
4025ea40 AH |
873 | } |
874 | ||
7e94cfcc NK |
875 | static void print_hostname(struct perf_header *ph, int fd __maybe_unused, |
876 | FILE *fp) | |
fbe96f29 | 877 | { |
7e94cfcc | 878 | fprintf(fp, "# hostname : %s\n", ph->env.hostname); |
fbe96f29 SE |
879 | } |
880 | ||
7e94cfcc NK |
881 | static void print_osrelease(struct perf_header *ph, int fd __maybe_unused, |
882 | FILE *fp) | |
fbe96f29 | 883 | { |
7e94cfcc | 884 | fprintf(fp, "# os release : %s\n", ph->env.os_release); |
fbe96f29 SE |
885 | } |
886 | ||
7e94cfcc | 887 | static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp) |
fbe96f29 | 888 | { |
7e94cfcc | 889 | fprintf(fp, "# arch : %s\n", ph->env.arch); |
fbe96f29 SE |
890 | } |
891 | ||
7e94cfcc NK |
892 | static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused, |
893 | FILE *fp) | |
fbe96f29 | 894 | { |
7e94cfcc | 895 | fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc); |
fbe96f29 SE |
896 | } |
897 | ||
7e94cfcc NK |
898 | static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused, |
899 | FILE *fp) | |
fbe96f29 | 900 | { |
7e94cfcc NK |
901 | fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online); |
902 | fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail); | |
fbe96f29 SE |
903 | } |
904 | ||
7e94cfcc NK |
905 | static void print_version(struct perf_header *ph, int fd __maybe_unused, |
906 | FILE *fp) | |
fbe96f29 | 907 | { |
7e94cfcc | 908 | fprintf(fp, "# perf version : %s\n", ph->env.version); |
fbe96f29 SE |
909 | } |
910 | ||
7e94cfcc NK |
911 | static void print_cmdline(struct perf_header *ph, int fd __maybe_unused, |
912 | FILE *fp) | |
fbe96f29 | 913 | { |
7e94cfcc | 914 | int nr, i; |
fbe96f29 | 915 | |
7e94cfcc | 916 | nr = ph->env.nr_cmdline; |
fbe96f29 SE |
917 | |
918 | fprintf(fp, "# cmdline : "); | |
919 | ||
768dd3f3 JO |
920 | for (i = 0; i < nr; i++) |
921 | fprintf(fp, "%s ", ph->env.cmdline_argv[i]); | |
fbe96f29 SE |
922 | fputc('\n', fp); |
923 | } | |
924 | ||
7e94cfcc NK |
925 | static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused, |
926 | FILE *fp) | |
fbe96f29 | 927 | { |
7e94cfcc | 928 | int nr, i; |
fbe96f29 | 929 | char *str; |
2bb00d2f | 930 | int cpu_nr = ph->env.nr_cpus_online; |
fbe96f29 | 931 | |
7e94cfcc NK |
932 | nr = ph->env.nr_sibling_cores; |
933 | str = ph->env.sibling_cores; | |
fbe96f29 SE |
934 | |
935 | for (i = 0; i < nr; i++) { | |
fbe96f29 | 936 | fprintf(fp, "# sibling cores : %s\n", str); |
7e94cfcc | 937 | str += strlen(str) + 1; |
fbe96f29 SE |
938 | } |
939 | ||
7e94cfcc NK |
940 | nr = ph->env.nr_sibling_threads; |
941 | str = ph->env.sibling_threads; | |
fbe96f29 SE |
942 | |
943 | for (i = 0; i < nr; i++) { | |
fbe96f29 | 944 | fprintf(fp, "# sibling threads : %s\n", str); |
7e94cfcc | 945 | str += strlen(str) + 1; |
fbe96f29 | 946 | } |
2bb00d2f KL |
947 | |
948 | if (ph->env.cpu != NULL) { | |
949 | for (i = 0; i < cpu_nr; i++) | |
950 | fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i, | |
951 | ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id); | |
952 | } else | |
953 | fprintf(fp, "# Core ID and Socket ID information is not available\n"); | |
fbe96f29 SE |
954 | } |
955 | ||
4e1b9c67 | 956 | static void free_event_desc(struct perf_evsel *events) |
fbe96f29 | 957 | { |
4e1b9c67 RR |
958 | struct perf_evsel *evsel; |
959 | ||
960 | if (!events) | |
961 | return; | |
962 | ||
963 | for (evsel = events; evsel->attr.size; evsel++) { | |
74cf249d ACM |
964 | zfree(&evsel->name); |
965 | zfree(&evsel->id); | |
4e1b9c67 RR |
966 | } |
967 | ||
968 | free(events); | |
969 | } | |
970 | ||
971 | static struct perf_evsel * | |
972 | read_event_desc(struct perf_header *ph, int fd) | |
973 | { | |
974 | struct perf_evsel *evsel, *events = NULL; | |
975 | u64 *id; | |
fbe96f29 | 976 | void *buf = NULL; |
62db9068 SE |
977 | u32 nre, sz, nr, i, j; |
978 | ssize_t ret; | |
979 | size_t msz; | |
fbe96f29 SE |
980 | |
981 | /* number of events */ | |
5323f60c | 982 | ret = readn(fd, &nre, sizeof(nre)); |
fbe96f29 SE |
983 | if (ret != (ssize_t)sizeof(nre)) |
984 | goto error; | |
985 | ||
986 | if (ph->needs_swap) | |
987 | nre = bswap_32(nre); | |
988 | ||
5323f60c | 989 | ret = readn(fd, &sz, sizeof(sz)); |
fbe96f29 SE |
990 | if (ret != (ssize_t)sizeof(sz)) |
991 | goto error; | |
992 | ||
993 | if (ph->needs_swap) | |
994 | sz = bswap_32(sz); | |
995 | ||
62db9068 | 996 | /* buffer to hold on file attr struct */ |
fbe96f29 SE |
997 | buf = malloc(sz); |
998 | if (!buf) | |
999 | goto error; | |
1000 | ||
4e1b9c67 RR |
1001 | /* the last event terminates with evsel->attr.size == 0: */ |
1002 | events = calloc(nre + 1, sizeof(*events)); | |
1003 | if (!events) | |
1004 | goto error; | |
1005 | ||
1006 | msz = sizeof(evsel->attr); | |
9fafd98f | 1007 | if (sz < msz) |
fbe96f29 SE |
1008 | msz = sz; |
1009 | ||
4e1b9c67 RR |
1010 | for (i = 0, evsel = events; i < nre; evsel++, i++) { |
1011 | evsel->idx = i; | |
fbe96f29 | 1012 | |
62db9068 SE |
1013 | /* |
1014 | * must read entire on-file attr struct to | |
1015 | * sync up with layout. | |
1016 | */ | |
5323f60c | 1017 | ret = readn(fd, buf, sz); |
fbe96f29 SE |
1018 | if (ret != (ssize_t)sz) |
1019 | goto error; | |
1020 | ||
1021 | if (ph->needs_swap) | |
1022 | perf_event__attr_swap(buf); | |
1023 | ||
4e1b9c67 | 1024 | memcpy(&evsel->attr, buf, msz); |
fbe96f29 | 1025 | |
5323f60c | 1026 | ret = readn(fd, &nr, sizeof(nr)); |
fbe96f29 SE |
1027 | if (ret != (ssize_t)sizeof(nr)) |
1028 | goto error; | |
1029 | ||
0807d2d8 | 1030 | if (ph->needs_swap) { |
fbe96f29 | 1031 | nr = bswap_32(nr); |
0807d2d8 ACM |
1032 | evsel->needs_swap = true; |
1033 | } | |
fbe96f29 | 1034 | |
4e1b9c67 RR |
1035 | evsel->name = do_read_string(fd, ph); |
1036 | ||
1037 | if (!nr) | |
1038 | continue; | |
1039 | ||
1040 | id = calloc(nr, sizeof(*id)); | |
1041 | if (!id) | |
1042 | goto error; | |
1043 | evsel->ids = nr; | |
1044 | evsel->id = id; | |
1045 | ||
1046 | for (j = 0 ; j < nr; j++) { | |
5323f60c | 1047 | ret = readn(fd, id, sizeof(*id)); |
4e1b9c67 RR |
1048 | if (ret != (ssize_t)sizeof(*id)) |
1049 | goto error; | |
1050 | if (ph->needs_swap) | |
1051 | *id = bswap_64(*id); | |
1052 | id++; | |
1053 | } | |
1054 | } | |
1055 | out: | |
04662523 | 1056 | free(buf); |
4e1b9c67 RR |
1057 | return events; |
1058 | error: | |
4cc97614 | 1059 | free_event_desc(events); |
4e1b9c67 RR |
1060 | events = NULL; |
1061 | goto out; | |
1062 | } | |
1063 | ||
2c5e8c52 PZ |
1064 | static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val, |
1065 | void *priv __attribute__((unused))) | |
1066 | { | |
1067 | return fprintf(fp, ", %s = %s", name, val); | |
1068 | } | |
1069 | ||
4e1b9c67 RR |
1070 | static void print_event_desc(struct perf_header *ph, int fd, FILE *fp) |
1071 | { | |
1072 | struct perf_evsel *evsel, *events = read_event_desc(ph, fd); | |
1073 | u32 j; | |
1074 | u64 *id; | |
1075 | ||
1076 | if (!events) { | |
1077 | fprintf(fp, "# event desc: not available or unable to read\n"); | |
1078 | return; | |
1079 | } | |
1080 | ||
1081 | for (evsel = events; evsel->attr.size; evsel++) { | |
1082 | fprintf(fp, "# event : name = %s, ", evsel->name); | |
fbe96f29 | 1083 | |
4e1b9c67 | 1084 | if (evsel->ids) { |
fbe96f29 | 1085 | fprintf(fp, ", id = {"); |
4e1b9c67 RR |
1086 | for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) { |
1087 | if (j) | |
1088 | fputc(',', fp); | |
1089 | fprintf(fp, " %"PRIu64, *id); | |
1090 | } | |
fbe96f29 | 1091 | fprintf(fp, " }"); |
4e1b9c67 | 1092 | } |
814c8c38 | 1093 | |
2c5e8c52 | 1094 | perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL); |
4e1b9c67 | 1095 | |
fbe96f29 SE |
1096 | fputc('\n', fp); |
1097 | } | |
4e1b9c67 RR |
1098 | |
1099 | free_event_desc(events); | |
fbe96f29 SE |
1100 | } |
1101 | ||
7e94cfcc | 1102 | static void print_total_mem(struct perf_header *ph, int fd __maybe_unused, |
1d037ca1 | 1103 | FILE *fp) |
fbe96f29 | 1104 | { |
7e94cfcc | 1105 | fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem); |
fbe96f29 SE |
1106 | } |
1107 | ||
7e94cfcc | 1108 | static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused, |
1d037ca1 | 1109 | FILE *fp) |
fbe96f29 | 1110 | { |
fbe96f29 | 1111 | u32 nr, c, i; |
7e94cfcc | 1112 | char *str, *tmp; |
fbe96f29 SE |
1113 | uint64_t mem_total, mem_free; |
1114 | ||
1115 | /* nr nodes */ | |
7e94cfcc NK |
1116 | nr = ph->env.nr_numa_nodes; |
1117 | str = ph->env.numa_nodes; | |
fbe96f29 SE |
1118 | |
1119 | for (i = 0; i < nr; i++) { | |
fbe96f29 | 1120 | /* node number */ |
7e94cfcc NK |
1121 | c = strtoul(str, &tmp, 0); |
1122 | if (*tmp != ':') | |
fbe96f29 SE |
1123 | goto error; |
1124 | ||
7e94cfcc NK |
1125 | str = tmp + 1; |
1126 | mem_total = strtoull(str, &tmp, 0); | |
1127 | if (*tmp != ':') | |
fbe96f29 SE |
1128 | goto error; |
1129 | ||
7e94cfcc NK |
1130 | str = tmp + 1; |
1131 | mem_free = strtoull(str, &tmp, 0); | |
1132 | if (*tmp != ':') | |
fbe96f29 SE |
1133 | goto error; |
1134 | ||
fbe96f29 SE |
1135 | fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," |
1136 | " free = %"PRIu64" kB\n", | |
7e94cfcc | 1137 | c, mem_total, mem_free); |
fbe96f29 | 1138 | |
7e94cfcc | 1139 | str = tmp + 1; |
fbe96f29 | 1140 | fprintf(fp, "# node%u cpu list : %s\n", c, str); |
1234471e NK |
1141 | |
1142 | str += strlen(str) + 1; | |
fbe96f29 SE |
1143 | } |
1144 | return; | |
1145 | error: | |
1146 | fprintf(fp, "# numa topology : not available\n"); | |
1147 | } | |
1148 | ||
7e94cfcc | 1149 | static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp) |
fbe96f29 | 1150 | { |
7e94cfcc | 1151 | fprintf(fp, "# cpuid : %s\n", ph->env.cpuid); |
fbe96f29 SE |
1152 | } |
1153 | ||
1d037ca1 | 1154 | static void print_branch_stack(struct perf_header *ph __maybe_unused, |
7e94cfcc | 1155 | int fd __maybe_unused, FILE *fp) |
330aa675 SE |
1156 | { |
1157 | fprintf(fp, "# contains samples with branch stack\n"); | |
1158 | } | |
1159 | ||
4025ea40 AH |
1160 | static void print_auxtrace(struct perf_header *ph __maybe_unused, |
1161 | int fd __maybe_unused, FILE *fp) | |
1162 | { | |
1163 | fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n"); | |
1164 | } | |
1165 | ||
7e94cfcc NK |
1166 | static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused, |
1167 | FILE *fp) | |
50a9667c RR |
1168 | { |
1169 | const char *delimiter = "# pmu mappings: "; | |
7e94cfcc | 1170 | char *str, *tmp; |
50a9667c RR |
1171 | u32 pmu_num; |
1172 | u32 type; | |
1173 | ||
7e94cfcc | 1174 | pmu_num = ph->env.nr_pmu_mappings; |
50a9667c RR |
1175 | if (!pmu_num) { |
1176 | fprintf(fp, "# pmu mappings: not available\n"); | |
1177 | return; | |
1178 | } | |
1179 | ||
7e94cfcc NK |
1180 | str = ph->env.pmu_mappings; |
1181 | ||
50a9667c | 1182 | while (pmu_num) { |
7e94cfcc NK |
1183 | type = strtoul(str, &tmp, 0); |
1184 | if (*tmp != ':') | |
1185 | goto error; | |
1186 | ||
1187 | str = tmp + 1; | |
1188 | fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type); | |
be4a2ded | 1189 | |
50a9667c | 1190 | delimiter = ", "; |
7e94cfcc NK |
1191 | str += strlen(str) + 1; |
1192 | pmu_num--; | |
50a9667c RR |
1193 | } |
1194 | ||
1195 | fprintf(fp, "\n"); | |
1196 | ||
1197 | if (!pmu_num) | |
1198 | return; | |
1199 | error: | |
1200 | fprintf(fp, "# pmu mappings: unable to read\n"); | |
1201 | } | |
1202 | ||
a8bb559b NK |
1203 | static void print_group_desc(struct perf_header *ph, int fd __maybe_unused, |
1204 | FILE *fp) | |
1205 | { | |
1206 | struct perf_session *session; | |
1207 | struct perf_evsel *evsel; | |
1208 | u32 nr = 0; | |
1209 | ||
1210 | session = container_of(ph, struct perf_session, header); | |
1211 | ||
0050f7aa | 1212 | evlist__for_each(session->evlist, evsel) { |
a8bb559b NK |
1213 | if (perf_evsel__is_group_leader(evsel) && |
1214 | evsel->nr_members > 1) { | |
1215 | fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", | |
1216 | perf_evsel__name(evsel)); | |
1217 | ||
1218 | nr = evsel->nr_members - 1; | |
1219 | } else if (nr) { | |
1220 | fprintf(fp, ",%s", perf_evsel__name(evsel)); | |
1221 | ||
1222 | if (--nr == 0) | |
1223 | fprintf(fp, "}\n"); | |
1224 | } | |
1225 | } | |
1226 | } | |
1227 | ||
08d95bd2 RR |
1228 | static int __event_process_build_id(struct build_id_event *bev, |
1229 | char *filename, | |
1230 | struct perf_session *session) | |
1231 | { | |
1232 | int err = -1; | |
08d95bd2 | 1233 | struct machine *machine; |
1f121b03 | 1234 | u16 cpumode; |
08d95bd2 RR |
1235 | struct dso *dso; |
1236 | enum dso_kernel_type dso_type; | |
1237 | ||
1238 | machine = perf_session__findnew_machine(session, bev->pid); | |
1239 | if (!machine) | |
1240 | goto out; | |
1241 | ||
1f121b03 | 1242 | cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
08d95bd2 | 1243 | |
1f121b03 | 1244 | switch (cpumode) { |
08d95bd2 RR |
1245 | case PERF_RECORD_MISC_KERNEL: |
1246 | dso_type = DSO_TYPE_KERNEL; | |
08d95bd2 RR |
1247 | break; |
1248 | case PERF_RECORD_MISC_GUEST_KERNEL: | |
1249 | dso_type = DSO_TYPE_GUEST_KERNEL; | |
08d95bd2 RR |
1250 | break; |
1251 | case PERF_RECORD_MISC_USER: | |
1252 | case PERF_RECORD_MISC_GUEST_USER: | |
1253 | dso_type = DSO_TYPE_USER; | |
08d95bd2 RR |
1254 | break; |
1255 | default: | |
1256 | goto out; | |
1257 | } | |
1258 | ||
aa7cc2ae | 1259 | dso = machine__findnew_dso(machine, filename); |
08d95bd2 RR |
1260 | if (dso != NULL) { |
1261 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; | |
1262 | ||
1263 | dso__set_build_id(dso, &bev->build_id); | |
1264 | ||
1f121b03 | 1265 | if (!is_kernel_module(filename, cpumode)) |
08d95bd2 RR |
1266 | dso->kernel = dso_type; |
1267 | ||
1268 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), | |
1269 | sbuild_id); | |
1270 | pr_debug("build id event received for %s: %s\n", | |
1271 | dso->long_name, sbuild_id); | |
d3a7c489 | 1272 | dso__put(dso); |
08d95bd2 RR |
1273 | } |
1274 | ||
1275 | err = 0; | |
1276 | out: | |
1277 | return err; | |
1278 | } | |
1279 | ||
1280 | static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, | |
1281 | int input, u64 offset, u64 size) | |
1282 | { | |
1283 | struct perf_session *session = container_of(header, struct perf_session, header); | |
1284 | struct { | |
1285 | struct perf_event_header header; | |
9ac3e487 | 1286 | u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))]; |
08d95bd2 RR |
1287 | char filename[0]; |
1288 | } old_bev; | |
1289 | struct build_id_event bev; | |
1290 | char filename[PATH_MAX]; | |
1291 | u64 limit = offset + size; | |
1292 | ||
1293 | while (offset < limit) { | |
1294 | ssize_t len; | |
1295 | ||
5323f60c | 1296 | if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) |
08d95bd2 RR |
1297 | return -1; |
1298 | ||
1299 | if (header->needs_swap) | |
1300 | perf_event_header__bswap(&old_bev.header); | |
1301 | ||
1302 | len = old_bev.header.size - sizeof(old_bev); | |
5323f60c | 1303 | if (readn(input, filename, len) != len) |
08d95bd2 RR |
1304 | return -1; |
1305 | ||
1306 | bev.header = old_bev.header; | |
1307 | ||
1308 | /* | |
1309 | * As the pid is the missing value, we need to fill | |
1310 | * it properly. The header.misc value give us nice hint. | |
1311 | */ | |
1312 | bev.pid = HOST_KERNEL_ID; | |
1313 | if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || | |
1314 | bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) | |
1315 | bev.pid = DEFAULT_GUEST_KERNEL_ID; | |
1316 | ||
1317 | memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); | |
1318 | __event_process_build_id(&bev, filename, session); | |
1319 | ||
1320 | offset += bev.header.size; | |
1321 | } | |
1322 | ||
1323 | return 0; | |
1324 | } | |
1325 | ||
1326 | static int perf_header__read_build_ids(struct perf_header *header, | |
1327 | int input, u64 offset, u64 size) | |
1328 | { | |
1329 | struct perf_session *session = container_of(header, struct perf_session, header); | |
1330 | struct build_id_event bev; | |
1331 | char filename[PATH_MAX]; | |
1332 | u64 limit = offset + size, orig_offset = offset; | |
1333 | int err = -1; | |
1334 | ||
1335 | while (offset < limit) { | |
1336 | ssize_t len; | |
1337 | ||
5323f60c | 1338 | if (readn(input, &bev, sizeof(bev)) != sizeof(bev)) |
08d95bd2 RR |
1339 | goto out; |
1340 | ||
1341 | if (header->needs_swap) | |
1342 | perf_event_header__bswap(&bev.header); | |
1343 | ||
1344 | len = bev.header.size - sizeof(bev); | |
5323f60c | 1345 | if (readn(input, filename, len) != len) |
08d95bd2 RR |
1346 | goto out; |
1347 | /* | |
1348 | * The a1645ce1 changeset: | |
1349 | * | |
1350 | * "perf: 'perf kvm' tool for monitoring guest performance from host" | |
1351 | * | |
1352 | * Added a field to struct build_id_event that broke the file | |
1353 | * format. | |
1354 | * | |
1355 | * Since the kernel build-id is the first entry, process the | |
1356 | * table using the old format if the well known | |
1357 | * '[kernel.kallsyms]' string for the kernel build-id has the | |
1358 | * first 4 characters chopped off (where the pid_t sits). | |
1359 | */ | |
1360 | if (memcmp(filename, "nel.kallsyms]", 13) == 0) { | |
1361 | if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) | |
1362 | return -1; | |
1363 | return perf_header__read_build_ids_abi_quirk(header, input, offset, size); | |
1364 | } | |
1365 | ||
1366 | __event_process_build_id(&bev, filename, session); | |
1367 | ||
1368 | offset += bev.header.size; | |
1369 | } | |
1370 | err = 0; | |
1371 | out: | |
1372 | return err; | |
1373 | } | |
1374 | ||
3d7eb86b NK |
1375 | static int process_tracing_data(struct perf_file_section *section __maybe_unused, |
1376 | struct perf_header *ph __maybe_unused, | |
1377 | int fd, void *data) | |
f1c67db7 | 1378 | { |
3dce2ce3 NK |
1379 | ssize_t ret = trace_report(fd, data, false); |
1380 | return ret < 0 ? -1 : 0; | |
f1c67db7 RR |
1381 | } |
1382 | ||
1383 | static int process_build_id(struct perf_file_section *section, | |
3d7eb86b | 1384 | struct perf_header *ph, int fd, |
1d037ca1 | 1385 | void *data __maybe_unused) |
f1c67db7 RR |
1386 | { |
1387 | if (perf_header__read_build_ids(ph, fd, section->offset, section->size)) | |
1388 | pr_debug("Failed to read buildids, continuing...\n"); | |
1389 | return 0; | |
1390 | } | |
1391 | ||
a1ae5655 | 1392 | static int process_hostname(struct perf_file_section *section __maybe_unused, |
3d7eb86b NK |
1393 | struct perf_header *ph, int fd, |
1394 | void *data __maybe_unused) | |
a1ae5655 NK |
1395 | { |
1396 | ph->env.hostname = do_read_string(fd, ph); | |
1397 | return ph->env.hostname ? 0 : -ENOMEM; | |
1398 | } | |
1399 | ||
1400 | static int process_osrelease(struct perf_file_section *section __maybe_unused, | |
3d7eb86b NK |
1401 | struct perf_header *ph, int fd, |
1402 | void *data __maybe_unused) | |
a1ae5655 NK |
1403 | { |
1404 | ph->env.os_release = do_read_string(fd, ph); | |
1405 | return ph->env.os_release ? 0 : -ENOMEM; | |
1406 | } | |
1407 | ||
1408 | static int process_version(struct perf_file_section *section __maybe_unused, | |
3d7eb86b NK |
1409 | struct perf_header *ph, int fd, |
1410 | void *data __maybe_unused) | |
a1ae5655 NK |
1411 | { |
1412 | ph->env.version = do_read_string(fd, ph); | |
1413 | return ph->env.version ? 0 : -ENOMEM; | |
1414 | } | |
1415 | ||
1416 | static int process_arch(struct perf_file_section *section __maybe_unused, | |
3d7eb86b NK |
1417 | struct perf_header *ph, int fd, |
1418 | void *data __maybe_unused) | |
a1ae5655 NK |
1419 | { |
1420 | ph->env.arch = do_read_string(fd, ph); | |
1421 | return ph->env.arch ? 0 : -ENOMEM; | |
1422 | } | |
1423 | ||
1424 | static int process_nrcpus(struct perf_file_section *section __maybe_unused, | |
3d7eb86b NK |
1425 | struct perf_header *ph, int fd, |
1426 | void *data __maybe_unused) | |
a1ae5655 | 1427 | { |
727ebd54 | 1428 | ssize_t ret; |
a1ae5655 NK |
1429 | u32 nr; |
1430 | ||
5323f60c | 1431 | ret = readn(fd, &nr, sizeof(nr)); |
a1ae5655 NK |
1432 | if (ret != sizeof(nr)) |
1433 | return -1; | |
1434 | ||
1435 | if (ph->needs_swap) | |
1436 | nr = bswap_32(nr); | |
1437 | ||
1438 | ph->env.nr_cpus_online = nr; | |
1439 | ||
5323f60c | 1440 | ret = readn(fd, &nr, sizeof(nr)); |
a1ae5655 NK |
1441 | if (ret != sizeof(nr)) |
1442 | return -1; | |
1443 | ||
1444 | if (ph->needs_swap) | |
1445 | nr = bswap_32(nr); | |
1446 | ||
1447 | ph->env.nr_cpus_avail = nr; | |
1448 | return 0; | |
1449 | } | |
1450 | ||
1451 | static int process_cpudesc(struct perf_file_section *section __maybe_unused, | |
3d7eb86b NK |
1452 | struct perf_header *ph, int fd, |
1453 | void *data __maybe_unused) | |
a1ae5655 NK |
1454 | { |
1455 | ph->env.cpu_desc = do_read_string(fd, ph); | |
1456 | return ph->env.cpu_desc ? 0 : -ENOMEM; | |
1457 | } | |
1458 | ||
1459 | static int process_cpuid(struct perf_file_section *section __maybe_unused, | |
3d7eb86b NK |
1460 | struct perf_header *ph, int fd, |
1461 | void *data __maybe_unused) | |
a1ae5655 NK |
1462 | { |
1463 | ph->env.cpuid = do_read_string(fd, ph); | |
1464 | return ph->env.cpuid ? 0 : -ENOMEM; | |
1465 | } | |
1466 | ||
1467 | static int process_total_mem(struct perf_file_section *section __maybe_unused, | |
3d7eb86b NK |
1468 | struct perf_header *ph, int fd, |
1469 | void *data __maybe_unused) | |
a1ae5655 NK |
1470 | { |
1471 | uint64_t mem; | |
727ebd54 | 1472 | ssize_t ret; |
a1ae5655 | 1473 | |
5323f60c | 1474 | ret = readn(fd, &mem, sizeof(mem)); |
a1ae5655 NK |
1475 | if (ret != sizeof(mem)) |
1476 | return -1; | |
1477 | ||
1478 | if (ph->needs_swap) | |
1479 | mem = bswap_64(mem); | |
1480 | ||
1481 | ph->env.total_mem = mem; | |
1482 | return 0; | |
1483 | } | |
1484 | ||
7c2f7afd RR |
1485 | static struct perf_evsel * |
1486 | perf_evlist__find_by_index(struct perf_evlist *evlist, int idx) | |
1487 | { | |
1488 | struct perf_evsel *evsel; | |
1489 | ||
0050f7aa | 1490 | evlist__for_each(evlist, evsel) { |
7c2f7afd RR |
1491 | if (evsel->idx == idx) |
1492 | return evsel; | |
1493 | } | |
1494 | ||
1495 | return NULL; | |
1496 | } | |
1497 | ||
1498 | static void | |
3d7eb86b NK |
1499 | perf_evlist__set_event_name(struct perf_evlist *evlist, |
1500 | struct perf_evsel *event) | |
7c2f7afd RR |
1501 | { |
1502 | struct perf_evsel *evsel; | |
1503 | ||
1504 | if (!event->name) | |
1505 | return; | |
1506 | ||
1507 | evsel = perf_evlist__find_by_index(evlist, event->idx); | |
1508 | if (!evsel) | |
1509 | return; | |
1510 | ||
1511 | if (evsel->name) | |
1512 | return; | |
1513 | ||
1514 | evsel->name = strdup(event->name); | |
1515 | } | |
1516 | ||
1517 | static int | |
1d037ca1 | 1518 | process_event_desc(struct perf_file_section *section __maybe_unused, |
3d7eb86b | 1519 | struct perf_header *header, int fd, |
1d037ca1 | 1520 | void *data __maybe_unused) |
7c2f7afd | 1521 | { |
3d7eb86b | 1522 | struct perf_session *session; |
7c2f7afd RR |
1523 | struct perf_evsel *evsel, *events = read_event_desc(header, fd); |
1524 | ||
1525 | if (!events) | |
1526 | return 0; | |
1527 | ||
3d7eb86b | 1528 | session = container_of(header, struct perf_session, header); |
7c2f7afd RR |
1529 | for (evsel = events; evsel->attr.size; evsel++) |
1530 | perf_evlist__set_event_name(session->evlist, evsel); | |
1531 | ||
1532 | free_event_desc(events); | |
1533 | ||
1534 | return 0; | |
1535 | } | |
1536 | ||
768dd3f3 | 1537 | static int process_cmdline(struct perf_file_section *section, |
3d7eb86b NK |
1538 | struct perf_header *ph, int fd, |
1539 | void *data __maybe_unused) | |
a1ae5655 | 1540 | { |
727ebd54 | 1541 | ssize_t ret; |
768dd3f3 JO |
1542 | char *str, *cmdline = NULL, **argv = NULL; |
1543 | u32 nr, i, len = 0; | |
a1ae5655 | 1544 | |
5323f60c | 1545 | ret = readn(fd, &nr, sizeof(nr)); |
a1ae5655 NK |
1546 | if (ret != sizeof(nr)) |
1547 | return -1; | |
1548 | ||
1549 | if (ph->needs_swap) | |
1550 | nr = bswap_32(nr); | |
1551 | ||
1552 | ph->env.nr_cmdline = nr; | |
768dd3f3 JO |
1553 | |
1554 | cmdline = zalloc(section->size + nr + 1); | |
1555 | if (!cmdline) | |
1556 | return -1; | |
1557 | ||
1558 | argv = zalloc(sizeof(char *) * (nr + 1)); | |
1559 | if (!argv) | |
1560 | goto error; | |
a1ae5655 NK |
1561 | |
1562 | for (i = 0; i < nr; i++) { | |
1563 | str = do_read_string(fd, ph); | |
1564 | if (!str) | |
1565 | goto error; | |
1566 | ||
768dd3f3 JO |
1567 | argv[i] = cmdline + len; |
1568 | memcpy(argv[i], str, strlen(str) + 1); | |
1569 | len += strlen(str) + 1; | |
a1ae5655 NK |
1570 | free(str); |
1571 | } | |
768dd3f3 JO |
1572 | ph->env.cmdline = cmdline; |
1573 | ph->env.cmdline_argv = (const char **) argv; | |
a1ae5655 NK |
1574 | return 0; |
1575 | ||
1576 | error: | |
768dd3f3 JO |
1577 | free(argv); |
1578 | free(cmdline); | |
a1ae5655 NK |
1579 | return -1; |
1580 | } | |
1581 | ||
2bb00d2f | 1582 | static int process_cpu_topology(struct perf_file_section *section, |
3d7eb86b NK |
1583 | struct perf_header *ph, int fd, |
1584 | void *data __maybe_unused) | |
a1ae5655 | 1585 | { |
727ebd54 | 1586 | ssize_t ret; |
a1ae5655 NK |
1587 | u32 nr, i; |
1588 | char *str; | |
1589 | struct strbuf sb; | |
2bb00d2f KL |
1590 | int cpu_nr = ph->env.nr_cpus_online; |
1591 | u64 size = 0; | |
1592 | ||
1593 | ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu)); | |
1594 | if (!ph->env.cpu) | |
1595 | return -1; | |
a1ae5655 | 1596 | |
5323f60c | 1597 | ret = readn(fd, &nr, sizeof(nr)); |
a1ae5655 | 1598 | if (ret != sizeof(nr)) |
2bb00d2f | 1599 | goto free_cpu; |
a1ae5655 NK |
1600 | |
1601 | if (ph->needs_swap) | |
1602 | nr = bswap_32(nr); | |
1603 | ||
1604 | ph->env.nr_sibling_cores = nr; | |
2bb00d2f | 1605 | size += sizeof(u32); |
a1ae5655 NK |
1606 | strbuf_init(&sb, 128); |
1607 | ||
1608 | for (i = 0; i < nr; i++) { | |
1609 | str = do_read_string(fd, ph); | |
1610 | if (!str) | |
1611 | goto error; | |
1612 | ||
1613 | /* include a NULL character at the end */ | |
1614 | strbuf_add(&sb, str, strlen(str) + 1); | |
2bb00d2f | 1615 | size += string_size(str); |
a1ae5655 NK |
1616 | free(str); |
1617 | } | |
1618 | ph->env.sibling_cores = strbuf_detach(&sb, NULL); | |
1619 | ||
5323f60c | 1620 | ret = readn(fd, &nr, sizeof(nr)); |
a1ae5655 NK |
1621 | if (ret != sizeof(nr)) |
1622 | return -1; | |
1623 | ||
1624 | if (ph->needs_swap) | |
1625 | nr = bswap_32(nr); | |
1626 | ||
1627 | ph->env.nr_sibling_threads = nr; | |
2bb00d2f | 1628 | size += sizeof(u32); |
a1ae5655 NK |
1629 | |
1630 | for (i = 0; i < nr; i++) { | |
1631 | str = do_read_string(fd, ph); | |
1632 | if (!str) | |
1633 | goto error; | |
1634 | ||
1635 | /* include a NULL character at the end */ | |
1636 | strbuf_add(&sb, str, strlen(str) + 1); | |
2bb00d2f | 1637 | size += string_size(str); |
a1ae5655 NK |
1638 | free(str); |
1639 | } | |
1640 | ph->env.sibling_threads = strbuf_detach(&sb, NULL); | |
2bb00d2f KL |
1641 | |
1642 | /* | |
1643 | * The header may be from old perf, | |
1644 | * which doesn't include core id and socket id information. | |
1645 | */ | |
1646 | if (section->size <= size) { | |
1647 | zfree(&ph->env.cpu); | |
1648 | return 0; | |
1649 | } | |
1650 | ||
1651 | for (i = 0; i < (u32)cpu_nr; i++) { | |
1652 | ret = readn(fd, &nr, sizeof(nr)); | |
1653 | if (ret != sizeof(nr)) | |
1654 | goto free_cpu; | |
1655 | ||
1656 | if (ph->needs_swap) | |
1657 | nr = bswap_32(nr); | |
1658 | ||
1659 | if (nr > (u32)cpu_nr) { | |
1660 | pr_debug("core_id number is too big." | |
1661 | "You may need to upgrade the perf tool.\n"); | |
1662 | goto free_cpu; | |
1663 | } | |
1664 | ph->env.cpu[i].core_id = nr; | |
1665 | ||
1666 | ret = readn(fd, &nr, sizeof(nr)); | |
1667 | if (ret != sizeof(nr)) | |
1668 | goto free_cpu; | |
1669 | ||
1670 | if (ph->needs_swap) | |
1671 | nr = bswap_32(nr); | |
1672 | ||
1673 | if (nr > (u32)cpu_nr) { | |
1674 | pr_debug("socket_id number is too big." | |
1675 | "You may need to upgrade the perf tool.\n"); | |
1676 | goto free_cpu; | |
1677 | } | |
1678 | ||
1679 | ph->env.cpu[i].socket_id = nr; | |
1680 | } | |
1681 | ||
a1ae5655 NK |
1682 | return 0; |
1683 | ||
1684 | error: | |
1685 | strbuf_release(&sb); | |
2bb00d2f KL |
1686 | free_cpu: |
1687 | zfree(&ph->env.cpu); | |
a1ae5655 NK |
1688 | return -1; |
1689 | } | |
1690 | ||
1691 | static int process_numa_topology(struct perf_file_section *section __maybe_unused, | |
3d7eb86b NK |
1692 | struct perf_header *ph, int fd, |
1693 | void *data __maybe_unused) | |
a1ae5655 | 1694 | { |
727ebd54 | 1695 | ssize_t ret; |
a1ae5655 NK |
1696 | u32 nr, node, i; |
1697 | char *str; | |
1698 | uint64_t mem_total, mem_free; | |
1699 | struct strbuf sb; | |
1700 | ||
1701 | /* nr nodes */ | |
5323f60c | 1702 | ret = readn(fd, &nr, sizeof(nr)); |
a1ae5655 NK |
1703 | if (ret != sizeof(nr)) |
1704 | goto error; | |
1705 | ||
1706 | if (ph->needs_swap) | |
1707 | nr = bswap_32(nr); | |
1708 | ||
1709 | ph->env.nr_numa_nodes = nr; | |
1710 | strbuf_init(&sb, 256); | |
1711 | ||
1712 | for (i = 0; i < nr; i++) { | |
1713 | /* node number */ | |
5323f60c | 1714 | ret = readn(fd, &node, sizeof(node)); |
a1ae5655 NK |
1715 | if (ret != sizeof(node)) |
1716 | goto error; | |
1717 | ||
5323f60c | 1718 | ret = readn(fd, &mem_total, sizeof(u64)); |
a1ae5655 NK |
1719 | if (ret != sizeof(u64)) |
1720 | goto error; | |
1721 | ||
5323f60c | 1722 | ret = readn(fd, &mem_free, sizeof(u64)); |
a1ae5655 NK |
1723 | if (ret != sizeof(u64)) |
1724 | goto error; | |
1725 | ||
1726 | if (ph->needs_swap) { | |
1727 | node = bswap_32(node); | |
1728 | mem_total = bswap_64(mem_total); | |
1729 | mem_free = bswap_64(mem_free); | |
1730 | } | |
1731 | ||
1732 | strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":", | |
1733 | node, mem_total, mem_free); | |
1734 | ||
1735 | str = do_read_string(fd, ph); | |
1736 | if (!str) | |
1737 | goto error; | |
1738 | ||
1739 | /* include a NULL character at the end */ | |
1740 | strbuf_add(&sb, str, strlen(str) + 1); | |
1741 | free(str); | |
1742 | } | |
1743 | ph->env.numa_nodes = strbuf_detach(&sb, NULL); | |
1744 | return 0; | |
1745 | ||
1746 | error: | |
1747 | strbuf_release(&sb); | |
1748 | return -1; | |
1749 | } | |
1750 | ||
1751 | static int process_pmu_mappings(struct perf_file_section *section __maybe_unused, | |
3d7eb86b NK |
1752 | struct perf_header *ph, int fd, |
1753 | void *data __maybe_unused) | |
a1ae5655 | 1754 | { |
727ebd54 | 1755 | ssize_t ret; |
a1ae5655 NK |
1756 | char *name; |
1757 | u32 pmu_num; | |
1758 | u32 type; | |
1759 | struct strbuf sb; | |
1760 | ||
5323f60c | 1761 | ret = readn(fd, &pmu_num, sizeof(pmu_num)); |
a1ae5655 NK |
1762 | if (ret != sizeof(pmu_num)) |
1763 | return -1; | |
1764 | ||
1765 | if (ph->needs_swap) | |
1766 | pmu_num = bswap_32(pmu_num); | |
1767 | ||
1768 | if (!pmu_num) { | |
1769 | pr_debug("pmu mappings not available\n"); | |
1770 | return 0; | |
1771 | } | |
1772 | ||
1773 | ph->env.nr_pmu_mappings = pmu_num; | |
1774 | strbuf_init(&sb, 128); | |
1775 | ||
1776 | while (pmu_num) { | |
5323f60c | 1777 | if (readn(fd, &type, sizeof(type)) != sizeof(type)) |
a1ae5655 NK |
1778 | goto error; |
1779 | if (ph->needs_swap) | |
1780 | type = bswap_32(type); | |
1781 | ||
1782 | name = do_read_string(fd, ph); | |
1783 | if (!name) | |
1784 | goto error; | |
1785 | ||
1786 | strbuf_addf(&sb, "%u:%s", type, name); | |
1787 | /* include a NULL character at the end */ | |
1788 | strbuf_add(&sb, "", 1); | |
1789 | ||
1790 | free(name); | |
1791 | pmu_num--; | |
1792 | } | |
1793 | ph->env.pmu_mappings = strbuf_detach(&sb, NULL); | |
1794 | return 0; | |
1795 | ||
1796 | error: | |
1797 | strbuf_release(&sb); | |
1798 | return -1; | |
1799 | } | |
1800 | ||
a8bb559b NK |
1801 | static int process_group_desc(struct perf_file_section *section __maybe_unused, |
1802 | struct perf_header *ph, int fd, | |
1803 | void *data __maybe_unused) | |
1804 | { | |
1805 | size_t ret = -1; | |
1806 | u32 i, nr, nr_groups; | |
1807 | struct perf_session *session; | |
1808 | struct perf_evsel *evsel, *leader = NULL; | |
1809 | struct group_desc { | |
1810 | char *name; | |
1811 | u32 leader_idx; | |
1812 | u32 nr_members; | |
1813 | } *desc; | |
1814 | ||
1815 | if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups)) | |
1816 | return -1; | |
1817 | ||
1818 | if (ph->needs_swap) | |
1819 | nr_groups = bswap_32(nr_groups); | |
1820 | ||
1821 | ph->env.nr_groups = nr_groups; | |
1822 | if (!nr_groups) { | |
1823 | pr_debug("group desc not available\n"); | |
1824 | return 0; | |
1825 | } | |
1826 | ||
1827 | desc = calloc(nr_groups, sizeof(*desc)); | |
1828 | if (!desc) | |
1829 | return -1; | |
1830 | ||
1831 | for (i = 0; i < nr_groups; i++) { | |
1832 | desc[i].name = do_read_string(fd, ph); | |
1833 | if (!desc[i].name) | |
1834 | goto out_free; | |
1835 | ||
1836 | if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32)) | |
1837 | goto out_free; | |
1838 | ||
1839 | if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32)) | |
1840 | goto out_free; | |
1841 | ||
1842 | if (ph->needs_swap) { | |
1843 | desc[i].leader_idx = bswap_32(desc[i].leader_idx); | |
1844 | desc[i].nr_members = bswap_32(desc[i].nr_members); | |
1845 | } | |
1846 | } | |
1847 | ||
1848 | /* | |
1849 | * Rebuild group relationship based on the group_desc | |
1850 | */ | |
1851 | session = container_of(ph, struct perf_session, header); | |
1852 | session->evlist->nr_groups = nr_groups; | |
1853 | ||
1854 | i = nr = 0; | |
0050f7aa | 1855 | evlist__for_each(session->evlist, evsel) { |
a8bb559b NK |
1856 | if (evsel->idx == (int) desc[i].leader_idx) { |
1857 | evsel->leader = evsel; | |
1858 | /* {anon_group} is a dummy name */ | |
210e812f | 1859 | if (strcmp(desc[i].name, "{anon_group}")) { |
a8bb559b | 1860 | evsel->group_name = desc[i].name; |
210e812f NK |
1861 | desc[i].name = NULL; |
1862 | } | |
a8bb559b NK |
1863 | evsel->nr_members = desc[i].nr_members; |
1864 | ||
1865 | if (i >= nr_groups || nr > 0) { | |
1866 | pr_debug("invalid group desc\n"); | |
1867 | goto out_free; | |
1868 | } | |
1869 | ||
1870 | leader = evsel; | |
1871 | nr = evsel->nr_members - 1; | |
1872 | i++; | |
1873 | } else if (nr) { | |
1874 | /* This is a group member */ | |
1875 | evsel->leader = leader; | |
1876 | ||
1877 | nr--; | |
1878 | } | |
1879 | } | |
1880 | ||
1881 | if (i != nr_groups || nr != 0) { | |
1882 | pr_debug("invalid group desc\n"); | |
1883 | goto out_free; | |
1884 | } | |
1885 | ||
1886 | ret = 0; | |
1887 | out_free: | |
50a2740b | 1888 | for (i = 0; i < nr_groups; i++) |
74cf249d | 1889 | zfree(&desc[i].name); |
a8bb559b NK |
1890 | free(desc); |
1891 | ||
1892 | return ret; | |
1893 | } | |
1894 | ||
99fa2984 AH |
1895 | static int process_auxtrace(struct perf_file_section *section, |
1896 | struct perf_header *ph, int fd, | |
1897 | void *data __maybe_unused) | |
1898 | { | |
1899 | struct perf_session *session; | |
1900 | int err; | |
1901 | ||
1902 | session = container_of(ph, struct perf_session, header); | |
1903 | ||
1904 | err = auxtrace_index__process(fd, section->size, session, | |
1905 | ph->needs_swap); | |
1906 | if (err < 0) | |
1907 | pr_err("Failed to process auxtrace index\n"); | |
1908 | return err; | |
1909 | } | |
1910 | ||
fbe96f29 SE |
1911 | struct feature_ops { |
1912 | int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist); | |
1913 | void (*print)(struct perf_header *h, int fd, FILE *fp); | |
f1c67db7 | 1914 | int (*process)(struct perf_file_section *section, |
3d7eb86b | 1915 | struct perf_header *h, int fd, void *data); |
fbe96f29 SE |
1916 | const char *name; |
1917 | bool full_only; | |
1918 | }; | |
1919 | ||
8cdfa78a RR |
1920 | #define FEAT_OPA(n, func) \ |
1921 | [n] = { .name = #n, .write = write_##func, .print = print_##func } | |
f1c67db7 RR |
1922 | #define FEAT_OPP(n, func) \ |
1923 | [n] = { .name = #n, .write = write_##func, .print = print_##func, \ | |
1924 | .process = process_##func } | |
8cdfa78a | 1925 | #define FEAT_OPF(n, func) \ |
f1c67db7 | 1926 | [n] = { .name = #n, .write = write_##func, .print = print_##func, \ |
a1ae5655 | 1927 | .process = process_##func, .full_only = true } |
8cdfa78a RR |
1928 | |
1929 | /* feature_ops not implemented: */ | |
2eeaaa09 SE |
1930 | #define print_tracing_data NULL |
1931 | #define print_build_id NULL | |
fbe96f29 SE |
1932 | |
1933 | static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = { | |
2eeaaa09 | 1934 | FEAT_OPP(HEADER_TRACING_DATA, tracing_data), |
f1c67db7 | 1935 | FEAT_OPP(HEADER_BUILD_ID, build_id), |
a1ae5655 NK |
1936 | FEAT_OPP(HEADER_HOSTNAME, hostname), |
1937 | FEAT_OPP(HEADER_OSRELEASE, osrelease), | |
1938 | FEAT_OPP(HEADER_VERSION, version), | |
1939 | FEAT_OPP(HEADER_ARCH, arch), | |
1940 | FEAT_OPP(HEADER_NRCPUS, nrcpus), | |
1941 | FEAT_OPP(HEADER_CPUDESC, cpudesc), | |
37e9d750 | 1942 | FEAT_OPP(HEADER_CPUID, cpuid), |
a1ae5655 | 1943 | FEAT_OPP(HEADER_TOTAL_MEM, total_mem), |
7c2f7afd | 1944 | FEAT_OPP(HEADER_EVENT_DESC, event_desc), |
a1ae5655 | 1945 | FEAT_OPP(HEADER_CMDLINE, cmdline), |
8cdfa78a RR |
1946 | FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology), |
1947 | FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology), | |
330aa675 | 1948 | FEAT_OPA(HEADER_BRANCH_STACK, branch_stack), |
a1ae5655 | 1949 | FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings), |
a8bb559b | 1950 | FEAT_OPP(HEADER_GROUP_DESC, group_desc), |
99fa2984 | 1951 | FEAT_OPP(HEADER_AUXTRACE, auxtrace), |
fbe96f29 SE |
1952 | }; |
1953 | ||
1954 | struct header_print_data { | |
1955 | FILE *fp; | |
1956 | bool full; /* extended list of headers */ | |
1957 | }; | |
1958 | ||
1959 | static int perf_file_section__fprintf_info(struct perf_file_section *section, | |
1960 | struct perf_header *ph, | |
1961 | int feat, int fd, void *data) | |
1962 | { | |
1963 | struct header_print_data *hd = data; | |
1964 | ||
1965 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { | |
1966 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " | |
1967 | "%d, continuing...\n", section->offset, feat); | |
1968 | return 0; | |
1969 | } | |
b1e5a9be | 1970 | if (feat >= HEADER_LAST_FEATURE) { |
fbe96f29 | 1971 | pr_warning("unknown feature %d\n", feat); |
f7a8a133 | 1972 | return 0; |
fbe96f29 SE |
1973 | } |
1974 | if (!feat_ops[feat].print) | |
1975 | return 0; | |
1976 | ||
1977 | if (!feat_ops[feat].full_only || hd->full) | |
1978 | feat_ops[feat].print(ph, fd, hd->fp); | |
1979 | else | |
1980 | fprintf(hd->fp, "# %s info available, use -I to display\n", | |
1981 | feat_ops[feat].name); | |
1982 | ||
1983 | return 0; | |
1984 | } | |
1985 | ||
1986 | int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) | |
1987 | { | |
1988 | struct header_print_data hd; | |
1989 | struct perf_header *header = &session->header; | |
cc9784bd | 1990 | int fd = perf_data_file__fd(session->file); |
fbe96f29 SE |
1991 | hd.fp = fp; |
1992 | hd.full = full; | |
1993 | ||
1994 | perf_header__process_sections(header, fd, &hd, | |
1995 | perf_file_section__fprintf_info); | |
1996 | return 0; | |
1997 | } | |
1998 | ||
fbe96f29 SE |
1999 | static int do_write_feat(int fd, struct perf_header *h, int type, |
2000 | struct perf_file_section **p, | |
2001 | struct perf_evlist *evlist) | |
2002 | { | |
2003 | int err; | |
2004 | int ret = 0; | |
2005 | ||
2006 | if (perf_header__has_feat(h, type)) { | |
b1e5a9be RR |
2007 | if (!feat_ops[type].write) |
2008 | return -1; | |
fbe96f29 SE |
2009 | |
2010 | (*p)->offset = lseek(fd, 0, SEEK_CUR); | |
2011 | ||
2012 | err = feat_ops[type].write(fd, h, evlist); | |
2013 | if (err < 0) { | |
2014 | pr_debug("failed to write feature %d\n", type); | |
2015 | ||
2016 | /* undo anything written */ | |
2017 | lseek(fd, (*p)->offset, SEEK_SET); | |
2018 | ||
2019 | return -1; | |
2020 | } | |
2021 | (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset; | |
2022 | (*p)++; | |
2023 | } | |
2024 | return ret; | |
2025 | } | |
2026 | ||
1c0b04d1 | 2027 | static int perf_header__adds_write(struct perf_header *header, |
361c99a6 | 2028 | struct perf_evlist *evlist, int fd) |
2ba08250 | 2029 | { |
9e827dd0 | 2030 | int nr_sections; |
fbe96f29 | 2031 | struct perf_file_section *feat_sec, *p; |
9e827dd0 FW |
2032 | int sec_size; |
2033 | u64 sec_start; | |
b1e5a9be | 2034 | int feat; |
fbe96f29 | 2035 | int err; |
9e827dd0 | 2036 | |
1c0b04d1 | 2037 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
9e827dd0 | 2038 | if (!nr_sections) |
d5eed904 | 2039 | return 0; |
9e827dd0 | 2040 | |
91b98804 | 2041 | feat_sec = p = calloc(nr_sections, sizeof(*feat_sec)); |
d5eed904 ACM |
2042 | if (feat_sec == NULL) |
2043 | return -ENOMEM; | |
9e827dd0 FW |
2044 | |
2045 | sec_size = sizeof(*feat_sec) * nr_sections; | |
2046 | ||
8d541e97 | 2047 | sec_start = header->feat_offset; |
f887f301 | 2048 | lseek(fd, sec_start + sec_size, SEEK_SET); |
2ba08250 | 2049 | |
b1e5a9be RR |
2050 | for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { |
2051 | if (do_write_feat(fd, header, feat, &p, evlist)) | |
2052 | perf_header__clear_feat(header, feat); | |
2053 | } | |
9e827dd0 | 2054 | |
f887f301 | 2055 | lseek(fd, sec_start, SEEK_SET); |
fbe96f29 SE |
2056 | /* |
2057 | * may write more than needed due to dropped feature, but | |
2058 | * this is okay, reader will skip the mising entries | |
2059 | */ | |
d5eed904 ACM |
2060 | err = do_write(fd, feat_sec, sec_size); |
2061 | if (err < 0) | |
2062 | pr_debug("failed to write feature section\n"); | |
9e827dd0 | 2063 | free(feat_sec); |
d5eed904 | 2064 | return err; |
9e827dd0 | 2065 | } |
2ba08250 | 2066 | |
8dc58101 TZ |
2067 | int perf_header__write_pipe(int fd) |
2068 | { | |
2069 | struct perf_pipe_file_header f_header; | |
2070 | int err; | |
2071 | ||
2072 | f_header = (struct perf_pipe_file_header){ | |
2073 | .magic = PERF_MAGIC, | |
2074 | .size = sizeof(f_header), | |
2075 | }; | |
2076 | ||
2077 | err = do_write(fd, &f_header, sizeof(f_header)); | |
2078 | if (err < 0) { | |
2079 | pr_debug("failed to write perf pipe header\n"); | |
2080 | return err; | |
2081 | } | |
2082 | ||
2083 | return 0; | |
2084 | } | |
2085 | ||
a91e5431 ACM |
2086 | int perf_session__write_header(struct perf_session *session, |
2087 | struct perf_evlist *evlist, | |
2088 | int fd, bool at_exit) | |
7c6a1c65 PZ |
2089 | { |
2090 | struct perf_file_header f_header; | |
2091 | struct perf_file_attr f_attr; | |
1c0b04d1 | 2092 | struct perf_header *header = &session->header; |
563aecb2 | 2093 | struct perf_evsel *evsel; |
944d62ba | 2094 | u64 attr_offset; |
a91e5431 | 2095 | int err; |
7c6a1c65 PZ |
2096 | |
2097 | lseek(fd, sizeof(f_header), SEEK_SET); | |
2098 | ||
0050f7aa | 2099 | evlist__for_each(session->evlist, evsel) { |
6606f873 RR |
2100 | evsel->id_offset = lseek(fd, 0, SEEK_CUR); |
2101 | err = do_write(fd, evsel->id, evsel->ids * sizeof(u64)); | |
d5eed904 ACM |
2102 | if (err < 0) { |
2103 | pr_debug("failed to write perf header\n"); | |
2104 | return err; | |
2105 | } | |
7c6a1c65 PZ |
2106 | } |
2107 | ||
944d62ba | 2108 | attr_offset = lseek(fd, 0, SEEK_CUR); |
7c6a1c65 | 2109 | |
0050f7aa | 2110 | evlist__for_each(evlist, evsel) { |
7c6a1c65 | 2111 | f_attr = (struct perf_file_attr){ |
6606f873 | 2112 | .attr = evsel->attr, |
7c6a1c65 | 2113 | .ids = { |
6606f873 RR |
2114 | .offset = evsel->id_offset, |
2115 | .size = evsel->ids * sizeof(u64), | |
7c6a1c65 PZ |
2116 | } |
2117 | }; | |
d5eed904 ACM |
2118 | err = do_write(fd, &f_attr, sizeof(f_attr)); |
2119 | if (err < 0) { | |
2120 | pr_debug("failed to write perf header attribute\n"); | |
2121 | return err; | |
2122 | } | |
7c6a1c65 PZ |
2123 | } |
2124 | ||
d645c442 AH |
2125 | if (!header->data_offset) |
2126 | header->data_offset = lseek(fd, 0, SEEK_CUR); | |
8d541e97 | 2127 | header->feat_offset = header->data_offset + header->data_size; |
7c6a1c65 | 2128 | |
d5eed904 | 2129 | if (at_exit) { |
1c0b04d1 | 2130 | err = perf_header__adds_write(header, evlist, fd); |
d5eed904 ACM |
2131 | if (err < 0) |
2132 | return err; | |
2133 | } | |
9e827dd0 | 2134 | |
7c6a1c65 PZ |
2135 | f_header = (struct perf_file_header){ |
2136 | .magic = PERF_MAGIC, | |
2137 | .size = sizeof(f_header), | |
2138 | .attr_size = sizeof(f_attr), | |
2139 | .attrs = { | |
944d62ba | 2140 | .offset = attr_offset, |
a91e5431 | 2141 | .size = evlist->nr_entries * sizeof(f_attr), |
7c6a1c65 PZ |
2142 | }, |
2143 | .data = { | |
1c0b04d1 ACM |
2144 | .offset = header->data_offset, |
2145 | .size = header->data_size, | |
7c6a1c65 | 2146 | }, |
44b3c578 | 2147 | /* event_types is ignored, store zeros */ |
7c6a1c65 PZ |
2148 | }; |
2149 | ||
1c0b04d1 | 2150 | memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); |
2ba08250 | 2151 | |
7c6a1c65 | 2152 | lseek(fd, 0, SEEK_SET); |
d5eed904 ACM |
2153 | err = do_write(fd, &f_header, sizeof(f_header)); |
2154 | if (err < 0) { | |
2155 | pr_debug("failed to write perf header\n"); | |
2156 | return err; | |
2157 | } | |
1c0b04d1 | 2158 | lseek(fd, header->data_offset + header->data_size, SEEK_SET); |
7c6a1c65 | 2159 | |
d5eed904 | 2160 | return 0; |
7c6a1c65 PZ |
2161 | } |
2162 | ||
1c0b04d1 | 2163 | static int perf_header__getbuffer64(struct perf_header *header, |
ba21594c ACM |
2164 | int fd, void *buf, size_t size) |
2165 | { | |
1e7972cc | 2166 | if (readn(fd, buf, size) <= 0) |
ba21594c ACM |
2167 | return -1; |
2168 | ||
1c0b04d1 | 2169 | if (header->needs_swap) |
ba21594c ACM |
2170 | mem_bswap_64(buf, size); |
2171 | ||
2172 | return 0; | |
2173 | } | |
2174 | ||
1c0b04d1 | 2175 | int perf_header__process_sections(struct perf_header *header, int fd, |
fbe96f29 | 2176 | void *data, |
1c0b04d1 | 2177 | int (*process)(struct perf_file_section *section, |
b1e5a9be RR |
2178 | struct perf_header *ph, |
2179 | int feat, int fd, void *data)) | |
2ba08250 | 2180 | { |
b1e5a9be | 2181 | struct perf_file_section *feat_sec, *sec; |
9e827dd0 FW |
2182 | int nr_sections; |
2183 | int sec_size; | |
b1e5a9be RR |
2184 | int feat; |
2185 | int err; | |
9e827dd0 | 2186 | |
1c0b04d1 | 2187 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
9e827dd0 | 2188 | if (!nr_sections) |
37562eac | 2189 | return 0; |
9e827dd0 | 2190 | |
91b98804 | 2191 | feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec)); |
9e827dd0 | 2192 | if (!feat_sec) |
37562eac | 2193 | return -1; |
9e827dd0 FW |
2194 | |
2195 | sec_size = sizeof(*feat_sec) * nr_sections; | |
2196 | ||
8d541e97 | 2197 | lseek(fd, header->feat_offset, SEEK_SET); |
9e827dd0 | 2198 | |
b1e5a9be RR |
2199 | err = perf_header__getbuffer64(header, fd, feat_sec, sec_size); |
2200 | if (err < 0) | |
769885f3 | 2201 | goto out_free; |
9e827dd0 | 2202 | |
b1e5a9be RR |
2203 | for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) { |
2204 | err = process(sec++, header, feat, fd, data); | |
2205 | if (err < 0) | |
2206 | goto out_free; | |
2ba08250 | 2207 | } |
b1e5a9be | 2208 | err = 0; |
769885f3 | 2209 | out_free: |
37562eac ACM |
2210 | free(feat_sec); |
2211 | return err; | |
769885f3 | 2212 | } |
4778d2e4 | 2213 | |
114382a0 SE |
2214 | static const int attr_file_abi_sizes[] = { |
2215 | [0] = PERF_ATTR_SIZE_VER0, | |
2216 | [1] = PERF_ATTR_SIZE_VER1, | |
239cc478 | 2217 | [2] = PERF_ATTR_SIZE_VER2, |
0f6a3015 | 2218 | [3] = PERF_ATTR_SIZE_VER3, |
6a21c0b5 | 2219 | [4] = PERF_ATTR_SIZE_VER4, |
114382a0 SE |
2220 | 0, |
2221 | }; | |
2222 | ||
2223 | /* | |
2224 | * In the legacy file format, the magic number is not used to encode endianness. | |
2225 | * hdr_sz was used to encode endianness. But given that hdr_sz can vary based | |
2226 | * on ABI revisions, we need to try all combinations for all endianness to | |
2227 | * detect the endianness. | |
2228 | */ | |
2229 | static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph) | |
73323f54 | 2230 | { |
114382a0 SE |
2231 | uint64_t ref_size, attr_size; |
2232 | int i; | |
73323f54 | 2233 | |
114382a0 SE |
2234 | for (i = 0 ; attr_file_abi_sizes[i]; i++) { |
2235 | ref_size = attr_file_abi_sizes[i] | |
2236 | + sizeof(struct perf_file_section); | |
2237 | if (hdr_sz != ref_size) { | |
2238 | attr_size = bswap_64(hdr_sz); | |
2239 | if (attr_size != ref_size) | |
2240 | continue; | |
73323f54 | 2241 | |
114382a0 SE |
2242 | ph->needs_swap = true; |
2243 | } | |
2244 | pr_debug("ABI%d perf.data file detected, need_swap=%d\n", | |
2245 | i, | |
2246 | ph->needs_swap); | |
2247 | return 0; | |
2248 | } | |
2249 | /* could not determine endianness */ | |
2250 | return -1; | |
2251 | } | |
73323f54 | 2252 | |
114382a0 SE |
2253 | #define PERF_PIPE_HDR_VER0 16 |
2254 | ||
2255 | static const size_t attr_pipe_abi_sizes[] = { | |
2256 | [0] = PERF_PIPE_HDR_VER0, | |
2257 | 0, | |
2258 | }; | |
2259 | ||
2260 | /* | |
2261 | * In the legacy pipe format, there is an implicit assumption that endiannesss | |
2262 | * between host recording the samples, and host parsing the samples is the | |
2263 | * same. This is not always the case given that the pipe output may always be | |
2264 | * redirected into a file and analyzed on a different machine with possibly a | |
2265 | * different endianness and perf_event ABI revsions in the perf tool itself. | |
2266 | */ | |
2267 | static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph) | |
2268 | { | |
2269 | u64 attr_size; | |
2270 | int i; | |
2271 | ||
2272 | for (i = 0 ; attr_pipe_abi_sizes[i]; i++) { | |
2273 | if (hdr_sz != attr_pipe_abi_sizes[i]) { | |
2274 | attr_size = bswap_64(hdr_sz); | |
2275 | if (attr_size != hdr_sz) | |
2276 | continue; | |
73323f54 SE |
2277 | |
2278 | ph->needs_swap = true; | |
2279 | } | |
114382a0 | 2280 | pr_debug("Pipe ABI%d perf.data file detected\n", i); |
73323f54 SE |
2281 | return 0; |
2282 | } | |
114382a0 SE |
2283 | return -1; |
2284 | } | |
2285 | ||
e84ba4e2 FT |
2286 | bool is_perf_magic(u64 magic) |
2287 | { | |
2288 | if (!memcmp(&magic, __perf_magic1, sizeof(magic)) | |
2289 | || magic == __perf_magic2 | |
2290 | || magic == __perf_magic2_sw) | |
2291 | return true; | |
2292 | ||
2293 | return false; | |
2294 | } | |
2295 | ||
114382a0 SE |
2296 | static int check_magic_endian(u64 magic, uint64_t hdr_sz, |
2297 | bool is_pipe, struct perf_header *ph) | |
2298 | { | |
2299 | int ret; | |
2300 | ||
2301 | /* check for legacy format */ | |
2302 | ret = memcmp(&magic, __perf_magic1, sizeof(magic)); | |
2303 | if (ret == 0) { | |
2a08c3ec | 2304 | ph->version = PERF_HEADER_VERSION_1; |
114382a0 SE |
2305 | pr_debug("legacy perf.data format\n"); |
2306 | if (is_pipe) | |
2307 | return try_all_pipe_abis(hdr_sz, ph); | |
2308 | ||
2309 | return try_all_file_abis(hdr_sz, ph); | |
2310 | } | |
2311 | /* | |
2312 | * the new magic number serves two purposes: | |
2313 | * - unique number to identify actual perf.data files | |
2314 | * - encode endianness of file | |
2315 | */ | |
f7913971 | 2316 | ph->version = PERF_HEADER_VERSION_2; |
73323f54 | 2317 | |
114382a0 SE |
2318 | /* check magic number with one endianness */ |
2319 | if (magic == __perf_magic2) | |
73323f54 SE |
2320 | return 0; |
2321 | ||
114382a0 SE |
2322 | /* check magic number with opposite endianness */ |
2323 | if (magic != __perf_magic2_sw) | |
73323f54 SE |
2324 | return -1; |
2325 | ||
2326 | ph->needs_swap = true; | |
2327 | ||
2328 | return 0; | |
2329 | } | |
2330 | ||
1c0b04d1 | 2331 | int perf_file_header__read(struct perf_file_header *header, |
37562eac ACM |
2332 | struct perf_header *ph, int fd) |
2333 | { | |
727ebd54 | 2334 | ssize_t ret; |
73323f54 | 2335 | |
37562eac | 2336 | lseek(fd, 0, SEEK_SET); |
37562eac | 2337 | |
73323f54 SE |
2338 | ret = readn(fd, header, sizeof(*header)); |
2339 | if (ret <= 0) | |
37562eac ACM |
2340 | return -1; |
2341 | ||
114382a0 SE |
2342 | if (check_magic_endian(header->magic, |
2343 | header->attr_size, false, ph) < 0) { | |
2344 | pr_debug("magic/endian check failed\n"); | |
73323f54 | 2345 | return -1; |
114382a0 | 2346 | } |
ba21594c | 2347 | |
73323f54 | 2348 | if (ph->needs_swap) { |
1c0b04d1 | 2349 | mem_bswap_64(header, offsetof(struct perf_file_header, |
73323f54 | 2350 | adds_features)); |
ba21594c ACM |
2351 | } |
2352 | ||
1c0b04d1 | 2353 | if (header->size != sizeof(*header)) { |
37562eac | 2354 | /* Support the previous format */ |
1c0b04d1 ACM |
2355 | if (header->size == offsetof(typeof(*header), adds_features)) |
2356 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); | |
37562eac ACM |
2357 | else |
2358 | return -1; | |
d327fa43 | 2359 | } else if (ph->needs_swap) { |
d327fa43 DA |
2360 | /* |
2361 | * feature bitmap is declared as an array of unsigned longs -- | |
2362 | * not good since its size can differ between the host that | |
2363 | * generated the data file and the host analyzing the file. | |
2364 | * | |
2365 | * We need to handle endianness, but we don't know the size of | |
2366 | * the unsigned long where the file was generated. Take a best | |
2367 | * guess at determining it: try 64-bit swap first (ie., file | |
2368 | * created on a 64-bit host), and check if the hostname feature | |
2369 | * bit is set (this feature bit is forced on as of fbe96f2). | |
2370 | * If the bit is not, undo the 64-bit swap and try a 32-bit | |
2371 | * swap. If the hostname bit is still not set (e.g., older data | |
2372 | * file), punt and fallback to the original behavior -- | |
2373 | * clearing all feature bits and setting buildid. | |
2374 | */ | |
80c0120a DA |
2375 | mem_bswap_64(&header->adds_features, |
2376 | BITS_TO_U64(HEADER_FEAT_BITS)); | |
d327fa43 DA |
2377 | |
2378 | if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { | |
80c0120a DA |
2379 | /* unswap as u64 */ |
2380 | mem_bswap_64(&header->adds_features, | |
2381 | BITS_TO_U64(HEADER_FEAT_BITS)); | |
2382 | ||
2383 | /* unswap as u32 */ | |
2384 | mem_bswap_32(&header->adds_features, | |
2385 | BITS_TO_U32(HEADER_FEAT_BITS)); | |
d327fa43 DA |
2386 | } |
2387 | ||
2388 | if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { | |
2389 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); | |
2390 | set_bit(HEADER_BUILD_ID, header->adds_features); | |
2391 | } | |
4778d2e4 | 2392 | } |
9e827dd0 | 2393 | |
1c0b04d1 | 2394 | memcpy(&ph->adds_features, &header->adds_features, |
ba21594c | 2395 | sizeof(ph->adds_features)); |
37562eac | 2396 | |
1c0b04d1 ACM |
2397 | ph->data_offset = header->data.offset; |
2398 | ph->data_size = header->data.size; | |
8d541e97 | 2399 | ph->feat_offset = header->data.offset + header->data.size; |
37562eac ACM |
2400 | return 0; |
2401 | } | |
2402 | ||
1c0b04d1 | 2403 | static int perf_file_section__process(struct perf_file_section *section, |
ba21594c | 2404 | struct perf_header *ph, |
da378962 | 2405 | int feat, int fd, void *data) |
37562eac | 2406 | { |
1c0b04d1 | 2407 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { |
9486aa38 | 2408 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " |
1c0b04d1 | 2409 | "%d, continuing...\n", section->offset, feat); |
37562eac ACM |
2410 | return 0; |
2411 | } | |
2412 | ||
b1e5a9be RR |
2413 | if (feat >= HEADER_LAST_FEATURE) { |
2414 | pr_debug("unknown feature %d, continuing...\n", feat); | |
2415 | return 0; | |
2416 | } | |
2417 | ||
f1c67db7 RR |
2418 | if (!feat_ops[feat].process) |
2419 | return 0; | |
37562eac | 2420 | |
3d7eb86b | 2421 | return feat_ops[feat].process(section, ph, fd, data); |
37562eac | 2422 | } |
2ba08250 | 2423 | |
1c0b04d1 | 2424 | static int perf_file_header__read_pipe(struct perf_pipe_file_header *header, |
454c407e TZ |
2425 | struct perf_header *ph, int fd, |
2426 | bool repipe) | |
7c6a1c65 | 2427 | { |
727ebd54 | 2428 | ssize_t ret; |
73323f54 SE |
2429 | |
2430 | ret = readn(fd, header, sizeof(*header)); | |
2431 | if (ret <= 0) | |
2432 | return -1; | |
2433 | ||
114382a0 SE |
2434 | if (check_magic_endian(header->magic, header->size, true, ph) < 0) { |
2435 | pr_debug("endian/magic failed\n"); | |
8dc58101 | 2436 | return -1; |
114382a0 SE |
2437 | } |
2438 | ||
2439 | if (ph->needs_swap) | |
2440 | header->size = bswap_64(header->size); | |
8dc58101 | 2441 | |
1c0b04d1 | 2442 | if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0) |
454c407e TZ |
2443 | return -1; |
2444 | ||
8dc58101 TZ |
2445 | return 0; |
2446 | } | |
2447 | ||
d4339569 | 2448 | static int perf_header__read_pipe(struct perf_session *session) |
8dc58101 | 2449 | { |
1c0b04d1 | 2450 | struct perf_header *header = &session->header; |
8dc58101 TZ |
2451 | struct perf_pipe_file_header f_header; |
2452 | ||
cc9784bd JO |
2453 | if (perf_file_header__read_pipe(&f_header, header, |
2454 | perf_data_file__fd(session->file), | |
454c407e | 2455 | session->repipe) < 0) { |
8dc58101 TZ |
2456 | pr_debug("incompatible file format\n"); |
2457 | return -EINVAL; | |
2458 | } | |
2459 | ||
8dc58101 TZ |
2460 | return 0; |
2461 | } | |
2462 | ||
69996df4 SE |
2463 | static int read_attr(int fd, struct perf_header *ph, |
2464 | struct perf_file_attr *f_attr) | |
2465 | { | |
2466 | struct perf_event_attr *attr = &f_attr->attr; | |
2467 | size_t sz, left; | |
2468 | size_t our_sz = sizeof(f_attr->attr); | |
727ebd54 | 2469 | ssize_t ret; |
69996df4 SE |
2470 | |
2471 | memset(f_attr, 0, sizeof(*f_attr)); | |
2472 | ||
2473 | /* read minimal guaranteed structure */ | |
2474 | ret = readn(fd, attr, PERF_ATTR_SIZE_VER0); | |
2475 | if (ret <= 0) { | |
2476 | pr_debug("cannot read %d bytes of header attr\n", | |
2477 | PERF_ATTR_SIZE_VER0); | |
2478 | return -1; | |
2479 | } | |
2480 | ||
2481 | /* on file perf_event_attr size */ | |
2482 | sz = attr->size; | |
114382a0 | 2483 | |
69996df4 SE |
2484 | if (ph->needs_swap) |
2485 | sz = bswap_32(sz); | |
2486 | ||
2487 | if (sz == 0) { | |
2488 | /* assume ABI0 */ | |
2489 | sz = PERF_ATTR_SIZE_VER0; | |
2490 | } else if (sz > our_sz) { | |
2491 | pr_debug("file uses a more recent and unsupported ABI" | |
2492 | " (%zu bytes extra)\n", sz - our_sz); | |
2493 | return -1; | |
2494 | } | |
2495 | /* what we have not yet read and that we know about */ | |
2496 | left = sz - PERF_ATTR_SIZE_VER0; | |
2497 | if (left) { | |
2498 | void *ptr = attr; | |
2499 | ptr += PERF_ATTR_SIZE_VER0; | |
2500 | ||
2501 | ret = readn(fd, ptr, left); | |
2502 | } | |
2503 | /* read perf_file_section, ids are read in caller */ | |
2504 | ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); | |
2505 | ||
2506 | return ret <= 0 ? -1 : 0; | |
2507 | } | |
2508 | ||
831394bd NK |
2509 | static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel, |
2510 | struct pevent *pevent) | |
cb9dd49e | 2511 | { |
831394bd | 2512 | struct event_format *event; |
cb9dd49e ACM |
2513 | char bf[128]; |
2514 | ||
831394bd NK |
2515 | /* already prepared */ |
2516 | if (evsel->tp_format) | |
2517 | return 0; | |
2518 | ||
3dce2ce3 NK |
2519 | if (pevent == NULL) { |
2520 | pr_debug("broken or missing trace data\n"); | |
2521 | return -1; | |
2522 | } | |
2523 | ||
831394bd | 2524 | event = pevent_find_event(pevent, evsel->attr.config); |
cb9dd49e ACM |
2525 | if (event == NULL) |
2526 | return -1; | |
2527 | ||
831394bd NK |
2528 | if (!evsel->name) { |
2529 | snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name); | |
2530 | evsel->name = strdup(bf); | |
2531 | if (evsel->name == NULL) | |
2532 | return -1; | |
2533 | } | |
cb9dd49e | 2534 | |
fcf65bf1 | 2535 | evsel->tp_format = event; |
cb9dd49e ACM |
2536 | return 0; |
2537 | } | |
2538 | ||
831394bd NK |
2539 | static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist, |
2540 | struct pevent *pevent) | |
cb9dd49e ACM |
2541 | { |
2542 | struct perf_evsel *pos; | |
2543 | ||
0050f7aa | 2544 | evlist__for_each(evlist, pos) { |
831394bd NK |
2545 | if (pos->attr.type == PERF_TYPE_TRACEPOINT && |
2546 | perf_evsel__prepare_tracepoint_event(pos, pevent)) | |
cb9dd49e ACM |
2547 | return -1; |
2548 | } | |
2549 | ||
2550 | return 0; | |
2551 | } | |
2552 | ||
d4339569 | 2553 | int perf_session__read_header(struct perf_session *session) |
8dc58101 | 2554 | { |
cc9784bd | 2555 | struct perf_data_file *file = session->file; |
1c0b04d1 | 2556 | struct perf_header *header = &session->header; |
ba21594c | 2557 | struct perf_file_header f_header; |
7c6a1c65 PZ |
2558 | struct perf_file_attr f_attr; |
2559 | u64 f_id; | |
7c6a1c65 | 2560 | int nr_attrs, nr_ids, i, j; |
cc9784bd | 2561 | int fd = perf_data_file__fd(file); |
7c6a1c65 | 2562 | |
334fe7a3 | 2563 | session->evlist = perf_evlist__new(); |
a91e5431 ACM |
2564 | if (session->evlist == NULL) |
2565 | return -ENOMEM; | |
2566 | ||
2c07144d | 2567 | session->evlist->env = &header->env; |
cc9784bd | 2568 | if (perf_data_file__is_pipe(file)) |
d4339569 | 2569 | return perf_header__read_pipe(session); |
8dc58101 | 2570 | |
69996df4 | 2571 | if (perf_file_header__read(&f_header, header, fd) < 0) |
4dc0a04b | 2572 | return -EINVAL; |
7c6a1c65 | 2573 | |
b314e5cf NK |
2574 | /* |
2575 | * Sanity check that perf.data was written cleanly; data size is | |
2576 | * initialized to 0 and updated only if the on_exit function is run. | |
2577 | * If data size is still 0 then the file contains only partial | |
2578 | * information. Just warn user and process it as much as it can. | |
2579 | */ | |
2580 | if (f_header.data.size == 0) { | |
2581 | pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n" | |
2582 | "Was the 'perf record' command properly terminated?\n", | |
cc9784bd | 2583 | file->path); |
b314e5cf NK |
2584 | } |
2585 | ||
69996df4 | 2586 | nr_attrs = f_header.attrs.size / f_header.attr_size; |
7c6a1c65 PZ |
2587 | lseek(fd, f_header.attrs.offset, SEEK_SET); |
2588 | ||
2589 | for (i = 0; i < nr_attrs; i++) { | |
a91e5431 | 2590 | struct perf_evsel *evsel; |
1c222bce | 2591 | off_t tmp; |
7c6a1c65 | 2592 | |
69996df4 | 2593 | if (read_attr(fd, header, &f_attr) < 0) |
769885f3 | 2594 | goto out_errno; |
ba21594c | 2595 | |
1060ab85 DA |
2596 | if (header->needs_swap) { |
2597 | f_attr.ids.size = bswap_64(f_attr.ids.size); | |
2598 | f_attr.ids.offset = bswap_64(f_attr.ids.offset); | |
eda3913b | 2599 | perf_event__attr_swap(&f_attr.attr); |
1060ab85 | 2600 | } |
eda3913b | 2601 | |
1c222bce | 2602 | tmp = lseek(fd, 0, SEEK_CUR); |
ef503831 | 2603 | evsel = perf_evsel__new(&f_attr.attr); |
7c6a1c65 | 2604 | |
a91e5431 ACM |
2605 | if (evsel == NULL) |
2606 | goto out_delete_evlist; | |
0807d2d8 ACM |
2607 | |
2608 | evsel->needs_swap = header->needs_swap; | |
a91e5431 ACM |
2609 | /* |
2610 | * Do it before so that if perf_evsel__alloc_id fails, this | |
2611 | * entry gets purged too at perf_evlist__delete(). | |
2612 | */ | |
2613 | perf_evlist__add(session->evlist, evsel); | |
7c6a1c65 PZ |
2614 | |
2615 | nr_ids = f_attr.ids.size / sizeof(u64); | |
a91e5431 ACM |
2616 | /* |
2617 | * We don't have the cpu and thread maps on the header, so | |
2618 | * for allocating the perf_sample_id table we fake 1 cpu and | |
2619 | * hattr->ids threads. | |
2620 | */ | |
2621 | if (perf_evsel__alloc_id(evsel, 1, nr_ids)) | |
2622 | goto out_delete_evlist; | |
2623 | ||
7c6a1c65 PZ |
2624 | lseek(fd, f_attr.ids.offset, SEEK_SET); |
2625 | ||
2626 | for (j = 0; j < nr_ids; j++) { | |
1c0b04d1 | 2627 | if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id))) |
769885f3 | 2628 | goto out_errno; |
7c6a1c65 | 2629 | |
a91e5431 | 2630 | perf_evlist__id_add(session->evlist, evsel, 0, j, f_id); |
7c6a1c65 | 2631 | } |
11deb1f9 | 2632 | |
7c6a1c65 PZ |
2633 | lseek(fd, tmp, SEEK_SET); |
2634 | } | |
2635 | ||
d04b35f8 ACM |
2636 | symbol_conf.nr_events = nr_attrs; |
2637 | ||
29f5ffd3 | 2638 | perf_header__process_sections(header, fd, &session->tevent, |
fbe96f29 | 2639 | perf_file_section__process); |
4778d2e4 | 2640 | |
831394bd | 2641 | if (perf_evlist__prepare_tracepoint_events(session->evlist, |
29f5ffd3 | 2642 | session->tevent.pevent)) |
cb9dd49e ACM |
2643 | goto out_delete_evlist; |
2644 | ||
4dc0a04b | 2645 | return 0; |
769885f3 ACM |
2646 | out_errno: |
2647 | return -errno; | |
a91e5431 ACM |
2648 | |
2649 | out_delete_evlist: | |
2650 | perf_evlist__delete(session->evlist); | |
2651 | session->evlist = NULL; | |
2652 | return -ENOMEM; | |
7c6a1c65 | 2653 | } |
0d3a5c88 | 2654 | |
45694aa7 | 2655 | int perf_event__synthesize_attr(struct perf_tool *tool, |
f4d83436 | 2656 | struct perf_event_attr *attr, u32 ids, u64 *id, |
743eb868 | 2657 | perf_event__handler_t process) |
2c46dbb5 | 2658 | { |
8115d60c | 2659 | union perf_event *ev; |
2c46dbb5 TZ |
2660 | size_t size; |
2661 | int err; | |
2662 | ||
2663 | size = sizeof(struct perf_event_attr); | |
9ac3e487 | 2664 | size = PERF_ALIGN(size, sizeof(u64)); |
2c46dbb5 TZ |
2665 | size += sizeof(struct perf_event_header); |
2666 | size += ids * sizeof(u64); | |
2667 | ||
2668 | ev = malloc(size); | |
2669 | ||
ce47dc56 CS |
2670 | if (ev == NULL) |
2671 | return -ENOMEM; | |
2672 | ||
2c46dbb5 TZ |
2673 | ev->attr.attr = *attr; |
2674 | memcpy(ev->attr.id, id, ids * sizeof(u64)); | |
2675 | ||
2676 | ev->attr.header.type = PERF_RECORD_HEADER_ATTR; | |
f4d83436 | 2677 | ev->attr.header.size = (u16)size; |
2c46dbb5 | 2678 | |
f4d83436 RR |
2679 | if (ev->attr.header.size == size) |
2680 | err = process(tool, ev, NULL, NULL); | |
2681 | else | |
2682 | err = -E2BIG; | |
2c46dbb5 TZ |
2683 | |
2684 | free(ev); | |
2685 | ||
2686 | return err; | |
2687 | } | |
2688 | ||
45694aa7 | 2689 | int perf_event__synthesize_attrs(struct perf_tool *tool, |
d20deb64 | 2690 | struct perf_session *session, |
a91e5431 | 2691 | perf_event__handler_t process) |
2c46dbb5 | 2692 | { |
6606f873 | 2693 | struct perf_evsel *evsel; |
a91e5431 | 2694 | int err = 0; |
2c46dbb5 | 2695 | |
0050f7aa | 2696 | evlist__for_each(session->evlist, evsel) { |
6606f873 RR |
2697 | err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids, |
2698 | evsel->id, process); | |
2c46dbb5 TZ |
2699 | if (err) { |
2700 | pr_debug("failed to create perf header attribute\n"); | |
2701 | return err; | |
2702 | } | |
2703 | } | |
2704 | ||
2705 | return err; | |
2706 | } | |
2707 | ||
47c3d109 AH |
2708 | int perf_event__process_attr(struct perf_tool *tool __maybe_unused, |
2709 | union perf_event *event, | |
10d0f086 | 2710 | struct perf_evlist **pevlist) |
2c46dbb5 | 2711 | { |
f4d83436 | 2712 | u32 i, ids, n_ids; |
a91e5431 | 2713 | struct perf_evsel *evsel; |
10d0f086 | 2714 | struct perf_evlist *evlist = *pevlist; |
2c46dbb5 | 2715 | |
10d0f086 | 2716 | if (evlist == NULL) { |
334fe7a3 | 2717 | *pevlist = evlist = perf_evlist__new(); |
10d0f086 | 2718 | if (evlist == NULL) |
a91e5431 ACM |
2719 | return -ENOMEM; |
2720 | } | |
2721 | ||
ef503831 | 2722 | evsel = perf_evsel__new(&event->attr.attr); |
a91e5431 | 2723 | if (evsel == NULL) |
2c46dbb5 TZ |
2724 | return -ENOMEM; |
2725 | ||
10d0f086 | 2726 | perf_evlist__add(evlist, evsel); |
a91e5431 | 2727 | |
8115d60c ACM |
2728 | ids = event->header.size; |
2729 | ids -= (void *)&event->attr.id - (void *)event; | |
2c46dbb5 | 2730 | n_ids = ids / sizeof(u64); |
a91e5431 ACM |
2731 | /* |
2732 | * We don't have the cpu and thread maps on the header, so | |
2733 | * for allocating the perf_sample_id table we fake 1 cpu and | |
2734 | * hattr->ids threads. | |
2735 | */ | |
2736 | if (perf_evsel__alloc_id(evsel, 1, n_ids)) | |
2737 | return -ENOMEM; | |
2c46dbb5 TZ |
2738 | |
2739 | for (i = 0; i < n_ids; i++) { | |
10d0f086 | 2740 | perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]); |
2c46dbb5 TZ |
2741 | } |
2742 | ||
7e0d6fc9 AH |
2743 | symbol_conf.nr_events = evlist->nr_entries; |
2744 | ||
2c46dbb5 TZ |
2745 | return 0; |
2746 | } | |
cd19a035 | 2747 | |
45694aa7 | 2748 | int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, |
d20deb64 | 2749 | struct perf_evlist *evlist, |
743eb868 | 2750 | perf_event__handler_t process) |
9215545e | 2751 | { |
8115d60c | 2752 | union perf_event ev; |
29208e57 | 2753 | struct tracing_data *tdata; |
9215545e | 2754 | ssize_t size = 0, aligned_size = 0, padding; |
1d037ca1 | 2755 | int err __maybe_unused = 0; |
9215545e | 2756 | |
29208e57 JO |
2757 | /* |
2758 | * We are going to store the size of the data followed | |
2759 | * by the data contents. Since the fd descriptor is a pipe, | |
2760 | * we cannot seek back to store the size of the data once | |
2761 | * we know it. Instead we: | |
2762 | * | |
2763 | * - write the tracing data to the temp file | |
2764 | * - get/write the data size to pipe | |
2765 | * - write the tracing data from the temp file | |
2766 | * to the pipe | |
2767 | */ | |
2768 | tdata = tracing_data_get(&evlist->entries, fd, true); | |
2769 | if (!tdata) | |
2770 | return -1; | |
2771 | ||
9215545e TZ |
2772 | memset(&ev, 0, sizeof(ev)); |
2773 | ||
2774 | ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA; | |
29208e57 | 2775 | size = tdata->size; |
9ac3e487 | 2776 | aligned_size = PERF_ALIGN(size, sizeof(u64)); |
9215545e TZ |
2777 | padding = aligned_size - size; |
2778 | ev.tracing_data.header.size = sizeof(ev.tracing_data); | |
2779 | ev.tracing_data.size = aligned_size; | |
2780 | ||
45694aa7 | 2781 | process(tool, &ev, NULL, NULL); |
9215545e | 2782 | |
29208e57 JO |
2783 | /* |
2784 | * The put function will copy all the tracing data | |
2785 | * stored in temp file to the pipe. | |
2786 | */ | |
2787 | tracing_data_put(tdata); | |
2788 | ||
9215545e TZ |
2789 | write_padded(fd, NULL, 0, padding); |
2790 | ||
2791 | return aligned_size; | |
2792 | } | |
2793 | ||
47c3d109 AH |
2794 | int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused, |
2795 | union perf_event *event, | |
8115d60c | 2796 | struct perf_session *session) |
9215545e | 2797 | { |
8115d60c | 2798 | ssize_t size_read, padding, size = event->tracing_data.size; |
cc9784bd JO |
2799 | int fd = perf_data_file__fd(session->file); |
2800 | off_t offset = lseek(fd, 0, SEEK_CUR); | |
9215545e TZ |
2801 | char buf[BUFSIZ]; |
2802 | ||
2803 | /* setup for reading amidst mmap */ | |
cc9784bd | 2804 | lseek(fd, offset + sizeof(struct tracing_data_event), |
9215545e TZ |
2805 | SEEK_SET); |
2806 | ||
29f5ffd3 | 2807 | size_read = trace_report(fd, &session->tevent, |
da378962 | 2808 | session->repipe); |
9ac3e487 | 2809 | padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read; |
9215545e | 2810 | |
cc9784bd | 2811 | if (readn(fd, buf, padding) < 0) { |
2caa48a2 ACM |
2812 | pr_err("%s: reading input file", __func__); |
2813 | return -1; | |
2814 | } | |
454c407e TZ |
2815 | if (session->repipe) { |
2816 | int retw = write(STDOUT_FILENO, buf, padding); | |
2caa48a2 ACM |
2817 | if (retw <= 0 || retw != padding) { |
2818 | pr_err("%s: repiping tracing data padding", __func__); | |
2819 | return -1; | |
2820 | } | |
454c407e | 2821 | } |
9215545e | 2822 | |
2caa48a2 ACM |
2823 | if (size_read + padding != size) { |
2824 | pr_err("%s: tracing data size mismatch", __func__); | |
2825 | return -1; | |
2826 | } | |
9215545e | 2827 | |
831394bd | 2828 | perf_evlist__prepare_tracepoint_events(session->evlist, |
29f5ffd3 | 2829 | session->tevent.pevent); |
8b6ee4c5 | 2830 | |
9215545e TZ |
2831 | return size_read + padding; |
2832 | } | |
c7929e47 | 2833 | |
45694aa7 | 2834 | int perf_event__synthesize_build_id(struct perf_tool *tool, |
d20deb64 | 2835 | struct dso *pos, u16 misc, |
8115d60c | 2836 | perf_event__handler_t process, |
743eb868 | 2837 | struct machine *machine) |
c7929e47 | 2838 | { |
8115d60c | 2839 | union perf_event ev; |
c7929e47 TZ |
2840 | size_t len; |
2841 | int err = 0; | |
2842 | ||
2843 | if (!pos->hit) | |
2844 | return err; | |
2845 | ||
2846 | memset(&ev, 0, sizeof(ev)); | |
2847 | ||
2848 | len = pos->long_name_len + 1; | |
9ac3e487 | 2849 | len = PERF_ALIGN(len, NAME_ALIGN); |
c7929e47 TZ |
2850 | memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id)); |
2851 | ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID; | |
2852 | ev.build_id.header.misc = misc; | |
23346f21 | 2853 | ev.build_id.pid = machine->pid; |
c7929e47 TZ |
2854 | ev.build_id.header.size = sizeof(ev.build_id) + len; |
2855 | memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len); | |
2856 | ||
45694aa7 | 2857 | err = process(tool, &ev, NULL, machine); |
c7929e47 TZ |
2858 | |
2859 | return err; | |
2860 | } | |
2861 | ||
1d037ca1 | 2862 | int perf_event__process_build_id(struct perf_tool *tool __maybe_unused, |
d20deb64 | 2863 | union perf_event *event, |
8115d60c | 2864 | struct perf_session *session) |
c7929e47 | 2865 | { |
8115d60c ACM |
2866 | __event_process_build_id(&event->build_id, |
2867 | event->build_id.filename, | |
a1645ce1 | 2868 | session); |
c7929e47 TZ |
2869 | return 0; |
2870 | } |