feat(host): expose memcard controls and qualify process persistence
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
This commit is contained in:
@@ -71,12 +71,14 @@ struct Request {
|
||||
job: AsyncWorkJobId,
|
||||
released: bool,
|
||||
saves_payload: bool,
|
||||
worker_returned: bool,
|
||||
}
|
||||
/// Main-lane control; the Card moves into the single worker while its source is protected.
|
||||
#[derive(Default)]
|
||||
pub struct Session {
|
||||
card_id: Option<[u8; 16]>,
|
||||
uncertain_target: Option<Map>,
|
||||
known_card_exclusions: Option<([u8; 16], [bool; BLOCK_COUNT])>,
|
||||
health: (bool, bool, bool),
|
||||
context: u64,
|
||||
epoch: u64,
|
||||
@@ -100,6 +102,82 @@ pub struct Session {
|
||||
deferred: Option<(u64, usize, u64, Instant)>,
|
||||
}
|
||||
impl Session {
|
||||
/// IO ownership, independent of a completed result retained for guest release.
|
||||
pub fn io_active(&self) -> bool {
|
||||
self.request.as_ref().is_some_and(|r| !r.worker_returned)
|
||||
}
|
||||
pub fn has_deferred_write(&self) -> bool {
|
||||
self.deferred.is_some()
|
||||
}
|
||||
pub fn cancel_pending(&mut self) {
|
||||
if let Some(r) = &self.request {
|
||||
r.control.cancel();
|
||||
}
|
||||
if let Some((id, local, ..)) = self.deferred.take() {
|
||||
self.slots[local].protected = false;
|
||||
self.memory_result = Some(RequestResult {
|
||||
id,
|
||||
state: RequestState::Failure,
|
||||
reason: RequestReason::Storage(Error::Canceled),
|
||||
});
|
||||
}
|
||||
}
|
||||
pub fn diagnostics(&self) -> super::diagnostics::SessionDiagnostics {
|
||||
use super::diagnostics::{RequestDiagnostics, SessionDiagnostics};
|
||||
let request = if let Some((id, local, ..)) = self.deferred {
|
||||
Some(RequestDiagnostics {
|
||||
result: RequestResult {
|
||||
id,
|
||||
state: RequestState::Pending,
|
||||
reason: RequestReason::None,
|
||||
},
|
||||
storage_error: None,
|
||||
slot: Some(local),
|
||||
operation: "commit",
|
||||
retained: true,
|
||||
io_active: false,
|
||||
deferred: true,
|
||||
})
|
||||
} else if let Some(r) = &self.request {
|
||||
Some(RequestDiagnostics {
|
||||
result: r.result,
|
||||
storage_error: r.worker_error,
|
||||
slot: r.saves_payload.then_some(r.local),
|
||||
operation: if r.saves_payload { "commit" } else { "presentation" },
|
||||
retained: !r.released,
|
||||
io_active: self.io_active(),
|
||||
deferred: false,
|
||||
})
|
||||
} else {
|
||||
self.memory_result.map(|result| RequestDiagnostics {
|
||||
result,
|
||||
storage_error: None,
|
||||
slot: None,
|
||||
operation: "commit",
|
||||
retained: true,
|
||||
io_active: false,
|
||||
deferred: false,
|
||||
})
|
||||
};
|
||||
SessionDiagnostics {
|
||||
app_id: self.mode.map(|_| self.app_id),
|
||||
card_id: self.card_id,
|
||||
epoch: self.epoch,
|
||||
mode: self.mode,
|
||||
health: self.health(),
|
||||
dirty_slots: self
|
||||
.slots
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, s)| s.data.read().ok().filter(|s| s.dirty).map(|_| i))
|
||||
.collect(),
|
||||
request,
|
||||
io_active: self.io_active(),
|
||||
removal_pending: self.removal_pending,
|
||||
reconciliation_required: self.reconcile_blocked,
|
||||
restart_required: self.faulted,
|
||||
}
|
||||
}
|
||||
pub fn context(&self) -> u64 {
|
||||
self.context
|
||||
}
|
||||
@@ -531,6 +609,7 @@ impl Session {
|
||||
job,
|
||||
released: !saves_payload,
|
||||
saves_payload,
|
||||
worker_returned: false,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
@@ -674,6 +753,7 @@ impl Session {
|
||||
job,
|
||||
released: false,
|
||||
saves_payload: true,
|
||||
worker_returned: false,
|
||||
});
|
||||
Ok(id)
|
||||
}
|
||||
@@ -692,6 +772,7 @@ impl Session {
|
||||
let Some(r) = &mut self.request else { return };
|
||||
match r.receiver.try_recv() {
|
||||
Ok(completion) => {
|
||||
r.worker_returned = true;
|
||||
r.worker_error = completion.result.error;
|
||||
let current =
|
||||
self.mode.is_some() && r.session == self.context && r.epoch == self.epoch;
|
||||
@@ -819,7 +900,7 @@ impl Session {
|
||||
return Err(Error::Busy);
|
||||
}
|
||||
r.released = true;
|
||||
if self.card.is_some() {
|
||||
if r.worker_returned {
|
||||
self.request = None;
|
||||
}
|
||||
Ok(())
|
||||
@@ -833,7 +914,7 @@ impl Session {
|
||||
r.control.cancel();
|
||||
r.released = true;
|
||||
}
|
||||
if self.card.is_some() {
|
||||
if !self.io_active() {
|
||||
self.request = None;
|
||||
}
|
||||
self.removal_pending = false;
|
||||
@@ -869,7 +950,23 @@ impl Session {
|
||||
load_existing: bool,
|
||||
control: &PublicationControl,
|
||||
) -> Result<()> {
|
||||
if self.has_in_flight() || self.boot_active || self.mode.is_none() {
|
||||
self.replace_storage_with(move || Ok(storage), load_existing, false, control)
|
||||
}
|
||||
pub(super) fn reconcile_storage_with(
|
||||
&mut self,
|
||||
open: impl FnOnce() -> Result<Box<dyn prometeu_hal::memcard::MemcardStorage>>,
|
||||
control: &PublicationControl,
|
||||
) -> Result<()> {
|
||||
self.replace_storage_with(open, false, true, control)
|
||||
}
|
||||
fn replace_storage_with(
|
||||
&mut self,
|
||||
open: impl FnOnce() -> Result<Box<dyn prometeu_hal::memcard::MemcardStorage>>,
|
||||
load_existing: bool,
|
||||
same_only: bool,
|
||||
control: &PublicationControl,
|
||||
) -> Result<()> {
|
||||
if self.io_active() || self.deferred.is_some() || self.boot_active || self.mode.is_none() {
|
||||
return Err(Error::Busy);
|
||||
}
|
||||
if self.icon.is_empty()
|
||||
@@ -882,11 +979,18 @@ impl Session {
|
||||
{
|
||||
self.uncertain_target = card.attempted_publication.clone();
|
||||
}
|
||||
let known = self.card.as_ref().map(|c| (c.current().card_id, c.known_exclusions()));
|
||||
if let Some(card) = &self.card {
|
||||
self.known_card_exclusions = Some((card.current().card_id, card.known_exclusions()));
|
||||
}
|
||||
let known = self.known_card_exclusions;
|
||||
self.card.take(); // Release the old scratch before mounting another image.
|
||||
self.scratch = None;
|
||||
self.reconcile_blocked = true;
|
||||
self.card = Some(Card::mount(storage)?);
|
||||
let candidate = Card::mount(open()?)?;
|
||||
if same_only && self.card_id != Some(candidate.current().card_id) {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
self.card = Some(candidate);
|
||||
let card = self.card.as_mut().unwrap();
|
||||
let same = self.card_id == Some(card.current().card_id);
|
||||
if let Some((id, known)) = known
|
||||
@@ -973,14 +1077,15 @@ impl Session {
|
||||
Ok(())
|
||||
}
|
||||
pub fn detach(&mut self) -> Result<()> {
|
||||
if self.deferred.is_some()
|
||||
|| self
|
||||
.request
|
||||
.as_ref()
|
||||
.is_some_and(|r| r.result.state == RequestState::Pending || self.card.is_none())
|
||||
{
|
||||
if self.deferred.is_some() || self.io_active() {
|
||||
return Err(Error::Busy);
|
||||
}
|
||||
if self.card.is_none() && self.scratch.is_none() {
|
||||
let mut scratch = Vec::new();
|
||||
scratch.try_reserve_exact(SLOT_BYTES).map_err(|_| Error::OutOfMemory)?;
|
||||
scratch.resize(SLOT_BYTES, 0);
|
||||
self.scratch = Some(scratch);
|
||||
}
|
||||
self.mode = Some(Mode::MemoryOnly);
|
||||
self.removal_pending = false;
|
||||
self.reconcile_blocked = false;
|
||||
|
||||
Reference in New Issue
Block a user