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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "librbd/io/QosImageDispatch.h"
#include "common/dout.h"
#include "librbd/AsioEngine.h"
#include "librbd/ImageCtx.h"
#include "librbd/io/FlushTracker.h"
#include <map>
#define dout_subsys ceph_subsys_rbd
#undef dout_prefix
#define dout_prefix *_dout << "librbd::io::QosImageDispatch: " << this << " " \
<< __func__ << ": "
namespace librbd {
namespace io {
namespace {
uint64_t get_extent_length(const Extents& extents) {
uint64_t length = 0;
for (auto& extent : extents) {
length += extent.second;
}
return length;
}
uint64_t calculate_tokens(bool read_op, uint64_t extent_length, uint64_t flag) {
if (read_op && ((flag & IMAGE_DISPATCH_FLAG_QOS_WRITE_MASK) != 0)) {
return 0;
} else if (!read_op && ((flag & IMAGE_DISPATCH_FLAG_QOS_READ_MASK) != 0)) {
return 0;
}
return (((flag & IMAGE_DISPATCH_FLAG_QOS_BPS_MASK) != 0) ? extent_length : 1);
}
static std::map<uint64_t, std::string> throttle_flags = {
{IMAGE_DISPATCH_FLAG_QOS_IOPS_THROTTLE, "rbd_qos_iops_throttle" },
{IMAGE_DISPATCH_FLAG_QOS_BPS_THROTTLE, "rbd_qos_bps_throttle" },
{IMAGE_DISPATCH_FLAG_QOS_READ_IOPS_THROTTLE, "rbd_qos_read_iops_throttle" },
{IMAGE_DISPATCH_FLAG_QOS_WRITE_IOPS_THROTTLE, "rbd_qos_write_iops_throttle" },
{IMAGE_DISPATCH_FLAG_QOS_READ_BPS_THROTTLE, "rbd_qos_read_bps_throttle" },
{IMAGE_DISPATCH_FLAG_QOS_WRITE_BPS_THROTTLE, "rbd_qos_write_bps_throttle" }
};
} // anonymous namespace
template <typename I>
QosImageDispatch<I>::QosImageDispatch(I* image_ctx)
: m_image_ctx(image_ctx), m_flush_tracker(new FlushTracker<I>(image_ctx)) {
auto cct = m_image_ctx->cct;
ldout(cct, 5) << "ictx=" << image_ctx << dendl;
SafeTimer *timer;
ceph::mutex *timer_lock;
ImageCtx::get_timer_instance(cct, &timer, &timer_lock);
for (auto flag : throttle_flags) {
m_throttles.push_back(make_pair(
flag.first,
new TokenBucketThrottle(cct, flag.second, 0, 0, timer, timer_lock)));
}
}
template <typename I>
QosImageDispatch<I>::~QosImageDispatch() {
for (auto t : m_throttles) {
delete t.second;
}
delete m_flush_tracker;
}
template <typename I>
void QosImageDispatch<I>::shut_down(Context* on_finish) {
m_flush_tracker->shut_down();
on_finish->complete(0);
}
template <typename I>
void QosImageDispatch<I>::apply_qos_schedule_tick_min(uint64_t tick) {
for (auto pair : m_throttles) {
pair.second->set_schedule_tick_min(tick);
}
}
template <typename I>
void QosImageDispatch<I>::apply_qos_limit(uint64_t flag, uint64_t limit,
uint64_t burst, uint64_t burst_seconds) {
auto cct = m_image_ctx->cct;
TokenBucketThrottle *throttle = nullptr;
for (auto pair : m_throttles) {
if (flag == pair.first) {
throttle = pair.second;
break;
}
}
ceph_assert(throttle != nullptr);
int r = throttle->set_limit(limit, burst, burst_seconds);
if (r < 0) {
lderr(cct) << throttle->get_name() << ": invalid qos parameter: "
<< "burst(" << burst << ") is less than "
<< "limit(" << limit << ")" << dendl;
// if apply failed, we should at least make sure the limit works.
throttle->set_limit(limit, 0, 1);
}
if (limit) {
m_qos_enabled_flag |= flag;
} else {
m_qos_enabled_flag &= ~flag;
}
}
template <typename I>
bool QosImageDispatch<I>::read(
AioCompletion* aio_comp, Extents &&image_extents, ReadResult &&read_result,
IOContext io_context, int op_flags, int read_flags,
const ZTracer::Trace &parent_trace, uint64_t tid,
std::atomic<uint32_t>* image_dispatch_flags,
DispatchResult* dispatch_result, Context** on_finish,
Context* on_dispatched) {
auto cct = m_image_ctx->cct;
ldout(cct, 20) << "tid=" << tid << ", image_extents=" << image_extents
<< dendl;
if (needs_throttle(true, image_extents, tid, image_dispatch_flags,
dispatch_result, on_finish, on_dispatched)) {
return true;
}
return false;
}
template <typename I>
bool QosImageDispatch<I>::write(
AioCompletion* aio_comp, Extents &&image_extents, bufferlist &&bl,
IOContext io_context, int op_flags, const ZTracer::Trace &parent_trace,
uint64_t tid, std::atomic<uint32_t>* image_dispatch_flags,
DispatchResult* dispatch_result, Context** on_finish,
Context* on_dispatched) {
auto cct = m_image_ctx->cct;
ldout(cct, 20) << "tid=" << tid << ", image_extents=" << image_extents
<< dendl;
if (needs_throttle(false, image_extents, tid, image_dispatch_flags,
dispatch_result, on_finish, on_dispatched)) {
return true;
}
return false;
}
template <typename I>
bool QosImageDispatch<I>::discard(
AioCompletion* aio_comp, Extents &&image_extents,
uint32_t discard_granularity_bytes, IOContext io_context,
const ZTracer::Trace &parent_trace, uint64_t tid,
std::atomic<uint32_t>* image_dispatch_flags,
DispatchResult* dispatch_result, Context** on_finish,
Context* on_dispatched) {
auto cct = m_image_ctx->cct;
ldout(cct, 20) << "tid=" << tid << ", image_extents=" << image_extents
<< dendl;
if (needs_throttle(false, image_extents, tid, image_dispatch_flags,
dispatch_result, on_finish, on_dispatched)) {
return true;
}
return false;
}
template <typename I>
bool QosImageDispatch<I>::write_same(
AioCompletion* aio_comp, Extents &&image_extents, bufferlist &&bl,
IOContext io_context, int op_flags, const ZTracer::Trace &parent_trace,
uint64_t tid, std::atomic<uint32_t>* image_dispatch_flags,
DispatchResult* dispatch_result, Context** on_finish,
Context* on_dispatched) {
auto cct = m_image_ctx->cct;
ldout(cct, 20) << "tid=" << tid << ", image_extents=" << image_extents
<< dendl;
if (needs_throttle(false, image_extents, tid, image_dispatch_flags,
dispatch_result, on_finish, on_dispatched)) {
return true;
}
return false;
}
template <typename I>
bool QosImageDispatch<I>::compare_and_write(
AioCompletion* aio_comp, Extents &&image_extents, bufferlist &&cmp_bl,
bufferlist &&bl, uint64_t *mismatch_offset, IOContext io_context,
int op_flags, const ZTracer::Trace &parent_trace, uint64_t tid,
std::atomic<uint32_t>* image_dispatch_flags,
DispatchResult* dispatch_result, Context** on_finish,
Context* on_dispatched) {
auto cct = m_image_ctx->cct;
ldout(cct, 20) << "tid=" << tid << ", image_extents=" << image_extents
<< dendl;
if (needs_throttle(false, image_extents, tid, image_dispatch_flags,
dispatch_result, on_finish, on_dispatched)) {
return true;
}
return false;
}
template <typename I>
bool QosImageDispatch<I>::flush(
AioCompletion* aio_comp, FlushSource flush_source,
const ZTracer::Trace &parent_trace, uint64_t tid,
std::atomic<uint32_t>* image_dispatch_flags,
DispatchResult* dispatch_result, Context** on_finish,
Context* on_dispatched) {
auto cct = m_image_ctx->cct;
ldout(cct, 20) << "tid=" << tid << dendl;
*dispatch_result = DISPATCH_RESULT_CONTINUE;
m_flush_tracker->flush(on_dispatched);
return true;
}
template <typename I>
void QosImageDispatch<I>::handle_finished(int r, uint64_t tid) {
auto cct = m_image_ctx->cct;
ldout(cct, 20) << "tid=" << tid << dendl;
m_flush_tracker->finish_io(tid);
}
template <typename I>
bool QosImageDispatch<I>::set_throttle_flag(
std::atomic<uint32_t>* image_dispatch_flags, uint32_t flag) {
uint32_t expected = image_dispatch_flags->load();
uint32_t desired;
do {
desired = expected | flag;
} while (!image_dispatch_flags->compare_exchange_weak(expected, desired));
return ((desired & IMAGE_DISPATCH_FLAG_QOS_MASK) ==
IMAGE_DISPATCH_FLAG_QOS_MASK);
}
template <typename I>
bool QosImageDispatch<I>::needs_throttle(
bool read_op, const Extents& image_extents, uint64_t tid,
std::atomic<uint32_t>* image_dispatch_flags,
DispatchResult* dispatch_result, Context** on_finish,
Context* on_dispatched) {
auto cct = m_image_ctx->cct;
auto extent_length = get_extent_length(image_extents);
bool all_qos_flags_set = false;
if (!read_op) {
m_flush_tracker->start_io(tid);
*on_finish = new LambdaContext([this, tid, on_finish=*on_finish](int r) {
handle_finished(r, tid);
on_finish->complete(r);
});
}
*dispatch_result = DISPATCH_RESULT_CONTINUE;
auto qos_enabled_flag = m_qos_enabled_flag;
for (auto [flag, throttle] : m_throttles) {
if ((qos_enabled_flag & flag) == 0) {
all_qos_flags_set = set_throttle_flag(image_dispatch_flags, flag);
continue;
}
auto tokens = calculate_tokens(read_op, extent_length, flag);
if (tokens > 0 &&
throttle->get(tokens, this, &QosImageDispatch<I>::handle_throttle_ready,
Tag{image_dispatch_flags, on_dispatched}, flag)) {
ldout(cct, 15) << "on_dispatched=" << on_dispatched << ", "
<< "flag=" << flag << dendl;
all_qos_flags_set = false;
} else {
all_qos_flags_set = set_throttle_flag(image_dispatch_flags, flag);
}
}
return !all_qos_flags_set;
}
template <typename I>
void QosImageDispatch<I>::handle_throttle_ready(Tag&& tag, uint64_t flag) {
auto cct = m_image_ctx->cct;
ldout(cct, 15) << "on_dispatched=" << tag.on_dispatched << ", "
<< "flag=" << flag << dendl;
if (set_throttle_flag(tag.image_dispatch_flags, flag)) {
// timer_lock is held -- so dispatch from outside the timer thread
m_image_ctx->asio_engine->post(tag.on_dispatched, 0);
}
}
} // namespace io
} // namespace librbd
template class librbd::io::QosImageDispatch<librbd::ImageCtx>;
|