2 * Moving/copying garbage collector
4 * Copyright 2012 Google, Inc.
8 #include "btree_iter.h"
9 #include "btree_update.h"
13 #include "eytzinger.h"
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>
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.
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
39 #define COPYGC_BUCKETS_PER_ITER(ca) \
40 ((ca)->free[RESERVE_MOVINGGC].size / 2)
43 * Max sectors to move per iteration: Have to take into account internal
44 * fragmentation from the multiple write points for each generation:
46 #define COPYGC_SECTORS_PER_ITER(ca) \
47 ((ca)->mi.bucket_size * COPYGC_BUCKETS_PER_ITER(ca))
49 static inline int sectors_used_cmp(copygc_heap *heap,
50 struct copygc_heap_entry l,
51 struct copygc_heap_entry r)
53 return bucket_sectors_used(l.mark) - bucket_sectors_used(r.mark);
56 static int bucket_offset_cmp(const void *_l, const void *_r, size_t size)
58 const struct copygc_heap_entry *l = _l;
59 const struct copygc_heap_entry *r = _r;
61 return (l->offset > r->offset) - (l->offset < r->offset);
64 static bool __copygc_pred(struct bch_dev *ca,
65 struct bkey_s_c_extent e)
67 copygc_heap *h = &ca->copygc_heap;
68 const struct bch_extent_ptr *ptr =
69 bch2_extent_has_device(e, ca->dev_idx);
72 struct copygc_heap_entry search = { .offset = ptr->offset };
74 size_t i = eytzinger0_find_le(h->data, h->used,
76 bucket_offset_cmp, &search);
79 ptr->offset < h->data[i].offset + ca->mi.bucket_size &&
80 ptr->gen == h->data[i].mark.gen);
86 static enum data_cmd copygc_pred(struct bch_fs *c, void *arg,
88 struct bkey_s_c_extent e,
89 struct bch_io_opts *io_opts,
90 struct data_opts *data_opts)
92 struct bch_dev *ca = arg;
94 if (!__copygc_pred(ca, e))
97 data_opts->btree_insert_flags = BTREE_INSERT_USE_RESERVE,
98 data_opts->rewrite_dev = ca->dev_idx;
102 static bool have_copygc_reserve(struct bch_dev *ca)
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);
114 static void bch2_copygc(struct bch_fs *c, struct bch_dev *ca)
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;
125 memset(&move_stats, 0, sizeof(move_stats));
126 closure_wait_event(&c->freelist_wait, have_copygc_reserve(ca));
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.
137 * We need bucket marks to be up to date - gc can't be recalculating
140 down_read(&c->gc_lock);
141 down_read(&ca->bucket_lock);
142 buckets = bucket_array(ca);
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;
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)
154 e = (struct copygc_heap_entry) {
155 .offset = bucket_to_sector(ca, b),
158 heap_add_or_replace(h, e, -sectors_used_cmp);
160 up_read(&ca->bucket_lock);
161 up_read(&c->gc_lock);
163 for (i = h->data; i < h->data + h->used; i++)
164 sectors_to_move += bucket_sectors_used(i->mark);
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);
171 buckets_to_move = h->used;
173 if (!buckets_to_move)
176 eytzinger0_sort(h->data, h->used,
178 bucket_offset_cmp, NULL);
180 ret = bch2_move_data(c, &ca->copygc_pd.rate,
181 SECTORS_IN_FLIGHT_PER_DEVICE,
183 writepoint_ptr(&ca->copygc_write_point),
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);
194 if (i->mark.gen == m.gen && bucket_sectors_used(m)) {
195 sectors_not_moved += bucket_sectors_used(m);
199 up_read(&ca->bucket_lock);
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);
207 atomic64_read(&move_stats.sectors_moved), sectors_not_moved,
208 buckets_to_move, buckets_not_moved);
211 static int bch2_copygc_thread(void *arg)
213 struct bch_dev *ca = arg;
214 struct bch_fs *c = ca->fs;
215 struct io_clock *clock = &c->io_clock[WRITE];
217 u64 available, want, next;
221 while (!kthread_should_stop()) {
222 if (kthread_wait_freezable(c->copy_gc_enabled))
225 last = atomic_long_read(&clock->now);
227 * don't start copygc until less than half the gc reserve is
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) *
236 bch2_kthread_io_clock_wait(clock, next);
246 void bch2_copygc_stop(struct bch_dev *ca)
248 ca->copygc_pd.rate.rate = UINT_MAX;
249 bch2_ratelimit_reset(&ca->copygc_pd.rate);
251 if (ca->copygc_thread)
252 kthread_stop(ca->copygc_thread);
253 ca->copygc_thread = NULL;
256 int bch2_copygc_start(struct bch_fs *c, struct bch_dev *ca)
258 struct task_struct *t;
260 BUG_ON(ca->copygc_thread);
262 if (c->opts.nochanges)
265 if (bch2_fs_init_fault("copygc_start"))
268 t = kthread_create(bch2_copygc_thread, ca, "bch_copygc");
272 ca->copygc_thread = t;
273 wake_up_process(ca->copygc_thread);
278 void bch2_dev_copygc_init(struct bch_dev *ca)
280 bch2_pd_controller_init(&ca->copygc_pd);
281 ca->copygc_pd.d_term = 0;