Skip to content
Open
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
32 changes: 16 additions & 16 deletions src/cache/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,52 @@ 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(),
}
}
}

/// A cache that stores entries at local disk paths.
pub struct DiskCache {
/// `LruDiskCache` does all the real work here.
lru: Arc<Mutex<LazyDiskCache>>,
lru: Arc<Mutex<LazyLruDiskCache>>,
/// Thread pool to execute disk I/O
pool: tokio::runtime::Handle,
preprocessor_cache_mode_config: PreprocessorCacheModeConfig,
preprocessor_cache: Arc<Mutex<LazyDiskCache>>,
preprocessor_cache: Arc<Mutex<LazyLruDiskCache>>,
rw_mode: CacheMode,
}

Expand All @@ -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(),
Expand Down Expand Up @@ -173,7 +173,7 @@ impl Storage for DiskCache {
}

async fn current_size(&self) -> Result<Option<u64>> {
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<Option<u64>> {
Ok(Some(self.lru.lock().unwrap().capacity()))
Expand Down
22 changes: 22 additions & 0 deletions tests/stats_output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub mod helpers;

use anyhow::Result;
use helpers::SccacheTest;
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(())
}
Loading