From nobody Sat Feb 7 23:46:57 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 028A4C54EBE for ; Thu, 12 Jan 2023 22:10:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232844AbjALWKi (ORCPT ); Thu, 12 Jan 2023 17:10:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232693AbjALWKH (ORCPT ); Thu, 12 Jan 2023 17:10:07 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 628935AC6B for ; Thu, 12 Jan 2023 14:00:38 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id t13-20020a056902018d00b0074747131938so20788338ybh.12 for ; Thu, 12 Jan 2023 14:00:38 -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=hUFqBM73Q9lYPdMltK2ThCupgV9eep/CgOS8HVgc6fI=; b=GAdw1Ka7cnbJBk//dl51ObqXXMMvDDovB1M6ELGF0LEwS3tdMa09jt7nUW+Z1IVbOQ gQswghN2Hiez2FrURXEWkZCSk6DafmoNLtZFfDO2k0YytwL0xUYSNh6BwREORVGcUelk j+V2eyneWxjqEWQ7MxTjskkl5BGL4f5n0MJjUcBMW2Eqed9DY1+OEl7UWdQ8MqSos4ym mVLInsDDZOo7qpz+mM7NMbd2P8HQkPgMURuc0X4u5SEQIxk69lTrVdz9HJyfghPvtyLN Gcbr17/y+EU5MTwuUv5cXjJWkBltiyG6r1OU0lnO21VqZN74F3ht2MERDZNCAgBMhExN Wl/g== 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=hUFqBM73Q9lYPdMltK2ThCupgV9eep/CgOS8HVgc6fI=; b=WPQc6fRncmXCMX7mtloHFB74zyY41sExfszSVuQCJ8FZvtvsKLz6B1ICOB7Ma25CH8 Gho8f7fiM+DKDewJmG3Mro8TNLaxHhvzEwpOxJYWu22jmUeDTId91yB8QOoHN95VD2kz 6zOyWMt6Wqqj54+Mg03v4ybu5Xx37mUyp2XYFMOFi2XWrItjRodM2mjVMdmwXKmbzCQo 8Q4B/QF4Np1xTQOpJ7IVw4CkgLPYYzms18H3B7Aiy9ETwjruX7Bdjyhj77vUMEKMWZY2 6vsnEEYbVVap9nO8NUU9UAM5eOQQizm908Z5+9HwZewv0lQO7cWuim4yeFkiUApF6Bk+ hnOg== X-Gm-Message-State: AFqh2kpvaiTrHL+DWb9mIq/6nqlTHPfNuBR5+cKE5UIIIHavxTwBE4/q eHEB+Mtuf0QiBI79c04lxDcKHNkVhQ+R X-Google-Smtp-Source: AMrXdXu7a+gPhbmbHAxwKTFuua6NBcl4OGgBsjJcylZB/1GuBpnOuQHXwpPlkQFnObaoDsz47eX0okUgZ18g X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:fa4e:1fbc:95a:3ea6]) (user=irogers job=sendgmr) by 2002:a81:88c5:0:b0:3ad:436f:4e6d with SMTP id y188-20020a8188c5000000b003ad436f4e6dmr2361136ywf.412.1673560837616; Thu, 12 Jan 2023 14:00:37 -0800 (PST) Date: Thu, 12 Jan 2023 14:00:24 -0800 Message-Id: <20230112220024.32709-1-irogers@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Subject: [PATCH v2] 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. 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 --- tools/perf/scripts/python/flamegraph.py | 96 +++++++++++++++++++------ 1 file changed, 74 insertions(+), 22 deletions(-) diff --git a/tools/perf/scripts/python/flamegraph.py b/tools/perf/scripts/p= ython/flamegraph.py index b6af1dd5f816..086619053e4e 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,52 @@ 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) + 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 +220,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