mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-28 20:03:22 +00:00
Export /proc/cpuinfo
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
16a319a8ea
commit
561516df98
49
kernel/src/fs/procfs/cpuinfo.rs
Normal file
49
kernel/src/fs/procfs/cpuinfo.rs
Normal file
@ -0,0 +1,49 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
//! This module offers `/proc/cpuinfo` file support, which provides
|
||||
//! information about the CPU architecture, cores, and other details.
|
||||
//!
|
||||
//! Reference: <https://man7.org/linux/man-pages/man5/proc_cpuinfo.5.html>
|
||||
|
||||
use ostd::cpu::num_cpus;
|
||||
|
||||
use crate::{
|
||||
arch::cpu::CpuInfo,
|
||||
fs::{
|
||||
procfs::template::{FileOps, ProcFileBuilder},
|
||||
utils::Inode,
|
||||
},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
/// Represents the inode at `/proc/cpuinfo`.
|
||||
pub struct CpuInfoFileOps;
|
||||
|
||||
impl CpuInfoFileOps {
|
||||
/// Create a new inode for `/proc/cpuinfo`.
|
||||
pub fn new_inode(parent: Weak<dyn Inode>) -> Arc<dyn Inode> {
|
||||
ProcFileBuilder::new(Self).parent(parent).build().unwrap()
|
||||
}
|
||||
|
||||
/// Collect and format CPU information for all cores.
|
||||
fn collect_cpu_info() -> String {
|
||||
let num_cpus = num_cpus() as u32;
|
||||
|
||||
// Iterate over each core and collect CPU information
|
||||
(0..num_cpus)
|
||||
.map(|core_id| {
|
||||
let cpuinfo = CpuInfo::new(core_id);
|
||||
cpuinfo.collect_cpu_info()
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n\n")
|
||||
}
|
||||
}
|
||||
|
||||
impl FileOps for CpuInfoFileOps {
|
||||
/// Retrieve the data for `/proc/cpuinfo`.
|
||||
fn data(&self) -> Result<Vec<u8>> {
|
||||
let output = Self::collect_cpu_info();
|
||||
Ok(output.into_bytes())
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ use loadavg::LoadAvgFileOps;
|
||||
use sys::SysDirOps;
|
||||
|
||||
use self::{
|
||||
cpuinfo::CpuInfoFileOps,
|
||||
meminfo::MemInfoFileOps,
|
||||
pid::PidDirOps,
|
||||
self_::SelfSymOps,
|
||||
@ -21,6 +22,7 @@ use crate::{
|
||||
process::{process_table, process_table::PidEvent, Pid},
|
||||
};
|
||||
|
||||
mod cpuinfo;
|
||||
mod filesystems;
|
||||
mod loadavg;
|
||||
mod meminfo;
|
||||
@ -110,6 +112,8 @@ impl DirOps for RootDirOps {
|
||||
MemInfoFileOps::new_inode(this_ptr.clone())
|
||||
} else if name == "loadavg" {
|
||||
LoadAvgFileOps::new_inode(this_ptr.clone())
|
||||
} else if name == "cpuinfo" {
|
||||
CpuInfoFileOps::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))?;
|
||||
@ -135,7 +139,8 @@ impl DirOps for RootDirOps {
|
||||
.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()));
|
||||
|
||||
cached_children
|
||||
.put_entry_if_not_found("cpuinfo", || CpuInfoFileOps::new_inode(this_ptr.clone()));
|
||||
for process in process_table::process_table_mut().iter() {
|
||||
let pid = process.pid().to_string();
|
||||
cached_children.put_entry_if_not_found(&pid, || {
|
||||
|
Reference in New Issue
Block a user