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 Sep 26, 2019
1 parent f6f2995 commit 74e4be0
Show file tree
Hide file tree
Showing 18 changed files with 830 additions and 17 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 @@ -326,6 +326,7 @@ pub fn into_instance_from_obj(
&data_initializers,
signatures.into_boxed_slice(),
None,
None,
Box::new(import_obj_state),
)
.expect("instance"))
Expand Down
16 changes: 13 additions & 3 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>...] <file> [<arg>...]
wasmtime [-odg] [--enable-simd] [--wasi-c] [--disable-cache | --cache-config=<cache_config_file>] [--env=<env>...] [--dir=<dir>...] [--mapdir=<mapping>...] --invoke=<fn> <file> [<arg>...]
wasmtime [-odg] [--enable-simd] [--jitdump] [--wasi-c] [--disable-cache | --cache-config=<cache_config_file>] [--preload=<wasm>...] [--env=<env>...] [--dir=<dir>...] [--mapdir=<mapping>...] <file> [<arg>...]
wasmtime [-odg] [--enable-simd] [--jitdump] [--wasi-c] [--disable-cache | --cache-config=<cache_config_file>] [--env=<env>...] [--dir=<dir>...] [--mapdir=<mapping>...] --invoke=<fn> <file> [<arg>...]
wasmtime --create-cache-config [--cache-config=<cache_config_file>]
wasmtime --help | --version
Expand All @@ -81,6 +81,7 @@ Options:
-g generate debug information
-d, --debug enable debug output on stderr/stdout
--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 @@ -102,6 +103,7 @@ struct Args {
flag_debug: bool,
flag_g: bool,
flag_enable_simd: bool,
flag_jitdump: bool,
flag_invoke: Option<String>,
flag_preload: Vec<String>,
flag_env: Vec<String>,
Expand Down Expand Up @@ -272,12 +274,20 @@ 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", "best")?;
}

let config = Config::new(settings::Flags::new(flag_builder), features, debug_info);
let config = Config::new(
settings::Flags::new(flag_builder),
features,
debug_info,
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 @@ -42,6 +42,13 @@ fn test_environ_translate() {
let mut resolver = NullResolver {};
let mut compiler = Compiler::new(isa);
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());
}
22 changes: 19 additions & 3 deletions wasmtime-api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,41 @@ 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,
}
}

pub fn create(flags: settings::Flags, features: Features, debug_info: bool) -> Context {
Context::new(create_compiler(flags), features, debug_info)
pub fn create(
flags: settings::Flags,
features: Features,
debug_info: bool,
perf_profile: bool,
) -> Context {
Context::new(create_compiler(flags), 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 @@ -33,13 +33,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
17 changes: 15 additions & 2 deletions wasmtime-api/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,41 @@ pub struct Config {
flags: settings::Flags,
features: Features,
debug_info: bool,
perf_profile: bool,
}

impl Config {
pub fn default() -> Config {
Config {
debug_info: false,
perf_profile: false,
features: Default::default(),
flags: default_flags(),
}
}

pub fn new(flags: settings::Flags, features: Features, debug_info: bool) -> Config {
pub fn new(
flags: settings::Flags,
features: Features,
debug_info: bool,
perf_profile: bool,
) -> Config {
Config {
flags,
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 flags(&self) -> &settings::Flags {
&self.flags
}
Expand Down Expand Up @@ -92,9 +104,10 @@ impl Store {
let flags = engine.borrow().config().flags().clone();
let features = engine.borrow().config().features().clone();
let debug_info = engine.borrow().config().debug_info();
let perf_profile = engine.borrow().config().perf_profile();
Store {
engine,
context: Context::create(flags, features, debug_info),
context: Context::create(flags, features, debug_info, 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 @@ -53,6 +53,7 @@ pub(crate) fn create_handle(
&data_initializers,
signatures.into_boxed_slice(),
None,
None,
state,
)
.expect("instance"))
Expand Down
2 changes: 2 additions & 0 deletions wasmtime-jit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ failure_derive = { version = "0.1.3", default-features = false }
target-lexicon = { version = "0.8.1", default-features = false }
hashbrown = { version = "0.6.0", optional = true }
wasmparser = "0.36.0"
gimli = "0.19.0"
object = "0.14.0"

[features]
default = ["std"]
Expand Down
17 changes: 16 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 region;
use std::boxed::Box;
use std::string::String;
use std::vec::Vec;
use wasmtime_runtime::{Mmap, VMFunctionBody};
use wasmtime_runtime::{JitDumpAgent, Mmap, VMFunctionBody};

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

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,
};

/// A WebAssembly code JIT compiler.
Expand Down Expand Up @@ -207,6 +207,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
9 changes: 9 additions & 0 deletions wasmtime-jit/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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 @@ -89,6 +90,7 @@ impl Context {
compiler,
global_exports: Rc::new(RefCell::new(HashMap::new())),
debug_info: false,
perf_profile: false,
features: Default::default(),
}
}
Expand All @@ -98,6 +100,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
Loading

0 comments on commit 74e4be0

Please sign in to comment.