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..7bc1ff102c4ffb3609c6c7e5981226a567788db5 |
--- /dev/null |
+++ b/scripts/slave/recipe_modules/chromium_android/resources/spawn_device_temp_monitor.py |
@@ -0,0 +1,104 @@ |
+#!/usr/bin/env python |
+# Copyright 2015 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""Launches a daemon to monitor android device temperaures. |
+ |
+This script will repeatedly poll the given devices for their |
+temperatures every 30 seconds via adb and uploads them to monarch |
Sergey Berezin
2015/09/09 16:47:13
Please don't mention internal tools in external CL
Sergey Berezin
2015/09/09 16:47:13
nit: our retention policy starts with once a minut
bpastene
2015/09/09 18:32:20
Done.
bpastene
2015/09/09 18:32:20
Done.
|
+through infra's ts_mon. |
+""" |
+ |
+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. |
+_CPU_TEMP_SENSOR = 'tsens_tz_sensor0' |
+ |
+# TODO(bpastene): change the following once infra.git becomes a checked |
+# out repo on slaves instead of a cipd managed package. |
Sergey Berezin
2015/09/09 16:47:14
nit: don't rely on this happening; we might do awa
bpastene
2015/09/09 18:32:20
Done.
|
+ |
+# Location of the infra-python package's run script. |
+_RUN_PY = '/opt/infra-python/run.py' |
+ |
+ |
+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. |
+ 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. |
+ 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 |
+ |
+ |
+def main(adb_path, |
+ devices_json, |
+ metric_prefix="android_device"): |
+ """Launches the device temperature monitor. |
+ |
+ Polls the devices for their battery and cpu temperatures |
+ 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 |
Sergey Berezin
2015/09/09 16:47:13
This is not the best metric name - you'll have hun
bpastene
2015/09/09 18:32:20
Done. As per our discussion offline, metrics will
|
+ |
+ Args: |
+ adb_path: Path to adb binary. |
+ devices_json: Json list of device serials to poll. |
+ metric_prefix: Prefix of the metric name. |
+ """ |
+ |
+ devices = json.loads(devices_json) |
+ 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) |
+ |
+ |
+if __name__ == '__main__': |
+ sys.exit(main(*sys.argv[1:])) |