SamplingTool.h 11 KB

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