Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: src/optimizing-compiler-thread.h

Issue 177493002: Introduce SynchronizedScope to allow heap access from compiler thread. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: addressed comments Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/log.cc ('k') | src/optimizing-compiler-thread.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 11 matching lines...) Expand all
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #ifndef V8_OPTIMIZING_COMPILER_THREAD_H_ 28 #ifndef V8_OPTIMIZING_COMPILER_THREAD_H_
29 #define V8_OPTIMIZING_COMPILER_THREAD_H_ 29 #define V8_OPTIMIZING_COMPILER_THREAD_H_
30 30
31 #include "atomicops.h" 31 #include "atomicops.h"
32 #include "assert-scope.h"
32 #include "flags.h" 33 #include "flags.h"
33 #include "list.h" 34 #include "list.h"
34 #include "platform.h" 35 #include "platform.h"
35 #include "platform/mutex.h" 36 #include "platform/mutex.h"
36 #include "platform/time.h" 37 #include "platform/time.h"
37 #include "unbound-queue-inl.h" 38 #include "unbound-queue-inl.h"
38 39
39 namespace v8 { 40 namespace v8 {
40 namespace internal { 41 namespace internal {
41 42
43 class CompilationInfo;
42 class HOptimizedGraphBuilder; 44 class HOptimizedGraphBuilder;
43 class OptimizedCompileJob; 45 class OptimizedCompileJob;
44 class SharedFunctionInfo; 46 class SharedFunctionInfo;
45 47
46 class OptimizingCompilerThread : public Thread { 48 class OptimizingCompilerThread : public Thread {
47 public: 49 public:
48 explicit OptimizingCompilerThread(Isolate *isolate) : 50 explicit OptimizingCompilerThread(Isolate *isolate) :
49 Thread("OptimizingCompilerThread"), 51 Thread("OptimizingCompilerThread"),
50 #ifdef DEBUG 52 #ifdef DEBUG
51 thread_id_(0), 53 thread_id_(0),
52 #endif 54 #endif
53 isolate_(isolate), 55 isolate_(isolate),
54 stop_semaphore_(0), 56 stop_semaphore_(0),
55 input_queue_semaphore_(0), 57 input_queue_semaphore_(0),
58 main_thread_semaphore_(0),
59 compiler_thread_semaphore_(0),
56 input_queue_capacity_(FLAG_concurrent_recompilation_queue_length), 60 input_queue_capacity_(FLAG_concurrent_recompilation_queue_length),
57 input_queue_length_(0), 61 input_queue_length_(0),
58 input_queue_shift_(0), 62 input_queue_shift_(0),
59 osr_buffer_capacity_(FLAG_concurrent_recompilation_queue_length + 4), 63 osr_buffer_capacity_(FLAG_concurrent_recompilation_queue_length + 4),
60 osr_buffer_cursor_(0), 64 osr_buffer_cursor_(0),
61 osr_hits_(0), 65 osr_hits_(0),
62 osr_attempts_(0), 66 osr_attempts_(0),
63 blocked_jobs_(0) { 67 blocked_jobs_(0) {
64 NoBarrier_Store(&stop_thread_, static_cast<AtomicWord>(CONTINUE)); 68 NoBarrier_Store(&loop_switch_, static_cast<AtomicWord>(CONTINUE));
65 input_queue_ = NewArray<OptimizedCompileJob*>(input_queue_capacity_); 69 input_queue_ = NewArray<OptimizedCompileJob*>(input_queue_capacity_);
66 if (FLAG_concurrent_osr) { 70 if (FLAG_concurrent_osr) {
67 // Allocate and mark OSR buffer slots as empty. 71 // Allocate and mark OSR buffer slots as empty.
68 osr_buffer_ = NewArray<OptimizedCompileJob*>(osr_buffer_capacity_); 72 osr_buffer_ = NewArray<OptimizedCompileJob*>(osr_buffer_capacity_);
69 for (int i = 0; i < osr_buffer_capacity_; i++) osr_buffer_[i] = NULL; 73 for (int i = 0; i < osr_buffer_capacity_; i++) osr_buffer_[i] = NULL;
70 } 74 }
71 } 75 }
72 76
73 ~OptimizingCompilerThread(); 77 ~OptimizingCompilerThread();
74 78
75 void Run(); 79 void Run();
76 void Stop(); 80 void Stop();
77 void Flush(); 81 void Flush();
78 void QueueForOptimization(OptimizedCompileJob* optimizing_compiler); 82 void QueueForOptimization(OptimizedCompileJob* optimizing_compiler);
79 void Unblock(); 83 void Unblock();
80 void InstallOptimizedFunctions(); 84 void InstallOptimizedFunctions();
81 OptimizedCompileJob* FindReadyOSRCandidate(Handle<JSFunction> function, 85 OptimizedCompileJob* FindReadyOSRCandidate(Handle<JSFunction> function,
82 BailoutId osr_ast_id); 86 BailoutId osr_ast_id);
83 bool IsQueuedForOSR(Handle<JSFunction> function, BailoutId osr_ast_id); 87 bool IsQueuedForOSR(Handle<JSFunction> function, BailoutId osr_ast_id);
84 88
85 bool IsQueuedForOSR(JSFunction* function); 89 bool IsQueuedForOSR(JSFunction* function);
86 90
87 inline bool IsQueueAvailable() { 91 inline bool IsQueueAvailable() {
88 LockGuard<Mutex> access_input_queue(&input_queue_mutex_); 92 LockGuard<Mutex> access_input_queue(&mutex_);
89 return input_queue_length_ < input_queue_capacity_; 93 return input_queue_length_ < input_queue_capacity_;
90 } 94 }
91 95
92 inline void AgeBufferedOsrJobs() { 96 inline void AgeBufferedOsrJobs() {
93 // Advance cursor of the cyclic buffer to next empty slot or stale OSR job. 97 // Advance cursor of the cyclic buffer to next empty slot or stale OSR job.
94 // Dispose said OSR job in the latter case. Calling this on every GC 98 // Dispose said OSR job in the latter case. Calling this on every GC
95 // should make sure that we do not hold onto stale jobs indefinitely. 99 // should make sure that we do not hold onto stale jobs indefinitely.
96 AddToOsrBuffer(NULL); 100 AddToOsrBuffer(NULL);
97 } 101 }
98 102
99 static bool Enabled(int max_available) { 103 static bool Enabled(int max_available) {
100 return (FLAG_concurrent_recompilation && max_available > 1); 104 return (FLAG_concurrent_recompilation && max_available > 1);
101 } 105 }
102 106
103 #ifdef DEBUG 107 #ifdef DEBUG
104 static bool IsOptimizerThread(Isolate* isolate); 108 static bool IsOptimizerThread(Isolate* isolate);
105 bool IsOptimizerThread(); 109 bool IsOptimizerThread();
106 #endif 110 #endif
107 111
112 void PauseMainThread();
113 void ContinueMainThread();
114 void YieldToCompilerThread();
115
116 // Use this scope during the optimization phase to synchronize with
117 // the main thread in order to access the heap.
118 class SynchronizedScope {
119 public:
120 explicit SynchronizedScope(CompilationInfo* info);
121 ~SynchronizedScope();
122
123 private:
124 CompilationInfo* info_;
125 AllowHandleAllocation allow_handle_allocation_;
126 AllowHandleDereference allow_handle_dereference_;
127 AllowHeapAllocation allow_heap_allocation_;
128 };
129
108 private: 130 private:
109 enum StopFlag { CONTINUE, STOP, FLUSH }; 131 enum LoopSwitch { CONTINUE, STOP, FLUSH };
132 enum FlushMode { DO_NOT_RESTORE_FUNCTION_CODE, RESTORE_FUNCTION_CODE };
110 133
111 void FlushInputQueue(bool restore_function_code); 134 inline void SetSwitchAndInterceptInterrupt(LoopSwitch loop_switch);
112 void FlushOutputQueue(bool restore_function_code); 135 inline void PrepareInterruption(LoopSwitch loop_switch);
113 void FlushOsrBuffer(bool restore_function_code); 136 inline void DisposeOptimizedCompileJob(OptimizedCompileJob* job,
137 FlushMode mode);
138
139 void FlushInputQueue(FlushMode mode);
140 void FlushOutputQueue(FlushMode mode);
141 void FlushOsrBuffer(FlushMode mode);
142
114 void CompileNext(); 143 void CompileNext();
115 OptimizedCompileJob* NextInput(); 144 OptimizedCompileJob* NextInput();
116 145
117 // Add a recompilation task for OSR to the cyclic buffer, awaiting OSR entry. 146 // Add a recompilation task for OSR to the cyclic buffer, awaiting OSR entry.
118 // Tasks evicted from the cyclic buffer are discarded. 147 // Tasks evicted from the cyclic buffer are discarded.
119 void AddToOsrBuffer(OptimizedCompileJob* compiler); 148 void AddToOsrBuffer(OptimizedCompileJob* compiler);
120 149
121 inline int InputQueueIndex(int i) { 150 inline int InputQueueIndex(int i) {
122 int result = (i + input_queue_shift_) % input_queue_capacity_; 151 int result = (i + input_queue_shift_) % input_queue_capacity_;
123 ASSERT_LE(0, result); 152 ASSERT_LE(0, result);
124 ASSERT_LT(result, input_queue_capacity_); 153 ASSERT_LT(result, input_queue_capacity_);
125 return result; 154 return result;
126 } 155 }
127 156
128 #ifdef DEBUG 157 #ifdef DEBUG
129 int thread_id_; 158 int thread_id_;
130 Mutex thread_id_mutex_; 159 Mutex thread_id_mutex_;
131 #endif 160 #endif
132 161
133 Isolate* isolate_; 162 Isolate* isolate_;
134 Semaphore stop_semaphore_; 163 Semaphore stop_semaphore_;
135 Semaphore input_queue_semaphore_; 164 Semaphore input_queue_semaphore_;
165 Semaphore main_thread_semaphore_;
166 Semaphore compiler_thread_semaphore_;
167 Mutex mutex_;
136 168
137 // Circular queue of incoming recompilation tasks (including OSR). 169 // Circular queue of incoming recompilation tasks (including OSR).
138 OptimizedCompileJob** input_queue_; 170 OptimizedCompileJob** input_queue_;
139 int input_queue_capacity_; 171 int input_queue_capacity_;
140 int input_queue_length_; 172 int input_queue_length_;
141 int input_queue_shift_; 173 int input_queue_shift_;
142 Mutex input_queue_mutex_;
143 174
144 // Queue of recompilation tasks ready to be installed (excluding OSR). 175 // Queue of recompilation tasks ready to be installed (excluding OSR).
145 UnboundQueue<OptimizedCompileJob*> output_queue_; 176 UnboundQueue<OptimizedCompileJob*> output_queue_;
146 177
147 // Cyclic buffer of recompilation tasks for OSR. 178 // Cyclic buffer of recompilation tasks for OSR.
148 OptimizedCompileJob** osr_buffer_; 179 OptimizedCompileJob** osr_buffer_;
149 int osr_buffer_capacity_; 180 int osr_buffer_capacity_;
150 int osr_buffer_cursor_; 181 int osr_buffer_cursor_;
151 182
152 volatile AtomicWord stop_thread_; 183 volatile AtomicWord loop_switch_;
153 TimeDelta time_spent_compiling_; 184 TimeDelta time_spent_compiling_;
154 TimeDelta time_spent_total_; 185 TimeDelta time_spent_total_;
155 186
156 int osr_hits_; 187 int osr_hits_;
157 int osr_attempts_; 188 int osr_attempts_;
158 189
159 int blocked_jobs_; 190 int blocked_jobs_;
160 }; 191 };
161 192
162 } } // namespace v8::internal 193 } } // namespace v8::internal
163 194
164 #endif // V8_OPTIMIZING_COMPILER_THREAD_H_ 195 #endif // V8_OPTIMIZING_COMPILER_THREAD_H_
OLDNEW
« no previous file with comments | « src/log.cc ('k') | src/optimizing-compiler-thread.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698