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

View File

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

View File

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