]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/journal_reclaim.c
Upload to experimental
[bcachefs-tools-debian] / libbcachefs / journal_reclaim.c
index 8de83e10375187803730214e3bee4c3cd425256c..8fa05bedb7dff8f36084fafc37106fe9915579a0 100644 (file)
@@ -3,13 +3,14 @@
 #include "bcachefs.h"
 #include "btree_key_cache.h"
 #include "btree_update.h"
+#include "buckets.h"
 #include "errcode.h"
 #include "error.h"
 #include "journal.h"
 #include "journal_io.h"
 #include "journal_reclaim.h"
 #include "replicas.h"
-#include "super.h"
+#include "sb-members.h"
 #include "trace.h"
 
 #include <linux/kthread.h>
@@ -49,16 +50,25 @@ unsigned bch2_journal_dev_buckets_available(struct journal *j,
        return available;
 }
 
-static void journal_set_remaining(struct journal *j, unsigned u64s_remaining)
+static inline void journal_set_watermark(struct journal *j)
 {
-       union journal_preres_state old, new;
-       u64 v = atomic64_read(&j->prereserved.counter);
-
-       do {
-               old.v = new.v = v;
-               new.remaining = u64s_remaining;
-       } while ((v = atomic64_cmpxchg(&j->prereserved.counter,
-                                      old.v, new.v)) != old.v);
+       struct bch_fs *c = container_of(j, struct bch_fs, journal);
+       bool low_on_space = j->space[journal_space_clean].total * 4 <=
+               j->space[journal_space_total].total;
+       bool low_on_pin = fifo_free(&j->pin) < j->pin.size / 4;
+       unsigned watermark = low_on_space || low_on_pin
+               ? BCH_WATERMARK_reclaim
+               : BCH_WATERMARK_stripe;
+
+       if (track_event_change(&c->times[BCH_TIME_blocked_journal_low_on_space],
+                              &j->low_on_space_start, low_on_space) ||
+           track_event_change(&c->times[BCH_TIME_blocked_journal_low_on_pin],
+                              &j->low_on_pin_start, low_on_pin))
+               trace_and_count(c, journal_full, c);
+
+       swap(watermark, j->watermark);
+       if (watermark > j->watermark)
+               journal_wake(j);
 }
 
 static struct journal_space
@@ -161,7 +171,6 @@ void bch2_journal_space_available(struct journal *j)
        struct bch_fs *c = container_of(j, struct bch_fs, journal);
        struct bch_dev *ca;
        unsigned clean, clean_ondisk, total;
-       s64 u64s_remaining = 0;
        unsigned max_entry_size  = min(j->buf[0].buf_size >> 9,
                                       j->buf[1].buf_size >> 9);
        unsigned i, nr_online = 0, nr_devs_want;
@@ -221,16 +230,10 @@ void bch2_journal_space_available(struct journal *j)
        else
                clear_bit(JOURNAL_MAY_SKIP_FLUSH, &j->flags);
 
-       u64s_remaining  = (u64) clean << 6;
-       u64s_remaining -= (u64) total << 3;
-       u64s_remaining = max(0LL, u64s_remaining);
-       u64s_remaining /= 4;
-       u64s_remaining = min_t(u64, u64s_remaining, U32_MAX);
+       journal_set_watermark(j);
 out:
        j->cur_entry_sectors    = !ret ? j->space[journal_space_discarded].next_entry : 0;
        j->cur_entry_error      = ret;
-       journal_set_remaining(j, u64s_remaining);
-       journal_set_watermark(j);
 
        if (!ret)
                journal_wake(j);
@@ -289,9 +292,8 @@ void bch2_journal_do_discards(struct journal *j)
  * entry, holding it open to ensure it gets replayed during recovery:
  */
 
