From nobody Sat Feb 7 17:15:24 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 24ED7C004D4 for ; Wed, 18 Jan 2023 07:54:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229840AbjARHyI (ORCPT ); Wed, 18 Jan 2023 02:54:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229737AbjARHxY (ORCPT ); Wed, 18 Jan 2023 02:53:24 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E932F4FCE5 for ; Tue, 17 Jan 2023 23:24:26 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id k135-20020a25248d000000b007d689f92d6dso10809342ybk.22 for ; Tue, 17 Jan 2023 23:24:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:mime-version:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=g5ejasQrFsU/5gVpFRFubfv/+PZvkyzMvOtZ1k9Ls2g=; b=sg3ax46z3ITf8FSZP4v7C7ytnbVk7hIKtf93DfqvhuueGbIMI3vje4kCIkHKREXLTn s4sxOff7VissWFrsaAzuyDwqtHQJOt8dvzLjdWbCfWhoaCqkjwUW9mpZG+BXbM0Z53fc 4TgqJGE3LUjS37rdL2Rb9gXQ336AMMMimusx1NBHUHXE4fE3Z+LbZzanir3vUTT36WBV dwOUKsJYKA5L1nJvNB7uuW8DhzjzzzQwGq2ntcyzqWVRZ8nZ6L0G1uIAqbks4iDCeahX snD5rDXsoyg3i8w94uHsaFMg9kPaoMxPx/QxSRl/nZMNnPhNNOY0PRkfgFFQH6+4PMjd +kPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc: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=1vMfrbaiWsikOgbyl8MpAwv9ZYiDI2GOOIIr6HrBzLi1l1REK07V2br4Tq5gyuSrJe 5WqZkRZmeVtXHXNysftmELh6yGfKkkDFfksU8kNPeLBmH+ReSk8xxOWJAuBqsfsrKUVW xDDFEqPiqlzu842JCGZPFw56Q968Y5nY4tjEYrg/ipvYGV0XUld4j5x6kju3OBZjMoqi DnpAqlkdkB2JDyTeAhP56BQXqnEtGb1bKim4VWnUQFjHMqiMzTXm9WONp3QPhH8i9qPc YwOUHjAHgcHEo8NYXabf5K/XwQrq8q8GcifGkFHP/UhZNFTQQ5HOf5+d5linLqwod9L7 JmHQ== X-Gm-Message-State: AFqh2kpUrUVz42UYBamuzkJAUwjfdDQkR12lX2Y2Fx6blsrD9nB0G52v mJtWk68VneGnvwq6M9e0Yr2DaxJDDTyc X-Google-Smtp-Source: AMrXdXudsO9xpF3o4NoK3cPpdWKIYrYY2Om/KWhPomCdbcIY4lxaTSpifcV4AXwwjc93A1quEvO7d7CGh7z+ X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:6cc7:ef8b:a10c:4c81]) (user=irogers job=sendgmr) by 2002:a81:cc2:0:b0:4db:3866:6fa6 with SMTP id 185-20020a810cc2000000b004db38666fa6mr716845ywm.44.1674026666209; Tue, 17 Jan 2023 23:24:26 -0800 (PST) Date: Tue, 17 Jan 2023 23:24:09 -0800 Message-Id: <20230118072409.147786-1-irogers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Subject: [PATCH v3] perf script flamegraph: Avoid d3-flame-graph package dependency From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, 996839@bugs.debian.org, Andreas Gerstmayr , Martin Spier , Brendan Gregg Cc: Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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=3D996839 Signed-off-by: Ian Rogers Reviewed-by: Andreas Gerstmayr --- 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/p= ython/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=3Dmissing-function-docstring =20 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 =3D """ + + + +
+ + + +""" =20 # pylint: disable=3Dtoo-few-public-methods class Node: @@ -50,16 +72,6 @@ class FlameGraphCLI: self.args =3D args self.stack =3D Node("all", "root") =20 - if self.args.format =3D=3D "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 (de= b) " - "package, specify an existing flame graph template " - "(--template PATH) or another output format " - "(--format FORMAT).".format(self.args.template), - file=3Dsys.stderr) - sys.exit(1) - @staticmethod def get_libtype_from_dso(dso): """ @@ -128,16 +140,63 @@ class FlameGraphCLI: } options_json =3D json.dumps(options) =20 + template_md5sum =3D None + if self.args.format =3D=3D "html": + if os.path.isfile(self.args.template): + template =3D f"file://{self.args.template}" + else: + if not self.args.allow_download: + print(f"""Warning: Flame Graph template '{self.arg= s.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=3Dsys.stderr) + if self.args.input =3D=3D "-": + print("""Not attempting to download Flame Grap= h 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-flameg= raph-base.html +and place it at: +/usr/share/d3-flame-graph/d3-flamegraph-base.html""", + file=3Dsys.stderr) + quit() + s =3D None + while s !=3D "y" and s !=3D "n": + s =3D input("Do you wish to download a templat= e from cdn.jsdelivr.net? (this warning can be suppressed with --allow-downl= oad) [yn] ").lower() + if s =3D=3D "n": + quit() + template =3D "https://cdn.jsdelivr.net/npm/d3-flame-gr= aph@4.1.3/dist/templates/d3-flamegraph-base.html" + template_md5sum =3D "143e0d06ba69b8370b9848dcd6ae3f36" + try: - with io.open(self.args.template, encoding=3D"utf-8") as te= mplate: - output_str =3D ( - template.read() - .replace("/** @options_json **/", options_json) - .replace("/** @flamegraph_json **/", stacks_json) - ) - except IOError as err: - print("Error reading template file: {}".format(err), file= =3Dsys.stderr) - sys.exit(1) + with urllib.request.urlopen(template) as template: + output_str =3D "".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=3Dsy= s.stderr) + output_str =3D minimal_html + template_md5sum =3D None + + if template_md5sum: + download_md5sum =3D hashlib.md5(output_str.encode("utf-8")= ).hexdigest() + if download_md5sum !=3D template_md5sum: + s =3D None + while s !=3D "y" and s !=3D "n": + s =3D input(f"""Unexpected template md5sum. +{download_md5sum} !=3D {template_md5sum}, for: +{output_str} +continue?[yn] """).lower() + if s =3D=3D "n": + quit() + + output_str =3D output_str.replace("/** @options_json **/", opt= ions_json) + output_str =3D output_str.replace("/** @flamegraph_json **/", = stacks_json) + output_fn =3D self.args.output or "flamegraph.html" else: output_str =3D stacks_json @@ -172,6 +231,10 @@ if __name__ =3D=3D "__main__": choices=3D["blue-green", "orange"]) parser.add_argument("-i", "--input", help=3Dargparse.SUPPRESS) + parser.add_argument("--allow-download", + default=3DFalse, + action=3D"store_true", + help=3D"allow unprompted downloading of HTML templ= ate") =20 cli_args =3D parser.parse_args() cli =3D FlameGraphCLI(cli_args) --=20 2.39.0.314.g84b9a713c41-goog