OLD | NEW |
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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 class OptimizingCompilerThread : public Thread { | 46 class OptimizingCompilerThread : public Thread { |
47 public: | 47 public: |
48 explicit OptimizingCompilerThread(Isolate *isolate) : | 48 explicit OptimizingCompilerThread(Isolate *isolate) : |
49 Thread("OptimizingCompilerThread"), | 49 Thread("OptimizingCompilerThread"), |
50 #ifdef DEBUG | 50 #ifdef DEBUG |
51 thread_id_(0), | 51 thread_id_(0), |
52 #endif | 52 #endif |
53 isolate_(isolate), | 53 isolate_(isolate), |
54 stop_semaphore_(0), | 54 stop_semaphore_(0), |
55 input_queue_semaphore_(0), | 55 input_queue_semaphore_(0), |
56 osr_cursor_(0), | 56 input_queue_capacity_(FLAG_concurrent_recompilation_queue_length), |
| 57 input_queue_length_(0), |
| 58 input_queue_shift_(0), |
| 59 osr_buffer_capacity_(FLAG_concurrent_recompilation_queue_length + 4), |
| 60 osr_buffer_cursor_(0), |
57 osr_hits_(0), | 61 osr_hits_(0), |
58 osr_attempts_(0), | 62 osr_attempts_(0), |
59 blocked_jobs_(0) { | 63 blocked_jobs_(0) { |
60 NoBarrier_Store(&stop_thread_, static_cast<AtomicWord>(CONTINUE)); | 64 NoBarrier_Store(&stop_thread_, static_cast<AtomicWord>(CONTINUE)); |
61 NoBarrier_Store(&queue_length_, static_cast<AtomicWord>(0)); | 65 input_queue_ = NewArray<RecompileJob*>(input_queue_capacity_); |
62 if (FLAG_concurrent_osr) { | 66 osr_buffer_ = NewArray<RecompileJob*>(osr_buffer_capacity_); |
63 osr_buffer_size_ = FLAG_concurrent_recompilation_queue_length + 4; | 67 // Mark OSR buffer slots as empty. |
64 osr_buffer_ = NewArray<RecompileJob*>(osr_buffer_size_); | 68 for (int i = 0; i < osr_buffer_capacity_; i++) osr_buffer_[i] = NULL; |
65 for (int i = 0; i < osr_buffer_size_; i++) osr_buffer_[i] = NULL; | |
66 } | |
67 } | 69 } |
68 | 70 |
69 ~OptimizingCompilerThread() { | 71 ~OptimizingCompilerThread() { |
70 if (FLAG_concurrent_osr) DeleteArray(osr_buffer_); | 72 ASSERT_EQ(0, input_queue_length_); |
| 73 #ifdef DEBUG |
| 74 for (int i = 0; i < osr_buffer_capacity_; i++) { |
| 75 CHECK_EQ(NULL, osr_buffer_[i]); |
| 76 } |
| 77 #endif |
| 78 DeleteArray(osr_buffer_); |
71 } | 79 } |
72 | 80 |
73 void Run(); | 81 void Run(); |
74 void Stop(); | 82 void Stop(); |
75 void Flush(); | 83 void Flush(); |
76 void QueueForOptimization(RecompileJob* optimizing_compiler); | 84 void QueueForOptimization(RecompileJob* optimizing_compiler); |
77 void Unblock(); | 85 void Unblock(); |
78 void InstallOptimizedFunctions(); | 86 void InstallOptimizedFunctions(); |
79 RecompileJob* FindReadyOSRCandidate(Handle<JSFunction> function, | 87 RecompileJob* FindReadyOSRCandidate(Handle<JSFunction> function, |
80 uint32_t osr_pc_offset); | 88 uint32_t osr_pc_offset); |
81 bool IsQueuedForOSR(Handle<JSFunction> function, uint32_t osr_pc_offset); | 89 bool IsQueuedForOSR(Handle<JSFunction> function, uint32_t osr_pc_offset); |
82 | 90 |
83 bool IsQueuedForOSR(JSFunction* function); | 91 bool IsQueuedForOSR(JSFunction* function); |
84 | 92 |
85 inline bool IsQueueAvailable() { | 93 inline bool IsQueueAvailable() { |
86 // We don't need a barrier since we have a data dependency right | 94 LockGuard<Mutex> access_input_queue(&input_queue_mutex_); |
87 // after. | 95 return input_queue_length_ < input_queue_capacity_; |
88 Atomic32 current_length = NoBarrier_Load(&queue_length_); | 96 } |
89 | 97 |
90 // This can be queried only from the execution thread. | 98 inline void AgeBufferedOsrJobs() { |
91 ASSERT(!IsOptimizerThread()); | 99 // Advance cursor of the cyclic buffer to next empty slot or stale OSR job. |
92 // Since only the execution thread increments queue_length_ and | 100 // Dispose said OSR job in the latter case. Calling this on every GC |
93 // only one thread can run inside an Isolate at one time, a direct | 101 // should make sure that we do not hold onto stale jobs indefinitely. |
94 // doesn't introduce a race -- queue_length_ may decreased in | 102 AddToOsrBuffer(NULL); |
95 // meantime, but not increased. | |
96 return (current_length < FLAG_concurrent_recompilation_queue_length); | |
97 } | 103 } |
98 | 104 |
99 #ifdef DEBUG | 105 #ifdef DEBUG |
100 bool IsOptimizerThread(); | 106 bool IsOptimizerThread(); |
101 #endif | 107 #endif |
102 | 108 |
103 private: | 109 private: |
104 enum StopFlag { CONTINUE, STOP, FLUSH }; | 110 enum StopFlag { CONTINUE, STOP, FLUSH }; |
105 | 111 |
106 void FlushInputQueue(bool restore_function_code); | 112 void FlushInputQueue(bool restore_function_code); |
107 void FlushOutputQueue(bool restore_function_code); | 113 void FlushOutputQueue(bool restore_function_code); |
108 void FlushOsrBuffer(bool restore_function_code); | 114 void FlushOsrBuffer(bool restore_function_code); |
109 void CompileNext(); | 115 void CompileNext(); |
| 116 RecompileJob* NextInput(); |
110 | 117 |
111 // Add a recompilation task for OSR to the cyclic buffer, awaiting OSR entry. | 118 // Add a recompilation task for OSR to the cyclic buffer, awaiting OSR entry. |
112 // Tasks evicted from the cyclic buffer are discarded. | 119 // Tasks evicted from the cyclic buffer are discarded. |
113 void AddToOsrBuffer(RecompileJob* compiler); | 120 void AddToOsrBuffer(RecompileJob* compiler); |
114 void AdvanceOsrCursor() { | 121 |
115 osr_cursor_ = (osr_cursor_ + 1) % osr_buffer_size_; | 122 inline int InputQueueIndex(int i) { |
| 123 int result = (i + input_queue_shift_) % input_queue_capacity_; |
| 124 ASSERT_LE(0, result); |
| 125 ASSERT_LT(result, input_queue_capacity_); |
| 126 return result; |
116 } | 127 } |
117 | 128 |
118 #ifdef DEBUG | 129 #ifdef DEBUG |
119 int thread_id_; | 130 int thread_id_; |
120 Mutex thread_id_mutex_; | 131 Mutex thread_id_mutex_; |
121 #endif | 132 #endif |
122 | 133 |
123 Isolate* isolate_; | 134 Isolate* isolate_; |
124 Semaphore stop_semaphore_; | 135 Semaphore stop_semaphore_; |
125 Semaphore input_queue_semaphore_; | 136 Semaphore input_queue_semaphore_; |
126 | 137 |
127 // Queue of incoming recompilation tasks (including OSR). | 138 // Circular queue of incoming recompilation tasks (including OSR). |
128 UnboundQueue<RecompileJob*> input_queue_; | 139 RecompileJob** input_queue_; |
| 140 int input_queue_capacity_; |
| 141 int input_queue_length_; |
| 142 int input_queue_shift_; |
| 143 Mutex input_queue_mutex_; |
| 144 |
129 // Queue of recompilation tasks ready to be installed (excluding OSR). | 145 // Queue of recompilation tasks ready to be installed (excluding OSR). |
130 UnboundQueue<RecompileJob*> output_queue_; | 146 UnboundQueue<RecompileJob*> output_queue_; |
| 147 |
131 // Cyclic buffer of recompilation tasks for OSR. | 148 // Cyclic buffer of recompilation tasks for OSR. |
132 // TODO(yangguo): This may keep zombie tasks indefinitely, holding on to | |
133 // a lot of memory. Fix this. | |
134 RecompileJob** osr_buffer_; | 149 RecompileJob** osr_buffer_; |
135 // Cursor for the cyclic buffer. | 150 int osr_buffer_capacity_; |
136 int osr_cursor_; | 151 int osr_buffer_cursor_; |
137 int osr_buffer_size_; | |
138 | 152 |
139 volatile AtomicWord stop_thread_; | 153 volatile AtomicWord stop_thread_; |
140 volatile Atomic32 queue_length_; | |
141 TimeDelta time_spent_compiling_; | 154 TimeDelta time_spent_compiling_; |
142 TimeDelta time_spent_total_; | 155 TimeDelta time_spent_total_; |
143 | 156 |
144 int osr_hits_; | 157 int osr_hits_; |
145 int osr_attempts_; | 158 int osr_attempts_; |
146 | 159 |
147 int blocked_jobs_; | 160 int blocked_jobs_; |
148 }; | 161 }; |
149 | 162 |
150 } } // namespace v8::internal | 163 } } // namespace v8::internal |
151 | 164 |
152 #endif // V8_OPTIMIZING_COMPILER_THREAD_H_ | 165 #endif // V8_OPTIMIZING_COMPILER_THREAD_H_ |
OLD | NEW |