Fix malloc_utils features (sysmalloc) (#7770)

Follow-up to:

- https://github.com/sigp/lighthouse/pull/7764

The `heaptrack` feature added in my previous PR was ineffective, because the jemalloc feature was turned on by the Linux target-specific dependency.

This PR tweaks the features such that:

- The jemalloc feature is just used to control whether jemalloc is compiled in. It is enabled on Linux by the target-specific dependency (see `lighthouse/Cargo.toml`), and completely disabled on Windows.
- If the `sysmalloc` feature is set on Linux then it overrides jemalloc when selecting an allocator, _even if_ the jemalloc feature is enabled (and the jemalloc dep was compiled).
This commit is contained in:
Michael Sproul
2025-08-15 13:46:38 +10:00
committed by GitHub
parent 90fa7c216e
commit 317dc0f56c
8 changed files with 61 additions and 36 deletions

View File

@@ -4,10 +4,23 @@ version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = { workspace = true }
# Features are not rich enough to express the complexity of our defaults, so we choose to just
# use the jemalloc feature to control whether the dependency is compiled, but avoid using it if
# the `sysmalloc` feature is set.
#
# On Windows, setting the jemalloc feature will result in a compile-time error.
[features]
default = []
mallinfo2 = []
jemalloc = ["tikv-jemallocator", "tikv-jemalloc-ctl"]
# The jemalloc feature enables the compilation of jemalloc dependencies. Jemalloc is also the
# default allocator, unless `sysmalloc` is set.
#
# It should be turned off on Windows.
jemalloc = ["tikv-jemalloc-ctl", "tikv-jemallocator"]
jemalloc-profiling = ["tikv-jemallocator/profiling"]
# Force the use of system malloc (or glibc) rather than jemalloc.
# This is a no-op on Windows where jemalloc is always disabled.
sysmalloc = []
[dependencies]
libc = "0.2.79"

View File

@@ -25,28 +25,35 @@
//! functions, then try to compile with the `not_glibc_interface` module.
#[cfg(all(
any(feature = "sysmalloc", not(feature = "jemalloc")),
target_os = "linux",
not(target_env = "musl"),
not(feature = "jemalloc")
not(target_env = "musl")
))]
pub mod glibc;
#[cfg(feature = "jemalloc")]
#[cfg(all(unix, not(feature = "sysmalloc"), feature = "jemalloc"))]
pub mod jemalloc;
pub use interface::*;
// Glibc malloc is the default on non-musl Linux if the sysmalloc feature is enabled, or jemalloc
// is disabled.
#[cfg(all(
any(feature = "sysmalloc", not(feature = "jemalloc")),
target_os = "linux",
not(target_env = "musl"),
not(feature = "jemalloc")
not(target_env = "musl")
))]
mod interface {
pub use crate::glibc::configure_glibc_malloc as configure_memory_allocator;
pub use crate::glibc::scrape_mallinfo_metrics as scrape_allocator_metrics;
pub fn allocator_name() -> String {
"glibc".to_string()
}
}
#[cfg(feature = "jemalloc")]
// Jemalloc is the default on UNIX (including musl) unless the sysmalloc feature is enabled.
#[cfg(all(unix, not(feature = "sysmalloc"), feature = "jemalloc"))]
mod interface {
#[allow(dead_code)]
pub fn configure_memory_allocator() -> Result<(), String> {
@@ -54,11 +61,21 @@ mod interface {
}
pub use crate::jemalloc::scrape_jemalloc_metrics as scrape_allocator_metrics;
pub fn allocator_name() -> String {
match crate::jemalloc::page_size() {
Ok(page_size) => format!("jemalloc ({}K)", page_size / 1024),
Err(e) => format!("jemalloc (error: {e:?})"),
}
}
}
#[cfg(all(
any(not(target_os = "linux"), target_env = "musl"),
not(feature = "jemalloc")
#[cfg(any(
not(unix),
all(
any(feature = "sysmalloc", not(feature = "jemalloc")),
any(not(target_os = "linux"), target_env = "musl")
)
))]
mod interface {
#[allow(dead_code, clippy::unnecessary_wraps)]
@@ -68,4 +85,8 @@ mod interface {
#[allow(dead_code)]
pub fn scrape_allocator_metrics() {}
pub fn allocator_name() -> String {
"system".to_string()
}
}