Skip to main content

InstanceState

Struct InstanceState 

Source
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: TenantId

Owning tenant.

§instance_id: InstanceId

Unique instance identifier.

§created_at: Instant

Walltime 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: AtomicU64

Total kernel dispatches issued by this instance (cumulative).

§gpu_bytes_allocated: AtomicU64

Total bytes of GPU memory this instance has allocated.

Implementations§

Source§

impl InstanceState

Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn with_deadline(self, deadline: Instant) -> Self

Set a deadline; returns self for builder-style use.

Source

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.

Source

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.

Source

pub fn record_kernel_dispatch(&self) -> u64

Increment the kernel dispatch counter and return the new value.

Source

pub fn record_gpu_alloc(&self, bytes: u64)

Add bytes to the GPU allocation total.

Source

pub fn is_past_deadline(&self) -> bool

True if the deadline has elapsed.

Source

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.

Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl HasInput for InstanceState

Source§

fn input(&self) -> &InputContext

Borrow the input context.
Source§

impl HasStreaming for InstanceState

Source§

fn streaming(&self) -> &StreamingContext

Borrow the streaming context.
Source§

impl JitArenaProvider for InstanceState

Source§

fn jit_arena_mut(&mut self) -> &mut ArenaState

Borrow the per-store JIT arena mutably.
Source§

impl TenantContext for InstanceState

Source§

fn tenant_id(&self) -> TenantId

The tenant this store belongs to. Returned to the dispatch closure on every host call so the cache lookup is scoped to the caller.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more