Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/g1/g1CardCounts.cpp
38920 views
/*1* Copyright (c) 2013, 2014, 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#include "precompiled.hpp"25#include "gc_implementation/g1/g1CardCounts.hpp"26#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"27#include "gc_implementation/g1/g1CollectorPolicy.hpp"28#include "gc_implementation/g1/g1GCPhaseTimes.hpp"29#include "memory/cardTableModRefBS.hpp"30#include "services/memTracker.hpp"31#include "utilities/copy.hpp"3233PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC3435void G1CardCountsMappingChangedListener::on_commit(uint start_idx, size_t num_regions, bool zero_filled) {36if (zero_filled) {37return;38}39MemRegion mr(G1CollectedHeap::heap()->bottom_addr_for_region(start_idx), num_regions * HeapRegion::GrainWords);40_counts->clear_range(mr);41}4243void G1CardCounts::clear_range(size_t from_card_num, size_t to_card_num) {44if (has_count_table()) {45assert(from_card_num < to_card_num,46err_msg("Wrong order? from: " SIZE_FORMAT ", to: " SIZE_FORMAT,47from_card_num, to_card_num));48Copy::fill_to_bytes(&_card_counts[from_card_num], (to_card_num - from_card_num));49}50}5152G1CardCounts::G1CardCounts(G1CollectedHeap *g1h):53_listener(), _g1h(g1h), _card_counts(NULL), _reserved_max_card_num(0) {54_listener.set_cardcounts(this);55}5657void G1CardCounts::initialize(G1RegionToSpaceMapper* mapper) {58assert(_g1h->max_capacity() > 0, "initialization order");59assert(_g1h->capacity() == 0, "initialization order");6061if (G1ConcRSHotCardLimit > 0) {62// The max value we can store in the counts table is63// max_jubyte. Guarantee the value of the hot64// threshold limit is no more than this.65guarantee(G1ConcRSHotCardLimit <= max_jubyte, "sanity");6667_ct_bs = _g1h->g1_barrier_set();68_ct_bot = _ct_bs->byte_for_const(_g1h->reserved_region().start());6970_card_counts = (jubyte*) mapper->reserved().start();71_reserved_max_card_num = mapper->reserved().byte_size();72mapper->set_mapping_changed_listener(&_listener);73}74}7576uint G1CardCounts::add_card_count(jbyte* card_ptr) {77// Returns the number of times the card has been refined.78// If we failed to reserve/commit the counts table, return 0.79// If card_ptr is beyond the committed end of the counts table,80// return 0.81// Otherwise return the actual count.82// Unless G1ConcRSHotCardLimit has been set appropriately,83// returning 0 will result in the card being considered84// cold and will be refined immediately.85uint count = 0;86if (has_count_table()) {87size_t card_num = ptr_2_card_num(card_ptr);88assert(card_num < _reserved_max_card_num,89err_msg("Card " SIZE_FORMAT " outside of card counts table (max size " SIZE_FORMAT ")",90card_num, _reserved_max_card_num));91count = (uint) _card_counts[card_num];92if (count < G1ConcRSHotCardLimit) {93_card_counts[card_num] =94(jubyte)(MIN2((uintx)(_card_counts[card_num] + 1), G1ConcRSHotCardLimit));95}96}97return count;98}99100bool G1CardCounts::is_hot(uint count) {101return (count >= G1ConcRSHotCardLimit);102}103104void G1CardCounts::clear_region(HeapRegion* hr) {105MemRegion mr(hr->bottom(), hr->end());106clear_range(mr);107}108109void G1CardCounts::clear_range(MemRegion mr) {110if (has_count_table()) {111const jbyte* from_card_ptr = _ct_bs->byte_for_const(mr.start());112// We use the last address in the range as the range could represent the113// last region in the heap. In which case trying to find the card will be an114// OOB access to the card table.115const jbyte* last_card_ptr = _ct_bs->byte_for_const(mr.last());116117#ifdef ASSERT118HeapWord* start_addr = _ct_bs->addr_for(from_card_ptr);119assert(start_addr == mr.start(), "MemRegion start must be aligned to a card.");120HeapWord* last_addr = _ct_bs->addr_for(last_card_ptr);121assert((last_addr + CardTableModRefBS::card_size_in_words) == mr.end(), "MemRegion end must be aligned to a card.");122#endif // ASSERT123124// Clear the counts for the (exclusive) card range.125size_t from_card_num = ptr_2_card_num(from_card_ptr);126size_t to_card_num = ptr_2_card_num(last_card_ptr) + 1;127clear_range(from_card_num, to_card_num);128}129}130131class G1CardCountsClearClosure : public HeapRegionClosure {132private:133G1CardCounts* _card_counts;134public:135G1CardCountsClearClosure(G1CardCounts* card_counts) :136HeapRegionClosure(), _card_counts(card_counts) { }137138139virtual bool doHeapRegion(HeapRegion* r) {140_card_counts->clear_region(r);141return false;142}143};144145void G1CardCounts::clear_all() {146assert(SafepointSynchronize::is_at_safepoint(), "don't call this otherwise");147G1CardCountsClearClosure cl(this);148_g1h->heap_region_iterate(&cl);149}150151152