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