CodeProfiling.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2012 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. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "CodeProfiling.h"
  27. #include "CodeProfile.h"
  28. #include <wtf/MetaAllocator.h>
  29. #if HAVE(SIGNAL_H) && ((PLATFORM(MAC) && CPU(X86_64)) || (OS(LINUX) && CPU(X86)))
  30. #include <signal.h>
  31. #endif
  32. #if OS(LINUX)
  33. #include <sys/time.h>
  34. #endif
  35. namespace JSC {
  36. volatile CodeProfile* CodeProfiling::s_profileStack = 0;
  37. CodeProfiling::Mode CodeProfiling::s_mode = CodeProfiling::Disabled;
  38. WTF::MetaAllocatorTracker* CodeProfiling::s_tracker = 0;
  39. #if COMPILER(CLANG)
  40. #pragma clang diagnostic push
  41. #pragma clang diagnostic ignored "-Wmissing-noreturn"
  42. #endif
  43. #if (PLATFORM(MAC) && CPU(X86_64)) || (OS(LINUX) && CPU(X86))
  44. // Helper function to start & stop the timer.
  45. // Presently we're using the wall-clock timer, since this seems to give the best results.
  46. static void setProfileTimer(unsigned usec)
  47. {
  48. itimerval timer;
  49. timer.it_value.tv_sec = 0;
  50. timer.it_value.tv_usec = usec;
  51. timer.it_interval.tv_sec = 0;
  52. timer.it_interval.tv_usec = usec;
  53. setitimer(ITIMER_REAL, &timer, 0);
  54. }
  55. #endif
  56. #if COMPILER(CLANG)
  57. #pragma clang diagnostic pop
  58. #endif
  59. #if PLATFORM(MAC) && CPU(X86_64)
  60. static void profilingTimer(int, siginfo_t*, void* uap)
  61. {
  62. mcontext_t context = static_cast<ucontext_t*>(uap)->uc_mcontext;
  63. CodeProfiling::sample(reinterpret_cast<void*>(context->__ss.__rip),
  64. reinterpret_cast<void**>(context->__ss.__rbp));
  65. }
  66. #elif OS(LINUX) && CPU(X86)
  67. static void profilingTimer(int, siginfo_t*, void* uap)
  68. {
  69. mcontext_t context = static_cast<ucontext_t*>(uap)->uc_mcontext;
  70. CodeProfiling::sample(reinterpret_cast<void*>(context.gregs[REG_EIP]),
  71. reinterpret_cast<void**>(context.gregs[REG_EBP]));
  72. }
  73. #endif
  74. // Callback triggered when the timer is fired.
  75. void CodeProfiling::sample(void* pc, void** framePointer)
  76. {
  77. CodeProfile* profileStack = const_cast<CodeProfile*>(s_profileStack);
  78. if (profileStack)
  79. profileStack->sample(pc, framePointer);
  80. }
  81. void CodeProfiling::notifyAllocator(WTF::MetaAllocator* allocator)
  82. {
  83. #if !OS(WINCE) && !OS(ORBIS)
  84. // Check for JSC_CODE_PROFILING.
  85. const char* codeProfilingMode = getenv("JSC_CODE_PROFILING");
  86. if (!codeProfilingMode)
  87. return;
  88. // Check for a valid mode, currently "1", "2", or "3".
  89. if (!codeProfilingMode[0] || codeProfilingMode[1])
  90. return;
  91. switch (*codeProfilingMode) {
  92. case '1':
  93. s_mode = Enabled;
  94. break;
  95. case '2':
  96. s_mode = Verbose;
  97. break;
  98. case '3':
  99. s_mode = VeryVerbose;
  100. break;
  101. default:
  102. return;
  103. }
  104. ASSERT(enabled());
  105. ASSERT(!s_tracker);
  106. s_tracker = new WTF::MetaAllocatorTracker();
  107. allocator->trackAllocations(s_tracker);
  108. #endif
  109. }
  110. void* CodeProfiling::getOwnerUIDForPC(void* address)
  111. {
  112. if (!s_tracker)
  113. return 0;
  114. WTF::MetaAllocatorHandle* handle = s_tracker->find(address);
  115. if (!handle)
  116. return 0;
  117. return handle->ownerUID();
  118. }
  119. void CodeProfiling::begin(const SourceCode& source)
  120. {
  121. // Push a new CodeProfile onto the stack for each script encountered.
  122. CodeProfile* profileStack = const_cast<CodeProfile*>(s_profileStack);
  123. bool alreadyProfiling = profileStack;
  124. s_profileStack = profileStack = new CodeProfile(source, profileStack);
  125. // Is the profiler already running - if so, the timer will already be set up.
  126. if (alreadyProfiling)
  127. return;
  128. #if (PLATFORM(MAC) && CPU(X86_64)) || (OS(LINUX) && CPU(X86))
  129. // Regsiter a signal handler & itimer.
  130. struct sigaction action;
  131. action.sa_sigaction = reinterpret_cast<void (*)(int, siginfo_t *, void *)>(profilingTimer);
  132. sigfillset(&action.sa_mask);
  133. action.sa_flags = SA_SIGINFO;
  134. sigaction(SIGALRM, &action, 0);
  135. setProfileTimer(100);
  136. #endif
  137. }
  138. void CodeProfiling::end()
  139. {
  140. // Pop the current profiler off the stack.
  141. CodeProfile* current = const_cast<CodeProfile*>(s_profileStack);
  142. ASSERT(current);
  143. s_profileStack = current->parent();
  144. // Is this the outermost script being profiled? - if not, just return.
  145. // We perform all output of profiles recursively from the outermost script,
  146. // to minimize profiling overhead from skewing results.
  147. if (s_profileStack)
  148. return;
  149. #if (PLATFORM(MAC) && CPU(X86_64)) || (OS(LINUX) && CPU(X86))
  150. // Stop profiling
  151. setProfileTimer(0);
  152. #endif
  153. current->report();
  154. delete current;
  155. }
  156. }