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