Skip to content

Commit

Permalink
refactor: rw
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem committed Jan 14, 2025
1 parent 13b477c commit d08852c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 38 deletions.
1 change: 0 additions & 1 deletion catalyst-gateway/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ regex = "1.11.1"
minijinja = "2.5.0"
stats_alloc = "0.1.10"
memory-stats = "1.0.0"
lazy_static = "1.5.0"

[dev-dependencies]
proptest = "1.5.0"
Expand Down
3 changes: 0 additions & 3 deletions catalyst-gateway/bin/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use tracing::{error, info};
use crate::{
cardano::start_followers,
db::{self, index::session::CassandraSession},
metrics::memory::MemoryMetrics,
service::{self, started},
settings::{DocsSettings, ServiceSettings, Settings},
};
Expand Down Expand Up @@ -38,8 +37,6 @@ impl Cli {
pub(crate) async fn exec(self) -> anyhow::Result<()> {
match self {
Self::Run(settings) => {
MemoryMetrics::start_metrics_updater();

Settings::init(settings)?;

let mut tasks = Vec::new();
Expand Down
58 changes: 24 additions & 34 deletions catalyst-gateway/bin/src/metrics/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
alloc::System,
sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex,
Arc, LazyLock, RwLock,
},
thread,
time::Duration,
Expand All @@ -14,21 +14,22 @@ use memory_stats::memory_stats;
use stats_alloc::{Region, Stats, StatsAlloc, INSTRUMENTED_SYSTEM};
use tracing::log::error;

lazy_static::lazy_static! {
/// A global, thread-safe container for memory metrics.
static ref GLOBAL_METRICS: Arc<Mutex<MemoryMetrics>> = Arc::new(Mutex::new(MemoryMetrics::default()));
}
/// Use the instrumented allocator for gathering allocation statistics.
/// Note: This wraps the global allocator.
/// All structs that use the global allocator can be tracked.
#[global_allocator]
static GLOBAL: &StatsAlloc<System> = &INSTRUMENTED_SYSTEM;

/// A global, thread-safe container for memory metrics.
static GLOBAL_METRICS: LazyLock<Arc<RwLock<MemoryMetrics>>> =
LazyLock::new(|| Arc::new(RwLock::new(MemoryMetrics::default())));

/// This is to prevent the init function from accidentally being called multiple times.
static IS_INITIALIZED: AtomicBool = AtomicBool::new(false);

/// Interval for updating memory metrics, in milliseconds.
const UPDATE_INTERVAL_MILLI: u64 = 1000;

/// Use the instrumented allocator for gathering allocation statistics.
#[global_allocator]
static GLOBAL: &StatsAlloc<System> = &INSTRUMENTED_SYSTEM;

/// A structure for storing memory metrics, including allocator statistics
/// and physical/virtual memory usage.
#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -59,45 +60,34 @@ impl MemoryMetrics {
}
}

/// Retrieves a clone of the current memory metrics.
///
/// # Returns
/// * A clone of the `MemoryMetrics` structure containing the latest metrics.
#[allow(dead_code)]
pub(crate) fn retrieve_metrics() -> Result<Self, String> {
match GLOBAL_METRICS.lock() {
Ok(metrics) => Ok(metrics.clone()),
Err(err) => {
let msg = format!("Failed to retrieve memory usage info: {err:?}");

error!("{err:?}");

Err(msg)
},
}
}

/// Starts a background thread to periodically update memory metrics.
///
/// This function spawns a thread that updates the global `MemoryMetrics`
/// structure at regular intervals defined by `UPDATE_INTERVAL_MILLI`.
pub(crate) fn start_metrics_updater() {
if IS_INITIALIZED.load(Ordering::SeqCst) {
pub(crate) fn init_metrics_updater() {
if IS_INITIALIZED.swap(true, Ordering::SeqCst) {
return;
}

IS_INITIALIZED.store(true, Ordering::SeqCst);

let stats = Region::new(GLOBAL);

thread::spawn(move || {
let interval = Duration::from_millis(UPDATE_INTERVAL_MILLI);
loop {
let allocator_stats = stats.change();
match GLOBAL_METRICS.lock() {
Ok(mut metrics) => metrics.update(allocator_stats),
match GLOBAL_METRICS.read() {
Ok(_) => {
match GLOBAL_METRICS.write() {
Ok(mut writable_metrics) => {
writable_metrics.update(allocator_stats);
},
Err(err) => {
error!("Failed to acquire write lock on metrics: {:?}", err);
},
}
},
Err(err) => {
error!("Failed to update memory usage metrics: {:?}", err);
error!("Failed to read memory usage metrics: {:?}", err);
},
}
thread::sleep(interval);
Expand Down
2 changes: 2 additions & 0 deletions catalyst-gateway/bin/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ pub(crate) mod memory;
/// Returns the default prometheus registry.
#[must_use]
pub(crate) fn init_prometheus() -> Registry {
memory::MemoryMetrics::init_metrics_updater();

default_registry().clone()
}

0 comments on commit d08852c

Please sign in to comment.