| @@ -158,7 +158,7 @@ impl Actor { |
| 158 | 158 | pub(crate) fn with_proc<R>(&self, f: impl FnOnce(&mut Proc) -> R) -> R { |
| 159 | 159 | // SAFETY: the cell outlives the actor's fiber, which is running. |
| 160 | 160 | let cell = unsafe { &*self.cell }; |
| 161 | | − let mut p = cell.proc.lock().expect("louise: actor poisoned"); |
| 161 | + let mut p = cell.lock(); |
| 162 | 162 | f(&mut p) |
| 163 | 163 | } |
| 164 | 164 | |
| @@ -210,6 +210,64 @@ pub(crate) struct Cell { |
| 210 | 210 | proc: Mutex<Proc>, |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | +impl Cell { |
| 214 | + /// Take the actor's lock. |
| 215 | + fn lock(&self) -> MutexGuard<'_, Proc> { |
| 216 | + #[cfg(feature = "lock-stats")] |
| 217 | + return lock_stats::lock(&self.proc); |
| 218 | + #[cfg(not(feature = "lock-stats"))] |
| 219 | + self.proc.lock().expect("louise: actor poisoned") |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +/// `--features lock-stats` (ADR 0028): how many times scheduler threads |
| 224 | +/// took an actor's lock, and how many of those found it taken. Counted per |
| 225 | +/// thread (a shared counter would itself be contended), added up when the |
| 226 | +/// thread stops, and printed at exit with `LOUISE_LEAKS`. |
| 227 | +#[cfg(feature = "lock-stats")] |
| 228 | +pub(crate) mod lock_stats { |
| 229 | + use std::sync::atomic::{AtomicU64, Ordering}; |
| 230 | + use std::sync::{Mutex, MutexGuard, TryLockError}; |
| 231 | + |
| 232 | + static TAKEN: AtomicU64 = AtomicU64::new(0); |
| 233 | + static CONTENDED: AtomicU64 = AtomicU64::new(0); |
| 234 | + |
| 235 | + thread_local! { |
| 236 | + // (taken, contended). Read and written only between suspensions. |
| 237 | + static MINE: std::cell::Cell<(u64, u64)> = const { std::cell::Cell::new((0, 0)) }; |
| 238 | + } |
| 239 | + |
| 240 | + pub(super) fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> { |
| 241 | + let (taken, contended) = MINE.get(); |
| 242 | + match m.try_lock() { |
| 243 | + Ok(guard) => { |
| 244 | + MINE.set((taken + 1, contended)); |
| 245 | + guard |
| 246 | + } |
| 247 | + Err(TryLockError::WouldBlock) => { |
| 248 | + MINE.set((taken + 1, contended + 1)); |
| 249 | + m.lock().expect("louise: actor poisoned") |
| 250 | + } |
| 251 | + Err(TryLockError::Poisoned(_)) => panic!("louise: actor poisoned"), |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + /// Add this thread's counts to the totals. |
| 256 | + pub(super) fn flush() { |
| 257 | + let (taken, contended) = MINE.replace((0, 0)); |
| 258 | + TAKEN.fetch_add(taken, Ordering::Relaxed); |
| 259 | + CONTENDED.fetch_add(contended, Ordering::Relaxed); |
| 260 | + } |
| 261 | + |
| 262 | + /// (taken, contended), once the scheduler threads stopped. |
| 263 | + pub(crate) fn totals() -> (u64, u64) { |
| 264 | + ( |
| 265 | + TAKEN.load(Ordering::Relaxed), |
| 266 | + CONTENDED.load(Ordering::Relaxed), |
| 267 | + ) |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 213 | 271 | // SAFETY: `actor` is dereferenced only by the thread running the actor |
| 214 | 272 | // (and freed after its fiber returned); the rest is atomic or locked. |
| 215 | 273 | unsafe impl Send for Cell {} |
| @@ -459,14 +517,14 @@ impl Shared { |
| 459 | 517 | /// Run `f` on the OTP state of `pid`, if its fiber has not finished. |
| 460 | 518 | pub(crate) fn with_proc<R>(&self, pid: i64, f: impl FnOnce(&mut Proc) -> R) -> Option<R> { |
| 461 | 519 | let cell = self.cell(pid)?; |
| 462 | | − let mut p = cell.proc.lock().expect("louise: actor poisoned"); |
| 520 | + let mut p = cell.lock(); |
| 463 | 521 | Some(f(&mut p)) |
| 464 | 522 | } |
| 465 | 523 | |
| 466 | 524 | /// Run `f` on the OTP state of `pid`, if it is alive (not exited). |
| 467 | 525 | pub(crate) fn with_alive<R>(&self, pid: i64, f: impl FnOnce(&mut Proc) -> R) -> Option<R> { |
| 468 | 526 | let cell = self.cell(pid)?; |
| 469 | | − let mut p = cell.proc.lock().expect("louise: actor poisoned"); |
| 527 | + let mut p = cell.lock(); |
| 470 | 528 | if p.dead { |
| 471 | 529 | return None; |
| 472 | 530 | } |
| @@ -783,7 +841,7 @@ pub(crate) fn exit_actor(sched: &Shared, pid: i64, reason: Exit) { |
| 783 | 841 | return; |
| 784 | 842 | }; |
| 785 | 843 | let exited = { |
| 786 | | − let mut p = cell.proc.lock().expect("louise: actor poisoned"); |
| 844 | + let mut p = cell.lock(); |
| 787 | 845 | let exited = p.exit(); |
| 788 | 846 | // Its fiber unwinds the next time it runs (after a blocking call |
| 789 | 847 | // returns: that call still reads the actor's values). |
| @@ -849,7 +907,7 @@ fn stop_all(sched: &Shared) { |
| 849 | 907 | for (pid, cell) in cells { |
| 850 | 908 | cell.kill.store(true, Ordering::Release); |
| 851 | 909 | let wake = { |
| 852 | | − let mut p = cell.proc.lock().expect("louise: actor poisoned"); |
| 910 | + let mut p = cell.lock(); |
| 853 | 911 | p.dead = true; |
| 854 | 912 | p.interrupt() |
| 855 | 913 | }; |
| @@ -958,6 +1016,8 @@ fn worker(sched: &Shared, index: usize, queue: Worker<i64>) { |
| 958 | 1016 | } |
| 959 | 1017 | } |
| 960 | 1018 | LOCAL.with(|l| l.borrow_mut().take()); |
| 1019 | + #[cfg(feature = "lock-stats")] |
| 1020 | + lock_stats::flush(); |
| 961 | 1021 | } |
| 962 | 1022 | |
| 963 | 1023 | /// The next actor for thread `index`: its own queue first (FIFO), then |
| @@ -1006,12 +1066,7 @@ fn run_slice(sched: &Shared, pid: i64) { |
| 1006 | 1066 | stop_running(sched); |
| 1007 | 1067 | return; |
| 1008 | 1068 | }; |
| 1009 | | − let fiber = cell |
| 1010 | | − .proc |
| 1011 | | − .lock() |
| 1012 | | − .expect("louise: actor poisoned") |
| 1013 | | − .run |
| 1014 | | − .start(); |
| 1069 | + let fiber = cell.lock().run.start(); |
| 1015 | 1070 | let Some(mut fiber) = fiber else { |
| 1016 | 1071 | stop_running(sched); |
| 1017 | 1072 | return; |
| @@ -1030,7 +1085,7 @@ fn run_slice(sched: &Shared, pid: i64) { |
| 1030 | 1085 | match result { |
| 1031 | 1086 | CoroutineResult::Yield(()) => { |
| 1032 | 1087 | let again = { |
| 1033 | | − let mut p = cell.proc.lock().expect("louise: actor poisoned"); |
| 1088 | + let mut p = cell.lock(); |
| 1034 | 1089 | p.stop(fiber) |
| 1035 | 1090 | }; |
| 1036 | 1091 | if again { |
| @@ -1042,11 +1097,7 @@ fn run_slice(sched: &Shared, pid: i64) { |
| 1042 | 1097 | } |
| 1043 | 1098 | } |
| 1044 | 1099 | CoroutineResult::Return(()) => { |
| 1045 | | − cell.proc |
| 1046 | | − .lock() |
| 1047 | | − .expect("louise: actor poisoned") |
| 1048 | | − .run |
| 1049 | | − .finish(); |
| 1100 | + cell.lock().run.finish(); |
| 1050 | 1101 | let empty = { |
| 1051 | 1102 | let mut actors = sched.actors.write().expect("louise: scheduler poisoned"); |
| 1052 | 1103 | actors.remove(&pid); |