-
Notifications
You must be signed in to change notification settings - Fork 1.7k
expose eager thread-local resource initialization on Engine #2946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ff87f45
expose eager thread-local initialization by the Engine
pchickey 1136917
golf
pchickey bf1d1a4
add test for eager thread initialization
pchickey 0a96b6b
overhead is on calls, not instantiation
pchickey 2a4c51b
switch eager vs lazy instantiation to a criterion bench
pchickey 035d541
add docs
pchickey 613309b
missing docs
pchickey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| use criterion::{criterion_group, criterion_main, Criterion}; | ||
| use std::thread; | ||
| use std::time::{Duration, Instant}; | ||
| use wasmtime::*; | ||
|
|
||
| fn measure_execution_time(c: &mut Criterion) { | ||
| // Baseline performance: a single measurment covers both initializing | ||
| // thread local resources and executing the first call. | ||
| // | ||
| // The other two bench functions should sum to this duration. | ||
| c.bench_function("lazy initialization at call", move |b| { | ||
| let (engine, module) = test_setup(); | ||
| b.iter_custom(move |iters| { | ||
| (0..iters) | ||
| .into_iter() | ||
| .map(|_| lazy_thread_instantiate(engine.clone(), module.clone())) | ||
| .sum() | ||
| }) | ||
| }); | ||
|
|
||
| // Using Engine::tls_eager_initialize: measure how long eager | ||
| // initialization takes on a new thread. | ||
| c.bench_function("eager initialization", move |b| { | ||
| let (engine, module) = test_setup(); | ||
| b.iter_custom(move |iters| { | ||
| (0..iters) | ||
| .into_iter() | ||
| .map(|_| { | ||
| let (init, _call) = eager_thread_instantiate(engine.clone(), module.clone()); | ||
| init | ||
| }) | ||
| .sum() | ||
| }) | ||
| }); | ||
|
|
||
| // Measure how long the first call takes on a thread after it has been | ||
| // eagerly initialized. | ||
| c.bench_function("call after eager initialization", move |b| { | ||
| let (engine, module) = test_setup(); | ||
| b.iter_custom(move |iters| { | ||
| (0..iters) | ||
| .into_iter() | ||
| .map(|_| { | ||
| let (_init, call) = eager_thread_instantiate(engine.clone(), module.clone()); | ||
| call | ||
| }) | ||
| .sum() | ||
| }) | ||
| }); | ||
| } | ||
|
|
||
| /// Creating a store and measuring the time to perform a call is the same behavior | ||
| /// in both setups. | ||
| fn duration_of_call(engine: &Engine, module: &Module) -> Duration { | ||
| let mut store = Store::new(engine, ()); | ||
| let inst = Instance::new(&mut store, module, &[]).expect("instantiate"); | ||
| let f = inst.get_func(&mut store, "f").expect("get f"); | ||
| let f = f.typed::<(), (), _>(&store).expect("type f"); | ||
|
|
||
| let call = Instant::now(); | ||
| f.call(&mut store, ()).expect("call f"); | ||
| call.elapsed() | ||
| } | ||
|
|
||
| /// When wasmtime first runs a function on a thread, it needs to initialize | ||
| /// some thread-local resources and install signal handlers. This benchmark | ||
| /// spawns a new thread, and returns the duration it took to execute the first | ||
| /// function call made on that thread. | ||
| fn lazy_thread_instantiate(engine: Engine, module: Module) -> Duration { | ||
| thread::spawn(move || duration_of_call(&engine, &module)) | ||
| .join() | ||
| .expect("thread joins") | ||
| } | ||
| /// This benchmark spawns a new thread, and records the duration to eagerly | ||
| /// initializes the thread local resources. It then creates a store and | ||
| /// instance, and records the duration it took to execute the first function | ||
| /// call. | ||
| fn eager_thread_instantiate(engine: Engine, module: Module) -> (Duration, Duration) { | ||
| thread::spawn(move || { | ||
| let init_start = Instant::now(); | ||
| Engine::tls_eager_initialize().expect("eager init"); | ||
| let init_duration = init_start.elapsed(); | ||
|
|
||
| (init_duration, duration_of_call(&engine, &module)) | ||
| }) | ||
| .join() | ||
| .expect("thread joins") | ||
| } | ||
|
|
||
| fn test_setup() -> (Engine, Module) { | ||
| // We only expect to create one Instance at a time, with a single memory. | ||
| let pool_count = 10; | ||
|
|
||
| let mut config = Config::new(); | ||
| config.allocation_strategy(InstanceAllocationStrategy::Pooling { | ||
| strategy: PoolingAllocationStrategy::NextAvailable, | ||
| module_limits: ModuleLimits { | ||
| memory_pages: 1, | ||
| ..Default::default() | ||
| }, | ||
| instance_limits: InstanceLimits { | ||
| count: pool_count, | ||
| memory_reservation_size: 1, | ||
| }, | ||
| }); | ||
| let engine = Engine::new(&config).unwrap(); | ||
|
|
||
| // The module has a memory (shouldn't matter) and a single function which is a no-op. | ||
| let module = Module::new(&engine, r#"(module (memory 1) (func (export "f")))"#).unwrap(); | ||
| (engine, module) | ||
| } | ||
|
|
||
| criterion_group!(benches, measure_execution_time); | ||
| criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could probably be a bit simpler with
if initialized { return }and thenp.set((state, true))afterwards