starry_api/imp/
time.rs

1use axerrno::{LinuxError, LinuxResult};
2use axhal::time::{monotonic_time, monotonic_time_nanos, nanos_to_ticks, wall_time};
3use linux_raw_sys::general::{
4    __kernel_clockid_t, CLOCK_MONOTONIC, CLOCK_REALTIME, timespec, timeval,
5};
6use starry_core::task::time_stat_output;
7
8use crate::{ptr::UserPtr, time::TimeValueLike};
9
10pub fn sys_clock_gettime(
11    clock_id: __kernel_clockid_t,
12    ts: UserPtr<timespec>,
13) -> LinuxResult<isize> {
14    let now = match clock_id as u32 {
15        CLOCK_REALTIME => wall_time(),
16        CLOCK_MONOTONIC => monotonic_time(),
17        _ => {
18            warn!(
19                "Called sys_clock_gettime for unsupported clock {}",
20                clock_id
21            );
22            return Err(LinuxError::EINVAL);
23        }
24    };
25    *ts.get_as_mut()? = timespec::from_time_value(now);
26    Ok(0)
27}
28
29pub fn sys_gettimeofday(ts: UserPtr<timeval>) -> LinuxResult<isize> {
30    *ts.get_as_mut()? = timeval::from_time_value(wall_time());
31    Ok(0)
32}
33
34#[repr(C)]
35pub struct Tms {
36    /// user time
37    tms_utime: usize,
38    /// system time
39    tms_stime: usize,
40    /// user time of children
41    tms_cutime: usize,
42    /// system time of children
43    tms_cstime: usize,
44}
45
46pub fn sys_times(tms: UserPtr<Tms>) -> LinuxResult<isize> {
47    let (_, utime_us, _, stime_us) = time_stat_output();
48    *tms.get_as_mut()? = Tms {
49        tms_utime: utime_us,
50        tms_stime: stime_us,
51        tms_cutime: utime_us,
52        tms_cstime: stime_us,
53    };
54    Ok(nanos_to_ticks(monotonic_time_nanos()) as _)
55}