Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1081)

Side by Side Diff: runtime/vm/thread_interrupter.cc

Issue 128653004: Use list of isolates in profiler (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/simulator.h" 5 #include "vm/simulator.h"
6 #include "vm/thread_interrupter.h" 6 #include "vm/thread_interrupter.h"
7 7
8 namespace dart { 8 namespace dart {
9 9
10 // Notes: 10 // Notes:
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 "Trace thread interrupter"); 45 "Trace thread interrupter");
46 46
47 bool ThreadInterrupter::initialized_ = false; 47 bool ThreadInterrupter::initialized_ = false;
48 bool ThreadInterrupter::shutdown_ = false; 48 bool ThreadInterrupter::shutdown_ = false;
49 bool ThreadInterrupter::thread_running_ = false; 49 bool ThreadInterrupter::thread_running_ = false;
50 ThreadId ThreadInterrupter::interrupter_thread_id_ = Thread::kInvalidThreadId; 50 ThreadId ThreadInterrupter::interrupter_thread_id_ = Thread::kInvalidThreadId;
51 Monitor* ThreadInterrupter::monitor_ = NULL; 51 Monitor* ThreadInterrupter::monitor_ = NULL;
52 intptr_t ThreadInterrupter::interrupt_period_ = 1000; 52 intptr_t ThreadInterrupter::interrupt_period_ = 1000;
53 ThreadLocalKey ThreadInterrupter::thread_state_key_ = 53 ThreadLocalKey ThreadInterrupter::thread_state_key_ =
54 Thread::kUnsetThreadLocalKey; 54 Thread::kUnsetThreadLocalKey;
55 ThreadInterrupter::ThreadState** ThreadInterrupter::threads_ = NULL;
56 intptr_t ThreadInterrupter::threads_capacity_ = 0;
57 intptr_t ThreadInterrupter::threads_size_ = 0;
58 55
59 56
60 void ThreadInterrupter::InitOnce() { 57 void ThreadInterrupter::InitOnce() {
61 ASSERT(!initialized_); 58 ASSERT(!initialized_);
62 initialized_ = true;
63 ASSERT(thread_state_key_ == Thread::kUnsetThreadLocalKey); 59 ASSERT(thread_state_key_ == Thread::kUnsetThreadLocalKey);
64 thread_state_key_ = Thread::CreateThreadLocal(); 60 thread_state_key_ = Thread::CreateThreadLocal();
65 ASSERT(thread_state_key_ != Thread::kUnsetThreadLocalKey); 61 ASSERT(thread_state_key_ != Thread::kUnsetThreadLocalKey);
66 monitor_ = new Monitor(); 62 monitor_ = new Monitor();
67 ResizeThreads(16); 63 initialized_ = true;
64 }
65
66
67 void ThreadInterrupter::Startup() {
siva 2014/02/05 19:09:15 If --no-profile is specified this should be a noop
Cutch 2014/02/05 23:00:31 Correct, with --no-profile this function never get
68 ASSERT(initialized_);
68 if (FLAG_trace_thread_interrupter) { 69 if (FLAG_trace_thread_interrupter) {
69 OS::Print("ThreadInterrupter starting up.\n"); 70 OS::Print("ThreadInterrupter starting up.\n");
70 } 71 }
71 ASSERT(interrupter_thread_id_ == Thread::kInvalidThreadId); 72 ASSERT(interrupter_thread_id_ == Thread::kInvalidThreadId);
72 { 73 {
73 MonitorLocker startup_ml(monitor_); 74 MonitorLocker startup_ml(monitor_);
74 Thread::Start(ThreadMain, 0); 75 Thread::Start(ThreadMain, 0);
75 while (!thread_running_) { 76 while (!thread_running_) {
76 startup_ml.Wait(); 77 startup_ml.Wait();
77 } 78 }
78 } 79 }
79 ASSERT(interrupter_thread_id_ != Thread::kInvalidThreadId); 80 ASSERT(interrupter_thread_id_ != Thread::kInvalidThreadId);
80 if (FLAG_trace_thread_interrupter) { 81 if (FLAG_trace_thread_interrupter) {
81 OS::Print("ThreadInterrupter running.\n"); 82 OS::Print("ThreadInterrupter running.\n");
82 } 83 }
83 } 84 }
84 85
85 86
86 void ThreadInterrupter::Shutdown() { 87 void ThreadInterrupter::Shutdown() {
87 if (shutdown_) { 88 if (shutdown_) {
88 // Already shutdown. 89 // Already shutdown.
89 return; 90 return;
90 } 91 }
91 ASSERT(initialized_); 92 ASSERT(initialized_);
92 if (FLAG_trace_thread_interrupter) { 93 if (FLAG_trace_thread_interrupter) {
93 OS::Print("ThreadInterrupter shutting down.\n"); 94 OS::Print("ThreadInterrupter shutting down.\n");
94 } 95 }
95 intptr_t size_at_shutdown = 0;
96 { 96 {
97 MonitorLocker ml(monitor_); 97 MonitorLocker ml(monitor_);
98 shutdown_ = true; 98 shutdown_ = true;
99 size_at_shutdown = threads_size_;
100 threads_size_ = 0;
101 threads_capacity_ = 0;
102 free(threads_);
103 threads_ = NULL;
104 } 99 }
105 { 100 {
106 MonitorLocker shutdown_ml(monitor_); 101 MonitorLocker shutdown_ml(monitor_);
107 while (thread_running_) { 102 while (thread_running_) {
108 shutdown_ml.Wait(); 103 shutdown_ml.Wait();
109 } 104 }
110 } 105 }
111 interrupter_thread_id_ = Thread::kInvalidThreadId; 106 interrupter_thread_id_ = Thread::kInvalidThreadId;
112 if (FLAG_trace_thread_interrupter) { 107 if (FLAG_trace_thread_interrupter) {
113 OS::Print("ThreadInterrupter shut down (%" Pd ").\n", size_at_shutdown); 108 OS::Print("ThreadInterrupter shut down.\n");
114 } 109 }
115 } 110 }
116 111
117 // Delay between interrupts. 112 // Delay between interrupts.
118 void ThreadInterrupter::SetInterruptPeriod(intptr_t period) { 113 void ThreadInterrupter::SetInterruptPeriod(intptr_t period) {
119 if (shutdown_) { 114 if (shutdown_) {
120 return; 115 return;
121 } 116 }
122 ASSERT(initialized_); 117 ASSERT(initialized_);
123 ASSERT(period > 0); 118 ASSERT(period > 0);
124 { 119 interrupt_period_ = period;
125 MonitorLocker ml(monitor_);
126 interrupt_period_ = period;
127 }
128 } 120 }
129 121
130 122
131 // Register the currently running thread for interrupts. If the current thread 123 // Register the currently running thread for interrupts. If the current thread
132 // is already registered, callback and data will be updated. 124 // is already registered, callback and data will be updated.
133 void ThreadInterrupter::Register(ThreadInterruptCallback callback, void* data) { 125 InterruptableThreadState* ThreadInterrupter::Register(
126 ThreadInterruptCallback callback, void* data) {
134 if (shutdown_) { 127 if (shutdown_) {
135 return; 128 return NULL;
136 } 129 }
137 ASSERT(initialized_); 130 ASSERT(initialized_);
138 { 131 InterruptableThreadState* state = _EnsureThreadStateCreated();
139 MonitorLocker ml(monitor_); 132 // Set callback and data.
140 _EnsureThreadStateCreated(); 133 UpdateStateObject(callback, data);
141 // Set callback and data. 134 return state;
142 UpdateStateObject(callback, data);
143 _Enable();
144 }
145 } 135 }
146 136
147 137
148 // Unregister the currently running thread for interrupts. 138 // Unregister the currently running thread for interrupts.
149 void ThreadInterrupter::Unregister() { 139 void ThreadInterrupter::Unregister() {
150 if (shutdown_) { 140 if (shutdown_) {
151 return; 141 return;
152 } 142 }
153 ASSERT(initialized_); 143 ASSERT(initialized_);
154 { 144 _EnsureThreadStateCreated();
155 MonitorLocker ml(monitor_); 145 // Clear callback and data.
156 _EnsureThreadStateCreated(); 146 UpdateStateObject(NULL, NULL);
157 // Clear callback and data.
158 UpdateStateObject(NULL, NULL);
159 _Disable();
160 }
161 } 147 }
162 148
163 149
164 void ThreadInterrupter::Enable() { 150 InterruptableThreadState* ThreadInterrupter::_EnsureThreadStateCreated() {
165 if (shutdown_) { 151 InterruptableThreadState* state = CurrentThreadState();
166 return;
167 }
168 ASSERT(initialized_);
169 {
170 MonitorLocker ml(monitor_);
171 _EnsureThreadStateCreated();
172 _Enable();
173 }
174 }
175
176
177 void ThreadInterrupter::Disable() {
178 if (shutdown_) {
179 return;
180 }
181 ASSERT(initialized_);
182 {
183 MonitorLocker ml(monitor_);
184 _EnsureThreadStateCreated();
185 _Disable();
186 }
187 }
188
189
190 void ThreadInterrupter::_EnsureThreadStateCreated() {
191 ThreadState* state = CurrentThreadState();
192 if (state == NULL) { 152 if (state == NULL) {
193 // Create thread state object lazily. 153 // Create thread state object lazily.
194 ThreadId current_thread = Thread::GetCurrentThreadId(); 154 ThreadId current_thread = Thread::GetCurrentThreadId();
195 if (FLAG_trace_thread_interrupter) { 155 if (FLAG_trace_thread_interrupter) {
196 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); 156 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread);
197 OS::Print("ThreadInterrupter Tracking %p\n", 157 OS::Print("ThreadInterrupter Tracking %p\n",
198 reinterpret_cast<void*>(tid)); 158 reinterpret_cast<void*>(tid));
199 } 159 }
200 state = new ThreadState(); 160 state = new InterruptableThreadState();
201 state->callback = NULL; 161 state->callback = NULL;
202 state->data = NULL; 162 state->data = NULL;
203 state->id = current_thread; 163 state->id = current_thread;
204 SetCurrentThreadState(state); 164 SetCurrentThreadState(state);
205 } 165 }
166 return state;
206 } 167 }
207 168
208 169
209 void ThreadInterrupter::_Enable() {
210 // Must be called with monitor_ locked.
211 ThreadId current_thread = Thread::GetCurrentThreadId();
212 if (Thread::Compare(current_thread, interrupter_thread_id_)) {
213 return;
214 }
215 intptr_t i = FindThreadIndex(current_thread);
216 if (i >= 0) {
217 return;
218 }
219 AddThread(current_thread);
220 if (FLAG_trace_thread_interrupter) {
221 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread);
222 OS::Print("ThreadInterrupter Added %p\n", reinterpret_cast<void*>(tid));
223 }
224 }
225
226 void ThreadInterrupter::_Disable() {
227 // Must be called with monitor_ locked.
228 ThreadId current_thread = Thread::GetCurrentThreadId();
229 if (Thread::Compare(current_thread, interrupter_thread_id_)) {
230 return;
231 }
232 intptr_t index = FindThreadIndex(current_thread);
233 if (index < 0) {
234 // Not registered.
235 return;
236 }
237 ThreadState* state = RemoveThread(index);
238 ASSERT(state != NULL);
239 ASSERT(state == ThreadInterrupter::CurrentThreadState());
240 if (FLAG_trace_thread_interrupter) {
241 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread);
242 OS::Print("ThreadInterrupter Removed %p\n", reinterpret_cast<void*>(tid));
243 }
244 }
245
246 void ThreadInterrupter::UpdateStateObject(ThreadInterruptCallback callback, 170 void ThreadInterrupter::UpdateStateObject(ThreadInterruptCallback callback,
247 void* data) { 171 void* data) {
248 // Must be called with monitor_ locked. 172 InterruptableThreadState* state = CurrentThreadState();
249 ThreadState* state = CurrentThreadState();
250 ThreadId current_thread = Thread::GetCurrentThreadId(); 173 ThreadId current_thread = Thread::GetCurrentThreadId();
251 ASSERT(state != NULL); 174 ASSERT(state != NULL);
252 ASSERT(Thread::Compare(state->id, Thread::GetCurrentThreadId())); 175 ASSERT(Thread::Compare(state->id, Thread::GetCurrentThreadId()));
253 SetCurrentThreadState(NULL); 176 SetCurrentThreadState(NULL);
254 // It is now safe to modify the state object. If an interrupt occurs, 177 // It is now safe to modify the state object. If an interrupt occurs,
255 // the current thread state will be NULL. 178 // the current thread state will be NULL.
256 state->callback = callback; 179 state->callback = callback;
257 state->data = data; 180 state->data = data;
258 SetCurrentThreadState(state); 181 SetCurrentThreadState(state);
259 if (FLAG_trace_thread_interrupter) { 182 if (FLAG_trace_thread_interrupter) {
260 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); 183 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread);
261 if (callback == NULL) { 184 if (callback == NULL) {
262 OS::Print("ThreadInterrupter Cleared %p\n", reinterpret_cast<void*>(tid)); 185 OS::Print("ThreadInterrupter Cleared %p\n", reinterpret_cast<void*>(tid));
263 } else { 186 } else {
264 OS::Print("ThreadInterrupter Updated %p\n", reinterpret_cast<void*>(tid)); 187 OS::Print("ThreadInterrupter Updated %p\n", reinterpret_cast<void*>(tid));
265 } 188 }
266 } 189 }
267 } 190 }
268 191
269 192
270 ThreadInterrupter::ThreadState* ThreadInterrupter::CurrentThreadState() { 193 InterruptableThreadState* ThreadInterrupter::GetCurrentThreadState() {
271 ThreadState* state = reinterpret_cast<ThreadState*>( 194 return _EnsureThreadStateCreated();
195 }
196
197
198 InterruptableThreadState* ThreadInterrupter::CurrentThreadState() {
199 InterruptableThreadState* state = reinterpret_cast<InterruptableThreadState*>(
272 Thread::GetThreadLocal(thread_state_key_)); 200 Thread::GetThreadLocal(thread_state_key_));
273 return state; 201 return state;
274 } 202 }
275 203
276 204
277 void ThreadInterrupter::SetCurrentThreadState(ThreadState* state) { 205 void ThreadInterrupter::SetCurrentThreadState(InterruptableThreadState* state) {
278 Thread::SetThreadLocal(thread_state_key_, reinterpret_cast<uword>(state)); 206 Thread::SetThreadLocal(thread_state_key_, reinterpret_cast<uword>(state));
279 } 207 }
280 208
281 209
282 void ThreadInterrupter::ResizeThreads(intptr_t new_capacity) {
283 // Must be called with monitor_ locked.
284 ASSERT(new_capacity < kMaxThreads);
285 ASSERT(new_capacity > threads_capacity_);
286 ThreadState* state = NULL;
287 threads_ = reinterpret_cast<ThreadState**>(
288 realloc(threads_, sizeof(state) * new_capacity));
289 for (intptr_t i = threads_capacity_; i < new_capacity; i++) {
290 threads_[i] = NULL;
291 }
292 threads_capacity_ = new_capacity;
293 }
294
295
296 void ThreadInterrupter::AddThread(ThreadId id) {
297 // Must be called with monitor_ locked.
298 if (threads_ == NULL) {
299 // We are shutting down.
300 return;
301 }
302 ThreadState* state = CurrentThreadState();
303 if (state->callback == NULL) {
304 // No callback.
305 return;
306 }
307 if (threads_size_ == threads_capacity_) {
308 ResizeThreads(threads_capacity_ == 0 ? 16 : threads_capacity_ * 2);
309 }
310 threads_[threads_size_] = state;
311 threads_size_++;
312 }
313
314
315 intptr_t ThreadInterrupter::FindThreadIndex(ThreadId id) {
316 // Must be called with monitor_ locked.
317 if (threads_ == NULL) {
318 // We are shutting down.
319 return -1;
320 }
321 for (intptr_t i = 0; i < threads_size_; i++) {
322 if (threads_[i]->id == id) {
323 return i;
324 }
325 }
326 return -1;
327 }
328
329
330 ThreadInterrupter::ThreadState* ThreadInterrupter::RemoveThread(intptr_t i) {
331 // Must be called with monitor_ locked.
332 if (threads_ == NULL) {
333 // We are shutting down.
334 return NULL;
335 }
336 ASSERT(i < threads_size_);
337 ThreadState* state = threads_[i];
338 ASSERT(state != NULL);
339 intptr_t last = threads_size_ - 1;
340 if (i != last) {
341 threads_[i] = threads_[last];
342 }
343 // Mark last as NULL.
344 threads_[last] = NULL;
345 // Pop.
346 threads_size_--;
347 return state;
348 }
349
350
351 void ThreadInterruptNoOp(const InterruptedThreadState& state, void* data) { 210 void ThreadInterruptNoOp(const InterruptedThreadState& state, void* data) {
352 // NoOp. 211 // NoOp.
353 } 212 }
354 213
355 214
356 void ThreadInterrupter::ThreadMain(uword parameters) { 215 void ThreadInterrupter::ThreadMain(uword parameters) {
357 ASSERT(initialized_); 216 ASSERT(initialized_);
358 InstallSignalHandler(); 217 InstallSignalHandler();
359 if (FLAG_trace_thread_interrupter) { 218 if (FLAG_trace_thread_interrupter) {
360 OS::Print("ThreadInterrupter thread running.\n"); 219 OS::Print("ThreadInterrupter thread running.\n");
361 } 220 }
362 { 221 {
363 // Signal to main thread we are ready. 222 // Signal to main thread we are ready.
364 MonitorLocker startup_ml(monitor_); 223 MonitorLocker startup_ml(monitor_);
365 thread_running_ = true; 224 thread_running_ = true;
366 interrupter_thread_id_ = Thread::GetCurrentThreadId(); 225 interrupter_thread_id_ = Thread::GetCurrentThreadId();
367 startup_ml.Notify(); 226 startup_ml.Notify();
368 } 227 }
369 { 228 {
370 MonitorLocker ml(monitor_); 229 MonitorLocker ml(Isolate::isolates_monitor_);
371 while (!shutdown_) { 230 while (!shutdown_) {
372 int64_t current_time = OS::GetCurrentTimeMicros(); 231 int64_t current_time = OS::GetCurrentTimeMicros();
373 InterruptThreads(current_time); 232 InterruptThreads(current_time);
374 ml.WaitMicros(interrupt_period_); 233 ml.WaitMicros(interrupt_period_);
375 } 234 }
376 } 235 }
377 if (FLAG_trace_thread_interrupter) { 236 if (FLAG_trace_thread_interrupter) {
378 OS::Print("ThreadInterrupter thread exiting.\n"); 237 OS::Print("ThreadInterrupter thread exiting.\n");
379 } 238 }
380 { 239 {
381 // Signal to main thread we are exiting. 240 // Signal to main thread we are exiting.
382 MonitorLocker shutdown_ml(monitor_); 241 MonitorLocker shutdown_ml(monitor_);
383 thread_running_ = false; 242 thread_running_ = false;
384 shutdown_ml.Notify(); 243 shutdown_ml.Notify();
385 } 244 }
386 } 245 }
387 246
388 } // namespace dart 247 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698