1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_foreground.h"
5 #include "btree_iter.h"
9 #include "disk_groups.h"
14 #include "rebalance.h"
18 #include <linux/freezer.h>
19 #include <linux/kthread.h>
20 #include <linux/sched/cputime.h>
23 * Check if an extent should be moved:
24 * returns -1 if it should not be moved, or
25 * device of pointer that should be moved, if known, or INT_MAX if unknown
27 static bool rebalance_pred(struct bch_fs *c, void *arg,
29 struct bch_io_opts *io_opts,
30 struct data_update_opts *data_opts)
32 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
35 data_opts->rewrite_ptrs = 0;
36 data_opts->target = io_opts->background_target;
37 data_opts->extra_replicas = 0;
38 data_opts->btree_insert_flags = 0;
40 if (io_opts->background_compression &&
41 !bch2_bkey_is_incompressible(k)) {
42 const union bch_extent_entry *entry;
43 struct extent_ptr_decoded p;
46 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
48 p.crc.compression_type !=
49 bch2_compression_opt_to_type(io_opts->background_compression))
50 data_opts->rewrite_ptrs |= 1U << i;
55 if (io_opts->background_target) {
56 const struct bch_extent_ptr *ptr;
59 bkey_for_each_ptr(ptrs, ptr) {
61 !bch2_dev_in_target(c, ptr->dev, io_opts->background_target) &&
62 bch2_target_accepts_data(c, BCH_DATA_user, io_opts->background_target))
63 data_opts->rewrite_ptrs |= 1U << i;
68 return data_opts->rewrite_ptrs != 0;
71 void bch2_rebalance_add_key(struct bch_fs *c,
73 struct bch_io_opts *io_opts)
75 struct data_update_opts update_opts = { 0 };
76 struct bkey_ptrs_c ptrs;
77 const struct bch_extent_ptr *ptr;
80 if (!rebalance_pred(c, NULL, k, io_opts, &update_opts))
84 ptrs = bch2_bkey_ptrs_c(k);
85 bkey_for_each_ptr(ptrs, ptr) {
86 if ((1U << i) && update_opts.rewrite_ptrs)
87 if (atomic64_add_return(k.k->size,
88 &bch_dev_bkey_exists(c, ptr->dev)->rebalance_work) ==
95 void bch2_rebalance_add_work(struct bch_fs *c, u64 sectors)
97 if (atomic64_add_return(sectors, &c->rebalance.work_unknown_dev) ==
102 struct rebalance_work {
103 int dev_most_full_idx;
104 unsigned dev_most_full_percent;
105 u64 dev_most_full_work;
106 u64 dev_most_full_capacity;
110 static void rebalance_work_accumulate(struct rebalance_work *w,
111 u64 dev_work, u64 unknown_dev, u64 capacity, int idx)
113 unsigned percent_full;
114 u64 work = dev_work + unknown_dev;
116 if (work < dev_work || work < unknown_dev)
118 work = min(work, capacity);
120 percent_full = div64_u64(work * 100, capacity);
122 if (percent_full >= w->dev_most_full_percent) {
123 w->dev_most_full_idx = idx;
124 w->dev_most_full_percent = percent_full;
125 w->dev_most_full_work = work;
126 w->dev_most_full_capacity = capacity;
129 if (w->total_work + dev_work >= w->total_work &&
130 w->total_work + dev_work >= dev_work)
131 w->total_work += dev_work;
134 static struct rebalance_work rebalance_work(struct bch_fs *c)
137 struct rebalance_work ret = { .dev_most_full_idx = -1 };
138 u64 unknown_dev = atomic64_read(&c->rebalance.work_unknown_dev);
141 for_each_online_member(ca, c, i)
142 rebalance_work_accumulate(&ret,
143 atomic64_read(&ca->rebalance_work),
145 bucket_to_sector(ca, ca->mi.nbuckets -
146 ca->mi.first_bucket),
149 rebalance_work_accumulate(&ret,
150 unknown_dev, 0, c->capacity, -1);
155 static void rebalance_work_reset(struct bch_fs *c)
160 for_each_online_member(ca, c, i)
161 atomic64_set(&ca->rebalance_work, 0);
163 atomic64_set(&c->rebalance.work_unknown_dev, 0);
166 static unsigned long curr_cputime(void)
170 task_cputime_adjusted(current, &utime, &stime);
171 return nsecs_to_jiffies(utime + stime);
174 static int bch2_rebalance_thread(void *arg)
176 struct bch_fs *c = arg;
177 struct bch_fs_rebalance *r = &c->rebalance;
178 struct io_clock *clock = &c->io_clock[WRITE];
179 struct rebalance_work w, p;
180 struct bch_move_stats move_stats;
181 unsigned long start, prev_start;
182 unsigned long prev_run_time, prev_run_cputime;
183 unsigned long cputime, prev_cputime;
189 io_start = atomic64_read(&clock->now);
190 p = rebalance_work(c);
191 prev_start = jiffies;
192 prev_cputime = curr_cputime();
194 bch2_move_stats_init(&move_stats, "rebalance");
195 while (!kthread_wait_freezable(r->enabled)) {
199 cputime = curr_cputime();
201 prev_run_time = start - prev_start;
202 prev_run_cputime = cputime - prev_cputime;
204 w = rebalance_work(c);
205 BUG_ON(!w.dev_most_full_capacity);
208 r->state = REBALANCE_WAITING;
209 kthread_wait_freezable(rebalance_work(c).total_work);
214 * If there isn't much work to do, throttle cpu usage:
216 throttle = prev_run_cputime * 100 /
217 max(1U, w.dev_most_full_percent) -
220 if (w.dev_most_full_percent < 20 && throttle > 0) {
221 r->throttled_until_iotime = io_start +
222 div_u64(w.dev_most_full_capacity *
223 (20 - w.dev_most_full_percent),
226 if (atomic64_read(&clock->now) + clock->max_slop <
227 r->throttled_until_iotime) {
228 r->throttled_until_cputime = start + throttle;
229 r->state = REBALANCE_THROTTLED;
231 bch2_kthread_io_clock_wait(clock,
232 r->throttled_until_iotime,
238 /* minimum 1 mb/sec: */
242 max(p.dev_most_full_percent, 1U) /
243 max(w.dev_most_full_percent, 1U));
245 io_start = atomic64_read(&clock->now);
248 prev_cputime = cputime;
250 r->state = REBALANCE_RUNNING;
251 memset(&move_stats, 0, sizeof(move_stats));
252 rebalance_work_reset(c);
256 BTREE_ID_NR, POS_MAX,
257 /* ratelimiting disabled for now */
258 NULL, /* &r->pd.rate, */
260 writepoint_ptr(&c->rebalance_write_point),
262 rebalance_pred, NULL);
268 void bch2_rebalance_work_to_text(struct printbuf *out, struct bch_fs *c)
270 struct bch_fs_rebalance *r = &c->rebalance;
271 struct rebalance_work w = rebalance_work(c);
273 if (!out->nr_tabstops)
274 printbuf_tabstop_push(out, 20);
276 prt_printf(out, "fullest_dev (%i):", w.dev_most_full_idx);
279 prt_human_readable_u64(out, w.dev_most_full_work << 9);
280 prt_printf(out, "/");
281 prt_human_readable_u64(out, w.dev_most_full_capacity << 9);
284 prt_printf(out, "total work:");
287 prt_human_readable_u64(out, w.total_work << 9);
288 prt_printf(out, "/");
289 prt_human_readable_u64(out, c->capacity << 9);
292 prt_printf(out, "rate:");
294 prt_printf(out, "%u", r->pd.rate.rate);
298 case REBALANCE_WAITING:
299 prt_printf(out, "waiting");
301 case REBALANCE_THROTTLED:
302 prt_printf(out, "throttled for %lu sec or ",
303 (r->throttled_until_cputime - jiffies) / HZ);
304 prt_human_readable_u64(out,
305 (r->throttled_until_iotime -
306 atomic64_read(&c->io_clock[WRITE].now)) << 9);
307 prt_printf(out, " io");
309 case REBALANCE_RUNNING:
310 prt_printf(out, "running");
316 void bch2_rebalance_stop(struct bch_fs *c)
318 struct task_struct *p;
320 c->rebalance.pd.rate.rate = UINT_MAX;
321 bch2_ratelimit_reset(&c->rebalance.pd.rate);
323 p = rcu_dereference_protected(c->rebalance.thread, 1);
324 c->rebalance.thread = NULL;
327 /* for sychronizing with rebalance_wakeup() */
335 int bch2_rebalance_start(struct bch_fs *c)
337 struct task_struct *p;
340 if (c->rebalance.thread)
343 if (c->opts.nochanges)
346 p = kthread_create(bch2_rebalance_thread, c, "bch-rebalance/%s", c->name);
347 ret = PTR_ERR_OR_ZERO(p);
349 bch_err(c, "error creating rebalance thread: %s", bch2_err_str(ret));
354 rcu_assign_pointer(c->rebalance.thread, p);
359 void bch2_fs_rebalance_init(struct bch_fs *c)
361 bch2_pd_controller_init(&c->rebalance.pd);
363 atomic64_set(&c->rebalance.work_unknown_dev, S64_MAX);