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_img
= "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
)
39 self
.vm
= iotests
.VM()
40 for i
in range(0, self
.max_drives
):
41 self
.vm
.add_drive(self
.test_img
)
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
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.
72 rd_nr
= max(params
['bps'] / rq_size
/ 2,
73 params
['bps_rd'] / rq_size
,
78 wr_nr
= max(params
['bps'] / rq_size
/ 2,
79 params
['bps_wr'] / rq_size
,
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
))
133 params
= {"bps": 4096,
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
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,
158 # Pick each out of all possible params and test
160 rate
= params
[tk
] * ndrives
161 burst_rate
= rate
* 7
164 # Configure the throttling settings
165 settings
= dict([(k
, 0) for k
in params
])
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
182 self
.do_test_throttle(ndrives
, 5, limits
)
184 class ThrottleTestCoroutine(ThrottleTestCase
):
185 test_img
= "null-co://"
187 if __name__
== '__main__':
188 iotests
.main(supported_fmts
=["raw"])