Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1647757 > unrolled thread
| Started by | David Carrillo-Cisneros <davidcc@google.com> |
|---|---|
| First post | 2017-05-23 10:00 +0200 |
| Last post | 2017-05-23 10:00 +0200 |
| Articles | 9 — 2 participants |
Back to article view | Back to linux.kernel
[PATCH v2 00/13] perf tool: add meta-data header support for pipe-mode David Carrillo-Cisneros <davidcc@google.com> - 2017-05-23 10:00 +0200
[PATCH v2 09/13] perf header: use struct feat_fd in read header records David Carrillo-Cisneros <davidcc@google.com> - 2017-05-23 10:00 +0200
[PATCH v2 12/13] perf tool: add show_feature_header to perf_tool David Carrillo-Cisneros <davidcc@google.com> - 2017-05-23 10:00 +0200
[PATCH v2 06/13] perf header: add struct feat_fd for write David Carrillo-Cisneros <davidcc@google.com> - 2017-05-23 10:00 +0200
[PATCH v2 03/13] perf header: fail on write_padded error David Carrillo-Cisneros <davidcc@google.com> - 2017-05-23 10:00 +0200
[PATCH v2 11/13] perf header: change FEAT_OP* macros David Carrillo-Cisneros <davidcc@google.com> - 2017-05-23 10:00 +0200
[PATCH v2 01/13] perf header: encapsulate read and swap David Carrillo-Cisneros <davidcc@google.com> - 2017-05-23 10:00 +0200
Re: [PATCH v2 01/13] perf header: encapsulate read and swap Namhyung Kim <namhyung@kernel.org> - 2017-05-24 17:40 +0200
[PATCH v2 05/13] perf header: revamp do_write David Carrillo-Cisneros <davidcc@google.com> - 2017-05-23 10:00 +0200
| From | David Carrillo-Cisneros <davidcc@google.com> |
|---|---|
| Date | 2017-05-23 10:00 +0200 |
| Subject | [PATCH v2 00/13] perf tool: add meta-data header support for pipe-mode |
| Message-ID | <tKcTv-a4-3@gated-at.bofh.it> |
v2: - Finer patch splitting.
- Add only one record type with a feature id instead of one
record per new feature (as suggested by Jiri).
- Add perf.data documentation.
(This is a rebased and updated version of Stephane Eranian's version
in https://patchwork.kernel.org/patch/1499081/)
Up until now, meta-data was only available when perf record
was used in "regular" mode, i.e., generating a perf.data file.
For users depending on pipe mode, neither host or event header
information were gathered. This patch addresses this limitation.
The difficulty in pipe mode is that information needs to be written
sequentially to the pipe. Meta data headers are usually generated
(and also expected) at the beginning of the file (or piped output).
To solve this problem, we introduce new synthetic record types,
one for each meta-data type. The approach is similar to what
is *ALREADY* used for BUILD_ID and TRACING_DATA.
We have modified util/header.c such that the same routines are used
to generate and read the meta-data information regardless of pipe-mode
vs. regular mode. To make this work, we added a new struct called
feat_fd which encapsulates all the information necessary to read or
write meta-data information to a file/pipe or from a file/pipe.
With this series, it is possible to get:
$ perf record -o - -e cycles sleep 1 | perf report --stdio --header
# ========
# captured on: Mon May 22 16:33:43 2017
# ========
#
# hostname : my_hostname
# os release : 4.11.0-dbx-up_perf
# perf version : 4.11.rc6.g6277c80
# arch : x86_64
# nrcpus online : 72
# nrcpus avail : 72
# cpudesc : Intel(R) Xeon(R) CPU E5-2696 v3 @ 2.30GHz
# cpuid : GenuineIntel,6,63,2
# total memory : 263457192 kB
# cmdline : /root/perf record -o - -e cycles -c 100000 sleep 1
# HEADER_CPU_TOPOLOGY info available, use -I to display
# HEADER_NUMA_TOPOLOGY info available, use -I to display
# pmu mappings: intel_bts = 6, uncore_imc_4 = 22, uncore_sbox_1 = 47, uncore_cbox_5 = 33, uncore_ha_0 = 16, uncore_cbox
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.000 MB - ]
...
Only patch 13/13 has a significant effect in perf's behavior. All other
are transparent preparatory changes or bug fixes.
David Carrillo-Cisneros (13):
perf header: encapsulate read and swap
perf header: add PROCESS_STR_FUN macro
perf header: fail on write_padded error
perf util: add const modifier to buf in "writen" function
perf header: revamp do_write
perf header: add struct feat_fd for write
perf header: use struct feat_fd for print
perf header: use struct feat_fd to process header records
perf header: use struct feat_fd in read header records
perf header: add a buffer to struct feat_fd
perf header: change FEAT_OP* macros
perf tool: add show_feature_header to perf_tool
perf tools: add feature header record to pipe-mode
tools/perf/Documentation/perf.data-file-format.txt | 10 +-
tools/perf/builtin-annotate.c | 1 +
tools/perf/builtin-inject.c | 1 +
tools/perf/builtin-record.c | 7 +
tools/perf/builtin-report.c | 12 +-
tools/perf/builtin-script.c | 4 +
tools/perf/util/build-id.c | 10 +-
tools/perf/util/build-id.h | 4 +-
tools/perf/util/event.c | 1 +
tools/perf/util/event.h | 8 +
tools/perf/util/header.c | 877 +++++++++++----------
tools/perf/util/header.h | 16 +-
tools/perf/util/session.c | 12 +
tools/perf/util/tool.h | 10 +-
tools/perf/util/util.c | 6 +-
tools/perf/util/util.h | 2 +-
16 files changed, 564 insertions(+), 417 deletions(-)
--
2.13.0.219.gdb65acc882-goog
[toc] | [next] | [standalone]
| From | David Carrillo-Cisneros <davidcc@google.com> |
|---|---|
| Date | 2017-05-23 10:00 +0200 |
| Subject | [PATCH v2 09/13] perf header: use struct feat_fd in read header records |
| Message-ID | <tKcTw-a4-29@gated-at.bofh.it> |
| In reply to | #1647757 |
As preparation for using header records in-pipe mode, replace
int fd with struct feat_fd in read functions for all header
record types.
This patch does not change behavior.
Signed-off-by: David Carrillo-Cisneros <davidcc@google.com>
---
tools/perf/util/header.c | 93 ++++++++++++++++++++++++------------------------
1 file changed, 46 insertions(+), 47 deletions(-)
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index dc923f5639f8..65cd2d1f1721 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -124,16 +124,16 @@ static int do_write_string(struct feat_fd *fd, const char *str)
return write_padded(fd, str, olen, len);
}
-static int __do_read(int fd, void *addr, ssize_t size)
+static int __do_read(struct feat_fd *fd, void *addr, ssize_t size)
{
- ssize_t ret = readn(fd, addr, size);
+ ssize_t ret = readn(fd->fd, addr, size);
if (ret != (ssize_t)size)
return ret < 0 ? (int)ret : -1;
return 0;
}
-static int do_read_u32(int fd, struct perf_header *ph, u32 *addr)
+static int do_read_u32(struct feat_fd *fd, u32 *addr)
{
int ret;
@@ -141,12 +141,12 @@ static int do_read_u32(int fd, struct perf_header *ph, u32 *addr)
if (ret)
return ret;
- if (ph->needs_swap)
+ if (fd->ph->needs_swap)
*addr = bswap_32(*addr);
return 0;
}
-static int do_read_u64(int fd, struct perf_header *ph, u64 *addr)
+static int do_read_u64(struct feat_fd *fd, u64 *addr)
{
int ret;
@@ -154,17 +154,17 @@ static int do_read_u64(int fd, struct perf_header *ph, u64 *addr)
if (ret)
return ret;
- if (ph->needs_swap)
+ if (fd->ph->needs_swap)
*addr = bswap_64(*addr);
return 0;
}
-static char *do_read_string(int fd, struct perf_header *ph)
+static char *do_read_string(struct feat_fd *fd)
{
u32 len;
char *buf;
- if (do_read_u32(fd, ph, &len))
+ if (do_read_u32(fd, &len))
return NULL;
buf = malloc(len);
@@ -1206,8 +1206,7 @@ static void free_event_desc(struct perf_evsel *events)
free(events);
}
-static struct perf_evsel *
-read_event_desc(struct perf_header *ph, int fd)
+static struct perf_evsel *read_event_desc(struct feat_fd *fd)
{
struct perf_evsel *evsel, *events = NULL;
u64 *id;
@@ -1217,10 +1216,10 @@ read_event_desc(struct perf_header *ph, int fd)
size_t msz;
/* number of events */
- if (do_read_u32(fd, ph, &nre))
+ if (do_read_u32(fd, &nre))
goto error;
- if (do_read_u32(fd, ph, &sz))
+ if (do_read_u32(fd, &sz))
goto error;
/* buffer to hold on file attr struct */
@@ -1248,18 +1247,18 @@ read_event_desc(struct perf_header *ph, int fd)
if (ret != (ssize_t)sz)
goto error;
- if (ph->needs_swap)
+ if (fd->ph->needs_swap)
perf_event__attr_swap(buf);
memcpy(&evsel->attr, buf, msz);
- if (do_read_u32(fd, ph, &nr))
+ if (do_read_u32(fd, &nr))
goto error;
- if (ph->needs_swap)
+ if (fd->ph->needs_swap)
evsel->needs_swap = true;
- evsel->name = do_read_string(fd, ph);
+ evsel->name = do_read_string(fd);
if (!nr)
continue;
@@ -1271,7 +1270,7 @@ read_event_desc(struct perf_header *ph, int fd)
evsel->id = id;
for (j = 0 ; j < nr; j++) {
- if (do_read_u64(fd, ph, id))
+ if (do_read_u64(fd, id))
goto error;
id++;
}
@@ -1293,7 +1292,7 @@ static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
static void print_event_desc(struct feat_fd *fd, FILE *fp)
{
- struct perf_evsel *evsel, *events = read_event_desc(fd->ph, fd->fd);
+ struct perf_evsel *evsel, *events = read_event_desc(fd);
u32 j;
u64 *id;
@@ -1587,7 +1586,7 @@ static int perf_header__read_build_ids(struct perf_header *header,
#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
static int process_##__feat(struct feat_fd *fd, void *data __maybe_unused) \
{\
- fd->ph->env.__feat_env = do_read_string(fd->fd, fd->ph); \
+ fd->ph->env.__feat_env = do_read_string(fd); \
return fd->ph->env.__feat_env ? 0 : -ENOMEM; \
}
@@ -1617,11 +1616,11 @@ static int process_nrcpus(struct feat_fd *fd, void *data __maybe_unused)
int ret;
u32 nr_cpus_avail, nr_cpus_online;
- ret = do_read_u32(fd->fd, fd->ph, &nr_cpus_avail);
+ ret = do_read_u32(fd, &nr_cpus_avail);
if (ret)
return ret;
- ret = do_read_u32(fd->fd, fd->ph, &nr_cpus_online);
+ ret = do_read_u32(fd, &nr_cpus_online);
if (ret)
return ret;
fd->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
@@ -1634,7 +1633,7 @@ static int process_total_mem(struct feat_fd *fd, void *data __maybe_unused)
u64 total_mem;
int ret;
- ret = do_read_u64(fd->fd, fd->ph, &total_mem);
+ ret = do_read_u64(fd, &total_mem);
if (ret)
return -1;
fd->ph->env.total_mem = (unsigned long long)total_mem;
@@ -1677,7 +1676,7 @@ static int
process_event_desc(struct feat_fd *fd, void *data __maybe_unused)
{
struct perf_session *session;
- struct perf_evsel *evsel, *events = read_event_desc(fd->ph, fd->fd);
+ struct perf_evsel *evsel, *events = read_event_desc(fd);
if (!events)
return 0;
@@ -1696,7 +1695,7 @@ static int process_cmdline(struct feat_fd *fd, void *data __maybe_unused)
char *str, *cmdline = NULL, **argv = NULL;
u32 nr, i, len = 0;
- if (do_read_u32(fd->fd, fd->ph, &nr))
+ if (do_read_u32(fd, &nr))
return -1;
fd->ph->env.nr_cmdline = nr;
@@ -1710,7 +1709,7 @@ static int process_cmdline(struct feat_fd *fd, void *data __maybe_unused)
goto error;
for (i = 0; i < nr; i++) {
- str = do_read_string(fd->fd, fd->ph);
+ str = do_read_string(fd);
if (!str)
goto error;
@@ -1742,7 +1741,7 @@ static int process_cpu_topology(struct feat_fd *fd, void *data __maybe_unused)
if (!ph->env.cpu)
return -1;
- if (do_read_u32(fd->fd, fd->ph, &nr))
+ if (do_read_u32(fd, &nr))
goto free_cpu;
ph->env.nr_sibling_cores = nr;
@@ -1750,7 +1749,7 @@ static int process_cpu_topology(struct feat_fd *fd, void *data __maybe_unused)
goto free_cpu;
for (i = 0; i < nr; i++) {
- str = do_read_string(fd->fd, fd->ph);
+ str = do_read_string(fd);
if (!str)
goto error;
@@ -1761,13 +1760,13 @@ static int process_cpu_topology(struct feat_fd *fd, void *data __maybe_unused)
}
ph->env.sibling_cores = strbuf_detach(&sb, NULL);
- if (do_read_u32(fd->fd, fd->ph, &nr))
+ if (do_read_u32(fd, &nr))
return -1;
ph->env.nr_sibling_threads = nr;
for (i = 0; i < nr; i++) {
- str = do_read_string(fd->fd, fd->ph);
+ str = do_read_string(fd);
if (!str)
goto error;
@@ -1788,12 +1787,12 @@ static int process_cpu_topology(struct feat_fd *fd, void *data __maybe_unused)
}
for (i = 0; i < (u32)cpu_nr; i++) {
- if (do_read_u32(fd->fd, fd->ph, &nr))
+ if (do_read_u32(fd, &nr))
goto free_cpu;
ph->env.cpu[i].core_id = nr;
- if (do_read_u32(fd->fd, fd->ph, &nr))
+ if (do_read_u32(fd, &nr))
goto free_cpu;
if (nr != (u32)-1 && nr > (u32)cpu_nr) {
@@ -1822,7 +1821,7 @@ static int process_numa_topology(struct feat_fd *fd, void *data __maybe_unused)
char *str;
/* nr nodes */
- if (do_read_u32(fd->fd, fd->ph, &nr))
+ if (do_read_u32(fd, &nr))
return -1;
nodes = zalloc(sizeof(*nodes) * nr);
@@ -1833,16 +1832,16 @@ static int process_numa_topology(struct feat_fd *fd, void *data __maybe_unused)
n = &nodes[i];
/* node number */
- if (do_read_u32(fd->fd, fd->ph, &n->node))
+ if (do_read_u32(fd, &n->node))
goto error;
- if (do_read_u64(fd->fd, fd->ph, &n->mem_total))
+ if (do_read_u64(fd, &n->mem_total))
goto error;
- if (do_read_u64(fd->fd, fd->ph, &n->mem_free))
+ if (do_read_u64(fd, &n->mem_free))
goto error;
- str = do_read_string(fd->fd, fd->ph);
+ str = do_read_string(fd);
if (!str)
goto error;
@@ -1869,7 +1868,7 @@ static int process_pmu_mappings(struct feat_fd *fd, void *data __maybe_unused)
u32 type;
struct strbuf sb;
- if (do_read_u32(fd->fd, fd->ph, &pmu_num))
+ if (do_read_u32(fd, &pmu_num))
return -1;
if (!pmu_num) {
@@ -1882,10 +1881,10 @@ static int process_pmu_mappings(struct feat_fd *fd, void *data __maybe_unused)
return -1;
while (pmu_num) {
- if (do_read_u32(fd->fd, fd->ph, &type))
+ if (do_read_u32(fd, &type))
goto error;
- name = do_read_string(fd->fd, fd->ph);
+ name = do_read_string(fd);
if (!name)
goto error;
@@ -1922,7 +1921,7 @@ static int process_group_desc(struct feat_fd *fd, void *data __maybe_unused)
u32 nr_members;
} *desc;
- if (do_read_u32(fd->fd, fd->ph, &nr_groups))
+ if (do_read_u32(fd, &nr_groups))
return -1;
ph->env.nr_groups = nr_groups;
@@ -1936,14 +1935,14 @@ static int process_group_desc(struct feat_fd *fd, void *data __maybe_unused)
return -1;
for (i = 0; i < nr_groups; i++) {
- desc[i].name = do_read_string(fd->fd, fd->ph);
+ desc[i].name = do_read_string(fd);
if (!desc[i].name)
goto out_free;
- if (do_read_u32(fd->fd, fd->ph, &desc[i].leader_idx))
+ if (do_read_u32(fd, &desc[i].leader_idx))
goto out_free;
- if (do_read_u32(fd->fd, fd->ph, &desc[i].nr_members))
+ if (do_read_u32(fd, &desc[i].nr_members))
goto out_free;
}
@@ -2013,13 +2012,13 @@ static int process_cache(struct feat_fd *fd, void *data __maybe_unused)
struct cpu_cache_level *caches;
u32 cnt, i, version;
- if (do_read_u32(fd->fd, fd->ph, &version))
+ if (do_read_u32(fd, &version))
return -1;
if (version != 1)
return -1;
- if (do_read_u32(fd->fd, fd->ph, &cnt))
+ if (do_read_u32(fd, &cnt))
return -1;
caches = zalloc(sizeof(*caches) * cnt);
@@ -2030,7 +2029,7 @@ static int process_cache(struct feat_fd *fd, void *data __maybe_unused)
struct cpu_cache_level c;
#define _R(v) \
- if (do_read_u32(fd->fd, fd->ph, &c.v))\
+ if (do_read_u32(fd, &c.v))\
goto out_free_caches; \
_R(level)
@@ -2040,7 +2039,7 @@ static int process_cache(struct feat_fd *fd, void *data __maybe_unused)
#undef _R
#define _R(v) \
- c.v = do_read_string(fd->fd, fd->ph); \
+ c.v = do_read_string(fd); \
if (!c.v) \
goto out_free_caches;
--
2.13.0.219.gdb65acc882-goog
[toc] | [prev] | [next] | [standalone]
| From | David Carrillo-Cisneros <davidcc@google.com> |
|---|---|
| Date | 2017-05-23 10:00 +0200 |
| Subject | [PATCH v2 12/13] perf tool: add show_feature_header to perf_tool |
| Message-ID | <tKcTx-a4-31@gated-at.bofh.it> |
| In reply to | #1647757 |
Add show_feat_hdr to control level of printed information
of feature headers.
Signed-off-by: David Carrillo-Cisneros <davidcc@google.com>
---
tools/perf/builtin-report.c | 11 ++++++++---
tools/perf/builtin-script.c | 3 +++
tools/perf/util/tool.h | 7 +++++++
3 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 22478ff2b706..7620d708c78b 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -985,9 +985,14 @@ int cmd_report(int argc, const char **argv)
perf_hpp_list.need_collapse = true;
}
- /* Force tty output for header output and per-thread stat. */
- if (report.header || report.header_only || report.show_threads)
- use_browser = 0;
+ if (report.header || report.header_only) {
+ report.tool.show_feat_hdr = SHOW_FEAT_HEADER;
+ /* Force tty output for header output and per-thread stat. */
+ if (report.show_threads)
+ use_browser = 0;
+ }
+ if (report.show_full_info)
+ report.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
if (strcmp(input_name, "-") != 0)
setup_browser(true);
diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
index d05aec491cff..0934d0885614 100644
--- a/tools/perf/builtin-script.c
+++ b/tools/perf/builtin-script.c
@@ -2684,10 +2684,13 @@ int cmd_script(int argc, const char **argv)
return -1;
if (header || header_only) {
+ script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
perf_session__fprintf_info(session, stdout, show_full_info);
if (header_only)
goto out_delete;
}
+ if (show_full_info)
+ script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
if (symbol__init(&session->header.env) < 0)
goto out_delete;
diff --git a/tools/perf/util/tool.h b/tools/perf/util/tool.h
index 829471a1c6d7..baeca808dfda 100644
--- a/tools/perf/util/tool.h
+++ b/tools/perf/util/tool.h
@@ -34,6 +34,12 @@ typedef int (*event_oe)(struct perf_tool *tool, union perf_event *event,
typedef s64 (*event_op3)(struct perf_tool *tool, union perf_event *event,
struct perf_session *session);
+enum show_feature_header {
+ SHOW_FEAT_NO_HEADER = 0,
+ SHOW_FEAT_HEADER,
+ SHOW_FEAT_HEADER_FULL_INFO,
+};
+
struct perf_tool {
event_sample sample,
read;
@@ -68,6 +74,7 @@ struct perf_tool {
bool ordered_events;
bool ordering_requires_timestamps;
bool namespace_events;
+ enum show_feature_header show_feat_hdr;
};
#endif /* __PERF_TOOL_H */
--
2.13.0.219.gdb65acc882-goog
[toc] | [prev] | [next] | [standalone]
| From | David Carrillo-Cisneros <davidcc@google.com> |
|---|---|
| Date | 2017-05-23 10:00 +0200 |
| Subject | [PATCH v2 06/13] perf header: add struct feat_fd for write |
| Message-ID | <tKcTx-a4-33@gated-at.bofh.it> |
| In reply to | #1647757 |
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 <davidcc@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
[toc] | [prev] | [next] | [standalone]
| From | David Carrillo-Cisneros <davidcc@google.com> |
|---|---|
| Date | 2017-05-23 10:00 +0200 |
| Subject | [PATCH v2 03/13] perf header: fail on write_padded error |
| Message-ID | <tKcTx-a4-35@gated-at.bofh.it> |
| In reply to | #1647757 |
Do not proceed if write_padded error failed.
Also, add comments to remind that return value of write_*
functions in util/header.c do not return number of bytes written.
Signed-off-by: David Carrillo-Cisneros <davidcc@google.com>
---
tools/perf/util/header.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 8f9200724979..6890f7c51814 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -72,6 +72,7 @@ bool perf_header__has_feat(const struct perf_header *header, int feat)
return test_bit(feat, header->adds_features);
}
+/* Return: 0 if succeded, -ERR if failed. */
static int do_write(int fd, const void *buf, size_t size)
{
while (size) {
@@ -87,6 +88,7 @@ static int do_write(int fd, const void *buf, size_t 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)
{
static const char zero_buf[NAME_ALIGN];
@@ -101,6 +103,7 @@ int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
#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(int fd, const char *str)
{
u32 len, olen;
@@ -3186,7 +3189,8 @@ int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
*/
tracing_data_put(tdata);
- write_padded(fd, NULL, 0, padding);
+ if (write_padded(fd, NULL, 0, padding))
+ return -1;
return aligned_size;
}
--
2.13.0.219.gdb65acc882-goog
[toc] | [prev] | [next] | [standalone]
| From | David Carrillo-Cisneros <davidcc@google.com> |
|---|---|
| Date | 2017-05-23 10:00 +0200 |
| Subject | [PATCH v2 11/13] perf header: change FEAT_OP* macros |
| Message-ID | <tKcTx-a4-41@gated-at.bofh.it> |
| In reply to | #1647757 |
There are three FEAT_OP* macros:
- FEAT_OPA: for features without process record.
- FEAT_OPP: for features with process record.
- FEAT_OPF: like FEAT_OPP but to show only if show_full_info flags
is set.
To add pipe-mode headers we need to specify whether a feature generates a
corresponding sample record. This is an extra variation to the existing
macros. To simplify, redefine macros instead so that:
- show_full_info is specified as an argument (to remove the
FEAT_OPF variation) and,
- it always sets "process" handler (to remove the FEAT_OPA variation).
Individual process handlers can be NULLed individually.
This allows to define two variations only:
- FEAT_OPR: has sample record.
- FEAT_OPN: doesn't has sample record.
Signed-off-by: David Carrillo-Cisneros <davidcc@google.com>
---
tools/perf/util/header.c | 72 +++++++++++++++++++++++++++++-------------------
1 file changed, 44 insertions(+), 28 deletions(-)
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index b7704b30ed52..7aa84a02b9bd 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -11,6 +11,7 @@
#include <linux/list.h>
#include <linux/kernel.h>
#include <linux/bitops.h>
+#include <linux/stringify.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
@@ -2106,42 +2107,57 @@ struct feature_ops {
int (*process)(struct feat_fd *fd, void *data);
const char *name;
bool full_only;
+ bool has_record;
};
-#define FEAT_OPA(n, func) \
- [n] = { .name = #n, .write = write_##func, .print = print_##func }
-#define FEAT_OPP(n, func) \
- [n] = { .name = #n, .write = write_##func, .print = print_##func, \
- .process = process_##func }
-#define FEAT_OPF(n, func) \
- [n] = { .name = #n, .write = write_##func, .print = print_##func, \
- .process = process_##func, .full_only = true }
+#define FEAT_OPR(n, func, __full_only) \
+ [HEADER_##n] = { \
+ .name = __stringify(HEADER_##n), \
+ .write = write_##func, \
+ .print = print_##func, \
+ .full_only = __full_only, \
+ .process = process_##func, \
+ .has_record = true \
+ }
+
+#define FEAT_OPN(n, func, __full_only) \
+ [HEADER_##n] = { \
+ .name = __stringify(HEADER_##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_OPP(HEADER_TRACING_DATA, tracing_data),
- FEAT_OPP(HEADER_BUILD_ID, build_id),
- FEAT_OPP(HEADER_HOSTNAME, hostname),
- FEAT_OPP(HEADER_OSRELEASE, osrelease),
- FEAT_OPP(HEADER_VERSION, version),
- FEAT_OPP(HEADER_ARCH, arch),
- FEAT_OPP(HEADER_NRCPUS, nrcpus),
- FEAT_OPP(HEADER_CPUDESC, cpudesc),
- FEAT_OPP(HEADER_CPUID, cpuid),
- FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
- FEAT_OPP(HEADER_EVENT_DESC, event_desc),
- FEAT_OPP(HEADER_CMDLINE, cmdline),
- FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
- FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
- FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
- FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
- FEAT_OPP(HEADER_GROUP_DESC, group_desc),
- FEAT_OPP(HEADER_AUXTRACE, auxtrace),
- FEAT_OPA(HEADER_STAT, stat),
- FEAT_OPF(HEADER_CACHE, cache),
+ 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_OPN(GROUP_DESC, group_desc, false),
+ FEAT_OPN(AUXTRACE, auxtrace, false),
+ FEAT_OPN(STAT, stat, false),
+ FEAT_OPN(CACHE, cache, true),
};
struct header_print_data {
--
2.13.0.219.gdb65acc882-goog
[toc] | [prev] | [next] | [standalone]
| From | David Carrillo-Cisneros <davidcc@google.com> |
|---|---|
| Date | 2017-05-23 10:00 +0200 |
| Subject | [PATCH v2 01/13] perf header: encapsulate read and swap |
| Message-ID | <tKcTx-a4-45@gated-at.bofh.it> |
| In reply to | #1647757 |
Most callers of readn in perf header read either a 32 or a 64 bits
number, error check it and swap it, if necessary.
Create do_read_u32 and do_read_u64 to simplify this usage.
Signed-off-by: David Carrillo-Cisneros <davidcc@google.com>
---
tools/perf/util/header.c | 212 +++++++++++++++++------------------------------
1 file changed, 76 insertions(+), 136 deletions(-)
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 948b2c5efb65..1dd4dbe13f88 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -117,27 +117,56 @@ static int do_write_string(int fd, const char *str)
return write_padded(fd, str, olen, len);
}
+static int __do_read(int fd, void *addr, ssize_t size)
+{
+ ssize_t ret = readn(fd, addr, size);
+
+ if (ret != (ssize_t)size)
+ return ret < 0 ? (int)ret : -1;
+ return 0;
+}
+
+static int do_read_u32(int fd, struct perf_header *ph, u32 *addr)
+{
+ int ret;
+
+ ret = __do_read(fd, addr, sizeof(*addr));
+ if (ret)
+ return ret;
+
+ if (ph->needs_swap)
+ *addr = bswap_32(*addr);
+ return 0;
+}
+
+static int do_read_u64(int fd, struct perf_header *ph, u64 *addr)
+{
+ int ret;
+
+ ret = __do_read(fd, addr, sizeof(*addr));
+ if (ret)
+ return ret;
+
+ if (ph->needs_swap)
+ *addr = bswap_64(*addr);
+ return 0;
+}
+
static char *do_read_string(int fd, struct perf_header *ph)
{
- ssize_t sz, ret;
u32 len;
char *buf;
- sz = readn(fd, &len, sizeof(len));
- if (sz < (ssize_t)sizeof(len))
+ if (do_read_u32(fd, ph, &len))
return NULL;
- if (ph->needs_swap)
- len = bswap_32(len);
-
buf = malloc(len);
if (!buf)
return NULL;
- ret = readn(fd, buf, len);
- if (ret == (ssize_t)len) {
+ if (!__do_read(fd, buf, len)) {
/*
- * strings are padded by zeroes
+ * note that strings are padded by zeroes
* thus the actual strlen of buf
* may be less than len
*/
@@ -1190,20 +1219,12 @@ read_event_desc(struct perf_header *ph, int fd)
size_t msz;
/* number of events */
- ret = readn(fd, &nre, sizeof(nre));
- if (ret != (ssize_t)sizeof(nre))
+ if (do_read_u32(fd, ph, &nre))
goto error;
- if (ph->needs_swap)
- nre = bswap_32(nre);
-
- ret = readn(fd, &sz, sizeof(sz));
- if (ret != (ssize_t)sizeof(sz))
+ if (do_read_u32(fd, ph, &sz))
goto error;
- if (ph->needs_swap)
- sz = bswap_32(sz);
-
/* buffer to hold on file attr struct */
buf = malloc(sz);
if (!buf)
@@ -1225,7 +1246,7 @@ read_event_desc(struct perf_header *ph, int fd)
* must read entire on-file attr struct to
* sync up with layout.
*/
- ret = readn(fd, buf, sz);
+ ret = __do_read(fd, buf, sz);
if (ret != (ssize_t)sz)
goto error;
@@ -1234,14 +1255,11 @@ read_event_desc(struct perf_header *ph, int fd)
memcpy(&evsel->attr, buf, msz);
- ret = readn(fd, &nr, sizeof(nr));
- if (ret != (ssize_t)sizeof(nr))
+ if (do_read_u32(fd, ph, &nr))
goto error;
- if (ph->needs_swap) {
- nr = bswap_32(nr);
+ if (ph->needs_swap)
evsel->needs_swap = true;
- }
evsel->name = do_read_string(fd, ph);
@@ -1255,11 +1273,8 @@ read_event_desc(struct perf_header *ph, int fd)
evsel->id = id;
for (j = 0 ; j < nr; j++) {
- ret = readn(fd, id, sizeof(*id));
- if (ret != (ssize_t)sizeof(*id))
+ if (do_read_u64(fd, ph, id))
goto error;
- if (ph->needs_swap)
- *id = bswap_64(*id);
id++;
}
}
@@ -1631,26 +1646,18 @@ static int process_nrcpus(struct perf_file_section *section __maybe_unused,
struct perf_header *ph, int fd,
void *data __maybe_unused)
{
- ssize_t ret;
- u32 nr;
-
- ret = readn(fd, &nr, sizeof(nr));
- if (ret != sizeof(nr))
- return -1;
-
- if (ph->needs_swap)
- nr = bswap_32(nr);
-
- ph->env.nr_cpus_avail = nr;
-
- ret = readn(fd, &nr, sizeof(nr));
- if (ret != sizeof(nr))
- return -1;
+ int ret;
+ u32 nr_cpus_avail, nr_cpus_online;
- if (ph->needs_swap)
- nr = bswap_32(nr);
+ ret = do_read_u32(fd, ph, &nr_cpus_avail);
+ if (ret)
+ return ret;
- ph->env.nr_cpus_online = nr;
+ ret = do_read_u32(fd, 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;
return 0;
}
@@ -1674,17 +1681,13 @@ static int process_total_mem(struct perf_file_section *section __maybe_unused,
struct perf_header *ph, int fd,
void *data __maybe_unused)
{
- uint64_t mem;
- ssize_t ret;
+ u64 total_mem;
+ int ret;
- ret = readn(fd, &mem, sizeof(mem));
- if (ret != sizeof(mem))
+ ret = do_read_u64(fd, ph, &total_mem);
+ if (ret)
return -1;
-
- if (ph->needs_swap)
- mem = bswap_64(mem);
-
- ph->env.total_mem = mem;
+ ph->env.total_mem = (unsigned long long)total_mem;
return 0;
}
@@ -1744,17 +1747,12 @@ static int process_cmdline(struct perf_file_section *section,
struct perf_header *ph, int fd,
void *data __maybe_unused)
{
- ssize_t ret;
char *str, *cmdline = NULL, **argv = NULL;
u32 nr, i, len = 0;
- ret = readn(fd, &nr, sizeof(nr));
- if (ret != sizeof(nr))
+ if (do_read_u32(fd, ph, &nr))
return -1;
- if (ph->needs_swap)
- nr = bswap_32(nr);
-
ph->env.nr_cmdline = nr;
cmdline = zalloc(section->size + nr + 1);
@@ -1789,7 +1787,6 @@ static int process_cpu_topology(struct perf_file_section *section,
struct perf_header *ph, int fd,
void *data __maybe_unused)
{
- ssize_t ret;
u32 nr, i;
char *str;
struct strbuf sb;
@@ -1800,15 +1797,10 @@ static int process_cpu_topology(struct perf_file_section *section,
if (!ph->env.cpu)
return -1;
- ret = readn(fd, &nr, sizeof(nr));
- if (ret != sizeof(nr))
+ if (do_read_u32(fd, ph, &nr))
goto free_cpu;
- if (ph->needs_swap)
- nr = bswap_32(nr);
-
ph->env.nr_sibling_cores = nr;
- size += sizeof(u32);
if (strbuf_init(&sb, 128) < 0)
goto free_cpu;
@@ -1820,20 +1812,14 @@ static int process_cpu_topology(struct perf_file_section *section,
/* 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);
- ret = readn(fd, &nr, sizeof(nr));
- if (ret != sizeof(nr))
+ if (do_read_u32(fd, ph, &nr))
return -1;
- if (ph->needs_swap)
- nr = bswap_32(nr);
-
ph->env.nr_sibling_threads = nr;
- size += sizeof(u32);
for (i = 0; i < nr; i++) {
str = do_read_string(fd, ph);
@@ -1843,7 +1829,6 @@ static int process_cpu_topology(struct perf_file_section *section,
/* 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);
@@ -1858,22 +1843,14 @@ static int process_cpu_topology(struct perf_file_section *section,
}
for (i = 0; i < (u32)cpu_nr; i++) {
- ret = readn(fd, &nr, sizeof(nr));
- if (ret != sizeof(nr))
+ if (do_read_u32(fd, ph, &nr))
goto free_cpu;
- if (ph->needs_swap)
- nr = bswap_32(nr);
-
ph->env.cpu[i].core_id = nr;
- ret = readn(fd, &nr, sizeof(nr));
- if (ret != sizeof(nr))
+ if (do_read_u32(fd, ph, &nr))
goto free_cpu;
- if (ph->needs_swap)
- nr = bswap_32(nr);
-
if (nr != (u32)-1 && nr > (u32)cpu_nr) {
pr_debug("socket_id number is too big."
"You may need to upgrade the perf tool.\n");
@@ -1897,18 +1874,13 @@ static int process_numa_topology(struct perf_file_section *section __maybe_unuse
void *data __maybe_unused)
{
struct numa_node *nodes, *n;
- ssize_t ret;
u32 nr, i;
char *str;
/* nr nodes */
- ret = readn(fd, &nr, sizeof(nr));
- if (ret != sizeof(nr))
+ if (do_read_u32(fd, ph, &nr))
return -1;
- if (ph->needs_swap)
- nr = bswap_32(nr);
-
nodes = zalloc(sizeof(*nodes) * nr);
if (!nodes)
return -ENOMEM;
@@ -1917,24 +1889,15 @@ static int process_numa_topology(struct perf_file_section *section __maybe_unuse
n = &nodes[i];
/* node number */
- ret = readn(fd, &n->node, sizeof(u32));
- if (ret != sizeof(n->node))
+ if (do_read_u32(fd, ph, &n->node))
goto error;
- ret = readn(fd, &n->mem_total, sizeof(u64));
- if (ret != sizeof(u64))
+ if (do_read_u64(fd, ph, &n->mem_total))
goto error;
- ret = readn(fd, &n->mem_free, sizeof(u64));
- if (ret != sizeof(u64))
+ if (do_read_u64(fd, ph, &n->mem_free))
goto error;
- if (ph->needs_swap) {
- n->node = bswap_32(n->node);
- n->mem_total = bswap_64(n->mem_total);
- n->mem_free = bswap_64(n->mem_free);
- }
-
str = do_read_string(fd, ph);
if (!str)
goto error;
@@ -1958,19 +1921,14 @@ static int process_pmu_mappings(struct perf_file_section *section __maybe_unused
struct perf_header *ph, int fd,
void *data __maybe_unused)
{
- ssize_t ret;
char *name;
u32 pmu_num;
u32 type;
struct strbuf sb;
- ret = readn(fd, &pmu_num, sizeof(pmu_num));
- if (ret != sizeof(pmu_num))
+ if (do_read_u32(fd, ph, &pmu_num))
return -1;
- if (ph->needs_swap)
- pmu_num = bswap_32(pmu_num);
-
if (!pmu_num) {
pr_debug("pmu mappings not available\n");
return 0;
@@ -1981,10 +1939,8 @@ static int process_pmu_mappings(struct perf_file_section *section __maybe_unused
return -1;
while (pmu_num) {
- if (readn(fd, &type, sizeof(type)) != sizeof(type))
+ if (do_read_u32(fd, ph, &type))
goto error;
- if (ph->needs_swap)
- type = bswap_32(type);
name = do_read_string(fd, ph);
if (!name)
@@ -2024,12 +1980,9 @@ static int process_group_desc(struct perf_file_section *section __maybe_unused,
u32 nr_members;
} *desc;
- if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
+ if (do_read_u32(fd, ph, &nr_groups))
return -1;
- if (ph->needs_swap)
- nr_groups = bswap_32(nr_groups);
-
ph->env.nr_groups = nr_groups;
if (!nr_groups) {
pr_debug("group desc not available\n");
@@ -2045,16 +1998,11 @@ static int process_group_desc(struct perf_file_section *section __maybe_unused,
if (!desc[i].name)
goto out_free;
- if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
+ if (do_read_u32(fd, ph, &desc[i].leader_idx))
goto out_free;
- if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
+ if (do_read_u32(fd, ph, &desc[i].nr_members))
goto out_free;
-
- if (ph->needs_swap) {
- desc[i].leader_idx = bswap_32(desc[i].leader_idx);
- desc[i].nr_members = bswap_32(desc[i].nr_members);
- }
}
/*
@@ -2127,21 +2075,15 @@ static int process_cache(struct perf_file_section *section __maybe_unused,
struct cpu_cache_level *caches;
u32 cnt, i, version;
- if (readn(fd, &version, sizeof(version)) != sizeof(version))
+ if (do_read_u32(fd, ph, &version))
return -1;
- if (ph->needs_swap)
- version = bswap_32(version);
-
if (version != 1)
return -1;
- if (readn(fd, &cnt, sizeof(cnt)) != sizeof(cnt))
+ if (do_read_u32(fd, ph, &cnt))
return -1;
- if (ph->needs_swap)
- cnt = bswap_32(cnt);
-
caches = zalloc(sizeof(*caches) * cnt);
if (!caches)
return -1;
@@ -2150,10 +2092,8 @@ static int process_cache(struct perf_file_section *section __maybe_unused,
struct cpu_cache_level c;
#define _R(v) \
- if (readn(fd, &c.v, sizeof(u32)) != sizeof(u32))\
+ if (do_read_u32(fd, ph, &c.v))\
goto out_free_caches; \
- if (ph->needs_swap) \
- c.v = bswap_32(c.v); \
_R(level)
_R(line_size)
--
2.13.0.219.gdb65acc882-goog
[toc] | [prev] | [next] | [standalone]
| From | Namhyung Kim <namhyung@kernel.org> |
|---|---|
| Date | 2017-05-24 17:40 +0200 |
| Subject | Re: [PATCH v2 01/13] perf header: encapsulate read and swap |
| Message-ID | <tKGyd-46k-1@gated-at.bofh.it> |
| In reply to | #1647767 |
On Tue, May 23, 2017 at 12:48:41AM -0700, David Carrillo-Cisneros wrote:
> Most callers of readn in perf header read either a 32 or a 64 bits
> number, error check it and swap it, if necessary.
>
> Create do_read_u32 and do_read_u64 to simplify this usage.
>
> Signed-off-by: David Carrillo-Cisneros <davidcc@google.com>
> ---
> tools/perf/util/header.c | 212 +++++++++++++++++------------------------------
> 1 file changed, 76 insertions(+), 136 deletions(-)
>
> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> index 948b2c5efb65..1dd4dbe13f88 100644
> --- a/tools/perf/util/header.c
> +++ b/tools/perf/util/header.c
> @@ -117,27 +117,56 @@ static int do_write_string(int fd, const char *str)
> return write_padded(fd, str, olen, len);
> }
>
> +static int __do_read(int fd, void *addr, ssize_t size)
> +{
> + ssize_t ret = readn(fd, addr, size);
> +
> + if (ret != (ssize_t)size)
The 'size' is already ssize_t.
Thanks,
Namhyung
> + return ret < 0 ? (int)ret : -1;
> + return 0;
> +}
[toc] | [prev] | [next] | [standalone]
| From | David Carrillo-Cisneros <davidcc@google.com> |
|---|---|
| Date | 2017-05-23 10:00 +0200 |
| Subject | [PATCH v2 05/13] perf header: revamp do_write |
| Message-ID | <tKcTx-a4-49@gated-at.bofh.it> |
| In reply to | #1647757 |
Now that writen takes a const buffer, use it in do_write instead
of duplicating readn functionality..
Export do_write to use it consistently in header.c and
build_id.c .
Signed-off-by: David Carrillo-Cisneros <davidcc@google.com>
---
tools/perf/util/build-id.c | 2 +-
tools/perf/util/header.c | 14 +++++---------
tools/perf/util/header.h | 2 ++
3 files changed, 8 insertions(+), 10 deletions(-)
diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index 168cc49654e7..84d2ea51e557 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -345,7 +345,7 @@ 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;
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 6890f7c51814..292fb2156a59 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -73,17 +73,13 @@ 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(int fd, const void *buf, size_t size)
{
- while (size) {
- int ret = write(fd, buf, size);
-
- if (ret < 0)
- return -errno;
+ ssize_t ret;
- size -= ret;
- buf += ret;
- }
+ ret = writen(fd, buf, size);
+ if (ret != (ssize_t)size)
+ return ret < 0 ? (int)ret: -1;
return 0;
}
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
index d30109b421ee..e98489c8bba7 100644
--- a/tools/perf/util/header.h
+++ b/tools/perf/util/header.h
@@ -144,6 +144,8 @@ bool is_perf_magic(u64 magic);
#define NAME_ALIGN 64
+int do_write(int fd, const void *buf, size_t size);
+
int write_padded(int fd, const void *bf, size_t count, size_t count_aligned);
/*
--
2.13.0.219.gdb65acc882-goog
[toc] | [prev] | [standalone]
Back to top | Article view | linux.kernel
csiph-web