Skip to content

Commit 94cb105

Browse files
committed
Use jemalloc-sys on Linux and OSX compilers
This commit adds opt-in support to the compiler to link to `jemalloc` in the compiler. When activated the compiler will depend on `jemalloc-sys`, instruct jemalloc to unprefix its symbols, and then link to it. The feature is activated by default on Linux/OSX compilers for x86_64/i686 platforms, and it's not enabled anywhere else for now. We may be able to opt-in other platforms in the future! Also note that the opt-in only happens on CI, it's otherwise unconditionally turned off by default. Closes #36963
1 parent 9696882 commit 94cb105

File tree

11 files changed

+57
-10
lines changed

11 files changed

+57
-10
lines changed

.travis.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ matrix:
3030

3131
- env: >
3232
RUST_CHECK_TARGET=dist
33-
RUST_CONFIGURE_ARGS="--enable-extended --enable-profiler --enable-lldb"
33+
RUST_CONFIGURE_ARGS="--enable-extended --enable-profiler --enable-lldb --set rust.jemalloc"
3434
SRC=.
3535
DEPLOY_ALT=1
3636
RUSTC_RETRY_LINKER_ON_SEGFAULT=1
@@ -53,7 +53,7 @@ matrix:
5353
# version that we're using, 8.2, cannot compile LLVM for OSX 10.7.
5454
- env: >
5555
RUST_CHECK_TARGET=check
56-
RUST_CONFIGURE_ARGS="--build=x86_64-apple-darwin --enable-sanitizers --enable-profiler"
56+
RUST_CONFIGURE_ARGS="--build=x86_64-apple-darwin --enable-sanitizers --enable-profiler --set rust.jemalloc"
5757
SRC=.
5858
RUSTC_RETRY_LINKER_ON_SEGFAULT=1
5959
MACOSX_DEPLOYMENT_TARGET=10.8
@@ -67,7 +67,7 @@ matrix:
6767
6868
- env: >
6969
RUST_CHECK_TARGET=check
70-
RUST_CONFIGURE_ARGS=--build=i686-apple-darwin
70+
RUST_CONFIGURE_ARGS="--build=i686-apple-darwin --set rust.jemalloc"
7171
SRC=.
7272
RUSTC_RETRY_LINKER_ON_SEGFAULT=1
7373
MACOSX_DEPLOYMENT_TARGET=10.8
@@ -87,7 +87,7 @@ matrix:
8787
# OSX 10.7 and `xcode7` is the latest Xcode able to compile LLVM for 10.7.
8888
- env: >
8989
RUST_CHECK_TARGET=dist
90-
RUST_CONFIGURE_ARGS="--build=i686-apple-darwin --enable-full-tools --enable-profiler --enable-lldb"
90+
RUST_CONFIGURE_ARGS="--build=i686-apple-darwin --enable-full-tools --enable-profiler --enable-lldb --set rust.jemalloc"
9191
SRC=.
9292
DEPLOY=1
9393
RUSTC_RETRY_LINKER_ON_SEGFAULT=1
@@ -102,7 +102,7 @@ matrix:
102102
103103
- env: >
104104
RUST_CHECK_TARGET=dist
105-
RUST_CONFIGURE_ARGS="--target=aarch64-apple-ios,armv7-apple-ios,armv7s-apple-ios,i386-apple-ios,x86_64-apple-ios --enable-full-tools --enable-sanitizers --enable-profiler --enable-lldb"
105+
RUST_CONFIGURE_ARGS="--target=aarch64-apple-ios,armv7-apple-ios,armv7s-apple-ios,i386-apple-ios,x86_64-apple-ios --enable-full-tools --enable-sanitizers --enable-profiler --enable-lldb --set rust.jemalloc"
106106
SRC=.
107107
DEPLOY=1
108108
RUSTC_RETRY_LINKER_ON_SEGFAULT=1

config.toml.example

