From b597accd4b8c21c1f7975b269876ef187037b46d Mon Sep 17 00:00:00 2001 From: Zelda Hessler Date: Mon, 4 Aug 2025 14:57:38 -0500 Subject: [PATCH 1/4] enable jemalloc on MacOS --- apollo-router/Cargo.toml | 2 +- apollo-router/src/axum_factory/axum_http_server_factory.rs | 6 +++--- apollo-router/src/axum_factory/metrics.rs | 2 +- apollo-router/src/executable.rs | 2 +- apollo-router/tests/integration/metrics.rs | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/apollo-router/Cargo.toml b/apollo-router/Cargo.toml index 3c9601af47..df3684192b 100644 --- a/apollo-router/Cargo.toml +++ b/apollo-router/Cargo.toml @@ -295,7 +295,7 @@ hyperlocal = { version = "0.9.1", default-features = false, features = [ "client", ] } -[target.'cfg(target_os = "linux")'.dependencies] +[target.'cfg(any(macos, target_os = "linux"))'.dependencies] tikv-jemallocator = { version = "0.6.0", optional = true } tikv-jemalloc-ctl = { version = "0.6.0", features = ["stats"], optional = true } diff --git a/apollo-router/src/axum_factory/axum_http_server_factory.rs b/apollo-router/src/axum_factory/axum_http_server_factory.rs index e21a3a51a5..e330aec5b4 100644 --- a/apollo-router/src/axum_factory/axum_http_server_factory.rs +++ b/apollo-router/src/axum_factory/axum_http_server_factory.rs @@ -84,7 +84,7 @@ fn session_count_instrument() -> ObservableGauge { #[cfg(all( feature = "global-allocator", not(feature = "dhat-heap"), - target_os = "linux" + any(target_os = "linux", target_os = "macos") ))] fn jemalloc_metrics_instruments() -> (tokio::task::JoinHandle<()>, Vec>) { use crate::axum_factory::metrics::jemalloc; @@ -506,7 +506,7 @@ where #[cfg(all( feature = "global-allocator", not(feature = "dhat-heap"), - target_os = "linux" + any(target_os = "linux", target_os = "macos") ))] let (_epoch_advance_loop, jemalloc_instrument) = jemalloc_metrics_instruments(); // Tie the lifetime of the various instruments to the lifetime of the router @@ -516,7 +516,7 @@ where #[cfg(all( feature = "global-allocator", not(feature = "dhat-heap"), - target_os = "linux" + any(target_os = "linux", target_os = "macos") ))] let _jemalloc_instrument = &jemalloc_instrument; service diff --git a/apollo-router/src/axum_factory/metrics.rs b/apollo-router/src/axum_factory/metrics.rs index 1e97b78c5b..52576997d4 100644 --- a/apollo-router/src/axum_factory/metrics.rs +++ b/apollo-router/src/axum_factory/metrics.rs @@ -1,7 +1,7 @@ #[cfg(all( feature = "global-allocator", not(feature = "dhat-heap"), - target_os = "linux" + any(target_os = "linux", target_os = "macos") ))] pub(crate) mod jemalloc { use std::time::Duration; diff --git a/apollo-router/src/executable.rs b/apollo-router/src/executable.rs index b50f6a65d3..189bf9b15c 100644 --- a/apollo-router/src/executable.rs +++ b/apollo-router/src/executable.rs @@ -43,7 +43,7 @@ use crate::uplink::UplinkConfig; #[cfg(all( feature = "global-allocator", not(feature = "dhat-heap"), - target_os = "linux" + any(target_os = "linux", target_os = "macos") ))] #[global_allocator] static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; diff --git a/apollo-router/tests/integration/metrics.rs b/apollo-router/tests/integration/metrics.rs index b15be00a90..21f23b1f22 100644 --- a/apollo-router/tests/integration/metrics.rs +++ b/apollo-router/tests/integration/metrics.rs @@ -1,7 +1,7 @@ #[cfg(all( feature = "global-allocator", not(feature = "dhat-heap"), - target_os = "linux" + any(target_os = "linux", target_os = "macos") ))] #[tokio::test(flavor = "multi_thread")] async fn test_jemalloc_metrics_are_emitted() { From eba6f7f44190a5a402eb61fb760d63e3e9c294c0 Mon Sep 17 00:00:00 2001 From: Zelda Hessler Date: Tue, 5 Aug 2025 08:59:10 -0500 Subject: [PATCH 2/4] update deps --- apollo-router/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apollo-router/Cargo.toml b/apollo-router/Cargo.toml index df3684192b..79441b1ef6 100644 --- a/apollo-router/Cargo.toml +++ b/apollo-router/Cargo.toml @@ -295,7 +295,7 @@ hyperlocal = { version = "0.9.1", default-features = false, features = [ "client", ] } -[target.'cfg(any(macos, target_os = "linux"))'.dependencies] +[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies] tikv-jemallocator = { version = "0.6.0", optional = true } tikv-jemalloc-ctl = { version = "0.6.0", features = ["stats"], optional = true } From 2ed8fb03abe4b96980a5f7386d2f7c2688c9aa34 Mon Sep 17 00:00:00 2001 From: Zelda Hessler Date: Tue, 5 Aug 2025 09:34:19 -0500 Subject: [PATCH 3/4] just target unix instead --- apollo-router/Cargo.toml | 11 +++-------- .../axum_factory/axum_http_server_factory.rs | 18 +++--------------- apollo-router/src/axum_factory/metrics.rs | 6 +----- apollo-router/src/executable.rs | 6 +----- apollo-router/tests/integration/metrics.rs | 6 +----- 5 files changed, 9 insertions(+), 38 deletions(-) diff --git a/apollo-router/Cargo.toml b/apollo-router/Cargo.toml index 79441b1ef6..ee0cb29657 100644 --- a/apollo-router/Cargo.toml +++ b/apollo-router/Cargo.toml @@ -34,7 +34,7 @@ default = ["global-allocator"] # [dependencies] # apollo-router = {version = "1.20", default-features = false} # ``` -global-allocator = ["dep:tikv-jemallocator", "tikv-jemalloc-ctl/stats"] +global-allocator = ["tikv-jemallocator/profiling", "tikv-jemalloc-ctl/stats", "tikv-jemalloc-ctl/profiling"] # if you are doing heap profiling dhat-heap = ["dhat"] @@ -286,18 +286,13 @@ log = "0.4.22" scopeguard = "1.2.0" chrono = "0.4.41" -[target.'cfg(macos)'.dependencies] -uname = "0.1.1" - [target.'cfg(unix)'.dependencies] uname = "0.1.1" hyperlocal = { version = "0.9.1", default-features = false, features = [ "client", ] } - -[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies] -tikv-jemallocator = { version = "0.6.0", optional = true } -tikv-jemalloc-ctl = { version = "0.6.0", features = ["stats"], optional = true } +tikv-jemallocator = { version = "0.6.0", features = ["profiling"], optional = true } +tikv-jemalloc-ctl = { version = "0.6.0", features = ["stats", "profiling"], optional = true } [dev-dependencies] axum = { version = "0.8.1", features = ["http2", "ws"] } diff --git a/apollo-router/src/axum_factory/axum_http_server_factory.rs b/apollo-router/src/axum_factory/axum_http_server_factory.rs index e330aec5b4..1825d747b3 100644 --- a/apollo-router/src/axum_factory/axum_http_server_factory.rs +++ b/apollo-router/src/axum_factory/axum_http_server_factory.rs @@ -81,11 +81,7 @@ fn session_count_instrument() -> ObservableGauge { .init() } -#[cfg(all( - feature = "global-allocator", - not(feature = "dhat-heap"), - any(target_os = "linux", target_os = "macos") -))] +#[cfg(all(feature = "global-allocator", not(feature = "dhat-heap"), unix))] fn jemalloc_metrics_instruments() -> (tokio::task::JoinHandle<()>, Vec>) { use crate::axum_factory::metrics::jemalloc; @@ -503,21 +499,13 @@ where experimental_log_on_broken_pipe: configuration.supergraph.experimental_log_on_broken_pipe, })); let session_count_instrument = session_count_instrument(); - #[cfg(all( - feature = "global-allocator", - not(feature = "dhat-heap"), - any(target_os = "linux", target_os = "macos") - ))] + #[cfg(all(feature = "global-allocator", not(feature = "dhat-heap"), unix))] let (_epoch_advance_loop, jemalloc_instrument) = jemalloc_metrics_instruments(); // Tie the lifetime of the various instruments to the lifetime of the router // by referencing them in a no-op layer. router = router.layer(layer_fn(move |service| { let _session_count_instrument = &session_count_instrument; - #[cfg(all( - feature = "global-allocator", - not(feature = "dhat-heap"), - any(target_os = "linux", target_os = "macos") - ))] + #[cfg(all(feature = "global-allocator", not(feature = "dhat-heap"), unix))] let _jemalloc_instrument = &jemalloc_instrument; service })); diff --git a/apollo-router/src/axum_factory/metrics.rs b/apollo-router/src/axum_factory/metrics.rs index 52576997d4..9f2d3ced3e 100644 --- a/apollo-router/src/axum_factory/metrics.rs +++ b/apollo-router/src/axum_factory/metrics.rs @@ -1,8 +1,4 @@ -#[cfg(all( - feature = "global-allocator", - not(feature = "dhat-heap"), - any(target_os = "linux", target_os = "macos") -))] +#[cfg(all(feature = "global-allocator", not(feature = "dhat-heap"), unix))] pub(crate) mod jemalloc { use std::time::Duration; diff --git a/apollo-router/src/executable.rs b/apollo-router/src/executable.rs index 189bf9b15c..b9a37e3265 100644 --- a/apollo-router/src/executable.rs +++ b/apollo-router/src/executable.rs @@ -40,11 +40,7 @@ use crate::router::ShutdownSource; use crate::uplink::Endpoints; use crate::uplink::UplinkConfig; -#[cfg(all( - feature = "global-allocator", - not(feature = "dhat-heap"), - any(target_os = "linux", target_os = "macos") -))] +#[cfg(all(feature = "global-allocator", not(feature = "dhat-heap"), unix))] #[global_allocator] static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; diff --git a/apollo-router/tests/integration/metrics.rs b/apollo-router/tests/integration/metrics.rs index 21f23b1f22..2a27665f66 100644 --- a/apollo-router/tests/integration/metrics.rs +++ b/apollo-router/tests/integration/metrics.rs @@ -1,8 +1,4 @@ -#[cfg(all( - feature = "global-allocator", - not(feature = "dhat-heap"), - any(target_os = "linux", target_os = "macos") -))] +#[cfg(all(feature = "global-allocator", not(feature = "dhat-heap"), unix))] #[tokio::test(flavor = "multi_thread")] async fn test_jemalloc_metrics_are_emitted() { use super::common::IntegrationTest; From db3b1dda4ce82856c2933f930d5e49265f2b8d14 Mon Sep 17 00:00:00 2001 From: Zelda Hessler Date: Tue, 5 Aug 2025 14:07:34 -0500 Subject: [PATCH 4/4] add changeset --- .changesets/feat_zelda_enable_jemalloc_on_macos.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changesets/feat_zelda_enable_jemalloc_on_macos.md diff --git a/.changesets/feat_zelda_enable_jemalloc_on_macos.md b/.changesets/feat_zelda_enable_jemalloc_on_macos.md new file mode 100644 index 0000000000..3605d162bc --- /dev/null +++ b/.changesets/feat_zelda_enable_jemalloc_on_macos.md @@ -0,0 +1,3 @@ +### Enable jemalloc on MacOS ([PR #8046](https://github.com/apollographql/router/pull/8046)) + +This PR enables the jemalloc allocator on MacOS by default. Previously, this was only done for Linux. We're making this change because it will make memory profiling easier than it currently is.