InspectorTimelineAgent.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * Copyright (C) 2013 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  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
  13. * distribution.
  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.
  17. *
  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.
  29. */
  30. #include "config.h"
  31. #if ENABLE(INSPECTOR)
  32. #include "InspectorTimelineAgent.h"
  33. #include "Event.h"
  34. #include "Frame.h"
  35. #include "FrameView.h"
  36. #include "IdentifiersFactory.h"
  37. #include "InspectorClient.h"
  38. #include "InspectorCounters.h"
  39. #include "InspectorFrontend.h"
  40. #include "InspectorInstrumentation.h"
  41. #include "InspectorMemoryAgent.h"
  42. #include "InspectorPageAgent.h"
  43. #include "InspectorState.h"
  44. #include "InstrumentingAgents.h"
  45. #include "IntRect.h"
  46. #include "RenderObject.h"
  47. #include "RenderView.h"
  48. #include "ResourceRequest.h"
  49. #include "ResourceResponse.h"
  50. #include "TimelineRecordFactory.h"
  51. #include "TimelineTraceEventProcessor.h"
  52. #include <wtf/CurrentTime.h>
  53. namespace WebCore {
  54. namespace TimelineAgentState {
  55. static const char timelineAgentEnabled[] = "timelineAgentEnabled";
  56. static const char timelineMaxCallStackDepth[] = "timelineMaxCallStackDepth";
  57. static const char includeDomCounters[] = "includeDomCounters";
  58. static const char includeNativeMemoryStatistics[] = "includeNativeMemoryStatistics";
  59. }
  60. // Must be kept in sync with WebInspector.TimelineModel.RecordType in TimelineModel.js
  61. namespace TimelineRecordType {
  62. static const char Program[] = "Program";
  63. static const char EventDispatch[] = "EventDispatch";
  64. static const char BeginFrame[] = "BeginFrame";
  65. static const char ScheduleStyleRecalculation[] = "ScheduleStyleRecalculation";
  66. static const char RecalculateStyles[] = "RecalculateStyles";
  67. static const char InvalidateLayout[] = "InvalidateLayout";
  68. static const char Layout[] = "Layout";
  69. static const char Paint[] = "Paint";
  70. static const char ScrollLayer[] = "ScrollLayer";
  71. static const char ResizeImage[] = "ResizeImage";
  72. static const char CompositeLayers[] = "CompositeLayers";
  73. static const char ParseHTML[] = "ParseHTML";
  74. static const char TimerInstall[] = "TimerInstall";
  75. static const char TimerRemove[] = "TimerRemove";
  76. static const char TimerFire[] = "TimerFire";
  77. static const char EvaluateScript[] = "EvaluateScript";
  78. static const char MarkLoad[] = "MarkLoad";
  79. static const char MarkDOMContent[] = "MarkDOMContent";
  80. static const char TimeStamp[] = "TimeStamp";
  81. static const char Time[] = "Time";
  82. static const char TimeEnd[] = "TimeEnd";
  83. static const char ScheduleResourceRequest[] = "ScheduleResourceRequest";
  84. static const char ResourceSendRequest[] = "ResourceSendRequest";
  85. static const char ResourceReceiveResponse[] = "ResourceReceiveResponse";
  86. static const char ResourceReceivedData[] = "ResourceReceivedData";
  87. static const char ResourceFinish[] = "ResourceFinish";
  88. static const char XHRReadyStateChange[] = "XHRReadyStateChange";
  89. static const char XHRLoad[] = "XHRLoad";
  90. static const char FunctionCall[] = "FunctionCall";
  91. static const char GCEvent[] = "GCEvent";
  92. static const char RequestAnimationFrame[] = "RequestAnimationFrame";
  93. static const char CancelAnimationFrame[] = "CancelAnimationFrame";
  94. static const char FireAnimationFrame[] = "FireAnimationFrame";
  95. static const char WebSocketCreate[] = "WebSocketCreate";
  96. static const char WebSocketSendHandshakeRequest[] = "WebSocketSendHandshakeRequest";
  97. static const char WebSocketReceiveHandshakeResponse[] = "WebSocketReceiveHandshakeResponse";
  98. static const char WebSocketDestroy[] = "WebSocketDestroy";
  99. // Event names visible to other modules.
  100. const char DecodeImage[] = "DecodeImage";
  101. const char Rasterize[] = "Rasterize";
  102. }
  103. void TimelineTimeConverter::reset()
  104. {
  105. m_startOffset = monotonicallyIncreasingTime() - currentTime();
  106. }
  107. void InspectorTimelineAgent::pushGCEventRecords()
  108. {
  109. if (!m_gcEvents.size())
  110. return;
  111. GCEvents events = m_gcEvents;
  112. m_gcEvents.clear();
  113. for (GCEvents::iterator i = events.begin(); i != events.end(); ++i) {
  114. RefPtr<InspectorObject> record = TimelineRecordFactory::createGenericRecord(m_timeConverter.fromMonotonicallyIncreasingTime(i->startTime), m_maxCallStackDepth);
  115. record->setObject("data", TimelineRecordFactory::createGCEventData(i->collectedBytes));
  116. record->setNumber("endTime", m_timeConverter.fromMonotonicallyIncreasingTime(i->endTime));
  117. addRecordToTimeline(record.release(), TimelineRecordType::GCEvent);
  118. }
  119. }
  120. void InspectorTimelineAgent::didGC(double startTime, double endTime, size_t collectedBytesCount)
  121. {
  122. m_gcEvents.append(GCEvent(startTime, endTime, collectedBytesCount));
  123. }
  124. InspectorTimelineAgent::~InspectorTimelineAgent()
  125. {
  126. clearFrontend();
  127. }
  128. void InspectorTimelineAgent::setFrontend(InspectorFrontend* frontend)
  129. {
  130. m_frontend = frontend->timeline();
  131. }
  132. void InspectorTimelineAgent::clearFrontend()
  133. {
  134. ErrorString error;
  135. stop(&error);
  136. m_frontend = 0;
  137. }
  138. void InspectorTimelineAgent::restore()
  139. {
  140. if (m_state->getBoolean(TimelineAgentState::timelineAgentEnabled)) {
  141. m_maxCallStackDepth = m_state->getLong(TimelineAgentState::timelineMaxCallStackDepth);
  142. ErrorString error;
  143. bool includeDomCounters = m_state->getBoolean(TimelineAgentState::includeDomCounters);
  144. bool includeNativeMemoryStatistics = m_state->getBoolean(TimelineAgentState::includeNativeMemoryStatistics);
  145. start(&error, &m_maxCallStackDepth, &includeDomCounters, &includeNativeMemoryStatistics);
  146. }
  147. }
  148. void InspectorTimelineAgent::start(ErrorString*, const int* maxCallStackDepth, const bool* includeDomCounters, const bool* includeNativeMemoryStatistics)
  149. {
  150. if (!m_frontend)
  151. return;
  152. if (maxCallStackDepth && *maxCallStackDepth > 0)
  153. m_maxCallStackDepth = *maxCallStackDepth;
  154. else
  155. m_maxCallStackDepth = 5;
  156. m_state->setLong(TimelineAgentState::timelineMaxCallStackDepth, m_maxCallStackDepth);
  157. m_state->setBoolean(TimelineAgentState::includeDomCounters, includeDomCounters && *includeDomCounters);
  158. m_state->setBoolean(TimelineAgentState::includeNativeMemoryStatistics, includeNativeMemoryStatistics && *includeNativeMemoryStatistics);
  159. m_timeConverter.reset();
  160. m_instrumentingAgents->setInspectorTimelineAgent(this);
  161. ScriptGCEvent::addEventListener(this);
  162. m_state->setBoolean(TimelineAgentState::timelineAgentEnabled, true);
  163. if (m_client && m_pageAgent)
  164. m_traceEventProcessor = adoptRef(new TimelineTraceEventProcessor(m_weakFactory.createWeakPtr(), m_client));
  165. }
  166. void InspectorTimelineAgent::stop(ErrorString*)
  167. {
  168. if (!m_state->getBoolean(TimelineAgentState::timelineAgentEnabled))
  169. return;
  170. m_traceEventProcessor->shutdown();
  171. m_traceEventProcessor.clear();
  172. m_weakFactory.revokeAll();
  173. m_instrumentingAgents->setInspectorTimelineAgent(0);
  174. ScriptGCEvent::removeEventListener(this);
  175. clearRecordStack();
  176. m_gcEvents.clear();
  177. m_state->setBoolean(TimelineAgentState::timelineAgentEnabled, false);
  178. }
  179. void InspectorTimelineAgent::canMonitorMainThread(ErrorString*, bool* result)
  180. {
  181. *result = m_client && m_client->canMonitorMainThread();
  182. }
  183. void InspectorTimelineAgent::supportsFrameInstrumentation(ErrorString*, bool* result)
  184. {
  185. *result = m_client && m_client->supportsFrameInstrumentation();
  186. }
  187. void InspectorTimelineAgent::didBeginFrame()
  188. {
  189. m_pendingFrameRecord = TimelineRecordFactory::createGenericRecord(timestamp(), 0);
  190. }
  191. void InspectorTimelineAgent::didCancelFrame()
  192. {
  193. m_pendingFrameRecord.clear();
  194. }
  195. void InspectorTimelineAgent::willCallFunction(const String& scriptName, int scriptLine, Frame* frame)
  196. {
  197. pushCurrentRecord(TimelineRecordFactory::createFunctionCallData(scriptName, scriptLine), TimelineRecordType::FunctionCall, true, frame);
  198. }
  199. void InspectorTimelineAgent::didCallFunction()
  200. {
  201. didCompleteCurrentRecord(TimelineRecordType::FunctionCall);
  202. }
  203. void InspectorTimelineAgent::willDispatchEvent(const Event& event, Frame* frame)
  204. {
  205. pushCurrentRecord(TimelineRecordFactory::createEventDispatchData(event), TimelineRecordType::EventDispatch, false, frame);
  206. }
  207. void InspectorTimelineAgent::didDispatchEvent()
  208. {
  209. didCompleteCurrentRecord(TimelineRecordType::EventDispatch);
  210. }
  211. void InspectorTimelineAgent::didInvalidateLayout(Frame* frame)
  212. {
  213. appendRecord(InspectorObject::create(), TimelineRecordType::InvalidateLayout, true, frame);
  214. }
  215. void InspectorTimelineAgent::willLayout(Frame* frame)
  216. {
  217. RenderObject* root = frame->view()->layoutRoot();
  218. bool partialLayout = !!root;
  219. if (!partialLayout)
  220. root = frame->contentRenderer();
  221. unsigned dirtyObjects = 0;
  222. unsigned totalObjects = 0;
  223. for (RenderObject* o = root; o; o = o->nextInPreOrder(root)) {
  224. ++totalObjects;
  225. if (o->needsLayout())
  226. ++dirtyObjects;
  227. }
  228. pushCurrentRecord(TimelineRecordFactory::createLayoutData(dirtyObjects, totalObjects, partialLayout), TimelineRecordType::Layout, true, frame);
  229. }
  230. void InspectorTimelineAgent::didLayout(RenderObject* root)
  231. {
  232. if (m_recordStack.isEmpty())
  233. return;
  234. TimelineRecordEntry& entry = m_recordStack.last();
  235. ASSERT(entry.type == TimelineRecordType::Layout);
  236. Vector<FloatQuad> quads;
  237. root->absoluteQuads(quads);
  238. if (quads.size() >= 1)
  239. TimelineRecordFactory::appendLayoutRoot(entry.data.get(), quads[0]);
  240. else
  241. ASSERT_NOT_REACHED();
  242. didCompleteCurrentRecord(TimelineRecordType::Layout);
  243. }
  244. void InspectorTimelineAgent::didScheduleStyleRecalculation(Frame* frame)
  245. {
  246. appendRecord(InspectorObject::create(), TimelineRecordType::ScheduleStyleRecalculation, true, frame);
  247. }
  248. void InspectorTimelineAgent::willRecalculateStyle(Frame* frame)
  249. {
  250. pushCurrentRecord(InspectorObject::create(), TimelineRecordType::RecalculateStyles, true, frame);
  251. }
  252. void InspectorTimelineAgent::didRecalculateStyle()
  253. {
  254. didCompleteCurrentRecord(TimelineRecordType::RecalculateStyles);
  255. }
  256. void InspectorTimelineAgent::willPaint(Frame* frame)
  257. {
  258. pushCurrentRecord(InspectorObject::create(), TimelineRecordType::Paint, true, frame, true);
  259. }
  260. void InspectorTimelineAgent::didPaint(RenderObject* renderer, const LayoutRect& clipRect)
  261. {
  262. TimelineRecordEntry& entry = m_recordStack.last();
  263. ASSERT(entry.type == TimelineRecordType::Paint);
  264. FloatQuad quad;
  265. localToPageQuad(*renderer, clipRect, &quad);
  266. entry.data = TimelineRecordFactory::createPaintData(quad);
  267. didCompleteCurrentRecord(TimelineRecordType::Paint);
  268. }
  269. void InspectorTimelineAgent::willScroll(Frame* frame)
  270. {
  271. pushCurrentRecord(InspectorObject::create(), TimelineRecordType::ScrollLayer, false, frame);
  272. }
  273. void InspectorTimelineAgent::didScroll()
  274. {
  275. didCompleteCurrentRecord(TimelineRecordType::ScrollLayer);
  276. }
  277. void InspectorTimelineAgent::willDecodeImage(const String& imageType)
  278. {
  279. pushCurrentRecord(TimelineRecordFactory::createDecodeImageData(imageType), TimelineRecordType::DecodeImage, true, 0);
  280. }
  281. void InspectorTimelineAgent::didDecodeImage()
  282. {
  283. didCompleteCurrentRecord(TimelineRecordType::DecodeImage);
  284. }
  285. void InspectorTimelineAgent::willResizeImage(bool shouldCache)
  286. {
  287. pushCurrentRecord(TimelineRecordFactory::createResizeImageData(shouldCache), TimelineRecordType::ResizeImage, true, 0);
  288. }
  289. void InspectorTimelineAgent::didResizeImage()
  290. {
  291. didCompleteCurrentRecord(TimelineRecordType::ResizeImage);
  292. }
  293. void InspectorTimelineAgent::willComposite()
  294. {
  295. pushCurrentRecord(InspectorObject::create(), TimelineRecordType::CompositeLayers, false, 0);
  296. }
  297. void InspectorTimelineAgent::didComposite()
  298. {
  299. didCompleteCurrentRecord(TimelineRecordType::CompositeLayers);
  300. }
  301. void InspectorTimelineAgent::willWriteHTML(unsigned startLine, Frame* frame)
  302. {
  303. pushCurrentRecord(TimelineRecordFactory::createParseHTMLData(startLine), TimelineRecordType::ParseHTML, true, frame);
  304. }
  305. void InspectorTimelineAgent::didWriteHTML(unsigned endLine)
  306. {
  307. if (!m_recordStack.isEmpty()) {
  308. TimelineRecordEntry entry = m_recordStack.last();
  309. entry.data->setNumber("endLine", endLine);
  310. didCompleteCurrentRecord(TimelineRecordType::ParseHTML);
  311. }
  312. }
  313. void InspectorTimelineAgent::didInstallTimer(int timerId, int timeout, bool singleShot, Frame* frame)
  314. {
  315. appendRecord(TimelineRecordFactory::createTimerInstallData(timerId, timeout, singleShot), TimelineRecordType::TimerInstall, true, frame);
  316. }
  317. void InspectorTimelineAgent::didRemoveTimer(int timerId, Frame* frame)
  318. {
  319. appendRecord(TimelineRecordFactory::createGenericTimerData(timerId), TimelineRecordType::TimerRemove, true, frame);
  320. }
  321. void InspectorTimelineAgent::willFireTimer(int timerId, Frame* frame)
  322. {
  323. pushCurrentRecord(TimelineRecordFactory::createGenericTimerData(timerId), TimelineRecordType::TimerFire, false, frame);
  324. }
  325. void InspectorTimelineAgent::didFireTimer()
  326. {
  327. didCompleteCurrentRecord(TimelineRecordType::TimerFire);
  328. }
  329. void InspectorTimelineAgent::willDispatchXHRReadyStateChangeEvent(const String& url, int readyState, Frame* frame)
  330. {
  331. pushCurrentRecord(TimelineRecordFactory::createXHRReadyStateChangeData(url, readyState), TimelineRecordType::XHRReadyStateChange, false, frame);
  332. }
  333. void InspectorTimelineAgent::didDispatchXHRReadyStateChangeEvent()
  334. {
  335. didCompleteCurrentRecord(TimelineRecordType::XHRReadyStateChange);
  336. }
  337. void InspectorTimelineAgent::willDispatchXHRLoadEvent(const String& url, Frame* frame)
  338. {
  339. pushCurrentRecord(TimelineRecordFactory::createXHRLoadData(url), TimelineRecordType::XHRLoad, true, frame);
  340. }
  341. void InspectorTimelineAgent::didDispatchXHRLoadEvent()
  342. {
  343. didCompleteCurrentRecord(TimelineRecordType::XHRLoad);
  344. }
  345. void InspectorTimelineAgent::willEvaluateScript(const String& url, int lineNumber, Frame* frame)
  346. {
  347. pushCurrentRecord(TimelineRecordFactory::createEvaluateScriptData(url, lineNumber), TimelineRecordType::EvaluateScript, true, frame);
  348. }
  349. void InspectorTimelineAgent::didEvaluateScript()
  350. {
  351. didCompleteCurrentRecord(TimelineRecordType::EvaluateScript);
  352. }
  353. void InspectorTimelineAgent::didScheduleResourceRequest(const String& url, Frame* frame)
  354. {
  355. appendRecord(TimelineRecordFactory::createScheduleResourceRequestData(url), TimelineRecordType::ScheduleResourceRequest, true, frame);
  356. }
  357. void InspectorTimelineAgent::willSendResourceRequest(unsigned long identifier, const ResourceRequest& request, Frame* frame)
  358. {
  359. String requestId = IdentifiersFactory::requestId(identifier);
  360. appendRecord(TimelineRecordFactory::createResourceSendRequestData(requestId, request), TimelineRecordType::ResourceSendRequest, true, frame);
  361. }
  362. void InspectorTimelineAgent::willReceiveResourceData(unsigned long identifier, Frame* frame, int length)
  363. {
  364. String requestId = IdentifiersFactory::requestId(identifier);
  365. pushCurrentRecord(TimelineRecordFactory::createReceiveResourceData(requestId, length), TimelineRecordType::ResourceReceivedData, false, frame);
  366. }
  367. void InspectorTimelineAgent::didReceiveResourceData()
  368. {
  369. didCompleteCurrentRecord(TimelineRecordType::ResourceReceivedData);
  370. }
  371. void InspectorTimelineAgent::willReceiveResourceResponse(unsigned long identifier, const ResourceResponse& response, Frame* frame)
  372. {
  373. String requestId = IdentifiersFactory::requestId(identifier);
  374. pushCurrentRecord(TimelineRecordFactory::createResourceReceiveResponseData(requestId, response), TimelineRecordType::ResourceReceiveResponse, false, frame);
  375. }
  376. void InspectorTimelineAgent::didReceiveResourceResponse()
  377. {
  378. didCompleteCurrentRecord(TimelineRecordType::ResourceReceiveResponse);
  379. }
  380. void InspectorTimelineAgent::didFinishLoadingResource(unsigned long identifier, bool didFail, double finishTime, Frame* frame)
  381. {
  382. appendRecord(TimelineRecordFactory::createResourceFinishData(IdentifiersFactory::requestId(identifier), didFail, finishTime * 1000), TimelineRecordType::ResourceFinish, false, frame);
  383. }
  384. void InspectorTimelineAgent::didTimeStamp(Frame* frame, const String& message)
  385. {
  386. appendRecord(TimelineRecordFactory::createTimeStampData(message), TimelineRecordType::TimeStamp, true, frame);
  387. }
  388. void InspectorTimelineAgent::time(Frame* frame, const String& message)
  389. {
  390. appendRecord(TimelineRecordFactory::createTimeStampData(message), TimelineRecordType::Time, true, frame);
  391. }
  392. void InspectorTimelineAgent::timeEnd(Frame* frame, const String& message)
  393. {
  394. appendRecord(TimelineRecordFactory::createTimeStampData(message), TimelineRecordType::TimeEnd, true, frame);
  395. }
  396. void InspectorTimelineAgent::didMarkDOMContentEvent(Frame* frame)
  397. {
  398. bool isMainFrame = frame && m_pageAgent && (frame == m_pageAgent->mainFrame());
  399. appendRecord(TimelineRecordFactory::createMarkData(isMainFrame), TimelineRecordType::MarkDOMContent, false, frame);
  400. }
  401. void InspectorTimelineAgent::didMarkLoadEvent(Frame* frame)
  402. {
  403. bool isMainFrame = frame && m_pageAgent && (frame == m_pageAgent->mainFrame());
  404. appendRecord(TimelineRecordFactory::createMarkData(isMainFrame), TimelineRecordType::MarkLoad, false, frame);
  405. }
  406. void InspectorTimelineAgent::didCommitLoad()
  407. {
  408. clearRecordStack();
  409. }
  410. void InspectorTimelineAgent::didRequestAnimationFrame(int callbackId, Frame* frame)
  411. {
  412. appendRecord(TimelineRecordFactory::createAnimationFrameData(callbackId), TimelineRecordType::RequestAnimationFrame, true, frame);
  413. }
  414. void InspectorTimelineAgent::didCancelAnimationFrame(int callbackId, Frame* frame)
  415. {
  416. appendRecord(TimelineRecordFactory::createAnimationFrameData(callbackId), TimelineRecordType::CancelAnimationFrame, true, frame);
  417. }
  418. void InspectorTimelineAgent::willFireAnimationFrame(int callbackId, Frame* frame)
  419. {
  420. pushCurrentRecord(TimelineRecordFactory::createAnimationFrameData(callbackId), TimelineRecordType::FireAnimationFrame, false, frame);
  421. }
  422. void InspectorTimelineAgent::didFireAnimationFrame()
  423. {
  424. didCompleteCurrentRecord(TimelineRecordType::FireAnimationFrame);
  425. }
  426. void InspectorTimelineAgent::willProcessTask()
  427. {
  428. pushCurrentRecord(InspectorObject::create(), TimelineRecordType::Program, false, 0);
  429. }
  430. void InspectorTimelineAgent::didProcessTask()
  431. {
  432. didCompleteCurrentRecord(TimelineRecordType::Program);
  433. }
  434. #if ENABLE(WEB_SOCKETS)
  435. void InspectorTimelineAgent::didCreateWebSocket(unsigned long identifier, const KURL& url, const String& protocol, Frame* frame)
  436. {
  437. appendRecord(TimelineRecordFactory::createWebSocketCreateData(identifier, url, protocol), TimelineRecordType::WebSocketCreate, true, frame);
  438. }
  439. void InspectorTimelineAgent::willSendWebSocketHandshakeRequest(unsigned long identifier, Frame* frame)
  440. {
  441. appendRecord(TimelineRecordFactory::createGenericWebSocketData(identifier), TimelineRecordType::WebSocketSendHandshakeRequest, true, frame);
  442. }
  443. void InspectorTimelineAgent::didReceiveWebSocketHandshakeResponse(unsigned long identifier, Frame* frame)
  444. {
  445. appendRecord(TimelineRecordFactory::createGenericWebSocketData(identifier), TimelineRecordType::WebSocketReceiveHandshakeResponse, false, frame);
  446. }
  447. void InspectorTimelineAgent::didDestroyWebSocket(unsigned long identifier, Frame* frame)
  448. {
  449. appendRecord(TimelineRecordFactory::createGenericWebSocketData(identifier), TimelineRecordType::WebSocketDestroy, true, frame);
  450. }
  451. #endif // ENABLE(WEB_SOCKETS)
  452. void InspectorTimelineAgent::addRecordToTimeline(PassRefPtr<InspectorObject> record, const String& type)
  453. {
  454. commitFrameRecord();
  455. innerAddRecordToTimeline(record, type);
  456. }
  457. void InspectorTimelineAgent::innerAddRecordToTimeline(PassRefPtr<InspectorObject> prpRecord, const String& type)
  458. {
  459. prpRecord->setString("type", type);
  460. RefPtr<TypeBuilder::Timeline::TimelineEvent> record = TypeBuilder::Timeline::TimelineEvent::runtimeCast(prpRecord);
  461. if (type == TimelineRecordType::Program)
  462. setNativeHeapStatistics(record.get());
  463. else
  464. setDOMCounters(record.get());
  465. if (m_recordStack.isEmpty())
  466. sendEvent(record.release());
  467. else {
  468. TimelineRecordEntry parent = m_recordStack.last();
  469. parent.children->pushObject(record.release());
  470. }
  471. }
  472. static size_t getUsedHeapSize()
  473. {
  474. HeapInfo info;
  475. ScriptGCEvent::getHeapSize(info);
  476. return info.usedJSHeapSize;
  477. }
  478. void InspectorTimelineAgent::setDOMCounters(TypeBuilder::Timeline::TimelineEvent* record)
  479. {
  480. record->setUsedHeapSize(getUsedHeapSize());
  481. if (m_state->getBoolean(TimelineAgentState::includeDomCounters)) {
  482. int documentCount = 0;
  483. int nodeCount = 0;
  484. if (m_inspectorType == PageInspector) {
  485. documentCount = InspectorCounters::counterValue(InspectorCounters::DocumentCounter);
  486. nodeCount = InspectorCounters::counterValue(InspectorCounters::NodeCounter);
  487. }
  488. int listenerCount = ThreadLocalInspectorCounters::current().counterValue(ThreadLocalInspectorCounters::JSEventListenerCounter);
  489. RefPtr<TypeBuilder::Timeline::DOMCounters> counters = TypeBuilder::Timeline::DOMCounters::create()
  490. .setDocuments(documentCount)
  491. .setNodes(nodeCount)
  492. .setJsEventListeners(listenerCount);
  493. record->setCounters(counters.release());
  494. }
  495. }
  496. // FIXME: This entire function can probably be removed, since it's a no-op.
  497. void InspectorTimelineAgent::setNativeHeapStatistics(TypeBuilder::Timeline::TimelineEvent* record)
  498. {
  499. if (!m_memoryAgent)
  500. return;
  501. if (!m_state->getBoolean(TimelineAgentState::includeNativeMemoryStatistics))
  502. return;
  503. RefPtr<InspectorObject> stats = InspectorObject::create();
  504. stats->setNumber("PrivateBytes", 0);
  505. record->setNativeHeapStatistics(stats.release());
  506. }
  507. void InspectorTimelineAgent::setFrameIdentifier(InspectorObject* record, Frame* frame)
  508. {
  509. if (!frame || !m_pageAgent)
  510. return;
  511. String frameId;
  512. if (frame && m_pageAgent)
  513. frameId = m_pageAgent->frameId(frame);
  514. record->setString("frameId", frameId);
  515. }
  516. void InspectorTimelineAgent::didCompleteCurrentRecord(const String& type)
  517. {
  518. // An empty stack could merely mean that the timeline agent was turned on in the middle of
  519. // an event. Don't treat as an error.
  520. if (!m_recordStack.isEmpty()) {
  521. if (m_platformInstrumentationClientInstalledAtStackDepth == m_recordStack.size()) {
  522. m_platformInstrumentationClientInstalledAtStackDepth = 0;
  523. PlatformInstrumentation::setClient(0);
  524. }
  525. pushGCEventRecords();
  526. TimelineRecordEntry entry = m_recordStack.last();
  527. m_recordStack.removeLast();
  528. ASSERT(entry.type == type);
  529. entry.record->setObject("data", entry.data);
  530. entry.record->setArray("children", entry.children);
  531. entry.record->setNumber("endTime", timestamp());
  532. size_t usedHeapSizeDelta = getUsedHeapSize() - entry.usedHeapSizeAtStart;
  533. if (usedHeapSizeDelta)
  534. entry.record->setNumber("usedHeapSizeDelta", usedHeapSizeDelta);
  535. addRecordToTimeline(entry.record, type);
  536. }
  537. }
  538. InspectorTimelineAgent::InspectorTimelineAgent(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorMemoryAgent* memoryAgent, InspectorCompositeState* state, InspectorType type, InspectorClient* client)
  539. : InspectorBaseAgent<InspectorTimelineAgent>("Timeline", instrumentingAgents, state)
  540. , m_pageAgent(pageAgent)
  541. , m_memoryAgent(memoryAgent)
  542. , m_frontend(0)
  543. , m_id(1)
  544. , m_maxCallStackDepth(5)
  545. , m_platformInstrumentationClientInstalledAtStackDepth(0)
  546. , m_inspectorType(type)
  547. , m_client(client)
  548. , m_weakFactory(this)
  549. {
  550. }
  551. void InspectorTimelineAgent::appendRecord(PassRefPtr<InspectorObject> data, const String& type, bool captureCallStack, Frame* frame)
  552. {
  553. pushGCEventRecords();
  554. RefPtr<InspectorObject> record = TimelineRecordFactory::createGenericRecord(timestamp(), captureCallStack ? m_maxCallStackDepth : 0);
  555. record->setObject("data", data);
  556. setFrameIdentifier(record.get(), frame);
  557. addRecordToTimeline(record.release(), type);
  558. }
  559. void InspectorTimelineAgent::sendEvent(PassRefPtr<InspectorObject> event)
  560. {
  561. // FIXME: runtimeCast is a hack. We do it because we can't build TimelineEvent directly now.
  562. RefPtr<TypeBuilder::Timeline::TimelineEvent> recordChecked = TypeBuilder::Timeline::TimelineEvent::runtimeCast(event);
  563. m_frontend->eventRecorded(recordChecked.release());
  564. }
  565. void InspectorTimelineAgent::pushCurrentRecord(PassRefPtr<InspectorObject> data, const String& type, bool captureCallStack, Frame* frame, bool hasLowLevelDetails)
  566. {
  567. pushGCEventRecords();
  568. commitFrameRecord();
  569. RefPtr<InspectorObject> record = TimelineRecordFactory::createGenericRecord(timestamp(), captureCallStack ? m_maxCallStackDepth : 0);
  570. setFrameIdentifier(record.get(), frame);
  571. m_recordStack.append(TimelineRecordEntry(record.release(), data, InspectorArray::create(), type, getUsedHeapSize()));
  572. if (hasLowLevelDetails && !m_platformInstrumentationClientInstalledAtStackDepth && !PlatformInstrumentation::hasClient()) {
  573. m_platformInstrumentationClientInstalledAtStackDepth = m_recordStack.size();
  574. PlatformInstrumentation::setClient(this);
  575. }
  576. }
  577. void InspectorTimelineAgent::commitFrameRecord()
  578. {
  579. if (!m_pendingFrameRecord)
  580. return;
  581. m_pendingFrameRecord->setObject("data", InspectorObject::create());
  582. innerAddRecordToTimeline(m_pendingFrameRecord.release(), TimelineRecordType::BeginFrame);
  583. }
  584. void InspectorTimelineAgent::clearRecordStack()
  585. {
  586. if (m_platformInstrumentationClientInstalledAtStackDepth) {
  587. m_platformInstrumentationClientInstalledAtStackDepth = 0;
  588. PlatformInstrumentation::setClient(0);
  589. }
  590. m_pendingFrameRecord.clear();
  591. m_recordStack.clear();
  592. m_id++;
  593. }
  594. void InspectorTimelineAgent::localToPageQuad(const RenderObject& renderer, const LayoutRect& rect, FloatQuad* quad)
  595. {
  596. Frame* frame = renderer.frame();
  597. FrameView* view = frame->view();
  598. FloatQuad absolute = renderer.localToAbsoluteQuad(FloatQuad(rect));
  599. quad->setP1(view->contentsToRootView(roundedIntPoint(absolute.p1())));
  600. quad->setP2(view->contentsToRootView(roundedIntPoint(absolute.p2())));
  601. quad->setP3(view->contentsToRootView(roundedIntPoint(absolute.p3())));
  602. quad->setP4(view->contentsToRootView(roundedIntPoint(absolute.p4())));
  603. }
  604. double InspectorTimelineAgent::timestamp()
  605. {
  606. return m_timeConverter.fromMonotonicallyIncreasingTime(WTF::monotonicallyIncreasingTime());
  607. }
  608. Page* InspectorTimelineAgent::page()
  609. {
  610. return m_pageAgent ? m_pageAgent->page() : 0;
  611. }
  612. } // namespace WebCore
  613. #endif // ENABLE(INSPECTOR)