342N/A/*
3854N/A * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
342N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
342N/A *
342N/A * This code is free software; you can redistribute it and/or modify it
342N/A * under the terms of the GNU General Public License version 2 only, as
342N/A * published by the Free Software Foundation.
342N/A *
342N/A * This code is distributed in the hope that it will be useful, but WITHOUT
342N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
342N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
342N/A * version 2 for more details (a copy is included in the LICENSE file that
342N/A * accompanied this code).
342N/A *
342N/A * You should have received a copy of the GNU General Public License version
342N/A * 2 along with this work; if not, write to the Free Software Foundation,
342N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
342N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
342N/A *
342N/A */
342N/A
1879N/A#ifndef SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONREMSET_HPP
1879N/A#define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONREMSET_HPP
1879N/A
1879N/A#include "gc_implementation/g1/sparsePRT.hpp"
1879N/A
342N/A// Remembered set for a heap region. Represent a set of "cards" that
342N/A// contain pointers into the owner heap region. Cards are defined somewhat
342N/A// abstractly, in terms of what the "BlockOffsetTable" in use can parse.
342N/A
342N/Aclass G1CollectedHeap;
342N/Aclass G1BlockOffsetSharedArray;
342N/Aclass HeapRegion;
342N/Aclass HeapRegionRemSetIterator;
3854N/Aclass PerRegionTable;
342N/Aclass SparsePRT;
342N/A
2058N/A// Essentially a wrapper around SparsePRTCleanupTask. See
2058N/A// sparsePRT.hpp for more details.
2058N/Aclass HRRSCleanupTask : public SparsePRTCleanupTask {
2058N/A};
342N/A
342N/A// The "_coarse_map" is a bitmap with one bit for each region, where set
342N/A// bits indicate that the corresponding region may contain some pointer
342N/A// into the owning region.
342N/A
342N/A// The "_fine_grain_entries" array is an open hash table of PerRegionTables
342N/A// (PRTs), indicating regions for which we're keeping the RS as a set of
342N/A// cards. The strategy is to cap the size of the fine-grain table,
342N/A// deleting an entry and setting the corresponding coarse-grained bit when
342N/A// we would overflow this cap.
342N/A
342N/A// We use a mixture of locking and lock-free techniques here. We allow
342N/A// threads to locate PRTs without locking, but threads attempting to alter
342N/A// a bucket list obtain a lock. This means that any failing attempt to
342N/A// find a PRT must be retried with the lock. It might seem dangerous that
342N/A// a read can find a PRT that is concurrently deleted. This is all right,
342N/A// because:
342N/A//
342N/A// 1) We only actually free PRT's at safe points (though we reuse them at
342N/A// other times).
342N/A// 2) We find PRT's in an attempt to add entries. If a PRT is deleted,
342N/A// it's _coarse_map bit is set, so the that we were attempting to add
342N/A// is represented. If a deleted PRT is re-used, a thread adding a bit,
342N/A// thinking the PRT is for a different region, does no harm.
342N/A
549N/Aclass OtherRegionsTable VALUE_OBJ_CLASS_SPEC {
342N/A friend class HeapRegionRemSetIterator;
342N/A
342N/A G1CollectedHeap* _g1h;
342N/A Mutex _m;
342N/A HeapRegion* _hr;
342N/A
342N/A // These are protected by "_m".
342N/A BitMap _coarse_map;
342N/A size_t _n_coarse_entries;
342N/A static jint _n_coarsenings;
342N/A
3854N/A PerRegionTable** _fine_grain_regions;
3854N/A size_t _n_fine_entries;
342N/A
3919N/A // The fine grain remembered sets are doubly linked together using
3919N/A // their 'next' and 'prev' fields.
3919N/A // This allows fast bulk freeing of all the fine grain remembered
3919N/A // set entries, and fast finding of all of them without iterating
3919N/A // over the _fine_grain_regions table.
3919N/A PerRegionTable * _first_all_fine_prts;
3919N/A PerRegionTable * _last_all_fine_prts;
3919N/A
3854N/A // Used to sample a subset of the fine grain PRTs to determine which
3854N/A // PRT to evict and coarsen.
342N/A size_t _fine_eviction_start;
342N/A static size_t _fine_eviction_stride;
342N/A static size_t _fine_eviction_sample_size;
342N/A
342N/A SparsePRT _sparse_table;
342N/A
342N/A // These are static after init.
342N/A static size_t _max_fine_entries;
342N/A static size_t _mod_max_fine_entries_mask;
342N/A
342N/A // Requires "prt" to be the first element of the bucket list appropriate
342N/A // for "hr". If this list contains an entry for "hr", return it,
342N/A // otherwise return "NULL".
3854N/A PerRegionTable* find_region_table(size_t ind, HeapRegion* hr) const;
342N/A
3854N/A // Find, delete, and return a candidate PerRegionTable, if any exists,
342N/A // adding the deleted region to the coarse bitmap. Requires the caller
342N/A // to hold _m, and the fine-grain table to be full.
3854N/A PerRegionTable* delete_region_table();
342N/A
342N/A // If a PRT for "hr" is in the bucket list indicated by "ind" (which must
342N/A // be the correct index for "hr"), delete it and return true; else return
342N/A // false.
342N/A bool del_single_region_table(size_t ind, HeapRegion* hr);
342N/A
342N/A // Indexed by thread X heap region, to minimize thread contention.
342N/A static int** _from_card_cache;
342N/A static size_t _from_card_cache_max_regions;
342N/A static size_t _from_card_cache_mem_size;
342N/A
3919N/A // link/add the given fine grain remembered set into the "all" list
3919N/A void link_to_all(PerRegionTable * prt);
3919N/A // unlink/remove the given fine grain remembered set into the "all" list
3919N/A void unlink_from_all(PerRegionTable * prt);
3919N/A
342N/Apublic:
342N/A OtherRegionsTable(HeapRegion* hr);
342N/A
342N/A HeapRegion* hr() const { return _hr; }
342N/A
342N/A // For now. Could "expand" some tables in the future, so that this made
342N/A // sense.
845N/A void add_reference(OopOrNarrowOopStar from, int tid);
342N/A
342N/A // Removes any entries shown by the given bitmaps to contain only dead
342N/A // objects.
342N/A void scrub(CardTableModRefBS* ctbs, BitMap* region_bm, BitMap* card_bm);
342N/A
342N/A // Not const because it takes a lock.
342N/A size_t occupied() const;
342N/A size_t occ_fine() const;
342N/A size_t occ_coarse() const;
342N/A size_t occ_sparse() const;
342N/A
342N/A static jint n_coarsenings() { return _n_coarsenings; }
342N/A
342N/A // Returns size in bytes.
342N/A // Not const because it takes a lock.
342N/A size_t mem_size() const;
342N/A static size_t static_mem_size();
342N/A static size_t fl_mem_size();
342N/A
845N/A bool contains_reference(OopOrNarrowOopStar from) const;
845N/A bool contains_reference_locked(OopOrNarrowOopStar from) const;
342N/A
342N/A void clear();
342N/A
342N/A // Specifically clear the from_card_cache.
342N/A void clear_fcc();
342N/A
342N/A // "from_hr" is being cleared; remove any entries from it.
342N/A void clear_incoming_entry(HeapRegion* from_hr);
342N/A
2058N/A void do_cleanup_work(HRRSCleanupTask* hrrs_cleanup_task);
2058N/A
342N/A // Declare the heap size (in # of regions) to the OtherRegionsTable.
342N/A // (Uses it to initialize from_card_cache).
342N/A static void init_from_card_cache(size_t max_regions);
342N/A
342N/A // Declares that only regions i s.t. 0 <= i < new_n_regs are in use.
342N/A // Make sure any entries for higher regions are invalid.
342N/A static void shrink_from_card_cache(size_t new_n_regs);
342N/A
342N/A static void print_from_card_cache();
342N/A};
342N/A
3863N/Aclass HeapRegionRemSet : public CHeapObj<mtGC> {
342N/A friend class VMStructs;
342N/A friend class HeapRegionRemSetIterator;
342N/A
342N/Apublic:
342N/A enum Event {
342N/A Event_EvacStart, Event_EvacEnd, Event_RSUpdateEnd
342N/A };
342N/A
342N/Aprivate:
342N/A G1BlockOffsetSharedArray* _bosa;
342N/A G1BlockOffsetSharedArray* bosa() const { return _bosa; }
342N/A
342N/A OtherRegionsTable _other_regions;
342N/A
342N/A enum ParIterState { Unclaimed, Claimed, Complete };
1261N/A volatile ParIterState _iter_state;
1261N/A volatile jlong _iter_claimed;
342N/A
342N/A // Unused unless G1RecordHRRSOops is true.
342N/A
342N/A static const int MaxRecorded = 1000000;
845N/A static OopOrNarrowOopStar* _recorded_oops;
845N/A static HeapWord** _recorded_cards;
845N/A static HeapRegion** _recorded_regions;
845N/A static int _n_recorded;
342N/A
342N/A static const int MaxRecordedEvents = 1000;
342N/A static Event* _recorded_events;
342N/A static int* _recorded_event_index;
342N/A static int _n_recorded_events;
342N/A
342N/A static void print_event(outputStream* str, Event evnt);
342N/A
342N/Apublic:
342N/A HeapRegionRemSet(G1BlockOffsetSharedArray* bosa,
342N/A HeapRegion* hr);
342N/A
342N/A static int num_par_rem_sets();
1261N/A static void setup_remset_size();
342N/A
342N/A HeapRegion* hr() const {
342N/A return _other_regions.hr();
342N/A }
342N/A
342N/A size_t occupied() const {
342N/A return _other_regions.occupied();
342N/A }
342N/A size_t occ_fine() const {
342N/A return _other_regions.occ_fine();
342N/A }
342N/A size_t occ_coarse() const {
342N/A return _other_regions.occ_coarse();
342N/A }
342N/A size_t occ_sparse() const {
342N/A return _other_regions.occ_sparse();
342N/A }
342N/A
342N/A static jint n_coarsenings() { return OtherRegionsTable::n_coarsenings(); }
342N/A
3854N/A // Used in the sequential case.
845N/A void add_reference(OopOrNarrowOopStar from) {
3854N/A _other_regions.add_reference(from, 0);
342N/A }
342N/A
3854N/A // Used in the parallel case.
845N/A void add_reference(OopOrNarrowOopStar from, int tid) {
342N/A _other_regions.add_reference(from, tid);
342N/A }
342N/A
342N/A // Removes any entries shown by the given bitmaps to contain only dead
342N/A // objects.
342N/A void scrub(CardTableModRefBS* ctbs, BitMap* region_bm, BitMap* card_bm);
342N/A
342N/A // The region is being reclaimed; clear its remset, and any mention of
342N/A // entries for this region in other remsets.
342N/A void clear();
342N/A
342N/A // Attempt to claim the region. Returns true iff this call caused an
342N/A // atomic transition from Unclaimed to Claimed.
342N/A bool claim_iter();
342N/A // Sets the iteration state to "complete".
342N/A void set_iter_complete();
342N/A // Returns "true" iff the region's iteration is complete.
342N/A bool iter_is_complete();
342N/A
1261N/A // Support for claiming blocks of cards during iteration
1261N/A size_t iter_claimed() const { return (size_t)_iter_claimed; }
1261N/A // Claim the next block of cards
1261N/A size_t iter_claimed_next(size_t step) {
1261N/A size_t current, next;
1261N/A do {
1261N/A current = iter_claimed();
1261N/A next = current + step;
1261N/A } while (Atomic::cmpxchg((jlong)next, &_iter_claimed, (jlong)current) != (jlong)current);
1261N/A return current;
1261N/A }
2602N/A void reset_for_par_iteration();
2602N/A
2602N/A bool verify_ready_for_par_iteration() {
2602N/A return (_iter_state == Unclaimed) && (_iter_claimed == 0);
2602N/A }
1261N/A
342N/A // Initialize the given iterator to iterate over this rem set.
342N/A void init_iterator(HeapRegionRemSetIterator* iter) const;
342N/A
342N/A // The actual # of bytes this hr_remset takes up.
342N/A size_t mem_size() {
342N/A return _other_regions.mem_size()
342N/A // This correction is necessary because the above includes the second
342N/A // part.
342N/A + sizeof(this) - sizeof(OtherRegionsTable);
342N/A }
342N/A
342N/A // Returns the memory occupancy of all static data structures associated
342N/A // with remembered sets.
342N/A static size_t static_mem_size() {
342N/A return OtherRegionsTable::static_mem_size();
342N/A }
342N/A
342N/A // Returns the memory occupancy of all free_list data structures associated
342N/A // with remembered sets.
342N/A static size_t fl_mem_size() {
342N/A return OtherRegionsTable::fl_mem_size();
342N/A }
342N/A
845N/A bool contains_reference(OopOrNarrowOopStar from) const {
342N/A return _other_regions.contains_reference(from);
342N/A }
342N/A void print() const;
342N/A
342N/A // Called during a stop-world phase to perform any deferred cleanups.
342N/A static void cleanup();
342N/A
342N/A // Declare the heap size (in # of regions) to the HeapRegionRemSet(s).
342N/A // (Uses it to initialize from_card_cache).
3681N/A static void init_heap(uint max_regions) {
3681N/A OtherRegionsTable::init_from_card_cache((size_t) max_regions);
342N/A }
342N/A
342N/A // Declares that only regions i s.t. 0 <= i < new_n_regs are in use.
3681N/A static void shrink_heap(uint new_n_regs) {
3681N/A OtherRegionsTable::shrink_from_card_cache((size_t) new_n_regs);
342N/A }
342N/A
342N/A#ifndef PRODUCT
342N/A static void print_from_card_cache() {
342N/A OtherRegionsTable::print_from_card_cache();
342N/A }
342N/A#endif
342N/A
845N/A static void record(HeapRegion* hr, OopOrNarrowOopStar f);
342N/A static void print_recorded();
342N/A static void record_event(Event evnt);
342N/A
2058N/A // These are wrappers for the similarly-named methods on
2058N/A // SparsePRT. Look at sparsePRT.hpp for more details.
2058N/A static void reset_for_cleanup_tasks();
2058N/A void do_cleanup_work(HRRSCleanupTask* hrrs_cleanup_task);
2058N/A static void finish_cleanup_task(HRRSCleanupTask* hrrs_cleanup_task);
2058N/A
342N/A // Run unit tests.
342N/A#ifndef PRODUCT
342N/A static void test();
342N/A#endif
342N/A};
342N/A
3863N/Aclass HeapRegionRemSetIterator : public CHeapObj<mtGC> {
342N/A
342N/A // The region over which we're iterating.
342N/A const HeapRegionRemSet* _hrrs;
342N/A
342N/A // Local caching of HRRS fields.
342N/A const BitMap* _coarse_map;
3854N/A PerRegionTable** _fine_grain_regions;
342N/A
342N/A G1BlockOffsetSharedArray* _bosa;
342N/A G1CollectedHeap* _g1h;
342N/A
342N/A // The number yielded since initialization.
342N/A size_t _n_yielded_fine;
342N/A size_t _n_yielded_coarse;
342N/A size_t _n_yielded_sparse;
342N/A
342N/A // If true we're iterating over the coarse table; if false the fine
342N/A // table.
342N/A enum IterState {
342N/A Sparse,
342N/A Fine,
342N/A Coarse
342N/A };
342N/A IterState _is;
342N/A
342N/A // In both kinds of iteration, heap offset of first card of current
342N/A // region.
342N/A size_t _cur_region_card_offset;
342N/A // Card offset within cur region.
342N/A size_t _cur_region_cur_card;
342N/A
342N/A // Coarse table iteration fields:
342N/A
342N/A // Current region index;
2822N/A int _coarse_cur_region_index;
2822N/A size_t _coarse_cur_region_cur_card;
342N/A
342N/A bool coarse_has_next(size_t& card_index);
342N/A
342N/A // Fine table iteration fields:
342N/A
342N/A // Index of bucket-list we're working on.
342N/A int _fine_array_index;
3854N/A
342N/A // Per Region Table we're doing within current bucket list.
3854N/A PerRegionTable* _fine_cur_prt;
342N/A
342N/A /* SparsePRT::*/ SparsePRTIter _sparse_iter;
342N/A
342N/A void fine_find_next_non_null_prt();
342N/A
342N/A bool fine_has_next();
342N/A bool fine_has_next(size_t& card_index);
342N/A
342N/Apublic:
342N/A // We require an iterator to be initialized before use, so the
342N/A // constructor does little.
342N/A HeapRegionRemSetIterator();
342N/A
342N/A void initialize(const HeapRegionRemSet* hrrs);
342N/A
342N/A // If there remains one or more cards to be yielded, returns true and
342N/A // sets "card_index" to one of those cards (which is then considered
342N/A // yielded.) Otherwise, returns false (and leaves "card_index"
342N/A // undefined.)
342N/A bool has_next(size_t& card_index);
342N/A
342N/A size_t n_yielded_fine() { return _n_yielded_fine; }
342N/A size_t n_yielded_coarse() { return _n_yielded_coarse; }
342N/A size_t n_yielded_sparse() { return _n_yielded_sparse; }
342N/A size_t n_yielded() {
342N/A return n_yielded_fine() + n_yielded_coarse() + n_yielded_sparse();
342N/A }
342N/A};
342N/A
1879N/A#endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONREMSET_HPP