-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rt: add new api for oneshot op submission and creation (#244)
This change adds a new API for creating and submitting oneshot operations. This new API is intended to obsolete the existing system, however transferring over has been left out of this PR to keep the change small. It is intended that a similar API for multishot operations be landed in a followup PR as well. This was also left out of this PR to keep the change small. This refactoring paves the way for further work on SQE linking, multishot operations, and other improvements and additions. The goal of this change is to allow us to split up the oneshot and multishot logic to allow for a cleaner and more extensible system, allow for user-provided operations, and allow users to control when and how their ops get submitted to the squeue.
- Loading branch information
1 parent
575d864
commit a021be7
Showing
20 changed files
with
312 additions
and
108 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,50 +1,59 @@ | ||
use crate::runtime::driver::op::{Completable, CqeResult, Op}; | ||
use crate::runtime::CONTEXT; | ||
use crate::{buf::BoundedBuf, io::SharedFd, BufResult}; | ||
use crate::{buf::BoundedBuf, io::SharedFd, BufResult, OneshotOutputTransform, UnsubmittedOneshot}; | ||
use io_uring::cqueue::Entry; | ||
use std::io; | ||
use std::marker::PhantomData; | ||
|
||
pub(crate) struct Write<T> { | ||
/// An unsubmitted write operation. | ||
pub type UnsubmittedWrite<T> = UnsubmittedOneshot<WriteData<T>, WriteTransform<T>>; | ||
|
||
#[allow(missing_docs)] | ||
pub struct WriteData<T> { | ||
/// Holds a strong ref to the FD, preventing the file from being closed | ||
/// while the operation is in-flight. | ||
#[allow(dead_code)] | ||
fd: SharedFd, | ||
_fd: SharedFd, | ||
|
||
buf: T, | ||
} | ||
|
||
impl<T: BoundedBuf> Op<Write<T>> { | ||
pub(crate) fn write_at(fd: &SharedFd, buf: T, offset: u64) -> io::Result<Op<Write<T>>> { | ||
use io_uring::{opcode, types}; | ||
|
||
CONTEXT.with(|x| { | ||
x.handle().expect("Not in a runtime context").submit_op( | ||
Write { | ||
fd: fd.clone(), | ||
buf, | ||
}, | ||
|write| { | ||
// Get raw buffer info | ||
let ptr = write.buf.stable_ptr(); | ||
let len = write.buf.bytes_init(); | ||
|
||
opcode::Write::new(types::Fd(fd.raw_fd()), ptr, len as _) | ||
.offset(offset as _) | ||
.build() | ||
}, | ||
) | ||
}) | ||
} | ||
#[allow(missing_docs)] | ||
pub struct WriteTransform<T> { | ||
_phantom: PhantomData<T>, | ||
} | ||
|
||
impl<T> Completable for Write<T> { | ||
impl<T> OneshotOutputTransform for WriteTransform<T> { | ||
type Output = BufResult<usize, T>; | ||
type StoredData = WriteData<T>; | ||
|
||
fn transform_oneshot_output(self, data: Self::StoredData, cqe: Entry) -> Self::Output { | ||
let res = if cqe.result() >= 0 { | ||
Ok(cqe.result() as usize) | ||
} else { | ||
Err(io::Error::from_raw_os_error(cqe.result())) | ||
}; | ||
|
||
fn complete(self, cqe: CqeResult) -> Self::Output { | ||
// Convert the operation result to `usize` | ||
let res = cqe.result.map(|v| v as usize); | ||
// Recover the buffer | ||
let buf = self.buf; | ||
(res, data.buf) | ||
} | ||
} | ||
|
||
impl<T: BoundedBuf> UnsubmittedWrite<T> { | ||
pub(crate) fn write_at(fd: &SharedFd, buf: T, offset: u64) -> Self { | ||
use io_uring::{opcode, types}; | ||
|
||
(res, buf) | ||
// Get raw buffer info | ||
let ptr = buf.stable_ptr(); | ||
let len = buf.bytes_init(); | ||
|
||
Self::new( | ||
WriteData { | ||
_fd: fd.clone(), | ||
buf, | ||
}, | ||
WriteTransform { | ||
_phantom: PhantomData::default(), | ||
}, | ||
opcode::Write::new(types::Fd(fd.raw_fd()), ptr, len as _) | ||
.offset(offset as _) | ||
.build(), | ||
) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.