| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #include "v8.h" | 30 #include "v8.h" |
| 31 | 31 |
| 32 #include "full-codegen.h" | 32 #include "full-codegen.h" |
| 33 #include "hydrogen.h" | 33 #include "hydrogen.h" |
| 34 #include "isolate.h" | 34 #include "isolate.h" |
| 35 #include "v8threads.h" | 35 #include "v8threads.h" |
| 36 | 36 |
| 37 namespace v8 { | 37 namespace v8 { |
| 38 namespace internal { | 38 namespace internal { |
| 39 | 39 |
| 40 OptimizingCompilerThread::~OptimizingCompilerThread() { |
| 41 ASSERT_EQ(0, input_queue_length_); |
| 42 DeleteArray(input_queue_); |
| 43 if (FLAG_concurrent_osr) { |
| 44 #ifdef DEBUG |
| 45 for (int i = 0; i < osr_buffer_capacity_; i++) { |
| 46 CHECK_EQ(NULL, osr_buffer_[i]); |
| 47 } |
| 48 #endif |
| 49 DeleteArray(osr_buffer_); |
| 50 } |
| 51 } |
| 52 |
| 40 | 53 |
| 41 void OptimizingCompilerThread::Run() { | 54 void OptimizingCompilerThread::Run() { |
| 42 #ifdef DEBUG | 55 #ifdef DEBUG |
| 43 { LockGuard<Mutex> lock_guard(&thread_id_mutex_); | 56 { LockGuard<Mutex> lock_guard(&thread_id_mutex_); |
| 44 thread_id_ = ThreadId::Current().ToInteger(); | 57 thread_id_ = ThreadId::Current().ToInteger(); |
| 45 } | 58 } |
| 46 #endif | 59 #endif |
| 47 Isolate::SetIsolateThreadLocals(isolate_, NULL); | 60 Isolate::SetIsolateThreadLocals(isolate_, NULL); |
| 48 DisallowHeapAllocation no_allocation; | 61 DisallowHeapAllocation no_allocation; |
| 49 DisallowHandleAllocation no_handles; | 62 DisallowHandleAllocation no_handles; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 99 |
| 87 CompileNext(); | 100 CompileNext(); |
| 88 | 101 |
| 89 if (FLAG_trace_concurrent_recompilation) { | 102 if (FLAG_trace_concurrent_recompilation) { |
| 90 time_spent_compiling_ += compiling_timer.Elapsed(); | 103 time_spent_compiling_ += compiling_timer.Elapsed(); |
| 91 } | 104 } |
| 92 } | 105 } |
| 93 } | 106 } |
| 94 | 107 |
| 95 | 108 |
| 109 RecompileJob* OptimizingCompilerThread::NextInput() { |
| 110 LockGuard<Mutex> access_input_queue_(&input_queue_mutex_); |
| 111 if (input_queue_length_ == 0) return NULL; |
| 112 RecompileJob* job = input_queue_[InputQueueIndex(0)]; |
| 113 ASSERT_NE(NULL, job); |
| 114 input_queue_shift_ = InputQueueIndex(1); |
| 115 input_queue_length_--; |
| 116 return job; |
| 117 } |
| 118 |
| 119 |
| 96 void OptimizingCompilerThread::CompileNext() { | 120 void OptimizingCompilerThread::CompileNext() { |
| 97 RecompileJob* job = NULL; | 121 RecompileJob* job = NextInput(); |
| 98 bool result = input_queue_.Dequeue(&job); | 122 ASSERT_NE(NULL, job); |
| 99 USE(result); | |
| 100 ASSERT(result); | |
| 101 Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(-1)); | |
| 102 | 123 |
| 103 // The function may have already been optimized by OSR. Simply continue. | 124 // The function may have already been optimized by OSR. Simply continue. |
| 104 RecompileJob::Status status = job->OptimizeGraph(); | 125 RecompileJob::Status status = job->OptimizeGraph(); |
| 105 USE(status); // Prevent an unused-variable error in release mode. | 126 USE(status); // Prevent an unused-variable error in release mode. |
| 106 ASSERT(status != RecompileJob::FAILED); | 127 ASSERT(status != RecompileJob::FAILED); |
| 107 | 128 |
| 108 // The function may have already been optimized by OSR. Simply continue. | 129 // The function may have already been optimized by OSR. Simply continue. |
| 109 // Use a mutex to make sure that functions marked for install | 130 // Use a mutex to make sure that functions marked for install |
| 110 // are always also queued. | 131 // are always also queued. |
| 111 output_queue_.Enqueue(job); | 132 output_queue_.Enqueue(job); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 124 Handle<JSFunction> function = info->closure(); | 145 Handle<JSFunction> function = info->closure(); |
| 125 function->ReplaceCode(function->shared()->code()); | 146 function->ReplaceCode(function->shared()->code()); |
| 126 } | 147 } |
| 127 } | 148 } |
| 128 delete info; | 149 delete info; |
| 129 } | 150 } |
| 130 | 151 |
| 131 | 152 |
| 132 void OptimizingCompilerThread::FlushInputQueue(bool restore_function_code) { | 153 void OptimizingCompilerThread::FlushInputQueue(bool restore_function_code) { |
| 133 RecompileJob* job; | 154 RecompileJob* job; |
| 134 while (input_queue_.Dequeue(&job)) { | 155 while ((job = NextInput())) { |
| 135 // This should not block, since we have one signal on the input queue | 156 // This should not block, since we have one signal on the input queue |
| 136 // semaphore corresponding to each element in the input queue. | 157 // semaphore corresponding to each element in the input queue. |
| 137 input_queue_semaphore_.Wait(); | 158 input_queue_semaphore_.Wait(); |
| 138 // OSR jobs are dealt with separately. | 159 // OSR jobs are dealt with separately. |
| 139 if (!job->info()->is_osr()) { | 160 if (!job->info()->is_osr()) { |
| 140 DisposeRecompileJob(job, restore_function_code); | 161 DisposeRecompileJob(job, restore_function_code); |
| 141 } | 162 } |
| 142 } | 163 } |
| 143 Release_Store(&queue_length_, static_cast<AtomicWord>(0)); | |
| 144 } | 164 } |
| 145 | 165 |
| 146 | 166 |
| 147 void OptimizingCompilerThread::FlushOutputQueue(bool restore_function_code) { | 167 void OptimizingCompilerThread::FlushOutputQueue(bool restore_function_code) { |
| 148 RecompileJob* job; | 168 RecompileJob* job; |
| 149 while (output_queue_.Dequeue(&job)) { | 169 while (output_queue_.Dequeue(&job)) { |
| 150 // OSR jobs are dealt with separately. | 170 // OSR jobs are dealt with separately. |
| 151 if (!job->info()->is_osr()) { | 171 if (!job->info()->is_osr()) { |
| 152 DisposeRecompileJob(job, restore_function_code); | 172 DisposeRecompileJob(job, restore_function_code); |
| 153 } | 173 } |
| 154 } | 174 } |
| 155 } | 175 } |
| 156 | 176 |
| 157 | 177 |
| 158 void OptimizingCompilerThread::FlushOsrBuffer(bool restore_function_code) { | 178 void OptimizingCompilerThread::FlushOsrBuffer(bool restore_function_code) { |
| 159 RecompileJob* job; | 179 for (int i = 0; i < osr_buffer_capacity_; i++) { |
| 160 for (int i = 0; i < osr_buffer_size_; i++) { | 180 if (osr_buffer_[i] != NULL) { |
| 161 job = osr_buffer_[i]; | 181 DisposeRecompileJob(osr_buffer_[i], restore_function_code); |
| 162 if (job != NULL) DisposeRecompileJob(job, restore_function_code); | 182 osr_buffer_[i] = NULL; |
| 183 } |
| 163 } | 184 } |
| 164 osr_cursor_ = 0; | |
| 165 } | 185 } |
| 166 | 186 |
| 167 | 187 |
| 168 void OptimizingCompilerThread::Flush() { | 188 void OptimizingCompilerThread::Flush() { |
| 169 ASSERT(!IsOptimizerThread()); | 189 ASSERT(!IsOptimizerThread()); |
| 170 Release_Store(&stop_thread_, static_cast<AtomicWord>(FLUSH)); | 190 Release_Store(&stop_thread_, static_cast<AtomicWord>(FLUSH)); |
| 191 if (FLAG_block_concurrent_recompilation) Unblock(); |
| 171 input_queue_semaphore_.Signal(); | 192 input_queue_semaphore_.Signal(); |
| 172 stop_semaphore_.Wait(); | 193 stop_semaphore_.Wait(); |
| 173 FlushOutputQueue(true); | 194 FlushOutputQueue(true); |
| 174 if (FLAG_concurrent_osr) FlushOsrBuffer(true); | 195 if (FLAG_concurrent_osr) FlushOsrBuffer(true); |
| 175 if (FLAG_trace_concurrent_recompilation) { | 196 if (FLAG_trace_concurrent_recompilation) { |
| 176 PrintF(" ** Flushed concurrent recompilation queues.\n"); | 197 PrintF(" ** Flushed concurrent recompilation queues.\n"); |
| 177 } | 198 } |
| 178 } | 199 } |
| 179 | 200 |
| 180 | 201 |
| 181 void OptimizingCompilerThread::Stop() { | 202 void OptimizingCompilerThread::Stop() { |
| 182 ASSERT(!IsOptimizerThread()); | 203 ASSERT(!IsOptimizerThread()); |
| 183 Release_Store(&stop_thread_, static_cast<AtomicWord>(STOP)); | 204 Release_Store(&stop_thread_, static_cast<AtomicWord>(STOP)); |
| 205 if (FLAG_block_concurrent_recompilation) Unblock(); |
| 184 input_queue_semaphore_.Signal(); | 206 input_queue_semaphore_.Signal(); |
| 185 stop_semaphore_.Wait(); | 207 stop_semaphore_.Wait(); |
| 186 | 208 |
| 187 if (FLAG_concurrent_recompilation_delay != 0) { | 209 if (FLAG_concurrent_recompilation_delay != 0) { |
| 188 // Barrier when loading queue length is not necessary since the write | 210 // At this point the optimizing compiler thread's event loop has stopped. |
| 189 // happens in CompileNext on the same thread. | 211 // There is no need for a mutex when reading input_queue_length_. |
| 190 // This is used only for testing. | 212 while (input_queue_length_ > 0) CompileNext(); |
| 191 while (NoBarrier_Load(&queue_length_) > 0) CompileNext(); | |
| 192 InstallOptimizedFunctions(); | 213 InstallOptimizedFunctions(); |
| 193 } else { | 214 } else { |
| 194 FlushInputQueue(false); | 215 FlushInputQueue(false); |
| 195 FlushOutputQueue(false); | 216 FlushOutputQueue(false); |
| 196 } | 217 } |
| 197 | 218 |
| 198 if (FLAG_concurrent_osr) FlushOsrBuffer(false); | 219 if (FLAG_concurrent_osr) FlushOsrBuffer(false); |
| 199 | 220 |
| 200 if (FLAG_trace_concurrent_recompilation) { | 221 if (FLAG_trace_concurrent_recompilation) { |
| 201 double percentage = time_spent_compiling_.PercentOf(time_spent_total_); | 222 double percentage = time_spent_compiling_.PercentOf(time_spent_total_); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 230 } else { | 251 } else { |
| 231 Compiler::InstallOptimizedCode(job); | 252 Compiler::InstallOptimizedCode(job); |
| 232 } | 253 } |
| 233 } | 254 } |
| 234 } | 255 } |
| 235 | 256 |
| 236 | 257 |
| 237 void OptimizingCompilerThread::QueueForOptimization(RecompileJob* job) { | 258 void OptimizingCompilerThread::QueueForOptimization(RecompileJob* job) { |
| 238 ASSERT(IsQueueAvailable()); | 259 ASSERT(IsQueueAvailable()); |
| 239 ASSERT(!IsOptimizerThread()); | 260 ASSERT(!IsOptimizerThread()); |
| 240 Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(1)); | |
| 241 CompilationInfo* info = job->info(); | 261 CompilationInfo* info = job->info(); |
| 242 if (info->is_osr()) { | 262 if (info->is_osr()) { |
| 243 if (FLAG_trace_concurrent_recompilation) { | 263 if (FLAG_trace_concurrent_recompilation) { |
| 244 PrintF(" ** Queueing "); | 264 PrintF(" ** Queueing "); |
| 245 info->closure()->PrintName(); | 265 info->closure()->PrintName(); |
| 246 PrintF(" for concurrent on-stack replacement.\n"); | 266 PrintF(" for concurrent on-stack replacement.\n"); |
| 247 } | 267 } |
| 248 AddToOsrBuffer(job); | |
| 249 osr_attempts_++; | 268 osr_attempts_++; |
| 250 BackEdgeTable::AddStackCheck(info); | 269 BackEdgeTable::AddStackCheck(info); |
| 270 AddToOsrBuffer(job); |
| 271 // Add job to the front of the input queue. |
| 272 LockGuard<Mutex> access_input_queue(&input_queue_mutex_); |
| 273 ASSERT_LT(input_queue_length_, input_queue_capacity_); |
| 274 // Move shift_ back by one. |
| 275 input_queue_shift_ = InputQueueIndex(input_queue_capacity_ - 1); |
| 276 input_queue_[InputQueueIndex(0)] = job; |
| 277 input_queue_length_++; |
| 251 } else { | 278 } else { |
| 252 info->closure()->MarkInRecompileQueue(); | 279 info->closure()->MarkInRecompileQueue(); |
| 280 // Add job to the back of the input queue. |
| 281 LockGuard<Mutex> access_input_queue(&input_queue_mutex_); |
| 282 ASSERT_LT(input_queue_length_, input_queue_capacity_); |
| 283 input_queue_[InputQueueIndex(input_queue_length_)] = job; |
| 284 input_queue_length_++; |
| 253 } | 285 } |
| 254 input_queue_.Enqueue(job); | 286 if (FLAG_block_concurrent_recompilation) { |
| 255 input_queue_semaphore_.Signal(); | 287 blocked_jobs_++; |
| 288 } else { |
| 289 input_queue_semaphore_.Signal(); |
| 290 } |
| 291 } |
| 292 |
| 293 |
| 294 void OptimizingCompilerThread::Unblock() { |
| 295 ASSERT(!IsOptimizerThread()); |
| 296 while (blocked_jobs_ > 0) { |
| 297 input_queue_semaphore_.Signal(); |
| 298 blocked_jobs_--; |
| 299 } |
| 256 } | 300 } |
| 257 | 301 |
| 258 | 302 |
| 259 RecompileJob* OptimizingCompilerThread::FindReadyOSRCandidate( | 303 RecompileJob* OptimizingCompilerThread::FindReadyOSRCandidate( |
| 260 Handle<JSFunction> function, uint32_t osr_pc_offset) { | 304 Handle<JSFunction> function, uint32_t osr_pc_offset) { |
| 261 ASSERT(!IsOptimizerThread()); | 305 ASSERT(!IsOptimizerThread()); |
| 262 RecompileJob* result = NULL; | 306 for (int i = 0; i < osr_buffer_capacity_; i++) { |
| 263 for (int i = 0; i < osr_buffer_size_; i++) { | 307 RecompileJob* current = osr_buffer_[i]; |
| 264 result = osr_buffer_[i]; | 308 if (current != NULL && |
| 265 if (result == NULL) continue; | 309 current->IsWaitingForInstall() && |
| 266 if (result->IsWaitingForInstall() && | 310 current->info()->HasSameOsrEntry(function, osr_pc_offset)) { |
| 267 result->info()->HasSameOsrEntry(function, osr_pc_offset)) { | |
| 268 osr_hits_++; | 311 osr_hits_++; |
| 269 osr_buffer_[i] = NULL; | 312 osr_buffer_[i] = NULL; |
| 270 return result; | 313 return current; |
| 271 } | 314 } |
| 272 } | 315 } |
| 273 return NULL; | 316 return NULL; |
| 274 } | 317 } |
| 275 | 318 |
| 276 | 319 |
| 277 bool OptimizingCompilerThread::IsQueuedForOSR(Handle<JSFunction> function, | 320 bool OptimizingCompilerThread::IsQueuedForOSR(Handle<JSFunction> function, |
| 278 uint32_t osr_pc_offset) { | 321 uint32_t osr_pc_offset) { |
| 279 ASSERT(!IsOptimizerThread()); | 322 ASSERT(!IsOptimizerThread()); |
| 280 for (int i = 0; i < osr_buffer_size_; i++) { | 323 for (int i = 0; i < osr_buffer_capacity_; i++) { |
| 281 if (osr_buffer_[i] != NULL && | 324 RecompileJob* current = osr_buffer_[i]; |
| 282 osr_buffer_[i]->info()->HasSameOsrEntry(function, osr_pc_offset)) { | 325 if (current != NULL && |
| 283 return !osr_buffer_[i]->IsWaitingForInstall(); | 326 current->info()->HasSameOsrEntry(function, osr_pc_offset)) { |
| 327 return !current->IsWaitingForInstall(); |
| 284 } | 328 } |
| 285 } | 329 } |
| 286 return false; | 330 return false; |
| 287 } | 331 } |
| 288 | 332 |
| 289 | 333 |
| 290 bool OptimizingCompilerThread::IsQueuedForOSR(JSFunction* function) { | 334 bool OptimizingCompilerThread::IsQueuedForOSR(JSFunction* function) { |
| 291 ASSERT(!IsOptimizerThread()); | 335 ASSERT(!IsOptimizerThread()); |
| 292 for (int i = 0; i < osr_buffer_size_; i++) { | 336 for (int i = 0; i < osr_buffer_capacity_; i++) { |
| 293 if (osr_buffer_[i] != NULL && | 337 RecompileJob* current = osr_buffer_[i]; |
| 294 *osr_buffer_[i]->info()->closure() == function) { | 338 if (current != NULL && *current->info()->closure() == function) { |
| 295 return !osr_buffer_[i]->IsWaitingForInstall(); | 339 return !current->IsWaitingForInstall(); |
| 296 } | 340 } |
| 297 } | 341 } |
| 298 return false; | 342 return false; |
| 299 } | 343 } |
| 300 | 344 |
| 301 | 345 |
| 302 void OptimizingCompilerThread::AddToOsrBuffer(RecompileJob* job) { | 346 void OptimizingCompilerThread::AddToOsrBuffer(RecompileJob* job) { |
| 303 ASSERT(!IsOptimizerThread()); | 347 ASSERT(!IsOptimizerThread()); |
| 304 // Store into next empty slot or replace next stale OSR job that's waiting | 348 // Find the next slot that is empty or has a stale job. |
| 305 // in vain. Dispose in the latter case. | |
| 306 RecompileJob* stale; | |
| 307 while (true) { | 349 while (true) { |
| 308 stale = osr_buffer_[osr_cursor_]; | 350 RecompileJob* stale = osr_buffer_[osr_buffer_cursor_]; |
| 309 if (stale == NULL) break; | 351 if (stale == NULL || stale->IsWaitingForInstall()) break; |
| 310 if (stale->IsWaitingForInstall()) { | 352 osr_buffer_cursor_ = (osr_buffer_cursor_ + 1) % osr_buffer_capacity_; |
| 311 CompilationInfo* info = stale->info(); | |
| 312 if (FLAG_trace_osr) { | |
| 313 PrintF("[COSR - Discarded "); | |
| 314 info->closure()->PrintName(); | |
| 315 PrintF(", AST id %d]\n", info->osr_ast_id().ToInt()); | |
| 316 } | |
| 317 DisposeRecompileJob(stale, false); | |
| 318 break; | |
| 319 } | |
| 320 AdvanceOsrCursor(); | |
| 321 } | 353 } |
| 322 | 354 |
| 323 osr_buffer_[osr_cursor_] = job; | 355 // Add to found slot and dispose the evicted job. |
| 324 AdvanceOsrCursor(); | 356 RecompileJob* evicted = osr_buffer_[osr_buffer_cursor_]; |
| 357 if (evicted != NULL) { |
| 358 ASSERT(evicted->IsWaitingForInstall()); |
| 359 CompilationInfo* info = evicted->info(); |
| 360 if (FLAG_trace_osr) { |
| 361 PrintF("[COSR - Discarded "); |
| 362 info->closure()->PrintName(); |
| 363 PrintF(", AST id %d]\n", info->osr_ast_id().ToInt()); |
| 364 } |
| 365 DisposeRecompileJob(evicted, false); |
| 366 } |
| 367 osr_buffer_[osr_buffer_cursor_] = job; |
| 368 osr_buffer_cursor_ = (osr_buffer_cursor_ + 1) % osr_buffer_capacity_; |
| 325 } | 369 } |
| 326 | 370 |
| 327 | 371 |
| 328 #ifdef DEBUG | 372 #ifdef DEBUG |
| 329 bool OptimizingCompilerThread::IsOptimizerThread() { | 373 bool OptimizingCompilerThread::IsOptimizerThread() { |
| 330 if (!FLAG_concurrent_recompilation) return false; | 374 if (!FLAG_concurrent_recompilation) return false; |
| 331 LockGuard<Mutex> lock_guard(&thread_id_mutex_); | 375 LockGuard<Mutex> lock_guard(&thread_id_mutex_); |
| 332 return ThreadId::Current().ToInteger() == thread_id_; | 376 return ThreadId::Current().ToInteger() == thread_id_; |
| 333 } | 377 } |
| 334 #endif | 378 #endif |
| 335 | 379 |
| 336 | 380 |
| 337 } } // namespace v8::internal | 381 } } // namespace v8::internal |
| OLD | NEW |