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