1 # Copyright 2013 The Chromium 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.
7 logger
= logging
.getLogger(__name__
)
10 class OmapThrottlingDetector(object):
11 """Class to detect and track thermal throttling on an OMAP 4."""
12 OMAP_TEMP_FILE
= ('/sys/devices/platform/omap/omap_temp_sensor.0/'
16 def IsSupported(device
):
17 return device
.FileExists(OmapThrottlingDetector
.OMAP_TEMP_FILE
)
19 def __init__(self
, device
):
23 def BecameThrottled(log_line
):
24 return 'omap_thermal_throttle' in log_line
27 def BecameUnthrottled(log_line
):
28 return 'omap_thermal_unthrottle' in log_line
31 def GetThrottlingTemperature(log_line
):
32 if 'throttle_delayed_work_fn' in log_line
:
33 return float([s
for s
in log_line
.split() if s
.isdigit()][0]) / 1000.0
35 def GetCurrentTemperature(self
):
36 tempdata
= self
._device
.ReadFile(OmapThrottlingDetector
.OMAP_TEMP_FILE
)
37 return float(tempdata
) / 1000.0
40 class ExynosThrottlingDetector(object):
41 """Class to detect and track thermal throttling on an Exynos 5."""
43 def IsSupported(device
):
44 return device
.FileExists('/sys/bus/exynos5-core')
46 def __init__(self
, device
):
50 def BecameThrottled(log_line
):
51 return 'exynos_tmu: Throttling interrupt' in log_line
54 def BecameUnthrottled(log_line
):
55 return 'exynos_thermal_unthrottle: not throttling' in log_line
58 def GetThrottlingTemperature(_log_line
):
62 def GetCurrentTemperature():
66 class ThermalThrottle(object):
67 """Class to detect and track thermal throttling.
70 Wait for IsThrottled() to be False before running test
71 After running test call HasBeenThrottled() to find out if the
72 test run was affected by thermal throttling.
75 def __init__(self
, device
):
77 self
._throttled
= False
79 if OmapThrottlingDetector
.IsSupported(device
):
80 self
._detector
= OmapThrottlingDetector(device
)
81 elif ExynosThrottlingDetector
.IsSupported(device
):
82 self
._detector
= ExynosThrottlingDetector(device
)
84 def HasBeenThrottled(self
):
85 """True if there has been any throttling since the last call to
86 HasBeenThrottled or IsThrottled.
88 return self
._ReadLog
()
90 def IsThrottled(self
):
91 """True if currently throttled."""
93 return self
._throttled
96 if not self
._detector
:
98 has_been_throttled
= False
99 serial_number
= str(self
._device
)
100 log
= self
._device
.RunShellCommand(
101 ['dmesg', '-c'], large_output
=True, check_return
=True)
102 degree_symbol
= unichr(0x00B0)
104 if self
._detector
.BecameThrottled(line
):
105 if not self
._throttled
:
106 logger
.warning('>>> Device %s thermally throttled', serial_number
)
107 self
._throttled
= True
108 has_been_throttled
= True
109 elif self
._detector
.BecameUnthrottled(line
):
111 logger
.warning('>>> Device %s thermally unthrottled', serial_number
)
112 self
._throttled
= False
113 has_been_throttled
= True
114 temperature
= self
._detector
.GetThrottlingTemperature(line
)
115 if temperature
is not None:
116 logger
.info(u
'Device %s thermally throttled at %3.1f%sC',
117 serial_number
, temperature
, degree_symbol
)
119 if logger
.isEnabledFor(logging
.DEBUG
):
120 # Print current temperature of CPU SoC.
121 temperature
= self
._detector
.GetCurrentTemperature()
122 if temperature
is not None:
123 logger
.debug(u
'Current SoC temperature of %s = %3.1f%sC',
124 serial_number
, temperature
, degree_symbol
)
126 # Print temperature of battery, to give a system temperature
127 dumpsys_log
= self
._device
.RunShellCommand(
128 ['dumpsys', 'battery'], check_return
=True)
129 for line
in dumpsys_log
:
130 if 'temperature' in line
:
131 btemp
= float([s
for s
in line
.split() if s
.isdigit()][0]) / 10.0
132 logger
.debug(u
'Current battery temperature of %s = %3.1f%sC',
133 serial_number
, btemp
, degree_symbol
)
135 return has_been_throttled