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. struct moving_io {
  11. struct keybuf_key *w;
  12. struct search s;
  13. struct bbio bio;
  14. };
  15. static bool moving_pred(struct keybuf *buf, struct bkey *k)
  16. {
  17. struct cache_set *c = container_of(buf, struct cache_set,
  18. moving_gc_keys);
  19. unsigned i;
  20. for (i = 0; i < KEY_PTRS(k); i++) {
  21. struct cache *ca = PTR_CACHE(c, k, i);
  22. struct bucket *g = PTR_BUCKET(c, k, i);
  23. if (GC_SECTORS_USED(g) < ca->gc_move_threshold)
  24. return true;
  25. }
  26. return false;
  27. }
  28. /* Moving GC - IO loop */
  29. static void moving_io_destructor(struct closure *cl)
  30. {
  31. struct moving_io *io = container_of(cl, struct moving_io, s.cl);
  32. kfree(io);
  33. }
  34. static void write_moving_finish(struct closure *cl)
  35. {
  36. struct moving_io *io = container_of(cl, struct moving_io, s.cl);
  37. struct bio *bio = &io->bio.bio;
  38. struct bio_vec *bv = bio_iovec_idx(bio, bio->bi_vcnt);
  39. while (bv-- != bio->bi_io_vec)
  40. __free_page(bv->bv_page);
  41. pr_debug("%s %s", io->s.op.insert_collision
  42. ? "collision moving" : "moved",
  43. pkey(&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. trace_bcache_write_moving(&io->bio.bio);
  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. trace_bcache_read_moving(bio);
  96. bch_submit_bbio(bio, s->op.c, &io->w->key, 0);
  97. continue_at(cl, write_moving, bch_gc_wq);
  98. }
  99. static void read_moving(struct closure *cl)
  100. {
  101. struct cache_set *c = container_of(cl, struct cache_set, moving_gc);
  102. struct keybuf_key *w;
  103. struct moving_io *io;
  104. struct bio *bio;
  105. /* XXX: if we error, background writeback could stall indefinitely */
  106. while (!test_bit(CACHE_SET_STOPPING, &c->flags)) {
  107. w = bch_keybuf_next_rescan(c, &c->moving_gc_keys, &MAX_KEY);
  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 (bch_bio_alloc_pages(bio, GFP_KERNEL))
  124. goto err;
  125. pr_debug("%s", pkey(&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, moving_pred);
  191. }