Skip to main content

btrfs_cli/filesystem/
commit_stats.rs

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