]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/rebalance.c
Update bcachefs sources to e14d7c7195 bcachefs: Compression levels
[bcachefs-tools-debian] / libbcachefs / rebalance.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "alloc_foreground.h"
5 #include "btree_iter.h"
6 #include "buckets.h"
7 #include "clock.h"
8 #include "compress.h"
9 #include "disk_groups.h"
10 #include "errcode.h"
11 #include "extents.h"
12 #include "io.h"
13 #include "move.h"
14 #include "rebalance.h"
15 #include "super-io.h"
16 #include "trace.h"
17
18 #include <linux/freezer.h>
19 #include <linux/kthread.h>
20 #include <linux/sched/cputime.h>
21
22 /*
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
26  */
27 static bool rebalance_pred(struct bch_fs *c, void *arg,
28                            struct bkey_s_c k,
29                            struct bch_io_opts *io_opts,
30                            struct data_update_opts *data_opts)
31 {
32         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
33         unsigned i;
34
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;
39
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;
44
45                 i = 0;
46                 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
47                         if (!p.ptr.cached &&
48                             p.crc.compression_type !=
49                             bch2_compression_opt_to_type(io_opts->background_compression))
50                                 data_opts->rewrite_ptrs |= 1U << i;
51                         i++;
52                 }
53         }
54
55         if (io_opts->background_target) {
56                 const struct bch_extent_ptr *ptr;
57
58                 i = 0;
59                 bkey_for_each_ptr(ptrs, ptr) {
60                         if (!ptr->cached &&
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;
64                         i++;
65                 }
66         }
67
68         return data_opts->rewrite_ptrs != 0;
69 }
70
71 void bch2_rebalance_add_key(struct bch_fs *c,
72                             struct bkey_s_c k,
73                             struct bch_io_opts *io_opts)
74 {
75         struct data_update_opts update_opts = { 0 };
76         struct bkey_ptrs_c ptrs;
77         const struct bch_extent_ptr *ptr;
78         unsigned i;
79
80         if (!rebalance_pred(c, NULL, k, io_opts, &update_opts))
81                 return;
82
83         i = 0;
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) ==
89                             k.k->size)
90                                 rebalance_wakeup(c);
91                 i++;
92         }
93 }
94
95 void bch2_rebalance_add_work(struct bch_fs *c, u64 sectors)
96 {
97         if (atomic64_add_return(sectors, &c->rebalance.work_unknown_dev) ==
98             sectors)
99                 rebalance_wakeup(c);
100 }
101
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;
107         u64             total_work;
108 };
109
110 static void rebalance_work_accumulate(struct rebalance_work *w,
111                 u64 dev_work, u64 unknown_dev, u64 capacity, int idx)
112 {
113         unsigned percent_full;
114         u64 work = dev_work + unknown_dev;
115
116         if (work < dev_work || work < unknown_dev)
117                 work = U64_MAX;
118         work = min(work, capacity);
119
120         percent_full = div64_u64(work * 100, capacity);
121
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;
127         }
128
129         if (w->total_work + dev_work >= w->total_work &&
130             w->total_work + dev_work >= dev_work)
131                 w->total_work += dev_work;
132 }
133
134 static struct rebalance_work rebalance_work(struct bch_fs *c)
135 {
136         struct bch_dev *ca;
137         struct rebalance_work ret = { .dev_most_full_idx = -1 };
138         u64 unknown_dev = atomic64_read(&c->rebalance.work_unknown_dev);
139         unsigned i;
140
141         for_each_online_member(ca, c, i)
142                 rebalance_work_accumulate(&ret,
143                         atomic64_read(&ca->rebalance_work),
144                         unknown_dev,
145                         bucket_to_sector(ca, ca->mi.nbuckets -
146                                          ca->mi.first_bucket),
147                         i);
148
149         rebalance_work_accumulate(&ret,
150                 unknown_dev, 0, c->capacity, -1);
151
152         return ret;
153 }
154
155 static void rebalance_work_reset(struct bch_fs *c)
156 {
157         struct bch_dev *ca;
158         unsigned i;
159
160         for_each_online_member(ca, c, i)
161                 atomic64_set(&ca->rebalance_work, 0);
162
163         atomic64_set(&c->rebalance.work_unknown_dev, 0);
164 }
165
166 static unsigned long curr_cputime(void)
167 {
168         u64 utime, stime;
169
170         task_cputime_adjusted(current, &utime, &stime);
171         return nsecs_to_jiffies(utime + stime);
172 }
173
174 static int bch2_rebalance_thread(void *arg)
175 {
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;
184         u64 io_start;
185         long throttle;
186
187         set_freezable();
188
189         io_start        = atomic64_read(&clock->now);
190         p               = rebalance_work(c);
191         prev_start      = jiffies;
192         prev_cputime    = curr_cputime();
193
194         bch2_move_stats_init(&move_stats, "rebalance");
195         while (!kthread_wait_freezable(r->enabled)) {
196                 cond_resched();
197
198                 start                   = jiffies;
199                 cputime                 = curr_cputime();
200
201                 prev_run_time           = start - prev_start;
202                 prev_run_cputime        = cputime - prev_cputime;
203
204                 w                       = rebalance_work(c);
205                 BUG_ON(!w.dev_most_full_capacity);
206
207                 if (!w.total_work) {
208                         r->state = REBALANCE_WAITING;
209                         kthread_wait_freezable(rebalance_work(c).total_work);
210                         continue;
211                 }
212
213                 /*
214                  * If there isn't much work to do, throttle cpu usage:
215                  */
216                 throttle = prev_run_cputime * 100 /
217                         max(1U, w.dev_most_full_percent) -
218                         prev_run_time;
219
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),
224                                         50);
225
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;
230
231                                 bch2_kthread_io_clock_wait(clock,
232                                         r->throttled_until_iotime,
233                                         throttle);
234                                 continue;
235                         }
236                 }
237
238                 /* minimum 1 mb/sec: */
239                 r->pd.rate.rate =
240                         max_t(u64, 1 << 11,
241                               r->pd.rate.rate *
242                               max(p.dev_most_full_percent, 1U) /
243                               max(w.dev_most_full_percent, 1U));
244
245                 io_start        = atomic64_read(&clock->now);
246                 p               = w;
247                 prev_start      = start;
248                 prev_cputime    = cputime;
249
250                 r->state = REBALANCE_RUNNING;
251                 memset(&move_stats, 0, sizeof(move_stats));
252                 rebalance_work_reset(c);
253
254                 bch2_move_data(c,
255                                0,               POS_MIN,
256                                BTREE_ID_NR,     POS_MAX,
257                                /* ratelimiting disabled for now */
258                                NULL, /*  &r->pd.rate, */
259                                &move_stats,
260                                writepoint_ptr(&c->rebalance_write_point),
261                                true,
262                                rebalance_pred, NULL);
263         }
264
265         return 0;
266 }
267
268 void bch2_rebalance_work_to_text(struct printbuf *out, struct bch_fs *c)
269 {
270         struct bch_fs_rebalance *r = &c->rebalance;
271         struct rebalance_work w = rebalance_work(c);
272
273         if (!out->nr_tabstops)
274                 printbuf_tabstop_push(out, 20);
275
276         prt_printf(out, "fullest_dev (%i):", w.dev_most_full_idx);
277         prt_tab(out);
278
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);
282         prt_newline(out);
283
284         prt_printf(out, "total work:");
285         prt_tab(out);
286
287         prt_human_readable_u64(out, w.total_work << 9);
288         prt_printf(out, "/");
289         prt_human_readable_u64(out, c->capacity << 9);
290         prt_newline(out);
291
292         prt_printf(out, "rate:");
293         prt_tab(out);
294         prt_printf(out, "%u", r->pd.rate.rate);
295         prt_newline(out);
296
297         switch (r->state) {
298         case REBALANCE_WAITING:
299                 prt_printf(out, "waiting");
300                 break;
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");
308                 break;
309         case REBALANCE_RUNNING:
310                 prt_printf(out, "running");
311                 break;
312         }
313         prt_newline(out);
314 }
315
316 void bch2_rebalance_stop(struct bch_fs *c)
317 {
318         struct task_struct *p;
319
320         c->rebalance.pd.rate.rate = UINT_MAX;
321         bch2_ratelimit_reset(&c->rebalance.pd.rate);
322
323         p = rcu_dereference_protected(c->rebalance.thread, 1);
324         c->rebalance.thread = NULL;
325
326         if (p) {
327                 /* for sychronizing with rebalance_wakeup() */
328                 synchronize_rcu();
329
330                 kthread_stop(p);
331                 put_task_struct(p);
332         }
333 }
334
335 int bch2_rebalance_start(struct bch_fs *c)
336 {
337         struct task_struct *p;
338         int ret;
339
340         if (c->rebalance.thread)
341                 return 0;
342
343         if (c->opts.nochanges)
344                 return 0;
345
346         p = kthread_create(bch2_rebalance_thread, c, "bch-rebalance/%s", c->name);
347         ret = PTR_ERR_OR_ZERO(p);
348         if (ret) {
349                 bch_err(c, "error creating rebalance thread: %s", bch2_err_str(ret));
350                 return ret;
351         }
352
353         get_task_struct(p);
354         rcu_assign_pointer(c->rebalance.thread, p);
355         wake_up_process(p);
356         return 0;
357 }
358
359 void bch2_fs_rebalance_init(struct bch_fs *c)
360 {
361         bch2_pd_controller_init(&c->rebalance.pd);
362
363         atomic64_set(&c->rebalance.work_unknown_dev, S64_MAX);
364 }