add byte counting for compressed state caches
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
6ffdc1b2a6
commit
e228dec4f2
1 changed files with 40 additions and 8 deletions
|
@ -1,18 +1,22 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashSet,
|
collections::{HashMap, HashSet},
|
||||||
fmt::Write,
|
fmt::Write,
|
||||||
mem::size_of,
|
mem::size_of,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
|
|
||||||
use conduit::{checked, err, expected, utils, utils::math::usize_from_f64, Result};
|
use conduit::{
|
||||||
|
at, checked, err, expected, utils,
|
||||||
|
utils::{bytes, math::usize_from_f64},
|
||||||
|
Result,
|
||||||
|
};
|
||||||
use database::Map;
|
use database::Map;
|
||||||
use lru_cache::LruCache;
|
use lru_cache::LruCache;
|
||||||
use ruma::{EventId, RoomId};
|
use ruma::{EventId, RoomId};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
rooms,
|
rooms,
|
||||||
rooms::short::{ShortStateHash, ShortStateKey},
|
rooms::short::{ShortId, ShortStateHash, ShortStateKey},
|
||||||
Dep,
|
Dep,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -53,12 +57,13 @@ pub struct HashSetCompressStateEvent {
|
||||||
pub removed: Arc<CompressedState>,
|
pub removed: Arc<CompressedState>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) type CompressedState = HashSet<CompressedStateEvent>;
|
|
||||||
pub(crate) type CompressedStateEvent = [u8; 2 * size_of::<u64>()];
|
|
||||||
type StateInfoLruCache = LruCache<ShortStateHash, ShortStateInfoVec>;
|
type StateInfoLruCache = LruCache<ShortStateHash, ShortStateInfoVec>;
|
||||||
type ShortStateInfoVec = Vec<ShortStateInfo>;
|
type ShortStateInfoVec = Vec<ShortStateInfo>;
|
||||||
type ParentStatesVec = Vec<ShortStateInfo>;
|
type ParentStatesVec = Vec<ShortStateInfo>;
|
||||||
|
|
||||||
|
pub(crate) type CompressedState = HashSet<CompressedStateEvent>;
|
||||||
|
pub(crate) type CompressedStateEvent = [u8; 2 * size_of::<ShortId>()];
|
||||||
|
|
||||||
impl crate::Service for Service {
|
impl crate::Service for Service {
|
||||||
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
||||||
let config = &args.server.config;
|
let config = &args.server.config;
|
||||||
|
@ -75,9 +80,28 @@ impl crate::Service for Service {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn memory_usage(&self, out: &mut dyn Write) -> Result<()> {
|
fn memory_usage(&self, out: &mut dyn Write) -> Result {
|
||||||
let stateinfo_cache = self.stateinfo_cache.lock().expect("locked").len();
|
let (cache_len, ents) = {
|
||||||
writeln!(out, "stateinfo_cache: {stateinfo_cache}")?;
|
let cache = self.stateinfo_cache.lock().expect("locked");
|
||||||
|
let ents = cache
|
||||||
|
.iter()
|
||||||
|
.map(at!(1))
|
||||||
|
.flat_map(|vec| vec.iter())
|
||||||
|
.fold(HashMap::new(), |mut ents, ssi| {
|
||||||
|
ents.insert(Arc::as_ptr(&ssi.added), compressed_state_size(&ssi.added));
|
||||||
|
ents.insert(Arc::as_ptr(&ssi.removed), compressed_state_size(&ssi.removed));
|
||||||
|
ents.insert(Arc::as_ptr(&ssi.full_state), compressed_state_size(&ssi.full_state));
|
||||||
|
ents
|
||||||
|
});
|
||||||
|
|
||||||
|
(cache.len(), ents)
|
||||||
|
};
|
||||||
|
|
||||||
|
let ents_len = ents.len();
|
||||||
|
let bytes = ents.values().copied().fold(0_usize, usize::saturating_add);
|
||||||
|
|
||||||
|
let bytes = bytes::pretty(bytes);
|
||||||
|
writeln!(out, "stateinfo_cache: {cache_len} {ents_len} ({bytes})")?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -435,3 +459,11 @@ impl Service {
|
||||||
.insert(&shortstatehash.to_be_bytes(), &value);
|
.insert(&shortstatehash.to_be_bytes(), &value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn compressed_state_size(compressed_state: &CompressedState) -> usize {
|
||||||
|
compressed_state
|
||||||
|
.len()
|
||||||
|
.checked_mul(size_of::<CompressedStateEvent>())
|
||||||
|
.expect("CompressedState size overflow")
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue