From: Mingzhe Zou <zoumingzhe@xxxxxx> When we want to shorten the moving gc interval, we must consider its impact, such as: performance, cache life. Usually ssd and nvme calculate the lifespan by the write cycles. When moving gc, only copy dirty data, which can reduce the amount of written data. This will improve moving gc speed, and extend cache life. Signed-off-by: Mingzhe Zou <mingzhe.zou@xxxxxxxxxxxx> --- drivers/md/bcache/alloc.c | 2 ++ drivers/md/bcache/bcache.h | 1 + drivers/md/bcache/btree.c | 10 +++++++++- drivers/md/bcache/movinggc.c | 16 ++++++++-------- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c index 4ae1018bf029..3d4b9f50b056 100644 --- a/drivers/md/bcache/alloc.c +++ b/drivers/md/bcache/alloc.c @@ -451,6 +451,7 @@ long bch_bucket_alloc(struct cache *ca, unsigned int reserve, bool wait) * should not do moving gc. So we set gc_sectors_used to the maximum. */ b->gc_sectors_used = ca->sb.bucket_size; + b->gc_sectors_dirty = ca->sb.bucket_size; if (reserve <= RESERVE_PRIO) { SET_GC_MARK(b, GC_MARK_METADATA); @@ -474,6 +475,7 @@ void __bch_bucket_free(struct cache *ca, struct bucket *b) { SET_GC_MARK(b, 0); b->gc_sectors_used = 0; + b->gc_sectors_dirty = 0; if (ca->set->avail_nbuckets < ca->set->nbuckets) { ca->set->avail_nbuckets++; diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h index 10f3f548629e..b3e8e4f513f1 100644 --- a/drivers/md/bcache/bcache.h +++ b/drivers/md/bcache/bcache.h @@ -201,6 +201,7 @@ struct bucket { uint8_t gen; uint8_t last_gc; /* Most out of date gen in the btree */ uint16_t gc_sectors_used; + uint16_t gc_sectors_dirty; }; /* diff --git a/drivers/md/bcache/btree.c b/drivers/md/bcache/btree.c index baa2149e9235..d4aeaaf1b9bc 100644 --- a/drivers/md/bcache/btree.c +++ b/drivers/md/bcache/btree.c @@ -1250,8 +1250,14 @@ static uint8_t __bch_btree_mark_key(struct cache_set *c, int level, if (level) SET_GC_MARK(g, GC_MARK_METADATA); - else if (KEY_DIRTY(k)) + else if (KEY_DIRTY(k)) { SET_GC_MARK(g, GC_MARK_DIRTY); + g->gc_sectors_dirty = min_t(uint16_t, c->cache->sb.bucket_size, + g->gc_sectors_dirty + KEY_SIZE(k)); + + BUG_ON(g->gc_sectors_dirty < KEY_SIZE(k) || + g->gc_sectors_dirty > c->cache->sb.bucket_size); + } else if (!GC_MARK(g)) SET_GC_MARK(g, GC_MARK_RECLAIMABLE); @@ -1743,6 +1749,7 @@ static void btree_gc_start(struct cache_set *c) if (!atomic_read(&b->pin)) { SET_GC_MARK(b, 0); b->gc_sectors_used = 0; + b->gc_sectors_dirty = 0; } } @@ -1806,6 +1813,7 @@ static void bch_btree_gc_finish(struct cache_set *c) continue; BUG_ON(!GC_MARK(b) && b->gc_sectors_used); + BUG_ON(!GC_MARK(b) && b->gc_sectors_dirty); if (!GC_MARK(b) || GC_MARK(b) == GC_MARK_RECLAIMABLE) c->avail_nbuckets++; diff --git a/drivers/md/bcache/movinggc.c b/drivers/md/bcache/movinggc.c index 2c3cb1b6e0af..98f2487f518d 100644 --- a/drivers/md/bcache/movinggc.c +++ b/drivers/md/bcache/movinggc.c @@ -27,7 +27,7 @@ static bool moving_pred(struct keybuf *buf, struct bkey *k) for (i = 0; i < KEY_PTRS(k); i++) if (ptr_available(c, k, i) && - GC_MOVE(PTR_BUCKET(c, k, i))) + GC_MOVE(PTR_BUCKET(c, k, i)) && KEY_DIRTY(k)) return true; return false; @@ -184,14 +184,14 @@ err: if (!IS_ERR_OR_NULL(w->private)) static bool bucket_cmp(struct bucket *l, struct bucket *r) { - return l->gc_sectors_used < r->gc_sectors_used; + return l->gc_sectors_dirty < r->gc_sectors_dirty; } static unsigned int bucket_heap_top(struct cache *ca) { struct bucket *b; - return (b = heap_peek(&ca->heap)) ? b->gc_sectors_used : 0; + return (b = heap_peek(&ca->heap)) ? b->gc_sectors_dirty : 0; } void bch_moving_gc(struct cache_set *c) @@ -215,17 +215,17 @@ void bch_moving_gc(struct cache_set *c) for_each_bucket(b, ca) { if (GC_MOVE(b) || GC_MARK(b) == GC_MARK_METADATA || - !b->gc_sectors_used || - b->gc_sectors_used == ca->sb.bucket_size || + !b->gc_sectors_dirty || + b->gc_sectors_dirty == ca->sb.bucket_size || atomic_read(&b->pin)) continue; if (!heap_full(&ca->heap)) { - sectors_to_move += b->gc_sectors_used; + sectors_to_move += b->gc_sectors_dirty; heap_add(&ca->heap, b, bucket_cmp); } else if (bucket_cmp(b, heap_peek(&ca->heap))) { sectors_to_move -= bucket_heap_top(ca); - sectors_to_move += b->gc_sectors_used; + sectors_to_move += b->gc_sectors_dirty; ca->heap.data[0] = b; heap_sift(&ca->heap, 0, bucket_cmp); @@ -237,7 +237,7 @@ void bch_moving_gc(struct cache_set *c) while (sectors_to_move > reserve_sectors) { heap_pop(&ca->heap, b, bucket_cmp); - sectors_to_move -= b->gc_sectors_used; + sectors_to_move -= b->gc_sectors_dirty; } while (heap_pop(&ca->heap, b, bucket_cmp)) -- 2.17.1.windows.2