def get_trace_statistics(trace_path: str, dev_path: str = "", shortcut: bool = False) -> list: """ Get statistics of particular trace :param trace_path: trace path :param dev_path: path of device which should be retrieved :param shortcut: Use shorter command :type trace_path: str :type shortcut: bool :return: If @dev_path specified - trace statistics, list of trace stats otherwise :raises Exception: if statistics are invalid """ command = 'iotrace' + (' -P' if shortcut else ' --trace-parser') command += ' -S' if shortcut else ' --statistics' command += (' -p ' if shortcut else ' --path ') + f'{trace_path}' command += (' -f ' if shortcut else ' --format ') + 'json' output = TestRun.executor.run(command) if output.stdout == "": raise CmdException("Invalid IO statistics", output) if dev_path == "": return parse_json(output.stdout)[0]['statistics'] expected_device_name = dev_path.split('/dev/')[1].replace("/","") for trace_stat in parse_json(output.stdout)[0]['statistics']: traced_device = trace_stat["desc"]["device"]["name"] if traced_device == expected_device_name: return trace_stat raise CmdException(f"No trace stats for device {dev_path}", output)
def install_opencas(): TestRun.LOGGER.info("Copying Open CAS repository to DUT") TestRun.executor.rsync(f"{TestRun.usr.repo_dir}/", f"{TestRun.usr.working_dir}/", exclude_list=["test/functional/results/"], delete=True) TestRun.LOGGER.info("Building Open CAS") output = TestRun.executor.run(f"cd {TestRun.usr.working_dir} && " "./configure && " "make -j") if output.exit_code != 0: raise CmdException("Make command executed with nonzero status", output) TestRun.LOGGER.info("Installing Open CAS") output = TestRun.executor.run(f"cd {TestRun.usr.working_dir} && " f"make install") if output.exit_code != 0: raise CmdException("Error while installing Open CAS", output) TestRun.LOGGER.info("Check if casadm is properly installed.") output = TestRun.executor.run("casadm -V") if output.exit_code != 0: raise CmdException("'casadm -V' command returned an error", output) else: TestRun.LOGGER.info(output.stdout)
def get_lba_histogram(trace_path: str, bucket_size: Size = Size(0, Unit.Byte), subrange_start: int = 0, subrange_end: int = 0, shortcut: bool = False) -> list: """ Get lba histogram of given trace path :param trace_path: trace path :param bucket_size: bucket size :param subrange_start: subrange start :param subrange_end: subrange end :param shortcut: Use shorter command :type trace_path: str :type bucket_size: Size :type subrange_start: int :type subrange_end: int :type shortcut: bool :return: LBA histogram :raises Exception: if iotrace command or histogram is invalid """ bucket_size_range = range(1, 4294967296) subrange_range = range(1, 9223372036854775808) if subrange_start and subrange_end: if subrange_start > subrange_end: subrange_start, subrange_end = subrange_end, subrange_start command = 'iotrace' + (' -P' if shortcut else ' --trace-parser') command += ' -B' if shortcut else ' --lba-histogram' command += (' -p ' if shortcut else ' --path ') + f'{trace_path}' if bucket_size is not None: if int(bucket_size.get_value(Unit.Byte)) not in bucket_size_range: raise CmdException( f"Given size is out of range {bucket_size_range}.") command += ' -b ' if shortcut else ' --bucket-size ' command += f'{int(bucket_size.get_value(Unit.Byte))}' if subrange_start is not None: if subrange_start not in subrange_range: raise CmdException( f"Given start position is out of range {subrange_range}.") command += ' -s ' if shortcut else ' --subrange-start ' command += f'{subrange_start}' if subrange_end is not None: if subrange_end not in subrange_range: raise CmdException( f"Given end position is out of range {subrange_range}.") command += ' -e ' if shortcut else ' --subrange-end ' command += f'{subrange_end}' command += (' -f ' if shortcut else ' --format ') + 'json' output = TestRun.executor.run(command) if output.stdout == "": raise CmdException("Invalid histogram", output) return parse_json(output.stdout)
def uninstall_opencas(): TestRun.LOGGER.info("Uninstalling Open CAS") output = TestRun.executor.run("casadm -V") if output.exit_code != 0: raise CmdException("Open CAS is not properly installed", output) else: TestRun.executor.run(f"cd {TestRun.usr.working_dir} && " f"make uninstall") if output.exit_code != 0: raise CmdException("There was an error during uninstall process", output)
def install_opencas(): TestRun.LOGGER.info("Installing Open CAS") output = TestRun.executor.run(f"cd {TestRun.usr.working_dir} && " f"make install") if output.exit_code != 0: raise CmdException("Error while installing Open CAS", output) TestRun.LOGGER.info("Check if casadm is properly installed.") output = TestRun.executor.run("casadm -V") if output.exit_code != 0: raise CmdException("'casadm -V' command returned an error", output) else: TestRun.LOGGER.info(output.stdout)
def set_param_cleaning_acp(cache_id: int, wake_up: int = None, flush_max_buffers: int = None): output = TestRun.executor.run( set_param_cleaning_acp_cmd(cache_id=str(cache_id), wake_up=str(wake_up), flush_max_buffers=str(flush_max_buffers))) if output.exit_code != 0: raise CmdException("Error while setting acp cleaning policy parameters.", output) return output
def get_trace_events(trace_path: str, raw: bool = False, shortcut: bool = False) -> list: """ Get all trace events of given trace path :param trace_path: trace path :param raw: output without processing :param shortcut: Use shorter command :type trace_path: str :type raw: bool :type shortcut: bool :return: trace events :raises Exception: if traces are invalid """ command = 'iotrace' + (' -P' if shortcut else ' --trace-parser') command += ' -P' if shortcut else ' --io' command += (' -p ' if shortcut else ' --path ') + f'{trace_path}' command += (' -f ' if shortcut else ' --format ') + 'json' if raw: command += ' -r ' if shortcut else ' --raw ' output = TestRun.executor.run(command) if output.stdout == "": raise CmdException("Invalid traces", output) return parse_json(output.stdout)
def _clean_opencas_repo(): TestRun.LOGGER.info("Cleaning Open CAS repo") output = TestRun.executor.run( f"cd {TestRun.usr.working_dir} && " "make distclean") if output.exit_code != 0: raise CmdException("make distclean command executed with nonzero status", output)
def remove_detached(core_device: Device, shortcut: bool = False): output = TestRun.executor.run( remove_detached_cmd(core_device=core_device.system_path, shortcut=shortcut)) if output.exit_code != 0: raise CmdException("Failed to remove detached core.", output) return output
def print_version(output_format: OutputFormat = None, shortcut: bool = False): _output_format = None if output_format is None else output_format.name output = TestRun.executor.run( version_cmd(output_format=_output_format, shortcut=shortcut)) if output.exit_code != 0: raise CmdException("Failed to print version.", output) return output
def list_caches(output_format: OutputFormat = None, shortcut: bool = False): _output_format = None if output_format is None else output_format.name output = TestRun.executor.run( list_cmd(output_format=_output_format, shortcut=shortcut)) if output.exit_code != 0: raise CmdException("Failed to list caches.", output) return output
def build_opencas(): TestRun.LOGGER.info("Building Open CAS") output = TestRun.executor.run(f"cd {TestRun.usr.working_dir} && " "./configure && " "make -j") if output.exit_code != 0: raise CmdException("Make command executed with nonzero status", output)
def reset_counters(cache_id: int, core_id: int = None, shortcut: bool = False): _core_id = None if core_id is None else str(core_id) output = TestRun.executor.run( reset_counters_cmd(cache_id=str(cache_id), core_id=_core_id, shortcut=shortcut)) if output.exit_code != 0: raise CmdException("Failed to reset counters.", output) return output
def print_statistics(cache_id: int, core_id: int = None, per_io_class: bool = False, io_class_id: int = None, filter: List[StatsFilter] = None, output_format: OutputFormat = None, shortcut: bool = False): _output_format = None if output_format is None else output_format.name _core_id = None if core_id is None else str(core_id) _io_class_id = None if io_class_id is None else str(io_class_id) if filter is None: _filter = filter else: names = (x.name for x in filter) _filter = ",".join(names) output = TestRun.executor.run( print_statistics_cmd(cache_id=str(cache_id), core_id=_core_id, per_io_class=per_io_class, io_class_id=_io_class_id, filter=_filter, output_format=_output_format, shortcut=shortcut)) if output.exit_code != 0: raise CmdException("Printing statistics failed.", output) return output
def load_cache(device: Device, shortcut: bool = False): output = TestRun.executor.run( load_cmd(cache_dev=device.system_path, shortcut=shortcut)) if output.exit_code != 0: raise CmdException("Failed to load cache.", output) return Cache(device)
def start_cache(cache_dev: Device, cache_mode: CacheMode = None, cache_line_size: CacheLineSize = None, cache_id: int = None, force: bool = False, load: bool = False, shortcut: bool = False, kernel_params: KernelParameters = KernelParameters()): if kernel_params != KernelParameters.read_current_settings(): reload_kernel_module("cas_cache", kernel_params.get_parameter_dictionary()) _cache_line_size = None if cache_line_size is None else str( int(cache_line_size.value.get_value(Unit.KibiByte))) _cache_id = None if cache_id is None else str(cache_id) _cache_mode = None if cache_mode is None else cache_mode.name.lower() output = TestRun.executor.run( start_cmd(cache_dev=cache_dev.path, cache_mode=_cache_mode, cache_line_size=_cache_line_size, cache_id=_cache_id, force=force, load=load, shortcut=shortcut)) if output.exit_code != 0: raise CmdException("Failed to start cache.", output) return Cache(cache_dev)
def detach_core(cache_id: int, core_id: int): output = TestRun.executor.run( script_detach_core_cmd(str(cache_id), str(core_id))) if output.exit_code != 0: raise CmdException("Failed to execute detach core script command.", output) return output
def try_add(core_device: Device, cache_id: int, core_id: int = None): output = TestRun.executor.run( script_try_add_cmd(str(cache_id), core_device.path, str(core_id) if core_id is not None else None)) if output.exit_code != 0: raise CmdException("Failed to execute try add script command.", output) return Core(core_device.path, cache_id)
def get_param_cleaning(cache_id: int, output_format: OutputFormat = None, shortcut: bool = False): _output_format = None if output_format is None else output_format.name output = TestRun.executor.run( get_param_cleaning_cmd(cache_id=str(cache_id), output_format=_output_format, shortcut=shortcut)) if output.exit_code != 0: raise CmdException("Getting cleaning policy params failed.", output) return output
def add_core(cache: Cache, core_dev: Device, core_id: int = None, shortcut: bool = False): _core_id = None if core_id is None else str(id) output = TestRun.executor.run( add_core_cmd(cache_id=str(cache.cache_id), core_dev=core_dev.system_path, core_id=_core_id, shortcut=shortcut)) if output.exit_code != 0: raise CmdException("Failed to add core.", output) return Core(core_dev.system_path, cache.cache_id)
def stop_all_caches(): if "No caches running" in list_caches().stdout: return TestRun.LOGGER.info("Stop all caches") casctl_stop() output = list_caches() if "No caches running" not in output.stdout: raise CmdException("Error while stopping caches.", output)
def get_io_stats(device_id): stats_output = TestRun.executor.run_expect_success( f"cat /proc/diskstats | grep '{device_id} '") if not stats_output.stdout.strip(): raise CmdException( "Failed to get statistics for device " + device_id, stats_output) return IoStats.parse(stats_line=stats_output.stdout.splitlines()[0])
def list_io_classes(cache_id: int, output_format: OutputFormat, shortcut: bool = False): _output_format = None if output_format is None else output_format.name output = TestRun.executor.run( list_io_classes_cmd(cache_id=str(cache_id), output_format=_output_format, shortcut=shortcut)) if output.exit_code != 0: raise CmdException("List IO class command failed.", output) return output
def load_io_classes(cache_id: int, file: str, shortcut: bool = False): output = TestRun.executor.run( load_io_classes_cmd(cache_id=str(cache_id), file=file, shortcut=shortcut)) if output.exit_code != 0: raise CmdException("Load IO class command failed.", output) return output
def execute_unplug_command(self): if TestRun.executor.run( f"echo 1 > /sys/block/{self.device_name}/device/remove" ).exit_code != 0: output = TestRun.executor.run( f"echo 1 > /sys/block/{self.device_name}/device/device/remove") if output.exit_code != 0: raise CmdException(f"Failed to unplug PCI disk using sysfs.", output)
def run_expect_fail(self, command, timeout: timedelta = timedelta(minutes=30)): output = self.run(command) if output.exit_code == 0: raise CmdException( f"Command '{command}' executed properly but error was expected.", output) return output
def set_param_cleaning_alru(cache_id: int, wake_up: int = None, staleness_time: int = None, flush_max_buffers: int = None, activity_threshold: int = None): output = TestRun.executor.run( set_param_cleaning_alru_cmd( cache_id=str(cache_id), wake_up=str(wake_up), staleness_time=str(staleness_time), flush_max_buffers=str(flush_max_buffers), activity_threshold=str(activity_threshold))) if output.exit_code != 0: raise CmdException("Error while setting alru cleaning policy parameters.", output) return output
def run_as_other_user(command, user: str, sudo: bool = False): prefix = f'sudo -u {user}' if sudo: command = 'sudo ' + command command = f'{prefix} {command}' output = TestRun.executor.run(command) if output.exit_code != 0 or output.stderr is not "": raise CmdException("Must be run as root.", output) return output
def stop_all_caches(): if "No caches running" in list_caches().stdout: return TestRun.LOGGER.info("Stop all caches") stop_output = casctl_stop() caches_output = list_caches() if "No caches running" not in caches_output.stdout: raise CmdException(f"Error while stopping caches. " f"Listing caches: {caches_output}", stop_output)
def flush(cache_id: int, core_id: int = None, shortcut: bool = False): if core_id is None: command = flush_cache_cmd(cache_id=str(cache_id), shortcut=shortcut) else: command = flush_core_cmd(cache_id=str(cache_id), core_id=str(core_id), shortcut=shortcut) output = TestRun.executor.run(command) if output.exit_code != 0: raise CmdException("Flushing failed.", output) return output