From 794841b366e31946160f940190b6fe9bf407d019 Mon Sep 17 00:00:00 2001 From: Artur Jamro Date: Tue, 23 Jul 2019 14:53:48 -0700 Subject: [PATCH] Properly initialize file_per_thread_logger for rayon thread pool (#211) * Properly initialize file_per_thread_logger for rayon thread pool --- Cargo.toml | 1 + src/utils.rs | 27 +++++++++++++++++++++++++++ src/wasm2obj.rs | 12 ++++++++++++ src/wasmtime.rs | 5 +++-- src/wast.rs | 5 +++-- wasmtime-environ/Cargo.toml | 2 +- 6 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/utils.rs diff --git a/Cargo.toml b/Cargo.toml index ac8d6c9150..6f71567511 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ file-per-thread-logger = "0.1.1" wabt = "0.8" 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 0000000000..b6804d27e1 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,27 @@ +pub fn init_file_per_thread_logger() { + 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 4edb94982e..4da5336666 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 d949cba1ad..b9c368e0cd 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 7e87bc189f..527668ae7d 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 ba7347aaaf..556c2700d9 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"]