Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(456)

Unified Diff: scripts/slave/recipe_modules/chromium_android/resources/spawn_device_temp_monitor.py

Issue 1308173006: Create daemon to monitor android device temperatures (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/build.git@master
Patch Set: respond to comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: scripts/slave/recipe_modules/chromium_android/resources/spawn_device_temp_monitor.py
diff --git a/scripts/slave/recipe_modules/chromium_android/resources/spawn_device_temp_monitor.py b/scripts/slave/recipe_modules/chromium_android/resources/spawn_device_temp_monitor.py
new file mode 100755
index 0000000000000000000000000000000000000000..79d9b4b6674ac53ef147260a5911a8f2fb0892ca
--- /dev/null
+++ b/scripts/slave/recipe_modules/chromium_android/resources/spawn_device_temp_monitor.py
@@ -0,0 +1,107 @@
+#!/usr/bin/env python
ghost stip (do not use) 2015/09/08 21:46:36 Add a license directive to the top. see other file
bpastene 2015/09/09 00:02:18 Done.
+
+"""
ghost stip (do not use) 2015/09/08 21:46:36 """Single line description of the module, ending i
bpastene 2015/09/09 00:02:19 Done.
+Launches a daemon to monitor the devices' temperatures by
+uploading them to monarch every 30 seconds.
+"""
+
+import json
+import logging
+import os
+import optparse
+import re
+import signal
+import subprocess
+import sys
+import time
+
+# Common name of sensor found in nexus devices to measure cpu (core0) temp
ghost stip (do not use) 2015/09/08 21:46:36 comments end in a period.
bpastene 2015/09/09 00:02:18 Done.
+_CPU_TEMP_SENSOR = 'tsens_tz_sensor0'
+
+# TODO(bpastene): change the following once infra.git becomes a checked
+# out repo slaves instead of a cipd managed package
ghost stip (do not use) 2015/09/08 21:46:36 same
bpastene 2015/09/09 00:02:18 Done.
+
+# Location of the infra-python package's run script
ghost stip (do not use) 2015/09/08 21:46:36 same
bpastene 2015/09/09 00:02:18 Done.
+_RUN_PY = '/opt/infra-python/run.py'
+
ghost stip (do not use) 2015/09/08 21:46:36 two spaces for top-level definitions
bpastene 2015/09/09 00:02:18 Done.
+class SigtermError(Exception):
+ """Exception used to catch a sigterm."""
+ pass
+
ghost stip (do not use) 2015/09/08 21:46:36 space
bpastene 2015/09/09 00:02:18 Done.
+def get_device_args(adb_path, metric_prefix, device):
+ bat_temp = None
+ cpu_temp = None
+ # Search for the file that the _CPU_TEMP_SENSOR dumps to and cat it
ghost stip (do not use) 2015/09/08 21:46:36 nit: add a . at the end of a comment.
bpastene 2015/09/09 00:02:18 Done.
+ cmd = [adb_path, '-s', device, 'shell',
+ 'grep -l "%s" /sys/class/thermal/thermal_zone*/type'
+ % (_CPU_TEMP_SENSOR)]
+ try:
+ cpu_temp_files = subprocess.check_output(cmd)
+ if (len(cpu_temp_files.splitlines()) == 1):
+ cpu_temp_file = re.sub('type$', 'temp', cpu_temp_files.strip())
+ cmd = [adb_path, '-s', device, 'shell',
+ 'cat %s' % (cpu_temp_file)]
+ file_contents = subprocess.check_output(cmd)
+ cpu_temp = int(file_contents)
+ except (subprocess.CalledProcessError, TypeError, ValueError):
+ cpu_temp = None
+
+ # Dump system battery info and grab the temp
ghost stip (do not use) 2015/09/08 21:46:36 nit: end in period.
bpastene 2015/09/09 00:02:19 Done.
+ cmd = [adb_path, '-s', device, 'shell', 'dumpsys battery']
+ try:
+ battery_info = subprocess.check_output(cmd)
+ for line in battery_info.splitlines():
+ m = re.match('^\s*temperature: ([0-9]+)\s*$', line)
+ if m:
+ bat_temp = int(m.group(1))
+ except (subprocess.CalledProcessError, TypeError, ValueError):
+ bat_temp = None
+
+ cpu_dict = {'name': "%s/%s/cpu_temp" % (metric_prefix, device),
+ 'value': cpu_temp}
+ cpu_temp_args = ['--float', json.dumps(cpu_dict)] if cpu_temp else []
+ battery_dict = {'name': '%s/%s/battery_temp' % (metric_prefix,
+ device), 'value': bat_temp}
+ bat_temp_args = ['--float',
+ json.dumps(battery_dict)] if bat_temp else []
+ return cpu_temp_args + bat_temp_args
+
ghost stip (do not use) 2015/09/08 21:46:36 space
bpastene 2015/09/09 00:02:19 Done.
+def main(adb_path,
+ devices_json,
+ metric_prefix="android_device"):
+ """Polls the devices for their battery and cpu temperatures
ghost stip (do not use) 2015/09/08 21:46:36 """One line description ending in a period. Fulle
bpastene 2015/09/09 00:02:18 Done.
+ every 30 seconds and uploads them to monarch through infra's
+ ts_mon. Fully qualified, the metric names would be
+ /chrome/infra/${metric_prefix}/${device_serial}/(battery|cpu)_temp
+
+ Args:
+ adb_path: Path to adb binary
+ devices_json: Json list of device serials to poll
+ metric_prefix: Prefix of the metric name.
+ """
+
+ def SigtermHandler(_signum, _unused_frame):
ghost stip (do not use) 2015/09/08 21:46:36 delete lines 83 - 85
bpastene 2015/09/09 00:02:18 Done.
+ raise SigtermError()
+ signal.signal(signal.SIGTERM, SigtermHandler)
+
+ devices = json.loads(devices_json)
+ try:
+ while True:
+ upload_cmd_args = []
+ for device in devices:
+ upload_cmd_args += get_device_args(adb_path, metric_prefix, device)
+
+ cmd = [_RUN_PY, 'infra.tools.send_ts_mon_values'] + upload_cmd_args
+ try:
+ subprocess.Popen(cmd)
+ except OSError:
+ logging.exception('Unable to call %s', _RUN_PY)
+
+ time.sleep(30)
+ except SigtermError:
ghost stip (do not use) 2015/09/08 21:46:36 delete lines 100 - 104
bpastene 2015/09/09 00:02:18 Done.
+ logging.info("Got a SIGTERM, shutting down")
+ except: # pylint: disable=bare-except
+ logging.exception('Unexpected exception in main.')
+
+if __name__ == '__main__':
+ sys.exit(main(*sys.argv[1:]))

Powered by Google App Engine
This is Rietveld 408576698