2 * Moving/copying garbage collector
4 * Copyright 2012 Google, Inc.
8 #include "btree_iter.h"
17 #include <trace/events/bcache.h>
18 #include <linux/freezer.h>
19 #include <linux/kthread.h>
20 #include <linux/wait.h>
22 /* Moving GC - IO loop */
24 static const struct bch_extent_ptr *moving_pred(struct cache *ca,
27 const struct bch_extent_ptr *ptr;
29 if (bkey_extent_is_data(k.k) &&
30 (ptr = bch_extent_has_device(bkey_s_c_to_extent(k),
32 PTR_BUCKET(ca, ptr)->mark.copygc)
38 static int issue_moving_gc_move(struct cache *ca,
39 struct moving_context *ctxt,
42 struct cache_set *c = ca->set;
43 const struct bch_extent_ptr *ptr;
46 ptr = moving_pred(ca, k);
47 if (!ptr) /* We raced - bucket's been reused */
50 ret = bch_data_move(c, ctxt, &ca->copygc_write_point, k, ptr);
52 trace_bcache_gc_copy(k.k);
54 trace_bcache_moving_gc_alloc_fail(c, k.k->size);
58 static void read_moving(struct cache *ca, size_t buckets_to_move,
61 struct cache_set *c = ca->set;
63 struct moving_context ctxt;
64 struct btree_iter iter;
66 u64 sectors_not_moved = 0;
67 size_t buckets_not_moved = 0;
69 bch_ratelimit_reset(&ca->moving_gc_pd.rate);
70 bch_move_ctxt_init(&ctxt, &ca->moving_gc_pd.rate,
71 SECTORS_IN_FLIGHT_PER_DEVICE);
72 bch_btree_iter_init(&iter, c, BTREE_ID_EXTENTS, POS_MIN);
75 if (kthread_should_stop())
77 if (bch_move_ctxt_wait(&ctxt))
79 k = bch_btree_iter_peek(&iter);
82 if (btree_iter_err(k))
85 if (!moving_pred(ca, k))
88 if (issue_moving_gc_move(ca, &ctxt, k)) {
89 bch_btree_iter_unlock(&iter);
91 /* memory allocation failure, wait for some IO to finish */
92 bch_move_ctxt_wait_for_io(&ctxt);
96 bch_btree_iter_advance_pos(&iter);
97 //bch_btree_iter_cond_resched(&iter);
99 /* unlock before calling moving_context_wait() */
100 bch_btree_iter_unlock(&iter);
104 bch_btree_iter_unlock(&iter);
105 bch_move_ctxt_exit(&ctxt);
106 trace_bcache_moving_gc_end(ca, ctxt.sectors_moved, ctxt.keys_moved,
109 /* don't check this if we bailed out early: */
110 for_each_bucket(g, ca)
111 if (g->mark.copygc && bucket_sectors_used(g)) {
112 sectors_not_moved += bucket_sectors_used(g);
116 if (sectors_not_moved)
117 bch_warn(c, "copygc finished but %llu/%llu sectors, %zu/%zu buckets not moved",
118 sectors_not_moved, sectors_to_move,
119 buckets_not_moved, buckets_to_move);
122 bch_btree_iter_unlock(&iter);
123 bch_move_ctxt_exit(&ctxt);
124 trace_bcache_moving_gc_end(ca, ctxt.sectors_moved, ctxt.keys_moved,
128 static bool have_copygc_reserve(struct cache *ca)
132 spin_lock(&ca->freelist_lock);
133 ret = fifo_used(&ca->free[RESERVE_MOVINGGC]) >=
134 COPYGC_BUCKETS_PER_ITER(ca);
135 spin_unlock(&ca->freelist_lock);
140 static void bch_moving_gc(struct cache *ca)
142 struct cache_set *c = ca->set;
144 struct bucket_mark new;
146 size_t buckets_to_move, buckets_unused = 0;
147 struct bucket_heap_entry e;
148 unsigned sectors_used, i;
151 if (!have_copygc_reserve(ca)) {
154 closure_init_stack(&cl);
156 closure_wait(&c->freelist_wait, &cl);
157 if (have_copygc_reserve(ca))
161 closure_wake_up(&c->freelist_wait);
164 reserve_sectors = COPYGC_SECTORS_PER_ITER(ca);
166 trace_bcache_moving_gc_start(ca);
169 * Find buckets with lowest sector counts, skipping completely
170 * empty buckets, by building a maxheap sorted by sector count,
171 * and repeatedly replacing the maximum element until all
172 * buckets have been visited.
176 * We need bucket marks to be up to date, so gc can't be recalculating
177 * them, and we don't want the allocator invalidating a bucket after
178 * we've decided to evacuate it but before we set copygc:
180 down_read(&c->gc_lock);
181 mutex_lock(&ca->heap_lock);
182 mutex_lock(&ca->set->bucket_lock);
185 for_each_bucket(g, ca) {
186 bucket_cmpxchg(g, new, new.copygc = 0);
188 if (bucket_unused(g)) {
193 if (g->mark.owned_by_allocator ||
197 sectors_used = bucket_sectors_used(g);
199 if (sectors_used >= ca->mi.bucket_size)
202 bucket_heap_push(ca, g, sectors_used);
206 for (i = 0; i < ca->heap.used; i++)
207 sectors_to_move += ca->heap.data[i].val;
209 while (sectors_to_move > COPYGC_SECTORS_PER_ITER(ca)) {
210 BUG_ON(!heap_pop(&ca->heap, e, bucket_min_cmp));
211 sectors_to_move -= e.val;
214 for (i = 0; i < ca->heap.used; i++)
215 bucket_cmpxchg(ca->heap.data[i].g, new, new.copygc = 1);
217 buckets_to_move = ca->heap.used;
219 mutex_unlock(&ca->set->bucket_lock);
220 mutex_unlock(&ca->heap_lock);
221 up_read(&c->gc_lock);
223 read_moving(ca, buckets_to_move, sectors_to_move);
226 static int bch_moving_gc_thread(void *arg)
228 struct cache *ca = arg;
229 struct cache_set *c = ca->set;
230 struct io_clock *clock = &c->io_clock[WRITE];
232 u64 available, want, next;
236 while (!kthread_should_stop()) {
237 if (kthread_wait_freezable(c->copy_gc_enabled))
240 last = atomic_long_read(&clock->now);
242 * don't start copygc until less than half the gc reserve is
245 available = buckets_available_cache(ca);
246 want = div64_u64((ca->mi.nbuckets - ca->mi.first_bucket) *
247 c->opts.gc_reserve_percent, 200);
248 if (available > want) {
249 next = last + (available - want) *
251 bch_kthread_io_clock_wait(clock, next);
261 void bch_moving_init_cache(struct cache *ca)
263 bch_pd_controller_init(&ca->moving_gc_pd);
264 ca->moving_gc_pd.d_term = 0;
267 int bch_moving_gc_thread_start(struct cache *ca)
269 struct task_struct *t;
271 /* The moving gc read thread must be stopped */
272 BUG_ON(ca->moving_gc_read != NULL);
274 if (ca->set->opts.nochanges)
277 if (bch_fs_init_fault("moving_gc_start"))
280 t = kthread_create(bch_moving_gc_thread, ca, "bch_copygc_read");
284 ca->moving_gc_read = t;
285 wake_up_process(ca->moving_gc_read);
290 void bch_moving_gc_stop(struct cache *ca)
292 ca->moving_gc_pd.rate.rate = UINT_MAX;
293 bch_ratelimit_reset(&ca->moving_gc_pd.rate);
295 if (ca->moving_gc_read)
296 kthread_stop(ca->moving_gc_read);
297 ca->moving_gc_read = NULL;