Skip to content

Commit bfcaa90

Browse files
author
Kent Overstreet
committed
bcachefs: bch_acct_compression
This adds per-compression-type accounting of compressed and uncompressed size as well as number of extents - meaning we can now see compression ratio (without walking the whole filesystem). Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 5668e5d commit bfcaa90

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

fs/bcachefs/buckets.c

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -707,13 +707,18 @@ static int __trigger_extent(struct btree_trans *trans,
707707
s64 replicas_sectors = 0;
708708
int ret = 0;
709709

710-
struct disk_accounting_pos acc = {
710+
struct disk_accounting_pos acc_replicas_key = {
711711
.type = BCH_DISK_ACCOUNTING_replicas,
712712
.replicas.data_type = data_type,
713713
.replicas.nr_devs = 0,
714714
.replicas.nr_required = 1,
715715
};
716716

717+
struct disk_accounting_pos acct_compression_key = {
718+
.type = BCH_DISK_ACCOUNTING_compression,
719+
};
720+
u64 compression_acct[3] = { 1, 0, 0 };
721+
717722
bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
718723
s64 disk_sectors = 0;
719724
ret = bch2_trigger_pointer(trans, btree_id, level, k, p, entry, &disk_sectors, flags);
@@ -722,15 +727,16 @@ static int __trigger_extent(struct btree_trans *trans,
722727

723728
bool stale = ret > 0;
724729

730+
if (p.ptr.cached && stale)
731+
continue;
732+
725733
if (p.ptr.cached) {
726-
if (!stale) {
727-
ret = bch2_mod_dev_cached_sectors(trans, p.ptr.dev, disk_sectors, gc);
728-
if (ret)
729-
return ret;
730-
}
734+
ret = bch2_mod_dev_cached_sectors(trans, p.ptr.dev, disk_sectors, gc);
735+
if (ret)
736+
return ret;
731737
} else if (!p.has_ec) {
732738
replicas_sectors += disk_sectors;
733-
acc.replicas.devs[acc.replicas.nr_devs++] = p.ptr.dev;
739+
acc_replicas_key.replicas.devs[acc_replicas_key.replicas.nr_devs++] = p.ptr.dev;
734740
} else {
735741
ret = bch2_trigger_stripe_ptr(trans, k, p, data_type, disk_sectors, flags);
736742
if (ret)
@@ -741,12 +747,43 @@ static int __trigger_extent(struct btree_trans *trans,
741747
* if so they're not required for mounting if we have an
742748
* erasure coded pointer in this extent:
743749
*/
744-
acc.replicas.nr_required = 0;
750+
acc_replicas_key.replicas.nr_required = 0;
751+
}
752+
753+
if (acct_compression_key.compression.type &&
754+
acct_compression_key.compression.type != p.crc.compression_type) {
755+
if (flags & BTREE_TRIGGER_overwrite)
756+
bch2_u64s_neg(compression_acct, ARRAY_SIZE(compression_acct));
757+
758+
ret = bch2_disk_accounting_mod(trans, &acct_compression_key, compression_acct,
759+
ARRAY_SIZE(compression_acct), gc);
760+
if (ret)
761+
return ret;
762+
763+
compression_acct[0] = 1;
764+
compression_acct[1] = 0;
765+
compression_acct[2] = 0;
745766
}
767+
768+
acct_compression_key.compression.type = p.crc.compression_type;
769+
if (p.crc.compression_type) {
770+
compression_acct[1] += p.crc.uncompressed_size;
771+
compression_acct[2] += p.crc.compressed_size;
772+
}
773+
}
774+
775+
if (acc_replicas_key.replicas.nr_devs) {
776+
ret = bch2_disk_accounting_mod(trans, &acc_replicas_key, &replicas_sectors, 1, gc);
777+
if (ret)
778+
return ret;
746779
}
747780

748-
if (acc.replicas.nr_devs) {
749-
ret = bch2_disk_accounting_mod(trans, &acc, &replicas_sectors, 1, gc);
781+
if (acct_compression_key.compression.type) {
782+
if (flags & BTREE_TRIGGER_overwrite)
783+
bch2_u64s_neg(compression_acct, ARRAY_SIZE(compression_acct));
784+
785+
ret = bch2_disk_accounting_mod(trans, &acct_compression_key, compression_acct,
786+
ARRAY_SIZE(compression_acct), gc);
750787
if (ret)
751788
return ret;
752789
}

fs/bcachefs/disk_accounting.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "btree_update.h"
77
#include "btree_write_buffer.h"
88
#include "buckets.h"
9+
#include "compress.h"
910
#include "disk_accounting.h"
1011
#include "error.h"
1112
#include "journal_io.h"
@@ -142,6 +143,9 @@ void bch2_accounting_key_to_text(struct printbuf *out, struct disk_accounting_po
142143
prt_printf(out, "dev=%u data_type=", k->dev_data_type.dev);
143144
bch2_prt_data_type(out, k->dev_data_type.data_type);
144145
break;
146+
case BCH_DISK_ACCOUNTING_compression:
147+
bch2_prt_compression_type(out, k->compression.type);
148+
break;
145149
}
146150
}
147151

fs/bcachefs/disk_accounting_format.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ static inline bool data_type_is_hidden(enum bch_data_type type)
9999
x(nr_inodes, 0) \
100100
x(persistent_reserved, 1) \
101101
x(replicas, 2) \
102-
x(dev_data_type, 3)
102+
x(dev_data_type, 3) \
103+
x(compression, 4)
103104

104105
enum disk_accounting_type {
105106
#define x(f, nr) BCH_DISK_ACCOUNTING_##f = nr,
@@ -124,6 +125,10 @@ struct bch_dev_stripe_buckets {
124125
__u8 dev;
125126
};
126127

128+
struct bch_acct_compression {
129+
__u8 type;
130+
};
131+
127132
struct disk_accounting_pos {
128133
union {
129134
struct {
@@ -134,6 +139,7 @@ struct disk_accounting_pos {
134139
struct bch_replicas_entry_v1 replicas;
135140
struct bch_dev_data_type dev_data_type;
136141
struct bch_dev_stripe_buckets dev_stripe_buckets;
142+
struct bch_acct_compression compression;
137143
};
138144
};
139145
struct bpos _pad;

0 commit comments

Comments
 (0)