Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/g1/g1CollectedHeap.inline.hpp
38920 views
/*1* Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1COLLECTEDHEAP_INLINE_HPP25#define SHARE_VM_GC_IMPLEMENTATION_G1_G1COLLECTEDHEAP_INLINE_HPP2627#include "gc_implementation/g1/concurrentMark.hpp"28#include "gc_implementation/g1/g1CollectedHeap.hpp"29#include "gc_implementation/g1/g1AllocRegion.inline.hpp"30#include "gc_implementation/g1/g1CollectorPolicy.hpp"31#include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"32#include "gc_implementation/g1/heapRegionManager.inline.hpp"33#include "gc_implementation/g1/heapRegionSet.inline.hpp"34#include "runtime/orderAccess.inline.hpp"35#include "utilities/taskqueue.hpp"3637PLABStats* G1CollectedHeap::alloc_buffer_stats(InCSetState dest) {38switch (dest.value()) {39case InCSetState::Young:40return &_survivor_plab_stats;41case InCSetState::Old:42return &_old_plab_stats;43default:44ShouldNotReachHere();45return NULL; // Keep some compilers happy46}47}4849size_t G1CollectedHeap::desired_plab_sz(InCSetState dest) {50size_t gclab_word_size = alloc_buffer_stats(dest)->desired_plab_sz();51// Prevent humongous PLAB sizes for two reasons:52// * PLABs are allocated using a similar paths as oops, but should53// never be in a humongous region54// * Allowing humongous PLABs needlessly churns the region free lists55return MIN2(_humongous_object_threshold_in_words, gclab_word_size);56}5758HeapWord* G1CollectedHeap::par_allocate_during_gc(InCSetState dest,59size_t word_size,60AllocationContext_t context) {61switch (dest.value()) {62case InCSetState::Young:63return survivor_attempt_allocation(word_size, context);64case InCSetState::Old:65return old_attempt_allocation(word_size, context);66default:67ShouldNotReachHere();68return NULL; // Keep some compilers happy69}70}7172// Inline functions for G1CollectedHeap7374inline AllocationContextStats& G1CollectedHeap::allocation_context_stats() {75return _allocation_context_stats;76}7778// Return the region with the given index. It assumes the index is valid.79inline HeapRegion* G1CollectedHeap::region_at(uint index) const { return _hrm.at(index); }8081inline uint G1CollectedHeap::addr_to_region(HeapWord* addr) const {82assert(is_in_reserved(addr),83err_msg("Cannot calculate region index for address " PTR_FORMAT " that is outside of the heap [" PTR_FORMAT ", " PTR_FORMAT ")",84p2i(addr), p2i(_reserved.start()), p2i(_reserved.end())));85return (uint)(pointer_delta(addr, _reserved.start(), sizeof(uint8_t)) >> HeapRegion::LogOfHRGrainBytes);86}8788inline HeapWord* G1CollectedHeap::bottom_addr_for_region(uint index) const {89return _hrm.reserved().start() + index * HeapRegion::GrainWords;90}9192template <class T>93inline HeapRegion* G1CollectedHeap::heap_region_containing_raw(const T addr) const {94assert(addr != NULL, "invariant");95assert(is_in_g1_reserved((const void*) addr),96err_msg("Address " PTR_FORMAT " is outside of the heap ranging from [" PTR_FORMAT " to " PTR_FORMAT ")",97p2i((void*)addr), p2i(g1_reserved().start()), p2i(g1_reserved().end())));98return _hrm.addr_to_region((HeapWord*) addr);99}100101template <class T>102inline HeapRegion* G1CollectedHeap::heap_region_containing(const T addr) const {103HeapRegion* hr = heap_region_containing_raw(addr);104if (hr->continuesHumongous()) {105return hr->humongous_start_region();106}107return hr;108}109110inline void G1CollectedHeap::reset_gc_time_stamp() {111_gc_time_stamp = 0;112OrderAccess::fence();113// Clear the cached CSet starting regions and time stamps.114// Their validity is dependent on the GC timestamp.115clear_cset_start_regions();116}117118inline void G1CollectedHeap::increment_gc_time_stamp() {119++_gc_time_stamp;120OrderAccess::fence();121}122123inline void G1CollectedHeap::old_set_remove(HeapRegion* hr) {124_old_set.remove(hr);125}126127inline bool G1CollectedHeap::obj_in_cs(oop obj) {128HeapRegion* r = _hrm.addr_to_region((HeapWord*) obj);129return r != NULL && r->in_collection_set();130}131132inline HeapWord* G1CollectedHeap::attempt_allocation(size_t word_size,133uint* gc_count_before_ret,134uint* gclocker_retry_count_ret) {135assert_heap_not_locked_and_not_at_safepoint();136assert(!isHumongous(word_size), "attempt_allocation() should not "137"be called for humongous allocation requests");138139AllocationContext_t context = AllocationContext::current();140HeapWord* result = _allocator->mutator_alloc_region(context)->attempt_allocation(word_size,141false /* bot_updates */);142if (result == NULL) {143result = attempt_allocation_slow(word_size,144context,145gc_count_before_ret,146gclocker_retry_count_ret);147}148assert_heap_not_locked();149if (result != NULL) {150dirty_young_block(result, word_size);151}152return result;153}154155inline HeapWord* G1CollectedHeap::survivor_attempt_allocation(size_t word_size,156AllocationContext_t context) {157assert(!isHumongous(word_size),158"we should not be seeing humongous-size allocations in this path");159160HeapWord* result = _allocator->survivor_gc_alloc_region(context)->attempt_allocation(word_size,161false /* bot_updates */);162if (result == NULL) {163MutexLockerEx x(FreeList_lock, Mutex::_no_safepoint_check_flag);164result = _allocator->survivor_gc_alloc_region(context)->attempt_allocation_locked(word_size,165false /* bot_updates */);166}167if (result != NULL) {168dirty_young_block(result, word_size);169}170return result;171}172173inline HeapWord* G1CollectedHeap::old_attempt_allocation(size_t word_size,174AllocationContext_t context) {175assert(!isHumongous(word_size),176"we should not be seeing humongous-size allocations in this path");177178HeapWord* result = _allocator->old_gc_alloc_region(context)->attempt_allocation(word_size,179true /* bot_updates */);180if (result == NULL) {181MutexLockerEx x(FreeList_lock, Mutex::_no_safepoint_check_flag);182result = _allocator->old_gc_alloc_region(context)->attempt_allocation_locked(word_size,183true /* bot_updates */);184}185return result;186}187188// It dirties the cards that cover the block so that so that the post189// write barrier never queues anything when updating objects on this190// block. It is assumed (and in fact we assert) that the block191// belongs to a young region.192inline void193G1CollectedHeap::dirty_young_block(HeapWord* start, size_t word_size) {194assert_heap_not_locked();195196// Assign the containing region to containing_hr so that we don't197// have to keep calling heap_region_containing_raw() in the198// asserts below.199DEBUG_ONLY(HeapRegion* containing_hr = heap_region_containing_raw(start);)200assert(word_size > 0, "pre-condition");201assert(containing_hr->is_in(start), "it should contain start");202assert(containing_hr->is_young(), "it should be young");203assert(!containing_hr->isHumongous(), "it should not be humongous");204205HeapWord* end = start + word_size;206assert(containing_hr->is_in(end - 1), "it should also contain end - 1");207208MemRegion mr(start, end);209g1_barrier_set()->g1_mark_as_young(mr);210}211212inline RefToScanQueue* G1CollectedHeap::task_queue(int i) const {213return _task_queues->queue(i);214}215216inline bool G1CollectedHeap::isMarkedPrev(oop obj) const {217return _cm->prevMarkBitMap()->isMarked((HeapWord *)obj);218}219220inline bool G1CollectedHeap::isMarkedNext(oop obj) const {221return _cm->nextMarkBitMap()->isMarked((HeapWord *)obj);222}223224// This is a fast test on whether a reference points into the225// collection set or not. Assume that the reference226// points into the heap.227inline bool G1CollectedHeap::is_in_cset(oop obj) {228bool ret = _in_cset_fast_test.is_in_cset((HeapWord*)obj);229// let's make sure the result is consistent with what the slower230// test returns231assert( ret || !obj_in_cs(obj), "sanity");232assert(!ret || obj_in_cs(obj), "sanity");233return ret;234}235236bool G1CollectedHeap::is_in_cset_or_humongous(const oop obj) {237return _in_cset_fast_test.is_in_cset_or_humongous((HeapWord*)obj);238}239240InCSetState G1CollectedHeap::in_cset_state(const oop obj) {241return _in_cset_fast_test.at((HeapWord*)obj);242}243244void G1CollectedHeap::register_humongous_region_with_in_cset_fast_test(uint index) {245_in_cset_fast_test.set_humongous(index);246}247248#ifndef PRODUCT249// Support for G1EvacuationFailureALot250251inline bool252G1CollectedHeap::evacuation_failure_alot_for_gc_type(bool gcs_are_young,253bool during_initial_mark,254bool during_marking) {255bool res = false;256if (during_marking) {257res |= G1EvacuationFailureALotDuringConcMark;258}259if (during_initial_mark) {260res |= G1EvacuationFailureALotDuringInitialMark;261}262if (gcs_are_young) {263res |= G1EvacuationFailureALotDuringYoungGC;264} else {265// GCs are mixed266res |= G1EvacuationFailureALotDuringMixedGC;267}268return res;269}270271inline void272G1CollectedHeap::set_evacuation_failure_alot_for_current_gc() {273if (G1EvacuationFailureALot) {274// Note we can't assert that _evacuation_failure_alot_for_current_gc275// is clear here. It may have been set during a previous GC but that GC276// did not copy enough objects (i.e. G1EvacuationFailureALotCount) to277// trigger an evacuation failure and clear the flags and and counts.278279// Check if we have gone over the interval.280const size_t gc_num = total_collections();281const size_t elapsed_gcs = gc_num - _evacuation_failure_alot_gc_number;282283_evacuation_failure_alot_for_current_gc = (elapsed_gcs >= G1EvacuationFailureALotInterval);284285// Now check if G1EvacuationFailureALot is enabled for the current GC type.286const bool gcs_are_young = g1_policy()->gcs_are_young();287const bool during_im = g1_policy()->during_initial_mark_pause();288const bool during_marking = mark_in_progress();289290_evacuation_failure_alot_for_current_gc &=291evacuation_failure_alot_for_gc_type(gcs_are_young,292during_im,293during_marking);294}295}296297inline bool G1CollectedHeap::evacuation_should_fail() {298if (!G1EvacuationFailureALot || !_evacuation_failure_alot_for_current_gc) {299return false;300}301// G1EvacuationFailureALot is in effect for current GC302// Access to _evacuation_failure_alot_count is not atomic;303// the value does not have to be exact.304if (++_evacuation_failure_alot_count < G1EvacuationFailureALotCount) {305return false;306}307_evacuation_failure_alot_count = 0;308return true;309}310311inline void G1CollectedHeap::reset_evacuation_should_fail() {312if (G1EvacuationFailureALot) {313_evacuation_failure_alot_gc_number = total_collections();314_evacuation_failure_alot_count = 0;315_evacuation_failure_alot_for_current_gc = false;316}317}318#endif // #ifndef PRODUCT319320inline bool G1CollectedHeap::is_in_young(const oop obj) {321if (obj == NULL) {322return false;323}324return heap_region_containing(obj)->is_young();325}326327// We don't need barriers for initializing stores to objects328// in the young gen: for the SATB pre-barrier, there is no329// pre-value that needs to be remembered; for the remembered-set330// update logging post-barrier, we don't maintain remembered set331// information for young gen objects.332inline bool G1CollectedHeap::can_elide_initializing_store_barrier(oop new_obj) {333return is_in_young(new_obj);334}335336inline bool G1CollectedHeap::is_obj_dead(const oop obj) const {337if (obj == NULL) {338return false;339}340return is_obj_dead(obj, heap_region_containing(obj));341}342343inline bool G1CollectedHeap::is_obj_ill(const oop obj) const {344if (obj == NULL) {345return false;346}347return is_obj_ill(obj, heap_region_containing(obj));348}349350inline void G1CollectedHeap::set_humongous_reclaim_candidate(uint region, bool value) {351assert(_hrm.at(region)->startsHumongous(), "Must start a humongous object");352_humongous_reclaim_candidates.set_candidate(region, value);353}354355inline bool G1CollectedHeap::is_humongous_reclaim_candidate(uint region) {356assert(_hrm.at(region)->startsHumongous(), "Must start a humongous object");357return _humongous_reclaim_candidates.is_candidate(region);358}359360inline void G1CollectedHeap::set_humongous_is_live(oop obj) {361uint region = addr_to_region((HeapWord*)obj);362// Clear the flag in the humongous_reclaim_candidates table. Also363// reset the entry in the _in_cset_fast_test table so that subsequent references364// to the same humongous object do not go into the slow path again.365// This is racy, as multiple threads may at the same time enter here, but this366// is benign.367// During collection we only ever clear the "candidate" flag, and only ever clear the368// entry in the in_cset_fast_table.369// We only ever evaluate the contents of these tables (in the VM thread) after370// having synchronized the worker threads with the VM thread, or in the same371// thread (i.e. within the VM thread).372if (is_humongous_reclaim_candidate(region)) {373set_humongous_reclaim_candidate(region, false);374_in_cset_fast_test.clear_humongous(region);375}376}377378inline bool G1CollectedHeap::requires_marking(const void* entry) const {379// Includes rejection of NULL pointers.380assert(is_in_reserved(entry),381err_msg("Non-heap pointer in SATB buffer: " PTR_FORMAT, p2i(entry)));382383HeapRegion* region = heap_region_containing(entry);384assert(region != NULL, err_msg("No region for " PTR_FORMAT, p2i(entry)));385if (entry >= region->next_top_at_mark_start()) {386return false;387}388389assert(((oop)entry)->is_oop(true /* ignore mark word */),390err_msg("Invalid oop in SATB buffer: " PTR_FORMAT, p2i(entry)));391392return ! isMarkedNext((oop) entry);393}394395#endif // SHARE_VM_GC_IMPLEMENTATION_G1_G1COLLECTEDHEAP_INLINE_HPP396397398