Revision: 12649
Author: [email protected]
Date: Tue Oct 2 02:58:11 2012
Log: Moving cpu profiling into its own thread.
BUG=None
Review URL: https://codereview.chromium.org/10857035
Patch from Sergey Rogulenko <[email protected]>.
http://code.google.com/p/v8/source/detail?r=12649
Modified:
/branches/bleeding_edge/src/cpu-profiler.cc
/branches/bleeding_edge/src/cpu-profiler.h
/branches/bleeding_edge/src/flag-definitions.h
/branches/bleeding_edge/src/platform-cygwin.cc
/branches/bleeding_edge/src/platform-freebsd.cc
/branches/bleeding_edge/src/platform-linux.cc
/branches/bleeding_edge/src/platform-macos.cc
/branches/bleeding_edge/src/platform-openbsd.cc
/branches/bleeding_edge/src/platform-solaris.cc
/branches/bleeding_edge/src/platform-win32.cc
/branches/bleeding_edge/src/platform.h
/branches/bleeding_edge/test/cctest/test-cpu-profiler.cc
=======================================
--- /branches/bleeding_edge/src/cpu-profiler.cc Tue Feb 7 00:00:36 2012
+++ /branches/bleeding_edge/src/cpu-profiler.cc Tue Oct 2 02:58:11 2012
@@ -45,10 +45,14 @@
static const int kProfilerStackSize = 64 * KB;
-ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator*
generator)
+ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator*
generator,
+ Sampler* sampler,
+ int period_in_useconds)
: Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)),
generator_(generator),
+ sampler_(sampler),
running_(true),
+ period_in_useconds_(period_in_useconds),
ticks_buffer_(sizeof(TickSampleEventRecord),
kTickSamplesBufferChunkSize,
kTickSamplesBufferChunksCount),
@@ -206,8 +210,9 @@
}
-bool ProfilerEventsProcessor::ProcessTicks(unsigned dequeue_order) {
- while (true) {
+bool ProfilerEventsProcessor::ProcessTicks(int64_t stop_time,
+ unsigned dequeue_order) {
+ while (stop_time == -1 || OS::Ticks() < stop_time) {
if (!ticks_from_vm_buffer_.IsEmpty()
&& ticks_from_vm_buffer_.Peek()->order == dequeue_order) {
TickSampleEventRecord record;
@@ -236,6 +241,19 @@
return true;
}
}
+ return false;
+}
+
+
+void ProfilerEventsProcessor::ProcessEventsQueue(int64_t stop_time,
+ unsigned* dequeue_order) {
+ while (OS::Ticks() < stop_time) {
+ if (ProcessTicks(stop_time, *dequeue_order)) {
+ // All ticks of the current dequeue_order are processed,
+ // proceed to the next code event.
+ ProcessCodeEvent(dequeue_order);
+ }
+ }
}
@@ -243,19 +261,18 @@
unsigned dequeue_order = 0;
while (running_) {
- // Process ticks until we have any.
- if (ProcessTicks(dequeue_order)) {
- // All ticks of the current dequeue_order are processed,
- // proceed to the next code event.
- ProcessCodeEvent(&dequeue_order);
+ int64_t stop_time = OS::Ticks() + period_in_useconds_;
+ if (sampler_ != NULL) {
+ sampler_->DoSample();
}
- YieldCPU();
+ ProcessEventsQueue(stop_time, &dequeue_order);
}
// Process remaining tick events.
ticks_buffer_.FlushResidualRecords();
// Perform processing until we have tick events, skip remaining code
events.
- while (ProcessTicks(dequeue_order) && ProcessCodeEvent(&dequeue_order))
{ }
+ while (ProcessTicks(-1, dequeue_order) &&
ProcessCodeEvent(&dequeue_order)) {
+ }
}
@@ -486,13 +503,15 @@
if (processor_ == NULL) {
Isolate* isolate = Isolate::Current();
+ Sampler* sampler = isolate->logger()->sampler();
// Disable logging when using the new implementation.
saved_logging_nesting_ = isolate->logger()->logging_nesting_;
isolate->logger()->logging_nesting_ = 0;
generator_ = new ProfileGenerator(profiles_);
- processor_ = new ProfilerEventsProcessor(generator_);
+ processor_ = new ProfilerEventsProcessor(generator_,
+ sampler,
+
FLAG_cpu_profiler_sampling_period);
NoBarrier_Store(&is_profiling_, true);
- processor_->Start();
// Enumerate stuff we already have in the heap.
if (isolate->heap()->HasBeenSetUp()) {
if (!FLAG_prof_browser_mode) {
@@ -505,12 +524,12 @@
isolate->logger()->LogAccessorCallbacks();
}
// Enable stack sampling.
- Sampler* sampler =
reinterpret_cast<Sampler*>(isolate->logger()->ticker_);
if (!sampler->IsActive()) {
sampler->Start();
need_to_stop_sampler_ = true;
}
sampler->IncreaseProfilingDepth();
+ processor_->Start();
}
}
@@ -545,16 +564,16 @@
void CpuProfiler::StopProcessor() {
+ NoBarrier_Store(&is_profiling_, false);
+ processor_->Stop();
+ processor_->Join();
Logger* logger = Isolate::Current()->logger();
- Sampler* sampler = reinterpret_cast<Sampler*>(logger->ticker_);
+ Sampler* sampler = logger->sampler();
sampler->DecreaseProfilingDepth();
if (need_to_stop_sampler_) {
sampler->Stop();
need_to_stop_sampler_ = false;
}
- NoBarrier_Store(&is_profiling_, false);
- processor_->Stop();
- processor_->Join();
delete processor_;
delete generator_;
processor_ = NULL;
=======================================
--- /branches/bleeding_edge/src/cpu-profiler.h Tue Aug 28 07:43:28 2012
+++ /branches/bleeding_edge/src/cpu-profiler.h Tue Oct 2 02:58:11 2012
@@ -124,7 +124,9 @@
// methods called by event producers: VM and stack sampler threads.
class ProfilerEventsProcessor : public Thread {
public:
- explicit ProfilerEventsProcessor(ProfileGenerator* generator);
+ explicit ProfilerEventsProcessor(ProfileGenerator* generator,
+ Sampler* sampler,
+ int period_in_useconds);
virtual ~ProfilerEventsProcessor() {}
// Thread control.
@@ -172,12 +174,16 @@
// Called from events processing thread (Run() method.)
bool ProcessCodeEvent(unsigned* dequeue_order);
- bool ProcessTicks(unsigned dequeue_order);
+ bool ProcessTicks(int64_t stop_time, unsigned dequeue_order);
+ void ProcessEventsQueue(int64_t stop_time, unsigned* dequeue_order);
INLINE(static bool FilterOutCodeCreateEvent(Logger::LogEventsAndTags
tag));
ProfileGenerator* generator_;
+ Sampler* sampler_;
bool running_;
+ // Sampling period in microseconds.
+ const int period_in_useconds_;
UnboundQueue<CodeEventsContainer> events_buffer_;
SamplingCircularQueue ticks_buffer_;
UnboundQueue<TickSampleEventRecord> ticks_from_vm_buffer_;
=======================================
--- /branches/bleeding_edge/src/flag-definitions.h Mon Oct 1 14:27:33 2012
+++ /branches/bleeding_edge/src/flag-definitions.h Tue Oct 2 02:58:11 2012
@@ -335,6 +335,10 @@
DEFINE_bool(cache_prototype_transitions, true, "cache prototype
transitions")
+// cpu-profiler.cc
+DEFINE_int(cpu_profiler_sampling_period, 1000,
+ "CPU profiler sampling period in microseconds")
+
// debug.cc
DEFINE_bool(trace_debug_json, false, "trace debugging JSON
request/response")
DEFINE_bool(debugger_auto_break, true,
=======================================
--- /branches/bleeding_edge/src/platform-cygwin.cc Mon Oct 1 05:11:06 2012
+++ /branches/bleeding_edge/src/platform-cygwin.cc Tue Oct 2 02:58:11 2012
@@ -766,6 +766,11 @@
ASSERT(!IsActive());
delete data_;
}
+
+
+void Sampler::DoSample() {
+ // TODO(rogulenko): implement
+}
void Sampler::Start() {
=======================================
--- /branches/bleeding_edge/src/platform-freebsd.cc Mon Oct 1 05:11:06 2012
+++ /branches/bleeding_edge/src/platform-freebsd.cc Tue Oct 2 02:58:11 2012
@@ -882,6 +882,11 @@
ASSERT(!IsActive());
delete data_;
}
+
+
+void Sampler::DoSample() {
+ // TODO(rogulenko): implement
+}
void Sampler::Start() {
=======================================
--- /branches/bleeding_edge/src/platform-linux.cc Mon Oct 1 14:27:33 2012
+++ /branches/bleeding_edge/src/platform-linux.cc Tue Oct 2 02:58:11 2012
@@ -1055,13 +1055,70 @@
}
+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_tid_(GetThreadID()) {}
+ PlatformData()
+ : vm_tgid_(getpid()),
+ vm_tid_(GetThreadID()) {}
- int vm_tid() const { return vm_tid_; }
+ 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
+ }
private:
+ const int vm_tgid_;
const int vm_tid_;
};
@@ -1077,27 +1134,10 @@
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_);
@@ -1119,7 +1159,6 @@
RuntimeProfiler::StopRuntimeProfilerThreadBeforeShutdown(instance_);
delete instance_;
instance_ = NULL;
- RestoreSignalHandler();
}
}
@@ -1128,66 +1167,20 @@
SamplerRegistry::State state;
while ((state = SamplerRegistry::GetState()) !=
SamplerRegistry::HAS_NO_SAMPLERS) {
- 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, this)) {
- return;
- }
- Sleep(HALF_INTERVAL);
+ if (rate_limiter_.SuspendIfNecessary()) continue;
+ if (RuntimeProfiler::IsEnabled()) {
if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile,
NULL)) {
return;
}
- Sleep(HALF_INTERVAL);
- } else {
- if (cpu_profiling_enabled) {
- if (!SamplerRegistry::IterateActiveSamplers(&DoCpuProfile,
- this)) {
- return;
- }
- }
- if (runtime_profiler_enabled) {
- if (!SamplerRegistry::IterateActiveSamplers(&DoRuntimeProfile,
- NULL)) {
- return;
- }
- }
- Sleep(FULL_INTERVAL);
}
+ Sleep(FULL_INTERVAL);
}
}
-
- static void DoCpuProfile(Sampler* sampler, void* raw_sender) {
- if (!sampler->IsProfiling()) return;
- SignalSender* sender = reinterpret_cast<SignalSender*>(raw_sender);
- sender->SendProfilingSignal(sampler->platform_data()->vm_tid());
- }
static void DoRuntimeProfile(Sampler* sampler, void* ignored) {
if (!sampler->isolate()->IsInitialized()) return;
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
@@ -1211,15 +1204,12 @@
#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);
@@ -1228,8 +1218,6 @@
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 +1245,13 @@
}
#endif
SignalSender::SetUp();
+ CpuProfilerSignalHandler::SetUp();
}
void OS::TearDown() {
SignalSender::TearDown();
+ CpuProfilerSignalHandler::TearDown();
delete limit_mutex;
}
@@ -1280,10 +1270,16 @@
ASSERT(!IsActive());
delete data_;
}
+
+
+void Sampler::DoSample() {
+ platform_data()->SendProfilingSignal();
+}
void Sampler::Start() {
ASSERT(!IsActive());
+ CpuProfilerSignalHandler::InstallSignalHandler();
SetActive(true);
SignalSender::AddActiveSampler(this);
}
@@ -1291,6 +1287,7 @@
void Sampler::Stop() {
ASSERT(IsActive());
+ CpuProfilerSignalHandler::RestoreSignalHandler();
SignalSender::RemoveActiveSampler(this);
SetActive(false);
}
=======================================
--- /branches/bleeding_edge/src/platform-macos.cc Mon Oct 1 05:11:06 2012
+++ /branches/bleeding_edge/src/platform-macos.cc Tue Oct 2 02:58:11 2012
@@ -908,6 +908,11 @@
ASSERT(!IsActive());
delete data_;
}
+
+
+void Sampler::DoSample() {
+ // TODO(rogulenko): implement
+}
void Sampler::Start() {
=======================================
--- /branches/bleeding_edge/src/platform-openbsd.cc Mon Oct 1 05:11:06 2012
+++ /branches/bleeding_edge/src/platform-openbsd.cc Tue Oct 2 02:58:11 2012
@@ -962,6 +962,11 @@
ASSERT(!IsActive());
delete data_;
}
+
+
+void Sampler::DoSample() {
+ // TODO(rogulenko): implement
+}
void Sampler::Start() {
=======================================
--- /branches/bleeding_edge/src/platform-solaris.cc Mon Oct 1 05:11:06 2012
+++ /branches/bleeding_edge/src/platform-solaris.cc Tue Oct 2 02:58:11 2012
@@ -885,6 +885,11 @@
ASSERT(!IsActive());
delete data_;
}
+
+
+void Sampler::DoSample() {
+ // TODO(rogulenko): implement
+}
void Sampler::Start() {
=======================================
--- /branches/bleeding_edge/src/platform-win32.cc Mon Oct 1 05:11:06 2012
+++ /branches/bleeding_edge/src/platform-win32.cc Tue Oct 2 02:58:11 2012
@@ -2112,6 +2112,11 @@
ASSERT(!IsActive());
delete data_;
}
+
+
+void Sampler::DoSample() {
+ // TODO(rogulenko): implement
+}
void Sampler::Start() {
=======================================
--- /branches/bleeding_edge/src/platform.h Mon Oct 1 05:11:06 2012
+++ /branches/bleeding_edge/src/platform.h Tue Oct 2 02:58:11 2012
@@ -740,6 +740,9 @@
DoSampleStack(sample);
IncSamplesTaken();
}
+
+ // Performs platform-specific stack sampling.
+ void DoSample();
// This method is called for each sampling period with the current
// program counter.
=======================================
--- /branches/bleeding_edge/test/cctest/test-cpu-profiler.cc Fri Jan 13
05:09:52 2012
+++ /branches/bleeding_edge/test/cctest/test-cpu-profiler.cc Tue Oct 2
02:58:11 2012
@@ -20,7 +20,7 @@
TEST(StartStop) {
CpuProfilesCollection profiles;
ProfileGenerator generator(&profiles);
- ProfilerEventsProcessor processor(&generator);
+ ProfilerEventsProcessor processor(&generator, NULL, 1000);
processor.Start();
processor.Stop();
processor.Join();
@@ -81,7 +81,7 @@
CpuProfilesCollection profiles;
profiles.StartProfiling("", 1);
ProfileGenerator generator(&profiles);
- ProfilerEventsProcessor processor(&generator);
+ ProfilerEventsProcessor processor(&generator, NULL, 1000);
processor.Start();
// Enqueue code creation events.
@@ -142,7 +142,7 @@
CpuProfilesCollection profiles;
profiles.StartProfiling("", 1);
ProfileGenerator generator(&profiles);
- ProfilerEventsProcessor processor(&generator);
+ ProfilerEventsProcessor processor(&generator, NULL, 1000);
processor.Start();
processor.CodeCreateEvent(i::Logger::BUILTIN_TAG,
@@ -232,7 +232,7 @@
CpuProfilesCollection profiles;
profiles.StartProfiling("", 1);
ProfileGenerator generator(&profiles);
- ProfilerEventsProcessor processor(&generator);
+ ProfilerEventsProcessor processor(&generator, NULL, 1000);
processor.Start();
processor.CodeCreateEvent(i::Logger::BUILTIN_TAG,
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev