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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use alloc::sync::Arc;
use core::time::Duration;
use rcore_fs::vfs::*;
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
pub struct TimeSpec {
pub sec: usize,
pub nsec: usize,
}
#[repr(C)]
#[derive(Debug, Copy, Clone, Default)]
pub struct TimeVal {
pub sec: usize,
pub usec: usize,
}
impl TimeVal {
pub fn now() -> TimeVal {
TimeSpec::now().into()
}
pub fn to_msec(&self) -> usize {
self.sec * 1_000 + self.usec / 1_000
}
}
impl TimeSpec {
pub fn now() -> TimeSpec {
let time = kernel_hal::timer::timer_now();
TimeSpec {
sec: time.as_secs() as usize,
nsec: (time.as_nanos() % 1_000_000_000) as usize,
}
}
pub fn update(inode: &Arc<dyn INode>) {
let now = TimeSpec::now().into();
if let Ok(mut metadata) = inode.metadata() {
metadata.atime = now;
metadata.mtime = now;
metadata.ctime = now;
inode.set_metadata(&metadata).ok();
}
}
pub fn to_msec(&self) -> usize {
self.sec * 1_000 + self.nsec / 1_000_000
}
}
impl From<Timespec> for TimeSpec {
fn from(t: Timespec) -> Self {
Self {
sec: t.sec as _,
nsec: t.nsec as _,
}
}
}
impl From<TimeSpec> for Timespec {
fn from(t: TimeSpec) -> Self {
Self {
sec: t.sec as _,
nsec: t.nsec as _,
}
}
}
impl From<TimeSpec> for Duration {
fn from(t: TimeSpec) -> Self {
Self::new(t.sec as _, t.nsec as _)
}
}
impl From<TimeSpec> for TimeVal {
fn from(t: TimeSpec) -> Self {
Self {
sec: t.sec,
usec: t.nsec / 1_000,
}
}
}
#[repr(C)]
pub struct RUsage {
pub utime: TimeVal,
pub stime: TimeVal,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Tms {
pub tms_utime: u64,
pub tms_stime: u64,
pub tms_cutime: u64,
pub tms_cstime: u64,
}
#[derive(Debug)]
#[repr(usize)]
pub enum ClockId {
ClockRealTime = 0,
ClockMonotonic = 1,
ClockProcessCpuTimeId = 2,
ClockThreadCpuTimeId = 3,
ClockMonotonicRaw = 4,
ClockRealTimeCoarse = 5,
ClockMonotonicCoarse = 6,
ClockBootTime = 7,
ClockRealTimeAlarm = 8,
ClockBootTimeAlarm = 9,
}
impl From<usize> for ClockId {
fn from(t: usize) -> ClockId {
match t {
0 => ClockId::ClockRealTime,
1 => ClockId::ClockMonotonic,
2 => ClockId::ClockProcessCpuTimeId,
3 => ClockId::ClockThreadCpuTimeId,
4 => ClockId::ClockMonotonicRaw,
5 => ClockId::ClockRealTimeCoarse,
6 => ClockId::ClockMonotonicCoarse,
7 => ClockId::ClockBootTime,
8 => ClockId::ClockRealTimeAlarm,
9 => ClockId::ClockBootTimeAlarm,
_ => unreachable!(),
}
}
}
#[derive(Debug)]
#[repr(usize)]
pub enum ClockFlags {
ZeroFlag = 0,
TimerAbsTime = 1,
}
impl From<usize> for ClockFlags {
fn from(t: usize) -> ClockFlags {
match t {
0 => ClockFlags::ZeroFlag,
1 => ClockFlags::TimerAbsTime,
_ => unreachable!(),
}
}
}