OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2011 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 "platform/EventTracer.h" | |
32 | |
33 #include "base/time/time.h" | |
34 #include "base/trace_event/trace_event.h" | |
35 #include "base/trace_event/trace_event_argument.h" | |
36 #include "platform/TracedValue.h" | |
37 #include "public/platform/Platform.h" | |
38 #include "wtf/Assertions.h" | |
39 #include "wtf/text/StringUTF8Adaptor.h" | |
40 #include <memory> | |
41 #include <stdio.h> | |
42 | |
43 namespace blink { | |
44 | |
45 static_assert(sizeof(TraceEvent::TraceEventHandle) == sizeof(base::trace_event::
TraceEventHandle), "TraceEventHandle types must be the same"); | |
46 static_assert(sizeof(TraceEvent::TraceEventAPIAtomicWord) == sizeof(const char*)
, "TraceEventAPIAtomicWord must be pointer-sized."); | |
47 | |
48 // The dummy variable is needed to avoid a crash when someone updates the state
variables | |
49 // before EventTracer::initialize() is called. | |
50 TraceEvent::TraceEventAPIAtomicWord dummyTraceSamplingState = 0; | |
51 TraceEvent::TraceEventAPIAtomicWord* traceSamplingState[3] = {&dummyTraceSamplin
gState, &dummyTraceSamplingState, &dummyTraceSamplingState }; | |
52 | |
53 void EventTracer::initialize() | |
54 { | |
55 traceSamplingState[0] = reinterpret_cast<TraceEvent::TraceEventAPIAtomicWord
*>(&TRACE_EVENT_API_THREAD_BUCKET(0)); | |
56 // FIXME: traceSamplingState[0] can be 0 in split-dll build. http://crbug.co
m/256965 | |
57 if (!traceSamplingState[0]) | |
58 traceSamplingState[0] = &dummyTraceSamplingState; | |
59 traceSamplingState[1] = reinterpret_cast<TraceEvent::TraceEventAPIAtomicWord
*>(&TRACE_EVENT_API_THREAD_BUCKET(1)); | |
60 if (!traceSamplingState[1]) | |
61 traceSamplingState[1] = &dummyTraceSamplingState; | |
62 traceSamplingState[2] = reinterpret_cast<TraceEvent::TraceEventAPIAtomicWord
*>(&TRACE_EVENT_API_THREAD_BUCKET(2)); | |
63 if (!traceSamplingState[2]) | |
64 traceSamplingState[2] = &dummyTraceSamplingState; | |
65 } | |
66 | |
67 const unsigned char* EventTracer::getTraceCategoryEnabledFlag(const char* catego
ryName) | |
68 { | |
69 return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(categoryName); | |
70 } | |
71 | |
72 TraceEvent::TraceEventHandle EventTracer::addTraceEvent(char phase, const unsign
ed char* categoryEnabledFlag, | |
73 const char* name, const char* scope, unsigned long long id, unsigned long lo
ng bindId, double timestamp, | |
74 int numArgs, const char* argNames[], const unsigned char argTypes[], | |
75 const unsigned long long argValues[], | |
76 std::unique_ptr<TracedValue> tracedValue1, | |
77 std::unique_ptr<TracedValue> tracedValue2, | |
78 unsigned flags) | |
79 { | |
80 std::unique_ptr<base::trace_event::ConvertableToTraceFormat> convertables[2]
; | |
81 ASSERT(numArgs <= 2); | |
82 // We move m_tracedValues from TracedValues for thread safety. | |
83 // https://crbug.com/478149 | |
84 if (numArgs >= 1 && argTypes[0] == TRACE_VALUE_TYPE_CONVERTABLE) | |
85 convertables[0] = std::move(tracedValue1->m_tracedValue); | |
86 if (numArgs >= 2 && argTypes[1] == TRACE_VALUE_TYPE_CONVERTABLE) | |
87 convertables[1] = std::move(tracedValue2->m_tracedValue); | |
88 return addTraceEvent(phase, categoryEnabledFlag, name, scope, id, bindId, ti
mestamp, numArgs, argNames, argTypes, argValues, convertables, flags); | |
89 } | |
90 | |
91 TraceEvent::TraceEventHandle EventTracer::addTraceEvent(char phase, const unsign
ed char* categoryEnabledFlag, | |
92 const char* name, const char* scope, unsigned long long id, unsigned long lo
ng bindId, double timestamp, | |
93 int numArgs, const char** argNames, const unsigned char* argTypes, | |
94 const unsigned long long* argValues, unsigned flags) | |
95 { | |
96 return addTraceEvent(phase, categoryEnabledFlag, name, scope, id, bindId, ti
mestamp, numArgs, argNames, argTypes, argValues, nullptr, flags); | |
97 } | |
98 | |
99 TraceEvent::TraceEventHandle EventTracer::addTraceEvent(char phase, const unsign
ed char* categoryEnabledFlag, | |
100 const char* name, const char* scope, unsigned long long id, unsigned long lo
ng bindId, double timestamp, | |
101 int numArgs, const char** argNames, const unsigned char* argTypes, const uns
igned long long* argValues, | |
102 std::unique_ptr<base::trace_event::ConvertableToTraceFormat>* convertables,
unsigned flags) | |
103 { | |
104 base::TimeTicks timestampTimeTicks = base::TimeTicks() + base::TimeDelta::Fr
omSecondsD(timestamp); | |
105 base::trace_event::TraceEventHandle handle = TRACE_EVENT_API_ADD_TRACE_EVENT
_WITH_THREAD_ID_AND_TIMESTAMP(phase, categoryEnabledFlag, name, scope, id, bindI
d, base::PlatformThread::CurrentId(), timestampTimeTicks, numArgs, argNames, arg
Types, argValues, convertables, flags); | |
106 TraceEvent::TraceEventHandle result; | |
107 memcpy(&result, &handle, sizeof(result)); | |
108 return result; | |
109 } | |
110 | |
111 void EventTracer::updateTraceEventDuration(const unsigned char* categoryEnabledF
lag, const char* name, TraceEvent::TraceEventHandle handle) | |
112 { | |
113 base::trace_event::TraceEventHandle traceEventHandle; | |
114 memcpy(&traceEventHandle, &handle, sizeof(handle)); | |
115 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(categoryEnabledFlag, name, trace
EventHandle); | |
116 } | |
117 | |
118 double EventTracer::systemTraceTime() | |
119 { | |
120 return (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | |
121 } | |
122 | |
123 } // namespace blink | |
OLD | NEW |