Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: tools/telemetry/telemetry/internal/backends/chrome_inspector/tracing_backend.py

Issue 1224083015: [telemetry] Add support for requesting memory dumps via DevTools API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Final adjustments Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 json
5 import logging 6 import logging
6 import socket 7 import socket
7 import time 8 import time
8 9
9 from telemetry import decorators 10 from telemetry import decorators
10 from telemetry.internal.backends.chrome_inspector import inspector_websocket 11 from telemetry.internal.backends.chrome_inspector import inspector_websocket
11 from telemetry.internal.backends.chrome_inspector import websocket 12 from telemetry.internal.backends.chrome_inspector import websocket
12 from telemetry.timeline import trace_data as trace_data_module 13 from telemetry.timeline import trace_data as trace_data_module
13 14
14 15
15 class TracingUnsupportedException(Exception): 16 class TracingUnsupportedException(Exception):
16 pass 17 pass
17 18
18 19
19 class TracingTimeoutException(Exception): 20 class TracingTimeoutException(Exception):
20 pass 21 pass
21 22
22 23
23 class TracingUnrecoverableException(Exception): 24 class TracingUnrecoverableException(Exception):
24 pass 25 pass
25 26
26 27
27 class TracingHasNotRunException(Exception): 28 class TracingHasNotRunException(Exception):
28 pass 29 pass
29 30
30 31
32 class TracingUnexpectedResponseException(Exception):
33 pass
34
35
31 class TracingBackend(object): 36 class TracingBackend(object):
32 def __init__(self, devtools_port): 37 def __init__(self, devtools_port):
33 self._inspector_websocket = inspector_websocket.InspectorWebsocket() 38 self._inspector_websocket = inspector_websocket.InspectorWebsocket()
34 self._inspector_websocket.RegisterDomain( 39 self._inspector_websocket.RegisterDomain(
35 'Tracing', self._NotificationHandler) 40 'Tracing', self._NotificationHandler)
36 41
37 self._inspector_websocket.Connect( 42 self._inspector_websocket.Connect(
38 'ws://127.0.0.1:%i/devtools/browser' % devtools_port) 43 'ws://127.0.0.1:%i/devtools/browser' % devtools_port)
39 self._trace_events = [] 44 self._trace_events = []
40 self._is_tracing_running = False 45 self._is_tracing_running = False
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 req = {'method': 'Tracing.end'} 88 req = {'method': 'Tracing.end'}
84 self._inspector_websocket.SendAndIgnoreResponse(req) 89 self._inspector_websocket.SendAndIgnoreResponse(req)
85 # After Tracing.end, chrome browser will send asynchronous notifications 90 # After Tracing.end, chrome browser will send asynchronous notifications
86 # containing trace data. This is until Tracing.tracingComplete is sent, 91 # containing trace data. This is until Tracing.tracingComplete is sent,
87 # which means there is no trace buffers pending flush. 92 # which means there is no trace buffers pending flush.
88 self._CollectTracingData(timeout) 93 self._CollectTracingData(timeout)
89 self._is_tracing_running = False 94 self._is_tracing_running = False
90 trace_data_builder.AddEventsTo( 95 trace_data_builder.AddEventsTo(
91 trace_data_module.CHROME_TRACE_PART, self._trace_events) 96 trace_data_module.CHROME_TRACE_PART, self._trace_events)
92 97
98 def DumpMemory(self, timeout=30):
99 """Dumps memory.
100
101 Returns:
102 GUID of the generated dump if successful, None otherwise.
103
104 Raises:
105 TracingTimeoutException: If more than |timeout| seconds has passed
106 since the last time any data is received.
107 TracingUnrecoverableException: If there is a websocket error.
108 TracingUnexpectedResponseException: If the response contains an error
109 or does not contain the expected result.
110 """
111 request = {
112 'method': 'Tracing.requestMemoryDump'
113 }
114 try:
115 response = self._inspector_websocket.SyncRequest(request, timeout)
116 except websocket.WebSocketTimeoutException:
117 raise TracingTimeoutException
118 except (socket.error, websocket.WebSocketException,
119 inspector_websocket.WebSocketDisconnected):
120 raise TracingUnrecoverableException
121
122 if ('error' in response or
123 'result' not in response or
124 'success' not in response['result'] or
125 'dumpGuid' not in response['result']):
126 raise TracingUnexpectedResponseException(
127 'Inspector returned unexpected response for '
128 'Tracing.requestMemoryDump:\n' + json.dumps(response, indent=2))
129
130 result = response['result']
131 return result['dumpGuid'] if result['success'] else None
132
93 def _CollectTracingData(self, timeout): 133 def _CollectTracingData(self, timeout):
94 """Collects tracing data. Assumes that Tracing.end has already been sent. 134 """Collects tracing data. Assumes that Tracing.end has already been sent.
95 135
96 Args: 136 Args:
97 timeout: The timeout in seconds. 137 timeout: The timeout in seconds.
98 138
99 Raises: 139 Raises:
100 TracingTimeoutException: If more than |timeout| seconds has passed 140 TracingTimeoutException: If more than |timeout| seconds has passed
101 since the last time any data is received. 141 since the last time any data is received.
102 TracingUnrecoverableException: If there is a websocket error. 142 TracingUnrecoverableException: If there is a websocket error.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 return True 176 return True
137 177
138 def Close(self): 178 def Close(self):
139 self._inspector_websocket.Disconnect() 179 self._inspector_websocket.Disconnect()
140 180
141 @decorators.Cache 181 @decorators.Cache
142 def IsTracingSupported(self): 182 def IsTracingSupported(self):
143 req = {'method': 'Tracing.hasCompleted'} 183 req = {'method': 'Tracing.hasCompleted'}
144 res = self._inspector_websocket.SyncRequest(req) 184 res = self._inspector_websocket.SyncRequest(req)
145 return not res.get('response') 185 return not res.get('response')
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698