Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 logging | 5 import logging |
| 6 | 6 |
| 7 from telemetry.core.backends.chrome import inspector_websocket | 7 from telemetry.core.backends.chrome import inspector_websocket |
| 8 from telemetry.core.platform import tracing_category_filter | 8 from telemetry.core.platform import tracing_category_filter |
| 9 from telemetry.core.platform import tracing_options | 9 from telemetry.core.platform import tracing_options |
| 10 | 10 |
| 11 | 11 |
| 12 class TracingUnsupportedException(Exception): | 12 class TracingUnsupportedException(Exception): |
| 13 pass | 13 pass |
| 14 | 14 |
| 15 | 15 |
| 16 class TracingTimeoutException(Exception): | 16 class TracingTimeoutException(Exception): |
| 17 pass | 17 pass |
| 18 | 18 |
| 19 | 19 |
| 20 class TracingBackend(object): | 20 class TracingBackend(object): |
| 21 def __init__(self, devtools_port, chrome_browser_backend): | 21 def __init__(self, devtools_port, chrome_browser_backend): |
| 22 self._inspector_websocket = inspector_websocket.InspectorWebsocket( | 22 self._inspector_websocket = inspector_websocket.InspectorWebsocket( |
| 23 self._NotificationHandler, | 23 self._NotificationHandler, |
| 24 self._ErrorHandler) | 24 self._ErrorHandler) |
| 25 | 25 |
| 26 self._inspector_websocket.Connect( | 26 self._inspector_websocket.Connect( |
| 27 'ws://127.0.0.1:%i/devtools/browser' % devtools_port) | 27 'ws://127.0.0.1:%i/devtools/browser' % devtools_port) |
| 28 self._tracing_data = [] | |
| 28 self._category_filter = None | 29 self._category_filter = None |
| 29 self._nesting = 0 | |
| 30 self._tracing_data = [] | |
| 31 self._is_tracing_running = False | 30 self._is_tracing_running = False |
| 32 self._chrome_browser_backend = chrome_browser_backend | 31 self._chrome_browser_backend = chrome_browser_backend |
| 33 | 32 |
| 34 @property | 33 @property |
| 35 def is_tracing_running(self): | 34 def is_tracing_running(self): |
| 36 return self._is_tracing_running | 35 return self._is_tracing_running |
| 37 | 36 |
| 38 def StartTracing(self, trace_options, custom_categories=None, timeout=10): | 37 def StartTracing(self, trace_options, custom_categories=None, timeout=10): |
| 39 """ Starts tracing on the first nested call and returns True. Returns False | 38 """ Starts tracing on the first call and returns True. Returns False |
| 40 and does nothing on subsequent nested calls. | 39 and does nothing on subsequent nested calls. |
| 41 """ | 40 """ |
| 42 self._nesting += 1 | |
| 43 if self.is_tracing_running: | 41 if self.is_tracing_running: |
|
dtu
2014/09/15 22:06:33
Maybe we don't need this check and/or this variabl
nednguyen
2014/09/15 22:09:47
No, if we allow StartTracing to be called multiple
chrishenry
2014/09/23 17:49:26
Should we throw exception? The return value from S
nednguyen
2014/10/03 17:13:47
done.
nednguyen
2014/10/03 17:13:47
tracing_controller_backend return False in case it
| |
| 44 new_category_filter = tracing_category_filter.TracingCategoryFilter( | 42 new_category_filter = tracing_category_filter.TracingCategoryFilter( |
| 45 filter_string=custom_categories) | 43 filter_string=custom_categories) |
| 46 is_subset = new_category_filter.IsSubset(self._category_filter) | 44 is_subset = new_category_filter.IsSubset(self._category_filter) |
| 47 assert(is_subset != False) | 45 assert(is_subset != False) |
| 48 if is_subset == None: | 46 if is_subset == None: |
| 49 logging.warning('Cannot determine if category filter of nested ' + | 47 logging.warning('Cannot determine if category filter of nested ' + |
| 50 'StartTracing call is subset of current filter.') | 48 'StartTracing call is subset of current filter.') |
| 51 return False | 49 return False |
| 52 self._CheckNotificationSupported() | 50 self._CheckNotificationSupported() |
| 53 #TODO(nednguyen): remove this when the stable branch pass 2118. | 51 #TODO(nednguyen): remove this when the stable branch pass 2118. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 69 req['params']['options'] = m[trace_options.record_mode] | 67 req['params']['options'] = m[trace_options.record_mode] |
| 70 self._category_filter = tracing_category_filter.TracingCategoryFilter( | 68 self._category_filter = tracing_category_filter.TracingCategoryFilter( |
| 71 filter_string=custom_categories) | 69 filter_string=custom_categories) |
| 72 if custom_categories: | 70 if custom_categories: |
| 73 req['params']['categories'] = custom_categories | 71 req['params']['categories'] = custom_categories |
| 74 self._inspector_websocket.SyncRequest(req, timeout) | 72 self._inspector_websocket.SyncRequest(req, timeout) |
| 75 self._is_tracing_running = True | 73 self._is_tracing_running = True |
| 76 return True | 74 return True |
| 77 | 75 |
| 78 def StopTracing(self, timeout=30): | 76 def StopTracing(self, timeout=30): |
| 79 """ Stops tracing on the innermost (!) nested call, because we cannot get | 77 """ Stops tracing and returns the raw json trace result. It this is called |
| 80 results otherwise. Resets _tracing_data on the outermost nested call. | 78 after tracing has been stopped, empty trace data is returned. |
| 81 Returns the result of the trace, as TracingTimelineData object. | |
| 82 """ | 79 """ |
| 83 self._nesting -= 1 | |
| 84 assert self._nesting >= 0 | |
| 85 if self.is_tracing_running: | 80 if self.is_tracing_running: |
| 86 req = {'method': 'Tracing.end'} | 81 req = {'method': 'Tracing.end'} |
| 87 self._inspector_websocket.SendAndIgnoreResponse(req) | 82 self._inspector_websocket.SendAndIgnoreResponse(req) |
| 88 # After Tracing.end, chrome browser will send asynchronous notifications | 83 # After Tracing.end, chrome browser will send asynchronous notifications |
| 89 # containing trace data. This is until Tracing.tracingComplete is sent, | 84 # containing trace data. This is until Tracing.tracingComplete is sent, |
| 90 # which means there is no trace buffers pending flush. | 85 # which means there is no trace buffers pending flush. |
| 91 try: | 86 try: |
| 92 self._inspector_websocket.DispatchNotificationsUntilDone(timeout) | 87 self._inspector_websocket.DispatchNotificationsUntilDone(timeout) |
| 93 except \ | 88 except \ |
| 94 inspector_websocket.DispatchNotificationsUntilDoneTimeoutException \ | 89 inspector_websocket.DispatchNotificationsUntilDoneTimeoutException \ |
| 95 as err: | 90 as err: |
| 96 raise TracingTimeoutException( | 91 raise TracingTimeoutException( |
| 97 'Trace data was not fully received due to timeout after %s ' | 92 'Trace data was not fully received due to timeout after %s ' |
| 98 'seconds. If the trace data is big, you may want to increase the ' | 93 'seconds. If the trace data is big, you may want to increase the ' |
| 99 'time out amount.' % err.elapsed_time) | 94 'time out amount.' % err.elapsed_time) |
| 100 self._is_tracing_running = False | 95 self._is_tracing_running = False |
| 101 if self._nesting == 0: | 96 return self._GetTraceResultAndReset() |
| 102 self._category_filter = None | |
| 103 return self._GetTraceResultAndReset() | |
| 104 else: | |
| 105 return self._GetTraceResult() | |
| 106 | |
| 107 def _GetTraceResult(self): | |
| 108 assert not self.is_tracing_running | |
| 109 return self._tracing_data | |
| 110 | 97 |
| 111 def _GetTraceResultAndReset(self): | 98 def _GetTraceResultAndReset(self): |
| 112 result = self._GetTraceResult() | 99 result = self._tracing_data |
| 113 | |
| 114 self._tracing_data = [] | 100 self._tracing_data = [] |
| 115 return result | 101 return result |
| 116 | 102 |
| 117 def _ErrorHandler(self, elapsed): | 103 def _ErrorHandler(self, elapsed): |
| 118 logging.error('Unrecoverable error after %ds reading tracing response.', | 104 logging.error('Unrecoverable error after %ds reading tracing response.', |
| 119 elapsed) | 105 elapsed) |
| 120 raise | 106 raise |
| 121 | 107 |
| 122 def _NotificationHandler(self, res): | 108 def _NotificationHandler(self, res): |
| 123 if 'Tracing.dataCollected' == res.get('method'): | 109 if 'Tracing.dataCollected' == res.get('method'): |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 136 | 122 |
| 137 def _CheckNotificationSupported(self): | 123 def _CheckNotificationSupported(self): |
| 138 """Ensures we're running against a compatible version of chrome.""" | 124 """Ensures we're running against a compatible version of chrome.""" |
| 139 req = {'method': 'Tracing.hasCompleted'} | 125 req = {'method': 'Tracing.hasCompleted'} |
| 140 res = self._inspector_websocket.SyncRequest(req) | 126 res = self._inspector_websocket.SyncRequest(req) |
| 141 if res.get('response'): | 127 if res.get('response'): |
| 142 raise TracingUnsupportedException( | 128 raise TracingUnsupportedException( |
| 143 'Tracing not supported for this browser') | 129 'Tracing not supported for this browser') |
| 144 elif 'error' in res: | 130 elif 'error' in res: |
| 145 return | 131 return |
| OLD | NEW |