Index: runtime/vm/thread_interrupter.cc |
diff --git a/runtime/vm/thread_interrupter.cc b/runtime/vm/thread_interrupter.cc |
index 632688af35e3f9839fcf3c9747a36f0d4ecef3ab..c3bd8565330ecd14be330695decee329311149ad 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_ = 0; |
ThreadLocalKey ThreadInterrupter::thread_state_key_ = |
Thread::kUnsetThreadLocalKey; |
@@ -98,6 +99,8 @@ void ThreadInterrupter::Shutdown() { |
OS::Print("ThreadInterrupter shutting down.\n"); |
} |
while (thread_running_) { |
+ // Notify in case of deep sleep. |
+ shutdown_ml.Notify(); |
shutdown_ml.Wait(); |
Ivan Posva
2014/05/25 12:25:16
Aren't you waiting on your own notification in thi
Cutch
2014/05/26 06:59:35
Moved the Notify out of the loop and replaced it w
|
} |
// Join in the interrupter thread. On Windows, a thread's exit-code can |
@@ -124,6 +127,22 @@ void ThreadInterrupter::SetInterruptPeriod(intptr_t period) { |
} |
+void ThreadInterrupter::WakeUp() { |
+ if (shutdown_) { |
+ return; |
+ } |
+ 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 +239,25 @@ 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(); |
+ profiled_isolate_count_ += isolate->ProfileInterrupt(); |
Ivan Posva
2014/05/25 19:07:48
Please add a comment in ProfileInterrupt that the
|
} |
+ |
+ 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_ = 0; |
Ivan Posva
2014/05/25 12:25:16
Please add a comment that a wait time of 0 means i
Cutch
2014/05/26 06:59:35
Switched to using Monitor::kNoTimeout which should
|
+ continue; |
+ } |
+ |
+ ASSERT(current_wait_time_ == interrupt_period_); |
} |
} |
if (FLAG_trace_thread_interrupter) { |