Skip to contentSkip to conversation

*** improved shrinker stats ***

PATCH New Daniel Hill 1 participant 4 messages Feb 19, 2024

lore
Share & tools
lore
#0 lore raw

cover *** improved shrinker stats ***

Since you asked so nicely Kent...

Daniel Hill (3):
bcachefs: add a printbuf formatter for percentages
bcachefs: add btree_key_cache shrinker stats
bcachefs: add hit/miss percent to btree_cache sysfs

 fs/bcachefs/btree_cache.c           | 35 +++++++++++++++++++++++++
 fs/bcachefs/btree_key_cache.c       | 40 +++++++++++++++++++++++++----
 fs/bcachefs/btree_key_cache_types.h |  4 +++
 fs/bcachefs/btree_types.h           |  3 +++
 fs/bcachefs/printbuf.c              | 14 ++++++++++
 fs/bcachefs/printbuf.h              |  1 +
 6 files changed, 92 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 bcachefs: add a printbuf formatter for percentages

---
 fs/bcachefs/printbuf.c | 14 ++++++++++++++
 fs/bcachefs/printbuf.h |  1 +
 2 files changed, 15 insertions(+)
fs/bcachefs/printbuf.c +14 -0
index b27d22925929..8e1f5b0bc2e9 100644
@@ -446,3 +446,17 @@ void bch2_prt_bitflags_vector(struct printbuf *out,
 		bch2_prt_printf(out, "%s", list[i]);
 	}
 }
+
+/* format a ratio of two u64s as a percentage (to three decimal places)
+ * supplying a default value for when the denominator is zero
+ */
+
+void bch2_prt_percent_rational_u64(struct printbuf *out,
+				   u64 numerator, u64 denominator, u64 def)
+{
+	u64 rem = 0;
+
+	bch2_prt_printf(out, "%3llu.%03llu%%",
+			denominator ? div64_u64_rem(numerator * 100, denominator, &rem) : def,
+			div64_u64(rem * 100, denominator));
+}
fs/bcachefs/printbuf.h +1 -0
index 9a4a56c40937..e8a87d17793b 100644
@@ -126,6 +126,7 @@ void bch2_prt_string_option(struct printbuf *, const char * const[], size_t);
 void bch2_prt_bitflags(struct printbuf *, const char * const[], u64);
 void bch2_prt_bitflags_vector(struct printbuf *, const char * const[],
 			      unsigned long *, unsigned);
+void bch2_prt_percent_rational_u64(struct printbuf *, u64, u64, u64);
 
 /* Initializer for a heap allocated printbuf: */
 #define PRINTBUF ((struct printbuf) { .heap_allocated = true })
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/3 bcachefs: add btree_key_cache shrinker stats

In reply to Daniel Hill

Some counters to keep track of what is causing us to skip over some entries causing potential leaks.

---
 fs/bcachefs/btree_key_cache.c       | 40 +++++++++++++++++++++++++----
 fs/bcachefs/btree_key_cache_types.h |  4 +++
 2 files changed, 39 insertions(+), 5 deletions(-)
fs/bcachefs/btree_key_cache.c +35 -5
index 74e52fd28abe..bed209a625cb 100644
@@ -827,6 +827,7 @@ static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
 	struct bucket_table *tbl;
 	struct bkey_cached *ck, *t;
 	size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
+	long shrinker_skipped = 0;
 	unsigned start, flags;
 	int srcu_idx;
 
@@ -854,7 +855,7 @@ static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
 	}
 
 	if (scanned >= nr)
-		goto out;
+		goto freed_out;
 
 	scanned += bc->nr_freed_pcpu;
 
@@ -871,6 +872,9 @@ static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
 		bc->nr_freed_pcpu--;
 	}
 
+freed_out:
+	atomic_long_add(freed > scanned ? 0 : scanned - freed, &bc->shrinker_srcu_skipped);
+
 	if (scanned >= nr)
 		goto out;
 
@@ -889,15 +893,21 @@ static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
 			next = rht_dereference_bucket_rcu(pos->next, tbl, bc->shrink_iter);
 			ck = container_of(pos, struct bkey_cached, hash);
 
