ysr@777: /* ysr@777: * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * ysr@777: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, ysr@777: * CA 95054 USA or visit www.sun.com if you need additional information or ysr@777: * have any questions. ysr@777: * ysr@777: */ ysr@777: ysr@777: #include "incls/_precompiled.incl" ysr@777: #include "incls/_concurrentZFThread.cpp.incl" ysr@777: ysr@777: // ======= Concurrent Zero-Fill Thread ======== ysr@777: ysr@777: // The CM thread is created when the G1 garbage collector is used ysr@777: ysr@777: int ConcurrentZFThread::_region_allocs = 0; ysr@777: int ConcurrentZFThread::_sync_zfs = 0; ysr@777: int ConcurrentZFThread::_zf_waits = 0; ysr@777: int ConcurrentZFThread::_regions_filled = 0; ysr@777: ysr@777: ConcurrentZFThread::ConcurrentZFThread() : ysr@777: ConcurrentGCThread(), ysr@777: _co_tracker(G1ZFGroup) ysr@777: { ysr@777: create_and_start(); ysr@777: } ysr@777: ysr@777: void ConcurrentZFThread::wait_for_ZF_completed(HeapRegion* hr) { ysr@777: assert(ZF_mon->owned_by_self(), "Precondition."); ysr@777: note_zf_wait(); ysr@777: while (hr->zero_fill_state() == HeapRegion::ZeroFilling) { ysr@777: ZF_mon->wait(Mutex::_no_safepoint_check_flag); ysr@777: } ysr@777: } ysr@777: ysr@777: void ConcurrentZFThread::processHeapRegion(HeapRegion* hr) { ysr@777: assert(!Universe::heap()->is_gc_active(), ysr@777: "This should not happen during GC."); ysr@777: assert(hr != NULL, "Precondition"); ysr@777: // These are unlocked reads, but if this test is successful, then no ysr@777: // other thread will attempt this zero filling. Only a GC thread can ysr@777: // modify the ZF state of a region whose state is zero-filling, and this ysr@777: // should only happen while the ZF thread is locking out GC. ysr@777: if (hr->zero_fill_state() == HeapRegion::ZeroFilling ysr@777: && hr->zero_filler() == Thread::current()) { ysr@777: assert(hr->top() == hr->bottom(), "better be empty!"); ysr@777: assert(!hr->isHumongous(), "Only free regions on unclean list."); ysr@777: Copy::fill_to_words(hr->bottom(), hr->capacity()/HeapWordSize); ysr@777: note_region_filled(); ysr@777: } ysr@777: } ysr@777: ysr@777: void ConcurrentZFThread::run() { ysr@777: initialize_in_thread(); ysr@777: Thread* thr_self = Thread::current(); ysr@777: _vtime_start = os::elapsedVTime(); ysr@777: wait_for_universe_init(); ysr@777: _co_tracker.enable(); ysr@777: _co_tracker.start(); ysr@777: ysr@777: G1CollectedHeap* g1 = G1CollectedHeap::heap(); ysr@777: _sts.join(); ysr@777: while (!_should_terminate) { ysr@777: _sts.leave(); ysr@777: ysr@777: { ysr@777: MutexLockerEx x(ZF_mon, Mutex::_no_safepoint_check_flag); ysr@777: ysr@777: // This local variable will hold a region being zero-filled. This ysr@777: // region will neither be on the unclean or zero-filled lists, and ysr@777: // will not be available for allocation; thus, we might have an ysr@777: // allocation fail, causing a full GC, because of this, but this is a ysr@777: // price we will pay. (In future, we might want to make the fact ysr@777: // that there's a region being zero-filled apparent to the G1 heap, ysr@777: // which could then wait for it in this extreme case...) ysr@777: HeapRegion* to_fill; ysr@777: ysr@777: while (!g1->should_zf() ysr@777: || (to_fill = g1->pop_unclean_region_list_locked()) == NULL) ysr@777: ZF_mon->wait(Mutex::_no_safepoint_check_flag); ysr@777: while (to_fill->zero_fill_state() == HeapRegion::ZeroFilling) ysr@777: ZF_mon->wait(Mutex::_no_safepoint_check_flag); ysr@777: ysr@777: // So now to_fill is non-NULL and is not ZeroFilling. It might be ysr@777: // Allocated or ZeroFilled. (The latter could happen if this thread ysr@777: // starts the zero-filling of a region, but a GC intervenes and ysr@777: // pushes new regions needing on the front of the filling on the ysr@777: // front of the list.) ysr@777: ysr@777: switch (to_fill->zero_fill_state()) { ysr@777: case HeapRegion::Allocated: ysr@777: to_fill = NULL; ysr@777: break; ysr@777: ysr@777: case HeapRegion::NotZeroFilled: ysr@777: to_fill->set_zero_fill_in_progress(thr_self); ysr@777: ysr@777: ZF_mon->unlock(); ysr@777: _sts.join(); ysr@777: processHeapRegion(to_fill); ysr@777: _sts.leave(); ysr@777: ZF_mon->lock_without_safepoint_check(); ysr@777: ysr@777: if (to_fill->zero_fill_state() == HeapRegion::ZeroFilling ysr@777: && to_fill->zero_filler() == thr_self) { ysr@777: to_fill->set_zero_fill_complete(); ysr@777: (void)g1->put_free_region_on_list_locked(to_fill); ysr@777: } ysr@777: break; ysr@777: ysr@777: case HeapRegion::ZeroFilled: ysr@777: (void)g1->put_free_region_on_list_locked(to_fill); ysr@777: break; ysr@777: ysr@777: case HeapRegion::ZeroFilling: ysr@777: ShouldNotReachHere(); ysr@777: break; ysr@777: } ysr@777: } ysr@777: _vtime_accum = (os::elapsedVTime() - _vtime_start); ysr@777: _sts.join(); ysr@777: ysr@777: _co_tracker.update(); ysr@777: } ysr@777: _co_tracker.update(false); ysr@777: _sts.leave(); ysr@777: ysr@777: assert(_should_terminate, "just checking"); ysr@777: terminate(); ysr@777: } ysr@777: ysr@777: bool ConcurrentZFThread::offer_yield() { ysr@777: if (_sts.should_yield()) { ysr@777: _sts.yield("Concurrent ZF"); ysr@777: return true; ysr@777: } else { ysr@777: return false; ysr@777: } ysr@777: } ysr@777: ysr@777: void ConcurrentZFThread::stop() { ysr@777: // it is ok to take late safepoints here, if needed ysr@777: MutexLockerEx mu(Terminator_lock); ysr@777: _should_terminate = true; ysr@777: while (!_has_terminated) { ysr@777: Terminator_lock->wait(); ysr@777: } ysr@777: } ysr@777: ysr@777: void ConcurrentZFThread::print() { ysr@777: gclog_or_tty->print("\"Concurrent ZF Thread\" "); ysr@777: Thread::print(); ysr@777: gclog_or_tty->cr(); ysr@777: } ysr@777: ysr@777: ysr@777: double ConcurrentZFThread::_vtime_accum; ysr@777: ysr@777: void ConcurrentZFThread::print_summary_info() { ysr@777: gclog_or_tty->print("\nConcurrent Zero-Filling:\n"); ysr@777: gclog_or_tty->print(" Filled %d regions, used %5.2fs.\n", ysr@777: _regions_filled, ysr@777: vtime_accum()); ysr@777: gclog_or_tty->print(" Of %d region allocs, %d (%5.2f%%) required sync ZF,\n", ysr@777: _region_allocs, _sync_zfs, ysr@777: (_region_allocs > 0 ? ysr@777: (float)_sync_zfs/(float)_region_allocs*100.0 : ysr@777: 0.0)); ysr@777: gclog_or_tty->print(" and %d (%5.2f%%) required a ZF wait.\n", ysr@777: _zf_waits, ysr@777: (_region_allocs > 0 ? ysr@777: (float)_zf_waits/(float)_region_allocs*100.0 : ysr@777: 0.0)); ysr@777: ysr@777: }