1# Copyright 2016 Google Inc. 2# 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 8class Hardware: 9 """Locks down and monitors hardware for benchmarking. 10 11 This is a common base for classes that can control the specific hardware 12 we are running on. Its purpose is to lock the hardware into a constant 13 benchmarking mode for the duration of a 'with' block. e.g.: 14 15 with hardware: 16 run_benchmark() 17 18 While benchmarking, the caller must call sanity_check() frequently to verify 19 the hardware state has not changed. 20 21 """ 22 23 def __init__(self): 24 self.warmup_time = 0 25 26 def __enter__(self): 27 return self 28 29 def __exit__(self, exception_type, exception_value, traceback): 30 pass 31 32 def filter_line(self, line): 33 """Returns False if the provided output line can be suppressed.""" 34 return True 35 36 def sanity_check(self): 37 """Raises a HardwareException if any hardware state is not as expected.""" 38 pass 39 40 def print_debug_diagnostics(self): 41 """Prints any info that may help improve or debug hardware monitoring.""" 42 pass 43 44 def sleep(self, sleeptime): 45 """Puts the hardware into a resting state for a fixed amount of time.""" 46 time.sleep(sleeptime) 47 48 49class HardwareException(Exception): 50 """Gets thrown when certain hardware state is not what we expect. 51 52 Generally this happens because of thermal conditions or other variables beyond 53 our control, and the appropriate course of action is to take a short nap 54 before resuming the benchmark. 55 56 """ 57 58 def __init__(self, message, sleeptime=60): 59 Exception.__init__(self, message) 60 self.sleeptime = sleeptime 61 62 63class Expectation: 64 """Simple helper for checking the readings on hardware gauges.""" 65 def __init__(self, value_type, min_value=None, max_value=None, 66 exact_value=None, name=None, sleeptime=60): 67 self.value_type = value_type 68 self.min_value = min_value 69 self.max_value = max_value 70 self.exact_value = exact_value 71 self.name = name 72 self.sleeptime = sleeptime 73 74 def check(self, stringvalue): 75 typedvalue = self.value_type(stringvalue) 76 if self.min_value is not None and typedvalue < self.min_value: 77 raise HardwareException("%s is too low (%s, min=%s)" % 78 (self.name, stringvalue, str(self.min_value)), 79 sleeptime=self.sleeptime) 80 if self.max_value is not None and typedvalue > self.max_value: 81 raise HardwareException("%s is too high (%s, max=%s)" % 82 (self.name, stringvalue, str(self.max_value)), 83 sleeptime=self.sleeptime) 84 if self.exact_value is not None and typedvalue != self.exact_value: 85 raise HardwareException("unexpected %s (%s, expected=%s)" % 86 (self.name, stringvalue, str(self.exact_value)), 87 sleeptime=self.sleeptime) 88 89 @staticmethod 90 def check_all(expectations, stringvalues): 91 if len(stringvalues) != len(expectations): 92 raise Exception("unexpected reading from hardware gauges " 93 "(expected %i values):\n%s" % 94 (len(expectations), '\n'.join(stringvalues))) 95 96 for value, expected in zip(stringvalues, expectations): 97 expected.check(value) 98