diff --git a/Cargo.lock b/Cargo.lock index 3531ce826..69d9c89c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -867,6 +867,19 @@ dependencies = [ "num-traits", ] +[[package]] +name = "generator" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc6bd114ceda131d3b1d665eba35788690ad37f5916457286b32ab6fd3c438dd" +dependencies = [ + "cfg-if", + "libc", + "log", + "rustversion", + "windows 0.58.0", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -1242,10 +1255,11 @@ dependencies = [ "tracing-opentelemetry", "tracing-serde", "tracing-subscriber", + "tracing-tracy", "uuid", "vmm-sys-util 0.13.0", - "windows", - "windows-result", + "windows 0.61.1", + "windows-result 0.3.2", "windows-sys 0.59.0", "windows-version", ] @@ -1287,7 +1301,7 @@ dependencies = [ "js-sys", "log", "wasm-bindgen", - "windows-core", + "windows-core 0.61.0", ] [[package]] @@ -1705,6 +1719,19 @@ version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" +[[package]] +name = "loom" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "419e0dc8046cb947daa77eb95ae174acfbddb7673b4151f56d1eed8e93fbfaca" +dependencies = [ + "cfg-if", + "generator", + "scoped-tls", + "tracing", + "tracing-subscriber", +] + [[package]] name = "mach2" version = "0.4.2" @@ -2720,6 +2747,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + [[package]] name = "scopeguard" version = "1.2.0" @@ -3436,6 +3469,38 @@ dependencies = [ "tracing-log", ] +[[package]] +name = "tracing-tracy" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eaa1852afa96e0fe9e44caa53dc0bd2d9d05e0f2611ce09f97f8677af56e4ba" +dependencies = [ + "tracing-core", + "tracing-subscriber", + "tracy-client", +] + +[[package]] +name = "tracy-client" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d90a2c01305b02b76fdd89ac8608bae27e173c829a35f7d76a345ab5d33836db" +dependencies = [ + "loom", + "once_cell", + "tracy-client-sys", +] + +[[package]] +name = "tracy-client-sys" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69fff37da548239c3bf9e64a12193d261e8b22b660991c6fd2df057c168f435f" +dependencies = [ + "cc", + "windows-targets 0.52.6", +] + [[package]] name = "try-lock" version = "0.2.5" @@ -3726,6 +3791,16 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" +dependencies = [ + "windows-core 0.58.0", + "windows-targets 0.52.6", +] + [[package]] name = "windows" version = "0.61.1" @@ -3733,7 +3808,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c5ee8f3d025738cb02bad7868bbb5f8a6327501e870bf51f1b455b0a2454a419" dependencies = [ "windows-collections", - "windows-core", + "windows-core 0.61.0", "windows-future", "windows-link", "windows-numerics", @@ -3745,7 +3820,20 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8" dependencies = [ - "windows-core", + "windows-core 0.61.0", +] + +[[package]] +name = "windows-core" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" +dependencies = [ + "windows-implement 0.58.0", + "windows-interface 0.58.0", + "windows-result 0.2.0", + "windows-strings 0.1.0", + "windows-targets 0.52.6", ] [[package]] @@ -3754,10 +3842,10 @@ version = "0.61.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4763c1de310c86d75a878046489e2e5ba02c649d185f21c67d4cf8a56d098980" dependencies = [ - "windows-implement", - "windows-interface", + "windows-implement 0.60.0", + "windows-interface 0.59.1", "windows-link", - "windows-result", + "windows-result 0.3.2", "windows-strings 0.4.0", ] @@ -3767,10 +3855,21 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a1d6bbefcb7b60acd19828e1bc965da6fcf18a7e39490c5f8be71e54a19ba32" dependencies = [ - "windows-core", + "windows-core 0.61.0", "windows-link", ] +[[package]] +name = "windows-implement" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "windows-implement" version = "0.60.0" @@ -3782,6 +3881,17 @@ dependencies = [ "syn", ] +[[package]] +name = "windows-interface" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "windows-interface" version = "0.59.1" @@ -3805,7 +3915,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1" dependencies = [ - "windows-core", + "windows-core 0.61.0", "windows-link", ] @@ -3815,11 +3925,20 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4286ad90ddb45071efd1a66dfa43eb02dd0dfbae1545ad6cc3c51cf34d7e8ba3" dependencies = [ - "windows-result", + "windows-result 0.3.2", "windows-strings 0.3.1", "windows-targets 0.53.0", ] +[[package]] +name = "windows-result" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-result" version = "0.3.2" @@ -3829,6 +3948,16 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-strings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +dependencies = [ + "windows-result 0.2.0", + "windows-targets 0.52.6", +] + [[package]] name = "windows-strings" version = "0.3.1" diff --git a/docs/hyperlight-metrics-logs-and-traces.md b/docs/hyperlight-metrics-logs-and-traces.md index 7427962c5..3dc62438d 100644 --- a/docs/hyperlight-metrics-logs-and-traces.md +++ b/docs/hyperlight-metrics-logs-and-traces.md @@ -62,18 +62,18 @@ $env:RUST_LOG='none,hyperlight_host=info,tracing=info'; cargo run --example trac ### Using OTLP exporter and Jaeger -In the [examples/otlp_tracing](../src/hyperlight_host/examples/otlp_tracing) directory, there is an example that shows how to capture and send trace and log information to an otlp_collector using the opentelemetry_otlp crate. With this example the following commands can be used to set the verbosity of the trace output to `INFO` and run the example to generate trace data: +In the [examples/tracing-otlp](../src/hyperlight_host/examples/tracing-otlp) directory, there is an example that shows how to capture and send trace and log information to an otlp_collector using the opentelemetry_otlp crate. With this example the following commands can be used to set the verbosity of the trace output to `INFO` and run the example to generate trace data: #### Linux ```bash -RUST_LOG='none,hyperlight_host=info,tracing=info' cargo run --example otlp_tracing +RUST_LOG='none,hyperlight_host=info,tracing=info' cargo run --example tracing-otlp ``` #### Windows ```powershell -$env:RUST_LOG='none,hyperlight_host=info,tracing=info';cargo run --example otlp_tracing +$env:RUST_LOG='none,hyperlight_host=info,tracing=info';cargo run --example tracing-otlp ``` The sample will run and generate trace data until any key is pressed. diff --git a/src/hyperlight_host/Cargo.toml b/src/hyperlight_host/Cargo.toml index 44b9fc304..5fdb74cc0 100644 --- a/src/hyperlight_host/Cargo.toml +++ b/src/hyperlight_host/Cargo.toml @@ -103,6 +103,7 @@ criterion = "0.5.1" tracing-chrome = "0.7.2" metrics-util = "0.19.1" metrics-exporter-prometheus = "0.17.0" +tracing-tracy = "0.11.4" [target.'cfg(windows)'.dev-dependencies] windows = { version = "0.61", features = [ diff --git a/src/hyperlight_host/examples/chrome-tracing/README.md b/src/hyperlight_host/examples/tracing-chrome/README.md similarity index 100% rename from src/hyperlight_host/examples/chrome-tracing/README.md rename to src/hyperlight_host/examples/tracing-chrome/README.md diff --git a/src/hyperlight_host/examples/chrome-tracing/main.rs b/src/hyperlight_host/examples/tracing-chrome/main.rs similarity index 98% rename from src/hyperlight_host/examples/chrome-tracing/main.rs rename to src/hyperlight_host/examples/tracing-chrome/main.rs index dd25bbf9b..8150463d3 100644 --- a/src/hyperlight_host/examples/chrome-tracing/main.rs +++ b/src/hyperlight_host/examples/tracing-chrome/main.rs @@ -23,7 +23,7 @@ use hyperlight_testing::simple_guest_as_string; use tracing_chrome::ChromeLayerBuilder; use tracing_subscriber::prelude::*; -// This example can be run with `cargo run --package hyperlight_host --example chrome-tracing --release` +// This example can be run with `cargo run --package hyperlight_host --example tracing-chrome --release` fn main() -> Result<()> { // set up tracer let (chrome_layer, _guard) = ChromeLayerBuilder::new().build(); diff --git a/src/hyperlight_host/examples/otlp_tracing/main.rs b/src/hyperlight_host/examples/tracing-otlp/main.rs similarity index 100% rename from src/hyperlight_host/examples/otlp_tracing/main.rs rename to src/hyperlight_host/examples/tracing-otlp/main.rs diff --git a/src/hyperlight_host/examples/tracing-tracy/README.md b/src/hyperlight_host/examples/tracing-tracy/README.md new file mode 100644 index 000000000..425c3d9dc --- /dev/null +++ b/src/hyperlight_host/examples/tracing-tracy/README.md @@ -0,0 +1,11 @@ +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. + +You can run it with: + +```console +TRACY_NO_EXIT=1 RUST_LOG=trace cargo run --package hyperlight-host --example tracing-tracy --profile release-with-debug +``` + +and then the client should show up in the profiler GUI: + +![pic of tracy profiler](image.png) \ No newline at end of file diff --git a/src/hyperlight_host/examples/tracing-tracy/image.png b/src/hyperlight_host/examples/tracing-tracy/image.png new file mode 100644 index 000000000..0dd5791f7 Binary files /dev/null and b/src/hyperlight_host/examples/tracing-tracy/image.png differ diff --git a/src/hyperlight_host/examples/tracing-tracy/main.rs b/src/hyperlight_host/examples/tracing-tracy/main.rs new file mode 100644 index 000000000..6b16c93bf --- /dev/null +++ b/src/hyperlight_host/examples/tracing-tracy/main.rs @@ -0,0 +1,60 @@ +/* +Copyright 2024 The Hyperlight Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +use hyperlight_host::func::{ParameterValue, ReturnType, ReturnValue}; +use hyperlight_host::sandbox::uninitialized::UninitializedSandbox; +use hyperlight_host::sandbox_state::sandbox::EvolvableSandbox; +use hyperlight_host::sandbox_state::transition::Noop; +use hyperlight_host::{GuestBinary, MultiUseSandbox, Result}; +use hyperlight_testing::simple_guest_as_string; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::EnvFilter; + +// An example of how to get tracy tracing working with hyperlight. +// Run with: +// TRACY_NO_EXIT=1 RUST_LOG=trace cargo run --package hyperlight-host --example tracing-tracy --profile release-with-debug, +// and then open the `tracy-profiler` GUI, and there should be an option to load the client created by this example. +fn main() -> Result<()> { + tracing::subscriber::set_global_default( + tracing_subscriber::registry() + .with(EnvFilter::from_default_env()) + .with(tracing_tracy::TracyLayer::default()), + ) + .expect("setup tracy layer"); + + let simple_guest_path = + simple_guest_as_string().expect("Cannot find the guest binary at the expected location."); + + // Create a new sandbox. + let usandbox = + UninitializedSandbox::new(GuestBinary::FilePath(simple_guest_path), None, None, None)?; + + let mut sbox = usandbox + .evolve(Noop::::default()) + .unwrap(); + + // do the function call + let current_time = std::time::Instant::now(); + let res = sbox.call_guest_function_by_name( + "Echo", + ReturnType::String, + Some(vec![ParameterValue::String("Hello, World!".to_string())]), + )?; + let elapsed = current_time.elapsed(); + println!("Function call finished in {:?}.", elapsed); + assert!(matches!(res, ReturnValue::String(s) if s == "Hello, World!")); + Ok(()) +} diff --git a/src/hyperlight_host/src/mem/shared_mem.rs b/src/hyperlight_host/src/mem/shared_mem.rs index fe6ba8dd9..933918b4b 100644 --- a/src/hyperlight_host/src/mem/shared_mem.rs +++ b/src/hyperlight_host/src/mem/shared_mem.rs @@ -78,7 +78,6 @@ macro_rules! generate_writer { ($fname:ident, $ty:ty) => { /// Write a value of type `$ty` to the memory at the given offset. #[allow(dead_code)] - #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")] pub(crate) fn $fname(&mut self, offset: usize, value: $ty) -> Result<()> { let data = self.as_mut_slice(); bounds_check!(offset, std::mem::size_of::<$ty>(), data.len()); @@ -586,8 +585,7 @@ impl ExclusiveSharedMemory { /// the safety documentation of pointer::offset. /// /// This is ensured by a check in ::new() - #[instrument(skip_all, parent = Span::current(), level= "Trace")] - pub(super) fn as_mut_slice<'a>(&'a mut self) -> &'a mut [u8] { + pub(super) fn as_mut_slice(&mut self) -> &mut [u8] { unsafe { std::slice::from_raw_parts_mut(self.base_ptr(), self.mem_size()) } } @@ -708,7 +706,6 @@ pub trait SharedMemory { /// Return the length of usable memory contained in `self`. /// The returned size does not include the size of the surrounding /// guard pages. - #[instrument(skip_all, parent = Span::current(), level= "Trace")] fn mem_size(&self) -> usize { self.region().size - 2 * PAGE_SIZE_USIZE }