Linux Perf Users
 help / color / mirror / Atom feed
From: Stephen Brennan <stephen.s.brennan@oracle.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Ingo Molnar <mingo@redhat.com>
Cc: Stephen Brennan <stephen.s.brennan@oracle.com>,
	linux-kernel@vger.kernel.org,
	James Clark <james.clark@linaro.org>,
	Mark Rutland <mark.rutland@arm.com>, Jiri Olsa <jolsa@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	linux-perf-users@vger.kernel.org
Subject: [PATCH 2/2] tools: perf: flamegraph.py: fix "Error reading report header"
Date: Thu,  1 Oct 2026 11:20:01 -0700	[thread overview]
Message-ID: <20261001182004.1160406-3-stephen.s.brennan@oracle.com> (raw)
In-Reply-To: <20261001182004.1160406-1-stephen.s.brennan@oracle.com>

When running "perf script flamegraph" in a directory without perf.data,
the script succeeds but prints out the following error:

    # perf script flamegraph -ag -F999 -- sleep 5
    failed to open perf.data: No such file or directory  (try 'perf record' first)
    Error reading report header: Command '['perf', 'report', '--header-only']' returned non-zero exit status 255.
    dumping data to flamegraph.html

If a "perf.data" file were present in the working directory, then the
error would not occur, but flamegraph.py would silently include an
incorrect header as the context for the flamegraph.

The issue is with the "-i" option and how it interacts with option
parsing in the flamegraph-report shell script. Below are the different
flamegraph-report script calls in recent git history:

[1]: perf script [...] -- "$@"
[2]: perf script [...] -i - -- "$@"
[3]: perf script [...] "$@"

[1] is prior to commit 23e0a63c6dd3f ("perf script: force stdin for
flamegraph in live mode"), [2] is after the same commit, and [3] is
after commit 29bab85418efd ("perf script: Fix hangup in offline
flamegraph report"). As noted in the latter commit, for the "perf script
flamegraph" invocation, the parent perf process which sets up the pipe
will set the appropriate "-i -" option when it invokes
flamegraph-report. Thus, the third form is the correct way to ensure
perf itself processes the "-i" option and thus processes the correct
input file.

As a result, for cases 1 and 2, there was an input file string passed to
flamegraph.py -- though it did not necessarily match the one that perf
was processing. For the current case (#3), the script doesn't receive an
input file string and thus assumes that "perf.data" is the input, even
when stdin is being processed.

All of this is fragile and works around what we really want: to know
what input file perf is processing. The previous commit implements a
helper for this, so drop the "-i" option entirely and use the helper to
properly detect the filename.

Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
---
 tools/perf/scripts/python/flamegraph.py | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/tools/perf/scripts/python/flamegraph.py b/tools/perf/scripts/python/flamegraph.py
index ad735990c5bec..d49b96a324a6f 100755
--- a/tools/perf/scripts/python/flamegraph.py
+++ b/tools/perf/scripts/python/flamegraph.py
@@ -28,6 +28,8 @@ import sys
 from typing import Dict, Optional, Union
 import urllib.request
 
+from perf_trace_context import perf_input_name, perf_script_context
+
 MINIMAL_HTML = """<head>
   <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/d3-flame-graph@4.1.3/dist/d3-flamegraph.css">
 </head>
@@ -122,20 +124,14 @@ class FlameGraphCLI:
         node.value += 1
 
     def get_report_header(self) -> str:
-        if self.args.input == "-":
+        if self.input_file_name == "-":
             # when this script is invoked with "perf script flamegraph",
             # no perf.data is created and we cannot read the header of it
             return ""
 
         try:
-            # if the file name other than perf.data is given,
-            # we read the header of that file
-            if self.args.input:
-                output = subprocess.check_output(["perf", "report", "--header-only",
-                                                  "-i", self.args.input])
-            else:
-                output = subprocess.check_output(["perf", "report", "--header-only"])
-
+            output = subprocess.check_output(["perf", "report", "--header-only",
+                                              "-i", self.input_file_name])
             result = output.decode("utf-8")
             if self.args.event_name:
                 result += "\nFocused event: " + self.args.event_name
@@ -144,6 +140,9 @@ class FlameGraphCLI:
             print(f"Error reading report header: {err}", file=sys.stderr)
             return ""
 
+    def trace_begin(self) -> None:
+        self.input_file_name = perf_input_name(perf_script_context) or "-"
+
     def trace_end(self) -> None:
         stacks_json = json.dumps(self.stack, default=lambda x: x.to_json())
 
@@ -167,7 +166,7 @@ js-d3-flame-graph or libjs-d3-flame-graph, specify an existing flame
 graph template (--template PATH) or use another output format (--format
 FORMAT).""",
                               file=sys.stderr)
-                        if self.args.input == "-":
+                        if self.input_file_name == "-":
                             print(
 """Not attempting to download Flame Graph template as script command line
 input is disabled due to using live mode. If you want to download the
@@ -248,8 +247,6 @@ if __name__ == "__main__":
                         default="blue-green",
                         help="flame graph color scheme",
                         choices=["blue-green", "orange"])
-    parser.add_argument("-i", "--input",
-                        help=argparse.SUPPRESS)
     parser.add_argument("--allow-download",
                         default=False,
                         action="store_true",
@@ -265,3 +262,4 @@ if __name__ == "__main__":
 
     process_event = cli.process_event
     trace_end = cli.trace_end
+    trace_begin = cli.trace_begin
-- 
2.52.0


  parent reply	other threads:[~2026-10-01 18:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-01 18:19 [PATCH 0/2] perf script flamegraph: Fix input file name errors Stephen Brennan
2026-10-01 18:20 ` [PATCH 1/2] tools: perf: add perf_input_name() helper Stephen Brennan
2026-10-02  9:12   ` sashiko-bot
2026-10-01 18:20 ` Stephen Brennan [this message]
2026-10-02  9:12   ` [PATCH 2/2] tools: perf: flamegraph.py: fix "Error reading report header" sashiko-bot
2026-10-02  5:24 ` [PATCH 0/2] perf script flamegraph: Fix input file name errors Ian Rogers
2026-10-02 18:27   ` Stephen Brennan

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=20261001182004.1160406-3-stephen.s.brennan@oracle.com \
    --to=stephen.s.brennan@oracle.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@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