-
Notifications
You must be signed in to change notification settings - Fork 6
/
mod.rs
345 lines (310 loc) · 11.5 KB
/
mod.rs
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
//! Module with shared runtime internals.
use std::cmp::min;
use std::future::Future;
use std::os::unix::io::AsRawFd;
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, TryLockError};
use std::time::{Duration, Instant};
use std::{io, task};
use heph_inbox as inbox;
use log::{debug, error, trace};
use mio::unix::SourceFd;
use mio::{event, Events, Interest, Poll, Registry, Token};
use crate::actor::{self, NewActor};
use crate::actor_ref::ActorRef;
use crate::rt::thread_waker::ThreadWaker;
use crate::rt::{ProcessId, ThreadSafe};
use crate::spawn::{ActorOptions, AddActorError, FutureOptions};
use crate::supervisor::Supervisor;
use crate::trace;
mod scheduler;
mod timers;
pub(crate) mod waker;
use scheduler::{ProcessData, Scheduler};
use timers::Timers;
use waker::WakerId;
/// Setup of [`RuntimeInternals`].
///
/// # Notes
///
/// This type only exists because [`Arc::new_cyclic`] doesn't work when
/// returning a result. And as [`RuntimeInternals`] needs to create a [`Poll`]
/// instance, which can fail, creating a new `RuntimeInternals` inside
/// `Arc::new_cyclic` doesn't work. So it needs to be a two step process, where
/// the second step (`RuntimeSetup::complete`) doesn't return an error and can
/// be called inside `Arc::new_cyclic`.
pub(crate) struct RuntimeSetup {
poll: Poll,
registry: Registry,
}
impl RuntimeSetup {
/// Complete the runtime setup.
pub(crate) fn complete(
self,
shared_id: WakerId,
worker_wakers: Box<[&'static ThreadWaker]>,
trace_log: Option<Arc<trace::SharedLog>>,
) -> RuntimeInternals {
// Needed by `RuntimeInternals::wake_workers`.
debug_assert!(worker_wakers.len() >= 1);
RuntimeInternals {
shared_id,
worker_wakers,
wake_worker_idx: AtomicUsize::new(0),
poll: Mutex::new(self.poll),
registry: self.registry,
scheduler: Scheduler::new(),
timers: Timers::new(),
trace_log,
}
}
}
/// Shared internals of the runtime.
#[derive(Debug)]
pub(crate) struct RuntimeInternals {
/// Waker id used to create [`task::Waker`]s for thread-safe actors.
shared_id: WakerId,
/// Thread wakers for all the workers.
worker_wakers: Box<[&'static ThreadWaker]>,
/// Index into `worker_wakers` to wake next, see
/// [`RuntimeInternals::wake_workers`].
wake_worker_idx: AtomicUsize,
/// Poll instance for all shared event sources. This is polled by the worker
/// thread.
poll: Mutex<Poll>,
/// Registry for the `Coordinator`'s `Poll` instance.
registry: Registry,
/// Scheduler for thread-safe actors.
scheduler: Scheduler,
/// Timers for thread-safe actors.
timers: Timers,
/// Shared trace log.
///
/// # Notes
///
/// Prefer not to use this but use [`trace::Log`] in local internals
/// instead.
trace_log: Option<Arc<trace::SharedLog>>,
}
/// Metrics for [`RuntimeInternals`].
#[derive(Debug)]
#[allow(dead_code)] // https://github.com/rust-lang/rust/issues/88900.
pub(crate) struct Metrics {
scheduler: scheduler::Metrics,
timers: timers::Metrics,
}
impl RuntimeInternals {
/// Setup new runtime internals.
pub(crate) fn setup() -> io::Result<RuntimeSetup> {
let poll = Poll::new()?;
let registry = poll.registry().try_clone()?;
Ok(RuntimeSetup { poll, registry })
}
/// Gather metrics about the shared runtime state.
pub(crate) fn metrics(&self) -> Metrics {
Metrics {
scheduler: self.scheduler.metrics(),
timers: self.timers.metrics(),
}
}
/// Returns a new [`task::Waker`] for the thread-safe actor with `pid`.
pub(crate) fn new_task_waker(&self, pid: ProcessId) -> task::Waker {
waker::new(self.shared_id, pid)
}
/// Register the shared [`Poll`] instance with `registry`.
pub(crate) fn register_worker_poll(&self, registry: &Registry, token: Token) -> io::Result<()> {
use mio::event::Source;
let poll = self.poll.lock().unwrap();
SourceFd(&poll.as_raw_fd()).register(registry, token, Interest::READABLE)
}
/// Returns `Ok(true)` if it polled the shared [`Poll`] instance, writing OS
/// events to `events`. Returns `Ok(false)` if another worker is currently
/// polling, which means this worker doesn't have to anymore. Otherwise it
/// returns an error.
pub(crate) fn try_poll(&self, events: &mut Events) -> io::Result<bool> {
match self.poll.try_lock() {
Ok(mut poll) => poll.poll(events, Some(Duration::ZERO)).map(|()| true),
Err(TryLockError::WouldBlock) => Ok(false),
Err(TryLockError::Poisoned(err)) => panic!("failed to lock shared poll: {}", err),
}
}
/// Register an `event::Source`, see [`mio::Registry::register`].
pub(crate) fn register<S>(
&self,
source: &mut S,
token: Token,
interest: Interest,
) -> io::Result<()>
where
S: event::Source + ?Sized,
{
self.registry.register(source, token, interest)
}
/// Reregister an `event::Source`, see [`mio::Registry::reregister`].
pub(crate) fn reregister<S>(
&self,
source: &mut S,
token: Token,
interest: Interest,
) -> io::Result<()>
where
S: event::Source + ?Sized,
{
self.registry.reregister(source, token, interest)
}
/// See [`Timers::add`].
pub(super) fn add_deadline(&self, pid: ProcessId, deadline: Instant) {
self.timers.add(pid, deadline);
}
/// See [`Timers::remove`].
pub(super) fn remove_deadline(&self, pid: ProcessId, deadline: Instant) {
self.timers.remove(pid, deadline);
}
/// See [`Timers::change`].
pub(super) fn change_deadline(&self, from: ProcessId, to: ProcessId, deadline: Instant) {
self.timers.change(from, deadline, to);
}
/// See [`Timers::remove_next`].
pub(crate) fn remove_next_deadline(&self, now: Instant) -> Option<ProcessId> {
self.timers.remove_next(now)
}
/// Determine the timeout to use in polling based on the current time
/// (`now`), the `current` timeout and the next deadline in the shared
/// timers.
///
/// If there are no timers this will return `current`. If `current` is
/// smaller than the next deadline in the timers this will also
/// return `current`. Otherwise this will return a timeout based on the
/// next deadline.
pub(crate) fn next_timeout(&self, now: Instant, current: Option<Duration>) -> Option<Duration> {
match self.timers.next() {
Some(deadline) => match deadline.checked_duration_since(now) {
// Timer has already expired, so no blocking.
None => Some(Duration::ZERO),
Some(timeout) => match current {
Some(current) if current < timeout => Some(current),
Some(..) | None => Some(timeout),
},
},
None => current,
}
}
#[allow(clippy::needless_pass_by_value)] // For `ActorOptions`.
pub(crate) fn spawn_setup<S, NA, ArgFn, E>(
self: &Arc<Self>,
supervisor: S,
mut new_actor: NA,
arg_fn: ArgFn,
options: ActorOptions,
) -> Result<ActorRef<NA::Message>, AddActorError<NA::Error, E>>
where
S: Supervisor<NA> + Send + Sync + 'static,
NA: NewActor<RuntimeAccess = ThreadSafe> + Sync + Send + 'static,
ArgFn: FnOnce(&mut actor::Context<NA::Message, ThreadSafe>) -> Result<NA::Argument, E>,
NA::Actor: Send + Sync + 'static,
NA::Message: Send,
{
// Setup adding a new process to the scheduler.
let actor_entry = self.scheduler.add_actor();
let pid = actor_entry.pid();
let name = new_actor.name();
debug!("spawning thread-safe actor: pid={}, name={}", pid, name);
// Create our actor context and our actor with it.
let (manager, sender, receiver) = inbox::Manager::new_small_channel();
let actor_ref = ActorRef::local(sender);
let mut ctx = actor::Context::new(receiver, ThreadSafe::new(pid, self.clone()));
let arg = arg_fn(&mut ctx).map_err(AddActorError::ArgFn)?;
let actor = new_actor.new(ctx, arg).map_err(AddActorError::NewActor)?;
// Add the actor to the scheduler.
actor_entry.add(
options.priority(),
supervisor,
new_actor,
actor,
manager,
options.is_ready(),
);
Ok(actor_ref)
}
/// Spawn a thread-safe `future`.
#[allow(clippy::needless_pass_by_value)]
pub(crate) fn spawn_future<Fut>(&self, future: Fut, options: FutureOptions)
where
Fut: Future<Output = ()> + Send + Sync + 'static,
{
self.scheduler.add_future(future, options.priority())
}
/// See [`Scheduler::mark_ready`].
pub(crate) fn mark_ready(&self, pid: ProcessId) {
self.scheduler.mark_ready(pid)
}
/// Wake `n` worker threads.
pub(crate) fn wake_workers(&self, n: usize) {
trace!("waking {} worker thread(s)", n);
// To prevent the Thundering herd problem [1] we don't wake all workers,
// only enough worker threads to handle all events. To spread the
// workload (somewhat more) evenly we wake the workers in a Round-Robin
// [2] fashion.
//
// [1]: https://en.wikipedia.org/wiki/Thundering_herd_problem
// [2]: https://en.wikipedia.org/wiki/Round-robin_scheduling
let n = min(n, self.worker_wakers.len());
// Safety: needs to sync with itself.
let wake_worker_idx =
self.wake_worker_idx.fetch_add(n, Ordering::AcqRel) % self.worker_wakers.len();
let (wake_second, wake_first) = self.worker_wakers.split_at(wake_worker_idx);
let workers_to_wake = wake_first.iter().chain(wake_second.iter());
let mut wakes_left = n;
for worker in workers_to_wake {
match worker.wake() {
Ok(true) => {
wakes_left -= 1;
if wakes_left == 0 {
break;
}
}
Ok(false) => {}
Err(err) => error!("error waking worker: {}", err),
}
}
}
/// See [`Scheduler::has_process`].
pub(crate) fn has_process(&self) -> bool {
self.scheduler.has_process()
}
/// See [`Scheduler::has_ready_process`].
pub(crate) fn has_ready_process(&self) -> bool {
self.scheduler.has_ready_process()
}
/// See [`Scheduler::remove`].
pub(crate) fn remove_process(&self) -> Option<Pin<Box<ProcessData>>> {
self.scheduler.remove()
}
/// See [`Scheduler::add_process`].
pub(crate) fn add_process(&self, process: Pin<Box<ProcessData>>) {
self.scheduler.add_process(process);
}
/// See [`Scheduler::complete`].
pub(crate) fn complete(&self, process: Pin<Box<ProcessData>>) {
self.scheduler.complete(process);
}
pub(crate) fn start_trace(&self) -> Option<trace::EventTiming> {
trace::start(&self.trace_log.as_deref())
}
pub(crate) fn finish_trace(
&self,
timing: Option<trace::EventTiming>,
pid: ProcessId,
description: &str,
attributes: &[(&str, &dyn trace::AttributeValue)],
) {
trace::finish(
self.trace_log.as_deref(),
timing,
pid.0 as u64,
description,
attributes,
)
}
}