3 # Tests for IO throttling
5 # Copyright (C) 2015 Red Hat, Inc.
6 # Copyright (C) 2015-2016 Igalia, S.L.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 nsec_per_sec = 1000000000
26 class ThrottleTestCase(iotests.QMPTestCase):
27 test_driver = "null-aio"
30 def blockstats(self, device):
31 result = self.vm.qmp("query-blockstats")
32 for r in result['return']:
33 if r['device'] == device:
35 return stat['rd_bytes'], stat['rd_operations'], stat['wr_bytes'], stat['wr_operations']
36 raise Exception("Device not found for blockstats: %s" % device)
38 def required_drivers(self):
39 return [self.test_driver]
41 @iotests.skip_if_unsupported(required_drivers)
43 self.vm = iotests.VM()
44 for i in range(0, self.max_drives):
45 self.vm.add_drive(self.test_driver + "://", "file.read-zeroes=on")
51 def configure_throttle(self, ndrives, params):
52 params['group'] = 'test'
54 # Set the I/O throttling parameters to all drives
55 for i in range(0, ndrives):
56 params['device'] = 'drive%d' % i
57 result = self.vm.qmp("block_set_io_throttle", conv_keys=False, **params)
58 self.assert_qmp(result, 'return', {})
60 def do_test_throttle(self, ndrives, seconds, params, first_drive = 0):
61 def check_limit(limit, num):
62 # IO throttling algorithm is discrete, allow 10% error so the test
64 return limit == 0 or \
65 (num < seconds * limit * 1.1 / ndrives
66 and num > seconds * limit * 0.9 / ndrives)
68 # Set vm clock to a known value
69 ns = seconds * nsec_per_sec
70 self.vm.qtest("clock_step %d" % ns)
72 # Submit enough requests so the throttling mechanism kicks
73 # in. The throttled requests won't be executed until we
74 # advance the virtual clock.
76 rd_nr = max(params['bps'] // rq_size // 2,
77 params['bps_rd'] // rq_size,
82 wr_nr = max(params['bps'] // rq_size // 2,
83 params['bps_wr'] // rq_size,
89 # Send I/O requests to all drives
90 for i in range(rd_nr):
91 for drive in range(0, ndrives):
92 idx = first_drive + drive
93 self.vm.hmp_qemu_io("drive%d" % idx, "aio_read %d %d" %
94 (i * rq_size, rq_size))
96 for i in range(wr_nr):
97 for drive in range(0, ndrives):
98 idx = first_drive + drive
99 self.vm.hmp_qemu_io("drive%d" % idx, "aio_write %d %d" %
100 (i * rq_size, rq_size))
102 # We'll store the I/O stats for each drive in these arrays
103 start_rd_bytes = [0] * ndrives
104 start_rd_iops = [0] * ndrives
105 start_wr_bytes = [0] * ndrives
106 start_wr_iops = [0] * ndrives
107 end_rd_bytes = [0] * ndrives
108 end_rd_iops = [0] * ndrives
109 end_wr_bytes = [0] * ndrives
110 end_wr_iops = [0] * ndrives
112 # Read the stats before advancing the clock
113 for i in range(0, ndrives):
114 idx = first_drive + i
115 start_rd_bytes[i], start_rd_iops[i], start_wr_bytes[i], \
116 start_wr_iops[i] = self.blockstats('drive%d' % idx)
118 self.vm.qtest("clock_step %d" % ns)
120 # Read the stats after advancing the clock
121 for i in range(0, ndrives):
122 idx = first_drive + i
123 end_rd_bytes[i], end_rd_iops[i], end_wr_bytes[i], \
124 end_wr_iops[i] = self.blockstats('drive%d' % idx)
126 # Check that the I/O is within the limits and evenly distributed
127 for i in range(0, ndrives):
128 rd_bytes = end_rd_bytes[i] - start_rd_bytes[i]
129 rd_iops = end_rd_iops[i] - start_rd_iops[i]
130 wr_bytes = end_wr_bytes[i] - start_wr_bytes[i]
131 wr_iops = end_wr_iops[i] - start_wr_iops[i]
133 self.assertTrue(check_limit(params['bps'], rd_bytes + wr_bytes))
134 self.assertTrue(check_limit(params['bps_rd'], rd_bytes))
135 self.assertTrue(check_limit(params['bps_wr'], wr_bytes))
136 self.assertTrue(check_limit(params['iops'], rd_iops + wr_iops))
137 self.assertTrue(check_limit(params['iops_rd'], rd_iops))
138 self.assertTrue(check_limit(params['iops_wr'], wr_iops))
140 # Allow remaining requests to finish. We submitted twice as many to
141 # ensure the throttle limit is reached.
142 self.vm.qtest("clock_step %d" % ns)
144 # Connect N drives to a VM and test I/O in all of them
146 params = {"bps": 4096,
153 # Repeat the test with different numbers of drives
154 for ndrives in range(1, self.max_drives + 1):
155 # Pick each out of all possible params and test
157 limits = dict([(k, 0) for k in params])
158 limits[tk] = params[tk] * ndrives
159 self.configure_throttle(ndrives, limits)
160 self.do_test_throttle(ndrives, 5, limits)
162 # Connect N drives to a VM and test I/O in just one of them a time
164 params = {"bps": 4096,
171 # Repeat the test for each one of the drives
172 for drive in range(0, self.max_drives):
173 # Pick each out of all possible params and test
175 limits = dict([(k, 0) for k in params])
176 limits[tk] = params[tk] * self.max_drives
177 self.configure_throttle(self.max_drives, limits)
178 self.do_test_throttle(1, 5, limits, drive)
180 def test_burst(self):
181 params = {"bps": 4096,
189 # Pick each out of all possible params and test
191 rate = params[tk] * ndrives
192 burst_rate = rate * 7
195 # Configure the throttling settings
196 settings = dict([(k, 0) for k in params])
198 settings['%s_max' % tk] = burst_rate
199 settings['%s_max_length' % tk] = burst_length
200 self.configure_throttle(ndrives, settings)
202 # Wait for the bucket to empty so we can do bursts
203 wait_ns = nsec_per_sec * burst_length * burst_rate // rate
204 self.vm.qtest("clock_step %d" % wait_ns)
206 # Test I/O at the max burst rate
207 limits = dict([(k, 0) for k in params])
208 limits[tk] = burst_rate
209 self.do_test_throttle(ndrives, burst_length, limits)
211 # Now test I/O at the normal rate
213 self.do_test_throttle(ndrives, 5, limits)
215 # Test that removing a drive from a throttle group should not
216 # affect the remaining members of the group.
217 # https://bugzilla.redhat.com/show_bug.cgi?id=1535914
218 def test_remove_group_member(self):
219 # Create a throttle group with two drives
220 # and set a 4 KB/s read limit.
227 self.configure_throttle(2, params)
229 # Read 4KB from drive0. This is performed immediately.
230 self.vm.hmp_qemu_io("drive0", "aio_read 0 4096")
232 # Read 2KB. The I/O limit has been exceeded so this
233 # request is throttled and a timer is set to wake it up.
234 self.vm.hmp_qemu_io("drive0", "aio_read 0 2048")
236 # Read 2KB again. We're still over the I/O limit so this is
237 # request is also throttled, but no new timer is set since
238 # there's already one.
239 self.vm.hmp_qemu_io("drive0", "aio_read 0 2048")
241 # Read from drive1. This request is also throttled, and no
242 # timer is set in drive1 because there's already one in
244 self.vm.hmp_qemu_io("drive1", "aio_read 0 4096")
246 # At this point only the first 4KB have been read from drive0.
247 # The other requests are throttled.
248 self.assertEqual(self.blockstats('drive0')[0], 4096)
249 self.assertEqual(self.blockstats('drive1')[0], 0)
251 # Remove drive0 from the throttle group and disable its I/O limits.
252 # drive1 remains in the group with a throttled request.
254 params['device'] = 'drive0'
255 result = self.vm.qmp("block_set_io_throttle", conv_keys=False, **params)
256 self.assert_qmp(result, 'return', {})
258 # Removing the I/O limits from drive0 drains its two pending requests.
259 # The read request in drive1 is still throttled.
260 self.assertEqual(self.blockstats('drive0')[0], 8192)
261 self.assertEqual(self.blockstats('drive1')[0], 0)
263 # Advance the clock 5 seconds. This completes the request in drive1
264 self.vm.qtest("clock_step %d" % (5 * nsec_per_sec))
266 # Now all requests have been processed.
267 self.assertEqual(self.blockstats('drive0')[0], 8192)
268 self.assertEqual(self.blockstats('drive1')[0], 4096)
270 class ThrottleTestCoroutine(ThrottleTestCase):
271 test_driver = "null-co"
273 class ThrottleTestGroupNames(iotests.QMPTestCase):
277 self.vm = iotests.VM()
278 for i in range(0, self.max_drives):
279 self.vm.add_drive("null-co://",
280 "throttling.iops-total=100,file.read-zeroes=on")
286 def set_io_throttle(self, device, params):
287 params["device"] = device
288 result = self.vm.qmp("block_set_io_throttle", conv_keys=False, **params)
289 self.assert_qmp(result, 'return', {})
291 def verify_name(self, device, name):
292 result = self.vm.qmp("query-block")
293 for r in result["return"]:
294 if r["device"] == device:
297 self.assertEqual(info["group"], name)
299 self.assertFalse('group' in info)
302 raise Exception("No group information found for '%s'" % device)
304 def test_group_naming(self):
312 # Check the drives added using the command line.
313 # The default throttling group name is the device name.
314 for i in range(self.max_drives):
315 devname = "drive%d" % i
316 self.verify_name(devname, devname)
318 # Clear throttling settings => the group name is gone.
319 for i in range(self.max_drives):
320 devname = "drive%d" % i
321 self.set_io_throttle(devname, params)
322 self.verify_name(devname, None)
324 # Set throttling settings using block_set_io_throttle and
325 # check the default group names.
327 for i in range(self.max_drives):
328 devname = "drive%d" % i
329 self.set_io_throttle(devname, params)
330 self.verify_name(devname, devname)
332 # Set a custom group name for each device
334 devname = "drive%d" % i
335 groupname = "group%d" % i
336 params['group'] = groupname
337 self.set_io_throttle(devname, params)
338 self.verify_name(devname, groupname)
340 # Put drive0 in group1 and check that all other devices remain
342 params['group'] = 'group1'
343 self.set_io_throttle('drive0', params)
344 self.verify_name('drive0', 'group1')
345 for i in range(1, self.max_drives):
346 devname = "drive%d" % i
347 groupname = "group%d" % i
348 self.verify_name(devname, groupname)
350 # Put drive0 in group2 and check that all other devices remain
352 params['group'] = 'group2'
353 self.set_io_throttle('drive0', params)
354 self.verify_name('drive0', 'group2')
355 for i in range(1, self.max_drives):
356 devname = "drive%d" % i
357 groupname = "group%d" % i
358 self.verify_name(devname, groupname)
360 # Clear throttling settings from drive0 check that all other
361 # devices remain unchanged
363 self.set_io_throttle('drive0', params)
364 self.verify_name('drive0', None)
365 for i in range(1, self.max_drives):
366 devname = "drive%d" % i
367 groupname = "group%d" % i
368 self.verify_name(devname, groupname)
370 class ThrottleTestRemovableMedia(iotests.QMPTestCase):
372 self.vm = iotests.VM()
373 self.vm.add_device("{},id=virtio-scsi".format(
374 iotests.get_virtio_scsi_device()))
380 def test_removable_media(self):
381 # Add a couple of dummy nodes named cd0 and cd1
382 result = self.vm.qmp("blockdev-add", driver="null-co",
383 read_zeroes=True, node_name="cd0")
384 self.assert_qmp(result, 'return', {})
385 result = self.vm.qmp("blockdev-add", driver="null-co",
386 read_zeroes=True, node_name="cd1")
387 self.assert_qmp(result, 'return', {})
389 # Attach a CD drive with cd0 inserted
390 result = self.vm.qmp("device_add", driver="scsi-cd",
391 id="dev0", drive="cd0")
392 self.assert_qmp(result, 'return', {})
395 args = { "id": "dev0", "iops": 100, "iops_rd": 0, "iops_wr": 0,
396 "bps": 50, "bps_rd": 0, "bps_wr": 0 }
397 result = self.vm.qmp("block_set_io_throttle", conv_keys=False, **args)
398 self.assert_qmp(result, 'return', {})
400 # Check that the I/O limits have been set
401 result = self.vm.qmp("query-block")
402 self.assert_qmp(result, 'return[0]/inserted/iops', 100)
403 self.assert_qmp(result, 'return[0]/inserted/bps', 50)
405 # Now eject cd0 and insert cd1
406 result = self.vm.qmp("blockdev-open-tray", id='dev0')
407 self.assert_qmp(result, 'return', {})
408 result = self.vm.qmp("blockdev-remove-medium", id='dev0')
409 self.assert_qmp(result, 'return', {})
410 result = self.vm.qmp("blockdev-insert-medium", id='dev0', node_name='cd1')
411 self.assert_qmp(result, 'return', {})
413 # Check that the I/O limits are still the same
414 result = self.vm.qmp("query-block")
415 self.assert_qmp(result, 'return[0]/inserted/iops', 100)
416 self.assert_qmp(result, 'return[0]/inserted/bps', 50)
419 result = self.vm.qmp("blockdev-remove-medium", id='dev0')
420 self.assert_qmp(result, 'return', {})
422 # Check that we can't set limits if the device has no medium
423 result = self.vm.qmp("block_set_io_throttle", conv_keys=False, **args)
424 self.assert_qmp(result, 'error/class', 'GenericError')
426 # Remove the CD drive
427 result = self.vm.qmp("device_del", id='dev0')
428 self.assert_qmp(result, 'return', {})
431 if __name__ == '__main__':
432 if 'null-co' not in iotests.supported_formats():
433 iotests.notrun('null-co driver support missing')
434 iotests.main(supported_fmts=["raw"])