blob: f1c8c2684358301a4bb3d64b59d2c273866a8078 [file] [log] [blame]
csmartdaltond7a9db62016-09-22 05:10:02 -07001# 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
csmartdaltond7a9db62016-09-22 05:10:02 -07008class Hardware:
csmartdaltonbf41fa82016-09-23 11:36:11 -07009 """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
csmartdaltond7a9db62016-09-22 05:10:02 -070023 def __init__(self):
24 self.kick_in_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 sanity_check(self):
csmartdaltonbf41fa82016-09-23 11:36:11 -070033 """Raises a HardwareException if any hardware state is not as expected."""
csmartdaltond7a9db62016-09-22 05:10:02 -070034 pass
35
36 def sleep(self, sleeptime):
csmartdaltonbf41fa82016-09-23 11:36:11 -070037 """Puts the hardware into a resting state for a fixed amount of time."""
csmartdaltond7a9db62016-09-22 05:10:02 -070038 time.sleep(sleeptime)
csmartdaltonbf41fa82016-09-23 11:36:11 -070039
40
41class HardwareException(Exception):
42 """Gets thrown when certain hardware state is not what we expect.
43
44 Generally this happens because of thermal conditions or other variables beyond
45 our control, and the appropriate course of action is to take a short nap
46 before resuming the benchmark.
47
48 """
49
50 def __init__(self, message, sleeptime=60):
51 Exception.__init__(self, message)
52 self.sleeptime = sleeptime
53
54
55class Expectation:
56 """Simple helper for checking the readings on hardware gauges."""
57 def __init__(self, value_type, min_value=None, max_value=None,
58 exact_value=None, name=None, sleeptime=60):
59 self.value_type = value_type
60 self.min_value = min_value
61 self.max_value = max_value
62 self.exact_value = exact_value
63 self.name = name
64 self.sleeptime = sleeptime
65
66 def check(self, stringvalue):
67 typedvalue = self.value_type(stringvalue)
68 if self.min_value is not None and typedvalue < self.min_value:
69 raise HardwareException("%s is too low (%s, min=%s)" %
70 (self.name, stringvalue, str(self.min_value)),
71 sleeptime=self.sleeptime)
72 if self.max_value is not None and typedvalue > self.max_value:
73 raise HardwareException("%s is too high (%s, max=%s)" %
74 (self.name, stringvalue, str(self.max_value)),
75 sleeptime=self.sleeptime)
76 if self.exact_value is not None and typedvalue != self.exact_value:
77 raise HardwareException("unexpected %s (%s, expected=%s)" %
78 (self.name, stringvalue, str(self.exact_value)),
79 sleeptime=self.sleeptime)
80
81 @staticmethod
82 def check_all(expectations, stringvalues):
83 if len(stringvalues) != len(expectations):
84 raise Exception("unexpected reading from hardware gauges "
85 "(expected %i values):\n%s" %
86 (len(expectations), '\n'.join(stringvalues)))
87
88 for value, expected in zip(stringvalues, expectations):
89 expected.check(value)