Fri, 02 Oct 2009 16:12:07 -0400
6885041: G1: inconsistent thread dump
Summary: When G1 is enabled, thread dumps are inconsistent as the info for some of the G1 threads is not formatted properly.
Reviewed-by: ysr, johnc
1 /*
2 * Copyright 2001-2009 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, ConcurrentG1RefineThread *next,
34 int worker_id_offset, int worker_id) :
35 ConcurrentGCThread(),
36 _worker_id_offset(worker_id_offset),
37 _worker_id(worker_id),
38 _active(false),
39 _next(next),
40 _cg1r(cg1r),
41 _vtime_accum(0.0),
42 _interval_ms(5.0)
43 {
44 create_and_start();
45 }
47 void ConcurrentG1RefineThread::sample_young_list_rs_lengths() {
48 G1CollectedHeap* g1h = G1CollectedHeap::heap();
49 G1CollectorPolicy* g1p = g1h->g1_policy();
50 if (g1p->adaptive_young_list_length()) {
51 int regions_visited = 0;
53 g1h->young_list_rs_length_sampling_init();
54 while (g1h->young_list_rs_length_sampling_more()) {
55 g1h->young_list_rs_length_sampling_next();
56 ++regions_visited;
58 // we try to yield every time we visit 10 regions
59 if (regions_visited == 10) {
60 if (_sts.should_yield()) {
61 _sts.yield("G1 refine");
62 // we just abandon the iteration
63 break;
64 }
65 regions_visited = 0;
66 }
67 }
69 g1p->check_prediction_validity();
70 }
71 }
73 void ConcurrentG1RefineThread::run() {
74 initialize_in_thread();
75 _vtime_start = os::elapsedVTime();
76 wait_for_universe_init();
78 while (!_should_terminate) {
79 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
80 // Wait for completed log buffers to exist.
81 {
82 MutexLockerEx x(DirtyCardQ_CBL_mon, Mutex::_no_safepoint_check_flag);
83 while (((_worker_id == 0 && !dcqs.process_completed_buffers()) ||
84 (_worker_id > 0 && !is_active())) &&
85 !_should_terminate) {
86 DirtyCardQ_CBL_mon->wait(Mutex::_no_safepoint_check_flag);
87 }
88 }
90 if (_should_terminate) {
91 return;
92 }
94 // Now we take them off (this doesn't hold locks while it applies
95 // closures.) (If we did a full collection, then we'll do a full
96 // traversal.
97 _sts.join();
98 int n_logs = 0;
99 int lower_limit = 0;
100 double start_vtime_sec; // only used when G1SmoothConcRefine is on
101 int prev_buffer_num; // only used when G1SmoothConcRefine is on
102 // This thread activation threshold
103 int threshold = G1UpdateBufferQueueProcessingThreshold * _worker_id;
104 // Next thread activation threshold
105 int next_threshold = threshold + G1UpdateBufferQueueProcessingThreshold;
106 int deactivation_threshold = MAX2<int>(threshold - G1UpdateBufferQueueProcessingThreshold / 2, 0);
108 if (G1SmoothConcRefine) {
109 lower_limit = 0;
110 start_vtime_sec = os::elapsedVTime();
111 prev_buffer_num = (int) dcqs.completed_buffers_num();
112 } else {
113 lower_limit = G1UpdateBufferQueueProcessingThreshold / 4; // For now.
114 }
115 while (dcqs.apply_closure_to_completed_buffer(_worker_id + _worker_id_offset, lower_limit)) {
116 double end_vtime_sec;
117 double elapsed_vtime_sec;
118 int elapsed_vtime_ms;
119 int curr_buffer_num = (int) dcqs.completed_buffers_num();
121 if (G1SmoothConcRefine) {
122 end_vtime_sec = os::elapsedVTime();
123 elapsed_vtime_sec = end_vtime_sec - start_vtime_sec;
124 elapsed_vtime_ms = (int) (elapsed_vtime_sec * 1000.0);
126 if (curr_buffer_num > prev_buffer_num ||
127 curr_buffer_num > next_threshold) {
128 decreaseInterval(elapsed_vtime_ms);
129 } else if (curr_buffer_num < prev_buffer_num) {
130 increaseInterval(elapsed_vtime_ms);
131 }
132 }
133 if (_worker_id == 0) {
134 sample_young_list_rs_lengths();
135 } else if (curr_buffer_num < deactivation_threshold) {
136 // If the number of the buffer has fallen below our threshold
137 // we should deactivate. The predecessor will reactivate this
138 // thread should the number of the buffers cross the threshold again.
139 MutexLockerEx x(DirtyCardQ_CBL_mon, Mutex::_no_safepoint_check_flag);
140 deactivate();
141 if (G1TraceConcurrentRefinement) {
142 gclog_or_tty->print_cr("G1-Refine-deactivated worker %d", _worker_id);
143 }
144 break;
145 }
147 // Check if we need to activate the next thread.
148 if (curr_buffer_num > next_threshold && _next != NULL && !_next->is_active()) {
149 MutexLockerEx x(DirtyCardQ_CBL_mon, Mutex::_no_safepoint_check_flag);
150 _next->activate();
151 DirtyCardQ_CBL_mon->notify_all();
152 if (G1TraceConcurrentRefinement) {
153 gclog_or_tty->print_cr("G1-Refine-activated worker %d", _next->_worker_id);
154 }
155 }
157 if (G1SmoothConcRefine) {
158 prev_buffer_num = curr_buffer_num;
159 _sts.leave();
160 os::sleep(Thread::current(), (jlong) _interval_ms, false);
161 _sts.join();
162 start_vtime_sec = os::elapsedVTime();
163 }
164 n_logs++;
165 }
166 _sts.leave();
168 if (os::supports_vtime()) {
169 _vtime_accum = (os::elapsedVTime() - _vtime_start);
170 } else {
171 _vtime_accum = 0.0;
172 }
173 }
174 assert(_should_terminate, "just checking");
176 terminate();
177 }
180 void ConcurrentG1RefineThread::yield() {
181 if (G1TraceConcurrentRefinement) gclog_or_tty->print_cr("G1-Refine-yield");
182 _sts.yield("G1 refine");
183 if (G1TraceConcurrentRefinement) gclog_or_tty->print_cr("G1-Refine-yield-end");
184 }
186 void ConcurrentG1RefineThread::stop() {
187 // it is ok to take late safepoints here, if needed
188 {
189 MutexLockerEx mu(Terminator_lock);
190 _should_terminate = true;
191 }
193 {
194 MutexLockerEx x(DirtyCardQ_CBL_mon, Mutex::_no_safepoint_check_flag);
195 DirtyCardQ_CBL_mon->notify_all();
196 }
198 {
199 MutexLockerEx mu(Terminator_lock);
200 while (!_has_terminated) {
201 Terminator_lock->wait();
202 }
203 }
204 if (G1TraceConcurrentRefinement) gclog_or_tty->print_cr("G1-Refine-stop");
205 }
207 void ConcurrentG1RefineThread::print() const {
208 print_on(tty);
209 }
211 void ConcurrentG1RefineThread::print_on(outputStream* st) const {
212 st->print("\"G1 Concurrent Refinement Thread#%d\" ", _worker_id);
213 Thread::print_on(st);
214 st->cr();
215 }