Commit bfcaa907 authored by Kent Overstreet's avatar Kent Overstreet
Browse files

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: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 5668e5de
Loading
Loading
Loading
Loading
+47 −10
Original line number Diff line number Diff line
@@ -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;
			}
		} 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;
	}
+4 −0
Original line number Diff line number Diff line
@@ -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;
	}
}

+7 −1
Original line number Diff line number Diff line
@@ -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;