diff --git a/Cargo.lock b/Cargo.lock index ce386a3bc8fd4..d3fb2e54f9669 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15204,6 +15204,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", @@ -15212,7 +15213,6 @@ dependencies = [ "substrate-build-script-utils", "substrate-rpc-client", "tempfile", - "tikv-jemallocator", "tokio", ] @@ -15483,6 +15483,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 = "28.0.0" @@ -16146,6 +16157,7 @@ version = "0.13.0" dependencies = [ "assert_cmd", "color-eyre", + "polkadot-jemalloc-shim", "polkadot-omni-node-lib", "substrate-build-script-utils", ] @@ -16288,6 +16300,7 @@ dependencies = [ "penpal-runtime", "people-rococo-runtime", "people-westend-runtime", + "polkadot-jemalloc-shim", "polkadot-omni-node-lib", "rococo-parachain-runtime", "sc-chain-spec", @@ -16749,8 +16762,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 b8319c84feb0c..41d9391c8ab04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -168,6 +168,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", @@ -1129,6 +1130,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 34753c749eeca..a142e36e351be 100644 --- a/cumulus/polkadot-omni-node/Cargo.toml +++ b/cumulus/polkadot-omni-node/Cargo.toml @@ -14,8 +14,12 @@ workspace = true [dependencies] color-eyre = { workspace = true } +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 } @@ -25,6 +29,7 @@ substrate-build-script-utils.workspace = 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 9e4ec730c5c0e..c8a85365957fa 100644 --- a/cumulus/polkadot-parachain/Cargo.toml +++ b/cumulus/polkadot-parachain/Cargo.toml @@ -41,6 +41,7 @@ parachains-common.workspace = true penpal-runtime.workspace = true people-rococo-runtime.workspace = true people-westend-runtime.workspace = true +polkadot-jemalloc-shim = { workspace = true } polkadot-omni-node-lib = { features = ["rococo-native", "westend-native"], workspace = true } rococo-parachain-runtime.workspace = true sc-chain-spec.default-features = true @@ -61,6 +62,9 @@ xcm.default-features = true xcm.workspace = 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 } @@ -70,6 +74,7 @@ substrate-build-script-utils.workspace = 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 d7a42e460e0d0..1d3f6aa82ebb2 100644 --- a/polkadot/Cargo.toml +++ b/polkadot/Cargo.toml @@ -68,6 +68,7 @@ path = "src/bin/prepare-worker.rs" [dependencies] color-eyre = { workspace = true } polkadot-cli = { features = ["rococo-native", "westend-native"], workspace = true, default-features = true } +polkadot-jemalloc-shim = { workspace = true } polkadot-node-core-pvf.default-features = true polkadot-node-core-pvf.workspace = true polkadot-node-core-pvf-common.default-features = true @@ -78,10 +79,9 @@ polkadot-node-core-pvf-prepare-worker.default-features = true polkadot-node-core-pvf-prepare-worker.workspace = true polkadot-overseer.default-features = true polkadot-overseer.workspace = true -tikv-jemallocator = { optional = true, features = ["unprefixed_malloc_on_supported_platforms"], workspace = 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 } @@ -101,13 +101,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 6221a70c78feb..b63dcab06d333 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 525e60665bd3f..383cbce6aa030 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..6a46082f3fa5a --- /dev/null +++ b/prdoc/pr_10709.prdoc @@ -0,0 +1,20 @@ +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 +- name: polkadot-sdk + bump: patch +- name: polkadot-node-core-pvf-execute-worker + bump: patch +- name: polkadot-node-core-pvf-prepare-worker + bump: patch +- name: polkadot-omni-node + bump: patch +- name: polkadot-parachain-bin + bump: patch diff --git a/umbrella/Cargo.toml b/umbrella/Cargo.toml index fef6b646ff983..7abc2abc88df7 100644 --- a/umbrella/Cargo.toml +++ b/umbrella/Cargo.toml @@ -915,8 +915,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", @@ -2480,16 +2478,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 68bc0a8c6d1cd..fe74b09c686ec 100644 --- a/umbrella/src/lib.rs +++ b/umbrella/src/lib.rs @@ -946,16 +946,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;