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_COMPILE_DISPATCHER_H_
6 #define V8_OPTIMIZING_COMPILE_DISPATCHER_H_
10 #include "src/base/atomicops.h"
11 #include "src/base/platform/condition-variable.h"
12 #include "src/base/platform/mutex.h"
13 #include "src/base/platform/platform.h"
14 #include "src/flags.h"
20 class HOptimizedGraphBuilder;
21 class OptimizedCompileJob;
22 class SharedFunctionInfo;
24 class OptimizingCompileDispatcher {
26 explicit OptimizingCompileDispatcher(Isolate* isolate)
28 input_queue_capacity_(FLAG_concurrent_recompilation_queue_length),
29 input_queue_length_(0),
30 input_queue_shift_(0),
31 osr_buffer_capacity_(FLAG_concurrent_recompilation_queue_length + 4),
32 osr_buffer_cursor_(0),
37 recompilation_delay_(FLAG_concurrent_recompilation_delay) {
38 base::NoBarrier_Store(&mode_, static_cast<base::AtomicWord>(COMPILE));
39 input_queue_ = NewArray<OptimizedCompileJob*>(input_queue_capacity_);
40 if (FLAG_concurrent_osr) {
41 // Allocate and mark OSR buffer slots as empty.
42 osr_buffer_ = NewArray<OptimizedCompileJob*>(osr_buffer_capacity_);
43 for (int i = 0; i < osr_buffer_capacity_; i++) osr_buffer_[i] = NULL;
47 ~OptimizingCompileDispatcher();
52 void QueueForOptimization(OptimizedCompileJob* optimizing_compiler);
54 void InstallOptimizedFunctions();
55 OptimizedCompileJob* FindReadyOSRCandidate(Handle<JSFunction> function,
56 BailoutId osr_ast_id);
57 bool IsQueuedForOSR(Handle<JSFunction> function, BailoutId osr_ast_id);
59 bool IsQueuedForOSR(JSFunction* function);
61 inline bool IsQueueAvailable() {
62 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_);
63 return input_queue_length_ < input_queue_capacity_;
66 inline void AgeBufferedOsrJobs() {
67 // Advance cursor of the cyclic buffer to next empty slot or stale OSR job.
68 // Dispose said OSR job in the latter case. Calling this on every GC
69 // should make sure that we do not hold onto stale jobs indefinitely.
73 static bool Enabled(int max_available) {
74 return (FLAG_concurrent_recompilation && max_available > 1);
80 enum ModeFlag { COMPILE, FLUSH };
82 void FlushOutputQueue(bool restore_function_code);
83 void FlushOsrBuffer(bool restore_function_code);
84 void CompileNext(OptimizedCompileJob* job);
85 OptimizedCompileJob* NextInput(bool check_if_flushing = false);
87 // Add a recompilation task for OSR to the cyclic buffer, awaiting OSR entry.
88 // Tasks evicted from the cyclic buffer are discarded.
89 void AddToOsrBuffer(OptimizedCompileJob* compiler);
91 inline int InputQueueIndex(int i) {
92 int result = (i + input_queue_shift_) % input_queue_capacity_;
94 DCHECK_LT(result, input_queue_capacity_);
100 // Circular queue of incoming recompilation tasks (including OSR).
101 OptimizedCompileJob** input_queue_;
102 int input_queue_capacity_;
103 int input_queue_length_;
104 int input_queue_shift_;
105 base::Mutex input_queue_mutex_;
107 // Queue of recompilation tasks ready to be installed (excluding OSR).
108 std::queue<OptimizedCompileJob*> output_queue_;
109 // Used for job based recompilation which has multiple producers on
110 // different threads.
111 base::Mutex output_queue_mutex_;
113 // Cyclic buffer of recompilation tasks for OSR.
114 OptimizedCompileJob** osr_buffer_;
115 int osr_buffer_capacity_;
116 int osr_buffer_cursor_;
118 volatile base::AtomicWord mode_;
126 base::Mutex ref_count_mutex_;
127 base::ConditionVariable ref_count_zero_;
129 // Copy of FLAG_concurrent_recompilation_delay that will be used from the
130 // background thread.
132 // Since flags might get modified while the background thread is running, it
133 // is not safe to access them directly.
134 int recompilation_delay_;
137 } // namespace v8::internal
139 #endif // V8_OPTIMIZING_COMPILE_DISPATCHER_H_