Skip to content

Commit 317dc0f

Browse files
Fix malloc_utils features (sysmalloc) (#7770)
Follow-up to: - #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).
1 parent 90fa7c2 commit 317dc0f

File tree

8 files changed

+61
-36
lines changed

8 files changed

+61
-36
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ BUILD_PATH_RISCV64 = "target/$(RISCV64_TAG)/release"
1616
PINNED_NIGHTLY ?= nightly
1717

1818
# List of features to use when cross-compiling. Can be overridden via the environment.
19-
CROSS_FEATURES ?= gnosis,slasher-lmdb,slasher-mdbx,slasher-redb,jemalloc,beacon-node-leveldb,beacon-node-redb
19+
CROSS_FEATURES ?= gnosis,slasher-lmdb,slasher-mdbx,slasher-redb,beacon-node-leveldb,beacon-node-redb
2020

2121
# Cargo profile for Cross builds. Default is for local builds, CI uses an override.
2222
CROSS_PROFILE ?= release

book/src/installation_source.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ Commonly used features include:
166166
- `slasher-lmdb`: support for the LMDB slasher backend. Enabled by default.
167167
- `slasher-mdbx`: support for the MDBX slasher backend.
168168
- `beacon-node-leveldb`: support for the leveldb backend. Enabled by default.
169-
- `jemalloc`: use [`jemalloc`][jemalloc] to allocate memory. Enabled by default on Linux and macOS.
170-
Not supported on Windows.
169+
- `sysmalloc`: use the system memory allocator rather than jemalloc. This is always enabled on
170+
Windows.
171171
- `spec-minimal`: support for the minimal preset (useful for testing).
172172
173173
Default features (e.g. `slasher-lmdb`, `beacon-node-leveldb`) may be opted out of using the `--no-default-features`
@@ -178,8 +178,6 @@ E.g.
178178
CARGO_INSTALL_EXTRA_FLAGS="--no-default-features" make
179179
```
180180
181-
[jemalloc]: https://jemalloc.net/
182-
183181
## Compilation Profiles
184182
185183
You can customise the compiler settings used to compile Lighthouse via

common/malloc_utils/Cargo.toml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,23 @@ version = "0.1.0"
44
authors = ["Paul Hauner <[email protected]>"]
55
edition = { workspace = true }
66

7+
# Features are not rich enough to express the complexity of our defaults, so we choose to just
8+
# use the jemalloc feature to control whether the dependency is compiled, but avoid using it if
9+
# the `sysmalloc` feature is set.
10+
#
11+
# On Windows, setting the jemalloc feature will result in a compile-time error.
712
[features]
13+
default = []
814
mallinfo2 = []
9-
jemalloc = ["tikv-jemallocator", "tikv-jemalloc-ctl"]
15+
# The jemalloc feature enables the compilation of jemalloc dependencies. Jemalloc is also the
16+
# default allocator, unless `sysmalloc` is set.
17+
#
18+
# It should be turned off on Windows.
19+
jemalloc = ["tikv-jemalloc-ctl", "tikv-jemallocator"]
1020
jemalloc-profiling = ["tikv-jemallocator/profiling"]
21+
# Force the use of system malloc (or glibc) rather than jemalloc.
22+
# This is a no-op on Windows where jemalloc is always disabled.
23+
sysmalloc = []
1124

1225
[dependencies]
1326
libc = "0.2.79"

common/malloc_utils/src/lib.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,57 @@
2525
//! functions, then try to compile with the `not_glibc_interface` module.
2626
2727
#[cfg(all(
28+
any(feature = "sysmalloc", not(feature = "jemalloc")),
2829
target_os = "linux",
29-
not(target_env = "musl"),
30-
not(feature = "jemalloc")
30+
not(target_env = "musl")
3131
))]
3232
pub mod glibc;
3333

34-
#[cfg(feature = "jemalloc")]
34+
#[cfg(all(unix, not(feature = "sysmalloc"), feature = "jemalloc"))]
3535
pub mod jemalloc;
3636

3737
pub use interface::*;
3838

39+
// Glibc malloc is the default on non-musl Linux if the sysmalloc feature is enabled, or jemalloc
40+
// is disabled.
3941
#[cfg(all(
42+
any(feature = "sysmalloc", not(feature = "jemalloc")),
4043
target_os = "linux",
41-
not(target_env = "musl"),
42-
not(feature = "jemalloc")
44+
not(target_env = "musl")
4345
))]
4446
mod interface {
4547
pub use crate::glibc::configure_glibc_malloc as configure_memory_allocator;
4648
pub use crate::glibc::scrape_mallinfo_metrics as scrape_allocator_metrics;
49+
50+
pub fn allocator_name() -> String {
51+
"glibc".to_string()
52+
}
4753
}
4854

49-
#[cfg(feature = "jemalloc")]
55+
// Jemalloc is the default on UNIX (including musl) unless the sysmalloc feature is enabled.
56+
#[cfg(all(unix, not(feature = "sysmalloc"), feature = "jemalloc"))]
5057
mod interface {
5158
#[allow(dead_code)]
5259
pub fn configure_memory_allocator() -> Result<(), String> {
5360
Ok(())
5461
}
5562

5663
pub use crate::jemalloc::scrape_jemalloc_metrics as scrape_allocator_metrics;
64+
65+
pub fn allocator_name() -> String {
66+
match crate::jemalloc::page_size() {
67+
Ok(page_size) => format!("jemalloc ({}K)", page_size / 1024),
68+
Err(e) => format!("jemalloc (error: {e:?})"),
69+
}
70+
}
5771
}
5872

59-
#[cfg(all(
60-
any(not(target_os = "linux"), target_env = "musl"),
61-
not(feature = "jemalloc")
73+
#[cfg(any(
74+
not(unix),
75+
all(
76+
any(feature = "sysmalloc", not(feature = "jemalloc")),
77+
any(not(target_os = "linux"), target_env = "musl")
78+
)
6279
))]
6380
mod interface {
6481
#[allow(dead_code, clippy::unnecessary_wraps)]
@@ -68,4 +85,8 @@ mod interface {
6885

6986
#[allow(dead_code)]
7087
pub fn scrape_allocator_metrics() {}
88+
89+
pub fn allocator_name() -> String {
90+
"system".to_string()
91+
}
7192
}

lcli/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ normal = ["malloc_utils"]
1111
[features]
1212
portable = ["bls/supranational-portable"]
1313
fake_crypto = ['bls/fake_crypto']
14-
jemalloc = ["malloc_utils/jemalloc"]
1514

1615
[dependencies]
1716
account_utils = { workspace = true }
@@ -31,7 +30,6 @@ hex = { workspace = true }
3130
lighthouse_network = { workspace = true }
3231
lighthouse_version = { workspace = true }
3332
log = { workspace = true }
34-
malloc_utils = { workspace = true }
3533
rayon = { workspace = true }
3634
serde = { workspace = true }
3735
serde_json = { workspace = true }
@@ -44,3 +42,9 @@ tracing-subscriber = { workspace = true }
4442
tree_hash = { workspace = true }
4543
types = { workspace = true }
4644
validator_dir = { workspace = true }
45+
46+
[target.'cfg(not(target_os = "windows"))'.dependencies]
47+
malloc_utils = { workspace = true, features = ["jemalloc"] }
48+
49+
[target.'cfg(target_os = "windows")'.dependencies]
50+
malloc_utils = { workspace = true, features = [] }

lighthouse/Cargo.toml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@ beacon-node-leveldb = ["store/leveldb"]
3535
beacon-node-redb = ["store/redb"]
3636
# Supports console subscriber for debugging
3737
console-subscriber = ["console-subscriber/default"]
38-
# Turns off jemalloc so that heaptrack may be used to analyse memory usage.
39-
heaptrack = []
40-
41-
# Deprecated. This is now enabled by default on non windows targets (unless heaptrack is enabled).
42-
jemalloc = []
38+
# Force the use of the system memory allocator rather than jemalloc.
39+
sysmalloc = ["malloc_utils/sysmalloc"]
4340

4441
[dependencies]
4542
account_manager = { "path" = "../account_manager" }
@@ -58,7 +55,6 @@ ethereum_hashing = { workspace = true }
5855
futures = { workspace = true }
5956
lighthouse_version = { workspace = true }
6057
logging = { workspace = true }
61-
malloc_utils = { workspace = true }
6258
metrics = { workspace = true }
6359
opentelemetry = { workspace = true }
6460
opentelemetry-otlp = { workspace = true }
@@ -77,12 +73,12 @@ unused_port = { workspace = true }
7773
validator_client = { workspace = true }
7874
validator_manager = { path = "../validator_manager" }
7975

80-
[target.'cfg(any(target_os = "windows", features = "heaptrack"))'.dependencies]
81-
malloc_utils = { workspace = true }
82-
83-
[target.'cfg(not(any(target_os = "windows", features = "heaptrack")))'.dependencies]
76+
[target.'cfg(not(target_os = "windows"))'.dependencies]
8477
malloc_utils = { workspace = true, features = ["jemalloc"] }
8578

79+
[target.'cfg(target_os = "windows")'.dependencies]
80+
malloc_utils = { workspace = true, features = [] }
81+
8682
[dev-dependencies]
8783
beacon_node_fallback = { workspace = true }
8884
beacon_processor = { workspace = true }

lighthouse/src/main.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,7 @@ fn bls_hardware_acceleration() -> bool {
7676
}
7777

7878
fn allocator_name() -> String {
79-
#[cfg(any(feature = "heaptrack", target_os = "windows"))]
80-
{
81-
"system".to_string()
82-
}
83-
#[cfg(not(any(feature = "heaptrack", target_os = "windows")))]
84-
match malloc_utils::jemalloc::page_size() {
85-
Ok(page_size) => format!("jemalloc ({}K)", page_size / 1024),
86-
Err(e) => format!("jemalloc (error: {e:?})"),
87-
}
79+
malloc_utils::allocator_name()
8880
}
8981

9082
fn build_profile_name() -> String {

wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
allocator
12
APIs
23
ARMv
34
AUR

0 commit comments

Comments
 (0)