
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1205 · main · 10 min 08 s
1Étoiles
0Forks
0Issues
| 1 | //! Hibernating the stacks of parked actors (ADR 0028): each thread lists |
| 2 | //! the actors that parked on it ([`Parked`]) and, two epochs later, |
| 3 | //! hibernates those still in the same wait. |
| 4 | |
| 5 | use std::collections::VecDeque; |
| 6 | use std::sync::atomic::Ordering; |
| 7 | use std::time::Duration; |
| 8 | |
| 9 | use crate::otp::Wait; |
| 10 | |
| 11 | use super::Shared; |
| 12 | use super::actor::Proc; |
| 13 | use super::local::LOCAL; |
| 14 | |
| 15 | /// Actors that parked on this thread (`run_slice`), oldest first, each at |
| 16 | /// most once (`Cell::listed`), with the epoch it was listed in. Two epochs |
| 17 | /// later the thread looks at it again: hibernated if it is still in a wait |
| 18 | /// that began two epochs ago, listed again if it parked since, dropped if |
| 19 | /// it is running or gone. |
| 20 | pub(super) struct Parked { |
| 21 | pub(super) list: VecDeque<(i64, u32)>, |
| 22 | /// Calls since the clock was last read. |
| 23 | pub(super) count: u32, |
| 24 | } |
| 25 | |
| 26 | impl Parked { |
| 27 | /// The entries two epochs old, at `epoch`. |
| 28 | fn due(&mut self, epoch: u32, out: &mut Vec<i64>) { |
| 29 | while let Some(&(pid, listed)) = self.list.front() { |
| 30 | if listed.wrapping_add(2) > epoch { |
| 31 | break; |
| 32 | } |
| 33 | self.list.pop_front(); |
| 34 | out.push(pid); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// Whether the actor waits in a way that nothing else reads its stack |
| 40 | /// in: a receive or a sleep. Not a blocking call (it runs on the actor's |
| 41 | /// values), nor a socket wait (short by design). |
| 42 | pub(super) fn hibernatable(p: &Proc) -> bool { |
| 43 | matches!(p.wait, Wait::Receive { .. } | Wait::Sleep { .. }) && !p.dead |
| 44 | } |
| 45 | |
| 46 | /// The epoch now: counts one more if [`PARKED_EPOCH`](super::PARKED_EPOCH) |
| 47 | /// has passed. |
| 48 | fn epoch(sched: &Shared) -> u32 { |
| 49 | let now = sched.started.elapsed().as_nanos() as u64; |
| 50 | let at = sched.epoch_at.load(Ordering::Relaxed); |
| 51 | if now.saturating_sub(at) >= sched.epoch_ns |
| 52 | && sched |
| 53 | .epoch_at |
| 54 | .compare_exchange(at, now, Ordering::Relaxed, Ordering::Relaxed) |
| 55 | .is_ok() |
| 56 | { |
| 57 | sched.epoch.fetch_add(1, Ordering::Relaxed); |
| 58 | } |
| 59 | sched.epoch.load(Ordering::Relaxed) |
| 60 | } |
| 61 | |
| 62 | /// `pid` parked on this thread, and was not listed: list it. |
| 63 | pub(super) fn parked_here(sched: &Shared, pid: i64) { |
| 64 | let epoch = sched.epoch.load(Ordering::Relaxed); |
| 65 | LOCAL.with(|l| { |
| 66 | if let Some(local) = l.borrow_mut().as_mut() { |
| 67 | local.parked.list.push_back((pid, epoch)); |
| 68 | } |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | /// Look again at this thread's listed actors that are due, and hibernate |
| 73 | /// those still parked since then (`check`: read the clock now, as after a |
| 74 | /// sleep; else every 256 calls). |
| 75 | pub(super) fn hibernate_due(sched: &Shared, check: bool) { |
| 76 | let mut due = Vec::new(); |
| 77 | LOCAL.with(|l| { |
| 78 | if let Some(local) = l.borrow_mut().as_mut() |
| 79 | && !local.parked.list.is_empty() |
| 80 | { |
| 81 | let parked = &mut local.parked; |
| 82 | parked.count = parked.count.wrapping_add(1); |
| 83 | if check || sched.epoch_ns == 0 || parked.count.is_multiple_of(256) { |
| 84 | parked.due(epoch(sched), &mut due); |
| 85 | } |
| 86 | } |
| 87 | }); |
| 88 | for pid in due { |
| 89 | hibernate(sched, pid); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /// How long this thread may sleep before it must hibernate parked actors: |
| 94 | /// an epoch if it has any, else as long as it likes. |
| 95 | pub(super) fn parked_timeout(sched: &Shared) -> Option<Duration> { |
| 96 | LOCAL.with(|l| { |
| 97 | l.borrow() |
| 98 | .as_ref() |
| 99 | .is_some_and(|local| !local.parked.list.is_empty()) |
| 100 | .then(|| Duration::from_nanos(sched.epoch_ns.max(1_000_000))) |
| 101 | }) |
| 102 | } |
| 103 | |
| 104 | /// Hibernate `pid`'s stack if it has been parked for two epochs: under |
| 105 | /// its lock, suspended and not queued, nothing can resume it, and in a |
| 106 | /// receive or a sleep nothing else reads its stack. `run_slice` thaws it |
| 107 | /// before it runs again, whoever wakes it (a message, a timer, a kill). |
| 108 | /// Parked since less long: listed again. Running or gone: dropped (it is |
| 109 | /// listed again when it parks). |
| 110 | fn hibernate(sched: &Shared, pid: i64) { |
| 111 | let Some(cell) = sched.cell(pid) else { |
| 112 | return; |
| 113 | }; |
| 114 | let mut p = cell.lock(); |
| 115 | let since = cell.parked_epoch.load(Ordering::Relaxed); |
| 116 | let now = sched.epoch.load(Ordering::Relaxed); |
| 117 | let parked = hibernatable(&p); |
| 118 | match p.run.idle_fiber() { |
| 119 | Some(fiber) if parked && since.wrapping_add(2) <= now => fiber.hibernate(), |
| 120 | Some(_) if parked => { |
| 121 | drop(p); |
| 122 | LOCAL.with(|l| { |
| 123 | if let Some(local) = l.borrow_mut().as_mut() { |
| 124 | local.parked.list.push_back((pid, since)); |
| 125 | } |
| 126 | }); |
| 127 | return; |
| 128 | } |
| 129 | _ => {} |
| 130 | } |
| 131 | cell.listed.store(false, Ordering::Relaxed); |
| 132 | } |