Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust/crates/psychoscript/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1894,7 +1894,7 @@ mod tests {
&vm,
&crate::snapshot::Capture {
code: CODE,
active_ms: 0,
active: std::time::Duration::ZERO,
carried: Default::default(),
},
);
Expand Down
27 changes: 11 additions & 16 deletions rust/crates/psychoscript/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ use std::time::Duration;
use crate::thread_clock::ThreadClock;
use crate::time::Instant;

/// `Limits::slice_ms`: the stretch in progress.
/// `Limits::slice_ms`: the stretch in progress, or `wall: None` while the run waits on the host.
///
/// On the wall clock, `wall` is where it began, moved on by the host calls since, so its elapsed time is the
/// stretch's own. Under `Clock::ThreadCpu`, `wall` is where it began until `from` is read, once the stretch has run
/// `SLICE_SLACK` of the slice: the run's CPU time without host calls and with them (`CpuMeter::read`). The stretch is
/// measured from there, against the slice and, with host calls, `WAIT_BACKSTOP` times it; `wall` is then the
/// earliest it could be over either, as a thread uses no more CPU time than passes, and the clock isn't read before.
/// On the wall clock, `wall` is `Some(at)` where the stretch began, moved on by the host calls since, so its elapsed
/// time is the stretch's own. Under `Clock::ThreadCpu`, `wall` is `Some(at)` where it began until `from` is read, once
/// the stretch has run `SLICE_SLACK` of the slice: the run's CPU time without host calls and with them
/// (`CpuMeter::read`). The stretch is measured from there, against the slice and, with host calls, `WAIT_BACKSTOP`
/// times it; `wall` is then `Some(at)` for the earliest it could be over either, as a thread uses no more CPU time
/// than passes, and the clock isn't read before. While waiting on the host (`wall: None`), no stretch is in progress.
pub(crate) struct Slice {
pub(crate) from: Option<(Duration, Duration)>,
pub(crate) wall: Instant,
pub(crate) wall: Option<Instant>,
}

/// What a run spent besides its active time, saved with its state so that a resume goes on from it rather than from
Expand Down Expand Up @@ -151,22 +152,16 @@ impl CpuMeter {
mark.wall = Instant::now();
}

/// The CPU time the run used, without host calls.
pub(crate) fn used(&mut self) -> Duration {
self.fold();
self.used
}

/// The CPU time the run used, without host calls and with them.
pub(crate) fn read(&mut self) -> (Duration, Duration) {
self.fold();
(self.used, self.total)
}

