/src/qemu/block/throttle.c
Line | Count | Source |
1 | | /* |
2 | | * QEMU block throttling filter driver infrastructure |
3 | | * |
4 | | * Copyright (c) 2017 Manos Pitsidianakis |
5 | | * |
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. |
10 | | * |
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. |
15 | | * |
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/>. |
18 | | */ |
19 | | |
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" |
28 | | |
29 | | static QemuOptsList throttle_opts = { |
30 | | .name = "throttle", |
31 | | .head = QTAILQ_HEAD_INITIALIZER(throttle_opts.head), |
32 | | .desc = { |
33 | | { |
34 | | .name = QEMU_OPT_THROTTLE_GROUP_NAME, |
35 | | .type = QEMU_OPT_STRING, |
36 | | .help = "Name of the throttle group", |
37 | | }, |
38 | | { /* end of list */ } |
39 | | }, |
40 | | }; |
41 | | |
42 | | /* |
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. |
46 | | */ |
47 | | static int throttle_parse_options(QDict *options, char **group, Error **errp) |
48 | 0 | { |
49 | 0 | int ret; |
50 | 0 | const char *group_name; |
51 | 0 | QemuOpts *opts = qemu_opts_create(&throttle_opts, NULL, 0, &error_abort); |
52 | |
|
53 | 0 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { |
54 | 0 | ret = -EINVAL; |
55 | 0 | goto fin; |
56 | 0 | } |
57 | | |
58 | 0 | group_name = qemu_opt_get(opts, QEMU_OPT_THROTTLE_GROUP_NAME); |
59 | 0 | if (!group_name) { |
60 | 0 | error_setg(errp, "Please specify a throttle group"); |
61 | 0 | ret = -EINVAL; |
62 | 0 | goto fin; |
63 | 0 | } else if (!throttle_group_exists(group_name)) { |
64 | 0 | error_setg(errp, "Throttle group '%s' does not exist", group_name); |
65 | 0 | ret = -EINVAL; |
66 | 0 | goto fin; |
67 | 0 | } |
68 | | |
69 | 0 | *group = g_strdup(group_name); |
70 | 0 | ret = 0; |
71 | 0 | fin: |
72 | 0 | qemu_opts_del(opts); |
73 | 0 | return ret; |
74 | 0 | } |
75 | | |
76 | | static int throttle_open(BlockDriverState *bs, QDict *options, |
77 | | int flags, Error **errp) |
78 | 0 | { |
79 | 0 | ThrottleGroupMember *tgm = bs->opaque; |
80 | 0 | char *group; |
81 | 0 | int ret; |
82 | |
|
83 | 0 | ret = bdrv_open_file_child(NULL, options, "file", bs, errp); |
84 | 0 | if (ret < 0) { |
85 | 0 | return ret; |
86 | 0 | } |
87 | | |
88 | 0 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
89 | |
|
90 | 0 | bs->supported_write_flags = bs->file->bs->supported_write_flags | |
91 | 0 | BDRV_REQ_WRITE_UNCHANGED; |
92 | 0 | bs->supported_zero_flags = bs->file->bs->supported_zero_flags | |
93 | 0 | BDRV_REQ_WRITE_UNCHANGED; |
94 | |
|
95 | 0 | ret = throttle_parse_options(options, &group, errp); |
96 | 0 | if (ret == 0) { |
97 | | /* Register membership to group with name group_name */ |
98 | 0 | throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs)); |
99 | 0 | g_free(group); |
100 | 0 | } |
101 | |
|
102 | 0 | return ret; |
103 | 0 | } |
104 | | |
105 | | static void throttle_close(BlockDriverState *bs) |
106 | 0 | { |
107 | 0 | ThrottleGroupMember *tgm = bs->opaque; |
108 | 0 | throttle_group_unregister_tgm(tgm); |
109 | 0 | } |
110 | | |
111 | | |
112 | | static int64_t coroutine_fn GRAPH_RDLOCK |
113 | | throttle_co_getlength(BlockDriverState *bs) |
114 | 0 | { |
115 | 0 | return bdrv_co_getlength(bs->file->bs); |
116 | 0 | } |
117 | | |
118 | | static int coroutine_fn GRAPH_RDLOCK |
119 | | throttle_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, |
120 | | QEMUIOVector *qiov, BdrvRequestFlags flags) |
121 | 0 | { |
122 | |
|
123 | 0 | ThrottleGroupMember *tgm = bs->opaque; |
124 | 0 | throttle_group_co_io_limits_intercept(tgm, bytes, THROTTLE_READ); |
125 | |
|
126 | 0 | return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); |
127 | 0 | } |
128 | | |
129 | | static int coroutine_fn GRAPH_RDLOCK |
130 | | throttle_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, |
131 | | QEMUIOVector *qiov, BdrvRequestFlags flags) |
132 | 0 | { |
133 | 0 | ThrottleGroupMember *tgm = bs->opaque; |
134 | 0 | throttle_group_co_io_limits_intercept(tgm, bytes, THROTTLE_WRITE); |
135 | |
|
136 | 0 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); |
137 | 0 | } |
138 | | |
139 | | static int coroutine_fn GRAPH_RDLOCK |
140 | | throttle_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes, |
141 | | BdrvRequestFlags flags) |
142 | 0 | { |
143 | 0 | ThrottleGroupMember *tgm = bs->opaque; |
144 | 0 | throttle_group_co_io_limits_intercept(tgm, bytes, THROTTLE_WRITE); |
145 | |
|
146 | 0 | return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); |
147 | 0 | } |
148 | | |
149 | | static int coroutine_fn GRAPH_RDLOCK |
150 | | throttle_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes) |
151 | 0 | { |
152 | 0 | ThrottleGroupMember *tgm = bs->opaque; |
153 | 0 | throttle_group_co_io_limits_intercept(tgm, bytes, THROTTLE_WRITE); |
154 | |
|
155 | 0 | return bdrv_co_pdiscard(bs->file, offset, bytes); |
156 | 0 | } |
157 | | |
158 | | static int coroutine_fn GRAPH_RDLOCK |
159 | | throttle_co_pwritev_compressed(BlockDriverState *bs, int64_t offset, |
160 | | int64_t bytes, QEMUIOVector *qiov) |
161 | 0 | { |
162 | 0 | return throttle_co_pwritev(bs, offset, bytes, qiov, |
163 | 0 | BDRV_REQ_WRITE_COMPRESSED); |
164 | 0 | } |
165 | | |
166 | | static int coroutine_fn GRAPH_RDLOCK throttle_co_flush(BlockDriverState *bs) |
167 | 0 | { |
168 | 0 | return bdrv_co_flush(bs->file->bs); |
169 | 0 | } |
170 | | |
171 | | static void throttle_detach_aio_context(BlockDriverState *bs) |
172 | 0 | { |
173 | 0 | ThrottleGroupMember *tgm = bs->opaque; |
174 | 0 | throttle_group_detach_aio_context(tgm); |
175 | 0 | } |
176 | | |
177 | | static void throttle_attach_aio_context(BlockDriverState *bs, |
178 | | AioContext *new_context) |
179 | 0 | { |
180 | 0 | ThrottleGroupMember *tgm = bs->opaque; |
181 | 0 | throttle_group_attach_aio_context(tgm, new_context); |
182 | 0 | } |
183 | | |
184 | | static int throttle_reopen_prepare(BDRVReopenState *reopen_state, |
185 | | BlockReopenQueue *queue, Error **errp) |
186 | 0 | { |
187 | 0 | int ret; |
188 | 0 | char *group = NULL; |
189 | |
|
190 | 0 | assert(reopen_state != NULL); |
191 | 0 | assert(reopen_state->bs != NULL); |
192 | |
|
193 | 0 | ret = throttle_parse_options(reopen_state->options, &group, errp); |
194 | 0 | reopen_state->opaque = group; |
195 | 0 | return ret; |
196 | 0 | } |
197 | | |
198 | | static void throttle_reopen_commit(BDRVReopenState *reopen_state) |
199 | 0 | { |
200 | 0 | BlockDriverState *bs = reopen_state->bs; |
201 | 0 | ThrottleGroupMember *tgm = bs->opaque; |
202 | 0 | char *group = reopen_state->opaque; |
203 | |
|
204 | 0 | assert(group); |
205 | |
|
206 | 0 | if (strcmp(group, throttle_group_get_name(tgm))) { |
207 | 0 | throttle_group_unregister_tgm(tgm); |
208 | 0 | throttle_group_register_tgm(tgm, group, bdrv_get_aio_context(bs)); |
209 | 0 | } |
210 | 0 | g_free(reopen_state->opaque); |
211 | 0 | reopen_state->opaque = NULL; |
212 | 0 | } |
213 | | |
214 | | static void throttle_reopen_abort(BDRVReopenState *reopen_state) |
215 | 0 | { |
216 | 0 | g_free(reopen_state->opaque); |
217 | 0 | reopen_state->opaque = NULL; |
218 | 0 | } |
219 | | |
220 | | static void throttle_drain_begin(BlockDriverState *bs) |
221 | 0 | { |
222 | 0 | ThrottleGroupMember *tgm = bs->opaque; |
223 | 0 | if (qatomic_fetch_inc(&tgm->io_limits_disabled) == 0) { |
224 | 0 | throttle_group_restart_tgm(tgm); |
225 | 0 | } |
226 | 0 | } |
227 | | |
228 | | static void throttle_drain_end(BlockDriverState *bs) |
229 | 0 | { |
230 | 0 | ThrottleGroupMember *tgm = bs->opaque; |
231 | 0 | assert(tgm->io_limits_disabled); |
232 | 0 | qatomic_dec(&tgm->io_limits_disabled); |
233 | 0 | } |
234 | | |
235 | | static const char *const throttle_strong_runtime_opts[] = { |
236 | | QEMU_OPT_THROTTLE_GROUP_NAME, |
237 | | |
238 | | NULL |
239 | | }; |
240 | | |
241 | | static BlockDriver bdrv_throttle = { |
242 | | .format_name = "throttle", |
243 | | .instance_size = sizeof(ThrottleGroupMember), |
244 | | |
245 | | .bdrv_open = throttle_open, |
246 | | .bdrv_close = throttle_close, |
247 | | .bdrv_co_flush = throttle_co_flush, |
248 | | |
249 | | .bdrv_child_perm = bdrv_default_perms, |
250 | | |
251 | | .bdrv_co_getlength = throttle_co_getlength, |
252 | | |
253 | | .bdrv_co_preadv = throttle_co_preadv, |
254 | | .bdrv_co_pwritev = throttle_co_pwritev, |
255 | | |
256 | | .bdrv_co_pwrite_zeroes = throttle_co_pwrite_zeroes, |
257 | | .bdrv_co_pdiscard = throttle_co_pdiscard, |
258 | | .bdrv_co_pwritev_compressed = throttle_co_pwritev_compressed, |
259 | | |
260 | | .bdrv_attach_aio_context = throttle_attach_aio_context, |
261 | | .bdrv_detach_aio_context = throttle_detach_aio_context, |
262 | | |
263 | | .bdrv_reopen_prepare = throttle_reopen_prepare, |
264 | | .bdrv_reopen_commit = throttle_reopen_commit, |
265 | | .bdrv_reopen_abort = throttle_reopen_abort, |
266 | | |
267 | | .bdrv_drain_begin = throttle_drain_begin, |
268 | | .bdrv_drain_end = throttle_drain_end, |
269 | | |
270 | | .is_filter = true, |
271 | | .strong_runtime_opts = throttle_strong_runtime_opts, |
272 | | }; |
273 | | |
274 | | static void bdrv_throttle_init(void) |
275 | 61 | { |
276 | 61 | bdrv_register(&bdrv_throttle); |
277 | 61 | } |
278 | | |
279 | | block_init(bdrv_throttle_init); |