Skip to content

Commit

Permalink
accounts-db: Sampled LRU eviction
Browse files Browse the repository at this point in the history
Instead of rigid LRU eviction, perform sampled LRU eviction.

Sampled LRU eviction takes K random keys and picks the one with the
lowest update timestamp. This way, even though the evicted element is
not always the oldest in the whole cache, removes the necessity of
maintaining a queue and reduces the contention caused by locking the
queue.

The K parameter is configurable, but the best performing value so far
was 8 and it's used as the default one.

The new eviction mechanism results in performance improvement in the
cache eviction benchmarks:

```
read_only_accounts_cache_eviction_lo_hi/load/8
                        time:   [1.4096 µs 1.4108 µs 1.4117 µs]
                        change: [-67.900% -64.278% -60.095%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 19 outliers among 100 measurements (19.00%)
  6 (6.00%) low severe
  13 (13.00%) low mild
read_only_accounts_cache_eviction_lo_hi/store/8
                        time:   [2.4480 µs 2.4638 µs 2.4759 µs]
                        change: [-77.943% -77.588% -77.268%] (p = 0.00 < 0.05)
                        Performance has improved.
Benchmarking read_only_accounts_cache_eviction_lo_hi/load/16
                        time:   [1.5526 µs 1.5536 µs 1.5544 µs]
                        change: [-79.195% -76.569% -73.647%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 21 outliers among 100 measurements (21.00%)
  18 (18.00%) low severe
  3 (3.00%) low mild
Benchmarking read_only_accounts_cache_eviction_lo_hi/store/16
                        time:   [2.8992 µs 2.9145 µs 2.9265 µs]
                        change: [-87.526% -87.320% -87.116%] (p = 0.00 < 0.05)
                        Performance has improved.
Benchmarking read_only_accounts_cache_eviction_lo_hi/load/32
                        time:   [1.7435 µs 1.7538 µs 1.7654 µs]
                        change: [-86.960% -85.829% -84.566%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 19 outliers among 100 measurements (19.00%)
  14 (14.00%) low mild
  3 (3.00%) high mild
  2 (2.00%) high severe
Benchmarking read_only_accounts_cache_eviction_lo_hi/store/32
                        time:   [3.4493 µs 3.5638 µs 3.7102 µs]
                        change: [-90.941% -90.560% -90.136%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  10 (10.00%) high severe
Benchmarking read_only_accounts_cache_eviction_lo_hi/load/64
                        time:   [3.7502 µs 4.0713 µs 4.4061 µs]
                        change: [-88.290% -85.987% -82.879%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) high mild
  1 (1.00%) high severe
Benchmarking read_only_accounts_cache_eviction_lo_hi/store/64
                        time:   [7.4959 µs 7.9429 µs 8.4066 µs]
                        change: [-90.591% -90.141% -89.641%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

Benchmarking read_only_accounts_cache_eviction_hi/load/8
                        time:   [1.6254 µs 1.6261 µs 1.6267 µs]
                        change: [-58.221% -54.458% -50.268%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 20 outliers among 100 measurements (20.00%)
  13 (13.00%) low severe
  7 (7.00%) low mild
Benchmarking read_only_accounts_cache_eviction_hi/store/8
                        time:   [2.5913 µs 2.6101 µs 2.6257 µs]
                        change: [-79.288% -79.077% -78.838%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) low mild
Benchmarking read_only_accounts_cache_eviction_hi/load/16
                        time:   [1.5098 µs 1.5108 µs 1.5116 µs]
                        change: [-78.379% -76.092% -73.612%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 21 outliers among 100 measurements (21.00%)
  17 (17.00%) low severe
  4 (4.00%) low mild
Benchmarking read_only_accounts_cache_eviction_hi/store/16
                        time:   [2.8342 µs 2.8470 µs 2.8568 µs]
                        change: [-86.859% -86.720% -86.575%] (p = 0.00 < 0.05)
                        Performance has improved.
Benchmarking read_only_accounts_cache_eviction_hi/load/32
                        time:   [2.1136 µs 2.2410 µs 2.3841 µs]
                        change: [-83.144% -81.384% -79.375%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 36 outliers among 100 measurements (36.00%)
  14 (14.00%) low severe
  1 (1.00%) low mild
  3 (3.00%) high mild
  18 (18.00%) high severe
Benchmarking read_only_accounts_cache_eviction_hi/store/32
                        time:   [3.2238 µs 3.2357 µs 3.2463 µs]
                        change: [-92.514% -92.427% -92.338%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  3 (3.00%) low mild
  1 (1.00%) high severe
Benchmarking read_only_accounts_cache_eviction_hi/load/64
                        time:   [5.5431 µs 5.7115 µs 5.9002 µs]
                        change: [-82.871% -81.517% -80.064%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild
Benchmarking read_only_accounts_cache_eviction_hi/store/64
                        time:   [7.2374 µs 7.7026 µs 8.1520 µs]
                        change: [-90.548% -90.069% -89.579%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 26 outliers among 100 measurements (26.00%)
  9 (9.00%) low severe
  3 (3.00%) low mild
  9 (9.00%) high mild
  5 (5.00%) high severe
```
  • Loading branch information
