Chromium Code Reviews| Index: src/platform-linux.cc |
| diff --git a/src/platform-linux.cc b/src/platform-linux.cc |
| index d02244896bcf8930ecd895a92a7b2ab5a5365b32..f023baba7a3e057fa49fbad10e1365f747187797 100644 |
| --- a/src/platform-linux.cc |
| +++ b/src/platform-linux.cc |
| @@ -1055,6 +1055,37 @@ static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) { |
| } |
| +class CpuProfilerSignalHandler { |
| + public: |
| + static void InstallSignalHandler() { |
| + if (signal_handler_installed_) return; |
| + 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 bool signal_handler_installed() { return signal_handler_installed_; } |
| + |
| + private: |
| + static bool signal_handler_installed_; |
| + static struct sigaction old_signal_handler_; |
| +}; |
| + |
| + |
| +bool CpuProfilerSignalHandler::signal_handler_installed_ = false; |
| +struct sigaction CpuProfilerSignalHandler::old_signal_handler_; |
| + |
| + |
| class Sampler::PlatformData : public Malloced { |
| public: |
| PlatformData() : vm_tid_(GetThreadID()) {} |
| @@ -1083,22 +1114,6 @@ class SignalSender : public Thread { |
| 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); |
| @@ -1119,7 +1134,6 @@ class SignalSender : public Thread { |
| RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_); |
| delete instance_; |
| instance_ = NULL; |
| - RestoreSignalHandler(); |
| } |
| } |
| @@ -1129,13 +1143,8 @@ class SignalSender : public Thread { |
| while ((state = SamplerRegistry::GetState()) != |
| SamplerRegistry::HAS_NO_SAMPLERS) { |
| bool cpu_profiling_enabled = |
| - (state == SamplerRegistry::HAS_CPU_PROFILING_SAMPLERS); |
| + (!FLAG_sample_stack_in_postprocessor_thread && 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) { |
| @@ -1180,7 +1189,7 @@ class SignalSender : public Thread { |
| } |
| void SendProfilingSignal(int tid) { |
| - if (!signal_handler_installed_) return; |
| + if (!CpuProfilerSignalHandler::signal_handler_installed()) return; |
| // Glibc doesn't provide a wrapper for tgkill(2). |
| #if defined(ANDROID) |
| syscall(__NR_tgkill, vm_tgid_, tid, SIGPROF); |
| @@ -1218,8 +1227,6 @@ class SignalSender : public Thread { |
| // 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); |
| @@ -1228,8 +1235,6 @@ 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() { |
| @@ -1257,11 +1262,13 @@ void OS::SetUp() { |
| } |
| #endif |
| SignalSender::SetUp(); |
| + CpuProfilerSignalHandler::InstallSignalHandler(); |
| } |
| void OS::TearDown() { |
| SignalSender::TearDown(); |
| + CpuProfilerSignalHandler::RestoreSignalHandler(); |
| delete limit_mutex; |
| } |
| @@ -1296,4 +1303,40 @@ void Sampler::Stop() { |
| } |
| +class CpuProfilerThread::PlatformData : public Malloced { |
| + public: |
| + PlatformData() : vm_tgid_(getpid()) {} |
| + |
| + int vm_tgid() const { return vm_tgid_; } |
| + |
| + private: |
| + const int vm_tgid_; |
| +}; |
| + |
| + |
| +CpuProfilerThread::CpuProfilerThread(Sampler* sampler) |
| + : Thread(Thread::Options("CpuProfiler", kCpuProfilerThreadStackSize)), |
| + sampler_(sampler) { |
| + data_ = new PlatformData; |
| +} |
| + |
| + |
| +CpuProfilerThread::~CpuProfilerThread() { |
| + delete data_; |
| +} |
| + |
| + |
| +void CpuProfilerThread::DoSample() { |
| + int tid = sampler_->platform_data()->vm_tid(); |
| + int tgid = data_->vm_tgid(); |
| + if (!CpuProfilerSignalHandler::signal_handler_installed()) return; |
| + // Glibc doesn't provide a wrapper for tgkill(2). |
| +#if defined(ANDROID) |
| + syscall(__NR_tgkill, tgid, tid, SIGPROF); |
| +#else |
| + syscall(SYS_tgkill, tgid, tid, SIGPROF); |
|
caseq
2012/08/17 12:56:35
looks like we could either use a tkill() or tgkill
|
| +#endif |
| +} |
| + |
| + |
| } } // namespace v8::internal |