From 6c647d68e2bdc349fad0a67de6b05a1a91aeb031 Mon Sep 17 00:00:00 2001 From: Aaron Siddhartha Mondal Date: Tue, 12 Mar 2024 01:00:27 +0100 Subject: [PATCH] Use mimalloc as global memory allocator (#749) This should make allocation behavior more consistent across different builds and reduce memory fragmentation issues. The allocation behavior of the `nativelink` executable is now configurable with `MIMALLOC_*` environment variables as described here: https://microsoft.github.io/mimalloc/environment.html --- BUILD.bazel | 1 + Cargo.lock | 120 ++++++++++++++++++++++++------------------ Cargo.toml | 1 + flake.nix | 25 +++++++-- src/bin/nativelink.rs | 4 ++ 5 files changed, 97 insertions(+), 54 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 8ee5c2451..619f65ae5 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -27,6 +27,7 @@ rust_binary( "@crates//:console-subscriber", "@crates//:futures", "@crates//:hyper", + "@crates//:mimalloc", "@crates//:parking_lot", "@crates//:prometheus-client", "@crates//:rustls-pemfile", diff --git a/Cargo.lock b/Cargo.lock index 60f2a39d6..a1896121b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,9 +19,9 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" [[package]] name = "ahash" -version = "0.8.10" +version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b79b82693f705137f8fb9b37871d99e4f9a7df12b917eed79c3d3954830a60b" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ "cfg-if", "once_cell", @@ -192,7 +192,7 @@ dependencies = [ "bytes", "fastrand", "hex", - "http 0.2.11", + "http 0.2.12", "hyper", "ring", "time", @@ -229,7 +229,7 @@ dependencies = [ "aws-types", "bytes", "fastrand", - "http 0.2.11", + "http 0.2.12", "http-body", "percent-encoding", "pin-project-lite", @@ -257,7 +257,7 @@ dependencies = [ "aws-smithy-xml", "aws-types", "bytes", - "http 0.2.11", + "http 0.2.12", "http-body", "once_cell", "percent-encoding", @@ -282,7 +282,7 @@ dependencies = [ "aws-smithy-types", "aws-types", "bytes", - "http 0.2.11", + "http 0.2.12", "once_cell", "regex-lite", "tracing", @@ -304,7 +304,7 @@ dependencies = [ "aws-smithy-types", "aws-types", "bytes", - "http 0.2.11", + "http 0.2.12", "once_cell", "regex-lite", "tracing", @@ -327,7 +327,7 @@ dependencies = [ "aws-smithy-types", "aws-smithy-xml", "aws-types", - "http 0.2.11", + "http 0.2.12", "once_cell", "regex-lite", "tracing", @@ -349,8 +349,8 @@ dependencies = [ "form_urlencoded", "hex", "hmac", - "http 0.2.11", - "http 1.0.0", + "http 0.2.12", + "http 1.1.0", "once_cell", "p256", "percent-encoding", @@ -385,7 +385,7 @@ dependencies = [ "crc32c", "crc32fast", "hex", - "http 0.2.11", + "http 0.2.12", "http-body", "md-5", "pin-project-lite", @@ -417,7 +417,7 @@ dependencies = [ "bytes", "bytes-utils", "futures-core", - "http 0.2.11", + "http 0.2.12", "http-body", "once_cell", "percent-encoding", @@ -443,7 +443,7 @@ checksum = "882b95acc2ed531309fad4b6bc87486383bb89f644a4243b377a00895c701f00" dependencies = [ "assert-json-diff", "aws-smithy-runtime-api", - "http 0.2.11", + "http 0.2.12", "pretty_assertions", "regex-lite", "roxmltree", @@ -475,7 +475,7 @@ dependencies = [ "bytes", "fastrand", "h2", - "http 0.2.11", + "http 0.2.12", "http-body", "hyper", "hyper-rustls", @@ -499,8 +499,8 @@ dependencies = [ "aws-smithy-async", "aws-smithy-types", "bytes", - "http 0.2.11", - "http 1.0.0", + "http 0.2.12", + "http 1.1.0", "pin-project-lite", "tokio", "tracing", @@ -517,7 +517,7 @@ dependencies = [ "bytes", "bytes-utils", "futures-core", - "http 0.2.11", + "http 0.2.12", "http-body", "itoa", "num-integer", @@ -549,7 +549,7 @@ dependencies = [ "aws-smithy-async", "aws-smithy-runtime-api", "aws-smithy-types", - "http 0.2.11", + "http 0.2.12", "rustc_version", "tracing", ] @@ -565,7 +565,7 @@ dependencies = [ "bitflags 1.3.2", "bytes", "futures-util", - "http 0.2.11", + "http 0.2.12", "http-body", "hyper", "itoa", @@ -595,7 +595,7 @@ dependencies = [ "async-trait", "bytes", "futures-util", - "http 0.2.11", + "http 0.2.12", "http-body", "mime", "rustversion", @@ -729,9 +729,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.5.1" +version = "4.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c918d541ef2913577a0f9566e9ce27cb35b6df072075769e0b26cb5a554520da" +checksum = "b230ab84b0ffdf890d5a10abdbc8b83ae1c4918275daea1ab8801f71536b2651" dependencies = [ "clap_builder", "clap_derive", @@ -739,9 +739,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.1" +version = "4.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f3e7391dad68afb0c2ede1bf619f579a3dc9c2ec67f089baa397123a2f3d1eb" +checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" dependencies = [ "anstream", "anstyle", @@ -1274,7 +1274,7 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http 0.2.11", + "http 0.2.12", "indexmap 2.2.5", "slab", "tokio", @@ -1349,9 +1349,9 @@ dependencies = [ [[package]] name = "http" -version = "0.2.11" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8947b1a6fad4393052c7ba1f4cd97bed3e953a95c79c92ad9b051a04611d9fbb" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" dependencies = [ "bytes", "fnv", @@ -1360,9 +1360,9 @@ dependencies = [ [[package]] name = "http" -version = "1.0.0" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b32afd38673a8016f7c9ae69e5af41a58f81b1d31689040f2f1959594ce194ea" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" dependencies = [ "bytes", "fnv", @@ -1376,7 +1376,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" dependencies = [ "bytes", - "http 0.2.11", + "http 0.2.12", "pin-project-lite", ] @@ -1409,7 +1409,7 @@ dependencies = [ "futures-core", "futures-util", "h2", - "http 0.2.11", + "http 0.2.12", "http-body", "httparse", "httpdate", @@ -1429,7 +1429,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" dependencies = [ "futures-util", - "http 0.2.11", + "http 0.2.12", "hyper", "log", "rustls 0.21.10", @@ -1508,6 +1508,16 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "libmimalloc-sys" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3979b5c37ece694f1f5e51e7ecc871fdb0f517ed04ee45f88d15d6d553cb9664" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "libredox" version = "0.0.1" @@ -1615,6 +1625,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "mimalloc" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa01922b5ea280a911e323e4d2fd24b7fe5cc4042e0d2cda3c40775cdc4bdc9c" +dependencies = [ + "libmimalloc-sys", +] + [[package]] name = "mime" version = "0.3.17" @@ -1669,6 +1688,7 @@ dependencies = [ "console-subscriber", "futures", "hyper", + "mimalloc", "nativelink-config", "nativelink-error", "nativelink-proto", @@ -1793,7 +1813,7 @@ dependencies = [ "filetime", "futures", "hex", - "http 1.0.0", + "http 1.1.0", "hyper", "hyper-rustls", "lz4_flex", @@ -2028,9 +2048,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "pest" -version = "2.7.7" +version = "2.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "219c0dcc30b6a27553f9cc242972b67f75b60eb0db71f0b5462f38b058c41546" +checksum = "56f8023d0fb78c8e03784ea1c7f3fa36e68a723138990b8d5a47d916b651e7a8" dependencies = [ "memchr", "thiserror", @@ -2039,9 +2059,9 @@ dependencies = [ [[package]] name = "pest_derive" -version = "2.7.7" +version = "2.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22e1288dbd7786462961e69bfd4df7848c1e37e8b74303dbdab82c3a9cdd2809" +checksum = "b0d24f72393fd16ab6ac5738bc33cdb6a9aa73f8b902e8fe29cf4e67d7dd1026" dependencies = [ "pest", "pest_generator", @@ -2049,9 +2069,9 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.7.7" +version = "2.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1381c29a877c6d34b8c176e734f35d7f7f5b3adaefe940cb4d1bb7af94678e2e" +checksum = "fdc17e2a6c7d0a492f0158d7a4bd66cc17280308bbaff78d5bef566dca35ab80" dependencies = [ "pest", "pest_meta", @@ -2062,9 +2082,9 @@ dependencies = [ [[package]] name = "pest_meta" -version = "2.7.7" +version = "2.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0934d6907f148c22a3acbda520c7eed243ad7487a30f51f6ce52b58b7077a8a" +checksum = "934cd7631c050f4674352a6e835d5f6711ffbfb9345c2fc0107155ac495ae293" dependencies = [ "once_cell", "pest", @@ -2083,18 +2103,18 @@ dependencies = [ [[package]] name = "pin-project" -version = "1.1.4" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0302c4a0442c456bd56f841aee5c3bfd17967563f6fadc9ceb9f9c23cf3807e0" +checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.1.4" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "266c042b60c9c76b8d53061e52b2e0d1116abc57cefc8c5cd671619a56ac3690" +checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", @@ -2332,7 +2352,7 @@ checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.4.5", + "regex-automata 0.4.6", "regex-syntax 0.8.2", ] @@ -2347,9 +2367,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" +checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" dependencies = [ "aho-corasick", "memchr", @@ -3010,7 +3030,7 @@ dependencies = [ "base64", "bytes", "h2", - "http 0.2.11", + "http 0.2.12", "http-body", "hyper", "hyper-timeout", @@ -3038,7 +3058,7 @@ dependencies = [ "bytes", "flate2", "h2", - "http 0.2.11", + "http 0.2.12", "http-body", "hyper", "hyper-timeout", diff --git a/Cargo.toml b/Cargo.toml index 004198e7f..d4f327312 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,7 @@ clap = { version = "4.5.1", features = ["derive"] } console-subscriber = { version = "0.2.0" } futures = "0.3.30" hyper = { version = "0.14.28" } +mimalloc = "0.1.39" parking_lot = "0.12.1" prometheus-client = "0.21.2" rustls-pemfile = "2.1.1" diff --git a/flake.nix b/flake.nix index c528812ef..7c2fbff8a 100644 --- a/flake.nix +++ b/flake.nix @@ -40,8 +40,25 @@ system, ... }: let - stable-rust = pkgs.rust-bin.stable."1.76.0"; - nightly-rust = pkgs.rust-bin.nightly."2024-03-01"; + stable-rust-version = "1.76.0"; + nightly-rust-version = "2024-03-01"; + + # TODO(aaronmondal): Make musl builds work on Darwin. + # See: https://github.com/TraceMachina/nativelink/issues/751 + stable-rust = + if pkgs.stdenv.isDarwin + then pkgs.rust-bin.stable.${stable-rust-version} + else pkgs.pkgsMusl.rust-bin.stable.${stable-rust-version}; + nightly-rust = + if pkgs.stdenv.isDarwin + then pkgs.rust-bin.nightly.${nightly-rust-version} + else pkgs.pkgsMusl.rust-bin.nightly.${nightly-rust-version}; + + # TODO(aaronmondal): Tools like rustdoc don't work with the `pkgsMusl` + # package set because of missing libgcc_s. Fix this upstream and use the + # `stable-rust` toolchain in the devShell as well. + # See: https://github.com/oxalica/rust-overlay/issues/161 + stable-rust-native = pkgs.rust-bin.stable.${stable-rust-version}; maybeDarwinDeps = pkgs.lib.optionals pkgs.stdenv.isDarwin [ pkgs.darwin.apple_sdk.frameworks.Security @@ -61,7 +78,7 @@ if pkgs.stdenv.isDarwin then crane.lib.${system} else - (crane.mkLib pkgs).overrideToolchain (pkgs.rust-bin.stable.latest.default.override { + (crane.mkLib pkgs).overrideToolchain (stable-rust.default.override { targets = ["x86_64-unknown-linux-musl"]; }); @@ -161,7 +178,7 @@ nativeBuildInputs = [ # Development tooling goes here. - stable-rust.default + stable-rust-native.default pkgs.pre-commit pkgs.bazel_7 pkgs.awscli2 diff --git a/src/bin/nativelink.rs b/src/bin/nativelink.rs index ea8141fa2..93a79d241 100644 --- a/src/bin/nativelink.rs +++ b/src/bin/nativelink.rs @@ -24,6 +24,7 @@ use futures::future::{select_all, BoxFuture, OptionFuture, TryFutureExt}; use futures::{FutureExt, StreamExt}; use hyper::server::conn::Http; use hyper::{Response, StatusCode}; +use mimalloc::MiMalloc; use nativelink_config::cas_server::{ CasConfig, CompressionAlgorithm, GlobalConfig, ListenerConfig, ServerConfig, WorkerConfig, }; @@ -66,6 +67,9 @@ use tower::util::ServiceExt; use tracing::{error, warn}; use tracing_subscriber::filter::{EnvFilter, LevelFilter}; +#[global_allocator] +static GLOBAL: MiMalloc = MiMalloc; + /// Note: This must be kept in sync with the documentation in `PrometheusConfig::path`. const DEFAULT_PROMETHEUS_METRICS_PATH: &str = "/metrics";