hyperlight_host/metrics/
mod.rs

1/*
2Copyright 2025  The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17// Counter metric that counter number of times a guest error occurred
18pub(crate) static METRIC_GUEST_ERROR: &str = "guest_errors_total";
19pub(crate) static METRIC_GUEST_ERROR_LABEL_CODE: &str = "code";
20
21// Counter metric that counts the number of times a guest function was called due to timing out
22pub(crate) static METRIC_GUEST_CANCELLATION: &str = "guest_cancellations_total";
23
24// Counter metric that counts the number of times a vCPU was erroneously kicked by a stale cancellation
25// This can happen in two scenarios:
26// 1. Linux: A signal from a previous guest call arrives late and interrupts a new call
27// 2. Windows: WHvCancelRunVirtualProcessor is called right after vCPU exits but RUNNING_BIT is still true
28pub(crate) static METRIC_ERRONEOUS_VCPU_KICKS: &str = "erroneous_vcpu_kicks_total";
29
30// Histogram metric that measures the duration of guest function calls
31#[cfg(feature = "function_call_metrics")]
32pub(crate) static METRIC_GUEST_FUNC_DURATION: &str = "guest_call_duration_seconds";
33
34// Histogram metric that measures the duration of host function calls
35#[cfg(feature = "function_call_metrics")]
36pub(crate) static METRIC_HOST_FUNC_DURATION: &str = "host_call_duration_seconds";
37
38/// If the the `function_call_metrics` feature is enabled, this function measures
39/// the time it takes to execute the given closure, and will then emit a guest call metric
40/// with the given function name.
41///
42/// If the feature is not enabled, the given closure is executed without any additional metrics being emitted,
43/// and the result of the closure is returned directly.
44pub(crate) fn maybe_time_and_emit_guest_call<T, F: FnOnce() -> T>(
45    #[allow(unused_variables)] name: &str,
46    f: F,
47) -> T {
48    cfg_if::cfg_if! {
49        if #[cfg(feature = "function_call_metrics")] {
50            use std::time::Instant;
51
52            let start = Instant::now();
53            let result = f();
54            let duration = start.elapsed();
55
56            static LABEL_GUEST_FUNC_NAME: &str = "function_name";
57            metrics::histogram!(METRIC_GUEST_FUNC_DURATION, LABEL_GUEST_FUNC_NAME => name.to_string()).record(duration);
58            result
59        } else {
60            f()
61        }
62    }
63}
64
65/// If the the `function_call_metrics` feature is enabled, this function measures
66/// the time it takes to execute the given closure, and will then emit a host call metric
67/// with the given function name.
68///
69/// If the feature is not enabled, the given closure is executed without any additional metrics being emitted,
70/// and the result of the closure is returned directly.
71pub(crate) fn maybe_time_and_emit_host_call<T, F: FnOnce() -> T>(
72    #[allow(unused_variables)] name: &str,
73    f: F,
74) -> T {
75    cfg_if::cfg_if! {
76        if #[cfg(feature = "function_call_metrics")] {
77            use std::time::Instant;
78
79            let start = Instant::now();
80            let result = f();
81            let duration = start.elapsed();
82
83            static LABEL_HOST_FUNC_NAME: &str = "function_name";
84            metrics::histogram!(METRIC_HOST_FUNC_DURATION, LABEL_HOST_FUNC_NAME => name.to_string()).record(duration);
85            result
86        } else {
87            f()
88        }
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use std::thread;
95    use std::time::Duration;
96
97    use hyperlight_testing::simple_guest_as_string;
98    use metrics::{Key, with_local_recorder};
99    use metrics_util::CompositeKey;
100
101    use super::*;
102    use crate::{GuestBinary, UninitializedSandbox};
103
104    #[test]
105    fn test_metrics_are_emitted() {
106        let recorder = metrics_util::debugging::DebuggingRecorder::new();
107        let snapshotter = recorder.snapshotter();
108        let snapshot = with_local_recorder(&recorder, || {
109            let uninit = UninitializedSandbox::new(
110                GuestBinary::FilePath(simple_guest_as_string().unwrap()),
111                None,
112            )
113            .unwrap();
114
115            let mut multi = uninit.evolve().unwrap();
116            let interrupt_handle = multi.interrupt_handle();
117
118            // interrupt the guest function call to "Spin" after 1 second
119            let thread = thread::spawn(move || {
120                thread::sleep(Duration::from_secs(1));
121                assert!(interrupt_handle.kill());
122            });
123
124            multi
125                .call::<i32>("PrintOutput", "Hello".to_string())
126                .unwrap();
127
128            multi.call::<i32>("Spin", ()).unwrap_err();
129            thread.join().unwrap();
130
131            snapshotter.snapshot()
132        });
133
134        // Convert snapshot into a hashmap for easier lookup
135        #[expect(clippy::mutable_key_type)]
136        let snapshot = snapshot.into_hashmap();
137
138        cfg_if::cfg_if! {
139            if #[cfg(feature = "function_call_metrics")] {
140                use metrics::Label;
141
142                let expected_num_metrics = 4;
143
144                // Verify that the histogram metrics are recorded correctly
145                assert_eq!(snapshot.len(), expected_num_metrics);
146
147                // 1. Guest call duration
148                let histogram_key = CompositeKey::new(
149                    metrics_util::MetricKind::Histogram,
150                    Key::from_parts(
151                        METRIC_GUEST_FUNC_DURATION,
152                        vec![Label::new("function_name", "PrintOutput")],
153                    ),
154                );
155                let histogram_value = &snapshot.get(&histogram_key).unwrap().2;
156                assert!(
157                    matches!(
158                        histogram_value,
159                        metrics_util::debugging::DebugValue::Histogram(histogram) if histogram.len() == 1
160                    ),
161                    "Histogram metric does not match expected value"
162                );
163
164                // 2. Guest cancellation
165                let counter_key = CompositeKey::new(
166                    metrics_util::MetricKind::Counter,
167                    Key::from_name(METRIC_GUEST_CANCELLATION),
168                );
169                assert_eq!(
170                    snapshot.get(&counter_key).unwrap().2,
171                    metrics_util::debugging::DebugValue::Counter(1)
172                );
173
174                // 3. Guest call duration
175                let histogram_key = CompositeKey::new(
176                    metrics_util::MetricKind::Histogram,
177                    Key::from_parts(
178                        METRIC_GUEST_FUNC_DURATION,
179                        vec![Label::new("function_name", "Spin")],
180                    ),
181                );
182                let histogram_value = &snapshot.get(&histogram_key).unwrap().2;
183                assert!(
184                    matches!(
185                        histogram_value,
186                        metrics_util::debugging::DebugValue::Histogram(histogram) if histogram.len() == 1
187                    ),
188                    "Histogram metric does not match expected value"
189                );
190            } else {
191                // Verify that the counter metrics are recorded correctly
192                assert_eq!(snapshot.len(), 1);
193
194                let counter_key = CompositeKey::new(
195                    metrics_util::MetricKind::Counter,
196                    Key::from_name(METRIC_GUEST_CANCELLATION),
197                );
198                assert_eq!(
199                    snapshot.get(&counter_key).unwrap().2,
200                    metrics_util::debugging::DebugValue::Counter(1)
201                );
202            }
203        }
204    }
205}