Skip to content
Open
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 core/continuum-kernel/src/resources/governor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl ResourceGovernor {
}

/// The live lease by id, from the ledger the board is built from (card 26df).
pub fn lease(&self, lease_id: &str) -> Option<&ResourceLease> {
pub fn lease(&self, lease_id: &str) -> Option<ResourceLease> {
self.ledger.lease(lease_id)
}

Expand Down
7 changes: 6 additions & 1 deletion core/continuum-kernel/src/resources/lease.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,14 @@ pub struct ResourceLease {
pub reclaim_policy: ReclaimPolicy,
}

/// The one expiry rule: a lease is past its deadline from `expires_at_ms` on.
pub(crate) fn expired(expires_at_ms: u64, now_ms: u64) -> bool {
now_ms >= expires_at_ms
}

impl ResourceLease {
pub fn is_expired(&self, now_ms: u64) -> bool {
now_ms >= self.expires_at_ms
expired(self.expires_at_ms, now_ms)
}

/// Can pressure take this back right now? Expired leases always yes (the
Expand Down
107 changes: 62 additions & 45 deletions core/continuum-kernel/src/resources/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,36 @@ impl LeaseView {
impl Held {
/// This lease's bytes still owed on top of physical use, given the oldest of the accepted observations that
/// must show it (its kind's, and its backing pool's on unified memory).
fn pending(&self, accepted: Option<super::observed::Observed>) -> u64 {
fn pending(&self, accepted: Option<super::observed::Observed>, bytes: u64) -> u64 {
match (self.landed_from, accepted) {
(Some(from), Some(sample)) if sample >= from => 0,
_ => self.lease.bytes,
_ => bytes,
}
}
}

impl ResourceLeaseLedger {
/// What the core holds for `held` now: its one record of the lease's size. A lease the core holds
/// nothing for holds nothing (the core is the record, so this is its answer, not a substitute).
fn held_bytes(&self, held: &Held) -> u64 {
match self.core.holding(held.ask(), held.meta.kind) {
Some(Bytes(bytes)) => bytes,
None => 0,
}
}

/// `held` as a caller sees it: its terms at the size the core holds now (zero when the core holds
/// nothing for it, so a lease whose claim is gone still expires and is reclaimed; Fable on #6308).
fn snapshot(&self, held: &Held) -> ResourceLease {
held.meta.with_bytes(self.held_bytes(held))
}

/// Every held lease, as callers see them, in lease-id order: the same set `lease_count` counts.
fn snapshots(&self) -> impl Iterator<Item = ResourceLease> + '_ {
self.leases.values().map(|held| self.snapshot(held))
}
}

impl ResourceLeaseLedger {
pub fn new() -> Self {
Self::default()
Expand All @@ -241,9 +263,8 @@ impl ResourceLeaseLedger {
pub fn granted(&self, kind: ResourceKind) -> u64 {
self.leases
.values()
.map(|held| &held.lease)
.filter(|l| l.kind == kind)
.map(|l| l.bytes)
.filter(|held| held.meta.kind == kind)
.map(|held| self.held_bytes(held))
.fold(0u64, |acc, b| acc.saturating_add(b))
}

Expand Down Expand Up @@ -350,9 +371,8 @@ impl ResourceLeaseLedger {
pub fn granted_to(&self, consumer_id: &str, kind: ResourceKind) -> u64 {
self.leases
.values()
.map(|held| &held.lease)
.filter(|l| l.kind == kind && l.consumer_id == consumer_id)
.map(|l| l.bytes)
.filter(|held| held.meta.kind == kind && held.meta.consumer_id == consumer_id)
.map(|held| self.held_bytes(held))
.fold(0u64, |acc, b| acc.saturating_add(b))
}

Expand Down Expand Up @@ -525,7 +545,7 @@ impl ResourceLeaseLedger {
.keys()
.filter(|(k, _)| *k == kind)
.map(|(_, c)| c.as_str())
.chain(self.leases.values().map(|held| &held.lease).filter(|l| l.kind == kind).map(|l| l.consumer_id.as_str()))
.chain(self.leases.values().map(|held| &held.meta).filter(|l| l.kind == kind).map(|l| l.consumer_id.as_str()))
.chain(self.measured.iter().filter(|(_, fs)| fs.iter().any(|f| f.kind == kind)).map(|(c, _)| c.as_str()))
.filter(|c| *c != consumer_id)
.collect();
Expand Down Expand Up @@ -617,7 +637,7 @@ impl ResourceLeaseLedger {
.ok_or_else(|| LeaseError::MissingLease {
lease_id: lease_id.to_string(),
})?;
let lease = &mut held.lease;
let lease = &mut held.meta;
if lease.is_expired(now_ms) {
return Err(LeaseError::ExpiredLease {
lease_id: lease_id.to_string(),
Expand All @@ -634,15 +654,17 @@ impl ResourceLeaseLedger {
pub fn release(&mut self, lease_id: &str) -> Result<ResourceLease, LeaseError> {
let held = self.leases.remove(lease_id).ok_or_else(|| LeaseError::MissingLease { lease_id: lease_id.to_string() })?;
let ask = held.ask();
if let Some(view) = held.view { view.bytes.store(0, Ordering::Release); }
// what it held, read from the core before the release frees it
let released = held.meta.with_bytes(self.held_bytes(&held));
if let Some(view) = &held.view { view.bytes.store(0, Ordering::Release); }
self.core.apply(&Act::Release(ask));
if self.core.status(ask).is_some() {
// still in the table: the core refused the release (its claim already gone); it leaves anyway
continuum_base::probe!(class = "resources.ledger.release_unknown", lease = %lease_id, "a released lease had no claim left in the core");
self.core.drop_ask(ask);
}
self.tell();
Ok(held.lease)
Ok(released)
}

/// Reduce a lease's footprint by `freed_bytes` without ending it — the
Expand All @@ -654,27 +676,26 @@ impl ResourceLeaseLedger {
/// lease is fail-loud (a stale id is a real bug). Returns the bytes still
/// held by the lease (0 if it was removed).
pub fn shrink(&mut self, lease_id: &str, freed_bytes: u64) -> Result<u64, LeaseError> {
let held = self
.leases
.get_mut(lease_id)
.ok_or_else(|| LeaseError::MissingLease {
lease_id: lease_id.to_string(),
})?;
let lease = &mut held.lease;
let held = self.leases.get(lease_id).ok_or_else(|| LeaseError::MissingLease { lease_id: lease_id.to_string() })?;
let (kind, ask, had) = (held.meta.kind, held.ask(), self.held_bytes(held));
// never more than the lease held: a holder that over-reports must not tell the core it freed more
// than the lease was (Fable on #6287)
let freed_bytes = freed_bytes.min(lease.bytes);
lease.bytes -= freed_bytes;
if let Some(view) = &held.view { view.bytes.store(lease.bytes, Ordering::Release); }
let (remaining, kind, ask) = (lease.bytes, lease.kind, held.ask());
if remaining == 0 {
self.leases.remove(lease_id);
}
let freed_bytes = freed_bytes.min(had);
if freed_bytes > 0 {
self.core.apply(&Act::Shrunk { ask, place: kind, by: Bytes(freed_bytes) });
}
// the core moved the bytes; what remains is its record, never a subtraction kept here
let remaining = match self.core.holding(ask, kind) {
Some(Bytes(left)) => left,
None => 0,
};
if let Some(view) = self.leases.get(lease_id).and_then(|held| held.view.as_ref()) {
view.bytes.store(remaining, Ordering::Release);
}
if remaining == 0 {
self.core.drop_ask(ask); // shrunk to nothing: its claim is gone, and so is its entry
// shrunk to nothing: its claim is gone, and so are its lease and its entry
self.leases.remove(lease_id);
self.core.drop_ask(ask);
}
self.tell();
Ok(remaining)
Expand All @@ -684,12 +705,7 @@ impl ResourceLeaseLedger {
/// uses this list to drive reclaim callbacks; the bytes free only when the
/// holder confirms via `release`. Returned in stable id order.
pub fn expire(&self, now_ms: u64) -> Vec<ResourceLease> {
self.leases
.values()
.map(|held| &held.lease)
.filter(|l| l.is_expired(now_ms))
.cloned()
.collect()
self.snapshots().filter(|l| l.is_expired(now_ms)).collect()
}

/// Pick leases to reclaim to free at least `target_bytes` of `kind`,
Expand Down Expand Up @@ -727,10 +743,8 @@ impl ResourceLeaseLedger {
return Some(Vec::new());
}
let ctx = ArbiterContext { now_ms, pressure };
let mut candidates: Vec<&ResourceLease> = self
.leases
.values()
.map(|held| &held.lease)
let mut candidates: Vec<ResourceLease> = self
.snapshots()
.filter(|l| l.kind == kind && l.reclaim_rank(now_ms).is_some())
.filter(|l| {
// Dwell protection — expired bytes are always fair game; a
Expand All @@ -750,7 +764,7 @@ impl ResourceLeaseLedger {
let mut remaining_per_consumer: BTreeMap<&str, u64> = BTreeMap::new();
let mut freed = 0u64;
let mut chosen = Vec::new();
for lease in candidates {
for lease in &candidates {
if freed >= target_bytes {
break;
}
Expand All @@ -774,16 +788,19 @@ impl ResourceLeaseLedger {
}

pub(super) fn lease_view(&mut self, id: &str) -> Option<LeaseView> {
let bytes = self.held_bytes(self.leases.get(id)?);
let held = self.leases.get_mut(id)?;
let coverage = held.view.get_or_insert_with(|| Arc::new(LeaseCoverage {
bytes: AtomicU64::new(held.lease.bytes),
expires_at_ms: AtomicU64::new(held.lease.expires_at_ms),
bytes: AtomicU64::new(bytes),
expires_at_ms: AtomicU64::new(held.meta.expires_at_ms),
}));
Some(LeaseView { coverage: Arc::downgrade(coverage), kind: held.lease.kind })
Some(LeaseView { coverage: Arc::downgrade(coverage), kind: held.meta.kind })
}

pub fn lease(&self, lease_id: &str) -> Option<&ResourceLease> {
self.leases.get(lease_id).map(|held| &held.lease)
/// `lease_id` as it stands: its terms at the size the core holds now. A snapshot, never a reference
/// into a second record.
pub fn lease(&self, lease_id: &str) -> Option<ResourceLease> {
self.leases.get(lease_id).map(|held| self.snapshot(held))
}

/// The published board. Omits a kind only when it is uncapacitied AND
Expand All @@ -795,7 +812,7 @@ impl ResourceLeaseLedger {
let mut kinds = Vec::new();
for kind in ResourceKind::ALL {
let capacity_bytes = self.capacity(kind);
let lease_count = self.leases.values().map(|held| &held.lease).filter(|l| l.kind == kind).count() as u32;
let lease_count = self.leases.values().filter(|held| held.meta.kind == kind).count() as u32;
let measured_bytes = self.measured(kind);
let physical_used_bytes = self.physical_used(kind);
if capacity_bytes == 0
Expand All @@ -821,7 +838,7 @@ impl ResourceLeaseLedger {
lease_count,
});
}
let mut leases: Vec<ResourceLease> = self.leases.values().map(|held| held.lease.clone()).collect();
let mut leases: Vec<ResourceLease> = self.snapshots().collect();
leases.sort_by(|a, b| a.lease_id.cmp(&b.lease_id));

// Flatten every consumer's latest footprint into stable-ordered
Expand Down
15 changes: 9 additions & 6 deletions core/continuum-kernel/src/resources/ledger/asks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ impl ResourceLeaseLedger {
pub fn carve(&mut self, reserve: &str, req: &LeaseRequest, lease_id: &str, now_ms: u64) -> Result<ResourceLease, CarveError> {
let (kind, bytes, from) = match self.leases.get(reserve) {
// a lapsed reserve stays charged for its fenced release; nothing is carved from it into a fresh term
Some(held) if held.lease.is_expired(now_ms) => return Err(LeaseError::ExpiredLease { lease_id: reserve.to_owned() }.into()),
Some(held) => (held.lease.kind, held.lease.bytes, held.ask()),
Some(held) if held.meta.is_expired(now_ms) => return Err(LeaseError::ExpiredLease { lease_id: reserve.to_owned() }.into()),
Some(held) => (held.meta.kind, self.held_bytes(held), held.ask()),
None => return Err(LeaseError::MissingLease { lease_id: reserve.to_owned() }.into()),
};
if kind != req.kind || bytes < req.bytes {
Expand All @@ -139,14 +139,17 @@ impl ResourceLeaseLedger {
let into = self.ask_id(&req.consumer_id);
let split = SplitAsk { from, moved: Need { place: kind, amount: Bytes(req.bytes) }, into, generation: self.generation() };
self.core.split(split).map_err(CarveError::Place)?;
let left = bytes - req.bytes;
// the core moved the bytes; what the reserve keeps is its record, never a subtraction kept here
let left = match self.core.holding(from, kind) {
Some(Bytes(left)) => left,
None => 0,
};
if left == 0 {
if let Some(held) = self.leases.remove(reserve) {
if let Some(view) = held.view { view.bytes.store(0, std::sync::atomic::Ordering::Release); }
}
} else if let Some(held) = self.leases.get_mut(reserve) {
held.lease.bytes = left;
if let Some(view) = &held.view { view.bytes.store(left, std::sync::atomic::Ordering::Release); }
} else if let Some(view) = self.leases.get(reserve).and_then(|held| held.view.as_ref()) {
view.bytes.store(left, std::sync::atomic::Ordering::Release);
}
let lease = ResourceLease {
lease_id: lease_id.to_owned(),
Expand Down
46 changes: 43 additions & 3 deletions core/continuum-kernel/src/resources/ledger/held.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use super::LeaseCoverage;
/// include the bytes). A holder that never declares is over-counted (refuses), never under-counted.
#[derive(Debug, Clone)]
pub(super) struct Held {
pub(super) lease: ResourceLease,
/// Everything of the lease but its size: the size is the core's (`PlacerCore::holding`), read on demand,
/// never kept here as a second record (step 1, ONE-FRAME-ONE-MEMORY.md).
pub(super) meta: LeaseMeta,
pub(super) view: Option<std::sync::Arc<LeaseCoverage>>,
/// When its holder declared it landed: only a reading observed at or after this can show it. None while
/// undeclared.
Expand All @@ -27,10 +29,48 @@ pub(super) struct Held {
ask: AskId,
}

/// A lease's identity, owner and terms: what the core does not hold. Its bytes are never here.
#[derive(Debug, Clone)]
pub(super) struct LeaseMeta {
pub(super) lease_id: String,
pub(super) consumer_id: String,
pub(super) kind: super::super::lease::ResourceKind,
pub(super) acquired_at_ms: u64,
pub(super) expires_at_ms: u64,
pub(super) reclaim_policy: super::super::lease::ReclaimPolicy,
}

impl LeaseMeta {
pub(super) fn is_expired(&self, now_ms: u64) -> bool {
super::super::lease::expired(self.expires_at_ms, now_ms)
}

/// The lease as a caller sees it, at the size the core holds for it now.
pub(super) fn with_bytes(&self, bytes: u64) -> ResourceLease {
ResourceLease {
lease_id: self.lease_id.clone(),
consumer_id: self.consumer_id.clone(),
kind: self.kind,
bytes,
acquired_at_ms: self.acquired_at_ms,
expires_at_ms: self.expires_at_ms,
reclaim_policy: self.reclaim_policy,
}
}
}

impl Held {
/// A lease just granted under core entry `ask`: no view yet, not landed.
/// A lease just granted under core entry `ask`: no view yet, not landed. Its bytes are the core's.
pub(super) fn new(lease: &ResourceLease, ask: AskId) -> Self {
Self { lease: lease.clone(), view: None, landed_from: None, ask }
let meta = LeaseMeta {
lease_id: lease.lease_id.clone(),
consumer_id: lease.consumer_id.clone(),
kind: lease.kind,
acquired_at_ms: lease.acquired_at_ms,
expires_at_ms: lease.expires_at_ms,
reclaim_policy: lease.reclaim_policy,
};
Self { meta, view: None, landed_from: None, ask }
}

pub(super) fn ask(&self) -> AskId {
Expand Down
6 changes: 3 additions & 3 deletions core/continuum-kernel/src/resources/ledger/shrinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl ResourceLeaseLedger {
/// Mark `lease_id` elastic: the law may ask it to give back down to `elastic.floor`.
pub fn make_elastic(&mut self, lease_id: &str, elastic: Elastic) -> Result<(), LeaseError> {
let held = self.leases.get(lease_id).ok_or_else(|| LeaseError::MissingLease { lease_id: lease_id.to_owned() })?;
let (ask, kind) = (held.ask(), held.lease.kind);
let (ask, kind) = (held.ask(), held.meta.kind);
self.core.apply(&Act::Elastic { ask, place: kind, floor: Bytes(elastic.floor), priority: elastic.priority });
self.tell();
Ok(())
Expand All @@ -42,7 +42,7 @@ impl ResourceLeaseLedger {
/// next settle shares the gap among the elastic leases left (Fable on #6287 at d8fbe6bc5c).
pub fn refused(&mut self, lease_id: &str) -> Result<(), LeaseError> {
let held = self.leases.get(lease_id).ok_or_else(|| LeaseError::MissingLease { lease_id: lease_id.to_owned() })?;
let (ask, kind, bytes) = (held.ask(), held.lease.kind, held.lease.bytes);
let (ask, kind, bytes) = (held.ask(), held.meta.kind, self.held_bytes(held));
self.core.apply(&Act::Elastic { ask, place: kind, floor: Bytes(bytes), priority: u8::MAX });
self.tell();
Ok(())
Expand All @@ -58,7 +58,7 @@ impl ResourceLeaseLedger {
for (seq, status) in entries {
let Status::Shrink { place, to } = status else { continue };
if let Some((lease_id, held)) = self.leases.by_ask(continuum_sage::place::AskId { owner, seq }) {
out.push(ShrinkDue { ask: held.ask(), lease_id: lease_id.clone(), kind: place, held: held.lease.bytes, to: to.0 });
out.push(ShrinkDue { ask: held.ask(), lease_id: lease_id.clone(), kind: place, held: self.held_bytes(held), to: to.0 });
}
}
}
Expand Down
Loading
Loading