-static void bch2_journal_reclaim_fast(struct journal *j)
+void bch2_journal_reclaim_fast(struct journal *j)
 {
-       struct journal_entry_pin_list temp;
        bool popped = false;
 
        lockdep_assert_held(&j->lock);
@@ -302,7 +304,7 @@ static void bch2_journal_reclaim_fast(struct journal *j)
         */
        while (!fifo_empty(&j->pin) &&
               !atomic_read(&fifo_peek_front(&j->pin).count)) {
-               fifo_pop(&j->pin, temp);
+               j->pin.front++;
                popped = true;
        }
 
@@ -310,19 +312,16 @@ static void bch2_journal_reclaim_fast(struct journal *j)
                bch2_journal_space_available(j);
 }
 
-void __bch2_journal_pin_put(struct journal *j, u64 seq)
+bool __bch2_journal_pin_put(struct journal *j, u64 seq)
 {
        struct journal_entry_pin_list *pin_list = journal_seq_pin(j, seq);
 
-       if (atomic_dec_and_test(&pin_list->count))
-               bch2_journal_reclaim_fast(j);
+       return atomic_dec_and_test(&pin_list->count);
 }
 
 void bch2_journal_pin_put(struct journal *j, u64 seq)
 {
-       struct journal_entry_pin_list *pin_list = journal_seq_pin(j, seq);
-
-       if (atomic_dec_and_test(&pin_list->count)) {
+       if (__bch2_journal_pin_put(j, seq)) {
                spin_lock(&j->lock);
                bch2_journal_reclaim_fast(j);
                spin_unlock(&j->lock);
@@ -345,7 +344,7 @@ static inline bool __journal_pin_drop(struct journal *j,
        list_del_init(&pin->list);
 
        /*
-        * Unpinning a journal entry make make journal_next_bucket() succeed, if
+        * Unpinning a journal entry may make journal_next_bucket() succeed, if
         * writing a new last_seq will now make another bucket available:
         */
        return atomic_dec_and_test(&pin_list->count) &&
@@ -372,15 +371,36 @@ static enum journal_pin_type journal_pin_type(journal_pin_flush_fn fn)
                return JOURNAL_PIN_other;
 }
 
-void bch2_journal_pin_set(struct journal *j, u64 seq,
+static inline void bch2_journal_pin_set_locked(struct journal *j, u64 seq,
                          struct journal_entry_pin *pin,
-                         journal_pin_flush_fn flush_fn)
+                         journal_pin_flush_fn flush_fn,
+                         enum journal_pin_type type)
+{
+       struct journal_entry_pin_list *pin_list = journal_seq_pin(j, seq);
+
+       /*
+        * flush_fn is how we identify journal pins in debugfs, so must always
+        * exist, even if it doesn't do anything:
+        */
+       BUG_ON(!flush_fn);
+
+       atomic_inc(&pin_list->count);
+       pin->seq        = seq;
+       pin->flush      = flush_fn;
+       list_add(&pin->list, &pin_list->list[type]);
+}
+
+void bch2_journal_pin_copy(struct journal *j,
+                          struct journal_entry_pin *dst,
+                          struct journal_entry_pin *src,
+                          journal_pin_flush_fn flush_fn)
 {
-       struct journal_entry_pin_list *pin_list;
        bool reclaim;
 
        spin_lock(&j->lock);
 
+       u64 seq = READ_ONCE(src->seq);
+
        if (seq < journal_last_seq(j)) {
                /*
                 * bch2_journal_pin_copy() raced with bch2_journal_pin_drop() on
@@ -392,18 +412,34 @@ void bch2_journal_pin_set(struct journal *j, u64 seq,
                return;
        }
 
-       pin_list = journal_seq_pin(j, seq);
+       reclaim = __journal_pin_drop(j, dst);
 
-       reclaim = __journal_pin_drop(j, pin);
+       bch2_journal_pin_set_locked(j, seq, dst, flush_fn, journal_pin_type(flush_fn));
 
-       atomic_inc(&pin_list->count);
-       pin->seq        = seq;
-       pin->flush      = flush_fn;
+       if (reclaim)
+               bch2_journal_reclaim_fast(j);
+       spin_unlock(&j->lock);
 
-       if (flush_fn)
-               list_add(&pin->list, &pin_list->list[journal_pin_type(flush_fn)]);
-       else
-               list_add(&pin->list, &pin_list->flushed);
+       /*
+        * If the journal is currently full,  we might want to call flush_fn
+        * immediately:
+        */
+       journal_wake(j);
+}
+
+void bch2_journal_pin_set(struct journal *j, u64 seq,
+                         struct journal_entry_pin *pin,
+                         journal_pin_flush_fn flush_fn)
+{
+       bool reclaim;
+
+       spin_lock(&j->lock);
+
+       BUG_ON(seq < journal_last_seq(j));
+
+       reclaim = __journal_pin_drop(j, pin);
+
+       bch2_journal_pin_set_locked(j, seq, pin, flush_fn, journal_pin_type(flush_fn));
 
        if (reclaim)
                bch2_journal_reclaim_fast(j);
@@ -418,6 +454,8 @@ void bch2_journal_pin_set(struct journal *j, u64 seq,
 
 /**
  * bch2_journal_pin_flush: ensure journal pin callback is no longer running
+ * @j:         journal object
+ * @pin:       pin to flush
  */
 void bch2_journal_pin_flush(struct journal *j, struct journal_entry_pin *pin)
 {
@@ -556,11 +594,6 @@ static u64 journal_seq_to_flush(struct journal *j)
                /* Try to keep the journal at most half full: */
                nr_buckets = ja->nr / 2;
 
-               /* And include pre-reservations: */
-               nr_buckets += DIV_ROUND_UP(j->prereserved.reserved,
-                                          (ca->mi.bucket_size << 6) -
-                                          journal_entry_overhead(j));
-
                nr_buckets = min(nr_buckets, ja->nr);
 
                bucket_to_flush = (ja->cur_idx + nr_buckets) % ja->nr;
@@ -578,7 +611,11 @@ static u64 journal_seq_to_flush(struct journal *j)
 }
 
 /**
- * bch2_journal_reclaim - free up journal buckets
+ * __bch2_journal_reclaim - free up journal buckets
+ * @j:         journal object
+ * @direct:    direct or background reclaim?
+ * @kicked:    requested to run since we last ran?
+ * Returns:    0 on success, or -EIO if the journal has been shutdown
  *
  * Background journal reclaim writes out btree nodes. It should be run
  * early enough so that we never completely run out of journal buckets.
@@ -635,10 +672,7 @@ static int __bch2_journal_reclaim(struct journal *j, bool direct, bool kicked)
                               msecs_to_jiffies(c->opts.journal_reclaim_delay)))
                        min_nr = 1;
 
-               if (j->prereserved.reserved * 4 > j->prereserved.remaining)
-                       min_nr = 1;
-
-               if (fifo_free(&j->pin) <= 32)
+               if (j->watermark != BCH_WATERMARK_stripe)
                        min_nr = 1;
 
                if (atomic_read(&c->btree_cache.dirty) * 2 > c->btree_cache.used)
@@ -649,8 +683,6 @@ static int __bch2_journal_reclaim(struct journal *j, bool direct, bool kicked)
                trace_and_count(c, journal_reclaim_start, c,
                                direct, kicked,
                                min_nr, min_key_cache,
-                               j->prereserved.reserved,
-                               j->prereserved.remaining,
                                atomic_read(&c->btree_cache.dirty),
                                c->btree_cache.used,
                                atomic_long_read(&c->btree_key_cache.nr_dirty),
@@ -757,7 +789,7 @@ int bch2_journal_reclaim_start(struct journal *j)
                           "bch-reclaim/%s", c->name);
        ret = PTR_ERR_OR_ZERO(p);
        if (ret) {
-               bch_err(c, "error creating journal reclaim thread: %s", bch2_err_str(ret));
+               bch_err_msg(c, ret, "creating journal reclaim thread");
                return ret;
        }
 
@@ -802,6 +834,7 @@ static int journal_flush_done(struct journal *j, u64 seq_to_flush,
 
 bool bch2_journal_flush_pins(struct journal *j, u64 seq_to_flush)
 {
+       /* time_stats this */
        bool did_work = false;
 
        if (!test_bit(JOURNAL_STARTED, &j->flags))