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