test: check all offsets touched for randtrimwrite
[fio.git] / t / run-fio-tests.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0-only
3 #
4 # Copyright (c) 2019 Western Digital Corporation or its affiliates.
5 #
6 """
7 # run-fio-tests.py
8 #
9 # Automate running of fio tests
10 #
11 # USAGE
12 # python3 run-fio-tests.py [-r fio-root] [-f fio-path] [-a artifact-root]
13 #                           [--skip # # #...] [--run-only # # #...]
14 #
15 #
16 # EXAMPLE
17 # # git clone git://git.kernel.dk/fio.git
18 # # cd fio
19 # # make -j
20 # # python3 t/run-fio-tests.py
21 #
22 #
23 # REQUIREMENTS
24 # - Python 3.5 (subprocess.run)
25 # - Linux (libaio ioengine, zbd tests, etc)
26 # - The artifact directory must be on a file system that accepts 512-byte IO
27 #   (t0002, t0003, t0004).
28 # - The artifact directory needs to be on an SSD. Otherwise tests that carry
29 #   out file-based IO will trigger a timeout (t0006).
30 # - 4 CPUs (t0009)
31 # - SciPy (steadystate_tests.py)
32 # - libzbc (zbd tests)
33 # - root privileges (zbd test)
34 # - kernel 4.19 or later for zoned null block devices (zbd tests)
35 # - CUnit support (unittests)
36 #
37 """
38
39 #
40 # TODO  run multiple tests simultaneously
41 # TODO  Add sgunmap tests (requires SAS SSD)
42 #
43
44 import os
45 import sys
46 import json
47 import time
48 import shutil
49 import logging
50 import argparse
51 import platform
52 import traceback
53 import subprocess
54 import multiprocessing
55 from pathlib import Path
56
57
58 class FioTest(object):
59     """Base for all fio tests."""
60
61     def __init__(self, exe_path, parameters, success):
62         self.exe_path = exe_path
63         self.parameters = parameters
64         self.success = success
65         self.output = {}
66         self.artifact_root = None
67         self.testnum = None
68         self.test_dir = None
69         self.passed = True
70         self.failure_reason = ''
71         self.command_file = None
72         self.stdout_file = None
73         self.stderr_file = None
74         self.exitcode_file = None
75
76     def setup(self, artifact_root, testnum):
77         """Setup instance variables for test."""
78
79         self.artifact_root = artifact_root
80         self.testnum = testnum
81         self.test_dir = os.path.join(artifact_root, "{:04d}".format(testnum))
82         if not os.path.exists(self.test_dir):
83             os.mkdir(self.test_dir)
84
85         self.command_file = os.path.join(
86             self.test_dir,
87             "{0}.command".format(os.path.basename(self.exe_path)))
88         self.stdout_file = os.path.join(
89             self.test_dir,
90             "{0}.stdout".format(os.path.basename(self.exe_path)))
91         self.stderr_file = os.path.join(
92             self.test_dir,
93             "{0}.stderr".format(os.path.basename(self.exe_path)))
94         self.exitcode_file = os.path.join(
95             self.test_dir,
96             "{0}.exitcode".format(os.path.basename(self.exe_path)))
97
98     def run(self):
99         """Run the test."""
100
101         raise NotImplementedError()
102
103     def check_result(self):
104         """Check test results."""
105
106         raise NotImplementedError()
107
108
109 class FioExeTest(FioTest):
110     """Test consists of an executable binary or script"""
111
112     def __init__(self, exe_path, parameters, success):
113         """Construct a FioExeTest which is a FioTest consisting of an
114         executable binary or script.
115
116         exe_path:       location of executable binary or script
117         parameters:     list of parameters for executable
118         success:        Definition of test success
119         """
120
121         FioTest.__init__(self, exe_path, parameters, success)
122
123     def run(self):
124         """Execute the binary or script described by this instance."""
125
126         command = [self.exe_path] + self.parameters
127         command_file = open(self.command_file, "w+")
128         command_file.write("%s\n" % command)
129         command_file.close()
130
131         stdout_file = open(self.stdout_file, "w+")
132         stderr_file = open(self.stderr_file, "w+")
133         exitcode_file = open(self.exitcode_file, "w+")
134         try:
135             proc = None
136             # Avoid using subprocess.run() here because when a timeout occurs,
137             # fio will be stopped with SIGKILL. This does not give fio a
138             # chance to clean up and means that child processes may continue
139             # running and submitting IO.
140             proc = subprocess.Popen(command,
141                                     stdout=stdout_file,
142                                     stderr=stderr_file,
143                                     cwd=self.test_dir,
144                                     universal_newlines=True)
145             proc.communicate(timeout=self.success['timeout'])
146             exitcode_file.write('{0}\n'.format(proc.returncode))
147             logging.debug("Test %d: return code: %d", self.testnum, proc.returncode)
148             self.output['proc'] = proc
149         except subprocess.TimeoutExpired:
150             proc.terminate()
151             proc.communicate()
152             assert proc.poll()
153             self.output['failure'] = 'timeout'
154         except Exception:
155             if proc:
156                 if not proc.poll():
157                     proc.terminate()
158                     proc.communicate()
159             self.output['failure'] = 'exception'
160             self.output['exc_info'] = sys.exc_info()
161         finally:
162             stdout_file.close()
163             stderr_file.close()
164             exitcode_file.close()
165
166     def check_result(self):
167         """Check results of test run."""
168
169         if 'proc' not in self.output:
170             if self.output['failure'] == 'timeout':
171                 self.failure_reason = "{0} timeout,".format(self.failure_reason)
172             else:
173                 assert self.output['failure'] == 'exception'
174                 self.failure_reason = '{0} exception: {1}, {2}'.format(
175                     self.failure_reason, self.output['exc_info'][0],
176                     self.output['exc_info'][1])
177
178             self.passed = False
179             return
180
181         if 'zero_return' in self.success:
182             if self.success['zero_return']:
183                 if self.output['proc'].returncode != 0:
184                     self.passed = False
185                     self.failure_reason = "{0} non-zero return code,".format(self.failure_reason)
186             else:
187                 if self.output['proc'].returncode == 0:
188                     self.failure_reason = "{0} zero return code,".format(self.failure_reason)
189                     self.passed = False
190
191         stderr_size = os.path.getsize(self.stderr_file)
192         if 'stderr_empty' in self.success:
193             if self.success['stderr_empty']:
194                 if stderr_size != 0:
195                     self.failure_reason = "{0} stderr not empty,".format(self.failure_reason)
196                     self.passed = False
197             else:
198                 if stderr_size == 0:
199                     self.failure_reason = "{0} stderr empty,".format(self.failure_reason)
200                     self.passed = False
201
202
203 class FioJobTest(FioExeTest):
204     """Test consists of a fio job"""
205
206     def __init__(self, fio_path, fio_job, success, fio_pre_job=None,
207                  fio_pre_success=None, output_format="normal"):
208         """Construct a FioJobTest which is a FioExeTest consisting of a
209         single fio job file with an optional setup step.
210
211         fio_path:           location of fio executable
212         fio_job:            location of fio job file
213         success:            Definition of test success
214         fio_pre_job:        fio job for preconditioning
215         fio_pre_success:    Definition of test success for fio precon job
216         output_format:      normal (default), json, jsonplus, or terse
217         """
218
219         self.fio_job = fio_job
220         self.fio_pre_job = fio_pre_job
221         self.fio_pre_success = fio_pre_success if fio_pre_success else success
222         self.output_format = output_format
223         self.precon_failed = False
224         self.json_data = None
225         self.fio_output = "{0}.output".format(os.path.basename(self.fio_job))
226         self.fio_args = [
227             "--max-jobs=16",
228             "--output-format={0}".format(self.output_format),
229             "--output={0}".format(self.fio_output),
230             self.fio_job,
231             ]
232         FioExeTest.__init__(self, fio_path, self.fio_args, success)
233
234     def setup(self, artifact_root, testnum):
235         """Setup instance variables for fio job test."""
236
237         super(FioJobTest, self).setup(artifact_root, testnum)
238
239         self.command_file = os.path.join(
240             self.test_dir,
241             "{0}.command".format(os.path.basename(self.fio_job)))
242         self.stdout_file = os.path.join(
243             self.test_dir,
244             "{0}.stdout".format(os.path.basename(self.fio_job)))
245         self.stderr_file = os.path.join(
246             self.test_dir,
247             "{0}.stderr".format(os.path.basename(self.fio_job)))
248         self.exitcode_file = os.path.join(
249             self.test_dir,
250             "{0}.exitcode".format(os.path.basename(self.fio_job)))
251
252     def run_pre_job(self):
253         """Run fio job precondition step."""
254
255         precon = FioJobTest(self.exe_path, self.fio_pre_job,
256                             self.fio_pre_success,
257                             output_format=self.output_format)
258         precon.setup(self.artifact_root, self.testnum)
259         precon.run()
260         precon.check_result()
261         self.precon_failed = not precon.passed
262         self.failure_reason = precon.failure_reason
263
264     def run(self):
265         """Run fio job test."""
266
267         if self.fio_pre_job:
268             self.run_pre_job()
269
270         if not self.precon_failed:
271             super(FioJobTest, self).run()
272         else:
273             logging.debug("Test %d: precondition step failed", self.testnum)
274
275     @classmethod
276     def get_file(cls, filename):
277         """Safely read a file."""
278         file_data = ''
279         success = True
280
281         try:
282             with open(filename, "r") as output_file:
283                 file_data = output_file.read()
284         except OSError:
285             success = False
286
287         return file_data, success
288
289     def check_result(self):
290         """Check fio job results."""
291
292         if self.precon_failed:
293             self.passed = False
294             self.failure_reason = "{0} precondition step failed,".format(self.failure_reason)
295             return
296
297         super(FioJobTest, self).check_result()
298
299         if not self.passed:
300             return
301
302         if 'json' not in self.output_format:
303             return
304
305         file_data, success = self.get_file(os.path.join(self.test_dir, self.fio_output))
306         if not success:
307             self.failure_reason = "{0} unable to open output file,".format(self.failure_reason)
308             self.passed = False
309             return
310
311         #
312         # Sometimes fio informational messages are included at the top of the
313         # JSON output, especially under Windows. Try to decode output as JSON
314         # data, skipping everything until the first {
315         #
316         lines = file_data.splitlines()
317         file_data = '\n'.join(lines[lines.index("{"):])
318         try:
319             self.json_data = json.loads(file_data)
320         except json.JSONDecodeError:
321             self.failure_reason = "{0} unable to decode JSON data,".format(self.failure_reason)
322             self.passed = False
323
324
325 class FioJobTest_t0005(FioJobTest):
326     """Test consists of fio test job t0005
327     Confirm that read['io_kbytes'] == write['io_kbytes'] == 102400"""
328
329     def check_result(self):
330         super(FioJobTest_t0005, self).check_result()
331
332         if not self.passed:
333             return
334
335         if self.json_data['jobs'][0]['read']['io_kbytes'] != 102400:
336             self.failure_reason = "{0} bytes read mismatch,".format(self.failure_reason)
337             self.passed = False
338         if self.json_data['jobs'][0]['write']['io_kbytes'] != 102400:
339             self.failure_reason = "{0} bytes written mismatch,".format(self.failure_reason)
340             self.passed = False
341
342
343 class FioJobTest_t0006(FioJobTest):
344     """Test consists of fio test job t0006
345     Confirm that read['io_kbytes'] ~ 2*write['io_kbytes']"""
346
347     def check_result(self):
348         super(FioJobTest_t0006, self).check_result()
349
350         if not self.passed:
351             return
352
353         ratio = self.json_data['jobs'][0]['read']['io_kbytes'] \
354             / self.json_data['jobs'][0]['write']['io_kbytes']
355         logging.debug("Test %d: ratio: %f", self.testnum, ratio)
356         if ratio < 1.99 or ratio > 2.01:
357             self.failure_reason = "{0} read/write ratio mismatch,".format(self.failure_reason)
358             self.passed = False
359
360
361 class FioJobTest_t0007(FioJobTest):
362     """Test consists of fio test job t0007
363     Confirm that read['io_kbytes'] = 87040"""
364
365     def check_result(self):
366         super(FioJobTest_t0007, self).check_result()
367
368         if not self.passed:
369             return
370
371         if self.json_data['jobs'][0]['read']['io_kbytes'] != 87040:
372             self.failure_reason = "{0} bytes read mismatch,".format(self.failure_reason)
373             self.passed = False
374
375
376 class FioJobTest_t0008(FioJobTest):
377     """Test consists of fio test job t0008
378     Confirm that read['io_kbytes'] = 32768 and that
379                 write['io_kbytes'] ~ 16568
380
381     I did runs with fio-ae2fafc8 and saw write['io_kbytes'] values of
382     16585, 16588. With two runs of fio-3.16 I obtained 16568"""
383
384     def check_result(self):
385         super(FioJobTest_t0008, self).check_result()
386
387         if not self.passed:
388             return
389
390         ratio = self.json_data['jobs'][0]['write']['io_kbytes'] / 16568
391         logging.debug("Test %d: ratio: %f", self.testnum, ratio)
392
393         if ratio < 0.99 or ratio > 1.01:
394             self.failure_reason = "{0} bytes written mismatch,".format(self.failure_reason)
395             self.passed = False
396         if self.json_data['jobs'][0]['read']['io_kbytes'] != 32768:
397             self.failure_reason = "{0} bytes read mismatch,".format(self.failure_reason)
398             self.passed = False
399
400
401 class FioJobTest_t0009(FioJobTest):
402     """Test consists of fio test job t0009
403     Confirm that runtime >= 60s"""
404
405     def check_result(self):
406         super(FioJobTest_t0009, self).check_result()
407
408         if not self.passed:
409             return
410
411         logging.debug('Test %d: elapsed: %d', self.testnum, self.json_data['jobs'][0]['elapsed'])
412
413         if self.json_data['jobs'][0]['elapsed'] < 60:
414             self.failure_reason = "{0} elapsed time mismatch,".format(self.failure_reason)
415             self.passed = False
416
417
418 class FioJobTest_t0012(FioJobTest):
419     """Test consists of fio test job t0012
420     Confirm ratios of job iops are 1:5:10
421     job1,job2,job3 respectively"""
422
423     def check_result(self):
424         super(FioJobTest_t0012, self).check_result()
425
426         if not self.passed:
427             return
428
429         iops_files = []
430         for i in range(1,4):
431             file_data, success = self.get_file(os.path.join(self.test_dir, "{0}_iops.{1}.log".format(os.path.basename(self.fio_job), i)))
432
433             if not success:
434                 self.failure_reason = "{0} unable to open output file,".format(self.failure_reason)
435                 self.passed = False
436                 return
437
438             iops_files.append(file_data.splitlines())
439
440         # there are 9 samples for job1 and job2, 4 samples for job3
441         iops1 = 0.0
442         iops2 = 0.0
443         iops3 = 0.0
444         for i in range(9):
445             iops1 = iops1 + float(iops_files[0][i].split(',')[1])
446             iops2 = iops2 + float(iops_files[1][i].split(',')[1])
447             iops3 = iops3 + float(iops_files[2][i].split(',')[1])
448
449             ratio1 = iops3/iops2
450             ratio2 = iops3/iops1
451             logging.debug(
452                 "sample {0}: job1 iops={1} job2 iops={2} job3 iops={3} job3/job2={4:.3f} job3/job1={5:.3f}".format(
453                     i, iops1, iops2, iops3, ratio1, ratio2
454                 )
455             )
456
457         # test job1 and job2 succeeded to recalibrate
458         if ratio1 < 1 or ratio1 > 3 or ratio2 < 7 or ratio2 > 13:
459             self.failure_reason = "{0} iops ratio mismatch iops1={1} iops2={2} iops3={3} expected r1~2 r2~10 got r1={4:.3f} r2={5:.3f},".format(
460                 self.failure_reason, iops1, iops2, iops3, ratio1, ratio2
461             )
462             self.passed = False
463             return
464
465
466 class FioJobTest_t0014(FioJobTest):
467     """Test consists of fio test job t0014
468         Confirm that job1_iops / job2_iops ~ 1:2 for entire duration
469         and that job1_iops / job3_iops ~ 1:3 for first half of duration.
470
471     The test is about making sure the flow feature can
472     re-calibrate the activity dynamically"""
473
474     def check_result(self):
475         super(FioJobTest_t0014, self).check_result()
476
477         if not self.passed:
478             return
479
480         iops_files = []
481         for i in range(1,4):
482             file_data, success = self.get_file(os.path.join(self.test_dir, "{0}_iops.{1}.log".format(os.path.basename(self.fio_job), i)))
483
484             if not success:
485                 self.failure_reason = "{0} unable to open output file,".format(self.failure_reason)
486                 self.passed = False
487                 return
488
489             iops_files.append(file_data.splitlines())
490
491         # there are 9 samples for job1 and job2, 4 samples for job3
492         iops1 = 0.0
493         iops2 = 0.0
494         iops3 = 0.0
495         for i in range(9):
496             if i < 4:
497                 iops3 = iops3 + float(iops_files[2][i].split(',')[1])
498             elif i == 4:
499                 ratio1 = iops1 / iops2
500                 ratio2 = iops1 / iops3
501
502
503                 if ratio1 < 0.43 or ratio1 > 0.57 or ratio2 < 0.21 or ratio2 > 0.45:
504                     self.failure_reason = "{0} iops ratio mismatch iops1={1} iops2={2} iops3={3}\
505                                                 expected r1~0.5 r2~0.33 got r1={4:.3f} r2={5:.3f},".format(
506                         self.failure_reason, iops1, iops2, iops3, ratio1, ratio2
507                     )
508                     self.passed = False
509
510             iops1 = iops1 + float(iops_files[0][i].split(',')[1])
511             iops2 = iops2 + float(iops_files[1][i].split(',')[1])
512
513             ratio1 = iops1/iops2
514             ratio2 = iops1/iops3
515             logging.debug(
516                 "sample {0}: job1 iops={1} job2 iops={2} job3 iops={3} job1/job2={4:.3f} job1/job3={5:.3f}".format(
517                     i, iops1, iops2, iops3, ratio1, ratio2
518                 )
519             )
520
521         # test job1 and job2 succeeded to recalibrate
522         if ratio1 < 0.43 or ratio1 > 0.57:
523             self.failure_reason = "{0} iops ratio mismatch iops1={1} iops2={2} expected ratio~0.5 got ratio={3:.3f},".format(
524                 self.failure_reason, iops1, iops2, ratio1
525             )
526             self.passed = False
527             return
528
529
530 class FioJobTest_t0015(FioJobTest):
531     """Test consists of fio test jobs t0015 and t0016
532     Confirm that mean(slat) + mean(clat) = mean(tlat)"""
533
534     def check_result(self):
535         super(FioJobTest_t0015, self).check_result()
536
537         if not self.passed:
538             return
539
540         slat = self.json_data['jobs'][0]['read']['slat_ns']['mean']
541         clat = self.json_data['jobs'][0]['read']['clat_ns']['mean']
542         tlat = self.json_data['jobs'][0]['read']['lat_ns']['mean']
543         logging.debug('Test %d: slat %f, clat %f, tlat %f', self.testnum, slat, clat, tlat)
544
545         if abs(slat + clat - tlat) > 1:
546             self.failure_reason = "{0} slat {1} + clat {2} = {3} != tlat {4},".format(
547                 self.failure_reason, slat, clat, slat+clat, tlat)
548             self.passed = False
549
550
551 class FioJobTest_t0019(FioJobTest):
552     """Test consists of fio test job t0019
553     Confirm that all offsets were touched sequentially"""
554
555     def check_result(self):
556         super(FioJobTest_t0019, self).check_result()
557
558         bw_log_filename = os.path.join(self.test_dir, "test_bw.log")
559         file_data, success = self.get_file(bw_log_filename)
560         log_lines = file_data.split('\n')
561
562         prev = -4096
563         for line in log_lines:
564             if len(line.strip()) == 0:
565                 continue
566             cur = int(line.split(',')[4])
567             if cur - prev != 4096:
568                 self.passed = False
569                 self.failure_reason = "offsets {0}, {1} not sequential".format(prev, cur)
570                 return
571             prev = cur
572
573         if cur/4096 != 255:
574             self.passed = False
575             self.failure_reason = "unexpected last offset {0}".format(cur)
576
577
578 class FioJobTest_t0020(FioJobTest):
579     """Test consists of fio test jobs t0020 and t0021
580     Confirm that almost all offsets were touched non-sequentially"""
581
582     def check_result(self):
583         super(FioJobTest_t0020, self).check_result()
584
585         bw_log_filename = os.path.join(self.test_dir, "test_bw.log")
586         file_data, success = self.get_file(bw_log_filename)
587         log_lines = file_data.split('\n')
588
589         seq_count = 0
590         offsets = set()
591
592         prev = int(log_lines[0].split(',')[4])
593         for line in log_lines[1:]:
594             offsets.add(prev/4096)
595             if len(line.strip()) == 0:
596                 continue
597             cur = int(line.split(',')[4])
598             if cur - prev == 4096:
599                 seq_count += 1
600             prev = cur
601
602         # 10 is an arbitrary threshold
603         if seq_count > 10:
604             self.passed = False
605             self.failure_reason = "too many ({0}) consecutive offsets".format(seq_count)
606
607         if len(offsets) != 256:
608             self.passed = False
609             self.failure_reason += " number of offsets is {0} instead of 256".format(len(offsets))
610
611         for i in range(256):
612             if not i in offsets:
613                 self.passed = False
614                 self.failure_reason += " missing offset {0}".format(i*4096)
615
616
617 class FioJobTest_t0022(FioJobTest):
618     """Test consists of fio test job t0022"""
619
620     def check_result(self):
621         super(FioJobTest_t0022, self).check_result()
622
623         bw_log_filename = os.path.join(self.test_dir, "test_bw.log")
624         file_data, success = self.get_file(bw_log_filename)
625         log_lines = file_data.split('\n')
626
627         filesize = 1024*1024
628         bs = 4096
629         seq_count = 0
630         offsets = set()
631
632         prev = int(log_lines[0].split(',')[4])
633         for line in log_lines[1:]:
634             offsets.add(prev/bs)
635             if len(line.strip()) == 0:
636                 continue
637             cur = int(line.split(',')[4])
638             if cur - prev == bs:
639                 seq_count += 1
640             prev = cur
641
642         # 10 is an arbitrary threshold
643         if seq_count > 10:
644             self.passed = False
645             self.failure_reason = "too many ({0}) consecutive offsets".format(seq_count)
646
647         if len(offsets) == filesize/bs:
648             self.passed = False
649             self.failure_reason += " no duplicate offsets found with norandommap=1".format(len(offsets))
650
651
652 class FioJobTest_t0023(FioJobTest):
653     """Test consists of fio test job t0023"""
654
655     def check_trimwrite(self, filename):
656         """Make sure that trims are followed by writes of the same size at the same offset."""
657
658         bw_log_filename = os.path.join(self.test_dir, filename)
659         file_data, success = self.get_file(bw_log_filename)
660         log_lines = file_data.split('\n')
661
662         prev_ddir = 1
663         for line in log_lines:
664             if len(line.strip()) == 0:
665                 continue
666             vals = line.split(',')
667             ddir = int(vals[2])
668             bs = int(vals[3])
669             offset = int(vals[4])
670             if prev_ddir == 1:
671                 if ddir != 2:
672                     self.passed = False
673                     self.failure_reason += " {0}: write not preceeded by trim: {1}".format(bw_log_filename, line)
674                     break
675             else:
676                 if ddir != 1:
677                     self.passed = False
678                     self.failure_reason += " {0}: trim not preceeded by write: {1}".format(bw_log_filename, line)
679                     break
680                 else:
681                     if prev_bs != bs:
682                         self.passed = False
683                         self.failure_reason += " {0}: block size does not match: {1}".format(bw_log_filename, line)
684                         break
685                     if prev_offset != offset:
686                         self.passed = False
687                         self.failure_reason += " {0}: offset does not match: {1}".format(bw_log_filename, line)
688                         break
689             prev_ddir = ddir
690             prev_bs = bs
691             prev_offset = offset
692
693
694     def check_all_offsets(self, filename, sectorsize, filesize):
695         file_data, success = self.get_file(os.path.join(self.test_dir, filename))
696         if not success:
697             self.passed = False
698             self.failure_reason = " could not open {0}".format(filename)
699             return
700
701         log_lines = file_data.split('\n')
702
703         offsets = set()
704
705         for line in log_lines:
706             if len(line.strip()) == 0:
707                 continue
708             vals = line.split(',')
709             bs = int(vals[3])
710             offset = int(vals[4])
711             if offset % sectorsize != 0:
712                 self.passed = False
713                 self.failure_reason += " {0}: offset {1} not a multiple of sector size {2}".format(filename, offset, sectorsize)
714                 break;
715             if bs % sectorsize != 0:
716                 self.passed = False
717                 self.failure_reason += " {0}: block size {1} not a multiple of sector size {2}".format(filename, bs, sectorsize)
718                 break;
719             for i in range(int(bs/sectorsize)):
720                 offsets.add(offset/sectorsize + i)
721
722         if len(offsets) != filesize/sectorsize:
723             self.passed = False
724             self.failure_reason += " {0}: only {1} offsets touched; expected {2}".format(filename, len(offsets), filesize/sectorsize)
725         else:
726             logging.debug("{0}: {1}  sectors touched".format(filename, len(offsets)))
727
728
729     def check_result(self):
730         super(FioJobTest_t0023, self).check_result()
731
732         filesize = 1024*1024
733
734         self.check_trimwrite("basic_bw.log")
735         self.check_trimwrite("bs_bw.log")
736         self.check_trimwrite("bsrange_bw.log")
737         self.check_trimwrite("bssplit_bw.log")
738         self.check_trimwrite("basic_no_rm_bw.log")
739         self.check_trimwrite("bs_no_rm_bw.log")
740         self.check_trimwrite("bsrange_no_rm_bw.log")
741         self.check_trimwrite("bssplit_no_rm_bw.log")
742
743         self.check_all_offsets("basic_bw.log", 4096, filesize)
744         self.check_all_offsets("bs_bw.log", 8192, filesize)
745         self.check_all_offsets("bsrange_bw.log", 512, filesize)
746         self.check_all_offsets("bssplit_bw.log", 512, filesize)
747
748
749 class FioJobTest_iops_rate(FioJobTest):
750     """Test consists of fio test job t0009
751     Confirm that job0 iops == 1000
752     and that job1_iops / job0_iops ~ 8
753     With two runs of fio-3.16 I observed a ratio of 8.3"""
754
755     def check_result(self):
756         super(FioJobTest_iops_rate, self).check_result()
757
758         if not self.passed:
759             return
760
761         iops1 = self.json_data['jobs'][0]['read']['iops']
762         logging.debug("Test %d: iops1: %f", self.testnum, iops1)
763         iops2 = self.json_data['jobs'][1]['read']['iops']
764         logging.debug("Test %d: iops2: %f", self.testnum, iops2)
765         ratio = iops2 / iops1
766         logging.debug("Test %d: ratio: %f", self.testnum, ratio)
767
768         if iops1 < 950 or iops1 > 1050:
769             self.failure_reason = "{0} iops value mismatch,".format(self.failure_reason)
770             self.passed = False
771
772         if ratio < 6 or ratio > 10:
773             self.failure_reason = "{0} iops ratio mismatch,".format(self.failure_reason)
774             self.passed = False
775
776
777 class Requirements(object):
778     """Requirements consists of multiple run environment characteristics.
779     These are to determine if a particular test can be run"""
780
781     _linux = False
782     _libaio = False
783     _io_uring = False
784     _zbd = False
785     _root = False
786     _zoned_nullb = False
787     _not_macos = False
788     _not_windows = False
789     _unittests = False
790     _cpucount4 = False
791
792     def __init__(self, fio_root):
793         Requirements._not_macos = platform.system() != "Darwin"
794         Requirements._not_windows = platform.system() != "Windows"
795         Requirements._linux = platform.system() == "Linux"
796
797         if Requirements._linux:
798             config_file = os.path.join(fio_root, "config-host.h")
799             contents, success = FioJobTest.get_file(config_file)
800             if not success:
801                 print("Unable to open {0} to check requirements".format(config_file))
802                 Requirements._zbd = True
803             else:
804                 Requirements._zbd = "CONFIG_HAS_BLKZONED" in contents
805                 Requirements._libaio = "CONFIG_LIBAIO" in contents
806
807             contents, success = FioJobTest.get_file("/proc/kallsyms")
808             if not success:
809                 print("Unable to open '/proc/kallsyms' to probe for io_uring support")
810             else:
811                 Requirements._io_uring = "io_uring_setup" in contents
812
813             Requirements._root = (os.geteuid() == 0)
814             if Requirements._zbd and Requirements._root:
815                 try:
816                     subprocess.run(["modprobe", "null_blk"],
817                                    stdout=subprocess.PIPE,
818                                    stderr=subprocess.PIPE)
819                     if os.path.exists("/sys/module/null_blk/parameters/zoned"):
820                         Requirements._zoned_nullb = True
821                 except Exception:
822                     pass
823
824         if platform.system() == "Windows":
825             utest_exe = "unittest.exe"
826         else:
827             utest_exe = "unittest"
828         unittest_path = os.path.join(fio_root, "unittests", utest_exe)
829         Requirements._unittests = os.path.exists(unittest_path)
830
831         Requirements._cpucount4 = multiprocessing.cpu_count() >= 4
832
833         req_list = [Requirements.linux,
834                     Requirements.libaio,
835                     Requirements.io_uring,
836                     Requirements.zbd,
837                     Requirements.root,
838                     Requirements.zoned_nullb,
839                     Requirements.not_macos,
840                     Requirements.not_windows,
841                     Requirements.unittests,
842                     Requirements.cpucount4]
843         for req in req_list:
844             value, desc = req()
845             logging.debug("Requirements: Requirement '%s' met? %s", desc, value)
846
847     @classmethod
848     def linux(cls):
849         """Are we running on Linux?"""
850         return Requirements._linux, "Linux required"
851
852     @classmethod
853     def libaio(cls):
854         """Is libaio available?"""
855         return Requirements._libaio, "libaio required"
856
857     @classmethod
858     def io_uring(cls):
859         """Is io_uring available?"""
860         return Requirements._io_uring, "io_uring required"
861
862     @classmethod
863     def zbd(cls):
864         """Is ZBD support available?"""
865         return Requirements._zbd, "Zoned block device support required"
866
867     @classmethod
868     def root(cls):
869         """Are we running as root?"""
870         return Requirements._root, "root required"
871
872     @classmethod
873     def zoned_nullb(cls):
874         """Are zoned null block devices available?"""
875         return Requirements._zoned_nullb, "Zoned null block device support required"
876
877     @classmethod
878     def not_macos(cls):
879         """Are we running on a platform other than macOS?"""
880         return Requirements._not_macos, "platform other than macOS required"
881
882     @classmethod
883     def not_windows(cls):
884         """Are we running on a platform other than Windws?"""
885         return Requirements._not_windows, "platform other than Windows required"
886
887     @classmethod
888     def unittests(cls):
889         """Were unittests built?"""
890         return Requirements._unittests, "Unittests support required"
891
892     @classmethod
893     def cpucount4(cls):
894         """Do we have at least 4 CPUs?"""
895         return Requirements._cpucount4, "4+ CPUs required"
896
897
898 SUCCESS_DEFAULT = {
899     'zero_return': True,
900     'stderr_empty': True,
901     'timeout': 600,
902     }
903 SUCCESS_NONZERO = {
904     'zero_return': False,
905     'stderr_empty': False,
906     'timeout': 600,
907     }
908 SUCCESS_STDERR = {
909     'zero_return': True,
910     'stderr_empty': False,
911     'timeout': 600,
912     }
913 TEST_LIST = [
914     {
915         'test_id':          1,
916         'test_class':       FioJobTest,
917         'job':              't0001-52c58027.fio',
918         'success':          SUCCESS_DEFAULT,
919         'pre_job':          None,
920         'pre_success':      None,
921         'requirements':     [],
922     },
923     {
924         'test_id':          2,
925         'test_class':       FioJobTest,
926         'job':              't0002-13af05ae-post.fio',
927         'success':          SUCCESS_DEFAULT,
928         'pre_job':          't0002-13af05ae-pre.fio',
929         'pre_success':      None,
930         'requirements':     [Requirements.linux, Requirements.libaio],
931     },
932     {
933         'test_id':          3,
934         'test_class':       FioJobTest,
935         'job':              't0003-0ae2c6e1-post.fio',
936         'success':          SUCCESS_NONZERO,
937         'pre_job':          't0003-0ae2c6e1-pre.fio',
938         'pre_success':      SUCCESS_DEFAULT,
939         'requirements':     [Requirements.linux, Requirements.libaio],
940     },
941     {
942         'test_id':          4,
943         'test_class':       FioJobTest,
944         'job':              't0004-8a99fdf6.fio',
945         'success':          SUCCESS_DEFAULT,
946         'pre_job':          None,
947         'pre_success':      None,
948         'requirements':     [Requirements.linux, Requirements.libaio],
949     },
950     {
951         'test_id':          5,
952         'test_class':       FioJobTest_t0005,
953         'job':              't0005-f7078f7b.fio',
954         'success':          SUCCESS_DEFAULT,
955         'pre_job':          None,
956         'pre_success':      None,
957         'output_format':    'json',
958         'requirements':     [Requirements.not_windows],
959     },
960     {
961         'test_id':          6,
962         'test_class':       FioJobTest_t0006,
963         'job':              't0006-82af2a7c.fio',
964         'success':          SUCCESS_DEFAULT,
965         'pre_job':          None,
966         'pre_success':      None,
967         'output_format':    'json',
968         'requirements':     [Requirements.linux, Requirements.libaio],
969     },
970     {
971         'test_id':          7,
972         'test_class':       FioJobTest_t0007,
973         'job':              't0007-37cf9e3c.fio',
974         'success':          SUCCESS_DEFAULT,
975         'pre_job':          None,
976         'pre_success':      None,
977         'output_format':    'json',
978         'requirements':     [],
979     },
980     {
981         'test_id':          8,
982         'test_class':       FioJobTest_t0008,
983         'job':              't0008-ae2fafc8.fio',
984         'success':          SUCCESS_DEFAULT,
985         'pre_job':          None,
986         'pre_success':      None,
987         'output_format':    'json',
988         'requirements':     [],
989     },
990     {
991         'test_id':          9,
992         'test_class':       FioJobTest_t0009,
993         'job':              't0009-f8b0bd10.fio',
994         'success':          SUCCESS_DEFAULT,
995         'pre_job':          None,
996         'pre_success':      None,
997         'output_format':    'json',
998         'requirements':     [Requirements.not_macos,
999                              Requirements.cpucount4],
1000         # mac os does not support CPU affinity
1001     },
1002     {
1003         'test_id':          10,
1004         'test_class':       FioJobTest,
1005         'job':              't0010-b7aae4ba.fio',
1006         'success':          SUCCESS_DEFAULT,
1007         'pre_job':          None,
1008         'pre_success':      None,
1009         'requirements':     [],
1010     },
1011     {
1012         'test_id':          11,
1013         'test_class':       FioJobTest_iops_rate,
1014         'job':              't0011-5d2788d5.fio',
1015         'success':          SUCCESS_DEFAULT,
1016         'pre_job':          None,
1017         'pre_success':      None,
1018         'output_format':    'json',
1019         'requirements':     [],
1020     },
1021     {
1022         'test_id':          12,
1023         'test_class':       FioJobTest_t0012,
1024         'job':              't0012.fio',
1025         'success':          SUCCESS_DEFAULT,
1026         'pre_job':          None,
1027         'pre_success':      None,
1028         'output_format':    'json',
1029         'requirements':     [],
1030     },
1031     {
1032         'test_id':          13,
1033         'test_class':       FioJobTest,
1034         'job':              't0013.fio',
1035         'success':          SUCCESS_DEFAULT,
1036         'pre_job':          None,
1037         'pre_success':      None,
1038         'output_format':    'json',
1039         'requirements':     [],
1040     },
1041     {
1042         'test_id':          14,
1043         'test_class':       FioJobTest_t0014,
1044         'job':              't0014.fio',
1045         'success':          SUCCESS_DEFAULT,
1046         'pre_job':          None,
1047         'pre_success':      None,
1048         'output_format':    'json',
1049         'requirements':     [],
1050     },
1051     {
1052         'test_id':          15,
1053         'test_class':       FioJobTest_t0015,
1054         'job':              't0015-e78980ff.fio',
1055         'success':          SUCCESS_DEFAULT,
1056         'pre_job':          None,
1057         'pre_success':      None,
1058         'output_format':    'json',
1059         'requirements':     [Requirements.linux, Requirements.libaio],
1060     },
1061     {
1062         'test_id':          16,
1063         'test_class':       FioJobTest_t0015,
1064         'job':              't0016-d54ae22.fio',
1065         'success':          SUCCESS_DEFAULT,
1066         'pre_job':          None,
1067         'pre_success':      None,
1068         'output_format':    'json',
1069         'requirements':     [],
1070     },
1071     {
1072         'test_id':          17,
1073         'test_class':       FioJobTest_t0015,
1074         'job':              't0017.fio',
1075         'success':          SUCCESS_DEFAULT,
1076         'pre_job':          None,
1077         'pre_success':      None,
1078         'output_format':    'json',
1079         'requirements':     [Requirements.not_windows],
1080     },
1081     {
1082         'test_id':          18,
1083         'test_class':       FioJobTest,
1084         'job':              't0018.fio',
1085         'success':          SUCCESS_DEFAULT,
1086         'pre_job':          None,
1087         'pre_success':      None,
1088         'requirements':     [Requirements.linux, Requirements.io_uring],
1089     },
1090     {
1091         'test_id':          19,
1092         'test_class':       FioJobTest_t0019,
1093         'job':              't0019.fio',
1094         'success':          SUCCESS_DEFAULT,
1095         'pre_job':          None,
1096         'pre_success':      None,
1097         'requirements':     [],
1098     },
1099     {
1100         'test_id':          20,
1101         'test_class':       FioJobTest_t0020,
1102         'job':              't0020.fio',
1103         'success':          SUCCESS_DEFAULT,
1104         'pre_job':          None,
1105         'pre_success':      None,
1106         'requirements':     [],
1107     },
1108     {
1109         'test_id':          21,
1110         'test_class':       FioJobTest_t0020,
1111         'job':              't0021.fio',
1112         'success':          SUCCESS_DEFAULT,
1113         'pre_job':          None,
1114         'pre_success':      None,
1115         'requirements':     [],
1116     },
1117     {
1118         'test_id':          22,
1119         'test_class':       FioJobTest_t0022,
1120         'job':              't0022.fio',
1121         'success':          SUCCESS_DEFAULT,
1122         'pre_job':          None,
1123         'pre_success':      None,
1124         'requirements':     [],
1125     },
1126     {
1127         'test_id':          23,
1128         'test_class':       FioJobTest_t0023,
1129         'job':              't0023.fio',
1130         'success':          SUCCESS_DEFAULT,
1131         'pre_job':          None,
1132         'pre_success':      None,
1133         'requirements':     [],
1134     },
1135     {
1136         'test_id':          1000,
1137         'test_class':       FioExeTest,
1138         'exe':              't/axmap',
1139         'parameters':       None,
1140         'success':          SUCCESS_DEFAULT,
1141         'requirements':     [],
1142     },
1143     {
1144         'test_id':          1001,
1145         'test_class':       FioExeTest,
1146         'exe':              't/ieee754',
1147         'parameters':       None,
1148         'success':          SUCCESS_DEFAULT,
1149         'requirements':     [],
1150     },
1151     {
1152         'test_id':          1002,
1153         'test_class':       FioExeTest,
1154         'exe':              't/lfsr-test',
1155         'parameters':       ['0xFFFFFF', '0', '0', 'verify'],
1156         'success':          SUCCESS_STDERR,
1157         'requirements':     [],
1158     },
1159     {
1160         'test_id':          1003,
1161         'test_class':       FioExeTest,
1162         'exe':              't/readonly.py',
1163         'parameters':       ['-f', '{fio_path}'],
1164         'success':          SUCCESS_DEFAULT,
1165         'requirements':     [],
1166     },
1167     {
1168         'test_id':          1004,
1169         'test_class':       FioExeTest,
1170         'exe':              't/steadystate_tests.py',
1171         'parameters':       ['{fio_path}'],
1172         'success':          SUCCESS_DEFAULT,
1173         'requirements':     [],
1174     },
1175     {
1176         'test_id':          1005,
1177         'test_class':       FioExeTest,
1178         'exe':              't/stest',
1179         'parameters':       None,
1180         'success':          SUCCESS_STDERR,
1181         'requirements':     [],
1182     },
1183     {
1184         'test_id':          1006,
1185         'test_class':       FioExeTest,
1186         'exe':              't/strided.py',
1187         'parameters':       ['{fio_path}'],
1188         'success':          SUCCESS_DEFAULT,
1189         'requirements':     [],
1190     },
1191     {
1192         'test_id':          1007,
1193         'test_class':       FioExeTest,
1194         'exe':              't/zbd/run-tests-against-nullb',
1195         'parameters':       ['-s', '1'],
1196         'success':          SUCCESS_DEFAULT,
1197         'requirements':     [Requirements.linux, Requirements.zbd,
1198                              Requirements.root],
1199     },
1200     {
1201         'test_id':          1008,
1202         'test_class':       FioExeTest,
1203         'exe':              't/zbd/run-tests-against-nullb',
1204         'parameters':       ['-s', '2'],
1205         'success':          SUCCESS_DEFAULT,
1206         'requirements':     [Requirements.linux, Requirements.zbd,
1207                              Requirements.root, Requirements.zoned_nullb],
1208     },
1209     {
1210         'test_id':          1009,
1211         'test_class':       FioExeTest,
1212         'exe':              'unittests/unittest',
1213         'parameters':       None,
1214         'success':          SUCCESS_DEFAULT,
1215         'requirements':     [Requirements.unittests],
1216     },
1217     {
1218         'test_id':          1010,
1219         'test_class':       FioExeTest,
1220         'exe':              't/latency_percentiles.py',
1221         'parameters':       ['-f', '{fio_path}'],
1222         'success':          SUCCESS_DEFAULT,
1223         'requirements':     [],
1224     },
1225     {
1226         'test_id':          1011,
1227         'test_class':       FioExeTest,
1228         'exe':              't/jsonplus2csv_test.py',
1229         'parameters':       ['-f', '{fio_path}'],
1230         'success':          SUCCESS_DEFAULT,
1231         'requirements':     [],
1232     },
1233     {
1234         'test_id':          1012,
1235         'test_class':       FioExeTest,
1236         'exe':              't/log_compression.py',
1237         'parameters':       ['-f', '{fio_path}'],
1238         'success':          SUCCESS_DEFAULT,
1239         'requirements':     [],
1240     },
1241 ]
1242
1243
1244 def parse_args():
1245     """Parse command-line arguments."""
1246
1247     parser = argparse.ArgumentParser()
1248     parser.add_argument('-r', '--fio-root',
1249                         help='fio root path')
1250     parser.add_argument('-f', '--fio',
1251                         help='path to fio executable (e.g., ./fio)')
1252     parser.add_argument('-a', '--artifact-root',
1253                         help='artifact root directory')
1254     parser.add_argument('-s', '--skip', nargs='+', type=int,
1255                         help='list of test(s) to skip')
1256     parser.add_argument('-o', '--run-only', nargs='+', type=int,
1257                         help='list of test(s) to run, skipping all others')
1258     parser.add_argument('-d', '--debug', action='store_true',
1259                         help='provide debug output')
1260     parser.add_argument('-k', '--skip-req', action='store_true',
1261                         help='skip requirements checking')
1262     parser.add_argument('-p', '--pass-through', action='append',
1263                         help='pass-through an argument to an executable test')
1264     args = parser.parse_args()
1265
1266     return args
1267
1268
1269 def main():
1270     """Entry point."""
1271
1272     args = parse_args()
1273     if args.debug:
1274         logging.basicConfig(level=logging.DEBUG)
1275     else:
1276         logging.basicConfig(level=logging.INFO)
1277
1278     pass_through = {}
1279     if args.pass_through:
1280         for arg in args.pass_through:
1281             if not ':' in arg:
1282                 print("Invalid --pass-through argument '%s'" % arg)
1283                 print("Syntax for --pass-through is TESTNUMBER:ARGUMENT")
1284                 return
1285             split = arg.split(":", 1)
1286             pass_through[int(split[0])] = split[1]
1287         logging.debug("Pass-through arguments: %s", pass_through)
1288
1289     if args.fio_root:
1290         fio_root = args.fio_root
1291     else:
1292         fio_root = str(Path(__file__).absolute().parent.parent)
1293     print("fio root is %s" % fio_root)
1294
1295     if args.fio:
1296         fio_path = args.fio
1297     else:
1298         if platform.system() == "Windows":
1299             fio_exe = "fio.exe"
1300         else:
1301             fio_exe = "fio"
1302         fio_path = os.path.join(fio_root, fio_exe)
1303     print("fio path is %s" % fio_path)
1304     if not shutil.which(fio_path):
1305         print("Warning: fio executable not found")
1306
1307     artifact_root = args.artifact_root if args.artifact_root else \
1308         "fio-test-{0}".format(time.strftime("%Y%m%d-%H%M%S"))
1309     os.mkdir(artifact_root)
1310     print("Artifact directory is %s" % artifact_root)
1311
1312     if not args.skip_req:
1313         req = Requirements(fio_root)
1314
1315     passed = 0
1316     failed = 0
1317     skipped = 0
1318
1319     for config in TEST_LIST:
1320         if (args.skip and config['test_id'] in args.skip) or \
1321            (args.run_only and config['test_id'] not in args.run_only):
1322             skipped = skipped + 1
1323             print("Test {0} SKIPPED (User request)".format(config['test_id']))
1324             continue
1325
1326         if issubclass(config['test_class'], FioJobTest):
1327             if config['pre_job']:
1328                 fio_pre_job = os.path.join(fio_root, 't', 'jobs',
1329                                            config['pre_job'])
1330             else:
1331                 fio_pre_job = None
1332             if config['pre_success']:
1333                 fio_pre_success = config['pre_success']
1334             else:
1335                 fio_pre_success = None
1336             if 'output_format' in config:
1337                 output_format = config['output_format']
1338             else:
1339                 output_format = 'normal'
1340             test = config['test_class'](
1341                 fio_path,
1342                 os.path.join(fio_root, 't', 'jobs', config['job']),
1343                 config['success'],
1344                 fio_pre_job=fio_pre_job,
1345                 fio_pre_success=fio_pre_success,
1346                 output_format=output_format)
1347             desc = config['job']
1348         elif issubclass(config['test_class'], FioExeTest):
1349             exe_path = os.path.join(fio_root, config['exe'])
1350             if config['parameters']:
1351                 parameters = [p.format(fio_path=fio_path) for p in config['parameters']]
1352             else:
1353                 parameters = []
1354             if Path(exe_path).suffix == '.py' and platform.system() == "Windows":
1355                 parameters.insert(0, exe_path)
1356                 exe_path = "python.exe"
1357             if config['test_id'] in pass_through:
1358                 parameters += pass_through[config['test_id']].split()
1359             test = config['test_class'](exe_path, parameters,
1360                                         config['success'])
1361             desc = config['exe']
1362         else:
1363             print("Test {0} FAILED: unable to process test config".format(config['test_id']))
1364             failed = failed + 1
1365             continue
1366
1367         if not args.skip_req:
1368             reqs_met = True
1369             for req in config['requirements']:
1370                 reqs_met, reason = req()
1371                 logging.debug("Test %d: Requirement '%s' met? %s", config['test_id'], reason,
1372                               reqs_met)
1373                 if not reqs_met:
1374                     break
1375             if not reqs_met:
1376                 print("Test {0} SKIPPED ({1}) {2}".format(config['test_id'], reason, desc))
1377                 skipped = skipped + 1
1378                 continue
1379
1380         try:
1381             test.setup(artifact_root, config['test_id'])
1382             test.run()
1383             test.check_result()
1384         except KeyboardInterrupt:
1385             break
1386         except Exception as e:
1387             test.passed = False
1388             test.failure_reason += str(e)
1389             logging.debug("Test %d exception:\n%s\n", config['test_id'], traceback.format_exc())
1390         if test.passed:
1391             result = "PASSED"
1392             passed = passed + 1
1393         else:
1394             result = "FAILED: {0}".format(test.failure_reason)
1395             failed = failed + 1
1396             contents, _ = FioJobTest.get_file(test.stderr_file)
1397             logging.debug("Test %d: stderr:\n%s", config['test_id'], contents)
1398             contents, _ = FioJobTest.get_file(test.stdout_file)
1399             logging.debug("Test %d: stdout:\n%s", config['test_id'], contents)
1400         print("Test {0} {1} {2}".format(config['test_id'], result, desc))
1401
1402     print("{0} test(s) passed, {1} failed, {2} skipped".format(passed, failed, skipped))
1403
1404     sys.exit(failed)
1405
1406
1407 if __name__ == '__main__':
1408     main()