#![allow(clippy::disallowed_macros)]
extern crate hyperlight_host;
use std::sync::{Arc, Barrier};
use std::thread::{JoinHandle, spawn};
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
use hyperlight_host::{GuestBinary, Result};
use hyperlight_testing::simple_guest_as_string;
fn main() {
let prometheus_handle = metrics_exporter_prometheus::PrometheusBuilder::new()
.install_recorder()
.expect("Failed to install Prometheus exporter");
do_hyperlight_stuff();
let payload = prometheus_handle.render();
println!("Prometheus metrics:\n{}", payload);
}
fn do_hyperlight_stuff() {
let hyperlight_guest_path =
simple_guest_as_string().expect("Cannot find the guest binary at the expected location.");
let mut join_handles: Vec<JoinHandle<Result<()>>> = vec![];
for _ in 0..20 {
let path = hyperlight_guest_path.clone();
let handle = spawn(move || -> Result<()> {
let mut usandbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None)?;
usandbox.register_print(fn_writer)?;
let mut multiuse_sandbox = usandbox.evolve().expect("Failed to evolve sandbox");
for _ in 0..5 {
multiuse_sandbox
.call::<String>("Echo", "a".to_string())
.unwrap();
}
let msg = "Hello, World!!\n".to_string();
for _ in 0..5 {
multiuse_sandbox
.call::<i32>("PrintOutput", msg.clone())
.unwrap();
}
Ok(())
});
join_handles.push(handle);
}
let usandbox =
UninitializedSandbox::new(GuestBinary::FilePath(hyperlight_guest_path.clone()), None)
.expect("Failed to create UninitializedSandbox");
let mut multiuse_sandbox = usandbox.evolve().expect("Failed to evolve sandbox");
let interrupt_handle = multiuse_sandbox.interrupt_handle();
const NUM_CALLS: i32 = 5;
let barrier = Arc::new(Barrier::new(2));
let barrier2 = barrier.clone();
let thread = std::thread::spawn(move || {
for _ in 0..NUM_CALLS {
barrier2.wait();
std::thread::sleep(std::time::Duration::from_millis(500));
interrupt_handle.kill();
}
});
for _ in 0..NUM_CALLS {
barrier.wait();
multiuse_sandbox.call::<()>("Spin", ()).unwrap_err();
}
for join_handle in join_handles {
let result = join_handle.join();
assert!(result.is_ok());
}
thread.join().unwrap();
}
fn fn_writer(_msg: String) -> Result<i32> {
Ok(0)
}