bcache: option to automatically run gc thread after writeback
authorColy Li <colyli@suse.de>
Thu, 13 Dec 2018 14:53:53 +0000 (22:53 +0800)
committerJens Axboe <axboe@kernel.dk>
Thu, 13 Dec 2018 15:15:54 +0000 (08:15 -0700)
The option gc_after_writeback is disabled by default, because garbage
collection will discard SSD data which drops cached data.

Echo 1 into /sys/fs/bcache/<UUID>/internal/gc_after_writeback will
enable this option, which wakes up gc thread when writeback accomplished
and all cached data is clean.

This option is helpful for people who cares writing performance more. In
heavy writing workload, all cached data can be clean only happens when
writeback thread cleans all cached data in I/O idle time. In such
situation a following gc running may help to shrink bcache B+ tree and
discard more clean data, which may be helpful for future writing
requests.

If you are not sure whether this is helpful for your own workload,
please leave it as disabled by default.

Signed-off-by: Coly Li <colyli@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/md/bcache/bcache.h
drivers/md/bcache/sysfs.c
drivers/md/bcache/writeback.c
drivers/md/bcache/writeback.h

index 96d2213f279ed39b0ea6f61836043caa63906eec..fdf75352e16a3aadf406c35484891c5aabcb431d 100644 (file)
@@ -626,6 +626,20 @@ struct cache_set {
        /* Where in the btree gc currently is */
        struct bkey             gc_done;
 
+       /*
+        * For automatical garbage collection after writeback completed, this
+        * varialbe is used as bit fields,
+        * - 0000 0001b (BCH_ENABLE_AUTO_GC): enable gc after writeback
+        * - 0000 0010b (BCH_DO_AUTO_GC):     do gc after writeback
+        * This is an optimization for following write request after writeback
+        * finished, but read hit rate dropped due to clean data on cache is
+        * discarded. Unless user explicitly sets it via sysfs, it won't be
+        * enabled.
+        */
+#define BCH_ENABLE_AUTO_GC     1
+#define BCH_DO_AUTO_GC         2
+       uint8_t                 gc_after_writeback;
+
        /*
         * The allocation code needs gc_mark in struct bucket to be correct, but
         * it's not while a gc is in progress. Protected by bucket_lock.
index c09748497cdc621d803bc27ec912ae29264b411e..621186b4240faafd9746c4f4d3b54f2e99ee3a4e 100644 (file)
@@ -128,6 +128,7 @@ rw_attribute(expensive_debug_checks);
 rw_attribute(cache_replacement_policy);
 rw_attribute(btree_shrinker_disabled);
 rw_attribute(copy_gc_enabled);
+rw_attribute(gc_after_writeback);
 rw_attribute(size);
 
 static ssize_t bch_snprint_string_list(char *buf,
@@ -693,6 +694,7 @@ SHOW(__bch_cache_set)
        sysfs_printf(gc_always_rewrite,         "%i", c->gc_always_rewrite);
        sysfs_printf(btree_shrinker_disabled,   "%i", c->shrinker_disabled);
        sysfs_printf(copy_gc_enabled,           "%i", c->copy_gc_enabled);
+       sysfs_printf(gc_after_writeback,        "%i", c->gc_after_writeback);
        sysfs_printf(io_disable,                "%i",
                     test_bit(CACHE_SET_IO_DISABLE, &c->flags));
 
@@ -793,6 +795,12 @@ STORE(__bch_cache_set)
        sysfs_strtoul(gc_always_rewrite,        c->gc_always_rewrite);
        sysfs_strtoul(btree_shrinker_disabled,  c->shrinker_disabled);
        sysfs_strtoul(copy_gc_enabled,          c->copy_gc_enabled);
+       /*
+        * write gc_after_writeback here may overwrite an already set
+        * BCH_DO_AUTO_GC, it doesn't matter because this flag will be
+        * set in next chance.
+        */
+       sysfs_strtoul_clamp(gc_after_writeback, c->gc_after_writeback, 0, 1);
 
        return size;
 }
@@ -873,6 +881,7 @@ static struct attribute *bch_cache_set_internal_files[] = {
        &sysfs_gc_always_rewrite,
        &sysfs_btree_shrinker_disabled,
        &sysfs_copy_gc_enabled,
+       &sysfs_gc_after_writeback,
        &sysfs_io_disable,
        NULL
 };
index 1696b212ec4e7652dad3fb7608c5b7dc92c7e40b..73f0efac2b9f703fe686c82572a599f8a5642494 100644 (file)
 #include <linux/sched/clock.h>
 #include <trace/events/bcache.h>
 
+static void update_gc_after_writeback(struct cache_set *c)
+{
+       if (c->gc_after_writeback != (BCH_ENABLE_AUTO_GC) ||
+           c->gc_stats.in_use < BCH_AUTO_GC_DIRTY_THRESHOLD)
+               return;
+
+       c->gc_after_writeback |= BCH_DO_AUTO_GC;
+}
+
 /* Rate limiting */
 static uint64_t __calc_target_rate(struct cached_dev *dc)
 {
@@ -191,6 +200,7 @@ static void update_writeback_rate(struct work_struct *work)
                if (!set_at_max_writeback_rate(c, dc)) {
                        down_read(&dc->writeback_lock);
                        __update_writeback_rate(dc);
+                       update_gc_after_writeback(c);
                        up_read(&dc->writeback_lock);
                }
        }
@@ -689,6 +699,23 @@ static int bch_writeback_thread(void *arg)
                                up_write(&dc->writeback_lock);
                                break;
                        }
+
+                       /*
+                        * When dirty data rate is high (e.g. 50%+), there might
+                        * be heavy buckets fragmentation after writeback
+                        * finished, which hurts following write performance.
+                        * If users really care about write performance they
+                        * may set BCH_ENABLE_AUTO_GC via sysfs, then when
+                        * BCH_DO_AUTO_GC is set, garbage collection thread
+                        * will be wake up here. After moving gc, the shrunk
+                        * btree and discarded free buckets SSD space may be
+                        * helpful for following write requests.
+                        */
+                       if (c->gc_after_writeback ==
+                           (BCH_ENABLE_AUTO_GC|BCH_DO_AUTO_GC)) {
+                               c->gc_after_writeback &= ~BCH_DO_AUTO_GC;
+                               force_wake_up_gc(c);
+                       }
                }
 
                up_write(&dc->writeback_lock);
index d2b9fdbc8994905e48dba5f2126cbea983ae602a..ce63be98a39d7586cf2e003041e7dc9bc4e841a9 100644 (file)
@@ -11,6 +11,8 @@
 #define WRITEBACK_RATE_UPDATE_SECS_MAX         60
 #define WRITEBACK_RATE_UPDATE_SECS_DEFAULT     5
 
+#define BCH_AUTO_GC_DIRTY_THRESHOLD    50
+
 /*
  * 14 (16384ths) is chosen here as something that each backing device
  * should be a reasonable fraction of the share, and not to blow up