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 data_opts->rewrite_ptrs |= 1U << i;
66 return data_opts->rewrite_ptrs != 0;
69 void bch2_rebalance_add_key(struct bch_fs *c,
71 struct bch_io_opts *io_opts)
73 struct data_update_opts update_opts = { 0 };
74 struct bkey_ptrs_c ptrs;
75 const struct bch_extent_ptr *ptr;
78 if (!rebalance_pred(c, NULL, k, io_opts, &update_opts))
82 ptrs = bch2_bkey_ptrs_c(k);
83 bkey_for_each_ptr(ptrs, ptr) {
84 if ((1U << i) && update_opts.rewrite_ptrs)
85 if (atomic64_add_return(k.k->size,
86 &bch_dev_bkey_exists(c, ptr->dev)->rebalance_work) ==
93 void bch2_rebalance_add_work(struct bch_fs *c, u64 sectors)
95 if (atomic64_add_return(sectors, &c->rebalance.work_unknown_dev) ==
100 struct rebalance_work {
101 int dev_most_full_idx;
102 unsigned dev_most_full_percent;
103 u64 dev_most_full_work;
104 u64 dev_most_full_capacity;
108 static void rebalance_work_accumulate(struct rebalance_work *w,
109 u64 dev_work, u64 unknown_dev, u64 capacity, int idx)
111 unsigned percent_full;
112 u64 work = dev_work + unknown_dev;
114 if (work < dev_work || work < unknown_dev)
116 work = min(work, capacity);
118 percent_full = div64_u64(work * 100, capacity);
120 if (percent_full >= w->dev_most_full_percent) {
121 w->dev_most_full_idx = idx;
122 w->dev_most_full_percent = percent_full;
123 w->dev_most_full_work = work;
124 w->dev_most_full_capacity = capacity;
127 if (w->total_work + dev_work >= w->total_work &&
128 w->total_work + dev_work >= dev_work)
129 w->total_work += dev_work;
132 static struct rebalance_work rebalance_work(struct bch_fs *c)
135 struct rebalance_work ret = { .dev_most_full_idx = -1 };
136 u64 unknown_dev = atomic64_read(&c->rebalance.work_unknown_dev);
139 for_each_online_member(ca, c, i)
140 rebalance_work_accumulate(&ret,
141 atomic64_read(&ca->rebalance_work),
143 bucket_to_sector(ca, ca->mi.nbuckets -
144 ca->mi.first_bucket),
147 rebalance_work_accumulate(&ret,
148 unknown_dev, 0, c->capacity, -1);
153 static void rebalance_work_reset(struct bch_fs *c)
158 for_each_online_member(ca, c, i)
159 atomic64_set(&ca->rebalance_work, 0);
161 atomic64_set(&c->rebalance.work_unknown_dev, 0);
164 static unsigned long curr_cputime(void)
168 task_cputime_adjusted(current, &utime, &stime);
169 return nsecs_to_jiffies(utime + stime);
172 static int bch2_rebalance_thread(void *arg)
174 struct bch_fs *c = arg;
175 struct bch_fs_rebalance *r = &c->rebalance;
176 struct io_clock *clock = &c->io_clock[WRITE];
177 struct rebalance_work w, p;
178 struct bch_move_stats move_stats;
179 unsigned long start, prev_start;
180 unsigned long prev_run_time, prev_run_cputime;
181 unsigned long cputime, prev_cputime;
187 io_start = atomic64_read(&clock->now);
188 p = rebalance_work(c);
189 prev_start = jiffies;
190 prev_cputime = curr_cputime();
192 bch2_move_stats_init(&move_stats, "rebalance");
193 while (!kthread_wait_freezable(r->enabled)) {
197 cputime = curr_cputime();
199 prev_run_time = start - prev_start;
200 prev_run_cputime = cputime - prev_cputime;
202 w = rebalance_work(c);
203 BUG_ON(!w.dev_most_full_capacity);
206 r->state = REBALANCE_WAITING;
207 kthread_wait_freezable(rebalance_work(c).total_work);
212 * If there isn't much work to do, throttle cpu usage:
214 throttle = prev_run_cputime * 100 /
215 max(1U, w.dev_most_full_percent) -
218 if (w.dev_most_full_percent < 20 && throttle > 0) {
219 r->throttled_until_iotime = io_start +
220 div_u64(w.dev_most_full_capacity *
221 (20 - w.dev_most_full_percent),
224 if (atomic64_read(&clock->now) + clock->max_slop <
225 r->throttled_until_iotime) {
226 r->throttled_until_cputime = start + throttle;
227 r->state = REBALANCE_THROTTLED;
229 bch2_kthread_io_clock_wait(clock,
230 r->throttled_until_iotime,
236 /* minimum 1 mb/sec: */
240 max(p.dev_most_full_percent, 1U) /
241 max(w.dev_most_full_percent, 1U));
243 io_start = atomic64_read(&clock->now);
246 prev_cputime = cputime;
248 r->state = REBALANCE_RUNNING;
249 memset(&move_stats, 0, sizeof(move_stats));
250 rebalance_work_reset(c);
254 BTREE_ID_NR, POS_MAX,
255 /* ratelimiting disabled for now */
256 NULL, /* &r->pd.rate, */
258 writepoint_ptr(&c->rebalance_write_point),
260 rebalance_pred, NULL);
266 void bch2_rebalance_work_to_text(struct printbuf *out, struct bch_fs *c)
268 struct bch_fs_rebalance *r = &c->rebalance;
269 struct rebalance_work w = rebalance_work(c);
271 if (!out->nr_tabstops)
272 printbuf_tabstop_push(out, 20);
274 prt_printf(out, "fullest_dev (%i):", w.dev_most_full_idx);
277 prt_human_readable_u64(out, w.dev_most_full_work << 9);
278 prt_printf(out, "/");
279 prt_human_readable_u64(out, w.dev_most_full_capacity << 9);
282 prt_printf(out, "total work:");
285 prt_human_readable_u64(out, w.total_work << 9);
286 prt_printf(out, "/");
287 prt_human_readable_u64(out, c->capacity << 9);
290 prt_printf(out, "rate:");
292 prt_printf(out, "%u", r->pd.rate.rate);
296 case REBALANCE_WAITING:
297 prt_printf(out, "waiting");
299 case REBALANCE_THROTTLED:
300 prt_printf(out, "throttled for %lu sec or ",
301 (r->throttled_until_cputime - jiffies) / HZ);
302 prt_human_readable_u64(out,
303 (r->throttled_until_iotime -
304 atomic64_read(&c->io_clock[WRITE].now)) << 9);
305 prt_printf(out, " io");
307 case REBALANCE_RUNNING:
308 prt_printf(out, "running");
314 void bch2_rebalance_stop(struct bch_fs *c)
316 struct task_struct *p;
318 c->rebalance.pd.rate.rate = UINT_MAX;
319 bch2_ratelimit_reset(&c->rebalance.pd.rate);
321 p = rcu_dereference_protected(c->rebalance.thread, 1);
322 c->rebalance.thread = NULL;
325 /* for sychronizing with rebalance_wakeup() */
333 int bch2_rebalance_start(struct bch_fs *c)
335 struct task_struct *p;
338 if (c->rebalance.thread)
341 if (c->opts.nochanges)
344 p = kthread_create(bch2_rebalance_thread, c, "bch-rebalance/%s", c->name);
345 ret = PTR_ERR_OR_ZERO(p);
347 bch_err(c, "error creating rebalance thread: %s", bch2_err_str(ret));
352 rcu_assign_pointer(c->rebalance.thread, p);
357 void bch2_fs_rebalance_init(struct bch_fs *c)
359 bch2_pd_controller_init(&c->rebalance.pd);
361 atomic64_set(&c->rebalance.work_unknown_dev, S64_MAX);