blob: bf2e6b38fbf5ea2a5615d806c5ccc4870961dc04 [file] [log] [blame]
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +02001import array
koder aka kdanilovbc2c8982015-06-13 02:50:43 +03002import os.path
3import logging
koder aka kdanilov108ac362017-01-19 20:17:16 +02004from typing import cast, Any, Tuple, List
koder aka kdanilovbc2c8982015-06-13 02:50:43 +03005
koder aka kdanilov6ab4d432015-06-22 00:26:28 +03006import wally
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03007
koder aka kdanilov108ac362017-01-19 20:17:16 +02008from ...utils import StopTestError, ssize2b, b2ssize
koder aka kdanilov70227062016-11-26 23:23:21 +02009from ...node_interfaces import IRPCNode
koder aka kdanilov108ac362017-01-19 20:17:16 +020010from ...node_utils import get_os
koder aka kdanilovf2865172016-12-30 03:35:11 +020011from ..itest import ThreadedTest
koder aka kdanilovf90de852017-01-20 18:12:27 +020012from ...result_classes import TimeSeries, DataSource
13from ..job import JobConfig
koder aka kdanilovf2865172016-12-30 03:35:11 +020014from .fio_task_parser import execution_time, fio_cfg_compile, FioJobConfig, FioParams, get_log_files
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020015from . import rpc_plugin
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020016from .fio_hist import expected_lat_bins
koder aka kdanilov7f59d562016-12-26 01:34:23 +020017
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030018
19logger = logging.getLogger("wally")
20
21
koder aka kdanilov108ac362017-01-19 20:17:16 +020022class FioTest(ThreadedTest):
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030023 soft_runcycle = 5 * 60
Michael Semenov8ba6e232015-08-28 10:57:18 +000024 retry_time = 30
koder aka kdanilov70227062016-11-26 23:23:21 +020025 configs_dir = os.path.dirname(__file__) # type: str
koder aka kdanilov7f59d562016-12-26 01:34:23 +020026 name = 'fio'
koder aka kdanilovf2865172016-12-30 03:35:11 +020027 job_config_cls = FioJobConfig
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030028
koder aka kdanilov70227062016-11-26 23:23:21 +020029 def __init__(self, *args, **kwargs) -> None:
30 super().__init__(*args, **kwargs)
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030031
koder aka kdanilov108ac362017-01-19 20:17:16 +020032 get = self.suite.params.get
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030033
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020034 self.remote_task_file = self.join_remote("task.fio")
35 self.remote_output_file = self.join_remote("fio_result.json")
36 self.use_system_fio = get('use_system_fio', False) # type: bool
37 self.use_sudo = get("use_sudo", True) # type: bool
38 self.force_prefill = get('force_prefill', False) # type: bool
39
koder aka kdanilov108ac362017-01-19 20:17:16 +020040 self.load_profile_name = self.suite.params['load'] # type: str
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030041
koder aka kdanilov70227062016-11-26 23:23:21 +020042 if os.path.isfile(self.load_profile_name):
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020043 self.load_profile_path = self.load_profile_name # type: str
koder aka kdanilov6ab4d432015-06-22 00:26:28 +030044 else:
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020045 self.load_profile_path = os.path.join(self.configs_dir, self.load_profile_name+ '.cfg')
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030046
koder aka kdanilov70227062016-11-26 23:23:21 +020047 self.load_profile = open(self.load_profile_path, 'rt').read() # type: str
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030048
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030049 if self.use_system_fio:
koder aka kdanilov70227062016-11-26 23:23:21 +020050 self.fio_path = "fio" # type: str
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030051 else:
koder aka kdanilov108ac362017-01-19 20:17:16 +020052 self.fio_path = os.path.join(self.suite.remote_dir, "fio")
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030053
koder aka kdanilov108ac362017-01-19 20:17:16 +020054 self.load_params = self.suite.params['params']
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020055 self.file_name = self.load_params['FILENAME']
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030056
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020057 if 'FILESIZE' not in self.load_params:
58 logger.debug("Getting test file sizes on all nodes")
59 try:
koder aka kdanilov108ac362017-01-19 20:17:16 +020060 sizes = {node.conn.fs.file_stat(self.file_name)[b'size']
61 for node in self.suite.nodes}
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020062 except Exception:
63 logger.exception("FILESIZE is not set in config file and fail to detect it." +
64 "Set FILESIZE or fix error and rerun test")
65 raise StopTestError()
koder aka kdanilova94dfe12015-08-19 13:04:51 +030066
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020067 if len(sizes) != 1:
68 logger.error("IO target file %r has different sizes on test nodes - %r",
69 self.file_name, sizes)
70 raise StopTestError()
koder aka kdanilova94dfe12015-08-19 13:04:51 +030071
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020072 self.file_size = list(sizes)[0]
koder aka kdanilov108ac362017-01-19 20:17:16 +020073 logger.info("Detected test file size is %sB", b2ssize(self.file_size))
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020074 self.load_params['FILESIZE'] = self.file_size
75 else:
76 self.file_size = ssize2b(self.load_params['FILESIZE'])
koder aka kdanilov70227062016-11-26 23:23:21 +020077
koder aka kdanilovf2865172016-12-30 03:35:11 +020078 self.job_configs = list(fio_cfg_compile(self.load_profile, self.load_profile_path,
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020079 cast(FioParams, self.load_params)))
koder aka kdanilov70227062016-11-26 23:23:21 +020080
koder aka kdanilovf2865172016-12-30 03:35:11 +020081 if len(self.job_configs) == 0:
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020082 logger.error("Empty fio config provided")
83 raise StopTestError()
koder aka kdanilov70227062016-11-26 23:23:21 +020084
koder aka kdanilov108ac362017-01-19 20:17:16 +020085 self.exec_folder = self.suite.remote_dir
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030086
koder aka kdanilov70227062016-11-26 23:23:21 +020087 def config_node(self, node: IRPCNode) -> None:
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +020088 plugin_code = open(rpc_plugin.__file__.rsplit(".", 1)[0] + ".py", "rb").read() # type: bytes
89 node.upload_plugin("fio", plugin_code)
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020090
koder aka kdanilovbc2c8982015-06-13 02:50:43 +030091 try:
koder aka kdanilov108ac362017-01-19 20:17:16 +020092 node.conn.fs.rmtree(self.suite.remote_dir)
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020093 except Exception:
94 pass
95
96 try:
koder aka kdanilov108ac362017-01-19 20:17:16 +020097 node.conn.fs.makedirs(self.suite.remote_dir)
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +020098 except Exception:
koder aka kdanilov108ac362017-01-19 20:17:16 +020099 msg = "Failed to recreate folder {} on remote {}.".format(self.suite.remote_dir, node)
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300100 logger.exception(msg)
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200101 raise StopTestError()
koder aka kdanilovbc2c8982015-06-13 02:50:43 +0300102
koder aka kdanilov108ac362017-01-19 20:17:16 +0200103 # TODO: check this during config validation
104 if self.file_size % (4 * (1024 ** 2)) != 0:
105 logger.error("Test file size must be proportional to 4MiB")
106 raise StopTestError()
107
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300108 self.install_utils(node)
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200109
110 mb = int(self.file_size / 1024 ** 2)
koder aka kdanilov108ac362017-01-19 20:17:16 +0200111 logger.info("Filling test file %s on node %s with %sMiB of random data", self.file_name, node.info, mb)
112 is_prefilled, fill_bw = node.conn.fio.fill_file(self.file_name, mb,
113 force=self.force_prefill,
114 fio_path=self.fio_path)
115
116 if not is_prefilled:
117 logger.info("Test file on node %s is already prefilled", node.info)
118 elif fill_bw is not None:
119 logger.info("Initial fio fill bw is %s MiBps for %s", fill_bw, node.info)
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200120
koder aka kdanilov70227062016-11-26 23:23:21 +0200121 def install_utils(self, node: IRPCNode) -> None:
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200122 os_info = get_os(node)
koder aka kdanilov6ab4d432015-06-22 00:26:28 +0300123 if self.use_system_fio:
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200124 if os_info.distro != 'ubuntu':
125 logger.error("Only ubuntu supported on test VM")
126 raise StopTestError()
127 node.conn.fio.install('fio', binary='fio')
128 else:
129 node.conn.fio.install('bzip2', binary='bzip2')
koder aka kdanilov70227062016-11-26 23:23:21 +0200130 fio_dir = os.path.dirname(os.path.dirname(wally.__file__)) # type: str
131 fio_dir = os.path.join(os.getcwd(), fio_dir)
132 fio_dir = os.path.join(fio_dir, 'fio_binaries')
133 fname = 'fio_{0.release}_{0.arch}.bz2'.format(os_info)
134 fio_path = os.path.join(fio_dir, fname) # type: str
135
136 if not os.path.exists(fio_path):
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200137 logger.error("No prebuild fio binary available for {0}".format(os_info))
138 raise StopTestError()
koder aka kdanilov70227062016-11-26 23:23:21 +0200139
140 bz_dest = self.join_remote('fio.bz2') # type: str
koder aka kdanilov108ac362017-01-19 20:17:16 +0200141 node.copy_file(fio_path, bz_dest, compress=False)
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200142 node.run("bzip2 --decompress {} ; chmod a+x {}".format(bz_dest, self.join_remote("fio")))
koder aka kdanilov70227062016-11-26 23:23:21 +0200143
koder aka kdanilovf90de852017-01-20 18:12:27 +0200144 def get_expected_runtime(self, job_config: JobConfig) -> int:
koder aka kdanilovf2865172016-12-30 03:35:11 +0200145 return execution_time(cast(FioJobConfig, job_config))
koder aka kdanilov70227062016-11-26 23:23:21 +0200146
koder aka kdanilovf90de852017-01-20 18:12:27 +0200147 def prepare_iteration(self, node: IRPCNode, job: JobConfig) -> None:
koder aka kdanilov108ac362017-01-19 20:17:16 +0200148 node.put_to_file(self.remote_task_file, str(job).encode("utf8"))
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200149
150 # TODO: get a link to substorage as a parameter
koder aka kdanilovf90de852017-01-20 18:12:27 +0200151 def run_iteration(self, node: IRPCNode, job: JobConfig) -> List[TimeSeries]:
koder aka kdanilov108ac362017-01-19 20:17:16 +0200152 exec_time = execution_time(cast(FioJobConfig, job))
153
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200154
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200155 fio_cmd_templ = "cd {exec_folder}; " + \
156 "{fio_path} --output-format=json --output={out_file} --alloc-size=262144 {job_file}"
157
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200158 cmd = fio_cmd_templ.format(exec_folder=self.exec_folder,
159 fio_path=self.fio_path,
160 out_file=self.remote_output_file,
161 job_file=self.remote_task_file)
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200162 must_be_empty = node.run(cmd, timeout=exec_time + max(300, exec_time), check_timeout=1).strip()
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200163
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200164 if must_be_empty:
165 logger.error("Unexpected fio output: %r", must_be_empty)
koder aka kdanilovbc2c8982015-06-13 02:50:43 +0300166
koder aka kdanilovf2865172016-12-30 03:35:11 +0200167 # put fio output into storage
168 fio_out = node.get_file_content(self.remote_output_file)
koder aka kdanilov108ac362017-01-19 20:17:16 +0200169
170 path = DataSource(suite_id=self.suite.storage_id,
171 job_id=job.storage_id,
172 node_id=node.node_id,
173 dev='fio',
174 sensor='stdout',
175 tag='json')
176
177 self.storage.put_extra(fio_out, path)
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200178 node.conn.fs.unlink(self.remote_output_file)
179
180 files = [name for name in node.conn.fs.listdir(self.exec_folder)]
koder aka kdanilov108ac362017-01-19 20:17:16 +0200181 result = []
182 for name, file_path in get_log_files(cast(FioJobConfig, job)):
183 log_files = [fname for fname in files if fname.startswith(file_path)]
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200184 if len(log_files) != 1:
185 logger.error("Found %s files, match log pattern %s(%s) - %s",
koder aka kdanilov108ac362017-01-19 20:17:16 +0200186 len(log_files), file_path, name, ",".join(log_files[10:]))
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200187 raise StopTestError()
188
189 fname = os.path.join(self.exec_folder, log_files[0])
190 raw_result = node.get_file_content(fname) # type: bytes
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200191 node.conn.fs.unlink(fname)
192
193 try:
194 log_data = raw_result.decode("utf8").split("\n")
195 except UnicodeEncodeError:
196 logger.exception("Error during parse %s fio log file - can't decode usint UTF8", name)
197 raise StopTestError()
198
199 parsed = array.array('L' if name == 'lat' else 'Q')
koder aka kdanilovf2865172016-12-30 03:35:11 +0200200 times = array.array('Q')
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200201
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200202 for idx, line in enumerate(log_data):
203 line = line.strip()
204 if line:
205 try:
206 time_ms_s, val_s, _, *rest = line.split(",")
koder aka kdanilovf2865172016-12-30 03:35:11 +0200207 times.append(int(time_ms_s.strip()))
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200208
koder aka kdanilov23e6bdf2016-12-24 02:18:54 +0200209 if name == 'lat':
210 vals = [int(i.strip()) for i in rest]
211
212 if len(vals) != expected_lat_bins:
213 logger.error("Expect {} bins in latency histogram, but found {} at time {}"
214 .format(expected_lat_bins, len(vals), time_ms_s))
215 raise StopTestError()
216
217 parsed.extend(vals)
218 else:
219 parsed.append(int(val_s.strip()))
220 except ValueError:
221 logger.exception("Error during parse %s fio log file in line %s: %r", name, idx, line)
222 raise StopTestError()
koder aka kdanilovffaf48d2016-12-27 02:25:29 +0200223
koder aka kdanilov108ac362017-01-19 20:17:16 +0200224 result.append(TimeSeries(name=name,
225 raw=raw_result,
226 second_axis_size=expected_lat_bins if name == 'lat' else 1,
227 data=parsed,
228 times=times,
229 source=path(sensor=name, tag=None)))
230 return result
koder aka kdanilov7f59d562016-12-26 01:34:23 +0200231
232 def format_for_console(self, data: Any) -> str:
233 raise NotImplementedError()