Skip to content

Commit c0a874f

Browse files
intelfxbehlendorf
authored andcommitted
zdb: add --class=(normal|special|...) to filter blocks by alloc class
When counting blocks to generate block size histograms (`-bb`), accept a `--class=` argument (as a comma-separated list of either "normal", "special", "dedup" or "other") to only consider blocks that belong to these metaslab classes. Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Ivan Shapovalov <[email protected]> Closes #16999
1 parent 8e97b98 commit c0a874f

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

cmd/zdb/zdb.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ extern uint_t zfs_btree_verify_intensity;
109109
enum {
110110
ARG_ALLOCATED = 256,
111111
ARG_BLOCK_BIN_MODE,
112+
ARG_BLOCK_CLASSES,
112113
};
113114

114115
static const char cmdname[] = "zdb";
@@ -141,6 +142,13 @@ static enum {
141142
BIN_ASIZE,
142143
} block_bin_mode = BIN_AUTO;
143144

145+
static enum {
146+
CLASS_NORMAL = 1 << 1,
147+
CLASS_SPECIAL = 1 << 2,
148+
CLASS_DEDUP = 1 << 3,
149+
CLASS_OTHER = 1 << 4,
150+
} block_classes = 0;
151+
144152
static void snprintf_blkptr_compact(char *, size_t, const blkptr_t *,
145153
boolean_t);
146154
static void mos_obj_refd(uint64_t);
@@ -761,6 +769,10 @@ usage(void)
761769
"block statistics\n");
762770
(void) fprintf(stderr, " --bin=(lsize|psize|asize) "
763771
"bin blocks based on this size in all three columns\n");
772+
(void) fprintf(stderr,
773+
" --class=(normal|special|dedup|other)[,...]\n"
774+
" only consider blocks from "
775+
"these allocation classes\n");
764776
(void) fprintf(stderr, " -B --backup "
765777
"backup stream\n");
766778
(void) fprintf(stderr, " -c --checksum "
@@ -5840,6 +5852,20 @@ dump_size_histograms(zdb_cb_t *zcb)
58405852
printf("(note: all categories are binned separately)\n");
58415853
break;
58425854
}
5855+
if (block_classes != 0) {
5856+
char buf[256] = "";
5857+
if (block_classes & CLASS_NORMAL)
5858+
strlcat(buf, "\"normal\", ", sizeof (buf));
5859+
if (block_classes & CLASS_SPECIAL)
5860+
strlcat(buf, "\"special\", ", sizeof (buf));
5861+
if (block_classes & CLASS_DEDUP)
5862+
strlcat(buf, "\"dedup\", ", sizeof (buf));
5863+
if (block_classes & CLASS_OTHER)
5864+
strlcat(buf, "\"other\", ", sizeof (buf));
5865+
buf[strlen(buf)-2] = '\0';
5866+
printf("(note: only blocks in these classes are counted: %s)\n",
5867+
buf);
5868+
}
58435869
/*
58445870
* Print the first line titles
58455871
*/
@@ -6189,6 +6215,38 @@ zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
61896215
return;
61906216
}
61916217

