-
Notifications
You must be signed in to change notification settings - Fork 41
Add back preadv2
optimization for try_acquire
on Linux
#90
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
Open
NobodyXu
wants to merge
12
commits into
rust-lang:main
Choose a base branch
from
NobodyXu:reintroduce-preadv2-optimization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c65cd8e
Add back `preadv2` optimization for `try_acquire` on Linux
NobodyXu f34c2bc
SFix compilation on musl: Vendor `RWF_NOWAIT` constant
NobodyXu 0d6baba
Enable `preadv2` optimization on android
NobodyXu dd0f271
Remove outdated comments
NobodyXu 6ad33aa
Fix `preadv2` implementation for x86_64 x32
NobodyXu 5602ef7
Hard code parameter `offset`
NobodyXu 5cf22b5
Hardcode more values in `preadv2`
NobodyXu 686fe22
Apply code review change
NobodyXu 181591c
Fix type of `offset` passed to `preadv2` syscall on x64
NobodyXu 9087cbe
Mark `preadv2` a safe function to call
NobodyXu 804310f
Refactor: Introduce variable `offset` to avoid hard-coded constants
NobodyXu 60cac97
Refactor: Use `cfg!` instead of `#[cfg]`
NobodyXu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,6 +285,30 @@ impl Client { | |
pub fn try_acquire(&self) -> io::Result<Option<Acquired>> { | ||
let mut buf = [0]; | ||
|
||
// On Linux, we can use preadv2 to do non-blocking read, | ||
// even if `O_NONBLOCK` is not set. | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
{ | ||
let read = self.read().as_raw_fd(); | ||
loop { | ||
match linux::non_blocking_read(read, &mut buf) { | ||
Ok(1) => return Ok(Some(Acquired { byte: buf[0] })), | ||
Ok(_) => { | ||
return Err(io::Error::new( | ||
io::ErrorKind::UnexpectedEof, | ||
"early EOF on jobserver pipe", | ||
)) | ||
} | ||
|
||
Err(e) if e.kind() == io::ErrorKind::WouldBlock => return Ok(None), | ||
Err(e) if e.kind() == io::ErrorKind::Interrupted => continue, | ||
Err(e) if e.kind() == io::ErrorKind::Unsupported => break, | ||
|
||
Err(err) => return Err(err), | ||
} | ||
} | ||
} | ||
|
||
let (mut fifo, is_non_blocking) = match self { | ||
Self::Fifo { | ||
file, | ||
|
@@ -368,6 +392,73 @@ impl Client { | |
} | ||
} | ||
|
||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
mod linux { | ||
use super::*; | ||
|
||
use libc::{iovec, off_t, ssize_t, syscall, SYS_preadv2}; | ||
|
||
// TODO: Replace this with libc::RWF_NOWAIT once they have it for musl | ||
// targets | ||
const RWF_NOWAIT: c_int = 0x00000008; | ||
NobodyXu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fn cvt_ssize(t: ssize_t) -> io::Result<ssize_t> { | ||
if t == -1 { | ||
Err(io::Error::last_os_error()) | ||
} else { | ||
Ok(t) | ||
} | ||
} | ||
|
||
fn preadv2(fd: c_int, iov: &iovec) -> ssize_t { | ||
let iovcnt: c_int = 1; | ||
let offset: off_t = -1; | ||
|
||
NobodyXu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if cfg!(all(target_arch = "x86_64", target_pointer_width = "64")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The branches here could use some comment. It's a bit surprising that 86-64 needs two special-cases but all other platforms (including powerpc, aarch64 and even i686) all go in the same bucket. |
||
unsafe { syscall(SYS_preadv2, fd, iov, iovcnt, offset, 0 as off_t, RWF_NOWAIT) } | ||
} else if cfg!(all(target_arch = "x86_64", target_pointer_width = "32")) { | ||
unsafe { syscall(SYS_preadv2, fd, iov, iovcnt, offset, RWF_NOWAIT) } | ||
} else { | ||
unsafe { | ||
syscall( | ||
SYS_preadv2, | ||
fd, | ||
iov, | ||
iovcnt, | ||
offset as libc::c_long, | ||
((offset as u64) >> 32) as libc::c_long, | ||
RWF_NOWAIT, | ||
) | ||
} | ||
} | ||
.try_into() | ||
.unwrap() | ||
} | ||
|
||
pub fn non_blocking_read(fd: c_int, buf: &mut [u8]) -> io::Result<usize> { | ||
static IS_NONBLOCKING_READ_UNSUPPORTED: AtomicBool = AtomicBool::new(false); | ||
|
||
if IS_NONBLOCKING_READ_UNSUPPORTED.load(Ordering::Relaxed) { | ||
return Err(io::ErrorKind::Unsupported.into()); | ||
} | ||
|
||
match cvt_ssize(preadv2( | ||
fd, | ||
&iovec { | ||
iov_base: buf.as_ptr() as *mut _, | ||
iov_len: buf.len(), | ||
}, | ||
)) { | ||
Ok(cnt) => Ok(cnt.try_into().unwrap()), | ||
Err(err) if matches!(err.raw_os_error(), Some(libc::EOPNOTSUPP | libc::ENOSYS)) => { | ||
IS_NONBLOCKING_READ_UNSUPPORTED.store(true, Ordering::Relaxed); | ||
Err(io::ErrorKind::Unsupported.into()) | ||
} | ||
Err(err) => Err(err), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Helper { | ||
thread: JoinHandle<()>, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.