[PATCH] Minor improvements for perf script flamegraph

Tianyou Li posted 1 patch 8 months, 1 week ago
tools/perf/scripts/python/flamegraph.py | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
[PATCH] Minor improvements for perf script flamegraph
Posted by Tianyou Li 8 months, 1 week ago
When processing the perf data file generated with multiple events,
the flamegraph script will count all the events regardless of
different event names. If specify the perf data file with -i option,
the script will try to read the header information regardless of
the file name specified, instead it will try to access the perf.data.

This patch tries to add a -e option to specify the event name that
the flamegraph will be generated accordingly. If the -e option omitted,
the behavior remains unchanged. If the -i option specified, the header
information will be read from that file.

Signed-off-by: Tianyou Li <tianyou.li@intel.com>
Reviewed-by: Pan Deng <pan.deng@intel.com>
Reviewed-by: Zhiguo Zhou <zhiguo.zhou@intel.com>
Reviewed-by: Wangyang Guo <wangyang.guo@intel.com>
Reviewed-by: Tim Chen <tim.c.chen@linux.intel.com>
---
 tools/perf/scripts/python/flamegraph.py | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/tools/perf/scripts/python/flamegraph.py b/tools/perf/scripts/python/flamegraph.py
index cf7ce8229a..eb78b93925 100755
--- a/tools/perf/scripts/python/flamegraph.py
+++ b/tools/perf/scripts/python/flamegraph.py
@@ -94,6 +94,11 @@ class FlameGraphCLI:
         return child
 
     def process_event(self, event):
+        # ignore events where the event name does not match
+        # the one specified by the user
+        if self.args.event_name and event.get("ev_name") != self.args.event_name:
+            return
+
         pid = event.get("sample", {}).get("pid", 0)
         # event["dso"] sometimes contains /usr/lib/debug/lib/modules/*/vmlinux
         # for user-space processes; let's use pid for kernel or user-space distinction
@@ -123,8 +128,15 @@ class FlameGraphCLI:
             return ""
 
         try:
-            output = subprocess.check_output(["perf", "report", "--header-only"])
-            return output.decode("utf-8")
+            if self.args.input:
+                output = subprocess.check_output(["perf", "script", "--header-only", "-i", self.args.input])
+            else:
+                output = subprocess.check_output(["perf", "report", "--header-only"])
+
+            result = output.decode("utf-8")
+            if self.args.event_name:
+                result += "\nFocused event: " + self.args.event_name
+            return result
         except Exception as err:  # pylint: disable=broad-except
             print("Error reading report header: {}".format(err), file=sys.stderr)
             return ""
@@ -235,6 +247,11 @@ if __name__ == "__main__":
                         default=False,
                         action="store_true",
                         help="allow unprompted downloading of HTML template")
+    parser.add_argument("-e", "--event",
+                        default="",
+                        dest="event_name",
+                        type=str,
+                        help="specify the event to generate flamegraph for")
 
     cli_args = parser.parse_args()
     cli = FlameGraphCLI(cli_args)
-- 
2.43.5
Re: [PATCH] Minor improvements for perf script flamegraph
Posted by Namhyung Kim 8 months ago
Hello,

On Tue, Jun 03, 2025 at 03:10:48PM +0800, Tianyou Li wrote:
> When processing the perf data file generated with multiple events,
> the flamegraph script will count all the events regardless of
> different event names. If specify the perf data file with -i option,
> the script will try to read the header information regardless of
> the file name specified, instead it will try to access the perf.data.
> 
> This patch tries to add a -e option to specify the event name that
> the flamegraph will be generated accordingly. If the -e option omitted,
> the behavior remains unchanged. If the -i option specified, the header
> information will be read from that file.

Looks like two separate changes.  Can you please split them?

Thanks,
Namhyung

> 
> Signed-off-by: Tianyou Li <tianyou.li@intel.com>
> Reviewed-by: Pan Deng <pan.deng@intel.com>
> Reviewed-by: Zhiguo Zhou <zhiguo.zhou@intel.com>
> Reviewed-by: Wangyang Guo <wangyang.guo@intel.com>
> Reviewed-by: Tim Chen <tim.c.chen@linux.intel.com>
> ---
>  tools/perf/scripts/python/flamegraph.py | 21 +++++++++++++++++++--
>  1 file changed, 19 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/scripts/python/flamegraph.py b/tools/perf/scripts/python/flamegraph.py
> index cf7ce8229a..eb78b93925 100755
> --- a/tools/perf/scripts/python/flamegraph.py
> +++ b/tools/perf/scripts/python/flamegraph.py
> @@ -94,6 +94,11 @@ class FlameGraphCLI:
>          return child
>  
>      def process_event(self, event):
> +        # ignore events where the event name does not match
> +        # the one specified by the user
> +        if self.args.event_name and event.get("ev_name") != self.args.event_name:
> +            return
> +
>          pid = event.get("sample", {}).get("pid", 0)
>          # event["dso"] sometimes contains /usr/lib/debug/lib/modules/*/vmlinux
>          # for user-space processes; let's use pid for kernel or user-space distinction
> @@ -123,8 +128,15 @@ class FlameGraphCLI:
>              return ""
>  
>          try:
> -            output = subprocess.check_output(["perf", "report", "--header-only"])
> -            return output.decode("utf-8")
> +            if self.args.input:
> +                output = subprocess.check_output(["perf", "script", "--header-only", "-i", self.args.input])
> +            else:
> +                output = subprocess.check_output(["perf", "report", "--header-only"])
> +
> +            result = output.decode("utf-8")
> +            if self.args.event_name:
> +                result += "\nFocused event: " + self.args.event_name
> +            return result
>          except Exception as err:  # pylint: disable=broad-except
>              print("Error reading report header: {}".format(err), file=sys.stderr)
>              return ""
> @@ -235,6 +247,11 @@ if __name__ == "__main__":
>                          default=False,
>                          action="store_true",
>                          help="allow unprompted downloading of HTML template")
> +    parser.add_argument("-e", "--event",
> +                        default="",
> +                        dest="event_name",
> +                        type=str,
> +                        help="specify the event to generate flamegraph for")
>  
>      cli_args = parser.parse_args()
>      cli = FlameGraphCLI(cli_args)
> -- 
> 2.43.5
>
RE: [PATCH] Minor improvements for perf script flamegraph
Posted by Li, Tianyou 8 months ago
Hi Namhyung,

