2 * Copyright (C) 2013 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.
32 #include "core/inspector/TraceEventDispatcher.h"
34 #include "core/inspector/InspectorClient.h"
35 #include "wtf/CurrentTime.h"
36 #include "wtf/Functional.h"
37 #include "wtf/MainThread.h"
38 #include "wtf/text/StringHash.h"
42 void TraceEventDispatcher::dispatchEventOnAnyThread(char phase, const unsigned char*, const char* name, unsigned long long id,
43 int numArgs, const char* const* argNames, const unsigned char* argTypes, const unsigned long long* argValues,
44 unsigned char flags, double timestamp)
46 TraceEventDispatcher* self = instance();
48 MutexLocker locker(self->m_mutex);
49 if (self->m_listeners->find(std::make_pair(name, phase)) == self->m_listeners->end())
52 self->enqueueEvent(timestamp, phase, name, id, currentThread(), numArgs, argNames, argTypes, argValues);
54 self->processBackgroundEvents();
57 void TraceEventDispatcher::enqueueEvent(double timestamp, char phase, const char* name, unsigned long long id, ThreadIdentifier threadIdentifier,
58 int argumentCount, const char* const* argumentNames, const unsigned char* argumentTypes, const unsigned long long* argumentValues)
60 const float eventProcessingThresholdInSeconds = 0.1;
62 MutexLocker locker(m_mutex);
63 // TraceEvent object is created here, rather than created in the callers of enqueEvent and passed as an argument of enqueEvent.
64 // http://crbug.com/394706
65 TraceEvent event(timestamp, phase, name, id, threadIdentifier, argumentCount, argumentNames, argumentTypes, argumentValues);
66 m_backgroundEvents.append(event);
67 if (m_processEventsTaskInFlight || event.timestamp() - m_lastEventProcessingTime <= eventProcessingThresholdInSeconds)
70 m_processEventsTaskInFlight = true;
71 callOnMainThread(bind(&TraceEventDispatcher::processBackgroundEventsTask, this));
74 void TraceEventDispatcher::processBackgroundEventsTask()
76 m_processEventsTaskInFlight = false;
77 processBackgroundEvents();
80 void TraceEventDispatcher::processBackgroundEvents()
82 ASSERT(isMainThread());
83 Vector<TraceEvent> events;
85 MutexLocker locker(m_mutex);
86 m_lastEventProcessingTime = WTF::monotonicallyIncreasingTime();
87 if (m_backgroundEvents.isEmpty())
89 events.reserveCapacity(m_backgroundEvents.capacity());
90 m_backgroundEvents.swap(events);
92 for (size_t eventIndex = 0, size = events.size(); eventIndex < size; ++eventIndex) {
93 const TraceEvent& event = events[eventIndex];
94 ListenersMap::iterator it = m_listeners->find(std::make_pair(event.name(), event.phase()));
95 if (it == m_listeners->end())
97 WillBeHeapVector<OwnPtrWillBeMember<TraceEventListener> >& listeners = *it->value.get();
98 for (size_t listenerIndex = 0; listenerIndex < listeners.size(); ++listenerIndex)
99 listeners[listenerIndex]->call(event);
103 void TraceEventDispatcher::addListener(const char* name, char phase, PassOwnPtrWillBeRawPtr<TraceEventListener> listener, InspectorClient* client)
105 static const char CategoryFilter[] = "-*," TRACE_DISABLED_BY_DEFAULT("devtools.timeline") "," TRACE_DISABLED_BY_DEFAULT("devtools.timeline.frame");
107 ASSERT(isMainThread());
108 MutexLocker locker(m_mutex);
109 if (m_listeners->isEmpty())
110 client->setTraceEventCallback(CategoryFilter, dispatchEventOnAnyThread);
111 ListenersMap::iterator it = m_listeners->find(std::make_pair(name, phase));
112 if (it == m_listeners->end())
113 m_listeners->add(std::make_pair(name, phase), adoptPtrWillBeNoop(new WillBeHeapVector<OwnPtrWillBeMember<TraceEventListener> >())).storedValue->value->append(listener);
115 it->value->append(listener);
118 void TraceEventDispatcher::removeAllListeners(void* eventTarget, InspectorClient* client)
120 ASSERT(isMainThread());
121 processBackgroundEvents();
123 MutexLocker locker(m_mutex);
125 ListenersMap remainingListeners;
126 for (ListenersMap::iterator it = m_listeners->begin(); it != m_listeners->end(); ++it) {
127 WillBeHeapVector<OwnPtrWillBeMember<TraceEventListener> >& listeners = *it->value.get();
128 for (size_t j = 0; j < listeners.size();) {
129 if (listeners[j]->target() == eventTarget)
134 if (!listeners.isEmpty())
135 remainingListeners.add(it->key, it->value.release());
137 m_listeners->swap(remainingListeners);
139 if (m_listeners->isEmpty())
140 client->resetTraceEventCallback();
143 size_t TraceEventDispatcher::TraceEvent::findParameter(const char* name) const
145 for (int i = 0; i < m_argumentCount; ++i) {
146 if (!strcmp(name, m_argumentNames[i]))
152 const TraceEvent::TraceValueUnion& TraceEventDispatcher::TraceEvent::parameter(const char* name, unsigned char expectedType) const
154 static blink::TraceEvent::TraceValueUnion missingValue;
155 size_t index = findParameter(name);
156 ASSERT(isMainThread());
157 if (index == kNotFound || m_argumentTypes[index] != expectedType) {
158 ASSERT_NOT_REACHED();
161 return m_argumentValues[index];