aoqi@0: /* aoqi@0: * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_MEMORY_CARDTABLEMODREFBS_HPP aoqi@0: #define SHARE_VM_MEMORY_CARDTABLEMODREFBS_HPP aoqi@0: aoqi@0: #include "memory/modRefBarrierSet.hpp" aoqi@0: #include "oops/oop.hpp" aoqi@0: #include "oops/oop.inline2.hpp" aoqi@0: aoqi@0: // This kind of "BarrierSet" allows a "CollectedHeap" to detect and aoqi@0: // enumerate ref fields that have been modified (since the last aoqi@0: // enumeration.) aoqi@0: aoqi@0: // As it currently stands, this barrier is *imprecise*: when a ref field in aoqi@0: // an object "o" is modified, the card table entry for the card containing aoqi@0: // the head of "o" is dirtied, not necessarily the card containing the aoqi@0: // modified field itself. For object arrays, however, the barrier *is* aoqi@0: // precise; only the card containing the modified element is dirtied. aoqi@0: // Any MemRegionClosures used to scan dirty cards should take these aoqi@0: // considerations into account. aoqi@0: aoqi@0: class Generation; aoqi@0: class OopsInGenClosure; aoqi@0: class DirtyCardToOopClosure; aoqi@0: class ClearNoncleanCardWrapper; aoqi@0: aoqi@0: class CardTableModRefBS: public ModRefBarrierSet { aoqi@0: // Some classes get to look at some private stuff. aoqi@0: friend class BytecodeInterpreter; aoqi@0: friend class VMStructs; aoqi@0: friend class CardTableRS; aoqi@0: friend class CheckForUnmarkedOops; // Needs access to raw card bytes. aoqi@0: friend class SharkBuilder; aoqi@0: #ifndef PRODUCT aoqi@0: // For debugging. aoqi@0: friend class GuaranteeNotModClosure; aoqi@0: #endif aoqi@0: protected: aoqi@0: aoqi@0: enum CardValues { aoqi@0: clean_card = -1, aoqi@0: // The mask contains zeros in places for all other values. aoqi@0: clean_card_mask = clean_card - 31, aoqi@0: aoqi@0: dirty_card = 0, aoqi@0: precleaned_card = 1, aoqi@0: claimed_card = 2, aoqi@0: deferred_card = 4, aoqi@0: last_card = 8, aoqi@0: CT_MR_BS_last_reserved = 16 aoqi@0: }; aoqi@0: aoqi@0: // a word's worth (row) of clean card values aoqi@0: static const intptr_t clean_card_row = (intptr_t)(-1); aoqi@0: aoqi@0: // dirty and precleaned are equivalent wrt younger_refs_iter. aoqi@0: static bool card_is_dirty_wrt_gen_iter(jbyte cv) { aoqi@0: return cv == dirty_card || cv == precleaned_card; aoqi@0: } aoqi@0: aoqi@0: // Returns "true" iff the value "cv" will cause the card containing it aoqi@0: // to be scanned in the current traversal. May be overridden by aoqi@0: // subtypes. aoqi@0: virtual bool card_will_be_scanned(jbyte cv) { aoqi@0: return CardTableModRefBS::card_is_dirty_wrt_gen_iter(cv); aoqi@0: } aoqi@0: aoqi@0: // Returns "true" iff the value "cv" may have represented a dirty card at aoqi@0: // some point. aoqi@0: virtual bool card_may_have_been_dirty(jbyte cv) { aoqi@0: return card_is_dirty_wrt_gen_iter(cv); aoqi@0: } aoqi@0: aoqi@0: // The declaration order of these const fields is important; see the aoqi@0: // constructor before changing. aoqi@0: const MemRegion _whole_heap; // the region covered by the card table aoqi@0: const size_t _guard_index; // index of very last element in the card aoqi@0: // table; it is set to a guard value aoqi@0: // (last_card) and should never be modified aoqi@0: const size_t _last_valid_index; // index of the last valid element aoqi@0: const size_t _page_size; // page size used when mapping _byte_map aoqi@0: const size_t _byte_map_size; // in bytes aoqi@0: jbyte* _byte_map; // the card marking array aoqi@0: aoqi@0: int _cur_covered_regions; aoqi@0: // The covered regions should be in address order. aoqi@0: MemRegion* _covered; aoqi@0: // The committed regions correspond one-to-one to the covered regions. aoqi@0: // They represent the card-table memory that has been committed to service aoqi@0: // the corresponding covered region. It may be that committed region for aoqi@0: // one covered region corresponds to a larger region because of page-size aoqi@0: // roundings. Thus, a committed region for one covered region may aoqi@0: // actually extend onto the card-table space for the next covered region. aoqi@0: MemRegion* _committed; aoqi@0: aoqi@0: // The last card is a guard card, and we commit the page for it so aoqi@0: // we can use the card for verification purposes. We make sure we never aoqi@0: // uncommit the MemRegion for that page. aoqi@0: MemRegion _guard_region; aoqi@0: aoqi@0: protected: aoqi@0: // Initialization utilities; covered_words is the size of the covered region aoqi@0: // in, um, words. aoqi@0: inline size_t cards_required(size_t covered_words); aoqi@0: inline size_t compute_byte_map_size(); aoqi@0: aoqi@0: // Finds and return the index of the region, if any, to which the given aoqi@0: // region would be contiguous. If none exists, assign a new region and aoqi@0: // returns its index. Requires that no more than the maximum number of aoqi@0: // covered regions defined in the constructor are ever in use. aoqi@0: int find_covering_region_by_base(HeapWord* base); aoqi@0: aoqi@0: // Same as above, but finds the region containing the given address aoqi@0: // instead of starting at a given base address. aoqi@0: int find_covering_region_containing(HeapWord* addr); aoqi@0: aoqi@0: // Resize one of the regions covered by the remembered set. aoqi@0: void resize_covered_region(MemRegion new_region); aoqi@0: aoqi@0: // Returns the leftmost end of a committed region corresponding to a aoqi@0: // covered region before covered region "ind", or else "NULL" if "ind" is aoqi@0: // the first covered region. aoqi@0: HeapWord* largest_prev_committed_end(int ind) const; aoqi@0: aoqi@0: // Returns the part of the region mr that doesn't intersect with aoqi@0: // any committed region other than self. Used to prevent uncommitting aoqi@0: // regions that are also committed by other regions. Also protects aoqi@0: // against uncommitting the guard region. aoqi@0: MemRegion committed_unique_to_self(int self, MemRegion mr) const; aoqi@0: aoqi@0: // Mapping from address to card marking array entry aoqi@0: jbyte* byte_for(const void* p) const { aoqi@0: assert(_whole_heap.contains(p), aoqi@0: err_msg("Attempt to access p = "PTR_FORMAT" out of bounds of " aoqi@0: " card marking array's _whole_heap = ["PTR_FORMAT","PTR_FORMAT")", aoqi@0: p2i(p), p2i(_whole_heap.start()), p2i(_whole_heap.end()))); aoqi@0: jbyte* result = &byte_map_base[uintptr_t(p) >> card_shift]; aoqi@0: assert(result >= _byte_map && result < _byte_map + _byte_map_size, aoqi@0: "out of bounds accessor for card marking array"); aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: // The card table byte one after the card marking array aoqi@0: // entry for argument address. Typically used for higher bounds aoqi@0: // for loops iterating through the card table. aoqi@0: jbyte* byte_after(const void* p) const { aoqi@0: return byte_for(p) + 1; aoqi@0: } aoqi@0: aoqi@0: // Iterate over the portion of the card-table which covers the given aoqi@0: // region mr in the given space and apply cl to any dirty sub-regions aoqi@0: // of mr. Dirty cards are _not_ cleared by the iterator method itself, aoqi@0: // but closures may arrange to do so on their own should they so wish. aoqi@0: void non_clean_card_iterate_serial(MemRegion mr, MemRegionClosure* cl); aoqi@0: aoqi@0: // A variant of the above that will operate in a parallel mode if aoqi@0: // worker threads are available, and clear the dirty cards as it aoqi@0: // processes them. aoqi@0: // XXX ??? MemRegionClosure above vs OopsInGenClosure below XXX aoqi@0: // XXX some new_dcto_cl's take OopClosure's, plus as above there are aoqi@0: // some MemRegionClosures. Clean this up everywhere. XXX aoqi@0: void non_clean_card_iterate_possibly_parallel(Space* sp, MemRegion mr, aoqi@0: OopsInGenClosure* cl, CardTableRS* ct); aoqi@0: aoqi@0: private: aoqi@0: // Work method used to implement non_clean_card_iterate_possibly_parallel() aoqi@0: // above in the parallel case. aoqi@0: void non_clean_card_iterate_parallel_work(Space* sp, MemRegion mr, aoqi@0: OopsInGenClosure* cl, CardTableRS* ct, aoqi@0: int n_threads); aoqi@0: aoqi@0: protected: aoqi@0: // Dirty the bytes corresponding to "mr" (not all of which must be aoqi@0: // covered.) aoqi@0: void dirty_MemRegion(MemRegion mr); aoqi@0: aoqi@0: // Clear (to clean_card) the bytes entirely contained within "mr" (not aoqi@0: // all of which must be covered.) aoqi@0: void clear_MemRegion(MemRegion mr); aoqi@0: aoqi@0: // *** Support for parallel card scanning. aoqi@0: aoqi@0: // This is an array, one element per covered region of the card table. aoqi@0: // Each entry is itself an array, with one element per chunk in the aoqi@0: // covered region. Each entry of these arrays is the lowest non-clean aoqi@0: // card of the corresponding chunk containing part of an object from the aoqi@0: // previous chunk, or else NULL. aoqi@0: typedef jbyte* CardPtr; aoqi@0: typedef CardPtr* CardArr; aoqi@0: CardArr* _lowest_non_clean; aoqi@0: size_t* _lowest_non_clean_chunk_size; aoqi@0: uintptr_t* _lowest_non_clean_base_chunk_index; aoqi@0: int* _last_LNC_resizing_collection; aoqi@0: aoqi@0: // Initializes "lowest_non_clean" to point to the array for the region aoqi@0: // covering "sp", and "lowest_non_clean_base_chunk_index" to the chunk aoqi@0: // index of the corresponding to the first element of that array. aoqi@0: // Ensures that these arrays are of sufficient size, allocating if necessary. aoqi@0: // May be called by several threads concurrently. aoqi@0: void get_LNC_array_for_space(Space* sp, aoqi@0: jbyte**& lowest_non_clean, aoqi@0: uintptr_t& lowest_non_clean_base_chunk_index, aoqi@0: size_t& lowest_non_clean_chunk_size); aoqi@0: aoqi@0: // Returns the number of chunks necessary to cover "mr". aoqi@0: size_t chunks_to_cover(MemRegion mr) { aoqi@0: return (size_t)(addr_to_chunk_index(mr.last()) - aoqi@0: addr_to_chunk_index(mr.start()) + 1); aoqi@0: } aoqi@0: aoqi@0: // Returns the index of the chunk in a stride which aoqi@0: // covers the given address. aoqi@0: uintptr_t addr_to_chunk_index(const void* addr) { aoqi@0: uintptr_t card = (uintptr_t) byte_for(addr); aoqi@0: return card / ParGCCardsPerStrideChunk; aoqi@0: } aoqi@0: aoqi@0: // Apply cl, which must either itself apply dcto_cl or be dcto_cl, aoqi@0: // to the cards in the stride (of n_strides) within the given space. aoqi@0: void process_stride(Space* sp, aoqi@0: MemRegion used, aoqi@0: jint stride, int n_strides, aoqi@0: OopsInGenClosure* cl, aoqi@0: CardTableRS* ct, aoqi@0: jbyte** lowest_non_clean, aoqi@0: uintptr_t lowest_non_clean_base_chunk_index, aoqi@0: size_t lowest_non_clean_chunk_size); aoqi@0: aoqi@0: // Makes sure that chunk boundaries are handled appropriately, by aoqi@0: // adjusting the min_done of dcto_cl, and by using a special card-table aoqi@0: // value to indicate how min_done should be set. aoqi@0: void process_chunk_boundaries(Space* sp, aoqi@0: DirtyCardToOopClosure* dcto_cl, aoqi@0: MemRegion chunk_mr, aoqi@0: MemRegion used, aoqi@0: jbyte** lowest_non_clean, aoqi@0: uintptr_t lowest_non_clean_base_chunk_index, aoqi@0: size_t lowest_non_clean_chunk_size); aoqi@0: aoqi@0: public: aoqi@0: // Constants aoqi@0: enum SomePublicConstants { aoqi@0: card_shift = 9, aoqi@0: card_size = 1 << card_shift, aoqi@0: card_size_in_words = card_size / sizeof(HeapWord) aoqi@0: }; aoqi@0: aoqi@0: static int clean_card_val() { return clean_card; } aoqi@0: static int clean_card_mask_val() { return clean_card_mask; } aoqi@0: static int dirty_card_val() { return dirty_card; } aoqi@0: static int claimed_card_val() { return claimed_card; } aoqi@0: static int precleaned_card_val() { return precleaned_card; } aoqi@0: static int deferred_card_val() { return deferred_card; } aoqi@0: aoqi@0: // For RTTI simulation. aoqi@0: bool is_a(BarrierSet::Name bsn) { aoqi@0: return bsn == BarrierSet::CardTableModRef || ModRefBarrierSet::is_a(bsn); aoqi@0: } aoqi@0: aoqi@0: CardTableModRefBS(MemRegion whole_heap, int max_covered_regions); aoqi@0: ~CardTableModRefBS(); aoqi@0: aoqi@0: // *** Barrier set functions. aoqi@0: aoqi@0: bool has_write_ref_pre_barrier() { return false; } aoqi@0: aoqi@0: // Record a reference update. Note that these versions are precise! aoqi@0: // The scanning code has to handle the fact that the write barrier may be aoqi@0: // either precise or imprecise. We make non-virtual inline variants of aoqi@0: // these functions here for performance. aoqi@0: protected: aoqi@0: void write_ref_field_work(oop obj, size_t offset, oop newVal); aoqi@0: virtual void write_ref_field_work(void* field, oop newVal, bool release = false); aoqi@0: public: aoqi@0: aoqi@0: bool has_write_ref_array_opt() { return true; } aoqi@0: bool has_write_region_opt() { return true; } aoqi@0: aoqi@0: inline void inline_write_region(MemRegion mr) { aoqi@0: dirty_MemRegion(mr); aoqi@0: } aoqi@0: protected: aoqi@0: void write_region_work(MemRegion mr) { aoqi@0: inline_write_region(mr); aoqi@0: } aoqi@0: public: aoqi@0: aoqi@0: inline void inline_write_ref_array(MemRegion mr) { aoqi@0: dirty_MemRegion(mr); aoqi@0: } aoqi@0: protected: aoqi@0: void write_ref_array_work(MemRegion mr) { aoqi@0: inline_write_ref_array(mr); aoqi@0: } aoqi@0: public: aoqi@0: aoqi@0: bool is_aligned(HeapWord* addr) { aoqi@0: return is_card_aligned(addr); aoqi@0: } aoqi@0: aoqi@0: // *** Card-table-barrier-specific things. aoqi@0: aoqi@0: template inline void inline_write_ref_field_pre(T* field, oop newVal) {} aoqi@0: aoqi@0: template inline void inline_write_ref_field(T* field, oop newVal, bool release) { aoqi@0: jbyte* byte = byte_for((void*)field); aoqi@0: if (release) { aoqi@0: // Perform a releasing store if requested. aoqi@0: OrderAccess::release_store((volatile jbyte*) byte, dirty_card); aoqi@0: } else { aoqi@0: *byte = dirty_card; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // These are used by G1, when it uses the card table as a temporary data aoqi@0: // structure for card claiming. aoqi@0: bool is_card_dirty(size_t card_index) { aoqi@0: return _byte_map[card_index] == dirty_card_val(); aoqi@0: } aoqi@0: aoqi@0: void mark_card_dirty(size_t card_index) { aoqi@0: _byte_map[card_index] = dirty_card_val(); aoqi@0: } aoqi@0: aoqi@0: bool is_card_clean(size_t card_index) { aoqi@0: return _byte_map[card_index] == clean_card_val(); aoqi@0: } aoqi@0: aoqi@0: // Card marking array base (adjusted for heap low boundary) aoqi@0: // This would be the 0th element of _byte_map, if the heap started at 0x0. aoqi@0: // But since the heap starts at some higher address, this points to somewhere aoqi@0: // before the beginning of the actual _byte_map. aoqi@0: jbyte* byte_map_base; aoqi@0: aoqi@0: // Return true if "p" is at the start of a card. aoqi@0: bool is_card_aligned(HeapWord* p) { aoqi@0: jbyte* pcard = byte_for(p); aoqi@0: return (addr_for(pcard) == p); aoqi@0: } aoqi@0: aoqi@0: HeapWord* align_to_card_boundary(HeapWord* p) { aoqi@0: jbyte* pcard = byte_for(p + card_size_in_words - 1); aoqi@0: return addr_for(pcard); aoqi@0: } aoqi@0: aoqi@0: // The kinds of precision a CardTableModRefBS may offer. aoqi@0: enum PrecisionStyle { aoqi@0: Precise, aoqi@0: ObjHeadPreciseArray aoqi@0: }; aoqi@0: aoqi@0: // Tells what style of precision this card table offers. aoqi@0: PrecisionStyle precision() { aoqi@0: return ObjHeadPreciseArray; // Only one supported for now. aoqi@0: } aoqi@0: aoqi@0: // ModRefBS functions. aoqi@0: virtual void invalidate(MemRegion mr, bool whole_heap = false); aoqi@0: void clear(MemRegion mr); aoqi@0: void dirty(MemRegion mr); aoqi@0: aoqi@0: // *** Card-table-RemSet-specific things. aoqi@0: aoqi@0: // Invoke "cl.do_MemRegion" on a set of MemRegions that collectively aoqi@0: // includes all the modified cards (expressing each card as a aoqi@0: // MemRegion). Thus, several modified cards may be lumped into one aoqi@0: // region. The regions are non-overlapping, and are visited in aoqi@0: // *decreasing* address order. (This order aids with imprecise card aoqi@0: // marking, where a dirty card may cause scanning, and summarization aoqi@0: // marking, of objects that extend onto subsequent cards.) aoqi@0: void mod_card_iterate(MemRegionClosure* cl) { aoqi@0: non_clean_card_iterate_serial(_whole_heap, cl); aoqi@0: } aoqi@0: aoqi@0: // Like the "mod_cards_iterate" above, except only invokes the closure aoqi@0: // for cards within the MemRegion "mr" (which is required to be aoqi@0: // card-aligned and sized.) aoqi@0: void mod_card_iterate(MemRegion mr, MemRegionClosure* cl) { aoqi@0: non_clean_card_iterate_serial(mr, cl); aoqi@0: } aoqi@0: aoqi@0: static uintx ct_max_alignment_constraint(); aoqi@0: aoqi@0: // Apply closure "cl" to the dirty cards containing some part of aoqi@0: // MemRegion "mr". aoqi@0: void dirty_card_iterate(MemRegion mr, MemRegionClosure* cl); aoqi@0: aoqi@0: // Return the MemRegion corresponding to the first maximal run aoqi@0: // of dirty cards lying completely within MemRegion mr. aoqi@0: // If reset is "true", then sets those card table entries to the given aoqi@0: // value. aoqi@0: MemRegion dirty_card_range_after_reset(MemRegion mr, bool reset, aoqi@0: int reset_val); aoqi@0: aoqi@0: // Provide read-only access to the card table array. aoqi@0: const jbyte* byte_for_const(const void* p) const { aoqi@0: return byte_for(p); aoqi@0: } aoqi@0: const jbyte* byte_after_const(const void* p) const { aoqi@0: return byte_after(p); aoqi@0: } aoqi@0: aoqi@0: // Mapping from card marking array entry to address of first word aoqi@0: HeapWord* addr_for(const jbyte* p) const { aoqi@0: assert(p >= _byte_map && p < _byte_map + _byte_map_size, aoqi@0: "out of bounds access to card marking array"); aoqi@0: size_t delta = pointer_delta(p, byte_map_base, sizeof(jbyte)); aoqi@0: HeapWord* result = (HeapWord*) (delta << card_shift); aoqi@0: assert(_whole_heap.contains(result), aoqi@0: err_msg("Returning result = "PTR_FORMAT" out of bounds of " aoqi@0: " card marking array's _whole_heap = ["PTR_FORMAT","PTR_FORMAT")", aoqi@0: p2i(result), p2i(_whole_heap.start()), p2i(_whole_heap.end()))); aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: // Mapping from address to card marking array index. aoqi@0: size_t index_for(void* p) { aoqi@0: assert(_whole_heap.contains(p), aoqi@0: err_msg("Attempt to access p = "PTR_FORMAT" out of bounds of " aoqi@0: " card marking array's _whole_heap = ["PTR_FORMAT","PTR_FORMAT")", aoqi@0: p2i(p), p2i(_whole_heap.start()), p2i(_whole_heap.end()))); aoqi@0: return byte_for(p) - _byte_map; aoqi@0: } aoqi@0: aoqi@0: const jbyte* byte_for_index(const size_t card_index) const { aoqi@0: return _byte_map + card_index; aoqi@0: } aoqi@0: aoqi@0: // Print a description of the memory for the barrier set aoqi@0: virtual void print_on(outputStream* st) const; aoqi@0: aoqi@0: void verify(); aoqi@0: void verify_guard(); aoqi@0: aoqi@0: // val_equals -> it will check that all cards covered by mr equal val aoqi@0: // !val_equals -> it will check that all cards covered by mr do not equal val aoqi@0: void verify_region(MemRegion mr, jbyte val, bool val_equals) PRODUCT_RETURN; aoqi@0: void verify_not_dirty_region(MemRegion mr) PRODUCT_RETURN; aoqi@0: void verify_dirty_region(MemRegion mr) PRODUCT_RETURN; aoqi@0: aoqi@0: static size_t par_chunk_heapword_alignment() { aoqi@0: return ParGCCardsPerStrideChunk * card_size_in_words; aoqi@0: } aoqi@0: aoqi@0: }; aoqi@0: aoqi@0: class CardTableRS; aoqi@0: aoqi@0: // A specialization for the CardTableRS gen rem set. aoqi@0: class CardTableModRefBSForCTRS: public CardTableModRefBS { aoqi@0: CardTableRS* _rs; aoqi@0: protected: aoqi@0: bool card_will_be_scanned(jbyte cv); aoqi@0: bool card_may_have_been_dirty(jbyte cv); aoqi@0: public: aoqi@0: CardTableModRefBSForCTRS(MemRegion whole_heap, aoqi@0: int max_covered_regions) : aoqi@0: CardTableModRefBS(whole_heap, max_covered_regions) {} aoqi@0: aoqi@0: void set_CTRS(CardTableRS* rs) { _rs = rs; } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: #endif // SHARE_VM_MEMORY_CARDTABLEMODREFBS_HPP