1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_foreground.h"
5 #include "btree_iter.h"
8 #include "disk_groups.h"
12 #include "rebalance.h"
15 #include <linux/freezer.h>
16 #include <linux/kthread.h>
17 #include <linux/sched/cputime.h>
18 #include <trace/events/bcachefs.h>
21 * Check if an extent should be moved:
22 * returns -1 if it should not be moved, or
23 * device of pointer that should be moved, if known, or INT_MAX if unknown
25 static int __bch2_rebalance_pred(struct bch_fs *c,
27 struct bch_io_opts *io_opts)
29 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
30 const union bch_extent_entry *entry;
31 struct extent_ptr_decoded p;
33 if (io_opts->background_compression &&
34 !bch2_bkey_is_incompressible(k))
35 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
37 p.crc.compression_type !=
38 bch2_compression_opt_to_type[io_opts->background_compression])
41 if (io_opts->background_target)
42 bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
44 !bch2_dev_in_target(c, p.ptr.dev, io_opts->background_target))
50 void bch2_rebalance_add_key(struct bch_fs *c,
52 struct bch_io_opts *io_opts)
57 dev = __bch2_rebalance_pred(c, k, io_opts);
61 counter = dev < INT_MAX
62 ? &bch_dev_bkey_exists(c, dev)->rebalance_work
63 : &c->rebalance.work_unknown_dev;
65 if (atomic64_add_return(k.k->size, counter) == k.k->size)
69 static enum data_cmd rebalance_pred(struct bch_fs *c, void *arg,
71 struct bch_io_opts *io_opts,
72 struct data_opts *data_opts)
74 if (__bch2_rebalance_pred(c, k, io_opts) >= 0) {
75 data_opts->target = io_opts->background_target;
76 data_opts->nr_replicas = 1;
77 data_opts->btree_insert_flags = 0;
78 return DATA_ADD_REPLICAS;
84 void bch2_rebalance_add_work(struct bch_fs *c, u64 sectors)
86 if (atomic64_add_return(sectors, &c->rebalance.work_unknown_dev) ==
91 struct rebalance_work {
92 int dev_most_full_idx;
93 unsigned dev_most_full_percent;
94 u64 dev_most_full_work;
95 u64 dev_most_full_capacity;
99 static void rebalance_work_accumulate(struct rebalance_work *w,
100 u64 dev_work, u64 unknown_dev, u64 capacity, int idx)
102 unsigned percent_full;
103 u64 work = dev_work + unknown_dev;
105 if (work < dev_work || work < unknown_dev)
107 work = min(work, capacity);
109 percent_full = div64_u64(work * 100, capacity);
111 if (percent_full >= w->dev_most_full_percent) {
112 w->dev_most_full_idx = idx;
113 w->dev_most_full_percent = percent_full;
114 w->dev_most_full_work = work;
115 w->dev_most_full_capacity = capacity;
118 if (w->total_work + dev_work >= w->total_work &&
119 w->total_work + dev_work >= dev_work)
120 w->total_work += dev_work;
123 static struct rebalance_work rebalance_work(struct bch_fs *c)
126 struct rebalance_work ret = { .dev_most_full_idx = -1 };
127 u64 unknown_dev = atomic64_read(&c->rebalance.work_unknown_dev);
130 for_each_online_member(ca, c, i)
131 rebalance_work_accumulate(&ret,
132 atomic64_read(&ca->rebalance_work),
134 bucket_to_sector(ca, ca->mi.nbuckets -
135 ca->mi.first_bucket),
138 rebalance_work_accumulate(&ret,
139 unknown_dev, 0, c->capacity, -1);
144 static void rebalance_work_reset(struct bch_fs *c)
149 for_each_online_member(ca, c, i)
150 atomic64_set(&ca->rebalance_work, 0);
152 atomic64_set(&c->rebalance.work_unknown_dev, 0);
155 static unsigned long curr_cputime(void)
159 task_cputime_adjusted(current, &utime, &stime);
160 return nsecs_to_jiffies(utime + stime);
163 static int bch2_rebalance_thread(void *arg)
165 struct bch_fs *c = arg;
166 struct bch_fs_rebalance *r = &c->rebalance;
167 struct io_clock *clock = &c->io_clock[WRITE];
168 struct rebalance_work w, p;
169 struct bch_move_stats move_stats;
170 unsigned long start, prev_start;
171 unsigned long prev_run_time, prev_run_cputime;
172 unsigned long cputime, prev_cputime;
178 io_start = atomic64_read(&clock->now);
179 p = rebalance_work(c);
180 prev_start = jiffies;
181 prev_cputime = curr_cputime();
183 bch_move_stats_init(&move_stats, "rebalance");
184 while (!kthread_wait_freezable(r->enabled)) {
188 cputime = curr_cputime();
190 prev_run_time = start - prev_start;
191 prev_run_cputime = cputime - prev_cputime;
193 w = rebalance_work(c);
194 BUG_ON(!w.dev_most_full_capacity);
197 r->state = REBALANCE_WAITING;
198 kthread_wait_freezable(rebalance_work(c).total_work);
203 * If there isn't much work to do, throttle cpu usage:
205 throttle = prev_run_cputime * 100 /
206 max(1U, w.dev_most_full_percent) -
209 if (w.dev_most_full_percent < 20 && throttle > 0) {
210 r->throttled_until_iotime = io_start +
211 div_u64(w.dev_most_full_capacity *
212 (20 - w.dev_most_full_percent),
215 if (atomic64_read(&clock->now) + clock->max_slop <
216 r->throttled_until_iotime) {
217 r->throttled_until_cputime = start + throttle;
218 r->state = REBALANCE_THROTTLED;
220 bch2_kthread_io_clock_wait(clock,
221 r->throttled_until_iotime,
227 /* minimum 1 mb/sec: */
231 max(p.dev_most_full_percent, 1U) /
232 max(w.dev_most_full_percent, 1U));
234 io_start = atomic64_read(&clock->now);
237 prev_cputime = cputime;
239 r->state = REBALANCE_RUNNING;
240 memset(&move_stats, 0, sizeof(move_stats));
241 rebalance_work_reset(c);
245 BTREE_ID_NR, POS_MAX,
246 /* ratelimiting disabled for now */
247 NULL, /* &r->pd.rate, */
248 writepoint_ptr(&c->rebalance_write_point),
249 rebalance_pred, NULL,
256 void bch2_rebalance_work_to_text(struct printbuf *out, struct bch_fs *c)
258 struct bch_fs_rebalance *r = &c->rebalance;
259 struct rebalance_work w = rebalance_work(c);
261 out->tabstops[0] = 20;
263 pr_buf(out, "fullest_dev (%i):", w.dev_most_full_idx);
266 bch2_hprint(out, w.dev_most_full_work << 9);
268 bch2_hprint(out, w.dev_most_full_capacity << 9);
271 pr_buf(out, "total work:");
274 bch2_hprint(out, w.total_work << 9);
276 bch2_hprint(out, c->capacity << 9);
279 pr_buf(out, "rate:");
281 pr_buf(out, "%u", r->pd.rate.rate);
285 case REBALANCE_WAITING:
286 pr_buf(out, "waiting");
288 case REBALANCE_THROTTLED:
289 pr_buf(out, "throttled for %lu sec or ",
290 (r->throttled_until_cputime - jiffies) / HZ);
292 (r->throttled_until_iotime -
293 atomic64_read(&c->io_clock[WRITE].now)) << 9);
296 case REBALANCE_RUNNING:
297 pr_buf(out, "running");
303 void bch2_rebalance_stop(struct bch_fs *c)
305 struct task_struct *p;
307 c->rebalance.pd.rate.rate = UINT_MAX;
308 bch2_ratelimit_reset(&c->rebalance.pd.rate);
310 p = rcu_dereference_protected(c->rebalance.thread, 1);
311 c->rebalance.thread = NULL;
314 /* for sychronizing with rebalance_wakeup() */
322 int bch2_rebalance_start(struct bch_fs *c)
324 struct task_struct *p;
326 if (c->rebalance.thread)
329 if (c->opts.nochanges)
332 p = kthread_create(bch2_rebalance_thread, c, "bch-rebalance/%s", c->name);
334 bch_err(c, "error creating rebalance thread: %li", PTR_ERR(p));
339 rcu_assign_pointer(c->rebalance.thread, p);
344 void bch2_fs_rebalance_init(struct bch_fs *c)
346 bch2_pd_controller_init(&c->rebalance.pd);
348 atomic64_set(&c->rebalance.work_unknown_dev, S64_MAX);