Skip to content

Commit

Permalink
Maintain pending io_uring ops count for early return on run_once
Browse files Browse the repository at this point in the history
Add a `pending_ops` field to `InnerLinuxIO` struct which is incremented
for each operation submitted to the ring and decremented when they are
taken off the completion queue. With this, we can exit from run_once if
there are no pending operations. Otherwise, in that case, it would hang
indefinitely due to call of `ring.submit_and_wait(1)`
  • Loading branch information
arpit-saxena committed Sep 26, 2024
1 parent 918578f commit cadf9a4
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions core/io/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{LimboError, Result};
use libc::{c_short, fcntl, flock, iovec, F_SETLK};
use log::{debug, trace};
use nix::fcntl::{FcntlArg, OFlag};
use std::cell::RefCell;
use std::cell::{RefCell, RefMut};
use std::fmt;
use std::os::unix::io::AsRawFd;
use std::rc::Rc;
Expand Down Expand Up @@ -37,6 +37,7 @@ pub struct InnerLinuxIO {
ring: io_uring::IoUring,
iovecs: [iovec; MAX_IOVECS],
next_iovec: usize,
pending_ops: usize,
}

impl LinuxIO {
Expand All @@ -49,6 +50,7 @@ impl LinuxIO {
iov_len: 0,
}; MAX_IOVECS],
next_iovec: 0,
pending_ops: 0,
};
Ok(Self {
inner: Rc::new(RefCell::new(inner)),
Expand Down Expand Up @@ -89,10 +91,15 @@ impl IO for LinuxIO {

fn run_once(&self) -> Result<()> {
trace!("run_once()");
let mut inner = self.inner.borrow_mut();
let ring = &mut inner.ring;
let inner = self.inner.borrow_mut();
let (mut pending_ops, mut ring) = RefMut::map_split(inner, |inner_ref: &mut InnerLinuxIO| (&mut inner_ref.pending_ops, &mut inner_ref.ring));
if *pending_ops == 0 {
return Ok(());
}

ring.submit_and_wait(1)?;
while let Some(cqe) = ring.completion().next() {
*pending_ops -= 1;
let result = cqe.result();
if result < 0 {
return Err(LimboError::LinuxIOError(format!(
Expand Down Expand Up @@ -198,6 +205,7 @@ impl File for LinuxFile {
.push(&read_e)
.expect("submission queue is full");
}
io.pending_ops += 1;
Ok(())
}

Expand All @@ -224,6 +232,7 @@ impl File for LinuxFile {
.push(&write)
.expect("submission queue is full");
}
io.pending_ops += 1;
Ok(())
}
}
Expand Down

0 comments on commit cadf9a4

Please sign in to comment.