Index: src/platform-linux.cc |
diff --git a/src/platform-linux.cc b/src/platform-linux.cc |
index 5609af0e87ed9c12131cde9391abd50317193fc1..ec48d6305e3de2ea4b51f0571a1c7a83bb0ae619 100644 |
--- a/src/platform-linux.cc |
+++ b/src/platform-linux.cc |
@@ -1025,7 +1025,6 @@ static int GetThreadID() { |
static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
USE(info); |
if (signal != SIGPROF) return; |
- |
Isolate* isolate = Isolate::UncheckedCurrent(); |
if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) { |
// We require a fully initialized and entered isolate. |
@@ -1040,7 +1039,7 @@ static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
if (sampler == NULL || !sampler->IsActive()) return; |
TickSample sample_obj; |
- TickSample* sample = CpuProfiler::StartTickSampleEvent(isolate); |
+ TickSample* sample = CpuProfiler::TickSampleEvent(isolate); |
if (sample == NULL) sample = &sample_obj; |
// Extracting the sample from the context is extremely machine dependent. |
@@ -1076,74 +1075,16 @@ static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
#endif // V8_HOST_ARCH_* |
sampler->SampleStack(sample); |
sampler->Tick(sample); |
- CpuProfiler::FinishTickSampleEvent(isolate); |
} |
-class CpuProfilerSignalHandler { |
- public: |
- static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } |
- static void TearDown() { delete mutex_; } |
- |
- static void InstallSignalHandler() { |
- struct sigaction sa; |
- ScopedLock lock(mutex_); |
- if (signal_handler_installed_counter_ > 0) { |
- signal_handler_installed_counter_++; |
- return; |
- } |
- sa.sa_sigaction = ProfilerSignalHandler; |
- sigemptyset(&sa.sa_mask); |
- sa.sa_flags = SA_RESTART | SA_SIGINFO; |
- if (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0) { |
- signal_handler_installed_counter_++; |
- } |
- } |
- |
- static void RestoreSignalHandler() { |
- ScopedLock lock(mutex_); |
- if (signal_handler_installed_counter_ == 0) |
- return; |
- if (signal_handler_installed_counter_ == 1) { |
- sigaction(SIGPROF, &old_signal_handler_, 0); |
- } |
- signal_handler_installed_counter_--; |
- } |
- |
- static bool signal_handler_installed() { |
- return signal_handler_installed_counter_ > 0; |
- } |
- |
- private: |
- static int signal_handler_installed_counter_; |
- static struct sigaction old_signal_handler_; |
- static Mutex* mutex_; |
-}; |
- |
- |
-int CpuProfilerSignalHandler::signal_handler_installed_counter_ = 0; |
-struct sigaction CpuProfilerSignalHandler::old_signal_handler_; |
-Mutex* CpuProfilerSignalHandler::mutex_ = NULL; |
- |
- |
class Sampler::PlatformData : public Malloced { |
public: |
- PlatformData() |
- : vm_tgid_(getpid()), |
- vm_tid_(GetThreadID()) {} |
+ PlatformData() : vm_tid_(GetThreadID()) {} |
- void SendProfilingSignal() { |
- if (!CpuProfilerSignalHandler::signal_handler_installed()) return; |
- // Glibc doesn't provide a wrapper for tgkill(2). |
-#if defined(ANDROID) |
- syscall(__NR_tgkill, vm_tgid_, vm_tid_, SIGPROF); |
-#else |
- syscall(SYS_tgkill, vm_tgid_, vm_tid_, SIGPROF); |
-#endif |
- } |
+ int vm_tid() const { return vm_tid_; } |
private: |
- const int vm_tgid_; |
const int vm_tid_; |
}; |
@@ -1159,11 +1100,28 @@ class SignalSender : public Thread { |
explicit SignalSender(int interval) |
: Thread(Thread::Options("SignalSender", kSignalSenderStackSize)), |
+ vm_tgid_(getpid()), |
interval_(interval) {} |
static void SetUp() { if (!mutex_) mutex_ = OS::CreateMutex(); } |
static void TearDown() { delete mutex_; } |
+ static void InstallSignalHandler() { |
+ struct sigaction sa; |
+ sa.sa_sigaction = ProfilerSignalHandler; |
+ sigemptyset(&sa.sa_mask); |
+ sa.sa_flags = SA_RESTART | SA_SIGINFO; |
+ signal_handler_installed_ = |
+ (sigaction(SIGPROF, &sa, &old_signal_handler_) == 0); |
+ } |
+ |
+ static void RestoreSignalHandler() { |
+ if (signal_handler_installed_) { |
+ sigaction(SIGPROF, &old_signal_handler_, 0); |
+ signal_handler_installed_ = false; |
+ } |
+ } |
+ |
static void AddActiveSampler(Sampler* sampler) { |
ScopedLock lock(mutex_); |
SamplerRegistry::AddActiveSampler(sampler); |
@@ -1184,6 +1142,7 @@ class SignalSender : public Thread { |
RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_); |
delete instance_; |
instance_ = NULL; |
+ RestoreSignalHandler(); |
} |
} |
@@ -1195,13 +1154,18 @@ class SignalSender : public Thread { |
bool cpu_profiling_enabled = |
(state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); |
bool runtime_profiler_enabled = RuntimeProfiler::IsEnabled(); |
+ if (cpu_profiling_enabled && !signal_handler_installed_) { |
+ InstallSignalHandler(); |
+ } else if (!cpu_profiling_enabled && signal_handler_installed_) { |
+ RestoreSignalHandler(); |
+ } |
// When CPU profiling is enabled both JavaScript and C++ code is |
// profiled. We must not suspend. |
if (!cpu_profiling_enabled) { |
if (rate_limiter_.SuspendIfNecessary()) continue; |
} |
if (cpu_profiling_enabled && runtime_profiler_enabled) { |
- if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, NULL)) { |
+ if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, this)) { |
return; |
} |
Sleep(HALF_INTERVAL); |
@@ -1211,7 +1175,8 @@ class SignalSender : public Thread { |
Sleep(HALF_INTERVAL); |
} else { |
if (cpu_profiling_enabled) { |
- if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, NULL)) { |
+ if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile, |
+ this)) { |
return; |
} |
} |
@@ -1226,9 +1191,10 @@ class SignalSender : public Thread { |
} |
} |
- static void DoCpuProfile(Sampler* sampler, void*) { |
+ static void DoCpuProfile(Sampler* sampler, void* raw_sender) { |
if (!sampler->IsProfiling()) return; |
- sampler->platform_data()->SendProfilingSignal(); |
+ SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender); |
+ sender->SendProfilingSignal(sampler->platform_data()->vm_tid()); |
} |
static void DoRuntimeProfile(Sampler* sampler, void* ignored) { |
@@ -1236,6 +1202,16 @@ class SignalSender : public Thread { |
sampler->isolate()->runtime_profiler()->NotifyTick(); |
} |
+ void SendProfilingSignal(int tid) { |
+ if (!signal_handler_installed_) return; |
+ // Glibc doesn't provide a wrapper for tgkill(2). |
+#if defined(ANDROID) |
+ syscall(__NR_tgkill, vm_tgid_, tid, SIGPROF); |
+#else |
+ syscall(SYS_tgkill, vm_tgid_, tid, SIGPROF); |
+#endif |
+ } |
+ |
void Sleep(SleepInterval full_or_half) { |
// Convert ms to us and subtract 100 us to compensate delays |
// occuring during signal delivery. |
@@ -1258,12 +1234,15 @@ class SignalSender : public Thread { |
#endif // ANDROID |
} |
+ const int vm_tgid_; |
const int interval_; |
RuntimeProfilerRateLimiter rate_limiter_; |
// Protects the process wide state below. |
static Mutex* mutex_; |
static SignalSender* instance_; |
+ static bool signal_handler_installed_; |
+ static struct sigaction old_signal_handler_; |
private: |
DISALLOW_COPY_AND_ASSIGN(SignalSender); |
@@ -1272,6 +1251,8 @@ class SignalSender : public Thread { |
Mutex* SignalSender::mutex_ = NULL; |
SignalSender* SignalSender::instance_ = NULL; |
+struct sigaction SignalSender::old_signal_handler_; |
+bool SignalSender::signal_handler_installed_ = false; |
void OS::SetUp() { |
@@ -1299,13 +1280,11 @@ void OS::SetUp() { |
} |
#endif |
SignalSender::SetUp(); |
- CpuProfilerSignalHandler::SetUp(); |
} |
void OS::TearDown() { |
SignalSender::TearDown(); |
- CpuProfilerSignalHandler::TearDown(); |
delete limit_mutex; |
} |
@@ -1315,7 +1294,6 @@ Sampler::Sampler(Isolate* isolate, int interval) |
interval_(interval), |
profiling_(false), |
active_(false), |
- has_processing_thread_(false), |
samples_taken_(0) { |
data_ = new PlatformData; |
} |
@@ -1327,11 +1305,6 @@ Sampler::~Sampler() { |
} |
-void Sampler::DoSample() { |
- platform_data()->SendProfilingSignal(); |
-} |
- |
- |
void Sampler::Start() { |
ASSERT(!IsActive()); |
SetActive(true); |
@@ -1346,14 +1319,4 @@ void Sampler::Stop() { |
} |
-void Sampler::StartSampling() { |
- CpuProfilerSignalHandler::InstallSignalHandler(); |
-} |
- |
- |
-void Sampler::StopSampling() { |
- CpuProfilerSignalHandler::RestoreSignalHandler(); |
-} |
- |
- |
} } // namespace v8::internal |