Optimize sys/stat a bit

This commit is contained in:
Wildan M
2026-03-07 20:42:25 +07:00
parent a5adc0cc69
commit 547722767a
2 changed files with 24 additions and 21 deletions
+10 -11
View File
@@ -1,5 +1,8 @@
use alloc::{string::String, vec::Vec};
use core::sync::atomic::{AtomicU64, AtomicU8, AtomicUsize, Ordering};
use core::{
fmt,
sync::atomic::{AtomicU64, AtomicU8, AtomicUsize, Ordering},
};
use crate::cpu_set::LogicalCpuId;
@@ -107,16 +110,12 @@ impl CpuStats {
}
}
impl CpuStatsData {
pub fn to_string(&self, cpu_id: LogicalCpuId) -> String {
format!(
"cpu{} {} {} {} {} {}",
cpu_id.get(),
self.user,
self.nice,
self.kernel,
self.idle,
self.irq,
impl fmt::Display for CpuStatsData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} {} {} {} {}",
self.user, self.nice, self.kernel, self.idle, self.irq,
)
}
}
+14 -10
View File
@@ -1,4 +1,7 @@
use core::fmt::Write as _;
use crate::{
arch::device::cpu,
context::{contexts, ContextRef, Status},
cpu_stats::{get_context_switch_count, get_contexts_count, irq_counts},
percpu::get_all_stats,
@@ -45,7 +48,7 @@ fn get_cpu_stats() -> String {
total_kernel += stat.kernel;
total_idle += stat.idle;
total_irq += stat.irq;
cpu_data += &format!("{}\n", stat.to_string(id));
let _ = write!(&mut cpu_data, "cpu{} {}\n", id.get(), stat);
}
format!(
"cpu {total_user} {total_nice} {total_kernel} {total_idle} {total_irq}\n\
@@ -57,15 +60,16 @@ fn get_cpu_stats() -> String {
fn get_irq_stats() -> String {
let irq = irq_counts();
let mut irq_total = 0;
let per_irq = irq
.iter()
.map(|c| {
irq_total += *c;
format!("{c}")
})
.collect::<Vec<_>>()
.join(" ");
format!("IRQs {irq_total} {per_irq}")
let mut output = String::with_capacity(64);
for &c in irq.iter() {
irq_total += c;
}
let _ = write!(output, "IRQs {}", irq_total);
for &c in irq.iter() {
let _ = write!(output, " {}", c);
}
output
}
/// Format contexts stats.