Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatically scale conduwuit caches by CPU-core-count #567

Merged
merged 3 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion conduwuit-example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ allow_profile_lookup_federation_requests = true
### Generic database options

# Set this to any float value to multiply conduwuit's in-memory LRU caches with.
# By default, the caches scale automatically with cpu-core-count.
# May be useful if you have significant memory to spare to increase performance.
#
# This was previously called `conduit_cache_capacity_modifier`
Expand All @@ -490,7 +491,7 @@ allow_profile_lookup_federation_requests = true

# Set this to any float value in megabytes for conduwuit to tell the database engine that this much memory is available for database-related caches.
# May be useful if you have significant memory to spare to increase performance.
# Defaults to 256.0
# Defaults to 128.0 + (64.0 * CPU core count).
#db_cache_capacity_mb = 256.0


Expand Down
34 changes: 22 additions & 12 deletions src/core/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use url::Url;

pub use self::check::check;
use self::proxy::ProxyConfig;
use crate::{error::Error, Err, Result};
use crate::{error::Error, utils::sys, Err, Result};

pub mod check;
pub mod proxy;
Expand Down Expand Up @@ -887,29 +887,29 @@ fn default_database_backups_to_keep() -> i16 { 1 }

fn default_database_backend() -> String { "rocksdb".to_owned() }

fn default_db_cache_capacity_mb() -> f64 { 256.0 }
fn default_db_cache_capacity_mb() -> f64 { 128.0 + parallelism_scaled_f64(64.0) }

fn default_pdu_cache_capacity() -> u32 { 150_000 }
fn default_pdu_cache_capacity() -> u32 { parallelism_scaled_u32(10_000).saturating_add(100_000) }

fn default_cache_capacity_modifier() -> f64 { 1.0 }

fn default_auth_chain_cache_capacity() -> u32 { 100_000 }
fn default_auth_chain_cache_capacity() -> u32 { parallelism_scaled_u32(10_000).saturating_add(100_000) }

fn default_shorteventid_cache_capacity() -> u32 { 500_000 }
fn default_shorteventid_cache_capacity() -> u32 { parallelism_scaled_u32(50_000).saturating_add(100_000) }

fn default_eventidshort_cache_capacity() -> u32 { 100_000 }
fn default_eventidshort_cache_capacity() -> u32 { parallelism_scaled_u32(25_000).saturating_add(100_000) }

fn default_shortstatekey_cache_capacity() -> u32 { 100_000 }
fn default_shortstatekey_cache_capacity() -> u32 { parallelism_scaled_u32(10_000).saturating_add(100_000) }

fn default_statekeyshort_cache_capacity() -> u32 { 100_000 }
fn default_statekeyshort_cache_capacity() -> u32 { parallelism_scaled_u32(10_000).saturating_add(100_000) }

fn default_server_visibility_cache_capacity() -> u32 { 100 }
fn default_server_visibility_cache_capacity() -> u32 { parallelism_scaled_u32(500) }

fn default_user_visibility_cache_capacity() -> u32 { 100 }
fn default_user_visibility_cache_capacity() -> u32 { parallelism_scaled_u32(1000) }

fn default_stateinfo_cache_capacity() -> u32 { 100 }
fn default_stateinfo_cache_capacity() -> u32 { parallelism_scaled_u32(1000) }

fn default_roomid_spacehierarchy_cache_capacity() -> u32 { 100 }
fn default_roomid_spacehierarchy_cache_capacity() -> u32 { parallelism_scaled_u32(1000) }

fn default_dns_cache_entries() -> u32 { 32768 }

Expand Down Expand Up @@ -1087,3 +1087,13 @@ fn default_admin_log_capture() -> String {
}

fn default_admin_room_tag() -> String { "m.server_notice".to_owned() }

#[allow(clippy::as_conversions, clippy::cast_precision_loss)]
fn parallelism_scaled_f64(val: f64) -> f64 { val * (sys::available_parallelism() as f64) }

fn parallelism_scaled_u32(val: u32) -> u32 {
let val = val.try_into().expect("failed to cast u32 to usize");
parallelism_scaled(val).try_into().unwrap_or(u32::MAX)
}

fn parallelism_scaled(val: usize) -> usize { val.saturating_mul(sys::available_parallelism()) }
Loading