[PATCH v3 08/15] perf header: use struct feat_fd to process header records

From: David Carrillo-Cisneros
Date: Tue Jun 06 2017 - 03:10:18 EST


As preparation for using header records in pipe-mode, replace
int fd with struct feat_fd ff in process functions for all header
record types.

To replace the argument struct perf-file_section *section in the
process_* functions for feature headers, add offset and size variables
to struct feat_fd.

This patch does not change behavior.

Signed-off-by: David Carrillo-Cisneros <davidcc@xxxxxxxxxx>
---
tools/perf/util/header.c | 170 ++++++++++++++++++++++-------------------------
1 file changed, 79 insertions(+), 91 deletions(-)

diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 7fd4ea15052e..3f22258a633f 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -61,6 +61,8 @@ struct perf_file_attr {
struct feat_fd {
struct perf_header *ph;
int fd;
+ ssize_t offset;
+ size_t size;
};

void perf_header__set_feat(struct perf_header *header, int feat)
@@ -1584,12 +1586,10 @@ static int perf_header__read_build_ids(struct perf_header *header,

/* 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 perf_file_section *section __maybe_unused, \
- struct perf_header *ph, int fd, \
- void *data __maybe_unused) \
+static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
{\
- ph->env.__feat_env = do_read_string(fd, ph); \
- return ph->env.__feat_env ? 0 : -ENOMEM; \
+ ff->ph->env.__feat_env = do_read_string(ff->fd, ff->ph); \
+ return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
}

FEAT_PROCESS_STR_FUN(hostname, hostname);
@@ -1599,53 +1599,46 @@ 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 perf_file_section *section __maybe_unused,
- struct perf_header *ph __maybe_unused,
- int fd, void *data)
+static int process_tracing_data(struct feat_fd *ff, void *data)
{
- ssize_t ret = trace_report(fd, data, false);
+ ssize_t ret = trace_report(ff->fd, data, false);
+
return ret < 0 ? -1 : 0;
}

-static int process_build_id(struct perf_file_section *section,
- struct perf_header *ph, int fd,
- void *data __maybe_unused)
+static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
{
- if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
+ 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 perf_file_section *section __maybe_unused,
- struct perf_header *ph, int fd,
- void *data __maybe_unused)
+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(fd, ph, &nr_cpus_avail);
+ ret = do_read_u32(ff->fd, ff->ph, &nr_cpus_avail);
if (ret)
return ret;

- ret = do_read_u32(fd, ph, &nr_cpus_online);
+ ret = do_read_u32(ff->fd, ff->ph, &nr_cpus_online);
if (ret)
return ret;
- ph->env.nr_cpus_avail = (int)nr_cpus_avail;
- ph->env.nr_cpus_online = (int)nr_cpus_online;
+ 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 perf_file_section *section __maybe_unused,
- struct perf_header *ph, int fd,
- void *data __maybe_unused)
+static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
{
u64 total_mem;
int ret;

- ret = do_read_u64(fd, ph, &total_mem);
+ ret = do_read_u64(ff->fd, ff->ph, &total_mem);
if (ret)
return -1;
- ph->env.total_mem = (unsigned long long)total_mem;
+ ff->ph->env.total_mem = (unsigned long long)total_mem;
return 0;
}

@@ -1682,17 +1675,15 @@ perf_evlist__set_event_name(struct perf_evlist *evlist,
}

static int
-process_event_desc(struct perf_file_section *section __maybe_unused,
- struct perf_header *header, int fd,
- void *data __maybe_unused)
+process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
{
struct perf_session *session;
- struct perf_evsel *evsel, *events = read_event_desc(header, fd);
+ struct perf_evsel *evsel, *events = read_event_desc(ff->ph, ff->fd);

if (!events)
return 0;

- session = container_of(header, struct perf_session, header);
+ session = container_of(ff->ph, struct perf_session, header);
for (evsel = events; evsel->attr.size; evsel++)
perf_evlist__set_event_name(session->evlist, evsel);

@@ -1701,19 +1692,17 @@ process_event_desc(struct perf_file_section *section __maybe_unused,
return 0;
}

-static int process_cmdline(struct perf_file_section *section,
- struct perf_header *ph, int fd,
- void *data __maybe_unused)
+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(fd, ph, &nr))
+ if (do_read_u32(ff->fd, ff->ph, &nr))
return -1;

- ph->env.nr_cmdline = nr;
+ ff->ph->env.nr_cmdline = nr;

- cmdline = zalloc(section->size + nr + 1);
+ cmdline = zalloc(ff->size + nr + 1);
if (!cmdline)
return -1;

@@ -1722,7 +1711,7 @@ static int process_cmdline(struct perf_file_section *section,
goto error;

for (i = 0; i < nr; i++) {
- str = do_read_string(fd, ph);
+ str = do_read_string(ff->fd, ff->ph);
if (!str)
goto error;

@@ -1731,8 +1720,8 @@ static int process_cmdline(struct perf_file_section *section,
len += strlen(str) + 1;
free(str);
}
- ph->env.cmdline = cmdline;
- ph->env.cmdline_argv = (const char **) argv;
+ ff->ph->env.cmdline = cmdline;
+ ff->ph->env.cmdline_argv = (const char **) argv;
return 0;

error:
@@ -1741,21 +1730,21 @@ static int process_cmdline(struct perf_file_section *section,
return -1;
}

-static int process_cpu_topology(struct perf_file_section *section,
- struct perf_header *ph, int fd,
- void *data __maybe_unused)
+static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
{
u32 nr, i;
char *str;
struct strbuf sb;
- int cpu_nr = ph->env.nr_cpus_avail;
+ int cpu_nr = ff->ph->env.nr_cpus_avail;
u64 size = 0;
+ struct perf_header *ph = ff->ph;
+ u64 start_offset = ff->offset;

ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
if (!ph->env.cpu)
return -1;

- if (do_read_u32(fd, ph, &nr))
+ if (do_read_u32(ff->fd, ff->ph, &nr))
goto free_cpu;

ph->env.nr_sibling_cores = nr;
@@ -1764,7 +1753,7 @@ static int process_cpu_topology(struct perf_file_section *section,
goto free_cpu;

for (i = 0; i < nr; i++) {
- str = do_read_string(fd, ph);
+ str = do_read_string(ff->fd, ff->ph);
if (!str)
goto error;

@@ -1776,14 +1765,14 @@ static int process_cpu_topology(struct perf_file_section *section,
}
ph->env.sibling_cores = strbuf_detach(&sb, NULL);

- if (do_read_u32(fd, ph, &nr))
+ if (do_read_u32(ff->fd, ff->ph, &nr))
return -1;

ph->env.nr_sibling_threads = nr;
size += sizeof(u32);

for (i = 0; i < nr; i++) {
- str = do_read_string(fd, ph);
+ str = do_read_string(ff->fd, ff->ph);
if (!str)
goto error;

@@ -1799,18 +1788,18 @@ static int process_cpu_topology(struct perf_file_section *section,
* The header may be from old perf,
* which doesn't include core id and socket id information.
*/
- if (section->size <= size) {
+ if (ff->size <= ff->offset - start_offset) {
zfree(&ph->env.cpu);
return 0;
}

for (i = 0; i < (u32)cpu_nr; i++) {
- if (do_read_u32(fd, ph, &nr))
+ if (do_read_u32(ff->fd, ff->ph, &nr))
goto free_cpu;

ph->env.cpu[i].core_id = nr;

- if (do_read_u32(fd, ph, &nr))
+ if (do_read_u32(ff->fd, ff->ph, &nr))
goto free_cpu;

if (nr != (u32)-1 && nr > (u32)cpu_nr) {
@@ -1831,16 +1820,15 @@ static int process_cpu_topology(struct perf_file_section *section,
return -1;
}

-static int process_numa_topology(struct perf_file_section *section __maybe_unused,
- struct perf_header *ph, int fd,
- void *data __maybe_unused)
+static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
{
+ struct perf_header *ph = ff->ph;
struct numa_node *nodes, *n;
u32 nr, i;
char *str;

/* nr nodes */
- if (do_read_u32(fd, ph, &nr))
+ if (do_read_u32(ff->fd, ff->ph, &nr))
return -1;

nodes = zalloc(sizeof(*nodes) * nr);
@@ -1851,16 +1839,16 @@ static int process_numa_topology(struct perf_file_section *section __maybe_unuse
n = &nodes[i];

/* node number */
- if (do_read_u32(fd, ph, &n->node))
+ if (do_read_u32(ff->fd, ff->ph, &n->node))
goto error;

- if (do_read_u64(fd, ph, &n->mem_total))
+ if (do_read_u64(ff->fd, ff->ph, &n->mem_total))
goto error;

- if (do_read_u64(fd, ph, &n->mem_free))
+ if (do_read_u64(ff->fd, ff->ph, &n->mem_free))
goto error;

- str = do_read_string(fd, ph);
+ str = do_read_string(ff->fd, ff->ph);
if (!str)
goto error;

@@ -1879,16 +1867,15 @@ static int process_numa_topology(struct perf_file_section *section __maybe_unuse
return -1;
}

-static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
- struct perf_header *ph, int fd,
- void *data __maybe_unused)
+static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
{
+ struct perf_header *ph = ff->ph;
char *name;
u32 pmu_num;
u32 type;
struct strbuf sb;

- if (do_read_u32(fd, ph, &pmu_num))
+ if (do_read_u32(ff->fd, ff->ph, &pmu_num))
return -1;

if (!pmu_num) {
@@ -1901,10 +1888,10 @@ static int process_pmu_mappings(struct perf_file_section *section __maybe_unused
return -1;

while (pmu_num) {
- if (do_read_u32(fd, ph, &type))
+ if (do_read_u32(ff->fd, ff->ph, &type))
goto error;

- name = do_read_string(fd, ph);
+ name = do_read_string(ff->fd, ff->ph);
if (!name)
goto error;

@@ -1928,10 +1915,9 @@ static int process_pmu_mappings(struct perf_file_section *section __maybe_unused
return -1;
}

-static int process_group_desc(struct perf_file_section *section __maybe_unused,
- struct perf_header *ph, int fd,
- void *data __maybe_unused)
+static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
{
+ struct perf_header *ph = ff->ph;
size_t ret = -1;
u32 i, nr, nr_groups;
struct perf_session *session;
@@ -1942,7 +1928,7 @@ static int process_group_desc(struct perf_file_section *section __maybe_unused,
u32 nr_members;
} *desc;

- if (do_read_u32(fd, ph, &nr_groups))
+ if (do_read_u32(ff->fd, ff->ph, &nr_groups))
return -1;

ph->env.nr_groups = nr_groups;
@@ -1956,14 +1942,14 @@ static int process_group_desc(struct perf_file_section *section __maybe_unused,
return -1;

for (i = 0; i < nr_groups; i++) {
- desc[i].name = do_read_string(fd, ph);
+ desc[i].name = do_read_string(ff->fd, ff->ph);
if (!desc[i].name)
goto out_free;

- if (do_read_u32(fd, ph, &desc[i].leader_idx))
+ if (do_read_u32(ff->fd, ff->ph, &desc[i].leader_idx))
goto out_free;

- if (do_read_u32(fd, ph, &desc[i].nr_members))
+ if (do_read_u32(ff->fd, ff->ph, &desc[i].nr_members))
goto out_free;
}

@@ -2014,36 +2000,32 @@ static int process_group_desc(struct perf_file_section *section __maybe_unused,
return ret;
}

-static int process_auxtrace(struct perf_file_section *section,
- struct perf_header *ph, int fd,
- void *data __maybe_unused)
+static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
{
struct perf_session *session;
int err;

- session = container_of(ph, struct perf_session, header);
+ session = container_of(ff->ph, struct perf_session, header);

- err = auxtrace_index__process(fd, section->size, session,
- ph->needs_swap);
+ 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 perf_file_section *section __maybe_unused,
- struct perf_header *ph __maybe_unused, int fd __maybe_unused,
- void *data __maybe_unused)
+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(fd, ph, &version))
+ if (do_read_u32(ff->fd, ff->ph, &version))
return -1;

if (version != 1)
return -1;

- if (do_read_u32(fd, ph, &cnt))
+ if (do_read_u32(ff->fd, ff->ph, &cnt))
return -1;

caches = zalloc(sizeof(*caches) * cnt);
@@ -2054,7 +2036,7 @@ static int process_cache(struct perf_file_section *section __maybe_unused,
struct cpu_cache_level c;

#define _R(v) \
- if (do_read_u32(fd, ph, &c.v))\
+ if (do_read_u32(ff->fd, ff->ph, &c.v))\
goto out_free_caches; \

_R(level)
@@ -2063,9 +2045,9 @@ static int process_cache(struct perf_file_section *section __maybe_unused,
_R(ways)
#undef _R

- #define _R(v) \
- c.v = do_read_string(fd, ph); \
- if (!c.v) \
+ #define _R(v) \
+ c.v = do_read_string(ff->fd, ff->ph); \
+ if (!c.v) \
goto out_free_caches;

_R(type)
@@ -2076,8 +2058,8 @@ static int process_cache(struct perf_file_section *section __maybe_unused,
caches[i] = c;
}

- ph->env.caches = caches;
- ph->env.caches_cnt = cnt;
+ ff->ph->env.caches = caches;
+ ff->ph->env.caches_cnt = cnt;
return 0;
out_free_caches:
free(caches);
@@ -2087,8 +2069,7 @@ static int process_cache(struct perf_file_section *section __maybe_unused,
struct feature_ops {
int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
void (*print)(struct feat_fd *ff, FILE *fp);
- int (*process)(struct perf_file_section *section,
- struct perf_header *h, int fd, void *data);
+ int (*process)(struct feat_fd *ff, void *data);
const char *name;
bool full_only;
};
@@ -2619,6 +2600,13 @@ static int perf_file_section__process(struct perf_file_section *section,
struct perf_header *ph,
int feat, int fd, void *data)
{
+ struct feat_fd fdd = {
+ .fd = fd,
+ .ph = ph,
+ .size = section->size,
+ .offset = section->offset,
+ };
+
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);
@@ -2633,7 +2621,7 @@ static int perf_file_section__process(struct perf_file_section *section,
if (!feat_ops[feat].process)
return 0;

- return feat_ops[feat].process(section, ph, fd, data);
+ return feat_ops[feat].process(&fdd, data);
}

static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
--
2.13.1.508.gb3defc5cc-goog