blob: bc26442ea2391012e8fd681ca9fafcedc3663c50 [file] [log] [blame]
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "optimizing-compiler-thread.h"
29
30#include "v8.h"
31
32#include "hydrogen.h"
33#include "isolate.h"
34#include "v8threads.h"
35
36namespace v8 {
37namespace internal {
38
39
40void OptimizingCompilerThread::Run() {
41#ifdef DEBUG
42 thread_id_ = ThreadId::Current().ToInteger();
43#endif
44 Isolate::SetIsolateThreadLocals(isolate_, NULL);
45
46 int64_t epoch = 0;
47 if (FLAG_trace_parallel_recompilation) epoch = OS::Ticks();
48
49 while (true) {
50 input_queue_semaphore_->Wait();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +000051 Logger::TimerEventScope timer(
danno@chromium.org1f34ad32012-11-26 14:53:56 +000052 isolate_, Logger::TimerEventScope::v8_recompile_parallel);
ulan@chromium.org750145a2013-03-07 15:14:13 +000053
54 if (FLAG_parallel_recompilation_delay != 0) {
55 OS::Sleep(FLAG_parallel_recompilation_delay);
56 }
57
yangguo@chromium.org304cc332012-07-24 07:59:48 +000058 if (Acquire_Load(&stop_thread_)) {
59 stop_semaphore_->Signal();
60 if (FLAG_trace_parallel_recompilation) {
61 time_spent_total_ = OS::Ticks() - epoch;
62 }
63 return;
64 }
65
66 int64_t compiling_start = 0;
67 if (FLAG_trace_parallel_recompilation) compiling_start = OS::Ticks();
68
ulan@chromium.org750145a2013-03-07 15:14:13 +000069 CompileNext();
yangguo@chromium.org304cc332012-07-24 07:59:48 +000070
yangguo@chromium.orgfb377212012-11-16 14:43:43 +000071 if (!FLAG_manual_parallel_recompilation) {
72 isolate_->stack_guard()->RequestCodeReadyEvent();
73 } else {
74 // In manual mode, do not trigger a code ready event.
75 // Instead, wait for the optimized functions to be installed manually.
76 output_queue_semaphore_->Signal();
77 }
yangguo@chromium.org304cc332012-07-24 07:59:48 +000078
79 if (FLAG_trace_parallel_recompilation) {
80 time_spent_compiling_ += OS::Ticks() - compiling_start;
81 }
82 }
83}
84
85
ulan@chromium.org750145a2013-03-07 15:14:13 +000086void OptimizingCompilerThread::CompileNext() {
87 Heap::RelocationLock relocation_lock(isolate_->heap());
88 OptimizingCompiler* optimizing_compiler = NULL;
89 input_queue_.Dequeue(&optimizing_compiler);
90 Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(-1));
91
92 // Function may have been optimized meanwhile by OSR.
93 if (FLAG_use_osr &&
94 optimizing_compiler->info()->closure()->IsOptimized()) {
95 return;
96 }
97
98 OptimizingCompiler::Status status = optimizing_compiler->OptimizeGraph();
99 ASSERT(status != OptimizingCompiler::FAILED);
100 // Prevent an unused-variable error in release mode.
101 USE(status);
102
103 output_queue_.Enqueue(optimizing_compiler);
104}
105
106
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000107void OptimizingCompilerThread::Stop() {
108 Release_Store(&stop_thread_, static_cast<AtomicWord>(true));
109 input_queue_semaphore_->Signal();
110 stop_semaphore_->Wait();
111
ulan@chromium.org750145a2013-03-07 15:14:13 +0000112 if (FLAG_parallel_recompilation_delay != 0) {
113 // Execution ended before we managed to compile and install the remaining
114 // functions in the queue. We still want to do that for debugging though.
115 // At this point the optimizing thread already stopped, so we finish
116 // processing the queue in the main thread.
117 InstallOptimizedFunctions();
118 // Barrier when loading queue length is not necessary since the write
119 // happens in CompileNext on the same thread.
120 while (NoBarrier_Load(&queue_length_) > 0) {
121 CompileNext();
122 InstallOptimizedFunctions();
123 }
124 }
125
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000126 if (FLAG_trace_parallel_recompilation) {
127 double compile_time = static_cast<double>(time_spent_compiling_);
128 double total_time = static_cast<double>(time_spent_total_);
129 double percentage = (compile_time * 100) / total_time;
130 PrintF(" ** Compiler thread did %.2f%% useful work\n", percentage);
131 }
132}
133
134
135void OptimizingCompilerThread::InstallOptimizedFunctions() {
136 HandleScope handle_scope(isolate_);
137 int functions_installed = 0;
138 while (!output_queue_.IsEmpty()) {
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000139 if (FLAG_manual_parallel_recompilation) {
140 output_queue_semaphore_->Wait();
141 }
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000142 OptimizingCompiler* compiler = NULL;
143 output_queue_.Dequeue(&compiler);
144 Compiler::InstallOptimizedCode(compiler);
145 functions_installed++;
146 }
147 if (FLAG_trace_parallel_recompilation && functions_installed != 0) {
148 PrintF(" ** Installed %d function(s).\n", functions_installed);
149 }
150}
151
152
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000153Handle<SharedFunctionInfo>
154 OptimizingCompilerThread::InstallNextOptimizedFunction() {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000155 ASSERT(FLAG_manual_parallel_recompilation ||
156 FLAG_parallel_recompilation_delay != 0);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000157 output_queue_semaphore_->Wait();
158 OptimizingCompiler* compiler = NULL;
159 output_queue_.Dequeue(&compiler);
ulan@chromium.org750145a2013-03-07 15:14:13 +0000160 // Copy a handle from deferred handle scope to the normal handle scope.
161 Handle<SharedFunctionInfo> shared(*compiler->info()->shared_info());
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000162 Compiler::InstallOptimizedCode(compiler);
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000163 return shared;
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000164}
165
166
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000167void OptimizingCompilerThread::QueueForOptimization(
168 OptimizingCompiler* optimizing_compiler) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000169 ASSERT(IsQueueAvailable());
170 Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(1));
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000171 input_queue_.Enqueue(optimizing_compiler);
172 input_queue_semaphore_->Signal();
173}
174
175#ifdef DEBUG
176bool OptimizingCompilerThread::IsOptimizerThread() {
177 if (!FLAG_parallel_recompilation) return false;
178 return ThreadId::Current().ToInteger() == thread_id_;
179}
180#endif
181
182
183} } // namespace v8::internal