movinggc.c 5.6 KB

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