movinggc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Moving/copying garbage collector
  3. *
  4. * Copyright 2012 Google, Inc.
  5. */
  6. #include "bcache.h"
  7. #include "btree.h"
  8. #include "debug.h"
  9. #include "request.h"
  10. #include <trace/events/bcache.h>
  11. struct moving_io {
  12. struct keybuf_key *w;
  13. struct search s;
  14. struct bbio bio;
  15. };
  16. static bool moving_pred(struct keybuf *buf, struct bkey *k)
  17. {
  18. struct cache_set *c = container_of(buf, struct cache_set,
  19. moving_gc_keys);
  20. unsigned i;
  21. for (i = 0; i < KEY_PTRS(k); i++) {
  22. struct cache *ca = PTR_CACHE(c, k, i);
  23. struct bucket *g = PTR_BUCKET(c, k, i);
  24. if (GC_SECTORS_USED(g) < ca->gc_move_threshold)
  25. return true;
  26. }
  27. return false;
  28. }
  29. /* Moving GC - IO loop */
  30. static void moving_io_destructor(struct closure *cl)
  31. {
  32. struct moving_io *io = container_of(cl, struct moving_io, s.cl);
  33. kfree(io);
  34. }
  35. static void write_moving_finish(struct closure *cl)
  36. {
  37. struct moving_io *io = container_of(cl, struct moving_io, s.cl);
  38. struct bio *bio = &io->bio.bio;
  39. struct bio_vec *bv = bio_iovec_idx(bio, bio->bi_vcnt);
  40. while (bv-- != bio->bi_io_vec)
  41. __free_page(bv->bv_page);
  42. if (io->s.op.insert_collision)
  43. trace_bcache_gc_copy_collision(&io->w->key);
  44. bch_keybuf_del(&io->s.op.c->moving_gc_keys, io->w);
  45. atomic_dec_bug(&io->s.op.c->in_flight);
  46. closure_wake_up(&io->s.op.c->moving_gc_wait);
  47. closure_return_with_destructor(cl, moving_io_destructor);
  48. }
  49. static void read_moving_endio(struct bio *bio, int error)
  50. {
  51. struct moving_io *io = container_of(bio->bi_private,
  52. struct moving_io, s.cl);
  53. if (error)
  54. io->s.error = error;
  55. bch_bbio_endio(io->s.op.c, bio, error, "reading data to move");
  56. }
  57. static void moving_init(struct moving_io *io)
  58. {
  59. struct bio *bio = &io->bio.bio;
  60. bio_init(bio);
  61. bio_get(bio);
  62. bio_set_prio(bio, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
  63. bio->bi_size = KEY_SIZE(&io->w->key) << 9;
  64. bio->bi_max_vecs = DIV_ROUND_UP(KEY_SIZE(&io->w->key),
  65. PAGE_SECTORS);
  66. bio->bi_private = &io->s.cl;
  67. bio->bi_io_vec = bio->bi_inline_vecs;
  68. bch_bio_map(bio, NULL);
  69. }
  70. static void write_moving(struct closure *cl)
  71. {
  72. struct search *s = container_of(cl, struct search, cl);
  73. struct moving_io *io = container_of(s, struct moving_io, s);
  74. if (!s->error) {
  75. moving_init(io);
  76. io->bio.bio.bi_sector = KEY_START(&io->w->key);
  77. s->op.lock = -1;
  78. s->op.write_prio = 1;
  79. s->op.cache_bio = &io->bio.bio;
  80. s->writeback = KEY_DIRTY(&io->w->key);
  81. s->op.csum = KEY_CSUM(&io->w->key);
  82. s->op.type = BTREE_REPLACE;
  83. bkey_copy(&s->op.replace, &io->w->key);
  84. closure_init(&s->op.cl, cl);
  85. bch_insert_data(&s->op.cl);
  86. }
  87. continue_at(cl, write_moving_finish, NULL);
  88. }
  89. static void read_moving_submit(struct closure *cl)
  90. {
  91. struct search *s = container_of(cl, struct search, cl);
  92. struct moving_io *io = container_of(s, struct moving_io, s);
  93. struct bio *bio = &io->bio.bio;
  94. bch_submit_bbio(bio, s->op.c, &io->w->key, 0);
  95. continue_at(cl, write_moving, bch_gc_wq);
  96. }
  97. static void read_moving(struct closure *cl)
  98. {
  99. struct cache_set *c = container_of(cl, struct cache_set, moving_gc);
  100. struct keybuf_key *w;
  101. struct moving_io *io;
  102. struct bio *bio;
  103. /* XXX: if we error, background writeback could stall indefinitely */
  104. while (!test_bit(CACHE_SET_STOPPING, &c->flags)) {
  105. w = bch_keybuf_next_rescan(c, &c->moving_gc_keys,
  106. &MAX_KEY, moving_pred);
  107. if (!w)
  108. break;
  109. io = kzalloc(sizeof(struct moving_io) + sizeof(struct bio_vec)
  110. * DIV_ROUND_UP(KEY_SIZE(&w->key), PAGE_SECTORS),
  111. GFP_KERNEL);
  112. if (!io)
  113. goto err;
  114. w->private = io;
  115. io->w = w;
  116. io->s.op.inode = KEY_INODE(&w->key);
  117. io->s.op.c = c;
  118. moving_init(io);
  119. bio = &io->bio.bio;
  120. bio->bi_rw = READ;
  121. bio->bi_end_io = read_moving_endio;
  122. if (bch_bio_alloc_pages(bio, GFP_KERNEL))
  123. goto err;
  124. trace_bcache_gc_copy(&w->key);
  125. closure_call(&io->s.cl, read_moving_submit, NULL, &c->gc.cl);
  126. if (atomic_inc_return(&c->in_flight) >= 64) {
  127. closure_wait_event(&c->moving_gc_wait, cl,
  128. atomic_read(&c->in_flight) < 64);
  129. continue_at(cl, read_moving, bch_gc_wq);
  130. }
  131. }
  132. if (0) {
  133. err: if (!IS_ERR_OR_NULL(w->private))
  134. kfree(w->private);
  135. bch_keybuf_del(&c->moving_gc_keys, w);
  136. }
  137. closure_return(cl);
  138. }
  139. static bool bucket_cmp(struct bucket *l, struct bucket *r)
  140. {
  141. return GC_SECTORS_USED(l) < GC_SECTORS_USED(r);
  142. }
  143. static unsigned bucket_heap_top(struct cache *ca)
  144. {
  145. return GC_SECTORS_USED(heap_peek(&ca->heap));
  146. }
  147. void bch_moving_gc(struct closure *cl)
  148. {
  149. struct cache_set *c = container_of(cl, struct cache_set, gc.cl);
  150. struct cache *ca;
  151. struct bucket *b;
  152. unsigned i;
  153. if (!c->copy_gc_enabled)
  154. closure_return(cl);
  155. mutex_lock(&c->bucket_lock);
  156. for_each_cache(ca, c, i) {
  157. unsigned sectors_to_move = 0;
  158. unsigned reserve_sectors = ca->sb.bucket_size *
  159. min(fifo_used(&ca->free), ca->free.size / 2);
  160. ca->heap.used = 0;
  161. for_each_bucket(b, ca) {
  162. if (!GC_SECTORS_USED(b))
  163. continue;
  164. if (!heap_full(&ca->heap)) {
  165. sectors_to_move += GC_SECTORS_USED(b);
  166. heap_add(&ca->heap, b, bucket_cmp);
  167. } else if (bucket_cmp(b, heap_peek(&ca->heap))) {
  168. sectors_to_move -= bucket_heap_top(ca);
  169. sectors_to_move += GC_SECTORS_USED(b);
  170. ca->heap.data[0] = b;
  171. heap_sift(&ca->heap, 0, bucket_cmp);
  172. }
  173. }
  174. while (sectors_to_move > reserve_sectors) {
  175. heap_pop(&ca->heap, b, bucket_cmp);
  176. sectors_to_move -= GC_SECTORS_USED(b);
  177. }
  178. ca->gc_move_threshold = bucket_heap_top(ca);
  179. pr_debug("threshold %u", ca->gc_move_threshold);
  180. }
  181. mutex_unlock(&c->bucket_lock);
  182. c->moving_gc_keys.last_scanned = ZERO_KEY;
  183. closure_init(&c->moving_gc, cl);
  184. read_moving(&c->moving_gc);
  185. closure_return(cl);
  186. }
  187. void bch_moving_init_cache_set(struct cache_set *c)
  188. {
  189. bch_keybuf_init(&c->moving_gc_keys);
  190. }