Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2155694ybv; Fri, 21 Feb 2020 09:57:21 -0800 (PST) X-Google-Smtp-Source: APXvYqz2aV6oEuOJC6+bGOR/4rDVSC58CcLfD/IYsdpZVuOJpdpunE7N7ERYuOVyH4QUExx5RLlS X-Received: by 2002:a05:6808:b39:: with SMTP id t25mr2980612oij.71.1582307841848; Fri, 21 Feb 2020 09:57:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582307841; cv=none; d=google.com; s=arc-20160816; b=j8QugQa3KLfaxwIo1g2aQHzts1hRi6Mm0eqaRS9mOHIo7T4L8ONgeBCXPdFor0HTJp 32k9zW63X7KO2vzsAMgNehhx70Mo7kgXhXKPXxugemvEEaEp+phkmnLpF3K08G+BCIGG Ehp5G+uZegfybP4bnBuqvxlY05kuS4WDNCMQd6oZnfCXmLAvuOVwbgBLm3mhcVrATycy xZRLwQpJNCTUOeGAKrsecDtjbc0/gTjZY6ORkT46YvyVVCaak+6BgnNvlDe7ANzYLoFm zXPj8gILZOBHa4K6pxs+NordCsQfdo9tzW5H+bm+725hx4aocG3gAhu5auFrtGz9giv+ /EMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=88Nxd/8ku9aw8ZzcS+9T+V3Q+Oha7xL17ZgefCeO0Ts=; b=nk3rIFnNTQyPK3M+4a8BBJ73DythcQoqqpYFIJq6cPLsyd6FQ95uLME1j53Tv2vwbZ Bokjr61EznUhJ2BGbODM0xeRg1UsMDTfe4HOM/962P3x/ip4v/M3oIR8/nqHo6av6TE+ c5bOvP/2eMIsYPZlHXq8yrUj+1arbG+34qarnY43xbW3nnAJj7gl2inWp+uTjTjsg0zb yUuKW/IBqU/O1FPkaRqXdWSCCRD7CruZCECXX1s7r+52+zu32D/IEWSZNNS8cowYI/UD rdRb4pp/XvfLDiWCQDaZXaqEnMGFQXJ1JkjXCWXghk+FXBqjAP8PW4iWfxCS5aBdakXt 4jSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RNLpHMpz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l198si1110674oig.120.2020.02.21.09.57.08; Fri, 21 Feb 2020 09:57:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RNLpHMpz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726408AbgBUR4q (ORCPT + 99 others); Fri, 21 Feb 2020 12:56:46 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:33091 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725947AbgBUR4q (ORCPT ); Fri, 21 Feb 2020 12:56:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582307805; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=88Nxd/8ku9aw8ZzcS+9T+V3Q+Oha7xL17ZgefCeO0Ts=; b=RNLpHMpzDpi4ErEQwHrwo/aAMbT0DYaBaT+zpVdZ7sGSSlyEfO3uPcqromTBJ0qdZovR2k t8Pl3sLWriWQjemyt+VTey5cihWpjXDh+czrL9LZSX7Uwb9tCiSU4qohybILs8Wau6pEr+ vXnnAeaKl4Pg8LATcfnx3vu6IgP0SG0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-1-kK2a9-HfNzq8c8Z5J2Azsg-1; Fri, 21 Feb 2020 12:56:39 -0500 X-MC-Unique: kK2a9-HfNzq8c8Z5J2Azsg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C7B60107ACC9; Fri, 21 Feb 2020 17:56:37 +0000 (UTC) Received: from agerstmayr-thinkpad.redhat.com (ovpn-204-47.brq.redhat.com [10.40.204.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id E4BE3790CF; Fri, 21 Feb 2020 17:56:29 +0000 (UTC) From: Andreas Gerstmayr To: linux-kernel@vger.kernel.org Cc: kabbott@redhat.com, skozina@redhat.com, mpetlan@redhat.com, nathans@redhat.com, mgoodwin@redhat.com, linux-perf-users@vger.kernel.org, bgregg@netflix.com, mspier@netflix.com, Andreas Gerstmayr , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim Subject: [RFC] perf script: add flamegraph.py script Date: Fri, 21 Feb 2020 18:55:01 +0100 Message-Id: <20200221175500.83774-1-agerstmayr@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This script works in tandem with d3-flame-graph to generate flame graphs from perf. It supports two output formats: JSON and HTML (the default). The HTML format will look for a standalone d3-flame-graph template file i= n /usr/share/d3-flame-graph/template.html and fill in the collected stacks. Usage: perf script flamegraph -a -F 99 sleep 60 Alternative usage: perf record -a -g -F 99 sleep 60 perf script report flamegraph Signed-off-by: Andreas Gerstmayr Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Arnaldo Carvalho de Melo Cc: Mark Rutland Cc: Alexander Shishkin Cc: Jiri Olsa Cc: Namhyung Kim --- I'm currently preparing packages for d3-flame-graph. For Fedora, the copr at https://copr.fedorainfracloud.org/coprs/agerstmayr/reviews/package/js-d3-= flame-graph/ can be installed, or alternatively the prebuilt standalone d3-flame-graph template can be downloaded from https://raw.githubusercontent.com/andreasgerstmayr/specs/master/reviews/j= s-d3-flame-graph/template.html and moved into /usr/share/d3-flame-graph/template.html .../perf/scripts/python/bin/flamegraph-record | 2 + .../perf/scripts/python/bin/flamegraph-report | 3 + tools/perf/scripts/python/flamegraph.py | 117 ++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100755 tools/perf/scripts/python/bin/flamegraph-record create mode 100755 tools/perf/scripts/python/bin/flamegraph-report create mode 100755 tools/perf/scripts/python/flamegraph.py diff --git a/tools/perf/scripts/python/bin/flamegraph-record b/tools/perf= /scripts/python/bin/flamegraph-record new file mode 100755 index 000000000000..725d66e71570 --- /dev/null +++ b/tools/perf/scripts/python/bin/flamegraph-record @@ -0,0 +1,2 @@ +#!/usr/bin/sh +perf record -g "$@" diff --git a/tools/perf/scripts/python/bin/flamegraph-report b/tools/perf= /scripts/python/bin/flamegraph-report new file mode 100755 index 000000000000..b1a79afd903b --- /dev/null +++ b/tools/perf/scripts/python/bin/flamegraph-report @@ -0,0 +1,3 @@ +#!/usr/bin/sh +# description: create flame graphs +perf script -s "$PERF_EXEC_PATH"/scripts/python/flamegraph.py -- "$@" diff --git a/tools/perf/scripts/python/flamegraph.py b/tools/perf/scripts= /python/flamegraph.py new file mode 100755 index 000000000000..2e9139ef2c4a --- /dev/null +++ b/tools/perf/scripts/python/flamegraph.py @@ -0,0 +1,117 @@ +# flamegraph.py - create flame graphs from perf samples +# SPDX-License-Identifier: GPL-2.0 +# +# Usage: +# +# perf record -a -g -F 99 sleep 60 +# perf script report flamegraph +# +# Combined data collection and flamegraph generation: +# +# perf script flamegraph -a -F 99 sleep 60 +# +# Written by Andreas Gerstmayr +# Flame Graphs invented by Brendan Gregg +# Works in tandem with d3-flame-graph by Martin Spier + +import sys +import os +import argparse +import json + + +class Node: + def __init__(self, name, libtype=3D""): + self.name =3D name + self.libtype =3D libtype + self.value =3D 0 + self.children =3D [] + + +class FlameGraphCLI: + def __init__(self, args): + self.args =3D args + self.stack =3D Node("root") + + if self.args.format =3D=3D "html" and \ + not os.path.isfile(self.args.template): + print(f"Flame Graph template '{self.args.template}' does not= " + + f"exist. Please install the d3-flame-graph package, " = + + f"specify an existing flame graph template " + + f"(--template PATH) or another output format " + + f"(--format FORMAT).", file=3Dsys.stderr) + sys.exit(1) + + def find_or_create_node(self, node, name, dso): + libtype =3D "kernel" if dso =3D=3D "[kernel.kallsyms]" else "" + if name is None: + name =3D "[unknown]" + + for child in node.children: + if child.name =3D=3D name and child.libtype =3D=3D libtype: + return child + + child =3D Node(name, libtype) + node.children.append(child) + return child + + def process_event(self, event): + node =3D self.find_or_create_node(self.stack, event["comm"], Non= e) + if "callchain" in event: + for entry in reversed(event['callchain']): + node =3D self.find_or_create_node( + node, entry.get("sym", {}).get("name"), event.get("d= so")) + else: + node =3D self.find_or_create_node( + node, entry.get("symbol"), event.get("dso")) + node.value +=3D 1 + + def trace_end(self): + def encoder(x): return x.__dict__ + json_str =3D json.dumps(self.stack, default=3Dencoder, + indent=3Dself.args.indent) + + if self.args.format =3D=3D "html": + try: + with open(self.args.template) as f: + output_str =3D f.read().replace("/** @flamegraph_par= ams **/", + json_str) + except IOError as e: + print(f"Error reading template file: {e}", file=3Dsys.st= derr) + sys.exit(1) + output_fn =3D self.args.output or "flamegraph.html" + else: + output_str =3D json_str + output_fn =3D self.args.output or "stacks.json" + + if output_fn =3D=3D "-": + sys.stdout.write(output_str) + else: + try: + with open(output_fn, "w") as out: + out.write(output_str) + except IOError as e: + print(f"Error writing output file: {e}", file=3Dsys.stde= rr) + sys.exit(1) + + +if __name__ =3D=3D "__main__": + parser =3D argparse.ArgumentParser(description=3D"Create flame graph= s.") + parser.add_argument("-F", "--format", + default=3D"html", choices=3D["json", "html"], + help=3D"output file format") + parser.add_argument("-o", "--output", + help=3D"output file name") + parser.add_argument("--indent", + type=3Dint, help=3D"JSON indentation") + parser.add_argument("--template", + default=3D"/usr/share/d3-flame-graph/template.ht= ml", + help=3D"path to flamegraph HTML template") + parser.add_argument("-i", "--input", + help=3Dargparse.SUPPRESS) + + args =3D parser.parse_args() + cli =3D FlameGraphCLI(args) + + process_event =3D cli.process_event + trace_end =3D cli.trace_end --=20 2.24.1