def configure_crash_handler(self): """ Configure the crash handler by: * Setting up core size to unlimited * Putting an appropriate crash handler on /proc/sys/kernel/core_pattern * Create files that the crash handler will use to figure which tests are active at a given moment The crash handler will pick up the core file and write it to self.debugdir, and perform analysis on it to generate a report. The program also outputs some results to syslog. If multiple tests are running, an attempt to verify if we still have the old PID on the system process table to determine whether it is a parent of the current test execution. If we can't determine it, the core file and the report file will be copied to all test debug dirs. """ self.pattern_file = '/proc/sys/kernel/core_pattern' try: # Enable core dumps resource.setrlimit(resource.RLIMIT_CORE, (-1, -1)) # Trying to backup core pattern and register our script self.core_pattern_backup = open(self.pattern_file, 'r').read() pattern_file = open(self.pattern_file, 'w') tools_dir = os.path.join(self.autodir, 'tools') crash_handler_path = os.path.join(tools_dir, 'crash_handler.py') pattern_file.write('|' + crash_handler_path + ' %p %t %u %s %h %e') # Writing the files that the crash handler is going to use self.debugdir_tmp_file = ('/tmp/autotest_results_dir.%s' % os.getpid()) utils.open_write_close(self.debugdir_tmp_file, self.debugdir + "\n") except Exception, e: self.crash_handling_enabled = False logging.error('Crash handling system disabled: %s' % e)
def answer_kickstart(self, answer_path): """ Replace KVM_TEST_CDKEY (in the unattended file) with the cdkey provided for this test and replace the KVM_TEST_MEDIUM with the tree url or nfs address provided for this test. @return: Answer file contents """ contents = open(self.unattended_file).read() dummy_cdkey_re = r"\bKVM_TEST_CDKEY\b" if re.search(dummy_cdkey_re, contents): if self.cdkey: contents = re.sub(dummy_cdkey_re, self.cdkey, contents) dummy_medium_re = r"\bKVM_TEST_MEDIUM\b" if self.medium == "cdrom": content = "cdrom" elif self.medium == "url": content = "url --url %s" % self.url elif self.medium == "nfs": content = "nfs --server=%s --dir=%s" % (self.nfs_server, self.nfs_dir) else: raise ValueError("Unexpected installation medium %s" % self.url) contents = re.sub(dummy_medium_re, content, contents) logging.debug("Unattended install contents:") for line in contents.splitlines(): logging.debug(line) utils.open_write_close(answer_path, contents)
def run_iozone_windows(test, params, env): """ Run IOzone for windows on a windows guest: 1) Log into a guest 2) Execute the IOzone test contained in the winutils.iso 3) Get results 4) Postprocess it with the IOzone postprocessing module @param test: kvm test object @param params: Dictionary with the test parameters @param env: Dictionary with test environment. """ vm = env.get_vm(params["main_vm"]) vm.verify_alive() timeout = int(params.get("login_timeout", 360)) session = vm.wait_for_login(timeout=timeout) results_path = os.path.join(test.resultsdir, 'raw_output_%s' % test.iteration) analysisdir = os.path.join(test.resultsdir, 'analysis_%s' % test.iteration) # Run IOzone and record its results c = params.get("iozone_cmd") t = int(params.get("iozone_timeout")) logging.info("Running IOzone command on guest, timeout %ss", t) results = session.cmd_output(cmd=c, timeout=t) utils.open_write_close(results_path, results) # Postprocess the results using the IOzone postprocessing module logging.info("Iteration succeed, postprocessing") a = postprocessing.IOzoneAnalyzer(list_files=[results_path], output_dir=analysisdir) a.analyze() p = postprocessing.IOzonePlotter(results_file=results_path, output_dir=analysisdir) p.plot_all()
def log_after_each_test(self, test): """ Logging hook called after a test finishs. """ test_sysinfodir = self._get_sysinfodir(test.outputdir) # create a symlink in the test sysinfo dir to the current boot reboot_dir = self._get_boot_subdir() assert os.path.exists(reboot_dir) symlink_dest = os.path.join(test_sysinfodir, "reboot_current") os.symlink( # create a relative symlink utils.get_relative_path(reboot_dir, os.path.dirname(symlink_dest)), symlink_dest) # run all the standard logging commands for log in self.test_loggables: log.run(test_sysinfodir) # grab any new data from /var/log/messages self._log_messages(test_sysinfodir) # log some sysinfo data into the test keyval file keyval = self.log_test_keyvals(test_sysinfodir) test.write_test_keyval(keyval) # log any changes to installed packages old_packages = set(self._installed_packages) new_packages = set(package.list_all()) added_path = os.path.join(test_sysinfodir, "added_packages") added_packages = "\n".join(new_packages - old_packages) + "\n" utils.open_write_close(added_path, added_packages) removed_path = os.path.join(test_sysinfodir, "removed_packages") removed_packages = "\n".join(old_packages - new_packages) + "\n" utils.open_write_close(removed_path, removed_packages)
def run_once(self, args=''): vars = 'UB_TMPDIR="%s" UB_RESULTDIR="%s"' % (self.tmpdir, self.resultsdir) os.chdir(self.srcdir) self.report_data = utils.system_output(vars + ' ./Run ' + args) self.results_path = os.path.join(self.resultsdir, 'raw_output_%s' % self.iteration) utils.open_write_close(self.results_path, self.report_data)
def request_devs(self): """ Implement setup process: unbind the PCI device and then bind it to the pci-stub driver. @return: a list of successfully requested devices' PCI IDs. """ base_dir = "/sys/bus/pci" stub_path = os.path.join(base_dir, "drivers/pci-stub") self.pci_ids = self.get_devs(self.devices_requested) logging.debug("The following pci_ids were found: %s", self.pci_ids) requested_pci_ids = [] self.dev_drivers = {} # Setup all devices specified for assignment to guest for pci_id in self.pci_ids: full_id = get_full_pci_id(pci_id) if not full_id: continue drv_path = os.path.join(base_dir, "devices/%s/driver" % full_id) dev_prev_driver= os.path.realpath(os.path.join(drv_path, os.readlink(drv_path))) self.dev_drivers[pci_id] = dev_prev_driver # Judge whether the device driver has been binded to stub if not self.is_binded_to_stub(full_id): logging.debug("Binding device %s to stub", full_id) vendor_id = get_vendor_from_pci_id(pci_id) stub_new_id = os.path.join(stub_path, 'new_id') unbind_dev = os.path.join(drv_path, 'unbind') stub_bind = os.path.join(stub_path, 'bind') info_write_to_files = [(vendor_id, stub_new_id), (full_id, unbind_dev), (full_id, stub_bind)] for content, file in info_write_to_files: try: utils.open_write_close(file, content) except IOError: logging.debug("Failed to write %s to file %s", content, file) continue if not self.is_binded_to_stub(full_id): logging.error("Binding device %s to stub failed", pci_id) continue else: logging.debug("Device %s already binded to stub", pci_id) requested_pci_ids.append(pci_id) self.pci_ids = requested_pci_ids return self.pci_ids
def run_once(self, num_groups=90): """ Run hackbench, store the output in raw output files per iteration and also in the results list attribute. @param num_groups: Number of children processes hackbench will spawn. """ hackbench_bin = os.path.join(self.srcdir, 'hackbench') cmd = '%s %s' % (hackbench_bin, num_groups) raw_output = utils.system_output(cmd, retain_output=True) self.results = raw_output path = os.path.join(self.resultsdir, 'raw_output_%s' % self.iteration) utils.open_write_close(path, raw_output)
def log_per_reboot_data(self): """ Logging hook called whenever a job starts, and again after any reboot. """ logdir = self._get_boot_subdir(next=True) if not os.path.exists(logdir): os.mkdir(logdir) for log in (self.test_loggables | self.boot_loggables): log.run(logdir) # also log any installed packages installed_path = os.path.join(logdir, "installed_packages") installed_packages = "\n".join(package.list_all()) + "\n" utils.open_write_close(installed_path, installed_packages)
def run_once(self): """ Runs a single iteration of the FFSB. """ self.dup_ffsb_profilefl() # Run FFSB using abspath cmd = '%s/ffsb %s/profile.cfg' % (self.srcdir, self.srcdir) logging.info("FFSB command: %s", cmd) self.results_path = os.path.join(self.resultsdir, 'raw_output_%s' % self.iteration) try: self.results = utils.system_output(cmd, retain_output=True) logging.info(self.results) utils.open_write_close(self.results_path, self.results) except error.CmdError, e: self.nfail += 1 logging.error('Failed to execute FFSB : %s', e)
def crash_handler_report(self): """ If core dumps are found on the debugdir after the execution of the test, let the user know. """ if self.crash_handling_enabled: core_dirs = glob.glob('%s/crash.*' % self.debugdir) if core_dirs: logging.warning('Programs crashed during test execution:') for dir in core_dirs: logging.warning('Please verify %s for more info', dir) # Remove the debugdir info file os.unlink(self.debugdir_tmp_file) # Restore the core pattern backup try: utils.open_write_close(self.pattern_file, self.core_pattern_backup) except EnvironmentError: pass
def run_once(self, dir = None, args = None): if not dir: self.dir = self.tmpdir else: self.dir = dir if not args: self.args = '--block=4096 --block=8192 --threads=10 --size=1024 --numruns=2' else: self.args = args os.chdir(self.srcdir) results = utils.system_output('./tiobench.pl --dir %s %s' % (self.dir, self.args)) logging.info(results) results_path = os.path.join(self.resultsdir, 'raw_output_%s' % self.iteration) utils.open_write_close(results_path, results)
def run_unittest_kvmctl(test, params, env): """ This is kvm userspace unit test, use kvm test harness kvmctl load binary test case file to test various functions of the kvm kernel module. The output of all unit tests can be found in the test result dir. @param test: KVM test object. @param params: Dictionary with the test parameters. @param env: Dictionary with test environment. """ case = params.get("case") srcdir = params.get("srcdir", test.srcdir) unit_dir = os.path.join(srcdir, "kvm_userspace", "kvm", "user") os.chdir(unit_dir) cmd = "./kvmctl test/x86/bootstrap test/x86/%s.flat" % case try: results = utils.system_output(cmd) except error.CmdError: raise error.TestFail("Unit test %s failed" % case) result_file = os.path.join(test.resultsdir, case) utils.open_write_close(result_file, results)
def run_once(self, testdir=None, args='', cthon_iterations=1): """ Runs the test, with the appropriate control file. """ os.chdir(self.srcdir) if testdir is None: testdir = self.tmpdir self.results_path = os.path.join(self.resultsdir, 'raw_output_%s' % self.iteration) try: if not args: # run basic test args = "-b -t" self.results = utils.system_output('./runtests -N %s %s %s' % (cthon_iterations, args, testdir)) utils.open_write_close(self.results_path, self.results) except error.CmdError, e: self.nfail += 1 logging.error("Test failed: %s", e)
def run_once(self, dir=None, args=None): """ Runs IOzone with appropriate parameters, record raw results in a per iteration raw output file as well as in the results attribute @param dir: IOzone file generation dir. @param args: Arguments to the iozone program. """ if not dir: dir = self.tmpdir os.chdir(dir) if not args: args = '-a' cmd = os.path.join(self.srcdir, 'src', 'current', 'iozone') self.results = utils.system_output('%s %s' % (cmd, args)) self.auto_mode = ("-a" in args) self.results_path = os.path.join(self.resultsdir, 'raw_output_%s' % self.iteration) self.analysisdir = os.path.join(self.resultsdir, 'analysis_%s' % self.iteration) utils.open_write_close(self.results_path, self.results)
def trace_config(self, path, value): """ Write value to a tracing config file under self.tracing_dir. """ path = os.path.join(self.tracing_dir, path) utils.open_write_close(path, value)
# grab any new data from /var/log/messages self._log_messages(test_sysinfodir) # grab any new data from systemd journal self._log_journal(test_sysinfodir) # log some sysinfo data into the test keyval file keyval = self.log_test_keyvals(test_sysinfodir) test.write_test_keyval(keyval) # log any changes to installed packages old_packages = set(self._installed_packages) new_packages = set(package.list_all()) added_path = os.path.join(test_sysinfodir, "added_packages") added_packages = "\n".join(new_packages - old_packages) + "\n" utils.open_write_close(added_path, added_packages) removed_path = os.path.join(test_sysinfodir, "removed_packages") removed_packages = "\n".join(old_packages - new_packages) + "\n" utils.open_write_close(removed_path, removed_packages) @log.log_and_ignore_errors("pre-test siteration sysinfo error:") def log_before_each_iteration(self, test, iteration=None): """Logging hook called before a test iteration. @param test: A test object. @param iteration: A test iteration. """ if not iteration: iteration = test.iteration logdir = self._get_iteration_subdir(test, iteration) _run_loggables_ignoring_errors(self.before_iteration_loggables, logdir)
def suspend(self, duration=10, ignore_kernel_warns=False, measure_arc=False): """ Do a single suspend for 'duration' seconds. Estimates the amount of time it takes to suspend for a board (see _SUSPEND_DELAY), so the actual RTC wakeup delay will be longer. Returns None on errors, or raises the exception when _throw is set. Returns a dict of general measurements, or a tuple (general_measurements, individual_device_times) when _device_times is set. @param duration: time in seconds to do a suspend prior to waking. @param ignore_kernel_warns: Ignore kernel errors. Defaults to false. """ if power_utils.get_sleep_state() == 'freeze': self._s0ix_residency_stats = power_status.S0ixResidencyStats() try: iteration = len(self.failures) + len(self.successes) + 1 # Retry suspend in case we hit a known (whitelisted) bug for _ in xrange(10): # Clear powerd_suspend RTC timestamp, to avoid stale results. utils.open_write_close(self.HWCLOCK_FILE, '') self._reset_logs() utils.system('sync') board_delay = self._SUSPEND_DELAY.get( self._get_board(), self._DEFAULT_SUSPEND_DELAY) # Clear the ARC logcat to make parsing easier. if measure_arc: command = 'android-sh -c "logcat -c"' utils.system(command, ignore_status=False) try: alarm = self._suspend(duration + board_delay) except sys_power.SpuriousWakeupError: # might be another error, we check for it ourselves below alarm = self._ALARM_FORCE_EARLY_WAKEUP if os.path.exists('/sys/firmware/log'): for msg in re.findall(r'^.*ERROR.*$', utils.read_file('/sys/firmware/log'), re.M): for board, pattern in sys_power.FirmwareError.WHITELIST: if (re.search(board, utils.get_board()) and re.search(pattern, msg)): logging.info('Whitelisted FW error: ' + msg) break else: firmware_log = os.path.join( self._logdir, 'firmware.log.' + str(iteration)) shutil.copy('/sys/firmware/log', firmware_log) logging.info('Saved firmware log: ' + firmware_log) raise sys_power.FirmwareError(msg.strip('\r\n ')) self._update_logs() if not self._check_for_errors(ignore_kernel_warns): hwclock_ts = self._hwclock_ts(alarm) if hwclock_ts: break else: raise error.TestWarn('Ten tries failed due to whitelisted bug') # calculate general measurements start_resume = self._ts('start_resume_time') kernel_down = (self._ts('end_suspend_time') - self._ts('start_suspend_time')) kernel_up = self._ts('end_resume_time') - start_resume devices_up = self._device_resume_time() total_up = hwclock_ts - alarm firmware_up = self._firmware_resume_time() board_up = total_up - kernel_up - firmware_up try: cpu_up = self._ts('cpu_ready_time', 0) - start_resume except error.TestError: # can be missing on non-SMP machines cpu_up = None if total_up > self._MAX_RESUME_TIME: raise error.TestError( 'Sanity check failed: missed RTC wakeup.') logging.info( 'Success(%d): %g down, %g up, %g board, %g firmware, ' '%g kernel, %g cpu, %g devices', iteration, kernel_down, total_up, board_up, firmware_up, kernel_up, cpu_up, devices_up) if hasattr(self, '_s0ix_residency_stats'): s0ix_residency_secs = \ self._s0ix_residency_stats.\ get_accumulated_residency_secs() if not s0ix_residency_secs: raise sys_power.S0ixResidencyNotChanged( 'S0ix residency did not change.') logging.info('S0ix residency : %d secs.', s0ix_residency_secs) successful_suspend = { 'seconds_system_suspend': kernel_down, 'seconds_system_resume': total_up, 'seconds_system_resume_firmware': firmware_up + board_up, 'seconds_system_resume_firmware_cpu': firmware_up, 'seconds_system_resume_firmware_ec': board_up, 'seconds_system_resume_kernel': kernel_up, 'seconds_system_resume_kernel_cpu': cpu_up, 'seconds_system_resume_kernel_dev': devices_up, } if measure_arc: successful_suspend['seconds_system_resume_arc'] = \ self._arc_resume_ts() - start_resume self.successes.append(successful_suspend) if hasattr(self, 'device_times'): self._individual_device_times(start_resume) return (self.successes[-1], self.device_times[-1]) else: return self.successes[-1] except sys_power.SuspendFailure as ex: message = '%s(%d): %s' % (type(ex).__name__, iteration, ex) logging.error(message) self.failures.append(ex) if self._throw: if type(ex).__name__ in ['KernelError', 'SuspendTimeout']: raise error.TestWarn(message) else: raise error.TestFail(message) return None
def reset(self): """Resets HCD by unbinding and binding driver.""" utils.open_write_close( os.path.join(self._hcd_path, 'unbind'), self._hcd_id) utils.open_write_close( os.path.join(self._hcd_path, 'bind'), self._hcd_id)
def setup_answer_file(self, filename, contents): utils.open_write_close(os.path.join(self.mount, filename), contents)
import os from autotest_lib.client.bin import utils from autotest_lib.client.common_lib import error def run_unittest_kvmctl(test, params, env): """ This is kvm userspace unit test, use kvm test harness kvmctl load binary test case file to test various functions of the kvm kernel module. The output of all unit tests can be found in the test result dir. @param test: KVM test object. @param params: Dictionary with the test parameters. @param env: Dictionary with test environment. """ case = params.get("case") srcdir = params.get("srcdir", test.srcdir) unit_dir = os.path.join(srcdir, "kvm_userspace", "kvm", "user") os.chdir(unit_dir) cmd = "./kvmctl test/x86/bootstrap test/x86/%s.flat" % case try: results = utils.system_output(cmd) except error.CmdError, e: raise error.TestFail("Unit test %s failed" % case) result_file = os.path.join(test.resultsdir, case) utils.open_write_close(result_file, results)
class base_sysinfo(object): def __init__(self, job_resultsdir): self.sysinfodir = self._get_sysinfodir(job_resultsdir) # pull in the post-test logs to collect self.test_loggables = set() for cmd in _DEFAULT_COMMANDS_TO_LOG_PER_TEST: self.test_loggables.add(command(cmd)) for filename in _DEFAULT_FILES_TO_LOG_PER_TEST: self.test_loggables.add(logfile(filename)) # pull in the EXTRA post-boot logs to collect self.boot_loggables = set() for cmd in _DEFAULT_COMMANDS_TO_LOG_PER_BOOT: self.boot_loggables.add(command(cmd)) for filename in _DEFAULT_FILES_TO_LOG_PER_BOOT: self.boot_loggables.add(logfile(filename)) # pull in the pre test iteration logs to collect self.before_iteration_loggables = set() for cmd in _DEFAULT_COMMANDS_TO_LOG_BEFORE_ITERATION: self.before_iteration_loggables.add( command(cmd, logf=cmd.replace(" ", "_") + '.before')) for fname in _DEFAULT_FILES_TO_LOG_BEFORE_ITERATION: self.before_iteration_loggables.add( logfile(fname, logf=os.path.basename(fname) + '.before')) # pull in the post test iteration logs to collect self.after_iteration_loggables = set() for cmd in _DEFAULT_COMMANDS_TO_LOG_AFTER_ITERATION: self.after_iteration_loggables.add( command(cmd, logf=cmd.replace(" ", "_") + '.after')) for fname in _DEFAULT_FILES_TO_LOG_AFTER_ITERATION: self.after_iteration_loggables.add( logfile(fname, logf=os.path.basename(fname) + '.after')) # add in a couple of extra files and commands we want to grab self.test_loggables.add(command("df -mP", logf="df")) # We compress the dmesg because it can get large when kernels are # configured with a large buffer and some tests trigger OOMs or # other large "spam" that fill it up... self.test_loggables.add( command("dmesg -c", logf="dmesg", compress_log=True)) self.boot_loggables.add(logfile("/proc/cmdline", log_in_keyval=True)) # log /proc/mounts but with custom filename since we already # log the output of the "mount" command as the filename "mount" self.boot_loggables.add(logfile('/proc/mounts', logf='proc_mounts')) self.boot_loggables.add( command("uname -a", logf="uname", log_in_keyval=True)) def serialize(self): return {"boot": self.boot_loggables, "test": self.test_loggables} def deserialize(self, serialized): self.boot_loggables = serialized["boot"] self.test_loggables = serialized["test"] @staticmethod def _get_sysinfodir(resultsdir): sysinfodir = os.path.join(resultsdir, "sysinfo") if not os.path.exists(sysinfodir): os.makedirs(sysinfodir) return sysinfodir def _get_reboot_count(self): if not glob.glob(os.path.join(self.sysinfodir, "*")): return -1 else: return len(glob.glob(os.path.join(self.sysinfodir, "boot.*"))) def _get_boot_subdir(self, next=False): reboot_count = self._get_reboot_count() if next: reboot_count += 1 if reboot_count < 1: return self.sysinfodir else: boot_dir = "boot.%d" % (reboot_count - 1) return os.path.join(self.sysinfodir, boot_dir) def _get_iteration_subdir(self, test, iteration): iter_dir = "iteration.%d" % iteration logdir = os.path.join(self._get_sysinfodir(test.outputdir), iter_dir) if not os.path.exists(logdir): os.mkdir(logdir) return logdir @log.log_and_ignore_errors("post-reboot sysinfo error:") def log_per_reboot_data(self): """ Logging hook called whenever a job starts, and again after any reboot. """ logdir = self._get_boot_subdir(next=True) if not os.path.exists(logdir): os.mkdir(logdir) for log in (self.test_loggables | self.boot_loggables): log.run(logdir) # also log any installed packages installed_path = os.path.join(logdir, "installed_packages") installed_packages = "\n".join(package.list_all()) + "\n" utils.open_write_close(installed_path, installed_packages) @log.log_and_ignore_errors("pre-test sysinfo error:") def log_before_each_test(self, test): """ Logging hook called before a test starts. """ self._installed_packages = package.list_all() if os.path.exists("/var/log/messages"): stat = os.stat("/var/log/messages") self._messages_size = stat.st_size self._messages_inode = stat.st_ino @log.log_and_ignore_errors("post-test sysinfo error:") def log_after_each_test(self, test): """ Logging hook called after a test finishs. """ test_sysinfodir = self._get_sysinfodir(test.outputdir) # create a symlink in the test sysinfo dir to the current boot reboot_dir = self._get_boot_subdir() assert os.path.exists(reboot_dir) symlink_dest = os.path.join(test_sysinfodir, "reboot_current") symlink_src = utils.get_relative_path(reboot_dir, os.path.dirname(symlink_dest)) try: os.symlink(symlink_src, symlink_dest) except Exception, e: raise Exception, '%s: whilst linking %s to %s' % (e, symlink_src, symlink_dest) # run all the standard logging commands for log in self.test_loggables: log.run(test_sysinfodir) # grab any new data from /var/log/messages self._log_messages(test_sysinfodir) # log some sysinfo data into the test keyval file keyval = self.log_test_keyvals(test_sysinfodir) test.write_test_keyval(keyval) # log any changes to installed packages old_packages = set(self._installed_packages) new_packages = set(package.list_all()) added_path = os.path.join(test_sysinfodir, "added_packages") added_packages = "\n".join(new_packages - old_packages) + "\n" utils.open_write_close(added_path, added_packages) removed_path = os.path.join(test_sysinfodir, "removed_packages") removed_packages = "\n".join(old_packages - new_packages) + "\n" utils.open_write_close(removed_path, removed_packages)
def cleanup(self): # Restore the original setting if system has CPUQuiet feature if os.path.exists(SYSFS_CPUQUIET_ENABLE): utils.open_write_close(SYSFS_CPUQUIET_ENABLE, self.is_cpuquiet_enabled)
def _set_crash_test_in_progress(self, in_progress): if in_progress: utils.open_write_close(self._CRASH_TEST_IN_PROGRESS, 'in-progress') logging.info('Created ' + self._CRASH_TEST_IN_PROGRESS) else: utils.system('rm -f "%s"' % (self._CRASH_TEST_IN_PROGRESS))
def cleanup(self): # Restore old console_suspend module param logging.info('restoring value for console_suspend: %s', self.old_console_suspend) utils.open_write_close(SYSFS_CONSOLE_SUSPEND, self.old_console_suspend)
def __init__(self, logdir, method=sys_power.do_suspend, throw=False, device_times=False, suspend_state=''): """ Prepare environment for suspending. @param suspend_state: Suspend state to enter into. It can be 'mem' or 'freeze' or an empty string. If the suspend state is an empty string, system suspends to the default pref. """ self.disconnect_3G_time = 0 self.successes = [] self.failures = [] self._logdir = logdir self._suspend = method self._throw = throw self._reset_pm_print_times = False self._restart_tlsdated = False self._log_file = None self._suspend_state = suspend_state if device_times: self.device_times = [] # stop tlsdated, make sure we/hwclock have /dev/rtc for ourselves if utils.system_output('initctl status tlsdated').find('start') != -1: utils.system('initctl stop tlsdated') self._restart_tlsdated = True # give process's file descriptors time to asynchronously tear down time.sleep(0.1) # prime powerd_suspend RTC timestamp saving and make sure hwclock works utils.open_write_close(self.HWCLOCK_FILE, '') hwclock_output = utils.system_output('hwclock -r --debug --utc', ignore_status=True) if not re.search('Using.*/dev interface to.*clock', hwclock_output): raise error.TestError('hwclock cannot find rtc: ' + hwclock_output) # activate device suspend timing debug output if hasattr(self, 'device_times'): if not int(utils.read_one_line('/sys/power/pm_print_times')): self._set_pm_print_times(True) self._reset_pm_print_times = True # Shut down 3G to remove its variability from suspend time measurements flim = flimflam.FlimFlam() service = flim.FindCellularService(0) if service: logging.info('Found 3G interface, disconnecting.') start_time = time.time() (success, status) = flim.DisconnectService(service=service, wait_timeout=60) if success: logging.info('3G disconnected successfully.') self.disconnect_3G_time = time.time() - start_time else: logging.error('Could not disconnect: %s.', status) self.disconnect_3G_time = -1 self._configure_suspend_state()