OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 #endif | 43 #endif |
44 Isolate::SetIsolateThreadLocals(isolate_, NULL); | 44 Isolate::SetIsolateThreadLocals(isolate_, NULL); |
45 | 45 |
46 int64_t epoch = 0; | 46 int64_t epoch = 0; |
47 if (FLAG_trace_parallel_recompilation) epoch = OS::Ticks(); | 47 if (FLAG_trace_parallel_recompilation) epoch = OS::Ticks(); |
48 | 48 |
49 while (true) { | 49 while (true) { |
50 input_queue_semaphore_->Wait(); | 50 input_queue_semaphore_->Wait(); |
51 Logger::TimerEventScope timer( | 51 Logger::TimerEventScope timer( |
52 isolate_, Logger::TimerEventScope::v8_recompile_parallel); | 52 isolate_, Logger::TimerEventScope::v8_recompile_parallel); |
| 53 |
| 54 if (FLAG_parallel_recompilation_delay != 0) { |
| 55 OS::Sleep(FLAG_parallel_recompilation_delay); |
| 56 } |
| 57 |
53 if (Acquire_Load(&stop_thread_)) { | 58 if (Acquire_Load(&stop_thread_)) { |
54 stop_semaphore_->Signal(); | 59 stop_semaphore_->Signal(); |
55 if (FLAG_trace_parallel_recompilation) { | 60 if (FLAG_trace_parallel_recompilation) { |
56 time_spent_total_ = OS::Ticks() - epoch; | 61 time_spent_total_ = OS::Ticks() - epoch; |
57 } | 62 } |
58 return; | 63 return; |
59 } | 64 } |
60 | 65 |
61 int64_t compiling_start = 0; | 66 int64_t compiling_start = 0; |
62 if (FLAG_trace_parallel_recompilation) compiling_start = OS::Ticks(); | 67 if (FLAG_trace_parallel_recompilation) compiling_start = OS::Ticks(); |
63 | 68 |
64 Heap::RelocationLock relocation_lock(isolate_->heap()); | 69 CompileNext(); |
65 OptimizingCompiler* optimizing_compiler = NULL; | |
66 input_queue_.Dequeue(&optimizing_compiler); | |
67 Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(-1)); | |
68 | 70 |
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); | |
77 if (!FLAG_manual_parallel_recompilation) { | 71 if (!FLAG_manual_parallel_recompilation) { |
78 isolate_->stack_guard()->RequestCodeReadyEvent(); | 72 isolate_->stack_guard()->RequestCodeReadyEvent(); |
79 } else { | 73 } else { |
80 // In manual mode, do not trigger a code ready event. | 74 // In manual mode, do not trigger a code ready event. |
81 // Instead, wait for the optimized functions to be installed manually. | 75 // Instead, wait for the optimized functions to be installed manually. |
82 output_queue_semaphore_->Signal(); | 76 output_queue_semaphore_->Signal(); |
83 } | 77 } |
84 | 78 |
85 if (FLAG_trace_parallel_recompilation) { | 79 if (FLAG_trace_parallel_recompilation) { |
86 time_spent_compiling_ += OS::Ticks() - compiling_start; | 80 time_spent_compiling_ += OS::Ticks() - compiling_start; |
87 } | 81 } |
88 } | 82 } |
89 } | 83 } |
90 | 84 |
91 | 85 |
| 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 |
92 void OptimizingCompilerThread::Stop() { | 107 void OptimizingCompilerThread::Stop() { |
93 Release_Store(&stop_thread_, static_cast<AtomicWord>(true)); | 108 Release_Store(&stop_thread_, static_cast<AtomicWord>(true)); |
94 input_queue_semaphore_->Signal(); | 109 input_queue_semaphore_->Signal(); |
95 stop_semaphore_->Wait(); | 110 stop_semaphore_->Wait(); |
96 | 111 |
| 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 |
97 if (FLAG_trace_parallel_recompilation) { | 126 if (FLAG_trace_parallel_recompilation) { |
98 double compile_time = static_cast<double>(time_spent_compiling_); | 127 double compile_time = static_cast<double>(time_spent_compiling_); |
99 double total_time = static_cast<double>(time_spent_total_); | 128 double total_time = static_cast<double>(time_spent_total_); |
100 double percentage = (compile_time * 100) / total_time; | 129 double percentage = (compile_time * 100) / total_time; |
101 PrintF(" ** Compiler thread did %.2f%% useful work\n", percentage); | 130 PrintF(" ** Compiler thread did %.2f%% useful work\n", percentage); |
102 } | 131 } |
103 } | 132 } |
104 | 133 |
105 | 134 |
106 void OptimizingCompilerThread::InstallOptimizedFunctions() { | 135 void OptimizingCompilerThread::InstallOptimizedFunctions() { |
107 HandleScope handle_scope(isolate_); | 136 HandleScope handle_scope(isolate_); |
108 int functions_installed = 0; | 137 int functions_installed = 0; |
109 while (!output_queue_.IsEmpty()) { | 138 while (!output_queue_.IsEmpty()) { |
110 if (FLAG_manual_parallel_recompilation) { | 139 if (FLAG_manual_parallel_recompilation) { |
111 output_queue_semaphore_->Wait(); | 140 output_queue_semaphore_->Wait(); |
112 } | 141 } |
113 OptimizingCompiler* compiler = NULL; | 142 OptimizingCompiler* compiler = NULL; |
114 output_queue_.Dequeue(&compiler); | 143 output_queue_.Dequeue(&compiler); |
115 Compiler::InstallOptimizedCode(compiler); | 144 Compiler::InstallOptimizedCode(compiler); |
116 functions_installed++; | 145 functions_installed++; |
117 } | 146 } |
118 if (FLAG_trace_parallel_recompilation && functions_installed != 0) { | 147 if (FLAG_trace_parallel_recompilation && functions_installed != 0) { |
119 PrintF(" ** Installed %d function(s).\n", functions_installed); | 148 PrintF(" ** Installed %d function(s).\n", functions_installed); |
120 } | 149 } |
121 } | 150 } |
122 | 151 |
123 | 152 |
124 Handle<SharedFunctionInfo> | 153 Handle<SharedFunctionInfo> |
125 OptimizingCompilerThread::InstallNextOptimizedFunction() { | 154 OptimizingCompilerThread::InstallNextOptimizedFunction() { |
126 ASSERT(FLAG_manual_parallel_recompilation); | 155 ASSERT(FLAG_manual_parallel_recompilation || |
| 156 FLAG_parallel_recompilation_delay != 0); |
127 output_queue_semaphore_->Wait(); | 157 output_queue_semaphore_->Wait(); |
128 OptimizingCompiler* compiler = NULL; | 158 OptimizingCompiler* compiler = NULL; |
129 output_queue_.Dequeue(&compiler); | 159 output_queue_.Dequeue(&compiler); |
130 Handle<SharedFunctionInfo> shared = compiler->info()->shared_info(); | 160 // Copy a handle from deferred handle scope to the normal handle scope. |
| 161 Handle<SharedFunctionInfo> shared(*compiler->info()->shared_info()); |
131 Compiler::InstallOptimizedCode(compiler); | 162 Compiler::InstallOptimizedCode(compiler); |
132 return shared; | 163 return shared; |
133 } | 164 } |
134 | 165 |
135 | 166 |
136 void OptimizingCompilerThread::QueueForOptimization( | 167 void OptimizingCompilerThread::QueueForOptimization( |
137 OptimizingCompiler* optimizing_compiler) { | 168 OptimizingCompiler* optimizing_compiler) { |
138 ASSERT(IsQueueAvailable()); | 169 ASSERT(IsQueueAvailable()); |
139 Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(1)); | 170 Barrier_AtomicIncrement(&queue_length_, static_cast<Atomic32>(1)); |
140 input_queue_.Enqueue(optimizing_compiler); | 171 input_queue_.Enqueue(optimizing_compiler); |
141 input_queue_semaphore_->Signal(); | 172 input_queue_semaphore_->Signal(); |
142 } | 173 } |
143 | 174 |
144 #ifdef DEBUG | 175 #ifdef DEBUG |
145 bool OptimizingCompilerThread::IsOptimizerThread() { | 176 bool OptimizingCompilerThread::IsOptimizerThread() { |
146 if (!FLAG_parallel_recompilation) return false; | 177 if (!FLAG_parallel_recompilation) return false; |
147 return ThreadId::Current().ToInteger() == thread_id_; | 178 return ThreadId::Current().ToInteger() == thread_id_; |
148 } | 179 } |
149 #endif | 180 #endif |
150 | 181 |
151 | 182 |
152 } } // namespace v8::internal | 183 } } // namespace v8::internal |
OLD | NEW |