[PATCH 05/27] xfs_db: compute average btree height

Darrick J. Wong
2025-02-06T22:51:02Z

Compute the btree height assuming that the blocks are 75% full.

Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
---
db/btheight.c | 31 +++++++++++++++++++++++++++++++
man/man8/xfs_db.8 | 6 +++++-
2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/db/btheight.c b/db/btheight.c
index 98165b522e4f6f..0456af08b39edf 100644
--- a/db/btheight.c
+++ b/db/btheight.c
@@ -259,6 +259,7 @@ _("%s: pointer size must be less than selected block size (%u bytes).\n"),
#define REPORT_MAX (1 << 0)
#define REPORT_MIN (1 << 1)
#define REPORT_ABSMAX (1 << 2)
+#define REPORT_AVG (1 << 3)

static void
report_absmax(const char *tag)
@@ -317,6 +318,34 @@ _("%s: best case per %u-byte block: %u records (leaf) / %u keyptrs (node)\n"),
calc_height(nr_records, records_per_block);
}

+ if (report_what & REPORT_AVG) {
+ records_per_block[0] *= 3;
+ records_per_block[0] /= 4;
+ records_per_block[1] *= 3;
+ records_per_block[1] /= 4;
+
+ if (records_per_block[0] < 2) {
+ fprintf(stderr,
+_("%s: cannot calculate average case scenario due to leaf geometry underflow.\n"),
+ tag);
+ return;
+ }
+
+ if (records_per_block[1] < 4) {
+ fprintf(stderr,
+_("%s: cannot calculate average case scenario due to node geometry underflow.\n"),
+ tag);
+ return;
+ }
+
+ printf(
+_("%s: average case per %u-byte block: %u records (leaf) / %u keyptrs (node)\n"),
+ tag, blocksize, records_per_block[0],
+ records_per_block[1]);
+
+ calc_height(nr_records, records_per_block);
+ }
+
if (report_what & REPORT_MIN) {
records_per_block[0] /= 2;
records_per_block[1] /= 2;
@@ -393,6 +422,8 @@ btheight_f(
report_what = REPORT_MAX;
else if (!strcmp(optarg, "absmax"))
report_what = REPORT_ABSMAX;
+ else if (!strcmp(optarg, "avg"))
+ report_what = REPORT_AVG;
else {
btheight_help();
return 0;
diff --git a/man/man8/xfs_db.8 b/man/man8/xfs_db.8
index 06f4464a928596..acee900adbda50 100644
--- a/man/man8/xfs_db.8
+++ b/man/man8/xfs_db.8
@@ -519,7 +519,7 @@ .SH COMMANDS
Dump all keys and pointers in intermediate btree nodes, and all records in leaf btree nodes.
.RE
.TP
-.BI "btheight [\-b " blksz "] [\-n " recs "] [\-w " max "|" min "|" absmax "] btree types..."
+.BI "btheight [\-b " blksz "] [\-n " recs "] [\-w " max "|" min "|" absmax "|" avg "] btree types..."
For a given number of btree records and a btree type, report the number of
records and blocks for each level of the btree, and the total number of blocks.
The btree type must be given after the options.
@@ -562,6 +562,10 @@ .SH COMMANDS
.B \-w min
shows only the worst case scenario, which is when the btree blocks are
half full.
+.TP
+.B \-w avg
+shows only the average case scenario, which is when the btree blocks are
+three quarters full.
.RE
.TP
.BI "convert " "type number" " [" "type number" "] ... " type