Alberto Garcia | 2ff1f2e | 2015-06-08 18:17:42 +0200 | [diff] [blame] | 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 | #ifndef THROTTLE_GROUPS_H |
| 26 | #define THROTTLE_GROUPS_H |
| 27 | |
Markus Armbruster | e2c1c34 | 2022-12-21 14:35:49 +0100 | [diff] [blame] | 28 | #include "qemu/coroutine.h" |
Alberto Garcia | 2ff1f2e | 2015-06-08 18:17:42 +0200 | [diff] [blame] | 29 | #include "qemu/throttle.h" |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 30 | #include "qom/object.h" |
Alberto Garcia | 2ff1f2e | 2015-06-08 18:17:42 +0200 | [diff] [blame] | 31 | |
Manos Pitsidianakis | 022cdc9 | 2017-08-25 16:20:23 +0300 | [diff] [blame] | 32 | /* The ThrottleGroupMember structure indicates membership in a ThrottleGroup |
| 33 | * and holds related data. |
| 34 | */ |
| 35 | |
| 36 | typedef struct ThrottleGroupMember { |
Manos Pitsidianakis | c61791f | 2017-08-25 16:20:24 +0300 | [diff] [blame] | 37 | AioContext *aio_context; |
Manos Pitsidianakis | 022cdc9 | 2017-08-25 16:20:23 +0300 | [diff] [blame] | 38 | /* throttled_reqs_lock protects the CoQueues for throttled requests. */ |
| 39 | CoMutex throttled_reqs_lock; |
| 40 | CoQueue throttled_reqs[2]; |
| 41 | |
| 42 | /* Nonzero if the I/O limits are currently being ignored; generally |
| 43 | * it is zero. Accessed with atomic operations. |
| 44 | */ |
| 45 | unsigned int io_limits_disabled; |
| 46 | |
Stefan Hajnoczi | bc19a0a | 2019-01-14 13:32:56 +0000 | [diff] [blame] | 47 | /* Number of pending throttle_group_restart_queue_entry() coroutines. |
| 48 | * Accessed with atomic operations. |
| 49 | */ |
| 50 | unsigned int restart_pending; |
| 51 | |
Manos Pitsidianakis | 022cdc9 | 2017-08-25 16:20:23 +0300 | [diff] [blame] | 52 | /* The following fields are protected by the ThrottleGroup lock. |
| 53 | * See the ThrottleGroup documentation for details. |
| 54 | * throttle_state tells us if I/O limits are configured. */ |
| 55 | ThrottleState *throttle_state; |
| 56 | ThrottleTimers throttle_timers; |
| 57 | unsigned pending_reqs[2]; |
| 58 | QLIST_ENTRY(ThrottleGroupMember) round_robin; |
| 59 | |
| 60 | } ThrottleGroupMember; |
| 61 | |
Manos Pitsidianakis | 432d889 | 2017-08-25 16:20:26 +0300 | [diff] [blame] | 62 | #define TYPE_THROTTLE_GROUP "throttle-group" |
Eduardo Habkost | 8063396 | 2020-09-16 14:25:19 -0400 | [diff] [blame] | 63 | OBJECT_DECLARE_SIMPLE_TYPE(ThrottleGroup, THROTTLE_GROUP) |
Manos Pitsidianakis | 432d889 | 2017-08-25 16:20:26 +0300 | [diff] [blame] | 64 | |
Manos Pitsidianakis | 022cdc9 | 2017-08-25 16:20:23 +0300 | [diff] [blame] | 65 | const char *throttle_group_get_name(ThrottleGroupMember *tgm); |
Alberto Garcia | 2ff1f2e | 2015-06-08 18:17:42 +0200 | [diff] [blame] | 66 | |
Max Reitz | 973f2dd | 2015-10-19 17:53:23 +0200 | [diff] [blame] | 67 | ThrottleState *throttle_group_incref(const char *name); |
| 68 | void throttle_group_unref(ThrottleState *ts); |
| 69 | |
Manos Pitsidianakis | 022cdc9 | 2017-08-25 16:20:23 +0300 | [diff] [blame] | 70 | void throttle_group_config(ThrottleGroupMember *tgm, ThrottleConfig *cfg); |
| 71 | void throttle_group_get_config(ThrottleGroupMember *tgm, ThrottleConfig *cfg); |
Alberto Garcia | 2ff1f2e | 2015-06-08 18:17:42 +0200 | [diff] [blame] | 72 | |
Manos Pitsidianakis | 022cdc9 | 2017-08-25 16:20:23 +0300 | [diff] [blame] | 73 | void throttle_group_register_tgm(ThrottleGroupMember *tgm, |
Manos Pitsidianakis | c61791f | 2017-08-25 16:20:24 +0300 | [diff] [blame] | 74 | const char *groupname, |
| 75 | AioContext *ctx); |
Manos Pitsidianakis | 022cdc9 | 2017-08-25 16:20:23 +0300 | [diff] [blame] | 76 | void throttle_group_unregister_tgm(ThrottleGroupMember *tgm); |
| 77 | void throttle_group_restart_tgm(ThrottleGroupMember *tgm); |
Alberto Garcia | 2ff1f2e | 2015-06-08 18:17:42 +0200 | [diff] [blame] | 78 | |
Manos Pitsidianakis | 022cdc9 | 2017-08-25 16:20:23 +0300 | [diff] [blame] | 79 | void coroutine_fn throttle_group_co_io_limits_intercept(ThrottleGroupMember *tgm, |
Vladimir Sementsov-Ogievskiy | 801625e | 2020-12-11 21:39:24 +0300 | [diff] [blame] | 80 | int64_t bytes, |
Alberto Garcia | 76f4afb | 2015-06-08 18:17:44 +0200 | [diff] [blame] | 81 | bool is_write); |
Manos Pitsidianakis | c61791f | 2017-08-25 16:20:24 +0300 | [diff] [blame] | 82 | void throttle_group_attach_aio_context(ThrottleGroupMember *tgm, |
| 83 | AioContext *new_context); |
| 84 | void throttle_group_detach_aio_context(ThrottleGroupMember *tgm); |
Manos Pitsidianakis | d8e7d87 | 2017-08-25 16:20:27 +0300 | [diff] [blame] | 85 | /* |
| 86 | * throttle_group_exists() must be called under the global |
| 87 | * mutex. |
| 88 | */ |
| 89 | bool throttle_group_exists(const char *name); |
Alberto Garcia | 76f4afb | 2015-06-08 18:17:44 +0200 | [diff] [blame] | 90 | |
Alberto Garcia | 2ff1f2e | 2015-06-08 18:17:42 +0200 | [diff] [blame] | 91 | #endif |