input-linux: initialize key state
[qemu/kevin.git] / tests / qemu-iotests / 093
blobffcb271b3694305f8292b56a1da2189ef54bbb26
1 #!/usr/bin/env python
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/>.
22 import iotests
24 nsec_per_sec = 1000000000
26 class ThrottleTestCase(iotests.QMPTestCase):
27 test_img = "null-aio://"
28 max_drives = 3
30 def blockstats(self, device):
31 result = self.vm.qmp("query-blockstats")
32 for r in result['return']:
33 if r['device'] == device:
34 stat = r['stats']
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 setUp(self):
39 self.vm = iotests.VM()
40 for i in range(0, self.max_drives):
41 self.vm.add_drive(self.test_img)
42 self.vm.launch()
44 def tearDown(self):
45 self.vm.shutdown()
47 def configure_throttle(self, ndrives, params):
48 params['group'] = 'test'
50 # Set the I/O throttling parameters to all drives
51 for i in range(0, ndrives):
52 params['device'] = 'drive%d' % i
53 result = self.vm.qmp("block_set_io_throttle", conv_keys=False, **params)
54 self.assert_qmp(result, 'return', {})
56 def do_test_throttle(self, ndrives, seconds, params):
57 def check_limit(limit, num):
58 # IO throttling algorithm is discrete, allow 10% error so the test
59 # is more robust
60 return limit == 0 or \
61 (num < seconds * limit * 1.1 / ndrives
62 and num > seconds * limit * 0.9 / ndrives)
64 # Set vm clock to a known value
65 ns = seconds * nsec_per_sec
66 self.vm.qtest("clock_step %d" % ns)
68 # Submit enough requests so the throttling mechanism kicks
69 # in. The throttled requests won't be executed until we
70 # advance the virtual clock.
71 rq_size = 512
72 rd_nr = max(params['bps'] / rq_size / 2,
73 params['bps_rd'] / rq_size,
74 params['iops'] / 2,
75 params['iops_rd'])
76 rd_nr *= seconds * 2
77 rd_nr /= ndrives
78 wr_nr = max(params['bps'] / rq_size / 2,
79 params['bps_wr'] / rq_size,
80 params['iops'] / 2,
81 params['iops_wr'])
82 wr_nr *= seconds * 2
83 wr_nr /= ndrives
85 # Send I/O requests to all drives
86 for i in range(rd_nr):
87 for drive in range(0, ndrives):
88 self.vm.hmp_qemu_io("drive%d" % drive, "aio_read %d %d" %
89 (i * rq_size, rq_size))
91 for i in range(wr_nr):
92 for drive in range(0, ndrives):
93 self.vm.hmp_qemu_io("drive%d" % drive, "aio_write %d %d" %
94 (i * rq_size, rq_size))
96 # We'll store the I/O stats for each drive in these arrays
97 start_rd_bytes = [0] * ndrives
98 start_rd_iops = [0] * ndrives
99 start_wr_bytes = [0] * ndrives
100 start_wr_iops = [0] * ndrives
101 end_rd_bytes = [0] * ndrives
102 end_rd_iops = [0] * ndrives
103 end_wr_bytes = [0] * ndrives
104 end_wr_iops = [0] * ndrives
106 # Read the stats before advancing the clock
107 for i in range(0, ndrives):
108 start_rd_bytes[i], start_rd_iops[i], start_wr_bytes[i], \
109 start_wr_iops[i] = self.blockstats('drive%d' % i)
111 self.vm.qtest("clock_step %d" % ns)
113 # Read the stats after advancing the clock
114 for i in range(0, ndrives):
115 end_rd_bytes[i], end_rd_iops[i], end_wr_bytes[i], \
116 end_wr_iops[i] = self.blockstats('drive%d' % i)
118 # Check that the I/O is within the limits and evenly distributed
119 for i in range(0, ndrives):
120 rd_bytes = end_rd_bytes[i] - start_rd_bytes[i]
121 rd_iops = end_rd_iops[i] - start_rd_iops[i]
122 wr_bytes = end_wr_bytes[i] - start_wr_bytes[i]
123 wr_iops = end_wr_iops[i] - start_wr_iops[i]
125 self.assertTrue(check_limit(params['bps'], rd_bytes + wr_bytes))
126 self.assertTrue(check_limit(params['bps_rd'], rd_bytes))
127 self.assertTrue(check_limit(params['bps_wr'], wr_bytes))
128 self.assertTrue(check_limit(params['iops'], rd_iops + wr_iops))
129 self.assertTrue(check_limit(params['iops_rd'], rd_iops))
130 self.assertTrue(check_limit(params['iops_wr'], wr_iops))
132 def test_all(self):
133 params = {"bps": 4096,
134 "bps_rd": 4096,
135 "bps_wr": 4096,
136 "iops": 10,
137 "iops_rd": 10,
138 "iops_wr": 10,
140 # Repeat the test with different numbers of drives
141 for ndrives in range(1, self.max_drives + 1):
142 # Pick each out of all possible params and test
143 for tk in params:
144 limits = dict([(k, 0) for k in params])
145 limits[tk] = params[tk] * ndrives
146 self.configure_throttle(ndrives, limits)
147 self.do_test_throttle(ndrives, 5, limits)
149 def test_burst(self):
150 params = {"bps": 4096,
151 "bps_rd": 4096,
152 "bps_wr": 4096,
153 "iops": 10,
154 "iops_rd": 10,
155 "iops_wr": 10,
157 ndrives = 1
158 # Pick each out of all possible params and test
159 for tk in params:
160 rate = params[tk] * ndrives
161 burst_rate = rate * 7
162 burst_length = 4
164 # Configure the throttling settings
165 settings = dict([(k, 0) for k in params])
166 settings[tk] = rate
167 settings['%s_max' % tk] = burst_rate
168 settings['%s_max_length' % tk] = burst_length
169 self.configure_throttle(ndrives, settings)
171 # Wait for the bucket to empty so we can do bursts
172 wait_ns = nsec_per_sec * burst_length * burst_rate / rate
173 self.vm.qtest("clock_step %d" % wait_ns)
175 # Test I/O at the max burst rate
176 limits = dict([(k, 0) for k in params])
177 limits[tk] = burst_rate
178 self.do_test_throttle(ndrives, burst_length, limits)
180 # Now test I/O at the normal rate
181 limits[tk] = rate
182 self.do_test_throttle(ndrives, 5, limits)
184 class ThrottleTestCoroutine(ThrottleTestCase):
185 test_img = "null-co://"
187 class ThrottleTestGroupNames(iotests.QMPTestCase):
188 test_img = "null-aio://"
189 max_drives = 3
191 def setUp(self):
192 self.vm = iotests.VM()
193 for i in range(0, self.max_drives):
194 self.vm.add_drive(self.test_img, "throttling.iops-total=100")
195 self.vm.launch()
197 def tearDown(self):
198 self.vm.shutdown()
200 def set_io_throttle(self, device, params):
201 params["device"] = device
202 result = self.vm.qmp("block_set_io_throttle", conv_keys=False, **params)
203 self.assert_qmp(result, 'return', {})
205 def verify_name(self, device, name):
206 result = self.vm.qmp("query-block")
207 for r in result["return"]:
208 if r["device"] == device:
209 info = r["inserted"]
210 if name:
211 self.assertEqual(info["group"], name)
212 else:
213 self.assertFalse(info.has_key('group'))
214 return
216 raise Exception("No group information found for '%s'" % device)
218 def test_group_naming(self):
219 params = {"bps": 0,
220 "bps_rd": 0,
221 "bps_wr": 0,
222 "iops": 0,
223 "iops_rd": 0,
224 "iops_wr": 0}
226 # Check the drives added using the command line.
227 # The default throttling group name is the device name.
228 for i in range(self.max_drives):
229 devname = "drive%d" % i
230 self.verify_name(devname, devname)
232 # Clear throttling settings => the group name is gone.
233 for i in range(self.max_drives):
234 devname = "drive%d" % i
235 self.set_io_throttle(devname, params)
236 self.verify_name(devname, None)
238 # Set throttling settings using block_set_io_throttle and
239 # check the default group names.
240 params["iops"] = 10
241 for i in range(self.max_drives):
242 devname = "drive%d" % i
243 self.set_io_throttle(devname, params)
244 self.verify_name(devname, devname)
246 # Set a custom group name for each device
247 for i in range(3):
248 devname = "drive%d" % i
249 groupname = "group%d" % i
250 params['group'] = groupname
251 self.set_io_throttle(devname, params)
252 self.verify_name(devname, groupname)
254 # Put drive0 in group1 and check that all other devices remain
255 # unchanged
256 params['group'] = 'group1'
257 self.set_io_throttle('drive0', params)
258 self.verify_name('drive0', 'group1')
259 for i in range(1, self.max_drives):
260 devname = "drive%d" % i
261 groupname = "group%d" % i
262 self.verify_name(devname, groupname)
264 # Put drive0 in group2 and check that all other devices remain
265 # unchanged
266 params['group'] = 'group2'
267 self.set_io_throttle('drive0', params)
268 self.verify_name('drive0', 'group2')
269 for i in range(1, self.max_drives):
270 devname = "drive%d" % i
271 groupname = "group%d" % i
272 self.verify_name(devname, groupname)
274 # Clear throttling settings from drive0 check that all other
275 # devices remain unchanged
276 params["iops"] = 0
277 self.set_io_throttle('drive0', params)
278 self.verify_name('drive0', None)
279 for i in range(1, self.max_drives):
280 devname = "drive%d" % i
281 groupname = "group%d" % i
282 self.verify_name(devname, groupname)
285 if __name__ == '__main__':
286 iotests.main(supported_fmts=["raw"])