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/mutex.h"
10 #include "src/base/platform/platform.h"
11 #include "src/base/platform/time.h"
12 #include "src/flags.h"
14 #include "src/unbound-queue-inl.h"
19 class HOptimizedGraphBuilder;
20 class OptimizedCompileJob;
21 class SharedFunctionInfo;
23 class OptimizingCompilerThread : public base::Thread {
25 explicit OptimizingCompilerThread(Isolate* isolate)
26 : Thread(Options("OptimizingCompilerThread")),
32 input_queue_semaphore_(0),
33 input_queue_capacity_(FLAG_concurrent_recompilation_queue_length),
34 input_queue_length_(0),
35 input_queue_shift_(0),
36 osr_buffer_capacity_(FLAG_concurrent_recompilation_queue_length + 4),
37 osr_buffer_cursor_(0),
41 tracing_enabled_(FLAG_trace_concurrent_recompilation),
42 job_based_recompilation_(FLAG_job_based_recompilation) {
43 base::NoBarrier_Store(&stop_thread_,
44 static_cast<base::AtomicWord>(CONTINUE));
45 input_queue_ = NewArray<OptimizedCompileJob*>(input_queue_capacity_);
46 if (FLAG_concurrent_osr) {
47 // Allocate and mark OSR buffer slots as empty.
48 osr_buffer_ = NewArray<OptimizedCompileJob*>(osr_buffer_capacity_);
49 for (int i = 0; i < osr_buffer_capacity_; i++) osr_buffer_[i] = NULL;
53 ~OptimizingCompilerThread();
58 void QueueForOptimization(OptimizedCompileJob* optimizing_compiler);
60 void InstallOptimizedFunctions();
61 OptimizedCompileJob* FindReadyOSRCandidate(Handle<JSFunction> function,
62 BailoutId osr_ast_id);
63 bool IsQueuedForOSR(Handle<JSFunction> function, BailoutId osr_ast_id);
65 bool IsQueuedForOSR(JSFunction* function);
67 inline bool IsQueueAvailable() {
68 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_);
69 return input_queue_length_ < input_queue_capacity_;
72 inline void AgeBufferedOsrJobs() {
73 // Advance cursor of the cyclic buffer to next empty slot or stale OSR job.
74 // Dispose said OSR job in the latter case. Calling this on every GC
75 // should make sure that we do not hold onto stale jobs indefinitely.
79 static bool Enabled(int max_available) {
80 return (FLAG_concurrent_recompilation && max_available > 1);
84 static bool IsOptimizerThread(Isolate* isolate);
85 bool IsOptimizerThread();
91 enum StopFlag { CONTINUE, STOP, FLUSH };
93 void FlushInputQueue(bool restore_function_code);
94 void FlushOutputQueue(bool restore_function_code);
95 void FlushOsrBuffer(bool restore_function_code);
97 OptimizedCompileJob* NextInput();
99 // Add a recompilation task for OSR to the cyclic buffer, awaiting OSR entry.
100 // Tasks evicted from the cyclic buffer are discarded.
101 void AddToOsrBuffer(OptimizedCompileJob* compiler);
103 inline int InputQueueIndex(int i) {
104 int result = (i + input_queue_shift_) % input_queue_capacity_;
105 DCHECK_LE(0, result);
106 DCHECK_LT(result, input_queue_capacity_);
112 base::Mutex thread_id_mutex_;
116 base::Semaphore stop_semaphore_;
117 base::Semaphore input_queue_semaphore_;
119 // Circular queue of incoming recompilation tasks (including OSR).
120 OptimizedCompileJob** input_queue_;
121 int input_queue_capacity_;
122 int input_queue_length_;
123 int input_queue_shift_;
124 base::Mutex input_queue_mutex_;
126 // Queue of recompilation tasks ready to be installed (excluding OSR).
127 UnboundQueue<OptimizedCompileJob*> output_queue_;
128 // Used for job based recompilation which has multiple producers on
129 // different threads.
130 base::Mutex output_queue_mutex_;
132 // Cyclic buffer of recompilation tasks for OSR.
133 OptimizedCompileJob** osr_buffer_;
134 int osr_buffer_capacity_;
135 int osr_buffer_cursor_;
137 volatile base::AtomicWord stop_thread_;
138 base::TimeDelta time_spent_compiling_;
139 base::TimeDelta time_spent_total_;
146 // Copies of FLAG_trace_concurrent_recompilation and
147 // FLAG_job_based_recompilation that will be used from the background thread.
149 // Since flags might get modified while the background thread is running, it
150 // is not safe to access them directly.
151 bool tracing_enabled_;
152 bool job_based_recompilation_;
155 } } // namespace v8::internal
157 #endif // V8_OPTIMIZING_COMPILER_THREAD_H_