]> git.saurik.com Git - apple/javascriptcore.git/blame - bytecode/SamplingTool.h
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / bytecode / SamplingTool.h
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 2008 Apple 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
6 * are met:
7 *
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.
16 *
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.
27 */
28
29#ifndef SamplingTool_h
30#define SamplingTool_h
31
14957cd0
A
32#include "Strong.h"
33#include "Nodes.h"
34#include "Opcode.h"
6fe7ccc8 35#include "SamplingCounter.h"
9dae56ea 36#include <wtf/Assertions.h>
6fe7ccc8 37#include <wtf/Atomics.h>
9dae56ea 38#include <wtf/HashMap.h>
6fe7ccc8 39#include <wtf/MainThread.h>
9dae56ea
A
40#include <wtf/Threading.h>
41
9dae56ea
A
42namespace JSC {
43
f9bf01c6
A
44 class ScriptExecutable;
45
ba379fdc 46 class SamplingFlags {
ba379fdc 47 public:
6fe7ccc8
A
48 JS_EXPORT_PRIVATE static void start();
49 JS_EXPORT_PRIVATE static void stop();
ba379fdc
A
50
51#if ENABLE(SAMPLING_FLAGS)
52 static void setFlag(unsigned flag)
53 {
54 ASSERT(flag >= 1);
55 ASSERT(flag <= 32);
56 s_flags |= 1u << (flag - 1);
57 }
58
59 static void clearFlag(unsigned flag)
60 {
61 ASSERT(flag >= 1);
62 ASSERT(flag <= 32);
63 s_flags &= ~(1u << (flag - 1));
64 }
65
66 static void sample();
67
68 class ScopedFlag {
69 public:
70 ScopedFlag(int flag)
71 : m_flag(flag)
72 {
73 setFlag(flag);
74 }
75
76 ~ScopedFlag()
77 {
78 clearFlag(m_flag);
79 }
80
81 private:
82 int m_flag;
83 };
84
14957cd0
A
85 static const void* addressOfFlags()
86 {
87 return &s_flags;
88 }
89
ba379fdc
A
90#endif
91 private:
6fe7ccc8 92 JS_EXPORTDATA static uint32_t s_flags;
ba379fdc
A
93#if ENABLE(SAMPLING_FLAGS)
94 static uint64_t s_flagCounts[33];
95#endif
96 };
97
6fe7ccc8
A
98#if ENABLE(SAMPLING_REGIONS)
99 class SamplingRegion {
100 public:
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)
105 {
106 if (!isMainThread()) {
107 m_name = 0;
108 return;
109 }
110
111 m_name = name;
112 exchangeCurrent(this, &m_previous);
113 ASSERT(!m_previous || m_previous > this);
114 }
115
116 ~SamplingRegion()
117 {
118 if (!m_name)
119 return;
120
121 ASSERT(bitwise_cast<SamplingRegion*>(s_currentOrReserved & ~1) == this);
122 exchangeCurrent(m_previous);
123 }
124
125 static void sample();
126
127 JS_EXPORT_PRIVATE static void dump();
128
129 private:
130 const char* m_name;
131 SamplingRegion* m_previous;
132
133 static void exchangeCurrent(SamplingRegion* current, SamplingRegion** previousPtr = 0)
134 {
135 uintptr_t previous;
136 while (true) {
137 previous = s_currentOrReserved;
138
139 // If it's reserved (i.e. sampling thread is reading it), loop around.
140 if (previous & 1) {
141#if OS(UNIX)
142 sched_yield();
143#endif
144 continue;
145 }
146
147 // If we're going to CAS, then make sure previous is set.
148 if (previousPtr)
149 *previousPtr = bitwise_cast<SamplingRegion*>(previous);
150
151 if (WTF::weakCompareAndSwapUIntPtr(&s_currentOrReserved, previous, bitwise_cast<uintptr_t>(current)))
152 break;
153 }
154 }
155
156 static void dumpInternal();
157
158 class Locker {
159 public:
160 Locker();
161 ~Locker();
162 };
163
164 static volatile uintptr_t s_currentOrReserved;
165
166 // rely on identity hashing of string constants
167 static Spectrum<const char*>* s_spectrum;
168
169 static unsigned long s_noneOfTheAbove;
170
171 static unsigned s_numberOfSamplesSinceDump;
172 };
173#else // ENABLE(SAMPLING_REGIONS)
174 class SamplingRegion {
175 public:
176 SamplingRegion(const char*) { }
177 JS_EXPORT_PRIVATE void dump();
178 };
179#endif // ENABLE(SAMPLING_REGIONS)
180
9dae56ea
A
181 class CodeBlock;
182 class ExecState;
183 class Interpreter;
184 class ScopeNode;
185 struct Instruction;
186
f9bf01c6 187 struct ScriptSampleRecord {
14957cd0
A
188 ScriptSampleRecord(JSGlobalData& globalData, ScriptExecutable* executable)
189 : m_executable(globalData, executable)
9dae56ea
A
190 , m_codeBlock(0)
191 , m_sampleCount(0)
192 , m_opcodeSampleCount(0)
193 , m_samples(0)
194 , m_size(0)
195 {
196 }
197
f9bf01c6 198 ~ScriptSampleRecord()
9dae56ea
A
199 {
200 if (m_samples)
201 free(m_samples);
202 }
203
204 void sample(CodeBlock*, Instruction*);
205
14957cd0 206 Strong<ScriptExecutable> m_executable;
9dae56ea
A
207 CodeBlock* m_codeBlock;
208 int m_sampleCount;
209 int m_opcodeSampleCount;
210 int* m_samples;
211 unsigned m_size;
212 };
213
6fe7ccc8 214 typedef HashMap<ScriptExecutable*, OwnPtr<ScriptSampleRecord> > ScriptSampleRecordMap;
9dae56ea 215
ba379fdc
A
216 class SamplingThread {
217 public:
218 // Sampling thread state.
219 static bool s_running;
220 static unsigned s_hertz;
221 static ThreadIdentifier s_samplingThread;
222
6fe7ccc8
A
223 JS_EXPORT_PRIVATE static void start(unsigned hertz=10000);
224 JS_EXPORT_PRIVATE static void stop();
ba379fdc 225
6fe7ccc8 226 static void threadStartFunc(void*);
ba379fdc
A
227 };
228
9dae56ea
A
229 class SamplingTool {
230 public:
f9bf01c6 231 friend struct CallRecord;
9dae56ea
A
232 friend class HostCallRecord;
233
234#if ENABLE(OPCODE_SAMPLING)
14957cd0
A
235 class CallRecord {
236 WTF_MAKE_NONCOPYABLE(CallRecord);
9dae56ea
A
237 public:
238 CallRecord(SamplingTool* samplingTool)
239 : m_samplingTool(samplingTool)
240 , m_savedSample(samplingTool->m_sample)
241 , m_savedCodeBlock(samplingTool->m_codeBlock)
242 {
243 }
244
245 ~CallRecord()
246 {
247 m_samplingTool->m_sample = m_savedSample;
248 m_samplingTool->m_codeBlock = m_savedCodeBlock;
249 }
250
251 private:
252 SamplingTool* m_samplingTool;
253 intptr_t m_savedSample;
254 CodeBlock* m_savedCodeBlock;
255 };
256
257 class HostCallRecord : public CallRecord {
258 public:
259 HostCallRecord(SamplingTool* samplingTool)
260 : CallRecord(samplingTool)
261 {
262 samplingTool->m_sample |= 0x1;
263 }
264 };
265#else
14957cd0
A
266 class CallRecord {
267 WTF_MAKE_NONCOPYABLE(CallRecord);
9dae56ea
A
268 public:
269 CallRecord(SamplingTool*)
270 {
271 }
272 };
273
274 class HostCallRecord : public CallRecord {
275 public:
276 HostCallRecord(SamplingTool* samplingTool)
277 : CallRecord(samplingTool)
278 {
279 }
280 };
281#endif
282
283 SamplingTool(Interpreter* interpreter)
284 : m_interpreter(interpreter)
9dae56ea
A
285 , m_codeBlock(0)
286 , m_sample(0)
287 , m_sampleCount(0)
288 , m_opcodeSampleCount(0)
ba379fdc 289#if ENABLE(CODEBLOCK_SAMPLING)
6fe7ccc8 290 , m_scopeSampleMap(adoptPtr(new ScriptSampleRecordMap))
ba379fdc 291#endif
9dae56ea
A
292 {
293 memset(m_opcodeSamples, 0, sizeof(m_opcodeSamples));
294 memset(m_opcodeSamplesInCTIFunctions, 0, sizeof(m_opcodeSamplesInCTIFunctions));
295 }
296
6fe7ccc8 297 JS_EXPORT_PRIVATE void setup();
9dae56ea
A
298 void dump(ExecState*);
299
6fe7ccc8 300 void notifyOfScope(JSGlobalData&, ScriptExecutable* scope);
9dae56ea
A
301
302 void sample(CodeBlock* codeBlock, Instruction* vPC)
303 {
304 ASSERT(!(reinterpret_cast<intptr_t>(vPC) & 0x3));
305 m_codeBlock = codeBlock;
306 m_sample = reinterpret_cast<intptr_t>(vPC);
307 }
308
309 CodeBlock** codeBlockSlot() { return &m_codeBlock; }
310 intptr_t* sampleSlot() { return &m_sample; }
311
312 void* encodeSample(Instruction* vPC, bool inCTIFunction = false, bool inHostFunction = false)
313 {
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));
316 }
317
ba379fdc
A
318 static void sample();
319
9dae56ea
A
320 private:
321 class Sample {
322 public:
323 Sample(volatile intptr_t sample, CodeBlock* volatile codeBlock)
324 : m_sample(sample)
325 , m_codeBlock(codeBlock)
326 {
327 }
328
ba379fdc 329 bool isNull() { return !m_sample; }
9dae56ea
A
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; }
334
335 private:
336 intptr_t m_sample;
337 CodeBlock* m_codeBlock;
338 };
ba379fdc
A
339
340 void doRun();
341 static SamplingTool* s_samplingTool;
9dae56ea
A
342
343 Interpreter* m_interpreter;
344
9dae56ea
A
345 // State tracked by the main thread, used by the sampling thread.
346 CodeBlock* m_codeBlock;
347 intptr_t m_sample;
348
349 // Gathered sample data.
350 long long m_sampleCount;
351 long long m_opcodeSampleCount;
352 unsigned m_opcodeSamples[numOpcodeIDs];
353 unsigned m_opcodeSamplesInCTIFunctions[numOpcodeIDs];
354
ba379fdc 355#if ENABLE(CODEBLOCK_SAMPLING)
f9bf01c6
A
356 Mutex m_scriptSampleMapMutex;
357 OwnPtr<ScriptSampleRecordMap> m_scopeSampleMap;
ba379fdc
A
358#endif
359 };
360
9dae56ea
A
361} // namespace JSC
362
363#endif // SamplingTool_h