From 2606bc248ca89c27fffef2c961dbb504f9af0726 Mon Sep 17 00:00:00 2001 From: Artur Jamro Date: Mon, 22 Jul 2019 16:22:56 -0700 Subject: [PATCH 1/2] Properly initialize file_per_thread_logger for rayon thread pool --- Cargo.toml | 1 + src/utils.rs | 28 ++++++++++++++++++++++++++++ src/wasm2obj.rs | 12 ++++++++++++ src/wasmtime.rs | 5 +++-- src/wast.rs | 5 +++-- wasmtime-environ/Cargo.toml | 2 +- 6 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 src/utils.rs diff --git a/Cargo.toml b/Cargo.toml index 9161e567d90d..aba63238d698 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ file-per-thread-logger = "0.1.1" wabt = "0.7" libc = "0.2.50" errno = "0.2.4" +rayon = "1.1" [workspace] diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 000000000000..c481611eca50 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,28 @@ +pub fn init_file_per_thread_logger() { + // There's a problem with borrow checker and static lifetimes across lambdas running + // in different threads, so we just use the global instead of function argument. + use super::LOG_FILENAME_PREFIX; + + file_per_thread_logger::initialize(LOG_FILENAME_PREFIX); + // Extending behavior of default spawner: + // https://docs.rs/rayon/1.1.0/rayon/struct.ThreadPoolBuilder.html#method.spawn_handler + // Source code says DefaultSpawner is implementation detail and + // shouldn't be used directly. + rayon::ThreadPoolBuilder::new() + .spawn_handler(|thread| { + let mut b = std::thread::Builder::new(); + if let Some(name) = thread.name() { + b = b.name(name.to_owned()); + } + if let Some(stack_size) = thread.stack_size() { + b = b.stack_size(stack_size); + } + b.spawn(|| { + file_per_thread_logger::initialize(LOG_FILENAME_PREFIX); + thread.run() + })?; + Ok(()) + }) + .build_global() + .unwrap(); +} diff --git a/src/wasm2obj.rs b/src/wasm2obj.rs index 4edb94982e17..4da53366660f 100644 --- a/src/wasm2obj.rs +++ b/src/wasm2obj.rs @@ -52,6 +52,10 @@ use wasmtime_debug::{emit_debugsections, read_debuginfo}; use wasmtime_environ::{Compiler, Cranelift, ModuleEnvironment, Tunables}; use wasmtime_obj::emit_module; +mod utils; + +static LOG_FILENAME_PREFIX: &str = "wasm2obj.dbg."; + const USAGE: &str = " Wasm to native object translation utility. Takes a binary WebAssembly module into a native object file. @@ -68,6 +72,7 @@ Options: --target build for the target triple; default is the host machine -g generate debug information --version print the Cranelift version + -d, --debug enable debug output on stderr/stdout "; #[derive(Deserialize, Debug, Clone)] @@ -76,6 +81,7 @@ struct Args { arg_output: String, arg_target: Option, flag_g: bool, + flag_debug: bool, } fn read_wasm_file(path: PathBuf) -> Result, io::Error> { @@ -95,6 +101,12 @@ fn main() { }) .unwrap_or_else(|e| e.exit()); + if args.flag_debug { + pretty_env_logger::init(); + } else { + utils::init_file_per_thread_logger(); + } + let path = Path::new(&args.arg_file); match handle_module( path.to_path_buf(), diff --git a/src/wasmtime.rs b/src/wasmtime.rs index d949cba1adb0..b9c368e0cdca 100644 --- a/src/wasmtime.rs +++ b/src/wasmtime.rs @@ -37,7 +37,6 @@ use cranelift_codegen::settings; use cranelift_codegen::settings::Configurable; use cranelift_native; use docopt::Docopt; -use file_per_thread_logger; use pretty_env_logger; use std::error::Error; use std::ffi::OsStr; @@ -56,6 +55,8 @@ use wasmtime_wast::instantiate_spectest; #[cfg(feature = "wasi-c")] use wasmtime_wasi_c::instantiate_wasi_c; +mod utils; + static LOG_FILENAME_PREFIX: &str = "wasmtime.dbg."; const USAGE: &str = " @@ -203,7 +204,7 @@ fn main() { if args.flag_debug { pretty_env_logger::init(); } else { - file_per_thread_logger::initialize(LOG_FILENAME_PREFIX); + utils::init_file_per_thread_logger(); } let isa_builder = cranelift_native::builder().unwrap_or_else(|_| { diff --git a/src/wast.rs b/src/wast.rs index 7e87bc189fad..527668ae7d84 100644 --- a/src/wast.rs +++ b/src/wast.rs @@ -32,13 +32,14 @@ use cranelift_codegen::settings; use cranelift_codegen::settings::Configurable; use cranelift_native; use docopt::Docopt; -use file_per_thread_logger; use pretty_env_logger; use std::path::Path; use std::process; use wasmtime_jit::Compiler; use wasmtime_wast::WastContext; +mod utils; + static LOG_FILENAME_PREFIX: &str = "cranelift.dbg."; const USAGE: &str = " @@ -76,7 +77,7 @@ fn main() { if args.flag_debug { pretty_env_logger::init(); } else { - file_per_thread_logger::initialize(LOG_FILENAME_PREFIX); + utils::init_file_per_thread_logger(); } let isa_builder = cranelift_native::builder().unwrap_or_else(|_| { diff --git a/wasmtime-environ/Cargo.toml b/wasmtime-environ/Cargo.toml index ba7347aaaf05..556c2700d921 100644 --- a/wasmtime-environ/Cargo.toml +++ b/wasmtime-environ/Cargo.toml @@ -19,7 +19,7 @@ lightbeam = { path = "../lightbeam", optional = true } failure = { version = "0.1.3", default-features = false } failure_derive = { version = "0.1.3", default-features = false } indexmap = "1.0.2" -rayon = "1.0" +rayon = "1.1" [features] default = ["std"] From 352ecb6533c13e818642ab3d47f070d840ee1508 Mon Sep 17 00:00:00 2001 From: Artur Jamro Date: Tue, 23 Jul 2019 14:49:32 -0700 Subject: [PATCH 2/2] Apply review changes --- src/utils.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index c481611eca50..b6804d27e14d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,9 +1,8 @@ pub fn init_file_per_thread_logger() { - // There's a problem with borrow checker and static lifetimes across lambdas running - // in different threads, so we just use the global instead of function argument. use super::LOG_FILENAME_PREFIX; file_per_thread_logger::initialize(LOG_FILENAME_PREFIX); + // Extending behavior of default spawner: // https://docs.rs/rayon/1.1.0/rayon/struct.ThreadPoolBuilder.html#method.spawn_handler // Source code says DefaultSpawner is implementation detail and