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

Unified Diff: src/optimizing-compiler-thread.cc

Issue 25505002: Improve queuing for concurrent OSR. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: src/optimizing-compiler-thread.cc
diff --git a/src/optimizing-compiler-thread.cc b/src/optimizing-compiler-thread.cc
index 202e6e5c57e0b2ac2624f369c704344300219e44..acd378e968ca8ef90f73b2b72b7c8d336babb4c4 100644
--- a/src/optimizing-compiler-thread.cc
+++ b/src/optimizing-compiler-thread.cc
@@ -95,10 +95,9 @@ void OptimizingCompilerThread::Run() {
void OptimizingCompilerThread::CompileNext() {
RecompileJob* job = NULL;
- bool result = input_queue_.Dequeue(&job);
- USE(result);
- ASSERT(result);
- Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(-1));
+ { LockGuard<Mutex> access_input(&input_mutex_);
+ job = input_queue_.Dequeue();
+ }
// The function may have already been optimized by OSR. Simply continue.
RecompileJob::Status status = job->OptimizeGraph();
@@ -131,7 +130,11 @@ static void DisposeRecompileJob(RecompileJob* job,
void OptimizingCompilerThread::FlushInputQueue(bool restore_function_code) {
RecompileJob* job;
- while (input_queue_.Dequeue(&job)) {
+ while (true) {
+ { LockGuard<Mutex> access_input(&input_mutex_);
+ if (input_queue_.length() == 0) break;
+ job = input_queue_.Dequeue();
+ }
// This should not block, since we have one signal on the input queue
// semaphore corresponding to each element in the input queue.
input_queue_semaphore_.Wait();
@@ -140,7 +143,6 @@ void OptimizingCompilerThread::FlushInputQueue(bool restore_function_code) {
DisposeRecompileJob(job, restore_function_code);
}
}
- Release_Store(&queue_length_, static_cast<AtomicWord>(0));
}
@@ -157,11 +159,10 @@ void OptimizingCompilerThread::FlushOutputQueue(bool restore_function_code) {
void OptimizingCompilerThread::FlushOsrBuffer(bool restore_function_code) {
RecompileJob* job;
- for (int i = 0; i < osr_buffer_size_; i++) {
- job = osr_buffer_[i];
+ for (int i = 0; i < osr_buffer_.capacity(); i++) {
+ job = osr_buffer_.Remove(i);
if (job != NULL) DisposeRecompileJob(job, restore_function_code);
}
- osr_cursor_ = 0;
}
@@ -185,10 +186,9 @@ void OptimizingCompilerThread::Stop() {
stop_semaphore_.Wait();
if (FLAG_concurrent_recompilation_delay != 0) {
- // Barrier when loading queue length is not necessary since the write
- // happens in CompileNext on the same thread.
- // This is used only for testing.
- while (NoBarrier_Load(&queue_length_) > 0) CompileNext();
+ // At this point the optimizing compiler threa's event loop has stopped.
+ // There is no need for a mutex when reading input_queue_.
+ while (input_queue_.length() > 0) CompileNext();
InstallOptimizedFunctions();
} else {
FlushInputQueue(false);
@@ -237,7 +237,6 @@ void OptimizingCompilerThread::InstallOptimizedFunctions() {
void OptimizingCompilerThread::QueueForOptimization(RecompileJob* job) {
ASSERT(IsQueueAvailable());
ASSERT(!IsOptimizerThread());
- Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(1));
CompilationInfo* info = job->info();
if (info->is_osr()) {
if (FLAG_trace_concurrent_recompilation) {
@@ -245,13 +244,18 @@ void OptimizingCompilerThread::QueueForOptimization(RecompileJob* job) {
info->closure()->PrintName();
PrintF(" for concurrent on-stack replacement.\n");
}
- AddToOsrBuffer(job);
osr_attempts_++;
BackEdgeTable::AddStackCheck(info);
+ AddToOsrBuffer(job);
+ { LockGuard<Mutex> access_input(&input_mutex_);
+ input_queue_.Prepend(job);
+ }
} else {
info->closure()->MarkInRecompileQueue();
+ { LockGuard<Mutex> access_input(&input_mutex_);
+ input_queue_.Enqueue(job);
+ }
}
- input_queue_.Enqueue(job);
input_queue_semaphore_.Signal();
}
@@ -259,14 +263,13 @@ void OptimizingCompilerThread::QueueForOptimization(RecompileJob* job) {
RecompileJob* OptimizingCompilerThread::FindReadyOSRCandidate(
Handle<JSFunction> function, uint32_t osr_pc_offset) {
ASSERT(!IsOptimizerThread());
- RecompileJob* result = NULL;
- for (int i = 0; i < osr_buffer_size_; i++) {
- result = osr_buffer_[i];
+ for (int i = 0; i < osr_buffer_.capacity(); i++) {
+ RecompileJob* result = osr_buffer_.Get(i);
if (result == NULL) continue;
if (result->IsWaitingForInstall() &&
result->info()->HasSameOsrEntry(function, osr_pc_offset)) {
osr_hits_++;
- osr_buffer_[i] = NULL;
+ osr_buffer_.Remove(i);
return result;
}
}
@@ -277,10 +280,11 @@ RecompileJob* OptimizingCompilerThread::FindReadyOSRCandidate(
bool OptimizingCompilerThread::IsQueuedForOSR(Handle<JSFunction> function,
uint32_t osr_pc_offset) {
ASSERT(!IsOptimizerThread());
- for (int i = 0; i < osr_buffer_size_; i++) {
- if (osr_buffer_[i] != NULL &&
- osr_buffer_[i]->info()->HasSameOsrEntry(function, osr_pc_offset)) {
- return !osr_buffer_[i]->IsWaitingForInstall();
+ for (int i = 0; i < osr_buffer_.capacity(); i++) {
+ RecompileJob* result = osr_buffer_.Get(i);
+ if (result != NULL &&
+ result->info()->HasSameOsrEntry(function, osr_pc_offset)) {
+ return !result->IsWaitingForInstall();
}
}
return false;
@@ -289,10 +293,10 @@ bool OptimizingCompilerThread::IsQueuedForOSR(Handle<JSFunction> function,
bool OptimizingCompilerThread::IsQueuedForOSR(JSFunction* function) {
ASSERT(!IsOptimizerThread());
- for (int i = 0; i < osr_buffer_size_; i++) {
- if (osr_buffer_[i] != NULL &&
- *osr_buffer_[i]->info()->closure() == function) {
- return !osr_buffer_[i]->IsWaitingForInstall();
+ for (int i = 0; i < osr_buffer_.capacity(); i++) {
+ RecompileJob* result = osr_buffer_.Get(i);
+ if (result != NULL && *result->info()->closure() == function) {
+ return !result->IsWaitingForInstall();
}
}
return false;
@@ -301,27 +305,25 @@ bool OptimizingCompilerThread::IsQueuedForOSR(JSFunction* function) {
void OptimizingCompilerThread::AddToOsrBuffer(RecompileJob* job) {
ASSERT(!IsOptimizerThread());
- // Store into next empty slot or replace next stale OSR job that's waiting
- // in vain. Dispose in the latter case.
- RecompileJob* stale;
+ // Find the next slot that is empty or has a stale job.
while (true) {
- stale = osr_buffer_[osr_cursor_];
- if (stale == NULL) break;
- if (stale->IsWaitingForInstall()) {
- CompilationInfo* info = stale->info();
- if (FLAG_trace_osr) {
- PrintF("[COSR - Discarded ");
- info->closure()->PrintName();
- PrintF(", AST id %d]\n", info->osr_ast_id().ToInt());
- }
- DisposeRecompileJob(stale, false);
- break;
- }
- AdvanceOsrCursor();
+ RecompileJob* stale = osr_buffer_.Current();
+ if (stale == NULL || stale->IsWaitingForInstall()) break;
+ osr_buffer_.AdvanceCursor();
}
- osr_buffer_[osr_cursor_] = job;
- AdvanceOsrCursor();
+ // Add to found slot and dispose the evicted job.
+ RecompileJob* evicted = osr_buffer_.Add(job);
+ if (evicted != NULL) {
+ ASSERT(evicted->IsWaitingForInstall());
+ CompilationInfo* info = evicted->info();
+ if (FLAG_trace_osr) {
+ PrintF("[COSR - Discarded ");
+ info->closure()->PrintName();
+ PrintF(", AST id %d]\n", info->osr_ast_id().ToInt());
+ }
+ DisposeRecompileJob(evicted, false);
+ }
}

Powered by Google App Engine
This is Rietveld 408576698