_hardware.py 3.1 KB

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