]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/movinggc.c
Update bcachefs sources to e99d29e402 bcachefs: zstd support, compression refactoring
[bcachefs-tools-debian] / libbcachefs / movinggc.c
1 /*
2  * Moving/copying garbage collector
3  *
4  * Copyright 2012 Google, Inc.
5  */
6
7 #include "bcachefs.h"
8 #include "btree_iter.h"
9 #include "btree_update.h"
10 #include "buckets.h"
11 #include "clock.h"
12 #include "extents.h"
13 #include "eytzinger.h"
14 #include "io.h"
15 #include "keylist.h"
16 #include "move.h"
17 #include "movinggc.h"
18 #include "super-io.h"
19
20 #include <trace/events/bcachefs.h>
21 #include <linux/freezer.h>
22 #include <linux/kthread.h>
23 #include <linux/math64.h>
24 #include <linux/sort.h>
25 #include <linux/wait.h>
26
27 /*
28  * We can't use the entire copygc reserve in one iteration of copygc: we may
29  * need the buckets we're freeing up to go back into the copygc reserve to make
30  * forward progress, but if the copygc reserve is full they'll be available for
31  * any allocation - and it's possible that in a given iteration, we free up most
32  * of the buckets we're going to free before we allocate most of the buckets
33  * we're going to allocate.
34  *
35  * If we only use half of the reserve per iteration, then in steady state we'll
36  * always have room in the reserve for the buckets we're going to need in the
37  * next iteration:
38  */
39 #define COPYGC_BUCKETS_PER_ITER(ca)                                     \
40         ((ca)->free[RESERVE_MOVINGGC].size / 2)
41
42 /*
43  * Max sectors to move per iteration: Have to take into account internal
44  * fragmentation from the multiple write points for each generation:
45  */
46 #define COPYGC_SECTORS_PER_ITER(ca)                                     \
47         ((ca)->mi.bucket_size * COPYGC_BUCKETS_PER_ITER(ca))
48
49 static inline int sectors_used_cmp(copygc_heap *heap,
50                                    struct copygc_heap_entry l,
51                                    struct copygc_heap_entry r)
52 {
53         return bucket_sectors_used(l.mark) - bucket_sectors_used(r.mark);
54 }
55
56 static int bucket_offset_cmp(const void *_l, const void *_r, size_t size)
57 {
58         const struct copygc_heap_entry *l = _l;
59         const struct copygc_heap_entry *r = _r;
60
61         return (l->offset > r->offset) - (l->offset < r->offset);
62 }
63
64 static bool __copygc_pred(struct bch_dev *ca,
65                           struct bkey_s_c_extent e)
66 {
67         copygc_heap *h = &ca->copygc_heap;
68         const struct bch_extent_ptr *ptr =
69                 bch2_extent_has_device(e, ca->dev_idx);
70
71         if (ptr) {
72                 struct copygc_heap_entry search = { .offset = ptr->offset };
73
74                 size_t i = eytzinger0_find_le(h->data, h->used,
75                                               sizeof(h->data[0]),
76                                               bucket_offset_cmp, &search);
77
78                 return (i >= 0 &&
79                         ptr->offset < h->data[i].offset + ca->mi.bucket_size &&
80                         ptr->gen == h->data[i].mark.gen);
81         }
82
83         return false;
84 }
85
86 static enum data_cmd copygc_pred(struct bch_fs *c, void *arg,
87                                  enum bkey_type type,
88                                  struct bkey_s_c_extent e,
89                                  struct bch_io_opts *io_opts,
90                                  struct data_opts *data_opts)
91 {
92         struct bch_dev *ca = arg;
93
94         if (!__copygc_pred(ca, e))
95                 return DATA_SKIP;
96
97         data_opts->btree_insert_flags   = BTREE_INSERT_USE_RESERVE,
98         data_opts->rewrite_dev          = ca->dev_idx;
99         return DATA_REWRITE;
100 }
101
102 static bool have_copygc_reserve(struct bch_dev *ca)
103 {
104         bool ret;
105
106         spin_lock(&ca->freelist_lock);
107         ret = fifo_used(&ca->free[RESERVE_MOVINGGC]) >=
108                 COPYGC_BUCKETS_PER_ITER(ca);
109         spin_unlock(&ca->freelist_lock);
110
111         return ret;
112 }
113
114 static void bch2_copygc(struct bch_fs *c, struct bch_dev *ca)
115 {
116         copygc_heap *h = &ca->copygc_heap;
117         struct copygc_heap_entry e, *i;
118         struct bucket_array *buckets;
119         struct bch_move_stats move_stats;
120         u64 sectors_to_move = 0, sectors_not_moved = 0;
121         u64 buckets_to_move, buckets_not_moved = 0;
122         size_t b;
123         int ret;
124
125         memset(&move_stats, 0, sizeof(move_stats));
126         closure_wait_event(&c->freelist_wait, have_copygc_reserve(ca));
127
128         /*
129          * Find buckets with lowest sector counts, skipping completely
130          * empty buckets, by building a maxheap sorted by sector count,
131          * and repeatedly replacing the maximum element until all
132          * buckets have been visited.
133          */
134         h->used = 0;
135
136         /*
137          * We need bucket marks to be up to date - gc can't be recalculating
138          * them:
139          */
140         down_read(&c->gc_lock);
141         down_read(&ca->bucket_lock);
142         buckets = bucket_array(ca);
143
144         for (b = buckets->first_bucket; b < buckets->nbuckets; b++) {
145                 struct bucket_mark m = READ_ONCE(buckets->b[b].mark);
146                 struct copygc_heap_entry e;
147
148                 if (m.owned_by_allocator ||
149                     m.data_type != BCH_DATA_USER ||
150                     !bucket_sectors_used(m) ||
151                     bucket_sectors_used(m) >= ca->mi.bucket_size)
152                         continue;
153
154                 e = (struct copygc_heap_entry) {
155                         .offset = bucket_to_sector(ca, b),
156                         .mark   = m
157                 };
158                 heap_add_or_replace(h, e, -sectors_used_cmp);
159         }
160         up_read(&ca->bucket_lock);
161         up_read(&c->gc_lock);
162
163         for (i = h->data; i < h->data + h->used; i++)
164                 sectors_to_move += bucket_sectors_used(i->mark);
165
166         while (sectors_to_move > COPYGC_SECTORS_PER_ITER(ca)) {
167                 BUG_ON(!heap_pop(h, e, -sectors_used_cmp));
168                 sectors_to_move -= bucket_sectors_used(e.mark);
169         }
170
171         buckets_to_move = h->used;
172
173         if (!buckets_to_move)
174                 return;
175
176         eytzinger0_sort(h->data, h->used,
177                         sizeof(h->data[0]),
178                         bucket_offset_cmp, NULL);
179
180         ret = bch2_move_data(c, &ca->copygc_pd.rate,
181                              SECTORS_IN_FLIGHT_PER_DEVICE,
182                              &ca->self,
183                              writepoint_ptr(&ca->copygc_write_point),
184                              POS_MIN, POS_MAX,
185                              copygc_pred, ca,
186                              &move_stats);
187
188         down_read(&ca->bucket_lock);
189         buckets = bucket_array(ca);
190         for (i = h->data; i < h->data + h->used; i++) {
191                 size_t b = sector_to_bucket(ca, i->offset);
192                 struct bucket_mark m = READ_ONCE(buckets->b[b].mark);
193
194                 if (i->mark.gen == m.gen && bucket_sectors_used(m)) {
195                         sectors_not_moved += bucket_sectors_used(m);
196                         buckets_not_moved++;
197                 }
198         }
199         up_read(&ca->bucket_lock);
200
201         if (sectors_not_moved && !ret)
202                 bch_warn(c, "copygc finished but %llu/%llu sectors, %llu/%llu buckets not moved",
203                          sectors_not_moved, sectors_to_move,
204                          buckets_not_moved, buckets_to_move);
205
206         trace_copygc(ca,
207                      atomic64_read(&move_stats.sectors_moved), sectors_not_moved,
208                      buckets_to_move, buckets_not_moved);
209 }
210
211 static int bch2_copygc_thread(void *arg)
212 {
213         struct bch_dev *ca = arg;
214         struct bch_fs *c = ca->fs;
215         struct io_clock *clock = &c->io_clock[WRITE];
216         unsigned long last;
217         u64 available, want, next;
218
219         set_freezable();
220
221         while (!kthread_should_stop()) {
222                 if (kthread_wait_freezable(c->copy_gc_enabled))
223                         break;
224
225                 last = atomic_long_read(&clock->now);
226                 /*
227                  * don't start copygc until less than half the gc reserve is
228                  * available:
229                  */
230                 available = dev_buckets_available(c, ca);
231                 want = div64_u64((ca->mi.nbuckets - ca->mi.first_bucket) *
232                                  c->opts.gc_reserve_percent, 200);
233                 if (available > want) {
234                         next = last + (available - want) *
235                                 ca->mi.bucket_size;
236                         bch2_kthread_io_clock_wait(clock, next);
237                         continue;
238                 }
239
240                 bch2_copygc(c, ca);
241         }
242
243         return 0;
244 }
245
246 void bch2_copygc_stop(struct bch_dev *ca)
247 {
248         ca->copygc_pd.rate.rate = UINT_MAX;
249         bch2_ratelimit_reset(&ca->copygc_pd.rate);
250
251         if (ca->copygc_thread)
252                 kthread_stop(ca->copygc_thread);
253         ca->copygc_thread = NULL;
254 }
255
256 int bch2_copygc_start(struct bch_fs *c, struct bch_dev *ca)
257 {
258         struct task_struct *t;
259
260         BUG_ON(ca->copygc_thread);
261
262         if (c->opts.nochanges)
263                 return 0;
264
265         if (bch2_fs_init_fault("copygc_start"))
266                 return -ENOMEM;
267
268         t = kthread_create(bch2_copygc_thread, ca, "bch_copygc");
269         if (IS_ERR(t))
270                 return PTR_ERR(t);
271
272         ca->copygc_thread = t;
273         wake_up_process(ca->copygc_thread);
274
275         return 0;
276 }
277
278 void bch2_dev_copygc_init(struct bch_dev *ca)
279 {
280         bch2_pd_controller_init(&ca->copygc_pd);
281         ca->copygc_pd.d_term = 0;
282 }