3 # Tests for IO throttling
5 # Copyright (C) 2015 Red Hat, Inc.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 class ThrottleTestCase(iotests
.QMPTestCase
):
24 test_img
= "null-aio://"
26 def blockstats(self
, device
):
27 result
= self
.vm
.qmp("query-blockstats")
28 for r
in result
['return']:
29 if r
['device'] == device
:
31 return stat
['rd_bytes'], stat
['rd_operations'], stat
['wr_bytes'], stat
['wr_operations']
32 raise Exception("Device not found for blockstats: %s" % device
)
35 self
.vm
= iotests
.VM().add_drive(self
.test_img
)
41 def do_test_throttle(self
, seconds
, params
):
42 def check_limit(limit
, num
):
43 # IO throttling algorithm is discrete, allow 10% error so the test
45 return limit
== 0 or \
46 (num
< seconds
* limit
* 1.1
47 and num
> seconds
* limit
* 0.9)
49 nsec_per_sec
= 1000000000
51 params
['device'] = 'drive0'
53 result
= self
.vm
.qmp("block_set_io_throttle", conv_keys
=False, **params
)
54 self
.assert_qmp(result
, 'return', {})
56 # Set vm clock to a known value
57 ns
= seconds
* nsec_per_sec
58 self
.vm
.qtest("clock_step %d" % ns
)
60 # Submit enough requests. They will drain bps_max and iops_max, but the
61 # rest requests won't get executed until we advance the virtual clock
62 # with qtest interface
64 rd_nr
= max(params
['bps'] / rq_size
/ 2,
65 params
['bps_rd'] / rq_size
,
69 wr_nr
= max(params
['bps'] / rq_size
/ 2,
70 params
['bps_wr'] / rq_size
,
74 for i
in range(rd_nr
):
75 self
.vm
.hmp_qemu_io("drive0", "aio_read %d %d" % (i
* rq_size
, rq_size
))
76 for i
in range(wr_nr
):
77 self
.vm
.hmp_qemu_io("drive0", "aio_write %d %d" % (i
* rq_size
, rq_size
))
79 start_rd_bytes
, start_rd_iops
, start_wr_bytes
, start_wr_iops
= self
.blockstats('drive0')
81 self
.vm
.qtest("clock_step %d" % ns
)
82 end_rd_bytes
, end_rd_iops
, end_wr_bytes
, end_wr_iops
= self
.blockstats('drive0')
84 rd_bytes
= end_rd_bytes
- start_rd_bytes
85 rd_iops
= end_rd_iops
- start_rd_iops
86 wr_bytes
= end_wr_bytes
- start_wr_bytes
87 wr_iops
= end_wr_iops
- start_wr_iops
89 self
.assertTrue(check_limit(params
['bps'], rd_bytes
+ wr_bytes
))
90 self
.assertTrue(check_limit(params
['bps_rd'], rd_bytes
))
91 self
.assertTrue(check_limit(params
['bps_wr'], wr_bytes
))
92 self
.assertTrue(check_limit(params
['iops'], rd_iops
+ wr_iops
))
93 self
.assertTrue(check_limit(params
['iops_rd'], rd_iops
))
94 self
.assertTrue(check_limit(params
['iops_wr'], wr_iops
))
97 params
= {"bps": 4096,
104 # Pick each out of all possible params and test
106 limits
= dict([(k
, 0) for k
in params
])
107 limits
[tk
] = params
[tk
]
108 self
.do_test_throttle(5, limits
)
110 class ThrottleTestCoroutine(ThrottleTestCase
):
111 test_img
= "null-co://"
113 if __name__
== '__main__':
114 iotests
.main(supported_fmts
=["raw"])