diff --git a/Cargo.lock b/Cargo.lock index cc46b48f4a..8eccbfe6d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -822,6 +822,40 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.8" +source = "registry+/~https://github.com/rust-lang/crates.io-index" +checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.3" +source = "registry+/~https://github.com/rust-lang/crates.io-index" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.15" +source = "registry+/~https://github.com/rust-lang/crates.io-index" +checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" +dependencies = [ + "autocfg", + "cfg-if 1.0.0", + "crossbeam-utils", + "memoffset", + "scopeguard", +] + [[package]] name = "crossbeam-utils" version = "0.8.15" @@ -1095,6 +1129,12 @@ dependencies = [ "syn 2.0.15", ] +[[package]] +name = "either" +version = "1.8.1" +source = "registry+/~https://github.com/rust-lang/crates.io-index" +checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" + [[package]] name = "encode_unicode" version = "0.3.6" @@ -1828,9 +1868,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.143" +version = "0.2.147" source = "registry+/~https://github.com/rust-lang/crates.io-index" -checksum = "edc207893e85c5d6be840e969b496b53d94cec8be2d501b214f50daa97fa8024" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "link-cplusplus" @@ -1879,6 +1919,15 @@ version = "2.5.0" source = "registry+/~https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+/~https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + [[package]] name = "mime" version = "0.3.17" @@ -2021,6 +2070,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+/~https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + [[package]] name = "num-bigint" version = "0.4.3" @@ -2204,7 +2262,7 @@ dependencies = [ "serde", "serde_json", "serde_yaml", - "sys-info", + "sysinfo", "tempfile", "test-bitcoincore-rpc", "tokio", @@ -2548,6 +2606,28 @@ dependencies = [ "rand_core 0.5.1", ] +[[package]] +name = "rayon" +version = "1.7.0" +source = "registry+/~https://github.com/rust-lang/crates.io-index" +checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.11.0" +source = "registry+/~https://github.com/rust-lang/crates.io-index" +checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "num_cpus", +] + [[package]] name = "rcgen" version = "0.10.0" @@ -3259,13 +3339,18 @@ dependencies = [ ] [[package]] -name = "sys-info" -version = "0.9.1" +name = "sysinfo" +version = "0.29.3" source = "registry+/~https://github.com/rust-lang/crates.io-index" -checksum = "0b3a0d0aba8bf96a0e1ddfdc352fc53b3df7f39318c71854910c3c4b024ae52c" +checksum = "5bcd0346f90b6bc83526c7b180039a8acd26a5c848cc556d457f6472eb148122" dependencies = [ - "cc", + "cfg-if 1.0.0", + "core-foundation-sys", "libc", + "ntapi", + "once_cell", + "rayon", + "winapi", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 13a26a8260..c3abf0ac91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,7 +50,7 @@ rustls-acme = { version = "0.7.1", features = ["axum"] } serde = { version = "1.0.137", features = ["derive"] } serde_json = { version = "1.0.81" } serde_yaml = "0.9.17" -sys-info = "0.9.1" +sysinfo = "0.29.2" tempfile = "3.2.0" tokio = { version = "1.17.0", features = ["rt-multi-thread"] } tokio-stream = "0.1.9" diff --git a/src/index.rs b/src/index.rs index ca9a52daaf..c310ce1b10 100644 --- a/src/index.rs +++ b/src/index.rs @@ -186,7 +186,20 @@ impl Index { database } Err(_) => { - let database = Database::builder().create(&path)?; + let db_cache_size = match options.db_cache_size { + Some(db_cache_size) => db_cache_size, + None => { + let mut sys = System::new(); + sys.refresh_memory(); + usize::try_from(sys.total_memory() / 4)? + } + }; + + log::info!("Setting DB cache size to {} bytes", db_cache_size); + + let database = Database::builder() + .set_cache_size(db_cache_size) + .create(&path)?; let mut tx = database.begin_write()?; diff --git a/src/lib.rs b/src/lib.rs index c3875abefc..1f51664923 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,6 +68,7 @@ use { thread, time::{Duration, Instant, SystemTime}, }, + sysinfo::{System, SystemExt}, tempfile::TempDir, tokio::{runtime::Runtime, task}, }; diff --git a/src/options.rs b/src/options.rs index 87c35e2030..a8853ae7a9 100644 --- a/src/options.rs +++ b/src/options.rs @@ -28,6 +28,11 @@ pub(crate) struct Options { pub(crate) cookie_file: Option, #[clap(long, help = "Store index in .")] pub(crate) data_dir: Option, + #[clap( + long, + help = "Set index cache to bytes. By default takes 1/4 of available RAM." + )] + pub(crate) db_cache_size: Option, #[clap( long, help = "Don't look for inscriptions below ." @@ -766,4 +771,11 @@ mod tests { Auth::CookieFile("/var/lib/Bitcoin/.cookie".into()) ); } + + #[test] + fn setting_db_cache_size() { + let arguments = + Arguments::try_parse_from(["ord", "--db-cache-size", "16000000000", "index", "run"]).unwrap(); + assert_eq!(arguments.options.db_cache_size, Some(16000000000)); + } } diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index daead622cd..f354c7e31a 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -301,7 +301,9 @@ impl Server { if !self.acme_domain.is_empty() { Ok(self.acme_domain.clone()) } else { - Ok(vec![sys_info::hostname()?]) + Ok(vec![System::new() + .host_name() + .ok_or(anyhow!("no hostname found"))?]) } } @@ -1332,7 +1334,7 @@ mod tests { let (_, server) = parse_server_args("ord server"); assert_eq!( server.acme_domains().unwrap(), - &[sys_info::hostname().unwrap()] + &[System::new().host_name().unwrap()] ); } @@ -1424,14 +1426,16 @@ mod tests { fn http_to_https_redirect_with_path() { TestServer::new_with_args(&[], &["--redirect-http-to-https", "--https"]).assert_redirect( "/sat/0", - &format!("https://{}/sat/0", sys_info::hostname().unwrap()), + &format!("https://{}/sat/0", System::new().host_name().unwrap()), ); } #[test] fn http_to_https_redirect_with_empty() { - TestServer::new_with_args(&[], &["--redirect-http-to-https", "--https"]) - .assert_redirect("/", &format!("https://{}/", sys_info::hostname().unwrap())); + TestServer::new_with_args(&[], &["--redirect-http-to-https", "--https"]).assert_redirect( + "/", + &format!("https://{}/", System::new().host_name().unwrap()), + ); } #[test]