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