1# Copyright (c) 2012 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 5"""Provides utility methods for controlling powerd in ChromiumOS.""" 6 7import errno 8import fcntl 9import logging 10import multiprocessing 11import os 12import time 13 14import common 15from autotest_lib.client.cros import rtc 16from autotest_lib.client.cros import upstart 17 18SYSFS_POWER_STATE = '/sys/power/state' 19SYSFS_WAKEUP_COUNT = '/sys/power/wakeup_count' 20 21PAUSE_ETHERNET_HOOK_FILE = '/run/autotest_pause_ethernet_hook' 22pause_ethernet_file = None 23 24 25class SuspendFailure(Exception): 26 """Base class for a failure during a single suspend/resume cycle.""" 27 pass 28 29 30class SuspendTimeout(SuspendFailure): 31 """Suspend took too long, got wakeup event (RTC tick) before it was done.""" 32 pass 33 34 35class KernelError(SuspendFailure): 36 """Kernel problem encountered during suspend/resume. 37 38 Whitelist is an array of a 2-entry tuples consisting of (source regexp, text 39 regexp). For example, kernel output might look like, 40 41 [ 597.079950] WARNING: CPU: 0 PID: 21415 at \ 42 <path>/v3.18/drivers/gpu/drm/i915/intel_pm.c:3687 \ 43 skl_update_other_pipe_wm+0x136/0x18a() 44 [ 597.079962] WARN_ON(!wm_changed) 45 46 source regexp should match first line above while text regexp can match 47 up to 2 lines below the source. Note timestamps are stripped prior to 48 comparing regexp. 49 """ 50 WHITELIST = [ 51 # crosbug.com/37594: debug tracing clock desync we don't care about 52 (r'kernel/trace/ring_buffer.c:\d+ rb_reserve_next_event', 53 r'Delta way too big!'), 54 # TODO(crosbug.com/p/52008): Remove from whitelist once watermark 55 # implementation has landed. 56 (r'v3.18/\S+/intel_pm.c:\d+ skl_update_other_pipe_wm', 57 r'WARN_ON\(\!wm_changed\)') 58 ] 59 60 61class FirmwareError(SuspendFailure): 62 """String 'ERROR' found in firmware log after resume.""" 63 WHITELIST = [ 64 # crosbug.com/36762: no one knows, but it has always been there 65 ('^stumpy', r'PNP: 002e\.4 70 irq size: 0x0000000001 not assigned'), 66 # crbug.com/221538: no one knows what ME errors mean anyway 67 ('^parrot', r'ME failed to respond'), 68 # b/64684441: eve SKU without eMMC 69 ('^eve', r'Card did not respond to voltage select!'), 70 ] 71 72 73class SpuriousWakeupError(SuspendFailure): 74 """Received spurious wakeup while suspending or woke before schedule.""" 75 S3_WHITELIST = [ # (<board>, <eventlog wake source>, <syslog wake source>) 76 # crbug.com/220014: spurious trackpad IRQs 77 ('^link', 'Wake Source | GPIO | 12', ''), 78 # crbug.com/345327: unknown, probably same as crbug.com/290923 79 ('^x86-alex', '', ''), # alex can report neither, blanket ignore 80 # crbug.com/355106: unknown, possibly related to crbug.com/290923 81 ('^lumpy|^parrot', '', 'PM1_STS: WAK PWRBTN'), 82 ] 83 S0_WHITELIST = [ # (<board>, <kernel wake source>) 84 # crbug.com/290923: spurious keyboard IRQ, believed to be from Servo 85 ('^x86-alex|^lumpy|^parrot|^butterfly', 'serio0'), 86 # crosbug.com/p/46140: battery event caused by MKBP 87 ('^elm|^oak', 'spi32766.0'), 88 ] 89 90 91class MemoryError(SuspendFailure): 92 """memory_suspend_test found memory corruption.""" 93 pass 94 95 96class SuspendNotAllowed(SuspendFailure): 97 """Suspend was not allowed to be performed.""" 98 pass 99 100 101class S0ixResidencyNotChanged(SuspendFailure): 102 """power_SuspendStress test found CPU/SoC is unable to idle properly 103 when suspended to S0ix. """ 104 pass 105 106 107def prepare_wakeup(seconds): 108 """Prepare the device to wake up from an upcoming suspend. 109 110 @param seconds: The number of seconds to allow the device to suspend. 111 """ 112 # May cause DUT not wake from sleep if the suspend time is 1 second. 113 # It happens when the current clock (floating point) is close to the 114 # next integer, as the RTC sysfs interface only accepts integers. 115 # Make sure it is larger than or equal to 2. 116 assert seconds >= 2 117 wakeup_count = read_wakeup_count() 118 estimated_alarm = int(rtc.get_seconds() + seconds) 119 logging.debug('Suspend for %d seconds, estimated wakealarm = %d', 120 seconds, estimated_alarm) 121 return (estimated_alarm, wakeup_count) 122 123 124def check_wakeup(estimated_alarm): 125 """Verify that the device did not wakeup early. 126 127 @param estimated_alarm: The lower bound time at which the device was 128 expected to wake up. 129 """ 130 now = rtc.get_seconds() 131 if now < estimated_alarm: 132 logging.error('Woke up early at %d', now) 133 raise SpuriousWakeupError('Woke from suspend early') 134 135 136def pause_check_network_hook(): 137 """Stop check_ethernet.hook from running. 138 139 Lock will be held until test exits, or resume_check_network_hook() is 140 called. check_ethernet.hook may also "break" the lock if it judges you've 141 held it too long. 142 143 Can be called multiple times, to refresh the lock. 144 """ 145 # If this function is called multiple times, we intentionally re-assign 146 # this global, which closes out the old lock and grabs it anew. 147 # We intentionally "touch" the file to update its mtime, so we can judge 148 # how long locks are held. 149 global pause_ethernet_file 150 pause_ethernet_file = open(PAUSE_ETHERNET_HOOK_FILE, 'w+') 151 try: 152 # This is a blocking call unless an error occurs. 153 fcntl.flock(pause_ethernet_file, fcntl.LOCK_EX) 154 except IOError: 155 pass 156 157 158def resume_check_network_hook(): 159 """Resume check_ethernet.hook. 160 161 Must call pause_check_network_hook() at least once before calling this. 162 """ 163 global pause_ethernet_file 164 # Closing the file descriptor releases the lock. 165 pause_ethernet_file.close() 166 pause_ethernet_file = None 167 168 169def do_suspend(wakeup_timeout, delay_seconds=0): 170 """Suspend using the power manager with a wakeup timeout. 171 172 Wait for |delay_seconds|, suspend the system(S3/S0iX) using 173 powerd_dbus_suspend program. powerd_dbus_suspend first sets a wakeup alarm 174 on the dut for |current_time + wakeup_timeout|. Thus the device tries to 175 resume at |current_time + wakeup_timeout| irrespective of when it suspended. 176 This means that RTC can trigger an early resume and prevent suspend. 177 178 Function will block until suspend/resume has completed or failed. 179 Returns the wake alarm time from the RTC as epoch. 180 181 @param wakeup_timeout: time from now after which the device has to. 182 @param delay_seconds: Number of seconds wait before suspending the DUT. 183 184 """ 185 pause_check_network_hook() 186 estimated_alarm, wakeup_count = prepare_wakeup(wakeup_timeout) 187 upstart.ensure_running('powerd') 188 command = ('/usr/bin/powerd_dbus_suspend --delay=%d --timeout=30 ' 189 '--wakeup_count=%d --wakeup_timeout=%d' % 190 (delay_seconds, wakeup_count, wakeup_timeout)) 191 logging.info("Running '%s'", command) 192 os.system(command) 193 check_wakeup(estimated_alarm) 194 return estimated_alarm 195 196 197def suspend_for(time_in_suspend, delay_seconds=0): 198 """Suspend using the power manager and spend |time_in_suspend| in suspend. 199 200 Wait for |delay_seconds|, suspend the system(S3/S0iX) using 201 powerd_dbus_suspend program. power manager sets a wakeup alarm on the dut 202 for |time_in_suspend| just before suspending 203 (writing power state to /sys/power/state). Thus the device spends most of 204 |time_in_suspend| in S0iX/S3. 205 206 Function will block until suspend/resume has completed or failed. 207 Returns the wake alarm time from the RTC as epoch. 208 209 @param time_in_suspend: Number of seconds to suspend the DUT. 210 @param delay_seconds: Number of seconds wait before suspending the DUT. 211 """ 212 pause_check_network_hook() 213 214 wakeup_count = read_wakeup_count() 215 upstart.ensure_running('powerd') 216 command = ('/usr/bin/powerd_dbus_suspend --delay=%d --timeout=30 ' 217 '--wakeup_count=%d --suspend_for_sec=%d' % 218 (delay_seconds, wakeup_count, time_in_suspend)) 219 logging.info("Running '%s'", command) 220 os.system(command) 221 222 223def suspend_bg_for_dark_resume(suspend_seconds, delay_seconds=0): 224 """Do a non-blocking suspend using power manager. 225 226 Wait for |delay_seconds|, then suspend with an rtc alarm for 227 waking up again after having been suspended for |suspend_seconds|, using 228 the powerd_dbus_suspend program all in the background. 229 230 @param suspend_seconds: Number of seconds to suspend the DUT. 231 @param delay_seconds: Number of seconds wait before suspending the DUT. 232 233 """ 234 upstart.ensure_running('powerd') 235 # Disarm any existing wake alarms so as to prevent early wakeups. 236 os.system('echo 0 > /sys/class/rtc/rtc0/wakealarm') 237 wakeup_count = read_wakeup_count() 238 command = ('/usr/bin/powerd_dbus_suspend --delay=%d --timeout=30 ' 239 '--wakeup_count=%d --wakeup_timeout=%d ' 240 '--disable_dark_resume=false' % 241 (delay_seconds, wakeup_count, suspend_seconds)) 242 logging.info("Running '%s'", command) 243 process = multiprocessing.Process(target=os.system, args=(command,)) 244 process.start() 245 246 247def kernel_suspend(seconds, state='mem'): 248 """Do a kernel suspend. 249 250 Suspend the system to @state, waking up again after |seconds|, by directly 251 writing to /sys/power/state. Function will block until suspend/resume has 252 completed or failed. 253 254 @param seconds: The number of seconds to suspend the device. 255 @param state: power state to suspend to. DEFAULT mem. 256 """ 257 alarm, wakeup_count = prepare_wakeup(seconds) 258 rtc.set_wake_alarm(alarm) 259 logging.debug('Saving wakeup count: %d', wakeup_count) 260 write_wakeup_count(wakeup_count) 261 try: 262 logging.info('Suspending at %d', rtc.get_seconds()) 263 with open(SYSFS_POWER_STATE, 'w') as sysfs_file: 264 sysfs_file.write(state) 265 except IOError as e: 266 logging.exception('Writing to %s failed', SYSFS_POWER_STATE) 267 if e.errno == errno.EBUSY and rtc.get_seconds() >= alarm: 268 # The kernel returns EBUSY if it has to abort because 269 # another wakeup fires before we've reached suspend. 270 raise SpuriousWakeupError('Received spurious wakeup in kernel.') 271 else: 272 # Some driver probably failed to suspend properly. 273 # A hint as to what failed is in errno. 274 raise KernelError('Suspend failed: %s' % e.strerror) 275 else: 276 logging.info('Woke from suspend at %d', rtc.get_seconds()) 277 logging.debug('New wakeup count: %d', read_wakeup_count()) 278 check_wakeup(alarm) 279 280 281def idle_suspend(seconds): 282 """ 283 Wait for the system to suspend to RAM (S3), scheduling the RTC to wake up 284 |seconds| after this function was called. Caller must ensure that the system 285 will idle-suspend in time for this to happen. Returns the wake alarm time 286 from the RTC as epoch. 287 288 @param seconds: The number of seconds before wakeup. 289 """ 290 alarm, _ = prepare_wakeup(seconds) 291 rtc.set_wake_alarm(alarm) 292 while rtc.get_seconds() < alarm: 293 time.sleep(0.2) 294 295 # tell powerd something happened, or it will immediately idle-suspend again 296 # TODO: switch to cros.power_utils#call_powerd_dbus_method once this 297 # horrible mess with the WiFi tests and this file's imports is solved 298 logging.debug('Simulating user activity after idle suspend...') 299 os.system('dbus-send --type=method_call --system ' 300 '--dest=org.chromium.PowerManager /org/chromium/PowerManager ' 301 'org.chromium.PowerManager.HandleUserActivity') 302 303 return alarm 304 305 306def memory_suspend(seconds, size=0): 307 """Do a memory suspend. 308 309 Suspend the system to RAM (S3), waking up again after |seconds|, using 310 the memory_suspend_test tool. Function will block until suspend/resume has 311 completed or failed. 312 313 @param seconds: The number of seconds to suspend the device. 314 @param size: Amount of memory to allocate, in bytes. 315 Set to 0 to let memory_suspend_test determine amount of memory. 316 """ 317 # since we cannot have utils.system_output in here, we need a workaround 318 output = '/tmp/memory_suspend_output' 319 wakeup_count = read_wakeup_count() 320 status = os.system('/usr/bin/memory_suspend_test --wakeup_count=%d ' 321 '--size=%d > %s --suspend_for_sec=%d' % 322 (wakeup_count, size, output, seconds)) 323 status = os.WEXITSTATUS(status) 324 if status == 2: 325 logging.error('memory_suspend_test found the following errors:') 326 for line in open(output, 'r'): 327 logging.error(line) 328 raise MemoryError('Memory corruption found after resume') 329 elif status == 1: 330 raise SuspendFailure('Failure in powerd_suspend during memory test') 331 elif status: 332 raise SuspendFailure('Unknown failure in memory_suspend_test (crash?)') 333 334 335def read_wakeup_count(): 336 """ 337 Retrieves the current value of /sys/power/wakeup_count. 338 """ 339 with open(SYSFS_WAKEUP_COUNT) as sysfs_file: 340 return int(sysfs_file.read().rstrip('\n')) 341 342 343def write_wakeup_count(wakeup_count): 344 """Writes a value to /sys/power/wakeup_count. 345 346 @param wakeup_count: The wakeup count value to write. 347 """ 348 try: 349 with open(SYSFS_WAKEUP_COUNT, 'w') as sysfs_file: 350 sysfs_file.write(str(wakeup_count)) 351 except IOError as e: 352 if (e.errno == errno.EINVAL and read_wakeup_count() != wakeup_count): 353 raise SpuriousWakeupError('wakeup_count changed before suspend') 354 else: 355 raise 356