From 5cd1753e22f2fdfcb7775de1c9efdd5e7ec6e223 Mon Sep 17 00:00:00 2001 From: Andre da Silva Date: Wed, 25 Mar 2026 00:24:49 -0300 Subject: [PATCH] Add profiling_libunwind feature Configure jemalloc with --enable-prof-libunwind to use libunwind for heap profiling backtraces. This avoids a known livelock in gcc's _Unwind_Backtrace in multi-threaded programs (jemalloc/jemalloc#2282). The feature implies profiling since jemalloc requires --enable-prof alongside --enable-prof-libunwind. On Linux, the build emits cargo:rustc-link-lib=unwind. On Apple platforms no extra link is needed since unwind symbols are part of libSystem. Closes tikv/jemallocator#146 Signed-off-by: Andre da Silva --- jemalloc-ctl/Cargo.toml | 1 + jemalloc-sys/Cargo.toml | 1 + jemalloc-sys/README.md | 9 +++++++++ jemalloc-sys/build.rs | 11 +++++++++++ jemallocator/Cargo.toml | 1 + 5 files changed, 23 insertions(+) diff --git a/jemalloc-ctl/Cargo.toml b/jemalloc-ctl/Cargo.toml index 90a7ed8e1..5eb49e178 100644 --- a/jemalloc-ctl/Cargo.toml +++ b/jemalloc-ctl/Cargo.toml @@ -37,6 +37,7 @@ tikv-jemallocator = { path = "../jemallocator", version = "0.6.1" } default = [] stats = ["tikv-jemalloc-sys/stats"] profiling = ["tikv-jemalloc-sys/profiling"] +profiling_libunwind = ["tikv-jemalloc-sys/profiling_libunwind", "profiling"] use_std = [ "libc/use_std" ] disable_initial_exec_tls = ["tikv-jemalloc-sys/disable_initial_exec_tls"] diff --git a/jemalloc-sys/Cargo.toml b/jemalloc-sys/Cargo.toml index 6b3efc0a9..23fb8fcb8 100644 --- a/jemalloc-sys/Cargo.toml +++ b/jemalloc-sys/Cargo.toml @@ -41,6 +41,7 @@ cc = "^1.0.13" [features] default = ["background_threads_runtime_support"] profiling = [] +profiling_libunwind = ["profiling"] debug = [] background_threads_runtime_support = [] background_threads = [ "background_threads_runtime_support" ] diff --git a/jemalloc-sys/README.md b/jemalloc-sys/README.md index db3103bd4..fdd69a24e 100644 --- a/jemalloc-sys/README.md +++ b/jemalloc-sys/README.md @@ -45,6 +45,15 @@ This crate provides following cargo feature flags: * `libgcc` (unless --disable-prof-libgcc) * `gcc intrinsics` (unless --disable-prof-gcc) +* `profiling_libunwind` (configure `jemalloc` with `--enable-prof-libunwind`): + Force jemalloc to use `libunwind` for backtracing during heap profiling + instead of the default gcc-based unwinding, which has a + [known livelock bug](https://github.com/jemalloc/jemalloc/issues/2282) in + multi-threaded programs using `_Unwind_Backtrace`. Enables `profiling` + automatically. On Linux, this requires `libunwind-dev` (or `libunwind-devel`) + to be installed. On macOS/iOS, unwind symbols are provided by the system and + no extra library is needed. + * `stats` (configure `jemalloc` with `--enable-stats`): Enable statistics gathering functionality. See the `jemalloc`'s "`opt.stats_print`" option documentation for usage details. diff --git a/jemalloc-sys/build.rs b/jemalloc-sys/build.rs index 3841d6bb6..d7e11f4fd 100644 --- a/jemalloc-sys/build.rs +++ b/jemalloc-sys/build.rs @@ -284,6 +284,17 @@ fn main() { cmd.arg("--enable-prof"); } + if env::var("CARGO_FEATURE_PROFILING_LIBUNWIND").is_ok() { + info!("CARGO_FEATURE_PROFILING_LIBUNWIND set"); + cmd.arg("--enable-prof-libunwind"); + // On Apple platforms unwind symbols live in libSystem, and on + // Windows libunwind is not available. Everywhere else (Linux, + // FreeBSD, etc.) we need to link it explicitly. + if !target.contains("apple") && !target.contains("windows") { + println!("cargo:rustc-link-lib=unwind"); + } + } + if env::var("CARGO_FEATURE_STATS").is_ok() { info!("CARGO_FEATURE_STATS set"); cmd.arg("--enable-stats"); diff --git a/jemallocator/Cargo.toml b/jemallocator/Cargo.toml index a2d01df57..90ca38763 100644 --- a/jemallocator/Cargo.toml +++ b/jemallocator/Cargo.toml @@ -48,6 +48,7 @@ tikv-jemalloc-ctl = { path = "../jemalloc-ctl", version = "0.6.1" } default = ["background_threads_runtime_support"] alloc_trait = [] profiling = ["tikv-jemalloc-sys/profiling"] +profiling_libunwind = ["tikv-jemalloc-sys/profiling_libunwind", "profiling"] debug = ["tikv-jemalloc-sys/debug"] stats = ["tikv-jemalloc-sys/stats"] background_threads_runtime_support = ["tikv-jemalloc-sys/background_threads_runtime_support"]