2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef InspectorTimelineAgent_h
32 #define InspectorTimelineAgent_h
35 #include "bindings/core/v8/ScriptGCEvent.h"
36 #include "core/InspectorFrontend.h"
37 #include "core/InspectorTypeBuilder.h"
38 #include "core/events/EventPath.h"
39 #include "core/inspector/InspectorBaseAgent.h"
40 #include "core/inspector/ScriptGCEventListener.h"
41 #include "core/inspector/TraceEventDispatcher.h"
42 #include "platform/JSONValues.h"
43 #include "platform/PlatformInstrumentation.h"
44 #include "platform/geometry/LayoutRect.h"
45 #include "wtf/HashMap.h"
46 #include "wtf/HashSet.h"
47 #include "wtf/PassOwnPtr.h"
48 #include "wtf/Vector.h"
49 #include "wtf/WeakPtr.h"
52 struct FetchInitiatorInfo;
53 struct TimelineImageInfo;
54 struct TimelineRecordEntry;
55 struct TimelineThreadState;
61 class ExecutionContext;
64 class GraphicsContext;
66 class InspectorClient;
67 class InspectorFrontend;
68 class InspectorOverlay;
69 class InspectorPageAgent;
70 class InspectorLayerTreeAgent;
78 class ResourceRequest;
79 class ResourceResponse;
80 class TimelineRecordStack;
81 class WebSocketHandshakeRequest;
82 class WebSocketHandshakeResponse;
85 typedef String ErrorString;
87 class InspectorTimelineAgent final
88 : public InspectorBaseAgent<InspectorTimelineAgent>
89 , public ScriptGCEventListener
90 , public InspectorBackendDispatcher::TimelineCommandHandler
91 , public PlatformInstrumentationClient {
92 WTF_MAKE_NONCOPYABLE(InspectorTimelineAgent);
94 enum InspectorType { PageInspector, WorkerInspector };
98 enum Phase { PhaseBegin, PhaseEnd };
99 GPUEvent(double timestamp, int phase, bool foreign, uint64_t usedGPUMemoryBytes, uint64_t limitGPUMemoryBytes) :
100 timestamp(timestamp),
101 phase(static_cast<Phase>(phase)),
103 usedGPUMemoryBytes(usedGPUMemoryBytes),
104 limitGPUMemoryBytes(limitGPUMemoryBytes) { }
108 uint64_t usedGPUMemoryBytes;
109 uint64_t limitGPUMemoryBytes;
112 static PassOwnPtrWillBeRawPtr<InspectorTimelineAgent> create(InspectorPageAgent* pageAgent, InspectorLayerTreeAgent* layerTreeAgent,
113 InspectorOverlay* overlay, InspectorType type, InspectorClient* client)
115 return adoptPtrWillBeNoop(new InspectorTimelineAgent(pageAgent, layerTreeAgent, overlay, type, client));
118 virtual ~InspectorTimelineAgent();
119 virtual void trace(Visitor*) override;
121 virtual void setFrontend(InspectorFrontend*) override;
122 virtual void clearFrontend() override;
123 virtual void restore() override;
125 virtual void enable(ErrorString*) override;
126 virtual void disable(ErrorString*) override;
127 virtual void start(ErrorString*, const int* maxCallStackDepth, const bool* bufferEvents, const String* liveEvents, const bool* includeCounters, const bool* includeGPUEvents) override;
128 virtual void stop(ErrorString*) override;
130 void setLayerTreeId(int layerTreeId) { m_layerTreeId = layerTreeId; }
131 int id() const { return m_id; }
133 void didCommitLoad();
135 // Methods called from WebCore.
136 bool willCallFunction(ExecutionContext*, int scriptId, const String& scriptName, int scriptLine);
137 void didCallFunction();
139 bool willDispatchEvent(Document* document, const Event& event, LocalDOMWindow* window, Node* node, const EventPath& eventPath);
140 bool willDispatchEventOnWindow(const Event& event, LocalDOMWindow* window);
141 void didDispatchEvent();
142 void didDispatchEventOnWindow();
144 void didBeginFrame(int frameId);
145 void didCancelFrame();
147 void didInvalidateLayout(LocalFrame*);
148 bool willLayout(LocalFrame*);
149 void didLayout(RenderObject*);
151 void willUpdateLayerTree();
152 void layerTreeDidChange();
153 void didUpdateLayerTree();
155 void didScheduleStyleRecalculation(Document*);
156 bool willRecalculateStyle(Document*);
157 void didRecalculateStyle(int elementCount);
159 void willPaint(RenderObject*, const GraphicsLayer*);
160 void didPaint(RenderObject*, const GraphicsLayer*, GraphicsContext*, const LayoutRect&);
162 void willPaintImage(RenderImage*);
163 void didPaintImage();
165 void willScrollLayer(RenderObject*);
166 void didScrollLayer();
168 void willComposite();
171 bool willWriteHTML(Document*, unsigned startLine);
172 void didWriteHTML(unsigned endLine);
174 void didInstallTimer(ExecutionContext*, int timerId, int timeout, bool singleShot);
175 void didRemoveTimer(ExecutionContext*, int timerId);
176 bool willFireTimer(ExecutionContext*, int timerId);
179 bool willDispatchXHRReadyStateChangeEvent(ExecutionContext*, XMLHttpRequest*);
180 void didDispatchXHRReadyStateChangeEvent();
181 bool willDispatchXHRLoadEvent(ExecutionContext*, XMLHttpRequest*);
182 void didDispatchXHRLoadEvent();
184 bool willEvaluateScript(LocalFrame*, const String&, int);
185 void didEvaluateScript();
187 void consoleTimeStamp(ExecutionContext*, const String& title);
188 void domContentLoadedEventFired(LocalFrame*);
189 void loadEventFired(LocalFrame*);
191 void consoleTime(ExecutionContext*, const String&);
192 void consoleTimeEnd(ExecutionContext*, const String&, ScriptState*);
193 void consoleTimeline(ExecutionContext*, const String& title, ScriptState*);
194 void consoleTimelineEnd(ExecutionContext*, const String& title, ScriptState*);
196 void willSendRequest(unsigned long, DocumentLoader*, const ResourceRequest&, const ResourceResponse&, const FetchInitiatorInfo&);
197 void didReceiveResourceResponse(LocalFrame*, unsigned long, DocumentLoader*, const ResourceResponse&, ResourceLoader*);
198 void didFinishLoading(unsigned long, DocumentLoader*, double monotonicFinishTime, int64_t);
199 void didFailLoading(unsigned long identifier, const ResourceError&);
200 void didReceiveData(LocalFrame*, unsigned long identifier, const char* data, int dataLength, int encodedDataLength);
202 void didRequestAnimationFrame(Document*, int callbackId);
203 void didCancelAnimationFrame(Document*, int callbackId);
204 bool willFireAnimationFrame(Document*, int callbackId);
205 void didFireAnimationFrame();
207 void willProcessTask();
208 void didProcessTask();
210 void didCreateWebSocket(Document*, unsigned long identifier, const KURL&, const String& protocol);
211 void willSendWebSocketHandshakeRequest(Document*, unsigned long identifier, const WebSocketHandshakeRequest*);
212 void didReceiveWebSocketHandshakeResponse(Document*, unsigned long identifier, const WebSocketHandshakeRequest*, const WebSocketHandshakeResponse*);
213 void didCloseWebSocket(Document*, unsigned long identifier);
215 void processGPUEvent(const GPUEvent&);
217 // ScriptGCEventListener methods.
218 virtual void didGC(double, double, size_t) override;
220 // PlatformInstrumentationClient methods.
221 virtual void willDecodeImage(const String& imageType) override;
222 virtual void didDecodeImage() override;
223 virtual void willResizeImage(bool shouldCache) override;
224 virtual void didResizeImage() override;
228 friend class TimelineRecordStack;
230 InspectorTimelineAgent(InspectorPageAgent*, InspectorLayerTreeAgent*, InspectorOverlay*, InspectorType, InspectorClient*);
232 // Trace event handlers
233 void onBeginImplSideFrame(const TraceEventDispatcher::TraceEvent&);
234 void onPaintSetupBegin(const TraceEventDispatcher::TraceEvent&);
235 void onPaintSetupEnd(const TraceEventDispatcher::TraceEvent&);
236 void onRasterTaskBegin(const TraceEventDispatcher::TraceEvent&);
237 void onRasterTaskEnd(const TraceEventDispatcher::TraceEvent&);
238 void onImageDecodeBegin(const TraceEventDispatcher::TraceEvent&);
239 void onImageDecodeEnd(const TraceEventDispatcher::TraceEvent&);
240 void onLayerDeleted(const TraceEventDispatcher::TraceEvent&);
241 void onDrawLazyPixelRef(const TraceEventDispatcher::TraceEvent&);
242 void onDecodeLazyPixelRefBegin(const TraceEventDispatcher::TraceEvent&);
243 void onDecodeLazyPixelRefEnd(const TraceEventDispatcher::TraceEvent&);
244 void onRequestMainThreadFrame(const TraceEventDispatcher::TraceEvent&);
245 void onActivateLayerTree(const TraceEventDispatcher::TraceEvent&);
246 void onDrawFrame(const TraceEventDispatcher::TraceEvent&);
247 void onLazyPixelRefDeleted(const TraceEventDispatcher::TraceEvent&);
248 void onEmbedderCallbackBegin(const TraceEventDispatcher::TraceEvent&);
249 void onEmbedderCallbackEnd(const TraceEventDispatcher::TraceEvent&);
251 void didFinishLoadingResource(unsigned long, bool didFail, double finishTime);
253 void sendEvent(PassRefPtr<TypeBuilder::Timeline::TimelineEvent>);
254 void appendRecord(PassRefPtr<JSONObject> data, const String& type, bool captureCallStack, LocalFrame*);
255 void pushCurrentRecord(PassRefPtr<JSONObject> data, const String& type, bool captureCallStack, LocalFrame*, bool hasLowLevelDetails = false);
256 TimelineThreadState& threadState(ThreadIdentifier);
258 void setFrameIdentifier(TypeBuilder::Timeline::TimelineEvent* record, LocalFrame*);
259 void populateImageDetails(JSONObject* data, const RenderImage&);
261 void didCompleteCurrentRecord(const String& type);
262 void unwindRecordStack();
264 void commitFrameRecord();
266 void addRecordToTimeline(PassRefPtr<TypeBuilder::Timeline::TimelineEvent>, double ts);
267 void innerAddRecordToTimeline(PassRefPtr<TypeBuilder::Timeline::TimelineEvent>);
268 PassRefPtr<TypeBuilder::Timeline::TimelineEvent> createCountersUpdate();
269 void clearRecordStack();
270 PassRefPtr<TypeBuilder::Timeline::TimelineEvent> createRecordForEvent(const TraceEventDispatcher::TraceEvent&, const String& type, PassRefPtr<JSONObject> data);
272 void localToPageQuad(const RenderObject& renderer, const LayoutRect&, FloatQuad*);
273 long long nodeId(Node*);
274 long long nodeId(RenderObject*);
278 LocalFrame* mainFrame() const;
282 void innerStop(bool fromConsole);
283 void setLiveEvents(const String&);
285 RawPtrWillBeMember<InspectorPageAgent> m_pageAgent;
286 RawPtrWillBeMember<InspectorLayerTreeAgent> m_layerTreeAgent;
287 InspectorFrontend::Timeline* m_frontend;
288 InspectorClient* m_client;
289 InspectorOverlay* m_overlay;
290 InspectorType m_inspectorType;
293 unsigned long long m_layerTreeId;
295 int m_maxCallStackDepth;
297 Vector<TimelineRecordEntry> m_recordStack;
298 RefPtr<TypeBuilder::Array<TypeBuilder::Timeline::TimelineEvent> > m_bufferedEvents;
299 Vector<String> m_consoleTimelines;
301 unsigned m_platformInstrumentationClientInstalledAtStackDepth;
302 RefPtr<TypeBuilder::Timeline::TimelineEvent> m_pendingFrameRecord;
303 RefPtr<TypeBuilder::Timeline::TimelineEvent> m_pendingGPURecord;
304 typedef HashMap<unsigned long long, TimelineImageInfo> PixelRefToImageInfoMap;
305 PixelRefToImageInfoMap m_pixelRefToImageInfo;
306 RenderImage* m_imageBeingPainted;
307 HashMap<unsigned long long, long long> m_layerToNodeMap;
308 double m_paintSetupStart;
309 double m_paintSetupEnd;
310 RefPtr<JSONObject> m_gpuTask;
311 RefPtr<JSONValue> m_pendingLayerTreeData;
312 typedef WillBeHeapHashMap<ThreadIdentifier, TimelineThreadState> ThreadStateMap;
313 ThreadStateMap m_threadStates;
314 bool m_mayEmitFirstPaint;
315 HashSet<String> m_liveEvents;
316 double m_lastProgressTimestamp;
321 #endif // !defined(InspectorTimelineAgent_h)