2 * Copyright (C) 2008 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 Computer, 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
35 #include "SamplingCounter.h"
36 #include <wtf/Assertions.h>
37 #include <wtf/Atomics.h>
38 #include <wtf/HashMap.h>
39 #include <wtf/MainThread.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();
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::weakCompareAndSwap(&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(JSGlobalData& globalData, ScriptExecutable* executable)
189 : m_executable(globalData, 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*, OwnPtr<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;
232 friend class HostCallRecord;
234 #if ENABLE(OPCODE_SAMPLING)
236 WTF_MAKE_NONCOPYABLE(CallRecord);
238 CallRecord(SamplingTool* samplingTool)
239 : m_samplingTool(samplingTool)
240 , m_savedSample(samplingTool->m_sample)
241 , m_savedCodeBlock(samplingTool->m_codeBlock)
247 m_samplingTool->m_sample = m_savedSample;
248 m_samplingTool->m_codeBlock = m_savedCodeBlock;
252 SamplingTool* m_samplingTool;
253 intptr_t m_savedSample;
254 CodeBlock* m_savedCodeBlock;
257 class HostCallRecord : public CallRecord {
259 HostCallRecord(SamplingTool* samplingTool)
260 : CallRecord(samplingTool)
262 samplingTool->m_sample |= 0x1;
267 WTF_MAKE_NONCOPYABLE(CallRecord);
269 CallRecord(SamplingTool*)
274 class HostCallRecord : public CallRecord {
276 HostCallRecord(SamplingTool* samplingTool)
277 : CallRecord(samplingTool)
283 SamplingTool(Interpreter* interpreter)
284 : m_interpreter(interpreter)
288 , m_opcodeSampleCount(0)
289 #if ENABLE(CODEBLOCK_SAMPLING)
290 , m_scopeSampleMap(adoptPtr(new ScriptSampleRecordMap))
293 memset(m_opcodeSamples, 0, sizeof(m_opcodeSamples));
294 memset(m_opcodeSamplesInCTIFunctions, 0, sizeof(m_opcodeSamplesInCTIFunctions));
297 JS_EXPORT_PRIVATE void setup();
298 void dump(ExecState*);
300 void notifyOfScope(JSGlobalData&, ScriptExecutable* scope);
302 void sample(CodeBlock* codeBlock, Instruction* vPC)
304 ASSERT(!(reinterpret_cast<intptr_t>(vPC) & 0x3));
305 m_codeBlock = codeBlock;
306 m_sample = reinterpret_cast<intptr_t>(vPC);
309 CodeBlock** codeBlockSlot() { return &m_codeBlock; }
310 intptr_t* sampleSlot() { return &m_sample; }
312 void* encodeSample(Instruction* vPC, bool inCTIFunction = false, bool inHostFunction = false)
314 ASSERT(!(reinterpret_cast<intptr_t>(vPC) & 0x3));
315 return reinterpret_cast<void*>(reinterpret_cast<intptr_t>(vPC) | (static_cast<intptr_t>(inCTIFunction) << 1) | static_cast<intptr_t>(inHostFunction));
318 static void sample();
323 Sample(volatile intptr_t sample, CodeBlock* volatile codeBlock)
325 , m_codeBlock(codeBlock)
329 bool isNull() { return !m_sample; }
330 CodeBlock* codeBlock() { return m_codeBlock; }
331 Instruction* vPC() { return reinterpret_cast<Instruction*>(m_sample & ~0x3); }
332 bool inHostFunction() { return m_sample & 0x1; }
333 bool inCTIFunction() { return m_sample & 0x2; }
337 CodeBlock* m_codeBlock;
341 static SamplingTool* s_samplingTool;
343 Interpreter* m_interpreter;
345 // State tracked by the main thread, used by the sampling thread.
346 CodeBlock* m_codeBlock;
349 // Gathered sample data.
350 long long m_sampleCount;
351 long long m_opcodeSampleCount;
352 unsigned m_opcodeSamples[numOpcodeIDs];
353 unsigned m_opcodeSamplesInCTIFunctions[numOpcodeIDs];
355 #if ENABLE(CODEBLOCK_SAMPLING)
356 Mutex m_scriptSampleMapMutex;
357 OwnPtr<ScriptSampleRecordMap> m_scopeSampleMap;
363 #endif // SamplingTool_h