Thanks for your review and suggestions. I have split the patch into two patches. Please let me know if anything else is needed. Thanks.

Regards,
Tianyou

-----Original Message-----
From: Namhyung Kim <namhyung@kernel.org> 
Sent: Tuesday, June 10, 2025 9:20 AM
To: Li, Tianyou <tianyou.li@intel.com>
Cc: 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@kernel.org>; Ian Rogers <irogers@google.com>; Hunter, Adrian <adrian.hunter@intel.com>; Kan Liang <kan.liang@linux.intel.com>; Guo, Wangyang <wangyang.guo@intel.com>; Deng, Pan <pan.deng@intel.com>; Zhou, Zhiguo <zhiguo.zhou@intel.com>; Chen, Tim C <tim.c.chen@intel.com>; tim.c.chen@linux.intel.com; linux-perf-users@vger.kernel.org; linux-kernel@vger.kernel.org
Subject: Re: [PATCH] Minor improvements for perf script flamegraph

Hello,

On Tue, Jun 03, 2025 at 03:10:48PM +0800, Tianyou Li wrote:
> When processing the perf data file generated with multiple events, the 
> flamegraph script will count all the events regardless of different 
> event names. If specify the perf data file with -i option, the script 
> will try to read the header information regardless of the file name 
> specified, instead it will try to access the perf.data.
> 
> This patch tries to add a -e option to specify the event name that the 
> flamegraph will be generated accordingly. If the -e option omitted, 
> the behavior remains unchanged. If the -i option specified, the header 
> information will be read from that file.

Looks like two separate changes.  Can you please split them?

Thanks,
Namhyung

> 
> Signed-off-by: Tianyou Li <tianyou.li@intel.com>
> Reviewed-by: Pan Deng <pan.deng@intel.com>
> Reviewed-by: Zhiguo Zhou <zhiguo.zhou@intel.com>
> Reviewed-by: Wangyang Guo <wangyang.guo@intel.com>
> Reviewed-by: Tim Chen <tim.c.chen@linux.intel.com>
> ---
>  tools/perf/scripts/python/flamegraph.py | 21 +++++++++++++++++++--
>  1 file changed, 19 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/scripts/python/flamegraph.py 
> b/tools/perf/scripts/python/flamegraph.py
> index cf7ce8229a..eb78b93925 100755
> --- a/tools/perf/scripts/python/flamegraph.py
> +++ b/tools/perf/scripts/python/flamegraph.py
> @@ -94,6 +94,11 @@ class FlameGraphCLI:
>          return child
>  
>      def process_event(self, event):
> +        # ignore events where the event name does not match
> +        # the one specified by the user
> +        if self.args.event_name and event.get("ev_name") != self.args.event_name:
> +            return
> +
>          pid = event.get("sample", {}).get("pid", 0)
>          # event["dso"] sometimes contains /usr/lib/debug/lib/modules/*/vmlinux
>          # for user-space processes; let's use pid for kernel or 
> user-space distinction @@ -123,8 +128,15 @@ class FlameGraphCLI:
>              return ""
>  
>          try:
> -            output = subprocess.check_output(["perf", "report", "--header-only"])
> -            return output.decode("utf-8")
> +            if self.args.input:
> +                output = subprocess.check_output(["perf", "script", "--header-only", "-i", self.args.input])
> +            else:
> +                output = subprocess.check_output(["perf", "report", 
> + "--header-only"])
> +
> +            result = output.decode("utf-8")
> +            if self.args.event_name:
> +                result += "\nFocused event: " + self.args.event_name
> +            return result
>          except Exception as err:  # pylint: disable=broad-except
>              print("Error reading report header: {}".format(err), file=sys.stderr)
>              return ""
> @@ -235,6 +247,11 @@ if __name__ == "__main__":
>                          default=False,
>                          action="store_true",
>                          help="allow unprompted downloading of HTML 
> template")
> +    parser.add_argument("-e", "--event",
> +                        default="",
> +                        dest="event_name",
> +                        type=str,
> +                        help="specify the event to generate 
> + flamegraph for")
>  
>      cli_args = parser.parse_args()
>      cli = FlameGraphCLI(cli_args)
> --
> 2.43.5
>