xref: /DragonOS/kernel/src/process/resource.rs (revision be8cdf4b8edcd9579572672411f4489039dea313)
1 use crate::{syscall::SystemError, time::TimeSpec};
2 
3 use super::ProcessControlBlock;
4 
5 #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
6 #[repr(C)]
7 pub struct RUsage {
8     /// User time used
9     pub ru_utime: TimeSpec,
10     /// System time used
11     pub ru_stime: TimeSpec,
12 
13     // 以下是linux的rusage结构体扩展
14     /// Maximum resident set size
15     pub ru_maxrss: usize,
16     /// Integral shared memory size
17     pub ru_ixrss: usize,
18     /// Integral unshared data size
19     pub ru_idrss: usize,
20     /// Integral unshared stack size
21     pub ru_isrss: usize,
22     /// Page reclaims (soft page faults)
23     pub ru_minflt: usize,
24     /// Page faults (hard page faults)
25     pub ru_majflt: usize,
26     /// Swaps
27     pub ru_nswap: usize,
28     /// Block input operations
29     pub ru_inblock: usize,
30     /// Block output operations
31     pub ru_oublock: usize,
32     /// IPC messages sent
33     pub ru_msgsnd: usize,
34     /// IPC messages received
35     pub ru_msgrcv: usize,
36     /// Signals received
37     pub ru_nsignals: usize,
38     /// Voluntary context switches
39     pub ru_nvcsw: usize,
40     /// Involuntary context switches
41     pub ru_nivcsw: usize,
42 }
43 
44 ///
45 ///  Definition of struct rusage taken from BSD 4.3 Reno
46 ///
47 ///  We don't support all of these yet, but we might as well have them....
48 ///  Otherwise, each time we add new items, programs which depend on this
49 ///  structure will lose.  This reduces the chances of that happening.
50 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
51 pub enum RUsageWho {
52     RUsageSelf = 0,
53     RUsageChildren = -1,
54     /// sys_wait4() uses this
55     RUsageBoth = -2,
56     /// only the calling thread
57     RusageThread = 1,
58 }
59 
60 impl TryFrom<i32> for RUsageWho {
61     type Error = SystemError;
62 
63     fn try_from(value: i32) -> Result<Self, Self::Error> {
64         match value {
65             0 => Ok(RUsageWho::RUsageSelf),
66             -1 => Ok(RUsageWho::RUsageChildren),
67             -2 => Ok(RUsageWho::RUsageBoth),
68             1 => Ok(RUsageWho::RusageThread),
69             _ => Err(SystemError::EINVAL),
70         }
71     }
72 }
73 
74 impl ProcessControlBlock {
75     /// 获取进程资源使用情况
76     ///
77     /// ## TODO
78     ///
79     /// 当前函数尚未实现,只是返回了一个默认的RUsage结构体
80     pub fn get_rusage(&self, _who: RUsageWho) -> Option<RUsage> {
81         let rusage = RUsage::default();
82 
83         Some(rusage)
84     }
85 }
86