OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_OPTIMIZING_COMPILE_DISPATCHER_H_ | 5 #ifndef V8_OPTIMIZING_COMPILER_THREAD_H_ |
6 #define V8_OPTIMIZING_COMPILE_DISPATCHER_H_ | 6 #define V8_OPTIMIZING_COMPILER_THREAD_H_ |
7 | |
8 #include <queue> | |
9 | 7 |
10 #include "src/base/atomicops.h" | 8 #include "src/base/atomicops.h" |
11 #include "src/base/platform/condition-variable.h" | 9 #include "src/base/platform/condition-variable.h" |
12 #include "src/base/platform/mutex.h" | 10 #include "src/base/platform/mutex.h" |
13 #include "src/base/platform/platform.h" | 11 #include "src/base/platform/platform.h" |
| 12 #include "src/base/platform/time.h" |
14 #include "src/flags.h" | 13 #include "src/flags.h" |
15 #include "src/list.h" | 14 #include "src/list.h" |
| 15 #include "src/unbound-queue-inl.h" |
16 | 16 |
17 namespace v8 { | 17 namespace v8 { |
18 namespace internal { | 18 namespace internal { |
19 | 19 |
20 class HOptimizedGraphBuilder; | 20 class HOptimizedGraphBuilder; |
21 class OptimizedCompileJob; | 21 class OptimizedCompileJob; |
22 class SharedFunctionInfo; | 22 class SharedFunctionInfo; |
23 | 23 |
24 class OptimizingCompileDispatcher { | 24 class OptimizingCompilerThread : public base::Thread { |
25 public: | 25 public: |
26 explicit OptimizingCompileDispatcher(Isolate* isolate) | 26 explicit OptimizingCompilerThread(Isolate* isolate) |
27 : isolate_(isolate), | 27 : Thread(Options("OptimizingCompilerThread")), |
| 28 #ifdef DEBUG |
| 29 thread_id_(0), |
| 30 #endif |
| 31 isolate_(isolate), |
| 32 stop_semaphore_(0), |
| 33 input_queue_semaphore_(0), |
28 input_queue_capacity_(FLAG_concurrent_recompilation_queue_length), | 34 input_queue_capacity_(FLAG_concurrent_recompilation_queue_length), |
29 input_queue_length_(0), | 35 input_queue_length_(0), |
30 input_queue_shift_(0), | 36 input_queue_shift_(0), |
31 osr_buffer_capacity_(FLAG_concurrent_recompilation_queue_length + 4), | 37 osr_buffer_capacity_(FLAG_concurrent_recompilation_queue_length + 4), |
32 osr_buffer_cursor_(0), | 38 osr_buffer_cursor_(0), |
33 osr_hits_(0), | 39 osr_hits_(0), |
34 osr_attempts_(0), | 40 osr_attempts_(0), |
35 blocked_jobs_(0), | 41 blocked_jobs_(0), |
36 ref_count_(0), | 42 ref_count_(0), |
| 43 tracing_enabled_(FLAG_trace_concurrent_recompilation), |
| 44 job_based_recompilation_(FLAG_job_based_recompilation), |
37 recompilation_delay_(FLAG_concurrent_recompilation_delay) { | 45 recompilation_delay_(FLAG_concurrent_recompilation_delay) { |
38 base::NoBarrier_Store(&mode_, static_cast<base::AtomicWord>(COMPILE)); | 46 base::NoBarrier_Store(&stop_thread_, |
| 47 static_cast<base::AtomicWord>(CONTINUE)); |
39 input_queue_ = NewArray<OptimizedCompileJob*>(input_queue_capacity_); | 48 input_queue_ = NewArray<OptimizedCompileJob*>(input_queue_capacity_); |
40 if (FLAG_concurrent_osr) { | 49 if (FLAG_concurrent_osr) { |
41 // Allocate and mark OSR buffer slots as empty. | 50 // Allocate and mark OSR buffer slots as empty. |
42 osr_buffer_ = NewArray<OptimizedCompileJob*>(osr_buffer_capacity_); | 51 osr_buffer_ = NewArray<OptimizedCompileJob*>(osr_buffer_capacity_); |
43 for (int i = 0; i < osr_buffer_capacity_; i++) osr_buffer_[i] = NULL; | 52 for (int i = 0; i < osr_buffer_capacity_; i++) osr_buffer_[i] = NULL; |
44 } | 53 } |
45 } | 54 } |
46 | 55 |
47 ~OptimizingCompileDispatcher(); | 56 ~OptimizingCompilerThread(); |
48 | 57 |
49 void Run(); | 58 void Run(); |
50 void Stop(); | 59 void Stop(); |
51 void Flush(); | 60 void Flush(); |
52 void QueueForOptimization(OptimizedCompileJob* optimizing_compiler); | 61 void QueueForOptimization(OptimizedCompileJob* optimizing_compiler); |
53 void Unblock(); | 62 void Unblock(); |
54 void InstallOptimizedFunctions(); | 63 void InstallOptimizedFunctions(); |
55 OptimizedCompileJob* FindReadyOSRCandidate(Handle<JSFunction> function, | 64 OptimizedCompileJob* FindReadyOSRCandidate(Handle<JSFunction> function, |
56 BailoutId osr_ast_id); | 65 BailoutId osr_ast_id); |
57 bool IsQueuedForOSR(Handle<JSFunction> function, BailoutId osr_ast_id); | 66 bool IsQueuedForOSR(Handle<JSFunction> function, BailoutId osr_ast_id); |
58 | 67 |
59 bool IsQueuedForOSR(JSFunction* function); | 68 bool IsQueuedForOSR(JSFunction* function); |
60 | 69 |
61 inline bool IsQueueAvailable() { | 70 inline bool IsQueueAvailable() { |
62 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); | 71 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); |
63 return input_queue_length_ < input_queue_capacity_; | 72 return input_queue_length_ < input_queue_capacity_; |
64 } | 73 } |
65 | 74 |
66 inline void AgeBufferedOsrJobs() { | 75 inline void AgeBufferedOsrJobs() { |
67 // Advance cursor of the cyclic buffer to next empty slot or stale OSR job. | 76 // 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 | 77 // 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. | 78 // should make sure that we do not hold onto stale jobs indefinitely. |
70 AddToOsrBuffer(NULL); | 79 AddToOsrBuffer(NULL); |
71 } | 80 } |
72 | 81 |
73 static bool Enabled(int max_available) { | 82 static bool Enabled(int max_available) { |
74 return (FLAG_concurrent_recompilation && max_available > 1); | 83 return (FLAG_concurrent_recompilation && max_available > 1); |
75 } | 84 } |
76 | 85 |
| 86 #ifdef DEBUG |
| 87 static bool IsOptimizerThread(Isolate* isolate); |
| 88 bool IsOptimizerThread(); |
| 89 #endif |
| 90 |
77 private: | 91 private: |
78 class CompileTask; | 92 class CompileTask; |
79 | 93 |
80 enum ModeFlag { COMPILE, FLUSH }; | 94 enum StopFlag { CONTINUE, STOP, FLUSH }; |
81 | 95 |
| 96 void FlushInputQueue(bool restore_function_code); |
82 void FlushOutputQueue(bool restore_function_code); | 97 void FlushOutputQueue(bool restore_function_code); |
83 void FlushOsrBuffer(bool restore_function_code); | 98 void FlushOsrBuffer(bool restore_function_code); |
84 void CompileNext(OptimizedCompileJob* job); | 99 void CompileNext(OptimizedCompileJob* job); |
85 OptimizedCompileJob* NextInput(bool check_if_flushing = false); | 100 OptimizedCompileJob* NextInput(bool check_if_flushing = false); |
86 | 101 |
87 // Add a recompilation task for OSR to the cyclic buffer, awaiting OSR entry. | 102 // Add a recompilation task for OSR to the cyclic buffer, awaiting OSR entry. |
88 // Tasks evicted from the cyclic buffer are discarded. | 103 // Tasks evicted from the cyclic buffer are discarded. |
89 void AddToOsrBuffer(OptimizedCompileJob* compiler); | 104 void AddToOsrBuffer(OptimizedCompileJob* compiler); |
90 | 105 |
91 inline int InputQueueIndex(int i) { | 106 inline int InputQueueIndex(int i) { |
92 int result = (i + input_queue_shift_) % input_queue_capacity_; | 107 int result = (i + input_queue_shift_) % input_queue_capacity_; |
93 DCHECK_LE(0, result); | 108 DCHECK_LE(0, result); |
94 DCHECK_LT(result, input_queue_capacity_); | 109 DCHECK_LT(result, input_queue_capacity_); |
95 return result; | 110 return result; |
96 } | 111 } |
97 | 112 |
| 113 #ifdef DEBUG |
| 114 int thread_id_; |
| 115 base::Mutex thread_id_mutex_; |
| 116 #endif |
| 117 |
98 Isolate* isolate_; | 118 Isolate* isolate_; |
| 119 base::Semaphore stop_semaphore_; |
| 120 base::Semaphore input_queue_semaphore_; |
99 | 121 |
100 // Circular queue of incoming recompilation tasks (including OSR). | 122 // Circular queue of incoming recompilation tasks (including OSR). |
101 OptimizedCompileJob** input_queue_; | 123 OptimizedCompileJob** input_queue_; |
102 int input_queue_capacity_; | 124 int input_queue_capacity_; |
103 int input_queue_length_; | 125 int input_queue_length_; |
104 int input_queue_shift_; | 126 int input_queue_shift_; |
105 base::Mutex input_queue_mutex_; | 127 base::Mutex input_queue_mutex_; |
106 | 128 |
107 // Queue of recompilation tasks ready to be installed (excluding OSR). | 129 // Queue of recompilation tasks ready to be installed (excluding OSR). |
108 std::queue<OptimizedCompileJob*> output_queue_; | 130 UnboundQueue<OptimizedCompileJob*> output_queue_; |
109 // Used for job based recompilation which has multiple producers on | 131 // Used for job based recompilation which has multiple producers on |
110 // different threads. | 132 // different threads. |
111 base::Mutex output_queue_mutex_; | 133 base::Mutex output_queue_mutex_; |
112 | 134 |
113 // Cyclic buffer of recompilation tasks for OSR. | 135 // Cyclic buffer of recompilation tasks for OSR. |
114 OptimizedCompileJob** osr_buffer_; | 136 OptimizedCompileJob** osr_buffer_; |
115 int osr_buffer_capacity_; | 137 int osr_buffer_capacity_; |
116 int osr_buffer_cursor_; | 138 int osr_buffer_cursor_; |
117 | 139 |
118 volatile base::AtomicWord mode_; | 140 volatile base::AtomicWord stop_thread_; |
| 141 base::TimeDelta time_spent_compiling_; |
| 142 base::TimeDelta time_spent_total_; |
119 | 143 |
120 int osr_hits_; | 144 int osr_hits_; |
121 int osr_attempts_; | 145 int osr_attempts_; |
122 | 146 |
123 int blocked_jobs_; | 147 int blocked_jobs_; |
124 | 148 |
125 int ref_count_; | 149 int ref_count_; |
126 base::Mutex ref_count_mutex_; | 150 base::Mutex ref_count_mutex_; |
127 base::ConditionVariable ref_count_zero_; | 151 base::ConditionVariable ref_count_zero_; |
128 | 152 |
129 // Copy of FLAG_concurrent_recompilation_delay that will be used from the | 153 // Copies of FLAG_trace_concurrent_recompilation, |
130 // background thread. | 154 // FLAG_concurrent_recompilation_delay and |
| 155 // FLAG_job_based_recompilation that will be used from the background thread. |
131 // | 156 // |
132 // Since flags might get modified while the background thread is running, it | 157 // Since flags might get modified while the background thread is running, it |
133 // is not safe to access them directly. | 158 // is not safe to access them directly. |
| 159 bool tracing_enabled_; |
| 160 bool job_based_recompilation_; |
134 int recompilation_delay_; | 161 int recompilation_delay_; |
135 }; | 162 }; |
136 } | |
137 } // namespace v8::internal | |
138 | 163 |
139 #endif // V8_OPTIMIZING_COMPILE_DISPATCHER_H_ | 164 } } // namespace v8::internal |
| 165 |
| 166 #endif // V8_OPTIMIZING_COMPILER_THREAD_H_ |
OLD | NEW |