
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 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 2 | use std::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard, TryLockError}; |
| 3 | |
| 4 | /// The locks counted. |
| 5 | #[derive(Clone, Copy)] |
| 6 | pub(crate) enum Kind { |
| 7 | Actor, |
| 8 | Control, |
| 9 | TableRead, |
| 10 | TableWrite, |
| 11 | Stacks, |
| 12 | } |
| 13 | |
| 14 | const KINDS: usize = 5; |
| 15 | |
| 16 | /// What each [`Kind`] is called in the report. |
| 17 | pub(crate) const NAMES: [&str; KINDS] = [ |
| 18 | "actor locks", |
| 19 | "control lock", |
| 20 | "actor table (read)", |
| 21 | "actor table (write)", |
| 22 | "stack pool", |
| 23 | ]; |
| 24 | |
| 25 | static TAKEN: [AtomicU64; KINDS] = [const { AtomicU64::new(0) }; KINDS]; |
| 26 | static CONTENDED: [AtomicU64; KINDS] = [const { AtomicU64::new(0) }; KINDS]; |
| 27 | |
| 28 | thread_local! { |
| 29 | // (taken, contended) per kind. Read and written only between |
| 30 | // suspensions. |
| 31 | static MINE: std::cell::Cell<[(u64, u64); KINDS]> = |
| 32 | const { std::cell::Cell::new([(0, 0); KINDS]) }; |
| 33 | } |
| 34 | |
| 35 | fn count(kind: Kind, contended: bool) { |
| 36 | let mut mine = MINE.get(); |
| 37 | let (taken, found) = &mut mine[kind as usize]; |
| 38 | *taken += 1; |
| 39 | *found += u64::from(contended); |
| 40 | MINE.set(mine); |
| 41 | } |
| 42 | |
| 43 | fn counted<G, E>(kind: Kind, tried: Result<G, TryLockError<E>>, wait: impl FnOnce() -> G) -> G { |
| 44 | match tried { |
| 45 | Ok(guard) => { |
| 46 | count(kind, false); |
| 47 | guard |
| 48 | } |
| 49 | Err(TryLockError::WouldBlock) => { |
| 50 | count(kind, true); |
| 51 | wait() |
| 52 | } |
| 53 | Err(TryLockError::Poisoned(_)) => panic!("louise: scheduler poisoned"), |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | pub(super) fn lock<T>(kind: Kind, m: &Mutex<T>) -> MutexGuard<'_, T> { |
| 58 | counted(kind, m.try_lock(), || { |
| 59 | m.lock().expect("louise: scheduler poisoned") |
| 60 | }) |
| 61 | } |
| 62 | |
| 63 | pub(super) fn read<T>(m: &RwLock<T>) -> RwLockReadGuard<'_, T> { |
| 64 | counted(Kind::TableRead, m.try_read(), || { |
| 65 | m.read().expect("louise: scheduler poisoned") |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | pub(super) fn write<T>(m: &RwLock<T>) -> RwLockWriteGuard<'_, T> { |
| 70 | counted(Kind::TableWrite, m.try_write(), || { |
| 71 | m.write().expect("louise: scheduler poisoned") |
| 72 | }) |
| 73 | } |
| 74 | |
| 75 | /// Add this thread's counts to the totals. |
| 76 | pub(super) fn flush() { |
| 77 | for (i, (taken, contended)) in MINE.replace([(0, 0); KINDS]).into_iter().enumerate() { |
| 78 | TAKEN[i].fetch_add(taken, Ordering::Relaxed); |
| 79 | CONTENDED[i].fetch_add(contended, Ordering::Relaxed); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// (taken, contended) per kind, once the scheduler threads stopped. |
| 84 | pub(crate) fn totals() -> [(u64, u64); KINDS] { |
| 85 | std::array::from_fn(|i| { |
| 86 | ( |
| 87 | TAKEN[i].load(Ordering::Relaxed), |
| 88 | CONTENDED[i].load(Ordering::Relaxed), |
| 89 | ) |
| 90 | }) |
| 91 | } |