Skip to content

Commit 6a7fd75

Browse files
Copilotsimongdavies
andcommitted
Refactor to reduce code duplication in metric tracking
Co-authored-by: simongdavies <1397489+simongdavies@users.noreply.github.com>
1 parent 32b77f9 commit 6a7fd75

3 files changed

Lines changed: 55 additions & 49 deletions

File tree

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -805,20 +805,22 @@ impl Hypervisor for HypervLinuxDriver {
805805
HyperlightExit::Cancelled()
806806
} else {
807807
#[cfg(gdb)]
808-
if debug_interrupt {
809-
self.interrupt_handle
810-
.debug_interrupt
811-
.store(false, Ordering::Relaxed);
812-
813-
// If the vCPU was stopped because of an interrupt, we need to
814-
// return a special exit reason so that the gdb thread can handle it
815-
// and resume execution
816-
HyperlightExit::Debug(VcpuStopReason::Interrupt)
817-
} else {
818-
// Track erroneous vCPU kick - stale signal from previous call
819-
metrics::counter!(crate::metrics::METRIC_ERRONEOUS_VCPU_KICK)
820-
.increment(1);
821-
HyperlightExit::Retry()
808+
{
809+
if debug_interrupt {
810+
self.interrupt_handle
811+
.debug_interrupt
812+
.store(false, Ordering::Relaxed);
813+
814+
// If the vCPU was stopped because of an interrupt, we need to
815+
// return a special exit reason so that the gdb thread can handle it
816+
// and resume execution
817+
HyperlightExit::Debug(VcpuStopReason::Interrupt)
818+
} else {
819+
// Track erroneous vCPU kick - stale signal from previous call
820+
metrics::counter!(crate::metrics::METRIC_ERRONEOUS_VCPU_KICK)
821+
.increment(1);
822+
HyperlightExit::Retry()
823+
}
822824
}
823825

824826
#[cfg(not(gdb))]

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -662,27 +662,29 @@ impl Hypervisor for HypervWindowsDriver {
662662
WHV_RUN_VP_EXIT_REASON(8193i32) => {
663663
debug!("HyperV Cancelled Details :\n {:#?}", &self);
664664
#[cfg(gdb)]
665-
if debug_interrupt {
666-
self.interrupt_handle
667-
.debug_interrupt
668-
.store(false, Ordering::Relaxed);
669-
670-
// If the vCPU was stopped because of an interrupt, we need to
671-
// return a special exit reason so that the gdb thread can handle it
672-
// and resume execution
673-
HyperlightExit::Debug(VcpuStopReason::Interrupt)
674-
} else if !cancel_was_requested_manually {
675-
// This was an internal cancellation
676-
// The virtualization stack can use this function to return the control
677-
// of a virtual processor back to the virtualization stack in case it
678-
// needs to change the state of a VM or to inject an event into the processor
679-
// see https://learn.microsoft.com/en-us/virtualization/api/hypervisor-platform/funcs/whvcancelrunvirtualprocessor#remarks
680-
debug!("Internal cancellation detected, returning Retry error");
681-
// Track erroneous vCPU kick - internal cancellation not requested by user
682-
metrics::counter!(crate::metrics::METRIC_ERRONEOUS_VCPU_KICK).increment(1);
683-
HyperlightExit::Retry()
684-
} else {
685-
HyperlightExit::Cancelled()
665+
{
666+
if debug_interrupt {
667+
self.interrupt_handle
668+
.debug_interrupt
669+
.store(false, Ordering::Relaxed);
670+
671+
// If the vCPU was stopped because of an interrupt, we need to
672+
// return a special exit reason so that the gdb thread can handle it
673+
// and resume execution
674+
HyperlightExit::Debug(VcpuStopReason::Interrupt)
675+
} else if !cancel_was_requested_manually {
676+
// This was an internal cancellation
677+
// The virtualization stack can use this function to return the control
678+
// of a virtual processor back to the virtualization stack in case it
679+
// needs to change the state of a VM or to inject an event into the processor
680+
// see https://learn.microsoft.com/en-us/virtualization/api/hypervisor-platform/funcs/whvcancelrunvirtualprocessor#remarks
681+
debug!("Internal cancellation detected, returning Retry error");
682+
// Track erroneous vCPU kick - internal cancellation not requested by user
683+
metrics::counter!(crate::metrics::METRIC_ERRONEOUS_VCPU_KICK).increment(1);
684+
HyperlightExit::Retry()
685+
} else {
686+
HyperlightExit::Cancelled()
687+
}
686688
}
687689

688690
#[cfg(not(gdb))]

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -750,20 +750,22 @@ impl Hypervisor for KVMDriver {
750750
HyperlightExit::Cancelled()
751751
} else {
752752
#[cfg(gdb)]
753-
if debug_interrupt {
754-
self.interrupt_handle
755-
.debug_interrupt
756-
.store(false, Ordering::Relaxed);
757-
758-
// If the vCPU was stopped because of an interrupt, we need to
759-
// return a special exit reason so that the gdb thread can handle it
760-
// and resume execution
761-
HyperlightExit::Debug(VcpuStopReason::Interrupt)
762-
} else {
763-
// Track erroneous vCPU kick - stale signal from previous call
764-
metrics::counter!(crate::metrics::METRIC_ERRONEOUS_VCPU_KICK)
765-
.increment(1);
766-
HyperlightExit::Retry()
753+
{
754+
if debug_interrupt {
755+
self.interrupt_handle
756+
.debug_interrupt
757+
.store(false, Ordering::Relaxed);
758+
759+
// If the vCPU was stopped because of an interrupt, we need to
760+
// return a special exit reason so that the gdb thread can handle it
761+
// and resume execution
762+
HyperlightExit::Debug(VcpuStopReason::Interrupt)
763+
} else {
764+
// Track erroneous vCPU kick - stale signal from previous call
765+
metrics::counter!(crate::metrics::METRIC_ERRONEOUS_VCPU_KICK)
766+
.increment(1);
767+
HyperlightExit::Retry()
768+
}
767769
}
768770

769771
#[cfg(not(gdb))]

0 commit comments

Comments
 (0)