pub struct InstanceState {
pub tenant_id: TenantId,
pub instance_id: InstanceId,
pub created_at: Instant,
pub deadline: Option<Instant>,
pub deadline_duration: Option<Duration>,
pub hard_deadline: Option<Instant>,
pub kernel_dispatches: AtomicU64,
pub gpu_bytes_allocated: AtomicU64,
/* private fields */
}Expand description
Side-channel data attached to each wasmtime Store.
Wasmtime gives every Store a T payload accessible from host functions —
we store identity, deadlines, and resource counters here so WASI-CUDA
host functions (S8) can access them via caller.data().
Fields§
§tenant_id: TenantIdOwning tenant.
instance_id: InstanceIdUnique instance identifier.
created_at: InstantWalltime the instance was created.
deadline: Option<Instant>Soft deadline (epoch-driven) for the current call.
None means “no host-imposed deadline”. This is re-armed at the start
of each TensorWasmExecutor::call_export
from InstanceState::deadline_duration, so back-to-back calls each
get a fresh wall-clock window instead of inheriting the elapsed time
from a previous call.
deadline_duration: Option<Duration>Per-call deadline as a Duration, retained so the executor can
re-arm the absolute InstanceState::deadline (and the wasmtime
epoch deadline) at the start of each call. None for instances
spawned without a deadline.
hard_deadline: Option<Instant>Absolute wall-clock instant past which the cooperative epoch callback must trap (terminate the guest) rather than yield.
This is the source of truth for the HARD deadline guarantee under
the cooperative-yield scheme (see
crate::executor::TensorWasmExecutor’s epoch callback). The
store’s epoch deadline is configured with
wasmtime::Store::epoch_deadline_callback in async-yield mode:
every time the epoch deadline trips, the callback compares
Instant::now() against this instant. If the instant has NOT
passed, the callback returns wasmtime::UpdateDeadline::Yield,
so the guest yields Pending to the async runtime (making a
compute-bound guest cancellable on future-drop) and the epoch
re-arms for another cooperative window. Once this instant HAS
passed, the callback returns an error, which wasmtime turns into
a trap — preserving the exact trap-on-deadline behaviour the
executor relied on under the old set_epoch_deadline-trap scheme.
Distinct from Self::deadline: deadline is the per-call soft
budget consulted by the scheduler/back-pressure surface and by the
timeout classification in call_export_with_args, whereas
hard_deadline is what the epoch callback enforces. The executor
keeps them aligned — for a call with a configured deadline both are
now + d; during instantiation hard_deadline is additionally
clamped by crate::executor::MAX_START_FN_DURATION so a runaway
start function still traps even on a deadline-less spawn.
None means “no hard cap” — the callback yields cooperatively
forever (used only if no cap of any kind applies).
kernel_dispatches: AtomicU64Total kernel dispatches issued by this instance (cumulative).
gpu_bytes_allocated: AtomicU64Total bytes of GPU memory this instance has allocated.
Implementations§
Source§impl InstanceState
impl InstanceState
Sourcepub fn new(tenant_id: TenantId, instance_id: InstanceId) -> Self
pub fn new(tenant_id: TenantId, instance_id: InstanceId) -> Self
Construct a fresh state with created_at = Instant::now().
The limiter is initialised with usize::MAX (no enforcement);
callers that want enforcement should chain
InstanceState::with_memory_limit (the limiter field is
pub(crate) so external code cannot widen it post-construction).
Sourcepub fn with_streaming(self, streaming: StreamingContext) -> Self
pub fn with_streaming(self, streaming: StreamingContext) -> Self
Install a StreamingContext on this state; returns self
for builder-style chaining.
Called from crate::executor::TensorWasmExecutor::spawn_instance
when crate::executor::SpawnConfig::streaming is set so the
wasi:tensor/host.emit-chunk host function reaches a live
downstream receiver. Spawns without a streaming context retain
the default StreamingContext::disabled shape.
Sourcepub fn streaming(&self) -> &StreamingContext
pub fn streaming(&self) -> &StreamingContext
Borrow the per-instance streaming context. Used by the
wasi:tensor/host linker registration to plumb the
per-instance state into the emit-chunk and flush host
functions.
Sourcepub fn with_input(self, input: InputContext) -> Self
pub fn with_input(self, input: InputContext) -> Self
Install an InputContext on this state; returns self for
builder-style chaining.
Called from crate::executor::TensorWasmExecutor’s spawn path
when crate::executor::SpawnConfig::input is non-empty so the
wasi:tensor/host.read-input host function reaches the staged
bytes. Spawns without input retain the default
InputContext::empty shape.
Sourcepub fn input(&self) -> &InputContext
pub fn input(&self) -> &InputContext
Borrow the per-instance input context. Used by the
wasi:tensor/host linker registration to plumb the per-instance
staged bytes into the input-len and read-input host functions.
Sourcepub fn with_memory_limit(self, max_memory_bytes: usize) -> Self
pub fn with_memory_limit(self, max_memory_bytes: usize) -> Self
Set the per-instance linear-memory cap (in bytes). Returns self
for builder-style use.
Sourcepub fn with_deadline(self, deadline: Instant) -> Self
pub fn with_deadline(self, deadline: Instant) -> Self
Set a deadline; returns self for builder-style use.
Sourcepub fn with_deadline_duration(self, d: Duration) -> Self
pub fn with_deadline_duration(self, d: Duration) -> Self
Record the per-call deadline duration so subsequent calls can re-arm the wall-clock deadline (and matching wasmtime epoch ticks) instead of inheriting the elapsed window from spawn time.
Also seeds the cooperative-scheduler context with the same
budget (clamped to u32::MAX ms ≈ 49 days, which is well above
any plausible production deadline) so guests calling
wasi:scheduler/host.yield() observe a non-zero return code
when the deadline is approaching.
T36: also seeds the scheduler’s absolute Instant deadline
(bp_deadline_instant) to [Self::deadline]. With both fields
installed, the guest’s yield() verdict is derived from the
same Instant that the back-pressure semaphore consults — so
the cooperative path and the resource-admission path agree
when the deadline trips.
Sourcepub fn scheduler(&self) -> &SchedulerContext
pub fn scheduler(&self) -> &SchedulerContext
Borrow the cooperative-scheduler context. Used by the
wasi:scheduler/host linker registration to plumb the
per-instance state into the yield and deadline-remaining-ms
host functions.
Sourcepub fn record_kernel_dispatch(&self) -> u64
pub fn record_kernel_dispatch(&self) -> u64
Increment the kernel dispatch counter and return the new value.
Sourcepub fn record_gpu_alloc(&self, bytes: u64)
pub fn record_gpu_alloc(&self, bytes: u64)
Add bytes to the GPU allocation total.
Sourcepub fn is_past_deadline(&self) -> bool
pub fn is_past_deadline(&self) -> bool
True if the deadline has elapsed.
Sourcepub fn jit_arena_mut(&mut self) -> &mut ArenaState
pub fn jit_arena_mut(&mut self) -> &mut ArenaState
Borrow the per-instance JIT scratch arena mutably.
Used by the host imports registered via
crate::jit_dispatch::add_jit_dispatch_to_linker to keep one
bump cursor and LIFO live stack per store, even when multiple
instances share a single wasmtime::Linker.
Sourcepub fn jit_arena(&self) -> &ArenaState
pub fn jit_arena(&self) -> &ArenaState
Borrow the per-instance JIT scratch arena.
Read-only counterpart to InstanceState::jit_arena_mut; useful
for tests asserting per-store isolation of the bump cursor.
Trait Implementations§
Source§impl Debug for InstanceState
impl Debug for InstanceState
Source§impl HasInput for InstanceState
impl HasInput for InstanceState
Source§fn input(&self) -> &InputContext
fn input(&self) -> &InputContext
Source§impl HasStreaming for InstanceState
impl HasStreaming for InstanceState
Source§fn streaming(&self) -> &StreamingContext
fn streaming(&self) -> &StreamingContext
Source§impl JitArenaProvider for InstanceState
impl JitArenaProvider for InstanceState
Source§fn jit_arena_mut(&mut self) -> &mut ArenaState
fn jit_arena_mut(&mut self) -> &mut ArenaState
Source§impl TenantContext for InstanceState
impl TenantContext for InstanceState
Auto Trait Implementations§
impl !Freeze for InstanceState
impl RefUnwindSafe for InstanceState
impl Send for InstanceState
impl Sync for InstanceState
impl Unpin for InstanceState
impl UnsafeUnpin for InstanceState
impl UnwindSafe for InstanceState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more