Skip to content

Commit 255369a

Browse files
committed
start building a component for benchmarking
Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
1 parent 8bfefd7 commit 255369a

File tree

8 files changed

+219
-2
lines changed

8 files changed

+219
-2
lines changed

src/hyperlight_wasm/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,8 @@ mshv3 = ["hyperlight-host/mshv3"]
8484
[[bench]]
8585
name = "benchmarks"
8686
harness = false
87+
88+
[[bench]]
89+
name = "benchmarks_components"
90+
harness = false
91+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use criterion::{Bencher, Criterion, criterion_group, criterion_main};
2+
use hyperlight_host::HyperlightError;
3+
use hyperlight_wasm::{LoadedWasmSandbox, Result, SandboxBuilder};
4+
5+
fn wasm_component_guest_call_benchmark(c: &mut Criterion) {
6+
let mut group = c.benchmark_group("wasm_component_guest_functions");
7+
8+
// let bench_guest_function = |b: &mut Bencher<'_>, ext| {
9+
// let mut loaded_wasm_sandbox = get_loaded_wasm_sandbox(ext);
10+
11+
// b.iter(|| {
12+
// loaded_wasm_sandbox
13+
// .call_guest_function::<String>("Echo", "Hello World!".to_string())
14+
// .unwrap()
15+
// });
16+
// };
17+
18+
// group.bench_function("wasm_guest_call", |b: &mut Bencher<'_>| {
19+
// bench_guest_function(b, "wasm");
20+
// });
21+
22+
// group.bench_function("wasm_guest_call_aot", |b: &mut Bencher<'_>| {
23+
// bench_guest_function(b, "aot");
24+
// });
25+
26+
group.finish();
27+
}
28+
29+
fn wasm_component_sandbox_benchmark(c: &mut Criterion) {
30+
let mut group = c.benchmark_group("wasm_component_sandboxes");
31+
let create_wasm_sandbox = || {
32+
get_loaded_wasm_sandbox("wasm");
33+
};
34+
35+
group.bench_function("create_sandbox", |b| {
36+
b.iter_with_large_drop(create_wasm_sandbox);
37+
});
38+
39+
group.bench_function("create_sandbox_and_drop", |b| {
40+
b.iter(create_wasm_sandbox);
41+
});
42+
43+
group.finish();
44+
}
45+
46+
fn get_loaded_wasm_sandbox(ext: &str) -> LoadedWasmSandbox {
47+
let mut sandbox = SandboxBuilder::new().build().unwrap();
48+
49+
let wasm_sandbox = sandbox.load_runtime().unwrap();
50+
51+
wasm_sandbox
52+
.load_module(format!("../../x64/release/component_sample.aot",))
53+
.unwrap()
54+
}
55+
56+
criterion_group! {
57+
name = benches_components;
58+
config = Criterion::default();//.warm_up_time(Duration::from_millis(50)); // If warm_up_time is default 3s warmup, the benchmark will fail due memory error
59+
targets = wasm_component_guest_call_benchmark, wasm_component_sandbox_benchmark
60+
}
61+
criterion_main!(benches_components);

src/hyperlight_wasm/scripts/build-wasm-examples.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,26 @@ else
3333

3434
docker build --build-arg GCC_VERSION=12 --build-arg WASI_SDK_VERSION_FULL=20.0 --cache-from ghcr.io/hyperlight-dev/wasm-clang-builder:latest -t wasm-clang-builder:latest . 2> ${OUTPUT_DIR}/dockerbuild.log
3535

