wasmtime_profiling/
lib.rs

1use std::error::Error;
2use std::fmt;
3use wasmtime_environ::entity::{EntityRef, PrimaryMap};
4use wasmtime_environ::wasm::DefinedFuncIndex;
5use wasmtime_environ::Module;
6use wasmtime_runtime::VMFunctionBody;
7
8cfg_if::cfg_if! {
9    if #[cfg(all(feature = "jitdump", target_os = "linux"))] {
10        #[path = "jitdump_linux.rs"]
11        mod jitdump;
12    } else {
13        #[path = "jitdump_disabled.rs"]
14        mod jitdump;
15    }
16}
17
18cfg_if::cfg_if! {
19    if #[cfg(all(feature = "vtune", target_os = "linux"))] {
20        #[path = "vtune_linux.rs"]
21        mod vtune;
22    } else {
23        #[path = "vtune_disabled.rs"]
24        mod vtune;
25    }
26}
27
28pub use crate::jitdump::JitDumpAgent;
29pub use crate::vtune::VTuneAgent;
30
31/// Common interface for profiling tools.
32pub trait ProfilingAgent: Send + Sync + 'static {
33    /// Notify the profiler of a new module loaded into memory
34    fn module_load(
35        &self,
36        module: &Module,
37        functions: &PrimaryMap<DefinedFuncIndex, *mut [VMFunctionBody]>,
38        dbg_image: Option<&[u8]>,
39    ) -> ();
40}
41
42/// Default agent for unsupported profiling build.
43#[derive(Debug, Default, Clone, Copy)]
44pub struct NullProfilerAgent;
45
46#[derive(Debug)]
47struct NullProfilerAgentError;
48
49impl fmt::Display for NullProfilerAgentError {
50    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51        write!(f, "A profiler agent is not supported by this build")
52    }
53}
54
55// This is important for other errors to wrap this one.
56impl Error for NullProfilerAgentError {
57    fn source(&self) -> Option<&(dyn Error + 'static)> {
58        // Generic error, underlying cause isn't tracked.
59        None
60    }
61}
62
63impl ProfilingAgent for NullProfilerAgent {
64    fn module_load(
65        &self,
66        _module: &Module,
67        _functions: &PrimaryMap<DefinedFuncIndex, *mut [VMFunctionBody]>,
68        _dbg_image: Option<&[u8]>,
69    ) -> () {
70    }
71}
72
73#[allow(dead_code)]
74fn debug_name(module: &Module, index: DefinedFuncIndex) -> String {
75    let index = module.func_index(index);
76    match module.func_names.get(&index) {
77        Some(s) => s.clone(),
78        None => format!("wasm::wasm-function[{}]", index.index()),
79    }
80}