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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! Configuration is used to configure the per thread behavior
//! of the uring runtime.
//!
use std::time::Duration;
/// Configuration options for the io_uring runtime.
///
/// Use the builder pattern to customize runtime behavior.
#[derive(Default)]
pub struct Configuration {
/// Adapter around a set of trace buffers which the data path threads will
/// write trace events to at runtime for debugging and analysis purposes
pub(crate) trace_buffer_manager: Option<Box<dyn crate::TraceConfiguration>>,
/// Should the event processing loop busy poll and not block in the kernel
/// if there are pending IOs. Busy polling avoids the cost of a thread
/// wakeup when an IO completes, but burns a CPU core instead. Offers
/// a latency vs. CPU utilization tradeoff. If using this feature, keep it
/// limited to a small handful of threads, which should be pinned to
/// specific CPU cores. This is a power user feature that should only be
/// used with careful testing and experimentation.
pub(crate) busy_poll: BusyPoll,
}
/// Controls whether the event loop busy-polls for I/O completion.
///
/// Busy polling can reduce latency at the cost of CPU usage.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub enum BusyPoll {
/// Never busy poll; always suspend the thread until I/O completes.
#[default]
Never,
/// Always busy poll; never suspend the thread.
Always,
/// Busy poll for the specified duration, then suspend.
Until(Duration),
}
impl From<Option<Duration>> for BusyPoll {
fn from(value: Option<Duration>) -> Self {
match value {
None => BusyPoll::Never,
Some(d) => BusyPoll::Until(d),
}
}
}
impl Configuration {
/// Creates a new `Configuration` with default settings.
pub fn new() -> Self {
Self::default()
}
/// Sets the trace buffer manager for runtime event tracing.
pub fn set_trace_buffer_manager(
mut self,
trace_buffer_manager: Box<dyn crate::TraceConfiguration>,
) -> Self {
self.trace_buffer_manager = Some(trace_buffer_manager);
self
}
/// Sets the busy polling behavior for the event loop.
pub fn set_busy_poll(mut self, busy_poll: BusyPoll) -> Self {
self.busy_poll = busy_poll;
self
}
}
#[cfg(test)]
mod test {
use crate::configuration::BusyPoll;
#[test]
fn configuration_default_test() {
let config = crate::Configuration::new();
assert_eq!(config.busy_poll, BusyPoll::Never);
assert!(config.trace_buffer_manager.is_none());
}
}