update: ptth_server metrics are at a stopping point for now
parent
eec17b10a1
commit
e79925dc14
|
@ -1,4 +1,7 @@
|
|||
use std::sync::Arc;
|
||||
use std::{
|
||||
sync::Arc,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use arc_swap::ArcSwap;
|
||||
use chrono::{DateTime, Utc};
|
||||
|
@ -35,6 +38,8 @@ pub struct Startup {
|
|||
|
||||
#[derive (Debug, serde::Serialize)]
|
||||
pub struct Interval {
|
||||
#[serde (skip)]
|
||||
pub at: Instant,
|
||||
pub utc: DateTime <Utc>,
|
||||
pub rss_mib: u64,
|
||||
|
||||
|
@ -94,6 +99,7 @@ impl Interval {
|
|||
let rss_mib = mem.rss ().get::<mebibyte> ();
|
||||
|
||||
let x = Self {
|
||||
at: Instant::now (),
|
||||
utc: Utc::now (),
|
||||
rss_mib,
|
||||
cpu_usage,
|
||||
|
@ -104,15 +110,53 @@ impl Interval {
|
|||
|
||||
pub async fn monitor (interval_writer: Arc <ArcSwap <Option <Interval>>>)
|
||||
{
|
||||
use std::time::Duration;
|
||||
|
||||
use uom::si::ratio::percent;
|
||||
|
||||
let mut interval = tokio::time::interval (Duration::from_secs (60));
|
||||
|
||||
#[derive (Default)]
|
||||
struct Window {
|
||||
window_length: u64,
|
||||
next_interval: u64,
|
||||
last_metrics: Arc <Option <Interval>>,
|
||||
}
|
||||
|
||||
impl Window {
|
||||
fn new (window_length: u64) -> Self {
|
||||
Window {
|
||||
window_length,
|
||||
next_interval: 0,
|
||||
last_metrics: Default::default (),
|
||||
}
|
||||
}
|
||||
|
||||
fn update (
|
||||
&mut self,
|
||||
counter: u64,
|
||||
new_metrics: &Arc <Option <Interval>>
|
||||
) {
|
||||
if counter >= self.next_interval {
|
||||
if let (Some (old), Some (new)) = (&*self.last_metrics, &**new_metrics) {
|
||||
let diff = new.cpu_usage.clone () - old.cpu_usage.clone ();
|
||||
trace! (
|
||||
"CPU usage over {} s: {}%",
|
||||
(new.at - old.at).as_secs (),
|
||||
diff.get::<percent> (),
|
||||
);
|
||||
}
|
||||
|
||||
self.next_interval += self.window_length;
|
||||
self.last_metrics = new_metrics.clone ();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut counter = 0_u64;
|
||||
let mut next_10_time = counter;
|
||||
let mut metrics_at_last_10: Arc <Option <Interval>> = Arc::new (None);
|
||||
|
||||
let mut windows = [1, 5, 10, 60, 1440]
|
||||
.iter ()
|
||||
.map (|len| Window::new (*len))
|
||||
.collect::<Vec <_>> ();
|
||||
|
||||
loop {
|
||||
interval.tick ().await;
|
||||
|
@ -126,14 +170,8 @@ impl Interval {
|
|||
};
|
||||
let new_interval_metrics = Arc::new (Some (new_interval_metrics));
|
||||
|
||||
if counter >= next_10_time {
|
||||
if let (Some (old), Some (new)) = (&*metrics_at_last_10, &*new_interval_metrics) {
|
||||
let diff = new.cpu_usage.clone () - old.cpu_usage.clone ();
|
||||
trace! ("CPU usage: {}%", diff.get::<percent> ());
|
||||
}
|
||||
|
||||
next_10_time += 1;
|
||||
metrics_at_last_10 = new_interval_metrics.clone ();
|
||||
for window in windows.iter_mut () {
|
||||
window.update (counter, &new_interval_metrics);
|
||||
}
|
||||
|
||||
interval_writer.store (new_interval_metrics);
|
||||
|
|
Loading…
Reference in New Issue