1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef V8_OPTIMIZING_COMPILER_THREAD_H_
6 #define V8_OPTIMIZING_COMPILER_THREAD_H_
8 #include "src/base/atomicops.h"
9 #include "src/base/platform/condition-variable.h"
10 #include "src/base/platform/mutex.h"
11 #include "src/base/platform/platform.h"
12 #include "src/base/platform/time.h"
13 #include "src/flags.h"
15 #include "src/unbound-queue-inl.h"
20 class HOptimizedGraphBuilder;
21 class OptimizedCompileJob;
22 class SharedFunctionInfo;
24 class OptimizingCompilerThread : public base::Thread {
26 explicit OptimizingCompilerThread(Isolate* isolate)
27 : Thread(Options("OptimizingCompilerThread")),
33 input_queue_semaphore_(0),
34 input_queue_capacity_(FLAG_concurrent_recompilation_queue_length),
35 input_queue_length_(0),
36 input_queue_shift_(0),
37 osr_buffer_capacity_(FLAG_concurrent_recompilation_queue_length + 4),
38 osr_buffer_cursor_(0),
43 tracing_enabled_(FLAG_trace_concurrent_recompilation),
44 job_based_recompilation_(FLAG_job_based_recompilation),
45 recompilation_delay_(FLAG_concurrent_recompilation_delay) {
46 base::NoBarrier_Store(&stop_thread_,
47 static_cast<base::AtomicWord>(CONTINUE));
48 input_queue_ = NewArray<OptimizedCompileJob*>(input_queue_capacity_);
49 if (FLAG_concurrent_osr) {
50 // Allocate and mark OSR buffer slots as empty.
51 osr_buffer_ = NewArray<OptimizedCompileJob*>(osr_buffer_capacity_);
52 for (int i = 0; i < osr_buffer_capacity_; i++) osr_buffer_[i] = NULL;
56 ~OptimizingCompilerThread();
61 void QueueForOptimization(OptimizedCompileJob* optimizing_compiler);
63 void InstallOptimizedFunctions();
64 OptimizedCompileJob* FindReadyOSRCandidate(Handle<JSFunction> function,
65 BailoutId osr_ast_id);
66 bool IsQueuedForOSR(Handle<JSFunction> function, BailoutId osr_ast_id);
68 bool IsQueuedForOSR(JSFunction* function);
70 inline bool IsQueueAvailable() {
71 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_);
72 return input_queue_length_ < input_queue_capacity_;
75 inline void AgeBufferedOsrJobs() {
76 // Advance cursor of the cyclic buffer to next empty slot or stale OSR job.
77 // Dispose said OSR job in the latter case. Calling this on every GC
78 // should make sure that we do not hold onto stale jobs indefinitely.
82 static bool Enabled(int max_available) {
83 return (FLAG_concurrent_recompilation && max_available > 1);
87 static bool IsOptimizerThread(Isolate* isolate);
88 bool IsOptimizerThread();
94 enum StopFlag { CONTINUE, STOP, FLUSH };
96 void FlushInputQueue(bool restore_function_code);
97 void FlushOutputQueue(bool restore_function_code);
98 void FlushOsrBuffer(bool restore_function_code);
99 void CompileNext(OptimizedCompileJob* job);
100 OptimizedCompileJob* NextInput(bool check_if_flushing = false);
102 // Add a recompilation task for OSR to the cyclic buffer, awaiting OSR entry.
103 // Tasks evicted from the cyclic buffer are discarded.
104 void AddToOsrBuffer(OptimizedCompileJob* compiler);
106 inline int InputQueueIndex(int i) {
107 int result = (i + input_queue_shift_) % input_queue_capacity_;
108 DCHECK_LE(0, result);
109 DCHECK_LT(result, input_queue_capacity_);
115 base::Mutex thread_id_mutex_;
119 base::Semaphore stop_semaphore_;
120 base::Semaphore input_queue_semaphore_;
122 // Circular queue of incoming recompilation tasks (including OSR).
123 OptimizedCompileJob** input_queue_;
124 int input_queue_capacity_;
125 int input_queue_length_;
126 int input_queue_shift_;
127 base::Mutex input_queue_mutex_;
129 // Queue of recompilation tasks ready to be installed (excluding OSR).
130 UnboundQueue<OptimizedCompileJob*> output_queue_;
131 // Used for job based recompilation which has multiple producers on
132 // different threads.
133 base::Mutex output_queue_mutex_;
135 // Cyclic buffer of recompilation tasks for OSR.
136 OptimizedCompileJob** osr_buffer_;
137 int osr_buffer_capacity_;
138 int osr_buffer_cursor_;
140 volatile base::AtomicWord stop_thread_;
141 base::TimeDelta time_spent_compiling_;
142 base::TimeDelta time_spent_total_;
150 base::Mutex ref_count_mutex_;
151 base::ConditionVariable ref_count_zero_;
153 // Copies of FLAG_trace_concurrent_recompilation,
154 // FLAG_concurrent_recompilation_delay and
155 // FLAG_job_based_recompilation that will be used from the background thread.
157 // Since flags might get modified while the background thread is running, it
158 // is not safe to access them directly.
159 bool tracing_enabled_;
160 bool job_based_recompilation_;
161 int recompilation_delay_;
164 } } // namespace v8::internal
166 #endif // V8_OPTIMIZING_COMPILER_THREAD_H_