Skip to content

Commit 1050b1b

Browse files
committed
Add tracy example
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 2b79625 commit 1050b1b

File tree

4 files changed

+210
-11
lines changed

4 files changed

+210
-11
lines changed

Cargo.lock

Lines changed: 140 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hyperlight_host/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ criterion = "0.5.1"
103103
tracing-chrome = "0.7.2"
104104
metrics-util = "0.19.1"
105105
metrics-exporter-prometheus = "0.17.0"
106+
tracing-tracy = "0.11.4"
106107

107108
[target.'cfg(windows)'.dev-dependencies]
108109
windows = { version = "0.61", features = [
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
This is an example of using the tracing-tracy tracing-subscriber. When ran, it will generate traces that can be viewed in the tracy profiler.
2+
3+
You can run it with:
4+
5+
```console
6+
TRACY_NO_EXIT=1 RUST_LOG=trace cargo run --package hyperlight-host --example tracing-tracy --profile release-with-debug
7+
```
8+
9+
and then the client should show up in the profiler GUI.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)