mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-26 10:53:25 +00:00
Add /proc/meminfo support
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
ff525112d0
commit
12ed40578d
48
kernel/aster-nix/src/fs/procfs/meminfo.rs
Normal file
48
kernel/aster-nix/src/fs/procfs/meminfo.rs
Normal file
@ -0,0 +1,48 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
//! This module offers `/proc/meminfo` file support, which tells the user space
|
||||
//! about the memory statistics in the entire system. The definition of the
|
||||
//! fields are similar to that of Linux's but there exist differences.
|
||||
//!
|
||||
//! Reference: <https://man7.org/linux/man-pages/man5/proc_meminfo.5.html>
|
||||
|
||||
use alloc::format;
|
||||
|
||||
use ostd::mm::stat;
|
||||
|
||||
use crate::{
|
||||
fs::{
|
||||
procfs::template::{FileOps, ProcFileBuilder},
|
||||
utils::Inode,
|
||||
},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
/// Represents the inode at `/proc/meminfo`.
|
||||
pub struct MemInfoFileOps;
|
||||
|
||||
impl MemInfoFileOps {
|
||||
pub fn new_inode(parent: Weak<dyn Inode>) -> Arc<dyn Inode> {
|
||||
ProcFileBuilder::new(Self).parent(parent).build().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
/// Total memory in the entire system in bytes.
|
||||
fn mem_total() -> usize {
|
||||
stat::mem_total()
|
||||
}
|
||||
|
||||
/// An estimation of how much memory is available for starting new
|
||||
/// applications, without disk operations.
|
||||
fn mem_available() -> usize {
|
||||
stat::mem_available()
|
||||
}
|
||||
|
||||
impl FileOps for MemInfoFileOps {
|
||||
fn data(&self) -> Result<Vec<u8>> {
|
||||
let total = mem_total();
|
||||
let available = mem_available();
|
||||
let output = format!("MemTotal:\t{}\nMemAvailable:\t{}\n", total, available);
|
||||
Ok(output.into_bytes())
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ use core::sync::atomic::{AtomicU64, Ordering};
|
||||
use sys::SysDirOps;
|
||||
|
||||
use self::{
|
||||
meminfo::MemInfoFileOps,
|
||||
pid::PidDirOps,
|
||||
self_::SelfSymOps,
|
||||
template::{DirOps, ProcDir, ProcDirBuilder, ProcSymBuilder, SymOps},
|
||||
@ -20,6 +21,7 @@ use crate::{
|
||||
};
|
||||
|
||||
mod filesystems;
|
||||
mod meminfo;
|
||||
mod pid;
|
||||
mod self_;
|
||||
mod sys;
|
||||
@ -102,6 +104,8 @@ impl DirOps for RootDirOps {
|
||||
SysDirOps::new_inode(this_ptr.clone())
|
||||
} else if name == "filesystems" {
|
||||
FileSystemsFileOps::new_inode(this_ptr.clone())
|
||||
} else if name == "meminfo" {
|
||||
MemInfoFileOps::new_inode(this_ptr.clone())
|
||||
} else if let Ok(pid) = name.parse::<Pid>() {
|
||||
let process_ref =
|
||||
process_table::get_process(pid).ok_or_else(|| Error::new(Errno::ENOENT))?;
|
||||
@ -123,6 +127,8 @@ impl DirOps for RootDirOps {
|
||||
cached_children.put_entry_if_not_found("filesystems", || {
|
||||
FileSystemsFileOps::new_inode(this_ptr.clone())
|
||||
});
|
||||
cached_children
|
||||
.put_entry_if_not_found("meminfo", || MemInfoFileOps::new_inode(this_ptr.clone()));
|
||||
|
||||
for process in process_table::process_table().iter() {
|
||||
let pid = process.pid().to_string();
|
||||
|
Reference in New Issue
Block a user