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