Skip to content

Commit 212506f

Browse files
committed
Make RocksDB config dependant of current hardware
1 parent 344a842 commit 212506f

File tree

5 files changed

+193
-21
lines changed

5 files changed

+193
-21
lines changed

Cargo.lock

Lines changed: 80 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ lru = "0.12.3"
123123
num-bigint = "0.4.3"
124124
num-format = "0.4.4"
125125
num-traits = "0.2.18"
126-
num_cpus = "1.16.0"
127126
octocrab = "0.42.1"
128127
oneshot = "0.1.6"
129128
pathdiff = "0.2.1"
@@ -172,6 +171,7 @@ similar-asserts = "1.5.0"
172171
static_assertions = "1.1.0"
173172
stdext = "0.3.3"
174173
syn = "2.0.52"
174+
sysinfo = "0.33.1"
175175
tempfile = "3.10.1"
176176
test-case = "3.3.1"
177177
test-log = { version = "0.2.15", default-features = false, features = [

examples/Cargo.lock

Lines changed: 81 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

linera-views/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ linera-base.workspace = true
4545
linera-views-derive.workspace = true
4646
linera-witty.workspace = true
4747
linked-hash-map.workspace = true
48-
num_cpus.workspace = true
4948
prometheus.workspace = true
5049
rand = { workspace = true, features = ["small_rng"] }
5150
rocksdb = { workspace = true, optional = true }
5251
scylla = { workspace = true, optional = true }
5352
serde.workspace = true
5453
sha3.workspace = true
5554
static_assertions.workspace = true
55+
sysinfo.workspace = true
5656
tempfile.workspace = true
5757
thiserror.workspace = true
5858
tokio = { workspace = true, features = ["rt", "sync"] }

linera-views/src/backends/rocks_db.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ use std::{
1414
};
1515

1616
use linera_base::ensure;
17+
use rocksdb::{BlockBasedOptions, Cache, DBCompactionStyle};
1718
use serde::{Deserialize, Serialize};
19+
use sysinfo::{CpuRefreshKind, MemoryRefreshKind, RefreshKind, System};
1820
use tempfile::TempDir;
1921
use thiserror::Error;
2022

@@ -49,8 +51,9 @@ const MAX_VALUE_SIZE: usize = 3221225072;
4951
// 8388608 and so for offset reason we decrease by 400
5052
const MAX_KEY_SIZE: usize = 8388208;
5153

52-
const DB_CACHE_SIZE: usize = 128 * 1024 * 1024; // 128 MiB
53-
const DB_MAX_WRITE_BUFFER_NUMBER: i32 = 8;
54+
const WRITE_BUFFER_SIZE: usize = 64 * 1024 * 1024; // 64 MB
55+
const MAX_WRITE_BUFFER_NUMBER: i32 = 32;
56+
const HYPER_CLOCK_CACHE_BLOCK_SIZE: usize = 8 * 1024; // 8 KB
5457

5558
/// The RocksDB client that we use.
5659
type DB = rocksdb::DBWithThreadMode<rocksdb::MultiThreaded>;
@@ -300,21 +303,38 @@ impl RocksDbStoreInternal {
300303
if !std::path::Path::exists(&path_buf) {
301304
std::fs::create_dir(path_buf.clone())?;
302305
}
306+
let sys = System::new_with_specifics(
307+
RefreshKind::nothing()
308+
.with_cpu(CpuRefreshKind::everything())
309+
.with_memory(MemoryRefreshKind::nothing().with_ram()),
310+
);
311+
let num_cpus = sys.cpus().len() as i32;
312+
let total_ram = sys.total_memory() as usize;
303313
let mut options = rocksdb::Options::default();
304314
options.create_if_missing(true);
305315
options.create_missing_column_families(true);
306316
// Flush in-memory buffer to disk more often
307-
options.set_write_buffer_size(DB_CACHE_SIZE);
308-
options.set_max_write_buffer_number(DB_MAX_WRITE_BUFFER_NUMBER);
317+
options.set_write_buffer_size(WRITE_BUFFER_SIZE);
318+
options.set_max_write_buffer_number(MAX_WRITE_BUFFER_NUMBER);
309319
options.set_compression_type(rocksdb::DBCompressionType::Lz4);
310-
options.set_level_zero_slowdown_writes_trigger(-1);
311-
options.set_level_zero_stop_writes_trigger(48);
312-
options.set_stats_dump_period_sec(60);
313-
options.enable_statistics();
314-
options.increase_parallelism(num_cpus::get() as i32);
315-
options.set_max_background_jobs(8);
320+
options.set_level_zero_slowdown_writes_trigger(12);
321+
options.set_level_zero_stop_writes_trigger(20);
322+
options.increase_parallelism((num_cpus / 2).max(1));
323+
options.set_max_background_jobs((num_cpus / 3).max(1));
316324
options.set_level_compaction_dynamic_level_bytes(true);
317325

326+
options.set_compaction_style(DBCompactionStyle::Level);
327+
options.set_target_file_size_base(WRITE_BUFFER_SIZE as u64);
328+
329+
let mut block_options = BlockBasedOptions::default();
330+
block_options.set_pin_l0_filter_and_index_blocks_in_cache(true);
331+
block_options.set_cache_index_and_filter_blocks(true);
332+
block_options.set_block_cache(&Cache::new_hyper_clock_cache(
333+
total_ram / 4,
334+
HYPER_CLOCK_CACHE_BLOCK_SIZE,
335+
));
336+
options.set_block_based_table_factory(&block_options);
337+
318338
let db = DB::open(&options, path_buf)?;
319339
let executor = RocksDbStoreExecutor {
320340
db: Arc::new(db),

0 commit comments

Comments
 (0)