Skip to content

Commit

Permalink
alloc: add force-jemalloc-macos feature
Browse files Browse the repository at this point in the history
  • Loading branch information
guswynn committed May 10, 2024
1 parent 4668910 commit 58dc8d9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
36 changes: 22 additions & 14 deletions src/alloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,7 @@ workspace = true
[dependencies]
mz-ore = { path = "../ore", default-features = false }
workspace-hack = { version = "0.0.0", path = "../workspace-hack", optional = true }

# Disable jemalloc on macOS, as it is not well supported [0][1][2]. The issues
# present as runaway latency on load test workloads that are comfortably handled
# by the macOS system allocator. Consider re-evaluating if jemalloc's macOS
# support improves.
#
# [0]: https://github.com/jemalloc/jemalloc/issues/26
# [1]: https://github.com/jemalloc/jemalloc/issues/843
# [2]: https://github.com/jemalloc/jemalloc/issues/1467
#
# Furthermore, as of August 2022, some engineers are using profiling tools, e.g.
# `heaptrack`, that only work with the system allocator.
[target.'cfg(not(target_os = "macos"))'.dependencies]
mz-prof = { path = "../prof", default-features = false }
mz-prof-http = { path = "../prof-http", default-features = false }
# According to jemalloc developers, `background_threads` should always be
# enabled, except in "esoteric" situations that don't apply to Materialize
# (namely, if the application relies on new threads not being created for
Expand All @@ -35,10 +21,32 @@ mz-prof-http = { path = "../prof-http", default-features = false }
# See: https://github.com/jemalloc/jemalloc/issues/956#issuecomment-316224733
tikv-jemallocator = { version = "0.5.0", features = ["profiling", "stats", "unprefixed_malloc_on_supported_platforms", "background_threads"], optional = true }

[target.'cfg(not(target_os = "macos"))'.dependencies]
mz-prof-http = { path = "../prof-http", default-features = false }

[features]
default = ["workspace-hack"]
# Whether to enable the use of jemalloc on platforms that support it.
jemalloc = ["tikv-jemallocator", "mz-prof/jemalloc", "mz-prof-http/jemalloc"]
# By default disable jemalloc on macOS, as it is not well supported [0][1]. The issues
# present as runaway latency on load test workloads that are comfortably handled
# by the macOS system allocator. Consider re-evaluating if jemalloc's macOS
# support improves.
#
# [0]: https://github.com/jemalloc/jemalloc/issues/26
# [1]: https://github.com/jemalloc/jemalloc/issues/843
#
# Furthermore, as of August 2022, some engineers are using profiling tools, e.g.
# `heaptrack`, that only work with the system allocator.
#
# Note that profiling as provided by `mz-prof-http` is not enabled for macos, as it
# appears to not work at all.
#
# Unfortunately, there doesn't appear to be a way to tell cargo to only include the
# tikv-jemallocator dependency if `force-jemalloc-macos` is enabled AND the platform
# is macos (Cargo doesn't support all(feature, platform) in the `target.cfg()` fields. This
# means mac users have to build one more dep :(.
force-jemalloc-macos = []

[package.metadata.cargo-udeps.ignore]
# The only reason we depend on mz-prof-http
Expand Down
18 changes: 15 additions & 3 deletions src/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@

use mz_ore::metrics::MetricsRegistry;

#[cfg(all(not(target_os = "macos"), feature = "jemalloc", not(miri)))]
#[cfg(all(
any(feature = "force-jemalloc-macos", not(target_os = "macos")),
feature = "jemalloc",
not(miri)
))]
#[global_allocator]
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;

/// Registers metrics for the global allocator into the provided registry.
///
/// What metrics are registered varies by platform. Not all platforms use
/// allocators that support metrics.
#[cfg(any(target_os = "macos", not(feature = "jemalloc"), miri))]
#[cfg(any(
all(not(feature = "force-jemalloc-macos"), target_os = "macos"),
not(feature = "jemalloc"),
miri
))]
#[allow(clippy::unused_async)]
pub async fn register_metrics_into(_: &MetricsRegistry) {
// No-op on platforms that don't use jemalloc.
Expand All @@ -27,7 +35,11 @@ pub async fn register_metrics_into(_: &MetricsRegistry) {
///
/// What metrics are registered varies by platform. Not all platforms use
/// allocators that support metrics.
#[cfg(all(not(target_os = "macos"), feature = "jemalloc", not(miri)))]
#[cfg(all(
any(feature = "force-jemalloc-macos", not(target_os = "macos")),
feature = "jemalloc",
not(miri)
))]
pub async fn register_metrics_into(registry: &MetricsRegistry) {
mz_prof::jemalloc::JemallocMetrics::register_into(registry).await;
}
1 change: 1 addition & 0 deletions src/clusterd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ workspace-hack = { version = "0.0.0", path = "../workspace-hack" }
[features]
default = ["tokio-console", "jemalloc"]
jemalloc = ["mz-alloc/jemalloc"]
force-jemalloc-macos = ["mz-alloc/force-jemalloc-macos"]
tokio-console = ["mz-ore/tokio-console"]

[package.metadata.cargo-udeps.ignore]
Expand Down
1 change: 1 addition & 0 deletions src/environmentd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ default = ["tokio-console", "jemalloc"]
# access to the file system.
dev-web = []
jemalloc = ["mz-alloc/jemalloc"]
force-jemalloc-macos = ["mz-alloc/force-jemalloc-macos"]
test = [
"postgres",
"regex",
Expand Down

0 comments on commit 58dc8d9

Please sign in to comment.