Skip to main content

btrfs_cli/filesystem/
commit_stats.rs

1use crate::{RunContext, Runnable, util::open_path};
2use anyhow::{Context, Result};
3use btrfs_uapi::{filesystem::filesystem_info, sysfs::SysfsBtrfs};
4use clap::Parser;
5use std::{os::unix::io::AsFd, path::PathBuf};
6
7/// Show commit statistics for a mounted filesystem
8#[derive(Parser, Debug)]
9#[allow(clippy::doc_markdown)]
10pub struct FilesystemCommitStatsCommand {
11    /// Print stats then reset the max_commit_ms counter (requires root)
12    #[clap(long, short = 'z')]
13    pub reset: bool,
14
15    pub path: PathBuf,
16}
17
18impl Runnable for FilesystemCommitStatsCommand {
19    fn run(&self, _ctx: &RunContext) -> Result<()> {
20        let file = open_path(&self.path)?;
21
22        let info = filesystem_info(file.as_fd()).with_context(|| {
23            format!(
24                "failed to get filesystem info for '{}'",
25                self.path.display()
26            )
27        })?;
28
29        let sysfs = SysfsBtrfs::new(&info.uuid);
30
31        let stats = sysfs
32            .commit_stats()
33            .context("failed to read commit_stats from sysfs")?;
34
35        println!("UUID: {}", info.uuid.as_hyphenated());
36        println!("Commit stats since mount:");
37        println!("  {:<28}{:>8}", "Total commits:", stats.commits);
38        println!(
39            "  {:<28}{:>8}ms",
40            "Last commit duration:", stats.last_commit_ms
41        );
42        println!(
43            "  {:<28}{:>8}ms",
44            "Max commit duration:", stats.max_commit_ms
45        );
46        println!(
47            "  {:<28}{:>8}ms",
48            "Total time spent in commit:", stats.total_commit_ms
49        );
50
51        if self.reset {
52            sysfs
53                .reset_commit_stats()
54                .context("failed to reset commit_stats (requires root)")?;
55            println!("NOTE: Max commit duration has been reset");
56        }
57
58        Ok(())
59    }
60}