i2o2 0.5.0

A io_uring based IO executor for sync and async runtimes
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#![doc = include_str!("../README.md")]

use std::any::Any;
use std::{io, mem};

use liburing_rs::{
    IOSQE_IO_DRAIN,
    io_uring_sqe,
    io_uring_sqe_set_data64,
    io_uring_sqe_set_flags,
};

use crate::opcode::sealed::RegisterOp;

mod builder;
mod flags;
mod handle;
pub mod opcode;
mod queue;
mod reply;
mod ring;
#[cfg(test)]
mod tests;
mod wake;

pub use self::builder::{CpuSet, I2o2Builder};
pub use self::handle::{I2o2Handle, RegisterError, SchedulerClosed, SubmitResult};
pub use self::opcode::types;
pub use self::reply::{ReplyReceiver, TryGetResultError};

#[cfg(not(target_os = "linux"))]
compiler_error!(
    "I2o2 only supports linux based operating systems, and requires relatively new kernel versions"
);

/// A guard type that can be any object.
pub type DynamicGuard = Box<dyn Any + Send>;

pub(crate) const MAGIC_ERRNO_NO_CAPACITY: i32 = -999;
pub(crate) const MAGIC_ERRNO_NOT_SIZE128: i32 = -1000;

/// Create a new [I2o2Scheduler] and [I2o2Handle] pair backed by io_uring.
///
/// This will use the default settings for the scheduler, you can optionally
/// use the [builder] to customise the ring behaviour.
///
/// NOTE: The scheduler cannot be sent across threads.
///
/// ## Example
///
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
///
/// let (scheduler, handle) = i2o2::create_for_current_thread::<()>()?;
///
/// // ... do work
///
/// # Ok(())
/// # }
/// ```
pub fn create_for_current_thread<G>() -> io::Result<(I2o2Scheduler<G>, I2o2Handle<G>)> {
    I2o2Builder::default().try_create()
}

/// Create a new [I2o2Scheduler] and [I2o2Handle] pair backed by io_uring and spawn the scheduler
/// in a background worker thread.
///
/// This will use the default settings for the scheduler, you can optionally
/// use the [builder] to customise the ring behaviour.
///
/// NOTE: The scheduler cannot be sent across threads.
///
/// ## Example
///
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
///
/// let (scheduler_handle, handle) = i2o2::create_and_spawn::<()>()?;
///
/// // ... do work
///
/// # Ok(())
/// # }
/// ```
pub fn create_and_spawn<G>()
-> io::Result<(std::thread::JoinHandle<io::Result<()>>, I2o2Handle<G>)>
where
    G: Send + 'static,
{
    I2o2Builder::default().try_spawn()
}

/// Create a new [I2o2Scheduler] and [I2o2Handle] pair backed by io_uring
/// with a custom configuration.
///
/// ## Example
///
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use std::time::Duration;
///
/// let (scheduler, handle) = i2o2::builder()
///     .with_io_polling(true)
///     .try_create::<()>()?;
///
/// // ... do work
///
/// # Ok(())
/// # }
/// ```
pub const fn builder() -> I2o2Builder {
    I2o2Builder::const_default()
}

/// The [I2o2Scheduler] runs an io_uring ring in the current thread and submits
/// IO events from the handle into the ring.
///
/// Communication between the handles and the scheduler can be done both synchronously
/// and asynchronously.
pub struct I2o2Scheduler<G = DynamicGuard> {
    ring: ring::IoRing,
    ring_size128: bool,
    state: TrackedState<G>,
    /// A waker handle for triggering a completion event on `self`
    /// intern causing events to be processed.
    waker: wake::Waker,
    /// A stream of incoming IO events to process.
    incoming_ops: queue::SchedulerReceiver<Packaged<opcode::AnyOp, G>>,
    /// A stream of incoming resource events to process.
    incoming_resources: queue::SchedulerReceiver<ResourceMessage<G>>,
    /// The value of the work counter that was last loaded.
    last_read_work_counter: u64,
    /// A null pointer used to prevent people from sending the scheduler across threads.
    _anti_send_ptr: *mut u8,
}

