1 // SPDX-License-Identifier: GPL-2.0
4 #include "alloc_foreground.h"
5 #include "btree_iter.h"
8 #include "disk_groups.h"
13 #include "rebalance.h"
17 #include <linux/freezer.h>
18 #include <linux/kthread.h>
19 #include <linux/sched/cputime.h>
22 * Check if an extent should be moved:
23 * returns -1 if it should not be moved, or
24 * device of pointer that should be moved, if known, or INT_MAX if unknown
26 static bool rebalance_pred(struct bch_fs *c, void *arg,
28 struct bch_io_opts *io_opts,
29 struct data_update_opts *data_opts)
31 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
34 data_opts->rewrite_ptrs = 0;
35 data_opts->target = io_opts->background_target;
36 data_opts->extra_replicas = 0;
37 data_opts->btree_insert_flags = 0;
39 if (io_opts->background_compression &&
40 !bch2_bkey_is_incompressible(k)) {
41 const union bch_extent_entry *entry;
42 struct extent_ptr_decoded p;
45 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
47 p.crc.compression_type !=
48 bch2_compression_opt_to_type[io_opts->background_compression])
49 data_opts->rewrite_ptrs |= 1U << i;
54 if (io_opts->background_target) {
55 const struct bch_extent_ptr *ptr;
58 bkey_for_each_ptr(ptrs, ptr) {
60 !bch2_dev_in_target(c, ptr->dev, io_opts->background_target) &&
61 bch2_target_accepts_data(c, BCH_DATA_user, io_opts->background_target))
62 data_opts->rewrite_ptrs |= 1U << i;
67 return data_opts->rewrite_ptrs != 0;
70 void bch2_rebalance_add_key(struct bch_fs *c,
72 struct bch_io_opts *io_opts)
74 struct data_update_opts update_opts = { 0 };
75 struct bkey_ptrs_c ptrs;
76 const struct bch_extent_ptr *ptr;
79 if (!rebalance_pred(c, NULL, k, io_opts, &update_opts))
83 ptrs = bch2_bkey_ptrs_c(k);
84 bkey_for_each_ptr(ptrs, ptr) {
85 if ((1U << i) && update_opts.rewrite_ptrs)
86 if (atomic64_add_return(k.k->size,
87 &bch_dev_bkey_exists(c, ptr->dev)->rebalance_work) ==
94 void bch2_rebalance_add_work(struct bch_fs *c, u64 sectors)
96 if (atomic64_add_return(sectors, &c->rebalance.work_unknown_dev) ==
101 struct rebalance_work {
102 int dev_most_full_idx;
103 unsigned dev_most_full_percent;
104 u64 dev_most_full_work;
105 u64 dev_most_full_capacity;
109 static void rebalance_work_accumulate(struct rebalance_work *w,
110 u64 dev_work, u64 unknown_dev, u64 capacity, int idx)
112 unsigned percent_full;
113 u64 work = dev_work + unknown_dev;
115 if (work < dev_work || work < unknown_dev)
117 work = min(work, capacity);
119 percent_full = div64_u64(work * 100, capacity);
121 if (percent_full >= w->dev_most_full_percent) {
122 w->dev_most_full_idx = idx;
123 w->dev_most_full_percent = percent_full;
124 w->dev_most_full_work = work;
125 w->dev_most_full_capacity = capacity;
128 if (w->total_work + dev_work >= w->total_work &&
129 w->total_work + dev_work >= dev_work)
130 w->total_work += dev_work;
133 static struct rebalance_work rebalance_work(struct bch_fs *c)
136 struct rebalance_work ret = { .dev_most_full_idx = -1 };
137 u64 unknown_dev = atomic64_read(&c->rebalance.work_unknown_dev);
140 for_each_online_member(ca, c, i)
141 rebalance_work_accumulate(&ret,
142 atomic64_read(&ca->rebalance_work),
144 bucket_to_sector(ca, ca->mi.nbuckets -
145 ca->mi.first_bucket),
148 rebalance_work_accumulate(&ret,
149 unknown_dev, 0, c->capacity, -1);
154 static void rebalance_work_reset(struct bch_fs *c)
159 for_each_online_member(ca, c, i)
160 atomic64_set(&ca->rebalance_work, 0);
162 atomic64_set(&c->rebalance.work_unknown_dev, 0);
165 static unsigned long curr_cputime(void)
169 task_cputime_adjusted(current, &utime, &stime);
170 return nsecs_to_jiffies(utime + stime);
173 static int bch2_rebalance_thread(void *arg)
175 struct bch_fs *c = arg;
176 struct bch_fs_rebalance *r = &c->rebalance;
177 struct io_clock *clock = &c->io_clock[WRITE];
178 struct rebalance_work w, p;
179 struct bch_move_stats move_stats;
180 unsigned long start, prev_start;
181 unsigned long prev_run_time, prev_run_cputime;
182 unsigned long cputime, prev_cputime;
188 io_start = atomic64_read(&clock->now);
189 p = rebalance_work(c);
190 prev_start = jiffies;
191 prev_cputime = curr_cputime();
193 bch2_move_stats_init(&move_stats, "rebalance");
194 while (!kthread_wait_freezable(r->enabled)) {
198 cputime = curr_cputime();
200 prev_run_time = start - prev_start;
201 prev_run_cputime = cputime - prev_cputime;
203 w = rebalance_work(c);
204 BUG_ON(!w.dev_most_full_capacity);
207 r->state = REBALANCE_WAITING;
208 kthread_wait_freezable(rebalance_work(c).total_work);
213 * If there isn't much work to do, throttle cpu usage:
215 throttle = prev_run_cputime * 100 /
216 max(1U, w.dev_most_full_percent) -
219 if (w.dev_most_full_percent < 20 && throttle > 0) {
220 r->throttled_until_iotime = io_start +
221 div_u64(w.dev_most_full_capacity *
222 (20 - w.dev_most_full_percent),
225 if (atomic64_read(&clock->now) + clock->max_slop <
226 r->throttled_until_iotime) {
227 r->throttled_until_cputime = start + throttle;
228 r->state = REBALANCE_THROTTLED;
230 bch2_kthread_io_clock_wait(clock,
231 r->throttled_until_iotime,
237 /* minimum 1 mb/sec: */
241 max(p.dev_most_full_percent, 1U) /
242 max(w.dev_most_full_percent, 1U));
244 io_start = atomic64_read(&clock->now);
247 prev_cputime = cputime;
249 r->state = REBALANCE_RUNNING;
250 memset(&move_stats, 0, sizeof(move_stats));
251 rebalance_work_reset(c);
255 BTREE_ID_NR, POS_MAX,
256 /* ratelimiting disabled for now */
257 NULL, /* &r->pd.rate, */
259 writepoint_ptr(&c->rebalance_write_point),
261 rebalance_pred, NULL);
267 void bch2_rebalance_work_to_text(struct printbuf *out, struct bch_fs *c)
269 struct bch_fs_rebalance *r = &c->rebalance;
270 struct rebalance_work w = rebalance_work(c);
272 if (!out->nr_tabstops)
273 printbuf_tabstop_push(out, 20);
275 prt_printf(out, "fullest_dev (%i):", w.dev_most_full_idx);
278 prt_human_readable_u64(out, w.dev_most_full_work << 9);
279 prt_printf(out, "/");
280 prt_human_readable_u64(out, w.dev_most_full_capacity << 9);
283 prt_printf(out, "total work:");
286 prt_human_readable_u64(out, w.total_work << 9);
287 prt_printf(out, "/");
288 prt_human_readable_u64(out, c->capacity << 9);
291 prt_printf(out, "rate:");
293 prt_printf(out, "%u", r->pd.rate.rate);
297 case REBALANCE_WAITING:
298 prt_printf(out, "waiting");
300 case REBALANCE_THROTTLED:
301 prt_printf(out, "throttled for %lu sec or ",
302 (r->throttled_until_cputime - jiffies) / HZ);
303 prt_human_readable_u64(out,
304 (r->throttled_until_iotime -
305 atomic64_read(&c->io_clock[WRITE].now)) << 9);
306 prt_printf(out, " io");
308 case REBALANCE_RUNNING:
309 prt_printf(out, "running");
315 void bch2_rebalance_stop(struct bch_fs *c)
317 struct task_struct *p;
319 c->rebalance.pd.rate.rate = UINT_MAX;
320 bch2_ratelimit_reset(&c->rebalance.pd.rate);
322 p = rcu_dereference_protected(c->rebalance.thread, 1);
323 c->rebalance.thread = NULL;
326 /* for sychronizing with rebalance_wakeup() */
334 int bch2_rebalance_start(struct bch_fs *c)
336 struct task_struct *p;
339 if (c->rebalance.thread)
342 if (c->opts.nochanges)
345 p = kthread_create(bch2_rebalance_thread, c, "bch-rebalance/%s", c->name);
346 ret = PTR_ERR_OR_ZERO(p);
348 bch_err(c, "error creating rebalance thread: %s", bch2_err_str(ret));
353 rcu_assign_pointer(c->rebalance.thread, p);
358 void bch2_fs_rebalance_init(struct bch_fs *c)
360 bch2_pd_controller_init(&c->rebalance.pd);
362 atomic64_set(&c->rebalance.work_unknown_dev, S64_MAX);