comparison src/share/vm/gc_implementation/g1/concurrentG1RefineThread.cpp @ 342:37f87013dfd8

6711316: Open source the Garbage-First garbage collector Summary: First mercurial integration of the code for the Garbage-First garbage collector. Reviewed-by: apetrusenko, iveresov, jmasa, sgoldman, tonyp, ysr
author ysr
date Thu, 05 Jun 2008 15:57:56 -0700
parents
children 4f360ec815ba
comparison
equal deleted inserted replaced
189:0b27f3512f9e 342:37f87013dfd8
1 /*
2 * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 #include "incls/_precompiled.incl"
26 #include "incls/_concurrentG1RefineThread.cpp.incl"
27
28 // ======= Concurrent Mark Thread ========
29
30 // The CM thread is created when the G1 garbage collector is used
31
32 ConcurrentG1RefineThread::
33 ConcurrentG1RefineThread(ConcurrentG1Refine* cg1r) :
34 ConcurrentGCThread(),
35 _cg1r(cg1r),
36 _started(false),
37 _in_progress(false),
38 _do_traversal(false),
39 _vtime_accum(0.0),
40 _co_tracker(G1CRGroup),
41 _interval_ms(5.0)
42 {
43 create_and_start();
44 }
45
46 const long timeout = 200; // ms.
47
48 void ConcurrentG1RefineThread::traversalBasedRefinement() {
49 _cg1r->wait_for_ConcurrentG1Refine_enabled();
50 MutexLocker x(G1ConcRefine_mon);
51 while (_cg1r->enabled()) {
52 MutexUnlocker ux(G1ConcRefine_mon);
53 ResourceMark rm;
54 HandleMark hm;
55
56 if (TraceG1Refine) gclog_or_tty->print_cr("G1-Refine starting pass");
57 _sts.join();
58 bool no_sleep = _cg1r->refine();
59 _sts.leave();
60 if (!no_sleep) {
61 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
62 // We do this only for the timeout; we don't expect this to be signalled.
63 CGC_lock->wait(Mutex::_no_safepoint_check_flag, timeout);
64 }
65 }
66 }
67
68 void ConcurrentG1RefineThread::queueBasedRefinement() {
69 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
70 // Wait for completed log buffers to exist.
71 {
72 MutexLockerEx x(DirtyCardQ_CBL_mon, Mutex::_no_safepoint_check_flag);
73 while (!_do_traversal && !dcqs.process_completed_buffers() &&
74 !_should_terminate) {
75 DirtyCardQ_CBL_mon->wait(Mutex::_no_safepoint_check_flag);
76 }
77 }
78
79 if (_should_terminate) {
80 return;
81 }
82
83 // Now we take them off (this doesn't hold locks while it applies
84 // closures.) (If we did a full collection, then we'll do a full
85 // traversal.
86 _sts.join();
87 if (_do_traversal) {
88 (void)_cg1r->refine();
89 switch (_cg1r->get_last_pya()) {
90 case PYA_cancel: case PYA_continue:
91 // Continue was caught and handled inside "refine". If it's still
92 // "continue" when we get here, we're done.
93 _do_traversal = false;
94 break;
95 case PYA_restart:
96 assert(_do_traversal, "Because of Full GC.");
97 break;
98 }
99 } else {
100 int n_logs = 0;
101 int lower_limit = 0;
102 double start_vtime_sec; // only used when G1SmoothConcRefine is on
103 int prev_buffer_num; // only used when G1SmoothConcRefine is on
104
105 if (G1SmoothConcRefine) {
106 lower_limit = 0;
107 start_vtime_sec = os::elapsedVTime();
108 prev_buffer_num = (int) dcqs.completed_buffers_num();
109 } else {
110 lower_limit = DCQBarrierProcessCompletedThreshold / 4; // For now.
111 }
112 while (dcqs.apply_closure_to_completed_buffer(0, lower_limit)) {
113 double end_vtime_sec;
114 double elapsed_vtime_sec;
115 int elapsed_vtime_ms;
116 int curr_buffer_num;
117
118 if (G1SmoothConcRefine) {
119 end_vtime_sec = os::elapsedVTime();
120 elapsed_vtime_sec = end_vtime_sec - start_vtime_sec;
121 elapsed_vtime_ms = (int) (elapsed_vtime_sec * 1000.0);
122 curr_buffer_num = (int) dcqs.completed_buffers_num();
123
124 if (curr_buffer_num > prev_buffer_num ||
125 curr_buffer_num > DCQBarrierProcessCompletedThreshold) {
126 decreaseInterval(elapsed_vtime_ms);
127 } else if (curr_buffer_num < prev_buffer_num) {
128 increaseInterval(elapsed_vtime_ms);
129 }
130 }
131
132 sample_young_list_rs_lengths();
133 _co_tracker.update(false);
134
135 if (G1SmoothConcRefine) {
136 start_vtime_sec = os::elapsedVTime();
137 prev_buffer_num = curr_buffer_num;
138
139 _sts.leave();
140 os::sleep(Thread::current(), (jlong) _interval_ms, false);
141 _sts.join();
142 }
143
144 n_logs++;
145 }
146 // Make sure we harvest the PYA, if any.
147 (void)_cg1r->get_pya();
148 }
149 _sts.leave();
150 }
151
152 void ConcurrentG1RefineThread::sample_young_list_rs_lengths() {
153 G1CollectedHeap* g1h = G1CollectedHeap::heap();
154 G1CollectorPolicy* g1p = g1h->g1_policy();
155 if (g1p->adaptive_young_list_length()) {
156 int regions_visited = 0;
157
158 g1h->young_list_rs_length_sampling_init();
159 while (g1h->young_list_rs_length_sampling_more()) {
160 g1h->young_list_rs_length_sampling_next();
161 ++regions_visited;
162
163 // we try to yield every time we visit 10 regions
164 if (regions_visited == 10) {
165 if (_sts.should_yield()) {
166 _sts.yield("G1 refine");
167 // we just abandon the iteration
168 break;
169 }
170 regions_visited = 0;
171 }
172 }
173
174 g1p->check_prediction_validity();
175 }
176 }
177
178 void ConcurrentG1RefineThread::run() {
179 initialize_in_thread();
180 _vtime_start = os::elapsedVTime();
181 wait_for_universe_init();
182
183 _co_tracker.enable();
184 _co_tracker.start();
185
186 while (!_should_terminate) {
187 // wait until started is set.
188 if (G1RSBarrierUseQueue) {
189 queueBasedRefinement();
190 } else {
191 traversalBasedRefinement();
192 }
193 _sts.join();
194 _co_tracker.update();
195 _sts.leave();
196 if (os::supports_vtime()) {
197 _vtime_accum = (os::elapsedVTime() - _vtime_start);
198 } else {
199 _vtime_accum = 0.0;
200 }
201 }
202 _sts.join();
203 _co_tracker.update(true);
204 _sts.leave();
205 assert(_should_terminate, "just checking");
206
207 terminate();
208 }
209
210
211 void ConcurrentG1RefineThread::yield() {
212 if (TraceG1Refine) gclog_or_tty->print_cr("G1-Refine-yield");
213 _sts.yield("G1 refine");
214 if (TraceG1Refine) gclog_or_tty->print_cr("G1-Refine-yield-end");
215 }
216
217 void ConcurrentG1RefineThread::stop() {
218 // it is ok to take late safepoints here, if needed
219 {
220 MutexLockerEx mu(Terminator_lock);
221 _should_terminate = true;
222 }
223
224 {
225 MutexLockerEx x(DirtyCardQ_CBL_mon, Mutex::_no_safepoint_check_flag);
226 DirtyCardQ_CBL_mon->notify_all();
227 }
228
229 {
230 MutexLockerEx mu(Terminator_lock);
231 while (!_has_terminated) {
232 Terminator_lock->wait();
233 }
234 }
235 if (TraceG1Refine) gclog_or_tty->print_cr("G1-Refine-stop");
236 }
237
238 void ConcurrentG1RefineThread::print() {
239 gclog_or_tty->print("\"Concurrent G1 Refinement Thread\" ");
240 Thread::print();
241 gclog_or_tty->cr();
242 }
243
244 void ConcurrentG1RefineThread::set_do_traversal(bool b) {
245 _do_traversal = b;
246 }