diff --git a/cli/src/main.rs b/cli/src/main.rs index 7ef0d0e9..04fa3a53 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -91,7 +91,12 @@ pub async fn run_wasm_main(run_args: RunArgs) -> Result<(), anyhow::Error> { Some(stem) => stem.to_string_lossy(), None => panic!("program cannot be a directory"), }; - ctx.run_main(&program_name, run_args.wasm_args()).await + ctx.run_main( + &program_name, + run_args.wasm_args(), + run_args.profile_guest(), + ) + .await } fn install_tracing_subscriber(verbosity: u8) { @@ -222,14 +227,9 @@ async fn create_execution_context( check_backends: bool, ) -> Result { let input = args.input(); - let mut ctx = ExecuteCtx::new( - input, - args.profiling_strategy(), - args.wasi_modules(), - args.profile_guest().cloned(), - )? - .with_log_stderr(args.log_stderr()) - .with_log_stdout(args.log_stdout()); + let mut ctx = ExecuteCtx::new(input, args.profiling_strategy(), args.wasi_modules())? + .with_log_stderr(args.log_stderr()) + .with_log_stdout(args.log_stdout()); if let Some(config_path) = args.config_path() { let config = FastlyConfig::from_file(config_path)?; diff --git a/cli/src/opts.rs b/cli/src/opts.rs index ee88b135..86f5bd61 100644 --- a/cli/src/opts.rs +++ b/cli/src/opts.rs @@ -61,6 +61,11 @@ pub struct RunArgs { #[command(flatten)] shared: SharedArgs, + /// Whether to profile the wasm guest. Takes an optional filename to save + /// the profile to + #[arg(long, default_missing_value = "guest-profile.json", num_args=0..=1, require_equals=true)] + profile_guest: Option, + /// Args to pass along to the binary being executed. #[arg(trailing_var_arg = true, allow_hyphen_values = true)] wasm_args: Vec, @@ -83,10 +88,6 @@ pub struct SharedArgs { /// Whether to enable wasmtime's builtin profiler. #[arg(long = "profiler", value_parser = check_wasmtime_profiler_mode)] profiler: Option, - /// Whether to profile the wasm guest. Takes an optional filename to save - /// the profile to - #[arg(long, default_missing_value = "guest-profile.json", num_args=0..=1, require_equals=true)] - profile_guest: Option, /// Set of experimental WASI modules to link against. #[arg(value_enum, long = "experimental_modules", required = false)] experimental_modules: Vec, @@ -120,6 +121,11 @@ impl RunArgs { pub fn shared(&self) -> &SharedArgs { &self.shared } + + /// The path to write a guest profile to + pub fn profile_guest(&self) -> Option<&PathBuf> { + self.profile_guest.as_ref() + } } impl SharedArgs { @@ -148,11 +154,6 @@ impl SharedArgs { self.profiler.unwrap_or(ProfilingStrategy::None) } - /// The path to write a guest profile to - pub fn profile_guest(&self) -> Option<&PathBuf> { - self.profile_guest.as_ref() - } - // Set of experimental wasi modules to link against. pub fn wasi_modules(&self) -> HashSet { self.experimental_modules.iter().map(|x| x.into()).collect() diff --git a/lib/src/execute.rs b/lib/src/execute.rs index fa6afb32..e5269bd8 100644 --- a/lib/src/execute.rs +++ b/lib/src/execute.rs @@ -68,8 +68,6 @@ pub struct ExecuteCtx { // `Arc` for the two fields below because this struct must be `Clone`. epoch_increment_thread: Option>>, epoch_increment_stop: Arc, - /// The path to save a guest profile to - guest_profile_path: Option, } impl ExecuteCtx { @@ -78,7 +76,6 @@ impl ExecuteCtx { module_path: impl AsRef, profiling_strategy: ProfilingStrategy, wasi_modules: HashSet, - guest_profile_path: Option, ) -> Result { let config = &configure_wasmtime(profiling_strategy); let engine = Engine::new(config)?; @@ -115,7 +112,6 @@ impl ExecuteCtx { secret_stores: Arc::new(SecretStores::new()), epoch_increment_thread, epoch_increment_stop, - guest_profile_path, }) } @@ -371,7 +367,12 @@ impl ExecuteCtx { outcome } - pub async fn run_main(self, program_name: &str, args: &[String]) -> Result<(), anyhow::Error> { + pub async fn run_main( + self, + program_name: &str, + args: &[String], + guest_profile_path: Option<&PathBuf>, + ) -> Result<(), anyhow::Error> { // placeholders for request, result sender channel, and remote IP let req = Request::get("http://example.com/").body(Body::empty())?; let req_id = 0; @@ -392,7 +393,7 @@ impl ExecuteCtx { self.secret_stores.clone(), ); - let profiler = self.guest_profile_path.as_ref().map(|_| { + let profiler = guest_profile_path.map(|_| { GuestProfiler::new( program_name, EPOCH_INTERRUPTION_PERIOD, @@ -421,10 +422,9 @@ impl ExecuteCtx { let result = main_func.call_async(&mut store, ()).await; // If we collected a profile, write it to the file - if let (Some(profile), Some(path)) = ( - store.data_mut().take_guest_profiler(), - self.guest_profile_path.as_ref(), - ) { + if let (Some(profile), Some(path)) = + (store.data_mut().take_guest_profiler(), guest_profile_path) + { if let Err(e) = std::fs::File::create(&path) .map_err(anyhow::Error::new) .and_then(|output| profile.finish(std::io::BufWriter::new(output)))