Mostly use cfg!() rather than #[cfg] for controlling sys_stat

This only adds 48 bytes overhead per cpu core in the PerCpuBlock struct.
Also fixes compilation with sys_stat enabled on x86_64.
This commit is contained in:
bjorn3
2025-09-07 12:27:04 +02:00
parent 0a58e9e806
commit b5822ac118
9 changed files with 50 additions and 45 deletions
+3 -4
View File
@@ -1,8 +1,7 @@
use crate::{arch::device::ROOT_IC_IDX, dtb::irqchip::IRQ_CHIP, scheme::irq::irq_trigger};
use core::sync::atomic::Ordering;
#[cfg(feature = "sys_stat")]
use crate::percpu::PercpuBlock;
// use crate::percpu::PercpuBlock;
unsafe fn irq_ack() -> (u32, Option<usize>) {
let ic = &mut IRQ_CHIP.irq_chip_list.chips[ROOT_IC_IDX.load(Ordering::Relaxed)].ic;
@@ -34,8 +33,8 @@ exception_stack!(irq_at_el1, |_stack| {
//TODO
pub unsafe fn trigger(irq: u32) {
#[cfg(feature = "sys_stat")]
PercpuBlock::current().stats.add_irq(irq);
// FIXME add_irq accepts a u8 as irq number
// PercpuBlock::current().stats.add_irq(irq);
irq_trigger(irq.try_into().unwrap());
IRQ_CHIP.irq_eoi(irq);
+1 -4
View File
@@ -11,6 +11,7 @@ use crate::{
},
interrupt, interrupt_stack,
ipi::{ipi, IpiKind, IpiTarget},
percpu::PercpuBlock,
scheme::{
debug::{debug_input, debug_notify},
irq::irq_trigger,
@@ -19,9 +20,6 @@ use crate::{
time,
};
#[cfg(feature = "sys_stat")]
use crate::percpu::PercpuBlock;
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum IrqMethod {
@@ -99,7 +97,6 @@ pub unsafe fn acknowledge(irq: usize) {
/// Sends an end-of-interrupt, so that the interrupt controller can go on to the next one.
pub unsafe fn eoi(irq: u8) {
#[cfg(feature = "sys_stat")]
PercpuBlock::current().stats.add_irq(irq);
match irq_method() {
+1 -3
View File
@@ -2,8 +2,6 @@ use core::sync::atomic::{AtomicUsize, Ordering};
use alloc::vec::Vec;
#[cfg(feature = "sys_stat")]
use crate::percpu::PercpuBlock;
use crate::{
context::{self, timeout},
device::{
@@ -12,6 +10,7 @@ use crate::{
},
interrupt, interrupt_stack,
ipi::{ipi, IpiKind, IpiTarget},
percpu::PercpuBlock,
scheme::{
debug::{debug_input, debug_notify},
irq::irq_trigger,
@@ -97,7 +96,6 @@ pub unsafe fn acknowledge(irq: usize) {
/// Sends an end-of-interrupt, so that the interrupt controller can go on to the next one.
pub unsafe fn eoi(irq: u8) {
#[cfg(feature = "sys_stat")]
PercpuBlock::current().stats.add_irq(irq);
match irq_method() {
+1 -3
View File
@@ -8,13 +8,12 @@ use core::{
use spin::RwLock;
use syscall::{SigProcControl, Sigcontrol, UPPER_FDTBL_TAG};
#[cfg(feature = "sys_stat")]
use crate::cpu_stats;
use crate::{
arch::{interrupt::InterruptStack, paging::PAGE_SIZE},
common::aligned_box::AlignedBox,
context::{self, arch, file::FileDescriptor},
cpu_set::{LogicalCpuId, LogicalCpuSet},
cpu_stats,
ipi::{ipi, IpiKind, IpiTarget},
memory::{allocate_p2frame, deallocate_p2frame, Enomem, Frame, RaiiFrame},
paging::{RmmA, RmmArch},
@@ -190,7 +189,6 @@ impl Context {
#[cfg(feature = "syscall_debug")]
syscall_debug_info: crate::syscall::debug::SyscallDebugInfo::default(),
};
#[cfg(feature = "sys_stat")]
cpu_stats::add_context();
Ok(this)
}
+10 -22
View File
@@ -15,14 +15,11 @@ use syscall::PtraceFlags;
use crate::{
context::{arch, contexts, Context},
cpu_set::LogicalCpuId,
interrupt,
cpu_stats, interrupt,
percpu::PercpuBlock,
ptrace, time,
};
#[cfg(feature = "sys_stat")]
use crate::cpu_stats;
use super::ContextRef;
enum UpdateResult {
@@ -141,13 +138,10 @@ pub enum SwitchResult {
/// to an idle context.
pub fn switch() -> SwitchResult {
let percpu = PercpuBlock::current();
#[cfg(feature = "sys_stat")]
{
cpu_stats::add_context_switch();
percpu
.stats
.add_time(percpu.switch_internals.pit_ticks.get());
}
cpu_stats::add_context_switch();
percpu
.stats
.add_time(percpu.switch_internals.pit_ticks.get());
//set PIT Interrupt counter to 0, giving each process same amount of PIT ticks
percpu.switch_internals.pit_ticks.set(0);
@@ -292,13 +286,10 @@ pub fn switch() -> SwitchResult {
// need to use the `switch_finish_hook` to be able to release the locks. Newly created
// contexts will return directly to the function pointer passed to context::spawn, and not
// reach this code until the next context switch back.
#[cfg(feature = "sys_stat")]
{
if next_context.userspace {
percpu.stats.set_state(cpu_stats::CpuState::User);
} else {
percpu.stats.set_state(cpu_stats::CpuState::Kernel);
}
if next_context.userspace {
percpu.stats.set_state(cpu_stats::CpuState::User);
} else {
percpu.stats.set_state(cpu_stats::CpuState::Kernel);
}
SwitchResult::Switched
@@ -306,10 +297,7 @@ pub fn switch() -> SwitchResult {
// No target was found, unset global lock and return
arch::CONTEXT_SWITCH_LOCK.store(false, Ordering::SeqCst);
#[cfg(feature = "sys_stat")]
{
percpu.stats.set_state(cpu_stats::CpuState::Idle);
}
percpu.stats.set_state(cpu_stats::CpuState::Idle);
SwitchResult::AllContextsIdle
}
+31 -1
View File
@@ -1,6 +1,8 @@
use core::sync::atomic::{AtomicU64, AtomicU8, AtomicUsize, Ordering};
use alloc::{string::String, vec::Vec};
use alloc::string::String;
#[cfg(feature = "sys_stat")]
use alloc::vec::Vec;
use crate::cpu_set::LogicalCpuId;
@@ -59,7 +61,12 @@ impl CpuStats {
///
/// # Parameters
/// * `new_state` - The state of the CPU for the following ticks.
#[inline]
pub fn set_state(&self, new_state: CpuState) {
if cfg!(not(feature = "sys_stat")) {
return;
}
self.state.store(new_state as u8, Ordering::Relaxed);
}
@@ -69,7 +76,12 @@ impl CpuStats {
///
/// # Parameters
/// * `ticks` - NUmber of ticks to add.
#[inline]
pub fn add_time(&self, ticks: usize) {
if cfg!(not(feature = "sys_stat")) {
return;
}
match self.state.load(Ordering::Relaxed) {
val if val == CpuState::Idle as u8 => self.idle.fetch_add(ticks, Ordering::Relaxed),
val if val == CpuState::User as u8 => self.user.fetch_add(ticks, Ordering::Relaxed),
@@ -85,7 +97,12 @@ impl CpuStats {
///
/// # Parameters
/// * `irq` - The ID of the interrupt that happened.
#[inline]
pub fn add_irq(&self, irq: u8) {
if cfg!(not(feature = "sys_stat")) {
return;
}
IRQ_COUNT[irq as usize].fetch_add(1, Ordering::Relaxed);
self.irq.fetch_add(1, Ordering::Relaxed);
}
@@ -118,26 +135,39 @@ impl Into<CpuStatsData> for &CpuStats {
}
/// Add a context switch to the count.
#[inline]
pub fn add_context_switch() {
if cfg!(not(feature = "sys_stat")) {
return;
}
CONTEXT_SWITCH_COUNT.fetch_add(1, Ordering::Relaxed);
}
/// Get the number of context switches.
#[cfg(feature = "sys_stat")]
pub fn get_context_switch_count() -> u64 {
CONTEXT_SWITCH_COUNT.load(Ordering::Relaxed)
}
/// Add a context creation to the count.
#[inline]
pub fn add_context() {
if cfg!(not(feature = "sys_stat")) {
return;
}
CONTEXTS_COUNT.fetch_add(1, Ordering::Relaxed);
}
/// Get the number of contexts created.
#[cfg(feature = "sys_stat")]
pub fn get_contexts_count() -> u64 {
CONTEXTS_COUNT.load(Ordering::Relaxed)
}
/// Get the count of each interrupt.
#[cfg(feature = "sys_stat")]
pub fn irq_counts() -> Vec<u64> {
IRQ_COUNT
.iter()
-1
View File
@@ -88,7 +88,6 @@ mod dtb;
mod cpu_set;
/// Stats for the CPUs
#[cfg(feature = "sys_stat")]
mod cpu_stats;
/// Context management
+2 -6
View File
@@ -10,14 +10,12 @@ use syscall::PtraceFlags;
use crate::{
context::{empty_cr3, memory::AddrSpaceWrapper, switch::ContextSwitchPercpu},
cpu_set::{LogicalCpuId, MAX_CPU_COUNT},
cpu_stats::CpuStats,
ptrace::Session,
};
#[cfg(feature = "sys_stat")]
use {
crate::cpu_stats::{CpuStats, CpuStatsData},
alloc::vec::Vec,
};
use {crate::cpu_stats::CpuStatsData, alloc::vec::Vec};
#[cfg(feature = "syscall_debug")]
use crate::syscall::debug::SyscallDebugInfo;
@@ -48,7 +46,6 @@ pub struct PercpuBlock {
pub misc_arch_info: crate::device::ArchPercpuMisc,
#[cfg(feature = "sys_stat")]
pub stats: CpuStats,
}
@@ -187,7 +184,6 @@ impl PercpuBlock {
misc_arch_info: Default::default(),
#[cfg(feature = "sys_stat")]
stats: CpuStats::default(),
}
}
+1 -1
View File
@@ -81,7 +81,7 @@ fn get_contexts_stats() -> (u64, u64) {
for status in statuses {
if matches!(status, Status::Runnable) {
running += 1;
} else if !matches!(status, Status::Dead) {
} else if !matches!(status, Status::Dead { .. }) {
blocked += 1;
}
}