-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathgettime.rs
More file actions
48 lines (44 loc) · 1.78 KB
/
Copy pathgettime.rs
File metadata and controls
48 lines (44 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use core::sync::atomic::Ordering;
use core::time::Duration;
use libkernel::{
error::{KernelError, Result},
memory::address::TUA,
};
use crate::clock::{ClockId, realtime::date, timespec::TimeSpec};
use crate::drivers::timer::{Instant, now};
use crate::sched::syscall_ctx::ProcessCtx;
use crate::{drivers::timer::uptime, memory::uaccess::copy_to_user};
pub async fn sys_clock_gettime(
ctx: &ProcessCtx,
clockid: i32,
time_spec: TUA<TimeSpec>,
) -> Result<usize> {
let time = match ClockId::try_from(clockid).map_err(|_| KernelError::InvalidValue)? {
ClockId::Realtime => date(),
ClockId::Monotonic => uptime(),
ClockId::ProcessCpuTimeId => {
let task = ctx.shared();
let total_time = task.process.stime.load(Ordering::Relaxed) as u64
+ task.process.utime.load(Ordering::Relaxed) as u64;
let last_update = Instant::from_user_normalized(
task.process.last_account.load(Ordering::Relaxed) as u64,
);
let now = now().unwrap();
let delta = now - last_update;
Duration::from(Instant::from_user_normalized(total_time)) + delta
}
ClockId::ThreadCpuTimeId => {
let task = ctx.shared();
let total_time = task.stime.load(Ordering::Relaxed) as u64
+ task.utime.load(Ordering::Relaxed) as u64;
let last_update =
Instant::from_user_normalized(task.last_account.load(Ordering::Relaxed) as u64);
let now = now().unwrap();
let delta = now - last_update;
Duration::from(Instant::from_user_normalized(total_time)) + delta
}
_ => return Err(KernelError::InvalidValue),
};
copy_to_user(time_spec, time.into()).await?;
Ok(0)
}