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