aoqi@0: /* mlarsson@7686: * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1COLLECTEDHEAP_INLINE_HPP aoqi@0: #define SHARE_VM_GC_IMPLEMENTATION_G1_G1COLLECTEDHEAP_INLINE_HPP aoqi@0: aoqi@0: #include "gc_implementation/g1/concurrentMark.hpp" aoqi@0: #include "gc_implementation/g1/g1CollectedHeap.hpp" aoqi@0: #include "gc_implementation/g1/g1AllocRegion.inline.hpp" aoqi@0: #include "gc_implementation/g1/g1CollectorPolicy.hpp" aoqi@0: #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp" tschatzl@7091: #include "gc_implementation/g1/heapRegionManager.inline.hpp" aoqi@0: #include "gc_implementation/g1/heapRegionSet.inline.hpp" goetz@6911: #include "runtime/orderAccess.inline.hpp" aoqi@0: #include "utilities/taskqueue.hpp" aoqi@0: tschatzl@7651: PLABStats* G1CollectedHeap::alloc_buffer_stats(InCSetState dest) { tschatzl@7651: switch (dest.value()) { tschatzl@7651: case InCSetState::Young: tschatzl@7651: return &_survivor_plab_stats; tschatzl@7651: case InCSetState::Old: tschatzl@7651: return &_old_plab_stats; tschatzl@7651: default: tschatzl@7651: ShouldNotReachHere(); tschatzl@7651: return NULL; // Keep some compilers happy tschatzl@7651: } tschatzl@7651: } tschatzl@7651: tschatzl@7651: size_t G1CollectedHeap::desired_plab_sz(InCSetState dest) { tschatzl@7651: size_t gclab_word_size = alloc_buffer_stats(dest)->desired_plab_sz(); tschatzl@7651: // Prevent humongous PLAB sizes for two reasons: tschatzl@7651: // * PLABs are allocated using a similar paths as oops, but should tschatzl@7651: // never be in a humongous region tschatzl@7651: // * Allowing humongous PLABs needlessly churns the region free lists tschatzl@7651: return MIN2(_humongous_object_threshold_in_words, gclab_word_size); tschatzl@7651: } tschatzl@7651: tschatzl@7651: HeapWord* G1CollectedHeap::par_allocate_during_gc(InCSetState dest, tschatzl@7651: size_t word_size, tschatzl@7651: AllocationContext_t context) { tschatzl@7651: switch (dest.value()) { tschatzl@7651: case InCSetState::Young: tschatzl@7651: return survivor_attempt_allocation(word_size, context); tschatzl@7651: case InCSetState::Old: tschatzl@7651: return old_attempt_allocation(word_size, context); tschatzl@7651: default: tschatzl@7651: ShouldNotReachHere(); tschatzl@7651: return NULL; // Keep some compilers happy tschatzl@7651: } tschatzl@7651: } tschatzl@7651: aoqi@0: // Inline functions for G1CollectedHeap aoqi@0: jcoomes@7159: inline AllocationContextStats& G1CollectedHeap::allocation_context_stats() { jcoomes@7159: return _allocation_context_stats; jcoomes@7159: } jcoomes@7159: aoqi@0: // Return the region with the given index. It assumes the index is valid. tschatzl@7091: inline HeapRegion* G1CollectedHeap::region_at(uint index) const { return _hrm.at(index); } drchase@6680: tschatzl@7010: inline uint G1CollectedHeap::addr_to_region(HeapWord* addr) const { tschatzl@7010: assert(is_in_reserved(addr), kevinw@9327: err_msg("Cannot calculate region index for address " PTR_FORMAT " that is outside of the heap [" PTR_FORMAT ", " PTR_FORMAT ")", tschatzl@7010: p2i(addr), p2i(_reserved.start()), p2i(_reserved.end()))); tschatzl@7010: return (uint)(pointer_delta(addr, _reserved.start(), sizeof(uint8_t)) >> HeapRegion::LogOfHRGrainBytes); tschatzl@7010: } tschatzl@7010: tschatzl@7050: inline HeapWord* G1CollectedHeap::bottom_addr_for_region(uint index) const { tschatzl@7091: return _hrm.reserved().start() + index * HeapRegion::GrainWords; tschatzl@7050: } aoqi@0: aoqi@0: template tschatzl@7050: inline HeapRegion* G1CollectedHeap::heap_region_containing_raw(const T addr) const { brutisso@7049: assert(addr != NULL, "invariant"); tschatzl@7050: assert(is_in_g1_reserved((const void*) addr), kevinw@9327: err_msg("Address " PTR_FORMAT " is outside of the heap ranging from [" PTR_FORMAT " to " PTR_FORMAT ")", tschatzl@7050: p2i((void*)addr), p2i(g1_reserved().start()), p2i(g1_reserved().end()))); tschatzl@7091: return _hrm.addr_to_region((HeapWord*) addr); drchase@6680: } drchase@6680: drchase@6680: template tschatzl@7050: inline HeapRegion* G1CollectedHeap::heap_region_containing(const T addr) const { brutisso@7049: HeapRegion* hr = heap_region_containing_raw(addr); brutisso@7049: if (hr->continuesHumongous()) { brutisso@7049: return hr->humongous_start_region(); aoqi@0: } aoqi@0: return hr; aoqi@0: } aoqi@0: goetz@6911: inline void G1CollectedHeap::reset_gc_time_stamp() { goetz@6911: _gc_time_stamp = 0; goetz@6911: OrderAccess::fence(); goetz@6911: // Clear the cached CSet starting regions and time stamps. goetz@6911: // Their validity is dependent on the GC timestamp. goetz@6911: clear_cset_start_regions(); goetz@6911: } goetz@6911: goetz@6911: inline void G1CollectedHeap::increment_gc_time_stamp() { goetz@6911: ++_gc_time_stamp; goetz@6911: OrderAccess::fence(); aoqi@0: } aoqi@0: aoqi@0: inline void G1CollectedHeap::old_set_remove(HeapRegion* hr) { aoqi@0: _old_set.remove(hr); aoqi@0: } aoqi@0: aoqi@0: inline bool G1CollectedHeap::obj_in_cs(oop obj) { tschatzl@7091: HeapRegion* r = _hrm.addr_to_region((HeapWord*) obj); aoqi@0: return r != NULL && r->in_collection_set(); aoqi@0: } aoqi@0: tschatzl@7050: inline HeapWord* G1CollectedHeap::attempt_allocation(size_t word_size, mlarsson@7686: uint* gc_count_before_ret, mlarsson@7686: uint* gclocker_retry_count_ret) { aoqi@0: assert_heap_not_locked_and_not_at_safepoint(); aoqi@0: assert(!isHumongous(word_size), "attempt_allocation() should not " aoqi@0: "be called for humongous allocation requests"); aoqi@0: sjohanss@7118: AllocationContext_t context = AllocationContext::current(); sjohanss@7118: HeapWord* result = _allocator->mutator_alloc_region(context)->attempt_allocation(word_size, sjohanss@7118: false /* bot_updates */); aoqi@0: if (result == NULL) { aoqi@0: result = attempt_allocation_slow(word_size, sjohanss@7118: context, aoqi@0: gc_count_before_ret, aoqi@0: gclocker_retry_count_ret); aoqi@0: } aoqi@0: assert_heap_not_locked(); aoqi@0: if (result != NULL) { aoqi@0: dirty_young_block(result, word_size); aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: sjohanss@7118: inline HeapWord* G1CollectedHeap::survivor_attempt_allocation(size_t word_size, sjohanss@7118: AllocationContext_t context) { aoqi@0: assert(!isHumongous(word_size), aoqi@0: "we should not be seeing humongous-size allocations in this path"); aoqi@0: sjohanss@7118: HeapWord* result = _allocator->survivor_gc_alloc_region(context)->attempt_allocation(word_size, sjohanss@7118: false /* bot_updates */); aoqi@0: if (result == NULL) { aoqi@0: MutexLockerEx x(FreeList_lock, Mutex::_no_safepoint_check_flag); sjohanss@7118: result = _allocator->survivor_gc_alloc_region(context)->attempt_allocation_locked(word_size, sjohanss@7118: false /* bot_updates */); aoqi@0: } aoqi@0: if (result != NULL) { aoqi@0: dirty_young_block(result, word_size); aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: sjohanss@7118: inline HeapWord* G1CollectedHeap::old_attempt_allocation(size_t word_size, sjohanss@7118: AllocationContext_t context) { aoqi@0: assert(!isHumongous(word_size), aoqi@0: "we should not be seeing humongous-size allocations in this path"); aoqi@0: sjohanss@7118: HeapWord* result = _allocator->old_gc_alloc_region(context)->attempt_allocation(word_size, sjohanss@7118: true /* bot_updates */); aoqi@0: if (result == NULL) { aoqi@0: MutexLockerEx x(FreeList_lock, Mutex::_no_safepoint_check_flag); sjohanss@7118: result = _allocator->old_gc_alloc_region(context)->attempt_allocation_locked(word_size, sjohanss@7118: true /* bot_updates */); aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: // It dirties the cards that cover the block so that so that the post aoqi@0: // write barrier never queues anything when updating objects on this aoqi@0: // block. It is assumed (and in fact we assert) that the block aoqi@0: // belongs to a young region. aoqi@0: inline void aoqi@0: G1CollectedHeap::dirty_young_block(HeapWord* start, size_t word_size) { aoqi@0: assert_heap_not_locked(); aoqi@0: aoqi@0: // Assign the containing region to containing_hr so that we don't aoqi@0: // have to keep calling heap_region_containing_raw() in the aoqi@0: // asserts below. aoqi@0: DEBUG_ONLY(HeapRegion* containing_hr = heap_region_containing_raw(start);) brutisso@7049: assert(word_size > 0, "pre-condition"); aoqi@0: assert(containing_hr->is_in(start), "it should contain start"); aoqi@0: assert(containing_hr->is_young(), "it should be young"); aoqi@0: assert(!containing_hr->isHumongous(), "it should not be humongous"); aoqi@0: aoqi@0: HeapWord* end = start + word_size; aoqi@0: assert(containing_hr->is_in(end - 1), "it should also contain end - 1"); aoqi@0: aoqi@0: MemRegion mr(start, end); aoqi@0: g1_barrier_set()->g1_mark_as_young(mr); aoqi@0: } aoqi@0: aoqi@0: inline RefToScanQueue* G1CollectedHeap::task_queue(int i) const { aoqi@0: return _task_queues->queue(i); aoqi@0: } aoqi@0: aoqi@0: inline bool G1CollectedHeap::isMarkedPrev(oop obj) const { aoqi@0: return _cm->prevMarkBitMap()->isMarked((HeapWord *)obj); aoqi@0: } aoqi@0: aoqi@0: inline bool G1CollectedHeap::isMarkedNext(oop obj) const { aoqi@0: return _cm->nextMarkBitMap()->isMarked((HeapWord *)obj); aoqi@0: } aoqi@0: aoqi@0: // This is a fast test on whether a reference points into the aoqi@0: // collection set or not. Assume that the reference aoqi@0: // points into the heap. tschatzl@7010: inline bool G1CollectedHeap::is_in_cset(oop obj) { tschatzl@7010: bool ret = _in_cset_fast_test.is_in_cset((HeapWord*)obj); aoqi@0: // let's make sure the result is consistent with what the slower aoqi@0: // test returns aoqi@0: assert( ret || !obj_in_cs(obj), "sanity"); aoqi@0: assert(!ret || obj_in_cs(obj), "sanity"); aoqi@0: return ret; aoqi@0: } aoqi@0: tschatzl@7010: bool G1CollectedHeap::is_in_cset_or_humongous(const oop obj) { tschatzl@7010: return _in_cset_fast_test.is_in_cset_or_humongous((HeapWord*)obj); tschatzl@7010: } tschatzl@7010: tschatzl@7651: InCSetState G1CollectedHeap::in_cset_state(const oop obj) { tschatzl@7010: return _in_cset_fast_test.at((HeapWord*)obj); tschatzl@7010: } tschatzl@7010: tschatzl@7010: void G1CollectedHeap::register_humongous_region_with_in_cset_fast_test(uint index) { tschatzl@7010: _in_cset_fast_test.set_humongous(index); tschatzl@7010: } tschatzl@7010: aoqi@0: #ifndef PRODUCT aoqi@0: // Support for G1EvacuationFailureALot aoqi@0: aoqi@0: inline bool aoqi@0: G1CollectedHeap::evacuation_failure_alot_for_gc_type(bool gcs_are_young, aoqi@0: bool during_initial_mark, aoqi@0: bool during_marking) { aoqi@0: bool res = false; aoqi@0: if (during_marking) { aoqi@0: res |= G1EvacuationFailureALotDuringConcMark; aoqi@0: } aoqi@0: if (during_initial_mark) { aoqi@0: res |= G1EvacuationFailureALotDuringInitialMark; aoqi@0: } aoqi@0: if (gcs_are_young) { aoqi@0: res |= G1EvacuationFailureALotDuringYoungGC; aoqi@0: } else { aoqi@0: // GCs are mixed aoqi@0: res |= G1EvacuationFailureALotDuringMixedGC; aoqi@0: } aoqi@0: return res; aoqi@0: } aoqi@0: aoqi@0: inline void aoqi@0: G1CollectedHeap::set_evacuation_failure_alot_for_current_gc() { aoqi@0: if (G1EvacuationFailureALot) { aoqi@0: // Note we can't assert that _evacuation_failure_alot_for_current_gc aoqi@0: // is clear here. It may have been set during a previous GC but that GC aoqi@0: // did not copy enough objects (i.e. G1EvacuationFailureALotCount) to aoqi@0: // trigger an evacuation failure and clear the flags and and counts. aoqi@0: aoqi@0: // Check if we have gone over the interval. aoqi@0: const size_t gc_num = total_collections(); aoqi@0: const size_t elapsed_gcs = gc_num - _evacuation_failure_alot_gc_number; aoqi@0: aoqi@0: _evacuation_failure_alot_for_current_gc = (elapsed_gcs >= G1EvacuationFailureALotInterval); aoqi@0: aoqi@0: // Now check if G1EvacuationFailureALot is enabled for the current GC type. aoqi@0: const bool gcs_are_young = g1_policy()->gcs_are_young(); aoqi@0: const bool during_im = g1_policy()->during_initial_mark_pause(); aoqi@0: const bool during_marking = mark_in_progress(); aoqi@0: aoqi@0: _evacuation_failure_alot_for_current_gc &= aoqi@0: evacuation_failure_alot_for_gc_type(gcs_are_young, aoqi@0: during_im, aoqi@0: during_marking); aoqi@0: } aoqi@0: } aoqi@0: tschatzl@7050: inline bool G1CollectedHeap::evacuation_should_fail() { aoqi@0: if (!G1EvacuationFailureALot || !_evacuation_failure_alot_for_current_gc) { aoqi@0: return false; aoqi@0: } aoqi@0: // G1EvacuationFailureALot is in effect for current GC aoqi@0: // Access to _evacuation_failure_alot_count is not atomic; aoqi@0: // the value does not have to be exact. aoqi@0: if (++_evacuation_failure_alot_count < G1EvacuationFailureALotCount) { aoqi@0: return false; aoqi@0: } aoqi@0: _evacuation_failure_alot_count = 0; aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: inline void G1CollectedHeap::reset_evacuation_should_fail() { aoqi@0: if (G1EvacuationFailureALot) { aoqi@0: _evacuation_failure_alot_gc_number = total_collections(); aoqi@0: _evacuation_failure_alot_count = 0; aoqi@0: _evacuation_failure_alot_for_current_gc = false; aoqi@0: } aoqi@0: } aoqi@0: #endif // #ifndef PRODUCT aoqi@0: aoqi@0: inline bool G1CollectedHeap::is_in_young(const oop obj) { brutisso@7049: if (obj == NULL) { brutisso@7049: return false; brutisso@7049: } brutisso@7049: return heap_region_containing(obj)->is_young(); aoqi@0: } aoqi@0: aoqi@0: // We don't need barriers for initializing stores to objects aoqi@0: // in the young gen: for the SATB pre-barrier, there is no aoqi@0: // pre-value that needs to be remembered; for the remembered-set aoqi@0: // update logging post-barrier, we don't maintain remembered set aoqi@0: // information for young gen objects. aoqi@0: inline bool G1CollectedHeap::can_elide_initializing_store_barrier(oop new_obj) { aoqi@0: return is_in_young(new_obj); aoqi@0: } aoqi@0: aoqi@0: inline bool G1CollectedHeap::is_obj_dead(const oop obj) const { brutisso@7049: if (obj == NULL) { brutisso@7049: return false; aoqi@0: } brutisso@7049: return is_obj_dead(obj, heap_region_containing(obj)); aoqi@0: } aoqi@0: aoqi@0: inline bool G1CollectedHeap::is_obj_ill(const oop obj) const { brutisso@7049: if (obj == NULL) { brutisso@7049: return false; aoqi@0: } brutisso@7049: return is_obj_ill(obj, heap_region_containing(obj)); aoqi@0: } aoqi@0: kbarrett@7830: inline void G1CollectedHeap::set_humongous_reclaim_candidate(uint region, bool value) { kbarrett@7830: assert(_hrm.at(region)->startsHumongous(), "Must start a humongous object"); kbarrett@7830: _humongous_reclaim_candidates.set_candidate(region, value); kbarrett@7830: } kbarrett@7830: kbarrett@7830: inline bool G1CollectedHeap::is_humongous_reclaim_candidate(uint region) { kbarrett@7830: assert(_hrm.at(region)->startsHumongous(), "Must start a humongous object"); kbarrett@7830: return _humongous_reclaim_candidates.is_candidate(region); kbarrett@7830: } kbarrett@7830: tschatzl@7010: inline void G1CollectedHeap::set_humongous_is_live(oop obj) { tschatzl@7010: uint region = addr_to_region((HeapWord*)obj); kbarrett@7830: // Clear the flag in the humongous_reclaim_candidates table. Also tschatzl@7010: // reset the entry in the _in_cset_fast_test table so that subsequent references tschatzl@7010: // to the same humongous object do not go into the slow path again. tschatzl@7010: // This is racy, as multiple threads may at the same time enter here, but this tschatzl@7010: // is benign. kbarrett@7830: // During collection we only ever clear the "candidate" flag, and only ever clear the tschatzl@7010: // entry in the in_cset_fast_table. tschatzl@7010: // We only ever evaluate the contents of these tables (in the VM thread) after tschatzl@7010: // having synchronized the worker threads with the VM thread, or in the same tschatzl@7010: // thread (i.e. within the VM thread). kbarrett@7830: if (is_humongous_reclaim_candidate(region)) { kbarrett@7830: set_humongous_reclaim_candidate(region, false); tschatzl@7010: _in_cset_fast_test.clear_humongous(region); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1COLLECTEDHEAP_INLINE_HPP