|
Line 0
a/Source/WebCore/inspector/TimelineTraceEventProcessor.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2012 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 |
|
| 31 |
#include "config.h" |
| 32 |
|
| 33 |
#if ENABLE(INSPECTOR) |
| 34 |
|
| 35 |
#include "TimelineTraceEventProcessor.h" |
| 36 |
|
| 37 |
#include "InspectorClient.h" |
| 38 |
#include "InspectorInstrumentation.h" |
| 39 |
#include "InspectorTimelineAgent.h" |
| 40 |
#include "TimelineRecordFactory.h" |
| 41 |
|
| 42 |
#include <wtf/CurrentTime.h> |
| 43 |
#include <wtf/MainThread.h> |
| 44 |
#include <wtf/ThreadSpecific.h> |
| 45 |
#include <wtf/Vector.h> |
| 46 |
|
| 47 |
namespace WebCore { |
| 48 |
|
| 49 |
namespace { |
| 50 |
|
| 51 |
class TraceEventDispatcher { |
| 52 |
WTF_MAKE_NONCOPYABLE(TraceEventDispatcher); |
| 53 |
public: |
| 54 |
static TraceEventDispatcher* instance() |
| 55 |
{ |
| 56 |
DEFINE_STATIC_LOCAL(TraceEventDispatcher, instance, ()); |
| 57 |
return &instance; |
| 58 |
} |
| 59 |
|
| 60 |
void addProcessor(TimelineTraceEventProcessor* processor, InspectorClient* client) |
| 61 |
{ |
| 62 |
MutexLocker locker(m_mutex); |
| 63 |
|
| 64 |
m_processors.append(processor); |
| 65 |
if (m_processors.size() == 1) |
| 66 |
client->setTraceEventCallback(dispatchEventOnAnyThread); |
| 67 |
} |
| 68 |
|
| 69 |
void removeProcessor(TimelineTraceEventProcessor* processor, InspectorClient* client) |
| 70 |
{ |
| 71 |
MutexLocker locker(m_mutex); |
| 72 |
|
| 73 |
size_t index = m_processors.find(processor); |
| 74 |
if (index == notFound) { |
| 75 |
ASSERT_NOT_REACHED(); |
| 76 |
return; |
| 77 |
} |
| 78 |
m_processors.remove(index); |
| 79 |
if (m_processors.isEmpty()) |
| 80 |
client->setTraceEventCallback(0); |
| 81 |
} |
| 82 |
|
| 83 |
private: |
| 84 |
enum { ProcessorsVectorInlineCapacity = 2 }; |
| 85 |
|
| 86 |
TraceEventDispatcher() { } |
| 87 |
|
| 88 |
static void dispatchEventOnAnyThread(char phase, const unsigned char*, const char* name, unsigned long long id, |
| 89 |
int numArgs, const char* const* argNames, const unsigned char* argTypes, const unsigned long long* argValues, |
| 90 |
unsigned char flags) |
| 91 |
{ |
| 92 |
TraceEventDispatcher* self = instance(); |
| 93 |
Vector<RefPtr<TimelineTraceEventProcessor>, ProcessorsVectorInlineCapacity> processors; |
| 94 |
{ |
| 95 |
MutexLocker locker(self->m_mutex); |
| 96 |
processors = self->m_processors; |
| 97 |
} |
| 98 |
for (int i = 0, size = processors.size(); i < size; ++i) { |
| 99 |
processors[i]->processEventOnAnyThread(static_cast<TimelineTraceEventProcessor::TraceEventPhase>(phase), |
| 100 |
name, id, numArgs, argNames, argTypes, argValues, flags); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
Mutex m_mutex; |
| 105 |
Vector<RefPtr<TimelineTraceEventProcessor>, ProcessorsVectorInlineCapacity> m_processors; |
| 106 |
}; |
| 107 |
|
| 108 |
} // namespce |
| 109 |
TimelineTraceEventProcessor::TimelineTraceEventProcessor(WeakPtr<InspectorTimelineAgent> timelineAgent, InspectorClient *client) |
| 110 |
: m_timelineAgent(timelineAgent) |
| 111 |
, m_inspectorClient(client) |
| 112 |
, m_pageId(reinterpret_cast<unsigned long long>(m_timelineAgent.get()->page())) |
| 113 |
{ |
| 114 |
TraceEventDispatcher::instance()->addProcessor(this, m_inspectorClient); |
| 115 |
} |
| 116 |
|
| 117 |
TimelineTraceEventProcessor::~TimelineTraceEventProcessor() |
| 118 |
{ |
| 119 |
TraceEventDispatcher::instance()->removeProcessor(this, m_inspectorClient); |
| 120 |
} |
| 121 |
|
| 122 |
const TimelineTraceEventProcessor::TraceValueUnion& TimelineTraceEventProcessor::TraceEvent::parameter(const char* name, TraceValueTypes expectedType) const |
| 123 |
{ |
| 124 |
const static TraceValueUnion missingValue; |
| 125 |
|
| 126 |
for (int i = 0; i < m_argumentCount; ++i) { |
| 127 |
if (!strcmp(name, m_argumentNames[i])) { |
| 128 |
if (m_argumentTypes[i] != expectedType) { |
| 129 |
ASSERT_NOT_REACHED(); |
| 130 |
return missingValue; |
| 131 |
} |
| 132 |
return *reinterpret_cast<const TraceValueUnion*>(m_argumentValues + i); |
| 133 |
} |
| 134 |
} |
| 135 |
ASSERT_NOT_REACHED(); |
| 136 |
return missingValue; |
| 137 |
} |
| 138 |
|
| 139 |
void TimelineTraceEventProcessor::processEventOnAnyThread(TraceEventPhase phase, const char* name, unsigned long long, |
| 140 |
int numArgs, const char* const* argNames, const unsigned char* argTypes, const unsigned long long* argValues, |
| 141 |
unsigned char) |
| 142 |
{ |
| 143 |
HashMap<String, EventTypeEntry>::iterator it = m_handlersByType.find(name); |
| 144 |
if (it == m_handlersByType.end()) |
| 145 |
return; |
| 146 |
|
| 147 |
TraceEvent event(WTF::monotonicallyIncreasingTime(), phase, name, currentThread(), numArgs, argNames, argTypes, argValues); |
| 148 |
|
| 149 |
if (!isMainThread()) { |
| 150 |
MutexLocker locker(m_backgroundEventsMutex); |
| 151 |
m_backgroundEvents.append(event); |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
processEvent(it->value, event); |
| 156 |
} |
| 157 |
|
| 158 |
void TimelineTraceEventProcessor::processEvent(const EventTypeEntry& eventTypeEntry, const TraceEvent& event) |
| 159 |
{ |
| 160 |
ASSERT(isMainThread()); |
| 161 |
|
| 162 |
TraceEventHandler handler = 0; |
| 163 |
switch (event.phase()) { |
| 164 |
case TracePhaseBegin: |
| 165 |
handler = eventTypeEntry.m_begin; |
| 166 |
break; |
| 167 |
case TracePhaseEnd: |
| 168 |
handler = eventTypeEntry.m_end; |
| 169 |
break; |
| 170 |
case TracePhaseInstant: |
| 171 |
handler = eventTypeEntry.m_instant; |
| 172 |
break; |
| 173 |
default: |
| 174 |
ASSERT_NOT_REACHED(); |
| 175 |
} |
| 176 |
if (!handler) { |
| 177 |
ASSERT_NOT_REACHED(); |
| 178 |
return; |
| 179 |
} |
| 180 |
(this->*handler)(event); |
| 181 |
} |
| 182 |
|
| 183 |
void TimelineTraceEventProcessor::sendTimelineRecord(PassRefPtr<InspectorObject> data, const String& recordType, double startTime, double endTime, const String& thread) |
| 184 |
{ |
| 185 |
ASSERT(isMainThread()); |
| 186 |
|
| 187 |
InspectorTimelineAgent* timelineAgent = m_timelineAgent.get(); |
| 188 |
if (!timelineAgent) |
| 189 |
return; |
| 190 |
timelineAgent->appendBackgroundThreadRecord(data, recordType, startTime, endTime, thread); |
| 191 |
} |
| 192 |
|
| 193 |
void TimelineTraceEventProcessor::processBackgroundEvents() |
| 194 |
{ |
| 195 |
ASSERT(isMainThread()); |
| 196 |
|
| 197 |
Vector<TraceEvent> events; |
| 198 |
{ |
| 199 |
MutexLocker locker(m_backgroundEventsMutex); |
| 200 |
events.reserveCapacity(m_backgroundEvents.capacity()); |
| 201 |
m_backgroundEvents.swap(events); |
| 202 |
} |
| 203 |
for (size_t i = 0, size = events.size(); i < size; ++i) { |
| 204 |
const TraceEvent& event = events[i]; |
| 205 |
processEvent(m_handlersByType.find(event.name())->value, event); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
} // namespace WebCore |
| 210 |
|
| 211 |
#endif // ENABLE(INSPECTOR) |