From d14055a019b7266a3ed38ddb19c018275a13f4d1 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Thu, 19 May 2022 17:39:13 -0700 Subject: feat(home): add a script to create flamegraph from a process This script (`perf-flamegraph-process') takes a process as an argument, and will run it with `perf' to capture the call graph, and will generate the flamegraph from it. The SVG is saved under `~/workspace/tmp/flamegraph'. --- home/default.nix | 1 + home/scripts/default.nix | 5 +++++ home/scripts/perf-flamegraph.nix | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 home/scripts/default.nix create mode 100644 home/scripts/perf-flamegraph.nix diff --git a/home/default.nix b/home/default.nix index 9d32671..485cc34 100644 --- a/home/default.nix +++ b/home/default.nix @@ -27,6 +27,7 @@ ./pcmanfm ./python ./scanner + ./scripts ./seahorse ./ssh ./sublime-music diff --git a/home/scripts/default.nix b/home/scripts/default.nix new file mode 100644 index 0000000..bc19e44 --- /dev/null +++ b/home/scripts/default.nix @@ -0,0 +1,5 @@ +{ ... }: + +{ + imports = [ ./perf-flamegraph.nix ]; +} diff --git a/home/scripts/perf-flamegraph.nix b/home/scripts/perf-flamegraph.nix new file mode 100644 index 0000000..f379591 --- /dev/null +++ b/home/scripts/perf-flamegraph.nix @@ -0,0 +1,22 @@ +{ pkgs, config, ... }: +let + perf-flamegraph-process = + pkgs.writeShellScriptBin "perf-flamegraph-process" '' + set -euo pipefail + + OUT_DIR="''${HOME}/workspace/tmp/flamegraph" + OUT_SVG="''${OUT_DIR}/$(date +%y%m%d-%H%M%S).svg" + + mkdir -p ''${OUT_DIR} + + ${pkgs.linuxPackages.perf}/bin/perf record -g --call-graph dwarf -F max "$@" + ${pkgs.linuxPackages.perf}/bin/perf script \ + | ${pkgs.flamegraph}/bin/stackcollapse-perf.pl \ + | ${pkgs.flamegraph}/bin/flamegraph.pl > "''${OUT_SVG}" + ''; +in { + config = { + home.packages = with pkgs; [ flamegraph perf-flamegraph-process ]; + }; +} + -- cgit 1.4.1