Path: blob/master/src/hotspot/share/gc/g1/g1CardCounts.cpp
40957 views
/*1* Copyright (c) 2013, 2018, 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/g1/g1CardCounts.hpp"26#include "gc/g1/g1CollectedHeap.inline.hpp"27#include "gc/shared/cardTableBarrierSet.hpp"28#include "services/memTracker.hpp"29#include "utilities/copy.hpp"3031void G1CardCountsMappingChangedListener::on_commit(uint start_idx, size_t num_regions, bool zero_filled) {32if (zero_filled) {33return;34}35MemRegion mr(G1CollectedHeap::heap()->bottom_addr_for_region(start_idx), num_regions * HeapRegion::GrainWords);36_counts->clear_range(mr);37}3839size_t G1CardCounts::compute_size(size_t mem_region_size_in_words) {40// We keep card counts for every card, so the size of the card counts table must41// be the same as the card table.42return G1CardTable::compute_size(mem_region_size_in_words);43}4445size_t G1CardCounts::heap_map_factor() {46// See G1CardCounts::compute_size() why we reuse the card table value.47return G1CardTable::heap_map_factor();48}4950void G1CardCounts::clear_range(size_t from_card_num, size_t to_card_num) {51if (has_count_table()) {52assert(from_card_num < to_card_num,53"Wrong order? from: " SIZE_FORMAT ", to: " SIZE_FORMAT,54from_card_num, to_card_num);55Copy::fill_to_bytes(&_card_counts[from_card_num], (to_card_num - from_card_num));56}57}5859G1CardCounts::G1CardCounts(G1CollectedHeap *g1h):60_listener(), _g1h(g1h), _ct(NULL), _card_counts(NULL), _reserved_max_card_num(0), _ct_bot(NULL) {61_listener.set_cardcounts(this);62}6364void G1CardCounts::initialize(G1RegionToSpaceMapper* mapper) {65assert(_g1h->reserved().byte_size() > 0, "initialization order");66assert(_g1h->capacity() == 0, "initialization order");6768if (G1ConcRSHotCardLimit > 0) {69// The max value we can store in the counts table is70// max_jubyte. Guarantee the value of the hot71// threshold limit is no more than this.72guarantee(G1ConcRSHotCardLimit <= max_jubyte, "sanity");7374_ct = _g1h->card_table();75_ct_bot = _ct->byte_for_const(_g1h->reserved().start());7677_card_counts = (jubyte*) mapper->reserved().start();78_reserved_max_card_num = mapper->reserved().byte_size();79mapper->set_mapping_changed_listener(&_listener);80}81}8283uint G1CardCounts::add_card_count(CardValue* card_ptr) {84// Returns the number of times the card has been refined.85// If we failed to reserve/commit the counts table, return 0.86// If card_ptr is beyond the committed end of the counts table,87// return 0.88// Otherwise return the actual count.89// Unless G1ConcRSHotCardLimit has been set appropriately,90// returning 0 will result in the card being considered91// cold and will be refined immediately.92uint count = 0;93if (has_count_table()) {94size_t card_num = ptr_2_card_num(card_ptr);95assert(card_num < _reserved_max_card_num,96"Card " SIZE_FORMAT " outside of card counts table (max size " SIZE_FORMAT ")",97card_num, _reserved_max_card_num);98count = (uint) _card_counts[card_num];99if (count < G1ConcRSHotCardLimit) {100_card_counts[card_num] =101(jubyte)(MIN2((uintx)(_card_counts[card_num] + 1), G1ConcRSHotCardLimit));102}103}104return count;105}106107bool G1CardCounts::is_hot(uint count) {108return (count >= G1ConcRSHotCardLimit);109}110111void G1CardCounts::clear_region(HeapRegion* hr) {112MemRegion mr(hr->bottom(), hr->end());113clear_range(mr);114}115116void G1CardCounts::clear_range(MemRegion mr) {117if (has_count_table()) {118const CardValue* from_card_ptr = _ct->byte_for_const(mr.start());119// We use the last address in the range as the range could represent the120// last region in the heap. In which case trying to find the card will be an121// OOB access to the card table.122const CardValue* last_card_ptr = _ct->byte_for_const(mr.last());123124#ifdef ASSERT125HeapWord* start_addr = _ct->addr_for(from_card_ptr);126assert(start_addr == mr.start(), "MemRegion start must be aligned to a card.");127HeapWord* last_addr = _ct->addr_for(last_card_ptr);128assert((last_addr + G1CardTable::card_size_in_words) == mr.end(), "MemRegion end must be aligned to a card.");129#endif // ASSERT130131// Clear the counts for the (exclusive) card range.132size_t from_card_num = ptr_2_card_num(from_card_ptr);133size_t to_card_num = ptr_2_card_num(last_card_ptr) + 1;134clear_range(from_card_num, to_card_num);135}136}137138class G1CardCountsClearClosure : public HeapRegionClosure {139private:140G1CardCounts* _card_counts;141public:142G1CardCountsClearClosure(G1CardCounts* card_counts) :143HeapRegionClosure(), _card_counts(card_counts) { }144145146virtual bool do_heap_region(HeapRegion* r) {147_card_counts->clear_region(r);148return false;149}150};151152void G1CardCounts::clear_all() {153assert(SafepointSynchronize::is_at_safepoint(), "don't call this otherwise");154G1CardCountsClearClosure cl(this);155_g1h->heap_region_iterate(&cl);156}157158159