io-engine 0.10.0

Library for block-based IO, intend to mask AIO/io_uring underneath.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use crate::callback_worker::Worker;
use rustix::fs::{FallocateFlags, fallocate, fsync};

use crate::tasks::{CbArgs, IOAction, IOEvent};
use crossfire::{BlockingRxTrait, Rx, Tx, spsc};
use rustix::io::Errno;
use std::fs::File;
use std::mem::MaybeUninit;
use std::os::fd::RawFd;
use std::os::unix::io::BorrowedFd;
use std::sync::{
    Arc,
    atomic::{AtomicBool, Ordering},
};
use std::{cell::UnsafeCell, io, os::fd::AsRawFd, thread, time::Duration};

// Relevant symbols from the native bindings exposed via aio-bindings
use io_engine_aio_bindings::{
    __NR_io_destroy, __NR_io_getevents, __NR_io_setup, __NR_io_submit, IOCB_CMD_PREAD,
    aio_context_t, io_event, iocb, syscall, timespec,
};

const EXIT_MAGIC: u64 = 0xFFFF_FFFF_FFFF_0000;

pub struct AioSlot<C: CbArgs> {
    iocb: iocb,
    _event: MaybeUninit<Box<IOEvent<C>>>,
}

impl<C: CbArgs> AioSlot<C> {
    pub fn new(slot_id: u64) -> Self {
        Self {
            iocb: iocb { aio_data: slot_id, aio_reqprio: 1, ..Default::default() },
            _event: MaybeUninit::uninit(),
        }
    }

    #[inline(always)]
    pub fn fill_exit_slot(&mut self, null_fd: RawFd) {
        let iocb = &mut self.iocb;
        // NOTE: the aio_data is init with slot_id, normally we don't touch it;
        // on exit the chosen slot will fill with EXIT_MAGIC on higher bits.
        // This slot will never used again, because no more io from upstream channel.
        iocb.aio_data |= EXIT_MAGIC;
        iocb.aio_fildes = null_fd as libc::__u32;
        iocb.aio_lio_opcode = IOCB_CMD_PREAD as libc::__u16;
        iocb.aio_buf = 0;
        iocb.aio_nbytes = 0;
        iocb.aio_offset = 0;
    }

    #[inline(always)]
    fn fill_noop_slot(&mut self, event: Box<IOEvent<C>>, null_fd: RawFd) {
        let iocb = &mut self.iocb;
        iocb.aio_lio_opcode = IOCB_CMD_PREAD as libc::__u16;
        iocb.aio_fildes = null_fd as libc::__u32;
        iocb.aio_buf = 0;
        iocb.aio_nbytes = 0;
        iocb.aio_offset = 0;
        self._event.write(event);
    }

    #[inline(always)]
    pub fn fill_buffer_slot(&mut self, mut event: Box<IOEvent<C>>) {
        let iocb = &mut self.iocb;
        iocb.aio_fildes = event.fd as libc::__u32;
        let (_offset, p, l) = event.get_param_for_io();
        iocb.aio_lio_opcode = event.action as libc::__u16;
        iocb.aio_buf = p as u64;
        iocb.aio_nbytes = l as u64;
        iocb.aio_offset = _offset as i64;
        self._event.write(event);
    }

    #[inline(always)]
    pub fn set_result<W: Worker<C>>(&mut self, written: usize, cb: &W) {
        let mut event = unsafe { self._event.assume_init_read() };
        if event.action.is_read_write() {
            // If it was a zero-length read (exit signal), callback is usually None, so this is safe.
            event.set_copied(written);
        }
        // for ALLOC or Fsync, the result is already set
        cb.done(event);
    }

    #[inline(always)]
    pub fn set_error<W: Worker<C>>(&mut self, errno: i32, cb: &W) {
        let mut event = unsafe { self._event.assume_init_read() };
        if event.action.is_read_write() {
            event.set_error(errno);
        }
        // for ALLOC or Fsync, the result is already set
        cb.done(event);
    }

    #[inline]
    fn submit_one(&mut self, aio_context: aio_context_t) -> Result<(), c_long> {
        let mut iocb_ptr: *mut iocb = &mut self.iocb as *mut _;
        let res = io_submit(aio_context, 1, &mut iocb_ptr);
        if res == 1 { Ok(()) } else { Err(res) }
    }

    #[inline]
    fn event(&mut self) -> &mut Box<IOEvent<C>> {
        unsafe { self._event.assume_init_mut() }
    }
}

struct AioInner<C: CbArgs> {
    depth: usize,
    context: aio_context_t,
    // NOTE: linux fs/aio.c submit_one has never dealt with IOCB_CMD_NOOP, it returns EINVAL.
    // so we have to open /dev/null to mock noop with 0 size read.
    null_file: File,
    slots: Vec<UnsafeCell<AioSlot<C>>>,
    // For poll_loop() count the inflict task number,
    // the submit_loop() might cache one slot_id but no way to put back into free_slot channel,
    // so submit_loop() will set the flag if `free_slot_temp` is not empty
    temp_slot_drop: AtomicBool,
}

impl<C: CbArgs> AioInner<C> {
    #[inline(always)]
    fn get_slot(&self, slot_id: u16) -> &mut AioSlot<C> {
        unsafe { &mut *self.slots[slot_id as usize].get() }
    }
}

unsafe impl<C: CbArgs> Send for AioInner<C> {}
unsafe impl<C: CbArgs> Sync for AioInner<C> {}

pub struct AioDriver<C: CbArgs, Q: BlockingRxTrait<Box<IOEvent<C>>>, W: Worker<C>> {
    _marker: std::marker::PhantomData<(C, Q, W)>,
}

impl<C: CbArgs, Q: BlockingRxTrait<Box<IOEvent<C>>> + Send + 'static, W: Worker<C> + Send + 'static>
    AioDriver<C, Q, W>
{
    pub fn start(depth: usize, rx: Q, cb_workers: W) -> io::Result<()> {
        let mut aio_context: aio_context_t = 0;
        if io_setup(depth as c_long, &mut aio_context) != 0 {
            return Err(io::Error::last_os_error());
        }
        let mut slots = Vec::with_capacity(depth);
        for slot_id in 0..depth {
            slots.push(UnsafeCell::new(AioSlot::<C>::new(slot_id as u64)));
        }
        let null_file = match File::open("/dev/null") {
            Err(e) => {
                error!("cannot open /dev/null: {}", e);
                return Err(e);
            }
            Ok(f) => f,
        };
        let inner = Arc::new(AioInner {
            depth,
            context: aio_context,
            slots,
            null_file,
            temp_slot_drop: AtomicBool::new(false),
        });

        let (s_free, r_free) = spsc::bounded_blocking::<u16>(depth);
        for i in 0..depth {
            let _ = s_free.send(i as u16);
        }
        let inner_submit = inner.clone();
        thread::spawn(move || Self::submit_loop(inner_submit, rx, r_free));
        thread::spawn(move || Self::poll_loop(inner, cb_workers, s_free));
        Ok(())
    }

    /// This worker process IOEvent fallocate & fsync
    fn background_worker(inner: Arc<AioInner<C>>, rx: Rx<spsc::Array<u16>>) {
        loop {
            match rx.recv() {
                Ok(slot_id) => {
                    let slot = inner.get_slot(slot_id);
                    let event = slot.event();
                    let mut size: usize = 0;
                    let fd = unsafe { BorrowedFd::borrow_raw(event.fd) };
                    let res: Result<(), Errno> = match event.action {
                        IOAction::Alloc => {
                            size = event.get_size() as usize;
                            fallocate(fd, FallocateFlags::empty(), event.offset as u64, size as u64)
                        }
                        IOAction::Fsync => fsync(fd),
                        _ => Err(Errno::INVAL), // Should not happen
                    };
                    if let Err(e) = res {
                        event.set_error(e.raw_os_error());
                    } else {
                        event.set_copied(size);
                    }
                    let _ = slot.submit_one(inner.context);
                }
                Err(_) => return, // exit
            }
        }
    }

    fn submit_loop(inner: Arc<AioInner<C>>, rx: Q, free_recv: Rx<spsc::Array<u16>>) {
        let depth = inner.depth;
        let mut iocbs = Vec::<*mut iocb>::with_capacity(depth);
        let aio_context = inner.context;
        let mut background_tx: Option<Tx<spsc::Array<u16>>> = None;
        let mut free_slot_temp: Option<u16> = None;

        macro_rules! event_fill_slot {
            ($event: expr, $slot_id: expr) => {{
                let slot = inner.get_slot($slot_id);
                if $event.action.is_read_write() {
                    slot.fill_buffer_slot($event);
                    iocbs.push(&mut slot.iocb as *mut iocb);
                } else {
                    slot.fill_noop_slot($event, inner.null_file.as_raw_fd());
                    if background_tx.is_none() {
                        let _inner = inner.clone();
                        let (_tx, _rx) = spsc::bounded_blocking::<u16>(depth);
                        thread::spawn(move || Self::background_worker(_inner, _rx));
                        background_tx = Some(_tx);
                    }
                    background_tx.as_ref().unwrap().send($slot_id).expect("ok");
                }
            }};
        }
        'MAIN: loop {
            match rx.recv() {
                Ok(event) => {
                    let slot_id =
                        free_slot_temp.take().unwrap_or_else(|| free_recv.recv().unwrap());
                    event_fill_slot!(event, slot_id);
                    while iocbs.len() < depth {
                        if let Ok(slot_id) = free_recv.try_recv() {
                            if let Ok(event) = rx.try_recv() {
                                event_fill_slot!(event, slot_id);
                            } else {
                                free_slot_temp.replace(slot_id);
                                break;
                            }
                        }
                    }
                }
                Err(_) => {
                    // Queue closed. Time to exit.
                    // We need a free slot to submit the exit signal.
                    // We block to get one because we must signal exit to the poller.
                    let slot_id = free_recv.recv().unwrap();
                    let slot = inner.get_slot(slot_id);
                    slot.fill_exit_slot(inner.null_file.as_raw_fd());
                    if let Err(e) = slot.submit_one(aio_context) {
                        error!("Failed to submit exit signal: {}", e);
                    }
                    break 'MAIN;
                }
            }

            // 3. Submit batch
            if !iocbs.is_empty() {
                let mut done: libc::c_long = 0;
                let mut left = iocbs.len();

                // Reserve quota
                'submit: loop {
                    let result = unsafe {
                        let arr = iocbs.as_mut_ptr().add(done as usize);
                        io_submit(aio_context, left as libc::c_long, arr)
                    };

                    if result < 0 {
                        // All remaining failed
                        if -result == Errno::INTR.raw_os_error() as i64 {
                            continue 'submit;
                        }
                        error!("io_submit error: {}", result);
                        break 'submit;
                    } else {
                        // Success (partial or full)
                        if result == left as libc::c_long {
                            trace!("io submit {} events", result);
                            break 'submit;
                        } else {
                            done += result;
                            left -= result as usize;
                            trace!("io submit {}/{} events", result, left);
                        }
                    }
                }
                iocbs.clear();
            }
        }
        if free_slot_temp.is_some() {
            inner.temp_slot_drop.store(true, Ordering::SeqCst);
        }
        info!("io_submit worker closed");
    }

    fn poll_loop(inner: Arc<AioInner<C>>, cb_workers: W, free_sender: Tx<spsc::Array<u16>>) {
        let depth = inner.depth;
        let mut infos = Vec::<io_event>::with_capacity(depth);
        let aio_context = inner.context;
        let mut is_running = true;

        macro_rules! has_inflight {
            () => {{
                if is_running {
                    true
                } else if free_sender.is_full() {
                    false
                } else if inner.temp_slot_drop.load(Ordering::Acquire) {
                    free_sender.len() + 1 < depth
                } else {
                    true
                }
            }};
        }
        while has_inflight!() {
            infos.clear();
            let result = io_getevents(
                aio_context,
                1,
                depth as i64,
                infos.as_mut_ptr(),
                std::ptr::null_mut(),
            );

            if result < 0 {
                if -result == Errno::INTR.raw_os_error() as i64 {
                    continue;
                }
                error!("io_getevents errno: {}", -result);
                thread::sleep(Duration::from_millis(10));
                continue;
            }

            assert!(result > 0);
            unsafe {
                infos.set_len(result as usize);
            }
            for info in &infos {
                let data = info.data;
                let slot_id = data as u16;
                // refer to fill_exit_slot()
                if data & EXIT_MAGIC == 0 {
                    let slot = inner.get_slot(slot_id);
                    if info.res >= 0 {
                        slot.set_result(info.res as usize, &cb_workers);
                    } else {
                        slot.set_error((-info.res) as i32, &cb_workers);
                    }
                } else {
                    // exit signal
                    is_running = false;
                }
                let _ = free_sender.send(slot_id);
            }
        }
        info!("io_poll worker exit cleaning up");
        // poll_loop() always exit last, after poll_submit()
        let _ = io_destroy(aio_context);
    }
}

use libc::c_long;

// -----------------------------------------------------------------------------------------------
// Inline functions that wrap the kernel calls for the entry points corresponding to Linux
// AIO functions
// -----------------------------------------------------------------------------------------------

// Initialize an AIO context for a given submission queue size within the kernel.
//
// See [io_setup(7)](http://man7.org/linux/man-pages/man2/io_setup.2.html) for details.
#[inline(always)]
fn io_setup(nr: c_long, ctxp: *mut aio_context_t) -> c_long {
    unsafe { syscall(__NR_io_setup as c_long, nr, ctxp) }
}

// Destroy an AIO context.
//
// See [io_destroy(7)](http://man7.org/linux/man-pages/man2/io_destroy.2.html) for details.
#[inline(always)]
fn io_destroy(ctx: aio_context_t) -> c_long {
    unsafe { syscall(__NR_io_destroy as c_long, ctx) }
}

// Submit a batch of IO operations.
//
// See [io_sumit(7)](http://man7.org/linux/man-pages/man2/io_submit.2.html) for details.
#[inline(always)]
fn io_submit(ctx: aio_context_t, nr: c_long, iocbpp: *mut *mut iocb) -> c_long {
    unsafe { syscall(__NR_io_submit as c_long, ctx, nr, iocbpp) }
}

// Retrieve completion events for previously submitted IO requests.
//
// See [io_getevents(7)](http://man7.org/linux/man-pages/man2/io_getevents.2.html) for details.
#[inline(always)]
fn io_getevents(
    ctx: aio_context_t, min_nr: c_long, max_nr: c_long, events: *mut io_event,
    timeout: *mut timespec,
) -> c_long {
    unsafe { syscall(__NR_io_getevents as c_long, ctx, min_nr, max_nr, events, timeout) }
}