xref: /DragonOS/kernel/src/process/resource.rs (revision edaf015400f83967c2fc940f07be0dbb5792246f)
1 use num_traits::FromPrimitive;
2 
3 use crate::{syscall::SystemError, time::TimeSpec};
4 
5 use super::ProcessControlBlock;
6 
7 #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8 #[repr(C)]
9 pub struct RUsage {
10     /// User time used
11     pub ru_utime: TimeSpec,
12     /// System time used
13     pub ru_stime: TimeSpec,
14 
15     // 以下是linux的rusage结构体扩展
16     /// Maximum resident set size
17     pub ru_maxrss: usize,
18     /// Integral shared memory size
19     pub ru_ixrss: usize,
20     /// Integral unshared data size
21     pub ru_idrss: usize,
22     /// Integral unshared stack size
23     pub ru_isrss: usize,
24     /// Page reclaims (soft page faults)
25     pub ru_minflt: usize,
26     /// Page faults (hard page faults)
27     pub ru_majflt: usize,
28     /// Swaps
29     pub ru_nswap: usize,
30     /// Block input operations
31     pub ru_inblock: usize,
32     /// Block output operations
33     pub ru_oublock: usize,
34     /// IPC messages sent
35     pub ru_msgsnd: usize,
36     /// IPC messages received
37     pub ru_msgrcv: usize,
38     /// Signals received
39     pub ru_nsignals: usize,
40     /// Voluntary context switches
41     pub ru_nvcsw: usize,
42     /// Involuntary context switches
43     pub ru_nivcsw: usize,
44 }
45 
46 ///
47 ///  Definition of struct rusage taken from BSD 4.3 Reno
48 ///
49 ///  We don't support all of these yet, but we might as well have them....
50 ///  Otherwise, each time we add new items, programs which depend on this
51 ///  structure will lose.  This reduces the chances of that happening.
52 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
53 pub enum RUsageWho {
54     RUsageSelf = 0,
55     RUsageChildren = -1,
56     /// sys_wait4() uses this
57     RUsageBoth = -2,
58     /// only the calling thread
59     RusageThread = 1,
60 }
61 
62 impl TryFrom<i32> for RUsageWho {
63     type Error = SystemError;
64 
65     fn try_from(value: i32) -> Result<Self, Self::Error> {
66         match value {
67             0 => Ok(RUsageWho::RUsageSelf),
68             -1 => Ok(RUsageWho::RUsageChildren),
69             -2 => Ok(RUsageWho::RUsageBoth),
70             1 => Ok(RUsageWho::RusageThread),
71             _ => Err(SystemError::EINVAL),
72         }
73     }
74 }
75 
76 /// Resource limit
77 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
78 #[repr(C)]
79 pub struct RLimit64 {
80     /// The current (soft) limit
81     pub rlim_cur: u64,
82     /// The hard limit
83     pub rlim_max: u64,
84 }
85 
86 /// Resource limit IDs
87 ///
88 /// ## Note
89 ///
90 /// 有些架构中,这里[5,9]的值是不同的,我们将来需要在这里增加条件编译
91 #[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive)]
92 pub enum RLimitID {
93     /// CPU time in sec
94     Cpu = 0,
95     /// Maximum file size
96     Fsize = 1,
97     /// Max data size
98     Data = 2,
99     /// Max stack size
100     Stack = 3,
101     /// Max core file size
102     Core = 4,
103     /// Max resident set size
104     Rss = 5,
105 
106     /// Max number of processes
107     Nproc = 6,
108     /// Max number of open files
109     Nofile = 7,
110     /// Max locked-in-memory address space
111     Memlock = 8,
112     /// Address space limit
113     As = 9,
114     /// Max number of file locks held
115     Locks = 10,
116 
117     /// Max number of pending signals
118     Sigpending = 11,
119     /// Max bytes in POSIX mqueues
120     Msgqueue = 12,
121     /// Max nice prio allowed to raise to
122     ///  0-39 for nice level 19 .. -20
123     Nice = 13,
124     /// Max realtime priority
125     Rtprio = 14,
126     /// Timeout for RT tasks in us
127     Rttime = 15,
128     Nlimits = 16,
129 }
130 
131 impl TryFrom<usize> for RLimitID {
132     type Error = SystemError;
133 
134     fn try_from(value: usize) -> Result<Self, Self::Error> {
135         <Self as FromPrimitive>::from_usize(value).ok_or(SystemError::EINVAL)
136     }
137 }
138 
139 impl ProcessControlBlock {
140     /// 获取进程资源使用情况
141     ///
142     /// ## TODO
143     ///
144     /// 当前函数尚未实现,只是返回了一个默认的RUsage结构体
145     pub fn get_rusage(&self, _who: RUsageWho) -> Option<RUsage> {
146         let rusage = RUsage::default();
147 
148         Some(rusage)
149     }
150 }
151