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

WIP: add api to check whether SQ and CQ count against memlock rlimit #36

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tokio-epoll-uring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ mod system;
#[cfg(target_os = "linux")]
pub use {
crate::system::submission::op_fut::Error,
system::kernel_support,
system::lifecycle::handle::SystemHandle,
system::lifecycle::thread_local::{thread_local_system, Handle},
system::lifecycle::System,
Expand Down
56 changes: 56 additions & 0 deletions tokio-epoll-uring/src/system/kernel_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::{io::Write, os::unix::process::CommandExt};

/// Check whether a [`crate::System`]'s consumes memlock rlimit budget.
///
/// Unsafe because this function forks & uses [`std::os::unix::process::Command::pre_exec`].
///
/// Panics in a bunch of places.
///
pub unsafe fn does_system_consume_memlock_rlimit_budget() -> bool {
// Fork a child process.
// In the child process, set up a tokio-epoll-uring system.
// Compare the memlock rlimit usage before and after.

// poor man's bincode
#[repr(C, packed)]
struct RlimitPrePost {
pre: (u64, u64),
post: (u64, u64),
}
// Ensure `true` binary exists.
std::process::Command::new("true")
.output()
.expect("canont exec `true` binary");
let output = std::process::Command::new("true")
.pre_exec(|| {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()?;
let pre = nix::sys::resource::getrlimit(nix::sys::resource::Resource::RLIMIT_MEMLOCK)
.unwrap();
let system = rt.block_on(crate::System::launch()).unwrap();
// use the memory
let ((), res) = rt.block_on(system.nop());
res.unwrap();
let post = nix::sys::resource::getrlimit(nix::sys::resource::Resource::RLIMIT_MEMLOCK)
.unwrap();
Comment on lines +29 to +36
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make sense. What we want is not to get the limit, but the usage.

There's a file in procfs for that, the test I added in an earlier commit uses it.

let output = RlimitPrePost { pre, post };
let output_as_slice = std::slice::from_raw_parts(
&output as *const _ as *const u8,
std::mem::size_of_val(&output),
);
std::io::stdout().write_all(output_as_slice).unwrap();
Ok(())
})
.output()
.unwrap();
assert_eq!(output.status.code().unwrap(), 0);
assert_eq!(output.stdout.len(), std::mem::size_of::<RlimitPrePost>());
let (head, output, tail): (_, &[RlimitPrePost], _) = output.stdout.align_to();
assert_eq!(head.len(), 0);
assert_eq!(tail.len(), 0);
assert_eq!(output.len(), 1);
let RlimitPrePost { pre, post } = output[0];
assert!(pre <= post, "above code can't use _less_ locked memory");
pre < post
}
2 changes: 2 additions & 0 deletions tokio-epoll-uring/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ mod test_util;
mod tests;

pub(crate) const RING_SIZE: u32 = 128;

pub mod kernel_support;
Loading