blob: 39ed5cc21752eb3ba0b5850d378344dae43e93e1 [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
kdanylov aka koder150b2192017-04-01 16:53:01 +030017def check_file_prefilled(path, used_size_mb, blocks_to_check=16):
koder aka kdanilov70227062016-11-26 23:23:21 +020018 used_size = used_size_mb * 1024 ** 2
koder aka kdanilov70227062016-11-26 23:23:21 +020019
20 try:
21 fstats = os.stat(path)
22 if stat.S_ISREG(fstats.st_mode) and fstats.st_size < used_size:
koder aka kdanilov108ac362017-01-19 20:17:16 +020023 return False
koder aka kdanilov70227062016-11-26 23:23:21 +020024 except EnvironmentError:
koder aka kdanilov108ac362017-01-19 20:17:16 +020025 return False
koder aka kdanilov70227062016-11-26 23:23:21 +020026
kdanylov aka koder150b2192017-04-01 16:53:01 +030027 offsets = [0, used_size - 1024] + [random.randrange(used_size - 1024) for _ in range(blocks_to_check)]
28 logger.debug(str(offsets))
koder aka kdanilov70227062016-11-26 23:23:21 +020029 with open(path, 'rb') as fd:
30 for offset in offsets:
31 fd.seek(offset)
32 if b"\x00" * 1024 == fd.read(1024):
koder aka kdanilov108ac362017-01-19 20:17:16 +020033 return False
koder aka kdanilov70227062016-11-26 23:23:21 +020034
koder aka kdanilov108ac362017-01-19 20:17:16 +020035 return True
koder aka kdanilov70227062016-11-26 23:23:21 +020036
37
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020038def rpc_fill_file(fname, size, force=False, fio_path='fio'):
39 if not force:
koder aka kdanilov108ac362017-01-19 20:17:16 +020040 if check_file_prefilled(fname, size):
41 return False, None
koder aka kdanilov70227062016-11-26 23:23:21 +020042
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020043 assert size % 4 == 0, "File size must be proportional to 4M"
koder aka kdanilov70227062016-11-26 23:23:21 +020044
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020045 cmd_templ = "{} --name=xxx --filename={} --direct=1 --bs=4m --size={}m --rw=write"
koder aka kdanilov70227062016-11-26 23:23:21 +020046
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020047 run_time = time.time()
48 subprocess.check_output(cmd_templ.format(fio_path, fname, size), shell=True)
49 run_time = time.time() - run_time
koder aka kdanilov70227062016-11-26 23:23:21 +020050
koder aka kdanilov108ac362017-01-19 20:17:16 +020051 prefill_bw = None if run_time < 1.0 else int(size / run_time)
52
53 return True, prefill_bw
koder aka kdanilov70227062016-11-26 23:23:21 +020054
55
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020056def rpc_install(name, binary):
57 try:
58 subprocess.check_output("which {}".format(binary), shell=True)
59 except:
60 subprocess.check_output("apt-get install -y {}".format(name), shell=True)