]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/movinggc.c
c6a9ac24c7855bfa3fde702a233b6adf3145b148
[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(void *arg, struct bkey_s_c_extent e)
65 {
66         struct bch_dev *ca = arg;
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 bool have_copygc_reserve(struct bch_dev *ca)
87 {
88         bool ret;
89
90         spin_lock(&ca->freelist_lock);
91         ret = fifo_used(&ca->free[RESERVE_MOVINGGC]) >=
92                 COPYGC_BUCKETS_PER_ITER(ca);
93         spin_unlock(&ca->freelist_lock);
94
95         return ret;
96 }
97
98 static void bch2_copygc(struct bch_fs *c, struct bch_dev *ca)
99 {
100         copygc_heap *h = &ca->copygc_heap;
101         struct copygc_heap_entry e, *i;
102         struct bucket *g;
103         u64 keys_moved, sectors_moved;
104         u64 sectors_to_move = 0, sectors_not_moved = 0;
105         u64 buckets_to_move, buckets_not_moved = 0;
106         int ret;
107
108         closure_wait_event(&c->freelist_wait, have_copygc_reserve(ca));
109
110         /*
111          * Find buckets with lowest sector counts, skipping completely
112          * empty buckets, by building a maxheap sorted by sector count,
113          * and repeatedly replacing the maximum element until all
114          * buckets have been visited.
115          */
116
117         /*
118          * We need bucket marks to be up to date - gc can't be recalculating
119          * them:
120          */
121         down_read(&c->gc_lock);
122         h->used = 0;
123         for_each_bucket(g, ca) {
124                 struct bucket_mark m = READ_ONCE(g->mark);
125                 struct copygc_heap_entry e;
126
127                 if (m.owned_by_allocator ||
128                     m.data_type != BCH_DATA_USER ||
129                     !bucket_sectors_used(m) ||
130                     bucket_sectors_used(m) >= ca->mi.bucket_size)
131                         continue;
132
133                 e = (struct copygc_heap_entry) {
134                         .offset = bucket_to_sector(ca, g - ca->buckets),
135                         .mark   = m
136                 };
137                 heap_add_or_replace(h, e, -sectors_used_cmp);
138         }
139         up_read(&c->gc_lock);
140
141         for (i = h->data; i < h->data + h->used; i++)
142                 sectors_to_move += bucket_sectors_used(i->mark);
143
144         while (sectors_to_move > COPYGC_SECTORS_PER_ITER(ca)) {
145                 BUG_ON(!heap_pop(h, e, -sectors_used_cmp));
146                 sectors_to_move -= bucket_sectors_used(e.mark);
147         }
148
149         buckets_to_move = h->used;
150
151         if (!buckets_to_move)
152                 return;
153
154         eytzinger0_sort(h->data, h->used,
155                         sizeof(h->data[0]),
156                         bucket_offset_cmp, NULL);
157
158         ret = bch2_move_data(c, &ca->copygc_pd.rate,
159                              SECTORS_IN_FLIGHT_PER_DEVICE,
160                              &ca->self,
161                              writepoint_ptr(&ca->copygc_write_point),
162                              BTREE_INSERT_USE_RESERVE,
163                              ca->dev_idx,
164                              copygc_pred, ca,
165                              &keys_moved,
166                              &sectors_moved);
167
168         for (i = h->data; i < h->data + h->used; i++) {
169                 size_t bucket = sector_to_bucket(ca, i->offset);
170                 struct bucket_mark m = READ_ONCE(ca->buckets[bucket].mark);
171
172                 if (i->mark.gen == m.gen && bucket_sectors_used(m)) {
173                         sectors_not_moved += bucket_sectors_used(m);
174                         buckets_not_moved++;
175                 }
176         }
177
178         if (sectors_not_moved && !ret)
179                 bch_warn(c, "copygc finished but %llu/%llu sectors, %llu/%llu buckets not moved",
180                          sectors_not_moved, sectors_to_move,
181                          buckets_not_moved, buckets_to_move);
182
183         trace_copygc(ca,
184                      sectors_moved, sectors_not_moved,
185                      buckets_to_move, buckets_not_moved);
186 }
187
188 static int bch2_copygc_thread(void *arg)
189 {
190         struct bch_dev *ca = arg;
191         struct bch_fs *c = ca->fs;
192         struct io_clock *clock = &c->io_clock[WRITE];
193         unsigned long last;
194         u64 available, want, next;
195
196         set_freezable();
197
198         while (!kthread_should_stop()) {
199                 if (kthread_wait_freezable(c->copy_gc_enabled))
200                         break;
201
202                 last = atomic_long_read(&clock->now);
203                 /*
204                  * don't start copygc until less than half the gc reserve is
205                  * available:
206                  */
207                 available = dev_buckets_available(c, ca);
208                 want = div64_u64((ca->mi.nbuckets - ca->mi.first_bucket) *
209                                  c->opts.gc_reserve_percent, 200);
210                 if (available > want) {
211                         next = last + (available - want) *
212                                 ca->mi.bucket_size;
213                         bch2_kthread_io_clock_wait(clock, next);
214                         continue;
215                 }
216
217                 bch2_copygc(c, ca);
218         }
219
220         return 0;
221 }
222
223 void bch2_copygc_stop(struct bch_dev *ca)
224 {
225         ca->copygc_pd.rate.rate = UINT_MAX;
226         bch2_ratelimit_reset(&ca->copygc_pd.rate);
227
228         if (ca->copygc_thread)
229                 kthread_stop(ca->copygc_thread);
230         ca->copygc_thread = NULL;
231 }
232
233 int bch2_copygc_start(struct bch_fs *c, struct bch_dev *ca)
234 {
235         struct task_struct *t;
236
237         BUG_ON(ca->copygc_thread);
238
239         if (c->opts.nochanges)
240                 return 0;
241
242         if (bch2_fs_init_fault("copygc_start"))
243                 return -ENOMEM;
244
245         t = kthread_create(bch2_copygc_thread, ca, "bch_copygc");
246         if (IS_ERR(t))
247                 return PTR_ERR(t);
248
249         ca->copygc_thread = t;
250         wake_up_process(ca->copygc_thread);
251
252         return 0;
253 }
254
255 void bch2_dev_copygc_init(struct bch_dev *ca)
256 {
257         bch2_pd_controller_init(&ca->copygc_pd);
258         ca->copygc_pd.d_term = 0;
259 }