Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp1456636rwb; Thu, 19 Jan 2023 10:52:07 -0800 (PST) X-Google-Smtp-Source: AMrXdXutdPvAnH/u0G+drWEcDYS7IhqHsGa7Aui7Km5Ld2EuE8JBfUdblqZ1m+iHsMhgO5wRikC1 X-Received: by 2002:a17:90b:3a89:b0:229:2e1:6bd3 with SMTP id om9-20020a17090b3a8900b0022902e16bd3mr12351815pjb.44.1674154327205; Thu, 19 Jan 2023 10:52:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674154327; cv=none; d=google.com; s=arc-20160816; b=z+jYa61eytf/L5g3IyY+sUpK/tJ5aOkwgKIHGGSXUz8yN9UyKDjaXIUzwNXT9ncYNg 6+BvAR6Ba3kPHCPPpv3uv3SGKoTVj6siLQYB53yXEq3pTQe/l3CpMTcpOyFTpNhCsYP1 hkOoxqkHLZowqHjCE6LFm/+g7Xfi8//r2h9qxZhzLpR9taiOoEfDyKfAt9xpA3P4o18d raNpA3OVBz3e/OIPGvH+ul7sQYTXQdUhsbqVwuK4JQ32U+dgfT4XFrjLyLpHzyocm6Y2 hbI9eG47q20YmtTBSwgJ2AHfg5KFapiwG7Bhod9cddkrpu8c9LBAigLR1AimXltDBr9U C16Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:mime-version:message-id:date :dkim-signature; bh=g5ejasQrFsU/5gVpFRFubfv/+PZvkyzMvOtZ1k9Ls2g=; b=avCaLVsuiWhEy5XJS5w+zM1GE7T4zsm+Md7zNhztFKEupe/brsxLcsyEFVpWtwn6yz rIUMjXwxr7+GFgpVMtZ0SRv4Xb1ea56A2DDaYjS5zGYYgXlAzu7YXe7wTrDyx8FC5c6I XxtnB6z5UI+mMvyub+pxxefr+6J6qMRLbGD3geaeFyig6U40XwegWM1ko1wpB1u4STmA IKz2UijMKFwbIxdwVTfbkZR2ujIQVXFaZ9kgPI2WRVs/u5pPeJtg9v+4DCTiNetMn5r1 XWMyw2wnynWocN5954uaWUkAw1PJJk0r++BxhB/+hNnvzC5dvT6EBE/NByI8snCsqYb6 Gs5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=b0YxZwON; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x6-20020a17090a970600b00225cb856740si5805615pjo.59.2023.01.19.10.52.01; Thu, 19 Jan 2023 10:52:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=b0YxZwON; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230043AbjASSbi (ORCPT + 45 others); Thu, 19 Jan 2023 13:31:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230113AbjASSbb (ORCPT ); Thu, 19 Jan 2023 13:31:31 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C4985CFC4 for ; Thu, 19 Jan 2023 10:31:30 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id om10-20020a17090b3a8a00b002299e350deaso3762798pjb.1 for ; Thu, 19 Jan 2023 10:31:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=to:from:subject:mime-version:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=g5ejasQrFsU/5gVpFRFubfv/+PZvkyzMvOtZ1k9Ls2g=; b=b0YxZwON3IV1/D404DMPS9jrW+M2J2WYkVE+iSbiN0STXINJePxujS9tiGUZi5i9G8 C36lUwVPqY7JFPAiKBudcJbQsi9SVQPM2IydPaO1zAjdnmHDpPWOJQlbCBYhMa7b+PqT X+vVrtxMoiSRxQpfs1FeU6yXXymOIcr5cKECICCm1n5GJrdSUfHzAAK1Xdgn9njEa7e2 AVyoCoEYej+TaYmBOLFNF7043pQxqtJjO0GbnE0Pec8l9KbB7gq7vJbXefZ5jIfs+/Px kIWoi5iZSo5VStr1tZ9lFx7c0/jR2Y2uzITBalGHkowv5JLcvJy1PWbvcdOVG7U7WH15 OyYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:from:subject:mime-version:message-id:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=g5ejasQrFsU/5gVpFRFubfv/+PZvkyzMvOtZ1k9Ls2g=; b=1dKZ6qRVvsMSDdELA0GNHnVF66I8BkEHKCIt+K89fYD4UWOEAAwxOL0QKAW1cTK1NN FhWWgRnhS0ht5kfWYfQgO+rXlM8ETgYbkXWdbAmFq31wxezQTCFxD2oisVrh58QpwLRE x/99IsNSoZXzfd37xUftFnSzr0bOk/Zx4hbOoXiGxWATZbDizSyILPDy741KAc7Lxr/a 52kaOlWus0H7OXZE3TbL9ht1W41F8xJj1Pt3JlUNJzAun99zT/sI81MwVwkx0IIYiHiI Lp8b8Jq33bz46XgVXO+sqOH3P7hJ7lN+Y3bunV7ukMIboTXyY+j23U8Pj67GBVHn7BtU 0dJQ== X-Gm-Message-State: AFqh2krL7lNwuorwxqqCGyxNhCFR6qgnYL//bbgyD6UGsY22/kDdW8Ns rGu0gxK6AqIuuk+NWAgRMJUmhV7xhRac X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:93c6:b65e:5f33:bc6b]) (user=irogers job=sendgmr) by 2002:a05:6a00:4c90:b0:58a:1e40:e474 with SMTP id eb16-20020a056a004c9000b0058a1e40e474mr1115087pfb.83.1674153089543; Thu, 19 Jan 2023 10:31:29 -0800 (PST) Date: Thu, 19 Jan 2023 10:31:16 -0800 Message-Id: <20230119183118.126387-1-irogers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.39.0.246.g2a6d74b583-goog Subject: [PATCH v3] perf script flamegraph: Avoid d3-flame-graph package dependency From: Ian Rogers To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Connor OBrien , Nathan Chancellor , Ian Rogers , Kumar Kartikeya Dwivedi , bpf@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently flame graph generation requires a d3-flame-graph template to be installed. Unfortunately this is hard to come by for things like Debian [1]. If the template isn't installed then ask if it should be downloaded from jsdelivr CDN. The downloaded HTML file is validated against an md5sum. If the download fails, generate a minimal flame graph with the javascript coming from links to jsdelivr CDN. v3. Adds a warning message and quits before download in live mode. v2. Change the warning to a prompt about downloading and add the --allow-download command line flag. Add an md5sum check for the downloaded HTML. [1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=996839 Signed-off-by: Ian Rogers --- tools/perf/scripts/python/flamegraph.py | 107 +++++++++++++++++++----- 1 file changed, 85 insertions(+), 22 deletions(-) diff --git a/tools/perf/scripts/python/flamegraph.py b/tools/perf/scripts/python/flamegraph.py index b6af1dd5f816..cf7ce8229a6c 100755 --- a/tools/perf/scripts/python/flamegraph.py +++ b/tools/perf/scripts/python/flamegraph.py @@ -19,12 +19,34 @@ # pylint: disable=missing-function-docstring from __future__ import print_function -import sys -import os -import io import argparse +import hashlib +import io import json +import os import subprocess +import sys +import urllib.request + +minimal_html = """ + + + +
+ + + + +""" # pylint: disable=too-few-public-methods class Node: @@ -50,16 +72,6 @@ class FlameGraphCLI: self.args = args self.stack = Node("all", "root") - if self.args.format == "html" and \ - not os.path.isfile(self.args.template): - print("Flame Graph template {} does not exist. Please install " - "the js-d3-flame-graph (RPM) or libjs-d3-flame-graph (deb) " - "package, specify an existing flame graph template " - "(--template PATH) or another output format " - "(--format FORMAT).".format(self.args.template), - file=sys.stderr) - sys.exit(1) - @staticmethod def get_libtype_from_dso(dso): """ @@ -128,16 +140,63 @@ class FlameGraphCLI: } options_json = json.dumps(options) + template_md5sum = None + if self.args.format == "html": + if os.path.isfile(self.args.template): + template = f"file://{self.args.template}" + else: + if not self.args.allow_download: + print(f"""Warning: Flame Graph template '{self.args.template}' +does not exist. To avoid this please install a package such as the +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 == "-": + 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 +template retry without live mode. For example, use 'perf record -a -g +-F 99 sleep 60' and 'perf script report flamegraph'. Alternatively, +download the template from: +https://cdn.jsdelivr.net/npm/d3-flame-graph@4.1.3/dist/templates/d3-flamegraph-base.html +and place it at: +/usr/share/d3-flame-graph/d3-flamegraph-base.html""", + file=sys.stderr) + quit() + s = None + while s != "y" and s != "n": + s = input("Do you wish to download a template from cdn.jsdelivr.net? (this warning can be suppressed with --allow-download) [yn] ").lower() + if s == "n": + quit() + template = "https://cdn.jsdelivr.net/npm/d3-flame-graph@4.1.3/dist/templates/d3-flamegraph-base.html" + template_md5sum = "143e0d06ba69b8370b9848dcd6ae3f36" + try: - with io.open(self.args.template, encoding="utf-8") as template: - output_str = ( - template.read() - .replace("/** @options_json **/", options_json) - .replace("/** @flamegraph_json **/", stacks_json) - ) - except IOError as err: - print("Error reading template file: {}".format(err), file=sys.stderr) - sys.exit(1) + with urllib.request.urlopen(template) as template: + output_str = "".join([ + l.decode("utf-8") for l in template.readlines() + ]) + except Exception as err: + print(f"Error reading template {template}: {err}\n" + "a minimal flame graph will be generated", file=sys.stderr) + output_str = minimal_html + template_md5sum = None + + if template_md5sum: + download_md5sum = hashlib.md5(output_str.encode("utf-8")).hexdigest() + if download_md5sum != template_md5sum: + s = None + while s != "y" and s != "n": + s = input(f"""Unexpected template md5sum. +{download_md5sum} != {template_md5sum}, for: +{output_str} +continue?[yn] """).lower() + if s == "n": + quit() + + output_str = output_str.replace("/** @options_json **/", options_json) + output_str = output_str.replace("/** @flamegraph_json **/", stacks_json) + output_fn = self.args.output or "flamegraph.html" else: output_str = stacks_json @@ -172,6 +231,10 @@ if __name__ == "__main__": choices=["blue-green", "orange"]) parser.add_argument("-i", "--input", help=argparse.SUPPRESS) + parser.add_argument("--allow-download", + default=False, + action="store_true", + help="allow unprompted downloading of HTML template") cli_args = parser.parse_args() cli = FlameGraphCLI(cli_args) -- 2.39.0.314.g84b9a713c41-goog