lsm_tree/vlog/gc/
report.rs

1// Copyright (c) 2024-present, fjall-rs
2// This source code is licensed under both the Apache 2.0 and MIT License
3// (found in the LICENSE-* files in the repository)
4
5use std::path::PathBuf;
6
7/// Statistics report for garbage collection
8#[derive(Debug)]
9#[allow(clippy::module_name_repetitions)]
10pub struct GcReport {
11    /// Path of value log
12    pub path: PathBuf,
13
14    /// Blob file count
15    pub blob_file_count: usize,
16
17    /// Blob files that have 100% stale blobs
18    pub stale_blob_file_count: usize,
19
20    /// Amount of stored bytes
21    pub total_bytes: u64,
22
23    /// Amount of bytes that could be freed
24    pub stale_bytes: u64,
25
26    /// Number of stored blobs
27    pub total_blobs: u64,
28
29    /// Number of blobs that could be freed
30    pub stale_blobs: u64,
31}
32
33impl std::fmt::Display for GcReport {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        writeln!(f, "--- GC report for vLog @ {} ---", self.path.display())?;
36        writeln!(f, "# files    : {}", self.blob_file_count)?;
37        writeln!(f, "# stale    : {}", self.stale_blob_file_count)?;
38        writeln!(f, "Total bytes: {}", self.total_bytes)?;
39        writeln!(f, "Stale bytes: {}", self.stale_bytes)?;
40        writeln!(f, "Total blobs: {}", self.total_blobs)?;
41        writeln!(f, "Stale blobs: {}", self.stale_blobs)?;
42        writeln!(f, "Stale ratio: {}", self.stale_ratio())?;
43        writeln!(f, "Space amp  : {}", self.space_amp())?;
44        writeln!(f, "--- GC report done ---")?;
45        Ok(())
46    }
47}
48
49impl GcReport {
50    /// Calculates the space amplification factor.
51    #[must_use]
52    pub fn space_amp(&self) -> f32 {
53        if self.total_bytes == 0 {
54            return 0.0;
55        }
56
57        let alive_bytes = self.total_bytes - self.stale_bytes;
58        if alive_bytes == 0 {
59            return 0.0;
60        }
61
62        self.total_bytes as f32 / alive_bytes as f32
63    }
64
65    /// Calculates the stale ratio (percentage).
66    #[must_use]
67    pub fn stale_ratio(&self) -> f32 {
68        if self.total_bytes == 0 {
69            return 0.0;
70        }
71
72        if self.stale_bytes == 0 {
73            return 0.0;
74        }
75
76        self.stale_bytes as f32 / self.total_bytes as f32
77    }
78}