]> git.lizzy.rs Git - rust.git/commitdiff
Merge #7334
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>
Mon, 18 Jan 2021 18:40:14 +0000 (18:40 +0000)
committerGitHub <noreply@github.com>
Mon, 18 Jan 2021 18:40:14 +0000 (18:40 +0000)
7334: Add back jemalloc support r=jonas-schievink a=jonas-schievink

jemalloc is useful due to its introspection API, which allows obtaining quick and accurate memory usage statistics without running into `mallinfo`'s limitations.

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
Cargo.lock
crates/profile/Cargo.toml
crates/profile/src/memory_usage.rs
crates/rust-analyzer/Cargo.toml
crates/rust-analyzer/src/bin/main.rs
xtask/src/install.rs
xtask/src/main.rs

index b6b6f03410c261adb15cf2953af2ebe0bff22414..35713a0c49592b4f487b01c4fe6a30d35e290808 100644 (file)
@@ -448,6 +448,12 @@ dependencies = [
  "percent-encoding",
 ]
 
+[[package]]
+name = "fs_extra"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
+
 [[package]]
 name = "fsevent"
 version = "2.0.2"
@@ -757,6 +763,38 @@ version = "0.4.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
 
+[[package]]
+name = "jemalloc-ctl"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c502a5ff9dd2924f1ed32ba96e3b65735d837b4bfd978d3161b1702e66aca4b7"
+dependencies = [
+ "jemalloc-sys",
+ "libc",
+ "paste",
+]
+
+[[package]]
+name = "jemalloc-sys"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0d3b9f3f5c9b31aa0f5ed3260385ac205db665baa41d49bb8338008ae94ede45"
+dependencies = [
+ "cc",
+ "fs_extra",
+ "libc",
+]
+
+[[package]]
+name = "jemallocator"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "43ae63fcfc45e99ab3d1b29a46782ad679e98436c3169d15a167a1108a724b69"
+dependencies = [
+ "jemalloc-sys",
+ "libc",
+]
+
 [[package]]
 name = "jod-thread"
 version = "0.1.2"
@@ -1104,6 +1142,25 @@ dependencies = [
  "drop_bomb",
 ]
 
+[[package]]
+name = "paste"
+version = "0.1.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "45ca20c77d80be666aef2b45486da86238fabe33e38306bd3118fe4af33fa880"
+dependencies = [
+ "paste-impl",
+ "proc-macro-hack",
+]
+
+[[package]]
+name = "paste-impl"
+version = "0.1.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d95a7db200b97ef370c8e6de0088252f7e0dfff7d047a28528e47456c0fc98b6"
+dependencies = [
+ "proc-macro-hack",
+]
+
 [[package]]
 name = "paths"
 version = "0.0.0"
@@ -1164,6 +1221,12 @@ version = "0.2.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "439697af366c49a6d0a010c56a0d97685bc140ce0d377b13a2ea2aa42d64a827"
 
+[[package]]
+name = "proc-macro-hack"
+version = "0.5.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
+
 [[package]]
 name = "proc-macro2"
 version = "1.0.24"
