Skip to content

Commit c1b3ed8

Browse files
committed
[trace-host] refactor mem_profile logic
- Define a separate struct that holds the functionality related to memory profiling of the guest Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent 92575a3 commit c1b3ed8

File tree

6 files changed

+159
-197
lines changed

6 files changed

+159
-197
lines changed

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ pub(crate) struct HypervLinuxDriver {
296296
gdb_conn: Option<DebugCommChannel<DebugResponse, DebugMsg>>,
297297
#[cfg(crashdump)]
298298
rt_cfg: SandboxRuntimeConfig,
299+
#[allow(dead_code)]
299300
#[cfg(feature = "trace_guest")]
300301
trace_info: TraceInfo,
301302
}
@@ -684,14 +685,6 @@ impl Hypervisor for HypervLinuxDriver {
684685
{
685686
Err(mshv_ioctls::MshvError::from(libc::EINTR))
686687
} else {
687-
#[cfg(feature = "trace_guest")]
688-
if self.trace_info.guest_start_epoch.is_none() {
689-
// Store the guest start epoch and cycles to trace the guest execution time
690-
crate::debug!("MSHV - Guest Start Epoch set");
691-
self.trace_info.guest_start_tsc =
692-
Some(hyperlight_guest_tracing::invariant_tsc::read_tsc());
693-
self.trace_info.guest_start_epoch = Some(std::time::Instant::now());
694-
}
695688
// Note: if a `InterruptHandle::kill()` called while this thread is **here**
696689
// Then the vcpu will run, but we will keep sending signals to this thread
697690
// to interrupt it until `running` is set to false. The `vcpu_fd::run()` call will
@@ -1093,7 +1086,7 @@ impl Hypervisor for HypervLinuxDriver {
10931086
}
10941087
}
10951088

1096-
#[cfg(feature = "trace_guest")]
1089+
#[cfg(feature = "mem_profile")]
10971090
fn trace_info_mut(&mut self) -> &mut TraceInfo {
10981091
&mut self.trace_info
10991092
}

src/hyperlight_host/src/hypervisor/hyperv_windows.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -569,14 +569,6 @@ impl Hypervisor for HypervWindowsDriver {
569569
Reserved: Default::default(),
570570
}
571571
} else {
572-
#[cfg(feature = "trace_guest")]
573-
if self.trace_info.guest_start_epoch.is_none() {
574-
// Store the guest start epoch and cycles to trace the guest execution time
575-
crate::debug!("HyperV - Guest Start Epoch set");
576-
self.trace_info.guest_start_tsc =
577-
Some(hyperlight_guest_tracing::invariant_tsc::read_tsc());
578-
self.trace_info.guest_start_epoch = Some(std::time::Instant::now());
579-
}
580572
self.processor.run()?
581573
};
582574
self.interrupt_handle
@@ -940,7 +932,7 @@ impl Hypervisor for HypervWindowsDriver {
940932
}
941933
}
942934

943-
#[cfg(feature = "trace_guest")]
935+
#[cfg(feature = "mem_profile")]
944936
fn trace_info_mut(&mut self) -> &mut TraceInfo {
945937
&mut self.trace_info
946938
}

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -646,15 +646,6 @@ impl Hypervisor for KVMDriver {
646646
{
647647
Err(kvm_ioctls::Error::new(libc::EINTR))
648648
} else {
649-
#[cfg(feature = "trace_guest")]
650-
if self.trace_info.guest_start_epoch.is_none() {
651-
// Store the guest start epoch and cycles to trace the guest execution time
652-
crate::debug!("KVM - Guest Start Epoch set");
653-
self.trace_info.guest_start_epoch = Some(std::time::Instant::now());
654-
self.trace_info.guest_start_tsc =
655-
Some(hyperlight_guest_tracing::invariant_tsc::read_tsc());
656-
}
657-
658649
// Note: if a `InterruptHandle::kill()` called while this thread is **here**
659650
// Then the vcpu will run, but we will keep sending signals to this thread
660651
// to interrupt it until `running` is set to false. The `vcpu_fd::run()` call will
@@ -1030,7 +1021,7 @@ impl Hypervisor for KVMDriver {
10301021
}
10311022
}
10321023

1033-
#[cfg(feature = "trace_guest")]
1024+
#[cfg(feature = "mem_profile")]
10341025
fn trace_info_mut(&mut self) -> &mut TraceInfo {
10351026
&mut self.trace_info
10361027
}

src/hyperlight_host/src/hypervisor/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::hypervisor::regs::{
2424
};
2525
use crate::mem::memory_region::{MemoryRegion, MemoryRegionFlags};
2626
use crate::metrics::METRIC_GUEST_CANCELLATION;
27-
#[cfg(feature = "trace_guest")]
27+
#[cfg(feature = "mem_profile")]
2828
use crate::sandbox::trace::TraceInfo;
2929
use crate::{HyperlightError, Result, log_then_return};
3030

@@ -314,7 +314,7 @@ pub(crate) trait Hypervisor: Debug + Send {
314314
fn check_stack_guard(&self) -> Result<bool>;
315315

316316
/// Get a mutable reference of the trace info for the guest
317-
#[cfg(feature = "trace_guest")]
317+
#[cfg(feature = "mem_profile")]
318318
fn trace_info_mut(&mut self) -> &mut TraceInfo;
319319
}
320320

src/hyperlight_host/src/sandbox/outb.rs

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

0 commit comments

Comments
 (0)