OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 import json |
| 6 import os |
| 7 import re |
| 8 import time |
| 9 |
| 10 from chrome_profiler import controllers |
| 11 |
| 12 from pylib import pexpect |
| 13 |
| 14 |
| 15 class ChromeTracingController(controllers.BaseController): |
| 16 def __init__(self, device, package_info, categories, ring_buffer): |
| 17 controllers.BaseController.__init__(self) |
| 18 self._device = device |
| 19 self._package_info = package_info |
| 20 self._categories = categories |
| 21 self._ring_buffer = ring_buffer |
| 22 self._trace_file = None |
| 23 self._trace_interval = None |
| 24 self._trace_start_re = \ |
| 25 re.compile(r'Logging performance trace to file') |
| 26 self._trace_finish_re = \ |
| 27 re.compile(r'Profiler finished[.] Results are in (.*)[.]') |
| 28 self._device.old_interface.StartMonitoringLogcat(clear=False) |
| 29 |
| 30 def __repr__(self): |
| 31 return 'chrome trace' |
| 32 |
| 33 @staticmethod |
| 34 def GetCategories(device, package_info): |
| 35 device.old_interface.BroadcastIntent( |
| 36 package_info.package, 'GPU_PROFILER_LIST_CATEGORIES') |
| 37 try: |
| 38 json_category_list = device.old_interface.WaitForLogMatch( |
| 39 re.compile(r'{"traceCategoriesList(.*)'), None, timeout=5).group(0) |
| 40 except pexpect.TIMEOUT: |
| 41 raise RuntimeError('Performance trace category list marker not found. ' |
| 42 'Is the correct version of the browser running?') |
| 43 |
| 44 record_categories = [] |
| 45 disabled_by_default_categories = [] |
| 46 json_data = json.loads(json_category_list)['traceCategoriesList'] |
| 47 for item in json_data: |
| 48 if item.startswith('disabled-by-default'): |
| 49 disabled_by_default_categories.append(item) |
| 50 else: |
| 51 record_categories.append(item) |
| 52 |
| 53 return record_categories, disabled_by_default_categories |
| 54 |
| 55 def StartTracing(self, interval): |
| 56 self._trace_interval = interval |
| 57 self._device.old_interface.SyncLogCat() |
| 58 self._device.old_interface.BroadcastIntent( |
| 59 self._package_info.package, 'GPU_PROFILER_START', |
| 60 '-e categories "%s"' % ','.join(self._categories), |
| 61 '-e continuous' if self._ring_buffer else '') |
| 62 # Chrome logs two different messages related to tracing: |
| 63 # |
| 64 # 1. "Logging performance trace to file" |
| 65 # 2. "Profiler finished. Results are in [...]" |
| 66 # |
| 67 # The first one is printed when tracing starts and the second one indicates |
| 68 # that the trace file is ready to be pulled. |
| 69 try: |
| 70 self._device.old_interface.WaitForLogMatch( |
| 71 self._trace_start_re, None, timeout=5) |
| 72 except pexpect.TIMEOUT: |
| 73 raise RuntimeError('Trace start marker not found. Is the correct version ' |
| 74 'of the browser running?') |
| 75 |
| 76 def StopTracing(self): |
| 77 self._device.old_interface.BroadcastIntent( |
| 78 self._package_info.package, |
| 79 'GPU_PROFILER_STOP') |
| 80 self._trace_file = self._device.old_interface.WaitForLogMatch( |
| 81 self._trace_finish_re, None, timeout=120).group(1) |
| 82 |
| 83 def PullTrace(self): |
| 84 # Wait a bit for the browser to finish writing the trace file. |
| 85 time.sleep(self._trace_interval / 4 + 1) |
| 86 |
| 87 trace_file = self._trace_file.replace('/storage/emulated/0/', '/sdcard/') |
| 88 host_file = os.path.join(os.path.curdir, os.path.basename(trace_file)) |
| 89 self._device.old_interface.PullFileFromDevice(trace_file, host_file) |
| 90 return host_file |
OLD | NEW |