Path: blob/main/crates/c-api/src/profiling.rs
1692 views
use crate::{wasm_byte_vec_t, wasm_name_t, wasmtime_error_t, wasmtime_module_t, wasmtime_store_t};1use std::slice;2use std::str::from_utf8;3use std::time::Duration;4use wasmtime::GuestProfiler;56pub struct wasmtime_guestprofiler_t {7guest_profiler: GuestProfiler,8}910wasmtime_c_api_macros::declare_own!(wasmtime_guestprofiler_t);1112#[repr(C)]13pub struct wasmtime_guestprofiler_modules_t<'a> {14name: &'a wasm_name_t,15module: &'a wasmtime_module_t,16}1718#[unsafe(no_mangle)]19pub unsafe extern "C" fn wasmtime_guestprofiler_new(20module_name: &wasm_name_t,21interval_nanos: u64,22modules: *const wasmtime_guestprofiler_modules_t,23modules_len: usize,24) -> Box<wasmtime_guestprofiler_t> {25let module_name = from_utf8(&module_name.as_slice()).expect("not valid utf-8");26let list = slice::from_raw_parts(modules, modules_len)27.iter()28.map(|entry| {29(30from_utf8(entry.name.as_slice())31.expect("not valid utf-8")32.to_owned(),33entry.module.module.clone(),34)35})36.collect::<Vec<_>>();37Box::new(wasmtime_guestprofiler_t {38guest_profiler: GuestProfiler::new(module_name, Duration::from_nanos(interval_nanos), list),39})40}4142#[unsafe(no_mangle)]43pub extern "C" fn wasmtime_guestprofiler_sample(44guestprofiler: &mut wasmtime_guestprofiler_t,45store: &wasmtime_store_t,46delta_nanos: u64,47) {48guestprofiler49.guest_profiler50.sample(&store.store, Duration::from_nanos(delta_nanos));51}5253#[unsafe(no_mangle)]54pub extern "C" fn wasmtime_guestprofiler_finish(55guestprofiler: Box<wasmtime_guestprofiler_t>,56out: &mut wasm_byte_vec_t,57) -> Option<Box<wasmtime_error_t>> {58let mut buf = vec![];59match guestprofiler.guest_profiler.finish(&mut buf) {60Ok(()) => {61out.set_buffer(buf);62None63}64Err(e) => Some(Box::new(e.into())),65}66}676869