btrfs-progs: tree-stats: add options for size output units

Add the usual long options for all byte size related values in the
output.

Issue: #268
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2024-07-17 03:59:50 +02:00
parent ef73193623
commit 0eb8a65844
2 changed files with 37 additions and 18 deletions

View File

@ -229,11 +229,27 @@ tree-stats [options] <device>
``Options``
-b
Print raw numbers in bytes.
-b|--raw
raw numbers in bytes, without the *B* suffix
-t <treeid>
Print stats only for the given treeid.
--human-readable
print human friendly numbers, base 1024, this is the default
--iec
select the 1024 base for the following options, according to the IEC standard
--si
select the 1000 base for the following options, according to the SI standard
--kbytes
show sizes in KiB, or kB with --si
--mbytes
show sizes in MiB, or MB with --si
--gbytes
show sizes in GiB, or GB with --si
--tbytes
show sizes in TiB, or TB with --si
EXIT STATUS
-----------

View File

@ -316,7 +316,7 @@ static void timeval_subtract(struct timeval *result, struct timeval *x,
}
static int calc_root_size(struct btrfs_root *tree_root, struct btrfs_key *key,
int find_inline)
int find_inline, unsigned int unit_mode)
{
struct btrfs_root *root;
struct btrfs_path path = { 0 };
@ -385,26 +385,25 @@ out_print:
pr_verbose(LOG_DEFAULT, "\tTotal read time: %d s %d us\n", (int)diff.tv_sec,
(int)diff.tv_usec);
} else {
pr_verbose(LOG_DEFAULT, "\tTotal size: %s\n", pretty_size(stat.total_bytes));
pr_verbose(LOG_DEFAULT, "\t\tInline data: %s\n", pretty_size(stat.total_inline));
pr_verbose(LOG_DEFAULT, "\tTotal size: %s\n", pretty_size_mode(stat.total_bytes, unit_mode));
pr_verbose(LOG_DEFAULT, "\t\tInline data: %s\n", pretty_size_mode(stat.total_inline, unit_mode));
pr_verbose(LOG_DEFAULT, "\tTotal seeks: %llu\n", stat.total_seeks);
pr_verbose(LOG_DEFAULT, "\t\tForward seeks: %llu\n", stat.forward_seeks);
pr_verbose(LOG_DEFAULT, "\t\tBackward seeks: %llu\n", stat.backward_seeks);
pr_verbose(LOG_DEFAULT, "\t\tAvg seek len: %s\n", stat.total_seeks ?
pretty_size(stat.total_seek_len / stat.total_seeks) :
pretty_size(0));
pretty_size_mode(stat.total_seek_len / stat.total_seeks, unit_mode) :
pretty_size_mode(0, unit_mode));
print_seek_histogram(&stat);
pr_verbose(LOG_DEFAULT, "\tTotal clusters: %llu\n", stat.total_clusters);
pr_verbose(LOG_DEFAULT, "\t\tAvg cluster size: %s\n",
pretty_size((stat.total_cluster_size /
stat.total_clusters)));
pretty_size_mode((stat.total_cluster_size /
stat.total_clusters), unit_mode));
pr_verbose(LOG_DEFAULT, "\t\tMin cluster size: %s\n",
pretty_size(stat.min_cluster_size));
pretty_size_mode(stat.min_cluster_size, unit_mode));
pr_verbose(LOG_DEFAULT, "\t\tMax cluster size: %s\n",
pretty_size(stat.max_cluster_size));
pretty_size_mode(stat.max_cluster_size, unit_mode));
pr_verbose(LOG_DEFAULT, "\tTotal disk spread: %s\n",
pretty_size(stat.highest_bytenr -
stat.lowest_bytenr));
pretty_size_mode(stat.highest_bytenr - stat.lowest_bytenr, unit_mode));
pr_verbose(LOG_DEFAULT, "\tTotal read time: %d s %d us\n", (int)diff.tv_sec,
(int)diff.tv_usec);
}
@ -442,6 +441,7 @@ static const char * const cmd_inspect_tree_stats_usage[] = {
"Print various stats for trees",
"",
OPTLINE("-b", "raw numbers in bytes"),
HELPINFO_UNITS_LONG,
OPTLINE("-t <rootid>", "print only tree with the given rootid"),
NULL
};
@ -451,10 +451,13 @@ static int cmd_inspect_tree_stats(const struct cmd_struct *cmd,
{
struct btrfs_key key = { .type = BTRFS_ROOT_ITEM_KEY };
struct btrfs_root *root;
unsigned int unit_mode;
int opt;
int ret = 0;
u64 tree_id = 0;
unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
optind = 0;
while ((opt = getopt(argc, argv, "vbt:")) != -1) {
switch (opt) {
@ -499,32 +502,32 @@ static int cmd_inspect_tree_stats(const struct cmd_struct *cmd,
pr_verbose(LOG_DEFAULT, "Calculating size of tree (%llu)\n", tree_id);
key.objectid = tree_id;
key.offset = (u64)-1;
ret = calc_root_size(root, &key, 1);
ret = calc_root_size(root, &key, 1, unit_mode);
goto out;
}
pr_verbose(LOG_DEFAULT, "Calculating size of root tree\n");
key.objectid = BTRFS_ROOT_TREE_OBJECTID;
ret = calc_root_size(root, &key, 0);
ret = calc_root_size(root, &key, 0, unit_mode);
if (ret)
goto out;
pr_verbose(LOG_DEFAULT, "Calculating size of extent tree\n");
key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
ret = calc_root_size(root, &key, 0);
ret = calc_root_size(root, &key, 0, unit_mode);
if (ret)
goto out;
pr_verbose(LOG_DEFAULT, "Calculating size of csum tree\n");
key.objectid = BTRFS_CSUM_TREE_OBJECTID;
ret = calc_root_size(root, &key, 0);
ret = calc_root_size(root, &key, 0, unit_mode);
if (ret)
goto out;
key.objectid = BTRFS_FS_TREE_OBJECTID;
key.offset = (u64)-1;
pr_verbose(LOG_DEFAULT, "Calculating size of fs tree\n");
ret = calc_root_size(root, &key, 1);
ret = calc_root_size(root, &key, 1, unit_mode);
if (ret)
goto out;
out: