//! The actor table, and the map keyed by the runtime's own ids it is //! built on. use std::collections::HashMap; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; use super::actor::Cell; #[cfg(feature = "lock-stats")] use super::lock_stats; /// A map keyed by the runtime's own ids (pids, subject tags), hashed with /// one multiplication (as FxHash does). The ids are counters, not /// program input, so SipHash's resistance to collision attacks buys /// nothing, and hashing pids with it cost 6% of `tests/bench/messages` /// (issue #61). pub(crate) type IdMap = HashMap>; #[derive(Default)] pub(crate) struct IdHasher(u64); impl std::hash::Hasher for IdHasher { fn finish(&self) -> u64 { self.0 } fn write(&mut self, bytes: &[u8]) { for b in bytes { self.write_u64(u64::from(*b)); } } fn write_u64(&mut self, n: u64) { self.0 = (self.0.rotate_left(5) ^ n).wrapping_mul(0x517c_c1b7_2722_0a95); } fn write_i64(&mut self, n: i64) { self.write_u64(n as u64); } fn write_usize(&mut self, n: usize) { self.write_u64(n as u64); } } /// The actor table: every actor whose fiber has not finished, by pid. /// /// In [`SHARDS`] parts by pid, each behind its own lock (issue #131): one /// lock was taken for every lookup of another actor (a send, a link) and /// every spawn and finish, and on 22 threads `tests/bench/churn.lse` /// found it taken for 58% of its reads and 86% of its writes. Consecutive /// pids fall in different shards. pub(super) struct Table { shards: [RwLock>>; SHARDS], /// The number of actors, kept beside the shards: whether the table /// is empty is known when an actor is removed without locking them all. live: AtomicUsize, } const SHARDS: usize = 64; impl Default for Table { fn default() -> Table { Table { shards: std::array::from_fn(|_| RwLock::new(IdMap::default())), live: AtomicUsize::new(0), } } } impl Table { fn shard(&self, pid: i64) -> &RwLock>> { &self.shards[pid as usize % SHARDS] } fn read(&self, pid: i64) -> RwLockReadGuard<'_, IdMap>> { #[cfg(feature = "lock-stats")] return lock_stats::read(self.shard(pid)); #[cfg(not(feature = "lock-stats"))] self.shard(pid).read().expect("louise: scheduler poisoned") } fn write(&self, pid: i64) -> RwLockWriteGuard<'_, IdMap>> { #[cfg(feature = "lock-stats")] return lock_stats::write(self.shard(pid)); #[cfg(not(feature = "lock-stats"))] self.shard(pid).write().expect("louise: scheduler poisoned") } pub(super) fn get(&self, pid: i64) -> Option> { self.read(pid).get(&pid).cloned() } /// Add a new actor. Counted first: an actor it spawns can only finish /// after this. pub(super) fn insert(&self, pid: i64, cell: Arc) { self.live.fetch_add(1, Ordering::AcqRel); self.write(pid).insert(pid, cell); } /// Remove a finished actor; whether it was the last. pub(super) fn remove(&self, pid: i64) -> bool { let removed = self.write(pid).remove(&pid).is_some(); removed && self.live.fetch_sub(1, Ordering::AcqRel) == 1 } /// Every actor, in pid order (the order they were spawned in, as the /// interpreter keeps them). pub(super) fn all(&self) -> Vec<(i64, Arc)> { let mut all: Vec<(i64, Arc)> = Vec::new(); for shard in &self.shards { #[cfg(feature = "lock-stats")] let shard = lock_stats::read(shard); #[cfg(not(feature = "lock-stats"))] let shard = shard.read().expect("louise: scheduler poisoned"); all.extend(shard.iter().map(|(pid, cell)| (*pid, cell.clone()))); } all.sort_unstable_by_key(|(pid, _)| *pid); all } }