Skip to content

Commit

Permalink
Insert the cache entry logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuDutSik committed Sep 24, 2024
1 parent ace1633 commit 5e8bfa5
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions linera-views/src/backends/lru_caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ use std::{

use linked_hash_map::LinkedHashMap;
#[cfg(with_metrics)]
use {linera_base::prometheus_util, prometheus::IntCounterVec};
use {
linera_base::prometheus_util,
prometheus::{HistogramVec, IntCounterVec},
};

#[cfg(with_testing)]
use crate::memory::MemoryStore;
Expand Down Expand Up @@ -85,6 +88,36 @@ static NUM_CACHE_FIND_SUCCESS: LazyLock<IntCounterVec> = LazyLock::new(|| {
.expect("Counter creation should not fail")
});

#[cfg(with_metrics)]
/// Size of the inserted value entry
static VALUE_CACHE_ENTRY_SIZE: LazyLock<HistogramVec> = LazyLock::new(|| {
prometheus_util::register_histogram_vec(
"value_cache_entry_size",
"Value cache entry size",
&[],
Some(vec![
10.0, 30.0, 100.0, 300.0, 1000.0, 3000.0, 10000.0, 30000.0, 100000.0, 300000.0,
1000000.0,
]),
)
.expect("Histogram can be created")
});

#[cfg(with_metrics)]
/// Size of the inserted find entry
static FIND_CACHE_ENTRY_SIZE: LazyLock<HistogramVec> = LazyLock::new(|| {
prometheus_util::register_histogram_vec(
"find_cache_entry_size",
"Find cache entry size",
&[],
Some(vec![
10.0, 30.0, 100.0, 300.0, 1000.0, 3000.0, 10000.0, 30000.0, 100000.0, 300000.0,
1000000.0,
]),
)
.expect("Histogram can be created")
});

#[derive(Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
enum CacheEntry {
Find,
Expand Down Expand Up @@ -445,6 +478,10 @@ impl LruPrefixCache {
/// Inserts a value entry into the cache.
pub fn insert_value(&mut self, key: Vec<u8>, cache_entry: ValueCacheEntry) {
let cache_size = cache_entry.size() + key.len();
#[cfg(with_metrics)]
VALUE_CACHE_ENTRY_SIZE
.with_label_values(&[])
.observe(cache_size as f64);
if cache_size > self.lru_cache_policy.max_cache_size {
// Inserting that entry would lead to complete clearing of the cache
// which is counterproductive
Expand Down Expand Up @@ -487,6 +524,10 @@ impl LruPrefixCache {
/// Inserts a find entry into the cache.
pub fn insert_find(&mut self, key_prefix: Vec<u8>, cache_entry: FindCacheEntry) {
let cache_size = cache_entry.size() + key_prefix.len();
#[cfg(with_metrics)]
FIND_CACHE_ENTRY_SIZE
.with_label_values(&[])
.observe(cache_size as f64);
if cache_size > self.lru_cache_policy.max_cache_size {
// Inserting that entry would lead to complete clearing of the cache
// which is counter productive
Expand All @@ -506,7 +547,7 @@ impl LruPrefixCache {
let keys = self
.map_value
.range(get_interval(key_prefix.clone()))
.map(|(x,_)| x.clone())
.map(|(x, _)| x.clone())
.collect::<Vec<_>>();
for key in keys {
self.map_value.remove(&key);
Expand Down

0 comments on commit 5e8bfa5

Please sign in to comment.