improvement: memory usage for caches

merge-requests/217/head
Timo Kösters 3 years ago
parent 68ee1a5408
commit 077e9ad438
No known key found for this signature in database
GPG Key ID: 356E705610F626D5

4
Cargo.lock generated

@ -2075,9 +2075,9 @@ dependencies = [
[[package]] [[package]]
name = "rocksdb" name = "rocksdb"
version = "0.16.0" version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c749134fda8bfc90d0de643d59bfc841dcb3ac8a1062e12b6754bd60235c48b3" checksum = "7a62eca5cacf2c8261128631bed9f045598d40bfbe4b29f5163f0f802f8f44a7"
dependencies = [ dependencies = [
"libc", "libc",
"librocksdb-sys", "librocksdb-sys",

@ -78,7 +78,7 @@ crossbeam = { version = "0.8.1", optional = true }
num_cpus = "1.13.0" num_cpus = "1.13.0"
threadpool = "1.8.1" threadpool = "1.8.1"
heed = { git = "https://github.com/timokoesters/heed.git", rev = "f6f825da7fb2c758867e05ad973ef800a6fe1d5d", optional = true } heed = { git = "https://github.com/timokoesters/heed.git", rev = "f6f825da7fb2c758867e05ad973ef800a6fe1d5d", optional = true }
rocksdb = { version = "0.16.0", features = ["multi-threaded-cf"], optional = true } rocksdb = { version = "0.17.0", features = ["multi-threaded-cf"], optional = true }
thread_local = "1.1.3" thread_local = "1.1.3"
# used for TURN server authentication # used for TURN server authentication
hmac = "0.11.0" hmac = "0.11.0"

@ -4,6 +4,7 @@ use std::{future::Future, pin::Pin, sync::Arc, collections::HashMap, sync::RwLoc
pub struct Engine { pub struct Engine {
rocks: rocksdb::DBWithThreadMode<rocksdb::MultiThreaded>, rocks: rocksdb::DBWithThreadMode<rocksdb::MultiThreaded>,
cache: rocksdb::Cache,
old_cfs: Vec<String>, old_cfs: Vec<String>,
} }
@ -56,6 +57,7 @@ impl DatabaseEngine for Arc<Engine> {
Ok(Arc::new(Engine { Ok(Arc::new(Engine {
rocks: db, rocks: db,
cache: rocksdb_cache,
old_cfs: cfs, old_cfs: cfs,
})) }))
} }
@ -84,33 +86,35 @@ impl DatabaseEngine for Arc<Engine> {
} }
fn memory_usage(&self) -> Result<String> { fn memory_usage(&self) -> Result<String> {
let stats = rocksdb::perf::get_memory_usage_stats(Some(&[&self.rocks]), None)?; let stats =
Ok(format!("Approximate memory usage of all the mem-tables: {:.3} MB\n\ rocksdb::perf::get_memory_usage_stats(Some(&[&self.rocks]), Some(&[&self.cache]))?;
Ok(format!(
"Approximate memory usage of all the mem-tables: {:.3} MB\n\
Approximate memory usage of un-flushed mem-tables: {:.3} MB\n\ Approximate memory usage of un-flushed mem-tables: {:.3} MB\n\
Approximate memory usage of all the table readers: {:.3} MB\n\ Approximate memory usage of all the table readers: {:.3} MB\n\
Approximate memory usage by cache: {:.3} MB", Approximate memory usage by cache: {:.3} MB",
stats.mem_table_total as f64 / 1024.0 / 1024.0, stats.mem_table_total as f64 / 1024.0 / 1024.0,
stats.mem_table_unflushed as f64 / 1024.0 / 1024.0, stats.mem_table_unflushed as f64 / 1024.0 / 1024.0,
stats.mem_table_readers_total as f64 / 1024.0 / 1024.0, stats.mem_table_readers_total as f64 / 1024.0 / 1024.0,
stats.cache_total as f64 / 1024.0 / 1024.0 stats.cache_total as f64 / 1024.0 / 1024.0
)) ))
} }
} }
impl RocksDbEngineTree<'_> { impl RocksDbEngineTree<'_> {
fn cf(&self) -> rocksdb::BoundColumnFamily<'_> { fn cf(&self) -> Arc<rocksdb::BoundColumnFamily<'_>> {
self.db.rocks.cf_handle(self.name).unwrap() self.db.rocks.cf_handle(self.name).unwrap()
} }
} }
impl Tree for RocksDbEngineTree<'_> { impl Tree for RocksDbEngineTree<'_> {
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> { fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
Ok(self.db.rocks.get_cf(self.cf(), key)?) Ok(self.db.rocks.get_cf(&self.cf(), key)?)
} }
fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> { fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> {
let lock = self.write_lock.read().unwrap(); let lock = self.write_lock.read().unwrap();
self.db.rocks.put_cf(self.cf(), key, value)?; self.db.rocks.put_cf(&self.cf(), key, value)?;
drop(lock); drop(lock);
self.watchers.wake(key); self.watchers.wake(key);
@ -120,21 +124,21 @@ impl Tree for RocksDbEngineTree<'_> {
fn insert_batch<'a>(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()> { fn insert_batch<'a>(&self, iter: &mut dyn Iterator<Item = (Vec<u8>, Vec<u8>)>) -> Result<()> {
for (key, value) in iter { for (key, value) in iter {
self.db.rocks.put_cf(self.cf(), key, value)?; self.db.rocks.put_cf(&self.cf(), key, value)?;
} }
Ok(()) Ok(())
} }
fn remove(&self, key: &[u8]) -> Result<()> { fn remove(&self, key: &[u8]) -> Result<()> {
Ok(self.db.rocks.delete_cf(self.cf(), key)?) Ok(self.db.rocks.delete_cf(&self.cf(), key)?)
} }
fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a> { fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a> {
Box::new( Box::new(
self.db self.db
.rocks .rocks
.iterator_cf(self.cf(), rocksdb::IteratorMode::Start) .iterator_cf(&self.cf(), rocksdb::IteratorMode::Start)
.map(|(k, v)| (Vec::from(k), Vec::from(v))), .map(|(k, v)| (Vec::from(k), Vec::from(v))),
) )
} }
@ -148,7 +152,7 @@ impl Tree for RocksDbEngineTree<'_> {
self.db self.db
.rocks .rocks
.iterator_cf( .iterator_cf(
self.cf(), &self.cf(),
rocksdb::IteratorMode::From( rocksdb::IteratorMode::From(
from, from,
if backwards { if backwards {
@ -165,9 +169,9 @@ impl Tree for RocksDbEngineTree<'_> {
fn increment(&self, key: &[u8]) -> Result<Vec<u8>> { fn increment(&self, key: &[u8]) -> Result<Vec<u8>> {
let lock = self.write_lock.write().unwrap(); let lock = self.write_lock.write().unwrap();
let old = self.db.rocks.get_cf(self.cf(), &key)?; let old = self.db.rocks.get_cf(&self.cf(), &key)?;
let new = utils::increment(old.as_deref()).unwrap(); let new = utils::increment(old.as_deref()).unwrap();
self.db.rocks.put_cf(self.cf(), key, &new)?; self.db.rocks.put_cf(&self.cf(), key, &new)?;
drop(lock); drop(lock);
Ok(new) Ok(new)
@ -177,9 +181,9 @@ impl Tree for RocksDbEngineTree<'_> {
let lock = self.write_lock.write().unwrap(); let lock = self.write_lock.write().unwrap();
for key in iter { for key in iter {
let old = self.db.rocks.get_cf(self.cf(), &key)?; let old = self.db.rocks.get_cf(&self.cf(), &key)?;
let new = utils::increment(old.as_deref()).unwrap(); let new = utils::increment(old.as_deref()).unwrap();
self.db.rocks.put_cf(self.cf(), key, new)?; self.db.rocks.put_cf(&self.cf(), key, new)?;
} }
drop(lock); drop(lock);
@ -195,7 +199,7 @@ impl Tree for RocksDbEngineTree<'_> {
self.db self.db
.rocks .rocks
.iterator_cf( .iterator_cf(
self.cf(), &self.cf(),
rocksdb::IteratorMode::From(&prefix, rocksdb::Direction::Forward), rocksdb::IteratorMode::From(&prefix, rocksdb::Direction::Forward),
) )
.map(|(k, v)| (Vec::from(k), Vec::from(v))) .map(|(k, v)| (Vec::from(k), Vec::from(v)))

@ -118,7 +118,7 @@ impl Admin {
if let Ok(response) = guard._db.memory_usage() { if let Ok(response) = guard._db.memory_usage() {
send_message(RoomMessageEventContent::text_plain(response), guard, &state_lock); send_message(RoomMessageEventContent::text_plain(response), guard, &state_lock);
} else { } else {
send_message(RoomMessageEventContent::text_plain("Failed to get database memory usage".to_string()), guard, &state_lock); send_message(RoomMessageEventContent::text_plain("Failed to get database memory usage.".to_string()), guard, &state_lock);
} }
} }
AdminCommand::SendMessage(message) => { AdminCommand::SendMessage(message) => {

Loading…
Cancel
Save