Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp800332ybb; Fri, 20 Mar 2020 08:17:00 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtBU/7LR8ScdCTNuDnPwoOex1+77wzezuh8eraC5CqADHFTnHe4ZbmyEskQo9aHrpBmSLwG X-Received: by 2002:aca:646:: with SMTP id 67mr6612903oig.4.1584717420015; Fri, 20 Mar 2020 08:17:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584717420; cv=none; d=google.com; s=arc-20160816; b=BtJnrcIQiB2+LEoE9wJGfIYY3pOV987GQANSFGDLwhtMrLShXhJW+VQs6fzSSp6Txb FfsyIArXlw46pDAp0bSoFFQnnbA8IMr/lSVEXg8gOekySXQZpB15RkXDnKbnYlbzugRb aXjL1KXPOCGSuK1FxZWDAac6i0rH4Chd4y4IUk1iR/0cIjdpBosx08L4NZuN/3skcUCd 7dvnBdqmZethUpJKwrvDrDAaScpWl69fmADZjiYv2WUxyw74Pqr7TG0xalTPgtoFWjSe HNldXvvDdgpVREFnzCRptMN7jok1LqgjrhFqJmmk5xrCl4kSUPEC+06VagV/vR1Klb9U XhHA== 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=NT3xX5x+CjX4pcqlJWLCY2rQSLIhaPt3fjn8rQbGRss=; b=FaOZzVKWNnDabO3S1UGbYmtuekF/4u/Ls9evfs7+aZQAG0+zdWo7t0FKcsttY1tUwP IFNqA+W+1iKqzXE1wx4Pav1t/ReI5y4+pIszQ4nJvp1hTYSCCGxv2cIpgg77Z50/jri4 rdouloIuX6xVm0pHdfHdiOIS3lXHgSY7ZgoHNOdHEsT/1emNLLLnhM6Bos2S6e2ggSQ4 9FNJU2vdliQCRwNUtuv/+HBQN+dr6V+7rtI8ksARgPxdO9eb2ykN3su4l+sCObckLKEi wNoYZ0yay0N+hg8XuslnLKUFyZkYdt84+PcszZPgtWrajM72pM4NjL38bi8VZ/VLMxWD u6FQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=SJbaTv5Q; 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 v7si3095888otp.43.2020.03.20.08.16.38; Fri, 20 Mar 2020 08:17:00 -0700 (PDT) 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=SJbaTv5Q; 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 S1727049AbgCTPOl (ORCPT + 99 others); Fri, 20 Mar 2020 11:14:41 -0400 Received: from us-smtp-delivery-74.mimecast.com ([216.205.24.74]:42302 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726801AbgCTPOl (ORCPT ); Fri, 20 Mar 2020 11:14:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584717279; 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=NT3xX5x+CjX4pcqlJWLCY2rQSLIhaPt3fjn8rQbGRss=; b=SJbaTv5QaOdEI8eGnOHXfsYeBAYmBWxyOx65iqJZoJxPz2yPqsNImqVuu4ru0/8zqWFz7P IoIh/02R40dk5n/j7CePuIaZ18YKCbZOmPnrshLpIwMZR/ciRNFMkzLNN8aeQKg71HC1pz Cet6ZPatko0/Yjt+PKRvtU+Gh3f8j4s= 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-301-0RRoD2XfOCGWa_G0s06Ubw-1; Fri, 20 Mar 2020 11:14:34 -0400 X-MC-Unique: 0RRoD2XfOCGWa_G0s06Ubw-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 7409118B642C; Fri, 20 Mar 2020 15:14:12 +0000 (UTC) Received: from agerstmayr-thinkpad.redhat.com (unknown [10.40.193.31]) by smtp.corp.redhat.com (Postfix) with ESMTP id D853673879; Fri, 20 Mar 2020 15:14:06 +0000 (UTC) From: Andreas Gerstmayr To: linux-perf-users@vger.kernel.org Cc: Martin Spier , Brendan Gregg , Andreas Gerstmayr , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , linux-kernel@vger.kernel.org Subject: [PATCH] perf script: add flamegraph.py script Date: Fri, 20 Mar 2020 16:13:48 +0100 Message-Id: <20200320151355.66302-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/d3-flamegraph-base.html and fill in the collect= ed stacks. Usage: perf record -a -g -F 99 sleep 60 perf script report flamegraph Combined: perf script flamegraph -a -F 99 sleep 60 Signed-off-by: Andreas Gerstmayr --- Package for d3-flame-graph is currently under review, in the meantime you= can=20 download the template from: https://cdn.jsdelivr.net/npm/d3-flame-graph@3.0.2/dist/templates/d3-flame= graph-base.html and move it to /usr/share/d3-flame-graph/d3-flamegraph-base.html .../perf/scripts/python/bin/flamegraph-record | 2 + .../perf/scripts/python/bin/flamegraph-report | 3 + tools/perf/scripts/python/flamegraph.py | 126 ++++++++++++++++++ 3 files changed, 131 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..5835d190ca42 --- /dev/null +++ b/tools/perf/scripts/python/flamegraph.py @@ -0,0 +1,126 @@ +# 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: +# +# 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 [] + + def toJSON(self): + return { + "n": self.name, + "l": self.libtype, + "v": self.value, + "c": self.children + } + + +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 js-d3-flame-graph (RPM) or= " + + f"libjs-d3-flame-graph (deb) package, specify an " + + f"existing flame graph template (--template PATH) or "= + + f"another output format (--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): + json_str =3D json.dumps(self.stack, default=3Dlambda x: x.toJSON= (), + 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_jso= n **/", + 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: + print(f"dumping data to {output_fn}") + 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/d3-flamegra= ph-base.html", + 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.25.1