nub_host_kvm/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}