blob: dec6d218c31c326ceac4d4464c8c9ce012a1be61 [file] [log] [blame]
// SPDX-License-Identifier: GPL-2.0
#include <errno.h>
#include <inttypes.h>
#include "util.h"
#include "string2.h"
#include <sys/param.h>
#include <sys/types.h>
#include <byteswap.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/compiler.h>
#include <linux/list.h>
#include <linux/kernel.h>
#include <linux/bitops.h>
#include <linux/stringify.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <linux/time64.h>
#include <dirent.h>
#include "evlist.h"
#include "evsel.h"
#include "header.h"
#include "memswap.h"
#include "../perf.h"
#include "trace-event.h"
#include "session.h"
#include "symbol.h"
#include "debug.h"
#include "cpumap.h"
#include "pmu.h"
#include "vdso.h"
#include "strbuf.h"
#include "build-id.h"
#include "data.h"
#include <api/fs/fs.h>
#include "asm/bug.h"
#include "tool.h"
#include "time-utils.h"
#include "units.h"
#include "sane_ctype.h"
/*
* magic2 = "PERFILE2"
* must be a numerical value to let the endianness
* determine the memory layout. That way we are able
* to detect endianness when reading the perf.data file
* back.
*
* we check for legacy (PERFFILE) format.
*/
static const char *__perf_magic1 = "PERFFILE";
static const u64 __perf_magic2 = 0x32454c4946524550ULL;
static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
#define PERF_MAGIC __perf_magic2
const char perf_version_string[] = PERF_VERSION;
struct perf_file_attr {
struct perf_event_attr attr;
struct perf_file_section ids;
};
struct feat_fd {
struct perf_header *ph;
int fd;
void *buf; /* Either buf != NULL or fd >= 0 */
ssize_t offset;
size_t size;
struct perf_evsel *events;
};
void perf_header__set_feat(struct perf_header *header, int feat)
{
set_bit(feat, header->adds_features);
}
void perf_header__clear_feat(struct perf_header *header, int feat)
{
clear_bit(feat, header->adds_features);
}
bool perf_header__has_feat(const struct perf_header *header, int feat)
{
return test_bit(feat, header->adds_features);
}
static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
{
ssize_t ret = writen(ff->fd, buf, size);
if (ret != (ssize_t)size)
return ret < 0 ? (int)ret : -1;
return 0;
}
static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
{
/* struct perf_event_header::size is u16 */
const size_t max_size = 0xffff - sizeof(struct perf_event_header);
size_t new_size = ff->size;
void *addr;
if (size + ff->offset > max_size)
return -E2BIG;
while (size > (new_size - ff->offset))
new_size <<= 1;
new_size = min(max_size, new_size);
if (ff->size < new_size) {
addr = realloc(ff->buf, new_size);
if (!addr)
return -ENOMEM;
ff->buf = addr;
ff->size = new_size;
}
memcpy(ff->buf + ff->offset, buf, size);
ff->offset += size;
return 0;
}
/* Return: 0 if succeded, -ERR if failed. */
int do_write(struct feat_fd *ff, const void *buf, size_t size)
{
if (!ff->buf)
return __do_write_fd(ff, buf, size);
return __do_write_buf(ff, buf, size);
}
/* Return: 0 if succeded, -ERR if failed. */
static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
{
u64 *p = (u64 *) set;
int i, ret;
ret = do_write(ff, &size, sizeof(size));
if (ret < 0)
return ret;
for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
ret = do_write(ff, p + i, sizeof(*p));
if (ret < 0)
return ret;
}
return 0;
}
/* Return: 0 if succeded, -ERR if failed. */
int write_padded(struct feat_fd *ff, const void *bf,
size_t count, size_t count_aligned)
{
static const char zero_buf[NAME_ALIGN];
int err = do_write(ff, bf, count);
if (!err)
err = do_write(ff, zero_buf, count_aligned - count);
return err;
}
#define string_size(str) \
(PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
/* Return: 0 if succeded, -ERR if failed. */
static int do_write_string(struct feat_fd *ff, const char *str)
{
u32 len, olen;
int ret;
olen = strlen(str) + 1;
len = PERF_ALIGN(olen, NAME_ALIGN);
/* write len, incl. \0 */
ret = do_write(ff, &len, sizeof(len));
if (ret < 0)
return ret;
return write_padded(ff, str, olen, len);
}
static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
{
ssize_t ret = readn(ff->fd, addr, size);
if (ret != size)
return ret < 0 ? (int)ret : -1;
return 0;
}
static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
{
if (size > (ssize_t)ff->size - ff->offset)
return -1;
memcpy(addr, ff->buf + ff->offset, size);
ff->offset += size;
return 0;
}
static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
{
if (!ff->buf)
return __do_read_fd(ff, addr, size);
return __do_read_buf(ff, addr, size);
}
static int do_read_u32(struct feat_fd *ff, u32 *addr)
{
int ret;
ret = __do_read(ff, addr, sizeof(*addr));
if (ret)
return ret;
if (ff->ph->needs_swap)
*addr = bswap_32(*addr);
return 0;
}
static int do_read_u64(struct feat_fd *ff, u64 *addr)
{
int ret;
ret = __do_read(ff, addr, sizeof(*addr));
if (ret)
return ret;
if (ff->ph->needs_swap)
*addr = bswap_64(*addr);
return 0;
}
static char *do_read_string(struct feat_fd *ff)
{
u32 len;
char *buf;
if (do_read_u32(ff, &len))
return NULL;
buf = malloc(len);
if (!buf)
return NULL;
if (!__do_read(ff, buf, len)) {
/*
* strings are padded by zeroes
* thus the actual strlen of buf
* may be less than len
*/
return buf;
}
free(buf);
return NULL;
}
/* Return: 0 if succeded, -ERR if failed. */
static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
{
unsigned long *set;
u64 size, *p;
int i, ret;
ret = do_read_u64(ff, &size);
if (ret)
return ret;
set = bitmap_alloc(size);
if (!set)
return -ENOMEM;
p = (u64 *) set;
for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
ret = do_read_u64(ff, p + i);
if (ret < 0) {
free(set);
return ret;
}
}
*pset = set;
*psize = size;
return 0;
}
static int write_tracing_data(struct feat_fd *ff,
struct perf_evlist *evlist)
{
if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
return -1;
return read_tracing_data(ff->fd, &evlist->entries);
}
static int write_build_id(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
struct perf_session *session;
int err;
session = container_of(ff->ph, struct perf_session, header);
if (!perf_session__read_build_ids(session, true))
return -1;
if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
return -1;
err = perf_session__write_buildid_table(session, ff);
if (err < 0) {
pr_debug("failed to write buildid table\n");
return err;
}
perf_session__cache_build_ids(session);
return 0;
}
static int write_hostname(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
struct utsname uts;
int ret;
ret = uname(&uts);
if (ret < 0)
return -1;
return do_write_string(ff, uts.nodename);
}
static int write_osrelease(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
struct utsname uts;
int ret;
ret = uname(&uts);
if (ret < 0)
return -1;
return do_write_string(ff, uts.release);
}
static int write_arch(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
struct utsname uts;
int ret;
ret = uname(&uts);
if (ret < 0)
return -1;
return do_write_string(ff, uts.machine);
}
static int write_version(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
return do_write_string(ff, perf_version_string);
}
static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
{
FILE *file;
char *buf = NULL;
char *s, *p;
const char *search = cpuinfo_proc;
size_t len = 0;
int ret = -1;
if (!search)
return -1;
file = fopen("/proc/cpuinfo", "r");
if (!file)
return -1;
while (getline(&buf, &len, file) > 0) {
ret = strncmp(buf, search, strlen(search));
if (!ret)
break;
}
if (ret) {
ret = -1;
goto done;
}
s = buf;
p = strchr(buf, ':');
if (p && *(p+1) == ' ' && *(p+2))
s = p + 2;
p = strchr(s, '\n');
if (p)
*p = '\0';
/* squash extra space characters (branding string) */
p = s;
while (*p) {
if (isspace(*p)) {
char *r = p + 1;
char *q = r;
*p = ' ';
while (*q && isspace(*q))
q++;
if (q != (p+1))
while ((*r++ = *q++));
}
p++;
}
ret = do_write_string(ff, s);
done:
free(buf);
fclose(file);
return ret;
}
static int write_cpudesc(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
const char *cpuinfo_procs[] = CPUINFO_PROC;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
int ret;
ret = __write_cpudesc(ff, cpuinfo_procs[i]);
if (ret >= 0)
return ret;
}
return -1;
}
static int write_nrcpus(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
long nr;
u32 nrc, nra;
int ret;
nrc = cpu__max_present_cpu();
nr = sysconf(_SC_NPROCESSORS_ONLN);
if (nr < 0)
return -1;
nra = (u32)(nr & UINT_MAX);
ret = do_write(ff, &nrc, sizeof(nrc));
if (ret < 0)
return ret;
return do_write(ff, &nra, sizeof(nra));
}
static int write_event_desc(struct feat_fd *ff,
struct perf_evlist *evlist)
{
struct perf_evsel *evsel;
u32 nre, nri, sz;
int ret;
nre = evlist->nr_entries;
/*
* write number of events
*/
ret = do_write(ff, &nre, sizeof(nre));
if (ret < 0)
return ret;
/*
* size of perf_event_attr struct
*/
sz = (u32)sizeof(evsel->attr);
ret = do_write(ff, &sz, sizeof(sz));
if (ret < 0)
return ret;
evlist__for_each_entry(evlist, evsel) {
ret = do_write(ff, &evsel->attr, sz);
if (ret < 0)
return ret;
/*
* write number of unique id per event
* there is one id per instance of an event
*
* copy into an nri to be independent of the
* type of ids,
*/
nri = evsel->ids;
ret = do_write(ff, &nri, sizeof(nri));
if (ret < 0)
return ret;
/*
* write event string as passed on cmdline
*/
ret = do_write_string(ff, perf_evsel__name(evsel));
if (ret < 0)
return ret;
/*
* write unique ids for this event
*/
ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
if (ret < 0)
return ret;
}
return 0;
}
static int write_cmdline(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
char buf[MAXPATHLEN];
u32 n;
int i, ret;
/* actual path to perf binary */
ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
if (ret <= 0)
return -1;
/* readlink() does not add null termination */
buf[ret] = '\0';
/* account for binary path */
n = perf_env.nr_cmdline + 1;
ret = do_write(ff, &n, sizeof(n));
if (ret < 0)
return ret;
ret = do_write_string(ff, buf);
if (ret < 0)
return ret;
for (i = 0 ; i < perf_env.nr_cmdline; i++) {
ret = do_write_string(ff, perf_env.cmdline_argv[i]);
if (ret < 0)
return ret;
}
return 0;
}
#define CORE_SIB_FMT \
"/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
#define THRD_SIB_FMT \
"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
struct cpu_topo {
u32 cpu_nr;
u32 core_sib;
u32 thread_sib;
char **core_siblings;
char **thread_siblings;
};
static int build_cpu_topo(struct cpu_topo *tp, int cpu)
{
FILE *fp;
char filename[MAXPATHLEN];
char *buf = NULL, *p;
size_t len = 0;
ssize_t sret;
u32 i = 0;
int ret = -1;
sprintf(filename, CORE_SIB_FMT, cpu);
fp = fopen(filename, "r");
if (!fp)
goto try_threads;
sret = getline(&buf, &len, fp);
fclose(fp);
if (sret <= 0)
goto try_threads;
p = strchr(buf, '\n');
if (p)
*p = '\0';
for (i = 0; i < tp->core_sib; i++) {
if (!strcmp(buf, tp->core_siblings[i]))
break;
}
if (i == tp->core_sib) {
tp->core_siblings[i] = buf;
tp->core_sib++;
buf = NULL;
len = 0;
}
ret = 0;
try_threads:
sprintf(filename, THRD_SIB_FMT, cpu);
fp = fopen(filename, "r");
if (!fp)
goto done;
if (getline(&buf, &len, fp) <= 0)
goto done;
p = strchr(buf, '\n');
if (p)
*p = '\0';
for (i = 0; i < tp->thread_sib; i++) {
if (!strcmp(buf, tp->thread_siblings[i]))
break;
}
if (i == tp->thread_sib) {
tp->thread_siblings[i] = buf;
tp->thread_sib++;
buf = NULL;
}
ret = 0;
done:
if(fp)
fclose(fp);
free(buf);
return ret;
}
static void free_cpu_topo(struct cpu_topo *tp)
{
u32 i;
if (!tp)
return;
for (i = 0 ; i < tp->core_sib; i++)
zfree(&tp->core_siblings[i]);
for (i = 0 ; i < tp->thread_sib; i++)
zfree(&tp->thread_siblings[i]);
free(tp);
}
static struct cpu_topo *build_cpu_topology(void)
{
struct cpu_topo *tp = NULL;
void *addr;
u32 nr, i;
size_t sz;
long ncpus;
int ret = -1;
struct cpu_map *map;
ncpus = cpu__max_present_cpu();
/* build online CPU map */
map = cpu_map__new(NULL);
if (map == NULL) {
pr_debug("failed to get system cpumap\n");
return NULL;
}
nr = (u32)(ncpus & UINT_MAX);
sz = nr * sizeof(char *);
addr = calloc(1, sizeof(*tp) + 2 * sz);
if (!addr)
goto out_free;
tp = addr;
tp->cpu_nr = nr;
addr += sizeof(*tp);
tp->core_siblings = addr;
addr += sz;
tp->thread_siblings = addr;
for (i = 0; i < nr; i++) {
if (!cpu_map__has(map, i))
continue;
ret = build_cpu_topo(tp, i);
if (ret < 0)
break;
}
out_free:
cpu_map__put(map);
if (ret) {
free_cpu_topo(tp);
tp = NULL;
}
return tp;
}
static int write_cpu_topology(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
struct cpu_topo *tp;
u32 i;
int ret, j;
tp = build_cpu_topology();
if (!tp)
return -1;
ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
if (ret < 0)
goto done;
for (i = 0; i < tp->core_sib; i++) {
ret = do_write_string(ff, tp->core_siblings[i]);
if (ret < 0)
goto done;
}
ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
if (ret < 0)
goto done;
for (i = 0; i < tp->thread_sib; i++) {
ret = do_write_string(ff, tp->thread_siblings[i]);
if (ret < 0)
break;
}
ret = perf_env__read_cpu_topology_map(&perf_env);
if (ret < 0)
goto done;
for (j = 0; j < perf_env.nr_cpus_avail; j++) {
ret = do_write(ff, &perf_env.cpu[j].core_id,
sizeof(perf_env.cpu[j].core_id));
if (ret < 0)
return ret;
ret = do_write(ff, &perf_env.cpu[j].socket_id,
sizeof(perf_env.cpu[j].socket_id));
if (ret < 0)
return ret;
}
done:
free_cpu_topo(tp);
return ret;
}
static int write_total_mem(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
char *buf = NULL;
FILE *fp;
size_t len = 0;
int ret = -1, n;
uint64_t mem;
fp = fopen("/proc/meminfo", "r");
if (!fp)
return -1;
while (getline(&buf, &len, fp) > 0) {
ret = strncmp(buf, "MemTotal:", 9);
if (!ret)
break;
}
if (!ret) {
n = sscanf(buf, "%*s %"PRIu64, &mem);
if (n == 1)
ret = do_write(ff, &mem, sizeof(mem));
} else
ret = -1;
free(buf);
fclose(fp);
return ret;
}
static int write_topo_node(struct feat_fd *ff, int node)
{
char str[MAXPATHLEN];
char field[32];
char *buf = NULL, *p;
size_t len = 0;
FILE *fp;
u64 mem_total, mem_free, mem;
int ret = -1;
sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
fp = fopen(str, "r");
if (!fp)
return -1;
while (getline(&buf, &len, fp) > 0) {
/* skip over invalid lines */
if (!strchr(buf, ':'))
continue;
if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
goto done;
if (!strcmp(field, "MemTotal:"))
mem_total = mem;
if (!strcmp(field, "MemFree:"))
mem_free = mem;
}
fclose(fp);
fp = NULL;
ret = do_write(ff, &mem_total, sizeof(u64));
if (ret)
goto done;
ret = do_write(ff, &mem_free, sizeof(u64));
if (ret)
goto done;
ret = -1;
sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
fp = fopen(str, "r");
if (!fp)
goto done;
if (getline(&buf, &len, fp) <= 0)
goto done;
p = strchr(buf, '\n');
if (p)
*p = '\0';
ret = do_write_string(ff, buf);
done:
free(buf);
if (fp)
fclose(fp);
return ret;
}
static int write_numa_topology(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
char *buf = NULL;
size_t len = 0;
FILE *fp;
struct cpu_map *node_map = NULL;
char *c;
u32 nr, i, j;
int ret = -1;
fp = fopen("/sys/devices/system/node/online", "r");
if (!fp)
return -1;
if (getline(&buf, &len, fp) <= 0)
goto done;
c = strchr(buf, '\n');
if (c)
*c = '\0';
node_map = cpu_map__new(buf);
if (!node_map)
goto done;
nr = (u32)node_map->nr;
ret = do_write(ff, &nr, sizeof(nr));
if (ret < 0)
goto done;
for (i = 0; i < nr; i++) {
j = (u32)node_map->map[i];
ret = do_write(ff, &j, sizeof(j));
if (ret < 0)
break;
ret = write_topo_node(ff, i);
if (ret < 0)
break;
}
done:
free(buf);
fclose(fp);
cpu_map__put(node_map);
return ret;
}
/*
* File format:
*
* struct pmu_mappings {
* u32 pmu_num;
* struct pmu_map {
* u32 type;
* char name[];
* }[pmu_num];
* };
*/
static int write_pmu_mappings(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
struct perf_pmu *pmu = NULL;
u32 pmu_num = 0;
int ret;
/*
* Do a first pass to count number of pmu to avoid lseek so this
* works in pipe mode as well.
*/
while ((pmu = perf_pmu__scan(pmu))) {
if (!pmu->name)
continue;
pmu_num++;
}
ret = do_write(ff, &pmu_num, sizeof(pmu_num));
if (ret < 0)
return ret;
while ((pmu = perf_pmu__scan(pmu))) {
if (!pmu->name)
continue;
ret = do_write(ff, &pmu->type, sizeof(pmu->type));
if (ret < 0)
return ret;
ret = do_write_string(ff, pmu->name);
if (ret < 0)
return ret;
}
return 0;
}
/*
* File format:
*
* struct group_descs {
* u32 nr_groups;
* struct group_desc {
* char name[];
* u32 leader_idx;
* u32 nr_members;
* }[nr_groups];
* };
*/
static int write_group_desc(struct feat_fd *ff,
struct perf_evlist *evlist)
{
u32 nr_groups = evlist->nr_groups;
struct perf_evsel *evsel;
int ret;
ret = do_write(ff, &nr_groups, sizeof(nr_groups));
if (ret < 0)
return ret;
evlist__for_each_entry(evlist, evsel) {
if (perf_evsel__is_group_leader(evsel) &&
evsel->nr_members > 1) {
const char *name = evsel->group_name ?: "{anon_group}";
u32 leader_idx = evsel->idx;
u32 nr_members = evsel->nr_members;
ret = do_write_string(ff, name);
if (ret < 0)
return ret;
ret = do_write(ff, &leader_idx, sizeof(leader_idx));
if (ret < 0)
return ret;
ret = do_write(ff, &nr_members, sizeof(nr_members));
if (ret < 0)
return ret;
}
}
return 0;
}
/*
* Return the CPU id as a raw string.
*
* Each architecture should provide a more precise id string that
* can be use to match the architecture's "mapfile".
*/
char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
{
return NULL;
}
/* Return zero when the cpuid from the mapfile.csv matches the
* cpuid string generated on this platform.
* Otherwise return non-zero.
*/
int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
{
regex_t re;
regmatch_t pmatch[1];
int match;
if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
/* Warn unable to generate match particular string. */
pr_info("Invalid regular expression %s\n", mapcpuid);
return 1;
}
match = !regexec(&re, cpuid, 1, pmatch, 0);
regfree(&re);
if (match) {
size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
/* Verify the entire string matched. */
if (match_len == strlen(cpuid))
return 0;
}
return 1;
}
/*
* default get_cpuid(): nothing gets recorded
* actual implementation must be in arch/$(SRCARCH)/util/header.c
*/
int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
{
return -1;
}
static int write_cpuid(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
char buffer[64];
int ret;
ret = get_cpuid(buffer, sizeof(buffer));
if (!ret)
goto write_it;
return -1;
write_it:
return do_write_string(ff, buffer);
}
static int write_branch_stack(struct feat_fd *ff __maybe_unused,
struct perf_evlist *evlist __maybe_unused)
{
return 0;
}
static int write_auxtrace(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
struct perf_session *session;
int err;
if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
return -1;
session = container_of(ff->ph, struct perf_session, header);
err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
if (err < 0)
pr_err("Failed to write auxtrace index\n");
return err;
}
static int write_clockid(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
return do_write(ff, &ff->ph->env.clockid_res_ns,
sizeof(ff->ph->env.clockid_res_ns));
}
static int cpu_cache_level__sort(const void *a, const void *b)
{
struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
return cache_a->level - cache_b->level;
}
static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
{
if (a->level != b->level)
return false;
if (a->line_size != b->line_size)
return false;
if (a->sets != b->sets)
return false;
if (a->ways != b->ways)
return false;
if (strcmp(a->type, b->type))
return false;
if (strcmp(a->size, b->size))
return false;
if (strcmp(a->map, b->map))
return false;
return true;
}
static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
{
char path[PATH_MAX], file[PATH_MAX];
struct stat st;
size_t len;
scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
if (stat(file, &st))
return 1;
scnprintf(file, PATH_MAX, "%s/level", path);
if (sysfs__read_int(file, (int *) &cache->level))
return -1;
scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
if (sysfs__read_int(file, (int *) &cache->line_size))
return -1;
scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
if (sysfs__read_int(file, (int *) &cache->sets))
return -1;
scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
if (sysfs__read_int(file, (int *) &cache->ways))
return -1;
scnprintf(file, PATH_MAX, "%s/type", path);
if (sysfs__read_str(file, &cache->type, &len))
return -1;
cache->type[len] = 0;
cache->type = rtrim(cache->type);
scnprintf(file, PATH_MAX, "%s/size", path);
if (sysfs__read_str(file, &cache->size, &len)) {
free(cache->type);
return -1;
}
cache->size[len] = 0;
cache->size = rtrim(cache->size);
scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
if (sysfs__read_str(file, &cache->map, &len)) {
free(cache->map);
free(cache->type);
return -1;
}
cache->map[len] = 0;
cache->map = rtrim(cache->map);
return 0;
}
static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
{
fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
}
static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
{
u32 i, cnt = 0;
long ncpus;
u32 nr, cpu;
u16 level;
ncpus = sysconf(_SC_NPROCESSORS_CONF);
if (ncpus < 0)
return -1;
nr = (u32)(ncpus & UINT_MAX);
for (cpu = 0; cpu < nr; cpu++) {
for (level = 0; level < 10; level++) {
struct cpu_cache_level c;
int err;
err = cpu_cache_level__read(&c, cpu, level);
if (err < 0)
return err;
if (err == 1)
break;
for (i = 0; i < cnt; i++) {
if (cpu_cache_level__cmp(&c, &caches[i]))
break;
}
if (i == cnt)
caches[cnt++] = c;
else
cpu_cache_level__free(&c);
if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
goto out;
}
}
out:
*cntp = cnt;
return 0;
}
#define MAX_CACHES 2000
static int write_cache(struct feat_fd *ff,
struct perf_evlist *evlist __maybe_unused)
{
struct cpu_cache_level caches[MAX_CACHES];
u32 cnt = 0, i, version = 1;
int ret;
ret = build_caches(caches, MAX_CACHES, &cnt);
if (ret)
goto out;
qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
ret = do_write(ff, &version, sizeof(u32));
if (ret < 0)
goto out;
ret = do_write(ff, &cnt, sizeof(u32));
if (ret < 0)
goto out;
for (i = 0; i < cnt; i++) {
struct cpu_cache_level *c = &caches[i];
#define _W(v) \
ret = do_write(ff, &c->v, sizeof(u32)); \
if (ret < 0) \
goto out;
_W(level)
_W(line_size)
_W(sets)
_W(ways)
#undef _W
#define _W(v) \
ret = do_write_string(ff, (const char *) c->v); \
if (ret < 0) \
goto out;
_W(type)
_W(size)
_W(map)
#undef _W
}
out:
for (i = 0; i < cnt; i++)
cpu_cache_level__free(&caches[i]);
return ret;
}
static int write_stat(struct feat_fd *ff __maybe_unused,
struct perf_evlist *evlist __maybe_unused)
{
return 0;
}
static int write_sample_time(struct feat_fd *ff,
struct perf_evlist *evlist)
{
int ret;
ret = do_write(ff, &evlist->first_sample_time,
sizeof(evlist->first_sample_time));
if (ret < 0)
return ret;
return do_write(ff, &evlist->last_sample_time,
sizeof(evlist->last_sample_time));
}
static int memory_node__read(struct memory_node *n, unsigned long idx)
{
unsigned int phys, size = 0;
char path[PATH_MAX];
struct dirent *ent;
DIR *dir;
#define for_each_memory(mem, dir) \
while ((ent = readdir(dir))) \
if (strcmp(ent->d_name, ".") && \
strcmp(ent->d_name, "..") && \
sscanf(ent->d_name, "memory%u", &mem) == 1)
scnprintf(path, PATH_MAX,
"%s/devices/system/node/node%lu",
sysfs__mountpoint(), idx);
dir = opendir(path);
if (!dir) {
pr_warning("failed: cant' open memory sysfs data\n");
return -1;
}
for_each_memory(phys, dir) {
size = max(phys, size);
}
size++;
n->set = bitmap_alloc(size);
if (!n->set) {
closedir(dir);
return -ENOMEM;
}
n->node = idx;
n->size = size;
rewinddir(dir);
for_each_memory(phys, dir) {
set_bit(phys, n->set);
}
closedir(dir);
return 0;
}
static int memory_node__sort(const void *a, const void *b)
{
const struct memory_node *na = a;
const struct memory_node *nb = b;
return na->node - nb->node;
}
static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
{
char path[PATH_MAX];
struct dirent *ent;
DIR *dir;
u64 cnt = 0;
int ret = 0;
scnprintf(path, PATH_MAX, "%s/devices/system/node/",
sysfs__mountpoint());
dir = opendir(path);
if (!dir) {
pr_debug2("%s: could't read %s, does this arch have topology information?\n",
__func__, path);
return -1;
}
while (!ret && (ent = readdir(dir))) {
unsigned int idx;
int r;
if (!strcmp(ent->d_name, ".") ||
!strcmp(ent->d_name, ".."))
continue;
r = sscanf(ent->d_name, "node%u", &idx);
if (r != 1)
continue;
if (WARN_ONCE(cnt >= size,
"failed to write MEM_TOPOLOGY, way too many nodes\n"))
return -1;
ret = memory_node__read(&nodes[cnt++], idx);
}
*cntp = cnt;
closedir(dir);
if (!ret)
qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
return ret;
}
#define MAX_MEMORY_NODES 2000
/*
* The MEM_TOPOLOGY holds physical memory map for every
* node in system. The format of data is as follows:
*
* 0 - version | for future changes
* 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
* 16 - count | number of nodes
*
* For each node we store map of physical indexes for
* each node:
*
* 32 - node id | node index
* 40 - size | size of bitmap
* 48 - bitmap | bitmap of memory indexes that belongs to node
*/
static int write_mem_topology(struct feat_fd *ff __maybe_unused,
struct perf_evlist *evlist __maybe_unused)
{
static struct memory_node nodes[MAX_MEMORY_NODES];
u64 bsize, version = 1, i, nr;
int ret;
ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
(unsigned long long *) &bsize);
if (ret)
return ret;
ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
if (ret)
return ret;
ret = do_write(ff, &version, sizeof(version));
if (ret < 0)
goto out;
ret = do_write(ff, &bsize, sizeof(bsize));
if (ret < 0)
goto out;
ret = do_write(ff, &nr, sizeof(nr));
if (ret < 0)
goto out;
for (i = 0; i < nr; i++) {
struct memory_node *n = &nodes[i];
#define _W(v) \
ret = do_write(ff, &n->v, sizeof(n->v)); \
if (ret < 0) \
goto out;
_W(node)
_W(size)
#undef _W
ret = do_write_bitmap(ff, n->set, n->size);
if (ret < 0)
goto out;
}
out:
return ret;
}
static void print_hostname(struct feat_fd *ff, FILE *fp)
{
fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
}
static void print_osrelease(struct feat_fd *ff, FILE *fp)
{
fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
}
static void print_arch(struct feat_fd *ff, FILE *fp)
{
fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
}
static void print_cpudesc(struct feat_fd *ff, FILE *fp)
{
fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
}
static void print_nrcpus(struct feat_fd *ff, FILE *fp)
{
fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
}
static void print_version(struct feat_fd *ff, FILE *fp)
{
fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
}
static void print_cmdline(struct feat_fd *ff, FILE *fp)
{
int nr, i;
nr = ff->ph->env.nr_cmdline;
fprintf(fp, "# cmdline : ");
for (i = 0; i < nr; i++) {
char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
if (!argv_i) {
fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
} else {
char *mem = argv_i;
do {
char *quote = strchr(argv_i, '\'');
if (!quote)
break;
*quote++ = '\0';
fprintf(fp, "%s\\\'", argv_i);
argv_i = quote;
} while (1);
fprintf(fp, "%s ", argv_i);
free(mem);
}
}
fputc('\n', fp);
}
static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
{
struct perf_header *ph = ff->ph;
int cpu_nr = ph->env.nr_cpus_avail;
int nr, i;
char *str;
nr = ph->env.nr_sibling_cores;
str = ph->env.sibling_cores;
for (i = 0; i < nr; i++) {
fprintf(fp, "# sibling cores : %s\n", str);
str += strlen(str) + 1;
}
nr = ph->env.nr_sibling_threads;
str = ph->env.sibling_threads;
for (i = 0; i < nr; i++) {
fprintf(fp, "# sibling threads : %s\n", str);
str += strlen(str) + 1;
}
if (ph->env.cpu != NULL) {
for (i = 0; i < cpu_nr; i++)
fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
} else
fprintf(fp, "# Core ID and Socket ID information is not available\n");
}
static void print_clockid(struct feat_fd *ff, FILE *fp)
{
fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
ff->ph->env.clockid_res_ns * 1000);
}
static void free_event_desc(struct perf_evsel *events)
{
struct perf_evsel *evsel;
if (!events)
return;
for (evsel = events; evsel->attr.size; evsel++) {
zfree(&evsel->name);
zfree(&evsel->id);
}
free(events);
}
static struct perf_evsel *read_event_desc(struct feat_fd *ff)
{
struct perf_evsel *evsel, *events = NULL;
u64 *id;
void *buf = NULL;
u32 nre, sz, nr, i, j;
size_t msz;
/* number of events */
if (do_read_u32(ff, &nre))
goto error;
if (do_read_u32(ff, &sz))
goto error;
/* buffer to hold on file attr struct */
buf = malloc(sz);
if (!buf)
goto error;
/* the last event terminates with evsel->attr.size == 0: */
events = calloc(nre + 1, sizeof(*events));
if (!events)
goto error;
msz = sizeof(evsel->attr);
if (sz < msz)
msz = sz;
for (i = 0, evsel = events; i < nre; evsel++, i++) {
evsel->idx = i;
/*
* must read entire on-file attr struct to
* sync up with layout.
*/
if (__do_read(ff, buf, sz))
goto error;
if (ff->ph->needs_swap)
perf_event__attr_swap(buf);
memcpy(&evsel->attr, buf, msz);
if (do_read_u32(ff, &nr))
goto error;
if (ff->ph->needs_swap)
evsel->needs_swap = true;
evsel->name = do_read_string(ff);
if (!evsel->name)
goto error;
if (!nr)
continue;
id = calloc(nr, sizeof(*id));
if (!id)
goto error;
evsel->ids = nr;
evsel->id = id;
for (j = 0 ; j < nr; j++) {
if (do_read_u64(ff, id))
goto error;
id++;
}
}
out:
free(buf);
return events;
error:
free_event_desc(events);
events = NULL;
goto out;
}
static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
void *priv __maybe_unused)
{
return fprintf(fp, ", %s = %s", name, val);
}
static void print_event_desc(struct feat_fd *ff, FILE *fp)
{
struct perf_evsel *evsel, *events;
u32 j;
u64 *id;
if (ff->events)
events = ff->events;
else
events = read_event_desc(ff);
if (!events) {
fprintf(fp, "# event desc: not available or unable to read\n");
return;
}
for (evsel = events; evsel->attr.size; evsel++) {
fprintf(fp, "# event : name = %s, ", evsel->name);
if (evsel->ids) {
fprintf(fp, ", id = {");
for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
if (j)
fputc(',', fp);
fprintf(fp, " %"PRIu64, *id);
}
fprintf(fp, " }");
}
perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
fputc('\n', fp);
}
free_event_desc(events);
ff->events = NULL;
}
static void print_total_mem(struct feat_fd *ff, FILE *fp)
{
fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
}
static void print_numa_topology(struct feat_fd *ff, FILE *fp)
{
int i;
struct numa_node *n;
for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
n = &ff->ph->env.numa_nodes[i];
fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
" free = %"PRIu64" kB\n",
n->node, n->mem_total, n->mem_free);
fprintf(fp, "# node%u cpu list : ", n->node);
cpu_map__fprintf(n->map, fp);
}
}
static void print_cpuid(struct feat_fd *ff, FILE *fp)
{
fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
}
static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
{
fprintf(fp, "# contains samples with branch stack\n");
}
static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
{
fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
}
static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
{
fprintf(fp, "# contains stat data\n");
}
static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
{
int i;
fprintf(fp, "# CPU cache info:\n");
for (i = 0; i < ff->ph->env.caches_cnt; i++) {
fprintf(fp, "# ");
cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
}
}
static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
{
const char *delimiter = "# pmu mappings: ";
char *str, *tmp;
u32 pmu_num;
u32 type;
pmu_num = ff->ph->env.nr_pmu_mappings;
if (!pmu_num) {
fprintf(fp, "# pmu mappings: not available\n");
return;
}
str = ff->ph->env.pmu_mappings;
while (pmu_num) {
type = strtoul(str, &tmp, 0);
if (*tmp != ':')
goto error;
str = tmp + 1;
fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
delimiter = ", ";
str += strlen(str) + 1;
pmu_num--;
}
fprintf(fp, "\n");
if (!pmu_num)
return;
error:
fprintf(fp, "# pmu mappings: unable to read\n");
}
static void print_group_desc(struct feat_fd *ff, FILE *fp)
{
struct perf_session *session;
struct perf_evsel *evsel;
u32 nr = 0;
session = container_of(ff->ph, struct perf_session, header);
evlist__for_each_entry(session->evlist, evsel) {
if (perf_evsel__is_group_leader(evsel) &&
evsel->nr_members > 1) {
fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
perf_evsel__name(evsel));
nr = evsel->nr_members - 1;
} else if (nr) {
fprintf(fp, ",%s", perf_evsel__name(evsel));
if (--nr == 0)
fprintf(fp, "}\n");
}
}
}
static void print_sample_time(struct feat_fd *ff, FILE *fp)
{
struct perf_session *session;
char time_buf[32];
double d;
session = container_of(ff->ph, struct perf_session, header);
timestamp__scnprintf_usec(session->evlist->first_sample_time,
time_buf, sizeof(time_buf));
fprintf(fp, "# time of first sample : %s\n", time_buf);
timestamp__scnprintf_usec(session->evlist->last_sample_time,
time_buf, sizeof(time_buf));
fprintf(fp, "# time of last sample : %s\n", time_buf);
d = (double)(session->evlist->last_sample_time -
session->evlist->first_sample_time) / NSEC_PER_MSEC;
fprintf(fp, "# sample duration : %10.3f ms\n", d);
}
static void memory_node__fprintf(struct memory_node *n,
unsigned long long bsize, FILE *fp)
{
char buf_map[100], buf_size[50];
unsigned long long size;
size = bsize * bitmap_weight(n->set, n->size);
unit_number__scnprintf(buf_size, 50, size);
bitmap_scnprintf(n->set, n->size, buf_map, 100);
fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
}
static void print_mem_topology(struct feat_fd *ff, FILE *fp)
{
struct memory_node *nodes;
int i, nr;
nodes = ff->ph->env.memory_nodes;
nr = ff->ph->env.nr_memory_nodes;
fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
nr, ff->ph->env.memory_bsize);
for (i = 0; i < nr; i++) {
memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
}
}
static int __event_process_build_id(struct build_id_event *bev,
char *filename,
struct perf_session *session)
{
int err = -1;
struct machine *machine;
u16 cpumode;
struct dso *dso;
enum dso_kernel_type dso_type;
machine = perf_session__findnew_machine(session, bev->pid);
if (!machine)
goto out;
cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
switch (cpumode) {
case PERF_RECORD_MISC_KERNEL:
dso_type = DSO_TYPE_KERNEL;
break;
case PERF_RECORD_MISC_GUEST_KERNEL:
dso_type = DSO_TYPE_GUEST_KERNEL;
break;
case PERF_RECORD_MISC_USER:
case PERF_RECORD_MISC_GUEST_USER:
dso_type = DSO_TYPE_USER;
break;
default:
goto out;
}
dso = machine__findnew_dso(machine, filename);
if (dso != NULL) {
char sbuild_id[SBUILD_ID_SIZE];
dso__set_build_id(dso, &bev->build_id);
if (dso_type != DSO_TYPE_USER) {
struct kmod_path m = { .name = NULL, };
if (!kmod_path__parse_name(&m, filename) && m.kmod)
dso__set_module_info(dso, &m, machine);
else
dso->kernel = dso_type;
free(m.name);
}
build_id__sprintf(dso->build_id, sizeof(dso->build_id),
sbuild_id);
pr_debug("build id event received for %s: %s\n",
dso->long_name, sbuild_id);
dso__put(dso);
}
err = 0;
out:
return err;
}
static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
int input, u64 offset, u64 size)
{
struct perf_session *session = container_of(header, struct perf_session, header);
struct {
struct perf_event_header header;
u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
char filename[0];
} old_bev;
struct build_id_event bev;
char filename[PATH_MAX];
u64 limit = offset + size;
while (offset < limit) {
ssize_t len;
if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
return -1;
if (header->needs_swap)
perf_event_header__bswap(&old_bev.header);
len = old_bev.header.size - sizeof(old_bev);
if (readn(input, filename, len) != len)
return -1;
bev.header = old_bev.header;
/*
* As the pid is the missing value, we need to fill
* it properly. The header.misc value give us nice hint.
*/
bev.pid = HOST_KERNEL_ID;
if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
bev.pid = DEFAULT_GUEST_KERNEL_ID;
memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
__event_process_build_id(&bev, filename, session);
offset += bev.header.size;
}
return 0;
}
static int perf_header__read_build_ids(struct perf_header *header,
int input, u64 offset, u64 size)
{
struct perf_session *session = container_of(header, struct perf_session, header);
struct build_id_event bev;
char filename[PATH_MAX];
u64 limit = offset + size, orig_offset = offset;
int err = -1;
while (offset < limit) {
ssize_t len;
if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
goto out;
if (header->needs_swap)
perf_event_header__bswap(&bev.header);
len = bev.header.size - sizeof(bev);
if (readn(input, filename, len) != len)
goto out;
/*
* The a1645ce1 changeset:
*
* "perf: 'perf kvm' tool for monitoring guest performance from host"
*
* Added a field to struct build_id_event that broke the file
* format.
*
* Since the kernel build-id is the first entry, process the
* table using the old format if the well known
* '[kernel.kallsyms]' string for the kernel build-id has the
* first 4 characters chopped off (where the pid_t sits).
*/
if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
return -1;
return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
}
__event_process_build_id(&bev, filename, session);
offset += bev.header.size;
}
err = 0;
out:
return err;
}
/* Macro for features that simply need to read and store a string. */
#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
{\
ff->ph->env.__feat_env = do_read_string(ff); \
return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
}
FEAT_PROCESS_STR_FUN(hostname, hostname);
FEAT_PROCESS_STR_FUN(osrelease, os_release);
FEAT_PROCESS_STR_FUN(version, version);
FEAT_PROCESS_STR_FUN(arch, arch);
FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
FEAT_PROCESS_STR_FUN(cpuid, cpuid);
static int process_tracing_data(struct feat_fd *ff, void *data)
{
ssize_t ret = trace_report(ff->fd, data, false);
return ret < 0 ? -1 : 0;
}
static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
{
if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
pr_debug("Failed to read buildids, continuing...\n");
return 0;
}
static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
{
int ret;
u32 nr_cpus_avail, nr_cpus_online;
ret = do_read_u32(ff, &nr_cpus_avail);
if (ret)
return ret;
ret = do_read_u32(ff, &nr_cpus_online);
if (ret)
return ret;
ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
return 0;
}
static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
{
u64 total_mem;
int ret;
ret = do_read_u64(ff, &total_mem);
if (ret)
return -1;
ff->ph->env.total_mem = (unsigned long long)total_mem;
return 0;
}
static struct perf_evsel *
perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
{
struct perf_evsel *evsel;
evlist__for_each_entry(evlist, evsel) {
if (evsel->idx == idx)
return evsel;
}
return NULL;
}
static void
perf_evlist__set_event_name(struct perf_evlist *evlist,
struct perf_evsel *event)
{
struct perf_evsel *evsel;
if (!event->name)
return;
evsel = perf_evlist__find_by_index(evlist, event->idx);
if (!evsel)
return;
if (evsel->name)
return;
evsel->name = strdup(event->name);
}
static int
process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
{
struct perf_session *session;
struct perf_evsel *evsel, *events = read_event_desc(ff);
if (!events)
return 0;
session = container_of(ff->ph, struct perf_session, header);
if (session->data->is_pipe) {
/* Save events for reading later by print_event_desc,
* since they can't be read again in pipe mode. */
ff->events = events;
}
for (evsel = events; evsel->attr.size; evsel++)
perf_evlist__set_event_name(session->evlist, evsel);
if (!session->data->is_pipe)
free_event_desc(events);
return 0;
}
static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
{
char *str, *cmdline = NULL, **argv = NULL;
u32 nr, i, len = 0;
if (do_read_u32(ff, &nr))
return -1;
ff->ph->env.nr_cmdline = nr;
cmdline = zalloc(ff->size + nr + 1);
if (!cmdline)
return -1;
argv = zalloc(sizeof(char *) * (nr + 1));
if (!argv)
goto error;
for (i = 0; i < nr; i++) {
str = do_read_string(ff);
if (!str)
goto error;
argv[i] = cmdline + len;
memcpy(argv[i], str, strlen(str) + 1);
len += strlen(str) + 1;
free(str);
}
ff->ph->env.cmdline = cmdline;
ff->ph->env.cmdline_argv = (const char **) argv;
return 0;
error:
free(argv);
free(cmdline);
return -1;
}
static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
{
u32 nr, i;
char *str;
struct strbuf sb;
int cpu_nr = ff->ph->env.nr_cpus_avail;
u64 size = 0;
struct perf_header *ph = ff->ph;
bool do_core_id_test = true;
ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
if (!ph->env.cpu)
return -1;
if (do_read_u32(ff, &nr))
goto free_cpu;
ph->env.nr_sibling_cores = nr;
size += sizeof(u32);
if (strbuf_init(&sb, 128) < 0)
goto free_cpu;
for (i = 0; i < nr; i++) {
str = do_read_string(ff);
if (!str)
goto error;
/* include a NULL character at the end */
if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
goto error;
size += string_size(str);
free(str);
}
ph->env.sibling_cores = strbuf_detach(&sb, NULL);
if (do_read_u32(ff, &nr))
return -1;
ph->env.nr_sibling_threads = nr;
size += sizeof(u32);
for (i = 0; i < nr; i++) {
str = do_read_string(ff);
if (!str)
goto error;
/* include a NULL character at the end */
if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
goto error;
size += string_size(str);
free(str);
}
ph->env.sibling_threads = strbuf_detach(&sb, NULL);
/*
* The header may be from old perf,
* which doesn't include core id and socket id information.
*/
if (ff->size <= size) {
zfree(&ph->env.cpu);
return 0;
}
/* On s390 the socket_id number is not related to the numbers of cpus.
* The socket_id number might be higher than the numbers of cpus.
* This depends on the configuration.
*/
if (ph->env.arch && !strncmp(ph->env.arch, "s390", 4))
do_core_id_test = false;
for (i = 0; i < (u32)cpu_nr; i++) {
if (do_read_u32(ff, &nr))
goto free_cpu;
ph->env.cpu[i].core_id = nr;
if (do_read_u32(ff, &nr))
goto free_cpu;
if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
pr_debug("socket_id number is too big."
"You may need to upgrade the perf tool.\n");
goto free_cpu;
}
ph->env.cpu[i].socket_id = nr;
}
return 0;
error:
strbuf_release(&sb);
free_cpu:
zfree(&ph->env.cpu);
return -1;
}
static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
{
struct numa_node *nodes, *n;
u32 nr, i;
char *str;
/* nr nodes */
if (do_read_u32(ff, &nr))
return -1;
nodes = zalloc(sizeof(*nodes) * nr);
if (!nodes)
return -ENOMEM;
for (i = 0; i < nr; i++) {
n = &nodes[i];
/* node number */
if (do_read_u32(ff, &n->node))
goto error;
if (do_read_u64(ff, &n->mem_total))
goto error;
if (do_read_u64(ff, &n->mem_free))
goto error;
str = do_read_string(ff);
if (!str)
goto error;
n->map = cpu_map__new(str);
if (!n->map)
goto error;
free(str);
}
ff->ph->env.nr_numa_nodes = nr;
ff->ph->env.numa_nodes = nodes;
return 0;
error:
free(nodes);
return -1;
}
static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
{
char *name;
u32 pmu_num;
u32 type;
struct strbuf sb;
if (do_read_u32(ff, &pmu_num))
return -1;
if (!pmu_num) {
pr_debug("pmu mappings not available\n");
return 0;
}
ff->ph->env.nr_pmu_mappings = pmu_num;
if (strbuf_init(&sb, 128) < 0)
return -1;
while (pmu_num) {
if (do_read_u32(ff, &type))
goto error;
name = do_read_string(ff);
if (!name)
goto error;
if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
goto error;
/* include a NULL character at the end */
if (strbuf_add(&sb, "", 1) < 0)
goto error;
if (!strcmp(name, "msr"))
ff->ph->env.msr_pmu_type = type;
free(name);
pmu_num--;
}
ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
return 0;
error:
strbuf_release(&sb);
return -1;
}
static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
{
size_t ret = -1;
u32 i, nr, nr_groups;
struct perf_session *session;
struct perf_evsel *evsel, *leader = NULL;
struct group_desc {
char *name;
u32 leader_idx;
u32 nr_members;
} *desc;
if (do_read_u32(ff, &nr_groups))
return -1;
ff->ph->env.nr_groups = nr_groups;
if (!nr_groups) {
pr_debug("group desc not available\n");
return 0;
}
desc = calloc(nr_groups, sizeof(*desc));
if (!desc)
return -1;
for (i = 0; i < nr_groups; i++) {
desc[i].name = do_read_string(ff);
if (!desc[i].name)
goto out_free;
if (do_read_u32(ff, &desc[i].leader_idx))
goto out_free;
if (do_read_u32(ff, &desc[i].nr_members))
goto out_free;
}
/*
* Rebuild group relationship based on the group_desc
*/
session = container_of(ff->ph, struct perf_session, header);
session->evlist->nr_groups = nr_groups;
i = nr = 0;
evlist__for_each_entry(session->evlist, evsel) {
if (evsel->idx == (int) desc[i].leader_idx) {
evsel->leader = evsel;
/* {anon_group} is a dummy name */
if (strcmp(desc[i].name, "{anon_group}")) {
evsel->group_name = desc[i].name;
desc[i].name = NULL;
}
evsel->nr_members = desc[i].nr_members;
if (i >= nr_groups || nr > 0) {
pr_debug("invalid group desc\n");
goto out_free;
}
leader = evsel;
nr = evsel->nr_members - 1;
i++;
} else if (nr) {
/* This is a group member */
evsel->leader = leader;
nr--;
}
}
if (i != nr_groups || nr != 0) {
pr_debug("invalid group desc\n");
goto out_free;
}
ret = 0;
out_free:
for (i = 0; i < nr_groups; i++)
zfree(&desc[i].name);
free(desc);
return ret;
}
static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
{
struct perf_session *session;
int err;
session = container_of(ff->ph, struct perf_session, header);
err = auxtrace_index__process(ff->fd, ff->size, session,
ff->ph->needs_swap);
if (err < 0)
pr_err("Failed to process auxtrace index\n");
return err;
}
static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
{
struct cpu_cache_level *caches;
u32 cnt, i, version;
if (do_read_u32(ff, &version))
return -1;
if (version != 1)
return -1;
if (do_read_u32(ff, &cnt))
return -1;
caches = zalloc(sizeof(*caches) * cnt);
if (!caches)
return -1;
for (i = 0; i < cnt; i++) {
struct cpu_cache_level c;
#define _R(v) \
if (do_read_u32(ff, &c.v))\
goto out_free_caches; \
_R(level)
_R(line_size)
_R(sets)
_R(ways)
#undef _R
#define _R(v) \
c.v = do_read_string(ff); \
if (!c.v) \
goto out_free_caches;
_R(type)
_R(size)
_R(map)
#undef _R
caches[i] = c;
}
ff->ph->env.caches = caches;
ff->ph->env.caches_cnt = cnt;
return 0;
out_free_caches:
free(caches);
return -1;
}
static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
{
struct perf_session *session;
u64 first_sample_time, last_sample_time;
int ret;
session = container_of(ff->ph, struct perf_session, header);
ret = do_read_u64(ff, &first_sample_time);
if (ret)
return -1;
ret = do_read_u64(ff, &last_sample_time);
if (ret)
return -1;
session->evlist->first_sample_time = first_sample_time;
session->evlist->last_sample_time = last_sample_time;
return 0;
}
static int process_mem_topology(struct feat_fd *ff,
void *data __maybe_unused)
{
struct memory_node *nodes;
u64 version, i, nr, bsize;
int ret = -1;
if (do_read_u64(ff, &version))
return -1;
if (version != 1)
return -1;
if (do_read_u64(ff, &bsize))
return -1;
if (do_read_u64(ff, &nr))
return -1;
nodes = zalloc(sizeof(*nodes) * nr);
if (!nodes)
return -1;
for (i = 0; i < nr; i++) {
struct memory_node n;
#define _R(v) \
if (do_read_u64(ff, &n.v)) \
goto out; \
_R(node)
_R(size)
#undef _R
if (do_read_bitmap(ff, &n.set, &n.size))
goto out;
nodes[i] = n;
}
ff->ph->env.memory_bsize = bsize;
ff->ph->env.memory_nodes = nodes;
ff->ph->env.nr_memory_nodes = nr;
ret = 0;
out:
if (ret)
free(nodes);
return ret;
}
static int process_clockid(struct feat_fd *ff,
void *data __maybe_unused)
{
if (do_read_u64(ff, &ff->ph->env.clockid_res_ns))
return -1;
return 0;
}
struct feature_ops {
int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
void (*print)(struct feat_fd *ff, FILE *fp);
int (*process)(struct feat_fd *ff, void *data);
const char *name;
bool full_only;
bool synthesize;
};
#define FEAT_OPR(n, func, __full_only) \
[HEADER_##n] = { \
.name = __stringify(n), \
.write = write_##func, \
.print = print_##func, \
.full_only = __full_only, \
.process = process_##func, \
.synthesize = true \
}
#define FEAT_OPN(n, func, __full_only) \
[HEADER_##n] = { \
.name = __stringify(n), \
.write = write_##func, \
.print = print_##func, \
.full_only = __full_only, \
.process = process_##func \
}
/* feature_ops not implemented: */
#define print_tracing_data NULL
#define print_build_id NULL
#define process_branch_stack NULL
#define process_stat NULL
static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
FEAT_OPN(TRACING_DATA, tracing_data, false),
FEAT_OPN(BUILD_ID, build_id, false),
FEAT_OPR(HOSTNAME, hostname, false),
FEAT_OPR(OSRELEASE, osrelease, false),
FEAT_OPR(VERSION, version, false),
FEAT_OPR(ARCH, arch, false),
FEAT_OPR(NRCPUS, nrcpus, false),
FEAT_OPR(CPUDESC, cpudesc, false),
FEAT_OPR(CPUID, cpuid, false),
FEAT_OPR(TOTAL_MEM, total_mem, false),
FEAT_OPR(EVENT_DESC, event_desc, false),
FEAT_OPR(CMDLINE, cmdline, false),
FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
FEAT_OPN(BRANCH_STACK, branch_stack, false),
FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
FEAT_OPR(GROUP_DESC, group_desc, false),
FEAT_OPN(AUXTRACE, auxtrace, false),
FEAT_OPN(STAT, stat, false),
FEAT_OPN(CACHE, cache, true),
FEAT_OPR(SAMPLE_TIME, sample_time, false),
FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
FEAT_OPR(CLOCKID, clockid, false)
};
struct header_print_data {
FILE *fp;
bool full; /* extended list of headers */
};
static int perf_file_section__fprintf_info(struct perf_file_section *section,
struct perf_header *ph,
int feat, int fd, void *data)
{
struct header_print_data *hd = data;
struct feat_fd ff;
if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
"%d, continuing...\n", section->offset, feat);
return 0;
}
if (feat >= HEADER_LAST_FEATURE) {
pr_warning("unknown feature %d\n", feat);
return 0;
}
if (!feat_ops[feat].print)
return 0;
ff = (struct feat_fd) {
.fd = fd,
.ph = ph,
};
if (!feat_ops[feat].full_only || hd->full)
feat_ops[feat].print(&ff, hd->fp);
else
fprintf(hd->fp, "# %s info available, use -I to display\n",
feat_ops[feat].name);
return 0;
}
int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
{
struct header_print_data hd;
struct perf_header *header = &session->header;
int fd = perf_data__fd(session->data);
struct stat st;
time_t stctime;
int ret, bit;
hd.fp = fp;
hd.full = full;
ret = fstat(fd, &st);
if (ret == -1)
return -1;
stctime = st.st_ctime;
fprintf(fp, "# captured on : %s", ctime(&stctime));
fprintf(fp, "# header version : %u\n", header->version);
fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
perf_header__process_sections(header, fd, &hd,
perf_file_section__fprintf_info);