xref: /wasmtime-44.0.1/crates/bench-api/src/lib.rs (revision 868f0c38)
1 //! A C API for benchmarking Wasmtime's WebAssembly compilation, instantiation,
2 //! and execution.
3 //!
4 //! The API expects calls that match the following state machine:
5 //!
6 //! ```text
7 //!               |
8 //!               |
9 //!               V
10 //! .---> wasm_bench_create
11 //! |        |        |
12 //! |        |        |
13 //! |        |        V
14 //! |        |   wasm_bench_compile
15 //! |        |     |            |
16 //! |        |     |            |     .----.
17 //! |        |     |            |     |    |
18 //! |        |     |            V     V    |
19 //! |        |     |     wasm_bench_instantiate <------.
20 //! |        |     |            |        |             |
21 //! |        |     |            |        |             |
22 //! |        |     |            |        |             |
23 //! |        |     |     .------'        '-----> wasm_bench_execute
24 //! |        |     |     |                             |
25 //! |        |     |     |                             |
26 //! |        V     V     V                             |
27 //! '------ wasm_bench_free <--------------------------'
28 //!               |
29 //!               |
30 //!               V
31 //! ```
32 //!
33 //! All API calls must happen on the same thread.
34 //!
35 //! Functions which return pointers use null as an error value. Function which
36 //! return `int` use `0` as OK and non-zero as an error value.
37 //!
38 //! # Example
39 //!
40 //! ```
41 //! use std::ptr;
42 //! use wasmtime_bench_api::*;
43 //!
44 //! let working_dir = std::env::current_dir().unwrap().display().to_string();
45 //! let stdout_path = "./stdout.log";
46 //! let stderr_path = "./stderr.log";
47 //!
48 //! // Functions to start/end timers for compilation.
49 //! //
50 //! // The `compilation_timer` pointer configured in the `WasmBenchConfig` is
51 //! // passed through.
52 //! extern "C" fn compilation_start(timer: *mut u8) {
53 //!     // Start your compilation timer here.
54 //! }
55 //! extern "C" fn compilation_end(timer: *mut u8) {
56 //!     // End your compilation timer here.
57 //! }
58 //!
59 //! // Similar for instantiation.
60 //! extern "C" fn instantiation_start(timer: *mut u8) {
61 //!     // Start your instantiation timer here.
62 //! }
63 //! extern "C" fn instantiation_end(timer: *mut u8) {
64 //!     // End your instantiation timer here.
65 //! }
66 //!
67 //! // Similar for execution.
68 //! extern "C" fn execution_start(timer: *mut u8) {
69 //!     // Start your execution timer here.
70 //! }
71 //! extern "C" fn execution_end(timer: *mut u8) {
72 //!     // End your execution timer here.
73 //! }
74 //!
75 //! let config = WasmBenchConfig {
76 //!     working_dir_ptr: working_dir.as_ptr(),
77 //!     working_dir_len: working_dir.len(),
78 //!     stdout_path_ptr: stdout_path.as_ptr(),
79 //!     stdout_path_len: stdout_path.len(),
80 //!     stderr_path_ptr: stderr_path.as_ptr(),
81 //!     stderr_path_len: stderr_path.len(),
82 //!     stdin_path_ptr: ptr::null(),
83 //!     stdin_path_len: 0,
84 //!     compilation_timer: ptr::null_mut(),
85 //!     compilation_start,
86 //!     compilation_end,
87 //!     instantiation_timer: ptr::null_mut(),
88 //!     instantiation_start,
89 //!     instantiation_end,
90 //!     execution_timer: ptr::null_mut(),
91 //!     execution_start,
92 //!     execution_end,
93 //!     execution_flags_ptr: ptr::null(),
94 //!     execution_flags_len: 0,
95 //! };
96 //!
97 //! let mut bench_api = ptr::null_mut();
98 //! unsafe {
99 //!     let code = wasm_bench_create(config, &mut bench_api);
100 //!     assert_eq!(code, OK);
101 //!     assert!(!bench_api.is_null());
102 //! };
103 //!
104 //! let wasm = wat::parse_bytes(br#"
105 //!     (module
106 //!         (func $bench_start (import "bench" "start"))
107 //!         (func $bench_end (import "bench" "end"))
108 //!         (func $start (export "_start")
109 //!             call $bench_start
110 //!             i32.const 1
111 //!             i32.const 2
112 //!             i32.add
113 //!             drop
114 //!             call $bench_end
115 //!         )
116 //!     )
117 //! "#).unwrap();
118 //!
119 //! // This will call the `compilation_{start,end}` timing functions on success.
120 //! let code = unsafe { wasm_bench_compile(bench_api, wasm.as_ptr(), wasm.len()) };
121 //! assert_eq!(code, OK);
122 //!
123 //! // This will call the `instantiation_{start,end}` timing functions on success.
124 //! let code = unsafe { wasm_bench_instantiate(bench_api) };
125 //! assert_eq!(code, OK);
126 //!
127 //! // This will call the `execution_{start,end}` timing functions on success.
128 //! let code = unsafe { wasm_bench_execute(bench_api) };
129 //! assert_eq!(code, OK);
130 //!
131 //! unsafe {
132 //!     wasm_bench_free(bench_api);
133 //! }
134 //! ```
135 
136 mod unsafe_send_sync;
137 
138 use crate::unsafe_send_sync::UnsafeSendSync;
139 use anyhow::{Context, Result};
140 use clap::Parser;
141 use std::os::raw::{c_int, c_void};
142 use std::slice;
143 use std::{env, path::PathBuf};
144 use target_lexicon::Triple;
145 use wasmtime::{Engine, Instance, Linker, Module, Store};
146 use wasmtime_cli_flags::{CommonOptions, WasiModules};
147 use wasmtime_wasi::{sync::WasiCtxBuilder, I32Exit, WasiCtx};
148 
149 pub type ExitCode = c_int;
150 pub const OK: ExitCode = 0;
151 pub const ERR: ExitCode = -1;
152 
153 // Randomize the location of heap objects to avoid accidental locality being an
154 // uncontrolled variable that obscures performance evaluation in our
155 // experiments.
156 #[cfg(feature = "shuffling-allocator")]
157 #[global_allocator]
158 static ALLOC: shuffling_allocator::ShufflingAllocator<std::alloc::System> =
159     shuffling_allocator::wrap!(&std::alloc::System);
160 
161 /// Configuration options for the benchmark.
162 #[repr(C)]
163 pub struct WasmBenchConfig {
164     /// The working directory where benchmarks should be executed.
165     pub working_dir_ptr: *const u8,
166     pub working_dir_len: usize,
167 
168     /// The file path that should be created and used as `stdout`.
169     pub stdout_path_ptr: *const u8,
170     pub stdout_path_len: usize,
171 
172     /// The file path that should be created and used as `stderr`.
173     pub stderr_path_ptr: *const u8,
174     pub stderr_path_len: usize,
175 
176     /// The (optional) file path that should be opened and used as `stdin`. If
177     /// not provided, then the WASI context will not have a `stdin` initialized.
178     pub stdin_path_ptr: *const u8,
179     pub stdin_path_len: usize,
180 
181     /// The functions to start and stop performance timers/counters during Wasm
182     /// compilation.
183     pub compilation_timer: *mut u8,
184     pub compilation_start: extern "C" fn(*mut u8),
185     pub compilation_end: extern "C" fn(*mut u8),
186 
187     /// The functions to start and stop performance timers/counters during Wasm
188     /// instantiation.
189     pub instantiation_timer: *mut u8,
190     pub instantiation_start: extern "C" fn(*mut u8),
191     pub instantiation_end: extern "C" fn(*mut u8),
192 
193     /// The functions to start and stop performance timers/counters during Wasm
194     /// execution.
195     pub execution_timer: *mut u8,
196     pub execution_start: extern "C" fn(*mut u8),
197     pub execution_end: extern "C" fn(*mut u8),
198 
199     /// The (optional) flags to use when running Wasmtime. These correspond to
200     /// the flags used when running Wasmtime from the command line.
201     pub execution_flags_ptr: *const u8,
202     pub execution_flags_len: usize,
203 }
204 
205 impl WasmBenchConfig {
206     fn working_dir(&self) -> Result<PathBuf> {
207         let working_dir =
208             unsafe { std::slice::from_raw_parts(self.working_dir_ptr, self.working_dir_len) };
209         let working_dir = std::str::from_utf8(working_dir)
210             .context("given working directory is not valid UTF-8")?;
211         Ok(working_dir.into())
212     }
213 
214     fn stdout_path(&self) -> Result<PathBuf> {
215         let stdout_path =
216             unsafe { std::slice::from_raw_parts(self.stdout_path_ptr, self.stdout_path_len) };
217         let stdout_path =
218             std::str::from_utf8(stdout_path).context("given stdout path is not valid UTF-8")?;
219         Ok(stdout_path.into())
220     }
221 
222     fn stderr_path(&self) -> Result<PathBuf> {
223         let stderr_path =
224             unsafe { std::slice::from_raw_parts(self.stderr_path_ptr, self.stderr_path_len) };
225         let stderr_path =
226             std::str::from_utf8(stderr_path).context("given stderr path is not valid UTF-8")?;
227         Ok(stderr_path.into())
228     }
229 
230     fn stdin_path(&self) -> Result<Option<PathBuf>> {
231         if self.stdin_path_ptr.is_null() {
232             return Ok(None);
233         }
234 
235         let stdin_path =
236             unsafe { std::slice::from_raw_parts(self.stdin_path_ptr, self.stdin_path_len) };
237         let stdin_path =
238             std::str::from_utf8(stdin_path).context("given stdin path is not valid UTF-8")?;
239         Ok(Some(stdin_path.into()))
240     }
241 
242     fn execution_flags(&self) -> Result<CommonOptions> {
243         let flags = if self.execution_flags_ptr.is_null() {
244             ""
245         } else {
246             let execution_flags = unsafe {
247                 std::slice::from_raw_parts(self.execution_flags_ptr, self.execution_flags_len)
248             };
249             std::str::from_utf8(execution_flags)
250                 .context("given execution flags string is not valid UTF-8")?
251         };
252         let options = CommonOptions::try_parse_from(
253             ["wasmtime"]
254                 .into_iter()
255                 .chain(flags.split(' ').filter(|s| !s.is_empty())),
256         )
257         .context("failed to parse options")?;
258         Ok(options)
259     }
260 }
261 
262 /// Exposes a C-compatible way of creating the engine from the bytes of a single
263 /// Wasm module.
264 ///
265 /// On success, the `out_bench_ptr` is initialized to a pointer to a structure
266 /// that contains the engine's initialized state, and `0` is returned. On
267 /// failure, a non-zero status code is returned and `out_bench_ptr` is left
268 /// untouched.
269 #[no_mangle]
270 pub extern "C" fn wasm_bench_create(
271     config: WasmBenchConfig,
272     out_bench_ptr: *mut *mut c_void,
273 ) -> ExitCode {
274     let result = (|| -> Result<_> {
275         let working_dir = config.working_dir()?;
276         let working_dir =
277             cap_std::fs::Dir::open_ambient_dir(&working_dir, cap_std::ambient_authority())
278                 .with_context(|| {
279                     format!(
280                         "failed to preopen the working directory: {}",
281                         working_dir.display(),
282                     )
283                 })?;
284 
285         let stdout_path = config.stdout_path()?;
286         let stderr_path = config.stderr_path()?;
287         let stdin_path = config.stdin_path()?;
288         let options = config.execution_flags()?;
289 
290         let state = Box::new(BenchState::new(
291             options,
292             config.compilation_timer,
293             config.compilation_start,
294             config.compilation_end,
295             config.instantiation_timer,
296             config.instantiation_start,
297             config.instantiation_end,
298             config.execution_timer,
299             config.execution_start,
300             config.execution_end,
301             move || {
302                 let mut cx = WasiCtxBuilder::new();
303 
304                 let stdout = std::fs::File::create(&stdout_path)
305                     .with_context(|| format!("failed to create {}", stdout_path.display()))?;
306                 let stdout = cap_std::fs::File::from_std(stdout);
307                 let stdout = wasi_cap_std_sync::file::File::from_cap_std(stdout);
308                 cx = cx.stdout(Box::new(stdout));
309 
310                 let stderr = std::fs::File::create(&stderr_path)
311                     .with_context(|| format!("failed to create {}", stderr_path.display()))?;
312                 let stderr = cap_std::fs::File::from_std(stderr);
313                 let stderr = wasi_cap_std_sync::file::File::from_cap_std(stderr);
314                 cx = cx.stderr(Box::new(stderr));
315 
316                 if let Some(stdin_path) = &stdin_path {
317                     let stdin = std::fs::File::open(stdin_path)
318                         .with_context(|| format!("failed to open {}", stdin_path.display()))?;
319                     let stdin = cap_std::fs::File::from_std(stdin);
320                     let stdin = wasi_cap_std_sync::file::File::from_cap_std(stdin);
321                     cx = cx.stdin(Box::new(stdin));
322                 }
323 
324                 // Allow access to the working directory so that the benchmark can read
325                 // its input workload(s).
326                 cx = cx.preopened_dir(working_dir.try_clone()?, ".")?;
327 
328                 // Pass this env var along so that the benchmark program can use smaller
329                 // input workload(s) if it has them and that has been requested.
330                 if let Ok(val) = env::var("WASM_BENCH_USE_SMALL_WORKLOAD") {
331                     cx = cx.env("WASM_BENCH_USE_SMALL_WORKLOAD", &val)?;
332                 }
333 
334                 Ok(cx.build())
335             },
336         )?);
337         Ok(Box::into_raw(state) as _)
338     })();
339 
340     if let Ok(bench_ptr) = result {
341         unsafe {
342             assert!(!out_bench_ptr.is_null());
343             *out_bench_ptr = bench_ptr;
344         }
345     }
346 
347     to_exit_code(result.map(|_| ()))
348 }
349 
350 /// Free the engine state allocated by this library.
351 #[no_mangle]
352 pub extern "C" fn wasm_bench_free(state: *mut c_void) {
353     assert!(!state.is_null());
354     unsafe {
355         drop(Box::from_raw(state as *mut BenchState));
356     }
357 }
358 
359 /// Compile the Wasm benchmark module.
360 #[no_mangle]
361 pub extern "C" fn wasm_bench_compile(
362     state: *mut c_void,
363     wasm_bytes: *const u8,
364     wasm_bytes_length: usize,
365 ) -> ExitCode {
366     let state = unsafe { (state as *mut BenchState).as_mut().unwrap() };
367     let wasm_bytes = unsafe { slice::from_raw_parts(wasm_bytes, wasm_bytes_length) };
368     let result = state.compile(wasm_bytes).context("failed to compile");
369     to_exit_code(result)
370 }
371 
372 /// Instantiate the Wasm benchmark module.
373 #[no_mangle]
374 pub extern "C" fn wasm_bench_instantiate(state: *mut c_void) -> ExitCode {
375     let state = unsafe { (state as *mut BenchState).as_mut().unwrap() };
376     let result = state.instantiate().context("failed to instantiate");
377     to_exit_code(result)
378 }
379 
380 /// Execute the Wasm benchmark module.
381 #[no_mangle]
382 pub extern "C" fn wasm_bench_execute(state: *mut c_void) -> ExitCode {
383     let state = unsafe { (state as *mut BenchState).as_mut().unwrap() };
384     let result = state.execute().context("failed to execute");
385     to_exit_code(result)
386 }
387 
388 /// Helper function for converting a Rust result to a C error code.
389 ///
390 /// This will print an error indicating some information regarding the failure.
391 fn to_exit_code<T>(result: impl Into<Result<T>>) -> ExitCode {
392     match result.into() {
393         Ok(_) => OK,
394         Err(error) => {
395             eprintln!("{:?}", error);
396             ERR
397         }
398     }
399 }
400 
401 /// This structure contains the actual Rust implementation of the state required
402 /// to manage the Wasmtime engine between calls.
403 struct BenchState {
404     linker: Linker<HostState>,
405     compilation_timer: *mut u8,
406     compilation_start: extern "C" fn(*mut u8),
407     compilation_end: extern "C" fn(*mut u8),
408     instantiation_timer: *mut u8,
409     instantiation_start: extern "C" fn(*mut u8),
410     instantiation_end: extern "C" fn(*mut u8),
411     make_wasi_cx: Box<dyn FnMut() -> Result<WasiCtx>>,
412     module: Option<Module>,
413     store_and_instance: Option<(Store<HostState>, Instance)>,
414     epoch_interruption: bool,
415     fuel: Option<u64>,
416 }
417 
418 struct HostState {
419     wasi: WasiCtx,
420     #[cfg(feature = "wasi-nn")]
421     wasi_nn: wasmtime_wasi_nn::WasiNnCtx,
422     #[cfg(feature = "wasi-crypto")]
423     wasi_crypto: wasmtime_wasi_crypto::WasiCryptoCtx,
424 }
425 
426 impl BenchState {
427     fn new(
428         options: CommonOptions,
429         compilation_timer: *mut u8,
430         compilation_start: extern "C" fn(*mut u8),
431         compilation_end: extern "C" fn(*mut u8),
432         instantiation_timer: *mut u8,
433         instantiation_start: extern "C" fn(*mut u8),
434         instantiation_end: extern "C" fn(*mut u8),
435         execution_timer: *mut u8,
436         execution_start: extern "C" fn(*mut u8),
437         execution_end: extern "C" fn(*mut u8),
438         make_wasi_cx: impl FnMut() -> Result<WasiCtx> + 'static,
439     ) -> Result<Self> {
440         let mut config = options.config(Some(&Triple::host().to_string()))?;
441         // NB: always disable the compilation cache.
442         config.disable_cache();
443         let engine = Engine::new(&config)?;
444         let mut linker = Linker::<HostState>::new(&engine);
445 
446         // Define the benchmarking start/end functions.
447         let execution_timer = unsafe {
448             // Safe because this bench API's contract requires that its methods
449             // are only ever called from a single thread.
450             UnsafeSendSync::new(execution_timer)
451         };
452         linker.func_wrap("bench", "start", move || {
453             execution_start(*execution_timer.get());
454             Ok(())
455         })?;
456         linker.func_wrap("bench", "end", move || {
457             execution_end(*execution_timer.get());
458             Ok(())
459         })?;
460 
461         let epoch_interruption = options.epoch_interruption;
462         let fuel = options.fuel;
463 
464         let wasi_modules = options.wasi_modules.unwrap_or(WasiModules::default());
465 
466         if wasi_modules.wasi_common {
467             wasmtime_wasi::add_to_linker(&mut linker, |cx| &mut cx.wasi)?;
468         }
469 
470         #[cfg(feature = "wasi-nn")]
471         if wasi_modules.wasi_nn {
472             wasmtime_wasi_nn::add_to_linker(&mut linker, |cx| &mut cx.wasi_nn)?;
473         }
474 
475         #[cfg(feature = "wasi-crypto")]
476         if wasi_modules.wasi_crypto {
477             wasmtime_wasi_crypto::add_to_linker(&mut linker, |cx| &mut cx.wasi_crypto)?;
478         }
479 
480         Ok(Self {
481             linker,
482             compilation_timer,
483             compilation_start,
484             compilation_end,
485             instantiation_timer,
486             instantiation_start,
487             instantiation_end,
488             make_wasi_cx: Box::new(make_wasi_cx) as _,
489             module: None,
490             store_and_instance: None,
491             epoch_interruption,
492             fuel,
493         })
494     }
495 
496     fn compile(&mut self, bytes: &[u8]) -> Result<()> {
497         assert!(
498             self.module.is_none(),
499             "create a new engine to repeat compilation"
500         );
501 
502         (self.compilation_start)(self.compilation_timer);
503         let module = Module::from_binary(self.linker.engine(), bytes)?;
504         (self.compilation_end)(self.compilation_timer);
505 
506         self.module = Some(module);
507         Ok(())
508     }
509 
510     fn instantiate(&mut self) -> Result<()> {
511         let module = self
512             .module
513             .as_ref()
514             .expect("compile the module before instantiating it");
515 
516         let host = HostState {
517             wasi: (self.make_wasi_cx)().context("failed to create a WASI context")?,
518             #[cfg(feature = "wasi-nn")]
519             wasi_nn: wasmtime_wasi_nn::WasiNnCtx::new()?,
520             #[cfg(feature = "wasi-crypto")]
521             wasi_crypto: wasmtime_wasi_crypto::WasiCryptoCtx::new(),
522         };
523 
524         // NB: Start measuring instantiation time *after* we've created the WASI
525         // context, since that needs to do file I/O to setup
526         // stdin/stdout/stderr.
527         (self.instantiation_start)(self.instantiation_timer);
528         let mut store = Store::new(self.linker.engine(), host);
529         if self.epoch_interruption {
530             store.set_epoch_deadline(1);
531         }
532         if let Some(fuel) = self.fuel {
533             store.add_fuel(fuel).unwrap();
534         }
535 
536         let instance = self.linker.instantiate(&mut store, &module)?;
537         (self.instantiation_end)(self.instantiation_timer);
538 
539         self.store_and_instance = Some((store, instance));
540         Ok(())
541     }
542 
543     fn execute(&mut self) -> Result<()> {
544         let (mut store, instance) = self
545             .store_and_instance
546             .take()
547             .expect("instantiate the module before executing it");
548 
549         let start_func = instance.get_typed_func::<(), ()>(&mut store, "_start")?;
550         match start_func.call(&mut store, ()) {
551             Ok(_) => Ok(()),
552             Err(trap) => {
553                 // Since _start will likely return by using the system `exit` call, we must
554                 // check the trap code to see if it actually represents a successful exit.
555                 if let Some(exit) = trap.downcast_ref::<I32Exit>() {
556                     if exit.0 == 0 {
557                         return Ok(());
558                     }
559                 }
560 
561                 Err(trap)
562             }
563         }
564     }
565 }
566