duke@435: /* coleenp@4037: * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/classLoader.hpp" stefank@2314: #include "classfile/javaClasses.hpp" stefank@2314: #include "gc_implementation/shared/vmGCOperations.hpp" stefank@2314: #include "memory/gcLocker.inline.hpp" stefank@2314: #include "memory/genCollectedHeap.hpp" stefank@2314: #include "memory/oopFactory.hpp" stefank@2314: #include "oops/instanceKlass.hpp" stefank@2314: #include "oops/instanceRefKlass.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "runtime/init.hpp" stefank@2314: #include "runtime/interfaceSupport.hpp" stefank@2314: #include "utilities/dtrace.hpp" stefank@2314: #include "utilities/preserveException.hpp" stefank@2314: #ifndef SERIALGC stefank@2314: #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" stefank@2314: #endif kamg@2445: dcubed@3202: #ifndef USDT2 duke@435: HS_DTRACE_PROBE_DECL1(hotspot, gc__begin, bool); duke@435: HS_DTRACE_PROBE_DECL(hotspot, gc__end); dcubed@3202: #endif /* !USDT2 */ duke@435: duke@435: // The same dtrace probe can't be inserted in two different files, so we duke@435: // have to call it here, so it's only in one file. Can't create new probes duke@435: // for the other file anymore. The dtrace probes have to remain stable. duke@435: void VM_GC_Operation::notify_gc_begin(bool full) { dcubed@3202: #ifndef USDT2 duke@435: HS_DTRACE_PROBE1(hotspot, gc__begin, full); jcoomes@1902: HS_DTRACE_WORKAROUND_TAIL_CALL_BUG(); dcubed@3202: #else /* USDT2 */ dcubed@3202: HOTSPOT_GC_BEGIN( dcubed@3202: full); dcubed@3202: #endif /* USDT2 */ duke@435: } duke@435: duke@435: void VM_GC_Operation::notify_gc_end() { dcubed@3202: #ifndef USDT2 duke@435: HS_DTRACE_PROBE(hotspot, gc__end); jcoomes@1902: HS_DTRACE_WORKAROUND_TAIL_CALL_BUG(); dcubed@3202: #else /* USDT2 */ dcubed@3202: HOTSPOT_GC_END( dcubed@3202: ); dcubed@3202: #endif /* USDT2 */ duke@435: } duke@435: duke@435: void VM_GC_Operation::acquire_pending_list_lock() { duke@435: // we may enter this with pending exception set coleenp@4047: InstanceRefKlass::acquire_pending_list_lock(&_pending_list_basic_lock); duke@435: } duke@435: duke@435: duke@435: void VM_GC_Operation::release_and_notify_pending_list_lock() { duke@435: coleenp@4047: InstanceRefKlass::release_and_notify_pending_list_lock(&_pending_list_basic_lock); duke@435: } duke@435: duke@435: // Allocations may fail in several threads at about the same time, duke@435: // resulting in multiple gc requests. We only want to do one of them. duke@435: // In case a GC locker is active and the need for a GC is already signalled, duke@435: // we want to skip this GC attempt altogether, without doing a futile duke@435: // safepoint operation. duke@435: bool VM_GC_Operation::skip_operation() const { duke@435: bool skip = (_gc_count_before != Universe::heap()->total_collections()); duke@435: if (_full && skip) { duke@435: skip = (_full_gc_count_before != Universe::heap()->total_full_collections()); duke@435: } duke@435: if (!skip && GC_locker::is_active_and_needs_gc()) { duke@435: skip = Universe::heap()->is_maximal_no_gc(); duke@435: assert(!(skip && (_gc_cause == GCCause::_gc_locker)), duke@435: "GC_locker cannot be active when initiating GC"); duke@435: } duke@435: return skip; duke@435: } duke@435: duke@435: bool VM_GC_Operation::doit_prologue() { duke@435: assert(Thread::current()->is_Java_thread(), "just checking"); brutisso@2532: assert(((_gc_cause != GCCause::_no_gc) && brutisso@2532: (_gc_cause != GCCause::_no_cause_specified)), "Illegal GCCause"); duke@435: duke@435: acquire_pending_list_lock(); duke@435: // If the GC count has changed someone beat us to the collection duke@435: // Get the Heap_lock after the pending_list_lock. duke@435: Heap_lock->lock(); ysr@777: duke@435: // Check invocations duke@435: if (skip_operation()) { duke@435: // skip collection duke@435: Heap_lock->unlock(); duke@435: release_and_notify_pending_list_lock(); duke@435: _prologue_succeeded = false; duke@435: } else { duke@435: _prologue_succeeded = true; ysr@777: SharedHeap* sh = SharedHeap::heap(); ysr@777: if (sh != NULL) sh->_thread_holds_heap_lock_for_gc = true; duke@435: } duke@435: return _prologue_succeeded; duke@435: } duke@435: duke@435: duke@435: void VM_GC_Operation::doit_epilogue() { duke@435: assert(Thread::current()->is_Java_thread(), "just checking"); duke@435: // Release the Heap_lock first. ysr@777: SharedHeap* sh = SharedHeap::heap(); ysr@777: if (sh != NULL) sh->_thread_holds_heap_lock_for_gc = false; duke@435: Heap_lock->unlock(); duke@435: release_and_notify_pending_list_lock(); duke@435: } duke@435: duke@435: bool VM_GC_HeapInspection::doit_prologue() { duke@435: if (Universe::heap()->supports_heap_inspection()) { duke@435: return VM_GC_Operation::doit_prologue(); duke@435: } else { duke@435: return false; duke@435: } duke@435: } duke@435: duke@435: bool VM_GC_HeapInspection::skip_operation() const { duke@435: assert(Universe::heap()->supports_heap_inspection(), "huh?"); duke@435: return false; duke@435: } duke@435: duke@435: void VM_GC_HeapInspection::doit() { duke@435: HandleMark hm; duke@435: CollectedHeap* ch = Universe::heap(); kevinw@1827: ch->ensure_parsability(false); // must happen, even if collection does kevinw@1827: // not happen (e.g. due to GC_locker) duke@435: if (_full_gc) { kevinw@1827: // The collection attempt below would be skipped anyway if kevinw@1827: // the gc locker is held. The following dump may then be a tad kevinw@1827: // misleading to someone expecting only live objects to show kevinw@1827: // up in the dump (see CR 6944195). Just issue a suitable warning kevinw@1827: // in that case and do not attempt to do a collection. kevinw@1827: // The latter is a subtle point, because even a failed attempt kevinw@1827: // to GC will, in fact, induce one in the future, which we kevinw@1827: // probably want to avoid in this case because the GC that we may kevinw@1827: // be about to attempt holds value for us only kevinw@1827: // if it happens now and not if it happens in the eventual kevinw@1827: // future. kevinw@1827: if (GC_locker::is_active()) { kevinw@1827: warning("GC locker is held; pre-dump GC was skipped"); kevinw@1827: } else { kevinw@1827: ch->collect_as_vm_thread(GCCause::_heap_inspection); kevinw@1827: } duke@435: } ysr@1050: HeapInspection::heap_inspection(_out, _need_prologue /* need_prologue */); duke@435: } duke@435: duke@435: duke@435: void VM_GenCollectForAllocation::doit() { kamg@2445: SvcGCMarker sgcm(SvcGCMarker::MINOR); duke@435: duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: GCCauseSetter gccs(gch, _gc_cause); duke@435: _res = gch->satisfy_failed_allocation(_size, _tlab); duke@435: assert(gch->is_in_reserved_or_null(_res), "result not in heap"); duke@435: duke@435: if (_res == NULL && GC_locker::is_active_and_needs_gc()) { duke@435: set_gc_locked(); duke@435: } duke@435: } duke@435: duke@435: void VM_GenCollectFull::doit() { kamg@2445: SvcGCMarker sgcm(SvcGCMarker::FULL); duke@435: duke@435: GenCollectedHeap* gch = GenCollectedHeap::heap(); duke@435: GCCauseSetter gccs(gch, _gc_cause); duke@435: gch->do_full_collection(gch->must_clear_all_soft_refs(), _max_level); duke@435: } apetrusenko@574: coleenp@4037: void VM_CollectForMetadataAllocation::doit() { kamg@2445: SvcGCMarker sgcm(SvcGCMarker::FULL); kamg@2445: coleenp@4037: CollectedHeap* heap = Universe::heap(); ysr@777: GCCauseSetter gccs(heap, _gc_cause); coleenp@4037: coleenp@4037: // Check again if the space is available. Another thread coleenp@4037: // may have similarly failed a metadata allocation and induced coleenp@4037: // a GC that freed space for the allocation. coleenp@4037: if (!MetadataAllocationFailALot) { coleenp@4037: _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype); ysr@777: } coleenp@4037: coleenp@4037: if (_result == NULL) { jmasa@4131: if (UseConcMarkSweepGC) { jmasa@4131: if (CMSClassUnloadingEnabled) { jmasa@4131: MetaspaceGC::set_should_concurrent_collect(true); jmasa@4131: } jmasa@4131: // For CMS expand since the collection is going to be concurrent. jmasa@4131: _result = jmasa@4131: _loader_data->metaspace_non_null()->expand_and_allocate(_size, _mdtype); jmasa@4131: } jmasa@4131: if (_result == NULL) { jmasa@4131: // Don't clear the soft refs. This GC is for reclaiming metadata jmasa@4131: // and is unrelated to the fullness of the Java heap which should jmasa@4131: // be the criteria for clearing SoftReferences. jmasa@4131: if (Verbose && PrintGCDetails && UseConcMarkSweepGC) { jmasa@4131: gclog_or_tty->print_cr("\nCMS full GC for Metaspace"); jmasa@4131: } coleenp@4037: heap->collect_as_vm_thread(GCCause::_metadata_GC_threshold); coleenp@4037: _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype); ysr@777: } jmasa@4131: if (_result == NULL && !UseConcMarkSweepGC /* CMS already tried */) { coleenp@4037: // If still failing, allow the Metaspace to expand. coleenp@4037: // See delta_capacity_until_GC() for explanation of the coleenp@4037: // amount of the expansion. coleenp@4037: // This should work unless there really is no more space coleenp@4037: // or a MaxMetaspaceSize has been specified on the command line. jmasa@4064: _result = jmasa@4064: _loader_data->metaspace_non_null()->expand_and_allocate(_size, _mdtype); jmasa@4064: jmasa@4131: } jmasa@4131: if (Verbose && PrintGCDetails && _result == NULL) { jmasa@4131: gclog_or_tty->print_cr("\nAfter Metaspace GC failed to allocate size " jmasa@4131: SIZE_FORMAT, _size); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: if (_result == NULL && GC_locker::is_active_and_needs_gc()) { apetrusenko@574: set_gc_locked(); apetrusenko@574: } apetrusenko@574: }