| 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" | |
| 14 | 13 |
| 15 namespace v8 { | 14 namespace v8 { |
| 16 namespace internal { | 15 namespace internal { |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 void DisposeOptimizedCompileJob(OptimizedCompileJob* job, | 19 void DisposeOptimizedCompileJob(OptimizedCompileJob* job, |
| 21 bool restore_function_code) { | 20 bool restore_function_code) { |
| 22 // The recompile job is allocated in the CompilationInfo's zone. | 21 // The recompile job is allocated in the CompilationInfo's zone. |
| 23 CompilationInfo* info = job->info(); | 22 CompilationInfo* info = job->info(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 #ifdef DEBUG | 93 #ifdef DEBUG |
| 95 for (int i = 0; i < osr_buffer_capacity_; i++) { | 94 for (int i = 0; i < osr_buffer_capacity_; i++) { |
| 96 CHECK_NULL(osr_buffer_[i]); | 95 CHECK_NULL(osr_buffer_[i]); |
| 97 } | 96 } |
| 98 #endif | 97 #endif |
| 99 DeleteArray(osr_buffer_); | 98 DeleteArray(osr_buffer_); |
| 100 } | 99 } |
| 101 } | 100 } |
| 102 | 101 |
| 103 | 102 |
| 104 void OptimizingCompilerThread::Run() { | |
| 105 #ifdef DEBUG | |
| 106 { base::LockGuard<base::Mutex> lock_guard(&thread_id_mutex_); | |
| 107 thread_id_ = ThreadId::Current().ToInteger(); | |
| 108 } | |
| 109 #endif | |
| 110 DisallowHeapAllocation no_allocation; | |
| 111 DisallowHandleAllocation no_handles; | |
| 112 DisallowHandleDereference no_deref; | |
| 113 | |
| 114 if (job_based_recompilation_) { | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 base::ElapsedTimer total_timer; | |
| 119 if (tracing_enabled_) total_timer.Start(); | |
| 120 | |
| 121 while (true) { | |
| 122 input_queue_semaphore_.Wait(); | |
| 123 TimerEventScope<TimerEventRecompileConcurrent> timer(isolate_); | |
| 124 | |
| 125 if (recompilation_delay_ != 0) { | |
| 126 base::OS::Sleep(recompilation_delay_); | |
| 127 } | |
| 128 | |
| 129 switch (static_cast<StopFlag>(base::Acquire_Load(&stop_thread_))) { | |
| 130 case CONTINUE: | |
| 131 break; | |
| 132 case STOP: | |
| 133 if (tracing_enabled_) { | |
| 134 time_spent_total_ = total_timer.Elapsed(); | |
| 135 } | |
| 136 stop_semaphore_.Signal(); | |
| 137 return; | |
| 138 case FLUSH: | |
| 139 // The main thread is blocked, waiting for the stop semaphore. | |
| 140 { AllowHandleDereference allow_handle_dereference; | |
| 141 FlushInputQueue(true); | |
| 142 } | |
| 143 base::Release_Store(&stop_thread_, | |
| 144 static_cast<base::AtomicWord>(CONTINUE)); | |
| 145 stop_semaphore_.Signal(); | |
| 146 // Return to start of consumer loop. | |
| 147 continue; | |
| 148 } | |
| 149 | |
| 150 base::ElapsedTimer compiling_timer; | |
| 151 if (tracing_enabled_) compiling_timer.Start(); | |
| 152 | |
| 153 CompileNext(NextInput()); | |
| 154 | |
| 155 if (tracing_enabled_) { | |
| 156 time_spent_compiling_ += compiling_timer.Elapsed(); | |
| 157 } | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 | |
| 162 OptimizedCompileJob* OptimizingCompilerThread::NextInput( | 103 OptimizedCompileJob* OptimizingCompilerThread::NextInput( |
| 163 bool check_if_flushing) { | 104 bool check_if_flushing) { |
| 164 base::LockGuard<base::Mutex> access_input_queue_(&input_queue_mutex_); | 105 base::LockGuard<base::Mutex> access_input_queue_(&input_queue_mutex_); |
| 165 if (input_queue_length_ == 0) return NULL; | 106 if (input_queue_length_ == 0) return NULL; |
| 166 OptimizedCompileJob* job = input_queue_[InputQueueIndex(0)]; | 107 OptimizedCompileJob* job = input_queue_[InputQueueIndex(0)]; |
| 167 DCHECK_NOT_NULL(job); | 108 DCHECK_NOT_NULL(job); |
| 168 input_queue_shift_ = InputQueueIndex(1); | 109 input_queue_shift_ = InputQueueIndex(1); |
| 169 input_queue_length_--; | 110 input_queue_length_--; |
| 170 if (check_if_flushing) { | 111 if (check_if_flushing) { |
| 171 if (static_cast<StopFlag>(base::Acquire_Load(&stop_thread_)) != CONTINUE) { | 112 if (static_cast<ModeFlag>(base::Acquire_Load(&mode_)) == FLUSH) { |
| 172 if (!job->info()->is_osr()) { | 113 if (!job->info()->is_osr()) { |
| 173 AllowHandleDereference allow_handle_dereference; | 114 AllowHandleDereference allow_handle_dereference; |
| 174 DisposeOptimizedCompileJob(job, true); | 115 DisposeOptimizedCompileJob(job, true); |
| 175 } | 116 } |
| 176 return NULL; | 117 return NULL; |
| 177 } | 118 } |
| 178 } | 119 } |
| 179 return job; | 120 return job; |
| 180 } | 121 } |
| 181 | 122 |
| 182 | 123 |
| 183 void OptimizingCompilerThread::CompileNext(OptimizedCompileJob* job) { | 124 void OptimizingCompilerThread::CompileNext(OptimizedCompileJob* job) { |
| 184 if (!job) return; | 125 if (!job) return; |
| 185 | 126 |
| 186 // The function may have already been optimized by OSR. Simply continue. | 127 // The function may have already been optimized by OSR. Simply continue. |
| 187 OptimizedCompileJob::Status status = job->OptimizeGraph(); | 128 OptimizedCompileJob::Status status = job->OptimizeGraph(); |
| 188 USE(status); // Prevent an unused-variable error in release mode. | 129 USE(status); // Prevent an unused-variable error in release mode. |
| 189 DCHECK(status != OptimizedCompileJob::FAILED); | 130 DCHECK(status != OptimizedCompileJob::FAILED); |
| 190 | 131 |
| 191 // The function may have already been optimized by OSR. Simply continue. | 132 // The function may have already been optimized by OSR. Simply continue. |
| 192 // Use a mutex to make sure that functions marked for install | 133 // Use a mutex to make sure that functions marked for install |
| 193 // are always also queued. | 134 // are always also queued. |
| 194 if (job_based_recompilation_) output_queue_mutex_.Lock(); | 135 base::LockGuard<base::Mutex> access_output_queue_(&output_queue_mutex_); |
| 195 output_queue_.Enqueue(job); | 136 output_queue_.push(job); |
| 196 if (job_based_recompilation_) output_queue_mutex_.Unlock(); | |
| 197 isolate_->stack_guard()->RequestInstallCode(); | 137 isolate_->stack_guard()->RequestInstallCode(); |
| 198 } | 138 } |
| 199 | 139 |
| 200 | 140 |
| 201 void OptimizingCompilerThread::FlushInputQueue(bool restore_function_code) { | 141 void OptimizingCompilerThread::FlushOutputQueue(bool restore_function_code) { |
| 202 OptimizedCompileJob* job; | 142 base::LockGuard<base::Mutex> access_output_queue_(&output_queue_mutex_); |
| 203 while ((job = NextInput())) { | 143 while (!output_queue_.empty()) { |
| 204 DCHECK(!job_based_recompilation_); | 144 OptimizedCompileJob* job = output_queue_.front(); |
| 205 // This should not block, since we have one signal on the input queue | 145 output_queue_.pop(); |
| 206 // semaphore corresponding to each element in the input queue. | 146 |
| 207 input_queue_semaphore_.Wait(); | |
| 208 // OSR jobs are dealt with separately. | 147 // OSR jobs are dealt with separately. |
| 209 if (!job->info()->is_osr()) { | 148 if (!job->info()->is_osr()) { |
| 210 DisposeOptimizedCompileJob(job, restore_function_code); | 149 DisposeOptimizedCompileJob(job, restore_function_code); |
| 211 } | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 | |
| 216 void OptimizingCompilerThread::FlushOutputQueue(bool restore_function_code) { | |
| 217 OptimizedCompileJob* job; | |
| 218 while (output_queue_.Dequeue(&job)) { | |
| 219 // OSR jobs are dealt with separately. | |
| 220 if (!job->info()->is_osr()) { | |
| 221 DisposeOptimizedCompileJob(job, restore_function_code); | |
| 222 } | 150 } |
| 223 } | 151 } |
| 224 } | 152 } |
| 225 | 153 |
| 226 | 154 |
| 227 void OptimizingCompilerThread::FlushOsrBuffer(bool restore_function_code) { | 155 void OptimizingCompilerThread::FlushOsrBuffer(bool restore_function_code) { |
| 228 for (int i = 0; i < osr_buffer_capacity_; i++) { | 156 for (int i = 0; i < osr_buffer_capacity_; i++) { |
| 229 if (osr_buffer_[i] != NULL) { | 157 if (osr_buffer_[i] != NULL) { |
| 230 DisposeOptimizedCompileJob(osr_buffer_[i], restore_function_code); | 158 DisposeOptimizedCompileJob(osr_buffer_[i], restore_function_code); |
| 231 osr_buffer_[i] = NULL; | 159 osr_buffer_[i] = NULL; |
| 232 } | 160 } |
| 233 } | 161 } |
| 234 } | 162 } |
| 235 | 163 |
| 236 | 164 |
| 237 void OptimizingCompilerThread::Flush() { | 165 void OptimizingCompilerThread::Flush() { |
| 238 DCHECK(!IsOptimizerThread()); | 166 base::Release_Store(&mode_, static_cast<base::AtomicWord>(FLUSH)); |
| 239 base::Release_Store(&stop_thread_, static_cast<base::AtomicWord>(FLUSH)); | |
| 240 if (FLAG_block_concurrent_recompilation) Unblock(); | 167 if (FLAG_block_concurrent_recompilation) Unblock(); |
| 241 if (!job_based_recompilation_) { | 168 { |
| 242 input_queue_semaphore_.Signal(); | |
| 243 stop_semaphore_.Wait(); | |
| 244 } else { | |
| 245 base::LockGuard<base::Mutex> lock_guard(&ref_count_mutex_); | 169 base::LockGuard<base::Mutex> lock_guard(&ref_count_mutex_); |
| 246 while (ref_count_ > 0) ref_count_zero_.Wait(&ref_count_mutex_); | 170 while (ref_count_ > 0) ref_count_zero_.Wait(&ref_count_mutex_); |
| 247 base::Release_Store(&stop_thread_, static_cast<base::AtomicWord>(CONTINUE)); | 171 base::Release_Store(&mode_, static_cast<base::AtomicWord>(COMPILE)); |
| 248 } | 172 } |
| 249 FlushOutputQueue(true); | 173 FlushOutputQueue(true); |
| 250 if (FLAG_concurrent_osr) FlushOsrBuffer(true); | 174 if (FLAG_concurrent_osr) FlushOsrBuffer(true); |
| 251 if (tracing_enabled_) { | 175 if (FLAG_trace_concurrent_recompilation) { |
| 252 PrintF(" ** Flushed concurrent recompilation queues.\n"); | 176 PrintF(" ** Flushed concurrent recompilation queues.\n"); |
| 253 } | 177 } |
| 254 } | 178 } |
| 255 | 179 |
| 256 | 180 |
| 257 void OptimizingCompilerThread::Stop() { | 181 void OptimizingCompilerThread::Stop() { |
| 258 DCHECK(!IsOptimizerThread()); | 182 base::Release_Store(&mode_, static_cast<base::AtomicWord>(FLUSH)); |
| 259 base::Release_Store(&stop_thread_, static_cast<base::AtomicWord>(STOP)); | |
| 260 if (FLAG_block_concurrent_recompilation) Unblock(); | 183 if (FLAG_block_concurrent_recompilation) Unblock(); |
| 261 if (!job_based_recompilation_) { | 184 { |
| 262 input_queue_semaphore_.Signal(); | |
| 263 stop_semaphore_.Wait(); | |
| 264 } else { | |
| 265 base::LockGuard<base::Mutex> lock_guard(&ref_count_mutex_); | 185 base::LockGuard<base::Mutex> lock_guard(&ref_count_mutex_); |
| 266 while (ref_count_ > 0) ref_count_zero_.Wait(&ref_count_mutex_); | 186 while (ref_count_ > 0) ref_count_zero_.Wait(&ref_count_mutex_); |
| 267 base::Release_Store(&stop_thread_, static_cast<base::AtomicWord>(CONTINUE)); | 187 base::Release_Store(&mode_, static_cast<base::AtomicWord>(COMPILE)); |
| 268 } | 188 } |
| 269 | 189 |
| 270 if (recompilation_delay_ != 0) { | 190 if (recompilation_delay_ != 0) { |
| 271 // At this point the optimizing compiler thread's event loop has stopped. | 191 // At this point the optimizing compiler thread's event loop has stopped. |
| 272 // There is no need for a mutex when reading input_queue_length_. | 192 // There is no need for a mutex when reading input_queue_length_. |
| 273 while (input_queue_length_ > 0) CompileNext(NextInput()); | 193 while (input_queue_length_ > 0) CompileNext(NextInput()); |
| 274 InstallOptimizedFunctions(); | 194 InstallOptimizedFunctions(); |
| 275 } else { | 195 } else { |
| 276 FlushInputQueue(false); | |
| 277 FlushOutputQueue(false); | 196 FlushOutputQueue(false); |
| 278 } | 197 } |
| 279 | 198 |
| 280 if (FLAG_concurrent_osr) FlushOsrBuffer(false); | 199 if (FLAG_concurrent_osr) FlushOsrBuffer(false); |
| 281 | 200 |
| 282 if (tracing_enabled_) { | 201 if ((FLAG_trace_osr || FLAG_trace_concurrent_recompilation) && |
| 283 double percentage = time_spent_compiling_.PercentOf(time_spent_total_); | 202 FLAG_concurrent_osr) { |
| 284 if (job_based_recompilation_) percentage = 100.0; | |
| 285 PrintF(" ** Compiler thread did %.2f%% useful work\n", percentage); | |
| 286 } | |
| 287 | |
| 288 if ((FLAG_trace_osr || tracing_enabled_) && FLAG_concurrent_osr) { | |
| 289 PrintF("[COSR hit rate %d / %d]\n", osr_hits_, osr_attempts_); | 203 PrintF("[COSR hit rate %d / %d]\n", osr_hits_, osr_attempts_); |
| 290 } | 204 } |
| 291 | |
| 292 Join(); | |
| 293 } | 205 } |
| 294 | 206 |
| 295 | 207 |
| 296 void OptimizingCompilerThread::InstallOptimizedFunctions() { | 208 void OptimizingCompilerThread::InstallOptimizedFunctions() { |
| 297 DCHECK(!IsOptimizerThread()); | |
| 298 HandleScope handle_scope(isolate_); | 209 HandleScope handle_scope(isolate_); |
| 299 | 210 |
| 300 OptimizedCompileJob* job; | 211 base::LockGuard<base::Mutex> access_output_queue_(&output_queue_mutex_); |
| 301 while (output_queue_.Dequeue(&job)) { | 212 while (!output_queue_.empty()) { |
| 213 OptimizedCompileJob* job = output_queue_.front(); |
| 214 output_queue_.pop(); |
| 302 CompilationInfo* info = job->info(); | 215 CompilationInfo* info = job->info(); |
| 303 Handle<JSFunction> function(*info->closure()); | 216 Handle<JSFunction> function(*info->closure()); |
| 304 if (info->is_osr()) { | 217 if (info->is_osr()) { |
| 305 if (FLAG_trace_osr) { | 218 if (FLAG_trace_osr) { |
| 306 PrintF("[COSR - "); | 219 PrintF("[COSR - "); |
| 307 function->ShortPrint(); | 220 function->ShortPrint(); |
| 308 PrintF(" is ready for install and entry at AST id %d]\n", | 221 PrintF(" is ready for install and entry at AST id %d]\n", |
| 309 info->osr_ast_id().ToInt()); | 222 info->osr_ast_id().ToInt()); |
| 310 } | 223 } |
| 311 job->WaitForInstall(); | 224 job->WaitForInstall(); |
| 312 // Remove stack check that guards OSR entry on original code. | 225 // Remove stack check that guards OSR entry on original code. |
| 313 Handle<Code> code = info->unoptimized_code(); | 226 Handle<Code> code = info->unoptimized_code(); |
| 314 uint32_t offset = code->TranslateAstIdToPcOffset(info->osr_ast_id()); | 227 uint32_t offset = code->TranslateAstIdToPcOffset(info->osr_ast_id()); |
| 315 BackEdgeTable::RemoveStackCheck(code, offset); | 228 BackEdgeTable::RemoveStackCheck(code, offset); |
| 316 } else { | 229 } else { |
| 317 if (function->IsOptimized()) { | 230 if (function->IsOptimized()) { |
| 318 if (tracing_enabled_) { | 231 if (FLAG_trace_concurrent_recompilation) { |
| 319 PrintF(" ** Aborting compilation for "); | 232 PrintF(" ** Aborting compilation for "); |
| 320 function->ShortPrint(); | 233 function->ShortPrint(); |
| 321 PrintF(" as it has already been optimized.\n"); | 234 PrintF(" as it has already been optimized.\n"); |
| 322 } | 235 } |
| 323 DisposeOptimizedCompileJob(job, false); | 236 DisposeOptimizedCompileJob(job, false); |
| 324 } else { | 237 } else { |
| 325 Handle<Code> code = Compiler::GetConcurrentlyOptimizedCode(job); | 238 Handle<Code> code = Compiler::GetConcurrentlyOptimizedCode(job); |
| 326 function->ReplaceCode( | 239 function->ReplaceCode( |
| 327 code.is_null() ? function->shared()->code() : *code); | 240 code.is_null() ? function->shared()->code() : *code); |
| 328 } | 241 } |
| 329 } | 242 } |
| 330 } | 243 } |
| 331 } | 244 } |
| 332 | 245 |
| 333 | 246 |
| 334 void OptimizingCompilerThread::QueueForOptimization(OptimizedCompileJob* job) { | 247 void OptimizingCompilerThread::QueueForOptimization(OptimizedCompileJob* job) { |
| 335 DCHECK(IsQueueAvailable()); | 248 DCHECK(IsQueueAvailable()); |
| 336 DCHECK(!IsOptimizerThread()); | |
| 337 CompilationInfo* info = job->info(); | 249 CompilationInfo* info = job->info(); |
| 338 if (info->is_osr()) { | 250 if (info->is_osr()) { |
| 339 osr_attempts_++; | 251 osr_attempts_++; |
| 340 AddToOsrBuffer(job); | 252 AddToOsrBuffer(job); |
| 341 // Add job to the front of the input queue. | 253 // Add job to the front of the input queue. |
| 342 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); | 254 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); |
| 343 DCHECK_LT(input_queue_length_, input_queue_capacity_); | 255 DCHECK_LT(input_queue_length_, input_queue_capacity_); |
| 344 // Move shift_ back by one. | 256 // Move shift_ back by one. |
| 345 input_queue_shift_ = InputQueueIndex(input_queue_capacity_ - 1); | 257 input_queue_shift_ = InputQueueIndex(input_queue_capacity_ - 1); |
| 346 input_queue_[InputQueueIndex(0)] = job; | 258 input_queue_[InputQueueIndex(0)] = job; |
| 347 input_queue_length_++; | 259 input_queue_length_++; |
| 348 } else { | 260 } else { |
| 349 // Add job to the back of the input queue. | 261 // Add job to the back of the input queue. |
| 350 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); | 262 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); |
| 351 DCHECK_LT(input_queue_length_, input_queue_capacity_); | 263 DCHECK_LT(input_queue_length_, input_queue_capacity_); |
| 352 input_queue_[InputQueueIndex(input_queue_length_)] = job; | 264 input_queue_[InputQueueIndex(input_queue_length_)] = job; |
| 353 input_queue_length_++; | 265 input_queue_length_++; |
| 354 } | 266 } |
| 355 if (FLAG_block_concurrent_recompilation) { | 267 if (FLAG_block_concurrent_recompilation) { |
| 356 blocked_jobs_++; | 268 blocked_jobs_++; |
| 357 } else if (job_based_recompilation_) { | 269 } else { |
| 358 V8::GetCurrentPlatform()->CallOnBackgroundThread( | 270 V8::GetCurrentPlatform()->CallOnBackgroundThread( |
| 359 new CompileTask(isolate_), v8::Platform::kShortRunningTask); | 271 new CompileTask(isolate_), v8::Platform::kShortRunningTask); |
| 360 } else { | |
| 361 input_queue_semaphore_.Signal(); | |
| 362 } | 272 } |
| 363 } | 273 } |
| 364 | 274 |
| 365 | 275 |
| 366 void OptimizingCompilerThread::Unblock() { | 276 void OptimizingCompilerThread::Unblock() { |
| 367 DCHECK(!IsOptimizerThread()); | |
| 368 while (blocked_jobs_ > 0) { | 277 while (blocked_jobs_ > 0) { |
| 369 if (job_based_recompilation_) { | 278 V8::GetCurrentPlatform()->CallOnBackgroundThread( |
| 370 V8::GetCurrentPlatform()->CallOnBackgroundThread( | 279 new CompileTask(isolate_), v8::Platform::kShortRunningTask); |
| 371 new CompileTask(isolate_), v8::Platform::kShortRunningTask); | |
| 372 } else { | |
| 373 input_queue_semaphore_.Signal(); | |
| 374 } | |
| 375 blocked_jobs_--; | 280 blocked_jobs_--; |
| 376 } | 281 } |
| 377 } | 282 } |
| 378 | 283 |
| 379 | 284 |
| 380 OptimizedCompileJob* OptimizingCompilerThread::FindReadyOSRCandidate( | 285 OptimizedCompileJob* OptimizingCompilerThread::FindReadyOSRCandidate( |
| 381 Handle<JSFunction> function, BailoutId osr_ast_id) { | 286 Handle<JSFunction> function, BailoutId osr_ast_id) { |
| 382 DCHECK(!IsOptimizerThread()); | |
| 383 for (int i = 0; i < osr_buffer_capacity_; i++) { | 287 for (int i = 0; i < osr_buffer_capacity_; i++) { |
| 384 OptimizedCompileJob* current = osr_buffer_[i]; | 288 OptimizedCompileJob* current = osr_buffer_[i]; |
| 385 if (current != NULL && | 289 if (current != NULL && |
| 386 current->IsWaitingForInstall() && | 290 current->IsWaitingForInstall() && |
| 387 current->info()->HasSameOsrEntry(function, osr_ast_id)) { | 291 current->info()->HasSameOsrEntry(function, osr_ast_id)) { |
| 388 osr_hits_++; | 292 osr_hits_++; |
| 389 osr_buffer_[i] = NULL; | 293 osr_buffer_[i] = NULL; |
| 390 return current; | 294 return current; |
| 391 } | 295 } |
| 392 } | 296 } |
| 393 return NULL; | 297 return NULL; |
| 394 } | 298 } |
| 395 | 299 |
| 396 | 300 |
| 397 bool OptimizingCompilerThread::IsQueuedForOSR(Handle<JSFunction> function, | 301 bool OptimizingCompilerThread::IsQueuedForOSR(Handle<JSFunction> function, |
| 398 BailoutId osr_ast_id) { | 302 BailoutId osr_ast_id) { |
| 399 DCHECK(!IsOptimizerThread()); | |
| 400 for (int i = 0; i < osr_buffer_capacity_; i++) { | 303 for (int i = 0; i < osr_buffer_capacity_; i++) { |
| 401 OptimizedCompileJob* current = osr_buffer_[i]; | 304 OptimizedCompileJob* current = osr_buffer_[i]; |
| 402 if (current != NULL && | 305 if (current != NULL && |
| 403 current->info()->HasSameOsrEntry(function, osr_ast_id)) { | 306 current->info()->HasSameOsrEntry(function, osr_ast_id)) { |
| 404 return !current->IsWaitingForInstall(); | 307 return !current->IsWaitingForInstall(); |
| 405 } | 308 } |
| 406 } | 309 } |
| 407 return false; | 310 return false; |
| 408 } | 311 } |
| 409 | 312 |
| 410 | 313 |
| 411 bool OptimizingCompilerThread::IsQueuedForOSR(JSFunction* function) { | 314 bool OptimizingCompilerThread::IsQueuedForOSR(JSFunction* function) { |
| 412 DCHECK(!IsOptimizerThread()); | |
| 413 for (int i = 0; i < osr_buffer_capacity_; i++) { | 315 for (int i = 0; i < osr_buffer_capacity_; i++) { |
| 414 OptimizedCompileJob* current = osr_buffer_[i]; | 316 OptimizedCompileJob* current = osr_buffer_[i]; |
| 415 if (current != NULL && *current->info()->closure() == function) { | 317 if (current != NULL && *current->info()->closure() == function) { |
| 416 return !current->IsWaitingForInstall(); | 318 return !current->IsWaitingForInstall(); |
| 417 } | 319 } |
| 418 } | 320 } |
| 419 return false; | 321 return false; |
| 420 } | 322 } |
| 421 | 323 |
| 422 | 324 |
| 423 void OptimizingCompilerThread::AddToOsrBuffer(OptimizedCompileJob* job) { | 325 void OptimizingCompilerThread::AddToOsrBuffer(OptimizedCompileJob* job) { |
| 424 DCHECK(!IsOptimizerThread()); | |
| 425 // Find the next slot that is empty or has a stale job. | 326 // Find the next slot that is empty or has a stale job. |
| 426 OptimizedCompileJob* stale = NULL; | 327 OptimizedCompileJob* stale = NULL; |
| 427 while (true) { | 328 while (true) { |
| 428 stale = osr_buffer_[osr_buffer_cursor_]; | 329 stale = osr_buffer_[osr_buffer_cursor_]; |
| 429 if (stale == NULL || stale->IsWaitingForInstall()) break; | 330 if (stale == NULL || stale->IsWaitingForInstall()) break; |
| 430 osr_buffer_cursor_ = (osr_buffer_cursor_ + 1) % osr_buffer_capacity_; | 331 osr_buffer_cursor_ = (osr_buffer_cursor_ + 1) % osr_buffer_capacity_; |
| 431 } | 332 } |
| 432 | 333 |
| 433 // Add to found slot and dispose the evicted job. | 334 // Add to found slot and dispose the evicted job. |
| 434 if (stale != NULL) { | 335 if (stale != NULL) { |
| 435 DCHECK(stale->IsWaitingForInstall()); | 336 DCHECK(stale->IsWaitingForInstall()); |
| 436 CompilationInfo* info = stale->info(); | 337 CompilationInfo* info = stale->info(); |
| 437 if (FLAG_trace_osr) { | 338 if (FLAG_trace_osr) { |
| 438 PrintF("[COSR - Discarded "); | 339 PrintF("[COSR - Discarded "); |
| 439 info->closure()->PrintName(); | 340 info->closure()->PrintName(); |
| 440 PrintF(", AST id %d]\n", info->osr_ast_id().ToInt()); | 341 PrintF(", AST id %d]\n", info->osr_ast_id().ToInt()); |
| 441 } | 342 } |
| 442 DisposeOptimizedCompileJob(stale, false); | 343 DisposeOptimizedCompileJob(stale, false); |
| 443 } | 344 } |
| 444 osr_buffer_[osr_buffer_cursor_] = job; | 345 osr_buffer_[osr_buffer_cursor_] = job; |
| 445 osr_buffer_cursor_ = (osr_buffer_cursor_ + 1) % osr_buffer_capacity_; | 346 osr_buffer_cursor_ = (osr_buffer_cursor_ + 1) % osr_buffer_capacity_; |
| 446 } | 347 } |
| 447 | 348 |
| 448 | 349 |
| 449 #ifdef DEBUG | |
| 450 bool OptimizingCompilerThread::IsOptimizerThread(Isolate* isolate) { | |
| 451 return isolate->concurrent_recompilation_enabled() && | |
| 452 isolate->optimizing_compiler_thread()->IsOptimizerThread(); | |
| 453 } | |
| 454 | |
| 455 | |
| 456 bool OptimizingCompilerThread::IsOptimizerThread() { | |
| 457 base::LockGuard<base::Mutex> lock_guard(&thread_id_mutex_); | |
| 458 return ThreadId::Current().ToInteger() == thread_id_; | |
| 459 } | |
| 460 #endif | |
| 461 | |
| 462 | |
| 463 } } // namespace v8::internal | 350 } } // namespace v8::internal |
| OLD | NEW |