Skip to content

Commit

Permalink
Adds perf jitdump support
Browse files Browse the repository at this point in the history
Patch adds support for the perf jitdump file specification.
With this patch it should be possible to see profile data for code
generated and maped at runtime. Specifically the patch adds support
for the JIT_CODE_LOAD and the JIT_DEBUG_INFO record as described in
the specification. Dumping jitfiles is enabled with the --jitdump
flag. When the -g flag is also used there is an attempt to dump file
and line number information where this option would be most useful
when the WASM file already includes DWARF debug information.
  • Loading branch information
jlb6740 committed Oct 28, 2019
1 parent faee3b4 commit 832220c
Show file tree
Hide file tree
Showing 17 changed files with 818 additions and 14 deletions.
1 change: 1 addition & 0 deletions misc/wasmtime-py/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ pub fn into_instance_from_obj(
&data_initializers,
signatures.into_boxed_slice(),
None,
None,
Box::new(import_obj_state),
)
.expect("instance"))
Expand Down
10 changes: 8 additions & 2 deletions src/bin/wasmtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ including calling the start function if one is present. Additional functions
given with --invoke are then called.
Usage:
wasmtime [-odg] [--enable-simd] [--wasi-c] [--disable-cache | --cache-config=<cache_config_file>] [--preload=<wasm>...] [--env=<env>...] [--dir=<dir>...] [--mapdir=<mapping>...] [--lightbeam | --cranelift] <file> [<arg>...]
wasmtime [-odg] [--enable-simd] [--wasi-c] [--disable-cache | --cache-config=<cache_config_file>] [--env=<env>...] [--dir=<dir>...] [--mapdir=<mapping>...] --invoke=<fn> [--lightbeam | --cranelift] <file> [<arg>...]
wasmtime [-odg] [--enable-simd] [--jitdump] [--wasi-c] [--disable-cache | --cache-config=<cache_config_file>] [--preload=<wasm>...] [--env=<env>...] [--dir=<dir>...] [--mapdir=<mapping>...] [--lightbeam | --cranelift] <file> [<arg>...]
wasmtime [-odg] [--enable-simd] [--jitdump] [--wasi-c] [--disable-cache | --cache-config=<cache_config_file>] [--env=<env>...] [--dir=<dir>...] [--mapdir=<mapping>...] --invoke=<fn> [--lightbeam | --cranelift] <file> [<arg>...]
wasmtime --create-cache-config [--cache-config=<cache_config_file>]
wasmtime --help | --version
Expand All @@ -83,6 +83,7 @@ Options:
--lightbeam use Lightbeam for all compilation
--cranelift use Cranelift for all compilation
--enable-simd enable proposed SIMD instructions
--jitdump generate perf jitdump files for runtime generated code
--wasi-c enable the wasi-c implementation of WASI
--preload=<wasm> load an additional wasm module before loading the main module
--env=<env> pass an environment variable (\"key=value\") to the program
Expand All @@ -106,6 +107,7 @@ struct Args {
flag_enable_simd: bool,
flag_lightbeam: bool,
flag_cranelift: bool,
flag_jitdump: bool,
flag_invoke: Option<String>,
flag_preload: Vec<String>,
flag_env: Vec<String>,
Expand Down Expand Up @@ -267,6 +269,9 @@ fn rmain() -> Result<(), Error> {
features.simd = true;
}

// Enable Jitdump if requested
let perf_profile = args.flag_jitdump;

// Enable optimization if requested.
if args.flag_optimize {
flag_builder.set("opt_level", "speed")?;
Expand All @@ -280,6 +285,7 @@ fn rmain() -> Result<(), Error> {
features,
debug_info,
strategy,
perf_profile,
);
let engine = HostRef::new(Engine::new(config));
let store = HostRef::new(Store::new(engine));
Expand Down
9 changes: 8 additions & 1 deletion tests/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ fn test_environ_translate() {
let mut resolver = NullResolver {};
let mut compiler = Compiler::new(isa, CompilationStrategy::Auto);
let global_exports = Rc::new(RefCell::new(HashMap::new()));
let instance = instantiate(&mut compiler, &data, &mut resolver, global_exports, false);
let instance = instantiate(
&mut compiler,
&data,
&mut resolver,
global_exports,
false,
false,
);
assert!(instance.is_ok());
}
16 changes: 14 additions & 2 deletions wasmtime-api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ pub struct Context {
compiler: Rc<RefCell<Compiler>>,
features: Features,
debug_info: bool,
perf_profile: bool,
}

impl Context {
pub fn new(compiler: Compiler, features: Features, debug_info: bool) -> Context {
pub fn new(
compiler: Compiler,
features: Features,
debug_info: bool,
perf_profile: bool,
) -> Context {
Context {
compiler: Rc::new(RefCell::new(compiler)),
features,
debug_info,
perf_profile,
}
}

Expand All @@ -27,14 +34,19 @@ impl Context {
features: Features,
debug_info: bool,
strategy: CompilationStrategy,
perf_profile: bool,
) -> Context {
Context::new(create_compiler(flags, strategy), features, debug_info)
Context::new(create_compiler(flags, strategy), features, debug_info, perf_profile)
}

pub(crate) fn debug_info(&self) -> bool {
self.debug_info
}

pub(crate) fn perf_profile(&self) -> bool {
self.perf_profile
}

pub(crate) fn compiler(&mut self) -> RefMut<Compiler> {
self.compiler.borrow_mut()
}
Expand Down
2 changes: 2 additions & 0 deletions wasmtime-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ pub fn instantiate_in_context(
) -> Result<(InstanceHandle, HashSet<Context>), Error> {
let mut contexts = HashSet::new();
let debug_info = context.debug_info();
let perf_profile = context.perf_profile();
let mut resolver = SimpleResolver { imports };
let instance = instantiate(
&mut context.compiler(),
data,
&mut resolver,
exports,
debug_info,
perf_profile,
)?;
contexts.insert(context);
Ok((instance, contexts))
Expand Down
11 changes: 10 additions & 1 deletion wasmtime-api/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ pub struct Config {
features: Features,
debug_info: bool,
strategy: CompilationStrategy,
perf_profile: bool,
}

impl Config {
pub fn default() -> Config {
Config {
debug_info: false,
perf_profile: false,
features: Default::default(),
flags: default_flags(),
strategy: CompilationStrategy::Auto,
Expand All @@ -41,19 +43,25 @@ impl Config {
features: Features,
debug_info: bool,
strategy: CompilationStrategy,
perf_profile: bool,
) -> Config {
Config {
flags,
features,
debug_info,
strategy,
perf_profile,
}
}

pub(crate) fn debug_info(&self) -> bool {
self.debug_info
}

pub(crate) fn perf_profile(&self) -> bool {
self.perf_profile
}

pub(crate) fn flags(&self) -> &settings::Flags {
&self.flags
}
Expand Down Expand Up @@ -107,9 +115,10 @@ impl Store {
let features = engine.borrow().config().features().clone();
let debug_info = engine.borrow().config().debug_info();
let strategy = engine.borrow().config().strategy();
let perf_profile = engine.borrow().config().perf_profile();
Store {
engine,
context: Context::create(flags, features, debug_info, strategy),
context: Context::create(flags, features, debug_info, strategy, perf_profile),
global_exports: Rc::new(RefCell::new(HashMap::new())),
signature_cache: HashMap::new(),
}
Expand Down
1 change: 1 addition & 0 deletions wasmtime-api/src/trampoline/create_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub(crate) fn create_handle(
&data_initializers,
signatures.into_boxed_slice(),
None,
None,
state,
)
.expect("instance"))
Expand Down
19 changes: 18 additions & 1 deletion wasmtime-jit/src/code_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::string::String;
use alloc::vec::Vec;
use core::{cmp, mem};
use region;
use wasmtime_runtime::{Mmap, VMFunctionBody};
use wasmtime_runtime::{JitDumpAgent, Mmap, VMFunctionBody};

/// Memory manager for executable code.
pub struct CodeMemory {
Expand Down Expand Up @@ -99,4 +99,21 @@ impl CodeMemory {
}
self.published = self.mmaps.len();
}

/// Clones jit_dump_agent and calls code for writing the jitdump record for a
/// newly loaded module.
pub fn perf_module_load(
&mut self,
module_name: &str,
jit_dump_agent: &JitDumpAgent,
dbg_image: Option<&[u8]>,
) -> () {
for map in &mut self.mmaps {
if map.len() > 0 {
jit_dump_agent
.clone()
.module_load(module_name, map.as_ptr(), map.len(), dbg_image);
}
}
}
}
14 changes: 12 additions & 2 deletions wasmtime-jit/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use wasmtime_environ::{
Relocations, Traps, Tunables, VMOffsets,
};
use wasmtime_runtime::{
get_mut_trap_registry, InstantiationError, SignatureRegistry, TrapRegistrationGuard,
VMFunctionBody,
get_mut_trap_registry, InstantiationError, JitDumpAgent, SignatureRegistry,
TrapRegistrationGuard, VMFunctionBody,
};

/// Select which kind of compilation to use.
Expand Down Expand Up @@ -236,6 +236,16 @@ impl Compiler {
self.code_memory.publish();
}

pub(crate) fn perf_module_load(
&mut self,
module_name: &str,
jit_dump_agent: &JitDumpAgent,
dbg_image: Option<&[u8]>,
) -> () {
self.code_memory
.perf_module_load(module_name, jit_dump_agent, dbg_image);
}

/// Shared signature registry.
pub fn signatures(&mut self) -> &mut SignatureRegistry {
&mut self.signatures
Expand Down
11 changes: 11 additions & 0 deletions wasmtime-jit/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub struct Context {
compiler: Box<Compiler>,
global_exports: Rc<RefCell<HashMap<String, Option<wasmtime_runtime::Export>>>>,
debug_info: bool,
perf_profile: bool,
features: Features,
}

Expand All @@ -88,6 +89,7 @@ impl Context {
compiler,
global_exports: Rc::new(RefCell::new(HashMap::new())),
debug_info: false,
perf_profile: false,
features: Default::default(),
}
}
Expand All @@ -97,6 +99,11 @@ impl Context {
self.debug_info
}

/// Get debug_info settings.
pub fn perf_profile(&self) -> bool {
self.perf_profile
}

/// Set debug_info settings.
pub fn set_debug_info(&mut self, value: bool) {
self.debug_info = value;
Expand Down Expand Up @@ -127,13 +134,15 @@ impl Context {
fn instantiate(&mut self, data: &[u8]) -> Result<InstanceHandle, SetupError> {
self.validate(&data).map_err(SetupError::Validate)?;
let debug_info = self.debug_info();
let perf_profile = self.perf_profile();

instantiate(
&mut *self.compiler,
&data,
&mut self.namespace,
Rc::clone(&self.global_exports),
debug_info,
perf_profile,
)
}

Expand Down Expand Up @@ -164,13 +173,15 @@ impl Context {
pub fn compile_module(&mut self, data: &[u8]) -> Result<CompiledModule, SetupError> {
self.validate(&data).map_err(SetupError::Validate)?;
let debug_info = self.debug_info();
let perf_profile = self.perf_profile();

CompiledModule::new(
&mut *self.compiler,
data,
&mut self.namespace,
Rc::clone(&self.global_exports),
debug_info,
perf_profile,
)
}

Expand Down
Loading

0 comments on commit 832220c

Please sign in to comment.