36-
for FILENAME in $(find . -name '*.c')
36+
for FILENAME in $(find . -name '*.c' -not -path './components/*')
3737
do
3838
echo Building ${FILENAME}
3939
# Build the wasm file with wasi-libc for wasmtime
40-
docker run --rm -i -v "${PWD}:/tmp/host" -v "${OUTPUT_DIR}:/tmp/output" wasm-clang-builder:latest /opt/wasi-sdk/bin/clang -flto -ffunction-sections -mexec-model=reactor -O3 -z stack-size=4096 -Wl,--initial-memory=65536 -Wl,--export=__data_end -Wl,--export=__heap_base,--export=malloc,--export=free,--export=__wasm_call_ctors -Wl,--strip-all,--no-entry -Wl,--allow-undefined -Wl,--gc-sections -o /tmp/output/${FILENAME%.*}-wasi-libc.wasm /tmp/host/${FILENAME}
40+
docker run --rm -i -v "${PWD}:/tmp/host" -v "${OUTPUT_DIR}:/tmp/output/" wasm-clang-builder:latest /opt/wasi-sdk/bin/clang -flto -ffunction-sections -mexec-model=reactor -O3 -z stack-size=4096 -Wl,--initial-memory=65536 -Wl,--export=__data_end -Wl,--export=__heap_base,--export=malloc,--export=free,--export=__wasm_call_ctors -Wl,--strip-all,--no-entry -Wl,--allow-undefined -Wl,--gc-sections -o /tmp/output/${FILENAME%.*}-wasi-libc.wasm /tmp/host/${FILENAME}
4141

4242
# Build AOT for Wasmtime; note that Wasmtime does not support
4343
# interpreting, so its wasm binary is secretly an AOT binary.
4444
cargo run -p hyperlight-wasm-aot compile ${OUTPUT_DIR}/${FILENAME%.*}-wasi-libc.wasm ${OUTPUT_DIR}/${FILENAME%.*}.aot
4545
cp ${OUTPUT_DIR}/${FILENAME%.*}.aot ${OUTPUT_DIR}/${FILENAME%.*}.wasm
4646
done
47+
48+
echo Building component
49+
# Build the wasm file with wasi-libc for wasmtime
50+
docker run --rm -i -v "${PWD}:/tmp/host" -v "${OUTPUT_DIR}:/tmp/output/" wasm-clang-builder:latest /opt/wasi-sdk/bin/clang -flto -ffunction-sections -mexec-model=reactor -O3 -z stack-size=4096 -Wl,--initial-memory=65536 -Wl,--export=__data_end -Wl,--export=__heap_base,--export=malloc,--export=free -o /tmp/output/runcomponent-p1.wasm /tmp/host/components/component.c /tmp/host/components/runcomponent.c /tmp/host/components/runcomponent_component_type.o
51+
52+
# Build AOT for Wasmtime; note that Wasmtime does not support
53+
# interpreting, so its wasm binary is secretly an AOT binary.
54+
cargo run -p hyperlight-wasm-aot compile ${OUTPUT_DIR}/runcomponent-p1.wasm ${OUTPUT_DIR}/runcomponent.aot
55+
cp ${OUTPUT_DIR}/runcomponent.aot ${OUTPUT_DIR}/runcomponent.wasm
4756
fi
4857

