1# Lint as: python2, python3 2# Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import time 7 8from autotest_lib.client.bin import test 9from autotest_lib.client.common_lib import error 10from autotest_lib.client.cros.power import power_suspend 11 12 13# In cases like crosbug.com/p/26289, we want results, but also want 14# to make sure we are suspending quickly enough. Retry with this amount 15# of extra suspend time to make sure we can get some results, even 16# if we throw a warning. 17EXTRA_TIME = 10 18 19 20class power_Resume(test.test): 21 """class for power_Resume test.""" 22 version = 1 23 preserve_srcdir = True 24 25 def initialize(self, 26 suspend_state='', 27 suspend_iterations=None, 28 iteration_delay=0): 29 """ 30 Entry point. 31 32 @param suspend_state: Force to suspend to a specific 33 state ("mem" or "freeze"). If the string is empty, suspend 34 state is left to the default pref on the system. 35 @param suspend_iterations: number of times to attempt suspend. 36 @param iteration_delay: number of seconds to wait between suspend iterations (default: 0). 37 """ 38 self._suspend_iterations = suspend_iterations 39 self._iteration_delay = iteration_delay 40 self._suspender = power_suspend.Suspender( 41 self.resultsdir, 42 throw=True, 43 device_times=True, 44 suspend_state=suspend_state) 45 46 47 def run_once(self, max_devs_returned=10, seconds=0, 48 ignore_kernel_warns=False, measure_arc=False): 49 system_suspends = [] 50 system_resumes = [] 51 while not self._done(): 52 time.sleep(self._iteration_delay) 53 54 suspend_time = 0.0 55 resume_time = 0.0 56 try: 57 (suspend_time, 58 resume_time) = self._suspend_once(max_devs_returned, seconds, 59 ignore_kernel_warns, 60 measure_arc) 61 except error.TestWarn: 62 (suspend_time, resume_time) = self._suspend_once( 63 max_devs_returned, seconds + EXTRA_TIME, 64 ignore_kernel_warns, measure_arc) 65 raise 66 system_suspends.append(suspend_time) 67 system_resumes.append(resume_time) 68 69 self.output_perf_value(description='system_suspend', 70 value=system_suspends, 71 units='sec', 72 higher_is_better=False) 73 self.output_perf_value(description='system_resume', 74 value=system_resumes, 75 units='sec', 76 higher_is_better=False) 77 78 def _done(self): 79 if self._suspend_iterations == None: 80 # At least one iteration. 81 self._suspend_iterations = 1 82 83 self._suspend_iterations -= 1 84 return self._suspend_iterations < 0 85 86 87 def _suspend_once(self, max_devs_returned, seconds, ignore_kernel_warns, 88 measure_arc=False): 89 (results, device_times) = \ 90 self._suspender.suspend(seconds, 91 ignore_kernel_warns=ignore_kernel_warns, 92 measure_arc=measure_arc) 93 94 # return as keyvals the slowest n devices 95 slowest_devs = sorted( 96 device_times, 97 key=device_times.get, 98 reverse=True)[:max_devs_returned] 99 for dev in slowest_devs: 100 results[dev] = device_times[dev] 101 102 self.write_perf_keyval(results) 103 return (results['seconds_system_suspend'], 104 results['seconds_system_resume']) 105 106 def cleanup(self): 107 """ 108 Clean the suspender. 109 """ 110 self._suspender.finalize() 111