yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 1 | // 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 | |
| 36 | namespace v8 { |
| 37 | namespace internal { |
| 38 | |
| 39 | |
| 40 | void 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.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 51 | Logger::TimerEventScope timer( |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 52 | isolate_, Logger::TimerEventScope::v8_recompile_parallel); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame^] | 53 | |
| 54 | if (FLAG_parallel_recompilation_delay != 0) { |
| 55 | OS::Sleep(FLAG_parallel_recompilation_delay); |
| 56 | } |
| 57 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 58 | 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.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame^] | 69 | CompileNext(); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 70 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 71 | 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.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 78 | |
| 79 | if (FLAG_trace_parallel_recompilation) { |
| 80 | time_spent_compiling_ += OS::Ticks() - compiling_start; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame^] | 86 | void 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.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 107 | void OptimizingCompilerThread::Stop() { |
| 108 | Release_Store(&stop_thread_, static_cast<AtomicWord>(true)); |
| 109 | input_queue_semaphore_->Signal(); |
| 110 | stop_semaphore_->Wait(); |
| 111 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame^] | 112 | 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.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 126 | 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 | |
| 135 | void OptimizingCompilerThread::InstallOptimizedFunctions() { |
| 136 | HandleScope handle_scope(isolate_); |
| 137 | int functions_installed = 0; |
| 138 | while (!output_queue_.IsEmpty()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 139 | if (FLAG_manual_parallel_recompilation) { |
| 140 | output_queue_semaphore_->Wait(); |
| 141 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 142 | 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.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 153 | Handle<SharedFunctionInfo> |
| 154 | OptimizingCompilerThread::InstallNextOptimizedFunction() { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame^] | 155 | ASSERT(FLAG_manual_parallel_recompilation || |
| 156 | FLAG_parallel_recompilation_delay != 0); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 157 | output_queue_semaphore_->Wait(); |
| 158 | OptimizingCompiler* compiler = NULL; |
| 159 | output_queue_.Dequeue(&compiler); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame^] | 160 | // Copy a handle from deferred handle scope to the normal handle scope. |
| 161 | Handle<SharedFunctionInfo> shared(*compiler->info()->shared_info()); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 162 | Compiler::InstallOptimizedCode(compiler); |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 163 | return shared; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 167 | void OptimizingCompilerThread::QueueForOptimization( |
| 168 | OptimizingCompiler* optimizing_compiler) { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 169 | ASSERT(IsQueueAvailable()); |
| 170 | Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(1)); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 171 | input_queue_.Enqueue(optimizing_compiler); |
| 172 | input_queue_semaphore_->Signal(); |
| 173 | } |
| 174 | |
| 175 | #ifdef DEBUG |
| 176 | bool 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 |