vmm/devices/pseudo/
boot_timer.rs

1// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use std::sync::{Arc, Barrier};
5
6use utils::time::TimestampUs;
7
8use crate::logger::info;
9use crate::vstate::bus::BusDevice;
10
11const MAGIC_VALUE_SIGNAL_GUEST_BOOT_COMPLETE: u8 = 123;
12
13/// Pseudo device to record the kernel boot time.
14#[derive(Debug, Clone)]
15pub struct BootTimer {
16    start_ts: TimestampUs,
17}
18
19impl BusDevice for BootTimer {
20    fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option<Arc<Barrier>> {
21        // Only handle byte length instructions at a zero offset.
22        if data.len() != 1 || offset != 0 {
23            return None;
24        }
25
26        if data[0] == MAGIC_VALUE_SIGNAL_GUEST_BOOT_COMPLETE {
27            let now_tm_us = TimestampUs::default();
28
29            let boot_time_us = now_tm_us.time_us - self.start_ts.time_us;
30            let boot_time_cpu_us = now_tm_us.cputime_us - self.start_ts.cputime_us;
31            info!(
32                "Guest-boot-time = {:>6} us {} ms, {:>6} CPU us {} CPU ms",
33                boot_time_us,
34                boot_time_us / 1000,
35                boot_time_cpu_us,
36                boot_time_cpu_us / 1000
37            );
38        }
39
40        None
41    }
42
43    fn read(&mut self, _base: u64, _offset: u64, _data: &mut [u8]) {}
44}
45
46impl BootTimer {
47    /// Create a device at a certain point in time.
48    pub fn new(start_ts: TimestampUs) -> BootTimer {
49        BootTimer { start_ts }
50    }
51}