Skip to content

Commit 794841b

Browse files
mrowqasunfishcode
authored andcommitted
Properly initialize file_per_thread_logger for rayon thread pool (#211)
* Properly initialize file_per_thread_logger for rayon thread pool
1 parent 1aff03a commit 794841b

File tree

6 files changed

+47
-5
lines changed

6 files changed

+47
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ file-per-thread-logger = "0.1.1"
4343
wabt = "0.8"
4444
libc = "0.2.50"
4545
errno = "0.2.4"
46+
rayon = "1.1"
4647

4748
[workspace]
4849

src/utils.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
pub fn init_file_per_thread_logger() {
2+
use super::LOG_FILENAME_PREFIX;
3+
4+
file_per_thread_logger::initialize(LOG_FILENAME_PREFIX);
5+
6+
// Extending behavior of default spawner:
7+
// https://docs.rs/rayon/1.1.0/rayon/struct.ThreadPoolBuilder.html#method.spawn_handler
8+
// Source code says DefaultSpawner is implementation detail and
9+
// shouldn't be used directly.
10+
rayon::ThreadPoolBuilder::new()
11+
.spawn_handler(|thread| {
12+
let mut b = std::thread::Builder::new();
13+
if let Some(name) = thread.name() {
14+
b = b.name(name.to_owned());
15+
}
16+
if let Some(stack_size) = thread.stack_size() {
17+
b = b.stack_size(stack_size);
18+
}
19+
b.spawn(|| {
20+
file_per_thread_logger::initialize(LOG_FILENAME_PREFIX);
21+
thread.run()
22+
})?;
23+
Ok(())
24+
})
25+
.build_global()
26+
.unwrap();
27+
}

src/wasm2obj.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ use wasmtime_debug::{emit_debugsections, read_debuginfo};
5252
use wasmtime_environ::{Compiler, Cranelift, ModuleEnvironment, Tunables};
5353
use wasmtime_obj::emit_module;
5454

55+
mod utils;
56+
57+
static LOG_FILENAME_PREFIX: &str = "wasm2obj.dbg.";
58+
5559
const USAGE: &str = "
5660
Wasm to native object translation utility.
5761
Takes a binary WebAssembly module into a native object file.
@@ -68,6 +72,7 @@ Options:
6872
--target <TARGET> build for the target triple; default is the host machine
6973
-g generate debug information
7074
--version print the Cranelift version
75+
-d, --debug enable debug output on stderr/stdout
7176
";
7277

7378
#[derive(Deserialize, Debug, Clone)]
@@ -76,6 +81,7 @@ struct Args {
7681
arg_output: String,
7782
arg_target: Option<String>,
7883
flag_g: bool,
84+
flag_debug: bool,
7985
}
8086

8187
fn read_wasm_file(path: PathBuf) -> Result<Vec<u8>, io::Error> {
@@ -95,6 +101,12 @@ fn main() {
95101
})
96102
.unwrap_or_else(|e| e.exit());
97103

104+
if args.flag_debug {
105+
pretty_env_logger::init();
106+
} else {
107+
utils::init_file_per_thread_logger();
108+
}
109+
98110
let path = Path::new(&args.arg_file);
99111
match handle_module(
100112
path.to_path_buf(),

src/wasmtime.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use cranelift_codegen::settings;
3737
use cranelift_codegen::settings::Configurable;
3838
use cranelift_native;
3939
use docopt::Docopt;
40-
use file_per_thread_logger;
4140
use pretty_env_logger;
4241
use std::error::Error;
4342
use std::ffi::OsStr;
@@ -56,6 +55,8 @@ use wasmtime_wast::instantiate_spectest;
5655
#[cfg(feature = "wasi-c")]
5756
use wasmtime_wasi_c::instantiate_wasi_c;
5857

58+
mod utils;
59+
5960
static LOG_FILENAME_PREFIX: &str = "wasmtime.dbg.";
6061

6162
const USAGE: &str = "
@@ -203,7 +204,7 @@ fn main() {
203204
if args.flag_debug {
204205
pretty_env_logger::init();
205206
} else {
206-
file_per_thread_logger::initialize(LOG_FILENAME_PREFIX);
207+
utils::init_file_per_thread_logger();
207208
}
208209

209210
let isa_builder = cranelift_native::builder().unwrap_or_else(|_| {

src/wast.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ use cranelift_codegen::settings;
3232
use cranelift_codegen::settings::Configurable;
3333
use cranelift_native;
3434
use docopt::Docopt;
35-
use file_per_thread_logger;
3635
use pretty_env_logger;
3736
use std::path::Path;
3837
use std::process;
3938
use wasmtime_jit::Compiler;
4039
use wasmtime_wast::WastContext;
4140

41+
mod utils;
42+
4243
static LOG_FILENAME_PREFIX: &str = "cranelift.dbg.";
4344

4445
const USAGE: &str = "
@@ -76,7 +77,7 @@ fn main() {
7677
if args.flag_debug {
7778
pretty_env_logger::init();
7879
} else {
79-
file_per_thread_logger::initialize(LOG_FILENAME_PREFIX);
80+
utils::init_file_per_thread_logger();
8081
}
8182

8283
let isa_builder = cranelift_native::builder().unwrap_or_else(|_| {

wasmtime-environ/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ lightbeam = { path = "../lightbeam", optional = true }
1919
failure = { version = "0.1.3", default-features = false }
2020
failure_derive = { version = "0.1.3", default-features = false }
2121
indexmap = "1.0.2"
22-
rayon = "1.0"
22+
rayon = "1.1"
2323

2424
[features]
2525
default = ["std"]

0 commit comments

Comments
 (0)