Replace MEMORY_USED RwLock'd array with a pair of atomic u64s,

for a non-noticeable performance improvement.
This commit is contained in:
Herbert Wolverson 2023-02-14 21:01:10 +00:00
parent 8f7293760c
commit c0f83dbc51
3 changed files with 10 additions and 12 deletions

View File

@ -1,3 +1,5 @@
use std::sync::atomic::AtomicU64;
use lazy_static::*; use lazy_static::*;
use parking_lot::RwLock; use parking_lot::RwLock;
@ -6,7 +8,5 @@ lazy_static! {
pub static ref CPU_USAGE : RwLock<Vec<f32>> = RwLock::new(Vec::with_capacity(128)); pub static ref CPU_USAGE : RwLock<Vec<f32>> = RwLock::new(Vec::with_capacity(128));
} }
lazy_static! { pub static RAM_USED: AtomicU64 = AtomicU64::new(0);
/// Global storage of current RAM usage pub static TOTAL_RAM: AtomicU64 = AtomicU64::new(0);
pub static ref MEMORY_USAGE : RwLock<Vec<u64>> = RwLock::new(vec![0, 0]);
}

View File

@ -59,11 +59,8 @@ pub async fn update_tracking() {
.map(|cpu| cpu.cpu_usage()) .map(|cpu| cpu.cpu_usage())
.collect::<Vec<f32>>(); .collect::<Vec<f32>>();
*CPU_USAGE.write() = cpu_usage; *CPU_USAGE.write() = cpu_usage;
{ RAM_USED.store(sys.used_memory(), std::sync::atomic::Ordering::Relaxed);
let mut mem_use = MEMORY_USAGE.write(); TOTAL_RAM.store(sys.total_memory(), std::sync::atomic::Ordering::Relaxed);
mem_use[0] = sys.used_memory();
mem_use[1] = sys.total_memory();
}
let error = get_data_from_server().await; // Ignoring errors to keep running let error = get_data_from_server().await; // Ignoring errors to keep running
if let Err(error) = error { if let Err(error) = error {
error!("Error in usage update loop: {:?}", error); error!("Error in usage update loop: {:?}", error);

View File

@ -1,7 +1,7 @@
mod cache; mod cache;
mod cache_manager; mod cache_manager;
use self::cache::{ use self::cache::{
CPU_USAGE, CURRENT_THROUGHPUT, HOST_COUNTS, MEMORY_USAGE, RTT_HISTOGRAM, CPU_USAGE, CURRENT_THROUGHPUT, HOST_COUNTS, RAM_USED, TOTAL_RAM, RTT_HISTOGRAM,
THROUGHPUT_BUFFER, TOP_10_DOWNLOADERS, WORST_10_RTT, THROUGHPUT_BUFFER, TOP_10_DOWNLOADERS, WORST_10_RTT,
}; };
use crate::{auth_guard::AuthGuard, tracker::cache::ThroughputPerSecond}; use crate::{auth_guard::AuthGuard, tracker::cache::ThroughputPerSecond};
@ -78,8 +78,9 @@ pub fn cpu_usage(_auth: AuthGuard) -> Json<Vec<f32>> {
#[get("/api/ram")] #[get("/api/ram")]
pub fn ram_usage(_auth: AuthGuard) -> Json<Vec<u64>> { pub fn ram_usage(_auth: AuthGuard) -> Json<Vec<u64>> {
let ram_usage = MEMORY_USAGE.read().clone(); let ram_usage = RAM_USED.load(std::sync::atomic::Ordering::Relaxed);
Json(ram_usage) let total_ram = TOTAL_RAM.load(std::sync::atomic::Ordering::Relaxed);
Json(vec![ram_usage, total_ram])
} }
#[get("/api/top_10_downloaders")] #[get("/api/top_10_downloaders")]