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
use self::collective::*;
use super::*;
#[doc(hidden)]
pub struct Task {
pub start: Instant,
pub machine: ShareableMachine,
}
impl Task {
pub fn new(machine: &ShareableMachine) -> Self {
Self {
start: std::time::Instant::now(),
machine: Arc::clone(machine),
}
}
}
pub struct SchedTask {
pub start: Instant,
pub machine_key: usize,
}
impl SchedTask {
pub fn new(machine_key: usize) -> Self {
Self {
start: Instant::now(),
machine_key,
}
}
}
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct ExecutorStats {
pub id: usize,
pub tasks_executed: u128,
pub instructs_sent: u128,
pub blocked_senders: u128,
pub max_blocked_senders: usize,
pub exhausted_slice: u128,
pub recv_time: std::time::Duration,
pub time_on_queue: std::time::Duration,
}
#[derive(Copy, Clone, Eq, PartialEq, SmartDefault, Debug)]
pub enum ExecutorState {
#[default]
Init,
Drain,
Parked,
Running,
}
#[doc(hidden)]
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum TrySendError {
Full,
Disconnected,
}
#[doc(hidden)]
pub struct SharedCollectiveSenderAdapter {
pub id: Uuid,
pub key: usize,
pub state: MachineState,
pub normalized_adapter: CommonCollectiveSenderAdapter,
}
impl SharedCollectiveSenderAdapter {
pub const fn get_id(&self) -> Uuid { self.id }
pub const fn get_key(&self) -> usize { self.key }
pub fn try_send(&mut self) -> Result<(), TrySendError> { self.normalized_adapter.try_send() }
}
#[doc(hidden)]
pub trait CollectiveSenderAdapter {
fn get_id(&self) -> Uuid;
fn get_key(&self) -> usize;
fn try_send(&mut self) -> Result<(), TrySendError>;
}
pub type CommonCollectiveSenderAdapter = Box<dyn CollectiveSenderAdapter>;
#[derive(Debug)]
pub struct SharedExecutorInfo {
state: ExecutorState,
start_idle: Instant,
}
impl SharedExecutorInfo {
pub fn set_idle(&mut self) -> Instant {
self.start_idle = Instant::now();
self.start_idle
}
pub fn set_state(&mut self, new: ExecutorState) { self.state = new }
pub const fn get_state(&self) -> ExecutorState { self.state }
pub fn compare_set_state(&mut self, old: ExecutorState, new: ExecutorState) {
if self.state == old {
self.state = new
}
}
pub fn get_state_and_elapsed(&self) -> (ExecutorState, Duration) { (self.state, self.start_idle.elapsed()) }
}
impl Default for SharedExecutorInfo {
fn default() -> Self {
Self {
state: ExecutorState::Init,
start_idle: Instant::now(),
}
}
}
#[doc(hidden)]
#[derive(Default)]
pub struct ExecutorData {
pub id: usize,
pub machine: ExecutorDataField,
pub blocked_senders: Vec<SharedCollectiveSenderAdapter>,
pub last_blocked_send_len: usize,
pub notifier: ExecutorDataField,
pub shared_info: Arc<Mutex<SharedExecutorInfo>>,
}
impl ExecutorData {
pub fn block_or_continue() {
tls_executor_data.with(|t| {
let mut tls = t.borrow_mut();
if tls.id == 0 {
return;
}
if let ExecutorDataField::Machine(machine) = &tls.machine {
if machine.state.get() != CollectiveState::Running {
tls.recursive_block();
}
}
});
}
pub fn recursive_block(&mut self) {
self.shared_info
.lock()
.as_mut()
.unwrap()
.compare_set_state(ExecutorState::Running, ExecutorState::Drain);
self.drain();
let mut mutable = self.shared_info.lock().unwrap();
mutable.compare_set_state(ExecutorState::Drain, ExecutorState::Running);
mutable.set_idle();
}
pub fn sender_blocked(&mut self, channel_id: usize, adapter: SharedCollectiveSenderAdapter) {
if adapter.state.get() == CollectiveState::SendBlock {
log::info!(
"Executor {} detected recursive send block, this should not happen",
self.id
);
unreachable!(
"block_or_continue() should be called to prevent entering sender_blocked with a blocked machine"
)
} else {
log::trace!("executor {} parking sender {}", self.id, channel_id);
adapter.state.set(CollectiveState::SendBlock);
self.blocked_senders.push(adapter);
}
}
fn drain(&mut self) {
let backoff = crossbeam::utils::Backoff::new();
let (machine_key, machine_state) = match &self.machine {
ExecutorDataField::Machine(machine) => (machine.key, machine.state.clone()),
_ => panic!("machine field was not set prior to running"),
};
while !self.blocked_senders.is_empty() {
self.shared_info.lock().as_mut().unwrap().set_idle();
let mut still_blocked: Vec<SharedCollectiveSenderAdapter> = Vec::with_capacity(self.blocked_senders.len());
let mut handled_recursive_sender = false;
for mut sender in self.blocked_senders.drain(..) {
match sender.try_send() {
Ok(()) if sender.key == machine_key => {
backoff.reset();
machine_state.set(CollectiveState::Running);
handled_recursive_sender = true;
},
Err(TrySendError::Disconnected) if sender.key == machine_key => {
backoff.reset();
machine_state.set(CollectiveState::Running);
handled_recursive_sender = true;
},
Ok(()) => {
backoff.reset();
match &self.notifier {
ExecutorDataField::Notifier(obj) => obj.notify_can_schedule(sender.key),
_ => log::error!("can't notify scheduler!!!"),
};
},
Err(TrySendError::Disconnected) => {
backoff.reset();
match &self.notifier {
ExecutorDataField::Notifier(obj) => obj.notify_can_schedule(sender.key),
_ => log::error!("can't notify scheduler!!!"),
};
},
Err(TrySendError::Full) => {
still_blocked.push(sender);
},
}
}
self.blocked_senders = still_blocked;
if handled_recursive_sender {
break;
}
if backoff.is_completed() && self.shared_info.lock().unwrap().get_state() != ExecutorState::Parked {
self.shared_info
.lock()
.as_mut()
.unwrap()
.set_state(ExecutorState::Parked);
match &self.notifier {
ExecutorDataField::Notifier(obj) => obj.notify_parked(self.id),
_ => log::error!("Executor {} doesn't have a notifier", self.id),
};
}
backoff.snooze();
}
log::debug!("drained recursive sender, allowing send to continue");
}
}
#[doc(hidden)]
#[derive(SmartDefault)]
pub enum ExecutorDataField {
#[default]
Uninitialized,
Notifier(ExecutorNotifierObj),
Machine(ShareableMachine),
}
pub trait ExecutorNotifier: Send + Sync + 'static {
fn notify_parked(&self, executor_id: usize);
fn notify_can_schedule(&self, machine_key: usize);
}
pub type ExecutorNotifierObj = std::sync::Arc<dyn ExecutorNotifier>;
thread_local! {
#[doc(hidden)]
#[allow(non_upper_case_globals)]
pub static tls_executor_data: RefCell<ExecutorData> = RefCell::new(ExecutorData::default());
}
#[cfg(test)]
mod tests {
#[allow(unused_imports)] use super::*;
}