Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(memory): Implement shared memory state across Relay #3821

Merged
merged 40 commits into from
Jul 22, 2024
Merged
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
90fb599
feat(memory): Implement shared memory state across Relay
iambriccardo Jul 15, 2024
b0ca49d
feat(memory): Add a way to track memory usage
iambriccardo Jul 16, 2024
7c8d357
Fix
iambriccardo Jul 16, 2024
cc4b577
Fix
iambriccardo Jul 16, 2024
c449dfd
Fix
iambriccardo Jul 16, 2024
2dc4e13
Fix
iambriccardo Jul 16, 2024
6bfd213
Fix
iambriccardo Jul 16, 2024
584a868
Fix
iambriccardo Jul 16, 2024
e7608a8
Remove usage of floats
iambriccardo Jul 16, 2024
75a3ffc
Improve
iambriccardo Jul 16, 2024
1069847
Improve
iambriccardo Jul 16, 2024
719bea7
Improve
iambriccardo Jul 16, 2024
45b35ee
Improve
iambriccardo Jul 16, 2024
6a8be96
Improve
iambriccardo Jul 17, 2024
f4a7d39
fi
iambriccardo Jul 17, 2024
8ceac2e
Fix
iambriccardo Jul 17, 2024
cc0596d
Fix
iambriccardo Jul 18, 2024
1d3b3e8
Fix
iambriccardo Jul 18, 2024
fa67244
Fix
iambriccardo Jul 18, 2024
08a6e85
Fix
iambriccardo Jul 18, 2024
bd9e159
Fix
iambriccardo Jul 18, 2024
450311d
Fix
iambriccardo Jul 18, 2024
707cb07
Fix
iambriccardo Jul 18, 2024
8670f0d
Fix
iambriccardo Jul 18, 2024
dfc5a9b
Fix
iambriccardo Jul 18, 2024
464ca43
Fix
iambriccardo Jul 18, 2024
e2ca3d4
Merge
iambriccardo Jul 18, 2024
05b1317
Fix
iambriccardo Jul 18, 2024
6823d7d
Update relay-server/src/endpoints/common.rs
iambriccardo Jul 19, 2024
78abada
Update relay-server/src/utils/memory.rs
iambriccardo Jul 19, 2024
8755d87
Update relay-server/src/utils/memory.rs
iambriccardo Jul 19, 2024
f0f2c9f
Update relay-server/src/utils/memory.rs
iambriccardo Jul 19, 2024
aafba46
Update relay-server/src/utils/memory.rs
iambriccardo Jul 19, 2024
998fdf2
Update relay-server/src/utils/memory.rs
iambriccardo Jul 19, 2024
aa1f39e
Update relay-server/src/utils/memory.rs
iambriccardo Jul 19, 2024
735d52c
Improve
iambriccardo Jul 19, 2024
f434135
Merge
iambriccardo Jul 19, 2024
d22dfa6
Merge
iambriccardo Jul 19, 2024
9204a7f
Merge
iambriccardo Jul 19, 2024
8a70bf2
Changelog
iambriccardo Jul 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix
  • Loading branch information
iambriccardo committed Jul 16, 2024
commit 6bfd2133c214778e9fa57de4eebf5395a49b723c
32 changes: 19 additions & 13 deletions relay-server/src/utils/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use sysinfo::{MemoryRefreshKind, System};
/// Count after which the [`MemoryStat`] data will be refreshed.
const UPDATE_TIME_THRESHOLD_SECONDS: f64 = 0.1;

#[derive(Clone, Copy)]
struct Memory {
pub used: u64,
pub total: u64,
Expand All @@ -24,7 +25,7 @@ impl Memory {
}

struct Inner {
data: RwLock<Memory>,
memory: RwLock<Memory>,
last_update: AtomicU64,
reference_time: Instant,
max_percent_threshold: f32,
Expand All @@ -39,7 +40,7 @@ impl MemoryStat {
// sysinfo docs suggest to use a single instance of `System` across the program.
let mut system = System::new();
Self(Arc::new(Inner {
data: RwLock::new(Self::build_data(&mut system)),
memory: RwLock::new(Self::build_data(&mut system)),
last_update: AtomicU64::new(0),
reference_time: Instant::now(),
max_percent_threshold,
Expand All @@ -48,15 +49,17 @@ impl MemoryStat {
}

pub fn has_enough_memory(&self) -> bool {
self.try_update();
// If we succeeded in updating the memory readings, we just return a copy of the newly read
// limits to avoid acquiring the read lock in the subsequent code.
if let Some(memory) = self.try_update() {
return memory.used_percent() < self.0.max_percent_threshold;
};

// TODO: we could make an optimization that if we already updated the memory, we can avoid
// acquiring a read lock and just evaluate the bottom expression on the newly added data.
let Ok(data) = self.0.data.read() else {
let Ok(memory_lock) = self.0.memory.read() else {
return false;
};

data.used_percent() < self.0.max_percent_threshold
memory_lock.used_percent() < self.0.max_percent_threshold
}

fn build_data(system: &mut System) -> Memory {
Expand All @@ -73,16 +76,16 @@ impl MemoryStat {
}
}

fn try_update(&self) {
fn try_update(&self) -> Option<Memory> {
let last_update = self.0.last_update.load(Ordering::Relaxed);
let elapsed_time = self.0.reference_time.elapsed().as_secs_f64();

if elapsed_time - (last_update as f64) < UPDATE_TIME_THRESHOLD_SECONDS {
return;
return None;
}

let (Ok(mut data), Ok(mut system)) = (self.0.data.write(), self.0.system.lock()) else {
return;
let (Ok(mut data), Ok(mut system)) = (self.0.memory.write(), self.0.system.lock()) else {
return None;
};

let Ok(_) = self.0.last_update.compare_exchange_weak(
Expand All @@ -91,10 +94,13 @@ impl MemoryStat {
Ordering::Relaxed,
Ordering::Relaxed,
) else {
return;
return None;
};

*data = Self::build_data(&mut system);
let new_data = Self::build_data(&mut system);
*data = new_data;

Some(new_data)
}
iambriccardo marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down