2268 } else {
2269 // The background collector can run but check to see if the
2270 // foreground collector has done a collection while the
2271 // background collector was waiting to get the CGC_lock
2272 // above. If yes, break so that _foregroundGCShouldWait
2273 // is cleared before returning.
2274 if (_collectorState == Idling) {
2275 break;
2276 }
2277 }
2278 }
2279
2280 assert(_foregroundGCShouldWait, "Foreground collector, if active, "
2281 "should be waiting");
2282
2283 switch (_collectorState) {
2284 case InitialMarking:
2285 {
2286 ReleaseForegroundGC x(this);
2287 stats().record_cms_begin();
2288 register_gc_start(GCCause::_cms_concurrent_mark);
2289
2290 VM_CMS_Initial_Mark initial_mark_op(this);
2291 VMThread::execute(&initial_mark_op);
2292 }
2293 // The collector state may be any legal state at this point
2294 // since the background collector may have yielded to the
2295 // foreground collector.
2296 break;
2297 case Marking:
2298 // initial marking in checkpointRootsInitialWork has been completed
2299 if (markFromRoots(true)) { // we were successful
2300 assert(_collectorState == Precleaning, "Collector state should "
2301 "have changed");
2302 } else {
2303 assert(_foregroundGCIsActive, "Internal state inconsistency");
2304 }
2305 break;
2306 case Precleaning:
2307 if (UseAdaptiveSizePolicy) {
2308 size_policy()->concurrent_precleaning_begin();
2309 }
2406 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
2407 _foregroundGCShouldWait = false;
2408 if (_foregroundGCIsActive) {
2409 CGC_lock->notify();
2410 }
2411 assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(),
2412 "Possible deadlock");
2413 }
2414 if (TraceCMSState) {
2415 gclog_or_tty->print_cr("CMS Thread " INTPTR_FORMAT
2416 " exiting collection CMS state %d",
2417 Thread::current(), _collectorState);
2418 }
2419 if (PrintGC && Verbose) {
2420 _cmsGen->print_heap_change(prev_used);
2421 }
2422 }
2423
2424 void CMSCollector::register_gc_start(GCCause::Cause cause) {
2425 _cms_start_registered = true;
2426 CollectedHeap* heap = GenCollectedHeap::heap();
2427 _gc_timer_cm->register_gc_start(os::elapsed_counter());
2428 _gc_tracer_cm->report_gc_start(cause, _gc_timer_cm->gc_start());
2429 }
2430
2431 void CMSCollector::register_gc_end() {
2432 if (_cms_start_registered) {
2433 _gc_timer_cm->register_gc_end(os::elapsed_counter());
2434 _gc_tracer_cm->report_gc_end(_gc_timer_cm->gc_end(), _gc_timer_cm->time_partitions());
2435 _cms_start_registered = false;
2436 }
2437 }
2438
2439 void CMSCollector::collect_in_foreground(bool clear_all_soft_refs) {
2440 assert(_foregroundGCIsActive && !_foregroundGCShouldWait,
2441 "Foreground collector should be waiting, not executing");
2442 assert(Thread::current()->is_VM_thread(), "A foreground collection"
2443 "may only be done by the VM Thread with the world stopped");
2444 assert(ConcurrentMarkSweepThread::vm_thread_has_cms_token(),
2445 "VM thread should have CMS token");
2446
|
2268 } else {
2269 // The background collector can run but check to see if the
2270 // foreground collector has done a collection while the
2271 // background collector was waiting to get the CGC_lock
2272 // above. If yes, break so that _foregroundGCShouldWait
2273 // is cleared before returning.
2274 if (_collectorState == Idling) {
2275 break;
2276 }
2277 }
2278 }
2279
2280 assert(_foregroundGCShouldWait, "Foreground collector, if active, "
2281 "should be waiting");
2282
2283 switch (_collectorState) {
2284 case InitialMarking:
2285 {
2286 ReleaseForegroundGC x(this);
2287 stats().record_cms_begin();
2288 VM_CMS_Initial_Mark initial_mark_op(this);
2289 VMThread::execute(&initial_mark_op);
2290 }
2291 // The collector state may be any legal state at this point
2292 // since the background collector may have yielded to the
2293 // foreground collector.
2294 break;
2295 case Marking:
2296 // initial marking in checkpointRootsInitialWork has been completed
2297 if (markFromRoots(true)) { // we were successful
2298 assert(_collectorState == Precleaning, "Collector state should "
2299 "have changed");
2300 } else {
2301 assert(_foregroundGCIsActive, "Internal state inconsistency");
2302 }
2303 break;
2304 case Precleaning:
2305 if (UseAdaptiveSizePolicy) {
2306 size_policy()->concurrent_precleaning_begin();
2307 }
2404 MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
2405 _foregroundGCShouldWait = false;
2406 if (_foregroundGCIsActive) {
2407 CGC_lock->notify();
2408 }
2409 assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(),
2410 "Possible deadlock");
2411 }
2412 if (TraceCMSState) {
2413 gclog_or_tty->print_cr("CMS Thread " INTPTR_FORMAT
2414 " exiting collection CMS state %d",
2415 Thread::current(), _collectorState);
2416 }
2417 if (PrintGC && Verbose) {
2418 _cmsGen->print_heap_change(prev_used);
2419 }
2420 }
2421
2422 void CMSCollector::register_gc_start(GCCause::Cause cause) {
2423 _cms_start_registered = true;
2424 _gc_timer_cm->register_gc_start(os::elapsed_counter());
2425 _gc_tracer_cm->report_gc_start(cause, _gc_timer_cm->gc_start());
2426 }
2427
2428 void CMSCollector::register_gc_end() {
2429 if (_cms_start_registered) {
2430 _gc_timer_cm->register_gc_end(os::elapsed_counter());
2431 _gc_tracer_cm->report_gc_end(_gc_timer_cm->gc_end(), _gc_timer_cm->time_partitions());
2432 _cms_start_registered = false;
2433 }
2434 }
2435
2436 void CMSCollector::collect_in_foreground(bool clear_all_soft_refs) {
2437 assert(_foregroundGCIsActive && !_foregroundGCShouldWait,
2438 "Foreground collector should be waiting, not executing");
2439 assert(Thread::current()->is_VM_thread(), "A foreground collection"
2440 "may only be done by the VM Thread with the world stopped");
2441 assert(ConcurrentMarkSweepThread::vm_thread_has_cms_token(),
2442 "VM thread should have CMS token");
2443
|