linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@redhat.com>, Namhyung Kim <namhyung@kernel.org>,
	Kan Liang <kan.liang@linux.intel.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Alexey Budankov <alexey.budankov@linux.intel.com>,
	linux-kernel@vger.kernel.org
Cc: Ian Rogers <irogers@google.com>
Subject: [PATCH] perf data: if a bad header size, retry in pipe mode
Date: Thu,  9 Apr 2020 11:57:44 -0700	[thread overview]
Message-ID: <20200409185744.255881-1-irogers@google.com> (raw)

Currently pipe mode files fail like:
$ perf record -o - sleep 1 > /tmp/perf.pipe.data
$ perf report -i /tmp/perf.pipe.data
incompatible file format (rerun with -v to learn more)

This change makes it so that if a perf.data file's header size is wrong
it is re-checked in pipe mode, where if wrong it fails as it currently
does.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/header.c | 35 ++++++++++++++++++++++++++---------
 tools/perf/util/header.h |  5 -----
 2 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index acbd046bf95c..1df00a4a71b6 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -3329,8 +3329,15 @@ static int check_magic_endian(u64 magic, uint64_t hdr_sz,
 	return 0;
 }
 
-int perf_file_header__read(struct perf_file_header *header,
-			   struct perf_header *ph, int fd)
+enum header_read_status {
+	HEADER_READ_OK,
+	HEADER_READ_BAD_SIZE,
+	HEADER_READ_BAD,
+};
+
+static enum header_read_status
+perf_file_header__read(struct perf_file_header *header,
+		       struct perf_header *ph, int fd)
 {
 	ssize_t ret;
 
@@ -3338,12 +3345,12 @@ int perf_file_header__read(struct perf_file_header *header,
 
 	ret = readn(fd, header, sizeof(*header));
 	if (ret <= 0)
-		return -1;
+		return HEADER_READ_BAD;
 
 	if (check_magic_endian(header->magic,
 			       header->attr_size, false, ph) < 0) {
 		pr_debug("magic/endian check failed\n");
-		return -1;
+		return HEADER_READ_BAD;
 	}
 
 	if (ph->needs_swap) {
@@ -3356,7 +3363,7 @@ int perf_file_header__read(struct perf_file_header *header,
 		if (header->size == offsetof(typeof(*header), adds_features))
 			bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
 		else
-			return -1;
+			return HEADER_READ_BAD_SIZE;
 	} else if (ph->needs_swap) {
 		/*
 		 * feature bitmap is declared as an array of unsigned longs --
@@ -3398,7 +3405,7 @@ int perf_file_header__read(struct perf_file_header *header,
 	ph->data_offset  = header->data.offset;
 	ph->data_size	 = header->data.size;
 	ph->feat_offset  = header->data.offset + header->data.size;
-	return 0;
+	return HEADER_READ_OK;
 }
 
 static int perf_file_section__process(struct perf_file_section *section,
@@ -3580,12 +3587,22 @@ int perf_session__read_header(struct perf_session *session)
 
 	session->evlist->env = &header->env;
 	session->machines.host.env = &header->env;
+
+	if (!perf_data__is_pipe(data)) {
+		enum header_read_status status =
+			perf_file_header__read(&f_header, header, fd);
+		if (status == HEADER_READ_BAD)
+			return -EINVAL;
+		if (status == HEADER_READ_BAD_SIZE) {
+			pr_debug("Data had a bad header size, retrying in pipe mode\n");
+			lseek(fd, 0, SEEK_SET);
+			data->is_pipe = true;
+		}
+	}
+
 	if (perf_data__is_pipe(data))
 		return perf_header__read_pipe(session);
 
-	if (perf_file_header__read(&f_header, header, fd) < 0)
-		return -EINVAL;
-
 	/*
 	 * Sanity check that perf.data was written cleanly; data size is
 	 * initialized to 0 and updated only if the on_exit function is run.
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
index 840f95cee349..3d7f39358265 100644
--- a/tools/perf/util/header.h
+++ b/tools/perf/util/header.h
@@ -73,11 +73,6 @@ struct perf_pipe_file_header {
 	u64				size;
 };
 
-struct perf_header;
-
-int perf_file_header__read(struct perf_file_header *header,
-			   struct perf_header *ph, int fd);
-
 struct perf_header {
 	enum perf_header_version	version;
 	bool				needs_swap;
-- 
2.26.0.292.g33ef6b2f38-goog


             reply	other threads:[~2020-04-09 18:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-09 18:57 Ian Rogers [this message]
2020-04-14 12:51 ` [PATCH] perf data: if a bad header size, retry in pipe mode Jiri Olsa
2020-04-14 14:01   ` Ian Rogers
2020-04-30 14:12     ` Ian Rogers
2020-04-30 15:34       ` Jiri Olsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200409185744.255881-1-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=alexey.budankov@linux.intel.com \
    --cc=jolsa@redhat.com \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).