mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-08 09:16:00 +00:00
Add jemalloc support
This commit is contained in:
4
.cargo/config.toml
Normal file
4
.cargo/config.toml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
[env]
|
||||||
|
# Set the number of arenas to 16 when using jemalloc.
|
||||||
|
JEMALLOC_SYS_WITH_MALLOC_CONF = "abort_conf:true,narenas:16"
|
||||||
|
|
||||||
40
Cargo.lock
generated
40
Cargo.lock
generated
@@ -2255,6 +2255,12 @@ dependencies = [
|
|||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fs_extra"
|
||||||
|
version = "1.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "funty"
|
name = "funty"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
@@ -3012,6 +3018,38 @@ version = "1.0.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
|
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jemalloc-ctl"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c1891c671f3db85d8ea8525dd43ab147f9977041911d24a03e5a36187a7bfde9"
|
||||||
|
dependencies = [
|
||||||
|
"jemalloc-sys",
|
||||||
|
"libc",
|
||||||
|
"paste",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jemalloc-sys"
|
||||||
|
version = "0.5.2+5.3.0-patched"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "134163979b6eed9564c98637b710b40979939ba351f59952708234ea11b5f3f8"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"fs_extra",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jemallocator"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "16c2514137880c52b0b4822b563fadd38257c1f380858addb74a400889696ea6"
|
||||||
|
dependencies = [
|
||||||
|
"jemalloc-sys",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "js-sys"
|
name = "js-sys"
|
||||||
version = "0.3.59"
|
version = "0.3.59"
|
||||||
@@ -3759,6 +3797,8 @@ dependencies = [
|
|||||||
name = "malloc_utils"
|
name = "malloc_utils"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"jemalloc-ctl",
|
||||||
|
"jemallocator",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"libc",
|
"libc",
|
||||||
"lighthouse_metrics",
|
"lighthouse_metrics",
|
||||||
|
|||||||
5
Makefile
5
Makefile
@@ -14,8 +14,11 @@ BUILD_PATH_AARCH64 = "target/$(AARCH64_TAG)/release"
|
|||||||
PINNED_NIGHTLY ?= nightly
|
PINNED_NIGHTLY ?= nightly
|
||||||
CLIPPY_PINNED_NIGHTLY=nightly-2022-05-19
|
CLIPPY_PINNED_NIGHTLY=nightly-2022-05-19
|
||||||
|
|
||||||
|
# List of features to use when building natively. Can be overriden via the environment.
|
||||||
|
FEATURES ?= jemalloc
|
||||||
|
|
||||||
# List of features to use when cross-compiling. Can be overridden via the environment.
|
# List of features to use when cross-compiling. Can be overridden via the environment.
|
||||||
CROSS_FEATURES ?= gnosis,slasher-lmdb,slasher-mdbx
|
CROSS_FEATURES ?= gnosis,slasher-lmdb,slasher-mdbx,jemalloc
|
||||||
|
|
||||||
# Cargo profile for Cross builds. Default is for local builds, CI uses an override.
|
# Cargo profile for Cross builds. Default is for local builds, CI uses an override.
|
||||||
CROSS_PROFILE ?= release
|
CROSS_PROFILE ?= release
|
||||||
|
|||||||
@@ -4,13 +4,16 @@ version = "0.1.0"
|
|||||||
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
authors = ["Paul Hauner <paul@paulhauner.com>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
lighthouse_metrics = { path = "../lighthouse_metrics" }
|
lighthouse_metrics = { path = "../lighthouse_metrics" }
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
libc = "0.2.79"
|
libc = "0.2.79"
|
||||||
parking_lot = "0.12.0"
|
parking_lot = "0.12.0"
|
||||||
|
jemallocator = { version = "0.5.0", optional = true, features = ["background_threads"] }
|
||||||
|
jemalloc-ctl = { version = "0.5.0", optional = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
mallinfo2 = []
|
mallinfo2 = []
|
||||||
|
jemalloc = ["jemallocator", "jemallocator/stats", "jemalloc-ctl"]
|
||||||
|
jemalloc-stats = ["jemallocator/stats"]
|
||||||
|
jemalloc-profiling = ["jemallocator/profiling"]
|
||||||
|
|||||||
52
common/malloc_utils/src/jemalloc.rs
Normal file
52
common/malloc_utils/src/jemalloc.rs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
//! Set the allocator to `jemalloc`.
|
||||||
|
//!
|
||||||
|
//! Due to `jemalloc` requiring configuration at compile time or immediately upon runtime
|
||||||
|
//! initialisation it is configured via a Cargo config file in `.cargo/config.toml`.
|
||||||
|
//!
|
||||||
|
//! The `jemalloc` tuning can be overriden by:
|
||||||
|
//!
|
||||||
|
//! A) `JEMALLOC_SYS_WITH_MALLOC_CONF` at compile-time.
|
||||||
|
//! B) `_RJEM_MALLOC_CONF` at runtime.
|
||||||
|
use jemalloc_ctl::{arenas, epoch, stats, Error};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use lighthouse_metrics::{set_gauge, try_create_int_gauge, IntGauge};
|
||||||
|
|
||||||
|
#[global_allocator]
|
||||||
|
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
||||||
|
|
||||||
|
// Metrics for jemalloc.
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref NUM_ARENAS: lighthouse_metrics::Result<IntGauge> =
|
||||||
|
try_create_int_gauge("jemalloc_num_arenas", "The number of arenas in use");
|
||||||
|
pub static ref BYTES_ALLOCATED: lighthouse_metrics::Result<IntGauge> =
|
||||||
|
try_create_int_gauge("jemalloc_bytes_allocated", "Equivalent to stats.allocated");
|
||||||
|
pub static ref BYTES_ACTIVE: lighthouse_metrics::Result<IntGauge> =
|
||||||
|
try_create_int_gauge("jemalloc_bytes_active", "Equivalent to stats.active");
|
||||||
|
pub static ref BYTES_MAPPED: lighthouse_metrics::Result<IntGauge> =
|
||||||
|
try_create_int_gauge("jemalloc_bytes_mapped", "Equivalent to stats.mapped");
|
||||||
|
pub static ref BYTES_METADATA: lighthouse_metrics::Result<IntGauge> =
|
||||||
|
try_create_int_gauge("jemalloc_bytes_metadata", "Equivalent to stats.metadata");
|
||||||
|
pub static ref BYTES_RESIDENT: lighthouse_metrics::Result<IntGauge> =
|
||||||
|
try_create_int_gauge("jemalloc_bytes_resident", "Equivalent to stats.resident");
|
||||||
|
pub static ref BYTES_RETAINED: lighthouse_metrics::Result<IntGauge> =
|
||||||
|
try_create_int_gauge("jemalloc_bytes_retained", "Equivalent to stats.retained");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn scrape_jemalloc_metrics() {
|
||||||
|
scrape_jemalloc_metrics_fallible().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn scrape_jemalloc_metrics_fallible() -> Result<(), Error> {
|
||||||
|
// Advance the epoch so that the underlying statistics are updated.
|
||||||
|
epoch::advance()?;
|
||||||
|
|
||||||
|
set_gauge(&NUM_ARENAS, arenas::narenas::read()? as i64);
|
||||||
|
set_gauge(&BYTES_ALLOCATED, stats::allocated::read()? as i64);
|
||||||
|
set_gauge(&BYTES_ACTIVE, stats::active::read()? as i64);
|
||||||
|
set_gauge(&BYTES_MAPPED, stats::mapped::read()? as i64);
|
||||||
|
set_gauge(&BYTES_METADATA, stats::metadata::read()? as i64);
|
||||||
|
set_gauge(&BYTES_RESIDENT, stats::resident::read()? as i64);
|
||||||
|
set_gauge(&BYTES_RETAINED, stats::retained::read()? as i64);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -24,17 +24,36 @@
|
|||||||
//! detecting `glibc` are best-effort. If this crate throws errors about undefined external
|
//! detecting `glibc` are best-effort. If this crate throws errors about undefined external
|
||||||
//! functions, then try to compile with the `not_glibc_interface` module.
|
//! functions, then try to compile with the `not_glibc_interface` module.
|
||||||
|
|
||||||
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
|
#[cfg(all(
|
||||||
|
target_os = "linux",
|
||||||
|
not(any(target_env = "musl", feature = "jemalloc"))
|
||||||
|
))]
|
||||||
mod glibc;
|
mod glibc;
|
||||||
|
|
||||||
|
#[cfg(feature = "jemalloc")]
|
||||||
|
mod jemalloc;
|
||||||
|
|
||||||
pub use interface::*;
|
pub use interface::*;
|
||||||
|
|
||||||
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
|
#[cfg(all(
|
||||||
|
target_os = "linux",
|
||||||
|
not(any(target_env = "musl", feature = "jemalloc"))
|
||||||
|
))]
|
||||||
mod interface {
|
mod interface {
|
||||||
pub use crate::glibc::configure_glibc_malloc as configure_memory_allocator;
|
pub use crate::glibc::configure_glibc_malloc as configure_memory_allocator;
|
||||||
pub use crate::glibc::scrape_mallinfo_metrics as scrape_allocator_metrics;
|
pub use crate::glibc::scrape_mallinfo_metrics as scrape_allocator_metrics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "jemalloc")]
|
||||||
|
mod interface {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn configure_memory_allocator() -> Result<(), String> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use crate::jemalloc::scrape_jemalloc_metrics as scrape_allocator_metrics;
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(any(not(target_os = "linux"), target_env = "musl"))]
|
#[cfg(any(not(target_os = "linux"), target_env = "musl"))]
|
||||||
mod interface {
|
mod interface {
|
||||||
#[allow(dead_code, clippy::unnecessary_wraps)]
|
#[allow(dead_code, clippy::unnecessary_wraps)]
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ gnosis = []
|
|||||||
slasher-mdbx = ["slasher/mdbx"]
|
slasher-mdbx = ["slasher/mdbx"]
|
||||||
# Support slasher LMDB backend.
|
# Support slasher LMDB backend.
|
||||||
slasher-lmdb = ["slasher/lmdb"]
|
slasher-lmdb = ["slasher/lmdb"]
|
||||||
|
# Use jemalloc.
|
||||||
|
jemalloc = ["malloc_utils/jemalloc"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
beacon_node = { "path" = "../beacon_node" }
|
beacon_node = { "path" = "../beacon_node" }
|
||||||
|
|||||||
@@ -31,6 +31,14 @@ fn bls_library_name() -> &'static str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn allocator_name() -> &'static str {
|
||||||
|
if cfg!(feature = "jemalloc") {
|
||||||
|
"jemalloc"
|
||||||
|
} else {
|
||||||
|
"system"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Enable backtraces unless a RUST_BACKTRACE value has already been explicitly provided.
|
// Enable backtraces unless a RUST_BACKTRACE value has already been explicitly provided.
|
||||||
if std::env::var("RUST_BACKTRACE").is_err() {
|
if std::env::var("RUST_BACKTRACE").is_err() {
|
||||||
@@ -51,10 +59,12 @@ fn main() {
|
|||||||
"{}\n\
|
"{}\n\
|
||||||
BLS library: {}\n\
|
BLS library: {}\n\
|
||||||
SHA256 hardware acceleration: {}\n\
|
SHA256 hardware acceleration: {}\n\
|
||||||
|
Allocator: {}\n\
|
||||||
Specs: mainnet (true), minimal ({}), gnosis ({})",
|
Specs: mainnet (true), minimal ({}), gnosis ({})",
|
||||||
VERSION.replace("Lighthouse/", ""),
|
VERSION.replace("Lighthouse/", ""),
|
||||||
bls_library_name(),
|
bls_library_name(),
|
||||||
have_sha_extensions(),
|
have_sha_extensions(),
|
||||||
|
allocator_name(),
|
||||||
cfg!(feature = "spec-minimal"),
|
cfg!(feature = "spec-minimal"),
|
||||||
cfg!(feature = "gnosis"),
|
cfg!(feature = "gnosis"),
|
||||||
).as_str()
|
).as_str()
|
||||||
|
|||||||
Reference in New Issue
Block a user