| @@ -7,7 +7,8 @@ |
| 7 | 7 | //! operation (each of which is a scheduling point in the interpreter); |
| 8 | 8 | //! - spawned actors run after their parent's current slice; |
| 9 | 9 | //! - time is virtual: when no actor can run, the clock jumps to the next |
| 10 | | −//! deadline, and due actors wake in pid order; |
| 10 | +//! deadline, and due actors wake in pid order. Once the program opens a |
| 11 | +//! socket, the clock follows real time instead (see [`sync_clock`]); |
| 11 | 12 | //! - exits follow Erlang. An abnormal exit stops linked actors, or sends |
| 12 | 13 | //! them an `ExitSignal` if they trap exits; monitors receive `Down`; |
| 13 | 14 | //! - when `main` stops, the other actors are stopped. |
| @@ -285,6 +286,9 @@ struct Control { |
| 285 | 286 | io_ready: HashSet<mio::Token>, |
| 286 | 287 | /// Stacks of `PROGRAM_STACK_SIZE` whose fibers finished. |
| 287 | 288 | stacks: Vec<Stack>, |
| 289 | + /// Set when the first socket opens: from then on the clock follows |
| 290 | + /// real time, counted up to this instant. |
| 291 | + synced: Option<std::time::Instant>, |
| 288 | 292 | } |
| 289 | 293 | |
| 290 | 294 | /// The scheduler, shared by its threads. |
| @@ -296,8 +300,11 @@ pub(crate) struct Shared { |
| 296 | 300 | actors: RwLock<HashMap<i64, Arc<Cell>>>, |
| 297 | 301 | next_pid: AtomicI64, |
| 298 | 302 | next_tag: AtomicI64, |
| 299 | | − /// Virtual time in milliseconds (written under `control`). |
| 303 | + /// Time in milliseconds (written under `control`): virtual, or real |
| 304 | + /// once `real_time`. |
| 300 | 305 | clock: AtomicU64, |
| 306 | + /// The clock follows real time (`Control::synced` is set). |
| 307 | + real_time: AtomicBool, |
| 301 | 308 | root: AtomicI64, |
| 302 | 309 | control: Mutex<Control>, |
| 303 | 310 | /// Run queues: one per thread (in `LOCAL`), plus this one for actors |
| @@ -396,6 +403,7 @@ impl Shared { |
| 396 | 403 | next_pid: AtomicI64::new(0), |
| 397 | 404 | next_tag: AtomicI64::new(0), |
| 398 | 405 | clock: AtomicU64::new(0), |
| 406 | + real_time: AtomicBool::new(false), |
| 399 | 407 | root: AtomicI64::new(-1), |
| 400 | 408 | control: Mutex::new(Control::default()), |
| 401 | 409 | injector: Injector::new(), |
| @@ -427,7 +435,8 @@ impl Shared { |
| 427 | 435 | self.next_tag.fetch_add(1, Ordering::Relaxed) + 1 |
| 428 | 436 | } |
| 429 | 437 | |
| 430 | | − /// The virtual clock, in milliseconds. |
| 438 | + /// The clock, in milliseconds. In real time, it may lag by the |
| 439 | + /// slice running when it was last counted. |
| 431 | 440 | pub(crate) fn clock(&self) -> u64 { |
| 432 | 441 | self.clock.load(Ordering::Acquire) |
| 433 | 442 | } |
| @@ -491,8 +500,16 @@ impl Shared { |
| 491 | 500 | self.nudge_idler(); |
| 492 | 501 | } |
| 493 | 502 | |
| 503 | + /// A token for a new socket. The first switches the clock to real |
| 504 | + /// time: a program talking to the outside world cannot be replayed |
| 505 | + /// anyway, and its timeouts (an idle connection's, say) must expire |
| 506 | + /// even while other actors keep every thread busy. |
| 494 | 507 | pub(crate) fn io_token(&self) -> mio::Token { |
| 495 | 508 | let mut c = self.control(); |
| 509 | + if c.synced.is_none() { |
| 510 | + c.synced = Some(std::time::Instant::now()); |
| 511 | + self.real_time.store(true, Ordering::SeqCst); |
| 512 | + } |
| 496 | 513 | c.next_token += 1; |
| 497 | 514 | mio::Token(c.next_token) |
| 498 | 515 | } |
| @@ -1008,10 +1025,14 @@ fn stop_running(sched: &Shared) { |
| 1008 | 1025 | } |
| 1009 | 1026 | } |
| 1010 | 1027 | |
| 1011 | | −/// Between slices: collect finished blocking calls and ready sockets, |
| 1012 | | −/// without waiting (skipped if another thread is at it). |
| 1028 | +/// Between slices: collect finished blocking calls, ready sockets and, |
| 1029 | +/// in real time, due timers, without waiting (skipped if another thread |
| 1030 | +/// is at it). |
| 1013 | 1031 | fn collect_events(sched: &Shared) { |
| 1014 | | − if sched.blocking.load(Ordering::SeqCst) == 0 && sched.io_waiting.load(Ordering::SeqCst) == 0 { |
| 1032 | + if sched.blocking.load(Ordering::SeqCst) == 0 |
| 1033 | + && sched.io_waiting.load(Ordering::SeqCst) == 0 |
| 1034 | + && !sched.real_time.load(Ordering::SeqCst) |
| 1035 | + { |
| 1015 | 1036 | return; |
| 1016 | 1037 | } |
| 1017 | 1038 | let Ok(mut c) = sched.control.try_lock() else { |
| @@ -1026,6 +1047,7 @@ fn collect_events(sched: &Shared) { |
| 1026 | 1047 | c.poll = Some(poll); |
| 1027 | 1048 | wake_io(sched, &mut c, &events); |
| 1028 | 1049 | } |
| 1050 | + fire_due(sched, &mut c); |
| 1029 | 1051 | } |
| 1030 | 1052 | |
| 1031 | 1053 | /// No actor runs or is queued: wait for blocking calls, sockets or the |
| @@ -1036,53 +1058,44 @@ fn collect_events(sched: &Shared) { |
| 1036 | 1058 | /// timers and new work signal the reactor (`WAKER`), so the reactor is the |
| 1037 | 1059 | /// only thing waited on. |
| 1038 | 1060 | fn idle(sched: &Shared) { |
| 1039 | | − let (mut poll, real_time, clock, deadline) = { |
| 1061 | + let (mut poll, timeout) = { |
| 1040 | 1062 | let mut c = sched.control(); |
| 1041 | 1063 | collect_blocking(sched, &mut c); |
| 1064 | + fire_due(sched, &mut c); |
| 1042 | 1065 | if sched.busy() { |
| 1043 | 1066 | return; |
| 1044 | 1067 | } |
| 1068 | + let real_time = c.synced.is_some(); |
| 1069 | + let deadline = next_deadline(sched, &mut c); |
| 1045 | 1070 | if c.io_waiters.is_empty() && c.blocking == 0 { |
| 1046 | | − // Only the clock can wake an actor now. |
| 1047 | | − if !advance_clock(sched, &mut c) { |
| 1071 | + // Only the clock can wake an actor now: jump to its next |
| 1072 | + // deadline, or (in real time) wait for it. |
| 1073 | + if !real_time && advance_clock(sched, &mut c) { |
| 1074 | + return; |
| 1075 | + } |
| 1076 | + if deadline.is_none() { |
| 1048 | 1077 | drop(c); |
| 1049 | 1078 | deadlock(sched); |
| 1079 | + return; |
| 1050 | 1080 | } |
| 1051 | | − return; |
| 1052 | 1081 | } |
| 1053 | | − let deadline = next_deadline(sched, &mut c); |
| 1054 | 1082 | let Some(poll) = c.poll.take() else { |
| 1055 | 1083 | return; |
| 1056 | 1084 | }; |
| 1057 | | − // Real time passes while waiting for sockets, and timers fire when |
| 1058 | | − // it reaches their deadline. Waiting only for blocking calls keeps |
| 1059 | | − // the clock virtual. |
| 1060 | | − (poll, !c.io_waiters.is_empty(), sched.clock(), deadline) |
| 1085 | + // In real time, wait until the next deadline at most. Otherwise |
| 1086 | + // only blocking calls are waited for, and the clock stays put. |
| 1087 | + let timeout = deadline |
| 1088 | + .filter(|_| real_time) |
| 1089 | + .map(|d| std::time::Duration::from_millis(d.saturating_sub(sched.clock()))); |
| 1090 | + (poll, timeout) |
| 1061 | 1091 | }; |
| 1062 | | − let timeout = match deadline { |
| 1063 | | − Some(d) if real_time => Some(std::time::Duration::from_millis(d.saturating_sub(clock))), |
| 1064 | | − _ => None, |
| 1065 | | − }; |
| 1066 | | − let start = std::time::Instant::now(); |
| 1067 | 1092 | let mut events = mio::Events::with_capacity(256); |
| 1068 | 1093 | wait_events(&mut poll, &mut events, timeout); |
| 1069 | 1094 | let mut c = sched.control(); |
| 1070 | 1095 | c.poll = Some(poll); |
| 1071 | | − if real_time { |
| 1072 | | − sched |
| 1073 | | − .clock |
| 1074 | | − .fetch_add(start.elapsed().as_millis() as u64, Ordering::AcqRel); |
| 1075 | | − } |
| 1076 | | − let woke = wake_io(sched, &mut c, &events); |
| 1096 | + wake_io(sched, &mut c, &events); |
| 1077 | 1097 | collect_blocking(sched, &mut c); |
| 1078 | | − if real_time |
| 1079 | | − && !woke |
| 1080 | | − && !sched.busy() |
| 1081 | | − && let Some(d) = deadline |
| 1082 | | − && sched.clock() >= d |
| 1083 | | − { |
| 1084 | | − advance_clock(sched, &mut c); |
| 1085 | | − } |
| 1098 | + fire_due(sched, &mut c); |
| 1086 | 1099 | } |
| 1087 | 1100 | |
| 1088 | 1101 | /// Nothing can ever run again: crash the root (stopping everyone). |
| @@ -1176,8 +1189,33 @@ fn next_deadline(sched: &Shared, c: &mut Control) -> Option<u64> { |
| 1176 | 1189 | None |
| 1177 | 1190 | } |
| 1178 | 1191 | |
| 1179 | | −/// Jump the virtual clock to the next deadline and wake those actors, in |
| 1180 | | −/// pid order. |
| 1192 | +/// In real time, count the time elapsed since the clock was last |
| 1193 | +/// counted, in whole milliseconds (the remainder is counted next time). |
| 1194 | +fn sync_clock(sched: &Shared, c: &mut Control) { |
| 1195 | + let Some(at) = c.synced else { |
| 1196 | + return; |
| 1197 | + }; |
| 1198 | + let ms = at.elapsed().as_millis() as u64; |
| 1199 | + if ms > 0 { |
| 1200 | + sched.clock.fetch_add(ms, Ordering::AcqRel); |
| 1201 | + c.synced = Some(at + std::time::Duration::from_millis(ms)); |
| 1202 | + } |
| 1203 | +} |
| 1204 | + |
| 1205 | +/// In real time, wake the actors whose deadline the clock has reached: |
| 1206 | +/// between slices, so they wake even if no thread is ever idle. |
| 1207 | +fn fire_due(sched: &Shared, c: &mut Control) { |
| 1208 | + if c.synced.is_none() { |
| 1209 | + return; |
| 1210 | + } |
| 1211 | + sync_clock(sched, c); |
| 1212 | + while next_deadline(sched, c).is_some_and(|d| d <= sched.clock()) { |
| 1213 | + advance_clock(sched, c); |
| 1214 | + } |
| 1215 | +} |
| 1216 | + |
| 1217 | +/// Jump the clock to the next deadline and wake those actors, in pid |
| 1218 | +/// order. |
| 1181 | 1219 | fn advance_clock(sched: &Shared, c: &mut Control) -> bool { |
| 1182 | 1220 | let Some(next) = next_deadline(sched, c) else { |
| 1183 | 1221 | return false; |