Skip to content

Commit 24fcf22

Browse files
committed
refactor: simplify creating RTCDevice
We were always constructing RTCDevice using a set of metrics that were defined in the RTC module itself. Don't leak the metrics to other modules. Instead, create a new() function that always constructs it the correct way. Signed-off-by: Babis Chalios <bchalios@amazon.es>
1 parent 924432c commit 24fcf22

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/vmm/src/builder.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use userfaultfd::Uffd;
1515
use utils::time::TimestampUs;
1616
#[cfg(target_arch = "aarch64")]
1717
use vm_memory::GuestAddress;
18-
#[cfg(target_arch = "aarch64")]
19-
use vm_superio::Rtc;
2018

2119
use crate::arch::{ConfigurationError, configure_system_for_boot, load_kernel};
2220
#[cfg(target_arch = "aarch64")]
@@ -628,9 +626,7 @@ fn attach_legacy_devices_aarch64(
628626
.map_err(VmmError::RegisterMMIODevice)?;
629627
}
630628

631-
let rtc = RTCDevice(Rtc::with_events(
632-
&crate::devices::legacy::rtc_pl031::METRICS,
633-
));
629+
let rtc = RTCDevice::new();
634630
vmm.mmio_device_manager
635631
.register_mmio_rtc(&mut vmm.resource_allocator, rtc, None)
636632
.map_err(VmmError::RegisterMMIODevice)

src/vmm/src/device_manager/persist.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,7 @@ impl<'a> Persist<'a> for MMIODeviceManager {
449449
)?;
450450
}
451451
if state.type_ == DeviceType::Rtc {
452-
let rtc = RTCDevice(vm_superio::Rtc::with_events(
453-
&crate::devices::legacy::rtc_pl031::METRICS,
454-
));
452+
let rtc = RTCDevice::new();
455453
constructor_args
456454
.resource_allocator
457455
.allocate_mmio_memory(

src/vmm/src/devices/legacy/rtc_pl031.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use std::convert::TryInto;
55

66
use serde::Serialize;
7+
use vm_superio::Rtc;
78
use vm_superio::rtc_pl031::RtcEvents;
89

910
use crate::logger::{IncMetric, SharedIncMetric, warn};
@@ -59,7 +60,19 @@ pub static METRICS: RTCDeviceMetrics = RTCDeviceMetrics::new();
5960

6061
/// Wrapper over vm_superio's RTC implementation.
6162
#[derive(Debug)]
62-
pub struct RTCDevice(pub vm_superio::Rtc<&'static RTCDeviceMetrics>);
63+
pub struct RTCDevice(vm_superio::Rtc<&'static RTCDeviceMetrics>);
64+
65+
impl Default for RTCDevice {
66+
fn default() -> Self {
67+
Self::new()
68+
}
69+
}
70+
71+
impl RTCDevice {
72+
pub fn new() -> RTCDevice {
73+
RTCDevice(Rtc::with_events(&METRICS))
74+
}
75+
}
6376

6477
impl std::ops::Deref for RTCDevice {
6578
type Target = vm_superio::Rtc<&'static RTCDeviceMetrics>;

0 commit comments

Comments
 (0)