Introduce struct feat_fd. This patch uses it as a wrapper
around fd in write_* functions for feature headers. Next
patches will extend its functionality to other feature
header functions.

This patch does not change behavior.

Signed-off-by: David Carrillo-Cisneros <davi...@google.com>
---
 tools/perf/util/build-id.c |   8 ++-
 tools/perf/util/build-id.h |   4 +-
 tools/perf/util/header.c   | 134 ++++++++++++++++++++++++++-------------------
 tools/perf/util/header.h   |   7 ++-
 4 files changed, 90 insertions(+), 63 deletions(-)

diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index 84d2ea51e557..292e90db3924 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -330,7 +330,7 @@ bool dso__build_id_is_kmod(const struct dso *dso, char *bf, 
size_t size)
                else
 
 static int write_buildid(const char *name, size_t name_len, u8 *build_id,
-                        pid_t pid, u16 misc, int fd)
+                        pid_t pid, u16 misc, struct feat_fd *fd)
 {
        int err;
        struct build_id_event b;
@@ -352,7 +352,8 @@ static int write_buildid(const char *name, size_t name_len, 
u8 *build_id,
        return write_padded(fd, name, name_len + 1, len);
 }
 
-static int machine__write_buildid_table(struct machine *machine, int fd)
+static int machine__write_buildid_table(struct machine *machine,
+                                       struct feat_fd *fd)
 {
        int err = 0;
        char nm[PATH_MAX];
@@ -397,7 +398,8 @@ static int machine__write_buildid_table(struct machine 
*machine, int fd)
        return err;
 }
 
-int perf_session__write_buildid_table(struct perf_session *session, int fd)
+int perf_session__write_buildid_table(struct perf_session *session,
+                                     struct feat_fd *fd)
 {
        struct rb_node *nd;
        int err = machine__write_buildid_table(&session->machines.host, fd);
diff --git a/tools/perf/util/build-id.h b/tools/perf/util/build-id.h
index a96081121179..84e5e8a52970 100644
--- a/tools/perf/util/build-id.h
+++ b/tools/perf/util/build-id.h
@@ -9,6 +9,7 @@
 
 extern struct perf_tool build_id__mark_dso_hit_ops;
 struct dso;
+struct feat_fd;
 
 int build_id__sprintf(const u8 *build_id, int len, char *bf);
 int sysfs__sprintf_build_id(const char *root_dir, char *sbuild_id);
@@ -26,7 +27,8 @@ int build_id__mark_dso_hit(struct perf_tool *tool, union 
perf_event *event,
 int dsos__hit_all(struct perf_session *session);
 
 bool perf_session__read_build_ids(struct perf_session *session, bool 
with_hits);
-int perf_session__write_buildid_table(struct perf_session *session, int fd);
+int perf_session__write_buildid_table(struct perf_session *session,
+                                     struct feat_fd *fd);
 int perf_session__cache_build_ids(struct perf_session *session);
 
 char *build_id_cache__origname(const char *sbuild_id);
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 292fb2156a59..4934ef543aef 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -57,6 +57,11 @@ struct perf_file_attr {
        struct perf_file_section        ids;
 };
 
+struct feat_fd {
+       struct perf_header *ph;
+       int fd;
+};
+
 void perf_header__set_feat(struct perf_header *header, int feat)
 {
        set_bit(feat, header->adds_features);
@@ -73,11 +78,11 @@ bool perf_header__has_feat(const struct perf_header 
*header, int feat)
 }
 
 /* Return: 0 if succeded, -ERR if failed. */
-int do_write(int fd, const void *buf, size_t size)
+int do_write(struct feat_fd *fd, const void *buf, size_t size)
 {
        ssize_t ret;
 
-       ret  = writen(fd, buf, size);
+       ret  = writen(fd->fd, buf, size);
        if (ret != (ssize_t)size)
                return ret < 0 ? (int)ret: -1;
 
@@ -85,7 +90,8 @@ int do_write(int fd, const void *buf, size_t size)
 }
 
 /* Return: 0 if succeded, -ERR if failed. */
-int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
+int write_padded(struct feat_fd *fd, const void *bf,
+                size_t count, size_t count_aligned)
 {
        static const char zero_buf[NAME_ALIGN];
        int err = do_write(fd, bf, count);
@@ -100,7 +106,7 @@ int write_padded(int fd, const void *bf, size_t count, 
size_t count_aligned)
        (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
 
 /* Return: 0 if succeded, -ERR if failed. */
-static int do_write_string(int fd, const char *str)
+static int do_write_string(struct feat_fd *fd, const char *str)
 {
        u32 len, olen;
        int ret;
@@ -176,20 +182,19 @@ static char *do_read_string(int fd, struct perf_header 
*ph)
        return NULL;
 }
 
-static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
-                           struct perf_evlist *evlist)
+static int write_tracing_data(struct feat_fd *fd,
+                             struct perf_evlist *evlist)
 {
-       return read_tracing_data(fd, &evlist->entries);
+       return read_tracing_data(fd->fd, &evlist->entries);
 }
 
-
-static int write_build_id(int fd, struct perf_header *h,
+static int write_build_id(struct feat_fd *fd,
                          struct perf_evlist *evlist __maybe_unused)
 {
        struct perf_session *session;
        int err;
 
-       session = container_of(h, struct perf_session, header);
+       session = container_of(fd->ph, struct perf_session, header);
 
        if (!perf_session__read_build_ids(session, true))
                return -1;
@@ -204,7 +209,7 @@ static int write_build_id(int fd, struct perf_header *h,
        return 0;
 }
 
-static int write_hostname(int fd, struct perf_header *h __maybe_unused,
+static int write_hostname(struct feat_fd *fd,
                          struct perf_evlist *evlist __maybe_unused)
 {
        struct utsname uts;
@@ -217,7 +222,7 @@ static int write_hostname(int fd, struct perf_header *h 
__maybe_unused,
        return do_write_string(fd, uts.nodename);
 }
 
-static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
+static int write_osrelease(struct feat_fd *fd,
                           struct perf_evlist *evlist __maybe_unused)
 {
        struct utsname uts;
@@ -230,7 +235,7 @@ static int write_osrelease(int fd, struct perf_header *h 
__maybe_unused,
        return do_write_string(fd, uts.release);
 }
 
-static int write_arch(int fd, struct perf_header *h __maybe_unused,
+static int write_arch(struct feat_fd *fd,
                      struct perf_evlist *evlist __maybe_unused)
 {
        struct utsname uts;
@@ -243,13 +248,13 @@ static int write_arch(int fd, struct perf_header *h 
__maybe_unused,
        return do_write_string(fd, uts.machine);
 }
 
-static int write_version(int fd, struct perf_header *h __maybe_unused,
+static int write_version(struct feat_fd *fd,
                         struct perf_evlist *evlist __maybe_unused)
 {
        return do_write_string(fd, perf_version_string);
 }
 
-static int __write_cpudesc(int fd, const char *cpuinfo_proc)
+static int __write_cpudesc(struct feat_fd *fd, const char *cpuinfo_proc)
 {
        FILE *file;
        char *buf = NULL;
@@ -306,7 +311,7 @@ static int __write_cpudesc(int fd, const char *cpuinfo_proc)
        return ret;
 }
 
-static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
+static int write_cpudesc(struct feat_fd *fd,
                       struct perf_evlist *evlist __maybe_unused)
 {
 #ifndef CPUINFO_PROC
@@ -325,7 +330,7 @@ static int write_cpudesc(int fd, struct perf_header *h 
__maybe_unused,
 }
 
 
-static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
+static int write_nrcpus(struct feat_fd *fd,
                        struct perf_evlist *evlist __maybe_unused)
 {
        long nr;
@@ -347,7 +352,7 @@ static int write_nrcpus(int fd, struct perf_header *h 
__maybe_unused,
        return do_write(fd, &nra, sizeof(nra));
 }
 
-static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
+static int write_event_desc(struct feat_fd *fd,
                            struct perf_evlist *evlist)
 {
        struct perf_evsel *evsel;
@@ -403,7 +408,7 @@ static int write_event_desc(int fd, struct perf_header *h 
__maybe_unused,
        return 0;
 }
 
-static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
+static int write_cmdline(struct feat_fd *fd,
                         struct perf_evlist *evlist __maybe_unused)
 {
        char buf[MAXPATHLEN];
@@ -583,8 +588,8 @@ static struct cpu_topo *build_cpu_topology(void)
        return tp;
 }
 
-static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
-                         struct perf_evlist *evlist __maybe_unused)
+static int write_cpu_topology(struct feat_fd *fd,
+                             struct perf_evlist *evlist __maybe_unused)
 {
        struct cpu_topo *tp;
        u32 i;
@@ -634,8 +639,8 @@ static int write_cpu_topology(int fd, struct perf_header *h 
__maybe_unused,
 
 
 
-static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
-                         struct perf_evlist *evlist __maybe_unused)
+static int write_total_mem(struct feat_fd *fd,
+                          struct perf_evlist *evlist __maybe_unused)
 {
        char *buf = NULL;
        FILE *fp;
@@ -663,7 +668,7 @@ static int write_total_mem(int fd, struct perf_header *h 
__maybe_unused,
        return ret;
 }
 
-static int write_topo_node(int fd, int node)
+static int write_topo_node(struct feat_fd *fd, int node)
 {
        char str[MAXPATHLEN];
        char field[32];
@@ -723,8 +728,8 @@ static int write_topo_node(int fd, int node)
        return ret;
 }
 
-static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
-                         struct perf_evlist *evlist __maybe_unused)
+static int write_numa_topology(struct feat_fd *fd,
+                              struct perf_evlist *evlist __maybe_unused)
 {
        char *buf = NULL;
        size_t len = 0;
@@ -784,11 +789,11 @@ static int write_numa_topology(int fd, struct perf_header 
*h __maybe_unused,
  * };
  */
 
-static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
+static int write_pmu_mappings(struct feat_fd *fd,
                              struct perf_evlist *evlist __maybe_unused)
 {
        struct perf_pmu *pmu = NULL;
-       off_t offset = lseek(fd, 0, SEEK_CUR);
+       off_t offset = lseek(fd->fd, 0, SEEK_CUR);
        __u32 pmu_num = 0;
        int ret;
 
@@ -811,9 +816,9 @@ static int write_pmu_mappings(int fd, struct perf_header *h 
__maybe_unused,
                        return ret;
        }
 
-       if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
+       if (pwrite(fd->fd, &pmu_num, sizeof(pmu_num), offset) != 
sizeof(pmu_num)) {
                /* discard all */
-               lseek(fd, offset, SEEK_SET);
+               lseek(fd->fd, offset, SEEK_SET);
                return -1;
        }
 
@@ -832,7 +837,7 @@ static int write_pmu_mappings(int fd, struct perf_header *h 
__maybe_unused,
  *     }[nr_groups];
  * };
  */
-static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
+static int write_group_desc(struct feat_fd *fd,
                            struct perf_evlist *evlist)
 {
        u32 nr_groups = evlist->nr_groups;
@@ -875,7 +880,7 @@ int __weak get_cpuid(char *buffer __maybe_unused, size_t sz 
__maybe_unused)
        return -1;
 }
 
-static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
+static int write_cpuid(struct feat_fd *fd,
                       struct perf_evlist *evlist __maybe_unused)
 {
        char buffer[64];
@@ -890,22 +895,21 @@ static int write_cpuid(int fd, struct perf_header *h 
__maybe_unused,
        return do_write_string(fd, buffer);
 }
 
-static int write_branch_stack(int fd __maybe_unused,
-                             struct perf_header *h __maybe_unused,
-                      struct perf_evlist *evlist __maybe_unused)
+static int write_branch_stack(struct feat_fd *fd __maybe_unused,
+                             struct perf_evlist *evlist __maybe_unused)
 {
        return 0;
 }
 
-static int write_auxtrace(int fd, struct perf_header *h,
+static int write_auxtrace(struct feat_fd *fd,
                          struct perf_evlist *evlist __maybe_unused)
 {
        struct perf_session *session;
        int err;
 
-       session = container_of(h, struct perf_session, header);
+       session = container_of(fd->ph, struct perf_session, header);
 
-       err = auxtrace_index__write(fd, &session->auxtrace_index);
+       err = auxtrace_index__write(fd->fd, &session->auxtrace_index);
        if (err < 0)
                pr_err("Failed to write auxtrace index\n");
        return err;
@@ -1052,8 +1056,8 @@ static int build_caches(struct cpu_cache_level caches[], 
u32 size, u32 *cntp)
 
 #define MAX_CACHES 2000
 
-static int write_cache(int fd, struct perf_header *h __maybe_unused,
-                         struct perf_evlist *evlist __maybe_unused)
+static int write_cache(struct feat_fd *fd,
+                      struct perf_evlist *evlist __maybe_unused)
 {
        struct cpu_cache_level caches[MAX_CACHES];
        u32 cnt = 0, i, version = 1;
@@ -1104,8 +1108,7 @@ static int write_cache(int fd, struct perf_header *h 
__maybe_unused,
        return ret;
 }
 
-static int write_stat(int fd __maybe_unused,
-                     struct perf_header *h __maybe_unused,
+static int write_stat(struct feat_fd *fd __maybe_unused,
                      struct perf_evlist *evlist __maybe_unused)
 {
        return 0;
@@ -2091,7 +2094,7 @@ static int process_cache(struct perf_file_section 
*section __maybe_unused,
 }
 
 struct feature_ops {
-       int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
+       int (*write)(struct feat_fd *fd, struct perf_evlist *evlist);
        void (*print)(struct perf_header *h, int fd, FILE *fp);
        int (*process)(struct perf_file_section *section,
                       struct perf_header *h, int fd, void *data);
@@ -2200,7 +2203,7 @@ int perf_header__fprintf_info(struct perf_session 
*session, FILE *fp, bool full)
        return 0;
 }
 
-static int do_write_feat(int fd, struct perf_header *h, int type,
+static int do_write_feat(struct feat_fd *fd, struct perf_header *h, int type,
                         struct perf_file_section **p,
                         struct perf_evlist *evlist)
 {
@@ -2211,18 +2214,18 @@ static int do_write_feat(int fd, struct perf_header *h, 
int type,
                if (!feat_ops[type].write)
                        return -1;
 
-               (*p)->offset = lseek(fd, 0, SEEK_CUR);
+               (*p)->offset = lseek(fd->fd, 0, SEEK_CUR);
 
-               err = feat_ops[type].write(fd, h, evlist);
+               err = feat_ops[type].write(fd, evlist);
                if (err < 0) {
                        pr_debug("failed to write feature %s\n", 
feat_ops[type].name);
 
                        /* undo anything written */
-                       lseek(fd, (*p)->offset, SEEK_SET);
+                       lseek(fd->fd, (*p)->offset, SEEK_SET);
 
                        return -1;
                }
-               (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
+               (*p)->size = lseek(fd->fd, 0, SEEK_CUR) - (*p)->offset;
                (*p)++;
        }
        return ret;
@@ -2232,12 +2235,18 @@ static int perf_header__adds_write(struct perf_header 
*header,
                                   struct perf_evlist *evlist, int fd)
 {
        int nr_sections;
+       struct feat_fd fdd;
        struct perf_file_section *feat_sec, *p;
        int sec_size;
        u64 sec_start;
        int feat;
        int err;
 
+       fdd = (struct feat_fd){
+               .fd  = fd,
+               .ph = header,
+       };
+
        nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
        if (!nr_sections)
                return 0;
@@ -2252,7 +2261,7 @@ static int perf_header__adds_write(struct perf_header 
*header,
        lseek(fd, sec_start + sec_size, SEEK_SET);
 
        for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
-               if (do_write_feat(fd, header, feat, &p, evlist))
+               if (do_write_feat(&fdd, header, feat, &p, evlist))
                        perf_header__clear_feat(header, feat);
        }
 
@@ -2261,7 +2270,7 @@ static int perf_header__adds_write(struct perf_header 
*header,
         * may write more than needed due to dropped feature, but
         * this is okay, reader will skip the mising entries
         */
-       err = do_write(fd, feat_sec, sec_size);
+       err = do_write(&fdd, feat_sec, sec_size);
        if (err < 0)
                pr_debug("failed to write feature section\n");
        free(feat_sec);
@@ -2271,14 +2280,17 @@ static int perf_header__adds_write(struct perf_header 
*header,
 int perf_header__write_pipe(int fd)
 {
        struct perf_pipe_file_header f_header;
+       struct feat_fd fdd;
        int err;
 
+       fdd = (struct feat_fd){ .fd = fd };
+
        f_header = (struct perf_pipe_file_header){
                .magic     = PERF_MAGIC,
                .size      = sizeof(f_header),
        };
 
-       err = do_write(fd, &f_header, sizeof(f_header));
+       err = do_write(&fdd, &f_header, sizeof(f_header));
        if (err < 0) {
                pr_debug("failed to write perf pipe header\n");
                return err;
@@ -2295,21 +2307,23 @@ int perf_session__write_header(struct perf_session 
*session,
        struct perf_file_attr   f_attr;
        struct perf_header *header = &session->header;
        struct perf_evsel *evsel;
+       struct feat_fd fdd;
        u64 attr_offset;
        int err;
 
+       fdd = (struct feat_fd){ .fd = fd};
        lseek(fd, sizeof(f_header), SEEK_SET);
 
        evlist__for_each_entry(session->evlist, evsel) {
                evsel->id_offset = lseek(fd, 0, SEEK_CUR);
-               err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
+               err = do_write(&fdd, evsel->id, evsel->ids * sizeof(u64));
                if (err < 0) {
                        pr_debug("failed to write perf header\n");
                        return err;
                }
        }
 
-       attr_offset = lseek(fd, 0, SEEK_CUR);
+       attr_offset = lseek(fdd.fd, 0, SEEK_CUR);
 
        evlist__for_each_entry(evlist, evsel) {
                f_attr = (struct perf_file_attr){
@@ -2319,7 +2333,7 @@ int perf_session__write_header(struct perf_session 
*session,
                                .size   = evsel->ids * sizeof(u64),
                        }
                };
-               err = do_write(fd, &f_attr, sizeof(f_attr));
+               err = do_write(&fdd, &f_attr, sizeof(f_attr));
                if (err < 0) {
                        pr_debug("failed to write perf header attribute\n");
                        return err;
@@ -2354,7 +2368,7 @@ int perf_session__write_header(struct perf_session 
*session,
        memcpy(&f_header.adds_features, &header->adds_features, 
sizeof(header->adds_features));
 
        lseek(fd, 0, SEEK_SET);
-       err = do_write(fd, &f_header, sizeof(f_header));
+       err = do_write(&fdd, &f_header, sizeof(f_header));
        if (err < 0) {
                pr_debug("failed to write perf header\n");
                return err;
@@ -2629,6 +2643,10 @@ static int perf_file_header__read_pipe(struct 
perf_pipe_file_header *header,
                                       struct perf_header *ph, int fd,
                                       bool repipe)
 {
+       struct feat_fd fdd = {
+               .fd = STDOUT_FILENO,
+               .ph = ph,
+       };
        ssize_t ret;
 
        ret = readn(fd, header, sizeof(*header));
@@ -2643,7 +2661,7 @@ static int perf_file_header__read_pipe(struct 
perf_pipe_file_header *header,
        if (ph->needs_swap)
                header->size = bswap_64(header->size);
 
-       if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
+       if (repipe && do_write(&fdd, header, sizeof(*header)) < 0)
                return -1;
 
        return 0;
@@ -3151,6 +3169,7 @@ int perf_event__synthesize_tracing_data(struct perf_tool 
*tool, int fd,
        union perf_event ev;
        struct tracing_data *tdata;
        ssize_t size = 0, aligned_size = 0, padding;
+       struct feat_fd fdd;
        int err __maybe_unused = 0;
 
        /*
@@ -3185,7 +3204,8 @@ int perf_event__synthesize_tracing_data(struct perf_tool 
*tool, int fd,
         */
        tracing_data_put(tdata);
 
-       if (write_padded(fd, NULL, 0, padding))
+       fdd = (struct feat_fd){ .fd = fd };
+       if (write_padded(&fdd, NULL, 0, padding))
                return -1;
 
        return aligned_size;
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
index e98489c8bba7..9d8dcd5eb727 100644
--- a/tools/perf/util/header.h
+++ b/tools/perf/util/header.h
@@ -144,9 +144,12 @@ bool is_perf_magic(u64 magic);
 
 #define NAME_ALIGN 64
 
-int do_write(int fd, const void *buf, size_t size);
+struct feat_fd;
 
-int write_padded(int fd, const void *bf, size_t count, size_t count_aligned);
+int do_write(struct feat_fd *fd, const void *buf, size_t size);
+
+int write_padded(struct feat_fd *fd, const void *bf,
+                size_t count, size_t count_aligned);
 
 /*
  * arch specific callback
-- 
2.13.0.219.gdb65acc882-goog

Reply via email to