about summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xbin/__dispatch.sh3
l---------bin/perf-flamegraph1
-rw-r--r--third_party/default.nix2
-rw-r--r--tools/perf-flamegraph.nix12
4 files changed, 18 insertions, 0 deletions
diff --git a/bin/__dispatch.sh b/bin/__dispatch.sh
index c0f662a81f..b265e2fd19 100755
--- a/bin/__dispatch.sh
+++ b/bin/__dispatch.sh
@@ -40,6 +40,9 @@ case "${TARGET_TOOL}" in
   ninja)
     attr="third_party.ninja"
     ;;
+  perf-flamegraph)
+    attr="tools.perf-flamegraph"
+    ;;
   rebuild-system)
     attr="ops.nixos.rebuild-system"
     ;;
diff --git a/bin/perf-flamegraph b/bin/perf-flamegraph
new file mode 120000
index 0000000000..8390ec9c96
--- /dev/null
+++ b/bin/perf-flamegraph
@@ -0,0 +1 @@
+__dispatch.sh
\ No newline at end of file
diff --git a/third_party/default.nix b/third_party/default.nix
index b0f2e1bd43..7d2f6cdaec 100644
--- a/third_party/default.nix
+++ b/third_party/default.nix
@@ -72,6 +72,7 @@ let
       fira
       fira-code
       fira-mono
+      flamegraph
       fontconfig
       freetype
       gettext
@@ -92,6 +93,7 @@ let
       kontemplate
       lib
       libredirect
+      linuxPackages
       luajit
       lutris
       makeFontsConf
diff --git a/tools/perf-flamegraph.nix b/tools/perf-flamegraph.nix
new file mode 100644
index 0000000000..fe5c9a1d59
--- /dev/null
+++ b/tools/perf-flamegraph.nix
@@ -0,0 +1,12 @@
+# Script that collects perf timing for the execution of a command and writes a
+# flamegraph to stdout
+{ pkgs, ... }:
+
+pkgs.writeShellScriptBin "perf-flamegraph" ''
+  set -euo pipefail
+
+  ${pkgs.linuxPackages.perf}/bin/perf record -g -a -F max "$@"
+  ${pkgs.linuxPackages.perf}/bin/perf script \
+    | ${pkgs.flamegraph}/bin/stackcollapse-perf.pl \
+    | ${pkgs.flamegraph}/bin/flamegraph.pl
+''