2 * Copyright (C) 2008, 2013 Apple 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef SamplingTool_h
30 #define SamplingTool_h
34 #include "SamplingCounter.h"
35 #include <wtf/Assertions.h>
36 #include <wtf/Atomics.h>
37 #include <wtf/HashMap.h>
38 #include <wtf/MainThread.h>
39 #include <wtf/Spectrum.h>
40 #include <wtf/Threading.h>
44 class ScriptExecutable
;
48 JS_EXPORT_PRIVATE
static void start();
49 JS_EXPORT_PRIVATE
static void stop();
51 #if ENABLE(SAMPLING_FLAGS)
52 static void setFlag(unsigned flag
)
56 s_flags
|= 1u << (flag
- 1);
59 static void clearFlag(unsigned flag
)
63 s_flags
&= ~(1u << (flag
- 1));
85 static const void* addressOfFlags()
92 JS_EXPORTDATA
static uint32_t s_flags
;
93 #if ENABLE(SAMPLING_FLAGS)
94 static uint64_t s_flagCounts
[33];
98 #if ENABLE(SAMPLING_REGIONS)
99 class SamplingRegion
{
101 // Create a scoped sampling region using a C string constant name that describes
102 // what you are doing. This must be a string constant that persists for the
103 // lifetime of the process and is immutable.
104 SamplingRegion(const char* name
)
106 if (!isMainThread()) {
112 exchangeCurrent(this, &m_previous
);
113 ASSERT(!m_previous
|| m_previous
> this);
121 ASSERT(bitwise_cast
<SamplingRegion
*>(s_currentOrReserved
& ~1) == this);
122 exchangeCurrent(m_previous
);
125 static void sample();
127 JS_EXPORT_PRIVATE
static void dump();
131 SamplingRegion
* m_previous
;
133 static void exchangeCurrent(SamplingRegion
* current
, SamplingRegion
** previousPtr
= 0)
137 previous
= s_currentOrReserved
;
139 // If it's reserved (i.e. sampling thread is reading it), loop around.
147 // If we're going to CAS, then make sure previous is set.
149 *previousPtr
= bitwise_cast
<SamplingRegion
*>(previous
);
151 if (WTF::weakCompareAndSwapUIntPtr(&s_currentOrReserved
, previous
, bitwise_cast
<uintptr_t>(current
)))
156 static void dumpInternal();
164 static volatile uintptr_t s_currentOrReserved
;
166 // rely on identity hashing of string constants
167 static Spectrum
<const char*>* s_spectrum
;
169 static unsigned long s_noneOfTheAbove
;
171 static unsigned s_numberOfSamplesSinceDump
;
173 #else // ENABLE(SAMPLING_REGIONS)
174 class SamplingRegion
{
176 SamplingRegion(const char*) { }
177 JS_EXPORT_PRIVATE
void dump();
179 #endif // ENABLE(SAMPLING_REGIONS)
187 struct ScriptSampleRecord
{
188 ScriptSampleRecord(VM
& vm
, ScriptExecutable
* executable
)
189 : m_executable(vm
, executable
)
192 , m_opcodeSampleCount(0)
198 ~ScriptSampleRecord()
204 void sample(CodeBlock
*, Instruction
*);
206 Strong
<ScriptExecutable
> m_executable
;
207 CodeBlock
* m_codeBlock
;
209 int m_opcodeSampleCount
;
214 typedef HashMap
<ScriptExecutable
*, std::unique_ptr
<ScriptSampleRecord
>> ScriptSampleRecordMap
;
216 class SamplingThread
{
218 // Sampling thread state.
219 static bool s_running
;
220 static unsigned s_hertz
;
221 static ThreadIdentifier s_samplingThread
;
223 JS_EXPORT_PRIVATE
static void start(unsigned hertz
=10000);
224 JS_EXPORT_PRIVATE
static void stop();
226 static void threadStartFunc(void*);
231 friend struct CallRecord
;
233 #if ENABLE(OPCODE_SAMPLING)
235 WTF_MAKE_NONCOPYABLE(CallRecord
);
237 CallRecord(SamplingTool
* samplingTool
, bool isHostCall
= false)
238 : m_samplingTool(samplingTool
)
239 , m_savedSample(samplingTool
->m_sample
)
240 , m_savedCodeBlock(samplingTool
->m_codeBlock
)
243 samplingTool
->m_sample
|= 0x1;
248 m_samplingTool
->m_sample
= m_savedSample
;
249 m_samplingTool
->m_codeBlock
= m_savedCodeBlock
;
253 SamplingTool
* m_samplingTool
;
254 intptr_t m_savedSample
;
255 CodeBlock
* m_savedCodeBlock
;
259 WTF_MAKE_NONCOPYABLE(CallRecord
);
261 CallRecord(SamplingTool
*, bool = false)
267 SamplingTool(Interpreter
* interpreter
)
268 : m_interpreter(interpreter
)
272 , m_opcodeSampleCount(0)
273 #if ENABLE(CODEBLOCK_SAMPLING)
274 , m_scopeSampleMap(adoptPtr(new ScriptSampleRecordMap
))
277 memset(m_opcodeSamples
, 0, sizeof(m_opcodeSamples
));
278 memset(m_opcodeSamplesInCTIFunctions
, 0, sizeof(m_opcodeSamplesInCTIFunctions
));
281 JS_EXPORT_PRIVATE
void setup();
282 void dump(ExecState
*);
284 void notifyOfScope(VM
&, ScriptExecutable
* scope
);
286 void sample(CodeBlock
* codeBlock
, Instruction
* vPC
)
288 ASSERT(!(reinterpret_cast<intptr_t>(vPC
) & 0x3));
289 m_codeBlock
= codeBlock
;
290 m_sample
= reinterpret_cast<intptr_t>(vPC
);
293 CodeBlock
** codeBlockSlot() { return &m_codeBlock
; }
294 intptr_t* sampleSlot() { return &m_sample
; }
296 void* encodeSample(Instruction
* vPC
, bool inCTIFunction
= false, bool inHostFunction
= false)
298 ASSERT(!(reinterpret_cast<intptr_t>(vPC
) & 0x3));
299 return reinterpret_cast<void*>(reinterpret_cast<intptr_t>(vPC
) | (static_cast<intptr_t>(inCTIFunction
) << 1) | static_cast<intptr_t>(inHostFunction
));
302 static void sample();
307 Sample(volatile intptr_t sample
, CodeBlock
* volatile codeBlock
)
309 , m_codeBlock(codeBlock
)
313 bool isNull() { return !m_sample
; }
314 CodeBlock
* codeBlock() { return m_codeBlock
; }
315 Instruction
* vPC() { return reinterpret_cast<Instruction
*>(m_sample
& ~0x3); }
316 bool inHostFunction() { return m_sample
& 0x1; }
317 bool inCTIFunction() { return m_sample
& 0x2; }
321 CodeBlock
* m_codeBlock
;
325 static SamplingTool
* s_samplingTool
;
327 Interpreter
* m_interpreter
;
329 // State tracked by the main thread, used by the sampling thread.
330 CodeBlock
* m_codeBlock
;
333 // Gathered sample data.
334 long long m_sampleCount
;
335 long long m_opcodeSampleCount
;
336 unsigned m_opcodeSamples
[numOpcodeIDs
];
337 unsigned m_opcodeSamplesInCTIFunctions
[numOpcodeIDs
];
339 #if ENABLE(CODEBLOCK_SAMPLING)
340 Mutex m_scriptSampleMapMutex
;
341 OwnPtr
<ScriptSampleRecordMap
> m_scopeSampleMap
;
347 #endif // SamplingTool_h