|
@@ -10,6 +10,7 @@ use alloc::{
|
|
};
|
|
};
|
|
|
|
|
|
use crate::{
|
|
use crate::{
|
|
|
|
+ arch::mm::LockedFrameAllocator,
|
|
filesystem::vfs::{
|
|
filesystem::vfs::{
|
|
core::{generate_inode_id, ROOT_INODE},
|
|
core::{generate_inode_id, ROOT_INODE},
|
|
FileType,
|
|
FileType,
|
|
@@ -37,6 +38,8 @@ use super::vfs::{
|
|
pub enum ProcFileType {
|
|
pub enum ProcFileType {
|
|
///展示进程状态信息
|
|
///展示进程状态信息
|
|
ProcStatus = 0,
|
|
ProcStatus = 0,
|
|
|
|
+ /// meminfo
|
|
|
|
+ ProcMeminfo = 1,
|
|
//todo: 其他文件类型
|
|
//todo: 其他文件类型
|
|
///默认文件类型
|
|
///默认文件类型
|
|
Default,
|
|
Default,
|
|
@@ -46,6 +49,7 @@ impl From<u8> for ProcFileType {
|
|
fn from(value: u8) -> Self {
|
|
fn from(value: u8) -> Self {
|
|
match value {
|
|
match value {
|
|
0 => ProcFileType::ProcStatus,
|
|
0 => ProcFileType::ProcStatus,
|
|
|
|
+ 1 => ProcFileType::ProcMeminfo,
|
|
_ => ProcFileType::Default,
|
|
_ => ProcFileType::Default,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -204,8 +208,34 @@ impl ProcFSInode {
|
|
return Ok((pdata.len() * size_of::<u8>()) as i64);
|
|
return Ok((pdata.len() * size_of::<u8>()) as i64);
|
|
}
|
|
}
|
|
|
|
|
|
- /// status文件读取函数
|
|
|
|
- fn read_status(
|
|
|
|
|
|
+ /// 打开 meminfo 文件
|
|
|
|
+ fn open_meminfo(&self, pdata: &mut ProcfsFilePrivateData) -> Result<i64, SystemError> {
|
|
|
|
+ // 获取内存信息
|
|
|
|
+ let usage = LockedFrameAllocator.get_usage();
|
|
|
|
+
|
|
|
|
+ // 传入数据
|
|
|
|
+ let data: &mut Vec<u8> = &mut pdata.data;
|
|
|
|
+
|
|
|
|
+ data.append(
|
|
|
|
+ &mut format!("MemTotal:\t{} kB\n", usage.total().bytes() >> 10)
|
|
|
|
+ .as_bytes()
|
|
|
|
+ .to_owned(),
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ data.append(
|
|
|
|
+ &mut format!("MemFree:\t{} kB\n", usage.free().bytes() >> 10)
|
|
|
|
+ .as_bytes()
|
|
|
|
+ .to_owned(),
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ // 去除多余的\0
|
|
|
|
+ self.trim_string(data);
|
|
|
|
+
|
|
|
|
+ return Ok((data.len() * size_of::<u8>()) as i64);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// proc文件系统读取函数
|
|
|
|
+ fn proc_read(
|
|
&self,
|
|
&self,
|
|
offset: usize,
|
|
offset: usize,
|
|
len: usize,
|
|
len: usize,
|
|
@@ -263,7 +293,7 @@ impl ProcFS {
|
|
mtime: TimeSpec::default(),
|
|
mtime: TimeSpec::default(),
|
|
ctime: TimeSpec::default(),
|
|
ctime: TimeSpec::default(),
|
|
file_type: FileType::Dir,
|
|
file_type: FileType::Dir,
|
|
- mode: ModeType::from_bits_truncate(0o777),
|
|
|
|
|
|
+ mode: ModeType::from_bits_truncate(0o555),
|
|
nlinks: 1,
|
|
nlinks: 1,
|
|
uid: 0,
|
|
uid: 0,
|
|
gid: 0,
|
|
gid: 0,
|
|
@@ -286,6 +316,24 @@ impl ProcFS {
|
|
// 释放锁
|
|
// 释放锁
|
|
drop(root_guard);
|
|
drop(root_guard);
|
|
|
|
|
|
|
|
+ // 创建meminfo文件
|
|
|
|
+ let inode = result.root_inode();
|
|
|
|
+ let binding = inode.create(
|
|
|
|
+ "meminfo",
|
|
|
|
+ FileType::File,
|
|
|
|
+ ModeType::from_bits_truncate(0o444),
|
|
|
|
+ );
|
|
|
|
+ if let Ok(meminfo) = binding {
|
|
|
|
+ let meminfo_file = meminfo
|
|
|
|
+ .as_any_ref()
|
|
|
|
+ .downcast_ref::<LockedProcFSInode>()
|
|
|
|
+ .unwrap();
|
|
|
|
+ meminfo_file.0.lock().fdata.pid = Pid::new(0);
|
|
|
|
+ meminfo_file.0.lock().fdata.ftype = ProcFileType::ProcMeminfo;
|
|
|
|
+ } else {
|
|
|
|
+ panic!("create meminfo error");
|
|
|
|
+ }
|
|
|
|
+
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -293,26 +341,26 @@ impl ProcFS {
|
|
/// @usage 在进程中调用并创建进程对应文件
|
|
/// @usage 在进程中调用并创建进程对应文件
|
|
pub fn register_pid(&self, pid: Pid) -> Result<(), SystemError> {
|
|
pub fn register_pid(&self, pid: Pid) -> Result<(), SystemError> {
|
|
// 获取当前inode
|
|
// 获取当前inode
|
|
- let proc: Arc<dyn IndexNode> = self.root_inode();
|
|
|
|
|
|
+ let inode: Arc<dyn IndexNode> = self.root_inode();
|
|
// 创建对应进程文件夹
|
|
// 创建对应进程文件夹
|
|
- let _pf: Arc<dyn IndexNode> = proc.create(
|
|
|
|
|
|
+ let pid_dir: Arc<dyn IndexNode> = inode.create(
|
|
&pid.to_string(),
|
|
&pid.to_string(),
|
|
FileType::Dir,
|
|
FileType::Dir,
|
|
- ModeType::from_bits_truncate(0o777),
|
|
|
|
|
|
+ ModeType::from_bits_truncate(0o555),
|
|
)?;
|
|
)?;
|
|
// 创建相关文件
|
|
// 创建相关文件
|
|
// status文件
|
|
// status文件
|
|
- let binding: Arc<dyn IndexNode> = _pf.create(
|
|
|
|
|
|
+ let binding: Arc<dyn IndexNode> = pid_dir.create(
|
|
"status",
|
|
"status",
|
|
FileType::File,
|
|
FileType::File,
|
|
- ModeType::from_bits_truncate(0o777),
|
|
|
|
|
|
+ ModeType::from_bits_truncate(0o444),
|
|
)?;
|
|
)?;
|
|
- let _sf: &LockedProcFSInode = binding
|
|
|
|
|
|
+ let status_file: &LockedProcFSInode = binding
|
|
.as_any_ref()
|
|
.as_any_ref()
|
|
.downcast_ref::<LockedProcFSInode>()
|
|
.downcast_ref::<LockedProcFSInode>()
|
|
.unwrap();
|
|
.unwrap();
|
|
- _sf.0.lock().fdata.pid = pid;
|
|
|
|
- _sf.0.lock().fdata.ftype = ProcFileType::ProcStatus;
|
|
|
|
|
|
+ status_file.0.lock().fdata.pid = pid;
|
|
|
|
+ status_file.0.lock().fdata.ftype = ProcFileType::ProcStatus;
|
|
|
|
|
|
//todo: 创建其他文件
|
|
//todo: 创建其他文件
|
|
|
|
|
|
@@ -352,6 +400,7 @@ impl IndexNode for LockedProcFSInode {
|
|
// 根据文件类型获取相应数据
|
|
// 根据文件类型获取相应数据
|
|
let file_size = match inode.fdata.ftype {
|
|
let file_size = match inode.fdata.ftype {
|
|
ProcFileType::ProcStatus => inode.open_status(&mut private_data)?,
|
|
ProcFileType::ProcStatus => inode.open_status(&mut private_data)?,
|
|
|
|
+ ProcFileType::ProcMeminfo => inode.open_meminfo(&mut private_data)?,
|
|
_ => {
|
|
_ => {
|
|
todo!()
|
|
todo!()
|
|
}
|
|
}
|
|
@@ -409,7 +458,8 @@ impl IndexNode for LockedProcFSInode {
|
|
|
|
|
|
// 根据文件类型读取相应数据
|
|
// 根据文件类型读取相应数据
|
|
match inode.fdata.ftype {
|
|
match inode.fdata.ftype {
|
|
- ProcFileType::ProcStatus => return inode.read_status(offset, len, buf, private_data),
|
|
|
|
|
|
+ ProcFileType::ProcStatus => return inode.proc_read(offset, len, buf, private_data),
|
|
|
|
+ ProcFileType::ProcMeminfo => return inode.proc_read(offset, len, buf, private_data),
|
|
ProcFileType::Default => (),
|
|
ProcFileType::Default => (),
|
|
};
|
|
};
|
|
|
|
|
|
@@ -720,10 +770,10 @@ pub fn procfs_init() -> Result<(), SystemError> {
|
|
let mut result = None;
|
|
let mut result = None;
|
|
INIT.call_once(|| {
|
|
INIT.call_once(|| {
|
|
kinfo!("Initializing ProcFS...");
|
|
kinfo!("Initializing ProcFS...");
|
|
- // 创建 sysfs 实例
|
|
|
|
|
|
+ // 创建 procfs 实例
|
|
let procfs: Arc<ProcFS> = ProcFS::new();
|
|
let procfs: Arc<ProcFS> = ProcFS::new();
|
|
|
|
|
|
- // sysfs 挂载
|
|
|
|
|
|
+ // procfs 挂载
|
|
let _t = ROOT_INODE()
|
|
let _t = ROOT_INODE()
|
|
.find("proc")
|
|
.find("proc")
|
|
.expect("Cannot find /proc")
|
|
.expect("Cannot find /proc")
|