Skip to content

Commit 3b7cb6e

Browse files
authored
Enable jitdump profiling support by default (#1310)
* Enable jitdump profiling support by default This the result of some of the investigation I was doing for #1017. I've done a number of refactorings here which culminated in a number of changes that all amount to what I think should result in jitdump support being enabled by default: * Pass in a list of finished functions instead of just a range to ensure that we're emitting jit dump data for a specific module rather than a whole `CodeMemory` which may have other modules. * Define `ProfilingStrategy` in the `wasmtime` crate to have everything locally-defined * Add support to the C API to enable profiling * Documentation added for profiling with jitdump to the book * Split out supported/unsupported files in `jitdump.rs` to avoid having lots of `#[cfg]`. * Make dependencies optional that are only used for `jitdump`. * Move initialization up-front to `JitDumpAgent::new()` instead of deferring it to the first module. * Pass around `Arc<dyn ProfilingAgent>` instead of `Option<Arc<Mutex<Box<dyn ProfilingAgent>>>>` The `jitdump` Cargo feature is now enabled by default which means that our published binaries, C API artifacts, and crates will support profiling at runtime by default. The support I don't think is fully fleshed out and working but I think it's probably in a good enough spot we can get users playing around with it!
1 parent 0a30fdf commit 3b7cb6e

27 files changed

+483
-320
lines changed

Cargo.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ doc = false
2323

2424
[dependencies]
2525
# Enable all supported architectures by default.
26-
wasmtime = { path = "crates/api" }
26+
wasmtime = { path = "crates/api", default-features = false }
2727
wasmtime-debug = { path = "crates/debug" }
2828
wasmtime-environ = { path = "crates/environ" }
2929
wasmtime-jit = { path = "crates/jit" }
3030
wasmtime-obj = { path = "crates/obj" }
31-
wasmtime-profiling = { path = "crates/profiling" }
3231
wasmtime-wast = { path = "crates/wast" }
3332
wasmtime-wasi = { path = "crates/wasi" }
3433
wasi-common = { path = "crates/wasi-common" }
@@ -72,13 +71,14 @@ members = [
7271
]
7372

7473
[features]
74+
default = ["jitdump", "wasmtime/wat"]
7575
lightbeam = [
7676
"wasmtime-environ/lightbeam",
7777
"wasmtime-jit/lightbeam",
7878
"wasmtime-wast/lightbeam",
7979
"wasmtime/lightbeam",
8080
]
81-
jitdump = ["wasmtime-profiling/jitdump"]
81+
jitdump = ["wasmtime/jitdump"]
8282
test_programs = ["test-programs/test_programs"]
8383

8484
[badges]

crates/api/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ tempfile = "3.1"
4040
maintenance = { status = "actively-developed" }
4141

4242
[features]
43-
default = ['wat']
43+
default = ['wat', 'jitdump']
4444

4545
# Enables experimental support for the lightbeam codegen backend, an alternative
4646
# to cranelift. Requires Nightly Rust currently, and this is not enabled by
4747
# default.
4848
lightbeam = ["wasmtime-jit/lightbeam"]
4949

50+
# Enables support for the `perf` jitdump profiler
51+
jitdump = ["wasmtime-jit/jitdump"]
52+
5053
[[test]]
5154
name = "host-segfault"
5255
harness = false

crates/api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use crate::func::*;
2626
pub use crate::instance::Instance;
2727
pub use crate::module::Module;
2828
pub use crate::r#ref::{AnyRef, HostInfo, HostRef};
29-
pub use crate::runtime::{Config, Engine, OptLevel, Store, Strategy};
29+
pub use crate::runtime::*;
3030
pub use crate::trap::Trap;
3131
pub use crate::types::*;
3232
pub use crate::values::*;

crates/api/src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ impl Module {
367367
&mut store.compiler_mut(),
368368
binary,
369369
store.engine().config().debug_info,
370-
store.engine().config().profiler.as_ref(),
370+
&*store.engine().config().profiler,
371371
)?;
372372

373373
Ok(Module {

crates/api/src/runtime.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use std::cell::RefCell;
33
use std::fmt;
44
use std::path::Path;
55
use std::rc::Rc;
6-
use std::sync::{Arc, Mutex};
6+
use std::sync::Arc;
77
use wasmparser::{OperatorValidatorConfig, ValidatingParserConfig};
88
use wasmtime_environ::settings::{self, Configurable};
99
use wasmtime_environ::CacheConfig;
1010
use wasmtime_jit::{native, CompilationStrategy, Compiler};
11-
use wasmtime_profiling::{JitDumpAgent, ProfilingAgent, ProfilingStrategy};
11+
use wasmtime_profiling::{JitDumpAgent, NullProfilerAgent, ProfilingAgent};
1212

1313
// Runtime Environment
1414

@@ -26,7 +26,7 @@ pub struct Config {
2626
pub(crate) debug_info: bool,
2727
pub(crate) strategy: CompilationStrategy,
2828
pub(crate) cache_config: CacheConfig,
29-
pub(crate) profiler: Option<Arc<Mutex<Box<dyn ProfilingAgent + Send>>>>,
29+
pub(crate) profiler: Arc<dyn ProfilingAgent>,
3030
}
3131

3232
impl Config {
@@ -65,7 +65,7 @@ impl Config {
6565
flags,
6666
strategy: CompilationStrategy::Auto,
6767
cache_config: CacheConfig::new_cache_disabled(),
68-
profiler: None,
68+
profiler: Arc::new(NullProfilerAgent),
6969
}
7070
}
7171

@@ -225,11 +225,9 @@ impl Config {
225225
/// Profiler creation calls the type's default initializer where the purpose is
226226
/// really just to put in place the type used for profiling.
227227
pub fn profiler(&mut self, profile: ProfilingStrategy) -> Result<&mut Self> {
228-
match profile {
229-
ProfilingStrategy::JitDumpProfiler => {
230-
self.profiler = { Some(Arc::new(Mutex::new(Box::new(JitDumpAgent::default())))) }
231-
}
232-
_ => self.profiler = { None },
228+
self.profiler = match profile {
229+
ProfilingStrategy::JitDump => Arc::new(JitDumpAgent::new()?) as Arc<dyn ProfilingAgent>,
230+
ProfilingStrategy::None => Arc::new(NullProfilerAgent),
233231
};
234232
Ok(self)
235233
}
@@ -381,6 +379,17 @@ pub enum OptLevel {
381379
SpeedAndSize,
382380
}
383381

382+
/// Select which profiling technique to support.
383+
#[derive(Debug, Clone, Copy)]
384+
pub enum ProfilingStrategy {
385+
/// No profiler support.
386+
None,
387+
388+
/// Collect profiling info for "jitdump" file format, used with `perf` on
389+
/// Linux.
390+
JitDump,
391+
}
392+
384393
// Engine
385394

386395
/// An `Engine` which is a global context for compilation and management of wasm

crates/c-api/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ test = false
1717
doctest = false
1818

1919
[dependencies]
20-
wasmtime = { path = "../api" }
20+
wasmtime = { path = "../api", default-features = false }
2121
wasi-common = { path = "../wasi-common" }
2222
wasmtime-wasi = { path = "../wasi" }
2323
wat = "1.0"
24+
25+
[features]
26+
default = ['jitdump']
27+
lightbeam = ["wasmtime/lightbeam"]
28+
jitdump = ["wasmtime/jitdump"]

crates/c-api/include/wasmtime.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ enum wasmtime_opt_level_enum { // OptLevel
2525
WASMTIME_OPT_LEVEL_SPEED_AND_SIZE,
2626
};
2727

28+
typedef uint8_t wasmtime_profiling_strategy_t;
29+
enum wasmtime_profiling_strategy_t { // ProfilingStrategy
30+
WASMTIME_PROFILING_STRATEGY_NONE,
31+
WASMTIME_PROFILING_STRATEGY_JITDUMP,
32+
};
33+
2834
#define WASMTIME_CONFIG_PROP(name, ty) \
2935
WASM_API_EXTERN void wasmtime_config_##name##_set(wasm_config_t*, ty);
3036

@@ -37,6 +43,7 @@ WASMTIME_CONFIG_PROP(wasm_multi_value, bool)
3743
WASMTIME_CONFIG_PROP(strategy, wasmtime_strategy_t)
3844
WASMTIME_CONFIG_PROP(cranelift_debug_verifier, bool)
3945
WASMTIME_CONFIG_PROP(cranelift_opt_level, wasmtime_opt_level_t)
46+
WASMTIME_CONFIG_PROP(profiler, wasmtime_profiling_strategy_t)
4047

4148
///////////////////////////////////////////////////////////////////////////////
4249

crates/c-api/src/ext.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use crate::{wasm_byte_vec_t, wasm_config_t, wasm_engine_t};
55
use std::str;
6-
use wasmtime::{OptLevel, Strategy};
6+
use wasmtime::{OptLevel, ProfilingStrategy, Strategy};
77

88
#[repr(u8)]
99
#[derive(Clone)]
@@ -21,6 +21,13 @@ pub enum wasmtime_opt_level_t {
2121
WASMTIME_OPT_LEVEL_SPEED_AND_SIZE,
2222
}
2323

24+
#[repr(u8)]
25+
#[derive(Clone)]
26+
pub enum wasmtime_profiling_strategy_t {
27+
WASMTIME_PROFILING_STRATEGY_NONE,
28+
WASMTIME_PROFILING_STRATEGY_JITDUMP,
29+
}
30+
2431
#[no_mangle]
2532
pub unsafe extern "C" fn wasmtime_config_debug_info_set(c: *mut wasm_config_t, enable: bool) {
2633
(*c).config.debug_info(enable);
@@ -88,6 +95,18 @@ pub unsafe extern "C" fn wasmtime_config_cranelift_opt_level_set(
8895
});
8996
}
9097

98+
#[no_mangle]
99+
pub unsafe extern "C" fn wasmtime_config_profiler_set(
100+
c: *mut wasm_config_t,
101+
strategy: wasmtime_profiling_strategy_t,
102+
) {
103+
use wasmtime_profiling_strategy_t::*;
104+
drop((*c).config.profiler(match strategy {
105+
WASMTIME_PROFILING_STRATEGY_NONE => ProfilingStrategy::None,
106+
WASMTIME_PROFILING_STRATEGY_JITDUMP => ProfilingStrategy::JitDump,
107+
}));
108+
}
109+
91110
#[no_mangle]
92111
pub unsafe extern "C" fn wasmtime_wat2wasm(
93112
_engine: *mut wasm_engine_t,

crates/jit/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ winapi = { version = "0.3.7", features = ["winnt", "impl-default"] }
3434

3535
[features]
3636
lightbeam = ["wasmtime-environ/lightbeam"]
37+
jitdump = ["wasmtime-profiling/jitdump"]
3738

3839
[badges]
3940
maintenance = { status = "actively-developed" }

0 commit comments

Comments
 (0)