+4
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@
392392
# generally only set for releases
393393
#remap-debuginfo = false
394394

395+
# Link the compiler against `jemalloc`, where on Linux and OSX it should
396+
# override the default allocator for rustc and LLVM.
397+
#jemalloc = false
398+
395399
# =============================================================================
396400
# Options for specific targets
397401
#

src/Cargo.lock

+18
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,11 @@ dependencies = [
772772
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
773773
]
774774

775+
[[package]]
776+
name = "fs_extra"
777+
version = "1.1.0"
778+
source = "registry+https://github.com/rust-lang/crates.io-index"
779+
775780
[[package]]
776781
name = "fst"
777782
version = "0.3.0"
@@ -983,6 +988,16 @@ name = "itoa"
983988
version = "0.4.3"
984989
source = "registry+https://github.com/rust-lang/crates.io-index"
985990

991+
[[package]]
992+
name = "jemalloc-sys"
993+
version = "0.1.8"
994+
source = "registry+https://github.com/rust-lang/crates.io-index"
995+
dependencies = [
996+
"cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)",
997+
"fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
998+
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
999+
]
1000+
9861001
[[package]]
9871002
name = "jobserver"
9881003
version = "0.1.11"
@@ -2168,6 +2183,7 @@ dependencies = [
21682183
"arena 0.0.0",
21692184
"env_logger 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)",
21702185
"graphviz 0.0.0",
2186+
"jemalloc-sys 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
21712187
"log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
21722188
"rustc 0.0.0",
21732189
"rustc-rayon 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3223,6 +3239,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
32233239
"checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
32243240
"checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
32253241
"checksum fs2 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
3242+
"checksum fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674"
32263243
"checksum fst 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d94485a00b1827b861dd9d1a2cc9764f9044d4c535514c0760a5a2012ef3399f"
32273244
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
32283245
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
@@ -3245,6 +3262,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
32453262
"checksum is-match 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7e5b386aef33a1c677be65237cb9d32c3f3ef56bd035949710c4bb13083eb053"
32463263
"checksum itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)" = "f58856976b776fedd95533137617a02fb25719f40e7d9b01c7043cd65474f450"
32473264
"checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b"
3265+
"checksum jemalloc-sys 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "bfc62c8e50e381768ce8ee0428ee53741929f7ebd73e4d83f669bcf7693e00ae"
32483266
"checksum jobserver 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "60af5f849e1981434e4a31d3d782c4774ae9b434ce55b101a96ecfd09147e8be"
32493267
"checksum json 0.11.13 (registry+https://github.com/rust-lang/crates.io-index)" = "9ad0485404155f45cce53a40d4b2d6ac356418300daed05273d9e26f91c390be"
32503268
"checksum jsonrpc-core 8.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ddf83704f4e79979a424d1082dd2c1e52683058056c9280efa19ac5f6bc9033c"

src/bootstrap/config.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub struct Config {
115115
pub hosts: Vec<Interned<String>>,
116116
pub targets: Vec<Interned<String>>,
117117
pub local_rebuild: bool,
118+
pub jemalloc: bool,
118119

119120
// dist misc
120121
pub dist_sign_folder: Option<PathBuf>,
@@ -259,7 +260,7 @@ struct Llvm {
259260
link_jobs: Option<u32>,
260261
link_shared: Option<bool>,
261262
version_suffix: Option<String>,
262-
clang_cl: Option<String>
263+
clang_cl: Option<String>,
263264
}
264265

265266
#[derive(Deserialize, Default, Clone)]
@@ -322,6 +323,7 @@ struct Rust {
322323
backtrace_on_ice: Option<bool>,
323324
verify_llvm_ir: Option<bool>,
324325
remap_debuginfo: Option<bool>,
326+
jemalloc: Option<bool>,
325327
}
326328

327329
/// TOML representation of how each build target is configured.
@@ -535,6 +537,7 @@ impl Config {
535537
set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
536538
set(&mut config.codegen_tests, rust.codegen_tests);
537539
set(&mut config.rust_rpath, rust.rpath);
540+
set(&mut config.jemalloc, rust.jemalloc);
538541
set(&mut config.backtrace, rust.backtrace);
539542
set(&mut config.channel, rust.channel.clone());
540543
set(&mut config.rust_dist_src, rust.dist_src);

src/bootstrap/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,11 @@ impl Build {
530530

531531
/// Get the space-separated set of activated features for the compiler.
532532
fn rustc_features(&self) -> String {
533-
String::new()
533+
let mut features = String::new();
534+
if self.config.jemalloc {
535+
features.push_str("jemalloc");
536+
}
537+
features
534538
}
535539

536540
/// Component directory that Cargo will produce output into (e.g.

src/ci/docker/dist-i686-linux/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ ENV RUST_CONFIGURE_ARGS \
9898
--enable-sanitizers \
9999
--enable-profiler \
100100
--set target.i686-unknown-linux-gnu.linker=clang \
101-
--build=i686-unknown-linux-gnu
101+
--build=i686-unknown-linux-gnu \
102+
--set rust.jemalloc
102103
ENV SCRIPT python2.7 ../x.py dist --build $HOSTS --host $HOSTS --target $HOSTS
103104
ENV CARGO_TARGET_I686_UNKNOWN_LINUX_GNU_LINKER=clang
104105

src/ci/docker/dist-x86_64-linux/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ ENV RUST_CONFIGURE_ARGS \
101101
--set target.x86_64-unknown-linux-gnu.linker=clang \
102102
--set target.x86_64-unknown-linux-gnu.ar=/rustroot/bin/llvm-ar \
103103
--set target.x86_64-unknown-linux-gnu.ranlib=/rustroot/bin/llvm-ranlib \
104-
--set llvm.thin-lto=true
104+
--set llvm.thin-lto=true \
105+
--set rust.jemalloc
105106
ENV SCRIPT python2.7 ../x.py dist --host $HOSTS --target $HOSTS
106107
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=clang
107108

src/librustc_driver/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ syntax = { path = "../libsyntax" }
3838
smallvec = { version = "0.6.5", features = ["union"] }
3939
syntax_ext = { path = "../libsyntax_ext" }
4040
syntax_pos = { path = "../libsyntax_pos" }
41+
42+
[dependencies.jemalloc-sys]
43+
version = '0.1.8'
44+
optional = true
45+
features = ['unprefixed_malloc_on_supported_platforms']

src/librustc_driver/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ extern crate syntax;
6464
extern crate syntax_ext;
6565
extern crate syntax_pos;
6666

67+
// Note that the linkage here should be all that we need, on Linux we're not
68+
// prefixing the symbols here so this should naturally override our default
69+
// allocator. On OSX it should override via the zone allocator. We shouldn't
70+
// enable this by default on other platforms, so other platforms aren't handled
71+
// here yet.
72+
#[cfg(feature = "jemalloc-sys")]
73+
extern crate jemalloc_sys;
74+
6775
use driver::CompileController;
6876
use pretty::{PpMode, UserIdentifiedItem};
6977

src/rustc/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ path = "rustc.rs"
1010
[dependencies]
1111
rustc_target = { path = "../librustc_target" }
1212
rustc_driver = { path = "../librustc_driver" }
13+
14+
[features]
15+
jemalloc = ['rustc_driver/jemalloc-sys']

src/tools/tidy/src/cargo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn verify(tomlfile: &Path, libfile: &Path, bad: &mut bool) {
8484

8585
// This is intentional, this dependency just makes the crate available
8686
// for others later on. Cover cases
87-
let whitelisted = whitelisted || krate.starts_with("panic");
87+
let whitelisted = krate.starts_with("panic");
8888
if toml.contains("name = \"std\"") && whitelisted {
8989
continue
9090
}

0 commit comments

Comments
 (0)