OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/optimizing-compiler-thread.h" | 5 #include "src/optimizing-compiler-thread.h" |
6 | 6 |
7 #include "src/v8.h" | 7 #include "src/v8.h" |
8 | 8 |
9 #include "src/base/atomicops.h" | 9 #include "src/base/atomicops.h" |
10 #include "src/full-codegen.h" | 10 #include "src/full-codegen.h" |
11 #include "src/hydrogen.h" | 11 #include "src/hydrogen.h" |
12 #include "src/isolate.h" | 12 #include "src/isolate.h" |
13 #include "src/v8threads.h" | 13 #include "src/v8threads.h" |
14 | 14 |
15 namespace v8 { | 15 namespace v8 { |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 OptimizingCompilerThread::~OptimizingCompilerThread() { | 18 OptimizingCompilerThread::~OptimizingCompilerThread() { |
19 ASSERT_EQ(0, input_queue_length_); | 19 DCHECK_EQ(0, input_queue_length_); |
20 DeleteArray(input_queue_); | 20 DeleteArray(input_queue_); |
21 if (FLAG_concurrent_osr) { | 21 if (FLAG_concurrent_osr) { |
22 #ifdef DEBUG | 22 #ifdef DEBUG |
23 for (int i = 0; i < osr_buffer_capacity_; i++) { | 23 for (int i = 0; i < osr_buffer_capacity_; i++) { |
24 CHECK_EQ(NULL, osr_buffer_[i]); | 24 CHECK_EQ(NULL, osr_buffer_[i]); |
25 } | 25 } |
26 #endif | 26 #endif |
27 DeleteArray(osr_buffer_); | 27 DeleteArray(osr_buffer_); |
28 } | 28 } |
29 } | 29 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 time_spent_compiling_ += compiling_timer.Elapsed(); | 81 time_spent_compiling_ += compiling_timer.Elapsed(); |
82 } | 82 } |
83 } | 83 } |
84 } | 84 } |
85 | 85 |
86 | 86 |
87 OptimizedCompileJob* OptimizingCompilerThread::NextInput() { | 87 OptimizedCompileJob* OptimizingCompilerThread::NextInput() { |
88 base::LockGuard<base::Mutex> access_input_queue_(&input_queue_mutex_); | 88 base::LockGuard<base::Mutex> access_input_queue_(&input_queue_mutex_); |
89 if (input_queue_length_ == 0) return NULL; | 89 if (input_queue_length_ == 0) return NULL; |
90 OptimizedCompileJob* job = input_queue_[InputQueueIndex(0)]; | 90 OptimizedCompileJob* job = input_queue_[InputQueueIndex(0)]; |
91 ASSERT_NE(NULL, job); | 91 DCHECK_NE(NULL, job); |
92 input_queue_shift_ = InputQueueIndex(1); | 92 input_queue_shift_ = InputQueueIndex(1); |
93 input_queue_length_--; | 93 input_queue_length_--; |
94 return job; | 94 return job; |
95 } | 95 } |
96 | 96 |
97 | 97 |
98 void OptimizingCompilerThread::CompileNext() { | 98 void OptimizingCompilerThread::CompileNext() { |
99 OptimizedCompileJob* job = NextInput(); | 99 OptimizedCompileJob* job = NextInput(); |
100 ASSERT_NE(NULL, job); | 100 DCHECK_NE(NULL, job); |
101 | 101 |
102 // The function may have already been optimized by OSR. Simply continue. | 102 // The function may have already been optimized by OSR. Simply continue. |
103 OptimizedCompileJob::Status status = job->OptimizeGraph(); | 103 OptimizedCompileJob::Status status = job->OptimizeGraph(); |
104 USE(status); // Prevent an unused-variable error in release mode. | 104 USE(status); // Prevent an unused-variable error in release mode. |
105 ASSERT(status != OptimizedCompileJob::FAILED); | 105 DCHECK(status != OptimizedCompileJob::FAILED); |
106 | 106 |
107 // The function may have already been optimized by OSR. Simply continue. | 107 // The function may have already been optimized by OSR. Simply continue. |
108 // Use a mutex to make sure that functions marked for install | 108 // Use a mutex to make sure that functions marked for install |
109 // are always also queued. | 109 // are always also queued. |
110 output_queue_.Enqueue(job); | 110 output_queue_.Enqueue(job); |
111 isolate_->stack_guard()->RequestInstallCode(); | 111 isolate_->stack_guard()->RequestInstallCode(); |
112 } | 112 } |
113 | 113 |
114 | 114 |
115 static void DisposeOptimizedCompileJob(OptimizedCompileJob* job, | 115 static void DisposeOptimizedCompileJob(OptimizedCompileJob* job, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 for (int i = 0; i < osr_buffer_capacity_; i++) { | 162 for (int i = 0; i < osr_buffer_capacity_; i++) { |
163 if (osr_buffer_[i] != NULL) { | 163 if (osr_buffer_[i] != NULL) { |
164 DisposeOptimizedCompileJob(osr_buffer_[i], restore_function_code); | 164 DisposeOptimizedCompileJob(osr_buffer_[i], restore_function_code); |
165 osr_buffer_[i] = NULL; | 165 osr_buffer_[i] = NULL; |
166 } | 166 } |
167 } | 167 } |
168 } | 168 } |
169 | 169 |
170 | 170 |
171 void OptimizingCompilerThread::Flush() { | 171 void OptimizingCompilerThread::Flush() { |
172 ASSERT(!IsOptimizerThread()); | 172 DCHECK(!IsOptimizerThread()); |
173 base::Release_Store(&stop_thread_, static_cast<base::AtomicWord>(FLUSH)); | 173 base::Release_Store(&stop_thread_, static_cast<base::AtomicWord>(FLUSH)); |
174 if (FLAG_block_concurrent_recompilation) Unblock(); | 174 if (FLAG_block_concurrent_recompilation) Unblock(); |
175 input_queue_semaphore_.Signal(); | 175 input_queue_semaphore_.Signal(); |
176 stop_semaphore_.Wait(); | 176 stop_semaphore_.Wait(); |
177 FlushOutputQueue(true); | 177 FlushOutputQueue(true); |
178 if (FLAG_concurrent_osr) FlushOsrBuffer(true); | 178 if (FLAG_concurrent_osr) FlushOsrBuffer(true); |
179 if (FLAG_trace_concurrent_recompilation) { | 179 if (FLAG_trace_concurrent_recompilation) { |
180 PrintF(" ** Flushed concurrent recompilation queues.\n"); | 180 PrintF(" ** Flushed concurrent recompilation queues.\n"); |
181 } | 181 } |
182 } | 182 } |
183 | 183 |
184 | 184 |
185 void OptimizingCompilerThread::Stop() { | 185 void OptimizingCompilerThread::Stop() { |
186 ASSERT(!IsOptimizerThread()); | 186 DCHECK(!IsOptimizerThread()); |
187 base::Release_Store(&stop_thread_, static_cast<base::AtomicWord>(STOP)); | 187 base::Release_Store(&stop_thread_, static_cast<base::AtomicWord>(STOP)); |
188 if (FLAG_block_concurrent_recompilation) Unblock(); | 188 if (FLAG_block_concurrent_recompilation) Unblock(); |
189 input_queue_semaphore_.Signal(); | 189 input_queue_semaphore_.Signal(); |
190 stop_semaphore_.Wait(); | 190 stop_semaphore_.Wait(); |
191 | 191 |
192 if (FLAG_concurrent_recompilation_delay != 0) { | 192 if (FLAG_concurrent_recompilation_delay != 0) { |
193 // At this point the optimizing compiler thread's event loop has stopped. | 193 // At this point the optimizing compiler thread's event loop has stopped. |
194 // There is no need for a mutex when reading input_queue_length_. | 194 // There is no need for a mutex when reading input_queue_length_. |
195 while (input_queue_length_ > 0) CompileNext(); | 195 while (input_queue_length_ > 0) CompileNext(); |
196 InstallOptimizedFunctions(); | 196 InstallOptimizedFunctions(); |
(...skipping 12 matching lines...) Expand all Loading... |
209 if ((FLAG_trace_osr || FLAG_trace_concurrent_recompilation) && | 209 if ((FLAG_trace_osr || FLAG_trace_concurrent_recompilation) && |
210 FLAG_concurrent_osr) { | 210 FLAG_concurrent_osr) { |
211 PrintF("[COSR hit rate %d / %d]\n", osr_hits_, osr_attempts_); | 211 PrintF("[COSR hit rate %d / %d]\n", osr_hits_, osr_attempts_); |
212 } | 212 } |
213 | 213 |
214 Join(); | 214 Join(); |
215 } | 215 } |
216 | 216 |
217 | 217 |
218 void OptimizingCompilerThread::InstallOptimizedFunctions() { | 218 void OptimizingCompilerThread::InstallOptimizedFunctions() { |
219 ASSERT(!IsOptimizerThread()); | 219 DCHECK(!IsOptimizerThread()); |
220 HandleScope handle_scope(isolate_); | 220 HandleScope handle_scope(isolate_); |
221 | 221 |
222 OptimizedCompileJob* job; | 222 OptimizedCompileJob* job; |
223 while (output_queue_.Dequeue(&job)) { | 223 while (output_queue_.Dequeue(&job)) { |
224 CompilationInfo* info = job->info(); | 224 CompilationInfo* info = job->info(); |
225 Handle<JSFunction> function(*info->closure()); | 225 Handle<JSFunction> function(*info->closure()); |
226 if (info->is_osr()) { | 226 if (info->is_osr()) { |
227 if (FLAG_trace_osr) { | 227 if (FLAG_trace_osr) { |
228 PrintF("[COSR - "); | 228 PrintF("[COSR - "); |
229 info->closure()->PrintName(); | 229 info->closure()->PrintName(); |
(...skipping 12 matching lines...) Expand all Loading... |
242 Handle<Code> code = Compiler::GetConcurrentlyOptimizedCode(job); | 242 Handle<Code> code = Compiler::GetConcurrentlyOptimizedCode(job); |
243 function->ReplaceCode( | 243 function->ReplaceCode( |
244 code.is_null() ? function->shared()->code() : *code); | 244 code.is_null() ? function->shared()->code() : *code); |
245 } | 245 } |
246 } | 246 } |
247 } | 247 } |
248 } | 248 } |
249 | 249 |
250 | 250 |
251 void OptimizingCompilerThread::QueueForOptimization(OptimizedCompileJob* job) { | 251 void OptimizingCompilerThread::QueueForOptimization(OptimizedCompileJob* job) { |
252 ASSERT(IsQueueAvailable()); | 252 DCHECK(IsQueueAvailable()); |
253 ASSERT(!IsOptimizerThread()); | 253 DCHECK(!IsOptimizerThread()); |
254 CompilationInfo* info = job->info(); | 254 CompilationInfo* info = job->info(); |
255 if (info->is_osr()) { | 255 if (info->is_osr()) { |
256 osr_attempts_++; | 256 osr_attempts_++; |
257 AddToOsrBuffer(job); | 257 AddToOsrBuffer(job); |
258 // Add job to the front of the input queue. | 258 // Add job to the front of the input queue. |
259 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); | 259 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); |
260 ASSERT_LT(input_queue_length_, input_queue_capacity_); | 260 DCHECK_LT(input_queue_length_, input_queue_capacity_); |
261 // Move shift_ back by one. | 261 // Move shift_ back by one. |
262 input_queue_shift_ = InputQueueIndex(input_queue_capacity_ - 1); | 262 input_queue_shift_ = InputQueueIndex(input_queue_capacity_ - 1); |
263 input_queue_[InputQueueIndex(0)] = job; | 263 input_queue_[InputQueueIndex(0)] = job; |
264 input_queue_length_++; | 264 input_queue_length_++; |
265 } else { | 265 } else { |
266 // Add job to the back of the input queue. | 266 // Add job to the back of the input queue. |
267 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); | 267 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); |
268 ASSERT_LT(input_queue_length_, input_queue_capacity_); | 268 DCHECK_LT(input_queue_length_, input_queue_capacity_); |
269 input_queue_[InputQueueIndex(input_queue_length_)] = job; | 269 input_queue_[InputQueueIndex(input_queue_length_)] = job; |
270 input_queue_length_++; | 270 input_queue_length_++; |
271 } | 271 } |
272 if (FLAG_block_concurrent_recompilation) { | 272 if (FLAG_block_concurrent_recompilation) { |
273 blocked_jobs_++; | 273 blocked_jobs_++; |
274 } else { | 274 } else { |
275 input_queue_semaphore_.Signal(); | 275 input_queue_semaphore_.Signal(); |
276 } | 276 } |
277 } | 277 } |
278 | 278 |
279 | 279 |
280 void OptimizingCompilerThread::Unblock() { | 280 void OptimizingCompilerThread::Unblock() { |
281 ASSERT(!IsOptimizerThread()); | 281 DCHECK(!IsOptimizerThread()); |
282 while (blocked_jobs_ > 0) { | 282 while (blocked_jobs_ > 0) { |
283 input_queue_semaphore_.Signal(); | 283 input_queue_semaphore_.Signal(); |
284 blocked_jobs_--; | 284 blocked_jobs_--; |
285 } | 285 } |
286 } | 286 } |
287 | 287 |
288 | 288 |
289 OptimizedCompileJob* OptimizingCompilerThread::FindReadyOSRCandidate( | 289 OptimizedCompileJob* OptimizingCompilerThread::FindReadyOSRCandidate( |
290 Handle<JSFunction> function, BailoutId osr_ast_id) { | 290 Handle<JSFunction> function, BailoutId osr_ast_id) { |
291 ASSERT(!IsOptimizerThread()); | 291 DCHECK(!IsOptimizerThread()); |
292 for (int i = 0; i < osr_buffer_capacity_; i++) { | 292 for (int i = 0; i < osr_buffer_capacity_; i++) { |
293 OptimizedCompileJob* current = osr_buffer_[i]; | 293 OptimizedCompileJob* current = osr_buffer_[i]; |
294 if (current != NULL && | 294 if (current != NULL && |
295 current->IsWaitingForInstall() && | 295 current->IsWaitingForInstall() && |
296 current->info()->HasSameOsrEntry(function, osr_ast_id)) { | 296 current->info()->HasSameOsrEntry(function, osr_ast_id)) { |
297 osr_hits_++; | 297 osr_hits_++; |
298 osr_buffer_[i] = NULL; | 298 osr_buffer_[i] = NULL; |
299 return current; | 299 return current; |
300 } | 300 } |
301 } | 301 } |
302 return NULL; | 302 return NULL; |
303 } | 303 } |
304 | 304 |
305 | 305 |
306 bool OptimizingCompilerThread::IsQueuedForOSR(Handle<JSFunction> function, | 306 bool OptimizingCompilerThread::IsQueuedForOSR(Handle<JSFunction> function, |
307 BailoutId osr_ast_id) { | 307 BailoutId osr_ast_id) { |
308 ASSERT(!IsOptimizerThread()); | 308 DCHECK(!IsOptimizerThread()); |
309 for (int i = 0; i < osr_buffer_capacity_; i++) { | 309 for (int i = 0; i < osr_buffer_capacity_; i++) { |
310 OptimizedCompileJob* current = osr_buffer_[i]; | 310 OptimizedCompileJob* current = osr_buffer_[i]; |
311 if (current != NULL && | 311 if (current != NULL && |
312 current->info()->HasSameOsrEntry(function, osr_ast_id)) { | 312 current->info()->HasSameOsrEntry(function, osr_ast_id)) { |
313 return !current->IsWaitingForInstall(); | 313 return !current->IsWaitingForInstall(); |
314 } | 314 } |
315 } | 315 } |
316 return false; | 316 return false; |
317 } | 317 } |
318 | 318 |
319 | 319 |
320 bool OptimizingCompilerThread::IsQueuedForOSR(JSFunction* function) { | 320 bool OptimizingCompilerThread::IsQueuedForOSR(JSFunction* function) { |
321 ASSERT(!IsOptimizerThread()); | 321 DCHECK(!IsOptimizerThread()); |
322 for (int i = 0; i < osr_buffer_capacity_; i++) { | 322 for (int i = 0; i < osr_buffer_capacity_; i++) { |
323 OptimizedCompileJob* current = osr_buffer_[i]; | 323 OptimizedCompileJob* current = osr_buffer_[i]; |
324 if (current != NULL && *current->info()->closure() == function) { | 324 if (current != NULL && *current->info()->closure() == function) { |
325 return !current->IsWaitingForInstall(); | 325 return !current->IsWaitingForInstall(); |
326 } | 326 } |
327 } | 327 } |
328 return false; | 328 return false; |
329 } | 329 } |
330 | 330 |
331 | 331 |
332 void OptimizingCompilerThread::AddToOsrBuffer(OptimizedCompileJob* job) { | 332 void OptimizingCompilerThread::AddToOsrBuffer(OptimizedCompileJob* job) { |
333 ASSERT(!IsOptimizerThread()); | 333 DCHECK(!IsOptimizerThread()); |
334 // Find the next slot that is empty or has a stale job. | 334 // Find the next slot that is empty or has a stale job. |
335 OptimizedCompileJob* stale = NULL; | 335 OptimizedCompileJob* stale = NULL; |
336 while (true) { | 336 while (true) { |
337 stale = osr_buffer_[osr_buffer_cursor_]; | 337 stale = osr_buffer_[osr_buffer_cursor_]; |
338 if (stale == NULL || stale->IsWaitingForInstall()) break; | 338 if (stale == NULL || stale->IsWaitingForInstall()) break; |
339 osr_buffer_cursor_ = (osr_buffer_cursor_ + 1) % osr_buffer_capacity_; | 339 osr_buffer_cursor_ = (osr_buffer_cursor_ + 1) % osr_buffer_capacity_; |
340 } | 340 } |
341 | 341 |
342 // Add to found slot and dispose the evicted job. | 342 // Add to found slot and dispose the evicted job. |
343 if (stale != NULL) { | 343 if (stale != NULL) { |
344 ASSERT(stale->IsWaitingForInstall()); | 344 DCHECK(stale->IsWaitingForInstall()); |
345 CompilationInfo* info = stale->info(); | 345 CompilationInfo* info = stale->info(); |
346 if (FLAG_trace_osr) { | 346 if (FLAG_trace_osr) { |
347 PrintF("[COSR - Discarded "); | 347 PrintF("[COSR - Discarded "); |
348 info->closure()->PrintName(); | 348 info->closure()->PrintName(); |
349 PrintF(", AST id %d]\n", info->osr_ast_id().ToInt()); | 349 PrintF(", AST id %d]\n", info->osr_ast_id().ToInt()); |
350 } | 350 } |
351 DisposeOptimizedCompileJob(stale, false); | 351 DisposeOptimizedCompileJob(stale, false); |
352 } | 352 } |
353 osr_buffer_[osr_buffer_cursor_] = job; | 353 osr_buffer_[osr_buffer_cursor_] = job; |
354 osr_buffer_cursor_ = (osr_buffer_cursor_ + 1) % osr_buffer_capacity_; | 354 osr_buffer_cursor_ = (osr_buffer_cursor_ + 1) % osr_buffer_capacity_; |
355 } | 355 } |
356 | 356 |
357 | 357 |
358 #ifdef DEBUG | 358 #ifdef DEBUG |
359 bool OptimizingCompilerThread::IsOptimizerThread(Isolate* isolate) { | 359 bool OptimizingCompilerThread::IsOptimizerThread(Isolate* isolate) { |
360 return isolate->concurrent_recompilation_enabled() && | 360 return isolate->concurrent_recompilation_enabled() && |
361 isolate->optimizing_compiler_thread()->IsOptimizerThread(); | 361 isolate->optimizing_compiler_thread()->IsOptimizerThread(); |
362 } | 362 } |
363 | 363 |
364 | 364 |
365 bool OptimizingCompilerThread::IsOptimizerThread() { | 365 bool OptimizingCompilerThread::IsOptimizerThread() { |
366 base::LockGuard<base::Mutex> lock_guard(&thread_id_mutex_); | 366 base::LockGuard<base::Mutex> lock_guard(&thread_id_mutex_); |
367 return ThreadId::Current().ToInteger() == thread_id_; | 367 return ThreadId::Current().ToInteger() == thread_id_; |
368 } | 368 } |
369 #endif | 369 #endif |
370 | 370 |
371 | 371 |
372 } } // namespace v8::internal | 372 } } // namespace v8::internal |
OLD | NEW |