2 * QEMU block throttling filter driver infrastructure
4 * Copyright (c) 2017 Manos Pitsidianakis
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "block/block-io.h"
22 #include "block/block_int.h"
23 #include "block/throttle-groups.h"
24 #include "qemu/module.h"
25 #include "qemu/option.h"
26 #include "qemu/throttle-options.h"
27 #include "qapi/error.h"
29 static QemuOptsList throttle_opts
= {
31 .head
= QTAILQ_HEAD_INITIALIZER(throttle_opts
.head
),
34 .name
= QEMU_OPT_THROTTLE_GROUP_NAME
,
35 .type
= QEMU_OPT_STRING
,
36 .help
= "Name of the throttle group",
43 * If this function succeeds then the throttle group name is stored in
44 * @group and must be freed by the caller.
45 * If there's an error then @group remains unmodified.
47 static int throttle_parse_options(QDict
*options
, char **group
, Error
**errp
)
50 const char *group_name
;
51 QemuOpts
*opts
= qemu_opts_create(&throttle_opts
, NULL
, 0, &error_abort
);
53 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
58 group_name
= qemu_opt_get(opts
, QEMU_OPT_THROTTLE_GROUP_NAME
);
60 error_setg(errp
, "Please specify a throttle group");
63 } else if (!throttle_group_exists(group_name
)) {
64 error_setg(errp
, "Throttle group '%s' does not exist", group_name
);
69 *group
= g_strdup(group_name
);
76 static int throttle_open(BlockDriverState
*bs
, QDict
*options
,
77 int flags
, Error
**errp
)
79 ThrottleGroupMember
*tgm
= bs
->opaque
;
83 ret
= bdrv_open_file_child(NULL
, options
, "file", bs
, errp
);
87 bs
->supported_write_flags
= bs
->file
->bs
->supported_write_flags
|
88 BDRV_REQ_WRITE_UNCHANGED
;
89 bs
->supported_zero_flags
= bs
->file
->bs
->supported_zero_flags
|
90 BDRV_REQ_WRITE_UNCHANGED
;
92 ret
= throttle_parse_options(options
, &group
, errp
);
94 /* Register membership to group with name group_name */
95 throttle_group_register_tgm(tgm
, group
, bdrv_get_aio_context(bs
));
102 static void throttle_close(BlockDriverState
*bs
)
104 ThrottleGroupMember
*tgm
= bs
->opaque
;
105 throttle_group_unregister_tgm(tgm
);
109 static int64_t coroutine_fn
throttle_co_getlength(BlockDriverState
*bs
)
111 return bdrv_co_getlength(bs
->file
->bs
);
114 static int coroutine_fn GRAPH_RDLOCK
115 throttle_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
116 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
119 ThrottleGroupMember
*tgm
= bs
->opaque
;
120 throttle_group_co_io_limits_intercept(tgm
, bytes
, false);
122 return bdrv_co_preadv(bs
->file
, offset
, bytes
, qiov
, flags
);
125 static int coroutine_fn GRAPH_RDLOCK
126 throttle_co_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
127 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
129 ThrottleGroupMember
*tgm
= bs
->opaque
;
130 throttle_group_co_io_limits_intercept(tgm
, bytes
, true);
132 return bdrv_co_pwritev(bs
->file
, offset
, bytes
, qiov
, flags
);
135 static int coroutine_fn GRAPH_RDLOCK
136 throttle_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
137 BdrvRequestFlags flags
)
139 ThrottleGroupMember
*tgm
= bs
->opaque
;
140 throttle_group_co_io_limits_intercept(tgm
, bytes
, true);
142 return bdrv_co_pwrite_zeroes(bs
->file
, offset
, bytes
, flags
);
145 static int coroutine_fn GRAPH_RDLOCK
146 throttle_co_pdiscard(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
148 ThrottleGroupMember
*tgm
= bs
->opaque
;
149 throttle_group_co_io_limits_intercept(tgm
, bytes
, true);
151 return bdrv_co_pdiscard(bs
->file
, offset
, bytes
);
154 static int coroutine_fn GRAPH_RDLOCK
155 throttle_co_pwritev_compressed(BlockDriverState
*bs
, int64_t offset
,
156 int64_t bytes
, QEMUIOVector
*qiov
)
158 return throttle_co_pwritev(bs
, offset
, bytes
, qiov
,
159 BDRV_REQ_WRITE_COMPRESSED
);
162 static int coroutine_fn GRAPH_RDLOCK
throttle_co_flush(BlockDriverState
*bs
)
164 return bdrv_co_flush(bs
->file
->bs
);
167 static void throttle_detach_aio_context(BlockDriverState
*bs
)
169 ThrottleGroupMember
*tgm
= bs
->opaque
;
170 throttle_group_detach_aio_context(tgm
);
173 static void throttle_attach_aio_context(BlockDriverState
*bs
,
174 AioContext
*new_context
)
176 ThrottleGroupMember
*tgm
= bs
->opaque
;
177 throttle_group_attach_aio_context(tgm
, new_context
);
180 static int throttle_reopen_prepare(BDRVReopenState
*reopen_state
,
181 BlockReopenQueue
*queue
, Error
**errp
)
186 assert(reopen_state
!= NULL
);
187 assert(reopen_state
->bs
!= NULL
);
189 ret
= throttle_parse_options(reopen_state
->options
, &group
, errp
);
190 reopen_state
->opaque
= group
;
194 static void throttle_reopen_commit(BDRVReopenState
*reopen_state
)
196 BlockDriverState
*bs
= reopen_state
->bs
;
197 ThrottleGroupMember
*tgm
= bs
->opaque
;
198 char *group
= reopen_state
->opaque
;
202 if (strcmp(group
, throttle_group_get_name(tgm
))) {
203 throttle_group_unregister_tgm(tgm
);
204 throttle_group_register_tgm(tgm
, group
, bdrv_get_aio_context(bs
));
206 g_free(reopen_state
->opaque
);
207 reopen_state
->opaque
= NULL
;
210 static void throttle_reopen_abort(BDRVReopenState
*reopen_state
)
212 g_free(reopen_state
->opaque
);
213 reopen_state
->opaque
= NULL
;
216 static void throttle_drain_begin(BlockDriverState
*bs
)
218 ThrottleGroupMember
*tgm
= bs
->opaque
;
219 if (qatomic_fetch_inc(&tgm
->io_limits_disabled
) == 0) {
220 throttle_group_restart_tgm(tgm
);
224 static void throttle_drain_end(BlockDriverState
*bs
)
226 ThrottleGroupMember
*tgm
= bs
->opaque
;
227 assert(tgm
->io_limits_disabled
);
228 qatomic_dec(&tgm
->io_limits_disabled
);
231 static const char *const throttle_strong_runtime_opts
[] = {
232 QEMU_OPT_THROTTLE_GROUP_NAME
,
237 static BlockDriver bdrv_throttle
= {
238 .format_name
= "throttle",
239 .instance_size
= sizeof(ThrottleGroupMember
),
241 .bdrv_open
= throttle_open
,
242 .bdrv_close
= throttle_close
,
243 .bdrv_co_flush
= throttle_co_flush
,
245 .bdrv_child_perm
= bdrv_default_perms
,
247 .bdrv_co_getlength
= throttle_co_getlength
,
249 .bdrv_co_preadv
= throttle_co_preadv
,
250 .bdrv_co_pwritev
= throttle_co_pwritev
,
252 .bdrv_co_pwrite_zeroes
= throttle_co_pwrite_zeroes
,
253 .bdrv_co_pdiscard
= throttle_co_pdiscard
,
254 .bdrv_co_pwritev_compressed
= throttle_co_pwritev_compressed
,
256 .bdrv_attach_aio_context
= throttle_attach_aio_context
,
257 .bdrv_detach_aio_context
= throttle_detach_aio_context
,
259 .bdrv_reopen_prepare
= throttle_reopen_prepare
,
260 .bdrv_reopen_commit
= throttle_reopen_commit
,
261 .bdrv_reopen_abort
= throttle_reopen_abort
,
263 .bdrv_drain_begin
= throttle_drain_begin
,
264 .bdrv_drain_end
= throttle_drain_end
,
267 .strong_runtime_opts
= throttle_strong_runtime_opts
,
270 static void bdrv_throttle_init(void)
272 bdrv_register(&bdrv_throttle
);
275 block_init(bdrv_throttle_init
);