3 #include "alloc_foreground.h"
4 #include "btree_iter.h"
7 #include "disk_groups.h"
11 #include "rebalance.h"
14 #include <linux/freezer.h>
15 #include <linux/kthread.h>
16 #include <linux/sched/cputime.h>
17 #include <trace/events/bcachefs.h>
19 static inline bool rebalance_ptr_pred(struct bch_fs *c,
20 struct extent_ptr_decoded p,
21 struct bch_io_opts *io_opts)
23 if (io_opts->background_target &&
24 !bch2_dev_in_target(c, p.ptr.dev, io_opts->background_target) &&
28 if (io_opts->background_compression &&
29 p.crc.compression_type !=
30 bch2_compression_opt_to_type[io_opts->background_compression])
36 void bch2_rebalance_add_key(struct bch_fs *c,
38 struct bch_io_opts *io_opts)
40 const union bch_extent_entry *entry;
41 struct extent_ptr_decoded p;
42 struct bkey_s_c_extent e;
44 if (!bkey_extent_is_data(k.k))
47 if (!io_opts->background_target &&
48 !io_opts->background_compression)
51 e = bkey_s_c_to_extent(k);
53 extent_for_each_ptr_decode(e, p, entry)
54 if (rebalance_ptr_pred(c, p, io_opts)) {
55 struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
57 if (atomic64_add_return(p.crc.compressed_size,
58 &ca->rebalance_work) ==
59 p.crc.compressed_size)
64 void bch2_rebalance_add_work(struct bch_fs *c, u64 sectors)
66 if (atomic64_add_return(sectors, &c->rebalance.work_unknown_dev) ==
71 static enum data_cmd rebalance_pred(struct bch_fs *c, void *arg,
73 struct bkey_s_c_extent e,
74 struct bch_io_opts *io_opts,
75 struct data_opts *data_opts)
77 const union bch_extent_entry *entry;
78 struct extent_ptr_decoded p;
80 /* Make sure we have room to add a new pointer: */
81 if (bkey_val_u64s(e.k) + BKEY_EXTENT_PTR_U64s_MAX >
82 BKEY_EXTENT_VAL_U64s_MAX)
85 extent_for_each_ptr_decode(e, p, entry)
86 if (rebalance_ptr_pred(c, p, io_opts))
91 data_opts->target = io_opts->background_target;
92 data_opts->btree_insert_flags = 0;
93 return DATA_ADD_REPLICAS;
96 struct rebalance_work {
97 int dev_most_full_idx;
98 unsigned dev_most_full_percent;
99 u64 dev_most_full_work;
100 u64 dev_most_full_capacity;
104 static void rebalance_work_accumulate(struct rebalance_work *w,
105 u64 dev_work, u64 unknown_dev, u64 capacity, int idx)
107 unsigned percent_full;
108 u64 work = dev_work + unknown_dev;
110 if (work < dev_work || work < unknown_dev)
112 work = min(work, capacity);
114 percent_full = div64_u64(work * 100, capacity);
116 if (percent_full >= w->dev_most_full_percent) {
117 w->dev_most_full_idx = idx;
118 w->dev_most_full_percent = percent_full;
119 w->dev_most_full_work = work;
120 w->dev_most_full_capacity = capacity;
123 if (w->total_work + dev_work >= w->total_work &&
124 w->total_work + dev_work >= dev_work)
125 w->total_work += dev_work;
128 static struct rebalance_work rebalance_work(struct bch_fs *c)
131 struct rebalance_work ret = { .dev_most_full_idx = -1 };
132 u64 unknown_dev = atomic64_read(&c->rebalance.work_unknown_dev);
135 for_each_online_member(ca, c, i)
136 rebalance_work_accumulate(&ret,
137 atomic64_read(&ca->rebalance_work),
139 bucket_to_sector(ca, ca->mi.nbuckets -
140 ca->mi.first_bucket),
143 rebalance_work_accumulate(&ret,
144 unknown_dev, 0, c->capacity, -1);
149 static void rebalance_work_reset(struct bch_fs *c)
154 for_each_online_member(ca, c, i)
155 atomic64_set(&ca->rebalance_work, 0);
157 atomic64_set(&c->rebalance.work_unknown_dev, 0);
160 static unsigned long curr_cputime(void)
164 task_cputime_adjusted(current, &utime, &stime);
165 return nsecs_to_jiffies(utime + stime);
168 static int bch2_rebalance_thread(void *arg)
170 struct bch_fs *c = arg;
171 struct bch_fs_rebalance *r = &c->rebalance;
172 struct io_clock *clock = &c->io_clock[WRITE];
173 struct rebalance_work w, p;
174 unsigned long start, prev_start;
175 unsigned long prev_run_time, prev_run_cputime;
176 unsigned long cputime, prev_cputime;
177 unsigned long io_start;
182 io_start = atomic_long_read(&clock->now);
183 p = rebalance_work(c);
184 prev_start = jiffies;
185 prev_cputime = curr_cputime();
187 while (!kthread_wait_freezable(r->enabled)) {
189 cputime = curr_cputime();
191 prev_run_time = start - prev_start;
192 prev_run_cputime = cputime - prev_cputime;
194 w = rebalance_work(c);
195 BUG_ON(!w.dev_most_full_capacity);
198 r->state = REBALANCE_WAITING;
199 kthread_wait_freezable(rebalance_work(c).total_work);
204 * If there isn't much work to do, throttle cpu usage:
206 throttle = prev_run_cputime * 100 /
207 max(1U, w.dev_most_full_percent) -
210 if (w.dev_most_full_percent < 20 && throttle > 0) {
211 r->state = REBALANCE_THROTTLED;
212 r->throttled_until_iotime = io_start +
213 div_u64(w.dev_most_full_capacity *
214 (20 - w.dev_most_full_percent),
216 r->throttled_until_cputime = start + throttle;
218 bch2_kthread_io_clock_wait(clock,
219 r->throttled_until_iotime,
224 /* minimum 1 mb/sec: */
228 max(p.dev_most_full_percent, 1U) /
229 max(w.dev_most_full_percent, 1U));
231 io_start = atomic_long_read(&clock->now);
234 prev_cputime = cputime;
236 r->state = REBALANCE_RUNNING;
237 memset(&r->move_stats, 0, sizeof(r->move_stats));
238 rebalance_work_reset(c);
241 /* ratelimiting disabled for now */
242 NULL, /* &r->pd.rate, */
243 writepoint_ptr(&c->rebalance_write_point),
245 rebalance_pred, NULL,
252 ssize_t bch2_rebalance_work_show(struct bch_fs *c, char *buf)
254 char *out = buf, *end = out + PAGE_SIZE;
255 struct bch_fs_rebalance *r = &c->rebalance;
256 struct rebalance_work w = rebalance_work(c);
259 bch2_hprint(h1, w.dev_most_full_work << 9);
260 bch2_hprint(h2, w.dev_most_full_capacity << 9);
261 out += scnprintf(out, end - out,
262 "fullest_dev (%i):\t%s/%s\n",
263 w.dev_most_full_idx, h1, h2);
265 bch2_hprint(h1, w.total_work << 9);
266 bch2_hprint(h2, c->capacity << 9);
267 out += scnprintf(out, end - out,
268 "total work:\t\t%s/%s\n",
271 out += scnprintf(out, end - out,
276 case REBALANCE_WAITING:
277 out += scnprintf(out, end - out, "waiting\n");
279 case REBALANCE_THROTTLED:
281 (r->throttled_until_iotime -
282 atomic_long_read(&c->io_clock[WRITE].now)) << 9);
283 out += scnprintf(out, end - out,
284 "throttled for %lu sec or %s io\n",
285 (r->throttled_until_cputime - jiffies) / HZ,
288 case REBALANCE_RUNNING:
289 out += scnprintf(out, end - out, "running\n");
290 out += scnprintf(out, end - out, "pos %llu:%llu\n",
291 r->move_stats.iter.pos.inode,
292 r->move_stats.iter.pos.offset);
299 void bch2_rebalance_stop(struct bch_fs *c)
301 struct task_struct *p;
303 c->rebalance.pd.rate.rate = UINT_MAX;
304 bch2_ratelimit_reset(&c->rebalance.pd.rate);
306 p = rcu_dereference_protected(c->rebalance.thread, 1);
307 c->rebalance.thread = NULL;
310 /* for sychronizing with rebalance_wakeup() */
318 int bch2_rebalance_start(struct bch_fs *c)
320 struct task_struct *p;
322 if (c->opts.nochanges)
325 p = kthread_create(bch2_rebalance_thread, c, "bch_rebalance");
330 rcu_assign_pointer(c->rebalance.thread, p);
335 void bch2_fs_rebalance_init(struct bch_fs *c)
337 bch2_pd_controller_init(&c->rebalance.pd);
339 atomic64_set(&c->rebalance.work_unknown_dev, S64_MAX);