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); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 53 | if (Acquire_Load(&stop_thread_)) { |
| 54 | stop_semaphore_->Signal(); |
| 55 | if (FLAG_trace_parallel_recompilation) { |
| 56 | time_spent_total_ = OS::Ticks() - epoch; |
| 57 | } |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | int64_t compiling_start = 0; |
| 62 | if (FLAG_trace_parallel_recompilation) compiling_start = OS::Ticks(); |
| 63 | |
| 64 | Heap::RelocationLock relocation_lock(isolate_->heap()); |
| 65 | OptimizingCompiler* optimizing_compiler = NULL; |
| 66 | input_queue_.Dequeue(&optimizing_compiler); |
| 67 | Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(-1)); |
| 68 | |
| 69 | ASSERT(!optimizing_compiler->info()->closure()->IsOptimized()); |
| 70 | |
| 71 | OptimizingCompiler::Status status = optimizing_compiler->OptimizeGraph(); |
| 72 | ASSERT(status != OptimizingCompiler::FAILED); |
| 73 | // Prevent an unused-variable error in release mode. |
| 74 | USE(status); |
| 75 | |
| 76 | output_queue_.Enqueue(optimizing_compiler); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 77 | if (!FLAG_manual_parallel_recompilation) { |
| 78 | isolate_->stack_guard()->RequestCodeReadyEvent(); |
| 79 | } else { |
| 80 | // In manual mode, do not trigger a code ready event. |
| 81 | // Instead, wait for the optimized functions to be installed manually. |
| 82 | output_queue_semaphore_->Signal(); |
| 83 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 84 | |
| 85 | if (FLAG_trace_parallel_recompilation) { |
| 86 | time_spent_compiling_ += OS::Ticks() - compiling_start; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void OptimizingCompilerThread::Stop() { |
| 93 | Release_Store(&stop_thread_, static_cast<AtomicWord>(true)); |
| 94 | input_queue_semaphore_->Signal(); |
| 95 | stop_semaphore_->Wait(); |
| 96 | |
| 97 | if (FLAG_trace_parallel_recompilation) { |
| 98 | double compile_time = static_cast<double>(time_spent_compiling_); |
| 99 | double total_time = static_cast<double>(time_spent_total_); |
| 100 | double percentage = (compile_time * 100) / total_time; |
| 101 | PrintF(" ** Compiler thread did %.2f%% useful work\n", percentage); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | void OptimizingCompilerThread::InstallOptimizedFunctions() { |
| 107 | HandleScope handle_scope(isolate_); |
| 108 | int functions_installed = 0; |
| 109 | while (!output_queue_.IsEmpty()) { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 110 | if (FLAG_manual_parallel_recompilation) { |
| 111 | output_queue_semaphore_->Wait(); |
| 112 | } |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 113 | OptimizingCompiler* compiler = NULL; |
| 114 | output_queue_.Dequeue(&compiler); |
| 115 | Compiler::InstallOptimizedCode(compiler); |
| 116 | functions_installed++; |
| 117 | } |
| 118 | if (FLAG_trace_parallel_recompilation && functions_installed != 0) { |
| 119 | PrintF(" ** Installed %d function(s).\n", functions_installed); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 124 | Handle<SharedFunctionInfo> |
| 125 | OptimizingCompilerThread::InstallNextOptimizedFunction() { |
| 126 | ASSERT(FLAG_manual_parallel_recompilation); |
| 127 | output_queue_semaphore_->Wait(); |
| 128 | OptimizingCompiler* compiler = NULL; |
| 129 | output_queue_.Dequeue(&compiler); |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 130 | Handle<SharedFunctionInfo> shared = compiler->info()->shared_info(); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 131 | Compiler::InstallOptimizedCode(compiler); |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 132 | return shared; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 136 | void OptimizingCompilerThread::QueueForOptimization( |
| 137 | OptimizingCompiler* optimizing_compiler) { |
mstarzinger@chromium.org | 068ea0a | 2013-01-30 09:39:44 +0000 | [diff] [blame] | 138 | ASSERT(IsQueueAvailable()); |
| 139 | Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(1)); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 140 | input_queue_.Enqueue(optimizing_compiler); |
| 141 | input_queue_semaphore_->Signal(); |
| 142 | } |
| 143 | |
| 144 | #ifdef DEBUG |
| 145 | bool OptimizingCompilerThread::IsOptimizerThread() { |
| 146 | if (!FLAG_parallel_recompilation) return false; |
| 147 | return ThreadId::Current().ToInteger() == thread_id_; |
| 148 | } |
| 149 | #endif |
| 150 | |
| 151 | |
| 152 | } } // namespace v8::internal |