feat(runtime): add optional pause and resume lifecycle hooks
Intrepid/Prometeu/Runtime/pipeline/pr-master Build started...
Intrepid/Prometeu/Runtime/pipeline/pr-master Build started...
This commit is contained in:
@@ -101,6 +101,7 @@ fn serialized_single_function_module_with_consts(
|
||||
TestProgram {
|
||||
const_pool,
|
||||
functions: vec![FunctionDecl {
|
||||
lifecycle_role: Default::default(),
|
||||
code_offset: 0,
|
||||
code_len: code.len() as u32,
|
||||
|
||||
|
||||
@@ -105,7 +105,8 @@ impl VirtualMachineRuntime {
|
||||
match step_result {
|
||||
Ok(())
|
||||
| Err(
|
||||
LogicalFrameEndingReason::FrameSync
|
||||
LogicalFrameEndingReason::LifecycleComplete
|
||||
| LogicalFrameEndingReason::FrameSync
|
||||
| LogicalFrameEndingReason::BudgetExhausted
|
||||
| LogicalFrameEndingReason::Halted
|
||||
| LogicalFrameEndingReason::EndOfRom
|
||||
@@ -138,6 +139,55 @@ impl VirtualMachineRuntime {
|
||||
}
|
||||
}
|
||||
|
||||
/// Uses the session's real services, outside logical-frame accounting.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn tick_lifecycle(
|
||||
&mut self,
|
||||
log_service: &mut LogService,
|
||||
fs: &mut VirtualFS,
|
||||
fs_state: &mut FsState,
|
||||
memcard: &mut MemcardService,
|
||||
open_files: &mut HashMap<u32, String>,
|
||||
next_handle: &mut u32,
|
||||
vm: &mut VirtualMachine,
|
||||
platform: &mut dyn RuntimePlatform,
|
||||
single_step: bool,
|
||||
) -> LogicalFrameEndingReason {
|
||||
if self.paused && !single_step && !self.debug_step_request {
|
||||
return LogicalFrameEndingReason::Breakpoint;
|
||||
}
|
||||
let result = catch_unwind(AssertUnwindSafe(|| {
|
||||
let mut ctx = HostContext::new_platform(Some(platform));
|
||||
let mut host = VmRuntimeHost {
|
||||
runtime: self,
|
||||
log_service,
|
||||
fs,
|
||||
fs_state,
|
||||
memcard,
|
||||
open_files,
|
||||
next_handle,
|
||||
};
|
||||
if single_step {
|
||||
vm.step(&mut host, &mut ctx)
|
||||
.err()
|
||||
.unwrap_or(LogicalFrameEndingReason::BudgetExhausted)
|
||||
} else {
|
||||
vm.run_budget(VirtualMachine::LIFECYCLE_SLICE, &mut host, &mut ctx)
|
||||
.map(|report| report.reason)
|
||||
.unwrap_or_else(LogicalFrameEndingReason::Panic)
|
||||
}
|
||||
}));
|
||||
let reason = result.unwrap_or_else(|payload| {
|
||||
LogicalFrameEndingReason::Panic(Self::host_panic_payload_to_string(payload))
|
||||
});
|
||||
if reason == LogicalFrameEndingReason::Breakpoint || single_step || self.debug_step_request
|
||||
{
|
||||
self.paused = true;
|
||||
self.debug_step_request = false;
|
||||
}
|
||||
reason
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn tick(
|
||||
&mut self,
|
||||
|
||||
@@ -20,7 +20,27 @@ pub enum VmSessionError {
|
||||
ProcessIsNotVmBacked(ProcessId),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum LifecycleTransition {
|
||||
PauseToHub,
|
||||
PauseToShell(TaskId),
|
||||
Resume,
|
||||
}
|
||||
|
||||
impl LifecycleTransition {
|
||||
pub(crate) fn role(self) -> prometeu_bytecode::registry::LifecycleRole {
|
||||
use prometeu_bytecode::registry::LifecycleRole;
|
||||
match self {
|
||||
Self::Resume => LifecycleRole::BeforeResume,
|
||||
_ => LifecycleRole::BeforePause,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VmSession {
|
||||
pub(crate) lifecycle_transition: Option<LifecycleTransition>,
|
||||
pub(crate) lifecycle_started: bool,
|
||||
pub(crate) pause_hook_complete: bool,
|
||||
pub id: VmSessionId,
|
||||
pub task_id: TaskId,
|
||||
pub process_id: ProcessId,
|
||||
@@ -45,6 +65,9 @@ impl VmSession {
|
||||
logs_count: Arc<AtomicU32>,
|
||||
) -> Self {
|
||||
Self {
|
||||
lifecycle_transition: None,
|
||||
lifecycle_started: false,
|
||||
pause_hook_complete: false,
|
||||
id: VmSessionId(task.id),
|
||||
task_id: task.id,
|
||||
process_id: process.id,
|
||||
|
||||
Reference in New Issue
Block a user