Index: src/v8-sampler.cc |
diff --git a/src/profiler/sampler.cc b/src/v8-sampler.cc |
similarity index 53% |
copy from src/profiler/sampler.cc |
copy to src/v8-sampler.cc |
index 45f1d4d3efcfd726409b16ec2aa80f0b673b0d92..8c66561066c652a00d851d8cd70d6e3a1c1bb4c6 100644 |
--- a/src/profiler/sampler.cc |
+++ b/src/v8-sampler.cc |
@@ -1,13 +1,14 @@ |
-// Copyright 2013 the V8 project authors. All rights reserved. |
+// Copyright 2016 the V8 project authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "src/profiler/sampler.h" |
+#include "include/v8.h" |
+#include "include/v8-sampler.h" |
+#include "src/base/platform/time.h" |
+#include <iostream> |
#if V8_OS_POSIX && !V8_OS_CYGWIN |
-#define USE_SIGNALS |
- |
#include <errno.h> |
#include <pthread.h> |
#include <signal.h> |
@@ -155,92 +156,27 @@ enum { REG_RBP = 10, REG_RSP = 15, REG_RIP = 16 }; |
#endif // V8_OS_ANDROID && !defined(__BIONIC_HAVE_UCONTEXT_T) |
- |
namespace v8 { |
-namespace internal { |
namespace { |
-class PlatformDataCommon : public Malloced { |
+class PlatformDataCommon : public i::Malloced { |
public: |
- PlatformDataCommon() : profiled_thread_id_(ThreadId::Current()) {} |
- ThreadId profiled_thread_id() { return profiled_thread_id_; } |
+ PlatformDataCommon() : profiled_thread_id_(i::ThreadId::Current()) {} |
+ i::ThreadId profiled_thread_id() { return profiled_thread_id_; } |
protected: |
~PlatformDataCommon() {} |
private: |
- ThreadId profiled_thread_id_; |
+ i::ThreadId profiled_thread_id_; |
}; |
- |
-bool IsSamePage(byte* ptr1, byte* ptr2) { |
- const uint32_t kPageSize = 4096; |
- uintptr_t mask = ~static_cast<uintptr_t>(kPageSize - 1); |
- return (reinterpret_cast<uintptr_t>(ptr1) & mask) == |
- (reinterpret_cast<uintptr_t>(ptr2) & mask); |
-} |
- |
- |
-// Check if the code at specified address could potentially be a |
-// frame setup code. |
-bool IsNoFrameRegion(Address address) { |
- struct Pattern { |
- int bytes_count; |
- byte bytes[8]; |
- int offsets[4]; |
- }; |
- byte* pc = reinterpret_cast<byte*>(address); |
- static Pattern patterns[] = { |
-#if V8_HOST_ARCH_IA32 |
- // push %ebp |
- // mov %esp,%ebp |
- {3, {0x55, 0x89, 0xe5}, {0, 1, -1}}, |
- // pop %ebp |
- // ret N |
- {2, {0x5d, 0xc2}, {0, 1, -1}}, |
- // pop %ebp |
- // ret |
- {2, {0x5d, 0xc3}, {0, 1, -1}}, |
-#elif V8_HOST_ARCH_X64 |
- // pushq %rbp |
- // movq %rsp,%rbp |
- {4, {0x55, 0x48, 0x89, 0xe5}, {0, 1, -1}}, |
- // popq %rbp |
- // ret N |
- {2, {0x5d, 0xc2}, {0, 1, -1}}, |
- // popq %rbp |
- // ret |
- {2, {0x5d, 0xc3}, {0, 1, -1}}, |
-#endif |
- {0, {}, {}} |
- }; |
- for (Pattern* pattern = patterns; pattern->bytes_count; ++pattern) { |
- for (int* offset_ptr = pattern->offsets; *offset_ptr != -1; ++offset_ptr) { |
- int offset = *offset_ptr; |
- if (!offset || IsSamePage(pc, pc - offset)) { |
- MSAN_MEMORY_IS_INITIALIZED(pc - offset, pattern->bytes_count); |
- if (!memcmp(pc - offset, pattern->bytes, pattern->bytes_count)) |
- return true; |
- } else { |
- // It is not safe to examine bytes on another page as it might not be |
- // allocated thus causing a SEGFAULT. |
- // Check the pattern part that's on the same page and |
- // pessimistically assume it could be the entire pattern match. |
- MSAN_MEMORY_IS_INITIALIZED(pc, pattern->bytes_count - offset); |
- if (!memcmp(pc, pattern->bytes + offset, pattern->bytes_count - offset)) |
- return true; |
- } |
- } |
- } |
- return false; |
-} |
- |
} // namespace |
#if defined(USE_SIGNALS) |
-class Sampler::PlatformData : public PlatformDataCommon { |
+class V8Sampler::PlatformData : public PlatformDataCommon { |
public: |
PlatformData() : vm_tid_(pthread_self()) {} |
pthread_t vm_tid() const { return vm_tid_; } |
@@ -255,7 +191,7 @@ class Sampler::PlatformData : public PlatformDataCommon { |
// Win32 profiler support. On Cygwin we use the same sampler implementation as |
// on Win32. |
-class Sampler::PlatformData : public PlatformDataCommon { |
+class V8Sampler::PlatformData : public PlatformDataCommon { |
public: |
// Get a handle to the calling thread. This is the thread that we are |
// going to profile. We need to make a copy of the handle because we are |
@@ -287,21 +223,21 @@ class Sampler::PlatformData : public PlatformDataCommon { |
#if defined(USE_SIMULATOR) |
class SimulatorHelper { |
public: |
- inline bool Init(Isolate* isolate) { |
+ inline bool Init(i::Isolate* isolate) { |
simulator_ = isolate->thread_local_top()->simulator_; |
// Check if there is active simulator. |
return simulator_ != NULL; |
} |
- inline void FillRegisters(v8::RegisterState* state) { |
+ inline void FillRegisters(RegisterState* state) { |
#if V8_TARGET_ARCH_ARM |
if (!simulator_->has_bad_pc()) { |
- state->pc = reinterpret_cast<Address>(simulator_->get_pc()); |
+ state->pc = reinterpret_cast<i::Address>(simulator_->get_pc()); |
} |
- state->sp = reinterpret_cast<Address>(simulator_->get_register( |
- Simulator::sp)); |
- state->fp = reinterpret_cast<Address>(simulator_->get_register( |
- Simulator::r11)); |
+ state->sp = reinterpret_cast<i::Address>(simulator_->get_register( |
+ i::Simulator::sp)); |
+ state->fp = reinterpret_cast<i::Address>(simulator_->get_register( |
+ i::Simulator::r11)); |
#elif V8_TARGET_ARCH_ARM64 |
if (simulator_->sp() == 0 || simulator_->fp() == 0) { |
// It's possible that the simulator is interrupted while it is updating |
@@ -317,37 +253,37 @@ class SimulatorHelper { |
// are of same bitness. |
return; |
} |
- state->pc = reinterpret_cast<Address>(simulator_->pc()); |
- state->sp = reinterpret_cast<Address>(simulator_->sp()); |
- state->fp = reinterpret_cast<Address>(simulator_->fp()); |
+ state->pc = reinterpret_cast<i::Address>(simulator_->pc()); |
+ state->sp = reinterpret_cast<i::Address>(simulator_->sp()); |
+ state->fp = reinterpret_cast<i::Address>(simulator_->fp()); |
#elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64 |
if (!simulator_->has_bad_pc()) { |
- state->pc = reinterpret_cast<Address>(simulator_->get_pc()); |
+ state->pc = reinterpret_cast<i::Address>(simulator_->get_pc()); |
} |
- state->sp = reinterpret_cast<Address>(simulator_->get_register( |
- Simulator::sp)); |
- state->fp = reinterpret_cast<Address>(simulator_->get_register( |
- Simulator::fp)); |
+ state->sp = reinterpret_cast<i::Address>(simulator_->get_register( |
+ i::Simulator::sp)); |
+ state->fp = reinterpret_cast<i::Address>(simulator_->get_register( |
+ i::Simulator::fp)); |
#elif V8_TARGET_ARCH_PPC |
if (!simulator_->has_bad_pc()) { |
- state->pc = reinterpret_cast<Address>(simulator_->get_pc()); |
+ state->pc = reinterpret_cast<i::Address>(simulator_->get_pc()); |
} |
state->sp = |
- reinterpret_cast<Address>(simulator_->get_register(Simulator::sp)); |
+ reinterpret_cast<i::Address>(simulator_->get_register(i::Simulator::sp)); |
state->fp = |
- reinterpret_cast<Address>(simulator_->get_register(Simulator::fp)); |
+ reinterpret_cast<i::Address>(simulator_->get_register(i::Simulator::fp)); |
#endif |
} |
private: |
- Simulator* simulator_; |
+ i::Simulator* simulator_; |
}; |
#endif // USE_SIMULATOR |
#if defined(USE_SIGNALS) |
-class SignalHandler : public AllStatic { |
+class SignalHandler { |
public: |
static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } |
static void TearDown() { delete mutex_; mutex_ = NULL; } |
@@ -370,7 +306,7 @@ class SignalHandler : public AllStatic { |
static void Install() { |
#if !V8_OS_NACL |
struct sigaction sa; |
- sa.sa_sigaction = &HandleProfilerSignal; |
+ sa.sa_sigaction = &SignalHandler::HandleProfilerSignal; |
sigemptyset(&sa.sa_mask); |
#if V8_OS_QNX |
sa.sa_flags = SA_SIGINFO; |
@@ -401,12 +337,113 @@ class SignalHandler : public AllStatic { |
static struct sigaction old_signal_handler_; |
}; |
- |
base::Mutex* SignalHandler::mutex_ = NULL; |
int SignalHandler::client_count_ = 0; |
struct sigaction SignalHandler::old_signal_handler_; |
bool SignalHandler::signal_handler_installed_ = false; |
+class SamplerThread : public base::Thread { |
+ public: |
+ static const int kSamplerThreadStackSize = 64 * i::KB; |
+ |
+ explicit SamplerThread() |
+ : Thread(base::Thread::Options("V8::SamplerThread", |
+ kSamplerThreadStackSize)) {} |
+ |
+ static void SetUp(int interval) { |
+ if (!mutex_) |
+ mutex_ = new base::Mutex(); |
+ SamplerThread::interval_ = interval; |
+ } |
+ static void TearDown() { delete mutex_; mutex_ = NULL; } |
+ |
+ static void SetInterval(int interval) { |
+ SamplerThread::interval_ = interval; |
+ } |
+ |
+ static void AddActiveSampler(V8Sampler* sampler) { |
+ bool need_to_start = false; |
+ base::LockGuard<base::Mutex> lock_guard(mutex_); |
+ if (instance_ == NULL) { |
+ // Start a thread that will send SIGPROF signal to VM threads, |
+ // when CPU profiling will be enabled. |
+ instance_ = new SamplerThread(); |
+ need_to_start = true; |
+ } |
+ |
+ DCHECK(sampler->IsActive()); |
+ DCHECK(!instance_->active_samplers_.Contains(sampler)); |
+ instance_->active_samplers_.Add(sampler); |
+ |
+ if (need_to_start) instance_->StartSynchronously(); |
+ } |
+ |
+ static void RemoveActiveSampler(V8Sampler* sampler) { |
+ SamplerThread* instance_to_remove = NULL; |
+ { |
+ base::LockGuard<base::Mutex> lock_guard(mutex_); |
+ |
+ DCHECK(sampler->IsActive()); |
+ bool removed = instance_->active_samplers_.RemoveElement(sampler); |
+ DCHECK(removed); |
+ USE(removed); |
+ |
+ // We cannot delete the instance immediately as we need to Join() the |
+ // thread but we are holding mutex_ and the thread may try to acquire it. |
+ if (instance_->active_samplers_.is_empty()) { |
+ instance_to_remove = instance_; |
+ instance_ = NULL; |
+ } |
+ } |
+ |
+ if (!instance_to_remove) return; |
+ instance_to_remove->Join(); |
+ delete instance_to_remove; |
+ } |
+ |
+ static V8Sampler* FetchActiveSampler(void* isolate) { |
+ for (int i = 0; i < instance_->active_samplers_.length(); ++i) { |
+ V8Sampler* sampler = instance_->active_samplers_.at(i); |
+ if (reinterpret_cast<void*>(sampler->isolate()) == isolate) |
+ return sampler; |
+ } |
+ return NULL; |
+ } |
+ |
+ // Implement Thread::Run(). |
+ void Run() override { |
+ while (true) { |
+ { |
+ base::LockGuard<base::Mutex> lock_guard(mutex_); |
+ if (active_samplers_.is_empty()) break; |
+ // When CPU profiling is enabled both JavaScript and C++ code is |
+ // profiled. We must not suspend. |
+ for (int i = 0; i < active_samplers_.length(); ++i) { |
+ V8Sampler* sampler = active_samplers_.at(i); |
+ if (!sampler->IsProfiling()) continue; |
+ sampler->DoSample(); |
+ } |
+ } |
+ base::OS::Sleep( |
+ base::TimeDelta::FromMilliseconds(SamplerThread::interval_)); |
+ } |
+ } |
+ |
+ private: |
+ // Protects the process wide state below. |
+ static base::Mutex* mutex_; |
+ static SamplerThread* instance_; |
+ |
+ static int interval_; |
+ i::List<V8Sampler*> active_samplers_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SamplerThread); |
+}; |
+ |
+base::Mutex* SamplerThread::mutex_ = NULL; |
+SamplerThread* SamplerThread::instance_ = NULL; |
+int SamplerThread::interval_ = 0; |
+ |
// As Native Client does not support signal handling, profiling is disabled. |
#if !V8_OS_NACL |
@@ -414,20 +451,21 @@ void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, |
void* context) { |
USE(info); |
if (signal != SIGPROF) return; |
- Isolate* isolate = Isolate::UnsafeCurrent(); |
+ i::Isolate* isolate = i::Isolate::UnsafeCurrent(); |
if (isolate == NULL || !isolate->IsInUse()) { |
// We require a fully initialized and entered isolate. |
return; |
} |
- if (v8::Locker::IsActive() && |
+ if (Locker::IsActive() && |
!isolate->thread_manager()->IsLockedByCurrentThread()) { |
return; |
} |
- Sampler* sampler = isolate->logger()->sampler(); |
+ V8Sampler* sampler = SamplerThread::FetchActiveSampler( |
+ reinterpret_cast<void*>(isolate)); |
if (sampler == NULL) return; |
- v8::RegisterState state; |
+ RegisterState state; |
#if defined(USE_SIMULATOR) |
SimulatorHelper helper; |
@@ -446,117 +484,117 @@ void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, |
#endif |
#if V8_OS_LINUX |
#if V8_HOST_ARCH_IA32 |
- state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_EIP]); |
- state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_ESP]); |
- state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_EBP]); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.gregs[REG_EIP]); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.gregs[REG_ESP]); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.gregs[REG_EBP]); |
#elif V8_HOST_ARCH_X64 |
- state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_RIP]); |
- state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_RSP]); |
- state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_RBP]); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.gregs[REG_RIP]); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.gregs[REG_RSP]); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.gregs[REG_RBP]); |
#elif V8_HOST_ARCH_ARM |
#if V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) |
// Old GLibc ARM versions used a gregs[] array to access the register |
// values from mcontext_t. |
- state.pc = reinterpret_cast<Address>(mcontext.gregs[R15]); |
- state.sp = reinterpret_cast<Address>(mcontext.gregs[R13]); |
- state.fp = reinterpret_cast<Address>(mcontext.gregs[R11]); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.gregs[R15]); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.gregs[R13]); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.gregs[R11]); |
#else |
- state.pc = reinterpret_cast<Address>(mcontext.arm_pc); |
- state.sp = reinterpret_cast<Address>(mcontext.arm_sp); |
- state.fp = reinterpret_cast<Address>(mcontext.arm_fp); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.arm_pc); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.arm_sp); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.arm_fp); |
#endif // V8_LIBC_GLIBC && !V8_GLIBC_PREREQ(2, 4) |
#elif V8_HOST_ARCH_ARM64 |
- state.pc = reinterpret_cast<Address>(mcontext.pc); |
- state.sp = reinterpret_cast<Address>(mcontext.sp); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.pc); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.sp); |
// FP is an alias for x29. |
- state.fp = reinterpret_cast<Address>(mcontext.regs[29]); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.regs[29]); |
#elif V8_HOST_ARCH_MIPS |
- state.pc = reinterpret_cast<Address>(mcontext.pc); |
- state.sp = reinterpret_cast<Address>(mcontext.gregs[29]); |
- state.fp = reinterpret_cast<Address>(mcontext.gregs[30]); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.pc); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.gregs[29]); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.gregs[30]); |
#elif V8_HOST_ARCH_MIPS64 |
- state.pc = reinterpret_cast<Address>(mcontext.pc); |
- state.sp = reinterpret_cast<Address>(mcontext.gregs[29]); |
- state.fp = reinterpret_cast<Address>(mcontext.gregs[30]); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.pc); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.gregs[29]); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.gregs[30]); |
#elif V8_HOST_ARCH_PPC |
- state.pc = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->nip); |
- state.sp = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->gpr[PT_R1]); |
- state.fp = reinterpret_cast<Address>(ucontext->uc_mcontext.regs->gpr[PT_R31]); |
+ state.pc = reinterpret_cast<i::Address>(ucontext->uc_mcontext.regs->nip); |
+ state.sp = reinterpret_cast<i::Address>(ucontext->uc_mcontext.regs->gpr[PT_R1]); |
+ state.fp = reinterpret_cast<i::Address>(ucontext->uc_mcontext.regs->gpr[PT_R31]); |
#endif // V8_HOST_ARCH_* |
#elif V8_OS_MACOSX |
#if V8_HOST_ARCH_X64 |
#if __DARWIN_UNIX03 |
- state.pc = reinterpret_cast<Address>(mcontext->__ss.__rip); |
- state.sp = reinterpret_cast<Address>(mcontext->__ss.__rsp); |
- state.fp = reinterpret_cast<Address>(mcontext->__ss.__rbp); |
+ state.pc = reinterpret_cast<i::Address>(mcontext->__ss.__rip); |
+ state.sp = reinterpret_cast<i::Address>(mcontext->__ss.__rsp); |
+ state.fp = reinterpret_cast<i::Address>(mcontext->__ss.__rbp); |
#else // !__DARWIN_UNIX03 |
- state.pc = reinterpret_cast<Address>(mcontext->ss.rip); |
- state.sp = reinterpret_cast<Address>(mcontext->ss.rsp); |
- state.fp = reinterpret_cast<Address>(mcontext->ss.rbp); |
+ state.pc = reinterpret_cast<i::Address>(mcontext->ss.rip); |
+ state.sp = reinterpret_cast<i::Address>(mcontext->ss.rsp); |
+ state.fp = reinterpret_cast<i::Address>(mcontext->ss.rbp); |
#endif // __DARWIN_UNIX03 |
#elif V8_HOST_ARCH_IA32 |
#if __DARWIN_UNIX03 |
- state.pc = reinterpret_cast<Address>(mcontext->__ss.__eip); |
- state.sp = reinterpret_cast<Address>(mcontext->__ss.__esp); |
- state.fp = reinterpret_cast<Address>(mcontext->__ss.__ebp); |
+ state.pc = reinterpret_cast<i::Address>(mcontext->__ss.__eip); |
+ state.sp = reinterpret_cast<i::Address>(mcontext->__ss.__esp); |
+ state.fp = reinterpret_cast<i::Address>(mcontext->__ss.__ebp); |
#else // !__DARWIN_UNIX03 |
- state.pc = reinterpret_cast<Address>(mcontext->ss.eip); |
- state.sp = reinterpret_cast<Address>(mcontext->ss.esp); |
- state.fp = reinterpret_cast<Address>(mcontext->ss.ebp); |
+ state.pc = reinterpret_cast<i::Address>(mcontext->ss.eip); |
+ state.sp = reinterpret_cast<i::Address>(mcontext->ss.esp); |
+ state.fp = reinterpret_cast<i::Address>(mcontext->ss.ebp); |
#endif // __DARWIN_UNIX03 |
#endif // V8_HOST_ARCH_IA32 |
#elif V8_OS_FREEBSD |
#if V8_HOST_ARCH_IA32 |
- state.pc = reinterpret_cast<Address>(mcontext.mc_eip); |
- state.sp = reinterpret_cast<Address>(mcontext.mc_esp); |
- state.fp = reinterpret_cast<Address>(mcontext.mc_ebp); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.mc_eip); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.mc_esp); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.mc_ebp); |
#elif V8_HOST_ARCH_X64 |
- state.pc = reinterpret_cast<Address>(mcontext.mc_rip); |
- state.sp = reinterpret_cast<Address>(mcontext.mc_rsp); |
- state.fp = reinterpret_cast<Address>(mcontext.mc_rbp); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.mc_rip); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.mc_rsp); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.mc_rbp); |
#elif V8_HOST_ARCH_ARM |
- state.pc = reinterpret_cast<Address>(mcontext.mc_r15); |
- state.sp = reinterpret_cast<Address>(mcontext.mc_r13); |
- state.fp = reinterpret_cast<Address>(mcontext.mc_r11); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.mc_r15); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.mc_r13); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.mc_r11); |
#endif // V8_HOST_ARCH_* |
#elif V8_OS_NETBSD |
#if V8_HOST_ARCH_IA32 |
- state.pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_EIP]); |
- state.sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_ESP]); |
- state.fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_EBP]); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.__gregs[_REG_EIP]); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.__gregs[_REG_ESP]); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.__gregs[_REG_EBP]); |
#elif V8_HOST_ARCH_X64 |
- state.pc = reinterpret_cast<Address>(mcontext.__gregs[_REG_RIP]); |
- state.sp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RSP]); |
- state.fp = reinterpret_cast<Address>(mcontext.__gregs[_REG_RBP]); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.__gregs[_REG_RIP]); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.__gregs[_REG_RSP]); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.__gregs[_REG_RBP]); |
#endif // V8_HOST_ARCH_* |
#elif V8_OS_OPENBSD |
#if V8_HOST_ARCH_IA32 |
- state.pc = reinterpret_cast<Address>(ucontext->sc_eip); |
- state.sp = reinterpret_cast<Address>(ucontext->sc_esp); |
- state.fp = reinterpret_cast<Address>(ucontext->sc_ebp); |
+ state.pc = reinterpret_cast<i::Address>(ucontext->sc_eip); |
+ state.sp = reinterpret_cast<i::Address>(ucontext->sc_esp); |
+ state.fp = reinterpret_cast<i::Address>(ucontext->sc_ebp); |
#elif V8_HOST_ARCH_X64 |
- state.pc = reinterpret_cast<Address>(ucontext->sc_rip); |
- state.sp = reinterpret_cast<Address>(ucontext->sc_rsp); |
- state.fp = reinterpret_cast<Address>(ucontext->sc_rbp); |
+ state.pc = reinterpret_cast<i::Address>(ucontext->sc_rip); |
+ state.sp = reinterpret_cast<i::Address>(ucontext->sc_rsp); |
+ state.fp = reinterpret_cast<i::Address>(ucontext->sc_rbp); |
#endif // V8_HOST_ARCH_* |
#elif V8_OS_SOLARIS |
- state.pc = reinterpret_cast<Address>(mcontext.gregs[REG_PC]); |
- state.sp = reinterpret_cast<Address>(mcontext.gregs[REG_SP]); |
- state.fp = reinterpret_cast<Address>(mcontext.gregs[REG_FP]); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.gregs[REG_PC]); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.gregs[REG_SP]); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.gregs[REG_FP]); |
#elif V8_OS_QNX |
#if V8_HOST_ARCH_IA32 |
- state.pc = reinterpret_cast<Address>(mcontext.cpu.eip); |
- state.sp = reinterpret_cast<Address>(mcontext.cpu.esp); |
- state.fp = reinterpret_cast<Address>(mcontext.cpu.ebp); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.cpu.eip); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.cpu.esp); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.cpu.ebp); |
#elif V8_HOST_ARCH_ARM |
- state.pc = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_PC]); |
- state.sp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_SP]); |
- state.fp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_FP]); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.cpu.gpr[ARM_REG_PC]); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.cpu.gpr[ARM_REG_SP]); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.cpu.gpr[ARM_REG_FP]); |
#endif // V8_HOST_ARCH_* |
#elif V8_OS_AIX |
- state.pc = reinterpret_cast<Address>(mcontext.jmp_context.iar); |
- state.sp = reinterpret_cast<Address>(mcontext.jmp_context.gpr[1]); |
- state.fp = reinterpret_cast<Address>(mcontext.jmp_context.gpr[31]); |
+ state.pc = reinterpret_cast<i::Address>(mcontext.jmp_context.iar); |
+ state.sp = reinterpret_cast<i::Address>(mcontext.jmp_context.gpr[1]); |
+ state.fp = reinterpret_cast<i::Address>(mcontext.jmp_context.gpr[31]); |
#endif // V8_OS_AIX |
#endif // USE_SIMULATOR |
sampler->SampleStack(state); |
@@ -566,188 +604,39 @@ void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, |
#endif |
-class SamplerThread : public base::Thread { |
- public: |
- static const int kSamplerThreadStackSize = 64 * KB; |
- |
- explicit SamplerThread(int interval) |
- : Thread(base::Thread::Options("SamplerThread", kSamplerThreadStackSize)), |
- interval_(interval) {} |
- |
- static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } |
- static void TearDown() { delete mutex_; mutex_ = NULL; } |
- |
- static void AddActiveSampler(Sampler* sampler) { |
- bool need_to_start = false; |
- base::LockGuard<base::Mutex> lock_guard(mutex_); |
- if (instance_ == NULL) { |
- // Start a thread that will send SIGPROF signal to VM threads, |
- // when CPU profiling will be enabled. |
- instance_ = new SamplerThread(sampler->interval()); |
- need_to_start = true; |
- } |
- |
- DCHECK(sampler->IsActive()); |
- DCHECK(!instance_->active_samplers_.Contains(sampler)); |
- DCHECK(instance_->interval_ == sampler->interval()); |
- instance_->active_samplers_.Add(sampler); |
- |
- if (need_to_start) instance_->StartSynchronously(); |
- } |
- |
- static void RemoveActiveSampler(Sampler* sampler) { |
- SamplerThread* instance_to_remove = NULL; |
- { |
- base::LockGuard<base::Mutex> lock_guard(mutex_); |
- |
- DCHECK(sampler->IsActive()); |
- bool removed = instance_->active_samplers_.RemoveElement(sampler); |
- DCHECK(removed); |
- USE(removed); |
- |
- // We cannot delete the instance immediately as we need to Join() the |
- // thread but we are holding mutex_ and the thread may try to acquire it. |
- if (instance_->active_samplers_.is_empty()) { |
- instance_to_remove = instance_; |
- instance_ = NULL; |
- } |
- } |
- |
- if (!instance_to_remove) return; |
- instance_to_remove->Join(); |
- delete instance_to_remove; |
- } |
- |
- // Implement Thread::Run(). |
- virtual void Run() { |
- while (true) { |
- { |
- base::LockGuard<base::Mutex> lock_guard(mutex_); |
- if (active_samplers_.is_empty()) break; |
- // When CPU profiling is enabled both JavaScript and C++ code is |
- // profiled. We must not suspend. |
- for (int i = 0; i < active_samplers_.length(); ++i) { |
- Sampler* sampler = active_samplers_.at(i); |
- if (!sampler->IsProfiling()) continue; |
- sampler->DoSample(); |
- } |
- } |
- base::OS::Sleep(base::TimeDelta::FromMilliseconds(interval_)); |
- } |
- } |
- |
- private: |
- // Protects the process wide state below. |
- static base::Mutex* mutex_; |
- static SamplerThread* instance_; |
- |
- const int interval_; |
- List<Sampler*> active_samplers_; |
- |
- DISALLOW_COPY_AND_ASSIGN(SamplerThread); |
-}; |
- |
- |
-base::Mutex* SamplerThread::mutex_ = NULL; |
-SamplerThread* SamplerThread::instance_ = NULL; |
- |
- |
-// |
-// StackTracer implementation |
-// |
-DISABLE_ASAN void TickSample::Init(Isolate* isolate, |
- const v8::RegisterState& regs, |
- RecordCEntryFrame record_c_entry_frame, |
- bool update_stats) { |
- timestamp = base::TimeTicks::HighResolutionNow(); |
- pc = reinterpret_cast<Address>(regs.pc); |
- state = isolate->current_vm_state(); |
- this->update_stats = update_stats; |
- |
- // Avoid collecting traces while doing GC. |
- if (state == GC) return; |
- |
- Address js_entry_sp = isolate->js_entry_sp(); |
- if (js_entry_sp == 0) return; // Not executing JS now. |
- |
- if (pc && IsNoFrameRegion(pc)) { |
- pc = 0; |
- return; |
- } |
- |
- ExternalCallbackScope* scope = isolate->external_callback_scope(); |
- Address handler = Isolate::handler(isolate->thread_local_top()); |
- // If there is a handler on top of the external callback scope then |
- // we have already entrered JavaScript again and the external callback |
- // is not the top function. |
- if (scope && scope->scope_address() < handler) { |
- external_callback = scope->callback(); |
- has_external_callback = true; |
- } else { |
- // sp register may point at an arbitrary place in memory, make |
- // sure MSAN doesn't complain about it. |
- MSAN_MEMORY_IS_INITIALIZED(regs.sp, sizeof(Address)); |
- // Sample potential return address value for frameless invocation of |
- // stubs (we'll figure out later, if this value makes sense). |
- tos = Memory::Address_at(reinterpret_cast<Address>(regs.sp)); |
- has_external_callback = false; |
- } |
- |
- SafeStackFrameIterator it(isolate, reinterpret_cast<Address>(regs.fp), |
- reinterpret_cast<Address>(regs.sp), js_entry_sp); |
- top_frame_type = it.top_frame_type(); |
- |
- SampleInfo info; |
- GetStackSample(isolate, regs, record_c_entry_frame, |
- reinterpret_cast<void**>(&stack[0]), kMaxFramesCount, &info); |
- frames_count = static_cast<unsigned>(info.frames_count); |
-} |
- |
- |
-void TickSample::GetStackSample(Isolate* isolate, const v8::RegisterState& regs, |
- RecordCEntryFrame record_c_entry_frame, |
- void** frames, size_t frames_limit, |
- v8::SampleInfo* sample_info) { |
- sample_info->frames_count = 0; |
- sample_info->vm_state = isolate->current_vm_state(); |
- if (sample_info->vm_state == GC) return; |
- |
- Address js_entry_sp = isolate->js_entry_sp(); |
- if (js_entry_sp == 0) return; // Not executing JS now. |
- |
- SafeStackFrameIterator it(isolate, reinterpret_cast<Address>(regs.fp), |
- reinterpret_cast<Address>(regs.sp), js_entry_sp); |
- size_t i = 0; |
- if (record_c_entry_frame == kIncludeCEntryFrame && !it.done() && |
- it.top_frame_type() == StackFrame::EXIT) { |
- frames[i++] = isolate->c_function(); |
- } |
- while (!it.done() && i < frames_limit) { |
- frames[i++] = it.frame()->pc(); |
- it.Advance(); |
- } |
- sample_info->frames_count = i; |
-} |
- |
- |
-void Sampler::SetUp() { |
+void V8Sampler::SetUp() { |
#if defined(USE_SIGNALS) |
SignalHandler::SetUp(); |
#endif |
- SamplerThread::SetUp(); |
+ SamplerThread::SetUp(kSamplingIntervalMs); |
} |
- |
-void Sampler::TearDown() { |
+void V8Sampler::TearDown() { |
SamplerThread::TearDown(); |
#if defined(USE_SIGNALS) |
SignalHandler::TearDown(); |
#endif |
} |
-Sampler::Sampler(Isolate* isolate, int interval) |
+void V8Sampler::SetInterval(int interval) { |
+ SamplerThread::SetInterval(interval); |
+} |
+ |
+void V8Sampler::CollectStackSample(const RegisterState& regs, |
+ void** frames, size_t frames_limit, |
+ SampleInfo* sample_info) { |
+ isolate_->GetStackSample(regs, frames, frames_limit, sample_info); |
+} |
+ |
+void V8Sampler::SetJitCodeEventHandler(JitCodeEventOptions options, |
+ void* data) { |
+ JitCodeEventHandler handler = |
+ reinterpret_cast<JitCodeEventHandler>(data); |
+ isolate_->SetJitCodeEventHandler(options, handler); |
+} |
+ |
+V8Sampler::V8Sampler(Isolate* isolate) |
: isolate_(isolate), |
- interval_(interval), |
profiling_(false), |
has_processing_thread_(false), |
active_(false), |
@@ -757,67 +646,63 @@ Sampler::Sampler(Isolate* isolate, int interval) |
data_ = new PlatformData; |
} |
-Sampler::~Sampler() { |
+V8Sampler::~V8Sampler() { |
DCHECK(!IsActive()); |
delete data_; |
} |
-void Sampler::Start() { |
+void V8Sampler::Start() { |
DCHECK(!IsActive()); |
SetActive(true); |
SamplerThread::AddActiveSampler(this); |
} |
- |
-void Sampler::Stop() { |
+void V8Sampler::Stop() { |
DCHECK(IsActive()); |
SamplerThread::RemoveActiveSampler(this); |
SetActive(false); |
} |
- |
-void Sampler::IncreaseProfilingDepth() { |
+void V8Sampler::IncreaseProfilingDepth() { |
base::NoBarrier_AtomicIncrement(&profiling_, 1); |
#if defined(USE_SIGNALS) |
SignalHandler::IncreaseSamplerCount(); |
#endif |
} |
- |
-void Sampler::DecreaseProfilingDepth() { |
+void V8Sampler::DecreaseProfilingDepth() { |
#if defined(USE_SIGNALS) |
SignalHandler::DecreaseSamplerCount(); |
#endif |
base::NoBarrier_AtomicIncrement(&profiling_, -1); |
} |
- |
-void Sampler::SampleStack(const v8::RegisterState& state) { |
- TickSample* sample = isolate_->cpu_profiler()->StartTickSample(); |
- TickSample sample_obj; |
+void V8Sampler::SampleStack(const RegisterState& state) { |
+ i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate_); |
+ i::TickSample* sample = i_isolate->cpu_profiler()->StartTickSample(); |
+ i::TickSample sample_obj; |
if (sample == NULL) sample = &sample_obj; |
- sample->Init(isolate_, state, TickSample::kIncludeCEntryFrame, true); |
+ sample->Init(i_isolate, state, i::TickSample::kIncludeCEntryFrame, true); |
if (is_counting_samples_) { |
if (sample->state == JS) ++js_sample_count_; |
if (sample->state == EXTERNAL) ++external_sample_count_; |
} |
Tick(sample); |
if (sample != &sample_obj) { |
- isolate_->cpu_profiler()->FinishTickSample(); |
+ i_isolate->cpu_profiler()->FinishTickSample(); |
} |
} |
- |
#if defined(USE_SIGNALS) |
-void Sampler::DoSample() { |
+void V8Sampler::DoSample() { |
if (!SignalHandler::Installed()) return; |
pthread_kill(platform_data()->vm_tid(), SIGPROF); |
} |
#elif V8_OS_WIN || V8_OS_CYGWIN |
-void Sampler::DoSample() { |
+void V8Sampler::DoSample() { |
HANDLE profiled_thread = platform_data()->profiled_thread(); |
if (profiled_thread == NULL) return; |
@@ -834,18 +719,18 @@ void Sampler::DoSample() { |
memset(&context, 0, sizeof(context)); |
context.ContextFlags = CONTEXT_FULL; |
if (GetThreadContext(profiled_thread, &context) != 0) { |
- v8::RegisterState state; |
+ RegisterState state; |
#if defined(USE_SIMULATOR) |
helper.FillRegisters(&state); |
#else |
#if V8_HOST_ARCH_X64 |
- state.pc = reinterpret_cast<Address>(context.Rip); |
- state.sp = reinterpret_cast<Address>(context.Rsp); |
- state.fp = reinterpret_cast<Address>(context.Rbp); |
+ state.pc = reinterpret_cast<i::Address>(context.Rip); |
+ state.sp = reinterpret_cast<i::Address>(context.Rsp); |
+ state.fp = reinterpret_cast<i::Address>(context.Rbp); |
#else |
- state.pc = reinterpret_cast<Address>(context.Eip); |
- state.sp = reinterpret_cast<Address>(context.Esp); |
- state.fp = reinterpret_cast<Address>(context.Ebp); |
+ state.pc = reinterpret_cast<i::Address>(context.Eip); |
+ state.sp = reinterpret_cast<i::Address>(context.Esp); |
+ state.fp = reinterpret_cast<i::Address>(context.Ebp); |
#endif |
#endif // USE_SIMULATOR |
SampleStack(state); |
@@ -855,6 +740,4 @@ void Sampler::DoSample() { |
#endif // USE_SIGNALS |
- |
-} // namespace internal |
-} // namespace v8 |
+} |