Introduce CPU clock and CPU timer

This commit is contained in:
Chen Chengjun
2024-05-31 18:11:52 +08:00
committed by Tate, Hongliang Tian
parent f1c1011f2b
commit c84efe7a90
10 changed files with 389 additions and 60 deletions

View File

@ -0,0 +1,67 @@
// SPDX-License-Identifier: MPL-2.0
use alloc::sync::Arc;
use core::time::Duration;
use aster_frame::sync::SpinLock;
use crate::time::Clock;
/// A clock used to record the CPU time for processes and threads.
pub struct CpuClock {
time: SpinLock<Duration>,
}
/// A profiling clock that contains a user CPU clock and a kernel CPU clock.
/// These two clocks record the CPU time in user mode and kernel mode respectively.
/// Reading this clock directly returns the sum of both times.
pub struct ProfClock {
user_clock: Arc<CpuClock>,
kernel_clock: Arc<CpuClock>,
}
impl CpuClock {
/// Creates a new `CpuClock`. The recorded time is initialized to 0.
pub fn new() -> Arc<Self> {
Arc::new(Self {
time: SpinLock::new(Duration::ZERO),
})
}
/// Adds `interval` to the original recorded time to update the `CpuClock`.
pub fn add_time(&self, interval: Duration) {
*self.time.lock_irq_disabled() += interval;
}
}
impl Clock for CpuClock {
fn read_time(&self) -> Duration {
*self.time.lock_irq_disabled()
}
}
impl ProfClock {
/// Creates a new `ProfClock`. The recorded time is initialized to 0.
pub fn new() -> Arc<Self> {
Arc::new(Self {
user_clock: CpuClock::new(),
kernel_clock: CpuClock::new(),
})
}
/// Returns a reference to the user CPU clock in this profiling clock.
pub fn user_clock(&self) -> &Arc<CpuClock> {
&self.user_clock
}
/// Returns a reference to the kernel CPU clock in this profiling clock.
pub fn kernel_clock(&self) -> &Arc<CpuClock> {
&self.kernel_clock
}
}
impl Clock for ProfClock {
fn read_time(&self) -> Duration {
self.user_clock.read_time() + self.kernel_clock.read_time()
}
}

View File

@ -1,7 +1,9 @@
// SPDX-License-Identifier: MPL-2.0
pub use cpu_clock::*;
pub use system_wide::*;
mod cpu_clock;
mod system_wide;
pub(super) fn init() {

View File

@ -124,6 +124,11 @@ impl BootTimeClock {
pub fn get() -> &'static Arc<BootTimeClock> {
CLOCK_BOOTTIME_INSTANCE.get().unwrap()
}
/// Get the cpu-local system-wide `TimerManager` singleton of this clock.
pub fn timer_manager() -> &'static Arc<TimerManager> {
CLOCK_BOOTTIME_MANAGER.get().unwrap()
}
}
impl Clock for JiffiesClock {
@ -232,7 +237,7 @@ define_system_clocks! {
CLOCK_BOOTTIME => BootTimeClock,
}
define_timer_managers![CLOCK_REALTIME, CLOCK_MONOTONIC,];
define_timer_managers![CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_BOOTTIME,];
/// Init the system-wide clocks.
fn init_system_wide_clocks() {