os/uapi/
resource.rs

1//! 资源限制相关的常量和类型定义。
2
3use core::{ffi::c_long, usize};
4
5#[repr(C)]
6#[derive(Debug, Clone, Copy)]
7/// 进程或其子进程的资源使用统计。
8pub struct Rusage {
9    /// ru_utime: 用户模式下消耗的 CPU 时间。
10    pub ru_utime: timeval,
11
12    /// ru_stime: 内核模式下消耗的 CPU 时间。
13    pub ru_stime: timeval,
14
15    /// ru_maxrss: 最大常驻内存集大小 (Maximum Resident Set Size),单位:千字节 (KB)。
16    pub ru_maxrss: c_long,
17
18    /// ru_ixrss: 积分共享内存大小 (Integral Shared Memory Size)。
19    pub ru_ixrss: c_long,
20
21    /// ru_idrss: 积分非共享数据大小 (Integral Unshared Data Size)。
22    pub ru_idrss: c_long,
23
24    /// ru_isrss: 积分非共享栈大小 (Integral Unshared Stack Size)。
25    pub ru_isrss: c_long,
26
27    /// ru_minflt: 页回收次数 (Page Reclaims),即次要页错误 (Minor Faults)。
28    pub ru_minflt: c_long,
29
30    /// ru_majflt: 页错误次数 (Page Faults),即主要页错误 (Major Faults)。
31    pub ru_majflt: c_long,
32
33    /// ru_nswap: 交换次数 (Swaps)。
34    pub ru_nswap: c_long,
35
36    /// ru_inblock: 块输入操作次数 (Block Input Operations)。
37    pub ru_inblock: c_long,
38
39    /// ru_oublock: 块输出操作次数 (Block Output Operations)。
40    pub ru_oublock: c_long,
41
42    /// ru_msgsnd: 发送的消息次数 (Messages Sent)。
43    pub ru_msgsnd: c_long,
44
45    /// ru_msgrcv: 接收的消息次数 (Messages Received)。
46    pub ru_msgrcv: c_long,
47
48    /// ru_nsignals: 接收到的信号次数 (Signals Received)。
49    pub ru_nsignals: c_long,
50
51    /// ru_nvcsw: 自愿上下文切换次数 (Voluntary Context Switches)。
52    pub ru_nvcsw: c_long,
53
54    /// ru_nivcsw: 非自愿上下文切换次数 (Involuntary Context Switches)。
55    pub ru_nivcsw: c_long,
56}
57
58/// 资源限制值相关的定义。
59pub mod rlimit_value {
60    /// 资源限制值类型,对应 C 语言中的 rlim_t,通常是 usize (64位无符号长整型)。
61    pub type RlimT = usize;
62
63    /// 表示资源无限制(无穷大)的值。
64    pub const RLIM_INFINITY: RlimT = usize::MAX;
65
66    // --- 默认资源限制值常量 ---
67    /// 栈的默认软限制:8MB。
68    pub const STACK_DEFAULT_LIMIT: RlimT = 8 * 1024 * 1024;
69    /// 默认文件描述符软限制(Current)。
70    pub const FILE_OPEN_CUR_DEFAULT: RlimT = 1024;
71    /// 默认文件描述符硬限制(Maximum)。
72    pub const FILE_OPEN_MAX_DEFAULT: RlimT = 4096;
73    /// 内存锁定限制的默认值:64KB。
74    pub const MEMLOCK_DEFAULT_LIMIT: RlimT = 64 * 1024;
75    /// 消息队列的最大字节数默认值:800KB。
76    pub const MQ_BYTES_MAX_DEFAULT: RlimT = 819200;
77}
78
79use rlimit_value::*;
80
81use crate::uapi::time::timeval;
82
83/// 资源限制 ID (Resource limit IDs)
84/// 使用枚举封装 RLIMIT_* 宏,强制类型安全。
85/// #[repr(i32)] 确保底层存储与 C 语言的整数 ID 兼容。
86#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87#[repr(i32)]
88pub enum ResourceId {
89    /// CPU 时间限制(秒)。
90    Cpu = 0,
91    /// 文件最大尺寸限制。
92    Fsize = 1,
93    /// 数据段最大尺寸限制。
94    Data = 2,
95    /// 栈最大尺寸限制。
96    Stack = 3,
97    /// core dump 文件最大尺寸限制。
98    Core = 4,
99
100    /// 最大驻留集大小(Max Resident Set Size)。
101    Rss = 5,
102    /// 用户最大进程数限制。
103    Nproc = 6,
104    /// 最大打开文件描述符数量限制。
105    Nofile = 7,
106    /// 锁定内存地址空间的最大限制(字节)。
107    Memlock = 8,
108    /// 进程虚拟地址空间的最大限制(字节)。
109    As = 9,
110
111    /// 最大文件锁数量限制。
112    Locks = 10,
113    /// 最大待处理信号数量限制。
114    Sigpending = 11,
115    /// POSIX 消息队列的最大字节数限制。
116    Msgqueue = 12,
117    /// 可设置的 nice 优先级上限。
118    Nice = 13,
119    /// 最大实时优先级限制。
120    Rtprio = 14,
121    /// 实时任务的超时时间限制(微秒)。
122    Rttime = 15,
123}
124
125/// 资源限制结构体,对应 C 语言的 struct rlimit。
126#[repr(C)]
127#[derive(Debug, Clone, Copy)]
128pub struct Rlimit {
129    /// 软限制(内核实际强制执行的值)。
130    pub rlim_cur: RlimT,
131    /// 硬限制(软限制的上限)。
132    pub rlim_max: RlimT,
133}
134
135impl Rlimit {
136    /// 构造一个新的 Rlimit 实例。
137    pub const fn new(rlim_cur: RlimT, rlim_max: RlimT) -> Self {
138        Rlimit { rlim_cur, rlim_max }
139    }
140
141    /// 构造一个无限限制的 Rlimit 实例。
142    pub const fn inf() -> Self {
143        Rlimit {
144            rlim_cur: RLIM_INFINITY,
145            rlim_max: RLIM_INFINITY,
146        }
147    }
148
149    /// 构造一个默认的 Rlimit 实例,软硬限制均为 0。
150    pub const fn default() -> Self {
151        Rlimit {
152            rlim_cur: 0,
153            rlim_max: 0,
154        }
155    }
156}
157
158/// 资源限制的总数量。
159pub const RLIM_NLIMITS: usize = 16;
160
161/// 启动时 init 任务的默认资源限制数组。
162pub const INIT_RLIMITS: [Rlimit; RLIM_NLIMITS] = {
163    let mut limits = [Rlimit::default(); RLIM_NLIMITS];
164
165    const fn id_to_index(id: ResourceId) -> usize {
166        id as u32 as usize
167    }
168
169    limits[id_to_index(ResourceId::Cpu)] = Rlimit::inf();
170    limits[id_to_index(ResourceId::Fsize)] = Rlimit::inf();
171    limits[id_to_index(ResourceId::Data)] = Rlimit::inf();
172    limits[id_to_index(ResourceId::Stack)] = Rlimit::new(STACK_DEFAULT_LIMIT, RLIM_INFINITY);
173    limits[id_to_index(ResourceId::Core)] = Rlimit::new(0, RLIM_INFINITY);
174    limits[id_to_index(ResourceId::Rss)] = Rlimit::inf();
175    limits[id_to_index(ResourceId::Nproc)] = Rlimit::new(0, 0);
176    limits[id_to_index(ResourceId::Nofile)] =
177        Rlimit::new(FILE_OPEN_CUR_DEFAULT, FILE_OPEN_MAX_DEFAULT);
178    limits[id_to_index(ResourceId::Memlock)] =
179        Rlimit::new(MEMLOCK_DEFAULT_LIMIT, MEMLOCK_DEFAULT_LIMIT);
180    limits[id_to_index(ResourceId::As)] = Rlimit::inf();
181    limits[id_to_index(ResourceId::Locks)] = Rlimit::inf();
182    limits[id_to_index(ResourceId::Sigpending)] = Rlimit::new(0, 0);
183    limits[id_to_index(ResourceId::Msgqueue)] =
184        Rlimit::new(MQ_BYTES_MAX_DEFAULT, MQ_BYTES_MAX_DEFAULT);
185    limits[id_to_index(ResourceId::Nice)] = Rlimit::new(0, 0);
186    limits[id_to_index(ResourceId::Rtprio)] = Rlimit::new(0, 0);
187    limits[id_to_index(ResourceId::Rttime)] = Rlimit::inf();
188    limits
189};
190
191/// 资源限制结构体数组
192#[repr(C)]
193#[derive(Debug, Clone, Copy)]
194pub struct RlimitStruct {
195    pub limits: [Rlimit; RLIM_NLIMITS],
196}
197
198impl RlimitStruct {
199    /// 构造一个新的 RlimitStruct 实例。
200    pub const fn new(limits: [Rlimit; RLIM_NLIMITS]) -> Self {
201        RlimitStruct { limits }
202    }
203}