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
|
use std::{io, marker::PhantomData};
#[cfg(all(target_os = "linux", feature = "iouring"))]
use crate::driver::IoUringDriver;
#[cfg(all(unix, feature = "legacy"))]
use crate::driver::LegacyDriver;
use crate::{
driver::Driver,
time::{driver::TimeDriver, Clock},
utils::thread_id::gen_id,
Runtime,
};
// ===== basic builder structure definition =====
/// Runtime builder
pub struct RuntimeBuilder<D> {
// iouring entries
entries: Option<u32>,
#[cfg(all(target_os = "linux", feature = "iouring"))]
urb: io_uring::Builder,
// blocking handle
#[cfg(feature = "sync")]
blocking_handle: crate::blocking::BlockingHandle,
// driver mark
_mark: PhantomData<D>,
}
scoped_thread_local!(pub(crate) static BUILD_THREAD_ID: usize);
impl<T> Default for RuntimeBuilder<T> {
/// Create a default runtime builder
#[must_use]
fn default() -> Self {
RuntimeBuilder::<T>::new()
}
}
impl<T> RuntimeBuilder<T> {
/// Create a default runtime builder
#[must_use]
pub fn new() -> Self {
Self {
entries: None,
#[cfg(all(target_os = "linux", feature = "iouring"))]
urb: io_uring::IoUring::builder(),
#[cfg(feature = "sync")]
blocking_handle: crate::blocking::BlockingStrategy::Panic.into(),
_mark: PhantomData,
}
}
}
// ===== buildable trait and forward methods =====
/// Buildable trait.
pub trait Buildable: Sized {
/// Build the runtime.
fn build(this: RuntimeBuilder<Self>) -> io::Result<Runtime<Self>>;
}
#[allow(unused)]
macro_rules! direct_build {
($ty: ty) => {
impl RuntimeBuilder<$ty> {
/// Build the runtime.
pub fn build(self) -> io::Result<Runtime<$ty>> {
Buildable::build(self)
}
}
};
}
#[cfg(all(target_os = "linux", feature = "iouring"))]
direct_build!(IoUringDriver);
#[cfg(all(target_os = "linux", feature = "iouring"))]
direct_build!(TimeDriver<IoUringDriver>);
#[cfg(all(unix, feature = "legacy"))]
direct_build!(LegacyDriver);
#[cfg(all(unix, feature = "legacy"))]
direct_build!(TimeDriver<LegacyDriver>);
// ===== builder impl =====
#[cfg(all(unix, feature = "legacy"))]
impl Buildable for LegacyDriver {
fn build(this: RuntimeBuilder<Self>) -> io::Result<Runtime<LegacyDriver>> {
let thread_id = gen_id();
#[cfg(feature = "sync")]
let blocking_handle = this.blocking_handle;
BUILD_THREAD_ID.set(&thread_id, || {
let driver = match this.entries {
Some(entries) => LegacyDriver::new_with_entries(entries)?,
None => LegacyDriver::new()?,
};
#[cfg(feature = "sync")]
let context = crate::runtime::Context::new(blocking_handle);
#[cfg(not(feature = "sync"))]
let context = crate::runtime::Context::new();
Ok(Runtime::new(context, driver))
})
}
}
#[cfg(all(target_os = "linux", feature = "iouring"))]
impl Buildable for IoUringDriver {
fn build(this: RuntimeBuilder<Self>) -> io::Result<Runtime<IoUringDriver>> {
let thread_id = gen_id(); // 线程 id | 与内核无关,仅仅是个唯一标识
#[cfg(feature = "sync")]
let blocking_handle = this.blocking_handle;
BUILD_THREAD_ID.set(&thread_id, || {
let driver = match this.entries {
Some(entries) => IoUringDriver::new_with_entries(&this.urb, entries)?,
None => IoUringDriver::new(&this.urb)?,
};
#[cfg(feature = "sync")]
let context = crate::runtime::Context::new(blocking_handle);
#[cfg(not(feature = "sync"))]
let context = crate::runtime::Context::new();
Ok(Runtime::new(context, driver))
})
}
}
impl<D> RuntimeBuilder<D> {
const MIN_ENTRIES: u32 = 256;
/// Set io_uring entries, min size is 256 and the default size is 1024.
#[must_use]
pub fn with_entries(mut self, entries: u32) -> Self {
// If entries is less than 256, it will be 256.
if entries < Self::MIN_ENTRIES {
self.entries = Some(Self::MIN_ENTRIES);
return self;
}
self.entries = Some(entries);
self
}
/// Replaces the default [`io_uring::Builder`], which controls the settings for the
/// inner `io_uring` API.
///
/// Refer to the [`io_uring::Builder`] documentation for all the supported methods.
#[cfg(all(target_os = "linux", feature = "iouring"))]
#[must_use]
pub fn uring_builder(mut self, urb: io_uring::Builder) -> Self {
self.urb = urb;
self
}
}
// ===== FusionDriver =====
/// Fake driver only for conditionally building.
#[cfg(any(all(target_os = "linux", feature = "iouring"), feature = "legacy"))]
pub struct FusionDriver;
#[cfg(any(all(target_os = "linux", feature = "iouring"), feature = "legacy"))]
impl RuntimeBuilder<FusionDriver> {
/// Build the runtime.
#[cfg(all(target_os = "linux", feature = "iouring", feature = "legacy"))]
pub fn build(self) -> io::Result<crate::FusionRuntime<IoUringDriver, LegacyDriver>> {
if crate::utils::detect_uring() {
let builder = RuntimeBuilder::<IoUringDriver> {
entries: self.entries,
urb: self.urb,
#[cfg(feature = "sync")]
blocking_handle: self.blocking_handle,
_mark: PhantomData,
};
info!("io_uring driver built");
Ok(builder.build()?.into())
} else {
let builder = RuntimeBuilder::<LegacyDriver> {
entries: self.entries,
urb: self.urb,
#[cfg(feature = "sync")]
blocking_handle: self.blocking_handle,
_mark: PhantomData,
};
info!("legacy driver built");
Ok(builder.build()?.into())
}
}
/// Build the runtime.
#[cfg(all(unix, not(all(target_os = "linux", feature = "iouring"))))]
pub fn build(self) -> io::Result<crate::FusionRuntime<LegacyDriver>> {
let builder = RuntimeBuilder::<LegacyDriver> {
entries: self.entries,
#[cfg(feature = "sync")]
blocking_handle: self.blocking_handle,
_mark: PhantomData,
};
Ok(builder.build()?.into())
}
/// Build the runtime.
#[cfg(all(target_os = "linux", feature = "iouring", not(feature = "legacy")))]
pub fn build(self) -> io::Result<crate::FusionRuntime<IoUringDriver>> {
let builder = RuntimeBuilder::<IoUringDriver> {
entries: self.entries,
urb: self.urb,
#[cfg(feature = "sync")]
blocking_handle: self.blocking_handle,
_mark: PhantomData,
};
Ok(builder.build()?.into())
}
}
#[cfg(any(all(target_os = "linux", feature = "iouring"), feature = "legacy"))]
impl RuntimeBuilder<TimeDriver<FusionDriver>> {
/// Build the runtime.
#[cfg(all(target_os = "linux", feature = "iouring", feature = "legacy"))]
pub fn build(
self,
) -> io::Result<crate::FusionRuntime<TimeDriver<IoUringDriver>, TimeDriver<LegacyDriver>>> {
if crate::utils::detect_uring() {
let builder = RuntimeBuilder::<TimeDriver<IoUringDriver>> {
entries: self.entries,
urb: self.urb,
#[cfg(feature = "sync")]
blocking_handle: self.blocking_handle,
_mark: PhantomData,
};
info!("io_uring driver with timer built");
Ok(builder.build()?.into())
} else {
let builder = RuntimeBuilder::<TimeDriver<LegacyDriver>> {
entries: self.entries,
urb: self.urb,
#[cfg(feature = "sync")]
blocking_handle: self.blocking_handle,
_mark: PhantomData,
};
info!("legacy driver with timer built");
Ok(builder.build()?.into())
}
}
/// Build the runtime.
#[cfg(all(unix, not(all(target_os = "linux", feature = "iouring"))))]
pub fn build(self) -> io::Result<crate::FusionRuntime<TimeDriver<LegacyDriver>>> {
let builder = RuntimeBuilder::<TimeDriver<LegacyDriver>> {
entries: self.entries,
#[cfg(feature = "sync")]
blocking_handle: self.blocking_handle,
_mark: PhantomData,
};
Ok(builder.build()?.into())
}
/// Build the runtime.
#[cfg(all(target_os = "linux", feature = "iouring", not(feature = "legacy")))]
pub fn build(&self) -> io::Result<crate::FusionRuntime<TimeDriver<IoUringDriver>>> {
let builder = RuntimeBuilder::<TimeDriver<IoUringDriver>> {
entries: self.entries,
urb: self.urb,
#[cfg(feature = "sync")]
blocking_handle: self.blocking_handle,
_mark: PhantomData,
};
Ok(builder.build()?.into())
}
}
// ===== enable_timer related =====
mod time_wrap {
pub trait TimeWrapable {}
}
#[cfg(all(target_os = "linux", feature = "iouring"))]
impl time_wrap::TimeWrapable for IoUringDriver {}
#[cfg(all(unix, feature = "legacy"))]
impl time_wrap::TimeWrapable for LegacyDriver {}
#[cfg(any(all(target_os = "linux", feature = "iouring"), feature = "legacy"))]
impl time_wrap::TimeWrapable for FusionDriver {}
impl<D: Driver> Buildable for TimeDriver<D>
where
D: Buildable,
{
/// Build the runtime
fn build(this: RuntimeBuilder<Self>) -> io::Result<Runtime<TimeDriver<D>>> {
let Runtime {
driver,
mut context,
} = Buildable::build(RuntimeBuilder::<D> {
entries: this.entries,
#[cfg(all(target_os = "linux", feature = "iouring"))]
urb: this.urb,
#[cfg(feature = "sync")]
blocking_handle: this.blocking_handle,
_mark: PhantomData,
})?;
let timer_driver = TimeDriver::new(driver, Clock::new());
context.time_handle = Some(timer_driver.handle.clone());
Ok(Runtime {
driver: timer_driver,
context,
})
}
}
impl<D: time_wrap::TimeWrapable> RuntimeBuilder<D> {
/// Enable all(currently only timer)
#[must_use]
pub fn enable_all(self) -> RuntimeBuilder<TimeDriver<D>> {
self.enable_timer()
}
/// Enable timer
#[must_use]
pub fn enable_timer(self) -> RuntimeBuilder<TimeDriver<D>> {
let Self {
entries,
#[cfg(all(target_os = "linux", feature = "iouring"))]
urb,
#[cfg(feature = "sync")]
blocking_handle,
..
} = self;
RuntimeBuilder {
entries,
#[cfg(all(target_os = "linux", feature = "iouring"))]
urb,
#[cfg(feature = "sync")]
blocking_handle,
_mark: PhantomData,
}
}
}
impl<D> RuntimeBuilder<D> {
/// Attach thread pool, this will overwrite blocking strategy.
/// All `spawn_blocking` will be executed on given thread pool.
#[cfg(feature = "sync")]
#[must_use]
pub fn attach_thread_pool(
mut self,
tp: Box<dyn crate::blocking::ThreadPool + Send + 'static>,
) -> Self {
self.blocking_handle = crate::blocking::BlockingHandle::Attached(tp);
self
}
/// Set blocking strategy, this will overwrite thread pool setting.
/// If `BlockingStrategy::Panic` is used, it will panic if `spawn_blocking` on this thread.
/// If `BlockingStrategy::ExecuteLocal` is used, it will execute with current thread, and may
/// cause tasks high latency.
/// Attaching a thread pool is recommended if `spawn_blocking` will be used.
#[cfg(feature = "sync")]
#[must_use]
pub fn with_blocking_strategy(mut self, strategy: crate::blocking::BlockingStrategy) -> Self {
self.blocking_handle = crate::blocking::BlockingHandle::Empty(strategy);
self
}
}
|