|  | 
|  | 1 | +/* | 
|  | 2 | +Copyright 2024 The Hyperlight Authors. | 
|  | 3 | +
 | 
|  | 4 | +Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | +you may not use this file except in compliance with the License. | 
|  | 6 | +You may obtain a copy of the License at | 
|  | 7 | +
 | 
|  | 8 | +    http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | +
 | 
|  | 10 | +Unless required by applicable law or agreed to in writing, software | 
|  | 11 | +distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | +See the License for the specific language governing permissions and | 
|  | 14 | +limitations under the License. | 
|  | 15 | +*/ | 
|  | 16 | + | 
|  | 17 | +use hyperlight_host::func::{ParameterValue, ReturnType, ReturnValue}; | 
|  | 18 | +use hyperlight_host::sandbox::uninitialized::UninitializedSandbox; | 
|  | 19 | +use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox; | 
|  | 20 | +use hyperlight_host::sandbox_state::transition::Noop; | 
|  | 21 | +use hyperlight_host::{GuestBinary, MultiUseSandbox, Result}; | 
|  | 22 | +use hyperlight_testing::simple_guest_as_string; | 
|  | 23 | +use tracing_subscriber::layer::SubscriberExt; | 
|  | 24 | +use tracing_subscriber::EnvFilter; | 
|  | 25 | + | 
|  | 26 | +// An example of how to get tracy tracing working with hyperlight. | 
|  | 27 | +// Run with: | 
|  | 28 | +// TRACY_NO_EXIT=1 RUST_LOG=trace cargo run --package hyperlight-host --example tracing-tracy --profile release-with-debug, | 
|  | 29 | +// and then open the `tracy-profiler` GUI, and there should be an option to load the client created by this example. | 
|  | 30 | +fn main() -> Result<()> { | 
|  | 31 | +    tracing::subscriber::set_global_default( | 
|  | 32 | +        tracing_subscriber::registry() | 
|  | 33 | +            .with(EnvFilter::from_default_env()) | 
|  | 34 | +            .with(tracing_tracy::TracyLayer::default()), | 
|  | 35 | +    ) | 
|  | 36 | +    .expect("setup tracy layer"); | 
|  | 37 | + | 
|  | 38 | +    let simple_guest_path = | 
|  | 39 | +        simple_guest_as_string().expect("Cannot find the guest binary at the expected location."); | 
|  | 40 | + | 
|  | 41 | +    // Create a new sandbox. | 
|  | 42 | +    let usandbox = | 
|  | 43 | +        UninitializedSandbox::new(GuestBinary::FilePath(simple_guest_path), None, None, None)?; | 
|  | 44 | + | 
|  | 45 | +    let mut sbox = usandbox | 
|  | 46 | +        .evolve(Noop::<UninitializedSandbox, MultiUseSandbox>::default()) | 
|  | 47 | +        .unwrap(); | 
|  | 48 | + | 
|  | 49 | +    // do the function call | 
|  | 50 | +    let current_time = std::time::Instant::now(); | 
|  | 51 | +    let res = sbox.call_guest_function_by_name( | 
|  | 52 | +        "Echo", | 
|  | 53 | +        ReturnType::String, | 
|  | 54 | +        Some(vec![ParameterValue::String("Hello, World!".to_string())]), | 
|  | 55 | +    )?; | 
|  | 56 | +    let elapsed = current_time.elapsed(); | 
|  | 57 | +    println!("Function call finished in {:?}.", elapsed); | 
|  | 58 | +    assert!(matches!(res, ReturnValue::String(s) if s == "Hello, World!")); | 
|  | 59 | +    Ok(()) | 
|  | 60 | +} | 
0 commit comments