]>
Commit | Line | Data |
---|---|---|
2ff1f2e3 AG |
1 | /* |
2 | * QEMU block throttling group infrastructure | |
3 | * | |
4 | * Copyright (C) Nodalink, EURL. 2014 | |
5 | * Copyright (C) Igalia, S.L. 2015 | |
6 | * | |
7 | * Authors: | |
8 | * BenoƮt Canet <benoit.canet@nodalink.com> | |
9 | * Alberto Garcia <berto@igalia.com> | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU General Public License as | |
13 | * published by the Free Software Foundation; either version 2 or | |
14 | * (at your option) version 3 of the License. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
23 | */ | |
24 | ||
80c71a24 | 25 | #include "qemu/osdep.h" |
31dce3cc | 26 | #include "sysemu/block-backend.h" |
2ff1f2e3 | 27 | #include "block/throttle-groups.h" |
76f4afb4 AG |
28 | #include "qemu/queue.h" |
29 | #include "qemu/thread.h" | |
30 | #include "sysemu/qtest.h" | |
2ff1f2e3 AG |
31 | |
32 | /* The ThrottleGroup structure (with its ThrottleState) is shared | |
27ccdd52 | 33 | * among different BlockBackends and it's independent from |
2ff1f2e3 AG |
34 | * AioContext, so in order to use it from different threads it needs |
35 | * its own locking. | |
36 | * | |
37 | * This locking is however handled internally in this file, so it's | |
d87d01e1 | 38 | * transparent to outside users. |
2ff1f2e3 AG |
39 | * |
40 | * The whole ThrottleGroup structure is private and invisible to | |
41 | * outside users, that only use it through its ThrottleState. | |
42 | * | |
27ccdd52 | 43 | * In addition to the ThrottleGroup structure, BlockBackendPublic has |
2ff1f2e3 | 44 | * fields that need to be accessed by other members of the group and |
27ccdd52 KW |
45 | * therefore also need to be protected by this lock. Once a |
46 | * BlockBackend is registered in a group those fields can be accessed | |
47 | * by other threads any time. | |
2ff1f2e3 AG |
48 | * |
49 | * Again, all this is handled internally and is mostly transparent to | |
50 | * the outside. The 'throttle_timers' field however has an additional | |
51 | * constraint because it may be temporarily invalid (see for example | |
52 | * bdrv_set_aio_context()). Therefore in this file a thread will | |
27ccdd52 KW |
53 | * access some other BlockBackend's timers only after verifying that |
54 | * that BlockBackend has throttled requests in the queue. | |
2ff1f2e3 AG |
55 | */ |
56 | typedef struct ThrottleGroup { | |
57 | char *name; /* This is constant during the lifetime of the group */ | |
58 | ||
59 | QemuMutex lock; /* This lock protects the following four fields */ | |
60 | ThrottleState ts; | |
31dce3cc KW |
61 | QLIST_HEAD(, BlockBackendPublic) head; |
62 | BlockBackend *tokens[2]; | |
2ff1f2e3 AG |
63 | bool any_timer_armed[2]; |
64 | ||
65 | /* These two are protected by the global throttle_groups_lock */ | |
66 | unsigned refcount; | |
67 | QTAILQ_ENTRY(ThrottleGroup) list; | |
68 | } ThrottleGroup; | |
69 | ||
70 | static QemuMutex throttle_groups_lock; | |
71 | static QTAILQ_HEAD(, ThrottleGroup) throttle_groups = | |
72 | QTAILQ_HEAD_INITIALIZER(throttle_groups); | |
73 | ||
74 | /* Increments the reference count of a ThrottleGroup given its name. | |
75 | * | |
76 | * If no ThrottleGroup is found with the given name a new one is | |
77 | * created. | |
78 | * | |
79 | * @name: the name of the ThrottleGroup | |
973f2ddf | 80 | * @ret: the ThrottleState member of the ThrottleGroup |
2ff1f2e3 | 81 | */ |
973f2ddf | 82 | ThrottleState *throttle_group_incref(const char *name) |
2ff1f2e3 AG |
83 | { |
84 | ThrottleGroup *tg = NULL; | |
85 | ThrottleGroup *iter; | |
86 | ||
87 | qemu_mutex_lock(&throttle_groups_lock); | |
88 | ||
89 | /* Look for an existing group with that name */ | |
90 | QTAILQ_FOREACH(iter, &throttle_groups, list) { | |
91 | if (!strcmp(name, iter->name)) { | |
92 | tg = iter; | |
93 | break; | |
94 | } | |
95 | } | |
96 | ||
97 | /* Create a new one if not found */ | |
98 | if (!tg) { | |
99 | tg = g_new0(ThrottleGroup, 1); | |
100 | tg->name = g_strdup(name); | |
101 | qemu_mutex_init(&tg->lock); | |
102 | throttle_init(&tg->ts); | |
103 | QLIST_INIT(&tg->head); | |
104 | ||
105 | QTAILQ_INSERT_TAIL(&throttle_groups, tg, list); | |
106 | } | |
107 | ||
108 | tg->refcount++; | |
109 | ||
110 | qemu_mutex_unlock(&throttle_groups_lock); | |
111 | ||
973f2ddf | 112 | return &tg->ts; |
2ff1f2e3 AG |
113 | } |
114 | ||
115 | /* Decrease the reference count of a ThrottleGroup. | |
116 | * | |
117 | * When the reference count reaches zero the ThrottleGroup is | |
118 | * destroyed. | |
119 | * | |
973f2ddf | 120 | * @ts: The ThrottleGroup to unref, given by its ThrottleState member |
2ff1f2e3 | 121 | */ |
973f2ddf | 122 | void throttle_group_unref(ThrottleState *ts) |
2ff1f2e3 | 123 | { |
973f2ddf HR |
124 | ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts); |
125 | ||
2ff1f2e3 AG |
126 | qemu_mutex_lock(&throttle_groups_lock); |
127 | if (--tg->refcount == 0) { | |
128 | QTAILQ_REMOVE(&throttle_groups, tg, list); | |
129 | qemu_mutex_destroy(&tg->lock); | |
130 | g_free(tg->name); | |
131 | g_free(tg); | |
132 | } | |
133 | qemu_mutex_unlock(&throttle_groups_lock); | |
134 | } | |
135 | ||
49d2165d KW |
136 | /* Get the name from a BlockBackend's ThrottleGroup. The name (and the pointer) |
137 | * is guaranteed to remain constant during the lifetime of the group. | |
2ff1f2e3 | 138 | * |
49d2165d | 139 | * @blk: a BlockBackend that is member of a throttling group |
2ff1f2e3 AG |
140 | * @ret: the name of the group. |
141 | */ | |
49d2165d | 142 | const char *throttle_group_get_name(BlockBackend *blk) |
2ff1f2e3 | 143 | { |
27ccdd52 KW |
144 | BlockBackendPublic *blkp = blk_get_public(blk); |
145 | ThrottleGroup *tg = container_of(blkp->throttle_state, ThrottleGroup, ts); | |
2ff1f2e3 AG |
146 | return tg->name; |
147 | } | |
148 | ||
31dce3cc KW |
149 | /* Return the next BlockBackend in the round-robin sequence, simulating a |
150 | * circular list. | |
2ff1f2e3 AG |
151 | * |
152 | * This assumes that tg->lock is held. | |
153 | * | |
31dce3cc KW |
154 | * @blk: the current BlockBackend |
155 | * @ret: the next BlockBackend in the sequence | |
2ff1f2e3 | 156 | */ |
31dce3cc | 157 | static BlockBackend *throttle_group_next_blk(BlockBackend *blk) |
2ff1f2e3 | 158 | { |
27ccdd52 KW |
159 | BlockBackendPublic *blkp = blk_get_public(blk); |
160 | ThrottleState *ts = blkp->throttle_state; | |
2ff1f2e3 | 161 | ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts); |
27ccdd52 | 162 | BlockBackendPublic *next = QLIST_NEXT(blkp, round_robin); |
2ff1f2e3 AG |
163 | |
164 | if (!next) { | |
31dce3cc | 165 | next = QLIST_FIRST(&tg->head); |
2ff1f2e3 AG |
166 | } |
167 | ||
31dce3cc | 168 | return blk_by_public(next); |
2ff1f2e3 AG |
169 | } |
170 | ||
6bf77e1c AG |
171 | /* |
172 | * Return whether a BlockBackend has pending requests. | |
173 | * | |
174 | * This assumes that tg->lock is held. | |
175 | * | |
176 | * @blk: the BlockBackend | |
177 | * @is_write: the type of operation (read/write) | |
178 | * @ret: whether the BlockBackend has pending requests. | |
179 | */ | |
180 | static inline bool blk_has_pending_reqs(BlockBackend *blk, | |
181 | bool is_write) | |
182 | { | |
183 | const BlockBackendPublic *blkp = blk_get_public(blk); | |
184 | return blkp->pending_reqs[is_write]; | |
185 | } | |
186 | ||
31dce3cc KW |
187 | /* Return the next BlockBackend in the round-robin sequence with pending I/O |
188 | * requests. | |
76f4afb4 AG |
189 | * |
190 | * This assumes that tg->lock is held. | |
191 | * | |
31dce3cc | 192 | * @blk: the current BlockBackend |
76f4afb4 | 193 | * @is_write: the type of operation (read/write) |
31dce3cc KW |
194 | * @ret: the next BlockBackend with pending requests, or blk if there is |
195 | * none. | |
76f4afb4 | 196 | */ |
31dce3cc | 197 | static BlockBackend *next_throttle_token(BlockBackend *blk, bool is_write) |
76f4afb4 | 198 | { |
27ccdd52 KW |
199 | BlockBackendPublic *blkp = blk_get_public(blk); |
200 | ThrottleGroup *tg = container_of(blkp->throttle_state, ThrottleGroup, ts); | |
31dce3cc | 201 | BlockBackend *token, *start; |
76f4afb4 AG |
202 | |
203 | start = token = tg->tokens[is_write]; | |
204 | ||
205 | /* get next bs round in round robin style */ | |
31dce3cc | 206 | token = throttle_group_next_blk(token); |
6bf77e1c | 207 | while (token != start && !blk_has_pending_reqs(token, is_write)) { |
31dce3cc | 208 | token = throttle_group_next_blk(token); |
76f4afb4 AG |
209 | } |
210 | ||
211 | /* If no IO are queued for scheduling on the next round robin token | |
212 | * then decide the token is the current bs because chances are | |
213 | * the current bs get the current request queued. | |
214 | */ | |
6bf77e1c | 215 | if (token == start && !blk_has_pending_reqs(token, is_write)) { |
31dce3cc | 216 | token = blk; |
76f4afb4 AG |
217 | } |
218 | ||
6bf77e1c AG |
219 | /* Either we return the original BB, or one with pending requests */ |
220 | assert(token == blk || blk_has_pending_reqs(token, is_write)); | |
221 | ||
76f4afb4 AG |
222 | return token; |
223 | } | |
224 | ||
31dce3cc KW |
225 | /* Check if the next I/O request for a BlockBackend needs to be throttled or |
226 | * not. If there's no timer set in this group, set one and update the token | |
227 | * accordingly. | |
76f4afb4 AG |
228 | * |
229 | * This assumes that tg->lock is held. | |
230 | * | |
31dce3cc | 231 | * @blk: the current BlockBackend |
76f4afb4 AG |
232 | * @is_write: the type of operation (read/write) |
233 | * @ret: whether the I/O request needs to be throttled or not | |
234 | */ | |
31dce3cc | 235 | static bool throttle_group_schedule_timer(BlockBackend *blk, bool is_write) |
76f4afb4 | 236 | { |
27ccdd52 KW |
237 | BlockBackendPublic *blkp = blk_get_public(blk); |
238 | ThrottleState *ts = blkp->throttle_state; | |
239 | ThrottleTimers *tt = &blkp->throttle_timers; | |
76f4afb4 AG |
240 | ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts); |
241 | bool must_wait; | |
242 | ||
d993b858 | 243 | if (atomic_read(&blkp->io_limits_disabled)) { |
ce0f1412 PB |
244 | return false; |
245 | } | |
246 | ||
76f4afb4 AG |
247 | /* Check if any of the timers in this group is already armed */ |
248 | if (tg->any_timer_armed[is_write]) { | |
249 | return true; | |
250 | } | |
251 | ||
252 | must_wait = throttle_schedule_timer(ts, tt, is_write); | |
253 | ||
31dce3cc | 254 | /* If a timer just got armed, set blk as the current token */ |
76f4afb4 | 255 | if (must_wait) { |
31dce3cc | 256 | tg->tokens[is_write] = blk; |
76f4afb4 AG |
257 | tg->any_timer_armed[is_write] = true; |
258 | } | |
259 | ||
260 | return must_wait; | |
261 | } | |
262 | ||
3b170dc8 PB |
263 | /* Start the next pending I/O request for a BlockBackend. Return whether |
264 | * any request was actually pending. | |
265 | * | |
266 | * @blk: the current BlockBackend | |
267 | * @is_write: the type of operation (read/write) | |
268 | */ | |
269 | static bool coroutine_fn throttle_group_co_restart_queue(BlockBackend *blk, | |
270 | bool is_write) | |
271 | { | |
272 | BlockBackendPublic *blkp = blk_get_public(blk); | |
273 | ||
274 | return qemu_co_queue_next(&blkp->throttled_reqs[is_write]); | |
275 | } | |
276 | ||
76f4afb4 AG |
277 | /* Look for the next pending I/O request and schedule it. |
278 | * | |
279 | * This assumes that tg->lock is held. | |
280 | * | |
31dce3cc | 281 | * @blk: the current BlockBackend |
76f4afb4 AG |
282 | * @is_write: the type of operation (read/write) |
283 | */ | |
31dce3cc | 284 | static void schedule_next_request(BlockBackend *blk, bool is_write) |
76f4afb4 | 285 | { |
27ccdd52 KW |
286 | BlockBackendPublic *blkp = blk_get_public(blk); |
287 | ThrottleGroup *tg = container_of(blkp->throttle_state, ThrottleGroup, ts); | |
76f4afb4 | 288 | bool must_wait; |
31dce3cc | 289 | BlockBackend *token; |
76f4afb4 AG |
290 | |
291 | /* Check if there's any pending request to schedule next */ | |
31dce3cc | 292 | token = next_throttle_token(blk, is_write); |
6bf77e1c | 293 | if (!blk_has_pending_reqs(token, is_write)) { |
76f4afb4 AG |
294 | return; |
295 | } | |
296 | ||
297 | /* Set a timer for the request if it needs to be throttled */ | |
298 | must_wait = throttle_group_schedule_timer(token, is_write); | |
299 | ||
300 | /* If it doesn't have to wait, queue it for immediate execution */ | |
301 | if (!must_wait) { | |
27ccdd52 | 302 | /* Give preference to requests from the current blk */ |
76f4afb4 | 303 | if (qemu_in_coroutine() && |
3b170dc8 | 304 | throttle_group_co_restart_queue(blk, is_write)) { |
31dce3cc | 305 | token = blk; |
76f4afb4 | 306 | } else { |
6bf77e1c | 307 | ThrottleTimers *tt = &blk_get_public(token)->throttle_timers; |
76f4afb4 | 308 | int64_t now = qemu_clock_get_ns(tt->clock_type); |
7258ed93 | 309 | timer_mod(tt->timers[is_write], now); |
76f4afb4 AG |
310 | tg->any_timer_armed[is_write] = true; |
311 | } | |
312 | tg->tokens[is_write] = token; | |
313 | } | |
314 | } | |
315 | ||
316 | /* Check if an I/O request needs to be throttled, wait and set a timer | |
317 | * if necessary, and schedule the next request using a round robin | |
318 | * algorithm. | |
319 | * | |
441565b2 | 320 | * @blk: the current BlockBackend |
76f4afb4 AG |
321 | * @bytes: the number of bytes for this I/O |
322 | * @is_write: the type of operation (read/write) | |
323 | */ | |
441565b2 | 324 | void coroutine_fn throttle_group_co_io_limits_intercept(BlockBackend *blk, |
76f4afb4 AG |
325 | unsigned int bytes, |
326 | bool is_write) | |
327 | { | |
328 | bool must_wait; | |
31dce3cc | 329 | BlockBackend *token; |
76f4afb4 | 330 | |
27ccdd52 KW |
331 | BlockBackendPublic *blkp = blk_get_public(blk); |
332 | ThrottleGroup *tg = container_of(blkp->throttle_state, ThrottleGroup, ts); | |
76f4afb4 AG |
333 | qemu_mutex_lock(&tg->lock); |
334 | ||
335 | /* First we check if this I/O has to be throttled. */ | |
27ccdd52 | 336 | token = next_throttle_token(blk, is_write); |
76f4afb4 AG |
337 | must_wait = throttle_group_schedule_timer(token, is_write); |
338 | ||
339 | /* Wait if there's a timer set or queued requests of this type */ | |
27ccdd52 KW |
340 | if (must_wait || blkp->pending_reqs[is_write]) { |
341 | blkp->pending_reqs[is_write]++; | |
76f4afb4 | 342 | qemu_mutex_unlock(&tg->lock); |
1ace7cea | 343 | qemu_co_queue_wait(&blkp->throttled_reqs[is_write], NULL); |
76f4afb4 | 344 | qemu_mutex_lock(&tg->lock); |
27ccdd52 | 345 | blkp->pending_reqs[is_write]--; |
76f4afb4 AG |
346 | } |
347 | ||
348 | /* The I/O will be executed, so do the accounting */ | |
27ccdd52 | 349 | throttle_account(blkp->throttle_state, is_write, bytes); |
76f4afb4 AG |
350 | |
351 | /* Schedule the next request */ | |
27ccdd52 | 352 | schedule_next_request(blk, is_write); |
76f4afb4 AG |
353 | |
354 | qemu_mutex_unlock(&tg->lock); | |
355 | } | |
356 | ||
3b170dc8 PB |
357 | typedef struct { |
358 | BlockBackend *blk; | |
359 | bool is_write; | |
360 | } RestartData; | |
361 | ||
362 | static void coroutine_fn throttle_group_restart_queue_entry(void *opaque) | |
7258ed93 | 363 | { |
3b170dc8 PB |
364 | RestartData *data = opaque; |
365 | BlockBackend *blk = data->blk; | |
366 | bool is_write = data->is_write; | |
7258ed93 PB |
367 | BlockBackendPublic *blkp = blk_get_public(blk); |
368 | ThrottleGroup *tg = container_of(blkp->throttle_state, ThrottleGroup, ts); | |
369 | bool empty_queue; | |
370 | ||
3b170dc8 | 371 | empty_queue = !throttle_group_co_restart_queue(blk, is_write); |
7258ed93 PB |
372 | |
373 | /* If the request queue was empty then we have to take care of | |
374 | * scheduling the next one */ | |
375 | if (empty_queue) { | |
376 | qemu_mutex_lock(&tg->lock); | |
377 | schedule_next_request(blk, is_write); | |
378 | qemu_mutex_unlock(&tg->lock); | |
379 | } | |
380 | } | |
381 | ||
3b170dc8 PB |
382 | static void throttle_group_restart_queue(BlockBackend *blk, bool is_write) |
383 | { | |
384 | Coroutine *co; | |
385 | RestartData rd = { | |
386 | .blk = blk, | |
387 | .is_write = is_write | |
388 | }; | |
389 | ||
390 | co = qemu_coroutine_create(throttle_group_restart_queue_entry, &rd); | |
391 | aio_co_enter(blk_get_aio_context(blk), co); | |
392 | } | |
393 | ||
27ccdd52 | 394 | void throttle_group_restart_blk(BlockBackend *blk) |
a72f6414 | 395 | { |
27ccdd52 | 396 | BlockBackendPublic *blkp = blk_get_public(blk); |
a72f6414 | 397 | |
7258ed93 PB |
398 | if (blkp->throttle_state) { |
399 | throttle_group_restart_queue(blk, 0); | |
400 | throttle_group_restart_queue(blk, 1); | |
a72f6414 PB |
401 | } |
402 | } | |
403 | ||
2ff1f2e3 AG |
404 | /* Update the throttle configuration for a particular group. Similar |
405 | * to throttle_config(), but guarantees atomicity within the | |
406 | * throttling group. | |
407 | * | |
97148076 | 408 | * @blk: a BlockBackend that is a member of the group |
2ff1f2e3 AG |
409 | * @cfg: the configuration to set |
410 | */ | |
97148076 | 411 | void throttle_group_config(BlockBackend *blk, ThrottleConfig *cfg) |
2ff1f2e3 | 412 | { |
97148076 | 413 | BlockBackendPublic *blkp = blk_get_public(blk); |
27ccdd52 KW |
414 | ThrottleTimers *tt = &blkp->throttle_timers; |
415 | ThrottleState *ts = blkp->throttle_state; | |
2ff1f2e3 AG |
416 | ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts); |
417 | qemu_mutex_lock(&tg->lock); | |
2ff1f2e3 | 418 | /* throttle_config() cancels the timers */ |
2f388b93 AG |
419 | if (timer_pending(tt->timers[0])) { |
420 | tg->any_timer_armed[0] = false; | |
421 | } | |
422 | if (timer_pending(tt->timers[1])) { | |
423 | tg->any_timer_armed[1] = false; | |
424 | } | |
425 | throttle_config(ts, tt, cfg); | |
2ff1f2e3 | 426 | qemu_mutex_unlock(&tg->lock); |
a72f6414 | 427 | |
7258ed93 | 428 | throttle_group_restart_blk(blk); |
2ff1f2e3 AG |
429 | } |
430 | ||
431 | /* Get the throttle configuration from a particular group. Similar to | |
432 | * throttle_get_config(), but guarantees atomicity within the | |
433 | * throttling group. | |
434 | * | |
97148076 | 435 | * @blk: a BlockBackend that is a member of the group |
2ff1f2e3 AG |
436 | * @cfg: the configuration will be written here |
437 | */ | |
97148076 | 438 | void throttle_group_get_config(BlockBackend *blk, ThrottleConfig *cfg) |
2ff1f2e3 | 439 | { |
97148076 | 440 | BlockBackendPublic *blkp = blk_get_public(blk); |
27ccdd52 | 441 | ThrottleState *ts = blkp->throttle_state; |
2ff1f2e3 AG |
442 | ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts); |
443 | qemu_mutex_lock(&tg->lock); | |
444 | throttle_get_config(ts, cfg); | |
445 | qemu_mutex_unlock(&tg->lock); | |
446 | } | |
447 | ||
76f4afb4 AG |
448 | /* ThrottleTimers callback. This wakes up a request that was waiting |
449 | * because it had been throttled. | |
450 | * | |
27ccdd52 | 451 | * @blk: the BlockBackend whose request had been throttled |
76f4afb4 AG |
452 | * @is_write: the type of operation (read/write) |
453 | */ | |
27ccdd52 | 454 | static void timer_cb(BlockBackend *blk, bool is_write) |
76f4afb4 | 455 | { |
27ccdd52 KW |
456 | BlockBackendPublic *blkp = blk_get_public(blk); |
457 | ThrottleState *ts = blkp->throttle_state; | |
76f4afb4 | 458 | ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts); |
76f4afb4 AG |
459 | |
460 | /* The timer has just been fired, so we can update the flag */ | |
461 | qemu_mutex_lock(&tg->lock); | |
462 | tg->any_timer_armed[is_write] = false; | |
463 | qemu_mutex_unlock(&tg->lock); | |
464 | ||
465 | /* Run the request that was waiting for this timer */ | |
7258ed93 | 466 | throttle_group_restart_queue(blk, is_write); |
76f4afb4 AG |
467 | } |
468 | ||
469 | static void read_timer_cb(void *opaque) | |
470 | { | |
471 | timer_cb(opaque, false); | |
472 | } | |
473 | ||
474 | static void write_timer_cb(void *opaque) | |
475 | { | |
476 | timer_cb(opaque, true); | |
477 | } | |
478 | ||
31dce3cc KW |
479 | /* Register a BlockBackend in the throttling group, also initializing its |
480 | * timers and updating its throttle_state pointer to point to it. If a | |
481 | * throttling group with that name does not exist yet, it will be created. | |
2ff1f2e3 | 482 | * |
31dce3cc | 483 | * @blk: the BlockBackend to insert |
2ff1f2e3 AG |
484 | * @groupname: the name of the group |
485 | */ | |
31dce3cc | 486 | void throttle_group_register_blk(BlockBackend *blk, const char *groupname) |
2ff1f2e3 AG |
487 | { |
488 | int i; | |
27ccdd52 | 489 | BlockBackendPublic *blkp = blk_get_public(blk); |
973f2ddf HR |
490 | ThrottleState *ts = throttle_group_incref(groupname); |
491 | ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts); | |
76f4afb4 AG |
492 | int clock_type = QEMU_CLOCK_REALTIME; |
493 | ||
494 | if (qtest_enabled()) { | |
495 | /* For testing block IO throttling only */ | |
496 | clock_type = QEMU_CLOCK_VIRTUAL; | |
497 | } | |
2ff1f2e3 | 498 | |
27ccdd52 | 499 | blkp->throttle_state = ts; |
2ff1f2e3 AG |
500 | |
501 | qemu_mutex_lock(&tg->lock); | |
31dce3cc | 502 | /* If the ThrottleGroup is new set this BlockBackend as the token */ |
2ff1f2e3 AG |
503 | for (i = 0; i < 2; i++) { |
504 | if (!tg->tokens[i]) { | |
31dce3cc | 505 | tg->tokens[i] = blk; |
2ff1f2e3 AG |
506 | } |
507 | } | |
508 | ||
27ccdd52 | 509 | QLIST_INSERT_HEAD(&tg->head, blkp, round_robin); |
76f4afb4 | 510 | |
27ccdd52 KW |
511 | throttle_timers_init(&blkp->throttle_timers, |
512 | blk_get_aio_context(blk), | |
76f4afb4 AG |
513 | clock_type, |
514 | read_timer_cb, | |
515 | write_timer_cb, | |
27ccdd52 | 516 | blk); |
76f4afb4 | 517 | |
2ff1f2e3 AG |
518 | qemu_mutex_unlock(&tg->lock); |
519 | } | |
520 | ||
31dce3cc KW |
521 | /* Unregister a BlockBackend from its group, removing it from the list, |
522 | * destroying the timers and setting the throttle_state pointer to NULL. | |
2ff1f2e3 | 523 | * |
31dce3cc KW |
524 | * The BlockBackend must not have pending throttled requests, so the caller has |
525 | * to drain them first. | |
5ac72418 | 526 | * |
2ff1f2e3 AG |
527 | * The group will be destroyed if it's empty after this operation. |
528 | * | |
31dce3cc | 529 | * @blk: the BlockBackend to remove |
2ff1f2e3 | 530 | */ |
31dce3cc | 531 | void throttle_group_unregister_blk(BlockBackend *blk) |
2ff1f2e3 | 532 | { |
27ccdd52 KW |
533 | BlockBackendPublic *blkp = blk_get_public(blk); |
534 | ThrottleGroup *tg = container_of(blkp->throttle_state, ThrottleGroup, ts); | |
2ff1f2e3 AG |
535 | int i; |
536 | ||
27ccdd52 KW |
537 | assert(blkp->pending_reqs[0] == 0 && blkp->pending_reqs[1] == 0); |
538 | assert(qemu_co_queue_empty(&blkp->throttled_reqs[0])); | |
539 | assert(qemu_co_queue_empty(&blkp->throttled_reqs[1])); | |
5ac72418 | 540 | |
2ff1f2e3 AG |
541 | qemu_mutex_lock(&tg->lock); |
542 | for (i = 0; i < 2; i++) { | |
31dce3cc KW |
543 | if (tg->tokens[i] == blk) { |
544 | BlockBackend *token = throttle_group_next_blk(blk); | |
27ccdd52 | 545 | /* Take care of the case where this is the last blk in the group */ |
31dce3cc | 546 | if (token == blk) { |
2ff1f2e3 AG |
547 | token = NULL; |
548 | } | |
549 | tg->tokens[i] = token; | |
550 | } | |
551 | } | |
552 | ||
27ccdd52 KW |
553 | /* remove the current blk from the list */ |
554 | QLIST_REMOVE(blkp, round_robin); | |
555 | throttle_timers_destroy(&blkp->throttle_timers); | |
2ff1f2e3 AG |
556 | qemu_mutex_unlock(&tg->lock); |
557 | ||
973f2ddf | 558 | throttle_group_unref(&tg->ts); |
27ccdd52 | 559 | blkp->throttle_state = NULL; |
2ff1f2e3 AG |
560 | } |
561 | ||
562 | static void throttle_groups_init(void) | |
563 | { | |
564 | qemu_mutex_init(&throttle_groups_lock); | |
565 | } | |
566 | ||
567 | block_init(throttle_groups_init); |