xref: /qemu/tests/qemu-iotests/093 (revision 226419d6)
1#!/usr/bin/env python
2#
3# Tests for IO throttling
4#
5# Copyright (C) 2015 Red Hat, Inc.
6# Copyright (C) 2015-2016 Igalia, S.L.
7#
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.
12#
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.
17#
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/>.
20#
21
22import iotests
23
24nsec_per_sec = 1000000000
25
26class ThrottleTestCase(iotests.QMPTestCase):
27    test_img = "null-aio://"
28    max_drives = 3
29
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)
37
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()
43
44    def tearDown(self):
45        self.vm.shutdown()
46
47    def configure_throttle(self, ndrives, params):
48        params['group'] = 'test'
49
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', {})
55
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)
63
64        # Set vm clock to a known value
65        ns = seconds * nsec_per_sec
66        self.vm.qtest("clock_step %d" % ns)
67
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
84
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))
90
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))
95
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
105
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)
110
111        self.vm.qtest("clock_step %d" % ns)
112
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)
117
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]
124
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))
131
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,
139                 }
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)
148
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,
156                 }
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
163
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)
170
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)
174
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)
179
180            # Now test I/O at the normal rate
181            limits[tk] = rate
182            self.do_test_throttle(ndrives, 5, limits)
183
184class ThrottleTestCoroutine(ThrottleTestCase):
185    test_img = "null-co://"
186
187if __name__ == '__main__':
188    iotests.main(supported_fmts=["raw"])
189