From 02ab460bc1d9f7f45cd037bbb448622315592e18 Mon Sep 17 00:00:00 2001 From: Hubert Bugaj Date: Fri, 15 May 2026 12:57:37 +0200 Subject: [PATCH] feat: add gperftools mise script --- Cargo.toml | 2 +- build.rs | 1 + mise-tasks/gperf/heap.sh | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100755 mise-tasks/gperf/heap.sh diff --git a/Cargo.toml b/Cargo.toml index 81bf54508ea0..b0a5562bdce3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -316,7 +316,7 @@ panic = "unwind" [profile.profiling] inherits = "dev" -opt-level = 0 +opt-level = 1 debug = true split-debuginfo = "unpacked" panic = "abort" diff --git a/build.rs b/build.rs index 79158d99d02f..a435c4191465 100644 --- a/build.rs +++ b/build.rs @@ -11,6 +11,7 @@ use std::{ fn main() { // Only needed when profiling Forest with `gperftools`. This might not work on all platforms. + println!("cargo:rerun-if-env-changed=FOREST_PROFILING_GPERFTOOLS_BUILD"); if is_env_truthy("FOREST_PROFILING_GPERFTOOLS_BUILD") { println!("cargo:rustc-link-lib=tcmalloc"); } diff --git a/mise-tasks/gperf/heap.sh b/mise-tasks/gperf/heap.sh new file mode 100755 index 000000000000..ca505d661e99 --- /dev/null +++ b/mise-tasks/gperf/heap.sh @@ -0,0 +1,39 @@ +#!/bin/bash +#MISE description="Build and heap-profile a Forest binary with gperftools. Usage: mise run gperf:heap [-- ]. Dumps to /tmp/gperfheap..prof.*" + +set -euo pipefail + +if [ $# -lt 1 ]; then + echo "usage: mise run gperf:heap [-- ]" >&2 + exit 2 +fi + +bin="$1" +shift +# Strip the `--` separator if mise forwarded it. +if [ "${1-}" = "--" ]; then + shift +fi + +# `FOREST_PROFILING_GPERFTOOLS_BUILD` toggles `cargo:rustc-link-lib=tcmalloc` in +# build.rs — without it, the binary won't be linked against gperftools and +# `HEAPPROFILE` is inert. +# `--message-format=json-render-diagnostics` reports the built executable path +# on stdout (respecting any `CARGO_TARGET_DIR` / `[build] target-dir` override); +# diagnostics still render on stderr. +exe=$( + FOREST_PROFILING_GPERFTOOLS_BUILD=1 cargo build \ + --no-default-features --features system-alloc --profile=profiling \ + --bin "$bin" --message-format=json-render-diagnostics | + sed -n 's/.*"executable":"\([^"]*\)".*/\1/p' | + tail -1 +) +if [ -z "$exe" ]; then + echo "could not locate built executable for $bin" >&2 + exit 1 +fi + +ulimit -n 8192 +HEAPPROFILE_USE_PID=t \ +HEAPPROFILE="/tmp/gperfheap.${bin}.prof" \ + "$exe" "$@"