Index: runtime/vm/thread_interrupter.cc |
diff --git a/runtime/vm/thread_interrupter.cc b/runtime/vm/thread_interrupter.cc |
index 632688af35e3f9839fcf3c9747a36f0d4ecef3ab..59300708cee81b0f8cca78fcad3d0dd36d0cf103 100644 |
--- a/runtime/vm/thread_interrupter.cc |
+++ b/runtime/vm/thread_interrupter.cc |
@@ -9,8 +9,8 @@ namespace dart { |
// Notes: |
// |
-// The ThreadInterrupter interrupts all registered threads once per |
-// interrupt period (default is every millisecond). While the thread is |
+// The ThreadInterrupter interrupts all threads actively running isolates once |
+// per interrupt period (default is 1 millisecond). While the thread is |
// interrupted, the thread's interrupt callback is invoked. Callbacks cannot |
// rely on being executed on the interrupted thread. |
// |
@@ -27,8 +27,8 @@ namespace dart { |
// * Allocating memory. |
// * Taking a lock. |
// |
-// The ThreadInterrupter has a single monitor (monitor_). This monitor guards |
-// access to the list of threads registered to receive interrupts (threads_). |
+// The ThreadInterrupter has a single monitor (monitor_). This monitor is used |
+// to synchronize startup, shutdown, and waking up from a deep sleep. |
// |
// A thread can only register and unregister itself. Each thread has a heap |
// allocated ThreadState. A thread's ThreadState is lazily allocated the first |
@@ -50,6 +50,7 @@ bool ThreadInterrupter::thread_running_ = false; |
ThreadId ThreadInterrupter::interrupter_thread_id_ = Thread::kInvalidThreadId; |
Monitor* ThreadInterrupter::monitor_ = NULL; |
intptr_t ThreadInterrupter::interrupt_period_ = 1000; |
+intptr_t ThreadInterrupter::current_wait_time_ = Monitor::kNoTimeout; |
ThreadLocalKey ThreadInterrupter::thread_state_key_ = |
Thread::kUnsetThreadLocalKey; |
@@ -97,7 +98,10 @@ void ThreadInterrupter::Shutdown() { |
if (FLAG_trace_thread_interrupter) { |
OS::Print("ThreadInterrupter shutting down.\n"); |
} |
+ // Wake up in case of deep sleep. |
+ WakeUp(); |
while (thread_running_) { |
+ // Wait for thread to exit. |
shutdown_ml.Wait(); |
} |
// Join in the interrupter thread. On Windows, a thread's exit-code can |
@@ -124,6 +128,19 @@ void ThreadInterrupter::SetInterruptPeriod(intptr_t period) { |
} |
+void ThreadInterrupter::WakeUp() { |
+ ASSERT(initialized_); |
+ { |
+ MonitorLocker ml(monitor_); |
+ if (!InDeepSleep()) { |
+ // No need to notify, regularly waking up. |
+ return; |
+ } |
+ // Notify the interrupter to wake it from its deep sleep. |
+ ml.Notify(); |
+ } |
+} |
+ |
// Register the currently running thread for interrupts. If the current thread |
// is already registered, callback and data will be updated. |
InterruptableThreadState* ThreadInterrupter::Register( |
@@ -220,11 +237,27 @@ void ThreadInterruptNoOp(const InterruptedThreadState& state, void* data) { |
class ThreadInterrupterVisitIsolates : public IsolateVisitor { |
public: |
- ThreadInterrupterVisitIsolates() { } |
+ ThreadInterrupterVisitIsolates() { |
+ profiled_isolate_count_ = 0; |
+ } |
+ |
void VisitIsolate(Isolate* isolate) { |
ASSERT(isolate != NULL); |
- isolate->ProfileInterrupt(); |
+ if (isolate->ProfileInterrupt()) { |
Ivan Posva
2014/05/26 07:38:45
If ProfileInterrupt returned the number of interru
Cutch
2014/05/26 08:23:33
Back to intptr_t and added a comment on ProfileInt
|
+ profiled_isolate_count_++; |
+ } |
} |
+ |
+ intptr_t profiled_isolate_count() const { |
+ return profiled_isolate_count_; |
+ } |
+ |
+ void set_profiled_isolate_count(intptr_t profiled_isolate_count) { |
+ profiled_isolate_count_ = profiled_isolate_count; |
+ } |
+ |
+ private: |
+ intptr_t profiled_isolate_count_; |
}; |
@@ -242,11 +275,32 @@ void ThreadInterrupter::ThreadMain(uword parameters) { |
startup_ml.Notify(); |
} |
{ |
- MonitorLocker wait_ml(monitor_); |
ThreadInterrupterVisitIsolates visitor; |
+ current_wait_time_ = interrupt_period_; |
+ MonitorLocker wait_ml(monitor_); |
while (!shutdown_) { |
+ intptr_t r = wait_ml.WaitMicros(current_wait_time_); |
+ |
+ if ((r == Monitor::kNotified) && InDeepSleep()) { |
+ // Woken up from deep sleep. |
+ ASSERT(visitor.profiled_isolate_count() == 0); |
+ // Return to regular interrupts. |
+ current_wait_time_ = interrupt_period_; |
+ } |
+ |
+ // Reset count before visiting isolates. |
+ visitor.set_profiled_isolate_count(0); |
Isolate::VisitIsolates(&visitor); |
- wait_ml.WaitMicros(interrupt_period_); |
+ |
+ if (visitor.profiled_isolate_count() == 0) { |
+ // No isolates were profiled. In order to reduce unnecessary CPU |
+ // load, we will wait until we are notified before attempting to |
+ // interrupt again. |
+ current_wait_time_ = Monitor::kNoTimeout; |
+ continue; |
+ } |
+ |
+ ASSERT(current_wait_time_ != Monitor::kNoTimeout); |
} |
} |
if (FLAG_trace_thread_interrupter) { |