-			if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
+			if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
+				shrinker_skipped++;
 				goto next;
+			}
 
-			if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
+			if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags)) {
 				clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
+				shrinker_skipped++;
+			}
 			else if (bkey_cached_lock_for_evict(ck)) {
 				bkey_cached_evict(bc, ck);
 				bkey_cached_free(bc, ck);
-			}
+
+			} else
+				shrinker_skipped++;
 
 			scanned++;
 			if (scanned >= nr)
@@ -913,6 +923,9 @@ static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
 
 	rcu_read_unlock();
 out:
+	atomic_long_inc(&bc->shrinker_count);
+	atomic_long_add(nr, &bc->shrinker_to_scan);
+	atomic_long_add(shrinker_skipped, &bc->shrinker_skipped);
 	memalloc_nofs_restore(flags);
 	srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
 	mutex_unlock(&bc->lock);
@@ -1044,12 +1057,29 @@ int bch2_fs_btree_key_cache_init(struct btree_key_cache *bc)
 
 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
 {
-	prt_printf(out, "nr_freed:\t%lu",	atomic_long_read(&c->nr_freed));
+	long scanned = atomic_long_read(&c->shrinker_to_scan);
+	long freed = atomic_long_read(&c->nr_freed);
+
+	prt_printf(out, "nr_freed:\t%lu",	freed);
 	prt_newline(out);
 	prt_printf(out, "nr_keys:\t%lu",	atomic_long_read(&c->nr_keys));
 	prt_newline(out);
 	prt_printf(out, "nr_dirty:\t%lu",	atomic_long_read(&c->nr_dirty));
 	prt_newline(out);
+	prt_printf(out, "shrinker_count:\t%lu",	atomic_long_read(&c->shrinker_count));
+	prt_newline(out);
+	prt_printf(out, "shrinker_to_scan:\t\t%lu", scanned);
+	prt_newline(out);
+	prt_printf(out, "shrinker_skipped:\t\t%lu",
+		   atomic_long_read(&c->shrinker_skipped));
+	prt_newline(out);
+	prt_printf(out, "shrinker_srcu_skipped:\t\t%lu",
+		   atomic_long_read(&c->shrinker_srcu_skipped));
+	prt_newline(out);
+	prt_printf(out, "freed/scanned: ");
+	bch2_prt_percent_rational_u64(out, freed, scanned, 100);
+	prt_newline(out);
+
 }
 
 void bch2_btree_key_cache_exit(void)
fs/bcachefs/btree_key_cache_types.h +4 -0
index 290e4e57df5b..ba5dd263b2e4 100644
@@ -24,6 +24,10 @@ struct btree_key_cache {
 	atomic_long_t		nr_freed;
 	atomic_long_t		nr_keys;
 	atomic_long_t		nr_dirty;
+	atomic_long_t		shrinker_count;
+	atomic_long_t		shrinker_to_scan;
+	atomic_long_t		shrinker_skipped;
+	atomic_long_t		shrinker_srcu_skipped;
 };
 
 struct bkey_cached_key {
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/3 bcachefs: add hit/miss percent to btree_cache sysfs

In reply to Daniel Hill

This should help us identify any performance issues with the btree node cache.

---
 fs/bcachefs/btree_cache.c | 35 +++++++++++++++++++++++++++++++++++
 fs/bcachefs/btree_types.h |  3 +++
 2 files changed, 38 insertions(+)
fs/bcachefs/btree_cache.c +35 -0
index 799750464969..8d2954ab3598 100644
@@ -418,6 +418,9 @@ void bch2_fs_btree_cache_exit(struct bch_fs *c)
 	flags = memalloc_nofs_save();
 	mutex_lock(&bc->lock);
 
+	free_percpu(bc->hits);
+	free_percpu(bc->misses);
+
 	if (c->verify_data)
 		list_move(&c->verify_data->list, &bc->live);
 
@@ -467,6 +470,11 @@ int bch2_fs_btree_cache_init(struct bch_fs *c)
 	unsigned i;
 	int ret = 0;
 
+	bc->hits   = __alloc_percpu(sizeof(u64) * BTREE_MAX_DEPTH, sizeof(u64));
+	bc->misses = __alloc_percpu(sizeof(u64) * BTREE_MAX_DEPTH, sizeof(u64));
+	if (!bc->hits || !bc->misses)
+		goto err;
+
 	ret = rhashtable_init(&bc->table, &bch_btree_cache_params);
 	if (ret)
 		goto err;
@@ -833,6 +841,7 @@ static struct btree *__bch2_btree_node_get(struct btree_trans *trans, struct btr
 	struct btree *b;
 	struct bset_tree *t;
 	bool need_relock = false;
+	bool is_hit = true;
 	int ret;
 
 	EBUG_ON(level >= BTREE_MAX_DEPTH);
@@ -852,6 +861,8 @@ static struct btree *__bch2_btree_node_get(struct btree_trans *trans, struct btr
 		if (!b)
 			goto retry;
 
+		is_hit = false;
+
 		if (IS_ERR(b))
 			return b;
 	} else {
@@ -925,6 +936,11 @@ static struct btree *__bch2_btree_node_get(struct btree_trans *trans, struct btr
 	EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
 	btree_check_header(c, b);
 
+	if (is_hit)
+		this_cpu_inc(bc->hits[level]);
+	else
+		this_cpu_inc(bc->misses[level]);
+
 	return b;
 }
 
@@ -1029,6 +1045,7 @@ struct btree *bch2_btree_node_get_noiter(struct btree_trans *trans,
 	struct btree_cache *bc = &c->btree_cache;
 	struct btree *b;
 	struct bset_tree *t;
+	bool is_hit = true;
 	int ret;
 
 	EBUG_ON(level >= BTREE_MAX_DEPTH);
@@ -1055,6 +1072,8 @@ struct btree *bch2_btree_node_get_noiter(struct btree_trans *trans,
 		    !bch2_btree_cache_cannibalize_lock(trans, NULL))
 			goto retry;
 
+		is_hit = false;
+
 		if (IS_ERR(b))
 			goto out;
 	} else {
@@ -1100,6 +1119,11 @@ struct btree *bch2_btree_node_get_noiter(struct btree_trans *trans,
 	EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
 	btree_check_header(c, b);
 out:
+	if (is_hit)
+		this_cpu_inc(bc->hits[level]);
+	else
+		this_cpu_inc(bc->misses[level]);
+
 	bch2_btree_cache_cannibalize_unlock(trans);
 	return b;
 }
@@ -1225,4 +1249,15 @@ void bch2_btree_cache_to_text(struct printbuf *out, const struct bch_fs *c)
 	prt_printf(out, "nr nodes:\t\t%u\n", c->btree_cache.used);
 	prt_printf(out, "nr dirty:\t\t%u\n", atomic_read(&c->btree_cache.dirty));
 	prt_printf(out, "cannibalize lock:\t%p\n", c->btree_cache.alloc_lock);
+
+	for (int i = 0; i < BTREE_MAX_DEPTH; i++) {
+		u64 hits = percpu_u64_get(&c->btree_cache.hits[i]);
+		u64 misses = percpu_u64_get(&c->btree_cache.misses[i]);
+		u64 total = hits + misses;
+
+		prt_printf(out, "level %i misses: ", i);
+		bch2_prt_percent_rational_u64(out, misses, total, 0);
+		prt_printf(out, " %llu+%llu/%llu", hits, misses, total);
+		prt_newline(out);
+	}
 }
fs/bcachefs/btree_types.h +3 -0
index b2ebf143c3b7..a10ab188908d 100644
@@ -179,6 +179,9 @@ struct btree_cache {
 	struct bbpos		pinned_nodes_end;
 	u64			pinned_nodes_leaf_mask;
 	u64			pinned_nodes_interior_mask;
+
+	u64 __percpu *hits;
+	u64 __percpu *misses;
 };
 
 struct btree_node_iter {
Signature · 1 line
2.43.0