impl<G> I2o2Scheduler<G> {
    /// Run the scheduler in the current thread until it is shut down.
    ///
    /// This will wait for all remaining tasks to complete.
    pub fn run(mut self) -> io::Result<()> {
        tracing::debug!("scheduler is running");

        #[cfg(test)]
        fail::fail_point!("scheduler_run_fail", |_| {
            Err(io::Error::other("test error triggered by failpoints"))
        });

        tracing::debug!("running scheduler event loop");

        self.run_event_loop()?;

        self.wait_for_remaining()?;
        tracing::debug!("scheduler shutting down");

        Ok(())
    }

    fn run_event_loop(&mut self) -> io::Result<()> {
        loop {
            if self.incoming_ops.is_disconnected() {
                tracing::info!("scheduler disconnected");
                break;
            }

            for _ in 0..50 {
                self.drain_incoming_io()?;
                self.drain_completions()?;
            }

            self.drain_incoming_resources()?;

            self.maybe_wait_for_events()?;
        }

        Ok(())
    }

    /// Attempts to drain all incoming IO ops and submit them to the ring.
    fn drain_incoming_io(&mut self) -> io::Result<()> {
        let pop_n = self.incoming_ops.len();

        #[cfg(feature = "trace-hotpath")]
        tracing::trace!(pop_n = pop_n, "attempting to draining incoming IO ops");

        let mut n_read = 0;
        for _ in 0..pop_n {
            let Some(sqe) = self.ring.get_available_sqe() else {
                break;
            };

            let msg = match self.incoming_ops.pop() {
                Some(msg) => msg,
                None => {
                    write_filler_op(sqe);
                    break;
                },
            };

            n_read += 1;

            if msg.entry.requires_size128() && !self.ring_size128 {
                #[cfg(feature = "trace-hotpath")]
                tracing::trace!(
                    "rejecting op because size128 is required but not active"
                );
                write_filler_op(sqe);
                msg.reply.set_result(MAGIC_ERRNO_NOT_SIZE128);
                continue;
            }

            self.state.register(sqe, msg);
        }

        let result = self.ring.submit();

        if self.incoming_ops.is_empty() {
            self.incoming_ops.wake_n(n_read);
        }

        result?;

        Ok(())
    }

    /// Attempts to drain all incoming resource ops and tie them to the ring.
    fn drain_incoming_resources(&mut self) -> io::Result<()> {
        #[cfg(feature = "trace-hotpath")]
        tracing::trace!(
            pop_n = self.incoming_resources.len(),
            "attempting to draining incoming resource ops"
        );

        let mut processed = 0;
        while let Some(msg) = self.incoming_resources.pop() {
            match msg {
                ResourceMessage::RegisterResource(op) => {
                    self.handle_resource_register_op(op);
                },
                ResourceMessage::UnregisterResource(op) => {
                    self.handle_resource_unregister_op(op);
                },
            }
            processed += 1;
        }

        if processed > 0 {
            self.incoming_resources.wake_all();
        }

        Ok(())
    }

    /// Processes all completion events from the ring.
    fn drain_completions(&mut self) -> io::Result<()> {
        #[cfg(feature = "trace-hotpath")]
        tracing::trace!("draining completion events");

        while self.ring.has_completions_ready() {
            for cqe in self.ring.iter_completions() {
                self.state.handle_cqe(cqe.user_data, cqe.result);
            }
        }

        Ok(())
    }

    /// Wait for events if there is no outstanding work to be done.
    fn maybe_wait_for_events(&mut self) -> io::Result<()> {
        if self.has_outstanding_work() {
            return Ok(());
        }

        self.waker.ask_for_wake();

        #[cfg(feature = "trace-hotpath")]
        tracing::trace!("checking for work");
        if self.has_outstanding_work() {
            return Ok(());
        }

        #[cfg(feature = "trace-hotpath")]
        tracing::trace!("no work, scheduler waiting on events...");

        self.ring.wait_for_completions()?;

        #[cfg(feature = "trace-hotpath")]
        tracing::trace!("woken");

        Ok(())
    }

    /// Waits for remaining inflight operations to complete.
    fn wait_for_remaining(&mut self) -> io::Result<()> {
        tracing::debug!("scheduler is draining remaining events");

        self.incoming_ops.wake_all();
        self.incoming_resources.wake_all();

        while !self.incoming_ops.is_empty() {
            self.drain_incoming_io()?;
            self.ring.wait_for_completions()?;
            self.drain_completions()?;
        }

        // Submits a SQE that will wait until all other pending SQEs complete.
        loop {
            if let Some(sqe) = self.ring.get_available_sqe() {
                write_drain_op(sqe);
                self.ring.submit()?;
                tracing::debug!("drain SQE submitted");
                break;
            };

            self.ring.wait_for_completions()?;
            self.drain_completions()?;
        }

        while !self.state.has_seen_drain_op() {
            self.ring.wait_for_completions()?;
            self.drain_completions()?;
        }

        tracing::debug!("scheduler has drained all events");

        Ok(())
    }

