]> www.infradead.org Git - nvme.git/commitdiff
bcachefs: bch_acct_compression
authorKent Overstreet <kent.overstreet@linux.dev>
Sun, 7 Jan 2024 02:42:36 +0000 (21:42 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 14 Jul 2024 23:00:13 +0000 (19:00 -0400)
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>
fs/bcachefs/buckets.c
fs/bcachefs/disk_accounting.c
fs/bcachefs/disk_accounting_format.h

index e3bf7ed5c07308f05ade95154e1887b55febd0b7..06e968d98018d477dc80fd87b7a9853608324a0a 100644 (file)
@@ -707,13 +707,18 @@ static int __trigger_extent(struct btree_trans *trans,
        s64 replicas_sectors = 0;
        int ret = 0;
 
-       struct disk_accounting_pos acc = {
+       struct disk_accounting_pos acc_replicas_key = {
                .type                   = BCH_DISK_ACCOUNTING_replicas,
                .replicas.data_type     = data_type,
                .replicas.nr_devs       = 0,
                .replicas.nr_required   = 1,
        };
 
+       struct disk_accounting_pos acct_compression_key = {
+               .type                   = BCH_DISK_ACCOUNTING_compression,
+       };
+       u64 compression_acct[3] = { 1, 0, 0 };
+
        bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
                s64 disk_sectors = 0;
                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,
 
                bool stale = ret > 0;
 
+               if (p.ptr.cached && stale)
+                       continue;
+
                if (p.ptr.cached) {
-                       if (!stale) {
-                               ret = bch2_mod_dev_cached_sectors(trans, p.ptr.dev, disk_sectors, gc);
-                               if (ret)
-                                       return ret;
-                       }
+                       ret = bch2_mod_dev_cached_sectors(trans, p.ptr.dev, disk_sectors, gc);
+                       if (ret)
+                               return ret;
                } else if (!p.has_ec) {
                        replicas_sectors       += disk_sectors;
-                       acc.replicas.devs[acc.replicas.nr_devs++] = p.ptr.dev;
+                       acc_replicas_key.replicas.devs[acc_replicas_key.replicas.nr_devs++] = p.ptr.dev;
                } else {
                        ret = bch2_trigger_stripe_ptr(trans, k, p, data_type, disk_sectors, flags);
                        if (ret)
@@ -741,12 +747,43 @@ static int __trigger_extent(struct btree_trans *trans,
                         * if so they're not required for mounting if we have an
                         * erasure coded pointer in this extent:
                         */
-                       acc.replicas.nr_required = 0;
+                       acc_replicas_key.replicas.nr_required = 0;
+               }
+
+               if (acct_compression_key.compression.type &&
+                   acct_compression_key.compression.type != p.crc.compression_type) {
+                       if (flags & BTREE_TRIGGER_overwrite)
+                               bch2_u64s_neg(compression_acct, ARRAY_SIZE(compression_acct));
+
+                       ret = bch2_disk_accounting_mod(trans, &acct_compression_key, compression_acct,
+                                                      ARRAY_SIZE(compression_acct), gc);
+                       if (ret)
+                               return ret;
+
+                       compression_acct[0] = 1;
+                       compression_acct[1] = 0;
+                       compression_acct[2] = 0;
                }
+
+               acct_compression_key.compression.type = p.crc.compression_type;
+               if (p.crc.compression_type) {
+                       compression_acct[1] += p.crc.uncompressed_size;
+                       compression_acct[2] += p.crc.compressed_size;
+               }
+       }
+
+       if (acc_replicas_key.replicas.nr_devs) {
+               ret = bch2_disk_accounting_mod(trans, &acc_replicas_key, &replicas_sectors, 1, gc);
+               if (ret)
+                       return ret;
        }
 
-       if (acc.replicas.nr_devs) {
-               ret = bch2_disk_accounting_mod(trans, &acc, &replicas_sectors, 1, gc);
+       if (acct_compression_key.compression.type) {
+               if (flags & BTREE_TRIGGER_overwrite)
+                       bch2_u64s_neg(compression_acct, ARRAY_SIZE(compression_acct));
+
+               ret = bch2_disk_accounting_mod(trans, &acct_compression_key, compression_acct,
+                                              ARRAY_SIZE(compression_acct), gc);
                if (ret)
                        return ret;
        }
index 1d45d828061895486d8d437f533b6eb59788dfdf..3b76b25b5ca2097d84d17b48f1a9457724324575 100644 (file)
@@ -6,6 +6,7 @@
 #include "btree_update.h"
 #include "btree_write_buffer.h"
 #include "buckets.h"
+#include "compress.h"
 #include "disk_accounting.h"
 #include "error.h"
 #include "journal_io.h"
@@ -142,6 +143,9 @@ void bch2_accounting_key_to_text(struct printbuf *out, struct disk_accounting_po
                prt_printf(out, "dev=%u data_type=", k->dev_data_type.dev);
                bch2_prt_data_type(out, k->dev_data_type.data_type);
                break;
+       case BCH_DISK_ACCOUNTING_compression:
+               bch2_prt_compression_type(out, k->compression.type);
+               break;
        }
 }
 
index af5f5789fe5d7586fcf09f24caf2b0f23bbd5241..2ded8ae650016f802df586dbab2ea2525d4d8c99 100644 (file)
@@ -99,7 +99,8 @@ static inline bool data_type_is_hidden(enum bch_data_type type)
        x(nr_inodes,            0)              \
        x(persistent_reserved,  1)              \
        x(replicas,             2)              \
-       x(dev_data_type,        3)
+       x(dev_data_type,        3)              \
+       x(compression,          4)
 
 enum disk_accounting_type {
 #define x(f, nr)       BCH_DISK_ACCOUNTING_##f = nr,
@@ -124,6 +125,10 @@ struct bch_dev_stripe_buckets {
        __u8                    dev;
 };
 
+struct bch_acct_compression {
+       __u8                    type;
+};
+
 struct disk_accounting_pos {
        union {
        struct {
@@ -134,6 +139,7 @@ struct disk_accounting_pos {
                struct bch_replicas_entry_v1    replicas;
                struct bch_dev_data_type        dev_data_type;
                struct bch_dev_stripe_buckets   dev_stripe_buckets;
+               struct bch_acct_compression     compression;
                };
        };
                struct bpos                     _pad;