/// The CPU time the run used with host calls, and the wall-clock time it spent waiting on them.
pub(crate) fn spent(&mut self) -> (Duration, Duration) {
/// The CPU time the run used without host calls, with them, and the wall-clock time it spent waiting on them.
pub(crate) fn totals(&mut self) -> (Duration, Duration, Duration) {
self.fold();
(self.total, self.waited)
(self.used, self.total, self.waited)
}

/// Wall-clock time spent in a wait on the calling thread, which used no CPU time meanwhile.
Expand Down
11 changes: 10 additions & 1 deletion rust/crates/psychoscript/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,15 @@ pub(crate) enum Queued {
/// Starts the calls made since the last handover, in the order they were made: the reference hands its queued calls
/// to the driver at every stop.
pub fn launch(vm: &mut Vm) {
if vm.host.queued.is_empty() {
return;
}
let span = vm.host_span();
launch_inner(vm);
vm.end_host_span(span);
}

fn launch_inner(vm: &mut Vm) {
for queued in std::mem::take(&mut vm.host.queued) {
match queued {
Queued::Call(p, f, r, holds) => spawn_call(vm, p, &f, *r, holds),
Expand Down Expand Up @@ -1025,7 +1034,7 @@ fn call_host(
// The run waits for the work: the reference's driver takes over, starting the calls queued so far.
Returned::Block(work) => {
vm.host.next_epoch();
launch(vm);
launch_inner(vm);
Returned::Now(block(vm, work))
}
returned => returned,
Expand Down
20 changes: 11 additions & 9 deletions rust/crates/psychoscript/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl Program {
}
let capture = snapshot::Capture {
code: &r.code,
active_ms: 0,
active: Duration::ZERO,
carried: Default::default(),
};
snapshot::capture(&r.vm, &capture).map_err(|refusal| refused(&r.vm, refusal))
Expand Down Expand Up @@ -510,7 +510,10 @@ impl Program {
run::call_main(&mut r.vm);
}
}
for (p, s) in r.vm.host.tasks.poll_ready() {
let span = r.vm.host_span();
let ready = r.vm.host.tasks.poll_ready();
r.vm.end_host_span(span);
for (p, s) in ready {
r.vm.host.settled(p);
if let crate::host::Settlement::Interrupted = s {
crate::host::interrupt(&mut r.vm);
Expand Down Expand Up @@ -695,14 +698,13 @@ impl Program {
r.skip -= 1;
return None;
}
let used = r.vm.time_used();
let active_ms = used.max(r.active_before).as_millis() as u64;
let carried = r.vm.carried();
let (used, carried) = r.vm.budget_used();
let active = used.max(r.active_before);
let captured = snapshot::capture(
&r.vm,
&snapshot::Capture {
code: &r.code,
active_ms,
active,
carried,
},
);
Expand Down Expand Up @@ -859,7 +861,7 @@ impl Program {
};
let c = snapshot::Capture {
code: &r.code,
active_ms: 0,
active: Duration::ZERO,
carried: Default::default(),
};
snapshot::capture(&r.vm, &c).unwrap_or_else(|_| panic!("the state can't be saved"))
Expand All @@ -868,11 +870,11 @@ impl Program {

fn restore(rt: &Runtime, state: &Snapshot) -> Result<Box<Running>, snapshot::SnapshotError> {
let restored = snapshot::header(state)?;
let (active_ms, carried) = (restored.active_ms, restored.carried);
let (active, carried) = (restored.active, restored.carried);
let mut started = run::start_for_resume(rt, restored)?;
snapshot::restore(&mut started.running.vm, state, &started.bytecode)?;
let mut r = started.running;
r.active_before = Duration::from_millis(active_ms);
r.active_before = active;
r.carried = carried;
// The program's body already started: what runs next comes from the saved frames and jobs.
r.mark_resumed();
Expand Down
22 changes: 18 additions & 4 deletions rust/crates/psychoscript/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ fn cross(c: &crate::cross::Cross) -> J {
/// What a capture needs to know beyond the VM.
pub struct Capture<'c> {
pub code: &'c str,
pub active_ms: u64,
pub active: std::time::Duration,
pub carried: crate::cpu::Carried,
}

Expand Down Expand Up @@ -589,7 +589,10 @@ pub fn capture(vm: &Vm, c: &Capture) -> Result<Snapshot, Refusal> {
"timeoutMs": limits.timeout_ms,
"maxOutputBytes": limits.max_output_bytes,
},
"budget": { "activeMs": c.active_ms },
"budget": {
"activeMs": c.active.as_millis() as u64,
"activeUs": c.active.as_micros() as u64,
},
"base": base.json(),
"state": state_hash(&state),
});
Expand Down Expand Up @@ -2524,7 +2527,7 @@ pub struct Restored {
/// The shape of the host globals it was saved with (`hosts_id`).
pub hosts: String,
pub limits: Limits,
pub active_ms: u64,
pub active: std::time::Duration,
pub carried: crate::cpu::Carried,
}

Expand Down Expand Up @@ -2588,7 +2591,13 @@ pub fn header(snapshot: &Snapshot) -> R<Restored> {
// Not saved: it bounds the source a run starts from, and a state brings its own.
max_source_bytes: None,
},
active_ms: field(budget, "activeMs")?.as_u64().unwrap_or(0),
active: {
let active_ms = field(budget, "activeMs")?.as_u64().unwrap_or(0);
match budget.get("activeUs").and_then(J::as_u64) {
Some(us) => std::time::Duration::from_micros(us),
None => std::time::Duration::from_millis(active_ms).max(us("stretchUs")),
}
},
carried: crate::cpu::Carried {
stretch: us("stretchUs"),
stretch_total: us("stretchTotalUs"),
Expand Down Expand Up @@ -3015,9 +3024,14 @@ fn restore_host(vm: &mut Vm, dec: &Dec, j: &J, base: Base) -> R<()> {

/// The calls a resumed run restarts, in the order they were in flight, so their futures start on the run's executor.
pub fn restart_calls(vm: &mut Vm) {
if vm.host.restarts.is_empty() {
return;
}
let span = vm.host_span();
for r in std::mem::take(&mut vm.host.restarts) {
host::restart(vm, r);
}
vm.end_host_span(span);
}

#[cfg(test)]
Expand Down
Loading