[PATCH 4/4] perf inject: Fix output from a file to a pipe
From: Namhyung Kim
Date: Wed Jul 07 2021 - 14:05:50 EST
When the input is a regular file but the output is a pipe, it should
write a pipe header. But just repiping would write a portion of the
existing header which is different in 'size' value. So we need to
prevent it and write a new pipe header along with other information
like event attributes and features.
This can handle something like this:
# perf record -a -B sleep 1
# perf inject -b -i perf.data | perf report -i -
Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
---
tools/perf/builtin-inject.c | 57 +++++++++++++++++++++++++++++++++++--
1 file changed, 55 insertions(+), 2 deletions(-)
diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index 7c126597d3f5..50ed158c8076 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -918,6 +918,7 @@ int cmd_inject(int argc, const char **argv)
.use_stdio = true,
};
int ret;
+ bool repipe = true;
struct option options[] = {
OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
@@ -992,10 +993,18 @@ int cmd_inject(int argc, const char **argv)
}
data.path = inject.input_name;
- if (!strcmp(inject.input_name, "-") || inject.output.is_pipe)
+ if (!strcmp(inject.input_name, "-") || inject.output.is_pipe) {
inject.is_pipe = true;
+ /*
+ * Do not repipe header when input is a regular file
+ * since either it can rewrite the header at the end
+ * or write a new pipe header.
+ */
+ if (strcmp(inject.input_name, "-"))
+ repipe = false;
+ }
- inject.session = __perf_session__new(&data, inject.is_pipe,
+ inject.session = __perf_session__new(&data, repipe,
perf_data__fd(&inject.output),
&inject.tool);
if (IS_ERR(inject.session))
@@ -1004,6 +1013,50 @@ int cmd_inject(int argc, const char **argv)
if (zstd_init(&(inject.session->zstd_data), 0) < 0)
pr_warning("Decompression initialization failed.\n");
+ if (!data.is_pipe && inject.output.is_pipe) {
+ ret = perf_header__write_pipe(perf_data__fd(&inject.output));
+ if (ret < 0) {
+ pr_err("Couldn't write a new pipe header.\n");
+ goto out_delete;
+ }
+
+ ret = perf_event__synthesize_attrs(&inject.tool,
+ inject.session->evlist,
+ perf_event__repipe);
+ if (ret < 0) {
+ pr_err("Couldn't inject synthesized attrs.\n");
+ goto out_delete;
+ }
+
+ ret = perf_event__synthesize_features(&inject.tool,
+ inject.session,
+ inject.session->evlist,
+ perf_event__repipe);
+ if (ret < 0) {
+ pr_err("Couldn't inject synthesized features.\n");
+ goto out_delete;
+ }
+
+ if (have_tracepoints(&inject.session->evlist->core.entries)) {
+ /*
+ * FIXME err <= 0 here actually means that
+ * there were no tracepoints so its not really
+ * an error, just that we don't need to
+ * synthesize anything. We really have to
+ * return this more properly and also
+ * propagate errors that now are calling die()
+ */
+ ret = perf_event__synthesize_tracing_data(&inject.tool,
+ perf_data__fd(&inject.output),
+ inject.session->evlist,
+ perf_event__repipe);
+ if (ret <= 0) {
+ pr_err("Couldn't inject tracing data.\n");
+ goto out_delete;
+ }
+ }
+ }
+
if (inject.build_ids && !inject.build_id_all) {
/*
* to make sure the mmap records are ordered correctly
--
2.32.0.93.g670b81a890-goog