movinggc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.c->moving_gc_keys, io->w);
  46. up(&io->s.c->moving_in_flight);
  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.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->write_prio = 1;
  79. s->cache_bio = &io->bio.bio;
  80. s->writeback = KEY_DIRTY(&io->w->key);
  81. s->csum = KEY_CSUM(&io->w->key);
  82. bkey_copy(&s->replace_key, &io->w->key);
  83. s->replace = true;
  84. closure_init(&s->btree, cl);
  85. bch_data_insert(&s->btree);
  86. }
  87. continue_at(cl, write_moving_finish, system_wq);
  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->c, &io->w->key, 0);
  95. continue_at(cl, write_moving, system_wq);
  96. }
  97. static void read_moving(struct cache_set *c)
  98. {
  99. struct keybuf_key *w;
  100. struct moving_io *io;
  101. struct bio *bio;
  102. struct closure cl;
  103. closure_init_stack(&cl);
  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.inode = KEY_INODE(&w->key);
  118. io->s.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. down(&c->moving_in_flight);
  127. closure_call(&io->s.cl, read_moving_submit, NULL, &cl);
  128. }
  129. if (0) {
  130. err: if (!IS_ERR_OR_NULL(w->private))
  131. kfree(w->private);
  132. bch_keybuf_del(&c->moving_gc_keys, w);
  133. }
  134. closure_sync(&cl);
  135. }
  136. static bool bucket_cmp(struct bucket *l, struct bucket *r)
  137. {
  138. return GC_SECTORS_USED(l) < GC_SECTORS_USED(r);
  139. }
  140. static unsigned bucket_heap_top(struct cache *ca)
  141. {
  142. return GC_SECTORS_USED(heap_peek(&ca->heap));
  143. }
  144. void bch_moving_gc(struct cache_set *c)
  145. {
  146. struct cache *ca;
  147. struct bucket *b;
  148. unsigned i;
  149. if (!c->copy_gc_enabled)
  150. return;
  151. mutex_lock(&c->bucket_lock);
  152. for_each_cache(ca, c, i) {
  153. unsigned sectors_to_move = 0;
  154. unsigned reserve_sectors = ca->sb.bucket_size *
  155. min(fifo_used(&ca->free), ca->free.size / 2);
  156. ca->heap.used = 0;
  157. for_each_bucket(b, ca) {
  158. if (!GC_SECTORS_USED(b))
  159. continue;
  160. if (!heap_full(&ca->heap)) {
  161. sectors_to_move += GC_SECTORS_USED(b);
  162. heap_add(&ca->heap, b, bucket_cmp);
  163. } else if (bucket_cmp(b, heap_peek(&ca->heap))) {
  164. sectors_to_move -= bucket_heap_top(ca);
  165. sectors_to_move += GC_SECTORS_USED(b);
  166. ca->heap.data[0] = b;
  167. heap_sift(&ca->heap, 0, bucket_cmp);
  168. }
  169. }
  170. while (sectors_to_move > reserve_sectors) {
  171. heap_pop(&ca->heap, b, bucket_cmp);
  172. sectors_to_move -= GC_SECTORS_USED(b);
  173. }
  174. ca->gc_move_threshold = bucket_heap_top(ca);
  175. pr_debug("threshold %u", ca->gc_move_threshold);
  176. }
  177. mutex_unlock(&c->bucket_lock);
  178. c->moving_gc_keys.last_scanned = ZERO_KEY;
  179. read_moving(c);
  180. }
  181. void bch_moving_init_cache_set(struct cache_set *c)
  182. {
  183. bch_keybuf_init(&c->moving_gc_keys);
  184. sema_init(&c->moving_in_flight, 64);
  185. }