Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 90 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
sha2 = { version = "0.10.8" }
shadow-rs = { version = "1.0.0", default-features = false }
sysinfo = { version = "0.34.2" }
tempfile = { version = "3.19.1" }
test-harness = { version = "0.3.0" }
thiserror = { version = "2.0" }
Expand Down
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jiff = { workspace = true }
log = { workspace = true }
pin-project = { workspace = true }
serde = { workspace = true }
sysinfo = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }

Expand Down
4 changes: 2 additions & 2 deletions crates/core/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn foyer_engine(c: &mut Criterion) {
{
let dir = tempdir_in("/tmp").unwrap();
let engine = runtime.block_on(async {
FoyerEngine::try_new(dir.path(), 0, 4 * 1024 * 1024 * 1024)
FoyerEngine::try_new(dir.path(), Some(0), 4 * 1024 * 1024 * 1024)
.await
.unwrap()
});
Expand All @@ -59,7 +59,7 @@ fn foyer_engine(c: &mut Criterion) {
.for_each(|len| {
let dir = tempdir_in("/tmp").unwrap();
let engine = runtime.block_on(async {
FoyerEngine::try_new(dir.path(), 0, 4 * 1024 * 1024 * 1024)
FoyerEngine::try_new(dir.path(), Some(0), 4 * 1024 * 1024 * 1024)
.await
.unwrap()
});
Expand Down
5 changes: 3 additions & 2 deletions crates/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ pub struct StorageConfig {
#[serde(default = "default_data_dir")]
pub data_dir: PathBuf,
pub disk_capacity: u64,
pub memory_capacity: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub memory_capacity: Option<u64>,
}

fn default_listen_addr() -> String {
Expand Down Expand Up @@ -181,7 +182,7 @@ impl Default for Config {
storage: StorageConfig {
data_dir: default_data_dir(),
disk_capacity: 512 * 1024 * 1024,
memory_capacity: 64 * 1024 * 1024,
memory_capacity: None,
},
telemetry: TelemetryConfig {
logs: LogsConfig {
Expand Down
20 changes: 17 additions & 3 deletions crates/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ use foyer::HybridCachePolicy;
use foyer::RecoverMode;
use foyer::RuntimeOptions;
use foyer::TokioRuntimeOptions;
use sysinfo::Pid;
use thiserror::Error;

use crate::num_cpus;

const DEFAULT_MEMORY_CAPACITY_FACTOR: f64 = 0.9;

#[derive(Debug, Error)]
#[error("{0}")]
pub struct EngineError(pub String);
Expand All @@ -41,7 +44,7 @@ pub struct FoyerEngine {
impl FoyerEngine {
pub async fn try_new(
data_dir: &Path,
memory_capacity: u64,
memory_capacity: Option<u64>,
disk_capacity: u64,
) -> Result<Self, EngineError> {
let _ = std::fs::create_dir_all(data_dir);
Expand All @@ -55,7 +58,18 @@ impl FoyerEngine {
let parallelism = num_cpus().get();
let cache = HybridCacheBuilder::new()
.with_policy(HybridCachePolicy::WriteOnInsertion)
.memory(memory_capacity as usize)
.memory(
(memory_capacity.map_or_else(
|| {
let s = sysinfo::System::new_all();
s.process(Pid::from_u32(std::process::id()))
.unwrap()
.memory() as usize
},
|v| v as usize,
) as f64
* DEFAULT_MEMORY_CAPACITY_FACTOR) as usize,
)
.with_shards(parallelism)
.with_eviction_config(FifoConfig::default())
.storage(foyer::Engine::Large)
Expand Down Expand Up @@ -112,7 +126,7 @@ mod tests {
async fn test_get() {
let temp_dir = tempfile::tempdir().unwrap();

let engine = FoyerEngine::try_new(temp_dir.path(), 512 * 1024, 1024 * 1024)
let engine = FoyerEngine::try_new(temp_dir.path(), Some(512 * 1024), 1024 * 1024)
.await
.unwrap();
engine.put(b"foo".to_vec().as_ref(), b"bar".to_vec().as_ref());
Expand Down
1 change: 0 additions & 1 deletion dev/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ listen_addr = "0.0.0.0:7654"
[storage]
data_dir = ".percas/data"
disk_capacity = 1_000_000_000
memory_capacity = 500_000_000

[telemetry.logs.file]
filter = "INFO"
Expand Down
Loading