diff --git a/kvdb-memorydb/Cargo.toml b/kvdb-memorydb/Cargo.toml
index 21dbe7128..1451a66b8 100644
--- a/kvdb-memorydb/Cargo.toml
+++ b/kvdb-memorydb/Cargo.toml
@@ -8,5 +8,6 @@ license = "GPL-3.0"
edition = "2018"
[dependencies]
+parity-util-mem = { path = "../parity-util-mem", version = "0.3" }
parking_lot = "0.9.0"
kvdb = { version = "0.2", path = "../kvdb" }
diff --git a/kvdb-memorydb/src/lib.rs b/kvdb-memorydb/src/lib.rs
index 9643861f9..ea0c85649 100644
--- a/kvdb-memorydb/src/lib.rs
+++ b/kvdb-memorydb/src/lib.rs
@@ -15,6 +15,7 @@
// along with Parity. If not, see .
use kvdb::{DBOp, DBTransaction, DBValue, KeyValueDB};
+use parity_util_mem::MallocSizeOf;
use parking_lot::RwLock;
use std::{
collections::{BTreeMap, HashMap},
@@ -23,7 +24,7 @@ use std::{
/// A key-value database fulfilling the `KeyValueDB` trait, living in memory.
/// This is generally intended for tests and is not particularly optimized.
-#[derive(Default)]
+#[derive(Default, MallocSizeOf)]
pub struct InMemory {
columns: RwLock, DBValue>>>,
}
diff --git a/kvdb-rocksdb/Cargo.toml b/kvdb-rocksdb/Cargo.toml
index f6387edc5..695c08def 100644
--- a/kvdb-rocksdb/Cargo.toml
+++ b/kvdb-rocksdb/Cargo.toml
@@ -22,6 +22,7 @@ parking_lot = "0.9.0"
regex = "1.3.1"
rocksdb = { version = "0.13", features = ["snappy"], default-features = false }
owning_ref = "0.4.0"
+parity-util-mem = { path = "../parity-util-mem", version = "0.3" }
[dev-dependencies]
alloc_counter = "0.0.4"
diff --git a/kvdb-rocksdb/src/lib.rs b/kvdb-rocksdb/src/lib.rs
index 0041b3dc3..2d666d139 100644
--- a/kvdb-rocksdb/src/lib.rs
+++ b/kvdb-rocksdb/src/lib.rs
@@ -18,6 +18,7 @@ mod iter;
use std::{cmp, collections::HashMap, convert::identity, error, fs, io, mem, path::Path, result};
+use parity_util_mem::MallocSizeOf;
use parking_lot::{Mutex, MutexGuard, RwLock};
use rocksdb::{
BlockBasedOptions, ColumnFamily, ColumnFamilyDescriptor, Error, Options, ReadOptions, WriteBatch, WriteOptions, DB,
@@ -57,6 +58,7 @@ pub const DB_DEFAULT_COLUMN_MEMORY_BUDGET_MB: MiB = 128;
/// The default memory budget in MiB.
pub const DB_DEFAULT_MEMORY_BUDGET_MB: MiB = 512;
+#[derive(MallocSizeOf)]
enum KeyState {
Insert(DBValue),
Delete,
@@ -229,6 +231,25 @@ struct DBAndColumns {
column_names: Vec,
}
+fn static_property_or_warn(db: &DB, prop: &str) -> usize {
+ match db.property_int_value(prop) {
+ Ok(Some(v)) => v as usize,
+ _ => {
+ warn!("Cannot read expected static property of RocksDb database: {}", prop);
+ 0
+ }
+ }
+}
+
+impl MallocSizeOf for DBAndColumns {
+ fn size_of(&self, ops: &mut parity_util_mem::MallocSizeOfOps) -> usize {
+ self.column_names.size_of(ops)
+ + static_property_or_warn(&self.db, "rocksdb.estimate-table-readers-mem")
+ + static_property_or_warn(&self.db, "rocksdb.cur-size-all-mem-tables")
+ + static_property_or_warn(&self.db, "rocksdb.block-cache-usage")
+ }
+}
+
impl DBAndColumns {
fn cf(&self, i: usize) -> &ColumnFamily {
self.db.cf_handle(&self.column_names[i]).expect("the specified column name is correct; qed")
@@ -236,12 +257,17 @@ impl DBAndColumns {
}
/// Key-Value database.
+#[derive(MallocSizeOf)]
pub struct Database {
db: RwLock