Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/g1/g1BlockOffsetTable.cpp
38921 views
/*1* Copyright (c) 2001, 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/g1BlockOffsetTable.inline.hpp"26#include "gc_implementation/g1/heapRegion.hpp"27#include "memory/space.hpp"28#include "oops/oop.inline.hpp"29#include "runtime/java.hpp"30#include "services/memTracker.hpp"3132PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC3334//////////////////////////////////////////////////////////////////////35// G1BlockOffsetSharedArray36//////////////////////////////////////////////////////////////////////3738G1BlockOffsetSharedArray::G1BlockOffsetSharedArray(MemRegion heap, G1RegionToSpaceMapper* storage) :39_reserved(), _end(NULL), _listener(), _offset_array(NULL) {4041_reserved = heap;42_end = NULL;4344MemRegion bot_reserved = storage->reserved();4546_offset_array = (u_char*)bot_reserved.start();47_end = _reserved.end();4849storage->set_mapping_changed_listener(&_listener);5051if (TraceBlockOffsetTable) {52gclog_or_tty->print_cr("G1BlockOffsetSharedArray::G1BlockOffsetSharedArray: ");53gclog_or_tty->print_cr(" "54" rs.base(): " INTPTR_FORMAT55" rs.size(): " INTPTR_FORMAT56" rs end(): " INTPTR_FORMAT,57bot_reserved.start(), bot_reserved.byte_size(), bot_reserved.end());58}59}6061bool G1BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const {62assert(p >= _reserved.start(), "just checking");63size_t delta = pointer_delta(p, _reserved.start());64return (delta & right_n_bits(LogN_words)) == (size_t)NoBits;65}6667//////////////////////////////////////////////////////////////////////68// G1BlockOffsetArray69//////////////////////////////////////////////////////////////////////7071G1BlockOffsetArray::G1BlockOffsetArray(G1BlockOffsetSharedArray* array,72MemRegion mr) :73G1BlockOffsetTable(mr.start(), mr.end()),74_unallocated_block(_bottom),75_array(array), _gsp(NULL) {76assert(_bottom <= _end, "arguments out of order");77}7879void G1BlockOffsetArray::set_space(G1OffsetTableContigSpace* sp) {80_gsp = sp;81}8283// The arguments follow the normal convention of denoting84// a right-open interval: [start, end)85void86G1BlockOffsetArray:: set_remainder_to_point_to_start(HeapWord* start, HeapWord* end) {8788if (start >= end) {89// The start address is equal to the end address (or to90// the right of the end address) so there are not cards91// that need to be updated..92return;93}9495// Write the backskip value for each region.96//97// offset98// card 2nd 3rd99// | +- 1st | |100// v v v v101// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-102// |x|0|0|0|0|0|0|0|1|1|1|1|1|1| ... |1|1|1|1|2|2|2|2|2|2| ...103// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-104// 11 19 75105// 12106//107// offset card is the card that points to the start of an object108// x - offset value of offset card109// 1st - start of first logarithmic region110// 0 corresponds to logarithmic value N_words + 0 and 2**(3 * 0) = 1111// 2nd - start of second logarithmic region112// 1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8113// 3rd - start of third logarithmic region114// 2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64115//116// integer below the block offset entry is an example of117// the index of the entry118//119// Given an address,120// Find the index for the address121// Find the block offset table entry122// Convert the entry to a back slide123// (e.g., with today's, offset = 0x81 =>124// back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8125// Move back N (e.g., 8) entries and repeat with the126// value of the new entry127//128size_t start_card = _array->index_for(start);129size_t end_card = _array->index_for(end-1);130assert(start ==_array->address_for_index(start_card), "Precondition");131assert(end ==_array->address_for_index(end_card)+N_words, "Precondition");132set_remainder_to_point_to_start_incl(start_card, end_card); // closed interval133}134135// Unlike the normal convention in this code, the argument here denotes136// a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()137// above.138void139G1BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card) {140if (start_card > end_card) {141return;142}143assert(start_card > _array->index_for(_bottom), "Cannot be first card");144assert(_array->offset_array(start_card-1) <= N_words,145"Offset card has an unexpected value");146size_t start_card_for_region = start_card;147u_char offset = max_jubyte;148for (int i = 0; i < BlockOffsetArray::N_powers; i++) {149// -1 so that the the card with the actual offset is counted. Another -1150// so that the reach ends in this region and not at the start151// of the next.152size_t reach = start_card - 1 + (BlockOffsetArray::power_to_cards_back(i+1) - 1);153offset = N_words + i;154if (reach >= end_card) {155_array->set_offset_array(start_card_for_region, end_card, offset);156start_card_for_region = reach + 1;157break;158}159_array->set_offset_array(start_card_for_region, reach, offset);160start_card_for_region = reach + 1;161}162assert(start_card_for_region > end_card, "Sanity check");163DEBUG_ONLY(check_all_cards(start_card, end_card);)164}165166// The card-interval [start_card, end_card] is a closed interval; this167// is an expensive check -- use with care and only under protection of168// suitable flag.169void G1BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const {170171if (end_card < start_card) {172return;173}174guarantee(_array->offset_array(start_card) == N_words, "Wrong value in second card");175for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {176u_char entry = _array->offset_array(c);177if (c - start_card > BlockOffsetArray::power_to_cards_back(1)) {178guarantee(entry > N_words,179err_msg("Should be in logarithmic region - "180"entry: " UINT32_FORMAT ", "181"_array->offset_array(c): " UINT32_FORMAT ", "182"N_words: " UINT32_FORMAT,183entry, _array->offset_array(c), N_words));184}185size_t backskip = BlockOffsetArray::entry_to_cards_back(entry);186size_t landing_card = c - backskip;187guarantee(landing_card >= (start_card - 1), "Inv");188if (landing_card >= start_card) {189guarantee(_array->offset_array(landing_card) <= entry,190err_msg("Monotonicity - landing_card offset: " UINT32_FORMAT ", "191"entry: " UINT32_FORMAT,192_array->offset_array(landing_card), entry));193} else {194guarantee(landing_card == start_card - 1, "Tautology");195// Note that N_words is the maximum offset value196guarantee(_array->offset_array(landing_card) <= N_words,197err_msg("landing card offset: " UINT32_FORMAT ", "198"N_words: " UINT32_FORMAT,199_array->offset_array(landing_card), N_words));200}201}202}203204HeapWord* G1BlockOffsetArray::block_start_unsafe(const void* addr) {205assert(_bottom <= addr && addr < _end,206"addr must be covered by this Array");207// Must read this exactly once because it can be modified by parallel208// allocation.209HeapWord* ub = _unallocated_block;210if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {211assert(ub < _end, "tautology (see above)");212return ub;213}214// Otherwise, find the block start using the table.215HeapWord* q = block_at_or_preceding(addr, false, 0);216return forward_to_block_containing_addr(q, addr);217}218219// This duplicates a little code from the above: unavoidable.220HeapWord*221G1BlockOffsetArray::block_start_unsafe_const(const void* addr) const {222assert(_bottom <= addr && addr < _end,223"addr must be covered by this Array");224// Must read this exactly once because it can be modified by parallel225// allocation.226HeapWord* ub = _unallocated_block;227if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {228assert(ub < _end, "tautology (see above)");229return ub;230}231// Otherwise, find the block start using the table.232HeapWord* q = block_at_or_preceding(addr, false, 0);233HeapWord* n = q + block_size(q);234return forward_to_block_containing_addr_const(q, n, addr);235}236237238HeapWord*239G1BlockOffsetArray::forward_to_block_containing_addr_slow(HeapWord* q,240HeapWord* n,241const void* addr) {242// We're not in the normal case. We need to handle an important subcase243// here: LAB allocation. An allocation previously recorded in the244// offset table was actually a lab allocation, and was divided into245// several objects subsequently. Fix this situation as we answer the246// query, by updating entries as we cross them.247248// If the fist object's end q is at the card boundary. Start refining249// with the corresponding card (the value of the entry will be basically250// set to 0). If the object crosses the boundary -- start from the next card.251size_t n_index = _array->index_for(n);252size_t next_index = _array->index_for(n) + !_array->is_card_boundary(n);253// Calculate a consistent next boundary. If "n" is not at the boundary254// already, step to the boundary.255HeapWord* next_boundary = _array->address_for_index(n_index) +256(n_index == next_index ? 0 : N_words);257assert(next_boundary <= _array->_end,258err_msg("next_boundary is beyond the end of the covered region "259" next_boundary " PTR_FORMAT " _array->_end " PTR_FORMAT,260next_boundary, _array->_end));261if (addr >= gsp()->top()) return gsp()->top();262while (next_boundary < addr) {263while (n <= next_boundary) {264q = n;265oop obj = oop(q);266if (obj->klass_or_null_acquire() == NULL) return q;267n += block_size(q);268}269assert(q <= next_boundary && n > next_boundary, "Consequence of loop");270// [q, n) is the block that crosses the boundary.271alloc_block_work2(&next_boundary, &next_index, q, n);272}273return forward_to_block_containing_addr_const(q, n, addr);274}275276// Note that the committed size of the covered space may have changed,277// so the table size might also wish to change.278void G1BlockOffsetArray::resize(size_t new_word_size) {279HeapWord* new_end = _bottom + new_word_size;280_end = new_end; // update _end281}282283//284// threshold_285// | _index_286// v v287// +-------+-------+-------+-------+-------+288// | i-1 | i | i+1 | i+2 | i+3 |289// +-------+-------+-------+-------+-------+290// ( ^ ]291// block-start292//293void G1BlockOffsetArray::alloc_block_work2(HeapWord** threshold_, size_t* index_,294HeapWord* blk_start, HeapWord* blk_end) {295// For efficiency, do copy-in/copy-out.296HeapWord* threshold = *threshold_;297size_t index = *index_;298299assert(blk_start != NULL && blk_end > blk_start,300"phantom block");301assert(blk_end > threshold, "should be past threshold");302assert(blk_start <= threshold, "blk_start should be at or before threshold");303assert(pointer_delta(threshold, blk_start) <= N_words,304"offset should be <= BlockOffsetSharedArray::N");305assert(Universe::heap()->is_in_reserved(blk_start),306"reference must be into the heap");307assert(Universe::heap()->is_in_reserved(blk_end-1),308"limit must be within the heap");309assert(threshold == _array->_reserved.start() + index*N_words,310"index must agree with threshold");311312DEBUG_ONLY(size_t orig_index = index;)313314// Mark the card that holds the offset into the block. Note315// that _next_offset_index and _next_offset_threshold are not316// updated until the end of this method.317_array->set_offset_array(index, threshold, blk_start);318319// We need to now mark the subsequent cards that this blk spans.320321// Index of card on which blk ends.322size_t end_index = _array->index_for(blk_end - 1);323324// Are there more cards left to be updated?325if (index + 1 <= end_index) {326HeapWord* rem_st = _array->address_for_index(index + 1);327// Calculate rem_end this way because end_index328// may be the last valid index in the covered region.329HeapWord* rem_end = _array->address_for_index(end_index) + N_words;330set_remainder_to_point_to_start(rem_st, rem_end);331}332333index = end_index + 1;334// Calculate threshold_ this way because end_index335// may be the last valid index in the covered region.336threshold = _array->address_for_index(end_index) + N_words;337assert(threshold >= blk_end, "Incorrect offset threshold");338339// index_ and threshold_ updated here.340*threshold_ = threshold;341*index_ = index;342343#ifdef ASSERT344// The offset can be 0 if the block starts on a boundary. That345// is checked by an assertion above.346size_t start_index = _array->index_for(blk_start);347HeapWord* boundary = _array->address_for_index(start_index);348assert((_array->offset_array(orig_index) == 0 &&349blk_start == boundary) ||350(_array->offset_array(orig_index) > 0 &&351_array->offset_array(orig_index) <= N_words),352err_msg("offset array should have been set - "353"orig_index offset: " UINT32_FORMAT ", "354"blk_start: " PTR_FORMAT ", "355"boundary: " PTR_FORMAT,356_array->offset_array(orig_index),357blk_start, boundary));358for (size_t j = orig_index + 1; j <= end_index; j++) {359assert(_array->offset_array(j) > 0 &&360_array->offset_array(j) <=361(u_char) (N_words+BlockOffsetArray::N_powers-1),362err_msg("offset array should have been set - "363UINT32_FORMAT " not > 0 OR "364UINT32_FORMAT " not <= " UINT32_FORMAT,365_array->offset_array(j),366_array->offset_array(j),367(u_char) (N_words+BlockOffsetArray::N_powers-1)));368}369#endif370}371372bool373G1BlockOffsetArray::verify_for_object(HeapWord* obj_start,374size_t word_size) const {375size_t first_card = _array->index_for(obj_start);376size_t last_card = _array->index_for(obj_start + word_size - 1);377if (!_array->is_card_boundary(obj_start)) {378// If the object is not on a card boundary the BOT entry of the379// first card should point to another object so we should not380// check that one.381first_card += 1;382}383for (size_t card = first_card; card <= last_card; card += 1) {384HeapWord* card_addr = _array->address_for_index(card);385HeapWord* block_start = block_start_const(card_addr);386if (block_start != obj_start) {387gclog_or_tty->print_cr("block start: " PTR_FORMAT " is incorrect - "388"card index: " SIZE_FORMAT " "389"card addr: " PTR_FORMAT " BOT entry: %u "390"obj: " PTR_FORMAT " word size: " SIZE_FORMAT " "391"cards: [" SIZE_FORMAT "," SIZE_FORMAT "]",392block_start, card, card_addr,393_array->offset_array(card),394obj_start, word_size, first_card, last_card);395return false;396}397}398return true;399}400401#ifndef PRODUCT402void403G1BlockOffsetArray::print_on(outputStream* out) {404size_t from_index = _array->index_for(_bottom);405size_t to_index = _array->index_for(_end);406out->print_cr(">> BOT for area [" PTR_FORMAT "," PTR_FORMAT ") "407"cards [" SIZE_FORMAT "," SIZE_FORMAT ")",408_bottom, _end, from_index, to_index);409for (size_t i = from_index; i < to_index; ++i) {410out->print_cr(" entry " SIZE_FORMAT_W(8) " | " PTR_FORMAT " : %3u",411i, _array->address_for_index(i),412(uint) _array->offset_array(i));413}414}415#endif // !PRODUCT416417//////////////////////////////////////////////////////////////////////418// G1BlockOffsetArrayContigSpace419//////////////////////////////////////////////////////////////////////420421HeapWord*422G1BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) {423assert(_bottom <= addr && addr < _end,424"addr must be covered by this Array");425HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1);426return forward_to_block_containing_addr(q, addr);427}428429HeapWord*430G1BlockOffsetArrayContigSpace::431block_start_unsafe_const(const void* addr) const {432assert(_bottom <= addr && addr < _end,433"addr must be covered by this Array");434HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1);435HeapWord* n = q + block_size(q);436return forward_to_block_containing_addr_const(q, n, addr);437}438439G1BlockOffsetArrayContigSpace::440G1BlockOffsetArrayContigSpace(G1BlockOffsetSharedArray* array,441MemRegion mr) :442G1BlockOffsetArray(array, mr)443{444_next_offset_threshold = NULL;445_next_offset_index = 0;446}447448HeapWord* G1BlockOffsetArrayContigSpace::initialize_threshold_raw() {449_next_offset_index = _array->index_for_raw(_bottom);450_next_offset_index++;451_next_offset_threshold =452_array->address_for_index_raw(_next_offset_index);453return _next_offset_threshold;454}455456void G1BlockOffsetArrayContigSpace::zero_bottom_entry_raw() {457size_t bottom_index = _array->index_for_raw(_bottom);458assert(_array->address_for_index_raw(bottom_index) == _bottom,459"Precondition of call");460_array->set_offset_array_raw(bottom_index, 0);461}462463HeapWord* G1BlockOffsetArrayContigSpace::initialize_threshold() {464_next_offset_index = _array->index_for(_bottom);465_next_offset_index++;466_next_offset_threshold =467_array->address_for_index(_next_offset_index);468return _next_offset_threshold;469}470471void472G1BlockOffsetArrayContigSpace::set_for_starts_humongous(HeapWord* new_top) {473assert(new_top <= _end, "_end should have already been updated");474475// The first BOT entry should have offset 0.476reset_bot();477alloc_block(_bottom, new_top);478}479480#ifndef PRODUCT481void482G1BlockOffsetArrayContigSpace::print_on(outputStream* out) {483G1BlockOffsetArray::print_on(out);484out->print_cr(" next offset threshold: " PTR_FORMAT, _next_offset_threshold);485out->print_cr(" next offset index: " SIZE_FORMAT, _next_offset_index);486}487#endif // !PRODUCT488489490