1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
#include "bcachefs.h"
#include "alloc.h"
#include "btree_iter.h"
#include "buckets.h"
#include "clock.h"
#include "extents.h"
#include "io.h"
#include "keylist.h"
#include "move.h"
#include "super-io.h"
#include "tier.h"
#include <linux/freezer.h>
#include <linux/kthread.h>
#include <trace/events/bcachefs.h>
struct tiering_state {
struct bch_tier *tier;
unsigned sectors;
unsigned stripe_size;
unsigned dev_idx;
struct bch_dev *ca;
};
static bool tiering_pred(struct bch_fs *c,
struct tiering_state *s,
struct bkey_s_c k)
{
if (bkey_extent_is_data(k.k)) {
struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
const struct bch_extent_ptr *ptr;
unsigned replicas = 0;
/* Make sure we have room to add a new pointer: */
if (bkey_val_u64s(e.k) + BKEY_EXTENT_PTR_U64s_MAX >
BKEY_EXTENT_VAL_U64s_MAX)
return false;
extent_for_each_ptr(e, ptr)
if (c->devs[ptr->dev]->mi.tier >= s->tier->idx)
replicas++;
return replicas < c->opts.data_replicas;
}
return false;
}
static void tier_put_device(struct tiering_state *s)
{
if (s->ca)
percpu_ref_put(&s->ca->io_ref);
s->ca = NULL;
}
/**
* refill_next - move on to refilling the next cache's tiering keylist
*/
static void tier_next_device(struct bch_fs *c, struct tiering_state *s)
{
if (!s->ca || s->sectors > s->stripe_size) {
tier_put_device(s);
s->sectors = 0;
s->dev_idx++;
spin_lock(&s->tier->devs.lock);
if (s->dev_idx >= s->tier->devs.nr)
s->dev_idx = 0;
if (s->tier->devs.nr) {
s->ca = s->tier->devs.d[s->dev_idx].dev;
percpu_ref_get(&s->ca->io_ref);
}
spin_unlock(&s->tier->devs.lock);
}
}
static int issue_tiering_move(struct bch_fs *c,
struct tiering_state *s,
struct moving_context *ctxt,
struct bkey_s_c k)
{
int ret;
ret = bch2_data_move(c, ctxt, &s->ca->tiering_write_point, k, NULL);
if (!ret) {
trace_tiering_copy(k.k);
s->sectors += k.k->size;
} else {
trace_tiering_alloc_fail(c, k.k->size);
}
return ret;
}
/**
* tiering_next_cache - issue a move to write an extent to the next cache
* device in round robin order
*/
static s64 read_tiering(struct bch_fs *c, struct bch_tier *tier)
{
struct moving_context ctxt;
struct tiering_state s;
struct btree_iter iter;
struct bkey_s_c k;
unsigned nr_devices = READ_ONCE(tier->devs.nr);
int ret;
if (!nr_devices)
return 0;
trace_tiering_start(c);
memset(&s, 0, sizeof(s));
s.tier = tier;
s.stripe_size = 2048; /* 1 mb for now */
bch2_move_ctxt_init(&ctxt, &tier->pd.rate,
nr_devices * SECTORS_IN_FLIGHT_PER_DEVICE);
bch2_btree_iter_init(&iter, c, BTREE_ID_EXTENTS, POS_MIN);
while (!kthread_should_stop() &&
!bch2_move_ctxt_wait(&ctxt) &&
(k = bch2_btree_iter_peek(&iter)).k &&
!btree_iter_err(k)) {
if (!tiering_pred(c, &s, k))
goto next;
tier_next_device(c, &s);
if (!s.ca)
break;
ret = issue_tiering_move(c, &s, &ctxt, k);
if (ret) {
bch2_btree_iter_unlock(&iter);
/* memory allocation failure, wait for some IO to finish */
bch2_move_ctxt_wait_for_io(&ctxt);
continue;
}
next:
bch2_btree_iter_advance_pos(&iter);
//bch2_btree_iter_cond_resched(&iter);
/* unlock before calling moving_context_wait() */
bch2_btree_iter_unlock(&iter);
cond_resched();
}
bch2_btree_iter_unlock(&iter);
tier_put_device(&s);
bch2_move_ctxt_exit(&ctxt);
trace_tiering_end(c, ctxt.sectors_moved, ctxt.keys_moved);
return ctxt.sectors_moved;
}
static int bch2_tiering_thread(void *arg)
{
struct bch_tier *tier = arg;
struct bch_fs *c = container_of(tier, struct bch_fs, tiers[tier->idx]);
struct io_clock *clock = &c->io_clock[WRITE];
struct bch_dev *ca;
u64 tier_capacity, available_sectors;
unsigned long last;
unsigned i;
set_freezable();
while (!kthread_should_stop()) {
if (kthread_wait_freezable(c->tiering_enabled &&
tier->devs.nr))
break;
while (1) {
struct bch_tier *faster_tier;
last = atomic_long_read(&clock->now);
tier_capacity = available_sectors = 0;
for (faster_tier = c->tiers;
faster_tier != tier;
faster_tier++) {
spin_lock(&faster_tier->devs.lock);
group_for_each_dev(ca, &faster_tier->devs, i) {
tier_capacity +=
(ca->mi.nbuckets -
ca->mi.first_bucket) << ca->bucket_bits;
available_sectors +=
dev_buckets_available(ca) << ca->bucket_bits;
}
spin_unlock(&faster_tier->devs.lock);
}
if (available_sectors < (tier_capacity >> 1))
break;
bch2_kthread_io_clock_wait(clock,
last +
available_sectors -
(tier_capacity >> 1));
if (kthread_should_stop())
return 0;
}
read_tiering(c, tier);
}
return 0;
}
static void __bch2_tiering_stop(struct bch_tier *tier)
{
tier->pd.rate.rate = UINT_MAX;
bch2_ratelimit_reset(&tier->pd.rate);
if (tier->migrate)
kthread_stop(tier->migrate);
tier->migrate = NULL;
}
void bch2_tiering_stop(struct bch_fs *c)
{
struct bch_tier *tier;
for (tier = c->tiers; tier < c->tiers + ARRAY_SIZE(c->tiers); tier++)
__bch2_tiering_stop(tier);
}
static int __bch2_tiering_start(struct bch_tier *tier)
{
if (!tier->migrate) {
struct task_struct *p =
kthread_create(bch2_tiering_thread, tier,
"bch_tier[%u]", tier->idx);
if (IS_ERR(p))
return PTR_ERR(p);
tier->migrate = p;
}
wake_up_process(tier->migrate);
return 0;
}
int bch2_tiering_start(struct bch_fs *c)
{
struct bch_tier *tier;
bool have_faster_tier = false;
if (c->opts.nochanges)
return 0;
for (tier = c->tiers; tier < c->tiers + ARRAY_SIZE(c->tiers); tier++) {
if (!tier->devs.nr)
continue;
if (have_faster_tier) {
int ret = __bch2_tiering_start(tier);
if (ret)
return ret;
} else {
__bch2_tiering_stop(tier);
}
have_faster_tier = true;
}
return 0;
}
void bch2_fs_tiering_init(struct bch_fs *c)
{
unsigned i;
for (i = 0; i < ARRAY_SIZE(c->tiers); i++) {
c->tiers[i].idx = i;
bch2_pd_controller_init(&c->tiers[i].pd);
}
}
|