As preparation for using header records in pipe mode, replace int fd with struct feat_fd fd in write functions for all header record types.
Record types that are not used in pipe-mode (such as auxtrace) will never be called with fd->buf set and fail if so. Signed-off-by: David Carrillo-Cisneros <davi...@google.com> --- tools/perf/util/build-id.c | 10 ++- tools/perf/util/build-id.h | 4 +- tools/perf/util/header.c | 194 +++++++++++++++++++++++++++++---------------- tools/perf/util/header.h | 7 +- 4 files changed, 140 insertions(+), 75 deletions(-) diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index 168cc49654e7..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; @@ -345,14 +345,15 @@ static int write_buildid(const char *name, size_t name_len, u8 *build_id, b.header.misc = misc; b.header.size = sizeof(b) + len; - err = writen(fd, &b, sizeof(b)); + err = do_write(fd, &b, sizeof(b)); if (err < 0) return err; 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 2415d41282d8..5ca603b7a7a3 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -57,6 +57,14 @@ struct perf_file_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; +}; + void perf_header__set_feat(struct perf_header *header, int feat) { set_bit(feat, header->adds_features); @@ -73,23 +81,36 @@ bool perf_header__has_feat(const struct perf_header *header, int feat) } /* Return: 0 if succeded, -ERR if failed. */ -static int do_write(int fd, const void *buf, size_t size) +int do_write(struct feat_fd *fd, const void *buf, size_t size) { - while (size) { - int ret = write(fd, buf, size); + void *addr; - if (ret < 0) - return -errno; + if (!fd->buf) { + ssize_t ret = writen(fd->fd, buf, size); - size -= ret; - buf += ret; + if (ret != (ssize_t)size) + return ret < 0 ? (int)ret : -1; + return 0; } +retry: + if (size > (fd->size - fd->offset)) { + addr = realloc(fd->buf, fd->size << 1); + if (!addr) + return -ENOSPC; + fd->buf = addr; + fd->size <<= 1; + goto retry; + } + + memcpy(fd->buf + fd->offset, buf, size); + fd->offset += size; return 0; } /* 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); @@ -104,7 +125,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; @@ -151,24 +172,31 @@ 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); + if (fd->buf) { + pr_err("Unsupported write_tracing_data to memory buffer.\n"); + return -1; + } + 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; + if (fd->buf) { + pr_err("Unsupported write_build_id to memory buffer.\n"); + return -1; + } err = perf_session__write_buildid_table(session, fd); if (err < 0) { pr_debug("failed to write buildid table\n"); @@ -179,7 +207,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; @@ -192,7 +220,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; @@ -205,7 +233,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; @@ -218,13 +246,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; @@ -281,7 +309,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 @@ -300,7 +328,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; @@ -322,7 +350,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; @@ -378,7 +406,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]; @@ -558,8 +586,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; @@ -609,8 +637,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; @@ -638,7 +666,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]; @@ -698,8 +726,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; @@ -759,15 +787,23 @@ 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); - __u32 pmu_num = 0; + u32 pmu_num = 0; int ret; - /* write real pmu_num later */ + /* + * 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(fd, &pmu_num, sizeof(pmu_num)); if (ret < 0) return ret; @@ -775,7 +811,6 @@ static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused, while ((pmu = perf_pmu__scan(pmu))) { if (!pmu->name) continue; - pmu_num++; ret = do_write(fd, &pmu->type, sizeof(pmu->type)); if (ret < 0) @@ -786,12 +821,6 @@ 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)) { - /* discard all */ - lseek(fd, offset, SEEK_SET); - return -1; - } - return 0; } @@ -807,7 +836,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; @@ -850,7 +879,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]; @@ -865,22 +894,25 @@ 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); + if (fd->buf) { + pr_err("Unsupported write_auxtrace to memory buffer.\n"); + return -1; + } + 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; @@ -1027,8 +1059,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; @@ -1079,8 +1111,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; @@ -2186,7 +2217,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); @@ -2295,7 +2326,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) { @@ -2306,18 +2337,22 @@ 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); + if (fd->buf) { + pr_err("do_write_feat to memory buffer\n"); + return -1; + } + (*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; @@ -2327,12 +2362,22 @@ 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; + /* + * may write more than needed due to dropped feature, but + * this is okay, reader will skip the mising entries + */ + fdd = (struct feat_fd){ + .fd = fd, + .ph = header, + }; + nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); if (!nr_sections) return 0; @@ -2347,7 +2392,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); } @@ -2356,7 +2401,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); @@ -2366,14 +2411,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; @@ -2390,21 +2438,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){ @@ -2414,7 +2464,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; @@ -2449,7 +2499,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; @@ -2724,6 +2774,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)); @@ -2738,7 +2792,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; @@ -3246,6 +3300,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; /* @@ -3280,7 +3335,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 d30109b421ee..9d8dcd5eb727 100644 --- a/tools/perf/util/header.h +++ b/tools/perf/util/header.h @@ -144,7 +144,12 @@ bool is_perf_magic(u64 magic); #define NAME_ALIGN 64 -int write_padded(int fd, const void *bf, size_t count, size_t count_aligned); +struct feat_fd; + +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.303.g4ebf302169-goog