@@ -1212,6 +1275,7 @@ name = "profile"
 version = "0.0.0"
 dependencies = [
  "cfg-if 1.0.0",
+ "jemalloc-ctl",
  "la-arena",
  "libc",
  "once_cell",
@@ -1354,6 +1418,7 @@ dependencies = [
  "ide",
  "ide_db",
  "itertools 0.10.0",
+ "jemallocator",
  "jod-thread",
  "log",
  "lsp-server",
index 9c7ce26beb3e43869135d01b14248864a42f278c..f7231c2b8eaab89606e1d4500b48ac02d5faa279 100644 (file)
@@ -14,12 +14,14 @@ once_cell = "1.3.1"
 cfg-if = "1"
 libc = "0.2.73"
 la-arena = { version = "0.2.0", path = "../../lib/arena" }
+jemalloc-ctl = { version = "0.3.3", optional = true }
 
 [target.'cfg(target_os = "linux")'.dependencies]
 perf-event = "0.4"
 
 [features]
 cpu_profiler = []
+jemalloc = ["jemalloc-ctl"]
 
 # Uncomment to enable for the whole crate graph
 # default = [ "cpu_profiler" ]
index 83390212ae0bbc81691430c2a332d2eecdd9725e..cb4e544477173759f9878a2a3cf3036acc71f8fc 100644 (file)
@@ -24,7 +24,12 @@ fn sub(self, rhs: MemoryUsage) -> MemoryUsage {
 impl MemoryUsage {
     pub fn current() -> MemoryUsage {
         cfg_if! {
-            if #[cfg(all(target_os = "linux", target_env = "gnu"))] {
+            if #[cfg(all(feature = "jemalloc", not(target_env = "msvc")))] {
+                jemalloc_ctl::epoch::advance().unwrap();
+                MemoryUsage {
+                    allocated: Bytes(jemalloc_ctl::stats::allocated::read().unwrap() as isize),
+                }
+            } else if #[cfg(all(target_os = "linux", target_env = "gnu"))] {
                 // Note: This is incredibly slow.
                 let alloc = unsafe { libc::mallinfo() }.uordblks as isize;
                 MemoryUsage { allocated: Bytes(alloc) }
index af7b86ead548aee693169496e2e823b405f39c4b..3cb45b030fd27e742bc06274083703b008f8fc47 100644 (file)
@@ -61,8 +61,14 @@ proc_macro_srv = { path = "../proc_macro_srv", version = "0.0.0" }
 [target.'cfg(windows)'.dependencies]
 winapi = "0.3.8"
 
+[target.'cfg(not(target_env = "msvc"))'.dependencies]
+jemallocator = { version = "0.3.2", optional = true }
+
 [dev-dependencies]
 expect-test = "1.1"
 test_utils = { path = "../test_utils" }
 mbe = { path = "../mbe" }
 tt = { path = "../tt" }
+
+[features]
+jemalloc = ["jemallocator", "profile/jemalloc"]
index bf42654a88cc1190452553b74d8df16dababd822..2f7f94a39373de75201ddb4cb9878316410e2c19 100644 (file)
 #[global_allocator]
 static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
 
+#[cfg(all(feature = "jemalloc", not(target_env = "msvc")))]
+#[global_allocator]
+static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
+
 fn main() {
     if let Err(err) = try_main() {
         eprintln!("{}", err);
index 12962bcfa1ff24408c7f54f11d148945573deafe..202c74426689d9684306ea80b9efd8343ceaf678 100644 (file)
@@ -67,6 +67,7 @@ pub struct ServerOpt {
 pub enum Malloc {
     System,
     Mimalloc,
+    Jemalloc,
 }
 
 impl InstallCmd {
@@ -176,6 +177,7 @@ fn install_server(opts: ServerOpt) -> Result<()> {
     let features = match opts.malloc {
         Malloc::System => &[][..],
         Malloc::Mimalloc => &["--features", "mimalloc"],
+        Malloc::Jemalloc => &["--features", "jemalloc"],
     };
 
     let cmd = cmd!("cargo install --path crates/rust-analyzer --locked --force {features...}");
index dec48629c9c924293d688558c8783acc87308d89..c3e5c732672503500d55e00c430d84ab2a729551 100644 (file)
@@ -49,7 +49,8 @@ fn main() -> Result<()> {
         --client[=CLIENT] Install only VS Code plugin.
                           CLIENT is one of 'code', 'code-exploration', 'code-insiders', 'codium', or 'code-oss'
         --server          Install only the language server
-        --mimalloc        Use mimalloc for server
+        --mimalloc        Use mimalloc allocator for server
+        --jemalloc        Use jemalloc allocator for server
     -h, --help            Prints help information
         "
                 );
@@ -65,8 +66,13 @@ fn main() -> Result<()> {
                 return Ok(());
             }
 
-            let malloc =
-                if args.contains("--mimalloc") { Malloc::Mimalloc } else { Malloc::System };
+            let malloc = if args.contains("--mimalloc") {
+                Malloc::Mimalloc
+            } else if args.contains("--jemalloc") {
+                Malloc::Jemalloc
+            } else {
+                Malloc::System
+            };
 
             let client_opt = args.opt_value_from_str("--client")?;