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