-
Notifications
You must be signed in to change notification settings - Fork 35
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
add Buffer pool support #276
Open
Sherlock-Holo
wants to merge
16
commits into
compio-rs:master
Choose a base branch
from
Sherlock-Holo:buffer-pool
base: master
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
16 commits
Select commit
Hold shift + click to select a range
3449e02
feat: add io-uring buffer pool
Sherlock-Holo b500f45
add compio_runtime::buffer_pool
Sherlock-Holo e674724
feat(driver): implement more traits for BorrowedBuffer
Sherlock-Holo f89048f
doc: hide as_driver_buffer_pool and get_buffer methods
Sherlock-Holo e90e0c6
feat(driver): add ReadAtBufferPool OpCode
Sherlock-Holo 92e1e69
feat(fs): implement read_at_buffer_pool
Sherlock-Holo c0ba96c
feat(net): implement recv_buffer_pool for udp
Sherlock-Holo a94fa7e
feat(net): implement recv_buffer_pool for uds
Sherlock-Holo 5f5462e
feat: add fallback buffer pool implement
Sherlock-Holo 9a9b673
feat: add RecvBufferPool and ReadAtBufferPool for all drivers
Sherlock-Holo aaf430c
feat(runtime): use generic driver BufferPool and BorrowedBuffer
Sherlock-Holo db360e2
feat: rewrite all buffer pool related methods
Sherlock-Holo ece142e
feat(io): add AsyncReadBufferPool and AsyncReadAtBufferPool
Sherlock-Holo bae934a
feat: make io-uring-buf-ring as an optional feature
Sherlock-Holo 439ff9c
remove async fd read buffer pool test
Sherlock-Holo c8353df
feat(runtime): add runtime check to make sure buffer pool won't cause UB
Sherlock-Holo 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 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 |
---|---|---|
|
@@ -42,6 +42,7 @@ socket2 = { workspace = true } | |
[target.'cfg(windows)'.dependencies] | ||
aligned-array = "1.0.1" | ||
once_cell = { workspace = true } | ||
pin-project-lite = { workspace = true } | ||
windows-sys = { workspace = true, features = [ | ||
"Win32_Foundation", | ||
"Win32_Networking_WinSock", | ||
|
@@ -58,12 +59,16 @@ windows-sys = { workspace = true, features = [ | |
# Linux specific dependencies | ||
[target.'cfg(target_os = "linux")'.dependencies] | ||
io-uring = { version = "0.6.2", optional = true } | ||
io_uring_buf_ring = { version = "0.1.0", optional = true } | ||
polling = { version = "3.3.0", optional = true } | ||
os_pipe = { workspace = true, optional = true } | ||
paste = { workspace = true } | ||
pin-project-lite = { workspace = true } | ||
slab = { version = "0.4.9", optional = true } | ||
|
||
# Other platform dependencies | ||
[target.'cfg(all(not(target_os = "linux"), unix))'.dependencies] | ||
pin-project-lite = { workspace = true } | ||
polling = "3.3.0" | ||
os_pipe = { workspace = true } | ||
|
||
|
@@ -77,11 +82,13 @@ compio-buf = { workspace = true, features = ["arrayvec"] } | |
|
||
[features] | ||
default = ["io-uring"] | ||
io-uring = ["dep:io-uring"] | ||
polling = ["dep:polling", "dep:os_pipe"] | ||
|
||
io-uring-sqe128 = [] | ||
io-uring-cqe32 = [] | ||
io-uring-socket = [] | ||
io-uring-buf-ring = ["dep:io_uring_buf_ring", "dep:slab"] | ||
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. Need to check in the fusion driver if this feature enabled. |
||
|
||
iocp-global = [] | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use std::{ | ||
borrow::{Borrow, BorrowMut}, | ||
cell::RefCell, | ||
collections::VecDeque, | ||
fmt::{Debug, Formatter}, | ||
mem::ManuallyDrop, | ||
ops::{Deref, DerefMut}, | ||
}; | ||
|
||
use compio_buf::{IntoInner, Slice}; | ||
|
||
/// Buffer pool | ||
/// | ||
/// A buffer pool to allow user no need to specify a specific buffer to do the | ||
/// IO operation | ||
pub struct BufferPool { | ||
buffers: RefCell<VecDeque<Vec<u8>>>, | ||
} | ||
|
||
impl Debug for BufferPool { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("BufferPool").finish_non_exhaustive() | ||
} | ||
} | ||
|
||
impl BufferPool { | ||
pub(crate) fn new(buffer_len: u16, buffer_size: usize) -> Self { | ||
let buffers = (0..buffer_len.next_power_of_two()) | ||
.map(|_| Vec::with_capacity(buffer_size)) | ||
.collect(); | ||
|
||
Self { | ||
buffers: RefCell::new(buffers), | ||
} | ||
} | ||
|
||
pub(crate) fn get_buffer(&self) -> Option<Vec<u8>> { | ||
self.buffers.borrow_mut().pop_front() | ||
} | ||
|
||
pub(crate) fn add_buffer(&self, mut buffer: Vec<u8>) { | ||
buffer.clear(); | ||
self.buffers.borrow_mut().push_back(buffer) | ||
} | ||
} | ||
|
||
/// Buffer borrowed from buffer pool | ||
/// | ||
/// When IO operation finish, user will obtain a `BorrowedBuffer` to access the | ||
/// filled data | ||
pub struct BorrowedBuffer<'a> { | ||
buffer: ManuallyDrop<Slice<Vec<u8>>>, | ||
pool: &'a BufferPool, | ||
} | ||
|
||
impl<'a> BorrowedBuffer<'a> { | ||
pub(crate) fn new(buffer: Slice<Vec<u8>>, pool: &'a BufferPool) -> Self { | ||
Self { | ||
buffer: ManuallyDrop::new(buffer), | ||
pool, | ||
} | ||
} | ||
} | ||
|
||
impl Debug for BorrowedBuffer<'_> { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("BorrowedBuffer").finish_non_exhaustive() | ||
} | ||
} | ||
|
||
impl Drop for BorrowedBuffer<'_> { | ||
fn drop(&mut self) { | ||
let buffer = unsafe { | ||
// Safety: we won't take self.buffer again | ||
ManuallyDrop::take(&mut self.buffer) | ||
}; | ||
self.pool.add_buffer(buffer.into_inner()); | ||
} | ||
} | ||
|
||
impl Deref for BorrowedBuffer<'_> { | ||
type Target = [u8]; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.buffer.deref() | ||
} | ||
} | ||
|
||
impl DerefMut for BorrowedBuffer<'_> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
self.buffer.deref_mut() | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for BorrowedBuffer<'_> { | ||
fn as_ref(&self) -> &[u8] { | ||
self.deref() | ||
} | ||
} | ||
|
||
impl AsMut<[u8]> for BorrowedBuffer<'_> { | ||
fn as_mut(&mut self) -> &mut [u8] { | ||
self.deref_mut() | ||
} | ||
} | ||
|
||
impl Borrow<[u8]> for BorrowedBuffer<'_> { | ||
fn borrow(&self) -> &[u8] { | ||
self.deref() | ||
} | ||
} | ||
|
||
impl BorrowMut<[u8]> for BorrowedBuffer<'_> { | ||
fn borrow_mut(&mut self) -> &mut [u8] { | ||
self.deref_mut() | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,145 @@ | ||
use std::{ | ||
borrow::{Borrow, BorrowMut}, | ||
fmt::{Debug, Formatter}, | ||
ops::{Deref, DerefMut}, | ||
}; | ||
|
||
/// Buffer pool | ||
/// | ||
/// A buffer pool to allow user no need to specify a specific buffer to do the | ||
/// IO operation | ||
pub struct BufferPool { | ||
inner: BufferPollInner, | ||
} | ||
|
||
impl Debug for BufferPool { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("BufferPool").finish_non_exhaustive() | ||
} | ||
} | ||
|
||
impl BufferPool { | ||
pub(crate) fn new_io_uring(buffer_pool: super::iour::buffer_pool::BufferPool) -> Self { | ||
Self { | ||
inner: BufferPollInner::IoUring(buffer_pool), | ||
} | ||
} | ||
|
||
pub(crate) fn as_io_uring(&self) -> &super::iour::buffer_pool::BufferPool { | ||
match &self.inner { | ||
BufferPollInner::IoUring(inner) => inner, | ||
BufferPollInner::Poll(_) => panic!("BufferPool type is not poll type"), | ||
} | ||
} | ||
|
||
pub(crate) fn as_poll(&self) -> &crate::fallback_buffer_pool::BufferPool { | ||
match &self.inner { | ||
BufferPollInner::Poll(inner) => inner, | ||
BufferPollInner::IoUring(_) => panic!("BufferPool type is not io-uring type"), | ||
} | ||
} | ||
|
||
pub(crate) fn new_poll(buffer_pool: crate::fallback_buffer_pool::BufferPool) -> Self { | ||
Self { | ||
inner: BufferPollInner::Poll(buffer_pool), | ||
} | ||
} | ||
|
||
pub(crate) fn into_poll(self) -> crate::fallback_buffer_pool::BufferPool { | ||
match self.inner { | ||
BufferPollInner::IoUring(_) => { | ||
panic!("BufferPool type is not io-uring type") | ||
} | ||
BufferPollInner::Poll(inner) => inner, | ||
} | ||
} | ||
|
||
pub(crate) fn into_io_uring(self) -> super::iour::buffer_pool::BufferPool { | ||
match self.inner { | ||
BufferPollInner::IoUring(inner) => inner, | ||
BufferPollInner::Poll(_) => panic!("BufferPool type is not poll type"), | ||
} | ||
} | ||
} | ||
|
||
enum BufferPollInner { | ||
IoUring(super::iour::buffer_pool::BufferPool), | ||
Poll(crate::fallback_buffer_pool::BufferPool), | ||
} | ||
|
||
/// Buffer borrowed from buffer pool | ||
/// | ||
/// When IO operation finish, user will obtain a `BorrowedBuffer` to access the | ||
/// filled data | ||
pub struct BorrowedBuffer<'a> { | ||
inner: BorrowedBufferInner<'a>, | ||
} | ||
|
||
impl<'a> BorrowedBuffer<'a> { | ||
pub(crate) fn new_io_uring(buffer: super::iour::buffer_pool::BorrowedBuffer<'a>) -> Self { | ||
Self { | ||
inner: BorrowedBufferInner::IoUring(buffer), | ||
} | ||
} | ||
|
||
pub(crate) fn new_poll(buffer: crate::fallback_buffer_pool::BorrowedBuffer<'a>) -> Self { | ||
Self { | ||
inner: BorrowedBufferInner::Poll(buffer), | ||
} | ||
} | ||
} | ||
|
||
enum BorrowedBufferInner<'a> { | ||
IoUring(super::iour::buffer_pool::BorrowedBuffer<'a>), | ||
Poll(crate::fallback_buffer_pool::BorrowedBuffer<'a>), | ||
} | ||
|
||
impl Debug for BorrowedBuffer<'_> { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("BorrowedBuffer").finish_non_exhaustive() | ||
} | ||
} | ||
|
||
impl Deref for BorrowedBuffer<'_> { | ||
type Target = [u8]; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
match &self.inner { | ||
BorrowedBufferInner::IoUring(inner) => inner.deref(), | ||
BorrowedBufferInner::Poll(inner) => inner.deref(), | ||
} | ||
} | ||
} | ||
|
||
impl DerefMut for BorrowedBuffer<'_> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
match &mut self.inner { | ||
BorrowedBufferInner::IoUring(inner) => inner.deref_mut(), | ||
BorrowedBufferInner::Poll(inner) => inner.deref_mut(), | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for BorrowedBuffer<'_> { | ||
fn as_ref(&self) -> &[u8] { | ||
self.deref() | ||
} | ||
} | ||
|
||
impl AsMut<[u8]> for BorrowedBuffer<'_> { | ||
fn as_mut(&mut self) -> &mut [u8] { | ||
self.deref_mut() | ||
} | ||
} | ||
|
||
impl Borrow<[u8]> for BorrowedBuffer<'_> { | ||
fn borrow(&self) -> &[u8] { | ||
self.deref() | ||
} | ||
} | ||
|
||
impl BorrowMut<[u8]> for BorrowedBuffer<'_> { | ||
fn borrow_mut(&mut self) -> &mut [u8] { | ||
self.deref_mut() | ||
} | ||
} |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If all targets need
pin-project-lite
, just write it once in[dependencies]