Skip to content

Commit 91f4478

Browse files
author
Kent Overstreet
committed
bcachefs: Convert bch2_compression_stats_to_text() to new accounting
We no longer have to walk the whole btree to calculate compression stats. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent bfcaa90 commit 91f4478

File tree

1 file changed

+19
-67
lines changed

1 file changed

+19
-67
lines changed

fs/bcachefs/sysfs.c

Lines changed: 19 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "buckets.h"
2323
#include "clock.h"
2424
#include "compress.h"
25+
#include "disk_accounting.h"
2526
#include "disk_groups.h"
2627
#include "ec.h"
2728
#include "inode.h"
@@ -255,91 +256,42 @@ static size_t bch2_btree_cache_size(struct bch_fs *c)
255256

256257
static int bch2_compression_stats_to_text(struct printbuf *out, struct bch_fs *c)
257258
{
258-
struct btree_trans *trans;
259-
enum btree_id id;
260-
struct compression_type_stats {
261-
u64 nr_extents;
262-
u64 sectors_compressed;
263-
u64 sectors_uncompressed;
264-
} s[BCH_COMPRESSION_TYPE_NR];
265-
u64 compressed_incompressible = 0;
266-
int ret = 0;
267-
268-
memset(s, 0, sizeof(s));
269-
270-
if (!test_bit(BCH_FS_started, &c->flags))
271-
return -EPERM;
272-
273-
trans = bch2_trans_get(c);
274-
275-
for (id = 0; id < BTREE_ID_NR; id++) {
276-
if (!btree_type_has_ptrs(id))
277-
continue;
278-
279-
ret = for_each_btree_key(trans, iter, id, POS_MIN,
280-
BTREE_ITER_all_snapshots, k, ({
281-
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
282-
struct bch_extent_crc_unpacked crc;
283-
const union bch_extent_entry *entry;
284-
bool compressed = false, incompressible = false;
285-
286-
bkey_for_each_crc(k.k, ptrs, crc, entry) {
287-
incompressible |= crc.compression_type == BCH_COMPRESSION_TYPE_incompressible;
288-
compressed |= crc_is_compressed(crc);
289-
290-
if (crc_is_compressed(crc)) {
291-
s[crc.compression_type].nr_extents++;
292-
s[crc.compression_type].sectors_compressed += crc.compressed_size;
293-
s[crc.compression_type].sectors_uncompressed += crc.uncompressed_size;
294-
}
295-
}
296-
297-
compressed_incompressible += compressed && incompressible;
298-
299-
if (!compressed) {
300-
unsigned t = incompressible ? BCH_COMPRESSION_TYPE_incompressible : 0;
301-
302-
s[t].nr_extents++;
303-
s[t].sectors_compressed += k.k->size;
304-
s[t].sectors_uncompressed += k.k->size;
305-
}
306-
0;
307-
}));
308-
}
309-
310-
bch2_trans_put(trans);
311-
312-
if (ret)
313-
return ret;
314-
259+
prt_str(out, "type");
315260
printbuf_tabstop_push(out, 12);
316261
printbuf_tabstop_push(out, 16);
317262
printbuf_tabstop_push(out, 16);
318263
printbuf_tabstop_push(out, 24);
319264
prt_printf(out, "type\tcompressed\runcompressed\raverage extent size\r\n");
320265

321-
for (unsigned i = 0; i < ARRAY_SIZE(s); i++) {
266+
for (unsigned i = 1; i < BCH_COMPRESSION_TYPE_NR; i++) {
267+
struct disk_accounting_pos a = {
268+
.type = BCH_DISK_ACCOUNTING_compression,
269+
.compression.type = i,
270+
};
271+
struct bpos p = disk_accounting_pos_to_bpos(&a);
272+
u64 v[3];
273+
bch2_accounting_mem_read(c, p, v, ARRAY_SIZE(v));
274+
275+
u64 nr_extents = v[0];
276+
u64 sectors_uncompressed = v[1];
277+
u64 sectors_compressed = v[2];
278+
322279
bch2_prt_compression_type(out, i);
323280
prt_tab(out);
324281

325-
prt_human_readable_u64(out, s[i].sectors_compressed << 9);
282+
prt_human_readable_u64(out, sectors_compressed << 9);
326283
prt_tab_rjust(out);
327284

328-
prt_human_readable_u64(out, s[i].sectors_uncompressed << 9);
285+
prt_human_readable_u64(out, sectors_uncompressed << 9);
329286
prt_tab_rjust(out);
330287

331-
prt_human_readable_u64(out, s[i].nr_extents
332-
? div_u64(s[i].sectors_uncompressed << 9, s[i].nr_extents)
288+
prt_human_readable_u64(out, nr_extents
289+
? div_u64(sectors_uncompressed << 9, nr_extents)
333290
: 0);
334291
prt_tab_rjust(out);
335292
prt_newline(out);
336293
}
337294

338-
if (compressed_incompressible) {
339-
prt_printf(out, "%llu compressed & incompressible extents", compressed_incompressible);
340-
prt_newline(out);
341-
}
342-
343295
return 0;
344296
}
345297

0 commit comments

Comments
 (0)