From dc76c5b7a82a9c8db6345d40aaa076c540b04c1c Mon Sep 17 00:00:00 2001 From: Mark Poliakov Date: Sun, 5 Jan 2025 12:25:22 +0200 Subject: [PATCH] abi: implement time functions --- kernel/libk/src/config/general.rs | 2 +- kernel/libk/src/task/runtime/timer.rs | 2 +- kernel/libk/src/task/sched.rs | 2 +- kernel/src/syscall/imp/sys_process.rs | 2 +- lib/abi/src/time.rs | 44 +++++++++++++++++++++++++-- 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/kernel/libk/src/config/general.rs b/kernel/libk/src/config/general.rs index 26c8a868..5cdcafdb 100644 --- a/kernel/libk/src/config/general.rs +++ b/kernel/libk/src/config/general.rs @@ -18,7 +18,7 @@ pub struct DebugOptions { impl Default for DebugOptions { fn default() -> Self { Self { - serial_level: LogLevel::Debug, + serial_level: LogLevel::Info, display_level: LogLevel::Info, disable_program_trace: false, } diff --git a/kernel/libk/src/task/runtime/timer.rs b/kernel/libk/src/task/runtime/timer.rs index a40a7de4..fddfee3a 100644 --- a/kernel/libk/src/task/runtime/timer.rs +++ b/kernel/libk/src/task/runtime/timer.rs @@ -85,7 +85,7 @@ pub struct SleepFuture { impl SleepFuture { pub fn remaining(&self) -> Option { let now = monotonic_time(); - match self.deadline.checked_sub(&now) { + match self.deadline.checked_sub_time(&now) { Some(duration) if !duration.is_zero() => Some(duration), _ => None, } diff --git a/kernel/libk/src/task/sched.rs b/kernel/libk/src/task/sched.rs index 4ee9b838..d4dcc5f3 100644 --- a/kernel/libk/src/task/sched.rs +++ b/kernel/libk/src/task/sched.rs @@ -179,7 +179,7 @@ impl Scheduler for CpuQueue { let t = monotonic_time(); if let Some(t0) = self.last_stats_measure.get() { - if let Some(dt) = t.checked_sub(&t0) { + if let Some(dt) = t.checked_sub_time(&t0) { self.update_stats(dt, current.as_ref()); } } diff --git a/kernel/src/syscall/imp/sys_process.rs b/kernel/src/syscall/imp/sys_process.rs index 82088a1c..efe81df7 100644 --- a/kernel/src/syscall/imp/sys_process.rs +++ b/kernel/src/syscall/imp/sys_process.rs @@ -242,7 +242,7 @@ pub(crate) fn nanosleep( Ok(()) => Ok(()), Err(Error::Interrupted) => { let now = monotonic_time(); - let rem = deadline.checked_sub(&now).unwrap_or_default(); + let rem = deadline.checked_sub_time(&now).unwrap_or_default(); remaining.write(rem); Err(Error::Interrupted) } diff --git a/lib/abi/src/time.rs b/lib/abi/src/time.rs index cd8efc68..02e8ba66 100644 --- a/lib/abi/src/time.rs +++ b/lib/abi/src/time.rs @@ -48,7 +48,48 @@ impl SystemTime { + (self.nanoseconds as u128 / 1000000) } - pub fn checked_sub(&self, other: &SystemTime) -> Option { + pub fn checked_add_duration(&self, other: &Duration) -> Option { + let nanoseconds = self.nanoseconds.checked_add(other.subsec_nanos() as u64)?; + let seconds = nanoseconds / NANOSECONDS_IN_SECOND; + let nanoseconds = nanoseconds % NANOSECONDS_IN_SECOND; + let seconds = self + .seconds + .checked_add(other.as_secs())? + .checked_add(seconds)?; + + Some(Self { + seconds, + nanoseconds, + }) + } + + pub fn checked_sub_duration(&self, other: &Duration) -> Option { + let mut seconds = self.seconds.checked_sub(other.as_secs())?; + let nanoseconds = if self.nanoseconds < other.subsec_nanos() as u64 { + seconds = seconds.checked_sub(1)?; + NANOSECONDS_IN_SECOND + self.seconds - other.subsec_nanos() as u64 + } else { + self.nanoseconds - other.subsec_nanos() as u64 + }; + + Some(Self { + seconds, + nanoseconds, + }) + } + + pub fn sub_time(&self, other: &SystemTime) -> Result { + if self >= other { + Ok(self.checked_sub_time(other).unwrap()) + } else { + match other.sub_time(self) { + Ok(d) => Err(d), + Err(d) => Ok(d), + } + } + } + + pub fn checked_sub_time(&self, other: &SystemTime) -> Option { let mut seconds = self.seconds.checked_sub(other.seconds)?; let nanoseconds = if self.nanoseconds < other.nanoseconds { seconds = seconds.checked_sub(1)?; @@ -92,7 +133,6 @@ mod tests { seconds: 1, nanoseconds: 143000000, }; - assert_eq!(t1.checked_sub(&t0), Some(Duration::from_millis(20))); assert_eq!(t0 + Duration::from_millis(20), t1); assert_eq!(t1.as_millis(), 1143); }