Skip to content

Commit fadcc56

Browse files
committed
[trace-host] refactor mem_profile logic
- Define a separate struct that holds the functionality related to memory profiling of the guest
1 parent c9bd6cf commit fadcc56

File tree

6 files changed

+161
-198
lines changed

6 files changed

+161
-198
lines changed

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ pub(crate) struct HypervLinuxDriver {
320320
gdb_conn: Option<DebugCommChannel<DebugResponse, DebugMsg>>,
321321
#[cfg(crashdump)]
322322
rt_cfg: SandboxRuntimeConfig,
323+
#[allow(dead_code)]
323324
#[cfg(feature = "trace_guest")]
324325
trace_info: TraceInfo,
325326
}
@@ -774,14 +775,6 @@ impl Hypervisor for HypervLinuxDriver {
774775
{
775776
Err(mshv_ioctls::MshvError::from(libc::EINTR))
776777
} else {
777-
#[cfg(feature = "trace_guest")]
778-
if self.trace_info.guest_start_epoch.is_none() {
779-
// Store the guest start epoch and cycles to trace the guest execution time
780-
crate::debug!("MSHV - Guest Start Epoch set");
781-
self.trace_info.guest_start_tsc =
782-
Some(hyperlight_guest_tracing::invariant_tsc::read_tsc());
783-
self.trace_info.guest_start_epoch = Some(std::time::Instant::now());
784-
}
785778
// Note: if a `InterruptHandle::kill()` called while this thread is **here**
786779
// Then the vcpu will run, but we will keep sending signals to this thread
787780
// to interrupt it until `running` is set to false. The `vcpu_fd::run()` call will
@@ -1147,7 +1140,7 @@ impl Hypervisor for HypervLinuxDriver {
11471140
Ok(X86_64Regs::from(self.vcpu_fd.get_regs()?))
11481141
}
11491142

1150-
#[cfg(feature = "trace_guest")]
1143+
#[cfg(feature = "mem_profile")]
11511144
fn trace_info_mut(&mut self) -> &mut TraceInfo {
11521145
&mut self.trace_info
11531146
}

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -761,14 +761,6 @@ impl Hypervisor for HypervWindowsDriver {
761761
Reserved: Default::default(),
762762
}
763763
} else {
764-
#[cfg(feature = "trace_guest")]
765-
if self.trace_info.guest_start_epoch.is_none() {
766-
// Store the guest start epoch and cycles to trace the guest execution time
767-
crate::debug!("HyperV - Guest Start Epoch set");
768-
self.trace_info.guest_start_tsc =
769-
Some(hyperlight_guest_tracing::invariant_tsc::read_tsc());
770-
self.trace_info.guest_start_epoch = Some(std::time::Instant::now());
771-
}
772764
self.processor.run()?
773765
};
774766
self.interrupt_handle
@@ -1100,7 +1092,7 @@ impl Hypervisor for HypervWindowsDriver {
11001092
Ok(X86_64Regs::from(regs))
11011093
}
11021094

1103-
#[cfg(feature = "trace_guest")]
1095+
#[cfg(feature = "mem_profile")]
11041096
fn trace_info_mut(&mut self) -> &mut TraceInfo {
11051097
&mut self.trace_info
11061098
}

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -693,15 +693,6 @@ impl Hypervisor for KVMDriver {
693693
{
694694
Err(kvm_ioctls::Error::new(libc::EINTR))
695695
} else {
696-
#[cfg(feature = "trace_guest")]
697-
if self.trace_info.guest_start_epoch.is_none() {
698-
// Store the guest start epoch and cycles to trace the guest execution time
699-
crate::debug!("KVM - Guest Start Epoch set");
700-
self.trace_info.guest_start_epoch = Some(std::time::Instant::now());
701-
self.trace_info.guest_start_tsc =
702-
Some(hyperlight_guest_tracing::invariant_tsc::read_tsc());
703-
}
704-
705696
// Note: if a `InterruptHandle::kill()` called while this thread is **here**
706697
// Then the vcpu will run, but we will keep sending signals to this thread
707698
// to interrupt it until `running` is set to false. The `vcpu_fd::run()` call will
@@ -1041,7 +1032,7 @@ impl Hypervisor for KVMDriver {
10411032
Ok(X86_64Regs::from(self.vcpu_fd.get_regs()?))
10421033
}
10431034

1044-
#[cfg(feature = "trace_guest")]
1035+
#[cfg(feature = "mem_profile")]
10451036
fn trace_info_mut(&mut self) -> &mut TraceInfo {
10461037
&mut self.trace_info
10471038
}

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::HyperlightError::StackOverflow;
2121
use crate::error::HyperlightError::ExecutionCanceledByHost;
2222
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
2323
use crate::metrics::METRIC_GUEST_CANCELLATION;
24-
#[cfg(feature = "trace_guest")]
24+
#[cfg(feature = "mem_profile")]
2525
use crate::sandbox::trace::TraceInfo;
2626
use crate::{HyperlightError, Result, log_then_return};
2727

@@ -233,11 +233,12 @@ pub(crate) trait Hypervisor: Debug + Send {
233233
fn check_stack_guard(&self) -> Result<bool>;
234234

235235
/// Read a register for trace/unwind purposes
236+
#[allow(dead_code)]
236237
#[cfg(feature = "trace_guest")]
237238
fn read_regs(&self) -> Result<arch::X86_64Regs>;
238239

239240
/// Get a mutable reference of the trace info for the guest
240-
#[cfg(feature = "trace_guest")]
241+
#[cfg(feature = "mem_profile")]
241242
fn trace_info_mut(&mut self) -> &mut TraceInfo;
242243
}
243244

src/hyperlight_host/src/sandbox/outb.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,14 @@ pub(crate) fn handle_outb(
184184
#[cfg(feature = "mem_profile")]
185185
OutBAction::TraceMemoryAlloc => {
186186
let regs = _hv.read_regs()?;
187-
crate::sandbox::trace::handle_trace_memory_alloc(&regs, mem_mgr, _hv.trace_info_mut())
187+
let trace_info = _hv.trace_info_mut();
188+
trace_info.handle_trace_mem_alloc(&regs, mem_mgr.as_ref())
188189
}
189190
#[cfg(feature = "mem_profile")]
190191
OutBAction::TraceMemoryFree => {
191192
let regs = _hv.read_regs()?;
192-
crate::sandbox::trace::handle_trace_memory_free(&regs, mem_mgr, _hv.trace_info_mut())
193+
let trace_info = _hv.trace_info_mut();
194+
trace_info.handle_trace_mem_free(&regs, mem_mgr.as_ref())
193195
}
194196
}
195197
}

0 commit comments

Comments
 (0)