1 # Copyright 2014 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.
5 import telemetry.core.platform.power_monitor as power_monitor
7 _TEMPERATURE_FILE = '/sys/class/thermal/thermal_zone0/temp'
10 class AndroidTemperatureMonitor(power_monitor.PowerMonitor):
12 Delegates monitoring to another PowerMonitor and adds temperature measurements
15 def __init__(self, monitor, device):
16 super(AndroidTemperatureMonitor, self).__init__()
18 self._power_monitor = monitor
19 self._can_monitor_with_power_monitor = None
21 def CanMonitorPower(self):
22 self._can_monitor_with_power_monitor = (
23 self._power_monitor.CanMonitorPower())
24 # Always report ability to monitor power to be able to provide temperature
25 # metrics and other useful power-related data from sensors.
28 def StartMonitoringPower(self, browser):
29 if self._can_monitor_with_power_monitor:
30 self._power_monitor.StartMonitoringPower(browser)
32 def StopMonitoringPower(self):
33 if self._can_monitor_with_power_monitor:
34 power_data = self._power_monitor.StopMonitoringPower()
36 power_data = {'identifier': 'android_temperature_monitor'}
38 # Take the current temperature as average based on the assumption that the
39 # temperature changes slowly during measurement time.
40 average_temperature = self._GetBoardTemperatureCelsius()
41 if average_temperature is None:
44 # Insert temperature into the appropriate position in the dictionary
45 # returned by StopMonitoringPower() creating appropriate sub-dictionaries on
46 # the way if necessary.
48 'component_utilization', 'whole_package', 'average_temperature_c']
49 temperature_insertion_point = power_data
50 for path_element in temperature_path[:-1]:
51 if not path_element in temperature_insertion_point:
52 temperature_insertion_point[path_element] = {}
53 temperature_insertion_point = temperature_insertion_point[path_element]
54 assert temperature_path[-1] not in temperature_insertion_point
55 temperature_insertion_point[temperature_path[-1]] = average_temperature
59 def _GetBoardTemperatureCelsius(self):
60 contents = self._device.ReadFile(_TEMPERATURE_FILE)
62 return float(contents[0])