11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "gc/g1/concurrentG1Refine.hpp"
27 #include "gc/g1/concurrentG1RefineThread.hpp"
28 #include "gc/g1/g1CollectedHeap.inline.hpp"
29 #include "gc/g1/g1CollectorPolicy.hpp"
30 #include "gc/g1/suspendibleThreadSet.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "runtime/handles.inline.hpp"
33 #include "runtime/mutexLocker.hpp"
34
35 ConcurrentG1RefineThread::
36 ConcurrentG1RefineThread(ConcurrentG1Refine* cg1r, ConcurrentG1RefineThread *next,
37 CardTableEntryClosure* refine_closure,
38 uint worker_id_offset, uint worker_id) :
39 ConcurrentGCThread(),
40 _refine_closure(refine_closure),
41 _worker_id_offset(worker_id_offset),
42 _worker_id(worker_id),
43 _active(false),
44 _next(next),
45 _monitor(NULL),
46 _cg1r(cg1r),
47 _vtime_accum(0.0)
48 {
49
50 // Each thread has its own monitor. The i-th thread is responsible for signaling
71 // A thread deactivates once the number of buffer reached a deactivation threshold
72 _deactivation_threshold = MAX2<int>(_threshold - cg1r()->thread_threshold_step(), cg1r()->green_zone());
73 }
74
75 void ConcurrentG1RefineThread::wait_for_completed_buffers() {
76 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
77 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
78 while (!_should_terminate && !is_active()) {
79 _monitor->wait(Mutex::_no_safepoint_check_flag);
80 }
81 }
82
83 bool ConcurrentG1RefineThread::is_active() {
84 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
85 return is_primary() ? dcqs.process_completed_buffers() : _active;
86 }
87
88 void ConcurrentG1RefineThread::activate() {
89 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
90 if (!is_primary()) {
91 if (G1TraceConcRefinement) {
92 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
93 gclog_or_tty->print_cr("G1-Refine-activated worker %d, on threshold %d, current %d",
94 _worker_id, _threshold, (int)dcqs.completed_buffers_num());
95 }
96 set_active(true);
97 } else {
98 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
99 dcqs.set_process_completed(true);
100 }
101 _monitor->notify();
102 }
103
104 void ConcurrentG1RefineThread::deactivate() {
105 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
106 if (!is_primary()) {
107 if (G1TraceConcRefinement) {
108 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
109 gclog_or_tty->print_cr("G1-Refine-deactivated worker %d, off threshold %d, current %d",
110 _worker_id, _deactivation_threshold, (int)dcqs.completed_buffers_num());
111 }
112 set_active(false);
113 } else {
114 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
115 dcqs.set_process_completed(false);
116 }
117 }
118
119 void ConcurrentG1RefineThread::run() {
120 initialize_in_thread();
121 wait_for_universe_init();
122
123 run_service();
124
125 terminate();
126 }
127
128 void ConcurrentG1RefineThread::run_service() {
129 _vtime_start = os::elapsedVTime();
130
131 while (!_should_terminate) {
157
158 // Check if we need to activate the next thread.
159 if (_next != NULL && !_next->is_active() && curr_buffer_num > _next->_threshold) {
160 _next->activate();
161 }
162 } while (dcqs.apply_closure_to_completed_buffer(_refine_closure, _worker_id + _worker_id_offset, cg1r()->green_zone()));
163
164 // We can exit the loop above while being active if there was a yield request.
165 if (is_active()) {
166 deactivate();
167 }
168 }
169
170 if (os::supports_vtime()) {
171 _vtime_accum = (os::elapsedVTime() - _vtime_start);
172 } else {
173 _vtime_accum = 0.0;
174 }
175 }
176
177 if (G1TraceConcRefinement) {
178 gclog_or_tty->print_cr("G1-Refine-stop");
179 }
180 }
181
182 void ConcurrentG1RefineThread::stop() {
183 // it is ok to take late safepoints here, if needed
184 {
185 MutexLockerEx mu(Terminator_lock);
186 _should_terminate = true;
187 }
188
189 stop_service();
190
191 {
192 MutexLockerEx mu(Terminator_lock);
193 while (!_has_terminated) {
194 Terminator_lock->wait();
195 }
196 }
197 }
198
199 void ConcurrentG1RefineThread::stop_service() {
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "gc/g1/concurrentG1Refine.hpp"
27 #include "gc/g1/concurrentG1RefineThread.hpp"
28 #include "gc/g1/g1CollectedHeap.inline.hpp"
29 #include "gc/g1/g1CollectorPolicy.hpp"
30 #include "gc/g1/suspendibleThreadSet.hpp"
31 #include "logging/log.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "runtime/handles.inline.hpp"
34 #include "runtime/mutexLocker.hpp"
35
36 ConcurrentG1RefineThread::
37 ConcurrentG1RefineThread(ConcurrentG1Refine* cg1r, ConcurrentG1RefineThread *next,
38 CardTableEntryClosure* refine_closure,
39 uint worker_id_offset, uint worker_id) :
40 ConcurrentGCThread(),
41 _refine_closure(refine_closure),
42 _worker_id_offset(worker_id_offset),
43 _worker_id(worker_id),
44 _active(false),
45 _next(next),
46 _monitor(NULL),
47 _cg1r(cg1r),
48 _vtime_accum(0.0)
49 {
50
51 // Each thread has its own monitor. The i-th thread is responsible for signaling
72 // A thread deactivates once the number of buffer reached a deactivation threshold
73 _deactivation_threshold = MAX2<int>(_threshold - cg1r()->thread_threshold_step(), cg1r()->green_zone());
74 }
75
76 void ConcurrentG1RefineThread::wait_for_completed_buffers() {
77 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
78 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
79 while (!_should_terminate && !is_active()) {
80 _monitor->wait(Mutex::_no_safepoint_check_flag);
81 }
82 }
83
84 bool ConcurrentG1RefineThread::is_active() {
85 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
86 return is_primary() ? dcqs.process_completed_buffers() : _active;
87 }
88
89 void ConcurrentG1RefineThread::activate() {
90 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
91 if (!is_primary()) {
92 log_debug(gc, refine)("G1-Refine-activated worker %d, on threshold %d, current %d",
93 _worker_id, _threshold, JavaThread::dirty_card_queue_set().completed_buffers_num());
94 set_active(true);
95 } else {
96 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
97 dcqs.set_process_completed(true);
98 }
99 _monitor->notify();
100 }
101
102 void ConcurrentG1RefineThread::deactivate() {
103 MutexLockerEx x(_monitor, Mutex::_no_safepoint_check_flag);
104 if (!is_primary()) {
105 log_debug(gc, refine)("G1-Refine-deactivated worker %d, off threshold %d, current %d",
106 _worker_id, _deactivation_threshold, JavaThread::dirty_card_queue_set().completed_buffers_num());
107 set_active(false);
108 } else {
109 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
110 dcqs.set_process_completed(false);
111 }
112 }
113
114 void ConcurrentG1RefineThread::run() {
115 initialize_in_thread();
116 wait_for_universe_init();
117
118 run_service();
119
120 terminate();
121 }
122
123 void ConcurrentG1RefineThread::run_service() {
124 _vtime_start = os::elapsedVTime();
125
126 while (!_should_terminate) {
152
153 // Check if we need to activate the next thread.
154 if (_next != NULL && !_next->is_active() && curr_buffer_num > _next->_threshold) {
155 _next->activate();
156 }
157 } while (dcqs.apply_closure_to_completed_buffer(_refine_closure, _worker_id + _worker_id_offset, cg1r()->green_zone()));
158
159 // We can exit the loop above while being active if there was a yield request.
160 if (is_active()) {
161 deactivate();
162 }
163 }
164
165 if (os::supports_vtime()) {
166 _vtime_accum = (os::elapsedVTime() - _vtime_start);
167 } else {
168 _vtime_accum = 0.0;
169 }
170 }
171
172 log_debug(gc, refine)("G1-Refine-stop");
173 }
174
175 void ConcurrentG1RefineThread::stop() {
176 // it is ok to take late safepoints here, if needed
177 {
178 MutexLockerEx mu(Terminator_lock);
179 _should_terminate = true;
180 }
181
182 stop_service();
183
184 {
185 MutexLockerEx mu(Terminator_lock);
186 while (!_has_terminated) {
187 Terminator_lock->wait();
188 }
189 }
190 }
191
192 void ConcurrentG1RefineThread::stop_service() {
|