Skip to content

Commit 119aaae

Browse files
committed
Measure remote call overhead
1 parent 50f1e21 commit 119aaae

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

guest/rust_storage/src/main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ use std::process::ExitCode;
55
extern "C" fn do_calculation(input: i32) -> i32 {
66
return unsafe { remote_function(double_int, input) };
77
}
8+
extern "C" fn do_nothing(_input: i32) -> i32 {
9+
return 42;
10+
}
811

912
extern "C" fn double_int(input: i32) -> i32 {
1013
return input * 2;
@@ -14,13 +17,15 @@ extern "C" fn double_int(input: i32) -> i32 {
1417
// in a dynamic ELF w/interpreter. We don't know the
1518
// base address of the binary, so we can't adjust the
1619
// symbol address.
17-
fn set_callback(_cb: extern "C" fn(i32) -> i32) {
20+
fn set_callback(name: &str, cb: extern "C" fn(i32) -> i32) {
1821
let sysnum = 0x10001; // Set callback syscall
1922
unsafe {
2023
asm!(
2124
"out 0, eax",
2225
in("eax") sysnum,
23-
in("rdi") _cb
26+
in("rdi") cb,
27+
in("rsi") name.as_ptr(),
28+
in("rdx") name.len()
2429
);
2530
}
2631
}
@@ -30,6 +35,7 @@ fn main() -> ExitCode
3035
println!("Hello, world!");
3136
let result = unsafe { remote_function(double_int, 21) };
3237
println!("Result from remote function: {}", result);
33-
set_callback(do_calculation);
38+
set_callback("do_calculation", do_calculation);
39+
set_callback("do_nothing", do_nothing);
3440
return ExitCode::from(result as u8);
3541
}

lib/tinykvm/machine.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ struct Machine
9595
const char* end() const noexcept { return sv.end(); }
9696
const char* c_str() const noexcept { return sv.begin(); }
9797
size_t size() const noexcept { return sv.size(); }
98+
std::string to_string() const { return std::string{sv}; }
9899

99100
bool is_sequential() const noexcept { return str.empty(); }
100101

src/storage.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,18 @@ int main(int argc, char** argv)
126126

127127
master_vm.remote_connect(storage_vm, false);
128128

129-
static uint64_t callback_address = 0x0;
129+
static std::map<std::string, uint64_t> callback_address;
130130
master_vm.install_unhandled_syscall_handler(
131131
[] (tinykvm::vCPU& cpu, unsigned sysnum) {
132132
auto& regs = cpu.registers();
133133
switch (sysnum) {
134-
case 0x10001: // Set callback address
135-
callback_address = regs.rdi;
136-
return;
134+
case 0x10001: { // Set callback address
135+
auto view = cpu.machine().string_or_view(regs.rsi, regs.rdx);
136+
const std::string name = view.to_string();
137+
callback_address[name] = regs.rdi;
138+
printf("Set callback '%s' to 0x%llX\n", name.c_str(), regs.rdi);
139+
return;
140+
}
137141
default:
138142
printf("Unhandled master VM syscall: %u\n", sysnum);
139143
regs.rax = -ENOSYS;
@@ -167,15 +171,29 @@ int main(int argc, char** argv)
167171
assert(vm.is_remote_connected());
168172

169173
/* Call 'do_calculation' with 21 as argument */
170-
if (callback_address == 0) {
174+
auto do_it = callback_address.find("do_nothing");
175+
if (do_it == callback_address.end()) {
176+
fprintf(stderr, "Error: no do_nothing() in guest\n");
177+
exit(1);
178+
}
179+
auto calc_it = callback_address.find("do_calculation");
180+
if (calc_it == callback_address.end()) {
171181
fprintf(stderr, "Error: no do_calculation() in guest\n");
172182
exit(1);
173183
}
174-
printf("Calling do_calculation() @ 0x%lX\n", callback_address);
175-
for (int i = 0; i < 100; i++)
176-
vm.timed_vmcall(callback_address, 5.0f, 21);
184+
auto call_overhead = timed_action([&] {
185+
for (int i = 0; i < 100; i++)
186+
vm.timed_vmcall(do_it->second, 5.0f, 21);
187+
}) / 100.0;
188+
printf("Call overhead: %.2fus\n", call_overhead * 1e6);
189+
190+
printf("Calling do_calculation() @ 0x%lX\n", calc_it->second);
191+
for (int i = 0; i < 10; i++)
192+
vm.timed_vmcall(calc_it->second, 5.0f, 21);
177193
auto fork_tdiff = timed_action([&] {
178-
vm.timed_vmcall(callback_address, 5.0f, 21);
179-
});
180-
printf("Fork call time: %.2fus Return value: %ld\n", fork_tdiff*1e6, vm.return_value());
194+
for (int i = 0; i < 100; i++)
195+
vm.timed_vmcall(calc_it->second, 5.0f, 21);
196+
}) / 100.0;
197+
fork_tdiff -= call_overhead;
198+
printf("Remote call time: %.2fus Return value: %ld\n", fork_tdiff * 1e6, vm.return_value());
181199
}

0 commit comments

Comments
 (0)