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