|
| 1 | +//! Monitor for the runtime. |
| 2 | +
|
1 | 3 | use std::sync::{Arc, Mutex};
|
2 | 4 | use std::thread;
|
3 |
| -use std::time::Duration; |
| 5 | +use std::time::{Duration, Instant}; |
| 6 | + |
| 7 | +use once_cell::sync::Lazy; |
4 | 8 |
|
5 | 9 | use crate::rt;
|
6 | 10 | use crate::task;
|
7 |
| -use crate::utils::abort_on_panic; |
8 |
| - |
9 |
| -pub fn spawn_thread() { |
10 |
| - thread::Builder::new() |
11 |
| - .name("async-std/monitor".to_string()) |
12 |
| - .spawn(|| { |
13 |
| - const PROBING_DURATION_MS: u64 = 500; |
14 |
| - const SCALING_DOWN_SEC: u64 = 1 * 60; // 1 minute |
15 |
| - |
16 |
| - abort_on_panic(|| { |
17 |
| - let running = &Arc::new(Mutex::new(false)); |
18 |
| - |
19 |
| - { |
20 |
| - let running = Arc::clone(running); |
21 |
| - task::spawn(async move { |
22 |
| - loop { |
23 |
| - *running.lock().unwrap() = true; |
24 |
| - task::sleep(Duration::from_millis(PROBING_DURATION_MS)).await; |
25 |
| - } |
26 |
| - }); |
27 |
| - } |
28 |
| - |
29 |
| - { |
30 |
| - task::spawn(async { |
31 |
| - loop { |
32 |
| - task::sleep(Duration::from_secs(SCALING_DOWN_SEC)).await; |
33 |
| - rt::scale_down(); |
34 |
| - } |
35 |
| - }); |
36 |
| - } |
37 |
| - |
38 |
| - loop { |
39 |
| - *running.lock().unwrap() = false; |
40 |
| - thread::sleep(Duration::from_millis(PROBING_DURATION_MS * 2)); |
41 |
| - if !*running.lock().unwrap() { |
42 |
| - eprintln!( |
43 |
| - "WARNING: You are blocking the runtime, please use spawn_blocking" |
44 |
| - ); |
45 |
| - rt::scale_up(); |
46 |
| - } |
47 |
| - } |
48 |
| - }) |
49 |
| - }) |
50 |
| - .expect("cannot start a monitor thread"); |
| 11 | + |
| 12 | +/// Context of monitor thread |
| 13 | +#[derive(Debug)] |
| 14 | +pub struct Context(()); |
| 15 | + |
| 16 | +impl Context { |
| 17 | + /// Spawn a new worker thread. |
| 18 | + pub fn scale_up(&self) { |
| 19 | + rt::scale_up(); |
| 20 | + } |
| 21 | + |
| 22 | + /// Terminate a worker thread if the number of current worker thread |
| 23 | + /// is greater than number avaliable cpu |
| 24 | + pub fn scale_down(&self) { |
| 25 | + rt::scale_down(); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +pub(crate) static MONITOR: Lazy<Mutex<&'static Monitor>> = |
| 30 | + Lazy::new(|| Mutex::new(&DEFAULT_MONITOR)); |
| 31 | + |
| 32 | +/// Monitor function. |
| 33 | +pub struct Monitor(pub fn(&Context)); |
| 34 | + |
| 35 | +impl Monitor { |
| 36 | + pub(crate) fn run(&self) { |
| 37 | + let scaler = Context(()); |
| 38 | + self.0(&scaler); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl std::fmt::Debug for Monitor { |
| 43 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 44 | + f.write_str("<monitor>") |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// Replace the monitor. |
| 49 | +/// |
| 50 | +/// This will replace monitor function used by monitor thread by runtime. |
| 51 | +/// |
| 52 | +/// Monitor can only be replaced before any task spawned into runtime. |
| 53 | +/// |
| 54 | +/// Default monitor is [`DEFAULT_MONITOR`] |
| 55 | +/// |
| 56 | +/// [`DEFAULT_MONITOR`]: static.DEFAULT_MONITOR.html |
| 57 | +pub fn replace(new: &'static Monitor) -> Result<&'static Monitor, &'static str> { |
| 58 | + let mut m = MONITOR.try_lock().map_err(|_| "Cannot change monitor")?; |
| 59 | + let old: &'static Monitor = &m; |
| 60 | + *m = new; |
| 61 | + Ok(old) |
| 62 | +} |
| 63 | + |
| 64 | +async fn repeater(interval: Duration, mut before: impl FnMut(), mut after: impl FnMut()) { |
| 65 | + loop { |
| 66 | + before(); |
| 67 | + task::sleep(interval).await; |
| 68 | + after(); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +fn repeater_blocking(interval: Duration, mut before: impl FnMut(), mut after: impl FnMut()) { |
| 73 | + loop { |
| 74 | + before(); |
| 75 | + thread::sleep(interval); |
| 76 | + after(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Default monitor |
| 81 | +/// |
| 82 | +/// Whenever runtime is blocked, print warning and scale up the runtime. |
| 83 | +/// This also try to scale down when there are too many worker thread. |
| 84 | +pub static DEFAULT_MONITOR: Monitor = Monitor(default_monitor); |
| 85 | + |
| 86 | +fn default_monitor(scaler: &Context) { |
| 87 | + const PROB_INTERVAL: Duration = Duration::from_millis(500); |
| 88 | + const SCALE_DOWN_INTERVAL: Duration = Duration::from_secs(60); |
| 89 | + |
| 90 | + let running = &Arc::new(Mutex::new(false)); |
| 91 | + |
| 92 | + { |
| 93 | + let running = Arc::clone(running); |
| 94 | + task::spawn(repeater( |
| 95 | + PROB_INTERVAL, |
| 96 | + move || { |
| 97 | + *running.lock().unwrap() = true; |
| 98 | + }, |
| 99 | + || {}, |
| 100 | + )); |
| 101 | + } |
| 102 | + |
| 103 | + let mut next_scalling_down = Instant::now() + SCALE_DOWN_INTERVAL; |
| 104 | + |
| 105 | + repeater_blocking( |
| 106 | + PROB_INTERVAL + Duration::from_millis(50), |
| 107 | + || { |
| 108 | + *running.lock().unwrap() = false; |
| 109 | + }, |
| 110 | + move || { |
| 111 | + if !*running.lock().unwrap() { |
| 112 | + eprintln!("WARNING: You are blocking the runtime, please use spawn_blocking"); |
| 113 | + scaler.scale_up(); |
| 114 | + } |
| 115 | + |
| 116 | + if next_scalling_down < Instant::now() { |
| 117 | + scaler.scale_down(); |
| 118 | + next_scalling_down += SCALE_DOWN_INTERVAL; |
| 119 | + } |
| 120 | + }, |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +/// Abort on blocking monitor. |
| 125 | +/// |
| 126 | +/// Whenever runtime is blocked, abort the program. |
| 127 | +pub static ABORT_ON_BLOCKING_MONITOR: Monitor = Monitor(abort_on_blocking_monitor); |
| 128 | + |
| 129 | +fn abort_on_blocking_monitor(_scaler: &Context) { |
| 130 | + const PROB_INTERVAL: Duration = Duration::from_secs(1); |
| 131 | + |
| 132 | + let running = &Arc::new(Mutex::new(false)); |
| 133 | + |
| 134 | + { |
| 135 | + let running = Arc::clone(running); |
| 136 | + task::spawn(repeater( |
| 137 | + PROB_INTERVAL, |
| 138 | + move || { |
| 139 | + *running.lock().unwrap() = true; |
| 140 | + }, |
| 141 | + || {}, |
| 142 | + )); |
| 143 | + } |
| 144 | + |
| 145 | + repeater_blocking( |
| 146 | + PROB_INTERVAL + Duration::from_millis(50), |
| 147 | + || { |
| 148 | + *running.lock().unwrap() = false; |
| 149 | + }, |
| 150 | + || { |
| 151 | + if !*running.lock().unwrap() { |
| 152 | + panic!("FATAL: You are blocking the runtime, please use spawn_blocking"); |
| 153 | + } |
| 154 | + }, |
| 155 | + ); |
51 | 156 | }
|
0 commit comments