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