    /// Registers a new resource with the ring providing there is capacity.
    fn handle_resource_register_op(&mut self, op: Packaged<Resource, G>) {
        let Packaged {
            entry,
            reply,
            guard,
        } = op;

        let result = if entry.is_buffer() {
            self.state.register_buffer_guard(guard)
        } else {
            self.state.register_file_guard(guard)
        };

        let (tag, offset) = match result {
            None => {
                reply.set_result(MAGIC_ERRNO_NO_CAPACITY);
                return;
            },
            Some(offset) if entry.is_buffer() => {
                let packed = flags::pack(flags::Flag::GuardedResourceBuffer, 0, offset);
                (packed, offset)
            },
            Some(offset) => {
                let packed = flags::pack(flags::Flag::GuardedResourceFile, 0, offset);
                (packed, offset)
            },
        };

        let result = match entry {
            Resource::Buffer(iovec) => self.ring.register_buffer(offset, iovec, tag),
            Resource::File(fd) => self.ring.register_file(offset, fd, tag),
        };

        if result.is_ok() {
            // This can never wrap because `offset` is only ever 32 bits.
            reply.set_result(offset as i32);
            return;
        }

        // We have to ensure we don't leak mem if there is an error.
        if entry.is_buffer() {
            self.state.drop_buffer_guard(offset);
        } else {
            self.state.drop_file_guard(offset);
        };

        let err = result.unwrap_err();
        reply.set_result(err.raw_os_error().unwrap());
    }

    /// Unregisters a resource tied to the ring.
    ///
    /// Cleanup of guards, etc... Will be handled on the completion event triggered
    /// when the resource is no longer required by the ring.
    fn handle_resource_unregister_op(&mut self, op: Packaged<ResourceIndex, G>) {
        let Packaged { entry, reply, .. } = op;

        let result = match entry {
            ResourceIndex::File(id) => self.ring.unregister_file(id),
        };

        if let Err(err) = result {
            reply.set_result(err.raw_os_error().unwrap());
        } else {
            reply.set_result(0);
        }
    }

    fn has_outstanding_work(&mut self) -> bool {
        let work_counter = self.waker.current_work_counter();
        let previous_count =
            mem::replace(&mut self.last_read_work_counter, work_counter);
        work_counter != previous_count
            || self.ring.has_completions_ready()
            || self.incoming_ops.is_disconnected()
    }
}

fn write_filler_op(sqe: &mut io_uring_sqe) {
    let user_data = flags::pack(flags::Flag::FillerOp, 0, 0);
    let op = opcode::Nop::new();
    op.register_with_sqe(sqe);
    unsafe { io_uring_sqe_set_data64(sqe, user_data) }
}

fn write_drain_op(sqe: &mut io_uring_sqe) {
    let user_data = flags::pack(flags::Flag::Drain, 0, 0);
    let op = opcode::Nop::new();
    op.register_with_sqe(sqe);
    unsafe {
        io_uring_sqe_set_data64(sqe, user_data);
        io_uring_sqe_set_flags(sqe, IOSQE_IO_DRAIN);
    };
}

struct TrackedState<G> {
    seen_drain_op: bool,
    free_registered_files: u32,
    free_registered_buffers: u32,
    /// Guards for allocated registered files.
    resource_file_guards: slab::Slab<Option<G>>,
    /// Guards for allocated registered buffers.
    resource_buffer_guards: slab::Slab<Option<G>>,
    /// A set of guards that should be kept alive as long as the ring requires.
    guards: slab::Slab<G>,
    /// A slab of reply handles for scheduled tasks.
    replies: slab::Slab<reply::ReplyNotify>,
}

impl<G> TrackedState<G> {
    fn new(free_registered_files: u32, free_registered_buffers: u32) -> Self {
        Self {
            seen_drain_op: false,
            free_registered_files,
            free_registered_buffers,
            resource_file_guards: slab::Slab::with_capacity(
                free_registered_files as usize,
            ),
            resource_buffer_guards: slab::Slab::with_capacity(
                free_registered_buffers as usize,
            ),
            guards: slab::Slab::default(),
            replies: slab::Slab::default(),
        }
    }

