sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "SamplingProfiler.h" |
| 28 | |
| 29 | #if ENABLE(SAMPLING_PROFILER) |
| 30 | |
| 31 | #include "CallFrame.h" |
| 32 | #include "CodeBlock.h" |
fpizlo@apple.com | 7b23164 | 2016-10-11 23:52:02 +0000 | [diff] [blame] | 33 | #include "CodeBlockSet.h" |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 34 | #include "HeapIterationScope.h" |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 35 | #include "HeapUtil.h" |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 36 | #include "InlineCallFrame.h" |
| 37 | #include "Interpreter.h" |
fpizlo@apple.com | 0ef4395 | 2016-12-08 22:14:50 +0000 | [diff] [blame] | 38 | #include "JSCInlines.h" |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 39 | #include "JSFunction.h" |
| 40 | #include "LLIntPCRanges.h" |
| 41 | #include "MarkedBlock.h" |
| 42 | #include "MarkedBlockSet.h" |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 43 | #include "MarkedSpaceInlines.h" |
ggaren@apple.com | c9a6ef4 | 2016-10-28 20:04:56 +0000 | [diff] [blame] | 44 | #include "NativeExecutable.h" |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 45 | #include "PCToCodeOriginMap.h" |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 46 | #include "SlotVisitor.h" |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 47 | #include "StrongInlines.h" |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 48 | #include "VM.h" |
keith_miller@apple.com | 3517aa4 | 2016-05-24 23:03:09 +0000 | [diff] [blame] | 49 | #include <wtf/HashSet.h> |
sbarati@apple.com | 51d8007 | 2016-06-14 02:29:26 +0000 | [diff] [blame] | 50 | #include <wtf/RandomNumber.h> |
keith_miller@apple.com | 3517aa4 | 2016-05-24 23:03:09 +0000 | [diff] [blame] | 51 | #include <wtf/RefPtr.h> |
joepeck@webkit.org | 7e07f39 | 2016-09-22 18:59:47 +0000 | [diff] [blame] | 52 | #include <wtf/text/StringBuilder.h> |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 53 | |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 54 | #if OS(DARWIN) |
| 55 | #include <cxxabi.h> |
| 56 | #include <dlfcn.h> |
| 57 | #endif |
| 58 | |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 59 | namespace JSC { |
| 60 | |
| 61 | static double sNumTotalStackTraces = 0; |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 62 | static double sNumTotalWalks = 0; |
| 63 | static double sNumFailedWalks = 0; |
| 64 | static const uint32_t sNumWalkReportingFrequency = 50; |
| 65 | static const double sWalkErrorPercentage = .05; |
| 66 | static const bool sReportStatsOnlyWhenTheyreAboveThreshold = false; |
| 67 | static const bool sReportStats = false; |
| 68 | |
| 69 | using FrameType = SamplingProfiler::FrameType; |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 70 | using UnprocessedStackFrame = SamplingProfiler::UnprocessedStackFrame; |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 71 | |
| 72 | ALWAYS_INLINE static void reportStats() |
| 73 | { |
| 74 | if (sReportStats && sNumTotalWalks && static_cast<uint64_t>(sNumTotalWalks) % sNumWalkReportingFrequency == 0) { |
| 75 | if (!sReportStatsOnlyWhenTheyreAboveThreshold || (sNumFailedWalks / sNumTotalWalks > sWalkErrorPercentage)) { |
| 76 | dataLogF("Num total walks: %llu. Failed walks percent: %lf\n", |
utatane.tea@gmail.com | 0cf8d74 | 2016-01-30 12:53:59 +0000 | [diff] [blame] | 77 | static_cast<unsigned long long>(sNumTotalWalks), sNumFailedWalks / sNumTotalWalks); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | class FrameWalker { |
| 83 | public: |
mark.lam@apple.com | a2668e0 | 2017-03-09 21:05:41 +0000 | [diff] [blame^] | 84 | FrameWalker(VM& vm, ExecState* callFrame, const AbstractLocker& codeBlockSetLocker, const AbstractLocker& machineThreadsLocker) |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 85 | : m_vm(vm) |
| 86 | , m_callFrame(callFrame) |
| 87 | , m_vmEntryFrame(vm.topVMEntryFrame) |
| 88 | , m_codeBlockSetLocker(codeBlockSetLocker) |
| 89 | , m_machineThreadsLocker(machineThreadsLocker) |
| 90 | { |
| 91 | } |
| 92 | |
sbarati@apple.com | 30cd116 | 2016-02-11 21:04:43 +0000 | [diff] [blame] | 93 | SUPPRESS_ASAN |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 94 | size_t walk(Vector<UnprocessedStackFrame>& stackTrace, bool& didRunOutOfSpace) |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 95 | { |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 96 | if (sReportStats) |
| 97 | sNumTotalWalks++; |
| 98 | resetAtMachineFrame(); |
| 99 | size_t maxStackTraceSize = stackTrace.size(); |
| 100 | while (!isAtTop() && !m_bailingOut && m_depth < maxStackTraceSize) { |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 101 | recordJSFrame(stackTrace); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 102 | advanceToParentFrame(); |
| 103 | resetAtMachineFrame(); |
| 104 | } |
| 105 | didRunOutOfSpace = m_depth >= maxStackTraceSize && !isAtTop(); |
| 106 | reportStats(); |
| 107 | return m_depth; |
| 108 | } |
| 109 | |
| 110 | bool wasValidWalk() const |
| 111 | { |
| 112 | return !m_bailingOut; |
| 113 | } |
| 114 | |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 115 | protected: |
| 116 | |
| 117 | SUPPRESS_ASAN |
| 118 | void recordJSFrame(Vector<UnprocessedStackFrame>& stackTrace) |
| 119 | { |
| 120 | CallSiteIndex callSiteIndex; |
| 121 | JSValue unsafeCallee = m_callFrame->unsafeCallee(); |
| 122 | CodeBlock* codeBlock = m_callFrame->unsafeCodeBlock(); |
| 123 | if (codeBlock) { |
| 124 | ASSERT(isValidCodeBlock(codeBlock)); |
| 125 | callSiteIndex = m_callFrame->unsafeCallSiteIndex(); |
| 126 | } |
| 127 | stackTrace[m_depth] = UnprocessedStackFrame(codeBlock, JSValue::encode(unsafeCallee), callSiteIndex); |
| 128 | m_depth++; |
| 129 | } |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 130 | |
sbarati@apple.com | 30cd116 | 2016-02-11 21:04:43 +0000 | [diff] [blame] | 131 | SUPPRESS_ASAN |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 132 | void advanceToParentFrame() |
| 133 | { |
sbarati@apple.com | 88a5fd6 | 2016-02-16 22:01:37 +0000 | [diff] [blame] | 134 | m_callFrame = m_callFrame->unsafeCallerFrame(m_vmEntryFrame); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | bool isAtTop() const |
| 138 | { |
| 139 | return !m_callFrame; |
| 140 | } |
| 141 | |
sbarati@apple.com | 30cd116 | 2016-02-11 21:04:43 +0000 | [diff] [blame] | 142 | SUPPRESS_ASAN |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 143 | void resetAtMachineFrame() |
| 144 | { |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 145 | if (isAtTop()) |
| 146 | return; |
| 147 | |
| 148 | if (!isValidFramePointer(m_callFrame)) { |
| 149 | // Guard against pausing the process at weird program points. |
| 150 | m_bailingOut = true; |
| 151 | if (sReportStats) |
| 152 | sNumFailedWalks++; |
| 153 | return; |
| 154 | } |
| 155 | |
sbarati@apple.com | 88a5fd6 | 2016-02-16 22:01:37 +0000 | [diff] [blame] | 156 | CodeBlock* codeBlock = m_callFrame->unsafeCodeBlock(); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 157 | if (!codeBlock) |
| 158 | return; |
| 159 | |
| 160 | if (!isValidCodeBlock(codeBlock)) { |
| 161 | m_bailingOut = true; |
| 162 | if (sReportStats) |
| 163 | sNumFailedWalks++; |
| 164 | return; |
| 165 | } |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 166 | } |
| 167 | |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 168 | bool isValidFramePointer(void* exec) |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 169 | { |
| 170 | uint8_t* fpCast = bitwise_cast<uint8_t*>(exec); |
| 171 | for (MachineThreads::Thread* thread = m_vm.heap.machineThreads().threadsListHead(m_machineThreadsLocker); thread; thread = thread->next) { |
| 172 | uint8_t* stackBase = static_cast<uint8_t*>(thread->stackBase); |
| 173 | uint8_t* stackLimit = static_cast<uint8_t*>(thread->stackEnd); |
| 174 | RELEASE_ASSERT(stackBase); |
| 175 | RELEASE_ASSERT(stackLimit); |
| 176 | if (fpCast <= stackBase && fpCast >= stackLimit) |
| 177 | return true; |
| 178 | } |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | bool isValidCodeBlock(CodeBlock* codeBlock) |
| 183 | { |
| 184 | if (!codeBlock) |
| 185 | return false; |
| 186 | bool result = m_vm.heap.codeBlockSet().contains(m_codeBlockSetLocker, codeBlock); |
| 187 | return result; |
| 188 | } |
| 189 | |
| 190 | VM& m_vm; |
| 191 | ExecState* m_callFrame; |
| 192 | VMEntryFrame* m_vmEntryFrame; |
mark.lam@apple.com | a2668e0 | 2017-03-09 21:05:41 +0000 | [diff] [blame^] | 193 | const AbstractLocker& m_codeBlockSetLocker; |
| 194 | const AbstractLocker& m_machineThreadsLocker; |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 195 | bool m_bailingOut { false }; |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 196 | size_t m_depth { 0 }; |
| 197 | }; |
| 198 | |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 199 | class CFrameWalker : public FrameWalker { |
| 200 | public: |
| 201 | typedef FrameWalker Base; |
| 202 | |
mark.lam@apple.com | a2668e0 | 2017-03-09 21:05:41 +0000 | [diff] [blame^] | 203 | CFrameWalker(VM& vm, void* machineFrame, ExecState* callFrame, const AbstractLocker& codeBlockSetLocker, const AbstractLocker& machineThreadsLocker) |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 204 | : Base(vm, callFrame, codeBlockSetLocker, machineThreadsLocker) |
| 205 | , m_machineFrame(machineFrame) |
| 206 | { |
| 207 | } |
| 208 | |
| 209 | size_t walk(Vector<UnprocessedStackFrame>& stackTrace, bool& didRunOutOfSpace) |
| 210 | { |
| 211 | if (sReportStats) |
| 212 | sNumTotalWalks++; |
| 213 | resetAtMachineFrame(); |
| 214 | size_t maxStackTraceSize = stackTrace.size(); |
| 215 | // The way the C walker decides if a frame it is about to trace is C or JS is by |
| 216 | // ensuring m_callFrame points to some frame above the machineFrame. |
| 217 | if (!isAtTop() && !m_bailingOut && m_machineFrame == m_callFrame) { |
| 218 | recordJSFrame(stackTrace); |
| 219 | Base::advanceToParentFrame(); |
| 220 | resetAtMachineFrame(); |
| 221 | } |
| 222 | |
| 223 | while (!isAtTop() && !m_bailingOut && m_depth < maxStackTraceSize) { |
| 224 | if (m_machineFrame >= m_callFrame) { |
| 225 | // If we get to this state we probably have an invalid trace. |
| 226 | m_bailingOut = true; |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | if (isCFrame()) { |
| 231 | RELEASE_ASSERT(!LLInt::isLLIntPC(frame()->callerFrame)); |
| 232 | stackTrace[m_depth] = UnprocessedStackFrame(frame()->pc); |
| 233 | m_depth++; |
| 234 | } else |
| 235 | recordJSFrame(stackTrace); |
| 236 | advanceToParentFrame(); |
| 237 | resetAtMachineFrame(); |
| 238 | } |
| 239 | didRunOutOfSpace = m_depth >= maxStackTraceSize && !isAtTop(); |
| 240 | reportStats(); |
| 241 | return m_depth; |
| 242 | } |
| 243 | |
| 244 | private: |
| 245 | |
| 246 | bool isCFrame() |
| 247 | { |
| 248 | return frame()->callerFrame != m_callFrame; |
| 249 | } |
| 250 | |
| 251 | void advanceToParentFrame() |
| 252 | { |
| 253 | if (!isCFrame()) |
| 254 | Base::advanceToParentFrame(); |
| 255 | m_machineFrame = frame()->callerFrame; |
| 256 | } |
| 257 | |
| 258 | void resetAtMachineFrame() |
| 259 | { |
| 260 | if (!isValidFramePointer(m_machineFrame)) { |
| 261 | // Guard against pausing the process at weird program points. |
| 262 | m_bailingOut = true; |
| 263 | if (sReportStats) |
| 264 | sNumFailedWalks++; |
| 265 | return; |
| 266 | } |
| 267 | Base::resetAtMachineFrame(); |
| 268 | } |
| 269 | |
| 270 | CallerFrameAndPC* frame() |
| 271 | { |
| 272 | return reinterpret_cast<CallerFrameAndPC*>(m_machineFrame); |
| 273 | } |
| 274 | |
| 275 | void* m_machineFrame; |
| 276 | }; |
| 277 | |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 278 | SamplingProfiler::SamplingProfiler(VM& vm, RefPtr<Stopwatch>&& stopwatch) |
| 279 | : m_vm(vm) |
| 280 | , m_stopwatch(WTFMove(stopwatch)) |
commit-queue@webkit.org | 18b9583 | 2016-04-26 16:47:33 +0000 | [diff] [blame] | 281 | , m_timingInterval(std::chrono::microseconds(Options::sampleInterval())) |
sbarati@apple.com | 207a683 | 2016-04-06 03:55:11 +0000 | [diff] [blame] | 282 | , m_threadIdentifier(0) |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 283 | , m_jscExecutionThread(nullptr) |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 284 | , m_isPaused(false) |
sbarati@apple.com | 207a683 | 2016-04-06 03:55:11 +0000 | [diff] [blame] | 285 | , m_isShutDown(false) |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 286 | { |
| 287 | if (sReportStats) { |
| 288 | sNumTotalWalks = 0; |
| 289 | sNumFailedWalks = 0; |
| 290 | } |
| 291 | |
| 292 | m_currentFrames.grow(256); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | SamplingProfiler::~SamplingProfiler() |
| 296 | { |
| 297 | } |
| 298 | |
mark.lam@apple.com | a2668e0 | 2017-03-09 21:05:41 +0000 | [diff] [blame^] | 299 | void SamplingProfiler::createThreadIfNecessary(const AbstractLocker&) |
sbarati@apple.com | 207a683 | 2016-04-06 03:55:11 +0000 | [diff] [blame] | 300 | { |
| 301 | ASSERT(m_lock.isLocked()); |
| 302 | |
| 303 | if (m_threadIdentifier) |
| 304 | return; |
| 305 | |
| 306 | RefPtr<SamplingProfiler> profiler = this; |
| 307 | m_threadIdentifier = createThread("jsc.sampling-profiler.thread", [profiler] { |
| 308 | profiler->timerLoop(); |
| 309 | }); |
| 310 | } |
| 311 | |
| 312 | void SamplingProfiler::timerLoop() |
| 313 | { |
| 314 | while (true) { |
| 315 | std::chrono::microseconds stackTraceProcessingTime = std::chrono::microseconds(0); |
| 316 | { |
| 317 | LockHolder locker(m_lock); |
| 318 | if (UNLIKELY(m_isShutDown)) |
| 319 | return; |
| 320 | |
| 321 | if (!m_isPaused && m_jscExecutionThread) |
| 322 | takeSample(locker, stackTraceProcessingTime); |
| 323 | |
| 324 | m_lastTime = m_stopwatch->elapsedTime(); |
| 325 | } |
| 326 | |
sbarati@apple.com | 51d8007 | 2016-06-14 02:29:26 +0000 | [diff] [blame] | 327 | // Read section 6.2 of this paper for more elaboration of why we add a random |
| 328 | // fluctuation here. The main idea is to prevent our timer from being in sync |
| 329 | // with some system process such as a scheduled context switch. |
| 330 | // http://plv.colorado.edu/papers/mytkowicz-pldi10.pdf |
| 331 | double randomSignedNumber = (randomNumber() * 2.0) - 1.0; // A random number between [-1, 1). |
| 332 | std::chrono::microseconds randomFluctuation = std::chrono::microseconds(static_cast<uint64_t>(randomSignedNumber * static_cast<double>(m_timingInterval.count()) * 0.20l)); |
| 333 | std::this_thread::sleep_for(m_timingInterval - std::min(m_timingInterval, stackTraceProcessingTime) + randomFluctuation); |
sbarati@apple.com | 207a683 | 2016-04-06 03:55:11 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
mark.lam@apple.com | a2668e0 | 2017-03-09 21:05:41 +0000 | [diff] [blame^] | 337 | void SamplingProfiler::takeSample(const AbstractLocker&, std::chrono::microseconds& stackTraceProcessingTime) |
sbarati@apple.com | 207a683 | 2016-04-06 03:55:11 +0000 | [diff] [blame] | 338 | { |
| 339 | ASSERT(m_lock.isLocked()); |
| 340 | if (m_vm.entryScope) { |
| 341 | double nowTime = m_stopwatch->elapsedTime(); |
| 342 | |
| 343 | LockHolder machineThreadsLocker(m_vm.heap.machineThreads().getLock()); |
| 344 | LockHolder codeBlockSetLocker(m_vm.heap.codeBlockSet().getLock()); |
| 345 | LockHolder executableAllocatorLocker(m_vm.executableAllocator.getLock()); |
| 346 | |
| 347 | bool didSuspend = m_jscExecutionThread->suspend(); |
| 348 | if (didSuspend) { |
| 349 | // While the JSC thread is suspended, we can't do things like malloc because the JSC thread |
| 350 | // may be holding the malloc lock. |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 351 | void* machineFrame; |
sbarati@apple.com | 207a683 | 2016-04-06 03:55:11 +0000 | [diff] [blame] | 352 | ExecState* callFrame; |
| 353 | void* machinePC; |
| 354 | bool topFrameIsLLInt = false; |
| 355 | void* llintPC; |
| 356 | { |
| 357 | MachineThreads::Thread::Registers registers; |
| 358 | m_jscExecutionThread->getRegisters(registers); |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 359 | machineFrame = registers.framePointer(); |
| 360 | callFrame = static_cast<ExecState*>(machineFrame); |
sbarati@apple.com | 207a683 | 2016-04-06 03:55:11 +0000 | [diff] [blame] | 361 | machinePC = registers.instructionPointer(); |
| 362 | llintPC = registers.llintPC(); |
| 363 | m_jscExecutionThread->freeRegisters(registers); |
| 364 | } |
| 365 | // FIXME: Lets have a way of detecting when we're parsing code. |
| 366 | // https://bugs.webkit.org/show_bug.cgi?id=152761 |
| 367 | if (m_vm.executableAllocator.isValidExecutableMemory(executableAllocatorLocker, machinePC)) { |
| 368 | if (m_vm.isExecutingInRegExpJIT) { |
| 369 | // FIXME: We're executing a regexp. Lets gather more intersting data. |
| 370 | // https://bugs.webkit.org/show_bug.cgi?id=152729 |
| 371 | callFrame = m_vm.topCallFrame; // We need to do this or else we'd fail our backtrace validation b/c this isn't a JS frame. |
| 372 | } |
| 373 | } else if (LLInt::isLLIntPC(machinePC)) { |
| 374 | topFrameIsLLInt = true; |
| 375 | // We're okay to take a normal stack trace when the PC |
| 376 | // is in LLInt code. |
| 377 | } else { |
| 378 | // We resort to topCallFrame to see if we can get anything |
| 379 | // useful. We usually get here when we're executing C code. |
| 380 | callFrame = m_vm.topCallFrame; |
| 381 | } |
| 382 | |
| 383 | size_t walkSize; |
| 384 | bool wasValidWalk; |
| 385 | bool didRunOutOfVectorSpace; |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 386 | if (Options::sampleCCode()) { |
| 387 | CFrameWalker walker(m_vm, machineFrame, callFrame, codeBlockSetLocker, machineThreadsLocker); |
| 388 | walkSize = walker.walk(m_currentFrames, didRunOutOfVectorSpace); |
| 389 | wasValidWalk = walker.wasValidWalk(); |
| 390 | } else { |
| 391 | FrameWalker walker(m_vm, callFrame, codeBlockSetLocker, machineThreadsLocker); |
sbarati@apple.com | 207a683 | 2016-04-06 03:55:11 +0000 | [diff] [blame] | 392 | walkSize = walker.walk(m_currentFrames, didRunOutOfVectorSpace); |
| 393 | wasValidWalk = walker.wasValidWalk(); |
| 394 | } |
| 395 | |
| 396 | m_jscExecutionThread->resume(); |
| 397 | |
| 398 | auto startTime = std::chrono::steady_clock::now(); |
| 399 | // We can now use data structures that malloc, and do other interesting things, again. |
| 400 | |
| 401 | // FIXME: It'd be interesting to take data about the program's state when |
| 402 | // we fail to take a stack trace: https://bugs.webkit.org/show_bug.cgi?id=152758 |
| 403 | if (wasValidWalk && walkSize) { |
| 404 | if (sReportStats) |
| 405 | sNumTotalStackTraces++; |
| 406 | Vector<UnprocessedStackFrame> stackTrace; |
| 407 | stackTrace.reserveInitialCapacity(walkSize); |
| 408 | for (size_t i = 0; i < walkSize; i++) { |
| 409 | UnprocessedStackFrame frame = m_currentFrames[i]; |
| 410 | stackTrace.uncheckedAppend(frame); |
| 411 | } |
| 412 | |
| 413 | m_unprocessedStackTraces.append(UnprocessedStackTrace { nowTime, machinePC, topFrameIsLLInt, llintPC, WTFMove(stackTrace) }); |
| 414 | |
| 415 | if (didRunOutOfVectorSpace) |
| 416 | m_currentFrames.grow(m_currentFrames.size() * 1.25); |
| 417 | } |
| 418 | |
| 419 | auto endTime = std::chrono::steady_clock::now(); |
| 420 | stackTraceProcessingTime = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 425 | static ALWAYS_INLINE unsigned tryGetBytecodeIndex(unsigned llintPC, CodeBlock* codeBlock, bool& isValid) |
| 426 | { |
ossy@webkit.org | c8cea7b | 2016-02-18 16:45:26 +0000 | [diff] [blame] | 427 | #if ENABLE(DFG_JIT) |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 428 | RELEASE_ASSERT(!codeBlock->hasCodeOrigins()); |
ossy@webkit.org | c8cea7b | 2016-02-18 16:45:26 +0000 | [diff] [blame] | 429 | #endif |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 430 | |
| 431 | #if USE(JSVALUE64) |
| 432 | unsigned bytecodeIndex = llintPC; |
| 433 | if (bytecodeIndex < codeBlock->instructionCount()) { |
| 434 | isValid = true; |
| 435 | return bytecodeIndex; |
| 436 | } |
| 437 | isValid = false; |
| 438 | return 0; |
| 439 | #else |
| 440 | Instruction* instruction = bitwise_cast<Instruction*>(llintPC); |
| 441 | if (instruction >= codeBlock->instructions().begin() && instruction < codeBlock->instructions().begin() + codeBlock->instructionCount()) { |
| 442 | isValid = true; |
| 443 | unsigned bytecodeIndex = instruction - codeBlock->instructions().begin(); |
| 444 | return bytecodeIndex; |
| 445 | } |
| 446 | isValid = false; |
| 447 | return 0; |
| 448 | #endif |
| 449 | } |
| 450 | |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 451 | void SamplingProfiler::processUnverifiedStackTraces() |
| 452 | { |
| 453 | // This function needs to be called from the JSC execution thread. |
| 454 | RELEASE_ASSERT(m_lock.isLocked()); |
| 455 | |
| 456 | TinyBloomFilter filter = m_vm.heap.objectSpace().blocks().filter(); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 457 | |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 458 | for (UnprocessedStackTrace& unprocessedStackTrace : m_unprocessedStackTraces) { |
| 459 | m_stackTraces.append(StackTrace()); |
| 460 | StackTrace& stackTrace = m_stackTraces.last(); |
| 461 | stackTrace.timestamp = unprocessedStackTrace.timestamp; |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 462 | |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 463 | auto populateCodeLocation = [] (CodeBlock* codeBlock, unsigned bytecodeIndex, StackFrame::CodeLocation& location) { |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 464 | if (bytecodeIndex < codeBlock->instructionCount()) { |
| 465 | int divot; |
| 466 | int startOffset; |
| 467 | int endOffset; |
| 468 | codeBlock->expressionRangeForBytecodeOffset(bytecodeIndex, divot, startOffset, endOffset, |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 469 | location.lineNumber, location.columnNumber); |
| 470 | location.bytecodeIndex = bytecodeIndex; |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 471 | } |
sbarati@apple.com | 38496ee | 2016-04-21 00:55:03 +0000 | [diff] [blame] | 472 | if (Options::collectSamplingProfilerDataForJSCShell()) { |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 473 | location.codeBlockHash = codeBlock->hash(); |
| 474 | location.jitType = codeBlock->jitType(); |
sbarati@apple.com | 38496ee | 2016-04-21 00:55:03 +0000 | [diff] [blame] | 475 | } |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 476 | }; |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 477 | |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 478 | auto appendCodeBlock = [&] (CodeBlock* codeBlock, unsigned bytecodeIndex) { |
| 479 | stackTrace.frames.append(StackFrame(codeBlock->ownerExecutable())); |
| 480 | m_liveCellPointers.add(codeBlock->ownerExecutable()); |
| 481 | populateCodeLocation(codeBlock, bytecodeIndex, stackTrace.frames.last().semanticLocation); |
| 482 | }; |
| 483 | |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 484 | auto appendEmptyFrame = [&] { |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 485 | stackTrace.frames.append(StackFrame()); |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 486 | }; |
| 487 | |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 488 | auto storeCalleeIntoLastFrame = [&] (EncodedJSValue encodedCallee) { |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 489 | // Set the callee if it's a valid GC object. |
| 490 | JSValue callee = JSValue::decode(encodedCallee); |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 491 | StackFrame& stackFrame = stackTrace.frames.last(); |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 492 | bool alreadyHasExecutable = !!stackFrame.executable; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 493 | if (!HeapUtil::isValueGCObject(m_vm.heap, filter, callee)) { |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 494 | if (!alreadyHasExecutable) |
| 495 | stackFrame.frameType = FrameType::Unknown; |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 496 | return; |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | JSCell* calleeCell = callee.asCell(); |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 500 | auto setFallbackFrameType = [&] { |
| 501 | ASSERT(!alreadyHasExecutable); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 502 | FrameType result = FrameType::Unknown; |
| 503 | CallData callData; |
| 504 | CallType callType; |
| 505 | callType = getCallData(calleeCell, callData); |
utatane.tea@gmail.com | f76f1b4 | 2016-03-05 17:01:04 +0000 | [diff] [blame] | 506 | if (callType == CallType::Host) |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 507 | result = FrameType::Host; |
| 508 | |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 509 | stackFrame.frameType = result; |
| 510 | }; |
| 511 | |
| 512 | auto addCallee = [&] (JSObject* callee) { |
| 513 | stackFrame.callee = callee; |
| 514 | m_liveCellPointers.add(callee); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 515 | }; |
| 516 | |
| 517 | if (calleeCell->type() != JSFunctionType) { |
keith_miller@apple.com | 45da760 | 2017-01-27 01:47:52 +0000 | [diff] [blame] | 518 | if (JSObject* object = jsDynamicCast<JSObject*>(*calleeCell->vm(), calleeCell)) |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 519 | addCallee(object); |
| 520 | |
| 521 | if (!alreadyHasExecutable) |
| 522 | setFallbackFrameType(); |
| 523 | |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 524 | return; |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 525 | } |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 526 | |
| 527 | addCallee(jsCast<JSFunction*>(calleeCell)); |
| 528 | |
| 529 | if (alreadyHasExecutable) |
| 530 | return; |
| 531 | |
| 532 | ExecutableBase* executable = jsCast<JSFunction*>(calleeCell)->executable(); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 533 | if (!executable) { |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 534 | setFallbackFrameType(); |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 535 | return; |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 536 | } |
| 537 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 538 | RELEASE_ASSERT(HeapUtil::isPointerGCObjectJSCell(m_vm.heap, filter, executable)); |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 539 | stackFrame.frameType = FrameType::Executable; |
| 540 | stackFrame.executable = executable; |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 541 | m_liveCellPointers.add(executable); |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 542 | }; |
| 543 | |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 544 | auto appendCodeOrigin = [&] (CodeBlock* machineCodeBlock, CodeOrigin origin) { |
| 545 | size_t startIndex = stackTrace.frames.size(); // We want to change stack traces that we're about to append. |
| 546 | |
| 547 | CodeOrigin machineOrigin; |
| 548 | origin.walkUpInlineStack([&] (const CodeOrigin& codeOrigin) { |
| 549 | machineOrigin = codeOrigin; |
| 550 | appendCodeBlock(codeOrigin.inlineCallFrame ? codeOrigin.inlineCallFrame->baselineCodeBlock.get() : machineCodeBlock, codeOrigin.bytecodeIndex); |
| 551 | }); |
| 552 | |
| 553 | if (Options::collectSamplingProfilerDataForJSCShell()) { |
| 554 | RELEASE_ASSERT(machineOrigin.isSet()); |
| 555 | RELEASE_ASSERT(!machineOrigin.inlineCallFrame); |
| 556 | |
| 557 | StackFrame::CodeLocation machineLocation = stackTrace.frames.last().semanticLocation; |
| 558 | |
| 559 | // We want to tell each inlined frame about the machine frame |
| 560 | // they were inlined into. Currently, we only use this for dumping |
| 561 | // output on the command line, but we could extend it to the web |
| 562 | // inspector in the future if we find a need for it there. |
| 563 | RELEASE_ASSERT(stackTrace.frames.size()); |
| 564 | for (size_t i = startIndex; i < stackTrace.frames.size() - 1; i++) |
| 565 | stackTrace.frames[i].machineLocation = std::make_pair(machineLocation, Strong<CodeBlock>(m_vm, machineCodeBlock)); |
| 566 | } |
| 567 | }; |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 568 | |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 569 | // Prepend the top-most inlined frame if needed and gather |
| 570 | // location information about where the top frame is executing. |
| 571 | size_t startIndex = 0; |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 572 | if (unprocessedStackTrace.frames.size() && !!unprocessedStackTrace.frames[0].verifiedCodeBlock) { |
| 573 | CodeBlock* topCodeBlock = unprocessedStackTrace.frames[0].verifiedCodeBlock; |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 574 | if (unprocessedStackTrace.topFrameIsLLInt) { |
| 575 | // We reuse LLInt CodeBlocks for the baseline JIT, so we need to check for both jit types. |
| 576 | // This might also be false for various reasons (known and unknown), even though |
| 577 | // it's super unlikely. One reason that this can be false is when we throw from a DFG frame, |
| 578 | // and we end up having to unwind past a VMEntryFrame, we will end up executing |
| 579 | // inside the LLInt's handleUncaughtException. So we just protect against this |
| 580 | // by ignoring it. |
| 581 | unsigned bytecodeIndex = 0; |
| 582 | if (topCodeBlock->jitType() == JITCode::InterpreterThunk || topCodeBlock->jitType() == JITCode::BaselineJIT) { |
| 583 | bool isValidPC; |
| 584 | unsigned bits; |
| 585 | #if USE(JSVALUE64) |
| 586 | bits = static_cast<unsigned>(bitwise_cast<uintptr_t>(unprocessedStackTrace.llintPC)); |
| 587 | #else |
| 588 | bits = bitwise_cast<unsigned>(unprocessedStackTrace.llintPC); |
| 589 | #endif |
| 590 | bytecodeIndex = tryGetBytecodeIndex(bits, topCodeBlock, isValidPC); |
| 591 | |
| 592 | UNUSED_PARAM(isValidPC); // FIXME: do something with this info for the web inspector: https://bugs.webkit.org/show_bug.cgi?id=153455 |
| 593 | |
| 594 | appendCodeBlock(topCodeBlock, bytecodeIndex); |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 595 | storeCalleeIntoLastFrame(unprocessedStackTrace.frames[0].unverifiedCallee); |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 596 | startIndex = 1; |
| 597 | } |
utatane.tea@gmail.com | 4392696 | 2016-11-27 06:08:16 +0000 | [diff] [blame] | 598 | } else if (std::optional<CodeOrigin> codeOrigin = topCodeBlock->findPC(unprocessedStackTrace.topPC)) { |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 599 | appendCodeOrigin(topCodeBlock, *codeOrigin); |
| 600 | storeCalleeIntoLastFrame(unprocessedStackTrace.frames[0].unverifiedCallee); |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 601 | startIndex = 1; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | for (size_t i = startIndex; i < unprocessedStackTrace.frames.size(); i++) { |
| 606 | UnprocessedStackFrame& unprocessedStackFrame = unprocessedStackTrace.frames[i]; |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 607 | if (CodeBlock* codeBlock = unprocessedStackFrame.verifiedCodeBlock) { |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 608 | CallSiteIndex callSiteIndex = unprocessedStackFrame.callSiteIndex; |
| 609 | |
| 610 | auto appendCodeBlockNoInlining = [&] { |
| 611 | bool isValidPC; |
| 612 | appendCodeBlock(codeBlock, tryGetBytecodeIndex(callSiteIndex.bits(), codeBlock, isValidPC)); |
| 613 | }; |
| 614 | |
| 615 | #if ENABLE(DFG_JIT) |
| 616 | if (codeBlock->hasCodeOrigins()) { |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 617 | if (codeBlock->canGetCodeOrigin(callSiteIndex)) |
| 618 | appendCodeOrigin(codeBlock, codeBlock->codeOrigin(callSiteIndex)); |
| 619 | else |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 620 | appendCodeBlock(codeBlock, std::numeric_limits<unsigned>::max()); |
| 621 | } else |
| 622 | appendCodeBlockNoInlining(); |
| 623 | #else |
| 624 | appendCodeBlockNoInlining(); |
| 625 | #endif |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 626 | } else if (unprocessedStackFrame.cCodePC) { |
| 627 | appendEmptyFrame(); |
| 628 | stackTrace.frames.last().cCodePC = unprocessedStackFrame.cCodePC; |
| 629 | stackTrace.frames.last().frameType = FrameType::C; |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 630 | } else |
| 631 | appendEmptyFrame(); |
| 632 | |
| 633 | // Note that this is okay to do if we walked the inline stack because |
| 634 | // the machine frame will be at the top of the processed stack trace. |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 635 | if (!unprocessedStackFrame.cCodePC) |
| 636 | storeCalleeIntoLastFrame(unprocessedStackFrame.unverifiedCallee); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 640 | m_unprocessedStackTraces.clear(); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | void SamplingProfiler::visit(SlotVisitor& slotVisitor) |
| 644 | { |
| 645 | RELEASE_ASSERT(m_lock.isLocked()); |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 646 | for (JSCell* cell : m_liveCellPointers) |
fpizlo@apple.com | f7240e0 | 2016-12-16 02:16:19 +0000 | [diff] [blame] | 647 | slotVisitor.appendUnbarriered(cell); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void SamplingProfiler::shutdown() |
| 651 | { |
sbarati@apple.com | 207a683 | 2016-04-06 03:55:11 +0000 | [diff] [blame] | 652 | LockHolder locker(m_lock); |
| 653 | m_isShutDown = true; |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void SamplingProfiler::start() |
| 657 | { |
| 658 | LockHolder locker(m_lock); |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 659 | start(locker); |
| 660 | } |
| 661 | |
mark.lam@apple.com | a2668e0 | 2017-03-09 21:05:41 +0000 | [diff] [blame^] | 662 | void SamplingProfiler::start(const AbstractLocker& locker) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 663 | { |
| 664 | ASSERT(m_lock.isLocked()); |
sbarati@apple.com | 207a683 | 2016-04-06 03:55:11 +0000 | [diff] [blame] | 665 | m_isPaused = false; |
| 666 | createThreadIfNecessary(locker); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 667 | } |
| 668 | |
mark.lam@apple.com | a2668e0 | 2017-03-09 21:05:41 +0000 | [diff] [blame^] | 669 | void SamplingProfiler::pause(const AbstractLocker&) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 670 | { |
| 671 | ASSERT(m_lock.isLocked()); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 672 | m_isPaused = true; |
| 673 | reportStats(); |
| 674 | } |
| 675 | |
mark.lam@apple.com | a2668e0 | 2017-03-09 21:05:41 +0000 | [diff] [blame^] | 676 | void SamplingProfiler::noticeCurrentThreadAsJSCExecutionThread(const AbstractLocker&) |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 677 | { |
| 678 | ASSERT(m_lock.isLocked()); |
| 679 | m_jscExecutionThread = m_vm.heap.machineThreads().machineThreadForCurrentThread(); |
| 680 | } |
| 681 | |
| 682 | void SamplingProfiler::noticeCurrentThreadAsJSCExecutionThread() |
| 683 | { |
| 684 | LockHolder locker(m_lock); |
| 685 | noticeCurrentThreadAsJSCExecutionThread(locker); |
| 686 | } |
| 687 | |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 688 | void SamplingProfiler::noticeJSLockAcquisition() |
| 689 | { |
| 690 | LockHolder locker(m_lock); |
| 691 | noticeCurrentThreadAsJSCExecutionThread(locker); |
| 692 | } |
| 693 | |
| 694 | void SamplingProfiler::noticeVMEntry() |
| 695 | { |
| 696 | LockHolder locker(m_lock); |
| 697 | ASSERT(m_vm.entryScope); |
| 698 | noticeCurrentThreadAsJSCExecutionThread(locker); |
| 699 | m_lastTime = m_stopwatch->elapsedTime(); |
sbarati@apple.com | 207a683 | 2016-04-06 03:55:11 +0000 | [diff] [blame] | 700 | createThreadIfNecessary(locker); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 701 | } |
| 702 | |
mark.lam@apple.com | a2668e0 | 2017-03-09 21:05:41 +0000 | [diff] [blame^] | 703 | void SamplingProfiler::clearData(const AbstractLocker&) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 704 | { |
| 705 | ASSERT(m_lock.isLocked()); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 706 | m_stackTraces.clear(); |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 707 | m_liveCellPointers.clear(); |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 708 | m_unprocessedStackTraces.clear(); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 709 | } |
| 710 | |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 711 | String SamplingProfiler::StackFrame::nameFromCallee(VM& vm) |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 712 | { |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 713 | if (!callee) |
| 714 | return String(); |
| 715 | |
mark.lam@apple.com | 09f92b8 | 2016-09-27 20:32:13 +0000 | [diff] [blame] | 716 | auto scope = DECLARE_CATCH_SCOPE(vm); |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 717 | ExecState* exec = callee->globalObject()->globalExec(); |
| 718 | auto getPropertyIfPureOperation = [&] (const Identifier& ident) -> String { |
sbarati@apple.com | ea97413 | 2016-02-17 22:11:39 +0000 | [diff] [blame] | 719 | PropertySlot slot(callee, PropertySlot::InternalMethodType::VMInquiry); |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 720 | PropertyName propertyName(ident); |
mark.lam@apple.com | 09f92b8 | 2016-09-27 20:32:13 +0000 | [diff] [blame] | 721 | bool hasProperty = callee->getPropertySlot(exec, propertyName, slot); |
| 722 | ASSERT_UNUSED(scope, !scope.exception()); |
| 723 | if (hasProperty) { |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 724 | if (slot.isValue()) { |
| 725 | JSValue nameValue = slot.getValue(exec, propertyName); |
| 726 | if (isJSString(nameValue)) |
| 727 | return asString(nameValue)->tryGetValue(); |
| 728 | } |
| 729 | } |
| 730 | return String(); |
| 731 | }; |
| 732 | |
| 733 | String name = getPropertyIfPureOperation(vm.propertyNames->displayName); |
| 734 | if (!name.isEmpty()) |
| 735 | return name; |
| 736 | |
| 737 | return getPropertyIfPureOperation(vm.propertyNames->name); |
| 738 | } |
| 739 | |
| 740 | String SamplingProfiler::StackFrame::displayName(VM& vm) |
| 741 | { |
| 742 | { |
| 743 | String name = nameFromCallee(vm); |
| 744 | if (!name.isEmpty()) |
| 745 | return name; |
| 746 | } |
| 747 | |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 748 | if (frameType == FrameType::Unknown || frameType == FrameType::C) { |
| 749 | #if OS(DARWIN) |
| 750 | if (frameType == FrameType::C) { |
| 751 | const char* mangledName = nullptr; |
| 752 | const char* cxaDemangled = nullptr; |
| 753 | Dl_info info; |
| 754 | if (dladdr(cCodePC, &info) && info.dli_sname) |
| 755 | mangledName = info.dli_sname; |
| 756 | if (mangledName) { |
| 757 | cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0); |
| 758 | return String(cxaDemangled ? cxaDemangled : mangledName); |
| 759 | } |
| 760 | WTF::dataLog("couldn't get a name"); |
| 761 | } |
| 762 | #endif |
keith_miller@apple.com | ac5c1c2 | 2017-02-02 01:49:20 +0000 | [diff] [blame] | 763 | return ASCIILiteral("(unknown)"); |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 764 | } |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 765 | if (frameType == FrameType::Host) |
| 766 | return ASCIILiteral("(host)"); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 767 | |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 768 | if (executable->isHostFunction()) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 769 | return static_cast<NativeExecutable*>(executable)->name(); |
| 770 | |
| 771 | if (executable->isFunctionExecutable()) |
| 772 | return static_cast<FunctionExecutable*>(executable)->inferredName().string(); |
| 773 | if (executable->isProgramExecutable() || executable->isEvalExecutable()) |
| 774 | return ASCIILiteral("(program)"); |
| 775 | if (executable->isModuleProgramExecutable()) |
| 776 | return ASCIILiteral("(module)"); |
| 777 | |
| 778 | RELEASE_ASSERT_NOT_REACHED(); |
| 779 | return String(); |
| 780 | } |
| 781 | |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 782 | String SamplingProfiler::StackFrame::displayNameForJSONTests(VM& vm) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 783 | { |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 784 | { |
| 785 | String name = nameFromCallee(vm); |
| 786 | if (!name.isEmpty()) |
| 787 | return name; |
| 788 | } |
| 789 | |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 790 | if (frameType == FrameType::Unknown || frameType == FrameType::C) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 791 | return ASCIILiteral("(unknown)"); |
| 792 | if (frameType == FrameType::Host) |
| 793 | return ASCIILiteral("(host)"); |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 794 | |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 795 | if (executable->isHostFunction()) |
| 796 | return static_cast<NativeExecutable*>(executable)->name(); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 797 | |
| 798 | if (executable->isFunctionExecutable()) { |
| 799 | String result = static_cast<FunctionExecutable*>(executable)->inferredName().string(); |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 800 | if (result.isEmpty()) |
| 801 | return ASCIILiteral("(anonymous function)"); |
| 802 | return result; |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 803 | } |
| 804 | if (executable->isEvalExecutable()) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 805 | return ASCIILiteral("(eval)"); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 806 | if (executable->isProgramExecutable()) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 807 | return ASCIILiteral("(program)"); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 808 | if (executable->isModuleProgramExecutable()) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 809 | return ASCIILiteral("(module)"); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 810 | |
| 811 | RELEASE_ASSERT_NOT_REACHED(); |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 812 | return String(); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 813 | } |
| 814 | |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 815 | int SamplingProfiler::StackFrame::functionStartLine() |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 816 | { |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 817 | if (frameType == FrameType::Unknown || frameType == FrameType::Host || frameType == FrameType::C) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 818 | return -1; |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 819 | |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 820 | if (executable->isHostFunction()) |
| 821 | return -1; |
| 822 | return static_cast<ScriptExecutable*>(executable)->firstLine(); |
| 823 | } |
| 824 | |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 825 | unsigned SamplingProfiler::StackFrame::functionStartColumn() |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 826 | { |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 827 | if (frameType == FrameType::Unknown || frameType == FrameType::Host || frameType == FrameType::C) |
peavo@outlook.com | a83d48a | 2016-05-02 21:20:15 +0000 | [diff] [blame] | 828 | return std::numeric_limits<unsigned>::max(); |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 829 | |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 830 | if (executable->isHostFunction()) |
peavo@outlook.com | a83d48a | 2016-05-02 21:20:15 +0000 | [diff] [blame] | 831 | return std::numeric_limits<unsigned>::max(); |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 832 | |
| 833 | return static_cast<ScriptExecutable*>(executable)->startColumn(); |
| 834 | } |
| 835 | |
| 836 | intptr_t SamplingProfiler::StackFrame::sourceID() |
| 837 | { |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 838 | if (frameType == FrameType::Unknown || frameType == FrameType::Host || frameType == FrameType::C) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 839 | return -1; |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 840 | |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 841 | if (executable->isHostFunction()) |
| 842 | return -1; |
| 843 | |
| 844 | return static_cast<ScriptExecutable*>(executable)->sourceID(); |
| 845 | } |
| 846 | |
| 847 | String SamplingProfiler::StackFrame::url() |
| 848 | { |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 849 | if (frameType == FrameType::Unknown || frameType == FrameType::Host || frameType == FrameType::C) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 850 | return emptyString(); |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 851 | |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 852 | if (executable->isHostFunction()) |
| 853 | return emptyString(); |
| 854 | |
| 855 | String url = static_cast<ScriptExecutable*>(executable)->sourceURL(); |
| 856 | if (url.isEmpty()) |
| 857 | return static_cast<ScriptExecutable*>(executable)->source().provider()->sourceURL(); // Fall back to sourceURL directive. |
| 858 | return url; |
| 859 | } |
| 860 | |
mark.lam@apple.com | a2668e0 | 2017-03-09 21:05:41 +0000 | [diff] [blame^] | 861 | Vector<SamplingProfiler::StackTrace> SamplingProfiler::releaseStackTraces(const AbstractLocker& locker) |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 862 | { |
| 863 | ASSERT(m_lock.isLocked()); |
| 864 | { |
| 865 | HeapIterationScope heapIterationScope(m_vm.heap); |
| 866 | processUnverifiedStackTraces(); |
| 867 | } |
| 868 | |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 869 | Vector<StackTrace> result(WTFMove(m_stackTraces)); |
| 870 | clearData(locker); |
| 871 | return result; |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | String SamplingProfiler::stackTracesAsJSON() |
| 875 | { |
fpizlo@apple.com | 0ef4395 | 2016-12-08 22:14:50 +0000 | [diff] [blame] | 876 | DeferGC deferGC(m_vm.heap); |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 877 | LockHolder locker(m_lock); |
| 878 | |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 879 | { |
| 880 | HeapIterationScope heapIterationScope(m_vm.heap); |
| 881 | processUnverifiedStackTraces(); |
| 882 | } |
| 883 | |
| 884 | StringBuilder json; |
commit-queue@webkit.org | 13c7bcc | 2016-03-02 04:30:45 +0000 | [diff] [blame] | 885 | json.append('['); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 886 | |
| 887 | bool loopedOnce = false; |
| 888 | auto comma = [&] { |
| 889 | if (loopedOnce) |
commit-queue@webkit.org | 13c7bcc | 2016-03-02 04:30:45 +0000 | [diff] [blame] | 890 | json.append(','); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 891 | }; |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 892 | for (StackTrace& stackTrace : m_stackTraces) { |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 893 | comma(); |
commit-queue@webkit.org | 13c7bcc | 2016-03-02 04:30:45 +0000 | [diff] [blame] | 894 | json.append('['); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 895 | loopedOnce = false; |
sbarati@apple.com | 806ca4a | 2016-01-20 21:51:00 +0000 | [diff] [blame] | 896 | for (StackFrame& stackFrame : stackTrace.frames) { |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 897 | comma(); |
commit-queue@webkit.org | 13c7bcc | 2016-03-02 04:30:45 +0000 | [diff] [blame] | 898 | json.append('"'); |
sbarati@apple.com | 8ec5c26 | 2016-02-04 21:51:40 +0000 | [diff] [blame] | 899 | json.append(stackFrame.displayNameForJSONTests(m_vm)); |
commit-queue@webkit.org | 13c7bcc | 2016-03-02 04:30:45 +0000 | [diff] [blame] | 900 | json.append('"'); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 901 | loopedOnce = true; |
| 902 | } |
commit-queue@webkit.org | 13c7bcc | 2016-03-02 04:30:45 +0000 | [diff] [blame] | 903 | json.append(']'); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 904 | loopedOnce = true; |
| 905 | } |
| 906 | |
commit-queue@webkit.org | 13c7bcc | 2016-03-02 04:30:45 +0000 | [diff] [blame] | 907 | json.append(']'); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 908 | |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 909 | clearData(locker); |
| 910 | |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 911 | return json.toString(); |
| 912 | } |
| 913 | |
keith_miller@apple.com | 3517aa4 | 2016-05-24 23:03:09 +0000 | [diff] [blame] | 914 | void SamplingProfiler::registerForReportAtExit() |
| 915 | { |
| 916 | static StaticLock registrationLock; |
| 917 | static HashSet<RefPtr<SamplingProfiler>>* profilesToReport; |
| 918 | |
| 919 | LockHolder holder(registrationLock); |
| 920 | |
| 921 | if (!profilesToReport) { |
| 922 | profilesToReport = new HashSet<RefPtr<SamplingProfiler>>(); |
| 923 | atexit([]() { |
| 924 | for (auto profile : *profilesToReport) |
| 925 | profile->reportDataToOptionFile(); |
| 926 | }); |
| 927 | } |
| 928 | |
| 929 | profilesToReport->add(adoptRef(this)); |
| 930 | m_needsReportAtExit = true; |
| 931 | } |
| 932 | |
| 933 | void SamplingProfiler::reportDataToOptionFile() |
| 934 | { |
| 935 | if (m_needsReportAtExit) { |
| 936 | m_needsReportAtExit = false; |
| 937 | const char* path = Options::samplingProfilerPath(); |
| 938 | StringPrintStream pathOut; |
| 939 | pathOut.print(path, "/"); |
| 940 | pathOut.print("JSCSampilingProfile-", reinterpret_cast<uintptr_t>(this), ".txt"); |
| 941 | auto out = FilePrintStream::open(pathOut.toCString().data(), "w"); |
| 942 | reportTopFunctions(*out); |
| 943 | reportTopBytecodes(*out); |
| 944 | } |
| 945 | } |
| 946 | |
sbarati@apple.com | 10ec78a | 2016-04-20 02:24:53 +0000 | [diff] [blame] | 947 | void SamplingProfiler::reportTopFunctions() |
| 948 | { |
keith_miller@apple.com | 3517aa4 | 2016-05-24 23:03:09 +0000 | [diff] [blame] | 949 | reportTopFunctions(WTF::dataFile()); |
| 950 | } |
| 951 | |
| 952 | void SamplingProfiler::reportTopFunctions(PrintStream& out) |
| 953 | { |
sbarati@apple.com | 10ec78a | 2016-04-20 02:24:53 +0000 | [diff] [blame] | 954 | LockHolder locker(m_lock); |
| 955 | |
| 956 | { |
| 957 | HeapIterationScope heapIterationScope(m_vm.heap); |
| 958 | processUnverifiedStackTraces(); |
| 959 | } |
| 960 | |
| 961 | |
| 962 | HashMap<String, size_t> functionCounts; |
| 963 | for (StackTrace& stackTrace : m_stackTraces) { |
| 964 | if (!stackTrace.frames.size()) |
| 965 | continue; |
| 966 | |
| 967 | StackFrame& frame = stackTrace.frames.first(); |
| 968 | String frameDescription = makeString(frame.displayName(m_vm), ":", String::number(frame.sourceID())); |
| 969 | functionCounts.add(frameDescription, 0).iterator->value++; |
| 970 | } |
| 971 | |
| 972 | auto takeMax = [&] () -> std::pair<String, size_t> { |
| 973 | String maxFrameDescription; |
| 974 | size_t maxFrameCount = 0; |
| 975 | for (auto entry : functionCounts) { |
| 976 | if (entry.value > maxFrameCount) { |
| 977 | maxFrameCount = entry.value; |
| 978 | maxFrameDescription = entry.key; |
| 979 | } |
| 980 | } |
| 981 | if (!maxFrameDescription.isEmpty()) |
| 982 | functionCounts.remove(maxFrameDescription); |
| 983 | return std::make_pair(maxFrameDescription, maxFrameCount); |
| 984 | }; |
| 985 | |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 986 | if (Options::samplingProfilerTopFunctionsCount()) { |
| 987 | out.print("\n\nSampling rate: ", m_timingInterval.count(), " microseconds\n"); |
| 988 | out.print("Top functions as <numSamples 'functionName:sourceID'>\n"); |
| 989 | for (size_t i = 0; i < Options::samplingProfilerTopFunctionsCount(); i++) { |
| 990 | auto pair = takeMax(); |
| 991 | if (pair.first.isEmpty()) |
| 992 | break; |
| 993 | out.printf("%6zu ", pair.second); |
| 994 | out.print(" '", pair.first, "'\n"); |
| 995 | } |
sbarati@apple.com | 10ec78a | 2016-04-20 02:24:53 +0000 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | |
| 999 | void SamplingProfiler::reportTopBytecodes() |
| 1000 | { |
keith_miller@apple.com | 3517aa4 | 2016-05-24 23:03:09 +0000 | [diff] [blame] | 1001 | reportTopBytecodes(WTF::dataFile()); |
| 1002 | } |
| 1003 | |
| 1004 | void SamplingProfiler::reportTopBytecodes(PrintStream& out) |
| 1005 | { |
sbarati@apple.com | 10ec78a | 2016-04-20 02:24:53 +0000 | [diff] [blame] | 1006 | LockHolder locker(m_lock); |
| 1007 | |
| 1008 | { |
| 1009 | HeapIterationScope heapIterationScope(m_vm.heap); |
| 1010 | processUnverifiedStackTraces(); |
| 1011 | } |
| 1012 | |
| 1013 | HashMap<String, size_t> bytecodeCounts; |
| 1014 | for (StackTrace& stackTrace : m_stackTraces) { |
| 1015 | if (!stackTrace.frames.size()) |
| 1016 | continue; |
| 1017 | |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 1018 | auto descriptionForLocation = [&] (StackFrame::CodeLocation location) -> String { |
| 1019 | String bytecodeIndex; |
| 1020 | String codeBlockHash; |
| 1021 | if (location.hasBytecodeIndex()) |
| 1022 | bytecodeIndex = String::number(location.bytecodeIndex); |
| 1023 | else |
| 1024 | bytecodeIndex = "<nil>"; |
| 1025 | |
| 1026 | if (location.hasCodeBlockHash()) { |
| 1027 | StringPrintStream stream; |
| 1028 | location.codeBlockHash.dump(stream); |
| 1029 | codeBlockHash = stream.toString(); |
| 1030 | } else |
| 1031 | codeBlockHash = "<nil>"; |
| 1032 | |
| 1033 | return makeString("#", codeBlockHash, ":", JITCode::typeName(location.jitType), ":", bytecodeIndex); |
| 1034 | }; |
| 1035 | |
sbarati@apple.com | 10ec78a | 2016-04-20 02:24:53 +0000 | [diff] [blame] | 1036 | StackFrame& frame = stackTrace.frames.first(); |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 1037 | String frameDescription = makeString(frame.displayName(m_vm), descriptionForLocation(frame.semanticLocation)); |
| 1038 | if (std::optional<std::pair<StackFrame::CodeLocation, Strong<CodeBlock>>> machineLocation = frame.machineLocation) { |
| 1039 | frameDescription = makeString(frameDescription, " <-- ", |
| 1040 | machineLocation->second->inferredName().data(), descriptionForLocation(machineLocation->first)); |
| 1041 | } |
sbarati@apple.com | 10ec78a | 2016-04-20 02:24:53 +0000 | [diff] [blame] | 1042 | bytecodeCounts.add(frameDescription, 0).iterator->value++; |
| 1043 | } |
| 1044 | |
| 1045 | auto takeMax = [&] () -> std::pair<String, size_t> { |
| 1046 | String maxFrameDescription; |
| 1047 | size_t maxFrameCount = 0; |
| 1048 | for (auto entry : bytecodeCounts) { |
| 1049 | if (entry.value > maxFrameCount) { |
| 1050 | maxFrameCount = entry.value; |
| 1051 | maxFrameDescription = entry.key; |
| 1052 | } |
| 1053 | } |
| 1054 | if (!maxFrameDescription.isEmpty()) |
| 1055 | bytecodeCounts.remove(maxFrameDescription); |
| 1056 | return std::make_pair(maxFrameDescription, maxFrameCount); |
| 1057 | }; |
| 1058 | |
sbarati@apple.com | 65f8e6e | 2017-01-28 01:04:06 +0000 | [diff] [blame] | 1059 | if (Options::samplingProfilerTopBytecodesCount()) { |
| 1060 | out.print("\n\nSampling rate: ", m_timingInterval.count(), " microseconds\n"); |
| 1061 | out.print("Hottest bytecodes as <numSamples 'functionName#hash:JITType:bytecodeIndex'>\n"); |
| 1062 | for (size_t i = 0; i < Options::samplingProfilerTopBytecodesCount(); i++) { |
| 1063 | auto pair = takeMax(); |
| 1064 | if (pair.first.isEmpty()) |
| 1065 | break; |
| 1066 | out.printf("%6zu ", pair.second); |
| 1067 | out.print(" '", pair.first, "'\n"); |
| 1068 | } |
sbarati@apple.com | 10ec78a | 2016-04-20 02:24:53 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 1072 | } // namespace JSC |
| 1073 | |
| 1074 | namespace WTF { |
| 1075 | |
| 1076 | using namespace JSC; |
| 1077 | |
| 1078 | void printInternal(PrintStream& out, SamplingProfiler::FrameType frameType) |
| 1079 | { |
| 1080 | switch (frameType) { |
sbarati@apple.com | d3d0c00 | 2016-01-30 01:11:05 +0000 | [diff] [blame] | 1081 | case SamplingProfiler::FrameType::Executable: |
| 1082 | out.print("Executable"); |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 1083 | break; |
| 1084 | case SamplingProfiler::FrameType::Host: |
| 1085 | out.print("Host"); |
| 1086 | break; |
keith_miller@apple.com | b4a8644 | 2017-02-02 01:23:37 +0000 | [diff] [blame] | 1087 | case SamplingProfiler::FrameType::C: |
sbarati@apple.com | a4ce86b | 2016-01-11 06:49:49 +0000 | [diff] [blame] | 1088 | case SamplingProfiler::FrameType::Unknown: |
| 1089 | out.print("Unknown"); |
| 1090 | break; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | } // namespace WTF |
| 1095 | |
| 1096 | #endif // ENABLE(SAMPLING_PROFILER) |