use crate::profiling::ProfilingAgent;
use anyhow::Result;
use ittapi::jit::MethodLoadBuilder;
use std::sync::Mutex;
struct VTuneAgent {
state: Mutex<State>,
}
#[derive(Default)]
struct State {
vtune: ittapi::jit::Jit,
}
pub fn new() -> Result<Box<dyn ProfilingAgent>> {
Ok(Box::new(VTuneAgent {
state: Mutex::new(State {
vtune: Default::default(),
}),
}))
}
impl Drop for VTuneAgent {
fn drop(&mut self) {
self.state.lock().unwrap().event_shutdown();
}
}
impl State {
fn notify_code(&mut self, module_name: &str, method_name: &str, addr: *const u8, len: usize) {
self.vtune
.load_method(
MethodLoadBuilder::new(method_name.to_owned(), addr, len)
.class_file_name(module_name.to_owned())
.source_file_name("<unknown wasm filename>".to_owned()),
)
.unwrap();
}
fn event_shutdown(&mut self) {
let _ = self.vtune.shutdown();
}
}
impl ProfilingAgent for VTuneAgent {
fn register_function(&self, name: &str, addr: *const u8, size: usize) {
self.state
.lock()
.unwrap()
.register_function(name, addr, size);
}
}
impl State {
fn register_function(&mut self, name: &str, addr: *const u8, size: usize) {
self.notify_code("wasmtime", name, addr, size);
}
}