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_COMPILE_DISPATCHER_H_ |
6 #define V8_OPTIMIZING_COMPILE_DISPATCHER_H_ | 6 #define V8_OPTIMIZING_COMPILE_DISPATCHER_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 | 9 |
10 #include "src/base/atomicops.h" | 10 #include "src/base/atomicops.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 class OptimizedCompileJob; | 21 class OptimizedCompileJob; |
22 class SharedFunctionInfo; | 22 class SharedFunctionInfo; |
23 | 23 |
24 class OptimizingCompileDispatcher { | 24 class OptimizingCompileDispatcher { |
25 public: | 25 public: |
26 explicit OptimizingCompileDispatcher(Isolate* isolate) | 26 explicit OptimizingCompileDispatcher(Isolate* isolate) |
27 : isolate_(isolate), | 27 : isolate_(isolate), |
28 input_queue_capacity_(FLAG_concurrent_recompilation_queue_length), | 28 input_queue_capacity_(FLAG_concurrent_recompilation_queue_length), |
29 input_queue_length_(0), | 29 input_queue_length_(0), |
30 input_queue_shift_(0), | 30 input_queue_shift_(0), |
31 osr_buffer_capacity_(FLAG_concurrent_recompilation_queue_length + 4), | |
32 osr_buffer_cursor_(0), | |
33 osr_hits_(0), | |
34 osr_attempts_(0), | |
35 blocked_jobs_(0), | 31 blocked_jobs_(0), |
36 ref_count_(0), | 32 ref_count_(0), |
37 recompilation_delay_(FLAG_concurrent_recompilation_delay) { | 33 recompilation_delay_(FLAG_concurrent_recompilation_delay) { |
38 base::NoBarrier_Store(&mode_, static_cast<base::AtomicWord>(COMPILE)); | 34 base::NoBarrier_Store(&mode_, static_cast<base::AtomicWord>(COMPILE)); |
39 input_queue_ = NewArray<OptimizedCompileJob*>(input_queue_capacity_); | 35 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; | |
44 } | |
45 } | 36 } |
46 | 37 |
47 ~OptimizingCompileDispatcher(); | 38 ~OptimizingCompileDispatcher(); |
48 | 39 |
49 void Run(); | 40 void Run(); |
50 void Stop(); | 41 void Stop(); |
51 void Flush(); | 42 void Flush(); |
52 void QueueForOptimization(OptimizedCompileJob* optimizing_compiler); | 43 void QueueForOptimization(OptimizedCompileJob* optimizing_compiler); |
53 void Unblock(); | 44 void Unblock(); |
54 void InstallOptimizedFunctions(); | 45 void InstallOptimizedFunctions(); |
55 OptimizedCompileJob* FindReadyOSRCandidate(Handle<JSFunction> function, | |
56 BailoutId osr_ast_id); | |
57 bool IsQueuedForOSR(Handle<JSFunction> function, BailoutId osr_ast_id); | |
58 | |
59 bool IsQueuedForOSR(JSFunction* function); | |
60 | 46 |
61 inline bool IsQueueAvailable() { | 47 inline bool IsQueueAvailable() { |
62 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); | 48 base::LockGuard<base::Mutex> access_input_queue(&input_queue_mutex_); |
63 return input_queue_length_ < input_queue_capacity_; | 49 return input_queue_length_ < input_queue_capacity_; |
64 } | 50 } |
65 | 51 |
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. | |
70 AddToOsrBuffer(NULL); | |
71 } | |
72 | |
73 static bool Enabled() { return FLAG_concurrent_recompilation; } | 52 static bool Enabled() { return FLAG_concurrent_recompilation; } |
74 | 53 |
75 private: | 54 private: |
76 class CompileTask; | 55 class CompileTask; |
77 | 56 |
78 enum ModeFlag { COMPILE, FLUSH }; | 57 enum ModeFlag { COMPILE, FLUSH }; |
79 | 58 |
80 void FlushOutputQueue(bool restore_function_code); | 59 void FlushOutputQueue(bool restore_function_code); |
81 void FlushOsrBuffer(bool restore_function_code); | |
82 void CompileNext(OptimizedCompileJob* job); | 60 void CompileNext(OptimizedCompileJob* job); |
83 OptimizedCompileJob* NextInput(bool check_if_flushing = false); | 61 OptimizedCompileJob* NextInput(bool check_if_flushing = false); |
84 | 62 |
85 // Add a recompilation task for OSR to the cyclic buffer, awaiting OSR entry. | |
86 // Tasks evicted from the cyclic buffer are discarded. | |
87 void AddToOsrBuffer(OptimizedCompileJob* compiler); | |
88 | |
89 inline int InputQueueIndex(int i) { | 63 inline int InputQueueIndex(int i) { |
90 int result = (i + input_queue_shift_) % input_queue_capacity_; | 64 int result = (i + input_queue_shift_) % input_queue_capacity_; |
91 DCHECK_LE(0, result); | 65 DCHECK_LE(0, result); |
92 DCHECK_LT(result, input_queue_capacity_); | 66 DCHECK_LT(result, input_queue_capacity_); |
93 return result; | 67 return result; |
94 } | 68 } |
95 | 69 |
96 Isolate* isolate_; | 70 Isolate* isolate_; |
97 | 71 |
98 // Circular queue of incoming recompilation tasks (including OSR). | 72 // Circular queue of incoming recompilation tasks (including OSR). |
99 OptimizedCompileJob** input_queue_; | 73 OptimizedCompileJob** input_queue_; |
100 int input_queue_capacity_; | 74 int input_queue_capacity_; |
101 int input_queue_length_; | 75 int input_queue_length_; |
102 int input_queue_shift_; | 76 int input_queue_shift_; |
103 base::Mutex input_queue_mutex_; | 77 base::Mutex input_queue_mutex_; |
104 | 78 |
105 // Queue of recompilation tasks ready to be installed (excluding OSR). | 79 // Queue of recompilation tasks ready to be installed (excluding OSR). |
106 std::queue<OptimizedCompileJob*> output_queue_; | 80 std::queue<OptimizedCompileJob*> output_queue_; |
107 // Used for job based recompilation which has multiple producers on | 81 // Used for job based recompilation which has multiple producers on |
108 // different threads. | 82 // different threads. |
109 base::Mutex output_queue_mutex_; | 83 base::Mutex output_queue_mutex_; |
110 | 84 |
111 // Cyclic buffer of recompilation tasks for OSR. | |
112 OptimizedCompileJob** osr_buffer_; | |
113 int osr_buffer_capacity_; | |
114 int osr_buffer_cursor_; | |
115 | |
116 volatile base::AtomicWord mode_; | 85 volatile base::AtomicWord mode_; |
117 | 86 |
118 int osr_hits_; | |
119 int osr_attempts_; | |
120 | |
121 int blocked_jobs_; | 87 int blocked_jobs_; |
122 | 88 |
123 int ref_count_; | 89 int ref_count_; |
124 base::Mutex ref_count_mutex_; | 90 base::Mutex ref_count_mutex_; |
125 base::ConditionVariable ref_count_zero_; | 91 base::ConditionVariable ref_count_zero_; |
126 | 92 |
127 // Copy of FLAG_concurrent_recompilation_delay that will be used from the | 93 // Copy of FLAG_concurrent_recompilation_delay that will be used from the |
128 // background thread. | 94 // background thread. |
129 // | 95 // |
130 // Since flags might get modified while the background thread is running, it | 96 // Since flags might get modified while the background thread is running, it |
131 // is not safe to access them directly. | 97 // is not safe to access them directly. |
132 int recompilation_delay_; | 98 int recompilation_delay_; |
133 }; | 99 }; |
134 } // namespace internal | 100 } // namespace internal |
135 } // namespace v8 | 101 } // namespace v8 |
136 | 102 |
137 #endif // V8_OPTIMIZING_COMPILE_DISPATCHER_H_ | 103 #endif // V8_OPTIMIZING_COMPILE_DISPATCHER_H_ |
OLD | NEW |