Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/cardTableModRefBS.hpp
32285 views
/*1* Copyright (c) 2000, 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#ifndef SHARE_VM_MEMORY_CARDTABLEMODREFBS_HPP25#define SHARE_VM_MEMORY_CARDTABLEMODREFBS_HPP2627#include "memory/modRefBarrierSet.hpp"28#include "oops/oop.hpp"29#include "oops/oop.inline2.hpp"3031// This kind of "BarrierSet" allows a "CollectedHeap" to detect and32// enumerate ref fields that have been modified (since the last33// enumeration.)3435// As it currently stands, this barrier is *imprecise*: when a ref field in36// an object "o" is modified, the card table entry for the card containing37// the head of "o" is dirtied, not necessarily the card containing the38// modified field itself. For object arrays, however, the barrier *is*39// precise; only the card containing the modified element is dirtied.40// Any MemRegionClosures used to scan dirty cards should take these41// considerations into account.4243class Generation;44class OopsInGenClosure;45class DirtyCardToOopClosure;46class ClearNoncleanCardWrapper;4748class CardTableModRefBS: public ModRefBarrierSet {49// Some classes get to look at some private stuff.50friend class BytecodeInterpreter;51friend class VMStructs;52friend class CardTableRS;53friend class CheckForUnmarkedOops; // Needs access to raw card bytes.54friend class SharkBuilder;55#ifndef PRODUCT56// For debugging.57friend class GuaranteeNotModClosure;58#endif59protected:6061enum CardValues {62clean_card = -1,63// The mask contains zeros in places for all other values.64clean_card_mask = clean_card - 31,6566dirty_card = 0,67precleaned_card = 1,68claimed_card = 2,69deferred_card = 4,70last_card = 8,71CT_MR_BS_last_reserved = 1672};7374// a word's worth (row) of clean card values75static const intptr_t clean_card_row = (intptr_t)(-1);7677// dirty and precleaned are equivalent wrt younger_refs_iter.78static bool card_is_dirty_wrt_gen_iter(jbyte cv) {79return cv == dirty_card || cv == precleaned_card;80}8182// Returns "true" iff the value "cv" will cause the card containing it83// to be scanned in the current traversal. May be overridden by84// subtypes.85virtual bool card_will_be_scanned(jbyte cv) {86return CardTableModRefBS::card_is_dirty_wrt_gen_iter(cv);87}8889// Returns "true" iff the value "cv" may have represented a dirty card at90// some point.91virtual bool card_may_have_been_dirty(jbyte cv) {92return card_is_dirty_wrt_gen_iter(cv);93}9495// The declaration order of these const fields is important; see the96// constructor before changing.97const MemRegion _whole_heap; // the region covered by the card table98size_t _guard_index; // index of very last element in the card99// table; it is set to a guard value100// (last_card) and should never be modified101size_t _last_valid_index; // index of the last valid element102const size_t _page_size; // page size used when mapping _byte_map103size_t _byte_map_size; // in bytes104jbyte* _byte_map; // the card marking array105106int _cur_covered_regions;107// The covered regions should be in address order.108MemRegion* _covered;109// The committed regions correspond one-to-one to the covered regions.110// They represent the card-table memory that has been committed to service111// the corresponding covered region. It may be that committed region for112// one covered region corresponds to a larger region because of page-size113// roundings. Thus, a committed region for one covered region may114// actually extend onto the card-table space for the next covered region.115MemRegion* _committed;116117// The last card is a guard card, and we commit the page for it so118// we can use the card for verification purposes. We make sure we never119// uncommit the MemRegion for that page.120MemRegion _guard_region;121122protected:123// Initialization utilities; covered_words is the size of the covered region124// in, um, words.125inline size_t cards_required(size_t covered_words) {126// Add one for a guard card, used to detect errors.127const size_t words = align_size_up(covered_words, card_size_in_words);128return words / card_size_in_words + 1;129}130131inline size_t compute_byte_map_size();132133// Finds and return the index of the region, if any, to which the given134// region would be contiguous. If none exists, assign a new region and135// returns its index. Requires that no more than the maximum number of136// covered regions defined in the constructor are ever in use.137int find_covering_region_by_base(HeapWord* base);138139// Same as above, but finds the region containing the given address140// instead of starting at a given base address.141int find_covering_region_containing(HeapWord* addr);142143// Resize one of the regions covered by the remembered set.144virtual void resize_covered_region(MemRegion new_region);145146// Returns the leftmost end of a committed region corresponding to a147// covered region before covered region "ind", or else "NULL" if "ind" is148// the first covered region.149HeapWord* largest_prev_committed_end(int ind) const;150151// Returns the part of the region mr that doesn't intersect with152// any committed region other than self. Used to prevent uncommitting153// regions that are also committed by other regions. Also protects154// against uncommitting the guard region.155MemRegion committed_unique_to_self(int self, MemRegion mr) const;156157// Mapping from address to card marking array entry158jbyte* byte_for(const void* p) const {159assert(_whole_heap.contains(p),160err_msg("Attempt to access p = " PTR_FORMAT " out of bounds of "161" card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")",162p2i(p), p2i(_whole_heap.start()), p2i(_whole_heap.end())));163jbyte* result = &byte_map_base[uintptr_t(p) >> card_shift];164assert(result >= _byte_map && result < _byte_map + _byte_map_size,165"out of bounds accessor for card marking array");166return result;167}168169// The card table byte one after the card marking array170// entry for argument address. Typically used for higher bounds171// for loops iterating through the card table.172jbyte* byte_after(const void* p) const {173return byte_for(p) + 1;174}175176// Iterate over the portion of the card-table which covers the given177// region mr in the given space and apply cl to any dirty sub-regions178// of mr. Dirty cards are _not_ cleared by the iterator method itself,179// but closures may arrange to do so on their own should they so wish.180void non_clean_card_iterate_serial(MemRegion mr, MemRegionClosure* cl);181182// A variant of the above that will operate in a parallel mode if183// worker threads are available, and clear the dirty cards as it184// processes them.185// XXX ??? MemRegionClosure above vs OopsInGenClosure below XXX186// XXX some new_dcto_cl's take OopClosure's, plus as above there are187// some MemRegionClosures. Clean this up everywhere. XXX188void non_clean_card_iterate_possibly_parallel(Space* sp, MemRegion mr,189OopsInGenClosure* cl, CardTableRS* ct);190191private:192// Work method used to implement non_clean_card_iterate_possibly_parallel()193// above in the parallel case.194void non_clean_card_iterate_parallel_work(Space* sp, MemRegion mr,195OopsInGenClosure* cl, CardTableRS* ct,196int n_threads);197198protected:199// Dirty the bytes corresponding to "mr" (not all of which must be200// covered.)201void dirty_MemRegion(MemRegion mr);202203// Clear (to clean_card) the bytes entirely contained within "mr" (not204// all of which must be covered.)205void clear_MemRegion(MemRegion mr);206207// *** Support for parallel card scanning.208209// This is an array, one element per covered region of the card table.210// Each entry is itself an array, with one element per chunk in the211// covered region. Each entry of these arrays is the lowest non-clean212// card of the corresponding chunk containing part of an object from the213// previous chunk, or else NULL.214typedef jbyte* CardPtr;215typedef CardPtr* CardArr;216CardArr* _lowest_non_clean;217size_t* _lowest_non_clean_chunk_size;218uintptr_t* _lowest_non_clean_base_chunk_index;219volatile int* _last_LNC_resizing_collection;220221// Initializes "lowest_non_clean" to point to the array for the region222// covering "sp", and "lowest_non_clean_base_chunk_index" to the chunk223// index of the corresponding to the first element of that array.224// Ensures that these arrays are of sufficient size, allocating if necessary.225// May be called by several threads concurrently.226void get_LNC_array_for_space(Space* sp,227jbyte**& lowest_non_clean,228uintptr_t& lowest_non_clean_base_chunk_index,229size_t& lowest_non_clean_chunk_size);230231// Returns the number of chunks necessary to cover "mr".232size_t chunks_to_cover(MemRegion mr) {233return (size_t)(addr_to_chunk_index(mr.last()) -234addr_to_chunk_index(mr.start()) + 1);235}236237// Returns the index of the chunk in a stride which238// covers the given address.239uintptr_t addr_to_chunk_index(const void* addr) {240uintptr_t card = (uintptr_t) byte_for(addr);241return card / ParGCCardsPerStrideChunk;242}243244// Apply cl, which must either itself apply dcto_cl or be dcto_cl,245// to the cards in the stride (of n_strides) within the given space.246void process_stride(Space* sp,247MemRegion used,248jint stride, int n_strides,249OopsInGenClosure* cl,250CardTableRS* ct,251jbyte** lowest_non_clean,252uintptr_t lowest_non_clean_base_chunk_index,253size_t lowest_non_clean_chunk_size);254255// Makes sure that chunk boundaries are handled appropriately, by256// adjusting the min_done of dcto_cl, and by using a special card-table257// value to indicate how min_done should be set.258void process_chunk_boundaries(Space* sp,259DirtyCardToOopClosure* dcto_cl,260MemRegion chunk_mr,261MemRegion used,262jbyte** lowest_non_clean,263uintptr_t lowest_non_clean_base_chunk_index,264size_t lowest_non_clean_chunk_size);265266public:267// Constants268enum SomePublicConstants {269card_shift = 9,270card_size = 1 << card_shift,271card_size_in_words = card_size / sizeof(HeapWord)272};273274static int clean_card_val() { return clean_card; }275static int clean_card_mask_val() { return clean_card_mask; }276static int dirty_card_val() { return dirty_card; }277static int claimed_card_val() { return claimed_card; }278static int precleaned_card_val() { return precleaned_card; }279static int deferred_card_val() { return deferred_card; }280281// For RTTI simulation.282bool is_a(BarrierSet::Name bsn) {283return bsn == BarrierSet::CardTableModRef || ModRefBarrierSet::is_a(bsn);284}285286CardTableModRefBS(MemRegion whole_heap, int max_covered_regions);287~CardTableModRefBS();288289virtual void initialize();290291// *** Barrier set functions.292293bool has_write_ref_pre_barrier() { return false; }294295// Record a reference update. Note that these versions are precise!296// The scanning code has to handle the fact that the write barrier may be297// either precise or imprecise. We make non-virtual inline variants of298// these functions here for performance.299protected:300void write_ref_field_work(oop obj, size_t offset, oop newVal);301virtual void write_ref_field_work(void* field, oop newVal, bool release = false);302public:303304bool has_write_ref_array_opt() { return true; }305bool has_write_region_opt() { return true; }306307inline void inline_write_region(MemRegion mr) {308dirty_MemRegion(mr);309}310protected:311void write_region_work(MemRegion mr) {312inline_write_region(mr);313}314public:315316inline void inline_write_ref_array(MemRegion mr) {317dirty_MemRegion(mr);318}319protected:320void write_ref_array_work(MemRegion mr) {321inline_write_ref_array(mr);322}323public:324325bool is_aligned(HeapWord* addr) {326return is_card_aligned(addr);327}328329// *** Card-table-barrier-specific things.330331template <class T> inline void inline_write_ref_field_pre(T* field, oop newVal) {}332333template <class T> inline void inline_write_ref_field(T* field, oop newVal, bool release) {334jbyte* byte = byte_for((void*)field);335if (release) {336// Perform a releasing store if requested.337OrderAccess::release_store((volatile jbyte*) byte, dirty_card);338} else {339*byte = dirty_card;340}341}342343// These are used by G1, when it uses the card table as a temporary data344// structure for card claiming.345bool is_card_dirty(size_t card_index) {346return _byte_map[card_index] == dirty_card_val();347}348349void mark_card_dirty(size_t card_index) {350_byte_map[card_index] = dirty_card_val();351}352353bool is_card_clean(size_t card_index) {354return _byte_map[card_index] == clean_card_val();355}356357// Card marking array base (adjusted for heap low boundary)358// This would be the 0th element of _byte_map, if the heap started at 0x0.359// But since the heap starts at some higher address, this points to somewhere360// before the beginning of the actual _byte_map.361jbyte* byte_map_base;362363// Return true if "p" is at the start of a card.364bool is_card_aligned(HeapWord* p) {365jbyte* pcard = byte_for(p);366return (addr_for(pcard) == p);367}368369HeapWord* align_to_card_boundary(HeapWord* p) {370jbyte* pcard = byte_for(p + card_size_in_words - 1);371return addr_for(pcard);372}373374// The kinds of precision a CardTableModRefBS may offer.375enum PrecisionStyle {376Precise,377ObjHeadPreciseArray378};379380// Tells what style of precision this card table offers.381PrecisionStyle precision() {382return ObjHeadPreciseArray; // Only one supported for now.383}384385// ModRefBS functions.386virtual void invalidate(MemRegion mr, bool whole_heap = false);387void clear(MemRegion mr);388void dirty(MemRegion mr);389390// *** Card-table-RemSet-specific things.391392// Invoke "cl.do_MemRegion" on a set of MemRegions that collectively393// includes all the modified cards (expressing each card as a394// MemRegion). Thus, several modified cards may be lumped into one395// region. The regions are non-overlapping, and are visited in396// *decreasing* address order. (This order aids with imprecise card397// marking, where a dirty card may cause scanning, and summarization398// marking, of objects that extend onto subsequent cards.)399void mod_card_iterate(MemRegionClosure* cl) {400non_clean_card_iterate_serial(_whole_heap, cl);401}402403// Like the "mod_cards_iterate" above, except only invokes the closure404// for cards within the MemRegion "mr" (which is required to be405// card-aligned and sized.)406void mod_card_iterate(MemRegion mr, MemRegionClosure* cl) {407non_clean_card_iterate_serial(mr, cl);408}409410static uintx ct_max_alignment_constraint();411412// Apply closure "cl" to the dirty cards containing some part of413// MemRegion "mr".414void dirty_card_iterate(MemRegion mr, MemRegionClosure* cl);415416// Return the MemRegion corresponding to the first maximal run417// of dirty cards lying completely within MemRegion mr.418// If reset is "true", then sets those card table entries to the given419// value.420MemRegion dirty_card_range_after_reset(MemRegion mr, bool reset,421int reset_val);422423// Provide read-only access to the card table array.424const jbyte* byte_for_const(const void* p) const {425return byte_for(p);426}427const jbyte* byte_after_const(const void* p) const {428return byte_after(p);429}430431// Mapping from card marking array entry to address of first word432HeapWord* addr_for(const jbyte* p) const {433assert(p >= _byte_map && p < _byte_map + _byte_map_size,434"out of bounds access to card marking array");435size_t delta = pointer_delta(p, byte_map_base, sizeof(jbyte));436HeapWord* result = (HeapWord*) (delta << card_shift);437assert(_whole_heap.contains(result),438err_msg("Returning result = " PTR_FORMAT " out of bounds of "439" card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")",440p2i(result), p2i(_whole_heap.start()), p2i(_whole_heap.end())));441return result;442}443444// Mapping from address to card marking array index.445size_t index_for(void* p) {446assert(_whole_heap.contains(p),447err_msg("Attempt to access p = " PTR_FORMAT " out of bounds of "448" card marking array's _whole_heap = [" PTR_FORMAT "," PTR_FORMAT ")",449p2i(p), p2i(_whole_heap.start()), p2i(_whole_heap.end())));450return byte_for(p) - _byte_map;451}452453const jbyte* byte_for_index(const size_t card_index) const {454return _byte_map + card_index;455}456457// Print a description of the memory for the barrier set458virtual void print_on(outputStream* st) const;459460void verify();461void verify_guard();462463// val_equals -> it will check that all cards covered by mr equal val464// !val_equals -> it will check that all cards covered by mr do not equal val465void verify_region(MemRegion mr, jbyte val, bool val_equals) PRODUCT_RETURN;466void verify_not_dirty_region(MemRegion mr) PRODUCT_RETURN;467void verify_dirty_region(MemRegion mr) PRODUCT_RETURN;468469static size_t par_chunk_heapword_alignment() {470return ParGCCardsPerStrideChunk * card_size_in_words;471}472473};474475class CardTableRS;476477// A specialization for the CardTableRS gen rem set.478class CardTableModRefBSForCTRS: public CardTableModRefBS {479CardTableRS* _rs;480protected:481bool card_will_be_scanned(jbyte cv);482bool card_may_have_been_dirty(jbyte cv);483public:484CardTableModRefBSForCTRS(MemRegion whole_heap,485int max_covered_regions) :486CardTableModRefBS(whole_heap, max_covered_regions) {}487488void set_CTRS(CardTableRS* rs) { _rs = rs; }489};490491492#endif // SHARE_VM_MEMORY_CARDTABLEMODREFBS_HPP493494495