6218+
if (block_classes != 0) {
6219+
spa_config_enter(zcb->zcb_spa, SCL_CONFIG, FTAG, RW_READER);
6220+
6221+
uint64_t vdev = DVA_GET_VDEV(&bp->blk_dva[0]);
6222+
uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[0]);
6223+
vdev_t *vd = vdev_lookup_top(zcb->zcb_spa, vdev);
6224+
ASSERT(vd != NULL);
6225+
metaslab_t *ms = vd->vdev_ms[offset >> vd->vdev_ms_shift];
6226+
ASSERT(ms != NULL);
6227+
metaslab_group_t *mg = ms->ms_group;
6228+
ASSERT(mg != NULL);
6229+
metaslab_class_t *mc = mg->mg_class;
6230+
ASSERT(mc != NULL);
6231+
6232+
spa_config_exit(zcb->zcb_spa, SCL_CONFIG, FTAG);
6233+
6234+
int class;
6235+
if (mc == spa_normal_class(zcb->zcb_spa)) {
6236+
class = CLASS_NORMAL;
6237+
} else if (mc == spa_special_class(zcb->zcb_spa)) {
6238+
class = CLASS_SPECIAL;
6239+
} else if (mc == spa_dedup_class(zcb->zcb_spa)) {
6240+
class = CLASS_DEDUP;
6241+
} else {
6242+
class = CLASS_OTHER;
6243+
}
6244+
6245+
if (!(block_classes & class)) {
6246+
goto hist_skipped;
6247+
}
6248+
}
6249+
61926250
/*
61936251
* The binning histogram bins by powers of two up to
61946252
* SPA_MAXBLOCKSIZE rather than creating bins for
@@ -6225,6 +6283,7 @@ zdb_count_block(zdb_cb_t *zcb, zilog_t *zilog, const blkptr_t *bp,
62256283
zcb->zcb_asize_len[bin] += BP_GET_ASIZE(bp);
62266284
zcb->zcb_asize_total += BP_GET_ASIZE(bp);
62276285

6286+
hist_skipped:
62286287
if (!do_claim)
62296288
return;
62306289

@@ -9469,6 +9528,8 @@ main(int argc, char **argv)
94699528
ARG_ALLOCATED},
94709529
{"bin", required_argument, NULL,
94719530
ARG_BLOCK_BIN_MODE},
9531+
{"class", required_argument, NULL,
9532+
ARG_BLOCK_CLASSES},
94729533
{0, 0, 0, 0}
94739534
};
94749535

@@ -9596,6 +9657,45 @@ main(int argc, char **argv)
95969657
usage();
95979658
}
95989659
break;
9660+
9661+
case ARG_BLOCK_CLASSES: {
9662+
char *buf = strdup(optarg), *tok = buf, *next,
9663+
*save = NULL;
9664+
9665+
while ((next = strtok_r(tok, ",", &save)) != NULL) {
9666+
tok = NULL;
9667+
9668+
if (strcmp(next, "normal") == 0) {
9669+
block_classes |= CLASS_NORMAL;
9670+
} else if (strcmp(next, "special") == 0) {
9671+
block_classes |= CLASS_SPECIAL;
9672+
} else if (strcmp(next, "dedup") == 0) {
9673+
block_classes |= CLASS_DEDUP;
9674+
} else if (strcmp(next, "other") == 0) {
9675+
block_classes |= CLASS_OTHER;
9676+
} else {
9677+
(void) fprintf(stderr,
9678+
"--class=\"%s\" must be a "
9679+
"comma-separated list of either "
9680+
"\"normal\", \"special\", "
9681+
"\"asize\" or \"other\"; "
9682+
"got \"%s\"\n",
9683+
optarg, next);
9684+
usage();
9685+
}
9686+
}
9687+
9688+
if (block_classes == 0) {
9689+
(void) fprintf(stderr,
9690+
"--class= must be a comma-separated "
9691+
"list of either \"normal\", \"special\", "
9692+
"\"asize\" or \"other\"; got empty\n");
9693+
usage();
9694+
}
9695+
9696+
free(buf);
9697+
break;
9698+
}
95999699
default:
96009700
usage();
96019701
break;

man/man8/zdb.8

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ For instance, with
154154
.Fl -bin Ns = Ns Li lsize ,
155155
a block with lsize of 16K and psize of 4K will be added to the 16K bin
156156
in all three columns.
157+
.It Fl -class Ns = Ns ( Li normal Ns | Ns Li special Ns | Ns Li dedup Ns | Ns Li other ) Ns Op , Ns
158+
When used with
159+
.Fl bb ,
160+
only consider blocks from these allocation classes.
157161
.It Fl B , -backup
158162
Generate a backup stream, similar to
159163
.Nm zfs Cm send ,

0 commit comments

Comments
 (0)