2 * QEMU block throttling group infrastructure
4 * Copyright (C) Nodalink, EURL. 2014
5 * Copyright (C) Igalia, S.L. 2015
8 * BenoƮt Canet <benoit.canet@nodalink.com>
9 * Alberto Garcia <berto@igalia.com>
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.
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.
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/>.
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"
37 static void throttle_group_obj_init(Object
*obj
);
38 static void throttle_group_obj_complete(UserCreatable
*obj
, Error
**errp
);
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
45 * This locking is however handled internally in this file, so it's
46 * transparent to outside users.
48 * The whole ThrottleGroup structure is private and invisible to
49 * outside users, that only use it through its ThrottleState.
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.
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.
64 typedef struct ThrottleGroup
{
67 /* refuse individual property change if initialization is complete */
69 char *name
; /* This is constant during the lifetime of the group */
71 QemuMutex lock
; /* This lock protects the following four fields */
73 QLIST_HEAD(, ThrottleGroupMember
) head
;
74 ThrottleGroupMember
*tokens
[2];
75 bool any_timer_armed
[2];
76 QEMUClockType clock_type
;
78 /* This field is protected by the global QEMU mutex */
79 QTAILQ_ENTRY(ThrottleGroup
) list
;
82 /* This is protected by the global QEMU mutex */
83 static QTAILQ_HEAD(, ThrottleGroup
) throttle_groups
=
84 QTAILQ_HEAD_INITIALIZER(throttle_groups
);
87 /* This function reads throttle_groups and must be called under the global
90 static ThrottleGroup
*throttle_group_by_name(const char *name
)
94 /* Look for an existing group with that name */
95 QTAILQ_FOREACH(iter
, &throttle_groups
, list
) {
96 if (!g_strcmp0(name
, iter
->name
)) {
104 /* This function reads throttle_groups and must be called under the global
107 bool throttle_group_exists(const char *name
)
109 return throttle_group_by_name(name
) != NULL
;
112 /* Increments the reference count of a ThrottleGroup given its name.
114 * If no ThrottleGroup is found with the given name a new one is
117 * This function edits throttle_groups and must be called under the global
120 * @name: the name of the ThrottleGroup
121 * @ret: the ThrottleState member of the ThrottleGroup
123 ThrottleState
*throttle_group_incref(const char *name
)
125 ThrottleGroup
*tg
= NULL
;
127 /* Look for an existing group with that name */
128 tg
= throttle_group_by_name(name
);
131 object_ref(OBJECT(tg
));
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
);
143 /* Decrease the reference count of a ThrottleGroup.
145 * When the reference count reaches zero the ThrottleGroup is
148 * This function edits throttle_groups and must be called under the global
151 * @ts: The ThrottleGroup to unref, given by its ThrottleState member
153 void throttle_group_unref(ThrottleState
*ts
)
155 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
156 object_unref(OBJECT(tg
));
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.
162 * @tgm: a ThrottleGroupMember
163 * @ret: the name of the group.
165 const char *throttle_group_get_name(ThrottleGroupMember
*tgm
)
167 ThrottleGroup
*tg
= container_of(tgm
->throttle_state
, ThrottleGroup
, ts
);
171 /* Return the next ThrottleGroupMember in the round-robin sequence, simulating
174 * This assumes that tg->lock is held.
176 * @tgm: the current ThrottleGroupMember
177 * @ret: the next ThrottleGroupMember in the sequence
179 static ThrottleGroupMember
*throttle_group_next_tgm(ThrottleGroupMember
*tgm
)
181 ThrottleState
*ts
= tgm
->throttle_state
;
182 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
183 ThrottleGroupMember
*next
= QLIST_NEXT(tgm
, round_robin
);
186 next
= QLIST_FIRST(&tg
->head
);
193 * Return whether a ThrottleGroupMember has pending requests.
195 * This assumes that tg->lock is held.
197 * @tgm: the ThrottleGroupMember
198 * @is_write: the type of operation (read/write)
199 * @ret: whether the ThrottleGroupMember has pending requests.
201 static inline bool tgm_has_pending_reqs(ThrottleGroupMember
*tgm
,
204 return tgm
->pending_reqs
[is_write
];
207 /* Return the next ThrottleGroupMember in the round-robin sequence with pending
210 * This assumes that tg->lock is held.
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
217 static ThrottleGroupMember
*next_throttle_token(ThrottleGroupMember
*tgm
,
220 ThrottleState
*ts
= tgm
->throttle_state
;
221 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
222 ThrottleGroupMember
*token
, *start
;
224 start
= token
= tg
->tokens
[is_write
];
226 /* get next bs round in round robin style */
227 token
= throttle_group_next_tgm(token
);
228 while (token
!= start
&& !tgm_has_pending_reqs(token
, is_write
)) {
229 token
= throttle_group_next_tgm(token
);
232 /* If no IO are queued for scheduling on the next round robin token
233 * then decide the token is the current tgm because chances are
234 * the current tgm got the current request queued.
236 if (token
== start
&& !tgm_has_pending_reqs(token
, is_write
)) {
240 /* Either we return the original TGM, or one with pending requests */
241 assert(token
== tgm
|| tgm_has_pending_reqs(token
, is_write
));
246 /* Check if the next I/O request for a ThrottleGroupMember needs to be
247 * throttled or not. If there's no timer set in this group, set one and update
248 * the token accordingly.
250 * This assumes that tg->lock is held.
252 * @tgm: the current ThrottleGroupMember
253 * @is_write: the type of operation (read/write)
254 * @ret: whether the I/O request needs to be throttled or not
256 static bool throttle_group_schedule_timer(ThrottleGroupMember
*tgm
,
259 ThrottleState
*ts
= tgm
->throttle_state
;
260 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
261 ThrottleTimers
*tt
= &tgm
->throttle_timers
;
264 if (atomic_read(&tgm
->io_limits_disabled
)) {
268 /* Check if any of the timers in this group is already armed */
269 if (tg
->any_timer_armed
[is_write
]) {
273 must_wait
= throttle_schedule_timer(ts
, tt
, is_write
);
275 /* If a timer just got armed, set tgm as the current token */
277 tg
->tokens
[is_write
] = tgm
;
278 tg
->any_timer_armed
[is_write
] = true;
284 /* Start the next pending I/O request for a ThrottleGroupMember. Return whether
285 * any request was actually pending.
287 * @tgm: the current ThrottleGroupMember
288 * @is_write: the type of operation (read/write)
290 static bool coroutine_fn
throttle_group_co_restart_queue(ThrottleGroupMember
*tgm
,
295 qemu_co_mutex_lock(&tgm
->throttled_reqs_lock
);
296 ret
= qemu_co_queue_next(&tgm
->throttled_reqs
[is_write
]);
297 qemu_co_mutex_unlock(&tgm
->throttled_reqs_lock
);
302 /* Look for the next pending I/O request and schedule it.
304 * This assumes that tg->lock is held.
306 * @tgm: the current ThrottleGroupMember
307 * @is_write: the type of operation (read/write)
309 static void schedule_next_request(ThrottleGroupMember
*tgm
, bool is_write
)
311 ThrottleState
*ts
= tgm
->throttle_state
;
312 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
314 ThrottleGroupMember
*token
;
316 /* Check if there's any pending request to schedule next */
317 token
= next_throttle_token(tgm
, is_write
);
318 if (!tgm_has_pending_reqs(token
, is_write
)) {
322 /* Set a timer for the request if it needs to be throttled */
323 must_wait
= throttle_group_schedule_timer(token
, is_write
);
325 /* If it doesn't have to wait, queue it for immediate execution */
327 /* Give preference to requests from the current tgm */
328 if (qemu_in_coroutine() &&
329 throttle_group_co_restart_queue(tgm
, is_write
)) {
332 ThrottleTimers
*tt
= &token
->throttle_timers
;
333 int64_t now
= qemu_clock_get_ns(tg
->clock_type
);
334 timer_mod(tt
->timers
[is_write
], now
);
335 tg
->any_timer_armed
[is_write
] = true;
337 tg
->tokens
[is_write
] = token
;
341 /* Check if an I/O request needs to be throttled, wait and set a timer
342 * if necessary, and schedule the next request using a round robin
345 * @tgm: the current ThrottleGroupMember
346 * @bytes: the number of bytes for this I/O
347 * @is_write: the type of operation (read/write)
349 void coroutine_fn
throttle_group_co_io_limits_intercept(ThrottleGroupMember
*tgm
,
354 ThrottleGroupMember
*token
;
355 ThrottleGroup
*tg
= container_of(tgm
->throttle_state
, ThrottleGroup
, ts
);
356 qemu_mutex_lock(&tg
->lock
);
358 /* First we check if this I/O has to be throttled. */
359 token
= next_throttle_token(tgm
, is_write
);
360 must_wait
= throttle_group_schedule_timer(token
, is_write
);
362 /* Wait if there's a timer set or queued requests of this type */
363 if (must_wait
|| tgm
->pending_reqs
[is_write
]) {
364 tgm
->pending_reqs
[is_write
]++;
365 qemu_mutex_unlock(&tg
->lock
);
366 qemu_co_mutex_lock(&tgm
->throttled_reqs_lock
);
367 qemu_co_queue_wait(&tgm
->throttled_reqs
[is_write
],
368 &tgm
->throttled_reqs_lock
);
369 qemu_co_mutex_unlock(&tgm
->throttled_reqs_lock
);
370 qemu_mutex_lock(&tg
->lock
);
371 tgm
->pending_reqs
[is_write
]--;
374 /* The I/O will be executed, so do the accounting */
375 throttle_account(tgm
->throttle_state
, is_write
, bytes
);
377 /* Schedule the next request */
378 schedule_next_request(tgm
, is_write
);
380 qemu_mutex_unlock(&tg
->lock
);
384 ThrottleGroupMember
*tgm
;
388 static void coroutine_fn
throttle_group_restart_queue_entry(void *opaque
)
390 RestartData
*data
= opaque
;
391 ThrottleGroupMember
*tgm
= data
->tgm
;
392 ThrottleState
*ts
= tgm
->throttle_state
;
393 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
394 bool is_write
= data
->is_write
;
397 empty_queue
= !throttle_group_co_restart_queue(tgm
, is_write
);
399 /* If the request queue was empty then we have to take care of
400 * scheduling the next one */
402 qemu_mutex_lock(&tg
->lock
);
403 schedule_next_request(tgm
, is_write
);
404 qemu_mutex_unlock(&tg
->lock
);
410 static void throttle_group_restart_queue(ThrottleGroupMember
*tgm
, bool is_write
)
413 RestartData
*rd
= g_new0(RestartData
, 1);
416 rd
->is_write
= is_write
;
418 co
= qemu_coroutine_create(throttle_group_restart_queue_entry
, rd
);
419 aio_co_enter(tgm
->aio_context
, co
);
422 void throttle_group_restart_tgm(ThrottleGroupMember
*tgm
)
424 if (tgm
->throttle_state
) {
425 throttle_group_restart_queue(tgm
, 0);
426 throttle_group_restart_queue(tgm
, 1);
430 /* Update the throttle configuration for a particular group. Similar
431 * to throttle_config(), but guarantees atomicity within the
434 * @tgm: a ThrottleGroupMember that is a member of the group
435 * @cfg: the configuration to set
437 void throttle_group_config(ThrottleGroupMember
*tgm
, ThrottleConfig
*cfg
)
439 ThrottleState
*ts
= tgm
->throttle_state
;
440 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
441 qemu_mutex_lock(&tg
->lock
);
442 throttle_config(ts
, tg
->clock_type
, cfg
);
443 qemu_mutex_unlock(&tg
->lock
);
445 throttle_group_restart_tgm(tgm
);
448 /* Get the throttle configuration from a particular group. Similar to
449 * throttle_get_config(), but guarantees atomicity within the
452 * @tgm: a ThrottleGroupMember that is a member of the group
453 * @cfg: the configuration will be written here
455 void throttle_group_get_config(ThrottleGroupMember
*tgm
, ThrottleConfig
*cfg
)
457 ThrottleState
*ts
= tgm
->throttle_state
;
458 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
459 qemu_mutex_lock(&tg
->lock
);
460 throttle_get_config(ts
, cfg
);
461 qemu_mutex_unlock(&tg
->lock
);
464 /* ThrottleTimers callback. This wakes up a request that was waiting
465 * because it had been throttled.
467 * @tgm: the ThrottleGroupMember whose request had been throttled
468 * @is_write: the type of operation (read/write)
470 static void timer_cb(ThrottleGroupMember
*tgm
, bool is_write
)
472 ThrottleState
*ts
= tgm
->throttle_state
;
473 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
475 /* The timer has just been fired, so we can update the flag */
476 qemu_mutex_lock(&tg
->lock
);
477 tg
->any_timer_armed
[is_write
] = false;
478 qemu_mutex_unlock(&tg
->lock
);
480 /* Run the request that was waiting for this timer */
481 throttle_group_restart_queue(tgm
, is_write
);
484 static void read_timer_cb(void *opaque
)
486 timer_cb(opaque
, false);
489 static void write_timer_cb(void *opaque
)
491 timer_cb(opaque
, true);
494 /* Register a ThrottleGroupMember from the throttling group, also initializing
495 * its timers and updating its throttle_state pointer to point to it. If a
496 * throttling group with that name does not exist yet, it will be created.
498 * This function edits throttle_groups and must be called under the global
501 * @tgm: the ThrottleGroupMember to insert
502 * @groupname: the name of the group
503 * @ctx: the AioContext to use
505 void throttle_group_register_tgm(ThrottleGroupMember
*tgm
,
506 const char *groupname
,
510 ThrottleState
*ts
= throttle_group_incref(groupname
);
511 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
513 tgm
->throttle_state
= ts
;
514 tgm
->aio_context
= ctx
;
516 qemu_mutex_lock(&tg
->lock
);
517 /* If the ThrottleGroup is new set this ThrottleGroupMember as the token */
518 for (i
= 0; i
< 2; i
++) {
519 if (!tg
->tokens
[i
]) {
524 QLIST_INSERT_HEAD(&tg
->head
, tgm
, round_robin
);
526 throttle_timers_init(&tgm
->throttle_timers
,
532 qemu_co_mutex_init(&tgm
->throttled_reqs_lock
);
533 qemu_co_queue_init(&tgm
->throttled_reqs
[0]);
534 qemu_co_queue_init(&tgm
->throttled_reqs
[1]);
536 qemu_mutex_unlock(&tg
->lock
);
539 /* Unregister a ThrottleGroupMember from its group, removing it from the list,
540 * destroying the timers and setting the throttle_state pointer to NULL.
542 * The ThrottleGroupMember must not have pending throttled requests, so the
543 * caller has to drain them first.
545 * The group will be destroyed if it's empty after this operation.
547 * @tgm the ThrottleGroupMember to remove
549 void throttle_group_unregister_tgm(ThrottleGroupMember
*tgm
)
551 ThrottleState
*ts
= tgm
->throttle_state
;
552 ThrottleGroup
*tg
= container_of(ts
, ThrottleGroup
, ts
);
553 ThrottleGroupMember
*token
;
557 /* Discard already unregistered tgm */
561 assert(tgm
->pending_reqs
[0] == 0 && tgm
->pending_reqs
[1] == 0);
562 assert(qemu_co_queue_empty(&tgm
->throttled_reqs
[0]));
563 assert(qemu_co_queue_empty(&tgm
->throttled_reqs
[1]));
565 qemu_mutex_lock(&tg
->lock
);
566 for (i
= 0; i
< 2; i
++) {
567 if (timer_pending(tgm
->throttle_timers
.timers
[i
])) {
568 tg
->any_timer_armed
[i
] = false;
569 schedule_next_request(tgm
, i
);
571 if (tg
->tokens
[i
] == tgm
) {
572 token
= throttle_group_next_tgm(tgm
);
573 /* Take care of the case where this is the last tgm in the group */
577 tg
->tokens
[i
] = token
;
581 /* remove the current tgm from the list */
582 QLIST_REMOVE(tgm
, round_robin
);
583 throttle_timers_destroy(&tgm
->throttle_timers
);
584 qemu_mutex_unlock(&tg
->lock
);
586 throttle_group_unref(&tg
->ts
);
587 tgm
->throttle_state
= NULL
;
590 void throttle_group_attach_aio_context(ThrottleGroupMember
*tgm
,
591 AioContext
*new_context
)
593 ThrottleTimers
*tt
= &tgm
->throttle_timers
;
594 throttle_timers_attach_aio_context(tt
, new_context
);
595 tgm
->aio_context
= new_context
;
598 void throttle_group_detach_aio_context(ThrottleGroupMember
*tgm
)
600 ThrottleGroup
*tg
= container_of(tgm
->throttle_state
, ThrottleGroup
, ts
);
601 ThrottleTimers
*tt
= &tgm
->throttle_timers
;
604 /* Requests must have been drained */
605 assert(tgm
->pending_reqs
[0] == 0 && tgm
->pending_reqs
[1] == 0);
606 assert(qemu_co_queue_empty(&tgm
->throttled_reqs
[0]));
607 assert(qemu_co_queue_empty(&tgm
->throttled_reqs
[1]));
609 /* Kick off next ThrottleGroupMember, if necessary */
610 qemu_mutex_lock(&tg
->lock
);
611 for (i
= 0; i
< 2; i
++) {
612 if (timer_pending(tt
->timers
[i
])) {
613 tg
->any_timer_armed
[i
] = false;
614 schedule_next_request(tgm
, i
);
617 qemu_mutex_unlock(&tg
->lock
);
619 throttle_timers_detach_aio_context(tt
);
620 tgm
->aio_context
= NULL
;
623 #undef THROTTLE_OPT_PREFIX
624 #define THROTTLE_OPT_PREFIX "x-"
626 /* Helper struct and array for QOM property setter/getter */
638 static ThrottleParamInfo properties
[] = {
640 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_TOTAL
,
641 THROTTLE_OPS_TOTAL
, AVG
,
644 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_TOTAL_MAX
,
645 THROTTLE_OPS_TOTAL
, MAX
,
648 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_TOTAL_MAX_LENGTH
,
649 THROTTLE_OPS_TOTAL
, BURST_LENGTH
,
652 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_READ
,
653 THROTTLE_OPS_READ
, AVG
,
656 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_READ_MAX
,
657 THROTTLE_OPS_READ
, MAX
,
660 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_READ_MAX_LENGTH
,
661 THROTTLE_OPS_READ
, BURST_LENGTH
,
664 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_WRITE
,
665 THROTTLE_OPS_WRITE
, AVG
,
668 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_WRITE_MAX
,
669 THROTTLE_OPS_WRITE
, MAX
,
672 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_WRITE_MAX_LENGTH
,
673 THROTTLE_OPS_WRITE
, BURST_LENGTH
,
676 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_TOTAL
,
677 THROTTLE_BPS_TOTAL
, AVG
,
680 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_TOTAL_MAX
,
681 THROTTLE_BPS_TOTAL
, MAX
,
684 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_TOTAL_MAX_LENGTH
,
685 THROTTLE_BPS_TOTAL
, BURST_LENGTH
,
688 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_READ
,
689 THROTTLE_BPS_READ
, AVG
,
692 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_READ_MAX
,
693 THROTTLE_BPS_READ
, MAX
,
696 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_READ_MAX_LENGTH
,
697 THROTTLE_BPS_READ
, BURST_LENGTH
,
700 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_WRITE
,
701 THROTTLE_BPS_WRITE
, AVG
,
704 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_WRITE_MAX
,
705 THROTTLE_BPS_WRITE
, MAX
,
708 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_WRITE_MAX_LENGTH
,
709 THROTTLE_BPS_WRITE
, BURST_LENGTH
,
712 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_SIZE
,
717 /* This function edits throttle_groups and must be called under the global
719 static void throttle_group_obj_init(Object
*obj
)
721 ThrottleGroup
*tg
= THROTTLE_GROUP(obj
);
723 tg
->clock_type
= QEMU_CLOCK_REALTIME
;
724 if (qtest_enabled()) {
725 /* For testing block IO throttling only */
726 tg
->clock_type
= QEMU_CLOCK_VIRTUAL
;
728 tg
->is_initialized
= false;
729 qemu_mutex_init(&tg
->lock
);
730 throttle_init(&tg
->ts
);
731 QLIST_INIT(&tg
->head
);
734 /* This function edits throttle_groups and must be called under the global
736 static void throttle_group_obj_complete(UserCreatable
*obj
, Error
**errp
)
738 ThrottleGroup
*tg
= THROTTLE_GROUP(obj
);
741 /* set group name to object id if it exists */
742 if (!tg
->name
&& tg
->parent_obj
.parent
) {
743 tg
->name
= object_get_canonical_path_component(OBJECT(obj
));
745 /* We must have a group name at this point */
748 /* error if name is duplicate */
749 if (throttle_group_exists(tg
->name
)) {
750 error_setg(errp
, "A group with this name already exists");
755 throttle_get_config(&tg
->ts
, &cfg
);
756 if (!throttle_is_valid(&cfg
, errp
)) {
759 throttle_config(&tg
->ts
, tg
->clock_type
, &cfg
);
760 QTAILQ_INSERT_TAIL(&throttle_groups
, tg
, list
);
761 tg
->is_initialized
= true;
764 /* This function edits throttle_groups and must be called under the global
766 static void throttle_group_obj_finalize(Object
*obj
)
768 ThrottleGroup
*tg
= THROTTLE_GROUP(obj
);
769 if (tg
->is_initialized
) {
770 QTAILQ_REMOVE(&throttle_groups
, tg
, list
);
772 qemu_mutex_destroy(&tg
->lock
);
776 static void throttle_group_set(Object
*obj
, Visitor
*v
, const char * name
,
777 void *opaque
, Error
**errp
)
780 ThrottleGroup
*tg
= THROTTLE_GROUP(obj
);
782 ThrottleParamInfo
*info
= opaque
;
783 Error
*local_err
= NULL
;
786 /* If we have finished initialization, don't accept individual property
787 * changes through QOM. Throttle configuration limits must be set in one
788 * transaction, as certain combinations are invalid.
790 if (tg
->is_initialized
) {
791 error_setg(&local_err
, "Property cannot be set after initialization");
795 visit_type_int64(v
, name
, &value
, &local_err
);
800 error_setg(&local_err
, "Property values cannot be negative");
805 switch (info
->category
) {
807 cfg
->buckets
[info
->type
].avg
= value
;
810 cfg
->buckets
[info
->type
].max
= value
;
813 if (value
> UINT_MAX
) {
814 error_setg(&local_err
, "%s value must be in the"
815 "range [0, %u]", info
->name
, UINT_MAX
);
818 cfg
->buckets
[info
->type
].burst_length
= value
;
821 cfg
->op_size
= value
;
826 error_propagate(errp
, local_err
);
831 static void throttle_group_get(Object
*obj
, Visitor
*v
, const char *name
,
832 void *opaque
, Error
**errp
)
834 ThrottleGroup
*tg
= THROTTLE_GROUP(obj
);
836 ThrottleParamInfo
*info
= opaque
;
839 throttle_get_config(&tg
->ts
, &cfg
);
840 switch (info
->category
) {
842 value
= cfg
.buckets
[info
->type
].avg
;
845 value
= cfg
.buckets
[info
->type
].max
;
848 value
= cfg
.buckets
[info
->type
].burst_length
;
855 visit_type_int64(v
, name
, &value
, errp
);
858 static void throttle_group_set_limits(Object
*obj
, Visitor
*v
,
859 const char *name
, void *opaque
,
863 ThrottleGroup
*tg
= THROTTLE_GROUP(obj
);
865 ThrottleLimits arg
= { 0 };
866 ThrottleLimits
*argp
= &arg
;
867 Error
*local_err
= NULL
;
869 visit_type_ThrottleLimits(v
, name
, &argp
, &local_err
);
873 qemu_mutex_lock(&tg
->lock
);
874 throttle_get_config(&tg
->ts
, &cfg
);
875 throttle_limits_to_config(argp
, &cfg
, &local_err
);
879 throttle_config(&tg
->ts
, tg
->clock_type
, &cfg
);
882 qemu_mutex_unlock(&tg
->lock
);
884 error_propagate(errp
, local_err
);
888 static void throttle_group_get_limits(Object
*obj
, Visitor
*v
,
889 const char *name
, void *opaque
,
892 ThrottleGroup
*tg
= THROTTLE_GROUP(obj
);
894 ThrottleLimits arg
= { 0 };
895 ThrottleLimits
*argp
= &arg
;
897 qemu_mutex_lock(&tg
->lock
);
898 throttle_get_config(&tg
->ts
, &cfg
);
899 qemu_mutex_unlock(&tg
->lock
);
901 throttle_config_to_limits(&cfg
, argp
);
903 visit_type_ThrottleLimits(v
, name
, &argp
, errp
);
906 static bool throttle_group_can_be_deleted(UserCreatable
*uc
)
908 return OBJECT(uc
)->ref
== 1;
911 static void throttle_group_obj_class_init(ObjectClass
*klass
, void *class_data
)
914 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(klass
);
916 ucc
->complete
= throttle_group_obj_complete
;
917 ucc
->can_be_deleted
= throttle_group_can_be_deleted
;
919 /* individual properties */
920 for (i
= 0; i
< sizeof(properties
) / sizeof(ThrottleParamInfo
); i
++) {
921 object_class_property_add(klass
,
926 NULL
, &properties
[i
],
931 object_class_property_add(klass
,
932 "limits", "ThrottleLimits",
933 throttle_group_get_limits
,
934 throttle_group_set_limits
,
939 static const TypeInfo throttle_group_info
= {
940 .name
= TYPE_THROTTLE_GROUP
,
941 .parent
= TYPE_OBJECT
,
942 .class_init
= throttle_group_obj_class_init
,
943 .instance_size
= sizeof(ThrottleGroup
),
944 .instance_init
= throttle_group_obj_init
,
945 .instance_finalize
= throttle_group_obj_finalize
,
946 .interfaces
= (InterfaceInfo
[]) {
947 { TYPE_USER_CREATABLE
},
952 static void throttle_groups_init(void)
954 type_register_static(&throttle_group_info
);
957 type_init(throttle_groups_init
);