From cf0224fc64af47cdb7bf9f3cb0307352bc0dc2bb Mon Sep 17 00:00:00 2001 From: iTrooz Date: Wed, 2 Jul 2025 17:12:33 +0200 Subject: [PATCH 1/5] rename LazyDiskCache to LazyLruDiskCache I think this name makes more sense, given that this type holds a lazily initialized LRU cache --- src/cache/disk.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/cache/disk.rs b/src/cache/disk.rs index 36d3be8ef..435415d20 100644 --- a/src/cache/disk.rs +++ b/src/cache/disk.rs @@ -27,40 +27,40 @@ use crate::errors::*; use super::{normalize_key, PreprocessorCacheModeConfig}; -enum LazyDiskCache { +enum LazyLruDiskCache { Uninit { root: OsString, max_size: u64 }, Init(LruDiskCache), } -impl LazyDiskCache { +impl LazyLruDiskCache { fn get_or_init(&mut self) -> Result<&mut LruDiskCache> { match self { - LazyDiskCache::Uninit { root, max_size } => { - *self = LazyDiskCache::Init(LruDiskCache::new(&root, *max_size)?); + LazyLruDiskCache::Uninit { root, max_size } => { + *self = LazyLruDiskCache::Init(LruDiskCache::new(&root, *max_size)?); self.get_or_init() } - LazyDiskCache::Init(d) => Ok(d), + LazyLruDiskCache::Init(d) => Ok(d), } } fn get(&mut self) -> Option<&mut LruDiskCache> { match self { - LazyDiskCache::Uninit { .. } => None, - LazyDiskCache::Init(d) => Some(d), + LazyLruDiskCache::Uninit { .. } => None, + LazyLruDiskCache::Init(d) => Some(d), } } fn capacity(&self) -> u64 { match self { - LazyDiskCache::Uninit { max_size, .. } => *max_size, - LazyDiskCache::Init(d) => d.capacity(), + LazyLruDiskCache::Uninit { max_size, .. } => *max_size, + LazyLruDiskCache::Init(d) => d.capacity(), } } fn path(&self) -> &Path { match self { - LazyDiskCache::Uninit { root, .. } => root.as_ref(), - LazyDiskCache::Init(d) => d.path(), + LazyLruDiskCache::Uninit { root, .. } => root.as_ref(), + LazyLruDiskCache::Init(d) => d.path(), } } } @@ -68,11 +68,11 @@ impl LazyDiskCache { /// A cache that stores entries at local disk paths. pub struct DiskCache { /// `LruDiskCache` does all the real work here. - lru: Arc>, + lru: Arc>, /// Thread pool to execute disk I/O pool: tokio::runtime::Handle, preprocessor_cache_mode_config: PreprocessorCacheModeConfig, - preprocessor_cache: Arc>, + preprocessor_cache: Arc>, rw_mode: CacheMode, } @@ -86,13 +86,13 @@ impl DiskCache { rw_mode: CacheMode, ) -> DiskCache { DiskCache { - lru: Arc::new(Mutex::new(LazyDiskCache::Uninit { + lru: Arc::new(Mutex::new(LazyLruDiskCache::Uninit { root: root.as_ref().to_os_string(), max_size, })), pool: pool.clone(), preprocessor_cache_mode_config, - preprocessor_cache: Arc::new(Mutex::new(LazyDiskCache::Uninit { + preprocessor_cache: Arc::new(Mutex::new(LazyLruDiskCache::Uninit { root: Path::new(root.as_ref()) .join("preprocessor") .into_os_string(), From 7eb97bf0bcebde152dc2fb7d13042fc8b56ae950 Mon Sep 17 00:00:00 2001 From: iTrooz Date: Wed, 2 Jul 2025 17:40:43 +0200 Subject: [PATCH 2/5] make `sccache -s` always return current cache size sccache server disk cache is usually initiated by the first compilation request. If `sccache -s` is issued before then, sccache will not return the currently occupied cache size This PR fixes that --- src/cache/disk.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cache/disk.rs b/src/cache/disk.rs index 435415d20..58b5cb3ba 100644 --- a/src/cache/disk.rs +++ b/src/cache/disk.rs @@ -173,7 +173,7 @@ impl Storage for DiskCache { } async fn current_size(&self) -> Result> { - Ok(self.lru.lock().unwrap().get().map(|l| l.size())) + Ok(Some(self.lru.lock().unwrap().get_or_init()?.size())) } async fn max_size(&self) -> Result> { Ok(Some(self.lru.lock().unwrap().capacity())) From a0de54c16bf23882dc43048ea7c839daed1c097d Mon Sep 17 00:00:00 2001 From: iTrooz Date: Thu, 3 Jul 2025 11:14:36 +0200 Subject: [PATCH 3/5] add test --- tests/stats_output.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/stats_output.rs diff --git a/tests/stats_output.rs b/tests/stats_output.rs new file mode 100644 index 000000000..7ff143041 --- /dev/null +++ b/tests/stats_output.rs @@ -0,0 +1,22 @@ +pub mod helpers; + +use helpers::SccacheTest; +use anyhow::Result; +use predicates::str::PredicateStrExt; +use serial_test::serial; + +#[test] +#[serial] +fn test_sccache_cache_size() -> Result<()> { + let test_info = SccacheTest::new(None)?; + + test_info + .show_text_stats(false)? + .try_stdout( + predicates::str::is_match(r"^Cache size\s+\d+\s") + .unwrap() + .from_utf8(), + )? + .try_success()?; + Ok(()) +} From 6878ef434c0941ac02c4f91fba2d8ed48af72267 Mon Sep 17 00:00:00 2001 From: iTrooz Date: Tue, 26 Aug 2025 18:59:36 +0200 Subject: [PATCH 4/5] fix formatting --- tests/stats_output.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/stats_output.rs b/tests/stats_output.rs index 7ff143041..1145ed746 100644 --- a/tests/stats_output.rs +++ b/tests/stats_output.rs @@ -1,7 +1,7 @@ pub mod helpers; -use helpers::SccacheTest; use anyhow::Result; +use helpers::SccacheTest; use predicates::str::PredicateStrExt; use serial_test::serial; @@ -14,8 +14,8 @@ fn test_sccache_cache_size() -> Result<()> { .show_text_stats(false)? .try_stdout( predicates::str::is_match(r"^Cache size\s+\d+\s") - .unwrap() - .from_utf8(), + .unwrap() + .from_utf8(), )? .try_success()?; Ok(()) From dc7243defd77cbc45dc19a5dd6a532f281717b45 Mon Sep 17 00:00:00 2001 From: iTrooz Date: Wed, 3 Sep 2025 20:14:36 +0200 Subject: [PATCH 5/5] fix test --- tests/stats_output.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/stats_output.rs b/tests/stats_output.rs index 1145ed746..7d9eaadf3 100644 --- a/tests/stats_output.rs +++ b/tests/stats_output.rs @@ -13,7 +13,7 @@ fn test_sccache_cache_size() -> Result<()> { test_info .show_text_stats(false)? .try_stdout( - predicates::str::is_match(r"^Cache size\s+\d+\s") + predicates::str::is_match(r"Cache size\s+\d+\s") .unwrap() .from_utf8(), )?