diff --git a/Cargo.lock b/Cargo.lock index 2fd0640b562c9..04fa4fee98347 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14938,6 +14938,7 @@ dependencies = [ "nix 0.29.0", "polkadot-cli", "polkadot-core-primitives", + "polkadot-jemalloc-shim", "polkadot-node-core-pvf", "polkadot-node-core-pvf-common", "polkadot-node-core-pvf-execute-worker", @@ -14946,7 +14947,6 @@ dependencies = [ "substrate-build-script-utils", "substrate-rpc-client", "tempfile", - "tikv-jemallocator", "tokio", ] @@ -15217,6 +15217,17 @@ dependencies = [ "tracing-gum", ] +[[package]] +name = "polkadot-jemalloc-shim" +version = "1.0.0" +dependencies = [ + "polkadot-cli", + "polkadot-node-core-pvf", + "polkadot-node-core-pvf-prepare-worker", + "polkadot-overseer", + "tikv-jemallocator", +] + [[package]] name = "polkadot-network-bridge" version = "7.0.0" @@ -15880,6 +15891,7 @@ version = "0.1.0" dependencies = [ "assert_cmd", "color-eyre", + "polkadot-jemalloc-shim", "polkadot-omni-node-lib", "substrate-build-script-utils", ] @@ -16020,6 +16032,7 @@ dependencies = [ "parachains-common", "penpal-runtime", "people-westend-runtime", + "polkadot-jemalloc-shim", "polkadot-omni-node-lib", "sc-chain-spec", "sc-cli", @@ -16480,8 +16493,6 @@ dependencies = [ "polkadot-node-core-pvf", "polkadot-node-core-pvf-checker", "polkadot-node-core-pvf-common", - "polkadot-node-core-pvf-execute-worker", - "polkadot-node-core-pvf-prepare-worker", "polkadot-node-core-runtime-api", "polkadot-node-metrics", "polkadot-node-network-protocol", diff --git a/Cargo.toml b/Cargo.toml index 2c7f87d573f14..d89b3f17948c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -159,6 +159,7 @@ members = [ "polkadot/core-primitives", "polkadot/erasure-coding", "polkadot/erasure-coding/fuzzer", + "polkadot/jemalloc-shim", "polkadot/node/collation-generation", "polkadot/node/core/approval-voting", "polkadot/node/core/approval-voting-parallel", @@ -1115,6 +1116,7 @@ polkadot-core-primitives = { path = "polkadot/core-primitives", default-features polkadot-dispute-distribution = { path = "polkadot/node/network/dispute-distribution", default-features = false } polkadot-erasure-coding = { path = "polkadot/erasure-coding", default-features = false } polkadot-gossip-support = { path = "polkadot/node/network/gossip-support", default-features = false } +polkadot-jemalloc-shim = { path = "polkadot/jemalloc-shim" } polkadot-network-bridge = { path = "polkadot/node/network/bridge", default-features = false } polkadot-node-collation-generation = { path = "polkadot/node/collation-generation", default-features = false } polkadot-node-core-approval-voting = { path = "polkadot/node/core/approval-voting", default-features = false } diff --git a/cumulus/polkadot-omni-node/Cargo.toml b/cumulus/polkadot-omni-node/Cargo.toml index 4e942ebc41ab4..a21336899afb1 100644 --- a/cumulus/polkadot-omni-node/Cargo.toml +++ b/cumulus/polkadot-omni-node/Cargo.toml @@ -16,8 +16,12 @@ workspace = true color-eyre = { workspace = true } # Local +polkadot-jemalloc-shim = { workspace = true } polkadot-omni-node-lib = { workspace = true, features = ["rococo-native", "westend-native"] } +[target.'cfg(target_os = "linux")'.dependencies] +polkadot-jemalloc-shim = { workspace = true, features = ["jemalloc-allocator"] } + [dev-dependencies] assert_cmd = { workspace = true } @@ -26,6 +30,7 @@ substrate-build-script-utils = { workspace = true, default-features = true } [features] default = [] +jemalloc-allocator = ["polkadot-jemalloc-shim/jemalloc-allocator"] runtime-benchmarks = [ "polkadot-omni-node-lib/runtime-benchmarks", ] diff --git a/cumulus/polkadot-parachain/Cargo.toml b/cumulus/polkadot-parachain/Cargo.toml index e0e4ac573c9a7..8405a77c66e14 100644 --- a/cumulus/polkadot-parachain/Cargo.toml +++ b/cumulus/polkadot-parachain/Cargo.toml @@ -48,6 +48,7 @@ sp-genesis-builder = { workspace = true, default-features = true } sp-keyring = { workspace = true, default-features = true } # Polkadot +polkadot-jemalloc-shim = { workspace = true } xcm = { workspace = true, default-features = true } # Cumulus @@ -55,6 +56,9 @@ cumulus-client-consensus-aura = { workspace = true } cumulus-primitives-core = { workspace = true, default-features = true } yet-another-parachain-runtime = { workspace = true } +[target.'cfg(target_os = "linux")'.dependencies] +polkadot-jemalloc-shim = { workspace = true, features = ["jemalloc-allocator"] } + [dev-dependencies] assert_cmd = { workspace = true } @@ -63,6 +67,7 @@ substrate-build-script-utils = { workspace = true, default-features = true } [features] default = [] +jemalloc-allocator = ["polkadot-jemalloc-shim/jemalloc-allocator"] runtime-benchmarks = [ "cumulus-primitives-core/runtime-benchmarks", "parachains-common/runtime-benchmarks", diff --git a/polkadot/Cargo.toml b/polkadot/Cargo.toml index 74f821e112c79..e5f51feefaa0d 100644 --- a/polkadot/Cargo.toml +++ b/polkadot/Cargo.toml @@ -67,7 +67,7 @@ path = "src/bin/prepare-worker.rs" [dependencies] color-eyre = { workspace = true } -tikv-jemallocator = { optional = true, features = ["unprefixed_malloc_on_supported_platforms"], workspace = true } +polkadot-jemalloc-shim = { workspace = true } # Crates in our workspace, defined as dependencies so we can pass them feature flags. polkadot-cli = { features = ["rococo-native", "westend-native"], workspace = true, default-features = true } @@ -80,7 +80,7 @@ polkadot-node-core-pvf-common = { workspace = true, default-features = true } polkadot-node-core-pvf-execute-worker = { workspace = true, default-features = true } [target.'cfg(target_os = "linux")'.dependencies] -tikv-jemallocator = { workspace = true, features = ["unprefixed_malloc_on_supported_platforms"] } +polkadot-jemalloc-shim = { workspace = true, features = ["jemalloc-allocator"] } [dev-dependencies] assert_cmd = { workspace = true } @@ -99,13 +99,7 @@ try-runtime = ["polkadot-cli/try-runtime"] fast-runtime = ["polkadot-cli/fast-runtime"] runtime-metrics = ["polkadot-cli/runtime-metrics"] pyroscope = ["polkadot-cli/pyroscope"] -jemalloc-allocator = [ - "dep:tikv-jemallocator", - "polkadot-cli/jemalloc-allocator", - "polkadot-node-core-pvf-prepare-worker/jemalloc-allocator", - "polkadot-node-core-pvf/jemalloc-allocator", - "polkadot-overseer/jemalloc-allocator", -] +jemalloc-allocator = ["polkadot-jemalloc-shim/jemalloc-allocator"] # Generate the metadata hash needed for CheckMetadataHash # in the builtin test runtimes (westend and rococo). diff --git a/polkadot/jemalloc-shim/Cargo.toml b/polkadot/jemalloc-shim/Cargo.toml new file mode 100644 index 0000000000000..cb850b5603d4e --- /dev/null +++ b/polkadot/jemalloc-shim/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "polkadot-jemalloc-shim" +version = "1.0.0" +description = "Shim crate to enable jemalloc-allocator feature for polkadot crates and setting global allocator to jemalloc" +license.workspace = true +authors.workspace = true +edition.workspace = true +homepage.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[package.metadata.polkadot-sdk] +exclude-from-umbrella = true + +[dependencies] +polkadot-cli = { optional = true, workspace = true } +polkadot-node-core-pvf = { optional = true, workspace = true } +polkadot-node-core-pvf-prepare-worker = { optional = true, workspace = true } +polkadot-overseer = { optional = true, workspace = true } +tikv-jemallocator = { optional = true, features = ["unprefixed_malloc_on_supported_platforms"], workspace = true } + +[features] +jemalloc-allocator = [ + "dep:tikv-jemallocator", + "polkadot-cli/jemalloc-allocator", + "polkadot-node-core-pvf-prepare-worker/jemalloc-allocator", + "polkadot-node-core-pvf/jemalloc-allocator", + "polkadot-overseer/jemalloc-allocator", +] diff --git a/polkadot/jemalloc-shim/src/lib.rs b/polkadot/jemalloc-shim/src/lib.rs new file mode 100644 index 0000000000000..26ba12fcb070a --- /dev/null +++ b/polkadot/jemalloc-shim/src/lib.rs @@ -0,0 +1,26 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Shim crate to enable `jemalloc-allocator` feature for Polkadot crates. +//! +//! Because [there doesn't exist any easier way right now](https://github.com/rust-lang/cargo/issues/1197), we +//! need an entire crate to handle `jemalloc` enabling/disabling. This way we can enable it by +//! default on Linux, but have it optional on all other OSes. + +/// Sets the global allocator to `jemalloc` when the feature is enabled. +#[cfg(feature = "jemalloc-allocator")] +#[global_allocator] +static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; diff --git a/polkadot/node/core/pvf/execute-worker/Cargo.toml b/polkadot/node/core/pvf/execute-worker/Cargo.toml index 4df425dfd199b..726cde3243449 100644 --- a/polkadot/node/core/pvf/execute-worker/Cargo.toml +++ b/polkadot/node/core/pvf/execute-worker/Cargo.toml @@ -11,6 +11,9 @@ repository.workspace = true [lints] workspace = true +[package.metadata.polkadot-sdk] +exclude-from-umbrella = true + [dependencies] cfg-if = { workspace = true } cpu-time = { workspace = true } diff --git a/polkadot/node/core/pvf/prepare-worker/Cargo.toml b/polkadot/node/core/pvf/prepare-worker/Cargo.toml index cc32086360cd7..a96d20ab35691 100644 --- a/polkadot/node/core/pvf/prepare-worker/Cargo.toml +++ b/polkadot/node/core/pvf/prepare-worker/Cargo.toml @@ -11,6 +11,9 @@ repository.workspace = true [lints] workspace = true +[package.metadata.polkadot-sdk] +exclude-from-umbrella = true + [[bench]] name = "prepare_rococo_runtime" harness = false diff --git a/polkadot/src/main.rs b/polkadot/src/main.rs index 1a96bf8fb00f6..4a41e14d15611 100644 --- a/polkadot/src/main.rs +++ b/polkadot/src/main.rs @@ -20,12 +20,6 @@ use color_eyre::eyre; -/// Global allocator. Changing it to another allocator will require changing -/// `memory_stats::MemoryAllocationTracker`. -#[cfg(any(target_os = "linux", feature = "jemalloc-allocator"))] -#[global_allocator] -static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; - fn main() -> eyre::Result<()> { color_eyre::install()?; polkadot_cli::run()?; diff --git a/prdoc/pr_10709.prdoc b/prdoc/pr_10709.prdoc new file mode 100644 index 0000000000000..fd6139d771e77 --- /dev/null +++ b/prdoc/pr_10709.prdoc @@ -0,0 +1,10 @@ +title: Introduce a "jemalloc-shim" crate +doc: +- audience: Node Operator + description: |- + This crate basically serves as a hack to properly enable `jemalloc` on Linux and disable it on all other OSes by default. +crates: +- name: polkadot + bump: patch +- name: polkadot-jemalloc-shim + bump: patch diff --git a/umbrella/Cargo.toml b/umbrella/Cargo.toml index 6204e38f998b5..5fd44713422e9 100644 --- a/umbrella/Cargo.toml +++ b/umbrella/Cargo.toml @@ -912,8 +912,6 @@ node = [ "polkadot-node-core-pvf", "polkadot-node-core-pvf-checker", "polkadot-node-core-pvf-common", - "polkadot-node-core-pvf-execute-worker", - "polkadot-node-core-pvf-prepare-worker", "polkadot-node-core-runtime-api", "polkadot-node-metrics", "polkadot-node-network-protocol", @@ -2472,16 +2470,6 @@ default-features = false optional = true path = "../polkadot/node/core/pvf/common" -[dependencies.polkadot-node-core-pvf-execute-worker] -default-features = false -optional = true -path = "../polkadot/node/core/pvf/execute-worker" - -[dependencies.polkadot-node-core-pvf-prepare-worker] -default-features = false -optional = true -path = "../polkadot/node/core/pvf/prepare-worker" - [dependencies.polkadot-node-core-runtime-api] default-features = false optional = true diff --git a/umbrella/src/lib.rs b/umbrella/src/lib.rs index ae0336f9d59eb..a9d26a796038e 100644 --- a/umbrella/src/lib.rs +++ b/umbrella/src/lib.rs @@ -942,16 +942,6 @@ pub use polkadot_node_core_pvf_checker; #[cfg(feature = "polkadot-node-core-pvf-common")] pub use polkadot_node_core_pvf_common; -/// Polkadot crate that contains the logic for executing PVFs. Used by the -/// polkadot-execute-worker binary. -#[cfg(feature = "polkadot-node-core-pvf-execute-worker")] -pub use polkadot_node_core_pvf_execute_worker; - -/// Polkadot crate that contains the logic for preparing PVFs. Used by the -/// polkadot-prepare-worker binary. -#[cfg(feature = "polkadot-node-core-pvf-prepare-worker")] -pub use polkadot_node_core_pvf_prepare_worker; - /// Wrapper around the parachain-related runtime APIs. #[cfg(feature = "polkadot-node-core-runtime-api")] pub use polkadot_node_core_runtime_api;