Path: blob/master/src/hotspot/share/gc/shared/cardTable.cpp
40957 views
/*1* Copyright (c) 2000, 2020, 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/shared/cardTable.hpp"26#include "gc/shared/collectedHeap.hpp"27#include "gc/shared/space.inline.hpp"28#include "logging/log.hpp"29#include "memory/virtualspace.hpp"30#include "runtime/java.hpp"31#include "runtime/os.hpp"32#include "services/memTracker.hpp"33#include "utilities/align.hpp"3435size_t CardTable::compute_byte_map_size() {36assert(_guard_index == cards_required(_whole_heap.word_size()) - 1,37"uninitialized, check declaration order");38assert(_page_size != 0, "uninitialized, check declaration order");39const size_t granularity = os::vm_allocation_granularity();40return align_up(_guard_index + 1, MAX2(_page_size, granularity));41}4243CardTable::CardTable(MemRegion whole_heap) :44_whole_heap(whole_heap),45_guard_index(0),46_last_valid_index(0),47_page_size(os::vm_page_size()),48_byte_map_size(0),49_byte_map(NULL),50_byte_map_base(NULL),51_cur_covered_regions(0),52_covered(MemRegion::create_array(_max_covered_regions, mtGC)),53_committed(MemRegion::create_array(_max_covered_regions, mtGC)),54_guard_region()55{56assert((uintptr_t(_whole_heap.start()) & (card_size - 1)) == 0, "heap must start at card boundary");57assert((uintptr_t(_whole_heap.end()) & (card_size - 1)) == 0, "heap must end at card boundary");5859assert(card_size <= 512, "card_size must be less than 512"); // why?60}6162CardTable::~CardTable() {63MemRegion::destroy_array(_covered, _max_covered_regions);64MemRegion::destroy_array(_committed, _max_covered_regions);65}6667void CardTable::initialize() {68_guard_index = cards_required(_whole_heap.word_size()) - 1;69_last_valid_index = _guard_index - 1;7071_byte_map_size = compute_byte_map_size();7273HeapWord* low_bound = _whole_heap.start();74HeapWord* high_bound = _whole_heap.end();7576_cur_covered_regions = 0;7778const size_t rs_align = _page_size == (size_t) os::vm_page_size() ? 0 :79MAX2(_page_size, (size_t) os::vm_allocation_granularity());80ReservedSpace heap_rs(_byte_map_size, rs_align, _page_size);8182MemTracker::record_virtual_memory_type((address)heap_rs.base(), mtGC);8384os::trace_page_sizes("Card Table", _guard_index + 1, _guard_index + 1,85_page_size, heap_rs.base(), heap_rs.size());86if (!heap_rs.is_reserved()) {87vm_exit_during_initialization("Could not reserve enough space for the "88"card marking array");89}9091// The assembler store_check code will do an unsigned shift of the oop,92// then add it to _byte_map_base, i.e.93//94// _byte_map = _byte_map_base + (uintptr_t(low_bound) >> card_shift)95_byte_map = (CardValue*) heap_rs.base();96_byte_map_base = _byte_map - (uintptr_t(low_bound) >> card_shift);97assert(byte_for(low_bound) == &_byte_map[0], "Checking start of map");98assert(byte_for(high_bound-1) <= &_byte_map[_last_valid_index], "Checking end of map");99100CardValue* guard_card = &_byte_map[_guard_index];101HeapWord* guard_page = align_down((HeapWord*)guard_card, _page_size);102_guard_region = MemRegion(guard_page, _page_size);103os::commit_memory_or_exit((char*)guard_page, _page_size, _page_size,104!ExecMem, "card table last card");105*guard_card = last_card;106107log_trace(gc, barrier)("CardTable::CardTable: ");108log_trace(gc, barrier)(" &_byte_map[0]: " INTPTR_FORMAT " &_byte_map[_last_valid_index]: " INTPTR_FORMAT,109p2i(&_byte_map[0]), p2i(&_byte_map[_last_valid_index]));110log_trace(gc, barrier)(" _byte_map_base: " INTPTR_FORMAT, p2i(_byte_map_base));111}112113int CardTable::find_covering_region_by_base(HeapWord* base) {114int i;115for (i = 0; i < _cur_covered_regions; i++) {116if (_covered[i].start() == base) return i;117if (_covered[i].start() > base) break;118}119// If we didn't find it, create a new one.120assert(_cur_covered_regions < _max_covered_regions,121"too many covered regions");122// Move the ones above up, to maintain sorted order.123for (int j = _cur_covered_regions; j > i; j--) {124_covered[j] = _covered[j-1];125_committed[j] = _committed[j-1];126}127int res = i;128_cur_covered_regions++;129_covered[res].set_start(base);130_covered[res].set_word_size(0);131CardValue* ct_start = byte_for(base);132HeapWord* ct_start_aligned = align_down((HeapWord*)ct_start, _page_size);133_committed[res].set_start(ct_start_aligned);134_committed[res].set_word_size(0);135return res;136}137138int CardTable::find_covering_region_containing(HeapWord* addr) {139for (int i = 0; i < _cur_covered_regions; i++) {140if (_covered[i].contains(addr)) {141return i;142}143}144assert(0, "address outside of heap?");145return -1;146}147148HeapWord* CardTable::largest_prev_committed_end(int ind) const {149HeapWord* max_end = NULL;150for (int j = 0; j < ind; j++) {151HeapWord* this_end = _committed[j].end();152if (this_end > max_end) max_end = this_end;153}154return max_end;155}156157MemRegion CardTable::committed_unique_to_self(int self, MemRegion mr) const {158MemRegion result = mr;159for (int r = 0; r < _cur_covered_regions; r += 1) {160if (r != self) {161result = result.minus(_committed[r]);162}163}164// Never include the guard page.165result = result.minus(_guard_region);166return result;167}168169void CardTable::resize_covered_region(MemRegion new_region) {170// We don't change the start of a region, only the end.171assert(_whole_heap.contains(new_region),172"attempt to cover area not in reserved area");173debug_only(verify_guard();)174// collided is true if the expansion would push into another committed region175debug_only(bool collided = false;)176int const ind = find_covering_region_by_base(new_region.start());177MemRegion const old_region = _covered[ind];178assert(old_region.start() == new_region.start(), "just checking");179if (new_region.word_size() != old_region.word_size()) {180// Commit new or uncommit old pages, if necessary.181MemRegion cur_committed = _committed[ind];182// Extend the end of this _committed region183// to cover the end of any lower _committed regions.184// This forms overlapping regions, but never interior regions.185HeapWord* const max_prev_end = largest_prev_committed_end(ind);186if (max_prev_end > cur_committed.end()) {187cur_committed.set_end(max_prev_end);188}189// Align the end up to a page size (starts are already aligned).190HeapWord* new_end = (HeapWord*) byte_after(new_region.last());191HeapWord* new_end_aligned = align_up(new_end, _page_size);192assert(new_end_aligned >= new_end, "align up, but less");193// Check the other regions (excludes "ind") to ensure that194// the new_end_aligned does not intrude onto the committed195// space of another region.196int ri = 0;197for (ri = ind + 1; ri < _cur_covered_regions; ri++) {198if (new_end_aligned > _committed[ri].start()) {199assert(new_end_aligned <= _committed[ri].end(),200"An earlier committed region can't cover a later committed region");201// Any region containing the new end202// should start at or beyond the region found (ind)203// for the new end (committed regions are not expected to204// be proper subsets of other committed regions).205assert(_committed[ri].start() >= _committed[ind].start(),206"New end of committed region is inconsistent");207new_end_aligned = _committed[ri].start();208// new_end_aligned can be equal to the start of its209// committed region (i.e., of "ind") if a second210// region following "ind" also start at the same location211// as "ind".212assert(new_end_aligned >= _committed[ind].start(),213"New end of committed region is before start");214debug_only(collided = true;)215// Should only collide with 1 region216break;217}218}219#ifdef ASSERT220for (++ri; ri < _cur_covered_regions; ri++) {221assert(!_committed[ri].contains(new_end_aligned),222"New end of committed region is in a second committed region");223}224#endif225// The guard page is always committed and should not be committed over.226// "guarded" is used for assertion checking below and recalls the fact227// that the would-be end of the new committed region would have228// penetrated the guard page.229HeapWord* new_end_for_commit = new_end_aligned;230231DEBUG_ONLY(bool guarded = false;)232if (new_end_for_commit > _guard_region.start()) {233new_end_for_commit = _guard_region.start();234DEBUG_ONLY(guarded = true;)235}236237if (new_end_for_commit > cur_committed.end()) {238// Must commit new pages.239MemRegion const new_committed =240MemRegion(cur_committed.end(), new_end_for_commit);241242assert(!new_committed.is_empty(), "Region should not be empty here");243os::commit_memory_or_exit((char*)new_committed.start(),244new_committed.byte_size(), _page_size,245!ExecMem, "card table expansion");246// Use new_end_aligned (as opposed to new_end_for_commit) because247// the cur_committed region may include the guard region.248} else if (new_end_aligned < cur_committed.end()) {249// Must uncommit pages.250MemRegion const uncommit_region =251committed_unique_to_self(ind, MemRegion(new_end_aligned,252cur_committed.end()));253if (!uncommit_region.is_empty()) {254if (!os::uncommit_memory((char*)uncommit_region.start(),255uncommit_region.byte_size())) {256assert(false, "Card table contraction failed");257// The call failed so don't change the end of the258// committed region. This is better than taking the259// VM down.260new_end_aligned = _committed[ind].end();261}262}263}264// In any case, we can reset the end of the current committed entry.265_committed[ind].set_end(new_end_aligned);266267#ifdef ASSERT268// Check that the last card in the new region is committed according269// to the tables.270bool covered = false;271for (int cr = 0; cr < _cur_covered_regions; cr++) {272if (_committed[cr].contains(new_end - 1)) {273covered = true;274break;275}276}277assert(covered, "Card for end of new region not committed");278#endif279280// The default of 0 is not necessarily clean cards.281CardValue* entry;282if (old_region.last() < _whole_heap.start()) {283entry = byte_for(_whole_heap.start());284} else {285entry = byte_after(old_region.last());286}287assert(index_for(new_region.last()) < _guard_index,288"The guard card will be overwritten");289// This line commented out cleans the newly expanded region and290// not the aligned up expanded region.291// CardValue* const end = byte_after(new_region.last());292CardValue* const end = (CardValue*) new_end_for_commit;293assert((end >= byte_after(new_region.last())) || collided || guarded,294"Expect to be beyond new region unless impacting another region");295// do nothing if we resized downward.296#ifdef ASSERT297for (int ri = 0; ri < _cur_covered_regions; ri++) {298if (ri != ind) {299// The end of the new committed region should not300// be in any existing region unless it matches301// the start of the next region.302assert(!_committed[ri].contains(end) ||303(_committed[ri].start() == (HeapWord*) end),304"Overlapping committed regions");305}306}307#endif308if (entry < end) {309memset(entry, clean_card, pointer_delta(end, entry, sizeof(CardValue)));310}311}312// In any case, the covered size changes.313_covered[ind].set_word_size(new_region.word_size());314315log_trace(gc, barrier)("CardTable::resize_covered_region: ");316log_trace(gc, barrier)(" _covered[%d].start(): " INTPTR_FORMAT " _covered[%d].last(): " INTPTR_FORMAT,317ind, p2i(_covered[ind].start()), ind, p2i(_covered[ind].last()));318log_trace(gc, barrier)(" _committed[%d].start(): " INTPTR_FORMAT " _committed[%d].last(): " INTPTR_FORMAT,319ind, p2i(_committed[ind].start()), ind, p2i(_committed[ind].last()));320log_trace(gc, barrier)(" byte_for(start): " INTPTR_FORMAT " byte_for(last): " INTPTR_FORMAT,321p2i(byte_for(_covered[ind].start())), p2i(byte_for(_covered[ind].last())));322log_trace(gc, barrier)(" addr_for(start): " INTPTR_FORMAT " addr_for(last): " INTPTR_FORMAT,323p2i(addr_for((CardValue*) _committed[ind].start())), p2i(addr_for((CardValue*) _committed[ind].last())));324325// Touch the last card of the covered region to show that it326// is committed (or SEGV).327debug_only((void) (*byte_for(_covered[ind].last()));)328debug_only(verify_guard();)329}330331// Note that these versions are precise! The scanning code has to handle the332// fact that the write barrier may be either precise or imprecise.333void CardTable::dirty_MemRegion(MemRegion mr) {334assert(align_down(mr.start(), HeapWordSize) == mr.start(), "Unaligned start");335assert(align_up (mr.end(), HeapWordSize) == mr.end(), "Unaligned end" );336CardValue* cur = byte_for(mr.start());337CardValue* last = byte_after(mr.last());338while (cur < last) {339*cur = dirty_card;340cur++;341}342}343344void CardTable::clear_MemRegion(MemRegion mr) {345// Be conservative: only clean cards entirely contained within the346// region.347CardValue* cur;348if (mr.start() == _whole_heap.start()) {349cur = byte_for(mr.start());350} else {351assert(mr.start() > _whole_heap.start(), "mr is not covered.");352cur = byte_after(mr.start() - 1);353}354CardValue* last = byte_after(mr.last());355memset(cur, clean_card, pointer_delta(last, cur, sizeof(CardValue)));356}357358void CardTable::clear(MemRegion mr) {359for (int i = 0; i < _cur_covered_regions; i++) {360MemRegion mri = mr.intersection(_covered[i]);361if (!mri.is_empty()) clear_MemRegion(mri);362}363}364365void CardTable::dirty(MemRegion mr) {366CardValue* first = byte_for(mr.start());367CardValue* last = byte_after(mr.last());368memset(first, dirty_card, last-first);369}370371// Unlike several other card table methods, dirty_card_iterate()372// iterates over dirty cards ranges in increasing address order.373void CardTable::dirty_card_iterate(MemRegion mr, MemRegionClosure* cl) {374for (int i = 0; i < _cur_covered_regions; i++) {375MemRegion mri = mr.intersection(_covered[i]);376if (!mri.is_empty()) {377CardValue *cur_entry, *next_entry, *limit;378for (cur_entry = byte_for(mri.start()), limit = byte_for(mri.last());379cur_entry <= limit;380cur_entry = next_entry) {381next_entry = cur_entry + 1;382if (*cur_entry == dirty_card) {383size_t dirty_cards;384// Accumulate maximal dirty card range, starting at cur_entry385for (dirty_cards = 1;386next_entry <= limit && *next_entry == dirty_card;387dirty_cards++, next_entry++);388MemRegion cur_cards(addr_for(cur_entry),389dirty_cards*card_size_in_words);390cl->do_MemRegion(cur_cards);391}392}393}394}395}396397MemRegion CardTable::dirty_card_range_after_reset(MemRegion mr,398bool reset,399int reset_val) {400for (int i = 0; i < _cur_covered_regions; i++) {401MemRegion mri = mr.intersection(_covered[i]);402if (!mri.is_empty()) {403CardValue* cur_entry, *next_entry, *limit;404for (cur_entry = byte_for(mri.start()), limit = byte_for(mri.last());405cur_entry <= limit;406cur_entry = next_entry) {407next_entry = cur_entry + 1;408if (*cur_entry == dirty_card) {409size_t dirty_cards;410// Accumulate maximal dirty card range, starting at cur_entry411for (dirty_cards = 1;412next_entry <= limit && *next_entry == dirty_card;413dirty_cards++, next_entry++);414MemRegion cur_cards(addr_for(cur_entry),415dirty_cards*card_size_in_words);416if (reset) {417for (size_t i = 0; i < dirty_cards; i++) {418cur_entry[i] = reset_val;419}420}421return cur_cards;422}423}424}425}426return MemRegion(mr.end(), mr.end());427}428429uintx CardTable::ct_max_alignment_constraint() {430return card_size * os::vm_page_size();431}432433void CardTable::verify_guard() {434// For product build verification435guarantee(_byte_map[_guard_index] == last_card,436"card table guard has been modified");437}438439void CardTable::invalidate(MemRegion mr) {440assert(align_down(mr.start(), HeapWordSize) == mr.start(), "Unaligned start");441assert(align_up (mr.end(), HeapWordSize) == mr.end(), "Unaligned end" );442for (int i = 0; i < _cur_covered_regions; i++) {443MemRegion mri = mr.intersection(_covered[i]);444if (!mri.is_empty()) dirty_MemRegion(mri);445}446}447448void CardTable::verify() {449verify_guard();450}451452#ifndef PRODUCT453void CardTable::verify_region(MemRegion mr, CardValue val, bool val_equals) {454CardValue* start = byte_for(mr.start());455CardValue* end = byte_for(mr.last());456bool failures = false;457for (CardValue* curr = start; curr <= end; ++curr) {458CardValue curr_val = *curr;459bool failed = (val_equals) ? (curr_val != val) : (curr_val == val);460if (failed) {461if (!failures) {462log_error(gc, verify)("== CT verification failed: [" INTPTR_FORMAT "," INTPTR_FORMAT "]", p2i(start), p2i(end));463log_error(gc, verify)("== %sexpecting value: %d", (val_equals) ? "" : "not ", val);464failures = true;465}466log_error(gc, verify)("== card " PTR_FORMAT " [" PTR_FORMAT "," PTR_FORMAT "], val: %d",467p2i(curr), p2i(addr_for(curr)),468p2i((HeapWord*) (((size_t) addr_for(curr)) + card_size)),469(int) curr_val);470}471}472guarantee(!failures, "there should not have been any failures");473}474475void CardTable::verify_not_dirty_region(MemRegion mr) {476verify_region(mr, dirty_card, false /* val_equals */);477}478479void CardTable::verify_dirty_region(MemRegion mr) {480verify_region(mr, dirty_card, true /* val_equals */);481}482#endif483484void CardTable::print_on(outputStream* st) const {485st->print_cr("Card table byte_map: [" INTPTR_FORMAT "," INTPTR_FORMAT "] _byte_map_base: " INTPTR_FORMAT,486p2i(_byte_map), p2i(_byte_map + _byte_map_size), p2i(_byte_map_base));487}488489490