aboutsummaryrefslogtreecommitdiff
path: root/tests/qemu-iotests/136
blob: d59400c9fcb1bee83005e8b6b1d504913b721250 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python3
#
# Tests for block device statistics
#
# Copyright (C) 2015 Igalia, S.L.
# Author: Alberto Garcia <berto@igalia.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import iotests
import os

interval_length = 10
nsec_per_sec = 1000000000
op_latency = nsec_per_sec // 1000 # See qtest_latency_ns in accounting.c
bad_sector = 8192
bad_offset = bad_sector * 512
blkdebug_file = os.path.join(iotests.test_dir, 'blkdebug.conf')

class BlockDeviceStatsTestCase(iotests.QMPTestCase):
    test_driver = "null-aio"
    total_rd_bytes = 0
    total_rd_ops = 0
    total_wr_bytes = 0
    total_wr_ops = 0
    total_wr_merged = 0
    total_flush_ops = 0
    failed_rd_ops = 0
    failed_wr_ops = 0
    invalid_rd_ops = 0
    invalid_wr_ops = 0
    wr_highest_offset = 0
    account_invalid = False
    account_failed = False

    def blockstats(self, device):
        result = self.vm.qmp("query-blockstats")
        for r in result['return']:
            if r['device'] == device:
                return r['stats']
        raise Exception("Device not found for blockstats: %s" % device)

    def create_blkdebug_file(self):
        file = open(blkdebug_file, 'w')
        file.write('''
[inject-error]
event = "read_aio"
errno = "5"
sector = "%d"

[inject-error]
event = "write_aio"
errno = "5"
sector = "%d"
''' % (bad_sector, bad_sector))
        file.close()

    def required_drivers(self):
        return [self.test_driver]

    @iotests.skip_if_unsupported(required_drivers)
    def setUp(self):
        drive_args = []
        drive_args.append("stats-intervals.0=%d" % interval_length)
        drive_args.append("stats-account-invalid=%s" %
                          (self.account_invalid and "on" or "off"))
        drive_args.append("stats-account-failed=%s" %
                          (self.account_failed and "on" or "off"))
        drive_args.append("file.image.read-zeroes=on")
        self.create_blkdebug_file()
        self.vm = iotests.VM().add_drive('blkdebug:%s:%s://' %
                                         (blkdebug_file, self.test_driver),
                                         ','.join(drive_args))
        self.vm.launch()
        # Set an initial value for the clock
        self.vm.qtest("clock_step %d" % nsec_per_sec)

    def tearDown(self):
        self.vm.shutdown()
        os.remove(blkdebug_file)

    def accounted_ops(self, read = False, write = False, flush = False):
        ops = 0
        if write:
            ops += self.total_wr_ops
            if self.account_failed:
                ops += self.failed_wr_ops
            if self.account_invalid:
                ops += self.invalid_wr_ops
        if read:
            ops += self.total_rd_ops
            if self.account_failed:
                ops += self.failed_rd_ops
            if self.account_invalid:
                ops += self.invalid_rd_ops
        if flush:
            ops += self.total_flush_ops
        return ops

    def accounted_latency(self, read = False, write = False, flush = False):
        latency = 0
        if write:
            latency += self.total_wr_ops * op_latency
            if self.account_failed:
                latency += self.failed_wr_ops * op_latency
        if read:
            latency += self.total_rd_ops * op_latency
            if self.account_failed:
                latency += self.failed_rd_ops * op_latency
        if flush:
            latency += self.total_flush_ops * op_latency
        return latency

    def check_values(self):
        stats = self.blockstats('drive0')

        # Check that the totals match with what we have calculated
        self.assertEqual(self.total_rd_bytes, stats['rd_bytes'])
        self.assertEqual(self.total_wr_bytes, stats['wr_bytes'])
        self.assertEqual(self.total_rd_ops, stats['rd_operations'])
        self.assertEqual(self.total_wr_ops, stats['wr_operations'])
        self.assertEqual(self.total_flush_ops, stats['flush_operations'])
        self.assertEqual(self.wr_highest_offset, stats['wr_highest_offset'])
        self.assertEqual(self.failed_rd_ops, stats['failed_rd_operations'])
        self.assertEqual(self.failed_wr_ops, stats['failed_wr_operations'])
        self.assertEqual(self.invalid_rd_ops, stats['invalid_rd_operations'])
        self.assertEqual(self.invalid_wr_ops, stats['invalid_wr_operations'])
        self.assertEqual(self.account_invalid, stats['account_invalid'])
        self.assertEqual(self.account_failed, stats['account_failed'])
        self.assertEqual(self.total_wr_merged, stats['wr_merged'])

        # Check that there's exactly one interval with the length we defined
        self.assertEqual(1, len(stats['timed_stats']))
        timed_stats = stats['timed_stats'][0]
        self.assertEqual(interval_length, timed_stats['interval_length'])

        total_rd_latency = self.accounted_latency(read = True)
        if (total_rd_latency != 0):
            self.assertEqual(total_rd_latency, stats['rd_total_time_ns'])
            self.assertEqual(op_latency, timed_stats['min_rd_latency_ns'])
            self.assertEqual(op_latency, timed_stats['max_rd_latency_ns'])
            self.assertEqual(op_latency, timed_stats['avg_rd_latency_ns'])
            self.assertLess(0, timed_stats['avg_rd_queue_depth'])
        else:
            self.assertEqual(0, stats['rd_total_time_ns'])
            self.assertEqual(0, timed_stats['min_rd_latency_ns'])
            self.assertEqual(0, timed_stats['max_rd_latency_ns'])
            self.assertEqual(0, timed_stats['avg_rd_latency_ns'])
            self.assertEqual(0, timed_stats['avg_rd_queue_depth'])

        # min read latency <= avg read latency <= max read latency
        self.assertLessEqual(timed_stats['min_rd_latency_ns'],
                             timed_stats['avg_rd_latency_ns'])
        self.assertLessEqual(timed_stats['avg_rd_latency_ns'],
                             timed_stats['max_rd_latency_ns'])

        total_wr_latency = self.accounted_latency(write = True)
        if (total_wr_latency != 0):
            self.assertEqual(total_wr_latency, stats['wr_total_time_ns'])
            self.assertEqual(op_latency, timed_stats['min_wr_latency_ns'])
            self.assertEqual(op_latency, timed_stats['max_wr_latency_ns'])
            self.assertEqual(op_latency, timed_stats['avg_wr_latency_ns'])
            self.assertLess(0, timed_stats['avg_wr_queue_depth'])
        else:
            self.assertEqual(0, stats['wr_total_time_ns'])
            self.assertEqual(0, timed_stats['min_wr_latency_ns'])
            self.assertEqual(0, timed_stats['max_wr_latency_ns'])
            self.assertEqual(0, timed_stats['avg_wr_latency_ns'])
            self.assertEqual(0, timed_stats['avg_wr_queue_depth'])

        # min write latency <= avg write latency <= max write latency
        self.assertLessEqual(timed_stats['min_wr_latency_ns'],
                             timed_stats['avg_wr_latency_ns'])
        self.assertLessEqual(timed_stats['avg_wr_latency_ns'],
                             timed_stats['max_wr_latency_ns'])

        total_flush_latency = self.accounted_latency(flush = True)
        if (total_flush_latency != 0):
            self.assertEqual(total_flush_latency, stats['flush_total_time_ns'])
            self.assertEqual(op_latency, timed_stats['min_flush_latency_ns'])
            self.assertEqual(op_latency, timed_stats['max_flush_latency_ns'])
            self.assertEqual(op_latency, timed_stats['avg_flush_latency_ns'])
        else:
            self.assertEqual(0, stats['flush_total_time_ns'])
            self.assertEqual(0, timed_stats['min_flush_latency_ns'])
            self.assertEqual(0, timed_stats['max_flush_latency_ns'])
            self.assertEqual(0, timed_stats['avg_flush_latency_ns'])

        # min flush latency <= avg flush latency <= max flush latency
        self.assertLessEqual(timed_stats['min_flush_latency_ns'],
                             timed_stats['avg_flush_latency_ns'])
        self.assertLessEqual(timed_stats['avg_flush_latency_ns'],
                             timed_stats['max_flush_latency_ns'])

        # idle_time_ns must be > 0 if we have performed any operation
        if (self.accounted_ops(read = True, write = True, flush = True) != 0):
            self.assertLess(0, stats['idle_time_ns'])
        else:
            self.assertFalse('idle_time_ns' in stats)

        # This test does not alter these, so they must be all 0
        self.assertEqual(0, stats['rd_merged'])
        self.assertEqual(0, stats['failed_flush_operations'])
        self.assertEqual(0, stats['invalid_flush_operations'])

    def do_test_stats(self, rd_size = 0, rd_ops = 0, wr_size = 0, wr_ops = 0,
                      flush_ops = 0, invalid_rd_ops = 0, invalid_wr_ops = 0,
                      failed_rd_ops = 0, failed_wr_ops = 0, wr_merged = 0):
        # The 'ops' list will contain all the requested I/O operations
        ops = []
        for i in range(rd_ops):
            ops.append("aio_read %d %d" % (i * rd_size, rd_size))

        for i in range(wr_ops):
            ops.append("aio_write %d %d" % (i * wr_size, wr_size))

        for i in range(flush_ops):
            ops.append("aio_flush")

        highest_offset = wr_ops * wr_size

        for i in range(invalid_rd_ops):
            ops.append("aio_read -i 0 512")

        for i in range(invalid_wr_ops):
            ops.append("aio_write -i 0 512")

        for i in range(failed_rd_ops):
            ops.append("aio_read %d 512" % bad_offset)

        for i in range(failed_wr_ops):
            ops.append("aio_write %d 512" % bad_offset)

        # We need an extra aio_flush to settle all outstanding AIO
        # operations before we can advance the virtual clock, so that
        # the last access happens before clock_step and idle_time_ns
        # will be greater than 0
        extra_flush = 0
        if rd_ops + wr_ops + invalid_rd_ops + invalid_wr_ops + \
                failed_rd_ops + failed_wr_ops > 0:
            extra_flush = 1

        if extra_flush > 0:
            ops.append("aio_flush")

        if failed_wr_ops > 0:
            highest_offset = max(highest_offset, bad_offset + 512)

        # Now perform all operations
        for op in ops:
            self.vm.hmp_qemu_io("drive0", op)

        # Update the expected totals
        self.total_rd_bytes += rd_ops * rd_size
        self.total_rd_ops += rd_ops
        self.total_wr_bytes += wr_ops * wr_size
        self.total_wr_ops += wr_ops
        self.total_wr_merged += wr_merged
        self.total_flush_ops += flush_ops + extra_flush
        self.invalid_rd_ops += invalid_rd_ops
        self.invalid_wr_ops += invalid_wr_ops
        self.failed_rd_ops += failed_rd_ops
        self.failed_wr_ops += failed_wr_ops

        self.wr_highest_offset = max(self.wr_highest_offset, highest_offset)

        # Advance the clock so idle_time_ns has a meaningful value
        self.vm.qtest("clock_step %d" % nsec_per_sec)

        # And check that the actual statistics match the expected ones
        self.check_values()

    def test_read_only(self):
        test_values = [[512,    1],
                       [65536,  1],
                       [512,   12],
                       [65536, 12]]
        for i in test_values:
            self.do_test_stats(rd_size = i[0], rd_ops = i[1])

    def test_write_only(self):
        test_values = [[512,    1],
                       [65536,  1],
                       [512,   12],
                       [65536, 12]]
        for i in test_values:
            self.do_test_stats(wr_size = i[0], wr_ops = i[1])

    def test_invalid(self):
        self.do_test_stats(invalid_rd_ops = 7)
        self.do_test_stats(invalid_wr_ops = 3)
        self.do_test_stats(invalid_rd_ops = 4, invalid_wr_ops = 5)

    def test_failed(self):
        self.do_test_stats(failed_rd_ops = 8)
        self.do_test_stats(failed_wr_ops = 6)
        self.do_test_stats(failed_rd_ops = 5, failed_wr_ops = 12)

    def test_flush(self):
        self.do_test_stats(flush_ops = 8)

    def test_all(self):
        # rd_size, rd_ops, wr_size, wr_ops, flush_ops
        # invalid_rd_ops,  invalid_wr_ops,
        # failed_rd_ops,   failed_wr_ops
        # wr_merged
        test_values = [[512,    1, 512,   1, 1, 4, 7, 5, 2, 0],
                       [65536,  1, 2048, 12, 7, 7, 5, 2, 5, 0],
                       [32768,  9, 8192,  1, 4, 3, 2, 4, 6, 0],
                       [16384, 11, 3584, 16, 9, 8, 6, 7, 3, 0]]
        for i in test_values:
            self.do_test_stats(*i)

    def test_no_op(self):
        # All values must be sane before doing any I/O
        self.check_values()


class BlockDeviceStatsTestAccountInvalid(BlockDeviceStatsTestCase):
    account_invalid = True
    account_failed = False

class BlockDeviceStatsTestAccountFailed(BlockDeviceStatsTestCase):
    account_invalid = False
    account_failed = True

class BlockDeviceStatsTestAccountBoth(BlockDeviceStatsTestCase):
    account_invalid = True
    account_failed = True

class BlockDeviceStatsTestCoroutine(BlockDeviceStatsTestCase):
    test_driver = "null-co"

if __name__ == '__main__':
    if 'null-co' not in iotests.supported_formats():
        iotests.notrun('null-co driver support missing')
    iotests.main(supported_fmts=["raw"])