vadorovsky committed Jan 13, 2025
1 parent 18dd59b commit 92de937
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 214 deletions.
4 changes: 2 additions & 2 deletions accounts-db/benches/read_only_accounts_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn bench_read_only_accounts_cache(c: &mut Criterion) {
let cache = Arc::new(ReadOnlyAccountsCache::new(
AccountsDb::DEFAULT_MAX_READ_ONLY_CACHE_DATA_SIZE_LO,
AccountsDb::DEFAULT_MAX_READ_ONLY_CACHE_DATA_SIZE_HI,
AccountsDb::READ_ONLY_CACHE_MS_TO_SKIP_LRU_UPDATE,
AccountsDb::DEFAULT_READ_ONLY_CACHE_EVICT_SAMPLE_SIZE,
));

for (pubkey, account) in accounts.iter() {
Expand Down Expand Up @@ -180,7 +180,7 @@ fn bench_read_only_accounts_cache_eviction(
let cache = Arc::new(ReadOnlyAccountsCache::new(
max_data_size_lo,
max_data_size_hi,
AccountsDb::READ_ONLY_CACHE_MS_TO_SKIP_LRU_UPDATE,
AccountsDb::DEFAULT_READ_ONLY_CACHE_EVICT_SAMPLE_SIZE,
));

// Fill up the cache.
Expand Down
15 changes: 10 additions & 5 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ pub const ACCOUNTS_DB_CONFIG_FOR_TESTING: AccountsDbConfig = AccountsDbConfig {
shrink_paths: None,
shrink_ratio: DEFAULT_ACCOUNTS_SHRINK_THRESHOLD_OPTION,
read_cache_limit_bytes: None,
read_cache_evict_sample_size: None,
write_cache_limit_bytes: None,
ancient_append_vec_offset: None,
ancient_storage_ideal_size: None,
Expand Down Expand Up @@ -529,6 +530,7 @@ pub const ACCOUNTS_DB_CONFIG_FOR_BENCHMARKS: AccountsDbConfig = AccountsDbConfig
shrink_paths: None,
shrink_ratio: DEFAULT_ACCOUNTS_SHRINK_THRESHOLD_OPTION,
read_cache_limit_bytes: None,
read_cache_evict_sample_size: None,
write_cache_limit_bytes: None,
ancient_append_vec_offset: None,
ancient_storage_ideal_size: None,
Expand Down Expand Up @@ -654,6 +656,7 @@ pub struct AccountsDbConfig {
/// The low and high watermark sizes for the read cache, in bytes.
/// If None, defaults will be used.
pub read_cache_limit_bytes: Option<(usize, usize)>,
pub read_cache_evict_sample_size: Option<usize>,
pub write_cache_limit_bytes: Option<u64>,
/// if None, ancient append vecs are set to ANCIENT_APPEND_VEC_DEFAULT_OFFSET
/// Some(offset) means include slots up to (max_slot - (slots_per_epoch - 'offset'))
Expand Down Expand Up @@ -1891,17 +1894,16 @@ pub struct PubkeyHashAccount {
impl AccountsDb {
pub const DEFAULT_ACCOUNTS_HASH_CACHE_DIR: &'static str = "accounts_hash_cache";

// read only cache does not update lru on read of an entry unless it has been at least this many ms since the last lru update
#[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))]
const READ_ONLY_CACHE_MS_TO_SKIP_LRU_UPDATE: u32 = 100;

// The default high and low watermark sizes for the accounts read cache.
// If the cache size exceeds MAX_SIZE_HI, it'll evict entries until the size is <= MAX_SIZE_LO.
#[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))]
const DEFAULT_MAX_READ_ONLY_CACHE_DATA_SIZE_LO: usize = 400 * 1024 * 1024;
#[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))]
const DEFAULT_MAX_READ_ONLY_CACHE_DATA_SIZE_HI: usize = 410 * 1024 * 1024;

#[cfg_attr(feature = "dev-context-only-utils", qualifiers(pub))]
const DEFAULT_READ_ONLY_CACHE_EVICT_SAMPLE_SIZE: usize = 10;

pub fn default_for_tests() -> Self {
Self::new_single_for_tests()
}
Expand Down Expand Up @@ -1979,6 +1981,9 @@ impl AccountsDb {
Self::DEFAULT_MAX_READ_ONLY_CACHE_DATA_SIZE_LO,
Self::DEFAULT_MAX_READ_ONLY_CACHE_DATA_SIZE_HI,
));
let read_cache_evict_sample_size = accounts_db_config
.read_cache_evict_sample_size
.unwrap_or(Self::DEFAULT_READ_ONLY_CACHE_EVICT_SAMPLE_SIZE);

// Increase the stack for foreground threads
// rayon needs a lot of stack
Expand Down Expand Up @@ -2034,7 +2039,7 @@ impl AccountsDb {
read_only_accounts_cache: ReadOnlyAccountsCache::new(
read_cache_size.0,
read_cache_size.1,
Self::READ_ONLY_CACHE_MS_TO_SKIP_LRU_UPDATE,
read_cache_evict_sample_size,
),
write_cache_limit_bytes: accounts_db_config.write_cache_limit_bytes,
partitioned_epoch_rewards_config: accounts_db_config.partitioned_epoch_rewards_config,
Expand Down
Loading

0 comments on commit 92de937

Please sign in to comment.