Skip to content

Commit 70af69b

Browse files
committed
[trace] Add instrumentation of hyperlight-wasm code
- generate tracing records for `wasm_runtime` to produce timing information at runtime
1 parent 565d83e commit 70af69b

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

src/wasm_runtime/src/component.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ static CUR_INSTANCE: Mutex<Option<Instance>> = Mutex::new(None);
4343
hyperlight_wasm_macro::wasm_guest_bindgen!();
4444

4545
// dummy for compatibility with the module loading approach
46+
#[hyperlight_guest_tracing::trace_function]
4647
fn init_wasm_runtime(_function_call: &FunctionCall) -> Result<Vec<u8>> {
4748
Ok(get_flatbuffer_result::<i32>(0))
4849
}
4950

51+
#[hyperlight_guest_tracing::trace_function]
5052
fn load_component_common(engine: &Engine, component: Component) -> Result<()> {
5153
let mut store = Store::new(engine, ());
5254
let instance = (*CUR_LINKER.lock())
@@ -58,6 +60,7 @@ fn load_component_common(engine: &Engine, component: Component) -> Result<()> {
5860
Ok(())
5961
}
6062

63+
#[hyperlight_guest_tracing::trace_function]
6164
fn load_wasm_module(function_call: &FunctionCall) -> Result<Vec<u8>> {
6265
if let (
6366
ParameterValue::VecBytes(ref wasm_bytes),
@@ -79,6 +82,7 @@ fn load_wasm_module(function_call: &FunctionCall) -> Result<Vec<u8>> {
7982
}
8083
}
8184

85+
#[hyperlight_guest_tracing::trace_function]
8286
fn load_wasm_module_phys(function_call: &FunctionCall) -> Result<Vec<u8>> {
8387
if let (ParameterValue::ULong(ref phys), ParameterValue::ULong(ref len), Some(ref engine)) = (
8488
&function_call.parameters.as_ref().unwrap()[0],
@@ -98,6 +102,7 @@ fn load_wasm_module_phys(function_call: &FunctionCall) -> Result<Vec<u8>> {
98102
}
99103

100104
#[no_mangle]
105+
#[hyperlight_guest_tracing::trace_function]
101106
pub extern "C" fn hyperlight_main() {
102107
platform::register_page_fault_handler();
103108

@@ -131,6 +136,7 @@ pub extern "C" fn hyperlight_main() {
131136
}
132137

133138
#[no_mangle]
139+
#[hyperlight_guest_tracing::trace_function]
134140
pub fn guest_dispatch_function(function_call: FunctionCall) -> Result<Vec<u8>> {
135141
Err(HyperlightGuestError::new(
136142
ErrorCode::GuestFunctionNotFound,

src/wasm_runtime/src/hostfuncs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub(crate) fn get_host_function_details() -> HostFunctionDetails {
3636
hyperlight_guest_bin::host_comm::get_host_function_details()
3737
}
3838

39+
#[hyperlight_guest_tracing::trace_function]
3940
pub(crate) fn hostfunc_type(d: &HostFunctionDefinition, e: &Engine) -> Result<FuncType> {
4041
let mut params = Vec::new();
4142
let mut last_was_vec = false;
@@ -79,6 +80,7 @@ pub(crate) fn hostfunc_type(d: &HostFunctionDefinition, e: &Engine) -> Result<Fu
7980
Ok(FuncType::new(e, params, results))
8081
}
8182

83+
#[hyperlight_guest_tracing::trace_function]
8284
pub(crate) fn call(
8385
d: &HostFunctionDefinition,
8486
mut c: Caller<'_, ()>,
@@ -115,6 +117,7 @@ pub(crate) fn call(
115117
Ok(())
116118
}
117119

120+
#[hyperlight_guest_tracing::trace_function]
118121
fn return_type_from_val(val: &ReturnValue) -> ReturnType {
119122
match val {
120123
ReturnValue::Int(_) => ReturnType::Int,

src/wasm_runtime/src/marshal.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use hyperlight_common::flatbuffer_wrappers::util::get_flatbuffer_result;
2929
use hyperlight_guest::error::{HyperlightGuestError, Result};
3030
use wasmtime::{AsContextMut, Extern, Val};
3131

32+
#[hyperlight_guest_tracing::trace_function]
3233
fn malloc<C: AsContextMut>(
3334
ctx: &mut C,
3435
get_export: &impl Fn(&mut C, &str) -> Option<Extern>,
@@ -46,6 +47,7 @@ fn malloc<C: AsContextMut>(
4647
Ok(addr)
4748
}
4849

50+
#[hyperlight_guest_tracing::trace_function]
4951
fn write<C: AsContextMut>(
5052
ctx: &mut C,
5153
get_export: &impl Fn(&mut C, &str) -> Option<Extern>,
@@ -67,6 +69,7 @@ fn write<C: AsContextMut>(
6769
Ok(())
6870
}
6971

72+
#[hyperlight_guest_tracing::trace_function]
7073
fn read<C: AsContextMut>(
7174
ctx: &mut C,
7275
get_export: &impl Fn(&mut C, &str) -> Option<Extern>,
@@ -88,6 +91,7 @@ fn read<C: AsContextMut>(
8891
Ok(())
8992
}
9093

94+
#[hyperlight_guest_tracing::trace_function]
9195
fn read_cstr<C: AsContextMut>(
9296
ctx: &mut C,
9397
get_export: &impl Fn(&mut C, &str) -> Option<Extern>,
@@ -126,6 +130,7 @@ fn read_cstr<C: AsContextMut>(
126130
})
127131
}
128132

133+
#[hyperlight_guest_tracing::trace_function]
129134
pub fn hl_param_to_val<C: AsContextMut>(
130135
mut ctx: C,
131136
get_export: impl Fn(&mut C, &str) -> Option<Extern>,
@@ -155,6 +160,7 @@ pub fn hl_param_to_val<C: AsContextMut>(
155160
}
156161
}
157162

163+
#[hyperlight_guest_tracing::trace_function]
158164
pub fn val_to_hl_result<C: AsContextMut>(
159165
mut ctx: C,
160166
get_export: impl Fn(&mut C, &str) -> Option<Extern>,
@@ -198,6 +204,7 @@ pub fn val_to_hl_result<C: AsContextMut>(
198204
}
199205
}
200206

207+
#[hyperlight_guest_tracing::trace_function]
201208
pub fn val_to_hl_param<'a, C: AsContextMut>(
202209
ctx: &mut C,
203210
get_export: impl Fn(&mut C, &str) -> Option<Extern>,
@@ -248,6 +255,7 @@ pub fn val_to_hl_param<'a, C: AsContextMut>(
248255
}
249256
}
250257

258+
#[hyperlight_guest_tracing::trace_function]
251259
pub fn hl_return_to_val<C: AsContextMut>(
252260
ctx: &mut C,
253261
get_export: impl Fn(&mut C, &str) -> Option<Extern>,

src/wasm_runtime/src/module.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static CUR_LINKER: Mutex<Option<Linker<()>>> = Mutex::new(None);
3939
static CUR_MODULE: Mutex<Option<Module>> = Mutex::new(None);
4040

4141
#[no_mangle]
42+
#[hyperlight_guest_tracing::trace_function]
4243
pub fn guest_dispatch_function(function_call: &FunctionCall) -> Result<Vec<u8>> {
4344
let engine = CUR_ENGINE.lock();
4445
let engine = engine.deref().as_ref().ok_or(HyperlightGuestError::new(
@@ -87,6 +88,7 @@ pub fn guest_dispatch_function(function_call: &FunctionCall) -> Result<Vec<u8>>
8788
)
8889
}
8990

91+
#[hyperlight_guest_tracing::trace_function]
9092
fn init_wasm_runtime() -> Result<Vec<u8>> {
9193
let mut config = Config::new();
9294
config.with_custom_code_memory(Some(alloc::sync::Arc::new(platform::WasmtimeCodeMemory {})));
@@ -114,6 +116,7 @@ fn init_wasm_runtime() -> Result<Vec<u8>> {
114116
Ok(get_flatbuffer_result::<i32>(0))
115117
}
116118

119+
#[hyperlight_guest_tracing::trace_function]
117120
fn load_wasm_module(function_call: &FunctionCall) -> Result<Vec<u8>> {
118121
if let (
119122
ParameterValue::VecBytes(ref wasm_bytes),
@@ -135,6 +138,7 @@ fn load_wasm_module(function_call: &FunctionCall) -> Result<Vec<u8>> {
135138
}
136139
}
137140

141+
#[hyperlight_guest_tracing::trace_function]
138142
fn load_wasm_module_phys(function_call: &FunctionCall) -> Result<Vec<u8>> {
139143
if let (ParameterValue::ULong(ref phys), ParameterValue::ULong(ref len), Some(ref engine)) = (
140144
&function_call.parameters.as_ref().unwrap()[0],
@@ -154,6 +158,7 @@ fn load_wasm_module_phys(function_call: &FunctionCall) -> Result<Vec<u8>> {
154158

155159
#[no_mangle]
156160
#[allow(clippy::fn_to_numeric_cast)] // GuestFunctionDefinition expects a function pointer as i64
161+
#[hyperlight_guest_tracing::trace_function]
157162
pub extern "C" fn hyperlight_main() {
158163
platform::register_page_fault_handler();
159164

src/wasm_runtime/src/platform.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use hyperlight_guest_bin::paging;
2626
// we start at
2727
// 0x100_0000_0000 and go up from there
2828
static FIRST_VADDR: AtomicU64 = AtomicU64::new(0x100_0000_0000u64);
29+
30+
#[hyperlight_guest_tracing::trace_function]
2931
fn page_fault_handler(
3032
_exception_number: u64,
3133
info: *mut handler::ExceptionInfo,
@@ -54,6 +56,8 @@ fn page_fault_handler(
5456
}
5557
false
5658
}
59+
60+
#[hyperlight_guest_tracing::trace_function]
5761
pub(crate) fn register_page_fault_handler() {
5862
// On amd64, vector 14 is #PF
5963
// See AMD64 Architecture Programmer's Manual, Volume 2
@@ -70,6 +74,7 @@ pub(crate) fn register_page_fault_handler() {
7074
* page-fault handling to hardcoded check if memory is in this region
7175
* (see above) */
7276
#[no_mangle]
77+
#[hyperlight_guest_tracing::trace_function]
7378
pub extern "C" fn wasmtime_mmap_new(_size: usize, _prot_flags: u32, ret: &mut *mut u8) -> i32 {
7479
if _size > 0x100_0000_0000 {
7580
panic!("wasmtime_mmap_{:x} {:x}", _size, _prot_flags);
@@ -83,6 +88,7 @@ pub extern "C" fn wasmtime_mmap_new(_size: usize, _prot_flags: u32, ret: &mut *m
8388
* the same), or possibly for changing permissions, which will be a no-op
8489
* as we don't properly implement permissions at the moment. */
8590
#[no_mangle]
91+
#[hyperlight_guest_tracing::trace_function]
8692
pub extern "C" fn wasmtime_mmap_remap(addr: *mut u8, size: usize, prot_flags: u32) -> i32 {
8793
if size > 0x100_0000_0000 {
8894
panic!(
@@ -94,12 +100,14 @@ pub extern "C" fn wasmtime_mmap_remap(addr: *mut u8, size: usize, prot_flags: u3
94100
}
95101

96102
#[no_mangle]
103+
#[hyperlight_guest_tracing::trace_function]
97104
pub extern "C" fn wasmtime_munmap(_ptr: *mut u8, _size: usize) -> i32 {
98105
0
99106
}
100107

101108
/* TODO: implement permissions properly */
102109
#[no_mangle]
110+
#[hyperlight_guest_tracing::trace_function]
103111
pub extern "C" fn wasmtime_mprotect(_ptr: *mut u8, _size: usize, prot_flags: u32) -> i32 {
104112
/* currently all memory is allocated RWX; we assume that
105113
* restricting to R or RX can be ignored */
@@ -110,6 +118,7 @@ pub extern "C" fn wasmtime_mprotect(_ptr: *mut u8, _size: usize, prot_flags: u32
110118
}
111119

112120
#[no_mangle]
121+
#[hyperlight_guest_tracing::trace_function]
113122
pub extern "C" fn wasmtime_page_size() -> usize {
114123
unsafe { hyperlight_guest_bin::OS_PAGE_SIZE as usize }
115124
}
@@ -118,6 +127,8 @@ pub extern "C" fn wasmtime_page_size() -> usize {
118127
type wasmtime_trap_handler_t =
119128
extern "C" fn(ip: usize, fp: usize, has_faulting_addr: bool, faulting_addr: usize);
120129
static WASMTIME_REQUESTED_TRAP_HANDLER: AtomicU64 = AtomicU64::new(0);
130+
131+
#[hyperlight_guest_tracing::trace_function]
121132
fn wasmtime_trap_handler(
122133
exception_number: u64,
123134
info: *mut handler::ExceptionInfo,
@@ -149,6 +160,7 @@ fn wasmtime_trap_handler(
149160
}
150161

151162
#[no_mangle]
163+
#[hyperlight_guest_tracing::trace_function]
152164
pub extern "C" fn wasmtime_init_traps(handler: wasmtime_trap_handler_t) -> i32 {
153165
WASMTIME_REQUESTED_TRAP_HANDLER.store(handler as usize as u64, Ordering::Relaxed);
154166
// On amd64, vector 6 is #UD
@@ -168,6 +180,7 @@ pub extern "C" fn wasmtime_init_traps(handler: wasmtime_trap_handler_t) -> i32 {
168180

169181
// The wasmtime_memory_image APIs are not yet supported.
170182
#[no_mangle]
183+
#[hyperlight_guest_tracing::trace_function]
171184
pub extern "C" fn wasmtime_memory_image_new(
172185
_ptr: *const u8,
173186
_len: usize,
@@ -178,6 +191,7 @@ pub extern "C" fn wasmtime_memory_image_new(
178191
}
179192

180193
#[no_mangle]
194+
#[hyperlight_guest_tracing::trace_function]
181195
pub extern "C" fn wasmtime_memory_image_map_at(
182196
_image: *mut c_void,
183197
_addr: *mut u8,
@@ -189,6 +203,7 @@ pub extern "C" fn wasmtime_memory_image_map_at(
189203
}
190204

191205
#[no_mangle]
206+
#[hyperlight_guest_tracing::trace_function]
192207
pub extern "C" fn wasmtime_memory_image_free(_image: *mut c_void) {
193208
/* This should never be called because wasmtime_memory_image_new
194209
* returns NULL */
@@ -198,11 +213,15 @@ pub extern "C" fn wasmtime_memory_image_free(_image: *mut c_void) {
198213
/* Because we only have a single thread in the guest at the moment, we
199214
* don't need real thread-local storage. */
200215
static FAKE_TLS: AtomicPtr<u8> = AtomicPtr::new(core::ptr::null_mut());
216+
201217
#[no_mangle]
218+
#[hyperlight_guest_tracing::trace_function]
202219
pub extern "C" fn wasmtime_tls_get() -> *mut u8 {
203220
FAKE_TLS.load(Ordering::Acquire)
204221
}
222+
205223
#[no_mangle]
224+
#[hyperlight_guest_tracing::trace_function]
206225
pub extern "C" fn wasmtime_tls_set(ptr: *mut u8) {
207226
FAKE_TLS.store(ptr, Ordering::Release)
208227
}
@@ -229,6 +248,7 @@ impl wasmtime::CustomCodeMemory for WasmtimeCodeMemory {
229248
}
230249
}
231250

251+
#[hyperlight_guest_tracing::trace_function]
232252
pub(crate) unsafe fn map_buffer(phys: u64, len: u64) -> NonNull<[u8]> {
233253
// TODO: Use a VA allocator
234254
let virt = phys as *mut u8;

0 commit comments

Comments
 (0)