blob: 5f5cfb5b39fa6efd941431085de01bd28e0a4c84 [file] [log] [blame]
koder aka kdanilov70227062016-11-26 23:23:21 +02001import os
2import time
3import stat
4import random
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +02005import logging
koder aka kdanilov70227062016-11-26 23:23:21 +02006import subprocess
7
8
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +02009mod_name = "fio"
10__version__ = (0, 1)
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030011
koder aka kdanilov70227062016-11-26 23:23:21 +020012
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020013logger = logging.getLogger("agent.fio")
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030014
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020015
koder aka kdanilov108ac362017-01-19 20:17:16 +020016# TODO: fix this in case if file is block device
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020017def check_file_prefilled(path, used_size_mb):
koder aka kdanilov70227062016-11-26 23:23:21 +020018 used_size = used_size_mb * 1024 ** 2
19 blocks_to_check = 16
20
21 try:
22 fstats = os.stat(path)
23 if stat.S_ISREG(fstats.st_mode) and fstats.st_size < used_size:
koder aka kdanilov108ac362017-01-19 20:17:16 +020024 return False
koder aka kdanilov70227062016-11-26 23:23:21 +020025 except EnvironmentError:
koder aka kdanilov108ac362017-01-19 20:17:16 +020026 return False
koder aka kdanilov70227062016-11-26 23:23:21 +020027
28 offsets = [random.randrange(used_size - 1024) for _ in range(blocks_to_check)]
29 offsets.append(used_size - 1024)
30 offsets.append(0)
31
32 with open(path, 'rb') as fd:
33 for offset in offsets:
34 fd.seek(offset)
35 if b"\x00" * 1024 == fd.read(1024):
koder aka kdanilov108ac362017-01-19 20:17:16 +020036 return False
koder aka kdanilov70227062016-11-26 23:23:21 +020037
koder aka kdanilov108ac362017-01-19 20:17:16 +020038 return True
koder aka kdanilov70227062016-11-26 23:23:21 +020039
40
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020041def rpc_fill_file(fname, size, force=False, fio_path='fio'):
42 if not force:
koder aka kdanilov108ac362017-01-19 20:17:16 +020043 if check_file_prefilled(fname, size):
44 return False, None
koder aka kdanilov70227062016-11-26 23:23:21 +020045
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020046 assert size % 4 == 0, "File size must be proportional to 4M"
koder aka kdanilov70227062016-11-26 23:23:21 +020047
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020048 cmd_templ = "{} --name=xxx --filename={} --direct=1 --bs=4m --size={}m --rw=write"
koder aka kdanilov70227062016-11-26 23:23:21 +020049
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020050 run_time = time.time()
51 subprocess.check_output(cmd_templ.format(fio_path, fname, size), shell=True)
52 run_time = time.time() - run_time
koder aka kdanilov70227062016-11-26 23:23:21 +020053
koder aka kdanilov108ac362017-01-19 20:17:16 +020054 prefill_bw = None if run_time < 1.0 else int(size / run_time)
55
56 return True, prefill_bw
koder aka kdanilov70227062016-11-26 23:23:21 +020057
58
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020059def rpc_install(name, binary):
60 try:
61 subprocess.check_output("which {}".format(binary), shell=True)
62 except:
63 subprocess.check_output("apt-get install -y {}".format(name), shell=True)