2012-07-19 18:58:23 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "optimizing-compiler-thread.h"
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "hydrogen.h"
|
|
|
|
#include "isolate.h"
|
|
|
|
#include "v8threads.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
|
|
void OptimizingCompilerThread::Run() {
|
|
|
|
#ifdef DEBUG
|
2013-08-29 09:58:30 +00:00
|
|
|
{ LockGuard<Mutex> lock_guard(&thread_id_mutex_);
|
2013-06-28 11:24:27 +00:00
|
|
|
thread_id_ = ThreadId::Current().ToInteger();
|
|
|
|
}
|
2012-07-19 18:58:23 +00:00
|
|
|
#endif
|
|
|
|
Isolate::SetIsolateThreadLocals(isolate_, NULL);
|
2013-06-03 15:32:22 +00:00
|
|
|
DisallowHeapAllocation no_allocation;
|
|
|
|
DisallowHandleAllocation no_handles;
|
|
|
|
DisallowHandleDereference no_deref;
|
2012-07-19 18:58:23 +00:00
|
|
|
|
2013-08-29 09:15:13 +00:00
|
|
|
ElapsedTimer total_timer;
|
|
|
|
if (FLAG_trace_concurrent_recompilation) total_timer.Start();
|
2012-07-20 08:56:20 +00:00
|
|
|
|
2012-07-19 18:58:23 +00:00
|
|
|
while (true) {
|
|
|
|
input_queue_semaphore_->Wait();
|
2012-11-22 13:04:11 +00:00
|
|
|
Logger::TimerEventScope timer(
|
2013-08-22 16:14:37 +00:00
|
|
|
isolate_, Logger::TimerEventScope::v8_recompile_concurrent);
|
2013-03-05 16:22:08 +00:00
|
|
|
|
2013-08-22 16:14:37 +00:00
|
|
|
if (FLAG_concurrent_recompilation_delay != 0) {
|
|
|
|
OS::Sleep(FLAG_concurrent_recompilation_delay);
|
2013-03-05 16:22:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 09:33:09 +00:00
|
|
|
switch (static_cast<StopFlag>(Acquire_Load(&stop_thread_))) {
|
|
|
|
case CONTINUE:
|
|
|
|
break;
|
|
|
|
case STOP:
|
2013-08-22 16:14:37 +00:00
|
|
|
if (FLAG_trace_concurrent_recompilation) {
|
2013-08-29 09:15:13 +00:00
|
|
|
time_spent_total_ = total_timer.Elapsed();
|
2013-08-07 09:33:09 +00:00
|
|
|
}
|
|
|
|
stop_semaphore_->Signal();
|
|
|
|
return;
|
|
|
|
case FLUSH:
|
|
|
|
// The main thread is blocked, waiting for the stop semaphore.
|
|
|
|
{ AllowHandleDereference allow_handle_dereference;
|
|
|
|
FlushInputQueue(true);
|
|
|
|
}
|
|
|
|
Release_Store(&queue_length_, static_cast<AtomicWord>(0));
|
|
|
|
Release_Store(&stop_thread_, static_cast<AtomicWord>(CONTINUE));
|
|
|
|
stop_semaphore_->Signal();
|
|
|
|
// Return to start of consumer loop.
|
|
|
|
continue;
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 09:15:13 +00:00
|
|
|
ElapsedTimer compiling_timer;
|
|
|
|
if (FLAG_trace_concurrent_recompilation) compiling_timer.Start();
|
2012-07-20 08:56:20 +00:00
|
|
|
|
2013-03-05 16:22:08 +00:00
|
|
|
CompileNext();
|
2012-07-19 18:58:23 +00:00
|
|
|
|
2013-08-22 16:14:37 +00:00
|
|
|
if (FLAG_trace_concurrent_recompilation) {
|
2013-08-29 09:15:13 +00:00
|
|
|
time_spent_compiling_ += compiling_timer.Elapsed();
|
2012-07-20 08:56:20 +00:00
|
|
|
}
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-05 16:22:08 +00:00
|
|
|
void OptimizingCompilerThread::CompileNext() {
|
|
|
|
OptimizingCompiler* optimizing_compiler = NULL;
|
2013-08-07 09:33:09 +00:00
|
|
|
bool result = input_queue_.Dequeue(&optimizing_compiler);
|
|
|
|
USE(result);
|
|
|
|
ASSERT(result);
|
2013-03-05 16:22:08 +00:00
|
|
|
Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(-1));
|
|
|
|
|
2013-03-14 16:35:32 +00:00
|
|
|
// The function may have already been optimized by OSR. Simply continue.
|
2013-03-05 16:22:08 +00:00
|
|
|
OptimizingCompiler::Status status = optimizing_compiler->OptimizeGraph();
|
2013-03-14 16:35:32 +00:00
|
|
|
USE(status); // Prevent an unused-variable error in release mode.
|
2013-03-05 16:22:08 +00:00
|
|
|
ASSERT(status != OptimizingCompiler::FAILED);
|
2013-03-12 18:03:18 +00:00
|
|
|
|
2013-03-14 16:35:32 +00:00
|
|
|
// The function may have already been optimized by OSR. Simply continue.
|
2013-06-21 08:38:12 +00:00
|
|
|
// Use a mutex to make sure that functions marked for install
|
|
|
|
// are always also queued.
|
2013-08-29 09:58:30 +00:00
|
|
|
LockGuard<Mutex> mark_and_queue(&install_mutex_);
|
2013-04-26 07:35:07 +00:00
|
|
|
{ Heap::RelocationLock relocation_lock(isolate_->heap());
|
2013-06-03 15:32:22 +00:00
|
|
|
AllowHandleDereference ahd;
|
2013-04-26 07:35:07 +00:00
|
|
|
optimizing_compiler->info()->closure()->MarkForInstallingRecompiledCode();
|
|
|
|
}
|
2013-03-14 16:35:32 +00:00
|
|
|
output_queue_.Enqueue(optimizing_compiler);
|
2013-03-05 16:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-07 09:33:09 +00:00
|
|
|
void OptimizingCompilerThread::FlushInputQueue(bool restore_function_code) {
|
|
|
|
OptimizingCompiler* optimizing_compiler;
|
|
|
|
// The optimizing compiler is allocated in the CompilationInfo's zone.
|
|
|
|
while (input_queue_.Dequeue(&optimizing_compiler)) {
|
|
|
|
// 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();
|
|
|
|
CompilationInfo* info = optimizing_compiler->info();
|
|
|
|
if (restore_function_code) {
|
|
|
|
Handle<JSFunction> function = info->closure();
|
|
|
|
function->ReplaceCode(function->shared()->code());
|
|
|
|
}
|
|
|
|
delete info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OptimizingCompilerThread::FlushOutputQueue(bool restore_function_code) {
|
|
|
|
OptimizingCompiler* optimizing_compiler;
|
|
|
|
// The optimizing compiler is allocated in the CompilationInfo's zone.
|
|
|
|
while (output_queue_.Dequeue(&optimizing_compiler)) {
|
|
|
|
CompilationInfo* info = optimizing_compiler->info();
|
|
|
|
if (restore_function_code) {
|
|
|
|
Handle<JSFunction> function = info->closure();
|
|
|
|
function->ReplaceCode(function->shared()->code());
|
|
|
|
}
|
|
|
|
delete info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OptimizingCompilerThread::Flush() {
|
|
|
|
ASSERT(!IsOptimizerThread());
|
|
|
|
Release_Store(&stop_thread_, static_cast<AtomicWord>(FLUSH));
|
|
|
|
input_queue_semaphore_->Signal();
|
|
|
|
stop_semaphore_->Wait();
|
|
|
|
FlushOutputQueue(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-19 18:58:23 +00:00
|
|
|
void OptimizingCompilerThread::Stop() {
|
2013-03-12 18:03:18 +00:00
|
|
|
ASSERT(!IsOptimizerThread());
|
2013-08-07 09:33:09 +00:00
|
|
|
Release_Store(&stop_thread_, static_cast<AtomicWord>(STOP));
|
2012-07-19 18:58:23 +00:00
|
|
|
input_queue_semaphore_->Signal();
|
|
|
|
stop_semaphore_->Wait();
|
2012-07-20 08:56:20 +00:00
|
|
|
|
2013-08-22 16:14:37 +00:00
|
|
|
if (FLAG_concurrent_recompilation_delay != 0) {
|
2013-03-05 16:22:08 +00:00
|
|
|
// Barrier when loading queue length is not necessary since the write
|
|
|
|
// happens in CompileNext on the same thread.
|
2013-08-07 09:33:09 +00:00
|
|
|
// This is used only for testing.
|
2013-06-21 08:37:05 +00:00
|
|
|
while (NoBarrier_Load(&queue_length_) > 0) CompileNext();
|
|
|
|
InstallOptimizedFunctions();
|
|
|
|
} else {
|
2013-08-07 09:33:09 +00:00
|
|
|
FlushInputQueue(false);
|
|
|
|
FlushOutputQueue(false);
|
2013-03-05 16:22:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 16:14:37 +00:00
|
|
|
if (FLAG_trace_concurrent_recompilation) {
|
2013-08-29 09:15:13 +00:00
|
|
|
double percentage = time_spent_compiling_.PercentOf(time_spent_total_);
|
2012-07-20 08:56:20 +00:00
|
|
|
PrintF(" ** Compiler thread did %.2f%% useful work\n", percentage);
|
|
|
|
}
|
2013-07-02 09:04:45 +00:00
|
|
|
|
|
|
|
Join();
|
2012-07-19 18:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OptimizingCompilerThread::InstallOptimizedFunctions() {
|
2013-03-12 18:03:18 +00:00
|
|
|
ASSERT(!IsOptimizerThread());
|
2012-07-19 18:58:23 +00:00
|
|
|
HandleScope handle_scope(isolate_);
|
2013-06-20 06:23:34 +00:00
|
|
|
OptimizingCompiler* compiler;
|
2013-06-21 08:38:12 +00:00
|
|
|
while (true) {
|
|
|
|
{ // Memory barrier to ensure marked functions are queued.
|
2013-08-29 09:58:30 +00:00
|
|
|
LockGuard<Mutex> marked_and_queued(&install_mutex_);
|
2013-06-21 08:38:12 +00:00
|
|
|
if (!output_queue_.Dequeue(&compiler)) return;
|
|
|
|
}
|
2012-07-19 18:58:23 +00:00
|
|
|
Compiler::InstallOptimizedCode(compiler);
|
|
|
|
}
|
2012-11-16 10:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-19 18:58:23 +00:00
|
|
|
void OptimizingCompilerThread::QueueForOptimization(
|
|
|
|
OptimizingCompiler* optimizing_compiler) {
|
2013-01-25 15:54:19 +00:00
|
|
|
ASSERT(IsQueueAvailable());
|
2013-03-12 18:03:18 +00:00
|
|
|
ASSERT(!IsOptimizerThread());
|
2013-01-25 15:54:19 +00:00
|
|
|
Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(1));
|
2013-03-14 16:35:32 +00:00
|
|
|
optimizing_compiler->info()->closure()->MarkInRecompileQueue();
|
2012-07-19 18:58:23 +00:00
|
|
|
input_queue_.Enqueue(optimizing_compiler);
|
|
|
|
input_queue_semaphore_->Signal();
|
|
|
|
}
|
|
|
|
|
2013-03-12 18:03:18 +00:00
|
|
|
|
2012-07-19 18:58:23 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool OptimizingCompilerThread::IsOptimizerThread() {
|
2013-08-22 16:14:37 +00:00
|
|
|
if (!FLAG_concurrent_recompilation) return false;
|
2013-08-29 09:58:30 +00:00
|
|
|
LockGuard<Mutex> lock_guard(&thread_id_mutex_);
|
2012-07-19 18:58:23 +00:00
|
|
|
return ThreadId::Current().ToInteger() == thread_id_;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|