TimelineTraceEventProcessor.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #ifndef TimelineTraceEventProcessor_h
  31. #define TimelineTraceEventProcessor_h
  32. #if ENABLE(INSPECTOR)
  33. #include "InspectorTimelineAgent.h"
  34. #include "InspectorValues.h"
  35. #include <wtf/HashMap.h>
  36. #include <wtf/HashSet.h>
  37. #include <wtf/Noncopyable.h>
  38. #include <wtf/Threading.h>
  39. #include <wtf/Vector.h>
  40. #include <wtf/WeakPtr.h>
  41. #include <wtf/text/WTFString.h>
  42. namespace WebCore {
  43. class InspectorClient;
  44. class InspectorTimelineAgent;
  45. class Page;
  46. class TimelineRecordStack {
  47. private:
  48. struct Entry {
  49. Entry(PassRefPtr<InspectorObject> record)
  50. : record(record)
  51. , children(InspectorArray::create())
  52. {
  53. }
  54. RefPtr<InspectorObject> record;
  55. RefPtr<InspectorArray> children;
  56. };
  57. public:
  58. TimelineRecordStack() { }
  59. TimelineRecordStack(WeakPtr<InspectorTimelineAgent>);
  60. void addScopedRecord(PassRefPtr<InspectorObject> record);
  61. void closeScopedRecord(double endTime);
  62. void addInstantRecord(PassRefPtr<InspectorObject> record);
  63. #ifndef NDEBUG
  64. bool isOpenRecordOfType(const String& type);
  65. #endif
  66. private:
  67. void send(PassRefPtr<InspectorObject>);
  68. WeakPtr<InspectorTimelineAgent> m_timelineAgent;
  69. Vector<Entry> m_stack;
  70. };
  71. class TimelineTraceEventProcessor : public ThreadSafeRefCounted<TimelineTraceEventProcessor> {
  72. public:
  73. // FIXME: re-use definitions in TraceEvent.h once it's promoted to all platforms.
  74. enum TraceEventPhase {
  75. TracePhaseBegin = 'B',
  76. TracePhaseEnd = 'E',
  77. TracePhaseInstant = 'I',
  78. TracePhaseCreateObject = 'N',
  79. TracePhaseDeleteObject = 'D'
  80. };
  81. TimelineTraceEventProcessor(WeakPtr<InspectorTimelineAgent>, InspectorClient*);
  82. ~TimelineTraceEventProcessor();
  83. void shutdown();
  84. void processEventOnAnyThread(TraceEventPhase, const char* name, unsigned long long id,
  85. int numArgs, const char* const* argNames, const unsigned char* argTypes, const unsigned long long* argValues,
  86. unsigned char flags);
  87. private:
  88. // FIXME: use the definition in TraceEvent.h once we expose the latter to all plaforms.
  89. union TraceValueUnion {
  90. bool m_bool;
  91. unsigned long long m_uint;
  92. long long m_int;
  93. double m_double;
  94. const void* m_pointer;
  95. const char* m_string;
  96. };
  97. enum TraceValueTypes {
  98. TypeBool = 1,
  99. TypeUInt = 2,
  100. TypeInt = 3,
  101. TypeDouble = 4,
  102. TypePointer = 5,
  103. TypeString = 6,
  104. TypeCopyString = 7
  105. };
  106. struct TimelineThreadState {
  107. TimelineThreadState() { }
  108. TimelineThreadState(WeakPtr<InspectorTimelineAgent> timelineAgent)
  109. : recordStack(timelineAgent)
  110. , inRasterizeEvent(false)
  111. {
  112. }
  113. TimelineRecordStack recordStack;
  114. bool inRasterizeEvent;
  115. };
  116. class TraceEvent {
  117. public:
  118. TraceEvent()
  119. : m_name(0)
  120. , m_argumentCount(0)
  121. {
  122. }
  123. TraceEvent(double timestamp, TraceEventPhase phase, const char* name, unsigned long long id, ThreadIdentifier threadIdentifier,
  124. int argumentCount, const char* const* argumentNames, const unsigned char* argumentTypes, const unsigned long long* argumentValues)
  125. : m_timestamp(timestamp)
  126. , m_phase(phase)
  127. , m_name(name)
  128. , m_id(id)
  129. , m_threadIdentifier(threadIdentifier)
  130. , m_argumentCount(argumentCount)
  131. {
  132. if (m_argumentCount > MaxArguments) {
  133. ASSERT_NOT_REACHED();
  134. m_argumentCount = MaxArguments;
  135. }
  136. for (int i = 0; i < m_argumentCount; ++i) {
  137. m_argumentNames[i] = argumentNames[i];
  138. m_argumentTypes[i] = argumentTypes[i];
  139. m_argumentValues[i] = argumentValues[i];
  140. }
  141. }
  142. double timestamp() const { return m_timestamp; }
  143. TraceEventPhase phase() const { return m_phase; }
  144. const char* name() const { return m_name; }
  145. unsigned long long id() const { return m_id; }
  146. ThreadIdentifier threadIdentifier() const { return m_threadIdentifier; }
  147. int argumentCount() const { return m_argumentCount; }
  148. bool isNull() const { return !m_name; }
  149. bool asBool(const char* name) const
  150. {
  151. return parameter(name, TypeBool).m_bool;
  152. }
  153. long long asInt(const char* name) const
  154. {
  155. size_t index = findParameter(name);
  156. if (index == notFound || (m_argumentTypes[index] != TypeInt && m_argumentTypes[index] != TypeUInt)) {
  157. ASSERT_NOT_REACHED();
  158. return 0;
  159. }
  160. return reinterpret_cast<const TraceValueUnion*>(m_argumentValues + index)->m_int;
  161. }
  162. unsigned long long asUInt(const char* name) const
  163. {
  164. return asInt(name);
  165. }
  166. double asDouble(const char* name) const
  167. {
  168. return parameter(name, TypeDouble).m_double;
  169. }
  170. const char* asString(const char* name) const
  171. {
  172. return parameter(name, TypeString).m_string;
  173. }
  174. private:
  175. enum { MaxArguments = 2 };
  176. size_t findParameter(const char*) const;
  177. const TraceValueUnion& parameter(const char* name, TraceValueTypes expectedType) const;
  178. double m_timestamp;
  179. TraceEventPhase m_phase;
  180. const char* m_name;
  181. unsigned long long m_id;
  182. ThreadIdentifier m_threadIdentifier;
  183. int m_argumentCount;
  184. const char* m_argumentNames[MaxArguments];
  185. unsigned char m_argumentTypes[MaxArguments];
  186. unsigned long long m_argumentValues[MaxArguments];
  187. };
  188. typedef void (TimelineTraceEventProcessor::*TraceEventHandler)(const TraceEvent&);
  189. TimelineThreadState& threadState(ThreadIdentifier thread)
  190. {
  191. ThreadStateMap::iterator it = m_threadStates.find(thread);
  192. if (it != m_threadStates.end())
  193. return it->value;
  194. return m_threadStates.add(thread, TimelineThreadState(m_timelineAgent)).iterator->value;
  195. }
  196. void processBackgroundEvents();
  197. PassRefPtr<InspectorObject> createRecord(const TraceEvent&, const String& recordType, PassRefPtr<InspectorObject> data = 0);
  198. void registerHandler(const char* name, TraceEventPhase, TraceEventHandler);
  199. void onBeginFrame(const TraceEvent&);
  200. void onPaintLayerBegin(const TraceEvent&);
  201. void onPaintLayerEnd(const TraceEvent&);
  202. void onRasterTaskBegin(const TraceEvent&);
  203. void onRasterTaskEnd(const TraceEvent&);
  204. void onImageDecodeBegin(const TraceEvent&);
  205. void onImageDecodeEnd(const TraceEvent&);
  206. void onLayerDeleted(const TraceEvent&);
  207. void onPaint(const TraceEvent&);
  208. WeakPtr<InspectorTimelineAgent> m_timelineAgent;
  209. TimelineTimeConverter m_timeConverter;
  210. InspectorClient* m_inspectorClient;
  211. unsigned long long m_pageId;
  212. typedef HashMap<std::pair<String, int>, TraceEventHandler> HandlersMap;
  213. HandlersMap m_handlersByType;
  214. Mutex m_backgroundEventsMutex;
  215. Vector<TraceEvent> m_backgroundEvents;
  216. typedef HashMap<ThreadIdentifier, TimelineThreadState> ThreadStateMap;
  217. ThreadStateMap m_threadStates;
  218. HashSet<unsigned long long> m_knownLayers;
  219. unsigned long long m_layerId;
  220. };
  221. } // namespace WebCore
  222. #endif // ENABLE(INSPECTOR)
  223. #endif // !defined(TimelineTraceEventProcessor_h)