Skip to content

Commit

Permalink
Add WasiCtxBuilder setters for the two clock types (#6669)
Browse files Browse the repository at this point in the history
* Add WasiCtxBuilder setters for the two clock types

Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>

* Move two clocks to dedicated fields in WasiCtx

* Rename clock traits to Host prefix convention

Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>

* Fix tests

---------

Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
  • Loading branch information
rylev authored Jun 30, 2023
1 parent a43d1dc commit b741f7c
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 41 deletions.
14 changes: 6 additions & 8 deletions crates/test-programs/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use wasmtime::{
Config, Engine, Store,
};
use wasmtime_wasi::preview2::{
clocks::{WasiMonotonicClock, WasiWallClock},
clocks::{HostMonotonicClock, HostWallClock},
pipe::ReadPipe,
wasi::command::add_to_linker,
wasi::command::Command,
DirPerms, FilePerms, Table, WasiClocks, WasiCtx, WasiCtxBuilder, WasiView,
DirPerms, FilePerms, Table, WasiCtx, WasiCtxBuilder, WasiView,
};

lazy_static::lazy_static! {
Expand Down Expand Up @@ -131,7 +131,7 @@ async fn random() -> Result<()> {
async fn time() -> Result<()> {
struct FakeWallClock;

impl WasiWallClock for FakeWallClock {
impl HostWallClock for FakeWallClock {
fn resolution(&self) -> Duration {
Duration::from_secs(1)
}
Expand All @@ -145,7 +145,7 @@ async fn time() -> Result<()> {
now: Mutex<u64>,
}

impl WasiMonotonicClock for FakeMonotonicClock {
impl HostMonotonicClock for FakeMonotonicClock {
fn resolution(&self) -> u64 {
1_000_000_000
}
Expand All @@ -160,10 +160,8 @@ async fn time() -> Result<()> {

let mut table = Table::new();
let wasi = WasiCtxBuilder::new()
.set_clocks(WasiClocks {
wall: Box::new(FakeWallClock),
monotonic: Box::new(FakeMonotonicClock { now: Mutex::new(0) }),
})
.set_monotonic_clock(FakeMonotonicClock { now: Mutex::new(0) })
.set_wall_clock(FakeWallClock)
.build(&mut table)?;

let (mut store, command) =
Expand Down
9 changes: 2 additions & 7 deletions crates/wasi/src/preview2/clocks.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
pub mod host;
use cap_std::time::Duration;

pub trait WasiWallClock: Send + Sync {
pub trait HostWallClock: Send + Sync {
fn resolution(&self) -> Duration;
fn now(&self) -> Duration;
}

pub trait WasiMonotonicClock: Send + Sync {
pub trait HostMonotonicClock: Send + Sync {
fn resolution(&self) -> u64;
fn now(&self) -> u64;
}

pub struct WasiClocks {
pub wall: Box<dyn WasiWallClock + Send + Sync>,
pub monotonic: Box<dyn WasiMonotonicClock + Send + Sync>,
}
16 changes: 8 additions & 8 deletions crates/wasi/src/preview2/clocks/host.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{WasiClocks, WasiMonotonicClock, WasiWallClock};
use super::{HostMonotonicClock, HostWallClock};
use cap_std::time::{Duration, Instant, SystemClock};
use cap_std::{ambient_authority, AmbientAuthority};
use cap_time_ext::{MonotonicClockExt, SystemClockExt};
Expand All @@ -16,7 +16,7 @@ impl WallClock {
}
}

impl WasiWallClock for WallClock {
impl HostWallClock for WallClock {
fn resolution(&self) -> Duration {
self.clock.resolution()
}
Expand Down Expand Up @@ -47,7 +47,7 @@ impl MonotonicClock {
}
}

impl WasiMonotonicClock for MonotonicClock {
impl HostMonotonicClock for MonotonicClock {
fn resolution(&self) -> u64 {
self.clock.resolution().as_nanos().try_into().unwrap()
}
Expand All @@ -64,10 +64,10 @@ impl WasiMonotonicClock for MonotonicClock {
}
}

pub fn clocks_ctx() -> WasiClocks {
// Create the per-instance clock resources.
let monotonic = Box::new(MonotonicClock::new(ambient_authority()));
let wall = Box::new(WallClock::new(ambient_authority()));
pub fn monotonic_clock() -> Box<dyn HostMonotonicClock + Send + Sync> {
Box::new(MonotonicClock::new(ambient_authority()))
}

WasiClocks { monotonic, wall }
pub fn wall_clock() -> Box<dyn HostWallClock + Send + Sync> {
Box::new(WallClock::new(ambient_authority()))
}
29 changes: 21 additions & 8 deletions crates/wasi/src/preview2/ctx.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::preview2::{
clocks::{self, WasiClocks},
clocks::{self, HostMonotonicClock, HostWallClock},
filesystem::{Dir, TableFsExt},
pipe, random, stdio,
stream::{InputStream, OutputStream, TableStreamExt},
DirPerms, FilePerms, Table,
};
use cap_rand::{Rng, RngCore, SeedableRng};

use super::clocks::host::{monotonic_clock, wall_clock};

pub struct WasiCtxBuilder {
stdin: Box<dyn InputStream>,
stdout: Box<dyn OutputStream>,
Expand All @@ -18,7 +20,8 @@ pub struct WasiCtxBuilder {
random: Box<dyn RngCore + Send + Sync>,
insecure_random: Box<dyn RngCore + Send + Sync>,
insecure_random_seed: u128,
clocks: WasiClocks,
wall_clock: Box<dyn HostWallClock + Send + Sync>,
monotonic_clock: Box<dyn HostMonotonicClock + Send + Sync>,
}

impl WasiCtxBuilder {
Expand All @@ -43,7 +46,8 @@ impl WasiCtxBuilder {
random: random::thread_rng(),
insecure_random,
insecure_random_seed,
clocks: clocks::host::clocks_ctx(),
wall_clock: wall_clock(),
monotonic_clock: monotonic_clock(),
}
}

Expand Down Expand Up @@ -155,8 +159,14 @@ impl WasiCtxBuilder {
self.insecure_random_seed = insecure_random_seed;
self
}
pub fn set_clocks(mut self, clocks: WasiClocks) -> Self {
self.clocks = clocks;

pub fn set_wall_clock(mut self, clock: impl clocks::HostWallClock + 'static) -> Self {
self.wall_clock = Box::new(clock);
self
}

pub fn set_monotonic_clock(mut self, clock: impl clocks::HostMonotonicClock + 'static) -> Self {
self.monotonic_clock = Box::new(clock);
self
}

Expand All @@ -172,7 +182,8 @@ impl WasiCtxBuilder {
random,
insecure_random,
insecure_random_seed,
clocks,
wall_clock,
monotonic_clock,
} = self;

let stdin = table.push_input_stream(stdin).context("stdin")?;
Expand All @@ -199,7 +210,8 @@ impl WasiCtxBuilder {
random,
insecure_random,
insecure_random_seed,
clocks,
wall_clock,
monotonic_clock,
})
}
}
Expand All @@ -215,7 +227,8 @@ pub struct WasiCtx {
pub(crate) random: Box<dyn RngCore + Send + Sync>,
pub(crate) insecure_random: Box<dyn RngCore + Send + Sync>,
pub(crate) insecure_random_seed: u128,
pub(crate) clocks: WasiClocks,
pub(crate) wall_clock: Box<dyn HostWallClock + Send + Sync>,
pub(crate) monotonic_clock: Box<dyn HostMonotonicClock + Send + Sync>,
pub(crate) env: Vec<(String, String)>,
pub(crate) args: Vec<String>,
pub(crate) preopens: Vec<(u32, String)>,
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi/src/preview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub mod wasi;

pub use cap_fs_ext::SystemTimeSpec;
pub use cap_rand::RngCore;
pub use clocks::{WasiClocks, WasiMonotonicClock, WasiWallClock};
pub use clocks::{HostMonotonicClock, HostWallClock};
pub use ctx::{WasiCtx, WasiCtxBuilder, WasiView};
pub use error::I32Exit;
pub use filesystem::{DirPerms, FilePerms};
Expand Down
8 changes: 4 additions & 4 deletions crates/wasi/src/preview2/preview2/clocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ impl TryFrom<SystemTime> for Datetime {
#[async_trait::async_trait]
impl<T: WasiView> wall_clock::Host for T {
async fn now(&mut self) -> anyhow::Result<Datetime> {
let now = self.ctx().clocks.wall.now();
let now = self.ctx().wall_clock.now();
Ok(Datetime {
seconds: now.as_secs(),
nanoseconds: now.subsec_nanos(),
})
}

async fn resolution(&mut self) -> anyhow::Result<Datetime> {
let res = self.ctx().clocks.wall.resolution();
let res = self.ctx().wall_clock.resolution();
Ok(Datetime {
seconds: res.as_secs(),
nanoseconds: res.subsec_nanos(),
Expand All @@ -46,11 +46,11 @@ impl<T: WasiView> wall_clock::Host for T {
#[async_trait::async_trait]
impl<T: WasiView> monotonic_clock::Host for T {
async fn now(&mut self) -> anyhow::Result<Instant> {
Ok(self.ctx().clocks.monotonic.now())
Ok(self.ctx().monotonic_clock.now())
}

async fn resolution(&mut self) -> anyhow::Result<Instant> {
Ok(self.ctx().clocks.monotonic.resolution())
Ok(self.ctx().monotonic_clock.resolution())
}

async fn subscribe(&mut self, when: Instant, absolute: bool) -> anyhow::Result<Pollable> {
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi/src/preview2/preview2/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<T: WasiView> poll::Host for T {
}
PollableEntry::MonotonicClock(when, absolute) => {
poll.subscribe_monotonic_clock(
&*self.ctx().clocks.monotonic,
&*self.ctx().monotonic_clock,
when,
absolute,
userdata,
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi/src/preview2/sched.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(dead_code)]
use crate::preview2::{
clocks::WasiMonotonicClock,
clocks::HostMonotonicClock,
stream::{InputStream, OutputStream},
};
use anyhow::Error;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl<'a> Poll<'a> {
}
pub fn subscribe_monotonic_clock(
&mut self,
clock: &'a dyn WasiMonotonicClock,
clock: &'a dyn HostMonotonicClock,
deadline: u64,
absolute: bool,
ud: Userdata,
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi/src/preview2/sched/subscription.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::preview2::{
clocks::WasiMonotonicClock,
clocks::HostMonotonicClock,
stream::{InputStream, OutputStream},
};
use anyhow::Error;
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<'a> RwSubscription<'a> {
}

pub struct MonotonicClockSubscription<'a> {
pub clock: &'a dyn WasiMonotonicClock,
pub clock: &'a dyn HostMonotonicClock,
pub absolute_deadline: u64,
}

Expand Down

0 comments on commit b741f7c

Please sign in to comment.