#![cfg(all(
tokio_unstable,
feature = "io-uring",
feature = "rt",
feature = "fs",
target_os = "linux"
))]
use std::{
fs,
time::{Duration, Instant},
};
use io_uring::IoUring;
pub fn io_uring_supported() -> bool {
match IoUring::new(256) {
Ok(_) => true,
Err(e) if e.raw_os_error() == Some(libc::ENOSYS) => false,
Err(_) => unreachable!(
"The target should either support io_uring or return ENOSYS if not supported"
),
}
}
#[allow(dead_code)]
pub async fn assert_fds_are_not_leaking(count_before: usize, opened_files: usize, timeout: u64) {
let fd_check_start = Instant::now();
let max_leaked_fd = opened_files / 2;
while fd_check_start.elapsed() < Duration::from_secs(timeout) {
tokio::task::yield_now().await;
let fd_count_after_cancel = fs::read_dir("/proc/self/fd").unwrap().count();
let leaked = fd_count_after_cancel.saturating_sub(count_before);
if leaked <= max_leaked_fd {
return;
}
}
panic!("Number of FDs is staying above {max_leaked_fd}. There is probably an FD leak.");
}