diff mbox series

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

Message ID cccf210bb40ff2455b03c8e6f77a9bef62d80cb3.1719910442.git.cccheng@synology.com (mailing list archive)
State New
Headers show
Series btrfs-progs: inspect tree-stats: support to show a specified tree | expand

Commit Message

Chung-Chiang Cheng July 2, 2024, 8:55 a.m. UTC
tree-stats currently displays only some global trees and fs-tree 5. Add
support to show the stats of a specified tree.

Signed-off-by: Chung-Chiang Cheng <cccheng@synology.com>
---
 cmds/inspect-tree-stats.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

Comments

David Sterba July 2, 2024, 11:24 p.m. UTC | #1
On Tue, Jul 02, 2024 at 04:55:02PM +0800, Chung-Chiang Cheng wrote:
> tree-stats currently displays only some global trees and fs-tree 5. Add
> support to show the stats of a specified tree.
> 
> Signed-off-by: Chung-Chiang Cheng <cccheng@synology.com>

That's useful, thanks. I've added the option to documentation too.
diff mbox series

Patch

diff --git a/cmds/inspect-tree-stats.c b/cmds/inspect-tree-stats.c
index 42dab804528a..82298cd04a28 100644
--- a/cmds/inspect-tree-stats.c
+++ b/cmds/inspect-tree-stats.c
@@ -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 <tree_id>", "print only tree with the given id"),
 	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);