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
use std::ptr;
use std::sync::atomic::{AtomicPtr, AtomicU64, AtomicUsize, Ordering};
use std::sync::{Mutex, Once};
use std::thread;
use std::time::Duration;

use crossbeam_utils::sync::{Parker, Unparker};

#[cfg(feature = "tracing")]
use log::trace;

use lelet_utils::{abort_on_panic, SimpleLock};

use super::machine;
use super::processor::Processor;
use super::Task;

/// how long a processor considered to be blocking
const BLOCKING_THRESHOLD: Duration = Duration::from_millis(10);

pub struct System {
    /// all processors
    processors: Vec<Processor>,

    /// for blocking detection
    tick: AtomicU64,

    sysmon_parker: SimpleLock<Parker>,
    sysmon_unparker: Unparker,

    processors_parker: Mutex<Parker>,
    processors_unparker: Unparker,
}

// just to make sure
impl Drop for System {
    fn drop(&mut self) {
        eprintln!("System must not be dropped once created");
        std::process::abort();
    }
}

impl System {
    /// create new system and leak it
    fn new(num_cpus: usize) -> &'static System {
        #[cfg(feature = "tracing")]
        trace!("Creating system");

        let processors: Vec<Processor> = (0..num_cpus).map(|_| Processor::new()).collect();

        let steal_orders: Vec<Vec<usize>> = (3..)
            .step_by(2)
            .filter(|&i| coprime(i, num_cpus))
            .take(num_cpus)
            .enumerate()
            .map(|(i, c)| {
                (0..num_cpus)
                    .map(move |j| ((c * j) + i) % num_cpus)
                    .filter(move |&s| s != i)
                    .collect()
            })
            .collect();

        // do some validity check
        assert!(!processors.is_empty());
        assert_eq!(processors.len(), steal_orders.len());
        for (i, s) in steal_orders.iter().enumerate() {
            assert!((0..num_cpus).eq(std::iter::once(i)
                .chain(s.clone().into_iter())
                .collect::<std::collections::BinaryHeap<usize>>()
                .into_sorted_vec()));
        }

        let sysmon_parker = Parker::new();
        let sysmon_unparker = sysmon_parker.unparker().clone();

        let processors_parker = Parker::new();
        let processors_unparker = processors_parker.unparker().clone();

        // we need fix memory location to pass to Processor::set_system
        // alloc in heap, and leak it
        let system_raw = Box::into_raw(Box::new(System {
            processors,

            tick: AtomicU64::new(0),

            sysmon_parker: SimpleLock::new(sysmon_parker),
            sysmon_unparker,

            processors_parker: Mutex::new(processors_parker),
            processors_unparker,
        }));

        let system: &'static System = unsafe { &*system_raw };

        let mut others: Vec<Option<(usize, Vec<&'static Processor>)>> = steal_orders
            .iter()
            .enumerate()
            .map(|(i, o)| Some((i, o.iter().map(|&i| &system.processors[i]).collect())))
            .collect();

        // although we temporary broke the reference invariant (mutable and immutable
        // are live in same time) here, this is safe because:
        // we only use mutable reference to call Processor::set_system(system, others),
        // we doesn't provide public direct/indirect access to System::processors[i]
        // from first argument, and in second argument, others[i] doesn't contain System::processors[i]
        // so code outside this block will not observe this broken invariant
        for (i, p) in unsafe { &mut *system_raw }
            .processors
            .iter_mut()
            .enumerate()
        {
            let o = others[i].take().unwrap();
            assert_eq!(i, o.0);

            p.set_system(system, o.1);
        }

        system
    }

