xref: /openbmc/qemu/tests/qemu-iotests/093 (revision a26ddb43963e77aeebc2a4f011d27b2d9c017f21)
1fb13bbf2SFam Zheng#!/usr/bin/env python
2fb13bbf2SFam Zheng#
3fb13bbf2SFam Zheng# Tests for IO throttling
4fb13bbf2SFam Zheng#
5fb13bbf2SFam Zheng# Copyright (C) 2015 Red Hat, Inc.
6a90cade0SAlberto Garcia# Copyright (C) 2015-2016 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
24a90cade0SAlberto Garciansec_per_sec = 1000000000
25a90cade0SAlberto Garcia
26fb13bbf2SFam Zhengclass ThrottleTestCase(iotests.QMPTestCase):
27fb13bbf2SFam Zheng    test_img = "null-aio://"
282db33f88SAlberto Garcia    max_drives = 3
29fb13bbf2SFam Zheng
30fb13bbf2SFam Zheng    def blockstats(self, device):
31fb13bbf2SFam Zheng        result = self.vm.qmp("query-blockstats")
32fb13bbf2SFam Zheng        for r in result['return']:
33fb13bbf2SFam Zheng            if r['device'] == device:
34fb13bbf2SFam Zheng                stat = r['stats']
35fb13bbf2SFam Zheng                return stat['rd_bytes'], stat['rd_operations'], stat['wr_bytes'], stat['wr_operations']
36fb13bbf2SFam Zheng        raise Exception("Device not found for blockstats: %s" % device)
37fb13bbf2SFam Zheng
38fb13bbf2SFam Zheng    def setUp(self):
392db33f88SAlberto Garcia        self.vm = iotests.VM()
402db33f88SAlberto Garcia        for i in range(0, self.max_drives):
412db33f88SAlberto Garcia            self.vm.add_drive(self.test_img)
42fb13bbf2SFam Zheng        self.vm.launch()
43fb13bbf2SFam Zheng
44fb13bbf2SFam Zheng    def tearDown(self):
45fb13bbf2SFam Zheng        self.vm.shutdown()
46fb13bbf2SFam Zheng
47a90cade0SAlberto Garcia    def configure_throttle(self, ndrives, params):
482db33f88SAlberto Garcia        params['group'] = 'test'
49fb13bbf2SFam Zheng
502db33f88SAlberto Garcia        # Set the I/O throttling parameters to all drives
512db33f88SAlberto Garcia        for i in range(0, ndrives):
522db33f88SAlberto Garcia            params['device'] = 'drive%d' % i
53fb13bbf2SFam Zheng            result = self.vm.qmp("block_set_io_throttle", conv_keys=False, **params)
54fb13bbf2SFam Zheng            self.assert_qmp(result, 'return', {})
55fb13bbf2SFam Zheng
56*a26ddb43SAlberto Garcia    def do_test_throttle(self, ndrives, seconds, params, first_drive = 0):
57a90cade0SAlberto Garcia        def check_limit(limit, num):
58a90cade0SAlberto Garcia            # IO throttling algorithm is discrete, allow 10% error so the test
59a90cade0SAlberto Garcia            # is more robust
60a90cade0SAlberto Garcia            return limit == 0 or \
61a90cade0SAlberto Garcia                   (num < seconds * limit * 1.1 / ndrives
62a90cade0SAlberto Garcia                   and num > seconds * limit * 0.9 / ndrives)
63a90cade0SAlberto Garcia
64fb13bbf2SFam Zheng        # Set vm clock to a known value
65fb13bbf2SFam Zheng        ns = seconds * nsec_per_sec
66fb13bbf2SFam Zheng        self.vm.qtest("clock_step %d" % ns)
67fb13bbf2SFam Zheng
68a90cade0SAlberto Garcia        # Submit enough requests so the throttling mechanism kicks
69a90cade0SAlberto Garcia        # in. The throttled requests won't be executed until we
70a90cade0SAlberto Garcia        # advance the virtual clock.
71fb13bbf2SFam Zheng        rq_size = 512
72fb13bbf2SFam Zheng        rd_nr = max(params['bps'] / rq_size / 2,
73fb13bbf2SFam Zheng                    params['bps_rd'] / rq_size,
74fb13bbf2SFam Zheng                    params['iops'] / 2,
75fb13bbf2SFam Zheng                    params['iops_rd'])
76fb13bbf2SFam Zheng        rd_nr *= seconds * 2
772db33f88SAlberto Garcia        rd_nr /= ndrives
78fb13bbf2SFam Zheng        wr_nr = max(params['bps'] / rq_size / 2,
79fb13bbf2SFam Zheng                    params['bps_wr'] / rq_size,
80fb13bbf2SFam Zheng                    params['iops'] / 2,
81fb13bbf2SFam Zheng                    params['iops_wr'])
82fb13bbf2SFam Zheng        wr_nr *= seconds * 2
832db33f88SAlberto Garcia        wr_nr /= ndrives
84fb13bbf2SFam Zheng
852db33f88SAlberto Garcia        # Send I/O requests to all drives
862db33f88SAlberto Garcia        for i in range(rd_nr):
872db33f88SAlberto Garcia            for drive in range(0, ndrives):
88*a26ddb43SAlberto Garcia                idx = first_drive + drive
89*a26ddb43SAlberto Garcia                self.vm.hmp_qemu_io("drive%d" % idx, "aio_read %d %d" %
902db33f88SAlberto Garcia                                    (i * rq_size, rq_size))
912db33f88SAlberto Garcia
922db33f88SAlberto Garcia        for i in range(wr_nr):
932db33f88SAlberto Garcia            for drive in range(0, ndrives):
94*a26ddb43SAlberto Garcia                idx = first_drive + drive
95*a26ddb43SAlberto Garcia                self.vm.hmp_qemu_io("drive%d" % idx, "aio_write %d %d" %
962db33f88SAlberto Garcia                                    (i * rq_size, rq_size))
972db33f88SAlberto Garcia
982db33f88SAlberto Garcia        # We'll store the I/O stats for each drive in these arrays
992db33f88SAlberto Garcia        start_rd_bytes = [0] * ndrives
1002db33f88SAlberto Garcia        start_rd_iops  = [0] * ndrives
1012db33f88SAlberto Garcia        start_wr_bytes = [0] * ndrives
1022db33f88SAlberto Garcia        start_wr_iops  = [0] * ndrives
1032db33f88SAlberto Garcia        end_rd_bytes   = [0] * ndrives
1042db33f88SAlberto Garcia        end_rd_iops    = [0] * ndrives
1052db33f88SAlberto Garcia        end_wr_bytes   = [0] * ndrives
1062db33f88SAlberto Garcia        end_wr_iops    = [0] * ndrives
1072db33f88SAlberto Garcia
1082db33f88SAlberto Garcia        # Read the stats before advancing the clock
1092db33f88SAlberto Garcia        for i in range(0, ndrives):
110*a26ddb43SAlberto Garcia            idx = first_drive + i
1112db33f88SAlberto Garcia            start_rd_bytes[i], start_rd_iops[i], start_wr_bytes[i], \
112*a26ddb43SAlberto Garcia                start_wr_iops[i] = self.blockstats('drive%d' % idx)
113fb13bbf2SFam Zheng
114fb13bbf2SFam Zheng        self.vm.qtest("clock_step %d" % ns)
115fb13bbf2SFam Zheng
1162db33f88SAlberto Garcia        # Read the stats after advancing the clock
1172db33f88SAlberto Garcia        for i in range(0, ndrives):
118*a26ddb43SAlberto Garcia            idx = first_drive + i
1192db33f88SAlberto Garcia            end_rd_bytes[i], end_rd_iops[i], end_wr_bytes[i], \
120*a26ddb43SAlberto Garcia                end_wr_iops[i] = self.blockstats('drive%d' % idx)
1212db33f88SAlberto Garcia
1222db33f88SAlberto Garcia        # Check that the I/O is within the limits and evenly distributed
1232db33f88SAlberto Garcia        for i in range(0, ndrives):
1242db33f88SAlberto Garcia            rd_bytes = end_rd_bytes[i] - start_rd_bytes[i]
1252db33f88SAlberto Garcia            rd_iops = end_rd_iops[i] - start_rd_iops[i]
1262db33f88SAlberto Garcia            wr_bytes = end_wr_bytes[i] - start_wr_bytes[i]
1272db33f88SAlberto Garcia            wr_iops = end_wr_iops[i] - start_wr_iops[i]
128fb13bbf2SFam Zheng
129fb13bbf2SFam Zheng            self.assertTrue(check_limit(params['bps'], rd_bytes + wr_bytes))
130fb13bbf2SFam Zheng            self.assertTrue(check_limit(params['bps_rd'], rd_bytes))
131fb13bbf2SFam Zheng            self.assertTrue(check_limit(params['bps_wr'], wr_bytes))
132fb13bbf2SFam Zheng            self.assertTrue(check_limit(params['iops'], rd_iops + wr_iops))
133fb13bbf2SFam Zheng            self.assertTrue(check_limit(params['iops_rd'], rd_iops))
134fb13bbf2SFam Zheng            self.assertTrue(check_limit(params['iops_wr'], wr_iops))
135fb13bbf2SFam Zheng
136*a26ddb43SAlberto Garcia    # Connect N drives to a VM and test I/O in all of them
137fb13bbf2SFam Zheng    def test_all(self):
138fb13bbf2SFam Zheng        params = {"bps": 4096,
139fb13bbf2SFam Zheng                  "bps_rd": 4096,
140fb13bbf2SFam Zheng                  "bps_wr": 4096,
141fb13bbf2SFam Zheng                  "iops": 10,
142fb13bbf2SFam Zheng                  "iops_rd": 10,
143fb13bbf2SFam Zheng                  "iops_wr": 10,
144fb13bbf2SFam Zheng                 }
1452db33f88SAlberto Garcia        # Repeat the test with different numbers of drives
1462db33f88SAlberto Garcia        for ndrives in range(1, self.max_drives + 1):
147fb13bbf2SFam Zheng            # Pick each out of all possible params and test
148fb13bbf2SFam Zheng            for tk in params:
149fb13bbf2SFam Zheng                limits = dict([(k, 0) for k in params])
1502db33f88SAlberto Garcia                limits[tk] = params[tk] * ndrives
151a90cade0SAlberto Garcia                self.configure_throttle(ndrives, limits)
152a90cade0SAlberto Garcia                self.do_test_throttle(ndrives, 5, limits)
153a90cade0SAlberto Garcia
154*a26ddb43SAlberto Garcia    # Connect N drives to a VM and test I/O in just one of them a time
155*a26ddb43SAlberto Garcia    def test_one(self):
156*a26ddb43SAlberto Garcia        params = {"bps": 4096,
157*a26ddb43SAlberto Garcia                  "bps_rd": 4096,
158*a26ddb43SAlberto Garcia                  "bps_wr": 4096,
159*a26ddb43SAlberto Garcia                  "iops": 10,
160*a26ddb43SAlberto Garcia                  "iops_rd": 10,
161*a26ddb43SAlberto Garcia                  "iops_wr": 10,
162*a26ddb43SAlberto Garcia                 }
163*a26ddb43SAlberto Garcia        # Repeat the test for each one of the drives
164*a26ddb43SAlberto Garcia        for drive in range(0, self.max_drives):
165*a26ddb43SAlberto Garcia            # Pick each out of all possible params and test
166*a26ddb43SAlberto Garcia            for tk in params:
167*a26ddb43SAlberto Garcia                limits = dict([(k, 0) for k in params])
168*a26ddb43SAlberto Garcia                limits[tk] = params[tk] * self.max_drives
169*a26ddb43SAlberto Garcia                self.configure_throttle(self.max_drives, limits)
170*a26ddb43SAlberto Garcia                self.do_test_throttle(1, 5, limits, drive)
171*a26ddb43SAlberto Garcia
172a90cade0SAlberto Garcia    def test_burst(self):
173a90cade0SAlberto Garcia        params = {"bps": 4096,
174a90cade0SAlberto Garcia                  "bps_rd": 4096,
175a90cade0SAlberto Garcia                  "bps_wr": 4096,
176a90cade0SAlberto Garcia                  "iops": 10,
177a90cade0SAlberto Garcia                  "iops_rd": 10,
178a90cade0SAlberto Garcia                  "iops_wr": 10,
179a90cade0SAlberto Garcia                 }
180a90cade0SAlberto Garcia        ndrives = 1
181a90cade0SAlberto Garcia        # Pick each out of all possible params and test
182a90cade0SAlberto Garcia        for tk in params:
183a90cade0SAlberto Garcia            rate = params[tk] * ndrives
184a90cade0SAlberto Garcia            burst_rate = rate * 7
185a90cade0SAlberto Garcia            burst_length = 4
186a90cade0SAlberto Garcia
187a90cade0SAlberto Garcia            # Configure the throttling settings
188a90cade0SAlberto Garcia            settings = dict([(k, 0) for k in params])
189a90cade0SAlberto Garcia            settings[tk] = rate
190a90cade0SAlberto Garcia            settings['%s_max' % tk] = burst_rate
191a90cade0SAlberto Garcia            settings['%s_max_length' % tk] = burst_length
192a90cade0SAlberto Garcia            self.configure_throttle(ndrives, settings)
193a90cade0SAlberto Garcia
194a90cade0SAlberto Garcia            # Wait for the bucket to empty so we can do bursts
195a90cade0SAlberto Garcia            wait_ns = nsec_per_sec * burst_length * burst_rate / rate
196a90cade0SAlberto Garcia            self.vm.qtest("clock_step %d" % wait_ns)
197a90cade0SAlberto Garcia
198a90cade0SAlberto Garcia            # Test I/O at the max burst rate
199a90cade0SAlberto Garcia            limits = dict([(k, 0) for k in params])
200a90cade0SAlberto Garcia            limits[tk] = burst_rate
201a90cade0SAlberto Garcia            self.do_test_throttle(ndrives, burst_length, limits)
202a90cade0SAlberto Garcia
203a90cade0SAlberto Garcia            # Now test I/O at the normal rate
204a90cade0SAlberto Garcia            limits[tk] = rate
2052db33f88SAlberto Garcia            self.do_test_throttle(ndrives, 5, limits)
206fb13bbf2SFam Zheng
207fb13bbf2SFam Zhengclass ThrottleTestCoroutine(ThrottleTestCase):
208fb13bbf2SFam Zheng    test_img = "null-co://"
209fb13bbf2SFam Zheng
210435d5ee6SAlberto Garciaclass ThrottleTestGroupNames(iotests.QMPTestCase):
211435d5ee6SAlberto Garcia    test_img = "null-aio://"
212435d5ee6SAlberto Garcia    max_drives = 3
213435d5ee6SAlberto Garcia
214435d5ee6SAlberto Garcia    def setUp(self):
215435d5ee6SAlberto Garcia        self.vm = iotests.VM()
216435d5ee6SAlberto Garcia        for i in range(0, self.max_drives):
217435d5ee6SAlberto Garcia            self.vm.add_drive(self.test_img, "throttling.iops-total=100")
218435d5ee6SAlberto Garcia        self.vm.launch()
219435d5ee6SAlberto Garcia
220435d5ee6SAlberto Garcia    def tearDown(self):
221435d5ee6SAlberto Garcia        self.vm.shutdown()
222435d5ee6SAlberto Garcia
223435d5ee6SAlberto Garcia    def set_io_throttle(self, device, params):
224435d5ee6SAlberto Garcia        params["device"] = device
225435d5ee6SAlberto Garcia        result = self.vm.qmp("block_set_io_throttle", conv_keys=False, **params)
226435d5ee6SAlberto Garcia        self.assert_qmp(result, 'return', {})
227435d5ee6SAlberto Garcia
228435d5ee6SAlberto Garcia    def verify_name(self, device, name):
229435d5ee6SAlberto Garcia        result = self.vm.qmp("query-block")
230435d5ee6SAlberto Garcia        for r in result["return"]:
231435d5ee6SAlberto Garcia            if r["device"] == device:
232435d5ee6SAlberto Garcia                info = r["inserted"]
233435d5ee6SAlberto Garcia                if name:
234435d5ee6SAlberto Garcia                    self.assertEqual(info["group"], name)
235435d5ee6SAlberto Garcia                else:
236435d5ee6SAlberto Garcia                    self.assertFalse(info.has_key('group'))
237435d5ee6SAlberto Garcia                return
238435d5ee6SAlberto Garcia
239435d5ee6SAlberto Garcia        raise Exception("No group information found for '%s'" % device)
240435d5ee6SAlberto Garcia
241435d5ee6SAlberto Garcia    def test_group_naming(self):
242435d5ee6SAlberto Garcia        params = {"bps": 0,
243435d5ee6SAlberto Garcia                  "bps_rd": 0,
244435d5ee6SAlberto Garcia                  "bps_wr": 0,
245435d5ee6SAlberto Garcia                  "iops": 0,
246435d5ee6SAlberto Garcia                  "iops_rd": 0,
247435d5ee6SAlberto Garcia                  "iops_wr": 0}
248435d5ee6SAlberto Garcia
249435d5ee6SAlberto Garcia        # Check the drives added using the command line.
250435d5ee6SAlberto Garcia        # The default throttling group name is the device name.
251435d5ee6SAlberto Garcia        for i in range(self.max_drives):
252435d5ee6SAlberto Garcia            devname = "drive%d" % i
253435d5ee6SAlberto Garcia            self.verify_name(devname, devname)
254435d5ee6SAlberto Garcia
255435d5ee6SAlberto Garcia        # Clear throttling settings => the group name is gone.
256435d5ee6SAlberto Garcia        for i in range(self.max_drives):
257435d5ee6SAlberto Garcia            devname = "drive%d" % i
258435d5ee6SAlberto Garcia            self.set_io_throttle(devname, params)
259435d5ee6SAlberto Garcia            self.verify_name(devname, None)
260435d5ee6SAlberto Garcia
261435d5ee6SAlberto Garcia        # Set throttling settings using block_set_io_throttle and
262435d5ee6SAlberto Garcia        # check the default group names.
263435d5ee6SAlberto Garcia        params["iops"] = 10
264435d5ee6SAlberto Garcia        for i in range(self.max_drives):
265435d5ee6SAlberto Garcia            devname = "drive%d" % i
266435d5ee6SAlberto Garcia            self.set_io_throttle(devname, params)
267435d5ee6SAlberto Garcia            self.verify_name(devname, devname)
268435d5ee6SAlberto Garcia
269435d5ee6SAlberto Garcia        # Set a custom group name for each device
270435d5ee6SAlberto Garcia        for i in range(3):
271435d5ee6SAlberto Garcia            devname = "drive%d" % i
272435d5ee6SAlberto Garcia            groupname = "group%d" % i
273435d5ee6SAlberto Garcia            params['group'] = groupname
274435d5ee6SAlberto Garcia            self.set_io_throttle(devname, params)
275435d5ee6SAlberto Garcia            self.verify_name(devname, groupname)
276435d5ee6SAlberto Garcia
277435d5ee6SAlberto Garcia        # Put drive0 in group1 and check that all other devices remain
278435d5ee6SAlberto Garcia        # unchanged
279435d5ee6SAlberto Garcia        params['group'] = 'group1'
280435d5ee6SAlberto Garcia        self.set_io_throttle('drive0', params)
281435d5ee6SAlberto Garcia        self.verify_name('drive0', 'group1')
282435d5ee6SAlberto Garcia        for i in range(1, self.max_drives):
283435d5ee6SAlberto Garcia            devname = "drive%d" % i
284435d5ee6SAlberto Garcia            groupname = "group%d" % i
285435d5ee6SAlberto Garcia            self.verify_name(devname, groupname)
286435d5ee6SAlberto Garcia
287435d5ee6SAlberto Garcia        # Put drive0 in group2 and check that all other devices remain
288435d5ee6SAlberto Garcia        # unchanged
289435d5ee6SAlberto Garcia        params['group'] = 'group2'
290435d5ee6SAlberto Garcia        self.set_io_throttle('drive0', params)
291435d5ee6SAlberto Garcia        self.verify_name('drive0', 'group2')
292435d5ee6SAlberto Garcia        for i in range(1, self.max_drives):
293435d5ee6SAlberto Garcia            devname = "drive%d" % i
294435d5ee6SAlberto Garcia            groupname = "group%d" % i
295435d5ee6SAlberto Garcia            self.verify_name(devname, groupname)
296435d5ee6SAlberto Garcia
297435d5ee6SAlberto Garcia        # Clear throttling settings from drive0 check that all other
298435d5ee6SAlberto Garcia        # devices remain unchanged
299435d5ee6SAlberto Garcia        params["iops"] = 0
300435d5ee6SAlberto Garcia        self.set_io_throttle('drive0', params)
301435d5ee6SAlberto Garcia        self.verify_name('drive0', None)
302435d5ee6SAlberto Garcia        for i in range(1, self.max_drives):
303435d5ee6SAlberto Garcia            devname = "drive%d" % i
304435d5ee6SAlberto Garcia            groupname = "group%d" % i
305435d5ee6SAlberto Garcia            self.verify_name(devname, groupname)
306435d5ee6SAlberto Garcia
307435d5ee6SAlberto Garcia
308fb13bbf2SFam Zhengif __name__ == '__main__':
309fb13bbf2SFam Zheng    iotests.main(supported_fmts=["raw"])
310