Lines Matching +full:cpu +full:- +full:nr

1 // SPDX-License-Identifier: GPL-2.0
35 #include "trace-event.h"
44 #include "build-id.h"
49 #include "time-utils.h"
53 #include "bpf-event.h"
54 #include "bpf-utils.h"
61 #include <traceevent/event-parse.h>
88 __set_bit(feat, header->adds_features); in perf_header__set_feat()
93 __clear_bit(feat, header->adds_features); in perf_header__clear_feat()
98 return test_bit(feat, header->adds_features); in perf_header__has_feat()
103 ssize_t ret = writen(ff->fd, buf, size); in __do_write_fd()
106 return ret < 0 ? (int)ret : -1; in __do_write_fd()
113 const size_t max_size = 0xffff - sizeof(struct perf_event_header); in __do_write_buf()
114 size_t new_size = ff->size; in __do_write_buf()
117 if (size + ff->offset > max_size) in __do_write_buf()
118 return -E2BIG; in __do_write_buf()
120 while (size > (new_size - ff->offset)) in __do_write_buf()
124 if (ff->size < new_size) { in __do_write_buf()
125 addr = realloc(ff->buf, new_size); in __do_write_buf()
127 return -ENOMEM; in __do_write_buf()
128 ff->buf = addr; in __do_write_buf()
129 ff->size = new_size; in __do_write_buf()
132 memcpy(ff->buf + ff->offset, buf, size); in __do_write_buf()
133 ff->offset += size; in __do_write_buf()
138 /* Return: 0 if succeeded, -ERR if failed. */
141 if (!ff->buf) in do_write()
146 /* Return: 0 if succeeded, -ERR if failed. */
165 /* Return: 0 if succeeded, -ERR if failed. */
173 err = do_write(ff, zero_buf, count_aligned - count); in write_padded()
181 /* Return: 0 if succeeded, -ERR if failed. */
200 ssize_t ret = readn(ff->fd, addr, size); in __do_read_fd()
203 return ret < 0 ? (int)ret : -1; in __do_read_fd()
209 if (size > (ssize_t)ff->size - ff->offset) in __do_read_buf()
210 return -1; in __do_read_buf()
212 memcpy(addr, ff->buf + ff->offset, size); in __do_read_buf()
213 ff->offset += size; in __do_read_buf()
221 if (!ff->buf) in __do_read()
234 if (ff->ph->needs_swap) in do_read_u32()
247 if (ff->ph->needs_swap) in do_read_u64()
277 /* Return: 0 if succeeded, -ERR if failed. */
290 return -ENOMEM; in do_read_bitmap()
311 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) in write_tracing_data()
312 return -1; in write_tracing_data()
314 return read_tracing_data(ff->fd, &evlist->core.entries); in write_tracing_data()
324 session = container_of(ff->ph, struct perf_session, header); in write_build_id()
327 return -1; in write_build_id()
329 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) in write_build_id()
330 return -1; in write_build_id()
350 return -1; in write_hostname()
363 return -1; in write_osrelease()
376 return -1; in write_arch()
394 int ret = -1; in __write_cpudesc()
397 return -1; in __write_cpudesc()
401 return -1; in __write_cpudesc()
410 ret = -1; in __write_cpudesc()
446 #define CPUINFO_PROC { "cpu", } in write_cpudesc()
450 #define CPUINFO_PROC { "cpu type", } in write_cpudesc()
452 #define CPUINFO_PROC { "cpu model", } in write_cpudesc()
474 return -1; in write_cpudesc()
481 long nr; in write_nrcpus() local
485 nrc = cpu__max_present_cpu().cpu; in write_nrcpus()
487 nr = sysconf(_SC_NPROCESSORS_ONLN); in write_nrcpus()
488 if (nr < 0) in write_nrcpus()
489 return -1; in write_nrcpus()
491 nra = (u32)(nr & UINT_MAX); in write_nrcpus()
507 nre = evlist->core.nr_entries; in write_event_desc()
519 sz = (u32)sizeof(evsel->core.attr); in write_event_desc()
525 ret = do_write(ff, &evsel->core.attr, sz); in write_event_desc()
535 nri = evsel->core.ids; in write_event_desc()
549 ret = do_write(ff, evsel->core.id, evsel->core.ids * sizeof(u64)); in write_event_desc()
594 return -1; in write_cpu_topology()
596 ret = do_write(ff, &tp->package_cpus_lists, sizeof(tp->package_cpus_lists)); in write_cpu_topology()
600 for (i = 0; i < tp->package_cpus_lists; i++) { in write_cpu_topology()
601 ret = do_write_string(ff, tp->package_cpus_list[i]); in write_cpu_topology()
605 ret = do_write(ff, &tp->core_cpus_lists, sizeof(tp->core_cpus_lists)); in write_cpu_topology()
609 for (i = 0; i < tp->core_cpus_lists; i++) { in write_cpu_topology()
610 ret = do_write_string(ff, tp->core_cpus_list[i]); in write_cpu_topology()
620 ret = do_write(ff, &perf_env.cpu[j].core_id, in write_cpu_topology()
621 sizeof(perf_env.cpu[j].core_id)); in write_cpu_topology()
624 ret = do_write(ff, &perf_env.cpu[j].socket_id, in write_cpu_topology()
625 sizeof(perf_env.cpu[j].socket_id)); in write_cpu_topology()
630 if (!tp->die_cpus_lists) in write_cpu_topology()
633 ret = do_write(ff, &tp->die_cpus_lists, sizeof(tp->die_cpus_lists)); in write_cpu_topology()
637 for (i = 0; i < tp->die_cpus_lists; i++) { in write_cpu_topology()
638 ret = do_write_string(ff, tp->die_cpus_list[i]); in write_cpu_topology()
644 ret = do_write(ff, &perf_env.cpu[j].die_id, in write_cpu_topology()
645 sizeof(perf_env.cpu[j].die_id)); in write_cpu_topology()
663 int ret = -1, n; in write_total_mem()
668 return -1; in write_total_mem()
680 ret = -1; in write_total_mem()
690 int ret = -1; in write_numa_topology()
695 return -ENOMEM; in write_numa_topology()
697 ret = do_write(ff, &tp->nr, sizeof(u32)); in write_numa_topology()
701 for (i = 0; i < tp->nr; i++) { in write_numa_topology()
702 struct numa_topology_node *n = &tp->nodes[i]; in write_numa_topology()
704 ret = do_write(ff, &n->node, sizeof(u32)); in write_numa_topology()
708 ret = do_write(ff, &n->mem_total, sizeof(u64)); in write_numa_topology()
712 ret = do_write(ff, &n->mem_free, sizeof(u64)); in write_numa_topology()
716 ret = do_write_string(ff, n->cpus); in write_numa_topology()
759 ret = do_write(ff, &pmu->type, sizeof(pmu->type)); in write_pmu_mappings()
763 ret = do_write_string(ff, pmu->name); in write_pmu_mappings()
795 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) { in write_group_desc()
796 const char *name = evsel->group_name ?: "{anon_group}"; in write_group_desc()
797 u32 leader_idx = evsel->core.idx; in write_group_desc()
798 u32 nr_members = evsel->core.nr_members; in write_group_desc()
817 * Return the CPU id as a raw string.
829 * Otherwise return non-zero.
846 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so); in strcmp_cpuid_str()
872 return -1; in write_cpuid()
889 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) in write_auxtrace()
890 return -1; in write_auxtrace()
892 session = container_of(ff->ph, struct perf_session, header); in write_auxtrace()
894 err = auxtrace_index__write(ff->fd, &session->auxtrace_index); in write_auxtrace()
903 return do_write(ff, &ff->ph->env.clock.clockid_res_ns, in write_clockid()
904 sizeof(ff->ph->env.clock.clockid_res_ns)); in write_clockid()
922 data32 = ff->ph->env.clock.clockid; in write_clock_data()
929 data64 = &ff->ph->env.clock.tod_ns; in write_clock_data()
936 data64 = &ff->ph->env.clock.clockid_ns; in write_clock_data()
950 return -ENOENT; in write_hybrid_topology()
952 ret = do_write(ff, &tp->nr, sizeof(u32)); in write_hybrid_topology()
956 for (i = 0; i < tp->nr; i++) { in write_hybrid_topology()
957 struct hybrid_topology_node *n = &tp->nodes[i]; in write_hybrid_topology()
959 ret = do_write_string(ff, n->pmu_name); in write_hybrid_topology()
963 ret = do_write_string(ff, n->cpus); in write_hybrid_topology()
981 session = container_of(ff->ph, struct perf_session, header); in write_dir_format()
982 data = session->data; in write_dir_format()
985 return -1; in write_dir_format()
987 return do_write(ff, &data->dir.version, sizeof(data->dir.version)); in write_dir_format()
991 * Check whether a CPU is online
994 * 1 -> if CPU is online
995 * 0 -> if CPU is offline
996 * -1 -> error case
998 int is_cpu_online(unsigned int cpu) in is_cpu_online() argument
1003 int status = -1; in is_cpu_online()
1007 "/sys/devices/system/cpu/cpu%d", cpu); in is_cpu_online()
1012 * Check if /sys/devices/system/cpu/cpux/online file in is_cpu_online()
1019 "/sys/devices/system/cpu/cpu%d/online", cpu); in is_cpu_online()
1025 * If read or open fails, return -1. in is_cpu_online()
1030 "devices/system/cpu/cpu%d/online", cpu); in is_cpu_online()
1045 struct perf_env *env = &ff->ph->env; in write_bpf_prog_info()
1050 down_read(&env->bpf_progs.lock); in write_bpf_prog_info()
1052 ret = do_write(ff, &env->bpf_progs.infos_cnt, in write_bpf_prog_info()
1053 sizeof(env->bpf_progs.infos_cnt)); in write_bpf_prog_info()
1057 root = &env->bpf_progs.infos; in write_bpf_prog_info()
1064 next = rb_next(&node->rb_node); in write_bpf_prog_info()
1066 node->info_linear->data_len; in write_bpf_prog_info()
1069 bpil_addr_to_offs(node->info_linear); in write_bpf_prog_info()
1070 ret = do_write(ff, node->info_linear, len); in write_bpf_prog_info()
1075 bpil_offs_to_addr(node->info_linear); in write_bpf_prog_info()
1080 up_read(&env->bpf_progs.lock); in write_bpf_prog_info()
1087 struct perf_env *env = &ff->ph->env; in write_bpf_btf()
1092 down_read(&env->bpf_progs.lock); in write_bpf_btf()
1094 ret = do_write(ff, &env->bpf_progs.btfs_cnt, in write_bpf_btf()
1095 sizeof(env->bpf_progs.btfs_cnt)); in write_bpf_btf()
1100 root = &env->bpf_progs.btfs; in write_bpf_btf()
1106 next = rb_next(&node->rb_node); in write_bpf_btf()
1107 ret = do_write(ff, &node->id, in write_bpf_btf()
1108 sizeof(u32) * 2 + node->data_size); in write_bpf_btf()
1113 up_read(&env->bpf_progs.lock); in write_bpf_btf()
1123 return cache_a->level - cache_b->level; in cpu_cache_level__sort()
1128 if (a->level != b->level) in cpu_cache_level__cmp()
1131 if (a->line_size != b->line_size) in cpu_cache_level__cmp()
1134 if (a->sets != b->sets) in cpu_cache_level__cmp()
1137 if (a->ways != b->ways) in cpu_cache_level__cmp()
1140 if (strcmp(a->type, b->type)) in cpu_cache_level__cmp()
1143 if (strcmp(a->size, b->size)) in cpu_cache_level__cmp()
1146 if (strcmp(a->map, b->map)) in cpu_cache_level__cmp()
1152 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level) in cpu_cache_level__read() argument
1158 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level); in cpu_cache_level__read()
1165 if (sysfs__read_int(file, (int *) &cache->level)) in cpu_cache_level__read()
1166 return -1; in cpu_cache_level__read()
1169 if (sysfs__read_int(file, (int *) &cache->line_size)) in cpu_cache_level__read()
1170 return -1; in cpu_cache_level__read()
1173 if (sysfs__read_int(file, (int *) &cache->sets)) in cpu_cache_level__read()
1174 return -1; in cpu_cache_level__read()
1177 if (sysfs__read_int(file, (int *) &cache->ways)) in cpu_cache_level__read()
1178 return -1; in cpu_cache_level__read()
1181 if (sysfs__read_str(file, &cache->type, &len)) in cpu_cache_level__read()
1182 return -1; in cpu_cache_level__read()
1184 cache->type[len] = 0; in cpu_cache_level__read()
1185 cache->type = strim(cache->type); in cpu_cache_level__read()
1188 if (sysfs__read_str(file, &cache->size, &len)) { in cpu_cache_level__read()
1189 zfree(&cache->type); in cpu_cache_level__read()
1190 return -1; in cpu_cache_level__read()
1193 cache->size[len] = 0; in cpu_cache_level__read()
1194 cache->size = strim(cache->size); in cpu_cache_level__read()
1197 if (sysfs__read_str(file, &cache->map, &len)) { in cpu_cache_level__read()
1198 zfree(&cache->size); in cpu_cache_level__read()
1199 zfree(&cache->type); in cpu_cache_level__read()
1200 return -1; in cpu_cache_level__read()
1203 cache->map[len] = 0; in cpu_cache_level__read()
1204 cache->map = strim(cache->map); in cpu_cache_level__read()
1210 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map); in cpu_cache_level__fprintf()
1214 * Build caches levels for a particular CPU from the data in
1215 * /sys/devices/system/cpu/cpu<cpu>/cache/
1219 int build_caches_for_cpu(u32 cpu, struct cpu_cache_level caches[], u32 *cntp) in build_caches_for_cpu() argument
1228 err = cpu_cache_level__read(&c, cpu, level); in build_caches_for_cpu()
1252 u32 nr, cpu, cnt = 0; in build_caches() local
1254 nr = cpu__max_cpu().cpu; in build_caches()
1256 for (cpu = 0; cpu < nr; cpu++) { in build_caches()
1257 int ret = build_caches_for_cpu(cpu, caches, &cnt); in build_caches()
1269 u32 max_caches = cpu__max_cpu().cpu * MAX_CACHE_LVL; in write_cache()
1292 ret = do_write(ff, &c->v, sizeof(u32)); \ in write_cache()
1303 ret = do_write_string(ff, (const char *) c->v); \ in write_cache()
1330 ret = do_write(ff, &evlist->first_sample_time, in write_sample_time()
1331 sizeof(evlist->first_sample_time)); in write_sample_time()
1335 return do_write(ff, &evlist->last_sample_time, in write_sample_time()
1336 sizeof(evlist->last_sample_time)); in write_sample_time()
1349 if (strcmp(ent->d_name, ".") && \ in memory_node__read()
1350 strcmp(ent->d_name, "..") && \ in memory_node__read()
1351 sscanf(ent->d_name, "memory%u", &mem) == 1) in memory_node__read()
1360 return -1; in memory_node__read()
1369 n->set = bitmap_zalloc(size); in memory_node__read()
1370 if (!n->set) { in memory_node__read()
1372 return -ENOMEM; in memory_node__read()
1375 n->node = idx; in memory_node__read()
1376 n->size = size; in memory_node__read()
1381 __set_bit(phys, n->set); in memory_node__read()
1401 return na->node - nb->node; in memory_node__sort()
1420 return -1; in build_mem_topology()
1427 if (!strcmp(ent->d_name, ".") || in build_mem_topology()
1428 !strcmp(ent->d_name, "..")) in build_mem_topology()
1431 r = sscanf(ent->d_name, "node%u", &idx); in build_mem_topology()
1441 ret = -ENOMEM; in build_mem_topology()
1467 * 0 - version | for future changes
1468 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1469 * 16 - count | number of nodes
1474 * 32 - node id | node index
1475 * 40 - size | size of bitmap
1476 * 48 - bitmap | bitmap of memory indexes that belongs to node
1482 u64 bsize, version = 1, i, nr = 0; in write_mem_topology() local
1490 ret = build_mem_topology(&nodes, &nr); in write_mem_topology()
1502 ret = do_write(ff, &nr, sizeof(nr)); in write_mem_topology()
1506 for (i = 0; i < nr; i++) { in write_mem_topology()
1510 ret = do_write(ff, &n->v, sizeof(n->v)); \ in write_mem_topology()
1519 ret = do_write_bitmap(ff, n->set, n->size); in write_mem_topology()
1525 memory_node__delete_nodes(nodes, nr); in write_mem_topology()
1534 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver)); in write_compressed()
1538 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type)); in write_compressed()
1542 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level)); in write_compressed()
1546 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio)); in write_compressed()
1550 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len)); in write_compressed()
1559 ret = do_write(ff, &pmu->nr_caps, sizeof(pmu->nr_caps)); in __write_pmu_caps()
1563 list_for_each_entry(caps, &pmu->caps, list) { in __write_pmu_caps()
1564 ret = do_write_string(ff, caps->name); in __write_pmu_caps()
1568 ret = do_write_string(ff, caps->value); in __write_pmu_caps()
1574 ret = do_write_string(ff, pmu->name); in __write_pmu_caps()
1585 struct perf_pmu *cpu_pmu = perf_pmus__find("cpu"); in write_cpu_pmu_caps()
1589 return -ENOENT; in write_cpu_pmu_caps()
1606 if (!strcmp(pmu->name, "cpu")) { in write_pmu_caps()
1608 * The "cpu" PMU is special and covered by in write_pmu_caps()
1632 if (!strcmp(pmu->name, "cpu")) { in write_pmu_caps()
1647 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname); in print_hostname()
1652 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release); in print_osrelease()
1657 fprintf(fp, "# arch : %s\n", ff->ph->env.arch); in print_arch()
1662 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc); in print_cpudesc()
1667 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online); in print_nrcpus()
1668 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail); in print_nrcpus()
1673 fprintf(fp, "# perf version : %s\n", ff->ph->env.version); in print_version()
1678 int nr, i; in print_cmdline() local
1680 nr = ff->ph->env.nr_cmdline; in print_cmdline()
1684 for (i = 0; i < nr; i++) { in print_cmdline()
1685 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]); in print_cmdline()
1687 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]); in print_cmdline()
1707 struct perf_header *ph = ff->ph; in print_cpu_topology()
1708 int cpu_nr = ph->env.nr_cpus_avail; in print_cpu_topology()
1709 int nr, i; in print_cpu_topology() local
1712 nr = ph->env.nr_sibling_cores; in print_cpu_topology()
1713 str = ph->env.sibling_cores; in print_cpu_topology()
1715 for (i = 0; i < nr; i++) { in print_cpu_topology()
1720 if (ph->env.nr_sibling_dies) { in print_cpu_topology()
1721 nr = ph->env.nr_sibling_dies; in print_cpu_topology()
1722 str = ph->env.sibling_dies; in print_cpu_topology()
1724 for (i = 0; i < nr; i++) { in print_cpu_topology()
1730 nr = ph->env.nr_sibling_threads; in print_cpu_topology()
1731 str = ph->env.sibling_threads; in print_cpu_topology()
1733 for (i = 0; i < nr; i++) { in print_cpu_topology()
1738 if (ph->env.nr_sibling_dies) { in print_cpu_topology()
1739 if (ph->env.cpu != NULL) { in print_cpu_topology()
1741 fprintf(fp, "# CPU %d: Core ID %d, " in print_cpu_topology()
1743 i, ph->env.cpu[i].core_id, in print_cpu_topology()
1744 ph->env.cpu[i].die_id, in print_cpu_topology()
1745 ph->env.cpu[i].socket_id); in print_cpu_topology()
1750 if (ph->env.cpu != NULL) { in print_cpu_topology()
1752 fprintf(fp, "# CPU %d: Core ID %d, " in print_cpu_topology()
1754 i, ph->env.cpu[i].core_id, in print_cpu_topology()
1755 ph->env.cpu[i].socket_id); in print_cpu_topology()
1765 ff->ph->env.clock.clockid_res_ns * 1000); in print_clockid()
1777 if (!ff->ph->env.clock.enabled) { in print_clock_data()
1783 ref = ff->ph->env.clock.tod_ns; in print_clock_data()
1785 ref -= tod_ns.tv_sec * NSEC_PER_SEC; in print_clock_data()
1789 ref = ff->ph->env.clock.clockid_ns; in print_clock_data()
1791 ref -= clockid_ns.tv_sec * NSEC_PER_SEC; in print_clock_data()
1794 clockid = ff->ph->env.clock.clockid; in print_clock_data()
1816 fprintf(fp, "# hybrid cpu system:\n"); in print_hybrid_topology()
1817 for (i = 0; i < ff->ph->env.nr_hybrid_nodes; i++) { in print_hybrid_topology()
1818 n = &ff->ph->env.hybrid_nodes[i]; in print_hybrid_topology()
1819 fprintf(fp, "# %s cpu list : %s\n", n->pmu_name, n->cpus); in print_hybrid_topology()
1828 session = container_of(ff->ph, struct perf_session, header); in print_dir_format()
1829 data = session->data; in print_dir_format()
1831 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version); in print_dir_format()
1837 struct perf_env *env = &ff->ph->env; in print_bpf_prog_info()
1841 down_read(&env->bpf_progs.lock); in print_bpf_prog_info()
1843 root = &env->bpf_progs.infos; in print_bpf_prog_info()
1850 next = rb_next(&node->rb_node); in print_bpf_prog_info()
1852 __bpf_event__print_bpf_prog_info(&node->info_linear->info, in print_bpf_prog_info()
1856 up_read(&env->bpf_progs.lock); in print_bpf_prog_info()
1861 struct perf_env *env = &ff->ph->env; in print_bpf_btf()
1865 down_read(&env->bpf_progs.lock); in print_bpf_btf()
1867 root = &env->bpf_progs.btfs; in print_bpf_btf()
1874 next = rb_next(&node->rb_node); in print_bpf_btf()
1875 fprintf(fp, "# btf info of id %u\n", node->id); in print_bpf_btf()
1878 up_read(&env->bpf_progs.lock); in print_bpf_btf()
1889 for (evsel = events; evsel->core.attr.size; evsel++) { in free_event_desc()
1890 zfree(&evsel->name); in free_event_desc()
1891 zfree(&evsel->core.id); in free_event_desc()
1899 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) { in perf_attr_check()
1905 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) { in perf_attr_check()
1908 attr->sample_type); in perf_attr_check()
1912 if (attr->read_format & ~(PERF_FORMAT_MAX-1)) { in perf_attr_check()
1915 attr->read_format); in perf_attr_check()
1919 if ((attr->sample_type & PERF_SAMPLE_BRANCH_STACK) && in perf_attr_check()
1920 (attr->branch_sample_type & ~(PERF_SAMPLE_BRANCH_MAX-1))) { in perf_attr_check()
1923 attr->branch_sample_type); in perf_attr_check()
1936 u32 nre, sz, nr, i, j; in read_event_desc() local
1951 /* the last event terminates with evsel->core.attr.size == 0: */ in read_event_desc()
1956 msz = sizeof(evsel->core.attr); in read_event_desc()
1961 evsel->core.idx = i; in read_event_desc()
1964 * must read entire on-file attr struct to in read_event_desc()
1970 if (ff->ph->needs_swap) in read_event_desc()
1973 memcpy(&evsel->core.attr, buf, msz); in read_event_desc()
1975 if (!perf_attr_check(&evsel->core.attr)) in read_event_desc()
1978 if (do_read_u32(ff, &nr)) in read_event_desc()
1981 if (ff->ph->needs_swap) in read_event_desc()
1982 evsel->needs_swap = true; in read_event_desc()
1984 evsel->name = do_read_string(ff); in read_event_desc()
1985 if (!evsel->name) in read_event_desc()
1988 if (!nr) in read_event_desc()
1991 id = calloc(nr, sizeof(*id)); in read_event_desc()
1994 evsel->core.ids = nr; in read_event_desc()
1995 evsel->core.id = id; in read_event_desc()
1997 for (j = 0 ; j < nr; j++) { in read_event_desc()
2024 if (ff->events) in print_event_desc()
2025 events = ff->events; in print_event_desc()
2034 for (evsel = events; evsel->core.attr.size; evsel++) { in print_event_desc()
2035 fprintf(fp, "# event : name = %s, ", evsel->name); in print_event_desc()
2037 if (evsel->core.ids) { in print_event_desc()
2039 for (j = 0, id = evsel->core.id; j < evsel->core.ids; j++, id++) { in print_event_desc()
2047 perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL); in print_event_desc()
2053 ff->events = NULL; in print_event_desc()
2058 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem); in print_total_mem()
2066 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) { in print_numa_topology()
2067 n = &ff->ph->env.numa_nodes[i]; in print_numa_topology()
2071 n->node, n->mem_total, n->mem_free); in print_numa_topology()
2073 fprintf(fp, "# node%u cpu list : ", n->node); in print_numa_topology()
2074 cpu_map__fprintf(n->map, fp); in print_numa_topology()
2080 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid); in print_cpuid()
2102 fprintf(fp, "# CPU cache info:\n"); in print_cache()
2103 for (i = 0; i < ff->ph->env.caches_cnt; i++) { in print_cache()
2105 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]); in print_cache()
2112 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown", in print_compressed()
2113 ff->ph->env.comp_level, ff->ph->env.comp_ratio); in print_compressed()
2137 __print_pmu_caps(fp, ff->ph->env.nr_cpu_pmu_caps, in print_cpu_pmu_caps()
2138 ff->ph->env.cpu_pmu_caps, (char *)"cpu"); in print_cpu_pmu_caps()
2145 for (int i = 0; i < ff->ph->env.nr_pmus_with_caps; i++) { in print_pmu_caps()
2146 pmu_caps = &ff->ph->env.pmu_caps[i]; in print_pmu_caps()
2147 __print_pmu_caps(fp, pmu_caps->nr_caps, pmu_caps->caps, in print_pmu_caps()
2148 pmu_caps->pmu_name); in print_pmu_caps()
2159 pmu_num = ff->ph->env.nr_pmu_mappings; in print_pmu_mappings()
2165 str = ff->ph->env.pmu_mappings; in print_pmu_mappings()
2177 pmu_num--; in print_pmu_mappings()
2192 u32 nr = 0; in print_group_desc() local
2194 session = container_of(ff->ph, struct perf_session, header); in print_group_desc()
2196 evlist__for_each_entry(session->evlist, evsel) { in print_group_desc()
2197 if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) { in print_group_desc()
2198 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", evsel__name(evsel)); in print_group_desc()
2200 nr = evsel->core.nr_members - 1; in print_group_desc()
2201 } else if (nr) { in print_group_desc()
2204 if (--nr == 0) in print_group_desc()
2216 session = container_of(ff->ph, struct perf_session, header); in print_sample_time()
2218 timestamp__scnprintf_usec(session->evlist->first_sample_time, in print_sample_time()
2222 timestamp__scnprintf_usec(session->evlist->last_sample_time, in print_sample_time()
2226 d = (double)(session->evlist->last_sample_time - in print_sample_time()
2227 session->evlist->first_sample_time) / NSEC_PER_MSEC; in print_sample_time()
2238 size = bsize * bitmap_weight(n->set, n->size); in memory_node__fprintf()
2241 bitmap_scnprintf(n->set, n->size, buf_map, 100); in memory_node__fprintf()
2242 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map); in memory_node__fprintf()
2248 int i, nr; in print_mem_topology() local
2250 nodes = ff->ph->env.memory_nodes; in print_mem_topology()
2251 nr = ff->ph->env.nr_memory_nodes; in print_mem_topology()
2253 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n", in print_mem_topology()
2254 nr, ff->ph->env.memory_bsize); in print_mem_topology()
2256 for (i = 0; i < nr; i++) { in print_mem_topology()
2257 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp); in print_mem_topology()
2265 int err = -1; in __event_process_build_id()
2271 machine = perf_session__findnew_machine(session, bev->pid); in __event_process_build_id()
2275 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; in __event_process_build_id()
2298 if (bev->header.misc & PERF_RECORD_MISC_BUILD_ID_SIZE) in __event_process_build_id()
2299 size = bev->size; in __event_process_build_id()
2301 build_id__init(&bid, bev->data, size); in __event_process_build_id()
2303 dso->header_build_id = 1; in __event_process_build_id()
2311 dso->kernel = dso_space; in __event_process_build_id()
2315 build_id__sprintf(&dso->bid, sbuild_id); in __event_process_build_id()
2317 dso->long_name, sbuild_id, size); in __event_process_build_id()
2343 return -1; in perf_header__read_build_ids_abi_quirk()
2345 if (header->needs_swap) in perf_header__read_build_ids_abi_quirk()
2348 len = old_bev.header.size - sizeof(old_bev); in perf_header__read_build_ids_abi_quirk()
2350 return -1; in perf_header__read_build_ids_abi_quirk()
2379 int err = -1; in perf_header__read_build_ids()
2387 if (header->needs_swap) in perf_header__read_build_ids()
2390 len = bev.header.size - sizeof(bev); in perf_header__read_build_ids()
2401 * Since the kernel build-id is the first entry, process the in perf_header__read_build_ids()
2403 * '[kernel.kallsyms]' string for the kernel build-id has the in perf_header__read_build_ids()
2407 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) in perf_header__read_build_ids()
2408 return -1; in perf_header__read_build_ids()
2425 free(ff->ph->env.__feat_env); \
2426 ff->ph->env.__feat_env = do_read_string(ff); \
2427 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
2440 ssize_t ret = trace_report(ff->fd, data, false); in process_tracing_data()
2442 return ret < 0 ? -1 : 0; in process_tracing_data()
2448 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size)) in process_build_id()
2465 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail; in process_nrcpus()
2466 ff->ph->env.nr_cpus_online = (int)nr_cpus_online; in process_nrcpus()
2477 return -1; in process_total_mem()
2478 ff->ph->env.total_mem = (unsigned long long)total_mem; in process_total_mem()
2487 if (evsel->core.idx == idx) in evlist__find_by_index()
2498 if (!event->name) in evlist__set_event_name()
2501 evsel = evlist__find_by_index(evlist, event->core.idx); in evlist__set_event_name()
2505 if (evsel->name) in evlist__set_event_name()
2508 evsel->name = strdup(event->name); in evlist__set_event_name()
2520 session = container_of(ff->ph, struct perf_session, header); in process_event_desc()
2522 if (session->data->is_pipe) { in process_event_desc()
2525 ff->events = events; in process_event_desc()
2528 for (evsel = events; evsel->core.attr.size; evsel++) in process_event_desc()
2529 evlist__set_event_name(session->evlist, evsel); in process_event_desc()
2531 if (!session->data->is_pipe) in process_event_desc()
2540 u32 nr, i, len = 0; in process_cmdline() local
2542 if (do_read_u32(ff, &nr)) in process_cmdline()
2543 return -1; in process_cmdline()
2545 ff->ph->env.nr_cmdline = nr; in process_cmdline()
2547 cmdline = zalloc(ff->size + nr + 1); in process_cmdline()
2549 return -1; in process_cmdline()
2551 argv = zalloc(sizeof(char *) * (nr + 1)); in process_cmdline()
2555 for (i = 0; i < nr; i++) { in process_cmdline()
2565 ff->ph->env.cmdline = cmdline; in process_cmdline()
2566 ff->ph->env.cmdline_argv = (const char **) argv; in process_cmdline()
2572 return -1; in process_cmdline()
2577 u32 nr, i; in process_cpu_topology() local
2580 int cpu_nr = ff->ph->env.nr_cpus_avail; in process_cpu_topology()
2582 struct perf_header *ph = ff->ph; in process_cpu_topology()
2585 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu)); in process_cpu_topology()
2586 if (!ph->env.cpu) in process_cpu_topology()
2587 return -1; in process_cpu_topology()
2589 if (do_read_u32(ff, &nr)) in process_cpu_topology()
2592 ph->env.nr_sibling_cores = nr; in process_cpu_topology()
2597 for (i = 0; i < nr; i++) { in process_cpu_topology()
2608 ph->env.sibling_cores = strbuf_detach(&sb, NULL); in process_cpu_topology()
2610 if (do_read_u32(ff, &nr)) in process_cpu_topology()
2611 return -1; in process_cpu_topology()
2613 ph->env.nr_sibling_threads = nr; in process_cpu_topology()
2616 for (i = 0; i < nr; i++) { in process_cpu_topology()
2627 ph->env.sibling_threads = strbuf_detach(&sb, NULL); in process_cpu_topology()
2633 if (ff->size <= size) { in process_cpu_topology()
2634 zfree(&ph->env.cpu); in process_cpu_topology()
2643 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4) in process_cpu_topology()
2644 || !strncmp(ph->env.arch, "aarch64", 7))) in process_cpu_topology()
2648 if (do_read_u32(ff, &nr)) in process_cpu_topology()
2651 ph->env.cpu[i].core_id = nr; in process_cpu_topology()
2654 if (do_read_u32(ff, &nr)) in process_cpu_topology()
2657 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) { in process_cpu_topology()
2663 ph->env.cpu[i].socket_id = nr; in process_cpu_topology()
2671 if (ff->size <= size) in process_cpu_topology()
2674 if (do_read_u32(ff, &nr)) in process_cpu_topology()
2675 return -1; in process_cpu_topology()
2677 ph->env.nr_sibling_dies = nr; in process_cpu_topology()
2680 for (i = 0; i < nr; i++) { in process_cpu_topology()
2691 ph->env.sibling_dies = strbuf_detach(&sb, NULL); in process_cpu_topology()
2694 if (do_read_u32(ff, &nr)) in process_cpu_topology()
2697 ph->env.cpu[i].die_id = nr; in process_cpu_topology()
2705 zfree(&ph->env.cpu); in process_cpu_topology()
2706 return -1; in process_cpu_topology()
2712 u32 nr, i; in process_numa_topology() local
2715 /* nr nodes */ in process_numa_topology()
2716 if (do_read_u32(ff, &nr)) in process_numa_topology()
2717 return -1; in process_numa_topology()
2719 nodes = zalloc(sizeof(*nodes) * nr); in process_numa_topology()
2721 return -ENOMEM; in process_numa_topology()
2723 for (i = 0; i < nr; i++) { in process_numa_topology()
2727 if (do_read_u32(ff, &n->node)) in process_numa_topology()
2730 if (do_read_u64(ff, &n->mem_total)) in process_numa_topology()
2733 if (do_read_u64(ff, &n->mem_free)) in process_numa_topology()
2740 n->map = perf_cpu_map__new(str); in process_numa_topology()
2741 if (!n->map) in process_numa_topology()
2746 ff->ph->env.nr_numa_nodes = nr; in process_numa_topology()
2747 ff->ph->env.numa_nodes = nodes; in process_numa_topology()
2752 return -1; in process_numa_topology()
2763 return -1; in process_pmu_mappings()
2770 ff->ph->env.nr_pmu_mappings = pmu_num; in process_pmu_mappings()
2772 return -1; in process_pmu_mappings()
2789 ff->ph->env.msr_pmu_type = type; in process_pmu_mappings()
2792 pmu_num--; in process_pmu_mappings()
2794 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL); in process_pmu_mappings()
2799 return -1; in process_pmu_mappings()
2804 size_t ret = -1; in process_group_desc()
2805 u32 i, nr, nr_groups; in process_group_desc() local
2815 return -1; in process_group_desc()
2817 ff->ph->env.nr_groups = nr_groups; in process_group_desc()
2825 return -1; in process_group_desc()
2842 session = container_of(ff->ph, struct perf_session, header); in process_group_desc()
2844 i = nr = 0; in process_group_desc()
2845 evlist__for_each_entry(session->evlist, evsel) { in process_group_desc()
2846 if (i < nr_groups && evsel->core.idx == (int) desc[i].leader_idx) { in process_group_desc()
2850 evsel->group_name = desc[i].name; in process_group_desc()
2853 evsel->core.nr_members = desc[i].nr_members; in process_group_desc()
2855 if (i >= nr_groups || nr > 0) { in process_group_desc()
2861 nr = evsel->core.nr_members - 1; in process_group_desc()
2863 } else if (nr) { in process_group_desc()
2867 nr--; in process_group_desc()
2871 if (i != nr_groups || nr != 0) { in process_group_desc()
2890 session = container_of(ff->ph, struct perf_session, header); in process_auxtrace()
2892 err = auxtrace_index__process(ff->fd, ff->size, session, in process_auxtrace()
2893 ff->ph->needs_swap); in process_auxtrace()
2905 return -1; in process_cache()
2908 return -1; in process_cache()
2911 return -1; in process_cache()
2915 return -1; in process_cache()
2943 ff->ph->env.caches = caches; in process_cache()
2944 ff->ph->env.caches_cnt = cnt; in process_cache()
2948 return -1; in process_cache()
2957 session = container_of(ff->ph, struct perf_session, header); in process_sample_time()
2961 return -1; in process_sample_time()
2965 return -1; in process_sample_time()
2967 session->evlist->first_sample_time = first_sample_time; in process_sample_time()
2968 session->evlist->last_sample_time = last_sample_time; in process_sample_time()
2976 u64 version, i, nr, bsize; in process_mem_topology() local
2977 int ret = -1; in process_mem_topology()
2980 return -1; in process_mem_topology()
2983 return -1; in process_mem_topology()
2986 return -1; in process_mem_topology()
2988 if (do_read_u64(ff, &nr)) in process_mem_topology()
2989 return -1; in process_mem_topology()
2991 nodes = zalloc(sizeof(*nodes) * nr); in process_mem_topology()
2993 return -1; in process_mem_topology()
2995 for (i = 0; i < nr; i++) { in process_mem_topology()
3013 ff->ph->env.memory_bsize = bsize; in process_mem_topology()
3014 ff->ph->env.memory_nodes = nodes; in process_mem_topology()
3015 ff->ph->env.nr_memory_nodes = nr; in process_mem_topology()
3027 if (do_read_u64(ff, &ff->ph->env.clock.clockid_res_ns)) in process_clockid()
3028 return -1; in process_clockid()
3041 return -1; in process_clock_data()
3044 return -1; in process_clock_data()
3048 return -1; in process_clock_data()
3050 ff->ph->env.clock.clockid = data32; in process_clock_data()
3054 return -1; in process_clock_data()
3056 ff->ph->env.clock.tod_ns = data64; in process_clock_data()
3060 return -1; in process_clock_data()
3062 ff->ph->env.clock.clockid_ns = data64; in process_clock_data()
3063 ff->ph->env.clock.enabled = true; in process_clock_data()
3071 u32 nr, i; in process_hybrid_topology() local
3073 /* nr nodes */ in process_hybrid_topology()
3074 if (do_read_u32(ff, &nr)) in process_hybrid_topology()
3075 return -1; in process_hybrid_topology()
3077 nodes = zalloc(sizeof(*nodes) * nr); in process_hybrid_topology()
3079 return -ENOMEM; in process_hybrid_topology()
3081 for (i = 0; i < nr; i++) { in process_hybrid_topology()
3084 n->pmu_name = do_read_string(ff); in process_hybrid_topology()
3085 if (!n->pmu_name) in process_hybrid_topology()
3088 n->cpus = do_read_string(ff); in process_hybrid_topology()
3089 if (!n->cpus) in process_hybrid_topology()
3093 ff->ph->env.nr_hybrid_nodes = nr; in process_hybrid_topology()
3094 ff->ph->env.hybrid_nodes = nodes; in process_hybrid_topology()
3098 for (i = 0; i < nr; i++) { in process_hybrid_topology()
3104 return -1; in process_hybrid_topology()
3113 session = container_of(ff->ph, struct perf_session, header); in process_dir_format()
3114 data = session->data; in process_dir_format()
3117 return -1; in process_dir_format()
3119 return do_read_u64(ff, &data->dir.version); in process_dir_format()
3126 struct perf_env *env = &ff->ph->env; in process_bpf_prog_info()
3129 int err = -1; in process_bpf_prog_info()
3131 if (ff->ph->needs_swap) { in process_bpf_prog_info()
3137 return -1; in process_bpf_prog_info()
3139 down_write(&env->bpf_progs.lock); in process_bpf_prog_info()
3160 info_linear->info_len = sizeof(struct bpf_prog_info); in process_bpf_prog_info()
3161 info_linear->data_len = data_len; in process_bpf_prog_info()
3162 if (do_read_u64(ff, (u64 *)(&info_linear->arrays))) in process_bpf_prog_info()
3164 if (__do_read(ff, &info_linear->info, info_len)) in process_bpf_prog_info()
3167 memset(((void *)(&info_linear->info)) + info_len, 0, in process_bpf_prog_info()
3168 sizeof(struct bpf_prog_info) - info_len); in process_bpf_prog_info()
3170 if (__do_read(ff, info_linear->data, data_len)) in process_bpf_prog_info()
3179 info_node->info_linear = info_linear; in process_bpf_prog_info()
3183 up_write(&env->bpf_progs.lock); in process_bpf_prog_info()
3188 up_write(&env->bpf_progs.lock); in process_bpf_prog_info()
3194 struct perf_env *env = &ff->ph->env; in process_bpf_btf()
3197 int err = -1; in process_bpf_btf()
3199 if (ff->ph->needs_swap) { in process_bpf_btf()
3205 return -1; in process_bpf_btf()
3207 down_write(&env->bpf_progs.lock); in process_bpf_btf()
3221 node->id = id; in process_bpf_btf()
3222 node->data_size = data_size; in process_bpf_btf()
3224 if (__do_read(ff, node->data, data_size)) in process_bpf_btf()
3233 up_write(&env->bpf_progs.lock); in process_bpf_btf()
3242 if (do_read_u32(ff, &(ff->ph->env.comp_ver))) in process_compressed()
3243 return -1; in process_compressed()
3245 if (do_read_u32(ff, &(ff->ph->env.comp_type))) in process_compressed()
3246 return -1; in process_compressed()
3248 if (do_read_u32(ff, &(ff->ph->env.comp_level))) in process_compressed()
3249 return -1; in process_compressed()
3251 if (do_read_u32(ff, &(ff->ph->env.comp_ratio))) in process_compressed()
3252 return -1; in process_compressed()
3254 if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len))) in process_compressed()
3255 return -1; in process_compressed()
3270 return -1; in __process_pmu_caps()
3277 return -1; in __process_pmu_caps()
3307 for (; i > 0; i--) in __process_pmu_caps()
3308 free((*caps)[i - 1]); in __process_pmu_caps()
3312 return -1; in __process_pmu_caps()
3318 int ret = __process_pmu_caps(ff, &ff->ph->env.nr_cpu_pmu_caps, in process_cpu_pmu_caps()
3319 &ff->ph->env.cpu_pmu_caps, in process_cpu_pmu_caps()
3320 &ff->ph->env.max_branches); in process_cpu_pmu_caps()
3322 if (!ret && !ff->ph->env.cpu_pmu_caps) in process_cpu_pmu_caps()
3323 pr_debug("cpu pmu capabilities not available\n"); in process_cpu_pmu_caps()
3335 return -1; in process_pmu_caps()
3344 return -ENOMEM; in process_pmu_caps()
3355 ret = -1; in process_pmu_caps()
3364 ff->ph->env.nr_pmus_with_caps = nr_pmu; in process_pmu_caps()
3365 ff->ph->env.pmu_caps = pmu_caps; in process_pmu_caps()
3406 // Only used in util/synthetic-events.c
3459 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { in perf_file_section__fprintf_info()
3461 "%d, continuing...\n", section->offset, feat); in perf_file_section__fprintf_info()
3476 if (!feat_ops[feat].full_only || hd->full) in perf_file_section__fprintf_info()
3477 feat_ops[feat].print(&ff, hd->fp); in perf_file_section__fprintf_info()
3479 fprintf(hd->fp, "# %s info available, use -I to display\n", in perf_file_section__fprintf_info()
3488 struct perf_header *header = &session->header; in perf_header__fprintf_info()
3489 int fd = perf_data__fd(session->data); in perf_header__fprintf_info()
3498 if (ret == -1) in perf_header__fprintf_info()
3499 return -1; in perf_header__fprintf_info()
3504 fprintf(fp, "# header version : %u\n", header->version); in perf_header__fprintf_info()
3505 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset); in perf_header__fprintf_info()
3506 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size); in perf_header__fprintf_info()
3507 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset); in perf_header__fprintf_info()
3512 if (session->data->is_pipe) in perf_header__fprintf_info()
3516 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) { in perf_header__fprintf_info()
3534 return do_write(h->ff, buf, sz); in feat_writer_cb()
3545 if (perf_header__has_feat(ff->ph, type)) { in do_write_feat()
3547 return -1; in do_write_feat()
3549 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__)) in do_write_feat()
3550 return -1; in do_write_feat()
3552 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR); in do_write_feat()
3558 if (fc && fc->copy) { in do_write_feat()
3564 /* ->copy() returns 0 if the feature was not copied */ in do_write_feat()
3565 err = fc->copy(fc, type, &h.fw); in do_write_feat()
3575 lseek(ff->fd, (*p)->offset, SEEK_SET); in do_write_feat()
3577 return -1; in do_write_feat()
3579 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset; in do_write_feat()
3602 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); in perf_header__adds_write()
3608 return -ENOMEM; in perf_header__adds_write()
3612 sec_start = header->feat_offset; in perf_header__adds_write()
3615 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { in perf_header__adds_write()
3661 struct perf_header *header = &session->header; in perf_session__do_write_header()
3670 evlist__for_each_entry(session->evlist, evsel) { in perf_session__do_write_header()
3671 evsel->id_offset = lseek(fd, 0, SEEK_CUR); in perf_session__do_write_header()
3672 err = do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64)); in perf_session__do_write_header()
3682 if (evsel->core.attr.size < sizeof(evsel->core.attr)) { in perf_session__do_write_header()
3688 evsel->core.attr.size = sizeof(evsel->core.attr); in perf_session__do_write_header()
3691 .attr = evsel->core.attr, in perf_session__do_write_header()
3693 .offset = evsel->id_offset, in perf_session__do_write_header()
3694 .size = evsel->core.ids * sizeof(u64), in perf_session__do_write_header()
3704 if (!header->data_offset) in perf_session__do_write_header()
3705 header->data_offset = lseek(fd, 0, SEEK_CUR); in perf_session__do_write_header()
3706 header->feat_offset = header->data_offset + header->data_size; in perf_session__do_write_header()
3720 .size = evlist->core.nr_entries * sizeof(f_attr), in perf_session__do_write_header()
3723 .offset = header->data_offset, in perf_session__do_write_header()
3724 .size = header->data_size, in perf_session__do_write_header()
3729 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); in perf_session__do_write_header()
3737 lseek(fd, header->data_offset + header->data_size, SEEK_SET); in perf_session__do_write_header()
3756 data_offset += evsel->core.ids * sizeof(u64); in perf_session__data_offset()
3758 data_offset += evlist->core.nr_entries * sizeof(struct perf_file_attr); in perf_session__data_offset()
3775 return -1; in perf_header__getbuffer64()
3777 if (header->needs_swap) in perf_header__getbuffer64()
3795 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); in perf_header__process_sections()
3801 return -1; in perf_header__process_sections()
3805 lseek(fd, header->feat_offset, SEEK_SET); in perf_header__process_sections()
3811 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) { in perf_header__process_sections()
3850 ph->needs_swap = true; in try_all_file_abis()
3854 ph->needs_swap); in try_all_file_abis()
3858 return -1; in try_all_file_abis()
3886 ph->needs_swap = true; in try_all_pipe_abis()
3891 return -1; in try_all_pipe_abis()
3912 ph->version = PERF_HEADER_VERSION_1; in check_magic_endian()
3921 * - unique number to identify actual perf.data files in check_magic_endian()
3922 * - encode endianness of file in check_magic_endian()
3924 ph->version = PERF_HEADER_VERSION_2; in check_magic_endian()
3932 return -1; in check_magic_endian()
3934 ph->needs_swap = true; in check_magic_endian()
3948 return -1; in perf_file_header__read()
3950 if (check_magic_endian(header->magic, in perf_file_header__read()
3951 header->attr_size, false, ph) < 0) { in perf_file_header__read()
3953 return -1; in perf_file_header__read()
3956 if (ph->needs_swap) { in perf_file_header__read()
3961 if (header->size != sizeof(*header)) { in perf_file_header__read()
3963 if (header->size == offsetof(typeof(*header), adds_features)) in perf_file_header__read()
3964 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); in perf_file_header__read()
3966 return -1; in perf_file_header__read()
3967 } else if (ph->needs_swap) { in perf_file_header__read()
3969 * feature bitmap is declared as an array of unsigned longs -- in perf_file_header__read()
3975 * guess at determining it: try 64-bit swap first (ie., file in perf_file_header__read()
3976 * created on a 64-bit host), and check if the hostname feature in perf_file_header__read()
3978 * If the bit is not, undo the 64-bit swap and try a 32-bit in perf_file_header__read()
3980 * file), punt and fallback to the original behavior -- in perf_file_header__read()
3983 mem_bswap_64(&header->adds_features, in perf_file_header__read()
3986 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { in perf_file_header__read()
3988 mem_bswap_64(&header->adds_features, in perf_file_header__read()
3992 mem_bswap_32(&header->adds_features, in perf_file_header__read()
3996 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { in perf_file_header__read()
3997 bitmap_zero(header->adds_features, HEADER_FEAT_BITS); in perf_file_header__read()
3998 __set_bit(HEADER_BUILD_ID, header->adds_features); in perf_file_header__read()
4002 memcpy(&ph->adds_features, &header->adds_features, in perf_file_header__read()
4003 sizeof(ph->adds_features)); in perf_file_header__read()
4005 ph->data_offset = header->data.offset; in perf_file_header__read()
4006 ph->data_size = header->data.size; in perf_file_header__read()
4007 ph->feat_offset = header->data.offset + header->data.size; in perf_file_header__read()
4018 .size = section->size, in perf_file_section__process()
4019 .offset = section->offset, in perf_file_section__process()
4022 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { in perf_file_section__process()
4024 "%d, continuing...\n", section->offset, feat); in perf_file_section__process()
4052 return -1; in perf_file_header__read_pipe()
4054 if (check_magic_endian(header->magic, header->size, true, ph) < 0) { in perf_file_header__read_pipe()
4056 return -1; in perf_file_header__read_pipe()
4059 if (ph->needs_swap) in perf_file_header__read_pipe()
4060 header->size = bswap_64(header->size); in perf_file_header__read_pipe()
4063 return -1; in perf_file_header__read_pipe()
4070 struct perf_header *header = &session->header; in perf_header__read_pipe()
4073 if (perf_file_header__read_pipe(&f_header, header, session->data, in perf_header__read_pipe()
4074 session->repipe, repipe_fd) < 0) { in perf_header__read_pipe()
4076 return -EINVAL; in perf_header__read_pipe()
4079 return f_header.size == sizeof(f_header) ? 0 : -1; in perf_header__read_pipe()
4085 struct perf_event_attr *attr = &f_attr->attr; in read_attr()
4087 size_t our_sz = sizeof(f_attr->attr); in read_attr()
4097 return -1; in read_attr()
4101 sz = attr->size; in read_attr()
4103 if (ph->needs_swap) in read_attr()
4111 " (%zu bytes extra)\n", sz - our_sz); in read_attr()
4112 return -1; in read_attr()
4115 left = sz - PERF_ATTR_SIZE_VER0; in read_attr()
4123 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); in read_attr()
4125 return ret <= 0 ? -1 : 0; in read_attr()
4135 if (evsel->tp_format) in evsel__prepare_tracepoint_event()
4140 return -1; in evsel__prepare_tracepoint_event()
4143 event = tep_find_event(pevent, evsel->core.attr.config); in evsel__prepare_tracepoint_event()
4145 pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config); in evsel__prepare_tracepoint_event()
4146 return -1; in evsel__prepare_tracepoint_event()
4149 if (!evsel->name) { in evsel__prepare_tracepoint_event()
4150 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name); in evsel__prepare_tracepoint_event()
4151 evsel->name = strdup(bf); in evsel__prepare_tracepoint_event()
4152 if (evsel->name == NULL) in evsel__prepare_tracepoint_event()
4153 return -1; in evsel__prepare_tracepoint_event()
4156 evsel->tp_format = event; in evsel__prepare_tracepoint_event()
4165 if (pos->core.attr.type == PERF_TYPE_TRACEPOINT && in evlist__prepare_tracepoint_events()
4167 return -1; in evlist__prepare_tracepoint_events()
4176 struct perf_data *data = session->data; in perf_session__read_header()
4177 struct perf_header *header = &session->header; in perf_session__read_header()
4184 session->evlist = evlist__new(); in perf_session__read_header()
4185 if (session->evlist == NULL) in perf_session__read_header()
4186 return -ENOMEM; in perf_session__read_header()
4188 session->evlist->env = &header->env; in perf_session__read_header()
4189 session->machines.host.env = &header->env; in perf_session__read_header()
4197 data->is_pipe = true; in perf_session__read_header()
4202 return -EINVAL; in perf_session__read_header()
4204 if (header->needs_swap && data->in_place_update) { in perf_session__read_header()
4205 pr_err("In-place update not supported when byte-swapping is required\n"); in perf_session__read_header()
4206 return -EINVAL; in perf_session__read_header()
4218 data->file.path); in perf_session__read_header()
4224 data->file.path); in perf_session__read_header()
4225 return -EINVAL; in perf_session__read_header()
4238 if (header->needs_swap) { in perf_session__read_header()
4250 evsel->needs_swap = header->needs_swap; in perf_session__read_header()
4255 evlist__add(session->evlist, evsel); in perf_session__read_header()
4259 * We don't have the cpu and thread maps on the header, so in perf_session__read_header()
4260 * for allocating the perf_sample_id table we fake 1 cpu and in perf_session__read_header()
4261 * hattr->ids threads. in perf_session__read_header()
4263 if (perf_evsel__alloc_id(&evsel->core, 1, nr_ids)) in perf_session__read_header()
4272 perf_evlist__id_add(&session->evlist->core, &evsel->core, 0, j, f_id); in perf_session__read_header()
4279 perf_header__process_sections(header, fd, &session->tevent, in perf_session__read_header()
4282 if (evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent)) in perf_session__read_header()
4290 return -errno; in perf_session__read_header()
4293 evlist__delete(session->evlist); in perf_session__read_header()
4294 session->evlist = NULL; in perf_session__read_header()
4295 return -ENOMEM; in perf_session__read_header()
4301 struct perf_tool *tool = session->tool; in perf_event__process_feature()
4304 int type = fe->header.type; in perf_event__process_feature()
4305 u64 feat = fe->feat_id; in perf_event__process_feature()
4309 pr_warning("invalid record type %d in pipe-mode\n", type); in perf_event__process_feature()
4313 pr_warning("invalid record type %d in pipe-mode\n", type); in perf_event__process_feature()
4314 return -1; in perf_event__process_feature()
4320 ff.buf = (void *)fe->data; in perf_event__process_feature()
4321 ff.size = event->header.size - sizeof(*fe); in perf_event__process_feature()
4322 ff.ph = &session->header; in perf_event__process_feature()
4325 ret = -1; in perf_event__process_feature()
4329 if (!feat_ops[feat].print || !tool->show_feat_hdr) in perf_event__process_feature()
4333 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) { in perf_event__process_feature()
4336 fprintf(stdout, "# %s info available, use -I to display\n", in perf_event__process_feature()
4346 struct perf_record_event_update *ev = &event->event_update; in perf_event__fprintf_event_update()
4350 ret = fprintf(fp, "\n... id: %" PRI_lu64 "\n", ev->id); in perf_event__fprintf_event_update()
4352 switch (ev->type) { in perf_event__fprintf_event_update()
4354 ret += fprintf(fp, "... scale: %f\n", ev->scale.scale); in perf_event__fprintf_event_update()
4357 ret += fprintf(fp, "... unit: %s\n", ev->unit); in perf_event__fprintf_event_update()
4360 ret += fprintf(fp, "... name: %s\n", ev->name); in perf_event__fprintf_event_update()
4365 map = cpu_map__new_data(&ev->cpus.cpus); in perf_event__fprintf_event_update()
4392 return -ENOMEM; in perf_event__process_attr()
4395 evsel = evsel__new(&event->attr.attr); in perf_event__process_attr()
4397 return -ENOMEM; in perf_event__process_attr()
4401 n_ids = event->header.size - sizeof(event->header) - event->attr.attr.size; in perf_event__process_attr()
4404 * We don't have the cpu and thread maps on the header, so in perf_event__process_attr()
4405 * for allocating the perf_sample_id table we fake 1 cpu and in perf_event__process_attr()
4406 * hattr->ids threads. in perf_event__process_attr()
4408 if (perf_evsel__alloc_id(&evsel->core, 1, n_ids)) in perf_event__process_attr()
4409 return -ENOMEM; in perf_event__process_attr()
4413 perf_evlist__id_add(&evlist->core, &evsel->core, 0, i, ids[i]); in perf_event__process_attr()
4423 struct perf_record_event_update *ev = &event->event_update; in perf_event__process_event_update()
4432 return -EINVAL; in perf_event__process_event_update()
4436 evsel = evlist__id2evsel(evlist, ev->id); in perf_event__process_event_update()
4438 return -EINVAL; in perf_event__process_event_update()
4440 switch (ev->type) { in perf_event__process_event_update()
4442 free((char *)evsel->unit); in perf_event__process_event_update()
4443 evsel->unit = strdup(ev->unit); in perf_event__process_event_update()
4446 free(evsel->name); in perf_event__process_event_update()
4447 evsel->name = strdup(ev->name); in perf_event__process_event_update()
4450 evsel->scale = ev->scale.scale; in perf_event__process_event_update()
4453 map = cpu_map__new_data(&ev->cpus.cpus); in perf_event__process_event_update()
4455 perf_cpu_map__put(evsel->core.own_cpus); in perf_event__process_event_update()
4456 evsel->core.own_cpus = map; in perf_event__process_event_update()
4470 ssize_t size_read, padding, size = event->tracing_data.size; in perf_event__process_tracing_data()
4471 int fd = perf_data__fd(session->data); in perf_event__process_tracing_data()
4481 if (!perf_data__is_pipe(session->data)) { in perf_event__process_tracing_data()
4489 size_read = trace_report(fd, &session->tevent, in perf_event__process_tracing_data()
4490 session->repipe); in perf_event__process_tracing_data()
4491 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read; in perf_event__process_tracing_data()
4495 return -1; in perf_event__process_tracing_data()
4497 if (session->repipe) { in perf_event__process_tracing_data()
4501 return -1; in perf_event__process_tracing_data()
4507 return -1; in perf_event__process_tracing_data()
4510 evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent); in perf_event__process_tracing_data()
4519 __event_process_build_id(&event->build_id, in perf_event__process_build_id()
4520 event->build_id.filename, in perf_event__process_build_id()