btrfs-progs: inspect tree-stats: support to show a specified tree

tree-stats currently displays only some global trees and fs-tree 5. Add
support to show the stats of a specified tree.

Issue: #268
Signed-off-by: Chung-Chiang Cheng <cccheng@synology.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Chung-Chiang Cheng 2024-07-02 16:55:02 +08:00 committed by David Sterba
parent 2eadd15e6b
commit ef8e050d64
2 changed files with 22 additions and 1 deletions

View File

@ -228,6 +228,9 @@ tree-stats [options] <device>
-b
Print raw numbers in bytes.
-t <treeid>
Print stats only for the given treeid.
EXIT STATUS
-----------

View File

@ -35,6 +35,7 @@
#include "common/help.h"
#include "common/messages.h"
#include "common/open-utils.h"
#include "common/string-utils.h"
#include "common/units.h"
#include "cmds/commands.h"
@ -441,6 +442,7 @@ static const char * const cmd_inspect_tree_stats_usage[] = {
"Print various stats for trees",
"",
OPTLINE("-b", "raw numbers in bytes"),
OPTLINE("-t <rootid>", "print only tree with the given rootid"),
NULL
};
@ -451,9 +453,10 @@ static int cmd_inspect_tree_stats(const struct cmd_struct *cmd,
struct btrfs_root *root;
int opt;
int ret = 0;
u64 tree_id = 0;
optind = 0;
while ((opt = getopt(argc, argv, "vb")) != -1) {
while ((opt = getopt(argc, argv, "vbt:")) != -1) {
switch (opt) {
case 'v':
verbose++;
@ -461,6 +464,13 @@ static int cmd_inspect_tree_stats(const struct cmd_struct *cmd,
case 'b':
no_pretty = true;
break;
case 't':
tree_id = arg_strtou64(optarg);
if (!tree_id) {
error("unrecognized tree id: %s", optarg);
exit(1);
}
break;
default:
usage_unknown_option(cmd, argv);
}
@ -485,6 +495,14 @@ static int cmd_inspect_tree_stats(const struct cmd_struct *cmd,
exit(1);
}
if (tree_id) {
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);
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);