koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 1 | import os |
| 2 | import time |
| 3 | import stat |
| 4 | import random |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 5 | import logging |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 6 | import subprocess |
| 7 | |
| 8 | |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 9 | mod_name = "fio" |
| 10 | __version__ = (0, 1) |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 11 | |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 12 | |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 13 | logger = logging.getLogger("agent.fio") |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 14 | |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 15 | |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame^] | 16 | # TODO: fix this in case if file is block device |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 17 | def check_file_prefilled(path, used_size_mb): |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 18 | 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 kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame^] | 24 | return False |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 25 | except EnvironmentError: |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame^] | 26 | return False |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 27 | |
| 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 kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame^] | 36 | return False |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 37 | |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame^] | 38 | return True |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 39 | |
| 40 | |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 41 | def rpc_fill_file(fname, size, force=False, fio_path='fio'): |
| 42 | if not force: |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame^] | 43 | if check_file_prefilled(fname, size): |
| 44 | return False, None |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 45 | |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 46 | assert size % 4 == 0, "File size must be proportional to 4M" |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 47 | |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 48 | cmd_templ = "{} --name=xxx --filename={} --direct=1 --bs=4m --size={}m --rw=write" |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 49 | |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 50 | 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 kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 53 | |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame^] | 54 | prefill_bw = None if run_time < 1.0 else int(size / run_time) |
| 55 | |
| 56 | return True, prefill_bw |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 57 | |
| 58 | |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 59 | def 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) |