Skip to content

Commit

Permalink
Move profile_guest option to run-mode args adn out of ExecuteCtx
Browse files Browse the repository at this point in the history
Co-authored-by: Jamey Sharp <jsharp@fastly.com>
  • Loading branch information
itsrainy and jameysharp committed Jul 5, 2023
1 parent 0f35e84 commit ec0714d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
18 changes: 9 additions & 9 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -222,14 +227,9 @@ async fn create_execution_context(
check_backends: bool,
) -> Result<ExecuteCtx, anyhow::Error> {
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)?;
Expand Down
19 changes: 10 additions & 9 deletions cli/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,

/// Args to pass along to the binary being executed.
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
wasm_args: Vec<String>,
Expand All @@ -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<ProfilingStrategy>,
/// 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<PathBuf>,
/// Set of experimental WASI modules to link against.
#[arg(value_enum, long = "experimental_modules", required = false)]
experimental_modules: Vec<ExperimentalModuleArg>,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<ExperimentalModule> {
self.experimental_modules.iter().map(|x| x.into()).collect()
Expand Down
20 changes: 10 additions & 10 deletions lib/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ pub struct ExecuteCtx {
// `Arc` for the two fields below because this struct must be `Clone`.
epoch_increment_thread: Option<Arc<JoinHandle<()>>>,
epoch_increment_stop: Arc<AtomicBool>,
/// The path to save a guest profile to
guest_profile_path: Option<PathBuf>,
}

impl ExecuteCtx {
Expand All @@ -78,7 +76,6 @@ impl ExecuteCtx {
module_path: impl AsRef<Path>,
profiling_strategy: ProfilingStrategy,
wasi_modules: HashSet<ExperimentalModule>,
guest_profile_path: Option<PathBuf>,
) -> Result<Self, Error> {
let config = &configure_wasmtime(profiling_strategy);
let engine = Engine::new(config)?;
Expand Down Expand Up @@ -115,7 +112,6 @@ impl ExecuteCtx {
secret_stores: Arc::new(SecretStores::new()),
epoch_increment_thread,
epoch_increment_stop,
guest_profile_path,
})
}

Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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)))
Expand Down

0 comments on commit ec0714d

Please sign in to comment.