|
| 1 | +//! Monitor for the runtime. |
| 2 | +
|
| 3 | +use std::sync::atomic::{AtomicBool, Ordering}; |
1 | 4 | use std::sync::{Arc, Mutex};
|
2 | 5 | use std::thread;
|
3 |
| -use std::time::Duration; |
| 6 | +use std::time::{Duration, Instant}; |
| 7 | + |
| 8 | +use once_cell::sync::Lazy; |
4 | 9 |
|
5 | 10 | use crate::rt;
|
6 | 11 | 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"); |
| 12 | + |
| 13 | +/// Context of monitor thread |
| 14 | +#[derive(Debug)] |
| 15 | +pub struct Context(()); |
| 16 | + |
| 17 | +impl Context { |
| 18 | + /// Spawn a new worker thread. |
| 19 | + pub fn scale_up(&self) { |
| 20 | + rt::scale_up(); |
| 21 | + } |
| 22 | + |
| 23 | + /// Terminate a worker thread if the number of current worker thread |
| 24 | + /// is greater than number avaliable cpu |
| 25 | + pub fn scale_down(&self) { |
| 26 | + rt::scale_down(); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +pub(crate) static MONITOR: Lazy<Mutex<&'static Monitor>> = Lazy::new(|| Mutex::new(&DEFAULT)); |
| 31 | + |
| 32 | +/// Monitor function. |
| 33 | +pub struct Monitor(pub fn(&Context)); |
| 34 | + |
| 35 | +impl Monitor { |
| 36 | + pub(crate) fn run(&self) { |
| 37 | + let ctx = Context(()); |
| 38 | + self.0(&ctx); |
| 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`] |
| 55 | +/// |
| 56 | +/// [`DEFAULT`]: static.DEFAULT.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 | +/// Default monitor |
| 65 | +/// |
| 66 | +/// Whenever runtime is blocked, print warning and scale up the runtime. |
| 67 | +/// This also try to scale down when there are too many worker thread. |
| 68 | +pub static DEFAULT: Monitor = Monitor(default); |
| 69 | + |
| 70 | +fn default(ctx: &Context) { |
| 71 | + const PROB_INTERVAL: Duration = Duration::from_millis(500); |
| 72 | + const SCALE_DOWN_INTERVAL: Duration = Duration::from_secs(10); |
| 73 | + |
| 74 | + let running = &Arc::new(AtomicBool::new(false)); |
| 75 | + |
| 76 | + { |
| 77 | + let running = Arc::clone(running); |
| 78 | + task::spawn(async move { |
| 79 | + loop { |
| 80 | + running.store(true, Ordering::SeqCst); |
| 81 | + task::sleep(PROB_INTERVAL).await; |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + let mut next_scalling_down = Instant::now() + SCALE_DOWN_INTERVAL; |
| 87 | + |
| 88 | + loop { |
| 89 | + running.store(false, Ordering::SeqCst); |
| 90 | + thread::sleep(PROB_INTERVAL + Duration::from_millis(10)); |
| 91 | + if !running.load(Ordering::SeqCst) { |
| 92 | + eprintln!("WARNING: You are blocking the runtime, please use spawn_blocking"); |
| 93 | + ctx.scale_up(); |
| 94 | + } |
| 95 | + |
| 96 | + if next_scalling_down <= Instant::now() { |
| 97 | + ctx.scale_down(); |
| 98 | + next_scalling_down += SCALE_DOWN_INTERVAL; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/// Abort on blocking. |
| 104 | +/// |
| 105 | +/// Will panic whenever runtime is blocked. |
| 106 | +pub static PANIC_ON_BLOCKING: Monitor = Monitor(panic_on_blocking); |
| 107 | + |
| 108 | +fn panic_on_blocking(_ctx: &Context) { |
| 109 | + const PROB_INTERVAL: Duration = Duration::from_millis(500); |
| 110 | + |
| 111 | + let running = &Arc::new(AtomicBool::new(false)); |
| 112 | + |
| 113 | + { |
| 114 | + let running = Arc::clone(running); |
| 115 | + task::spawn(async move { |
| 116 | + loop { |
| 117 | + running.store(true, Ordering::SeqCst); |
| 118 | + task::sleep(PROB_INTERVAL).await; |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + loop { |
| 124 | + running.store(false, Ordering::SeqCst); |
| 125 | + thread::sleep(PROB_INTERVAL + PROB_INTERVAL); |
| 126 | + if !running.load(Ordering::SeqCst) { |
| 127 | + panic!("FATAL: You are blocking the runtime, please use spawn_blocking"); |
| 128 | + } |
| 129 | + } |
51 | 130 | }
|
0 commit comments