12 #include <linux/compiler.h> 13 #include <linux/list.h> 14 #include <linux/kernel.h> 15 #include <linux/bitops.h> 16 #include <linux/stringify.h> 18 #include <sys/utsname.h> 19 #include <linux/time64.h> 37 #include <api/fs/fs.h> 58 #define PERF_MAGIC __perf_magic2 63 struct perf_event_attr
attr;
78 set_bit(feat, header->adds_features);
83 clear_bit(feat, header->adds_features);
88 return test_bit(feat, header->adds_features);
93 ssize_t ret =
writen(ff->
fd, buf, size);
95 if (ret != (ssize_t)size)
96 return ret < 0 ? (int)ret : -1;
103 const size_t max_size = 0xffff -
sizeof(
struct perf_event_header);
104 size_t new_size = ff->
size;
107 if (size + ff->
offset > max_size)
110 while (size > (new_size - ff->
offset))
112 new_size =
min(max_size, new_size);
114 if (ff->
size < new_size) {
115 addr = realloc(ff->
buf, new_size);
139 u64 *p = (u64 *)
set;
142 ret =
do_write(ff, &size,
sizeof(size));
146 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
147 ret =
do_write(ff, p + i,
sizeof(*p));
157 size_t count,
size_t count_aligned)
163 err =
do_write(ff, zero_buf, count_aligned - count);
168 #define string_size(str) \ 169 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32)) 177 olen = strlen(str) + 1;
181 ret =
do_write(ff, &len,
sizeof(len));
190 ssize_t ret =
readn(ff->
fd, addr, size);
193 return ret < 0 ? (int)ret : -1;
220 ret =
__do_read(ff, addr,
sizeof(*addr));
225 *addr = bswap_32(*addr);
233 ret =
__do_read(ff, addr,
sizeof(*addr));
238 *addr = bswap_64(*addr);
278 set = bitmap_alloc(size);
282 bitmap_zero(
set, size);
286 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
302 if (WARN(ff->
buf,
"Error: calling %s in pipe-mode.\n", __func__))
319 if (WARN(ff->
buf,
"Error: calling %s in pipe-mode.\n", __func__))
324 pr_debug(
"failed to write buildid table\n");
382 const char *search = cpuinfo_proc;
389 file = fopen(
"/proc/cpuinfo",
"r");
393 while (getline(&buf, &len, file) > 0) {
394 ret = strncmp(buf, search, strlen(search));
406 p = strchr(buf,
':');
407 if (p && *(p+1) ==
' ' && *(p+2))
423 while ((*r++ = *q++));
440 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
459 nr = sysconf(_SC_NPROCESSORS_ONLN);
463 nra = (u32)(nr & UINT_MAX);
465 ret =
do_write(ff, &nrc,
sizeof(nrc));
469 return do_write(ff, &nra,
sizeof(nra));
484 ret =
do_write(ff, &nre,
sizeof(nre));
491 sz = (u32)
sizeof(evsel->
attr);
492 ret =
do_write(ff, &sz,
sizeof(sz));
508 ret =
do_write(ff, &nri,
sizeof(nri));
531 char buf[MAXPATHLEN];
536 ret = readlink(
"/proc/self/exe", buf,
sizeof(buf) - 1);
562 #define CORE_SIB_FMT \ 563 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list" 564 #define THRD_SIB_FMT \ 565 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list" 579 char *buf = NULL, *p;
586 fp = fopen(filename,
"r");
590 sret = getline(&buf, &len, fp);
595 p = strchr(buf,
'\n');
599 for (i = 0; i < tp->
core_sib; i++) {
613 fp = fopen(filename,
"r");
617 if (getline(&buf, &len, fp) <= 0)
620 p = strchr(buf,
'\n');
672 pr_debug(
"failed to get system cpumap\n");
676 nr = (u32)(ncpus & UINT_MAX);
678 sz = nr *
sizeof(
char *);
679 addr = calloc(1,
sizeof(*tp) + 2 * sz);
690 for (i = 0; i <
nr; i++) {
723 for (i = 0; i < tp->
core_sib; i++) {
768 fp = fopen(
"/proc/meminfo",
"r");
772 while (getline(&buf, &len, fp) > 0) {
773 ret = strncmp(buf,
"MemTotal:", 9);
778 n = sscanf(buf,
"%*s %"PRIu64, &mem);
780 ret =
do_write(ff, &mem,
sizeof(mem));
790 char str[MAXPATHLEN];
792 char *buf = NULL, *p;
795 u64 mem_total, mem_free,
mem;
798 sprintf(str,
"/sys/devices/system/node/node%d/meminfo", node);
799 fp = fopen(str,
"r");
803 while (getline(&buf, &len, fp) > 0) {
805 if (!strchr(buf,
':'))
807 if (sscanf(buf,
"%*s %*d %31s %"PRIu64, field, &mem) != 2)
809 if (!strcmp(field,
"MemTotal:"))
811 if (!strcmp(field,
"MemFree:"))
818 ret =
do_write(ff, &mem_total,
sizeof(u64));
822 ret =
do_write(ff, &mem_free,
sizeof(u64));
827 sprintf(str,
"/sys/devices/system/node/node%d/cpulist", node);
829 fp = fopen(str,
"r");
833 if (getline(&buf, &len, fp) <= 0)
836 p = strchr(buf,
'\n');
854 struct cpu_map *node_map = NULL;
859 fp = fopen(
"/sys/devices/system/node/online",
"r");
863 if (getline(&buf, &len, fp) <= 0)
866 c = strchr(buf,
'\n');
874 nr = (u32)node_map->
nr;
876 ret =
do_write(ff, &nr,
sizeof(nr));
880 for (i = 0; i <
nr; i++) {
881 j = (u32)node_map->
map[i];
926 ret =
do_write(ff, &pmu_num,
sizeof(pmu_num));
965 ret =
do_write(ff, &nr_groups,
sizeof(nr_groups));
973 u32 leader_idx = evsel->
idx;
980 ret =
do_write(ff, &leader_idx,
sizeof(leader_idx));
984 ret =
do_write(ff, &nr_members,
sizeof(nr_members));
996 int __weak
get_cpuid(
char *buffer __maybe_unused,
size_t sz __maybe_unused)
1007 ret =
get_cpuid(buffer,
sizeof(buffer));
1028 if (WARN(ff->
buf,
"Error: calling %s in pipe-mode.\n", __func__))
1035 pr_err(
"Failed to write auxtrace index\n");
1067 if (strcmp(a->
map, b->
map))
1079 scnprintf(path,
PATH_MAX,
"devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1080 scnprintf(file,
PATH_MAX,
"%s/%s", sysfs__mountpoint(), path);
1082 if (stat(file, &st))
1085 scnprintf(file,
PATH_MAX,
"%s/level", path);
1086 if (sysfs__read_int(file, (
int *) &cache->
level))
1089 scnprintf(file,
PATH_MAX,
"%s/coherency_line_size", path);
1090 if (sysfs__read_int(file, (
int *) &cache->
line_size))
1093 scnprintf(file,
PATH_MAX,
"%s/number_of_sets", path);
1094 if (sysfs__read_int(file, (
int *) &cache->
sets))
1097 scnprintf(file,
PATH_MAX,
"%s/ways_of_associativity", path);
1098 if (sysfs__read_int(file, (
int *) &cache->
ways))
1101 scnprintf(file,
PATH_MAX,
"%s/type", path);
1102 if (sysfs__read_str(file, &cache->
type, &len))
1105 cache->
type[len] = 0;
1108 scnprintf(file,
PATH_MAX,
"%s/size", path);
1109 if (sysfs__read_str(file, &cache->
size, &len)) {
1114 cache->
size[len] = 0;
1117 scnprintf(file,
PATH_MAX,
"%s/shared_cpu_list", path);
1118 if (sysfs__read_str(file, &cache->
map, &len)) {
1124 cache->
map[len] = 0;
1141 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1145 nr = (u32)(ncpus & UINT_MAX);
1147 for (cpu = 0; cpu < nr; cpu++) {
1148 for (level = 0; level < 10; level++) {
1159 for (i = 0; i < cnt; i++) {
1169 if (WARN_ONCE(cnt == size,
"way too many cpu caches.."))
1178 #define MAX_CACHES 2000 1193 ret =
do_write(ff, &version,
sizeof(u32));
1197 ret =
do_write(ff, &cnt,
sizeof(u32));
1201 for (i = 0; i < cnt; i++) {
1205 ret = do_write(ff, &c->v, sizeof(u32)); \ 1216 ret = do_write_string(ff, (const char *) c->v); \ 1227 for (i = 0; i < cnt; i++)
1255 unsigned int phys,
size = 0;
1260 #define for_each_memory(mem, dir) \ 1261 while ((ent = readdir(dir))) \ 1262 if (strcmp(ent->d_name, ".") && \ 1263 strcmp(ent->d_name, "..") && \ 1264 sscanf(ent->d_name, "memory%u", &mem) == 1) 1267 "%s/devices/system/node/node%lu",
1268 sysfs__mountpoint(), idx);
1270 dir = opendir(path);
1272 pr_warning(
"failed: cant' open memory sysfs data\n");
1277 size = max(phys, size);
1282 n->
set = bitmap_alloc(size);
1288 bitmap_zero(n->
set, size);
1295 set_bit(phys, n->
set);
1318 scnprintf(path,
PATH_MAX,
"%s/devices/system/node/",
1319 sysfs__mountpoint());
1321 dir = opendir(path);
1323 pr_debug2(
"%s: could't read %s, does this arch have topology information?\n",
1328 while (!ret && (ent = readdir(dir))) {
1332 if (!strcmp(ent->d_name,
".") ||
1333 !strcmp(ent->d_name,
".."))
1336 r = sscanf(ent->d_name,
"node%u", &idx);
1340 if (WARN_ONCE(cnt >= size,
1341 "failed to write MEM_TOPOLOGY, way too many nodes\n"))
1356 #define MAX_MEMORY_NODES 2000 1376 static struct memory_node nodes[MAX_MEMORY_NODES];
1377 u64 bsize,
version = 1, i, nr;
1380 ret = sysfs__read_xll(
"devices/system/memory/block_size_bytes",
1381 (
unsigned long long *) &bsize);
1389 ret =
do_write(ff, &version,
sizeof(version));
1393 ret =
do_write(ff, &bsize,
sizeof(bsize));
1397 ret =
do_write(ff, &nr,
sizeof(nr));
1401 for (i = 0; i < nr; i++) {
1405 ret = do_write(ff, &n->v, sizeof(n->v)); \ 1435 fprintf(fp,
"# arch : %s\n", ff->
ph->
env.
arch);
1451 fprintf(fp,
"# perf version : %s\n", ff->
ph->
env.
version);
1460 fprintf(fp,
"# cmdline : ");
1462 for (i = 0; i < nr; i++) {
1469 char *quote = strchr(argv_i,
'\'');
1473 fprintf(fp,
"%s\\\'", argv_i);
1476 fprintf(fp,
"%s ", argv_i);
1493 for (i = 0; i < nr; i++) {
1494 fprintf(fp,
"# sibling cores : %s\n", str);
1495 str += strlen(str) + 1;
1501 for (i = 0; i < nr; i++) {
1502 fprintf(fp,
"# sibling threads : %s\n", str);
1503 str += strlen(str) + 1;
1506 if (ph->
env.
cpu != NULL) {
1507 for (i = 0; i < cpu_nr; i++)
1508 fprintf(fp,
"# CPU %d: Core ID %d, Socket ID %d\n", i,
1511 fprintf(fp,
"# Core ID and Socket ID information is not available\n");
1521 for (evsel = events; evsel->
attr.size; evsel++) {
1534 u32 nre, sz, nr, i, j;
1550 events = calloc(nre + 1,
sizeof(*events));
1554 msz =
sizeof(evsel->
attr);
1558 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1586 id = calloc(nr,
sizeof(*
id));
1592 for (j = 0 ; j < nr; j++) {
1608 void *
priv __maybe_unused)
1610 return fprintf(fp,
", %s = %s", name, val);
1625 fprintf(fp,
"# event desc: not available or unable to read\n");
1629 for (evsel = events; evsel->
attr.size; evsel++) {
1630 fprintf(fp,
"# event : name = %s, ", evsel->
name);
1633 fprintf(fp,
", id = {");
1634 for (j = 0,
id = evsel->
id; j < evsel->ids; j++,
id++) {
1637 fprintf(fp,
" %"PRIu64, *
id);
1653 fprintf(fp,
"# total memory : %llu kB\n", ff->
ph->
env.
total_mem);
1664 fprintf(fp,
"# node%u meminfo : total = %"PRIu64
" kB," 1665 " free = %"PRIu64
" kB\n",
1668 fprintf(fp,
"# node%u cpu list : ", n->
node);
1675 fprintf(fp,
"# cpuid : %s\n", ff->
ph->
env.
cpuid);
1680 fprintf(fp,
"# contains samples with branch stack\n");
1685 fprintf(fp,
"# contains AUX area data (e.g. instruction trace)\n");
1690 fprintf(fp,
"# contains stat data\n");
1697 fprintf(fp,
"# CPU cache info:\n");
1706 const char *delimiter =
"# pmu mappings: ";
1713 fprintf(fp,
"# pmu mappings: not available\n");
1720 type = strtoul(str, &tmp, 0);
1725 fprintf(fp,
"%s%s = %" PRIu32, delimiter, str, type);
1728 str += strlen(str) + 1;
1737 fprintf(fp,
"# pmu mappings: unable to read\n");
1751 fprintf(fp,
"# group: %s{%s", evsel->
group_name ?:
"",
1773 time_buf,
sizeof(time_buf));
1774 fprintf(fp,
"# time of first sample : %s\n", time_buf);
1777 time_buf,
sizeof(time_buf));
1778 fprintf(fp,
"# time of last sample : %s\n", time_buf);
1783 fprintf(fp,
"# sample duration : %10.3f ms\n", d);
1787 unsigned long long bsize, FILE *fp)
1789 char buf_map[100], buf_size[50];
1790 unsigned long long size;
1792 size = bsize * bitmap_weight(n->
set, n->
size);
1795 bitmap_scnprintf(n->
set, n->
size, buf_map, 100);
1796 fprintf(fp,
"# %3" PRIu64
" [%s]: %s\n", n->
node, buf_size, buf_map);
1807 fprintf(fp,
"# memory nodes (nr %d, block size 0x%llx):\n",
1810 for (i = 0; i < nr; i++) {
1829 cpumode = bev->
header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1832 case PERF_RECORD_MISC_KERNEL:
1835 case PERF_RECORD_MISC_GUEST_KERNEL:
1838 case PERF_RECORD_MISC_USER:
1839 case PERF_RECORD_MISC_GUEST_USER:
1865 pr_debug(
"build id event received for %s: %s\n",
1880 struct perf_event_header header;
1886 u64 limit = offset +
size;
1888 while (offset < limit) {
1891 if (
readn(input, &old_bev,
sizeof(old_bev)) !=
sizeof(old_bev))
1897 len = old_bev.header.size -
sizeof(old_bev);
1898 if (
readn(input, filename, len) != len)
1901 bev.
header = old_bev.header;
1908 if (bev.
header.misc == PERF_RECORD_MISC_GUEST_USER ||
1909 bev.
header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1915 offset += bev.
header.size;
1927 u64 limit = offset +
size, orig_offset = offset;
1930 while (offset < limit) {
1933 if (
readn(input, &bev,
sizeof(bev)) !=
sizeof(bev))
1939 len = bev.
header.size -
sizeof(bev);
1940 if (
readn(input, filename, len) != len)
1955 if (memcmp(filename,
"nel.kallsyms]", 13) == 0) {
1956 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1963 offset += bev.
header.size;
1971 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \ 1972 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \ 1974 ff->ph->env.__feat_env = do_read_string(ff); \ 1975 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \ 1989 return ret < 0 ? -1 : 0;
1995 pr_debug(
"Failed to read buildids, continuing...\n");
2002 u32 nr_cpus_avail, nr_cpus_online;
2034 if (evsel->
idx == idx)
2077 for (evsel = events; evsel->
attr.size; evsel++)
2088 char *
str, *cmdline = NULL, **argv = NULL;
2100 argv =
zalloc(
sizeof(
char *) * (nr + 1));
2104 for (i = 0; i < nr; i++) {
2109 argv[i] = cmdline + len;
2110 memcpy(argv[i], str, strlen(str) + 1);
2111 len += strlen(str) + 1;
2141 size +=
sizeof(u32);
2145 for (i = 0; i < nr; i++) {
2151 if (
strbuf_add(&sb, str, strlen(str) + 1) < 0)
2162 size +=
sizeof(u32);
2164 for (i = 0; i < nr; i++) {
2170 if (
strbuf_add(&sb, str, strlen(str) + 1) < 0)
2181 if (ff->
size <= size) {
2186 for (i = 0; i < (u32)cpu_nr; i++) {
2195 if (nr != (u32)-1 && nr > (u32)cpu_nr) {
2196 pr_debug(
"socket_id number is too big." 2197 "You may need to upgrade the perf tool.\n");
2223 nodes =
zalloc(
sizeof(*nodes) * nr);
2227 for (i = 0; i < nr; i++) {
2270 pr_debug(
"pmu mappings not available\n");
2292 if (!strcmp(name,
"msr"))
2309 u32 i, nr, nr_groups;
2323 pr_debug(
"group desc not available\n");
2327 desc = calloc(nr_groups,
sizeof(*
desc));
2331 for (i = 0; i < nr_groups; i++) {
2351 if (evsel->
idx == (
int)
desc[i].leader_idx) {
2354 if (strcmp(
desc[i].
name,
"{anon_group}")) {
2356 desc[i].name = NULL;
2360 if (i >= nr_groups || nr > 0) {
2376 if (i != nr_groups || nr != 0) {
2383 for (i = 0; i < nr_groups; i++)
2400 pr_err(
"Failed to process auxtrace index\n");
2418 caches =
zalloc(
sizeof(*caches) * cnt);
2422 for (i = 0; i < cnt; i++) {
2426 if (do_read_u32(ff, &c.v))\ 2427 goto out_free_caches; \ 2436 c.v = do_read_string(ff); \ 2438 goto out_free_caches; 2459 u64 first_sample_time, last_sample_time;
2478 void *
data __maybe_unused)
2496 nodes =
zalloc(
sizeof(*nodes) * nr);
2500 for (i = 0; i < nr; i++) {
2504 if (do_read_u64(ff, &n.v)) \ 2538 #define FEAT_OPR(n, func, __full_only) \ 2540 .name = __stringify(n), \ 2541 .write = write_##func, \ 2542 .print = print_##func, \ 2543 .full_only = __full_only, \ 2544 .process = process_##func, \ 2545 .synthesize = true \ 2548 #define FEAT_OPN(n, func, __full_only) \ 2550 .name = __stringify(n), \ 2551 .write = write_##func, \ 2552 .print = print_##func, \ 2553 .full_only = __full_only, \ 2554 .process = process_##func \ 2558 #define print_tracing_data NULL 2559 #define print_build_id NULL 2561 #define process_branch_stack NULL 2562 #define process_stat NULL 2567 FEAT_OPN(BUILD_ID, build_id,
false),
2568 FEAT_OPR(HOSTNAME, hostname,
false),
2569 FEAT_OPR(OSRELEASE, osrelease,
false),
2575 FEAT_OPR(TOTAL_MEM, total_mem,
false),
2576 FEAT_OPR(EVENT_DESC, event_desc,
false),
2578 FEAT_OPR(CPU_TOPOLOGY, cpu_topology,
true),
2579 FEAT_OPR(NUMA_TOPOLOGY, numa_topology,
true),
2581 FEAT_OPR(PMU_MAPPINGS, pmu_mappings,
false),
2582 FEAT_OPN(GROUP_DESC, group_desc,
false),
2586 FEAT_OPR(SAMPLE_TIME, sample_time,
false),
2587 FEAT_OPR(MEM_TOPOLOGY, mem_topology,
true),
2597 int feat,
int fd,
void *
data)
2602 if (lseek(fd, section->
offset, SEEK_SET) == (off_t)-1) {
2603 pr_debug(
"Failed to lseek to %" PRIu64
" offset for feature " 2604 "%d, continuing...\n", section->
offset, feat);
2611 if (!feat_ops[feat].print)
2619 if (!feat_ops[feat].full_only || hd->
full)
2620 feat_ops[feat].
print(&ff, hd->
fp);
2622 fprintf(hd->
fp,
"# %s info available, use -I to display\n",
2623 feat_ops[feat].
name);
2639 ret = fstat(fd, &st);
2643 fprintf(fp,
"# captured on : %s", ctime(&st.st_ctime));
2645 fprintf(fp,
"# header version : %u\n", header->
version);
2646 fprintf(fp,
"# data offset : %" PRIu64
"\n", header->
data_offset);
2647 fprintf(fp,
"# data size : %" PRIu64
"\n", header->
data_size);
2648 fprintf(fp,
"# feat offset : %" PRIu64
"\n", header->
feat_offset);
2656 fprintf(fp,
"# missing features: ");
2659 fprintf(fp,
"%s ", feat_ops[bit].
name);
2674 if (!feat_ops[type].write)
2677 if (WARN(ff->
buf,
"Error: calling %s in pipe-mode.\n", __func__))
2680 (*p)->offset = lseek(ff->
fd, 0, SEEK_CUR);
2682 err = feat_ops[type].
write(ff, evlist);
2684 pr_debug(
"failed to write feature %s\n", feat_ops[type].
name);
2687 lseek(ff->
fd, (*p)->offset, SEEK_SET);
2691 (*p)->size = lseek(ff->
fd, 0, SEEK_CUR) - (*p)->offset;
2717 feat_sec = p = calloc(nr_sections,
sizeof(*feat_sec));
2718 if (feat_sec == NULL)
2721 sec_size =
sizeof(*feat_sec) * nr_sections;
2724 lseek(fd, sec_start + sec_size, SEEK_SET);
2731 lseek(fd, sec_start, SEEK_SET);
2736 err =
do_write(&ff, feat_sec, sec_size);
2738 pr_debug(
"failed to write feature section\n");
2753 .size =
sizeof(f_header),
2756 err =
do_write(&ff, &f_header,
sizeof(f_header));
2758 pr_debug(
"failed to write perf pipe header\n");
2767 int fd,
bool at_exit)
2778 lseek(fd,
sizeof(f_header), SEEK_SET);
2781 evsel->
id_offset = lseek(fd, 0, SEEK_CUR);
2784 pr_debug(
"failed to write perf header\n");
2789 attr_offset = lseek(ff.
fd, 0, SEEK_CUR);
2796 .size = evsel->
ids *
sizeof(u64),
2799 err =
do_write(&ff, &f_attr,
sizeof(f_attr));
2801 pr_debug(
"failed to write perf header attribute\n");
2818 .size =
sizeof(f_header),
2819 .attr_size =
sizeof(f_attr),
2821 .offset = attr_offset,
2831 memcpy(&f_header.adds_features, &header->adds_features,
sizeof(header->adds_features));
2833 lseek(fd, 0, SEEK_SET);
2834 err =
do_write(&ff, &f_header,
sizeof(f_header));
2836 pr_debug(
"failed to write perf header\n");
2845 int fd,
void *buf,
size_t size)
2847 if (
readn(fd, buf, size) <= 0)
2860 int feat,
int fd,
void *data))
2872 feat_sec = sec = calloc(nr_sections,
sizeof(*feat_sec));
2876 sec_size =
sizeof(*feat_sec) * nr_sections;
2885 err = process(sec++, header, feat, fd, data);
2896 [0] = PERF_ATTR_SIZE_VER0,
2897 [1] = PERF_ATTR_SIZE_VER1,
2898 [2] = PERF_ATTR_SIZE_VER2,
2899 [3] = PERF_ATTR_SIZE_VER3,
2900 [4] = PERF_ATTR_SIZE_VER4,
2912 uint64_t ref_size, attr_size;
2915 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2916 ref_size = attr_file_abi_sizes[i]
2918 if (hdr_sz != ref_size) {
2919 attr_size = bswap_64(hdr_sz);
2920 if (attr_size != ref_size)
2925 pr_debug(
"ABI%d perf.data file detected, need_swap=%d\n",
2934 #define PERF_PIPE_HDR_VER0 16 2953 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2954 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2955 attr_size = bswap_64(hdr_sz);
2956 if (attr_size != hdr_sz)
2961 pr_debug(
"Pipe ABI%d perf.data file detected\n", i);
2986 pr_debug(
"legacy perf.data format\n");
3017 lseek(fd, 0, SEEK_SET);
3019 ret =
readn(fd, header,
sizeof(*header));
3025 pr_debug(
"magic/endian check failed\n");
3034 if (header->
size !=
sizeof(*header)) {
3036 if (header->
size == offsetof(typeof(*header), adds_features))
3075 memcpy(&ph->adds_features, &header->adds_features,
3076 sizeof(ph->adds_features));
3086 int feat,
int fd,
void *data)
3091 .size = section->
size,
3092 .offset = section->
offset,
3095 if (lseek(fd, section->
offset, SEEK_SET) == (off_t)-1) {
3096 pr_debug(
"Failed to lseek to %" PRIu64
" offset for feature " 3097 "%d, continuing...\n", section->
offset, feat);
3102 pr_debug(
"unknown feature %d, continuing...\n", feat);
3106 if (!feat_ops[feat].process)
3109 return feat_ops[feat].
process(&fdd, data);
3117 .
fd = STDOUT_FILENO,
3122 ret =
readn(fd, header,
sizeof(*header));
3132 header->
size = bswap_64(header->
size);
3134 if (repipe &&
do_write(&ff, header,
sizeof(*header)) < 0)
3148 pr_debug(
"incompatible file format\n");
3158 struct perf_event_attr *
attr = &f_attr->
attr;
3160 size_t our_sz =
sizeof(f_attr->
attr);
3163 memset(f_attr, 0,
sizeof(*f_attr));
3166 ret =
readn(fd, attr, PERF_ATTR_SIZE_VER0);
3168 pr_debug(
"cannot read %d bytes of header attr\n",
3169 PERF_ATTR_SIZE_VER0);
3181 sz = PERF_ATTR_SIZE_VER0;
3182 }
else if (sz > our_sz) {
3183 pr_debug(
"file uses a more recent and unsupported ABI" 3184 " (%zu bytes extra)\n", sz - our_sz);
3188 left = sz - PERF_ATTR_SIZE_VER0;
3191 ptr += PERF_ATTR_SIZE_VER0;
3193 ret =
readn(fd, ptr, left);
3196 ret =
readn(fd, &f_attr->
ids,
sizeof(f_attr->
ids));
3198 return ret <= 0 ? -1 : 0;
3202 struct pevent *pevent)
3204 struct event_format *
event;
3211 if (pevent == NULL) {
3212 pr_debug(
"broken or missing trace data\n");
3216 event = pevent_find_event(pevent, evsel->
attr.config);
3217 if (event == NULL) {
3218 pr_debug(
"cannot find event format for %d\n", (
int)evsel->
attr.config);
3223 snprintf(bf,
sizeof(bf),
"%s:%s", event->system, event->name);
3224 evsel->
name = strdup(bf);
3225 if (evsel->
name == NULL)
3234 struct pevent *pevent)
3239 if (pos->
attr.type == PERF_TYPE_TRACEPOINT &&
3254 int nr_attrs, nr_ids, i, j;
3258 if (session->
evlist == NULL)
3276 pr_warning(
"WARNING: The %s file's data size field is 0 which is unexpected.\n" 3277 "Was the 'perf record' command properly terminated?\n",
3284 for (i = 0; i < nr_attrs; i++) {
3297 tmp = lseek(fd, 0, SEEK_CUR);
3301 goto out_delete_evlist;
3310 nr_ids = f_attr.
ids.
size /
sizeof(u64);
3317 goto out_delete_evlist;
3321 for (j = 0; j < nr_ids; j++) {
3328 lseek(fd, tmp, SEEK_SET);
3336 goto out_delete_evlist;
3349 struct perf_event_attr *
attr, u32 ids, u64 *
id,
3356 size =
sizeof(
struct perf_event_attr);
3357 size = PERF_ALIGN(size,
sizeof(u64));
3358 size +=
sizeof(
struct perf_event_header);
3359 size += ids *
sizeof(u64);
3366 ev->attr.attr = *
attr;
3367 memcpy(ev->attr.id,
id, ids *
sizeof(u64));
3370 ev->attr.header.size = (u16)size;
3372 if (ev->attr.header.size == size)
3373 err = process(tool, ev, NULL, NULL);
3393 sz_hdr =
sizeof(fe->
header);
3398 memset(&ff, 0,
sizeof(ff));
3404 ff.
size = sz - sz_hdr;
3407 if (!feat_ops[feat].synthesize) {
3408 pr_debug(
"No record header feature for header :%d\n", feat);
3414 ret = feat_ops[
feat].
write(&ff, evlist);
3415 if (ret || ff.
offset <= (ssize_t)
sizeof(*fe)) {
3416 pr_debug(
"Error writing feature\n");
3421 memset(fe, 0,
sizeof(*fe));
3427 ret = process(tool, ff.
buf, NULL, NULL);
3438 fe->
header.size =
sizeof(*fe);
3440 ret = process(tool, ff.
buf, NULL, NULL);
3452 int type = fe->
header.type;
3456 pr_warning(
"invalid record type %d in pipe-mode\n", type);
3460 pr_warning(
"invalid record type %d in pipe-mode\n", type);
3464 if (!feat_ops[feat].process)
3471 if (feat_ops[feat].process(&ff, NULL))
3477 if (!feat_ops[feat].full_only ||
3479 feat_ops[feat].
print(&ff, stdout);
3481 fprintf(stdout,
"# %s info available, use -I to display\n",
3482 feat_ops[feat].
name);
3493 size +=
sizeof(*ev);
3494 size = PERF_ALIGN(size,
sizeof(u64));
3499 ev->
header.size = (u16)size;
3519 strncpy(ev->
data, evsel->
unit, size);
3520 err = process(tool, (
union perf_event *)ev, NULL, NULL);
3540 err = process(tool, (
union perf_event*) ev, NULL, NULL);
3551 size_t len = strlen(evsel->
name);
3558 strncpy(ev->
data, evsel->
name, len);
3559 err = process(tool, (
union perf_event*) ev, NULL, NULL);
3582 ev->
header.size = (u16)size;
3584 ev->
id = evsel->
id[0];
3590 err = process(tool, (
union perf_event*) ev, NULL, NULL);
3603 ret = fprintf(fp,
"\n... id: %" PRIu64
"\n", ev->
id);
3608 ret += fprintf(fp,
"... scale: %f\n", ev_scale->
scale);
3611 ret += fprintf(fp,
"... unit: %s\n", ev->
data);
3614 ret += fprintf(fp,
"... name: %s\n", ev->
data);
3618 ret += fprintf(fp,
"... ");
3624 ret += fprintf(fp,
"failed to get cpus\n");
3627 ret += fprintf(fp,
"... unknown type\n");
3643 evsel->
id, process);
3645 pr_debug(
"failed to create perf header attribute\n");
3655 return counter->
unit && *counter->
unit;
3660 return counter->
scale != 1;
3685 pr_err(
"Couldn't synthesize evsel unit.\n");
3693 pr_err(
"Couldn't synthesize evsel counter.\n");
3701 pr_err(
"Couldn't synthesize evsel cpus.\n");
3713 pr_err(
"Couldn't synthesize evsel name.\n");
3729 if (evlist == NULL) {
3741 ids =
event->header.size;
3743 n_ids = ids /
sizeof(u64);
3752 for (i = 0; i < n_ids; i++) {
3770 if (!pevlist || *pevlist == NULL)
3797 pr_err(
"failed to get event_update cpus\n");
3811 ssize_t
size = 0, aligned_size = 0, padding;
3813 int err __maybe_unused = 0;
3830 memset(&ev, 0,
sizeof(ev));
3834 aligned_size = PERF_ALIGN(size,
sizeof(u64));
3835 padding = aligned_size -
size;
3839 process(tool, &ev, NULL, NULL);
3851 return aligned_size;
3858 ssize_t size_read, padding,
size =
event->tracing_data.size;
3860 off_t offset = lseek(fd, 0, SEEK_CUR);
3869 padding = PERF_ALIGN(size_read,
sizeof(u64)) - size_read;
3871 if (
readn(fd, buf, padding) < 0) {
3872 pr_err(
"%s: reading input file", __func__);
3876 int retw = write(STDOUT_FILENO, buf, padding);
3877 if (retw <= 0 || retw != padding) {
3878 pr_err(
"%s: repiping tracing data padding", __func__);
3883 if (size_read + padding != size) {
3884 pr_err(
"%s: tracing data size mismatch", __func__);
3891 return size_read + padding;
3895 struct dso *pos, u16 misc,
3906 memset(&ev, 0,
sizeof(ev));
3917 err = process(tool, &ev, NULL, machine);
int strbuf_init(struct strbuf *sb, ssize_t hint)
struct perf_event_attr attr
struct perf_evlist * evlist
int read_tracing_data(int fd, struct list_head *pattrs)
struct perf_probe_event events[MAX_PROBES]
void mem_bswap_64(void *src, int byte_size)
int perf_session__write_buildid_table(struct perf_session *session, struct feat_fd *fd)
const char ** cmdline_argv
void mem_bswap_32(void *src, int byte_size)
void(* print)(struct feat_fd *ff, FILE *fp)
struct perf_file_section ids
int(* perf_event__handler_t)(struct perf_tool *tool, union perf_event *event, struct perf_sample *sample, struct machine *machine)
static struct machine * perf_session__findnew_machine(struct perf_session *session, pid_t pid)
struct numa_node * numa_nodes
int auxtrace_index__process(int fd, u64 size, struct perf_session *session, bool needs_swap)
static struct version version
struct feature_event feat
void perf_event__attr_swap(struct perf_event_attr *attr)
struct tracing_data_event tracing_data
ssize_t readn(int fd, void *buf, size_t n)
#define DEFAULT_GUEST_KERNEL_ID
static int input(yyscan_t yyscanner)
struct perf_data_file file
#define pr_debug2(fmt,...)
struct cpu_map * cpu_map__new_data(struct cpu_map_data *data)
ssize_t trace_report(int fd, struct trace_event *tevent, bool __repipe)
void perf_evlist__delete(struct perf_evlist *evlist)
int(* write)(struct feat_fd *ff, struct perf_evlist *evlist)
int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr, attr__fprintf_f attr__fprintf, void *priv)
int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
int tracing_data_put(struct tracing_data *tdata)
bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
x86 movsq based memset() in arch/x86/lib/memset_64.S") MEMSET_FN(memset_erms
void dso__put(struct dso *dso)
int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
struct dso * machine__findnew_dso(struct machine *machine, const char *filename)
struct perf_pmu * perf_pmu__scan(struct perf_pmu *pmu)
size_t cpu_map__fprintf(struct cpu_map *map, FILE *fp)
struct trace_event tevent
void * cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
void cpu_map__put(struct cpu_map *map)
u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))]
unsigned long long memory_bsize
void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, int cpu, int thread, u64 id)
unsigned long long total_mem
struct perf_evlist * evlist
#define pr_debug(fmt,...)
static struct perf_session * session
#define evlist__for_each_entry(evlist, evsel)
static struct perf_evlist * evsel_list
struct perf_event_header header
struct cpu_topology_map * cpu
struct memory_node * memory_nodes
static struct perf_tool tool
struct event_format * tp_format
static int str(yyscan_t scanner, int token)
int strbuf_add(struct strbuf *sb, const void *data, size_t len)
struct build_id_event build_id
struct perf_evsel * perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
int unit_number__scnprintf(char *buf, size_t size, u64 n)
int cpu__max_present_cpu(void)
enum dso_kernel_type kernel
bool cpu_map__has(struct cpu_map *cpus, int cpu)
static int perf_data__fd(struct perf_data *data)
x86 movsq based memcpy() in arch/x86/lib/memcpy_64.S") MEMCPY_FN(memcpy_erms
struct cpu_map * own_cpus
struct cpu_cache_level * caches
int perf_session__cache_build_ids(struct perf_session *session)
const char * perf_evsel__name(struct perf_evsel *evsel)
static struct perf_evsel * perf_evsel__new(struct perf_event_attr *attr)
struct perf_event_header header
void perf_event_header__bswap(struct perf_event_header *hdr)
static int perf_data__is_pipe(struct perf_data *data)
int auxtrace_index__write(int fd, struct list_head *head)
int(* process)(struct feat_fd *ff, void *data)
struct tracing_data * tracing_data_get(struct list_head *pattrs, int fd, bool temp)
u8 build_id[BUILD_ID_SIZE]
struct perf_evsel * leader
struct list_head auxtrace_index
char * strbuf_detach(struct strbuf *sb, size_t *sz)
int build_id__sprintf(const u8 *build_id, int len, char *bf)
static unsigned int ncpus
void dso__set_build_id(struct dso *dso, void *build_id)
struct perf_header header
#define kmod_path__parse_name(__m, __p)
unsigned int msr_pmu_type
struct perf_evsel * events
void dso__set_module_info(struct dso *dso, struct kmod_path *m, struct machine *machine)
int strbuf_addf(struct strbuf *sb, const char *fmt,...)
int perf_env__read_cpu_topology_map(struct perf_env *env)
static bool perf_evsel__is_group_leader(const struct perf_evsel *evsel)
ssize_t writen(int fd, const void *buf, size_t n)
void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map, u16 type, int max)
#define pr_warning(fmt,...)
struct cpu_map * cpu_map__new(const char *cpu_list)
struct perf_event_attr attr
struct perf_event_header header
struct perf_evlist * perf_evlist__new(void)
struct perf_event_attr attr
struct perf_event_header header
void strbuf_release(struct strbuf *sb)
struct perf_event_header header
void cpu_cache_level__free(struct cpu_cache_level *cache)
void static void * zalloc(size_t size)