    fn has_seen_drain_op(&self) -> bool {
        self.seen_drain_op
    }

    fn handle_cqe(&mut self, user_data: u64, result: i32) {
        let (flag, reply_idx, guard_idx) = flags::unpack(user_data);

        #[cfg(feature = "trace-hotpath")]
        tracing::trace!(flag = ?flag, task_id = reply_idx, result = result, "completion");

        match flag {
            flags::Flag::FillerOp | flags::Flag::Wake => {},
            flags::Flag::Drain => {
                self.seen_drain_op = true;
            },
            flags::Flag::Guarded => {
                self.acknowledge_reply(reply_idx, result);
                self.drop_guard_if_exists(guard_idx);
            },
            flags::Flag::Unguarded => {
                self.acknowledge_reply(reply_idx, result);
            },
            flags::Flag::GuardedResourceBuffer => {
                self.drop_buffer_guard(guard_idx);
            },
            flags::Flag::GuardedResourceFile => {
                self.drop_file_guard(guard_idx);
            },
        }
    }

    fn register(&mut self, free_sqe: &mut io_uring_sqe, op: Packaged<opcode::AnyOp, G>) {
        let Packaged {
            entry,
            reply,
            guard,
        } = op;

        let reply_idx = self.replies.insert(reply);

        let flag = if guard.is_none() {
            flags::Flag::Unguarded
        } else {
            flags::Flag::Guarded
        };

        let guard_idx = guard.map(|g| self.register_guard(g)).unwrap_or(0);

        #[cfg(feature = "trace-hotpath")]
        tracing::trace!(task_id = reply_idx, flag = ?flag, "registered entry");

        let user_data = flags::pack(flag, reply_idx as u32, guard_idx);

        // Write the entry to the SQE in the ring.
        entry.register_with_sqe(free_sqe);
        unsafe { io_uring_sqe_set_data64(free_sqe, user_data) };
    }

    fn register_guard(&mut self, guard: G) -> u32 {
        self.guards.insert(guard) as u32
    }

    fn acknowledge_reply(&mut self, reply_idx: u32, result: i32) {
        let reply = self.replies.remove(reply_idx as usize);
        reply.set_result(result);
    }

    fn drop_guard_if_exists(&mut self, guard_idx: u32) {
        drop(self.guards.try_remove(guard_idx as usize));
    }

    fn register_buffer_guard(&mut self, guard: Option<G>) -> Option<u32> {
        if self.free_registered_buffers > 0 {
            self.free_registered_buffers -= 1;
            Some(self.resource_buffer_guards.insert(guard) as u32)
        } else {
            None
        }
    }

    fn drop_buffer_guard(&mut self, guard_idx: u32) {
        let value = self.resource_buffer_guards.try_remove(guard_idx as usize);
        if value.is_some() {
            self.free_registered_buffers += 1;
        }
    }

    fn register_file_guard(&mut self, guard: Option<G>) -> Option<u32> {
        if self.free_registered_files > 0 {
            self.free_registered_files -= 1;
            Some(self.resource_file_guards.insert(guard) as u32)
        } else {
            None
        }
    }

    fn drop_file_guard(&mut self, guard_idx: u32) {
        let value = self.resource_file_guards.try_remove(guard_idx as usize);
        if value.is_some() {
            self.free_registered_files += 1;
        }
    }
}

enum ResourceMessage<G> {
    /// Register a new resource to the ring.
    RegisterResource(Packaged<Resource, G>),
    /// Unregister an existing resource on the ring.
    UnregisterResource(Packaged<ResourceIndex, G>),
}

#[repr(align(64))]
struct Packaged<E, G> {
    entry: E,
    reply: reply::ReplyNotify,
    guard: Option<G>,
}

enum Resource {
    Buffer(liburing_rs::iovec),
    File(std::os::fd::RawFd),
}

impl Resource {
    fn is_buffer(&self) -> bool {
        matches!(self, Resource::Buffer { .. })
    }
}

// SAFETY: The handle ensures the buffers are safe to send across a thread boundary.
unsafe impl Send for Resource {}

enum ResourceIndex {
    File(u32),
}