TimelineTraceEventProcessor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 "TimelineTraceEventProcessor.h"
  33. #include "InspectorClient.h"
  34. #include "InspectorInstrumentation.h"
  35. #include "TimelineRecordFactory.h"
  36. #include <wtf/CurrentTime.h>
  37. #include <wtf/MainThread.h>
  38. #include <wtf/ThreadSpecific.h>
  39. #include <wtf/Vector.h>
  40. namespace WebCore {
  41. namespace {
  42. class TraceEventDispatcher {
  43. WTF_MAKE_NONCOPYABLE(TraceEventDispatcher);
  44. public:
  45. static TraceEventDispatcher* instance()
  46. {
  47. DEFINE_STATIC_LOCAL(TraceEventDispatcher, instance, ());
  48. return &instance;
  49. }
  50. void addProcessor(TimelineTraceEventProcessor* processor, InspectorClient* client)
  51. {
  52. MutexLocker locker(m_mutex);
  53. m_processors.append(processor);
  54. if (m_processors.size() == 1)
  55. client->setTraceEventCallback(dispatchEventOnAnyThread);
  56. }
  57. void removeProcessor(TimelineTraceEventProcessor* processor, InspectorClient* client)
  58. {
  59. MutexLocker locker(m_mutex);
  60. size_t index = m_processors.find(processor);
  61. if (index == notFound) {
  62. ASSERT_NOT_REACHED();
  63. return;
  64. }
  65. m_processors.remove(index);
  66. if (m_processors.isEmpty())
  67. client->setTraceEventCallback(0);
  68. }
  69. private:
  70. TraceEventDispatcher() { }
  71. static void dispatchEventOnAnyThread(char phase, const unsigned char*, const char* name, unsigned long long id,
  72. int numArgs, const char* const* argNames, const unsigned char* argTypes, const unsigned long long* argValues,
  73. unsigned char flags)
  74. {
  75. TraceEventDispatcher* self = instance();
  76. Vector<RefPtr<TimelineTraceEventProcessor> > processors;
  77. {
  78. MutexLocker locker(self->m_mutex);
  79. processors = self->m_processors;
  80. }
  81. for (int i = 0, size = processors.size(); i < size; ++i) {
  82. processors[i]->processEventOnAnyThread(static_cast<TimelineTraceEventProcessor::TraceEventPhase>(phase),
  83. name, id, numArgs, argNames, argTypes, argValues, flags);
  84. }
  85. }
  86. Mutex m_mutex;
  87. Vector<RefPtr<TimelineTraceEventProcessor> > m_processors;
  88. };
  89. } // namespce
  90. TimelineRecordStack::TimelineRecordStack(WeakPtr<InspectorTimelineAgent> timelineAgent)
  91. : m_timelineAgent(timelineAgent)
  92. {
  93. }
  94. void TimelineRecordStack::addScopedRecord(PassRefPtr<InspectorObject> record)
  95. {
  96. m_stack.append(Entry(record));
  97. }
  98. void TimelineRecordStack::closeScopedRecord(double endTime)
  99. {
  100. if (m_stack.isEmpty())
  101. return;
  102. Entry last = m_stack.last();
  103. m_stack.removeLast();
  104. last.record->setNumber("endTime", endTime);
  105. if (last.children->length())
  106. last.record->setArray("children", last.children);
  107. addInstantRecord(last.record);
  108. }
  109. void TimelineRecordStack::addInstantRecord(PassRefPtr<InspectorObject> record)
  110. {
  111. if (m_stack.isEmpty())
  112. send(record);
  113. else
  114. m_stack.last().children->pushObject(record);
  115. }
  116. #ifndef NDEBUG
  117. bool TimelineRecordStack::isOpenRecordOfType(const String& type)
  118. {
  119. String lastRecordType;
  120. return m_stack.isEmpty() || (m_stack.last().record->getString("type", &lastRecordType) && type == lastRecordType);
  121. }
  122. #endif
  123. void TimelineRecordStack::send(PassRefPtr<InspectorObject> record)
  124. {
  125. InspectorTimelineAgent* timelineAgent = m_timelineAgent.get();
  126. if (!timelineAgent)
  127. return;
  128. timelineAgent->sendEvent(record);
  129. }
  130. TimelineTraceEventProcessor::TimelineTraceEventProcessor(WeakPtr<InspectorTimelineAgent> timelineAgent, InspectorClient *client)
  131. : m_timelineAgent(timelineAgent)
  132. , m_timeConverter(timelineAgent.get()->timeConverter())
  133. , m_inspectorClient(client)
  134. , m_pageId(reinterpret_cast<unsigned long long>(m_timelineAgent.get()->page()))
  135. , m_layerId(0)
  136. {
  137. registerHandler(InstrumentationEvents::BeginFrame, TracePhaseInstant, &TimelineTraceEventProcessor::onBeginFrame);
  138. registerHandler(InstrumentationEvents::PaintLayer, TracePhaseBegin, &TimelineTraceEventProcessor::onPaintLayerBegin);
  139. registerHandler(InstrumentationEvents::PaintLayer, TracePhaseEnd, &TimelineTraceEventProcessor::onPaintLayerEnd);
  140. registerHandler(InstrumentationEvents::RasterTask, TracePhaseBegin, &TimelineTraceEventProcessor::onRasterTaskBegin);
  141. registerHandler(InstrumentationEvents::RasterTask, TracePhaseEnd, &TimelineTraceEventProcessor::onRasterTaskEnd);
  142. registerHandler(InstrumentationEvents::Layer, TracePhaseDeleteObject, &TimelineTraceEventProcessor::onLayerDeleted);
  143. registerHandler(InstrumentationEvents::Paint, TracePhaseInstant, &TimelineTraceEventProcessor::onPaint);
  144. registerHandler(PlatformInstrumentation::ImageDecodeEvent, TracePhaseBegin, &TimelineTraceEventProcessor::onImageDecodeBegin);
  145. registerHandler(PlatformInstrumentation::ImageDecodeEvent, TracePhaseEnd, &TimelineTraceEventProcessor::onImageDecodeEnd);
  146. TraceEventDispatcher::instance()->addProcessor(this, m_inspectorClient);
  147. }
  148. TimelineTraceEventProcessor::~TimelineTraceEventProcessor()
  149. {
  150. }
  151. void TimelineTraceEventProcessor::registerHandler(const char* name, TraceEventPhase phase, TraceEventHandler handler)
  152. {
  153. m_handlersByType.set(std::make_pair(name, phase), handler);
  154. }
  155. void TimelineTraceEventProcessor::shutdown()
  156. {
  157. TraceEventDispatcher::instance()->removeProcessor(this, m_inspectorClient);
  158. }
  159. size_t TimelineTraceEventProcessor::TraceEvent::findParameter(const char* name) const
  160. {
  161. for (int i = 0; i < m_argumentCount; ++i) {
  162. if (!strcmp(name, m_argumentNames[i]))
  163. return i;
  164. }
  165. return notFound;
  166. }
  167. const TimelineTraceEventProcessor::TraceValueUnion& TimelineTraceEventProcessor::TraceEvent::parameter(const char* name, TraceValueTypes expectedType) const
  168. {
  169. static TraceValueUnion missingValue;
  170. size_t index = findParameter(name);
  171. if (index == notFound || m_argumentTypes[index] != expectedType) {
  172. ASSERT_NOT_REACHED();
  173. return missingValue;
  174. }
  175. return *reinterpret_cast<const TraceValueUnion*>(m_argumentValues + index);
  176. }
  177. void TimelineTraceEventProcessor::processEventOnAnyThread(TraceEventPhase phase, const char* name, unsigned long long id,
  178. int numArgs, const char* const* argNames, const unsigned char* argTypes, const unsigned long long* argValues,
  179. unsigned char)
  180. {
  181. HandlersMap::iterator it = m_handlersByType.find(std::make_pair(name, phase));
  182. if (it == m_handlersByType.end())
  183. return;
  184. TraceEvent event(WTF::monotonicallyIncreasingTime(), phase, name, id, currentThread(), numArgs, argNames, argTypes, argValues);
  185. if (!isMainThread()) {
  186. MutexLocker locker(m_backgroundEventsMutex);
  187. m_backgroundEvents.append(event);
  188. return;
  189. }
  190. (this->*(it->value))(event);
  191. }
  192. void TimelineTraceEventProcessor::onBeginFrame(const TraceEvent&)
  193. {
  194. processBackgroundEvents();
  195. }
  196. void TimelineTraceEventProcessor::onPaintLayerBegin(const TraceEvent& event)
  197. {
  198. m_layerId = event.asUInt(InstrumentationEventArguments::LayerId);
  199. ASSERT(m_layerId);
  200. }
  201. void TimelineTraceEventProcessor::onPaintLayerEnd(const TraceEvent&)
  202. {
  203. m_layerId = 0;
  204. }
  205. void TimelineTraceEventProcessor::onRasterTaskBegin(const TraceEvent& event)
  206. {
  207. unsigned long long layerId = event.asUInt(InstrumentationEventArguments::LayerId);
  208. if (!m_knownLayers.contains(layerId))
  209. return;
  210. TimelineThreadState& state = threadState(event.threadIdentifier());
  211. ASSERT(!state.inRasterizeEvent);
  212. state.inRasterizeEvent = true;
  213. RefPtr<InspectorObject> record = createRecord(event, TimelineRecordType::Rasterize);
  214. state.recordStack.addScopedRecord(record.release());
  215. }
  216. void TimelineTraceEventProcessor::onRasterTaskEnd(const TraceEvent& event)
  217. {
  218. TimelineThreadState& state = threadState(event.threadIdentifier());
  219. if (!state.inRasterizeEvent)
  220. return;
  221. ASSERT(state.recordStack.isOpenRecordOfType(TimelineRecordType::Rasterize));
  222. state.recordStack.closeScopedRecord(m_timeConverter.fromMonotonicallyIncreasingTime(event.timestamp()));
  223. state.inRasterizeEvent = false;
  224. }
  225. void TimelineTraceEventProcessor::onImageDecodeBegin(const TraceEvent& event)
  226. {
  227. TimelineThreadState& state = threadState(event.threadIdentifier());
  228. if (!state.inRasterizeEvent)
  229. return;
  230. state.recordStack.addScopedRecord(createRecord(event, TimelineRecordType::DecodeImage));
  231. }
  232. void TimelineTraceEventProcessor::onImageDecodeEnd(const TraceEvent& event)
  233. {
  234. TimelineThreadState& state = threadState(event.threadIdentifier());
  235. if (!state.inRasterizeEvent)
  236. return;
  237. ASSERT(state.recordStack.isOpenRecordOfType(TimelineRecordType::DecodeImage));
  238. state.recordStack.closeScopedRecord(m_timeConverter.fromMonotonicallyIncreasingTime(event.timestamp()));
  239. }
  240. void TimelineTraceEventProcessor::onLayerDeleted(const TraceEvent& event)
  241. {
  242. unsigned long long id = event.id();
  243. ASSERT(id);
  244. processBackgroundEvents();
  245. m_knownLayers.remove(id);
  246. }
  247. void TimelineTraceEventProcessor::onPaint(const TraceEvent& event)
  248. {
  249. if (!m_layerId)
  250. return;
  251. unsigned long long pageId = event.asUInt(InstrumentationEventArguments::PageId);
  252. if (pageId == m_pageId)
  253. m_knownLayers.add(m_layerId);
  254. }
  255. PassRefPtr<InspectorObject> TimelineTraceEventProcessor::createRecord(const TraceEvent& event, const String& recordType, PassRefPtr<InspectorObject> data)
  256. {
  257. double startTime = m_timeConverter.fromMonotonicallyIncreasingTime(event.timestamp());
  258. RefPtr<InspectorObject> record = TimelineRecordFactory::createBackgroundRecord(startTime, String::number(event.threadIdentifier()));
  259. record->setString("type", recordType);
  260. record->setObject("data", data ? data : InspectorObject::create());
  261. return record.release();
  262. }
  263. void TimelineTraceEventProcessor::processBackgroundEvents()
  264. {
  265. ASSERT(isMainThread());
  266. Vector<TraceEvent> events;
  267. {
  268. MutexLocker locker(m_backgroundEventsMutex);
  269. events.reserveCapacity(m_backgroundEvents.capacity());
  270. m_backgroundEvents.swap(events);
  271. }
  272. for (size_t i = 0, size = events.size(); i < size; ++i) {
  273. const TraceEvent& event = events[i];
  274. HandlersMap::iterator it = m_handlersByType.find(std::make_pair(event.name(), event.phase()));
  275. ASSERT(it != m_handlersByType.end() && it->value);
  276. (this->*(it->value))(event);
  277. }
  278. }
  279. } // namespace WebCore
  280. #endif // ENABLE(INSPECTOR)