    #[inline(always)]
    fn sysmon_run(&'static self) {
        #[cfg(feature = "tracing")]
        trace!("Sysmon is running");

        // this will also ensure that only one sysmon thread is running
        let parker = self.sysmon_parker.try_lock().unwrap();

        // spawn machine for every processor
        self.processors.iter().for_each(machine::spawn);

        loop {
            let check_tick = self.tick.fetch_add(1, Ordering::Relaxed) + 1;

            thread::sleep(BLOCKING_THRESHOLD);

            if !self.is_empty() {
                for p in &self.processors {
                    if p.get_last_seen() < check_tick {
                        #[cfg(feature = "tracing")]
                        trace!("{:?} is blocked, spawn new machine for it", p);

                        machine::spawn(p);
                    }
                }
            } else {
                #[cfg(feature = "tracing")]
                trace!("Sysmon entering sleep");

                parker.park();

                #[cfg(feature = "tracing")]
                trace!("Sysmon exiting sleep");
            }
        }
    }

    #[inline(always)]
    fn is_empty(&self) -> bool {
        self.global_is_empty() && self.processors.iter().all(|p| p.local_is_empty())
    }

    #[inline(always)]
    pub fn global_is_empty(&self) -> bool {
        self.processors.iter().all(|p| p.global_is_empty())
    }

    #[inline(always)]
    pub fn processors_wait_notif(&self) {
        self.processors_parker.lock().unwrap().park();
    }

    #[inline(always)]
    pub fn processors_send_notif(&self) {
        self.processors_unparker.unpark();
    }

    #[inline(always)]
    pub fn push(&self, task: Task) {
        match machine::direct_push(task) {
            Ok(()) => {}
            Err(task) => {
                let mut p = task.tag().processor_hint();
                if p.is_null() {
                    p = unsafe { self.processors.get_unchecked(0) };
                }

                unsafe { &*p }.push_global(task);
            }
        }

        self.sysmon_unparker.unpark();
        self.processors_send_notif();
    }

    #[inline(always)]
    pub fn now(&self) -> u64 {
        self.tick.load(Ordering::Relaxed)
    }
}

#[inline(always)]
pub fn get() -> &'static System {
    static SYSTEM: (AtomicPtr<System>, Once) = (AtomicPtr::new(ptr::null_mut()), Once::new());
    SYSTEM.1.call_once(|| {
        thread::spawn(move || abort_on_panic(move || get().sysmon_run()));
        let system = System::new(lock_num_cpus()) as *const _ as *mut _;
        SYSTEM.0.store(system, Ordering::Relaxed);
    });
    unsafe { &*SYSTEM.0.load(Ordering::Relaxed) }
}

/// Detach current thread from executor pool
///
/// this is useful if you know that you are going to do blocking that longer
/// than blocking threshold.
#[inline(always)]
pub fn detach_current_thread() {
    machine::respawn();
}

static NUM_CPUS: AtomicUsize = AtomicUsize::new(0);

/// Set the number of executor thread
///
/// Analogous to `GOMAXPROCS` in golang,
/// can only be set once and before executor is running,
/// if not set before executor running, it will be the number of available cpu in the host
#[inline(always)]
pub fn set_num_cpus(size: usize) -> Result<(), String> {
    let old_value = NUM_CPUS.compare_and_swap(0, size, Ordering::Relaxed);
    if old_value == 0 {
        Ok(())
    } else {
        Err(format!("num_cpus already set to {}", old_value))
    }
}

/// Get the number of executor thread
///
/// `None` if the executor thread is not run yet
#[inline(always)]
pub fn get_num_cpus() -> Option<usize> {
    let n = NUM_CPUS.load(Ordering::Relaxed);
    if n == 0 {
        None
    } else {
        Some(n)
    }
}

#[inline(always)]
fn lock_num_cpus() -> usize {
    let num_cpus = &NUM_CPUS;
    if num_cpus.load(Ordering::Relaxed) == 0 {
        num_cpus.compare_and_swap(0, std::cmp::max(1, num_cpus::get()), Ordering::Relaxed);
    }
    num_cpus.load(Ordering::Relaxed)
}

#[inline(always)]
fn coprime(a: usize, b: usize) -> bool {
    gcd(a, b) == 1
}

#[inline(always)]
fn gcd(a: usize, b: usize) -> usize {
    let mut p = (a, b);
    while p.1 != 0 {
        p = (p.1, p.0 % p.1);
    }
    p.0
}