Move `PerCpuCounter` to `aster-util` to broaden its scope of use,
and add/modify some methods to improve readability.
This commit is contained in:
parent
b76f11f7b3
commit
f869ed508f
|
|
@ -11,6 +11,7 @@ typeflags-util = { path = "../typeflags-util" }
|
|||
aster-rights-proc = { path = "../aster-rights-proc" }
|
||||
aster-rights = { path = "../aster-rights" }
|
||||
inherit-methods-macro = { git = "https://github.com/asterinas/inherit-methods-macro", rev = "98f7e3e" }
|
||||
osdk-heap-allocator = { path = "../../../osdk/deps/heap-allocator" }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ extern crate alloc;
|
|||
pub mod coeff;
|
||||
pub mod dup;
|
||||
pub mod mem_obj_slice;
|
||||
pub mod per_cpu_counter;
|
||||
pub mod printer;
|
||||
pub mod safe_ptr;
|
||||
pub mod slot_vec;
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ impl PerCpuCounter {
|
|||
}
|
||||
|
||||
/// Adds `increment` to the counter on the given CPU.
|
||||
pub fn add(&self, on_cpu: CpuId, increment: isize) {
|
||||
pub fn add_on_cpu(&self, on_cpu: CpuId, increment: isize) {
|
||||
self.per_cpu_counter
|
||||
.get_on_cpu(on_cpu)
|
||||
.fetch_add(increment, Ordering::Relaxed);
|
||||
|
|
@ -37,7 +37,7 @@ impl PerCpuCounter {
|
|||
///
|
||||
/// This function may be inaccurate since other CPUs may be
|
||||
/// updating the counter.
|
||||
pub fn get(&self) -> usize {
|
||||
pub fn sum_all_cpus(&self) -> usize {
|
||||
let mut total: isize = 0;
|
||||
for cpu in all_cpus() {
|
||||
total =
|
||||
|
|
@ -51,4 +51,21 @@ impl PerCpuCounter {
|
|||
total as usize
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the counter value on a specific CPU.
|
||||
pub fn get_on_cpu(&self, cpu: CpuId) -> usize {
|
||||
let val = self.per_cpu_counter.get_on_cpu(cpu).load(Ordering::Relaxed);
|
||||
if val < 0 {
|
||||
// See explanation in `sum_all_cpus`.
|
||||
0
|
||||
} else {
|
||||
val as usize
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for PerCpuCounter {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,6 @@
|
|||
mod iovec;
|
||||
pub mod net;
|
||||
mod padded;
|
||||
pub mod per_cpu_counter;
|
||||
pub mod random;
|
||||
mod read_cstring;
|
||||
pub mod ring_buffer;
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ use core::{array, num::NonZeroUsize, ops::Range};
|
|||
|
||||
use align_ext::AlignExt;
|
||||
use aster_rights::Rights;
|
||||
use aster_util::per_cpu_counter::PerCpuCounter;
|
||||
use ostd::{
|
||||
cpu::CpuId,
|
||||
mm::{
|
||||
|
|
@ -32,7 +33,6 @@ use crate::{
|
|||
prelude::*,
|
||||
process::{Process, ResourceType},
|
||||
thread::exception::PageFaultInfo,
|
||||
util::per_cpu_counter::PerCpuCounter,
|
||||
vm::{
|
||||
perms::VmPerms,
|
||||
vmo::{Vmo, VmoRightsOp},
|
||||
|
|
@ -735,13 +735,13 @@ impl Vmar_ {
|
|||
}
|
||||
|
||||
pub fn get_rss_counter(&self, rss_type: RssType) -> usize {
|
||||
self.rss_counters[rss_type as usize].get()
|
||||
self.rss_counters[rss_type as usize].sum_all_cpus()
|
||||
}
|
||||
|
||||
fn add_rss_counter(&self, rss_type: RssType, val: isize) {
|
||||
// There are races but updating a remote counter won't cause any problems.
|
||||
let cpu_id = CpuId::current_racy();
|
||||
self.rss_counters[rss_type as usize].add(cpu_id, val);
|
||||
self.rss_counters[rss_type as usize].add_on_cpu(cpu_id, val);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue