1193N/A/*
3792N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
1193N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1193N/A *
1193N/A * This code is free software; you can redistribute it and/or modify it
1193N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
1193N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1193N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1193N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1193N/A * version 2 for more details (a copy is included in the LICENSE file that
1193N/A * accompanied this code).
1193N/A *
1193N/A * You should have received a copy of the GNU General Public License version
1193N/A * 2 along with this work; if not, write to the Free Software Foundation,
1193N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1193N/A *
1193N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1193N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A *
2362N/A */
1193N/A
1193N/A#include "precompiled.hpp"
3793N/A#include "gc_implementation/g1/g1CardCounts.hpp"
1193N/A#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
5459N/A#include "gc_implementation/g1/g1CollectorPolicy.hpp"
5459N/A#include "gc_implementation/g1/g1GCPhaseTimes.hpp"
5459N/A#include "memory/cardTableModRefBS.hpp"
5459N/A#include "services/memTracker.hpp"
3793N/A#include "utilities/copy.hpp"
5459N/A
5459N/Avoid G1CardCounts::clear_range(size_t from_card_num, size_t to_card_num) {
5459N/A if (has_count_table()) {
5459N/A check_card_num(from_card_num,
5459N/A err_msg("from card num out of range: "SIZE_FORMAT, from_card_num));
5459N/A assert(from_card_num < to_card_num,
5459N/A err_msg("Wrong order? from: " SIZE_FORMAT ", to: "SIZE_FORMAT,
5459N/A from_card_num, to_card_num));
1193N/A assert(to_card_num <= _committed_max_card_num,
1193N/A err_msg("to card num out of range: "
5459N/A "to: "SIZE_FORMAT ", "
5459N/A "max: "SIZE_FORMAT,
1193N/A to_card_num, _committed_max_card_num));
1193N/A
1193N/A to_card_num = MIN2(_committed_max_card_num, to_card_num);
5459N/A
1193N/A Copy::fill_to_bytes(&_card_counts[from_card_num], (to_card_num - from_card_num));
5459N/A }
5459N/A}
5459N/A
5804N/AG1CardCounts::G1CardCounts(G1CollectedHeap *g1h):
1193N/A _g1h(g1h), _card_counts(NULL),
1193N/A _reserved_max_card_num(0), _committed_max_card_num(0),
5804N/A _committed_size(0) {}
5804N/A
5804N/Avoid G1CardCounts::initialize() {
5804N/A assert(_g1h->max_capacity() > 0, "initialization order");
5804N/A assert(_g1h->capacity() == 0, "initialization order");
5804N/A
5790N/A if (G1ConcRSHotCardLimit > 0) {
5790N/A // The max value we can store in the counts table is
5790N/A // max_jubyte. Guarantee the value of the hot
5459N/A // threshold limit is no more than this.
5459N/A guarantee(G1ConcRSHotCardLimit <= max_jubyte, "sanity");
5459N/A
5459N/A ModRefBarrierSet* bs = _g1h->mr_bs();
5459N/A guarantee(bs->is_a(BarrierSet::CardTableModRef), "Precondition");
5459N/A _ct_bs = (CardTableModRefBS*)bs;
5459N/A _ct_bot = _ct_bs->byte_for_const(_g1h->reserved_region().start());
5459N/A
5459N/A // Allocate/Reserve the counts table
5459N/A size_t reserved_bytes = _g1h->max_capacity();
5459N/A _reserved_max_card_num = reserved_bytes >> CardTableModRefBS::card_shift;
5459N/A
5459N/A size_t reserved_size = _reserved_max_card_num * sizeof(jbyte);
5459N/A ReservedSpace rs(ReservedSpace::allocation_align_size_up(reserved_size));
5459N/A if (!rs.is_reserved()) {
5459N/A warning("Could not reserve enough space for the card counts table");
5459N/A guarantee(!has_reserved_count_table(), "should be NULL");
5459N/A return;
5459N/A }
5459N/A
5459N/A MemTracker::record_virtual_memory_type((address)rs.base(), mtGC);
5459N/A
5459N/A _card_counts_storage.initialize(rs, 0);
5459N/A _card_counts = (jubyte*) _card_counts_storage.low();
5459N/A }
5459N/A}
5459N/A
5459N/Avoid G1CardCounts::resize(size_t heap_capacity) {
5459N/A // Expand the card counts table to handle a heap with the given capacity.
5459N/A
5459N/A if (!has_reserved_count_table()) {
5459N/A // Don't expand if we failed to reserve the card counts table.
5459N/A return;
5459N/A }
5459N/A
5459N/A assert(_committed_size ==
5459N/A ReservedSpace::allocation_align_size_up(_committed_size),
5459N/A err_msg("Unaligned? committed_size: " SIZE_FORMAT, _committed_size));
5459N/A
5459N/A // Verify that the committed space for the card counts matches our
5459N/A // committed max card num. Note for some allocation alignments, the
5459N/A // amount of space actually committed for the counts table will be able
5459N/A // to span more cards than the number spanned by the maximum heap.
5459N/A size_t prev_committed_size = _committed_size;
5459N/A size_t prev_committed_card_num = committed_to_card_num(prev_committed_size);
5459N/A
5459N/A assert(prev_committed_card_num == _committed_max_card_num,
5459N/A err_msg("Card mismatch: "
5459N/A "prev: " SIZE_FORMAT ", "
5459N/A "committed: "SIZE_FORMAT", "
5459N/A "reserved: "SIZE_FORMAT,
5459N/A prev_committed_card_num, _committed_max_card_num, _reserved_max_card_num));
5459N/A
5459N/A size_t new_size = (heap_capacity >> CardTableModRefBS::card_shift) * sizeof(jbyte);
5459N/A size_t new_committed_size = ReservedSpace::allocation_align_size_up(new_size);
5459N/A size_t new_committed_card_num = committed_to_card_num(new_committed_size);
5459N/A
5462N/A if (_committed_max_card_num < new_committed_card_num) {
5462N/A // we need to expand the backing store for the card counts
5459N/A size_t expand_size = new_committed_size - prev_committed_size;
5459N/A
5459N/A if (!_card_counts_storage.expand_by(expand_size)) {
5459N/A warning("Card counts table backing store commit failure");
5459N/A return;
5459N/A }
5459N/A assert(_card_counts_storage.committed_size() == new_committed_size,
5459N/A "expansion commit failure");
5459N/A
5459N/A _committed_size = new_committed_size;
5459N/A _committed_max_card_num = new_committed_card_num;
5459N/A
5459N/A clear_range(prev_committed_card_num, _committed_max_card_num);
5459N/A }
5459N/A}
5459N/A
5459N/Auint G1CardCounts::add_card_count(jbyte* card_ptr) {
5459N/A // Returns the number of times the card has been refined.
5459N/A // If we failed to reserve/commit the counts table, return 0.
5459N/A // If card_ptr is beyond the committed end of the counts table,
5459N/A // return 0.
5459N/A // Otherwise return the actual count.
5459N/A // Unless G1ConcRSHotCardLimit has been set appropriately,
5459N/A // returning 0 will result in the card being considered
5459N/A // cold and will be refined immediately.
5459N/A uint count = 0;
5459N/A if (has_count_table()) {
5459N/A size_t card_num = ptr_2_card_num(card_ptr);
5459N/A if (card_num < _committed_max_card_num) {
5459N/A count = (uint) _card_counts[card_num];
5459N/A if (count < G1ConcRSHotCardLimit) {
5459N/A _card_counts[card_num] =
5459N/A (jubyte)(MIN2((uintx)(_card_counts[card_num] + 1), G1ConcRSHotCardLimit));
5459N/A }
5459N/A }
5459N/A }
5459N/A return count;
5459N/A}
5459N/A
5459N/Abool G1CardCounts::is_hot(uint count) {
5459N/A return (count >= G1ConcRSHotCardLimit);
5459N/A}
5459N/A
5459N/Avoid G1CardCounts::clear_region(HeapRegion* hr) {
5459N/A assert(!hr->isHumongous(), "Should have been cleared");
5459N/A if (has_count_table()) {
5459N/A HeapWord* bottom = hr->bottom();
1193N/A
5459N/A // We use the last address in hr as hr could be the
5459N/A // last region in the heap. In which case trying to find
5459N/A // the card for hr->end() will be an OOB accesss to the
5459N/A // card table.
5459N/A HeapWord* last = hr->end() - 1;
5459N/A assert(_g1h->g1_committed().contains(last),
5459N/A err_msg("last not in committed: "
5459N/A "last: " PTR_FORMAT ", "
5459N/A "committed: [" PTR_FORMAT ", " PTR_FORMAT ")",
5459N/A last,
5459N/A _g1h->g1_committed().start(),
5459N/A _g1h->g1_committed().end()));
5459N/A
5459N/A const jbyte* from_card_ptr = _ct_bs->byte_for_const(bottom);
5459N/A const jbyte* last_card_ptr = _ct_bs->byte_for_const(last);
5459N/A
5459N/A#ifdef ASSERT
5459N/A HeapWord* start_addr = _ct_bs->addr_for(from_card_ptr);
5459N/A assert(start_addr == hr->bottom(), "alignment");
5459N/A HeapWord* last_addr = _ct_bs->addr_for(last_card_ptr);
5459N/A assert((last_addr + CardTableModRefBS::card_size_in_words) == hr->end(), "alignment");
5459N/A#endif // ASSERT
5459N/A
5459N/A // Clear the counts for the (exclusive) card range.
5459N/A size_t from_card_num = ptr_2_card_num(from_card_ptr);
5459N/A size_t to_card_num = ptr_2_card_num(last_card_ptr) + 1;
5459N/A clear_range(from_card_num, to_card_num);
5459N/A }
5459N/A}
5459N/A
5459N/Avoid G1CardCounts::clear_all() {
5459N/A assert(SafepointSynchronize::is_at_safepoint(), "don't call this otherwise");
5459N/A clear_range((size_t)0, _committed_max_card_num);
5459N/A}
5459N/A
5459N/AG1CardCounts::~G1CardCounts() {
5459N/A if (has_reserved_count_table()) {
1193N/A _card_counts_storage.release();
5459N/A }
5459N/A}
5459N/A
5459N/A