use crate::prelude::*;
use crate::profiling_agent::ProfilingAgent;
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, code: &[u8]) {
self.vtune
.load_method(
MethodLoadBuilder::new(method_name.to_owned(), code.as_ptr(), code.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, code: &[u8]) {
self.state.lock().unwrap().register_function(name, code);
}
}
impl State {
fn register_function(&mut self, name: &str, code: &[u8]) {
self.notify_code("wasmtime", name, code);
}
}