barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 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 | |
| 26 | #include "config.h" |
| 27 | #include "CodeProfiling.h" |
| 28 | |
| 29 | #include "CodeProfile.h" |
eric@webkit.org | fa5d72e | 2012-03-21 23:18:20 +0000 | [diff] [blame] | 30 | #include <wtf/MetaAllocator.h> |
paroga@webkit.org | f839c95 | 2012-02-06 12:33:47 +0000 | [diff] [blame] | 31 | |
| 32 | #if HAVE(SIGNAL_H) |
| 33 | #include <signal.h> |
| 34 | #endif |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 35 | |
achristensen@apple.com | 85f6ab5 | 2014-08-05 00:30:15 +0000 | [diff] [blame] | 36 | #if OS(LINUX) || OS(DARWIN) |
yuqiang.xian@intel.com | aa9f8cc | 2012-02-17 07:10:12 +0000 | [diff] [blame] | 37 | #include <sys/time.h> |
| 38 | #endif |
| 39 | |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 40 | namespace JSC { |
| 41 | |
| 42 | volatile CodeProfile* CodeProfiling::s_profileStack = 0; |
| 43 | CodeProfiling::Mode CodeProfiling::s_mode = CodeProfiling::Disabled; |
| 44 | WTF::MetaAllocatorTracker* CodeProfiling::s_tracker = 0; |
| 45 | |
fpizlo@apple.com | 6dab539 | 2012-01-30 01:41:09 +0000 | [diff] [blame] | 46 | #if COMPILER(CLANG) |
| 47 | #pragma clang diagnostic push |
| 48 | #pragma clang diagnostic ignored "-Wmissing-noreturn" |
| 49 | #endif |
| 50 | |
mitz@apple.com | 37934feb | 2014-02-03 08:17:33 +0000 | [diff] [blame] | 51 | #if (OS(DARWIN) && !PLATFORM(EFL) && !PLATFORM(GTK) && CPU(X86_64)) || (OS(LINUX) && CPU(X86)) |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 52 | // Helper function to start & stop the timer. |
| 53 | // Presently we're using the wall-clock timer, since this seems to give the best results. |
| 54 | static void setProfileTimer(unsigned usec) |
| 55 | { |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 56 | itimerval timer; |
| 57 | timer.it_value.tv_sec = 0; |
| 58 | timer.it_value.tv_usec = usec; |
| 59 | timer.it_interval.tv_sec = 0; |
| 60 | timer.it_interval.tv_usec = usec; |
| 61 | setitimer(ITIMER_REAL, &timer, 0); |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 62 | } |
barraclough@apple.com | bd8628e | 2012-01-30 23:56:00 +0000 | [diff] [blame] | 63 | #endif |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 64 | |
fpizlo@apple.com | 6dab539 | 2012-01-30 01:41:09 +0000 | [diff] [blame] | 65 | #if COMPILER(CLANG) |
| 66 | #pragma clang diagnostic pop |
| 67 | #endif |
| 68 | |
mitz@apple.com | 37934feb | 2014-02-03 08:17:33 +0000 | [diff] [blame] | 69 | #if OS(DARWIN) && !PLATFORM(EFL) && !PLATFORM(GTK) && CPU(X86_64) |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 70 | static void profilingTimer(int, siginfo_t*, void* uap) |
| 71 | { |
| 72 | mcontext_t context = static_cast<ucontext_t*>(uap)->uc_mcontext; |
| 73 | CodeProfiling::sample(reinterpret_cast<void*>(context->__ss.__rip), |
| 74 | reinterpret_cast<void**>(context->__ss.__rbp)); |
| 75 | } |
yuqiang.xian@intel.com | aa9f8cc | 2012-02-17 07:10:12 +0000 | [diff] [blame] | 76 | #elif OS(LINUX) && CPU(X86) |
| 77 | static void profilingTimer(int, siginfo_t*, void* uap) |
| 78 | { |
| 79 | mcontext_t context = static_cast<ucontext_t*>(uap)->uc_mcontext; |
| 80 | CodeProfiling::sample(reinterpret_cast<void*>(context.gregs[REG_EIP]), |
| 81 | reinterpret_cast<void**>(context.gregs[REG_EBP])); |
| 82 | } |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 83 | #endif |
| 84 | |
| 85 | // Callback triggered when the timer is fired. |
| 86 | void CodeProfiling::sample(void* pc, void** framePointer) |
| 87 | { |
| 88 | CodeProfile* profileStack = const_cast<CodeProfile*>(s_profileStack); |
| 89 | if (profileStack) |
| 90 | profileStack->sample(pc, framePointer); |
| 91 | } |
| 92 | |
| 93 | void CodeProfiling::notifyAllocator(WTF::MetaAllocator* allocator) |
| 94 | { |
| 95 | // Check for JSC_CODE_PROFILING. |
| 96 | const char* codeProfilingMode = getenv("JSC_CODE_PROFILING"); |
| 97 | if (!codeProfilingMode) |
| 98 | return; |
| 99 | |
| 100 | // Check for a valid mode, currently "1", "2", or "3". |
| 101 | if (!codeProfilingMode[0] || codeProfilingMode[1]) |
| 102 | return; |
| 103 | switch (*codeProfilingMode) { |
| 104 | case '1': |
| 105 | s_mode = Enabled; |
| 106 | break; |
| 107 | case '2': |
| 108 | s_mode = Verbose; |
| 109 | break; |
| 110 | case '3': |
| 111 | s_mode = VeryVerbose; |
| 112 | break; |
| 113 | default: |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | ASSERT(enabled()); |
| 118 | ASSERT(!s_tracker); |
| 119 | s_tracker = new WTF::MetaAllocatorTracker(); |
| 120 | allocator->trackAllocations(s_tracker); |
| 121 | } |
| 122 | |
| 123 | void* CodeProfiling::getOwnerUIDForPC(void* address) |
| 124 | { |
| 125 | if (!s_tracker) |
| 126 | return 0; |
| 127 | WTF::MetaAllocatorHandle* handle = s_tracker->find(address); |
| 128 | if (!handle) |
| 129 | return 0; |
| 130 | return handle->ownerUID(); |
| 131 | } |
| 132 | |
| 133 | void CodeProfiling::begin(const SourceCode& source) |
| 134 | { |
| 135 | // Push a new CodeProfile onto the stack for each script encountered. |
| 136 | CodeProfile* profileStack = const_cast<CodeProfile*>(s_profileStack); |
| 137 | bool alreadyProfiling = profileStack; |
| 138 | s_profileStack = profileStack = new CodeProfile(source, profileStack); |
| 139 | |
| 140 | // Is the profiler already running - if so, the timer will already be set up. |
| 141 | if (alreadyProfiling) |
| 142 | return; |
| 143 | |
mitz@apple.com | 37934feb | 2014-02-03 08:17:33 +0000 | [diff] [blame] | 144 | #if (OS(DARWIN) && !PLATFORM(EFL) && !PLATFORM(GTK) && CPU(X86_64)) || (OS(LINUX) && CPU(X86)) |
barraclough@apple.com | bd8628e | 2012-01-30 23:56:00 +0000 | [diff] [blame] | 145 | // Regsiter a signal handler & itimer. |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 146 | struct sigaction action; |
yuqiang.xian@intel.com | aa9f8cc | 2012-02-17 07:10:12 +0000 | [diff] [blame] | 147 | action.sa_sigaction = reinterpret_cast<void (*)(int, siginfo_t *, void *)>(profilingTimer); |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 148 | sigfillset(&action.sa_mask); |
| 149 | action.sa_flags = SA_SIGINFO; |
| 150 | sigaction(SIGALRM, &action, 0); |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 151 | setProfileTimer(100); |
barraclough@apple.com | bd8628e | 2012-01-30 23:56:00 +0000 | [diff] [blame] | 152 | #endif |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void CodeProfiling::end() |
| 156 | { |
| 157 | // Pop the current profiler off the stack. |
| 158 | CodeProfile* current = const_cast<CodeProfile*>(s_profileStack); |
| 159 | ASSERT(current); |
| 160 | s_profileStack = current->parent(); |
| 161 | |
| 162 | // Is this the outermost script being profiled? - if not, just return. |
| 163 | // We perform all output of profiles recursively from the outermost script, |
| 164 | // to minimize profiling overhead from skewing results. |
| 165 | if (s_profileStack) |
| 166 | return; |
| 167 | |
mitz@apple.com | 37934feb | 2014-02-03 08:17:33 +0000 | [diff] [blame] | 168 | #if (OS(DARWIN) && !PLATFORM(EFL) && !PLATFORM(GTK) && CPU(X86_64)) || (OS(LINUX) && CPU(X86)) |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 169 | // Stop profiling |
| 170 | setProfileTimer(0); |
barraclough@apple.com | bd8628e | 2012-01-30 23:56:00 +0000 | [diff] [blame] | 171 | #endif |
barraclough@apple.com | f51cff3 | 2012-01-29 03:47:13 +0000 | [diff] [blame] | 172 | |
| 173 | current->report(); |
| 174 | delete current; |
| 175 | } |
| 176 | |
| 177 | } |