xref: /openbmc/qemu/tests/qemu-iotests/093 (revision 0b7e89b1)
1#!/usr/bin/env python
2#
3# Tests for IO throttling
4#
5# Copyright (C) 2015 Red Hat, Inc.
6#
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.
11#
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.
16#
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/>.
19#
20
21import iotests
22
23class ThrottleTestCase(iotests.QMPTestCase):
24    test_img = "null-aio://"
25
26    def blockstats(self, device):
27        result = self.vm.qmp("query-blockstats")
28        for r in result['return']:
29            if r['device'] == device:
30                stat = r['stats']
31                return stat['rd_bytes'], stat['rd_operations'], stat['wr_bytes'], stat['wr_operations']
32        raise Exception("Device not found for blockstats: %s" % device)
33
34    def setUp(self):
35        self.vm = iotests.VM().add_drive(self.test_img)
36        self.vm.launch()
37
38    def tearDown(self):
39        self.vm.shutdown()
40
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
44            # is more robust
45            return limit == 0 or \
46                   (num < seconds * limit * 1.1
47                   and num > seconds * limit * 0.9)
48
49        nsec_per_sec = 1000000000
50
51        params['device'] = 'drive0'
52
53        result = self.vm.qmp("block_set_io_throttle", conv_keys=False, **params)
54        self.assert_qmp(result, 'return', {})
55
56        # Set vm clock to a known value
57        ns = seconds * nsec_per_sec
58        self.vm.qtest("clock_step %d" % ns)
59
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
63        rq_size = 512
64        rd_nr = max(params['bps'] / rq_size / 2,
65                    params['bps_rd'] / rq_size,
66                    params['iops'] / 2,
67                    params['iops_rd'])
68        rd_nr *= seconds * 2
69        wr_nr = max(params['bps'] / rq_size / 2,
70                    params['bps_wr'] / rq_size,
71                    params['iops'] / 2,
72                    params['iops_wr'])
73        wr_nr *= seconds * 2
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))
78
79        start_rd_bytes, start_rd_iops, start_wr_bytes, start_wr_iops = self.blockstats('drive0')
80
81        self.vm.qtest("clock_step %d" % ns)
82        end_rd_bytes, end_rd_iops, end_wr_bytes, end_wr_iops = self.blockstats('drive0')
83
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
88
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))
95
96    def test_all(self):
97        params = {"bps": 4096,
98                  "bps_rd": 4096,
99                  "bps_wr": 4096,
100                  "iops": 10,
101                  "iops_rd": 10,
102                  "iops_wr": 10,
103                 }
104        # Pick each out of all possible params and test
105        for tk in params:
106            limits = dict([(k, 0) for k in params])
107            limits[tk] = params[tk]
108            self.do_test_throttle(5, limits)
109
110class ThrottleTestCoroutine(ThrottleTestCase):
111    test_img = "null-co://"
112
113if __name__ == '__main__':
114    iotests.main(supported_fmts=["raw"])
115