bcachefs: Add allocator thread state to sysfs
authorKent Overstreet <kent.overstreet@gmail.com>
Sun, 18 Apr 2021 21:54:56 +0000 (17:54 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:08:54 +0000 (17:08 -0400)
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/alloc_background.c
fs/bcachefs/alloc_background.h
fs/bcachefs/alloc_types.h
fs/bcachefs/bcachefs.h
fs/bcachefs/movinggc.c
fs/bcachefs/sysfs.c

index 055b6b55966649fd84b4fc0b3760b97f5e28002d..54e58b377e51702c11572d42001f8b8a77f06850 100644 (file)
 #include <linux/sched/task.h>
 #include <linux/sort.h>
 
+const char * const bch2_allocator_states[] = {
+#define x(n)   #n,
+       ALLOC_THREAD_STATES()
+#undef x
+       NULL
+};
+
 static const unsigned BCH_ALLOC_V1_FIELD_BYTES[] = {
 #define x(name, bits) [BCH_ALLOC_FIELD_V1_##name] = bits / 8,
        BCH_ALLOC_FIELDS_V1()
@@ -469,7 +476,7 @@ static int wait_buckets_available(struct bch_fs *c, struct bch_dev *ca)
        s64 available;
        int ret = 0;
 
-       ca->allocator_state = ALLOCATOR_BLOCKED;
+       ca->allocator_state = ALLOCATOR_blocked;
        closure_wake_up(&c->freelist_wait);
 
        while (1) {
@@ -497,7 +504,7 @@ static int wait_buckets_available(struct bch_fs *c, struct bch_dev *ca)
        }
 
        __set_current_state(TASK_RUNNING);
-       ca->allocator_state = ALLOCATOR_RUNNING;
+       ca->allocator_state = ALLOCATOR_running;
        closure_wake_up(&c->freelist_wait);
 
        return ret;
@@ -978,15 +985,15 @@ static int push_invalidated_bucket(struct bch_fs *c, struct bch_dev *ca, size_t
                                fifo_pop(&ca->free_inc, bucket);
 
                                closure_wake_up(&c->freelist_wait);
-                               ca->allocator_state = ALLOCATOR_RUNNING;
+                               ca->allocator_state = ALLOCATOR_running;
 
                                spin_unlock(&c->freelist_lock);
                                goto out;
                        }
                }
 
-               if (ca->allocator_state != ALLOCATOR_BLOCKED_FULL) {
-                       ca->allocator_state = ALLOCATOR_BLOCKED_FULL;
+               if (ca->allocator_state != ALLOCATOR_blocked_full) {
+                       ca->allocator_state = ALLOCATOR_blocked_full;
                        closure_wake_up(&c->freelist_wait);
                }
 
@@ -1053,12 +1060,12 @@ static int bch2_allocator_thread(void *arg)
 
        while (1) {
                if (!allocator_thread_running(ca)) {
-                       ca->allocator_state = ALLOCATOR_STOPPED;
+                       ca->allocator_state = ALLOCATOR_stopped;
                        if (kthread_wait_freezable(allocator_thread_running(ca)))
                                break;
                }
 
-               ca->allocator_state = ALLOCATOR_RUNNING;
+               ca->allocator_state = ALLOCATOR_running;
 
                cond_resched();
                if (kthread_should_stop())
@@ -1139,7 +1146,7 @@ static int bch2_allocator_thread(void *arg)
 
 stop:
        pr_debug("alloc thread stopping (ret %i)", ret);
-       ca->allocator_state = ALLOCATOR_STOPPED;
+       ca->allocator_state = ALLOCATOR_stopped;
        closure_wake_up(&c->freelist_wait);
        return 0;
 }
@@ -1319,7 +1326,7 @@ void bch2_dev_allocator_quiesce(struct bch_fs *c, struct bch_dev *ca)
 {
        if (ca->alloc_thread)
                closure_wait_event(&c->freelist_wait,
-                                  ca->allocator_state != ALLOCATOR_RUNNING);
+                                  ca->allocator_state != ALLOCATOR_running);
 }
 
 /* stop allocator thread: */
index 6fededcd9f8686276beadd696b713c734eeffb5b..73e1c27c96e31e7bc922617ece21031e679cc0d7 100644 (file)
@@ -6,6 +6,8 @@
 #include "alloc_types.h"
 #include "debug.h"
 
+extern const char * const bch2_allocator_states[];
+
 struct bkey_alloc_unpacked {
        u64             bucket;
        u8              dev;
@@ -100,7 +102,7 @@ static inline void bch2_wake_allocator(struct bch_dev *ca)
        p = rcu_dereference(ca->alloc_thread);
        if (p) {
                wake_up_process(p);
-               ca->allocator_state = ALLOCATOR_RUNNING;
+               ca->allocator_state = ALLOCATOR_running;
        }
        rcu_read_unlock();
 }
index be164d6108bbcdbb3f2ca48bf8e6dc0618d4b6a8..4a1cd8b73d16b1e500a84ae9290d9bf144c502cf 100644 (file)
 
 struct ec_bucket_buf;
 
+#define ALLOC_THREAD_STATES()          \
+       x(stopped)                      \
+       x(running)                      \
+       x(blocked)                      \
+       x(blocked_full)
+
+enum allocator_states {
+#define x(n)   ALLOCATOR_##n,
+       ALLOC_THREAD_STATES()
+#undef x
+};
+
 enum alloc_reserve {
        RESERVE_BTREE_MOVINGGC  = -2,
        RESERVE_BTREE           = -1,
index 12441f943a6d0ba0e730ad22ae9bd4f835360ad9..9502f393a59ff6ea42e0990910241c1ee91b10b0 100644 (file)
@@ -457,16 +457,7 @@ struct bch_dev {
        size_t                  inc_gen_needs_gc;
        size_t                  inc_gen_really_needs_gc;
 
-       /*
-        * XXX: this should be an enum for allocator state, so as to include
-        * error state
-        */
-       enum {
-               ALLOCATOR_STOPPED,
-               ALLOCATOR_RUNNING,
-               ALLOCATOR_BLOCKED,
-               ALLOCATOR_BLOCKED_FULL,
-       }                       allocator_state;
+       enum allocator_states   allocator_state;
 
        alloc_heap              alloc_heap;
 
index e25fa0a2a4b5751c4a05807fde781c9c85737a67..3d57a72e63e430eb9a68e85ed63039dab4f4e5db 100644 (file)
@@ -108,7 +108,7 @@ static bool have_copygc_reserve(struct bch_dev *ca)
 
        spin_lock(&ca->fs->freelist_lock);
        ret = fifo_full(&ca->free[RESERVE_MOVINGGC]) ||
-               ca->allocator_state != ALLOCATOR_RUNNING;
+               ca->allocator_state != ALLOCATOR_running;
        spin_unlock(&ca->fs->freelist_lock);
 
        return ret;
index ff93e5ba9f419c083fcc8a582d40e32fc3acf35a..c4d79096c53ad47126e09bec820dab98ad305fab 100644 (file)
@@ -805,7 +805,8 @@ static void dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
               "open_buckets_wait\t%s\n"
               "open_buckets_btree\t%u\n"
               "open_buckets_user\t%u\n"
-              "btree reserve cache\t%u\n",
+              "btree reserve cache\t%u\n"
+              "thread state:\t\t%s\n",
               stats.buckets_ec,
               __dev_buckets_available(ca, stats),
               fifo_used(&ca->free_inc),                ca->free_inc.size,
@@ -818,7 +819,8 @@ static void dev_alloc_debug_to_text(struct printbuf *out, struct bch_dev *ca)
               c->open_buckets_wait.list.first          ? "waiting" : "empty",
               nr[BCH_DATA_btree],
               nr[BCH_DATA_user],
-              c->btree_reserve_cache_nr);
+              c->btree_reserve_cache_nr,
+              bch2_allocator_states[ca->allocator_state]);
 }
 
 static const char * const bch2_rw[] = {