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