4958
popd
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "runcomponent.h"
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
void exports_example_runcomponent_guest_echo(runcomponent_string_t *msg, runcomponent_string_t *ret)
6+
{
7+
ret->len = msg->len;
8+
ret->ptr = (uint8_t *) malloc(ret->len);
9+
memcpy(ret->ptr, msg->ptr, ret->len);
10+
runcomponent_string_free(msg);
11+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Generated by `wit-bindgen` 0.38.0. DO NOT EDIT!
2+
#include "runcomponent.h"
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
// Imported Functions from `example:runcomponent/host@0.1.0`
7+
8+
__attribute__((__import_module__("example:runcomponent/host@0.1.0"), __import_name__("get-time-since-boot-microsecond")))
9+
extern int64_t __wasm_import_example_runcomponent_host_get_time_since_boot_microsecond(void);
10+
11+
// Exported Functions from `example:runcomponent/guest@0.1.0`
12+
13+
__attribute__((__weak__, __export_name__("cabi_post_example:runcomponent/guest@0.1.0#echo")))
14+
void __wasm_export_exports_example_runcomponent_guest_echo_post_return(uint8_t * arg0) {
15+
if ((*((size_t*) (arg0 + 4))) > 0) {
16+
free(*((uint8_t **) (arg0 + 0)));
17+
}
18+
}
19+
20+
// Canonical ABI intrinsics
21+
22+
__attribute__((__weak__, __export_name__("cabi_realloc")))
23+
void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) {
24+
(void) old_size;
25+
if (new_size == 0) return (void*) align;
26+
void *ret = realloc(ptr, new_size);
27+
if (!ret) abort();
28+
return ret;
29+
}
30+
31+
// Helper Functions
32+
33+
void runcomponent_string_set(runcomponent_string_t *ret, const char*s) {
34+
ret->ptr = (uint8_t*) s;
35+
ret->len = strlen(s);
36+
}
37+
38+
void runcomponent_string_dup(runcomponent_string_t *ret, const char*s) {
39+
ret->len = strlen(s);
40+
ret->ptr = (uint8_t*) cabi_realloc(NULL, 0, 1, ret->len * 1);
41+
memcpy(ret->ptr, s, ret->len * 1);
42+
}
43+
44+
void runcomponent_string_free(runcomponent_string_t *ret) {
45+
if (ret->len > 0) {
46+
free(ret->ptr);
47+
}
48+
ret->ptr = NULL;
49+
ret->len = 0;
50+
}
51+
52+
// Component Adapters
53+
54+
__attribute__((__aligned__(4)))
55+
static uint8_t RET_AREA[8];
56+
57+
int64_t example_runcomponent_host_get_time_since_boot_microsecond(void) {
58+
int64_t ret = __wasm_import_example_runcomponent_host_get_time_since_boot_microsecond();
59+
return ret;
60+
}
61+
62+
__attribute__((__export_name__("example:runcomponent/guest@0.1.0#echo")))
63+
uint8_t * __wasm_export_exports_example_runcomponent_guest_echo(uint8_t * arg, size_t arg0) {
64+
runcomponent_string_t arg1 = (runcomponent_string_t) { (uint8_t*)(arg), (arg0) };
65+
runcomponent_string_t ret;
66+
exports_example_runcomponent_guest_echo(&arg1, &ret);
67+
uint8_t *ptr = (uint8_t *) &RET_AREA;
68+
*((size_t*)(ptr + 4)) = (ret).len;
69+
*((uint8_t **)(ptr + 0)) = (uint8_t *) (ret).ptr;
70+
return ptr;
71+
}
72+
73+
// Ensure that the *_component_type.o object is linked in
74+
75+
extern void __component_type_object_force_link_runcomponent(void);
76+
void __component_type_object_force_link_runcomponent_public_use_in_this_compilation_unit(void) {
77+
__component_type_object_force_link_runcomponent();
78+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Generated by `wit-bindgen` 0.38.0. DO NOT EDIT!
2+
#ifndef __BINDINGS_RUNCOMPONENT_H
3+
#define __BINDINGS_RUNCOMPONENT_H
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <stdint.h>
9+
#include <stdbool.h>
10+
#include <stddef.h>
11+
12+
typedef struct runcomponent_string_t {
13+
uint8_t*ptr;
14+
size_t len;
15+
} runcomponent_string_t;
16+
17+
// Imported Functions from `example:runcomponent/host@0.1.0`
18+
extern int64_t example_runcomponent_host_get_time_since_boot_microsecond(void);
19+
20+
// Exported Functions from `example:runcomponent/guest@0.1.0`
21+
void exports_example_runcomponent_guest_echo(runcomponent_string_t *msg, runcomponent_string_t *ret);
22+
23+
// Helper Functions
24+
25+
// Transfers ownership of `s` into the string `ret`
26+
void runcomponent_string_set(runcomponent_string_t *ret, const char*s);
27+
28+
// Creates a copy of the input nul-terminate string `s` and
29+
// stores it into the component model string `ret`.
30+
void runcomponent_string_dup(runcomponent_string_t *ret, const char*s);
31+
32+
// Deallocates the string pointed to by `ret`, deallocating
33+
// the memory behind the string.
34+
void runcomponent_string_free(runcomponent_string_t *ret);
35+
36+
#ifdef __cplusplus
37+
}
38+
#endif
39+
#endif
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package example:runcomponent@0.1.0;
2+
3+
interface guest {
4+
echo: func(msg: string) -> string;
5+
}
6+
7+
interface host {
8+
get-time-since-boot-microsecond: func() -> s64;
9+
}
10+
11+
world runcomponent {
12+
export guest;
13+
import host;
14+
}
Binary file not shown.

0 commit comments

Comments
 (0)