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-visit.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 start = token = tg->tokens[is_write]; 225 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); 230 } 231 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. 235 */ 236 if (token == start && !tgm_has_pending_reqs(token, is_write)) { 237 token = tgm; 238 } 239 240 /* Either we return the original TGM, or one with pending requests */ 241 assert(token == tgm || tgm_has_pending_reqs(token, is_write)); 242 243 return token; 244 } 245 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. 249 * 250 * This assumes that tg->lock is held. 251 * 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 255 */ 256 static bool throttle_group_schedule_timer(ThrottleGroupMember *tgm, 257 bool is_write) 258 { 259 ThrottleState *ts = tgm->throttle_state; 260 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts); 261 ThrottleTimers *tt = &tgm->throttle_timers; 262 bool must_wait; 263 264 if (atomic_read(&tgm->io_limits_disabled)) { 265 return false; 266 } 267 268 /* Check if any of the timers in this group is already armed */ 269 if (tg->any_timer_armed[is_write]) { 270 return true; 271 } 272 273 must_wait = throttle_schedule_timer(ts, tt, is_write); 274 275 /* If a timer just got armed, set tgm as the current token */ 276 if (must_wait) { 277 tg->tokens[is_write] = tgm; 278 tg->any_timer_armed[is_write] = true; 279 } 280 281 return must_wait; 282 } 283 284 /* Start the next pending I/O request for a ThrottleGroupMember. Return whether 285 * any request was actually pending. 286 * 287 * @tgm: the current ThrottleGroupMember 288 * @is_write: the type of operation (read/write) 289 */ 290 static bool coroutine_fn throttle_group_co_restart_queue(ThrottleGroupMember *tgm, 291 bool is_write) 292 { 293 bool ret; 294 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); 298 299 return ret; 300 } 301 302 /* Look for the next pending I/O request and schedule it. 303 * 304 * This assumes that tg->lock is held. 305 * 306 * @tgm: the current ThrottleGroupMember 307 * @is_write: the type of operation (read/write) 308 */ 309 static void schedule_next_request(ThrottleGroupMember *tgm, bool is_write) 310 { 311 ThrottleState *ts = tgm->throttle_state; 312 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts); 313 bool must_wait; 314 ThrottleGroupMember *token; 315 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)) { 319 return; 320 } 321 322 /* Set a timer for the request if it needs to be throttled */ 323 must_wait = throttle_group_schedule_timer(token, is_write); 324 325 /* If it doesn't have to wait, queue it for immediate execution */ 326 if (!must_wait) { 327 /* Give preference to requests from the current tgm */ 328 if (qemu_in_coroutine() && 329 throttle_group_co_restart_queue(tgm, is_write)) { 330 token = tgm; 331 } else { 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; 336 } 337 tg->tokens[is_write] = token; 338 } 339 } 340 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 343 * algorithm. 344 * 345 * @tgm: the current ThrottleGroupMember 346 * @bytes: the number of bytes for this I/O 347 * @is_write: the type of operation (read/write) 348 */ 349 void coroutine_fn throttle_group_co_io_limits_intercept(ThrottleGroupMember *tgm, 350 unsigned int bytes, 351 bool is_write) 352 { 353 bool must_wait; 354 ThrottleGroupMember *token; 355 ThrottleGroup *tg = container_of(tgm->throttle_state, ThrottleGroup, ts); 356 qemu_mutex_lock(&tg->lock); 357 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); 361 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]--; 372 } 373 374 /* The I/O will be executed, so do the accounting */ 375 throttle_account(tgm->throttle_state, is_write, bytes); 376 377 /* Schedule the next request */ 378 schedule_next_request(tgm, is_write); 379 380 qemu_mutex_unlock(&tg->lock); 381 } 382 383 typedef struct { 384 ThrottleGroupMember *tgm; 385 bool is_write; 386 } RestartData; 387 388 static void coroutine_fn throttle_group_restart_queue_entry(void *opaque) 389 { 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; 395 bool empty_queue; 396 397 empty_queue = !throttle_group_co_restart_queue(tgm, is_write); 398 399 /* If the request queue was empty then we have to take care of 400 * scheduling the next one */ 401 if (empty_queue) { 402 qemu_mutex_lock(&tg->lock); 403 schedule_next_request(tgm, is_write); 404 qemu_mutex_unlock(&tg->lock); 405 } 406 407 g_free(data); 408 } 409 410 static void throttle_group_restart_queue(ThrottleGroupMember *tgm, bool is_write) 411 { 412 Coroutine *co; 413 RestartData *rd = g_new0(RestartData, 1); 414 415 rd->tgm = tgm; 416 rd->is_write = is_write; 417 418 co = qemu_coroutine_create(throttle_group_restart_queue_entry, rd); 419 aio_co_enter(tgm->aio_context, co); 420 } 421 422 void throttle_group_restart_tgm(ThrottleGroupMember *tgm) 423 { 424 if (tgm->throttle_state) { 425 throttle_group_restart_queue(tgm, 0); 426 throttle_group_restart_queue(tgm, 1); 427 } 428 } 429 430 /* Update the throttle configuration for a particular group. Similar 431 * to throttle_config(), but guarantees atomicity within the 432 * throttling group. 433 * 434 * @tgm: a ThrottleGroupMember that is a member of the group 435 * @cfg: the configuration to set 436 */ 437 void throttle_group_config(ThrottleGroupMember *tgm, ThrottleConfig *cfg) 438 { 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); 444 445 throttle_group_restart_tgm(tgm); 446 } 447 448 /* Get the throttle configuration from a particular group. Similar to 449 * throttle_get_config(), but guarantees atomicity within the 450 * throttling group. 451 * 452 * @tgm: a ThrottleGroupMember that is a member of the group 453 * @cfg: the configuration will be written here 454 */ 455 void throttle_group_get_config(ThrottleGroupMember *tgm, ThrottleConfig *cfg) 456 { 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); 462 } 463 464 /* ThrottleTimers callback. This wakes up a request that was waiting 465 * because it had been throttled. 466 * 467 * @tgm: the ThrottleGroupMember whose request had been throttled 468 * @is_write: the type of operation (read/write) 469 */ 470 static void timer_cb(ThrottleGroupMember *tgm, bool is_write) 471 { 472 ThrottleState *ts = tgm->throttle_state; 473 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts); 474 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); 479 480 /* Run the request that was waiting for this timer */ 481 throttle_group_restart_queue(tgm, is_write); 482 } 483 484 static void read_timer_cb(void *opaque) 485 { 486 timer_cb(opaque, false); 487 } 488 489 static void write_timer_cb(void *opaque) 490 { 491 timer_cb(opaque, true); 492 } 493 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. 497 * 498 * This function edits throttle_groups and must be called under the global 499 * mutex. 500 * 501 * @tgm: the ThrottleGroupMember to insert 502 * @groupname: the name of the group 503 * @ctx: the AioContext to use 504 */ 505 void throttle_group_register_tgm(ThrottleGroupMember *tgm, 506 const char *groupname, 507 AioContext *ctx) 508 { 509 int i; 510 ThrottleState *ts = throttle_group_incref(groupname); 511 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts); 512 513 tgm->throttle_state = ts; 514 tgm->aio_context = ctx; 515 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]) { 520 tg->tokens[i] = tgm; 521 } 522 } 523 524 QLIST_INSERT_HEAD(&tg->head, tgm, round_robin); 525 526 throttle_timers_init(&tgm->throttle_timers, 527 tgm->aio_context, 528 tg->clock_type, 529 read_timer_cb, 530 write_timer_cb, 531 tgm); 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]); 535 536 qemu_mutex_unlock(&tg->lock); 537 } 538 539 /* Unregister a ThrottleGroupMember from its group, removing it from the list, 540 * destroying the timers and setting the throttle_state pointer to NULL. 541 * 542 * The ThrottleGroupMember must not have pending throttled requests, so the 543 * caller has to drain them first. 544 * 545 * The group will be destroyed if it's empty after this operation. 546 * 547 * @tgm the ThrottleGroupMember to remove 548 */ 549 void throttle_group_unregister_tgm(ThrottleGroupMember *tgm) 550 { 551 ThrottleState *ts = tgm->throttle_state; 552 ThrottleGroup *tg = container_of(ts, ThrottleGroup, ts); 553 ThrottleGroupMember *token; 554 int i; 555 556 if (!ts) { 557 /* Discard already unregistered tgm */ 558 return; 559 } 560 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])); 564 565 qemu_mutex_lock(&tg->lock); 566 for (i = 0; i < 2; i++) { 567 if (tg->tokens[i] == tgm) { 568 token = throttle_group_next_tgm(tgm); 569 /* Take care of the case where this is the last tgm in the group */ 570 if (token == tgm) { 571 token = NULL; 572 } 573 tg->tokens[i] = token; 574 } 575 } 576 577 /* remove the current tgm from the list */ 578 QLIST_REMOVE(tgm, round_robin); 579 throttle_timers_destroy(&tgm->throttle_timers); 580 qemu_mutex_unlock(&tg->lock); 581 582 throttle_group_unref(&tg->ts); 583 tgm->throttle_state = NULL; 584 } 585 586 void throttle_group_attach_aio_context(ThrottleGroupMember *tgm, 587 AioContext *new_context) 588 { 589 ThrottleTimers *tt = &tgm->throttle_timers; 590 throttle_timers_attach_aio_context(tt, new_context); 591 tgm->aio_context = new_context; 592 } 593 594 void throttle_group_detach_aio_context(ThrottleGroupMember *tgm) 595 { 596 ThrottleTimers *tt = &tgm->throttle_timers; 597 throttle_timers_detach_aio_context(tt); 598 tgm->aio_context = NULL; 599 } 600 601 #undef THROTTLE_OPT_PREFIX 602 #define THROTTLE_OPT_PREFIX "x-" 603 604 /* Helper struct and array for QOM property setter/getter */ 605 typedef struct { 606 const char *name; 607 BucketType type; 608 enum { 609 AVG, 610 MAX, 611 BURST_LENGTH, 612 IOPS_SIZE, 613 } category; 614 } ThrottleParamInfo; 615 616 static ThrottleParamInfo properties[] = { 617 { 618 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_TOTAL, 619 THROTTLE_OPS_TOTAL, AVG, 620 }, 621 { 622 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_TOTAL_MAX, 623 THROTTLE_OPS_TOTAL, MAX, 624 }, 625 { 626 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_TOTAL_MAX_LENGTH, 627 THROTTLE_OPS_TOTAL, BURST_LENGTH, 628 }, 629 { 630 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_READ, 631 THROTTLE_OPS_READ, AVG, 632 }, 633 { 634 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_READ_MAX, 635 THROTTLE_OPS_READ, MAX, 636 }, 637 { 638 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_READ_MAX_LENGTH, 639 THROTTLE_OPS_READ, BURST_LENGTH, 640 }, 641 { 642 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_WRITE, 643 THROTTLE_OPS_WRITE, AVG, 644 }, 645 { 646 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_WRITE_MAX, 647 THROTTLE_OPS_WRITE, MAX, 648 }, 649 { 650 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_WRITE_MAX_LENGTH, 651 THROTTLE_OPS_WRITE, BURST_LENGTH, 652 }, 653 { 654 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_TOTAL, 655 THROTTLE_BPS_TOTAL, AVG, 656 }, 657 { 658 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_TOTAL_MAX, 659 THROTTLE_BPS_TOTAL, MAX, 660 }, 661 { 662 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_TOTAL_MAX_LENGTH, 663 THROTTLE_BPS_TOTAL, BURST_LENGTH, 664 }, 665 { 666 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_READ, 667 THROTTLE_BPS_READ, AVG, 668 }, 669 { 670 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_READ_MAX, 671 THROTTLE_BPS_READ, MAX, 672 }, 673 { 674 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_READ_MAX_LENGTH, 675 THROTTLE_BPS_READ, BURST_LENGTH, 676 }, 677 { 678 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_WRITE, 679 THROTTLE_BPS_WRITE, AVG, 680 }, 681 { 682 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_WRITE_MAX, 683 THROTTLE_BPS_WRITE, MAX, 684 }, 685 { 686 THROTTLE_OPT_PREFIX QEMU_OPT_BPS_WRITE_MAX_LENGTH, 687 THROTTLE_BPS_WRITE, BURST_LENGTH, 688 }, 689 { 690 THROTTLE_OPT_PREFIX QEMU_OPT_IOPS_SIZE, 691 0, IOPS_SIZE, 692 } 693 }; 694 695 /* This function edits throttle_groups and must be called under the global 696 * mutex */ 697 static void throttle_group_obj_init(Object *obj) 698 { 699 ThrottleGroup *tg = THROTTLE_GROUP(obj); 700 701 tg->clock_type = QEMU_CLOCK_REALTIME; 702 if (qtest_enabled()) { 703 /* For testing block IO throttling only */ 704 tg->clock_type = QEMU_CLOCK_VIRTUAL; 705 } 706 tg->is_initialized = false; 707 qemu_mutex_init(&tg->lock); 708 throttle_init(&tg->ts); 709 QLIST_INIT(&tg->head); 710 } 711 712 /* This function edits throttle_groups and must be called under the global 713 * mutex */ 714 static void throttle_group_obj_complete(UserCreatable *obj, Error **errp) 715 { 716 ThrottleGroup *tg = THROTTLE_GROUP(obj); 717 ThrottleConfig cfg; 718 719 /* set group name to object id if it exists */ 720 if (!tg->name && tg->parent_obj.parent) { 721 tg->name = object_get_canonical_path_component(OBJECT(obj)); 722 } 723 /* We must have a group name at this point */ 724 assert(tg->name); 725 726 /* error if name is duplicate */ 727 if (throttle_group_exists(tg->name)) { 728 error_setg(errp, "A group with this name already exists"); 729 return; 730 } 731 732 /* check validity */ 733 throttle_get_config(&tg->ts, &cfg); 734 if (!throttle_is_valid(&cfg, errp)) { 735 return; 736 } 737 throttle_config(&tg->ts, tg->clock_type, &cfg); 738 QTAILQ_INSERT_TAIL(&throttle_groups, tg, list); 739 tg->is_initialized = true; 740 } 741 742 /* This function edits throttle_groups and must be called under the global 743 * mutex */ 744 static void throttle_group_obj_finalize(Object *obj) 745 { 746 ThrottleGroup *tg = THROTTLE_GROUP(obj); 747 if (tg->is_initialized) { 748 QTAILQ_REMOVE(&throttle_groups, tg, list); 749 } 750 qemu_mutex_destroy(&tg->lock); 751 g_free(tg->name); 752 } 753 754 static void throttle_group_set(Object *obj, Visitor *v, const char * name, 755 void *opaque, Error **errp) 756 757 { 758 ThrottleGroup *tg = THROTTLE_GROUP(obj); 759 ThrottleConfig *cfg; 760 ThrottleParamInfo *info = opaque; 761 Error *local_err = NULL; 762 int64_t value; 763 764 /* If we have finished initialization, don't accept individual property 765 * changes through QOM. Throttle configuration limits must be set in one 766 * transaction, as certain combinations are invalid. 767 */ 768 if (tg->is_initialized) { 769 error_setg(&local_err, "Property cannot be set after initialization"); 770 goto ret; 771 } 772 773 visit_type_int64(v, name, &value, &local_err); 774 if (local_err) { 775 goto ret; 776 } 777 if (value < 0) { 778 error_setg(&local_err, "Property values cannot be negative"); 779 goto ret; 780 } 781 782 cfg = &tg->ts.cfg; 783 switch (info->category) { 784 case AVG: 785 cfg->buckets[info->type].avg = value; 786 break; 787 case MAX: 788 cfg->buckets[info->type].max = value; 789 break; 790 case BURST_LENGTH: 791 if (value > UINT_MAX) { 792 error_setg(&local_err, "%s value must be in the" 793 "range [0, %u]", info->name, UINT_MAX); 794 goto ret; 795 } 796 cfg->buckets[info->type].burst_length = value; 797 break; 798 case IOPS_SIZE: 799 cfg->op_size = value; 800 break; 801 } 802 803 ret: 804 error_propagate(errp, local_err); 805 return; 806 807 } 808 809 static void throttle_group_get(Object *obj, Visitor *v, const char *name, 810 void *opaque, Error **errp) 811 { 812 ThrottleGroup *tg = THROTTLE_GROUP(obj); 813 ThrottleConfig cfg; 814 ThrottleParamInfo *info = opaque; 815 int64_t value; 816 817 throttle_get_config(&tg->ts, &cfg); 818 switch (info->category) { 819 case AVG: 820 value = cfg.buckets[info->type].avg; 821 break; 822 case MAX: 823 value = cfg.buckets[info->type].max; 824 break; 825 case BURST_LENGTH: 826 value = cfg.buckets[info->type].burst_length; 827 break; 828 case IOPS_SIZE: 829 value = cfg.op_size; 830 break; 831 } 832 833 visit_type_int64(v, name, &value, errp); 834 } 835 836 static void throttle_group_set_limits(Object *obj, Visitor *v, 837 const char *name, void *opaque, 838 Error **errp) 839 840 { 841 ThrottleGroup *tg = THROTTLE_GROUP(obj); 842 ThrottleConfig cfg; 843 ThrottleLimits arg = { 0 }; 844 ThrottleLimits *argp = &arg; 845 Error *local_err = NULL; 846 847 visit_type_ThrottleLimits(v, name, &argp, &local_err); 848 if (local_err) { 849 goto ret; 850 } 851 qemu_mutex_lock(&tg->lock); 852 throttle_get_config(&tg->ts, &cfg); 853 throttle_limits_to_config(argp, &cfg, &local_err); 854 if (local_err) { 855 goto unlock; 856 } 857 throttle_config(&tg->ts, tg->clock_type, &cfg); 858 859 unlock: 860 qemu_mutex_unlock(&tg->lock); 861 ret: 862 error_propagate(errp, local_err); 863 return; 864 } 865 866 static void throttle_group_get_limits(Object *obj, Visitor *v, 867 const char *name, void *opaque, 868 Error **errp) 869 { 870 ThrottleGroup *tg = THROTTLE_GROUP(obj); 871 ThrottleConfig cfg; 872 ThrottleLimits arg = { 0 }; 873 ThrottleLimits *argp = &arg; 874 875 qemu_mutex_lock(&tg->lock); 876 throttle_get_config(&tg->ts, &cfg); 877 qemu_mutex_unlock(&tg->lock); 878 879 throttle_config_to_limits(&cfg, argp); 880 881 visit_type_ThrottleLimits(v, name, &argp, errp); 882 } 883 884 static bool throttle_group_can_be_deleted(UserCreatable *uc) 885 { 886 return OBJECT(uc)->ref == 1; 887 } 888 889 static void throttle_group_obj_class_init(ObjectClass *klass, void *class_data) 890 { 891 size_t i = 0; 892 UserCreatableClass *ucc = USER_CREATABLE_CLASS(klass); 893 894 ucc->complete = throttle_group_obj_complete; 895 ucc->can_be_deleted = throttle_group_can_be_deleted; 896 897 /* individual properties */ 898 for (i = 0; i < sizeof(properties) / sizeof(ThrottleParamInfo); i++) { 899 object_class_property_add(klass, 900 properties[i].name, 901 "int", 902 throttle_group_get, 903 throttle_group_set, 904 NULL, &properties[i], 905 &error_abort); 906 } 907 908 /* ThrottleLimits */ 909 object_class_property_add(klass, 910 "limits", "ThrottleLimits", 911 throttle_group_get_limits, 912 throttle_group_set_limits, 913 NULL, NULL, 914 &error_abort); 915 } 916 917 static const TypeInfo throttle_group_info = { 918 .name = TYPE_THROTTLE_GROUP, 919 .parent = TYPE_OBJECT, 920 .class_init = throttle_group_obj_class_init, 921 .instance_size = sizeof(ThrottleGroup), 922 .instance_init = throttle_group_obj_init, 923 .instance_finalize = throttle_group_obj_finalize, 924 .interfaces = (InterfaceInfo[]) { 925 { TYPE_USER_CREATABLE }, 926 { } 927 }, 928 }; 929 930 static void throttle_groups_init(void) 931 { 932 type_register_static(&throttle_group_info); 933 } 934 935 type_init(throttle_groups_init); 936