Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 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 | |
Thieu Le | 1904d00 | 2010-11-30 17:10:24 -0800 | [diff] [blame] | 5 | import logging, os, platform, time |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 6 | from autotest_lib.client.common_lib import error |
| 7 | |
| 8 | |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 9 | class TimeoutError(error.TestError): |
| 10 | """Error raised when we time out when waiting on a condition.""" |
| 11 | |
| 12 | |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 13 | def poll_for_condition( |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 14 | condition, exception=None, timeout=10, sleep_interval=0.1, desc=None): |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 15 | """Poll until a condition becomes true. |
| 16 | |
| 17 | condition: function taking no args and returning bool |
| 18 | exception: exception to throw if condition doesn't become true |
| 19 | timeout: maximum number of seconds to wait |
| 20 | sleep_interval: time to sleep between polls |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 21 | desc: description of default TimeoutError used if 'exception' is None |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 22 | |
| 23 | Raises: |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 24 | 'exception' arg if supplied; site_utils.TimeoutError otherwise |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 25 | """ |
| 26 | start_time = time.time() |
| 27 | while True: |
| 28 | if condition(): |
| 29 | return |
| 30 | if time.time() + sleep_interval - start_time > timeout: |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 31 | if exception: |
| 32 | raise exception |
| 33 | |
| 34 | if desc: |
| 35 | desc = 'Timed out waiting for condition: %s' % desc |
| 36 | else: |
| 37 | desc = 'Timed out waiting for unnamed condition' |
Mitsuru Oshima | 5d3e454 | 2010-08-18 13:46:06 -0700 | [diff] [blame] | 38 | raise TimeoutError, desc |
rginda | f25f73f | 2010-04-07 14:55:25 -0700 | [diff] [blame] | 39 | |
Daniel Erat | 3e3f7f4 | 2010-03-29 17:19:14 -0700 | [diff] [blame] | 40 | time.sleep(sleep_interval) |
Thieu Le | 1904d00 | 2010-11-30 17:10:24 -0800 | [diff] [blame] | 41 | |
| 42 | |
| 43 | def save_vm_state(checkpoint): |
| 44 | """Saves the current state of the virtual machine. |
| 45 | |
| 46 | This function is a NOOP if the test is not running under a virtual machine |
| 47 | with the USB serial port redirected. |
| 48 | |
| 49 | Arguments: |
| 50 | checkpoint - Name used to identify this state |
| 51 | |
| 52 | Returns: |
| 53 | None |
| 54 | """ |
| 55 | # The QEMU monitor has been redirected to the guest serial port located at |
| 56 | # /dev/ttyUSB0. To save the state of the VM, we just send the 'savevm' |
| 57 | # command to the serial port. |
| 58 | proc = platform.processor() |
| 59 | if 'QEMU' in proc and os.path.exists('/dev/ttyUSB0'): |
| 60 | logging.info('Saving VM state "%s"' % checkpoint) |
| 61 | serial = open('/dev/ttyUSB0', 'w') |
| 62 | serial.write("savevm %s\r\n" % checkpoint) |
| 63 | logging.info('Done saving VM state "%s"' % checkpoint) |
Mandeep Singh Baines | 142ac8d | 2011-02-18 13:31:08 -0800 | [diff] [blame] | 64 | |
| 65 | |
| 66 | def check_raw_dmesg(dmesg, message_level, whitelist): |
| 67 | """Checks dmesg for unexpected warnings. |
| 68 | |
| 69 | This function parses dmesg for message with message_level <= message_level |
| 70 | which do not appear in the whitelist. |
| 71 | |
| 72 | Arguments: |
| 73 | dmesg - string containing raw dmesg buffer |
| 74 | message_level - minimum message priority to check |
| 75 | whitelist - messages to ignore |
| 76 | |
| 77 | Returns: |
| 78 | List of unexpected warnings |
| 79 | """ |
| 80 | |
| 81 | unexpected = [] |
| 82 | for line in dmesg.splitlines(): |
| 83 | if int(line[1]) <= message_level: |
| 84 | if not 'used greatest stack depth' in line: |
| 85 | stripped_line = line.split('] ', 1)[1] |
| 86 | if not stripped_line in whitelist: |
| 87 | unexpected.append(stripped_line) |
| 88 | return unexpected |