mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-28 20:03:22 +00:00
Implementation of load average calculation and corresponding /proc entry
Moved everything into the kernel side. Add a SchedulerStats trait Use 'fixed' to represent fixed-point numbers Make the loadavg calculation lazy
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
efd49a96e3
commit
4701eaf0cb
46
kernel/src/fs/procfs/loadavg.rs
Normal file
46
kernel/src/fs/procfs/loadavg.rs
Normal file
@ -0,0 +1,46 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
//! This module offers `/proc/loadavg` file support, which tells the user space
|
||||
//! about the cpu load average for the last 1, 5, and 15 minutes.
|
||||
//!
|
||||
//! Reference: <https://www.man7.org/linux/man-pages/man5/proc_loadavg.5.html>
|
||||
|
||||
use alloc::format;
|
||||
|
||||
use crate::{
|
||||
fs::{
|
||||
procfs::template::{FileOps, ProcFileBuilder},
|
||||
utils::Inode,
|
||||
},
|
||||
prelude::*,
|
||||
process::posix_thread,
|
||||
sched::{self, loadavg::get_loadavg},
|
||||
};
|
||||
|
||||
/// Represents the inode at `/proc/loadavg`.
|
||||
pub struct LoadAvgFileOps;
|
||||
|
||||
impl LoadAvgFileOps {
|
||||
pub fn new_inode(parent: Weak<dyn Inode>) -> Arc<dyn Inode> {
|
||||
ProcFileBuilder::new(Self).parent(parent).build().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl FileOps for LoadAvgFileOps {
|
||||
fn data(&self) -> Result<Vec<u8>> {
|
||||
let avg = get_loadavg();
|
||||
let (nr_queued, nr_running) = sched::nr_queued_and_running();
|
||||
|
||||
let output = format!(
|
||||
"{:.2} {:.2} {:.2} {}/{} {}\n",
|
||||
avg[0],
|
||||
avg[1],
|
||||
avg[2],
|
||||
nr_running,
|
||||
nr_queued,
|
||||
posix_thread::last_tid(),
|
||||
);
|
||||
|
||||
Ok(output.into_bytes())
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
use core::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use loadavg::LoadAvgFileOps;
|
||||
use sys::SysDirOps;
|
||||
|
||||
use self::{
|
||||
@ -21,6 +22,7 @@ use crate::{
|
||||
};
|
||||
|
||||
mod filesystems;
|
||||
mod loadavg;
|
||||
mod meminfo;
|
||||
mod pid;
|
||||
mod self_;
|
||||
@ -106,6 +108,8 @@ impl DirOps for RootDirOps {
|
||||
FileSystemsFileOps::new_inode(this_ptr.clone())
|
||||
} else if name == "meminfo" {
|
||||
MemInfoFileOps::new_inode(this_ptr.clone())
|
||||
} else if name == "loadavg" {
|
||||
LoadAvgFileOps::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))?;
|
||||
@ -129,6 +133,8 @@ impl DirOps for RootDirOps {
|
||||
});
|
||||
cached_children
|
||||
.put_entry_if_not_found("meminfo", || MemInfoFileOps::new_inode(this_ptr.clone()));
|
||||
cached_children
|
||||
.put_entry_if_not_found("loadavg", || LoadAvgFileOps::new_inode(this_ptr.clone()));
|
||||
|
||||
for process in process_table::process_table_mut().iter() {
|
||||
let pid = process.pid().to_string();
|
||||
|
Reference in New Issue
Block a user