Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Share GuestProfiler symbol tables through GuestProfilerModules (#14753)

andrewl6097fa3591e0bb9a
diff --git a/crates/c-api/include/wasmtime/profiling.h b/crates/c-api/include/wasmtime/profiling.h
index 885fec56ae..7f55fe9d2c 100644
--- a/crates/c-api/include/wasmtime/profiling.h
+++ b/crates/c-api/include/wasmtime/profiling.h
@@ -45,7 +45,8 @@ WASM_API_EXTERN void wasmtime_guestprofiler_delete(
* \brief Alias to #wasmtime_guestprofiler_modules
*
* \struct #wasmtime_guestprofiler_modules
- * \brief Tuple of name and module for passing into #wasmtime_guestprofiler_new.
+ * \brief Tuple of name and module for passing into #wasmtime_guestprofiler_new
+ * and #wasmtime_guestprofiler_module_set_new.
*/
typedef struct wasmtime_guestprofiler_modules {
const wasm_name_t *name; //!< Name recorded in the profile.
@@ -76,6 +77,94 @@ WASM_API_EXTERN /* own */ wasmtime_guestprofiler_t *wasmtime_guestprofiler_new(
uint64_t interval_nanos, const wasmtime_guestprofiler_modules_t *modules,
size_t modules_len);
+/**
+ * \brief A set of modules prepared once for use by any number of profilers.
+ *
+ * Preparing a module builds its symbol table, which demangles the name of
+ * every function it defines; for modules with many functions this is most of
+ * the cost of #wasmtime_guestprofiler_new, which repeats it for every profile.
+ * Embedders that profile the same modules repeatedly can prepare them once
+ * with #wasmtime_guestprofiler_module_set_new and start each profile with
+ * #wasmtime_guestprofiler_new_with_module_set.
+ *
+ * A module set may be shared between threads, and profiles started from the
+ * same set are independent of one another.
+ *
+ * For more information see the Rust documentation at:
+ * https://docs.wasmtime.dev/api/wasmtime/struct.GuestProfilerModules.html
+ */
+typedef struct wasmtime_guestprofiler_module_set
+ wasmtime_guestprofiler_module_set_t;
+
+/**
+ * \brief Prepares a set of modules for profiling.
+ *
+ * \param engine engine the modules were created with
+ * \param modules modules and associated names that will appear in
+ * captured stack traces, pointer to the first element
+ * \param modules_len count of elements in `modules`
+ * \param out where to write the created module set, which is owned by
+ * the caller, on success
+ *
+ * \return Returns #wasmtime_error_t owned by the caller in case of error,
+ * `NULL` otherwise. An error is returned if `engine` has guest debugging
+ * enabled.
+ *
+ * This function does not take ownership of `engine` or of the elements of
+ * `modules`. See the Rust documentation for guidance on which modules should
+ * not be included in `modules`.
+ *
+ * For more information see the Rust documentation at:
+ * https://docs.wasmtime.dev/api/wasmtime/struct.GuestProfilerModules.html#method.new
+ */
+WASM_API_EXTERN /* own */ wasmtime_error_t *
+wasmtime_guestprofiler_module_set_new(
+ const wasm_engine_t *engine,
+ const wasmtime_guestprofiler_modules_t *modules, size_t modules_len,
+ /* own */ wasmtime_guestprofiler_module_set_t **out);
+
+/**
+ * \brief Creates a shallow clone of the specified module set, increasing the
+ * internal reference count.
+ */
+WASM_API_EXTERN /* own */ wasmtime_guestprofiler_module_set_t *
+wasmtime_guestprofiler_module_set_clone(
+ const wasmtime_guestprofiler_module_set_t *set);
+
+/**
+ * \brief Deletes a module set.
+ *
+ * Profilers already started from the set are unaffected.
+ *
+ * \param set module set that is being deleted
+ */
+WASM_API_EXTERN void wasmtime_guestprofiler_module_set_delete(
+ /* own */ wasmtime_guestprofiler_module_set_t *set);
+
+/**
+ * \brief Begin profiling a new guest using a prepared module set.
+ *
+ * \param module_name name recorded in the profile
+ * \param interval_nanos intended sampling interval in nanoseconds recorded in
+ * the profile
+ * \param modules module set whose modules will appear in captured stack
+ * traces
+ *
+ * \return Created profiler that is owned by the caller.
+ *
+ * This behaves like #wasmtime_guestprofiler_new, except that the per-module
+ * preparation has already been done. This function does not take ownership of
+ * the arguments; the profiler keeps its own reference to `modules`, which may
+ * be deleted while the profiler is still in use.
+ *
+ * For more information see the Rust documentation at:
+ * https://docs.wasmtime.dev/api/wasmtime/struct.GuestProfiler.html#method.new_with_modules
+ */
+WASM_API_EXTERN /* own */ wasmtime_guestprofiler_t *
+wasmtime_guestprofiler_new_with_module_set(
+ const wasm_name_t *module_name, uint64_t interval_nanos,
+ const wasmtime_guestprofiler_module_set_t *modules);
+
/**
* \brief Add a sample to the profile.
*
diff --git a/crates/c-api/src/profiling.rs b/crates/c-api/src/profiling.rs
index 307b434f80..0bb9932fdc 100644
--- a/crates/c-api/src/profiling.rs
+++ b/crates/c-api/src/profiling.rs
@@ -1,11 +1,10 @@
use crate::{
- wasm_byte_vec_t, wasm_engine_t, wasm_name_t, wasmtime_error_t, wasmtime_module_t,
- wasmtime_store_t,
+ handle_result, wasm_byte_vec_t, wasm_engine_t, wasm_name_t, wasmtime_error_t,
+ wasmtime_module_t, wasmtime_store_t,
};
-use std::slice;
use std::str::from_utf8;
use std::time::Duration;
-use wasmtime::GuestProfiler;
+use wasmtime::{GuestProfiler, GuestProfilerModules, Module};
pub struct wasmtime_guestprofiler_t {
guest_profiler: GuestProfiler,
@@ -13,22 +12,24 @@ pub struct wasmtime_guestprofiler_t {
wasmtime_c_api_macros::declare_own!(wasmtime_guestprofiler_t);
+pub struct wasmtime_guestprofiler_module_set_t {
+ modules: GuestProfilerModules,
+}
+
+wasmtime_c_api_macros::declare_own!(wasmtime_guestprofiler_module_set_t);
+
#[repr(C)]
pub struct wasmtime_guestprofiler_modules_t<'a> {
name: &'a wasm_name_t,
module: &'a wasmtime_module_t,
}
-#[unsafe(no_mangle)]
-pub unsafe extern "C" fn wasmtime_guestprofiler_new(
- engine: &wasm_engine_t,
- module_name: &wasm_name_t,
- interval_nanos: u64,
+/// Converts a C array of name/module pairs into the form the Rust API takes.
+unsafe fn module_list(
modules: *const wasmtime_guestprofiler_modules_t,
modules_len: usize,
-) -> Box<wasmtime_guestprofiler_t> {
- let module_name = from_utf8(&module_name.as_slice()).expect("not valid utf-8");
- let list = slice::from_raw_parts(modules, modules_len)
+) -> Vec<(String, Module)> {
+ crate::slice_from_raw_parts(modules, modules_len)
.iter()
.map(|entry| {
(
@@ -38,7 +39,19 @@ pub unsafe extern "C" fn wasmtime_guestprofiler_new(
entry.module.module.clone(),
)
})
- .collect::<Vec<_>>();
+ .collect()
+}
+
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn wasmtime_guestprofiler_new(
+ engine: &wasm_engine_t,
+ module_name: &wasm_name_t,
+ interval_nanos: u64,
+ modules: *const wasmtime_guestprofiler_modules_t,
+ modules_len: usize,
+) -> Box<wasmtime_guestprofiler_t> {
+ let module_name = from_utf8(&module_name.as_slice()).expect("not valid utf-8");
+ let list = module_list(modules, modules_len);
Box::new(wasmtime_guestprofiler_t {
guest_profiler: GuestProfiler::new(
&engine.engine,
@@ -50,6 +63,44 @@ pub unsafe extern "C" fn wasmtime_guestprofiler_new(
})
}
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn wasmtime_guestprofiler_module_set_new(
+ engine: &wasm_engine_t,
+ modules: *const wasmtime_guestprofiler_modules_t,
+ modules_len: usize,
+ out: &mut *mut wasmtime_guestprofiler_module_set_t,
+) -> Option<Box<wasmtime_error_t>> {
+ let list = module_list(modules, modules_len);
+ handle_result(GuestProfilerModules::new(&engine.engine, list), |modules| {
+ *out = Box::into_raw(Box::new(wasmtime_guestprofiler_module_set_t { modules }));
+ })
+}
+
+#[unsafe(no_mangle)]
+pub extern "C" fn wasmtime_guestprofiler_module_set_clone(
+ set: &wasmtime_guestprofiler_module_set_t,
+) -> Box<wasmtime_guestprofiler_module_set_t> {
+ Box::new(wasmtime_guestprofiler_module_set_t {
+ modules: set.modules.clone(),
+ })
+}
+
+#[unsafe(no_mangle)]
+pub extern "C" fn wasmtime_guestprofiler_new_with_module_set(
+ module_name: &wasm_name_t,
+ interval_nanos: u64,
+ modules: &wasmtime_guestprofiler_module_set_t,
+) -> Box<wasmtime_guestprofiler_t> {
+ let module_name = from_utf8(&module_name.as_slice()).expect("not valid utf-8");
+ Box::new(wasmtime_guestprofiler_t {
+ guest_profiler: GuestProfiler::new_with_modules(
+ module_name,
+ Duration::from_nanos(interval_nanos),
+ &modules.modules,
+ ),
+ })
+}
+
#[unsafe(no_mangle)]
pub extern "C" fn wasmtime_guestprofiler_sample(
guestprofiler: &mut wasmtime_guestprofiler_t,
diff --git a/crates/wasmtime/src/runtime.rs b/crates/wasmtime/src/runtime.rs
index 0913b117ae..52580f99ed 100644
--- a/crates/wasmtime/src/runtime.rs
+++ b/crates/wasmtime/src/runtime.rs
@@ -119,7 +119,7 @@ pub use vm::{PoolConcurrencyLimitError, PoolingAllocatorMetrics};
#[cfg(feature = "profiling")]
mod profiling;
#[cfg(feature = "profiling")]
-pub use profiling::GuestProfiler;
+pub use profiling::{GuestProfiler, GuestProfilerModules};
#[cfg(feature = "async")]
pub(crate) mod stack;
diff --git a/crates/wasmtime/src/runtime/profiling.rs b/crates/wasmtime/src/runtime/profiling.rs
index 336639c798..dfef6e64fb 100644
--- a/crates/wasmtime/src/runtime/profiling.rs
+++ b/crates/wasmtime/src/runtime/profiling.rs
@@ -33,6 +33,12 @@ use wasmtime_environ::{FuncKey, demangle_function_name_or_index};
/// way to do that is to call it from a callback registered with
/// [`Store::epoch_deadline_callback()`](crate::Store::epoch_deadline_callback).
///
+/// Starting a profile with [`GuestProfiler::new`] prepares the profiled
+/// modules each time, which for modules with many functions can take
+/// milliseconds. To profile the same modules many times, for example once per
+/// instance, prepare them once with [`GuestProfilerModules`] and start each
+/// profile with [`GuestProfiler::new_with_modules`].
+///
/// # Accuracy
///
/// The data collection granularity is limited by the mechanism you use to
@@ -75,42 +81,93 @@ use wasmtime_environ::{FuncKey, demangle_function_name_or_index};
#[derive(Debug)]
pub struct GuestProfiler {
profile: Profile,
- modules: Modules,
+ modules: GuestProfilerModules,
+ /// Library handle in `profile` for each entry of `modules`, by index.
+ libs: Vec<fxprof_processed_profile::LibraryHandle>,
process: fxprof_processed_profile::ProcessHandle,
thread: fxprof_processed_profile::ThreadHandle,
start: Instant,
marker: CallMarker,
}
+/// The set of modules a [`GuestProfiler`] attributes samples to, prepared
+/// once and shared by any number of profilers.
+///
+/// Preparing a module builds its symbol table, which demangles the name of
+/// every function it defines; for modules with many functions this is the
+/// bulk of the cost of starting a profile. [`GuestProfiler::new`] does this
+/// work every time it is called. Embedders that profile the same modules
+/// repeatedly, for example once per request or once per instance, can instead
+/// build a `GuestProfilerModules` once and pass it to
+/// [`GuestProfiler::new_with_modules`] for each profile, which then does no
+/// per-module work beyond registering the prepared symbol tables.
+///
+/// Cloning is cheap, since the prepared modules are reference-counted, and a
+/// `GuestProfilerModules` can be shared between threads. Profiles started
+/// from the same set are independent of one another.
+///
+/// # Example
+///
+/// ```
+/// # use std::time::Duration;
+/// # use wasmtime::*;
+/// # fn main() -> Result<()> {
+/// let engine = Engine::default();
+/// let module = Module::new(&engine, r#"(module (func (export "run")))"#)?;
+///
+/// // Prepare the modules once...
+/// let modules = GuestProfilerModules::new(&engine, [("main.wasm".to_string(), module.clone())])?;
+///
+/// // ...then start a profile for each guest that runs them.
+/// for _ in 0..3 {
+/// let profiler = GuestProfiler::new_with_modules("main", Duration::from_millis(10), &modules);
+/// let mut store = Store::new(&engine, profiler);
+/// // Arrange to call `GuestProfiler::sample` while the guest runs, for
+/// // example from `Store::epoch_deadline_callback`.
+/// let instance = Instance::new(&mut store, &module, &[])?;
+/// instance
+/// .get_typed_func::<(), ()>(&mut store, "run")?
+/// .call(&mut store, ())?;
+/// let mut profile = Vec::new();
+/// store.into_data().finish(&mut profile)?;
+/// }
+/// # Ok(())
+/// # }
+/// ```
+#[derive(Clone, Debug)]
+pub struct GuestProfilerModules {
+ /// Sorted by `text_range.start`, for lookup by program counter.
+ modules: Arc<[PreparedModule]>,
+}
+
#[derive(Debug)]
-struct ProfiledModule {
+struct PreparedModule {
+ name: String,
module: Module,
- fxprof_libhandle: fxprof_processed_profile::LibraryHandle,
text_range: Range<usize>,
+ symbol_table: Arc<SymbolTable>,
}
-type Modules = Vec<ProfiledModule>;
-
-impl GuestProfiler {
- /// Begin profiling a new guest. When this function is called, the current
- /// wall-clock time is recorded as the start time for the guest.
+impl GuestProfilerModules {
+ /// Prepares `modules` for profiling.
///
- /// The `module_name` parameter is recorded in the profile to help identify
- /// where the profile came from.
+ /// Each entry pairs a module with the name it is given in profiles. Only
+ /// modules in `modules` appear in stack traces of profiles that use this
+ /// set; stack frames executing host code or functions from other modules
+ /// are omitted. See the "Security" section of the [`GuestProfiler`]
+ /// documentation for guidance on which modules to include. Modules that
+ /// define no functions are skipped, since no sample can land in them.
///
- /// The `interval` parameter should match the rate at which you intend
- /// to call `sample`. However, this is used as a hint and not required to
- /// exactly match the real sample rate.
+ /// # Errors
///
- /// Only modules which are present in the `modules` vector will appear in
- /// stack traces in this profile. Any stack frames which were executing
- /// host code or functions from other modules will be omitted. See the
- /// "Security" section of the [`GuestProfiler`] documentation for guidance
- /// on what modules should not be included in this list.
+ /// Returns an error if `engine` has guest debugging enabled, which
+ /// profiling does not support.
+ ///
+ /// # Panics
+ ///
+ /// Panics if any module in `modules` was not created with `engine`.
pub fn new(
engine: &Engine,
- module_name: &str,
- interval: Duration,
modules: impl IntoIterator<Item = (String, Module)>,
) -> Result<Self> {
// Check that guest debugging is not enabled. The
@@ -122,11 +179,6 @@ impl GuestProfiler {
crate::bail!("Profiling cannot be performed when guest-debugging is enabled.");
}
- let zero = ReferenceTimestamp::from_millis_since_unix_epoch(0.0);
- let mut profile = Profile::new(module_name, zero, interval.into());
-
- // Past this point, we just need to think about modules as we pull out
- // the disparate module information from components.
let mut modules: Vec<_> = modules
.into_iter()
.filter_map(|(name, module)| {
@@ -143,33 +195,136 @@ impl GuestProfiler {
let end = (module.engine_code().text_range().start + end).raw();
start..end
};
-
- module_symbols(name, &module).map(|lib| {
- let libhandle = profile.add_lib(lib);
- ProfiledModule {
- module,
- fxprof_libhandle: libhandle,
- text_range,
- }
+ let symbol_table = module_symbol_table(&module)?;
+ Some(PreparedModule {
+ name,
+ module,
+ text_range,
+ symbol_table,
})
})
.collect();
modules.sort_unstable_by_key(|m| m.text_range.start);
+ Ok(Self {
+ modules: modules.into(),
+ })
+ }
+
+ /// Prepares the static modules of `component`, plus `extra_modules`, for
+ /// profiling.
+ ///
+ /// See [`GuestProfilerModules::new`] for additional information.
+ #[cfg(feature = "component-model")]
+ pub fn new_component(
+ engine: &Engine,
+ component: &Component,
+ extra_modules: impl IntoIterator<Item = (String, Module)>,
+ ) -> Result<Self> {
+ let modules = component
+ .static_modules()
+ .map(|m| (m.name().unwrap_or("<unknown>").to_string(), m.clone()))
+ .chain(extra_modules);
+ Self::new(engine, modules)
+ }
+}
+
+impl GuestProfiler {
+ /// Begin profiling a new guest. When this function is called, the current
+ /// wall-clock time is recorded as the start time for the guest.
+ ///
+ /// The `module_name` parameter is recorded in the profile to help identify
+ /// where the profile came from.
+ ///
+ /// The `interval` parameter should match the rate at which you intend
+ /// to call `sample`. However, this is used as a hint and not required to
+ /// exactly match the real sample rate.
+ ///
+ /// Only modules which are present in the `modules` vector will appear in
+ /// stack traces in this profile. Any stack frames which were executing
+ /// host code or functions from other modules will be omitted. See the
+ /// "Security" section of the [`GuestProfiler`] documentation for guidance
+ /// on what modules should not be included in this list.
+ ///
+ /// This prepares `modules` afresh for every profile, which for modules
+ /// with many functions is most of this function's cost. To profile the
+ /// same modules repeatedly, prepare them once with
+ /// [`GuestProfilerModules::new`] and use
+ /// [`GuestProfiler::new_with_modules`] instead.
+ ///
+ /// # Errors
+ ///
+ /// Returns an error if `engine` has guest debugging enabled, which
+ /// profiling does not support.
+ ///
+ /// # Panics
+ ///
+ /// Panics if any module in `modules` was not created with `engine`.
+ pub fn new(
+ engine: &Engine,
+ module_name: &str,
+ interval: Duration,
+ modules: impl IntoIterator<Item = (String, Module)>,
+ ) -> Result<Self> {
+ let modules = GuestProfilerModules::new(engine, modules)?;
+ Ok(Self::new_with_modules(module_name, interval, &modules))
+ }
+
+ /// Begin profiling a new guest using a set of modules prepared with
+ /// [`GuestProfilerModules`]. When this function is called, the current
+ /// wall-clock time is recorded as the start time for the guest.
+ ///
+ /// This behaves like [`GuestProfiler::new`], except that the per-module
+ /// preparation has already been done, so starting a profile costs the
+ /// same however many functions the modules define. Only modules in
+ /// `modules` will appear in stack traces in this profile.
+ ///
+ /// See [`GuestProfiler::new`] for the meaning of `module_name` and
+ /// `interval`.
+ pub fn new_with_modules(
+ module_name: &str,
+ interval: Duration,
+ modules: &GuestProfilerModules,
+ ) -> Self {
+ let zero = ReferenceTimestamp::from_millis_since_unix_epoch(0.0);
+ let mut profile = Profile::new(module_name, zero, interval.into());
+
+ let libs = modules
+ .modules
+ .iter()
+ .map(|m| {
+ // The profile keys libraries by value, so the symbol table is
+ // attached after the library is added rather than carried in
+ // the `LibraryInfo`, which would hash every function name.
+ let lib = profile.add_lib(LibraryInfo {
+ name: m.name.clone(),
+ debug_name: String::new(),
+ path: String::new(),
+ debug_path: String::new(),
+ debug_id: DebugId::nil(),
+ code_id: None,
+ arch: None,
+ symbol_table: None,
+ });
+ profile.set_lib_symbol_table(lib, m.symbol_table.clone());
+ lib
+ })
+ .collect();
profile.set_reference_timestamp(std::time::SystemTime::now().into());
let process = profile.add_process(module_name, 0, Timestamp::from_nanos_since_reference(0));
let thread = profile.add_thread(process, 0, Timestamp::from_nanos_since_reference(0), true);
let start = Instant::now();
let marker = CallMarker::new(&mut profile);
- Ok(Self {
+ Self {
profile,
- modules,
+ modules: modules.clone(),
+ libs,
process,
thread,
start,
marker,
- })
+ }
}
/// Create a new profiler for the provided component
@@ -185,11 +340,8 @@ impl GuestProfiler {
component: Component,
extra_modules: impl IntoIterator<Item = (String, Module)>,
) -> Result<Self> {
- let modules = component
- .static_modules()
- .map(|m| (m.name().unwrap_or("<unknown>").to_string(), m.clone()))
- .chain(extra_modules);
- Self::new(engine, component_name, interval, modules)
+ let modules = GuestProfilerModules::new_component(engine, &component, extra_modules)?;
+ Ok(Self::new_with_modules(component_name, interval, &modules))
}
/// Add a sample to the profile. This function collects a backtrace from
@@ -205,7 +357,7 @@ impl GuestProfiler {
self.start.elapsed().as_nanos().try_into().unwrap(),
);
let backtrace = Backtrace::new(store.as_context().0);
- let frames = lookup_frames(&self.modules, &backtrace);
+ let frames = lookup_frames(&self.modules.modules, &self.libs, &backtrace);
let stack = self
.profile
.intern_stack_frames(self.thread, frames.into_iter());
@@ -225,7 +377,7 @@ impl GuestProfiler {
CallHook::CallingWasm | CallHook::ReturningFromWasm => {}
CallHook::CallingHost => {
let backtrace = Backtrace::new(store.as_context().0);
- let frames = lookup_frames(&self.modules, &backtrace);
+ let frames = lookup_frames(&self.modules.modules, &self.libs, &backtrace);
let marker = self.profile.add_marker(
self.thread,
MarkerTiming::IntervalStart(now),
@@ -261,7 +413,7 @@ impl GuestProfiler {
}
}
-fn module_symbols(name: String, module: &Module) -> Option<LibraryInfo> {
+fn module_symbol_table(module: &Module) -> Option<Arc<SymbolTable>> {
let compiled = module.compiled_module();
let symbols = Vec::from_iter(
module
@@ -290,20 +442,12 @@ fn module_symbols(name: String, module: &Module) -> Option<LibraryInfo> {
return None;
}
- Some(LibraryInfo {
- name,
- debug_name: String::new(),
- path: String::new(),
- debug_path: String::new(),
- debug_id: DebugId::nil(),
- code_id: None,
- arch: None,
- symbol_table: Some(Arc::new(SymbolTable::new(symbols))),
- })
+ Some(Arc::new(SymbolTable::new(symbols)))
}
fn lookup_frames<'a>(
- modules: &'a Modules,
+ modules: &'a [PreparedModule],
+ libs: &'a [fxprof_processed_profile::LibraryHandle],
backtrace: &'a Backtrace,
) -> impl Iterator<Item = FrameInfo> + 'a {
backtrace
@@ -329,7 +473,7 @@ fn lookup_frames<'a>(
let module_text_start = module.module.text().as_ptr_range().start as usize;
return Some(FrameInfo {
frame: Frame::RelativeAddressFromReturnAddress(
- module.fxprof_libhandle,
+ libs[idx],
u32::try_from(frame.pc() - module_text_start).unwrap(),
),
category_pair: CategoryHandle::OTHER.into(),
diff --git a/docs/examples-profiling-guest.md b/docs/examples-profiling-guest.md
index 3ccd6457cb..272a0f77f3 100644
--- a/docs/examples-profiling-guest.md
+++ b/docs/examples-profiling-guest.md
@@ -12,3 +12,12 @@ To use this profiler with the Wasmtime CLI, pass the
When used with `-W timeout=N`, the timeout will be rounded up to the nearest
multiple of the profiling interval.
+
+To use this profiler when embedding Wasmtime, see [`GuestProfiler`]. An
+embedder that profiles the same modules many times, for example once per
+instance, can prepare them once with [`GuestProfilerModules`] and start each
+profile with [`GuestProfiler::new_with_modules`]; `wasmtime serve` does this.
+
+[`GuestProfiler`]: https://docs.wasmtime.dev/api/wasmtime/struct.GuestProfiler.html
+[`GuestProfilerModules`]: https://docs.wasmtime.dev/api/wasmtime/struct.GuestProfilerModules.html
+[`GuestProfiler::new_with_modules`]: https://docs.wasmtime.dev/api/wasmtime/struct.GuestProfiler.html#method.new_with_modules
diff --git a/src/commands/serve.rs b/src/commands/serve.rs
index c41d778f6d..3d05e06840 100644
--- a/src/commands/serve.rs
+++ b/src/commands/serve.rs
@@ -736,10 +736,24 @@ impl ServeCommand {
let sem_connections = Arc::new(Semaphore::new(max_concurrent_connections));
+ // `--profile=guest` starts a new profiler for every instance, so the
+ // component's modules are prepared for profiling once, up front.
+ #[cfg(feature = "profiling")]
+ let guest_profiler_modules = match &self.run.profile {
+ Some(Profile::Guest { .. }) => Some(wasmtime::GuestProfilerModules::new_component(
+ engine,
+ &component,
+ std::iter::empty(),
+ )?),
+ _ => None,
+ };
+
let handler = ProxyHandler::new(HostHandlerState {
sem_requests: Semaphore::new(max_concurrent_requests),
cmd: self,
component,
+ #[cfg(feature = "profiling")]
+ guest_profiler_modules,
request_headers,
max_instance_reuse_count,
max_instance_concurrent_reuse_count,
@@ -946,6 +960,10 @@ impl WorkerState for HostWorkerState {
struct HostHandlerState {
cmd: ServeCommand,
component: Component,
+ /// The component's modules prepared for `--profile=guest`, shared by every
+ /// instance's profiler; `None` when guest profiling is disabled.
+ #[cfg(feature = "profiling")]
+ guest_profiler_modules: Option<wasmtime::GuestProfilerModules>,
request_headers: RequestHeaders,
max_instance_reuse_count: usize,
max_instance_concurrent_reuse_count: usize,
@@ -958,7 +976,7 @@ struct HostHandlerState {
impl HostHandlerState {
async fn instantiate_into(&self, store: &mut Store<Host>) -> Result<Proxy> {
- let write_profile = setup_epoch_handler(&self.cmd, &mut *store, self.component.clone())?;
+ let write_profile = setup_epoch_handler(self, &mut *store)?;
store.data_mut().write_profile = Some(write_profile);
self.instance.instantiate_async(&mut *store).await
}
@@ -1169,15 +1187,18 @@ impl Drop for EpochThread {
type WriteProfile = Box<dyn FnOnce(StoreContextMut<Host>) + Send>;
-fn setup_epoch_handler(
- cmd: &ServeCommand,
- store: &mut Store<Host>,
- component: Component,
-) -> Result<WriteProfile> {
+fn setup_epoch_handler(state: &HostHandlerState, store: &mut Store<Host>) -> Result<WriteProfile> {
+ let cmd = &state.cmd;
// Profiling Enabled
if let Some(Profile::Guest { interval, path }) = &cmd.run.profile {
#[cfg(feature = "profiling")]
- return setup_guest_profiler(store, path.clone(), *interval, component.clone());
+ {
+ let modules = state
+ .guest_profiler_modules
+ .as_ref()
+ .expect("modules are prepared when guest profiling is enabled");
+ return setup_guest_profiler(store, path.clone(), *interval, modules);
+ }
#[cfg(not(feature = "profiling"))]
{
let _ = (path, interval);
@@ -1199,19 +1220,17 @@ fn setup_guest_profiler(
store: &mut Store<Host>,
path: String,
interval: Duration,
- component: Component,
+ modules: &wasmtime::GuestProfilerModules,
) -> Result<WriteProfile> {
use wasmtime::{AsContext, GuestProfiler, StoreContext, StoreContextMut};
let module_name = "<main>";
- store.data_mut().guest_profiler = Some(Arc::new(GuestProfiler::new_component(
- store.engine(),
+ store.data_mut().guest_profiler = Some(Arc::new(GuestProfiler::new_with_modules(
module_name,
interval,
- component,
- std::iter::empty(),
- )?));
+ modules,
+ )));
fn sample(
mut store: wasmtime::StoreHookState<Host>,