Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize jobserver try_acquire #1037

Merged
merged 8 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ edition = "2018"
rust-version = "1.63"

[dependencies]
jobserver = { version = "0.1.20", default-features = false, optional = true }
jobserver = { version = "0.1.30", default-features = false, optional = true }
once_cell = { version = "1.19", optional = true }

[target.'cfg(unix)'.dependencies]
# Don't turn on the feature "std" for this, see https://github.com/rust-lang/cargo/issues/4866
# which is still an issue with `resolver = "1"`.
libc = { version = "0.2.62", default-features = false, optional = true }

[features]
parallel = ["libc", "jobserver"]
parallel = ["libc", "jobserver", "once_cell"]

[dev-dependencies]
tempfile = "3"
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,7 @@ impl Build {
}

// Limit our parallelism globally with a jobserver.
let tokens = parallel::job_token::ActiveJobTokenServer::new()?;
let tokens = parallel::job_token::ActiveJobTokenServer::new();

// When compiling objects in parallel we do a few dirty tricks to speed
// things up:
Expand Down
102 changes: 58 additions & 44 deletions src/parallel/job_token.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{marker::PhantomData, mem::MaybeUninit, sync::Once};
use std::marker::PhantomData;

use crate::Error;

use once_cell::sync::OnceCell;

pub(crate) struct JobToken(PhantomData<()>);

impl JobToken {
Expand Down Expand Up @@ -34,19 +36,13 @@ impl JobTokenServer {
/// that has to be static so that it will be shared by all cc
/// compilation.
fn new() -> &'static Self {
// TODO: Replace with a OnceLock once MSRV is 1.70
NobodyXu marked this conversation as resolved.
Show resolved Hide resolved
static INIT: Once = Once::new();
static mut JOBSERVER: MaybeUninit<JobTokenServer> = MaybeUninit::uninit();

unsafe {
INIT.call_once(|| {
let server = inherited_jobserver::JobServer::from_env()
.map(Self::Inherited)
.unwrap_or_else(|| Self::InProcess(inprocess_jobserver::JobServer::new()));
JOBSERVER.write(server);
});
JOBSERVER.assume_init_ref()
}
static JOBSERVER: OnceCell<JobTokenServer> = OnceCell::new();

JOBSERVER.get_or_init(|| {
unsafe { inherited_jobserver::JobServer::from_env() }
.map(Self::Inherited)
.unwrap_or_else(|| Self::InProcess(inprocess_jobserver::JobServer::new()))
})
}
}

Expand All @@ -56,14 +52,12 @@ pub(crate) enum ActiveJobTokenServer {
}

impl ActiveJobTokenServer {
pub(crate) fn new() -> Result<Self, Error> {
pub(crate) fn new() -> Self {
match JobTokenServer::new() {
JobTokenServer::Inherited(inherited_jobserver) => {
inherited_jobserver.enter_active().map(Self::Inherited)
}
JobTokenServer::InProcess(inprocess_jobserver) => {
Ok(Self::InProcess(inprocess_jobserver))
Self::Inherited(inherited_jobserver.enter_active())
}
JobTokenServer::InProcess(inprocess_jobserver) => Self::InProcess(inprocess_jobserver),
}
}

Expand All @@ -76,7 +70,7 @@ impl ActiveJobTokenServer {
}

mod inherited_jobserver {
use super::JobToken;
use super::{JobToken, OnceCell};

use crate::{parallel::async_executor::YieldOnce, Error, ErrorKind};

Expand Down Expand Up @@ -139,31 +133,39 @@ mod inherited_jobserver {
}
}

pub(super) fn enter_active(&self) -> Result<ActiveJobServer<'_>, Error> {
ActiveJobServer::new(self)
pub(super) fn enter_active(&self) -> ActiveJobServer<'_> {
ActiveJobServer {
jobserver: self,
helper_thread: OnceCell::new(),
}
}
}

pub(crate) struct ActiveJobServer<'a> {
jobserver: &'a JobServer,
helper_thread: jobserver::HelperThread,
struct HelperThread {
inner: jobserver::HelperThread,
/// When rx is dropped, all the token stored within it will be dropped.
rx: mpsc::Receiver<io::Result<jobserver::Acquired>>,
}

impl<'a> ActiveJobServer<'a> {
fn new(jobserver: &'a JobServer) -> Result<Self, Error> {
impl HelperThread {
fn new(jobserver: &JobServer) -> Result<Self, Error> {
let (tx, rx) = mpsc::channel();

Ok(Self {
rx,
helper_thread: jobserver.inner.clone().into_helper_thread(move |res| {
inner: jobserver.inner.clone().into_helper_thread(move |res| {
let _ = tx.send(res);
})?,
jobserver,
})
}
}

pub(crate) struct ActiveJobServer<'a> {
jobserver: &'a JobServer,
helper_thread: OnceCell<HelperThread>,
}

impl<'a> ActiveJobServer<'a> {
pub(super) async fn acquire(&self) -> Result<JobToken, Error> {
let mut has_requested_token = false;

Expand All @@ -173,26 +175,38 @@ mod inherited_jobserver {
break Ok(JobToken::new());
}

// Cold path, no global implicit token, obtain one
match self.rx.try_recv() {
Ok(res) => {
let acquired = res?;
match self.jobserver.inner.try_acquire() {
Ok(Some(acquired)) => {
acquired.drop_without_releasing();
break Ok(JobToken::new());
}
Err(mpsc::TryRecvError::Disconnected) => {
break Err(Error::new(
ErrorKind::JobserverHelpThreadError,
"jobserver help thread has returned before ActiveJobServer is dropped",
))
}
Err(mpsc::TryRecvError::Empty) => {
if !has_requested_token {
self.helper_thread.request_token();
has_requested_token = true;
Ok(None) => YieldOnce::default().await,
Err(err) if err.kind() == io::ErrorKind::Unsupported => {
// Fallback to creating a help thread with blocking acquire
let helper_thread = self
.helper_thread
.get_or_try_init(|| HelperThread::new(&self.jobserver))?;

match helper_thread.rx.try_recv() {
Ok(res) => {
let acquired = res?;
acquired.drop_without_releasing();
break Ok(JobToken::new());
}
Err(mpsc::TryRecvError::Disconnected) => break Err(Error::new(
ErrorKind::JobserverHelpThreadError,
"jobserver help thread has returned before ActiveJobServer is dropped",
)),
Err(mpsc::TryRecvError::Empty) => {
if !has_requested_token {
helper_thread.inner.request_token();
has_requested_token = true;
}
YieldOnce::default().await
}
}
YieldOnce::default().await
}
Err(err) => break Err(err.into()),
}
}
}
Expand Down