Print this page
rev 4518 : 8013934: Garbage collection event for CMS has wrong cause for System.gc()
Reviewed-by: brutisso, jwilhelm
Split |
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.cpp
+++ new/src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.cpp
1 1 /*
2 2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 *
23 23 */
24 24
25 25 #include "precompiled.hpp"
26 26 #include "classfile/systemDictionary.hpp"
27 27 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.inline.hpp"
28 28 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
29 29 #include "memory/genCollectedHeap.hpp"
30 30 #include "oops/instanceRefKlass.hpp"
31 31 #include "oops/oop.inline.hpp"
32 32 #include "runtime/init.hpp"
33 33 #include "runtime/interfaceSupport.hpp"
34 34 #include "runtime/java.hpp"
35 35 #include "runtime/javaCalls.hpp"
36 36 #include "runtime/mutexLocker.hpp"
37 37 #include "runtime/os.hpp"
38 38 #include "runtime/vmThread.hpp"
39 39
40 40 // ======= Concurrent Mark Sweep Thread ========
41 41
42 42 // The CMS thread is created when Concurrent Mark Sweep is used in the
43 43 // older of two generations in a generational memory system.
44 44
45 45 ConcurrentMarkSweepThread*
46 46 ConcurrentMarkSweepThread::_cmst = NULL;
47 47 CMSCollector* ConcurrentMarkSweepThread::_collector = NULL;
48 48 bool ConcurrentMarkSweepThread::_should_terminate = false;
49 49 int ConcurrentMarkSweepThread::_CMS_flag = CMS_nil;
50 50
51 51 volatile jint ConcurrentMarkSweepThread::_pending_yields = 0;
52 52 volatile jint ConcurrentMarkSweepThread::_pending_decrements = 0;
53 53
54 54 volatile jint ConcurrentMarkSweepThread::_icms_disabled = 0;
55 55 volatile bool ConcurrentMarkSweepThread::_should_run = false;
56 56 // When icms is enabled, the icms thread is stopped until explicitly
57 57 // started.
58 58 volatile bool ConcurrentMarkSweepThread::_should_stop = true;
59 59
60 60 SurrogateLockerThread*
61 61 ConcurrentMarkSweepThread::_slt = NULL;
62 62 SurrogateLockerThread::SLT_msg_type
63 63 ConcurrentMarkSweepThread::_sltBuffer = SurrogateLockerThread::empty;
64 64 Monitor*
65 65 ConcurrentMarkSweepThread::_sltMonitor = NULL;
66 66
67 67 ConcurrentMarkSweepThread::ConcurrentMarkSweepThread(CMSCollector* collector)
68 68 : ConcurrentGCThread() {
69 69 assert(UseConcMarkSweepGC, "UseConcMarkSweepGC should be set");
70 70 assert(_cmst == NULL, "CMS thread already created");
71 71 _cmst = this;
72 72 assert(_collector == NULL, "Collector already set");
73 73 _collector = collector;
74 74
75 75 set_name("Concurrent Mark-Sweep GC Thread");
76 76
77 77 if (os::create_thread(this, os::cgc_thread)) {
78 78 // An old comment here said: "Priority should be just less
79 79 // than that of VMThread". Since the VMThread runs at
80 80 // NearMaxPriority, the old comment was inaccurate, but
81 81 // changing the default priority to NearMaxPriority-1
82 82 // could change current behavior, so the default of
83 83 // NearMaxPriority stays in place.
84 84 //
85 85 // Note that there's a possibility of the VMThread
86 86 // starving if UseCriticalCMSThreadPriority is on.
87 87 // That won't happen on Solaris for various reasons,
88 88 // but may well happen on non-Solaris platforms.
89 89 int native_prio;
90 90 if (UseCriticalCMSThreadPriority) {
91 91 native_prio = os::java_to_os_priority[CriticalPriority];
92 92 } else {
93 93 native_prio = os::java_to_os_priority[NearMaxPriority];
94 94 }
95 95 os::set_native_priority(this, native_prio);
96 96
97 97 if (!DisableStartThread) {
98 98 os::start_thread(this);
99 99 }
100 100 }
101 101 _sltMonitor = SLT_lock;
102 102 assert(!CMSIncrementalMode || icms_is_enabled(), "Error");
103 103 }
104 104
105 105 void ConcurrentMarkSweepThread::run() {
106 106 assert(this == cmst(), "just checking");
107 107
108 108 this->record_stack_base_and_size();
109 109 this->initialize_thread_local_storage();
110 110 this->set_active_handles(JNIHandleBlock::allocate_block());
111 111 // From this time Thread::current() should be working.
112 112 assert(this == Thread::current(), "just checking");
113 113 if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) {
114 114 warning("Couldn't bind CMS thread to processor %u", CPUForCMSThread);
115 115 }
116 116 // Wait until Universe::is_fully_initialized()
117 117 {
118 118 CMSLoopCountWarn loopX("CMS::run", "waiting for "
119 119 "Universe::is_fully_initialized()", 2);
120 120 MutexLockerEx x(CGC_lock, true);
121 121 set_CMS_flag(CMS_cms_wants_token);
122 122 // Wait until Universe is initialized and all initialization is completed.
123 123 while (!is_init_completed() && !Universe::is_fully_initialized() &&
124 124 !_should_terminate) {
125 125 CGC_lock->wait(true, 200);
126 126 loopX.tick();
127 127 }
128 128 // Wait until the surrogate locker thread that will do
129 129 // pending list locking on our behalf has been created.
130 130 // We cannot start the SLT thread ourselves since we need
131 131 // to be a JavaThread to do so.
132 132 CMSLoopCountWarn loopY("CMS::run", "waiting for SLT installation", 2);
↓ open down ↓ |
132 lines elided |
↑ open up ↑ |
133 133 while (_slt == NULL && !_should_terminate) {
134 134 CGC_lock->wait(true, 200);
135 135 loopY.tick();
136 136 }
137 137 clear_CMS_flag(CMS_cms_wants_token);
138 138 }
139 139
140 140 while (!_should_terminate) {
141 141 sleepBeforeNextCycle();
142 142 if (_should_terminate) break;
143 - _collector->collect_in_background(false); // !clear_all_soft_refs
143 + GCCause::Cause cause = _collector->_full_gc_requested ?
144 + _collector->_full_gc_cause : GCCause::_cms_concurrent_mark;
145 + _collector->collect_in_background(false, cause);
144 146 }
145 147 assert(_should_terminate, "just checking");
146 148 // Check that the state of any protocol for synchronization
147 149 // between background (CMS) and foreground collector is "clean"
148 150 // (i.e. will not potentially block the foreground collector,
149 151 // requiring action by us).
150 152 verify_ok_to_terminate();
151 153 // Signal that it is terminated
152 154 {
153 155 MutexLockerEx mu(Terminator_lock,
154 156 Mutex::_no_safepoint_check_flag);
155 157 assert(_cmst == this, "Weird!");
156 158 _cmst = NULL;
157 159 Terminator_lock->notify();
158 160 }
159 161
160 162 // Thread destructor usually does this..
161 163 ThreadLocalStorage::set_thread(NULL);
162 164 }
163 165
164 166 #ifndef PRODUCT
165 167 void ConcurrentMarkSweepThread::verify_ok_to_terminate() const {
166 168 assert(!(CGC_lock->owned_by_self() || cms_thread_has_cms_token() ||
167 169 cms_thread_wants_cms_token()),
168 170 "Must renounce all worldly possessions and desires for nirvana");
169 171 _collector->verify_ok_to_terminate();
170 172 }
171 173 #endif
172 174
173 175 // create and start a new ConcurrentMarkSweep Thread for given CMS generation
174 176 ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) {
175 177 if (!_should_terminate) {
176 178 assert(cmst() == NULL, "start() called twice?");
177 179 ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector);
178 180 assert(cmst() == th, "Where did the just-created CMS thread go?");
179 181 return th;
180 182 }
181 183 return NULL;
182 184 }
183 185
184 186 void ConcurrentMarkSweepThread::stop() {
185 187 if (CMSIncrementalMode) {
186 188 // Disable incremental mode and wake up the thread so it notices the change.
187 189 disable_icms();
188 190 start_icms();
189 191 }
190 192 // it is ok to take late safepoints here, if needed
191 193 {
192 194 MutexLockerEx x(Terminator_lock);
193 195 _should_terminate = true;
194 196 }
195 197 { // Now post a notify on CGC_lock so as to nudge
196 198 // CMS thread(s) that might be slumbering in
197 199 // sleepBeforeNextCycle.
198 200 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
199 201 CGC_lock->notify_all();
200 202 }
201 203 { // Now wait until (all) CMS thread(s) have exited
202 204 MutexLockerEx x(Terminator_lock);
203 205 while(cmst() != NULL) {
204 206 Terminator_lock->wait();
205 207 }
206 208 }
207 209 }
208 210
209 211 void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) {
210 212 assert(tc != NULL, "Null ThreadClosure");
211 213 if (_cmst != NULL) {
212 214 tc->do_thread(_cmst);
213 215 }
214 216 assert(Universe::is_fully_initialized(),
215 217 "Called too early, make sure heap is fully initialized");
216 218 if (_collector != NULL) {
217 219 AbstractWorkGang* gang = _collector->conc_workers();
218 220 if (gang != NULL) {
219 221 gang->threads_do(tc);
220 222 }
221 223 }
222 224 }
223 225
224 226 void ConcurrentMarkSweepThread::print_on(outputStream* st) const {
225 227 st->print("\"%s\" ", name());
226 228 Thread::print_on(st);
227 229 st->cr();
228 230 }
229 231
230 232 void ConcurrentMarkSweepThread::print_all_on(outputStream* st) {
231 233 if (_cmst != NULL) {
232 234 _cmst->print_on(st);
233 235 }
234 236 if (_collector != NULL) {
235 237 AbstractWorkGang* gang = _collector->conc_workers();
236 238 if (gang != NULL) {
237 239 gang->print_worker_threads_on(st);
238 240 }
239 241 }
240 242 }
241 243
242 244 void ConcurrentMarkSweepThread::synchronize(bool is_cms_thread) {
243 245 assert(UseConcMarkSweepGC, "just checking");
244 246
245 247 MutexLockerEx x(CGC_lock,
246 248 Mutex::_no_safepoint_check_flag);
247 249 if (!is_cms_thread) {
248 250 assert(Thread::current()->is_VM_thread(), "Not a VM thread");
249 251 CMSSynchronousYieldRequest yr;
250 252 while (CMS_flag_is_set(CMS_cms_has_token)) {
251 253 // indicate that we want to get the token
252 254 set_CMS_flag(CMS_vm_wants_token);
253 255 CGC_lock->wait(true);
254 256 }
255 257 // claim the token and proceed
256 258 clear_CMS_flag(CMS_vm_wants_token);
257 259 set_CMS_flag(CMS_vm_has_token);
258 260 } else {
259 261 assert(Thread::current()->is_ConcurrentGC_thread(),
260 262 "Not a CMS thread");
261 263 // The following barrier assumes there's only one CMS thread.
262 264 // This will need to be modified is there are more CMS threads than one.
263 265 while (CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token)) {
264 266 set_CMS_flag(CMS_cms_wants_token);
265 267 CGC_lock->wait(true);
266 268 }
267 269 // claim the token
268 270 clear_CMS_flag(CMS_cms_wants_token);
269 271 set_CMS_flag(CMS_cms_has_token);
270 272 }
271 273 }
272 274
273 275 void ConcurrentMarkSweepThread::desynchronize(bool is_cms_thread) {
274 276 assert(UseConcMarkSweepGC, "just checking");
275 277
276 278 MutexLockerEx x(CGC_lock,
277 279 Mutex::_no_safepoint_check_flag);
278 280 if (!is_cms_thread) {
279 281 assert(Thread::current()->is_VM_thread(), "Not a VM thread");
280 282 assert(CMS_flag_is_set(CMS_vm_has_token), "just checking");
281 283 clear_CMS_flag(CMS_vm_has_token);
282 284 if (CMS_flag_is_set(CMS_cms_wants_token)) {
283 285 // wake-up a waiting CMS thread
284 286 CGC_lock->notify();
285 287 }
286 288 assert(!CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token),
287 289 "Should have been cleared");
288 290 } else {
289 291 assert(Thread::current()->is_ConcurrentGC_thread(),
290 292 "Not a CMS thread");
291 293 assert(CMS_flag_is_set(CMS_cms_has_token), "just checking");
292 294 clear_CMS_flag(CMS_cms_has_token);
293 295 if (CMS_flag_is_set(CMS_vm_wants_token)) {
294 296 // wake-up a waiting VM thread
295 297 CGC_lock->notify();
296 298 }
297 299 assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token),
298 300 "Should have been cleared");
299 301 }
300 302 }
301 303
302 304 // Wait until the next synchronous GC, a concurrent full gc request,
303 305 // or a timeout, whichever is earlier.
304 306 void ConcurrentMarkSweepThread::wait_on_cms_lock(long t_millis) {
305 307 MutexLockerEx x(CGC_lock,
306 308 Mutex::_no_safepoint_check_flag);
307 309 if (_should_terminate || _collector->_full_gc_requested) {
308 310 return;
309 311 }
310 312 set_CMS_flag(CMS_cms_wants_token); // to provoke notifies
311 313 CGC_lock->wait(Mutex::_no_safepoint_check_flag, t_millis);
312 314 clear_CMS_flag(CMS_cms_wants_token);
313 315 assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token),
314 316 "Should not be set");
315 317 }
316 318
317 319 void ConcurrentMarkSweepThread::sleepBeforeNextCycle() {
318 320 while (!_should_terminate) {
319 321 if (CMSIncrementalMode) {
320 322 icms_wait();
321 323 return;
322 324 } else {
323 325 // Wait until the next synchronous GC, a concurrent full gc
324 326 // request or a timeout, whichever is earlier.
325 327 wait_on_cms_lock(CMSWaitDuration);
326 328 }
327 329 // Check if we should start a CMS collection cycle
328 330 if (_collector->shouldConcurrentCollect()) {
329 331 return;
330 332 }
331 333 // .. collection criterion not yet met, let's go back
332 334 // and wait some more
333 335 }
334 336 }
335 337
336 338 // Incremental CMS
337 339 void ConcurrentMarkSweepThread::start_icms() {
338 340 assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
339 341 MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
340 342 trace_state("start_icms");
341 343 _should_run = true;
342 344 iCMS_lock->notify_all();
343 345 }
344 346
345 347 void ConcurrentMarkSweepThread::stop_icms() {
346 348 assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
347 349 MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
348 350 if (!_should_stop) {
349 351 trace_state("stop_icms");
350 352 _should_stop = true;
351 353 _should_run = false;
352 354 asynchronous_yield_request();
353 355 iCMS_lock->notify_all();
354 356 }
355 357 }
356 358
357 359 void ConcurrentMarkSweepThread::icms_wait() {
358 360 assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking");
359 361 if (_should_stop && icms_is_enabled()) {
360 362 MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag);
361 363 trace_state("pause_icms");
362 364 _collector->stats().stop_cms_timer();
363 365 while(!_should_run && icms_is_enabled()) {
364 366 iCMS_lock->wait(Mutex::_no_safepoint_check_flag);
365 367 }
366 368 _collector->stats().start_cms_timer();
367 369 _should_stop = false;
368 370 trace_state("pause_icms end");
369 371 }
370 372 }
371 373
372 374 // Note: this method, although exported by the ConcurrentMarkSweepThread,
373 375 // which is a non-JavaThread, can only be called by a JavaThread.
374 376 // Currently this is done at vm creation time (post-vm-init) by the
375 377 // main/Primordial (Java)Thread.
376 378 // XXX Consider changing this in the future to allow the CMS thread
377 379 // itself to create this thread?
378 380 void ConcurrentMarkSweepThread::makeSurrogateLockerThread(TRAPS) {
379 381 assert(UseConcMarkSweepGC, "SLT thread needed only for CMS GC");
380 382 assert(_slt == NULL, "SLT already created");
381 383 _slt = SurrogateLockerThread::make(THREAD);
382 384 }
↓ open down ↓ |
229 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX