Wed, 25 Mar 2009 13:10:54 -0700
6543938: G1: remove the concept of popularity
Reviewed-by: iveresov, tonyp
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 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_concurrentG1RefineThread.cpp.incl"
28 // ======= Concurrent Mark Thread ========
30 // The CM thread is created when the G1 garbage collector is used
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 }
46 const long timeout = 200; // ms.
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;
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 }
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 }
79 if (_should_terminate) {
80 return;
81 }
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
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;
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();
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 }
132 sample_young_list_rs_lengths();
133 _co_tracker.update(false);
135 if (G1SmoothConcRefine) {
136 prev_buffer_num = curr_buffer_num;
137 _sts.leave();
138 os::sleep(Thread::current(), (jlong) _interval_ms, false);
139 _sts.join();
140 start_vtime_sec = os::elapsedVTime();
141 }
142 n_logs++;
143 }
144 // Make sure we harvest the PYA, if any.
145 (void)_cg1r->get_pya();
146 }
147 _sts.leave();
148 }
150 void ConcurrentG1RefineThread::sample_young_list_rs_lengths() {
151 G1CollectedHeap* g1h = G1CollectedHeap::heap();
152 G1CollectorPolicy* g1p = g1h->g1_policy();
153 if (g1p->adaptive_young_list_length()) {
154 int regions_visited = 0;
156 g1h->young_list_rs_length_sampling_init();
157 while (g1h->young_list_rs_length_sampling_more()) {
158 g1h->young_list_rs_length_sampling_next();
159 ++regions_visited;
161 // we try to yield every time we visit 10 regions
162 if (regions_visited == 10) {
163 if (_sts.should_yield()) {
164 _sts.yield("G1 refine");
165 // we just abandon the iteration
166 break;
167 }
168 regions_visited = 0;
169 }
170 }
172 g1p->check_prediction_validity();
173 }
174 }
176 void ConcurrentG1RefineThread::run() {
177 initialize_in_thread();
178 _vtime_start = os::elapsedVTime();
179 wait_for_universe_init();
181 _co_tracker.enable();
182 _co_tracker.start();
184 while (!_should_terminate) {
185 // wait until started is set.
186 if (G1RSBarrierUseQueue) {
187 queueBasedRefinement();
188 } else {
189 traversalBasedRefinement();
190 }
191 _sts.join();
192 _co_tracker.update();
193 _sts.leave();
194 if (os::supports_vtime()) {
195 _vtime_accum = (os::elapsedVTime() - _vtime_start);
196 } else {
197 _vtime_accum = 0.0;
198 }
199 }
200 _sts.join();
201 _co_tracker.update(true);
202 _sts.leave();
203 assert(_should_terminate, "just checking");
205 terminate();
206 }
209 void ConcurrentG1RefineThread::yield() {
210 if (TraceG1Refine) gclog_or_tty->print_cr("G1-Refine-yield");
211 _sts.yield("G1 refine");
212 if (TraceG1Refine) gclog_or_tty->print_cr("G1-Refine-yield-end");
213 }
215 void ConcurrentG1RefineThread::stop() {
216 // it is ok to take late safepoints here, if needed
217 {
218 MutexLockerEx mu(Terminator_lock);
219 _should_terminate = true;
220 }
222 {
223 MutexLockerEx x(DirtyCardQ_CBL_mon, Mutex::_no_safepoint_check_flag);
224 DirtyCardQ_CBL_mon->notify_all();
225 }
227 {
228 MutexLockerEx mu(Terminator_lock);
229 while (!_has_terminated) {
230 Terminator_lock->wait();
231 }
232 }
233 if (TraceG1Refine) gclog_or_tty->print_cr("G1-Refine-stop");
234 }
236 void ConcurrentG1RefineThread::print() {
237 gclog_or_tty->print("\"Concurrent G1 Refinement Thread\" ");
238 Thread::print();
239 gclog_or_tty->cr();
240 }
242 void ConcurrentG1RefineThread::set_do_traversal(bool b) {
243 _do_traversal = b;
244 }