]> git.proxmox.com Git - mirror_qemu.git/blob - block/throttle-groups.c
d46c56b31e7ca57d7cbd4bf871c25fade729224a
[mirror_qemu.git] / block / throttle-groups.c
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
25 #include "qemu/osdep.h"
26 #include "sysemu/block-backend.h"
27 #include "block/throttle-groups.h"
28 #include "qemu/throttle-options.h"
29 #include "qemu/queue.h"
30 #include "qemu/thread.h"
31 #include "sysemu/qtest.h"
32 #include "qapi/error.h"
33 #include "qapi/qapi-visit-block-core.h"
34 #include "qom/object.h"
35 #include "qom/object_interfaces.h"
36
37 static void throttle_group_obj_init(Object *obj);
38 static void throttle_group_obj_complete(UserCreatable *obj, Error **errp);
39
40 /* The ThrottleGroup structure (with its ThrottleState) is shared
41 * among different ThrottleGroupMembers and it's independent from
42 * AioContext, so in order to use it from different threads it needs
43 * its own locking.
44 *
45 * This locking is however handled internally in this file, so it's
46 * transparent to outside users.
47 *
48 * The whole ThrottleGroup structure is private and invisible to
49 * outside users, that only use it through its ThrottleState.
50 *
51 * In addition to the ThrottleGroup structure, ThrottleGroupMember has
52 * fields that need to be accessed by other members of the group and
53 * therefore also need to be protected by this lock. Once a
54 * ThrottleGroupMember is registered in a group those fields can be accessed
55 * by other threads any time.
56 *
57 * Again, all this is handled internally and is mostly transparent to
58 * the outside. The 'throttle_timers' field however has an additional
59 * constraint because it may be temporarily invalid (see for example
60 * blk_set_aio_context()). Therefore in this file a thread will
61 * access some other ThrottleGroupMember's timers only after verifying that
62 * that ThrottleGroupMember has throttled requests in the queue.
63 */
64 typedef struct ThrottleGroup {
65 Object parent_obj;
66
67 /* refuse individual property change if initialization is complete */
68 bool is_initialized;
69 char *name; /* This is constant during the lifetime of the group */
70
71 QemuMutex lock; /* This lock protects the following four fields */
72 ThrottleState ts;
73 QLIST_HEAD(, ThrottleGroupMember) head;
74 ThrottleGroupMember *tokens[2];
75 bool any_timer_armed[2];
76 QEMUClockType clock_type;
77
78 /* This field is protected by the global QEMU mutex */
79 QTAILQ_ENTRY(ThrottleGroup) list;
80 } ThrottleGroup;
81
82 /* This is protected by the global QEMU mutex */
83 static QTAILQ_HEAD(, ThrottleGroup) throttle_groups =
84 QTAILQ_HEAD_INITIALIZER(throttle_groups);
85
86
87 /* This function reads throttle_groups and must be called under the global
88 * mutex.
89 */
90 static ThrottleGroup *throttle_group_by_name(const char *name)
91 {
92 ThrottleGroup *iter;
93
94 /* Look for an existing group with that name */
95 QTAILQ_FOREACH(iter, &throttle_groups, list) {
96 if (!g_strcmp0(name, iter->name)) {
97 return iter;
98 }
99 }
100
101 return NULL;
102 }
103
104 /* This function reads throttle_groups and must be called under the global
105 * mutex.
106 */
107 bool throttle_group_exists(const char *name)
108 {
109 return throttle_group_by_name(name) != NULL;
110 }
111
112 /* Increments the reference count of a ThrottleGroup given its name.
113 *
114 * If no ThrottleGroup is found with the given name a new one is
115 * created.
116 *
117 * This function edits throttle_groups and must be called under the global
118 * mutex.
119 *
120 * @name: the name of the ThrottleGroup
121 * @ret: the ThrottleState member of the ThrottleGroup
122 */
123 ThrottleState *throttle_group_incref(const char *name)
124 {
125 ThrottleGroup *tg = NULL;
126
127 /* Look for an existing group with that name */
128 tg = throttle_group_by_name(name);
129
130 if (tg) {
131 object_ref(OBJECT(tg));
132 } else {
133 /* Create a new one if not found */
134 /* new ThrottleGroup obj will have a refcnt = 1 */
135 tg = THROTTLE_GROUP(object_new(TYPE_THROTTLE_GROUP));
136 tg->name = g_strdup(name);
137 throttle_group_obj_complete(USER_CREATABLE(tg), &error_abort);
138 }
139
140 return &tg->ts;
141 }
142
143 /* Decrease the reference count of a ThrottleGroup.
144 *
145 * When the reference count reaches zero the ThrottleGroup is
146 * destroyed.
147 *
148 * This function edits throttle_groups and must be called under the global
149 * mutex.
150 *
151 * @ts: The ThrottleGroup to unref, given by its ThrottleState member
152 */
153 void throttle_group_unref(ThrottleState *ts)
154 {
155 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
156 object_unref(OBJECT(tg));
157 }
158
159 /* Get the name from a ThrottleGroupMember's group. The name (and the pointer)
160 * is guaranteed to remain constant during the lifetime of the group.
161 *
162 * @tgm: a ThrottleGroupMember
163 * @ret: the name of the group.
164 */
165 const char *throttle_group_get_name(ThrottleGroupMember *tgm)
166 {
167 ThrottleGroup *tg = container_of(tgm->throttle_state, ThrottleGroup, ts);
168 return tg->name;
169 }
170
171 /* Return the next ThrottleGroupMember in the round-robin sequence, simulating
172 * a circular list.
173 *
174 * This assumes that tg->lock is held.
175 *
176 * @tgm: the current ThrottleGroupMember
177 * @ret: the next ThrottleGroupMember in the sequence
178 */
179 static ThrottleGroupMember *throttle_group_next_tgm(ThrottleGroupMember *tgm)
180 {
181 ThrottleState *ts = tgm->throttle_state;
182 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
183 ThrottleGroupMember *next = QLIST_NEXT(tgm, round_robin);
184
185 if (!next) {
186 next = QLIST_FIRST(&tg->head);
187 }
188
189 return next;
190 }
191
192 /*
193 * Return whether a ThrottleGroupMember has pending requests.
194 *
195 * This assumes that tg->lock is held.
196 *
197 * @tgm: the ThrottleGroupMember
198 * @is_write: the type of operation (read/write)
199 * @ret: whether the ThrottleGroupMember has pending requests.
200 */
201 static inline bool tgm_has_pending_reqs(ThrottleGroupMember *tgm,
202 bool is_write)
203 {
204 return tgm->pending_reqs[is_write];
205 }
206
207 /* Return the next ThrottleGroupMember in the round-robin sequence with pending
208 * I/O requests.
209 *
210 * This assumes that tg->lock is held.
211 *
212 * @tgm: the current ThrottleGroupMember
213 * @is_write: the type of operation (read/write)
214 * @ret: the next ThrottleGroupMember with pending requests, or tgm if
215 * there is none.
216 */
217 static ThrottleGroupMember *next_throttle_token(ThrottleGroupMember *tgm,
218 bool is_write)
219 {
220 ThrottleState *ts = tgm->throttle_state;
221 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
222 ThrottleGroupMember *token, *start;
223
224 /* If this member has its I/O limits disabled then it means that
225 * it's being drained. Skip the round-robin search and return tgm
226 * immediately if it has pending requests. Otherwise we could be
227 * forcing it to wait for other member's throttled requests. */
228 if (tgm_has_pending_reqs(tgm, is_write) &&
229 atomic_read(&tgm->io_limits_disabled)) {
230 return tgm;
231 }
232
233 start = token = tg->tokens[is_write];
234
235 /* get next bs round in round robin style */
236 token = throttle_group_next_tgm(token);
237 while (token != start && !tgm_has_pending_reqs(token, is_write)) {
238 token = throttle_group_next_tgm(token);
239 }
240
241 /* If no IO are queued for scheduling on the next round robin token
242 * then decide the token is the current tgm because chances are
243 * the current tgm got the current request queued.
244 */
245 if (token == start && !tgm_has_pending_reqs(token, is_write)) {
246 token = tgm;
247 }
248
249 /* Either we return the original TGM, or one with pending requests */
250 assert(token == tgm || tgm_has_pending_reqs(token, is_write));
251
252 return token;
253 }
254
255 /* Check if the next I/O request for a ThrottleGroupMember needs to be
256 * throttled or not. If there's no timer set in this group, set one and update
257 * the token accordingly.
258 *
259 * This assumes that tg->lock is held.
260 *
261 * @tgm: the current ThrottleGroupMember
262 * @is_write: the type of operation (read/write)
263 * @ret: whether the I/O request needs to be throttled or not
264 */
265 static bool throttle_group_schedule_timer(ThrottleGroupMember *tgm,
266 bool is_write)
267 {
268 ThrottleState *ts = tgm->throttle_state;
269 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
270 ThrottleTimers *tt = &tgm->throttle_timers;
271 bool must_wait;
272
273 if (atomic_read(&tgm->io_limits_disabled)) {
274 return false;
275 }
276
277 /* Check if any of the timers in this group is already armed */
278 if (tg->any_timer_armed[is_write]) {
279 return true;
280 }
281
282 must_wait = throttle_schedule_timer(ts, tt, is_write);
283
284 /* If a timer just got armed, set tgm as the current token */
285 if (must_wait) {
286 tg->tokens[is_write] = tgm;
287 tg->any_timer_armed[is_write] = true;
288 }
289
290 return must_wait;
291 }
292
293 /* Start the next pending I/O request for a ThrottleGroupMember. Return whether
294 * any request was actually pending.
295 *
296 * @tgm: the current ThrottleGroupMember
297 * @is_write: the type of operation (read/write)
298 */
299 static bool coroutine_fn throttle_group_co_restart_queue(ThrottleGroupMember *tgm,
300 bool is_write)
301 {
302 bool ret;
303
304 qemu_co_mutex_lock(&tgm->throttled_reqs_lock);
305 ret = qemu_co_queue_next(&tgm->throttled_reqs[is_write]);
306 qemu_co_mutex_unlock(&tgm->throttled_reqs_lock);
307
308 return ret;
309 }
310
311 /* Look for the next pending I/O request and schedule it.
312 *
313 * This assumes that tg->lock is held.
314 *
315 * @tgm: the current ThrottleGroupMember
316 * @is_write: the type of operation (read/write)
317 */
318 static void schedule_next_request(ThrottleGroupMember *tgm, bool is_write)
319 {
320 ThrottleState *ts = tgm->throttle_state;
321 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
322 bool must_wait;
323 ThrottleGroupMember *token;
324
325 /* Check if there's any pending request to schedule next */
326 token = next_throttle_token(tgm, is_write);
327 if (!tgm_has_pending_reqs(token, is_write)) {
328 return;
329 }
330
331 /* Set a timer for the request if it needs to be throttled */
332 must_wait = throttle_group_schedule_timer(token, is_write);
333
334 /* If it doesn't have to wait, queue it for immediate execution */
335 if (!must_wait) {
336 /* Give preference to requests from the current tgm */
337 if (qemu_in_coroutine() &&
338 throttle_group_co_restart_queue(tgm, is_write)) {
339 token = tgm;
340 } else {
341 ThrottleTimers *tt = &token->throttle_timers;
342 int64_t now = qemu_clock_get_ns(tg->clock_type);
343 timer_mod(tt->timers[is_write], now);
344 tg->any_timer_armed[is_write] = true;
345 }
346 tg->tokens[is_write] = token;
347 }
348 }
349
350 /* Check if an I/O request needs to be throttled, wait and set a timer
351 * if necessary, and schedule the next request using a round robin
352 * algorithm.
353 *
354 * @tgm: the current ThrottleGroupMember
355 * @bytes: the number of bytes for this I/O
356 * @is_write: the type of operation (read/write)
357 */
358 void coroutine_fn throttle_group_co_io_limits_intercept(ThrottleGroupMember *tgm,
359 unsigned int bytes,
360 bool is_write)
361 {
362 bool must_wait;
363 ThrottleGroupMember *token;
364 ThrottleGroup *tg = container_of(tgm->throttle_state, ThrottleGroup, ts);
365 qemu_mutex_lock(&tg->lock);
366
367 /* First we check if this I/O has to be throttled. */
368 token = next_throttle_token(tgm, is_write);
369 must_wait = throttle_group_schedule_timer(token, is_write);
370
371 /* Wait if there's a timer set or queued requests of this type */
372 if (must_wait || tgm->pending_reqs[is_write]) {
373 tgm->pending_reqs[is_write]++;
374 qemu_mutex_unlock(&tg->lock);
375 qemu_co_mutex_lock(&tgm->throttled_reqs_lock);
376 qemu_co_queue_wait(&tgm->throttled_reqs[is_write],
377 &tgm->throttled_reqs_lock);
378 qemu_co_mutex_unlock(&tgm->throttled_reqs_lock);
379 qemu_mutex_lock(&tg->lock);
380 tgm->pending_reqs[is_write]--;
381 }
382
383 /* The I/O will be executed, so do the accounting */
384 throttle_account(tgm->throttle_state, is_write, bytes);
385
386 /* Schedule the next request */
387 schedule_next_request(tgm, is_write);
388
389 qemu_mutex_unlock(&tg->lock);
390 }
391
392 typedef struct {
393 ThrottleGroupMember *tgm;
394 bool is_write;
395 } RestartData;
396
397 static void coroutine_fn throttle_group_restart_queue_entry(void *opaque)
398 {
399 RestartData *data = opaque;
400 ThrottleGroupMember *tgm = data->tgm;
401 ThrottleState *ts = tgm->throttle_state;
402 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
403 bool is_write = data->is_write;
404 bool empty_queue;
405
406 empty_queue = !throttle_group_co_restart_queue(tgm, is_write);
407
408 /* If the request queue was empty then we have to take care of
409 * scheduling the next one */
410 if (empty_queue) {
411 qemu_mutex_lock(&tg->lock);
412 schedule_next_request(tgm, is_write);
413 qemu_mutex_unlock(&tg->lock);
414 }
415
416 g_free(data);
417 }
418
419 static void throttle_group_restart_queue(ThrottleGroupMember *tgm, bool is_write)
420 {
421 Coroutine *co;
422 RestartData *rd = g_new0(RestartData, 1);
423
424 rd->tgm = tgm;
425 rd->is_write = is_write;
426
427 co = qemu_coroutine_create(throttle_group_restart_queue_entry, rd);
428 aio_co_enter(tgm->aio_context, co);
429 }
430
431 void throttle_group_restart_tgm(ThrottleGroupMember *tgm)
432 {
433 if (tgm->throttle_state) {
434 throttle_group_restart_queue(tgm, 0);
435 throttle_group_restart_queue(tgm, 1);
436 }
437 }
438
439 /* Update the throttle configuration for a particular group. Similar
440 * to throttle_config(), but guarantees atomicity within the
441 * throttling group.
442 *
443 * @tgm: a ThrottleGroupMember that is a member of the group
444 * @cfg: the configuration to set
445 */
446 void throttle_group_config(ThrottleGroupMember *tgm, ThrottleConfig *cfg)
447 {
448 ThrottleState *ts = tgm->throttle_state;
449 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
450 qemu_mutex_lock(&tg->lock);
451 throttle_config(ts, tg->clock_type, cfg);
452 qemu_mutex_unlock(&tg->lock);
453
454 throttle_group_restart_tgm(tgm);
455 }
456
457 /* Get the throttle configuration from a particular group. Similar to
458 * throttle_get_config(), but guarantees atomicity within the
459 * throttling group.
460 *
461 * @tgm: a ThrottleGroupMember that is a member of the group
462 * @cfg: the configuration will be written here
463 */
464 void throttle_group_get_config(ThrottleGroupMember *tgm, ThrottleConfig *cfg)
465 {
466 ThrottleState *ts = tgm->throttle_state;
467 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
468 qemu_mutex_lock(&tg->lock);
469 throttle_get_config(ts, cfg);
470 qemu_mutex_unlock(&tg->lock);
471 }
472
473 /* ThrottleTimers callback. This wakes up a request that was waiting
474 * because it had been throttled.
475 *
476 * @tgm: the ThrottleGroupMember whose request had been throttled
477 * @is_write: the type of operation (read/write)
478 */
479 static void timer_cb(ThrottleGroupMember *tgm, bool is_write)
480 {
481 ThrottleState *ts = tgm->throttle_state;
482 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
483
484 /* The timer has just been fired, so we can update the flag */
485 qemu_mutex_lock(&tg->lock);
486 tg->any_timer_armed[is_write] = false;
487 qemu_mutex_unlock(&tg->lock);
488
489 /* Run the request that was waiting for this timer */
490 throttle_group_restart_queue(tgm, is_write);
491 }
492
493 static void read_timer_cb(void *opaque)
494 {
495 timer_cb(opaque, false);
496 }
497
498 static void write_timer_cb(void *opaque)
499 {
500 timer_cb(opaque, true);
501 }
502
503 /* Register a ThrottleGroupMember from the throttling group, also initializing
504 * its timers and updating its throttle_state pointer to point to it. If a
505 * throttling group with that name does not exist yet, it will be created.
506 *
507 * This function edits throttle_groups and must be called under the global
508 * mutex.
509 *
510 * @tgm: the ThrottleGroupMember to insert
511 * @groupname: the name of the group
512 * @ctx: the AioContext to use
513 */
514 void throttle_group_register_tgm(ThrottleGroupMember *tgm,
515 const char *groupname,
516 AioContext *ctx)
517 {
518 int i;
519 ThrottleState *ts = throttle_group_incref(groupname);
520 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
521
522 tgm->throttle_state = ts;
523 tgm->aio_context = ctx;
524
525 qemu_mutex_lock(&tg->lock);
526 /* If the ThrottleGroup is new set this ThrottleGroupMember as the token */
527 for (i = 0; i < 2; i++) {
528 if (!tg->tokens[i]) {
529 tg->tokens[i] = tgm;
530 }
531 }
532
533 QLIST_INSERT_HEAD(&tg->head, tgm, round_robin);
534
535 throttle_timers_init(&tgm->throttle_timers,
536 tgm->aio_context,
537 tg->clock_type,
538 read_timer_cb,
539 write_timer_cb,
540 tgm);
541 qemu_co_mutex_init(&tgm->throttled_reqs_lock);
542 qemu_co_queue_init(&tgm->throttled_reqs[0]);
543 qemu_co_queue_init(&tgm->throttled_reqs[1]);
544
545 qemu_mutex_unlock(&tg->lock);
546 }
547
548 /* Unregister a ThrottleGroupMember from its group, removing it from the list,
549 * destroying the timers and setting the throttle_state pointer to NULL.
550 *
551 * The ThrottleGroupMember must not have pending throttled requests, so the
552 * caller has to drain them first.
553 *
554 * The group will be destroyed if it's empty after this operation.
555 *
556 * @tgm the ThrottleGroupMember to remove
557 */
558 void throttle_group_unregister_tgm(ThrottleGroupMember *tgm)
559 {
560 ThrottleState *ts = tgm->throttle_state;
561 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts);
562 ThrottleGroupMember *token;
563 int i;
564
565 if (!ts) {
566 /* Discard already unregistered tgm */
567 return;
568 }
569
570 assert(tgm->pending_reqs[0] == 0 && tgm->pending_reqs[1] == 0);
571 assert(qemu_co_queue_empty(&tgm->throttled_reqs[0]));
572 assert(qemu_co_queue_empty(&tgm->throttled_reqs[1]));
573
574 qemu_mutex_lock(&tg->lock);
575 for (i = 0; i < 2; i++) {
576 if (timer_pending(tgm->throttle_timers.timers[i])) {
577 tg->any_timer_armed[i] = false;
578 schedule_next_request(tgm, i);
579 }
580 if (tg->tokens[i] == tgm) {
581 token = throttle_group_next_tgm(tgm);
582 /* Take care of the case where this is the last tgm in the group */
583 if (token == tgm) {
584 token = NULL;
585 }
586 tg->tokens[i] = token;
587 }
588 }
589
590 /* remove the current tgm from the list */
591 QLIST_REMOVE(tgm, round_robin);
592 throttle_timers_destroy(&tgm->throttle_timers);
593 qemu_mutex_unlock(&tg->lock);
594
595 throttle_group_unref(&tg->ts);
596 tgm->throttle_state = NULL;
597 }
598
599 void throttle_group_attach_aio_context(ThrottleGroupMember *tgm,
600 AioContext *new_context)
601 {
602 ThrottleTimers *tt = &tgm->throttle_timers;
603 throttle_timers_attach_aio_context(tt, new_context);
604 tgm->aio_context = new_context;
605 }
606
607 void throttle_group_detach_aio_context(ThrottleGroupMember *tgm)
608 {
609 ThrottleGroup *tg = container_of(tgm->throttle_state, ThrottleGroup, ts);
610 ThrottleTimers *tt = &tgm->throttle_timers;
611 int i;
612
613 /* Requests must have been drained */
614 assert(tgm->pending_reqs[0] == 0 && tgm->pending_reqs[1] == 0);
615 assert(qemu_co_queue_empty(&tgm->throttled_reqs[0]));
616 assert(qemu_co_queue_empty(&tgm->throttled_reqs[1]));
617
618 /* Kick off next ThrottleGroupMember, if necessary */
619 qemu_mutex_lock(&tg->lock);
620 for (i = 0; i < 2; i++) {
621 if (timer_pending(tt->timers[i])) {
622 tg->any_timer_armed[i] = false;
623 schedule_next_request(tgm, i);
624 }
625 }
626 qemu_mutex_unlock(&tg->lock);
627
628 throttle_timers_detach_aio_context(tt);
629 tgm->aio_context = NULL;
630 }
631
632 #undef THROTTLE_OPT_PREFIX
633 #define THROTTLE_OPT_PREFIX "x-"
634
635 /* Helper struct and array for QOM property setter/getter */
636 typedef struct {
637 const char *name;
638 BucketType type;
639 enum {
640 AVG,
641 MAX,
642 BURST_LENGTH,
643 IOPS_SIZE,
644 } category;
645 } ThrottleParamInfo;
646
647 static ThrottleParamInfo properties[] = {
648 {
649 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_TOTAL,
650 THROTTLE_OPS_TOTAL, AVG,
651 },
652 {
653 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_TOTAL_MAX,
654 THROTTLE_OPS_TOTAL, MAX,
655 },
656 {
657 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_TOTAL_MAX_LENGTH,
658 THROTTLE_OPS_TOTAL, BURST_LENGTH,
659 },
660 {
661 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_READ,
662 THROTTLE_OPS_READ, AVG,
663 },
664 {
665 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_READ_MAX,
666 THROTTLE_OPS_READ, MAX,
667 },
668 {
669 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_READ_MAX_LENGTH,
670 THROTTLE_OPS_READ, BURST_LENGTH,
671 },
672 {
673 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_WRITE,
674 THROTTLE_OPS_WRITE, AVG,
675 },
676 {
677 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_WRITE_MAX,
678 THROTTLE_OPS_WRITE, MAX,
679 },
680 {
681 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_WRITE_MAX_LENGTH,
682 THROTTLE_OPS_WRITE, BURST_LENGTH,
683 },
684 {
685 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_TOTAL,
686 THROTTLE_BPS_TOTAL, AVG,
687 },
688 {
689 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_TOTAL_MAX,
690 THROTTLE_BPS_TOTAL, MAX,
691 },
692 {
693 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_TOTAL_MAX_LENGTH,
694 THROTTLE_BPS_TOTAL, BURST_LENGTH,
695 },
696 {
697 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_READ,
698 THROTTLE_BPS_READ, AVG,
699 },
700 {
701 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_READ_MAX,
702 THROTTLE_BPS_READ, MAX,
703 },
704 {
705 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_READ_MAX_LENGTH,
706 THROTTLE_BPS_READ, BURST_LENGTH,
707 },
708 {
709 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_WRITE,
710 THROTTLE_BPS_WRITE, AVG,
711 },
712 {
713 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_WRITE_MAX,
714 THROTTLE_BPS_WRITE, MAX,
715 },
716 {
717 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_WRITE_MAX_LENGTH,
718 THROTTLE_BPS_WRITE, BURST_LENGTH,
719 },
720 {
721 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_SIZE,
722 0, IOPS_SIZE,
723 }
724 };
725
726 /* This function edits throttle_groups and must be called under the global
727 * mutex */
728 static void throttle_group_obj_init(Object *obj)
729 {
730 ThrottleGroup *tg = THROTTLE_GROUP(obj);
731
732 tg->clock_type = QEMU_CLOCK_REALTIME;
733 if (qtest_enabled()) {
734 /* For testing block IO throttling only */
735 tg->clock_type = QEMU_CLOCK_VIRTUAL;
736 }
737 tg->is_initialized = false;
738 qemu_mutex_init(&tg->lock);
739 throttle_init(&tg->ts);
740 QLIST_INIT(&tg->head);
741 }
742
743 /* This function edits throttle_groups and must be called under the global
744 * mutex */
745 static void throttle_group_obj_complete(UserCreatable *obj, Error **errp)
746 {
747 ThrottleGroup *tg = THROTTLE_GROUP(obj);
748 ThrottleConfig cfg;
749
750 /* set group name to object id if it exists */
751 if (!tg->name && tg->parent_obj.parent) {
752 tg->name = object_get_canonical_path_component(OBJECT(obj));
753 }
754 /* We must have a group name at this point */
755 assert(tg->name);
756
757 /* error if name is duplicate */
758 if (throttle_group_exists(tg->name)) {
759 error_setg(errp, "A group with this name already exists");
760 return;
761 }
762
763 /* check validity */
764 throttle_get_config(&tg->ts, &cfg);
765 if (!throttle_is_valid(&cfg, errp)) {
766 return;
767 }
768 throttle_config(&tg->ts, tg->clock_type, &cfg);
769 QTAILQ_INSERT_TAIL(&throttle_groups, tg, list);
770 tg->is_initialized = true;
771 }
772
773 /* This function edits throttle_groups and must be called under the global
774 * mutex */
775 static void throttle_group_obj_finalize(Object *obj)
776 {
777 ThrottleGroup *tg = THROTTLE_GROUP(obj);
778 if (tg->is_initialized) {
779 QTAILQ_REMOVE(&throttle_groups, tg, list);
780 }
781 qemu_mutex_destroy(&tg->lock);
782 g_free(tg->name);
783 }
784
785 static void throttle_group_set(Object *obj, Visitor *v, const char * name,
786 void *opaque, Error **errp)
787
788 {
789 ThrottleGroup *tg = THROTTLE_GROUP(obj);
790 ThrottleConfig *cfg;
791 ThrottleParamInfo *info = opaque;
792 Error *local_err = NULL;
793 int64_t value;
794
795 /* If we have finished initialization, don't accept individual property
796 * changes through QOM. Throttle configuration limits must be set in one
797 * transaction, as certain combinations are invalid.
798 */
799 if (tg->is_initialized) {
800 error_setg(&local_err, "Property cannot be set after initialization");
801 goto ret;
802 }
803
804 visit_type_int64(v, name, &value, &local_err);
805 if (local_err) {
806 goto ret;
807 }
808 if (value < 0) {
809 error_setg(&local_err, "Property values cannot be negative");
810 goto ret;
811 }
812
813 cfg = &tg->ts.cfg;
814 switch (info->category) {
815 case AVG:
816 cfg->buckets[info->type].avg = value;
817 break;
818 case MAX:
819 cfg->buckets[info->type].max = value;
820 break;
821 case BURST_LENGTH:
822 if (value > UINT_MAX) {
823 error_setg(&local_err, "%s value must be in the"
824 "range [0, %u]", info->name, UINT_MAX);
825 goto ret;
826 }
827 cfg->buckets[info->type].burst_length = value;
828 break;
829 case IOPS_SIZE:
830 cfg->op_size = value;
831 break;
832 }
833
834 ret:
835 error_propagate(errp, local_err);
836 return;
837
838 }
839
840 static void throttle_group_get(Object *obj, Visitor *v, const char *name,
841 void *opaque, Error **errp)
842 {
843 ThrottleGroup *tg = THROTTLE_GROUP(obj);
844 ThrottleConfig cfg;
845 ThrottleParamInfo *info = opaque;
846 int64_t value;
847
848 throttle_get_config(&tg->ts, &cfg);
849 switch (info->category) {
850 case AVG:
851 value = cfg.buckets[info->type].avg;
852 break;
853 case MAX:
854 value = cfg.buckets[info->type].max;
855 break;
856 case BURST_LENGTH:
857 value = cfg.buckets[info->type].burst_length;
858 break;
859 case IOPS_SIZE:
860 value = cfg.op_size;
861 break;
862 }
863
864 visit_type_int64(v, name, &value, errp);
865 }
866
867 static void throttle_group_set_limits(Object *obj, Visitor *v,
868 const char *name, void *opaque,
869 Error **errp)
870
871 {
872 ThrottleGroup *tg = THROTTLE_GROUP(obj);
873 ThrottleConfig cfg;
874 ThrottleLimits arg = { 0 };
875 ThrottleLimits *argp = &arg;
876 Error *local_err = NULL;
877
878 visit_type_ThrottleLimits(v, name, &argp, &local_err);
879 if (local_err) {
880 goto ret;
881 }
882 qemu_mutex_lock(&tg->lock);
883 throttle_get_config(&tg->ts, &cfg);
884 throttle_limits_to_config(argp, &cfg, &local_err);
885 if (local_err) {
886 goto unlock;
887 }
888 throttle_config(&tg->ts, tg->clock_type, &cfg);
889
890 unlock:
891 qemu_mutex_unlock(&tg->lock);
892 ret:
893 error_propagate(errp, local_err);
894 return;
895 }
896
897 static void throttle_group_get_limits(Object *obj, Visitor *v,
898 const char *name, void *opaque,
899 Error **errp)
900 {
901 ThrottleGroup *tg = THROTTLE_GROUP(obj);
902 ThrottleConfig cfg;
903 ThrottleLimits arg = { 0 };
904 ThrottleLimits *argp = &arg;
905
906 qemu_mutex_lock(&tg->lock);
907 throttle_get_config(&tg->ts, &cfg);
908 qemu_mutex_unlock(&tg->lock);
909
910 throttle_config_to_limits(&cfg, argp);
911
912 visit_type_ThrottleLimits(v, name, &argp, errp);
913 }
914
915 static bool throttle_group_can_be_deleted(UserCreatable *uc)
916 {
917 return OBJECT(uc)->ref == 1;
918 }
919
920 static void throttle_group_obj_class_init(ObjectClass *klass, void *class_data)
921 {
922 size_t i = 0;
923 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass);
924
925 ucc->complete = throttle_group_obj_complete;
926 ucc->can_be_deleted = throttle_group_can_be_deleted;
927
928 /* individual properties */
929 for (i = 0; i < sizeof(properties) / sizeof(ThrottleParamInfo); i++) {
930 object_class_property_add(klass,
931 properties[i].name,
932 "int",
933 throttle_group_get,
934 throttle_group_set,
935 NULL, &properties[i],
936 &error_abort);
937 }
938
939 /* ThrottleLimits */
940 object_class_property_add(klass,
941 "limits", "ThrottleLimits",
942 throttle_group_get_limits,
943 throttle_group_set_limits,
944 NULL, NULL,
945 &error_abort);
946 }
947
948 static const TypeInfo throttle_group_info = {
949 .name = TYPE_THROTTLE_GROUP,
950 .parent = TYPE_OBJECT,
951 .class_init = throttle_group_obj_class_init,
952 .instance_size = sizeof(ThrottleGroup),
953 .instance_init = throttle_group_obj_init,
954 .instance_finalize = throttle_group_obj_finalize,
955 .interfaces = (InterfaceInfo[]) {
956 { TYPE_USER_CREATABLE },
957 { }
958 },
959 };
960
961 static void throttle_groups_init(void)
962 {
963 type_register_static(&throttle_group_info);
964 }
965
966 type_init(throttle_groups_init);