
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#1203 · main · 9 min 56 s
1Étoiles
0Forks
0Issues
| 1 | //! The actor table, and the map keyed by the runtime's own ids it is |
| 2 | //! built on. |
| 3 | |
| 4 | use std::collections::HashMap; |
| 5 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 6 | use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; |
| 7 | |
| 8 | use super::actor::Cell; |
| 9 | #[cfg(feature = "lock-stats")] |
| 10 | use super::lock_stats; |
| 11 | |
| 12 | /// A map keyed by the runtime's own ids (pids, subject tags), hashed with |
| 13 | /// one multiplication (as FxHash does). The ids are counters, not |
| 14 | /// program input, so SipHash's resistance to collision attacks buys |
| 15 | /// nothing, and hashing pids with it cost 6% of `tests/bench/messages` |
| 16 | /// (issue #61). |
| 17 | pub(crate) type IdMap<V> = HashMap<i64, V, std::hash::BuildHasherDefault<IdHasher>>; |
| 18 | |
| 19 | #[derive(Default)] |
| 20 | pub(crate) struct IdHasher(u64); |
| 21 | |
| 22 | impl std::hash::Hasher for IdHasher { |
| 23 | fn finish(&self) -> u64 { |
| 24 | self.0 |
| 25 | } |
| 26 | |
| 27 | fn write(&mut self, bytes: &[u8]) { |
| 28 | for b in bytes { |
| 29 | self.write_u64(u64::from(*b)); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn write_u64(&mut self, n: u64) { |
| 34 | self.0 = (self.0.rotate_left(5) ^ n).wrapping_mul(0x517c_c1b7_2722_0a95); |
| 35 | } |
| 36 | |
| 37 | fn write_i64(&mut self, n: i64) { |
| 38 | self.write_u64(n as u64); |
| 39 | } |
| 40 | |
| 41 | fn write_usize(&mut self, n: usize) { |
| 42 | self.write_u64(n as u64); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// The actor table: every actor whose fiber has not finished, by pid. |
| 47 | /// |
| 48 | /// In [`SHARDS`] parts by pid, each behind its own lock (issue #131): one |
| 49 | /// lock was taken for every lookup of another actor (a send, a link) and |
| 50 | /// every spawn and finish, and on 22 threads `tests/bench/churn.lse` |
| 51 | /// found it taken for 58% of its reads and 86% of its writes. Consecutive |
| 52 | /// pids fall in different shards. |
| 53 | pub(super) struct Table { |
| 54 | shards: [RwLock<IdMap<Arc<Cell>>>; SHARDS], |
| 55 | /// The number of actors, kept beside the shards: whether the table |
| 56 | /// is empty is known when an actor is removed without locking them all. |
| 57 | live: AtomicUsize, |
| 58 | } |
| 59 | |
| 60 | const SHARDS: usize = 64; |
| 61 | |
| 62 | impl Default for Table { |
| 63 | fn default() -> Table { |
| 64 | Table { |
| 65 | shards: std::array::from_fn(|_| RwLock::new(IdMap::default())), |
| 66 | live: AtomicUsize::new(0), |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl Table { |
| 72 | fn shard(&self, pid: i64) -> &RwLock<IdMap<Arc<Cell>>> { |
| 73 | &self.shards[pid as usize % SHARDS] |
| 74 | } |
| 75 | |
| 76 | fn read(&self, pid: i64) -> RwLockReadGuard<'_, IdMap<Arc<Cell>>> { |
| 77 | #[cfg(feature = "lock-stats")] |
| 78 | return lock_stats::read(self.shard(pid)); |
| 79 | #[cfg(not(feature = "lock-stats"))] |
| 80 | self.shard(pid).read().expect("louise: scheduler poisoned") |
| 81 | } |
| 82 | |
| 83 | fn write(&self, pid: i64) -> RwLockWriteGuard<'_, IdMap<Arc<Cell>>> { |
| 84 | #[cfg(feature = "lock-stats")] |
| 85 | return lock_stats::write(self.shard(pid)); |
| 86 | #[cfg(not(feature = "lock-stats"))] |
| 87 | self.shard(pid).write().expect("louise: scheduler poisoned") |
| 88 | } |
| 89 | |
| 90 | pub(super) fn get(&self, pid: i64) -> Option<Arc<Cell>> { |
| 91 | self.read(pid).get(&pid).cloned() |
| 92 | } |
| 93 | |
| 94 | /// Add a new actor. Counted first: an actor it spawns can only finish |
| 95 | /// after this. |
| 96 | pub(super) fn insert(&self, pid: i64, cell: Arc<Cell>) { |
| 97 | self.live.fetch_add(1, Ordering::AcqRel); |
| 98 | self.write(pid).insert(pid, cell); |
| 99 | } |
| 100 | |
| 101 | /// Remove a finished actor; whether it was the last. |
| 102 | pub(super) fn remove(&self, pid: i64) -> bool { |
| 103 | let removed = self.write(pid).remove(&pid).is_some(); |
| 104 | removed && self.live.fetch_sub(1, Ordering::AcqRel) == 1 |
| 105 | } |
| 106 | |
| 107 | /// Every actor, in pid order (the order they were spawned in, as the |
| 108 | /// interpreter keeps them). |
| 109 | pub(super) fn all(&self) -> Vec<(i64, Arc<Cell>)> { |
| 110 | let mut all: Vec<(i64, Arc<Cell>)> = Vec::new(); |
| 111 | for shard in &self.shards { |
| 112 | #[cfg(feature = "lock-stats")] |
| 113 | let shard = lock_stats::read(shard); |
| 114 | #[cfg(not(feature = "lock-stats"))] |
| 115 | let shard = shard.read().expect("louise: scheduler poisoned"); |
| 116 | all.extend(shard.iter().map(|(pid, cell)| (*pid, cell.clone()))); |
| 117 | } |
| 118 | all.sort_unstable_by_key(|(pid, _)| *pid); |
| 119 | all |
| 120 | } |
| 121 | } |