btrfs_cli/filesystem/
commit_stats.rs1use crate::{Format, Runnable};
2use anyhow::{Context, Result};
3use btrfs_uapi::{filesystem::filesystem_info, sysfs::SysfsBtrfs};
4use clap::Parser;
5use std::{fs::File, os::unix::io::AsFd, path::PathBuf};
6
7#[derive(Parser, Debug)]
9pub struct FilesystemCommitStatsCommand {
10 #[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 = File::open(&self.path).with_context(|| {
20 format!("failed to open '{}'", self.path.display())
21 })?;
22
23 let info = filesystem_info(file.as_fd()).with_context(|| {
24 format!(
25 "failed to get filesystem info for '{}'",
26 self.path.display()
27 )
28 })?;
29
30 let sysfs = SysfsBtrfs::new(&info.uuid);
31
32 let stats = sysfs
33 .commit_stats()
34 .context("failed to read commit_stats from sysfs")?;
35
36 println!("UUID: {}", info.uuid.as_hyphenated());
37 println!("Commit stats since mount:");
38 println!(" {:<28}{:>8}", "Total commits:", stats.commits);
39 println!(
40 " {:<28}{:>8}ms",
41 "Last commit duration:", stats.last_commit_ms
42 );
43 println!(
44 " {:<28}{:>8}ms",
45 "Max commit duration:", stats.max_commit_ms
46 );
47 println!(
48 " {:<28}{:>8}ms",
49 "Total time spent in commit:", stats.total_commit_ms
50 );
51
52 if self.reset {
53 sysfs
54 .reset_commit_stats()
55 .context("failed to reset commit_stats (requires root)")?;
56 println!("NOTE: Max commit duration has been reset");
57 }
58
59 Ok(())
60 }
61}