ysr@777: /* xdono@1014: * Copyright 2001-2009 Sun Microsystems, Inc. All Rights Reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * ysr@777: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, ysr@777: * CA 95054 USA or visit www.sun.com if you need additional information or ysr@777: * have any questions. ysr@777: * ysr@777: */ ysr@777: ysr@777: #include "incls/_precompiled.incl" ysr@777: #include "incls/_heapRegionRemSet.cpp.incl" ysr@777: ysr@777: #define HRRS_VERBOSE 0 ysr@777: ysr@777: #define PRT_COUNT_OCCUPIED 1 ysr@777: ysr@777: // OtherRegionsTable ysr@777: ysr@777: class PerRegionTable: public CHeapObj { ysr@777: friend class OtherRegionsTable; ysr@777: friend class HeapRegionRemSetIterator; ysr@777: ysr@777: HeapRegion* _hr; ysr@777: BitMap _bm; ysr@777: #if PRT_COUNT_OCCUPIED ysr@777: jint _occupied; ysr@777: #endif ysr@777: PerRegionTable* _next_free; ysr@777: ysr@777: PerRegionTable* next_free() { return _next_free; } ysr@777: void set_next_free(PerRegionTable* prt) { _next_free = prt; } ysr@777: ysr@777: ysr@777: static PerRegionTable* _free_list; ysr@777: ysr@777: #ifdef _MSC_VER ysr@777: // For some reason even though the classes are marked as friend they are unable ysr@777: // to access CardsPerRegion when private/protected. Only the windows c++ compiler ysr@777: // says this Sun CC and linux gcc don't have a problem with access when private ysr@777: ysr@777: public: ysr@777: ysr@777: #endif // _MSC_VER ysr@777: ysr@777: enum SomePrivateConstants { ysr@777: CardsPerRegion = HeapRegion::GrainBytes >> CardTableModRefBS::card_shift ysr@777: }; ysr@777: ysr@777: protected: ysr@777: // We need access in order to union things into the base table. ysr@777: BitMap* bm() { return &_bm; } ysr@777: apetrusenko@980: #if PRT_COUNT_OCCUPIED ysr@777: void recount_occupied() { ysr@777: _occupied = (jint) bm()->count_one_bits(); ysr@777: } apetrusenko@980: #endif ysr@777: ysr@777: PerRegionTable(HeapRegion* hr) : ysr@777: _hr(hr), ysr@777: #if PRT_COUNT_OCCUPIED ysr@777: _occupied(0), ysr@777: #endif ysr@777: _bm(CardsPerRegion, false /* in-resource-area */) ysr@777: {} ysr@777: ysr@777: static void free(PerRegionTable* prt) { ysr@777: while (true) { ysr@777: PerRegionTable* fl = _free_list; ysr@777: prt->set_next_free(fl); ysr@777: PerRegionTable* res = ysr@777: (PerRegionTable*) ysr@777: Atomic::cmpxchg_ptr(prt, &_free_list, fl); ysr@777: if (res == fl) return; ysr@777: } ysr@777: ShouldNotReachHere(); ysr@777: } ysr@777: ysr@777: static PerRegionTable* alloc(HeapRegion* hr) { ysr@777: PerRegionTable* fl = _free_list; ysr@777: while (fl != NULL) { ysr@777: PerRegionTable* nxt = fl->next_free(); ysr@777: PerRegionTable* res = ysr@777: (PerRegionTable*) ysr@777: Atomic::cmpxchg_ptr(nxt, &_free_list, fl); ysr@777: if (res == fl) { ysr@777: fl->init(hr); ysr@777: return fl; ysr@777: } else { ysr@777: fl = _free_list; ysr@777: } ysr@777: } ysr@777: assert(fl == NULL, "Loop condition."); ysr@777: return new PerRegionTable(hr); ysr@777: } ysr@777: ysr@777: void add_card_work(short from_card, bool par) { ysr@777: if (!_bm.at(from_card)) { ysr@777: if (par) { ysr@777: if (_bm.par_at_put(from_card, 1)) { ysr@777: #if PRT_COUNT_OCCUPIED ysr@777: Atomic::inc(&_occupied); ysr@777: #endif ysr@777: } ysr@777: } else { ysr@777: _bm.at_put(from_card, 1); ysr@777: #if PRT_COUNT_OCCUPIED ysr@777: _occupied++; ysr@777: #endif ysr@777: } ysr@777: } ysr@777: } ysr@777: ysr@777: void add_reference_work(oop* from, bool par) { ysr@777: // Must make this robust in case "from" is not in "_hr", because of ysr@777: // concurrency. ysr@777: ysr@777: #if HRRS_VERBOSE ysr@777: gclog_or_tty->print_cr(" PRT::Add_reference_work(" PTR_FORMAT "->" PTR_FORMAT").", ysr@777: from, *from); ysr@777: #endif ysr@777: ysr@777: HeapRegion* loc_hr = hr(); ysr@777: // If the test below fails, then this table was reused concurrently ysr@777: // with this operation. This is OK, since the old table was coarsened, ysr@777: // and adding a bit to the new table is never incorrect. ysr@777: if (loc_hr->is_in_reserved(from)) { ysr@777: size_t hw_offset = pointer_delta((HeapWord*)from, loc_hr->bottom()); ysr@777: size_t from_card = ysr@777: hw_offset >> ysr@777: (CardTableModRefBS::card_shift - LogHeapWordSize); ysr@777: ysr@777: add_card_work((short) from_card, par); ysr@777: } ysr@777: } ysr@777: ysr@777: public: ysr@777: ysr@777: HeapRegion* hr() const { return _hr; } ysr@777: ysr@777: #if PRT_COUNT_OCCUPIED ysr@777: jint occupied() const { ysr@777: // Overkill, but if we ever need it... ysr@777: // guarantee(_occupied == _bm.count_one_bits(), "Check"); ysr@777: return _occupied; ysr@777: } ysr@777: #else ysr@777: jint occupied() const { ysr@777: return _bm.count_one_bits(); ysr@777: } ysr@777: #endif ysr@777: ysr@777: void init(HeapRegion* hr) { ysr@777: _hr = hr; ysr@777: #if PRT_COUNT_OCCUPIED ysr@777: _occupied = 0; ysr@777: #endif ysr@777: _bm.clear(); ysr@777: } ysr@777: ysr@777: void add_reference(oop* from) { ysr@777: add_reference_work(from, /*parallel*/ true); ysr@777: } ysr@777: ysr@777: void seq_add_reference(oop* from) { ysr@777: add_reference_work(from, /*parallel*/ false); ysr@777: } ysr@777: ysr@777: void scrub(CardTableModRefBS* ctbs, BitMap* card_bm) { ysr@777: HeapWord* hr_bot = hr()->bottom(); swamyv@924: size_t hr_first_card_index = ctbs->index_for(hr_bot); ysr@777: bm()->set_intersection_at_offset(*card_bm, hr_first_card_index); ysr@777: #if PRT_COUNT_OCCUPIED ysr@777: recount_occupied(); ysr@777: #endif ysr@777: } ysr@777: ysr@777: void add_card(short from_card_index) { ysr@777: add_card_work(from_card_index, /*parallel*/ true); ysr@777: } ysr@777: ysr@777: void seq_add_card(short from_card_index) { ysr@777: add_card_work(from_card_index, /*parallel*/ false); ysr@777: } ysr@777: ysr@777: // (Destructively) union the bitmap of the current table into the given ysr@777: // bitmap (which is assumed to be of the same size.) ysr@777: void union_bitmap_into(BitMap* bm) { ysr@777: bm->set_union(_bm); ysr@777: } ysr@777: ysr@777: // Mem size in bytes. ysr@777: size_t mem_size() const { ysr@777: return sizeof(this) + _bm.size_in_words() * HeapWordSize; ysr@777: } ysr@777: ysr@777: static size_t fl_mem_size() { ysr@777: PerRegionTable* cur = _free_list; ysr@777: size_t res = 0; ysr@777: while (cur != NULL) { ysr@777: res += sizeof(PerRegionTable); ysr@777: cur = cur->next_free(); ysr@777: } ysr@777: return res; ysr@777: } ysr@777: ysr@777: // Requires "from" to be in "hr()". ysr@777: bool contains_reference(oop* from) const { ysr@777: assert(hr()->is_in_reserved(from), "Precondition."); ysr@777: size_t card_ind = pointer_delta(from, hr()->bottom(), ysr@777: CardTableModRefBS::card_size); ysr@777: return _bm.at(card_ind); ysr@777: } ysr@777: }; ysr@777: ysr@777: PerRegionTable* PerRegionTable::_free_list = NULL; ysr@777: ysr@777: ysr@777: #define COUNT_PAR_EXPANDS 0 ysr@777: ysr@777: #if COUNT_PAR_EXPANDS ysr@777: static jint n_par_expands = 0; ysr@777: static jint n_par_contracts = 0; ysr@777: static jint par_expand_list_len = 0; ysr@777: static jint max_par_expand_list_len = 0; ysr@777: ysr@777: static void print_par_expand() { ysr@777: Atomic::inc(&n_par_expands); ysr@777: Atomic::inc(&par_expand_list_len); ysr@777: if (par_expand_list_len > max_par_expand_list_len) { ysr@777: max_par_expand_list_len = par_expand_list_len; ysr@777: } ysr@777: if ((n_par_expands % 10) == 0) { ysr@777: gclog_or_tty->print_cr("\n\n%d par expands: %d contracts, " ysr@777: "len = %d, max_len = %d\n.", ysr@777: n_par_expands, n_par_contracts, par_expand_list_len, ysr@777: max_par_expand_list_len); ysr@777: } ysr@777: } ysr@777: #endif ysr@777: ysr@777: class PosParPRT: public PerRegionTable { ysr@777: PerRegionTable** _par_tables; ysr@777: ysr@777: enum SomePrivateConstants { ysr@777: ReserveParTableExpansion = 1 ysr@777: }; ysr@777: ysr@777: void par_expand() { ysr@777: int n = HeapRegionRemSet::num_par_rem_sets()-1; ysr@777: if (n <= 0) return; ysr@777: if (_par_tables == NULL) { ysr@777: PerRegionTable* res = ysr@777: (PerRegionTable*) ysr@777: Atomic::cmpxchg_ptr((PerRegionTable*)ReserveParTableExpansion, ysr@777: &_par_tables, NULL); ysr@777: if (res != NULL) return; ysr@777: // Otherwise, we reserved the right to do the expansion. ysr@777: ysr@777: PerRegionTable** ptables = NEW_C_HEAP_ARRAY(PerRegionTable*, n); ysr@777: for (int i = 0; i < n; i++) { ysr@777: PerRegionTable* ptable = PerRegionTable::alloc(hr()); ysr@777: ptables[i] = ptable; ysr@777: } ysr@777: // Here we do not need an atomic. ysr@777: _par_tables = ptables; ysr@777: #if COUNT_PAR_EXPANDS ysr@777: print_par_expand(); ysr@777: #endif ysr@777: // We must put this table on the expanded list. ysr@777: PosParPRT* exp_head = _par_expanded_list; ysr@777: while (true) { ysr@777: set_next_par_expanded(exp_head); ysr@777: PosParPRT* res = ysr@777: (PosParPRT*) ysr@777: Atomic::cmpxchg_ptr(this, &_par_expanded_list, exp_head); ysr@777: if (res == exp_head) return; ysr@777: // Otherwise. ysr@777: exp_head = res; ysr@777: } ysr@777: ShouldNotReachHere(); ysr@777: } ysr@777: } ysr@777: ysr@777: void par_contract() { ysr@777: assert(_par_tables != NULL, "Precondition."); ysr@777: int n = HeapRegionRemSet::num_par_rem_sets()-1; ysr@777: for (int i = 0; i < n; i++) { ysr@777: _par_tables[i]->union_bitmap_into(bm()); ysr@777: PerRegionTable::free(_par_tables[i]); ysr@777: _par_tables[i] = NULL; ysr@777: } ysr@777: #if PRT_COUNT_OCCUPIED ysr@777: // We must recount the "occupied." ysr@777: recount_occupied(); ysr@777: #endif ysr@777: FREE_C_HEAP_ARRAY(PerRegionTable*, _par_tables); ysr@777: _par_tables = NULL; ysr@777: #if COUNT_PAR_EXPANDS ysr@777: Atomic::inc(&n_par_contracts); ysr@777: Atomic::dec(&par_expand_list_len); ysr@777: #endif ysr@777: } ysr@777: ysr@777: static PerRegionTable** _par_table_fl; ysr@777: ysr@777: PosParPRT* _next; ysr@777: ysr@777: static PosParPRT* _free_list; ysr@777: ysr@777: PerRegionTable** par_tables() const { ysr@777: assert(uintptr_t(NULL) == 0, "Assumption."); ysr@777: if (uintptr_t(_par_tables) <= ReserveParTableExpansion) ysr@777: return NULL; ysr@777: else ysr@777: return _par_tables; ysr@777: } ysr@777: ysr@777: PosParPRT* _next_par_expanded; ysr@777: PosParPRT* next_par_expanded() { return _next_par_expanded; } ysr@777: void set_next_par_expanded(PosParPRT* ppprt) { _next_par_expanded = ppprt; } ysr@777: static PosParPRT* _par_expanded_list; ysr@777: ysr@777: public: ysr@777: ysr@777: PosParPRT(HeapRegion* hr) : PerRegionTable(hr), _par_tables(NULL) {} ysr@777: ysr@777: jint occupied() const { ysr@777: jint res = PerRegionTable::occupied(); ysr@777: if (par_tables() != NULL) { ysr@777: for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets()-1; i++) { ysr@777: res += par_tables()[i]->occupied(); ysr@777: } ysr@777: } ysr@777: return res; ysr@777: } ysr@777: ysr@777: void init(HeapRegion* hr) { ysr@777: PerRegionTable::init(hr); ysr@777: _next = NULL; ysr@777: if (par_tables() != NULL) { ysr@777: for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets()-1; i++) { ysr@777: par_tables()[i]->init(hr); ysr@777: } ysr@777: } ysr@777: } ysr@777: ysr@777: static void free(PosParPRT* prt) { ysr@777: while (true) { ysr@777: PosParPRT* fl = _free_list; ysr@777: prt->set_next(fl); ysr@777: PosParPRT* res = ysr@777: (PosParPRT*) ysr@777: Atomic::cmpxchg_ptr(prt, &_free_list, fl); ysr@777: if (res == fl) return; ysr@777: } ysr@777: ShouldNotReachHere(); ysr@777: } ysr@777: ysr@777: static PosParPRT* alloc(HeapRegion* hr) { ysr@777: PosParPRT* fl = _free_list; ysr@777: while (fl != NULL) { ysr@777: PosParPRT* nxt = fl->next(); ysr@777: PosParPRT* res = ysr@777: (PosParPRT*) ysr@777: Atomic::cmpxchg_ptr(nxt, &_free_list, fl); ysr@777: if (res == fl) { ysr@777: fl->init(hr); ysr@777: return fl; ysr@777: } else { ysr@777: fl = _free_list; ysr@777: } ysr@777: } ysr@777: assert(fl == NULL, "Loop condition."); ysr@777: return new PosParPRT(hr); ysr@777: } ysr@777: ysr@777: PosParPRT* next() const { return _next; } ysr@777: void set_next(PosParPRT* nxt) { _next = nxt; } ysr@777: PosParPRT** next_addr() { return &_next; } ysr@777: ysr@777: void add_reference(oop* from, int tid) { ysr@777: // Expand if necessary. ysr@777: PerRegionTable** pt = par_tables(); ysr@777: if (par_tables() == NULL && tid > 0 && hr()->is_gc_alloc_region()) { ysr@777: par_expand(); ysr@777: pt = par_tables(); ysr@777: } ysr@777: if (pt != NULL) { ysr@777: // We always have to assume that mods to table 0 are in parallel, ysr@777: // because of the claiming scheme in parallel expansion. A thread ysr@777: // with tid != 0 that finds the table to be NULL, but doesn't succeed ysr@777: // in claiming the right of expanding it, will end up in the else ysr@777: // clause of the above if test. That thread could be delayed, and a ysr@777: // thread 0 add reference could see the table expanded, and come ysr@777: // here. Both threads would be adding in parallel. But we get to ysr@777: // not use atomics for tids > 0. ysr@777: if (tid == 0) { ysr@777: PerRegionTable::add_reference(from); ysr@777: } else { ysr@777: pt[tid-1]->seq_add_reference(from); ysr@777: } ysr@777: } else { ysr@777: // Not expanded -- add to the base table. ysr@777: PerRegionTable::add_reference(from); ysr@777: } ysr@777: } ysr@777: ysr@777: void scrub(CardTableModRefBS* ctbs, BitMap* card_bm) { ysr@777: assert(_par_tables == NULL, "Precondition"); ysr@777: PerRegionTable::scrub(ctbs, card_bm); ysr@777: } ysr@777: ysr@777: size_t mem_size() const { ysr@777: size_t res = ysr@777: PerRegionTable::mem_size() + sizeof(this) - sizeof(PerRegionTable); ysr@777: if (_par_tables != NULL) { ysr@777: for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets()-1; i++) { ysr@777: res += _par_tables[i]->mem_size(); ysr@777: } ysr@777: } ysr@777: return res; ysr@777: } ysr@777: ysr@777: static size_t fl_mem_size() { ysr@777: PosParPRT* cur = _free_list; ysr@777: size_t res = 0; ysr@777: while (cur != NULL) { ysr@777: res += sizeof(PosParPRT); ysr@777: cur = cur->next(); ysr@777: } ysr@777: return res; ysr@777: } ysr@777: ysr@777: bool contains_reference(oop* from) const { ysr@777: if (PerRegionTable::contains_reference(from)) return true; ysr@777: if (_par_tables != NULL) { ysr@777: for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets()-1; i++) { ysr@777: if (_par_tables[i]->contains_reference(from)) return true; ysr@777: } ysr@777: } ysr@777: return false; ysr@777: } ysr@777: ysr@777: static void par_contract_all(); ysr@777: ysr@777: }; ysr@777: ysr@777: void PosParPRT::par_contract_all() { ysr@777: PosParPRT* hd = _par_expanded_list; ysr@777: while (hd != NULL) { ysr@777: PosParPRT* nxt = hd->next_par_expanded(); ysr@777: PosParPRT* res = ysr@777: (PosParPRT*) ysr@777: Atomic::cmpxchg_ptr(nxt, &_par_expanded_list, hd); ysr@777: if (res == hd) { ysr@777: // We claimed the right to contract this table. ysr@777: hd->set_next_par_expanded(NULL); ysr@777: hd->par_contract(); ysr@777: hd = _par_expanded_list; ysr@777: } else { ysr@777: hd = res; ysr@777: } ysr@777: } ysr@777: } ysr@777: ysr@777: PosParPRT* PosParPRT::_free_list = NULL; ysr@777: PosParPRT* PosParPRT::_par_expanded_list = NULL; ysr@777: ysr@777: jint OtherRegionsTable::_cache_probes = 0; ysr@777: jint OtherRegionsTable::_cache_hits = 0; ysr@777: ysr@777: size_t OtherRegionsTable::_max_fine_entries = 0; ysr@777: size_t OtherRegionsTable::_mod_max_fine_entries_mask = 0; ysr@777: #if SAMPLE_FOR_EVICTION ysr@777: size_t OtherRegionsTable::_fine_eviction_stride = 0; ysr@777: size_t OtherRegionsTable::_fine_eviction_sample_size = 0; ysr@777: #endif ysr@777: ysr@777: OtherRegionsTable::OtherRegionsTable(HeapRegion* hr) : ysr@777: _g1h(G1CollectedHeap::heap()), ysr@777: _m(Mutex::leaf, "An OtherRegionsTable lock", true), ysr@777: _hr(hr), ysr@777: _coarse_map(G1CollectedHeap::heap()->max_regions(), ysr@777: false /* in-resource-area */), ysr@777: _fine_grain_regions(NULL), ysr@777: _n_fine_entries(0), _n_coarse_entries(0), ysr@777: #if SAMPLE_FOR_EVICTION ysr@777: _fine_eviction_start(0), ysr@777: #endif ysr@777: _sparse_table(hr) ysr@777: { ysr@777: typedef PosParPRT* PosParPRTPtr; ysr@777: if (_max_fine_entries == 0) { ysr@777: assert(_mod_max_fine_entries_mask == 0, "Both or none."); kvn@1080: _max_fine_entries = (size_t)(1 << G1LogRSRegionEntries); ysr@777: _mod_max_fine_entries_mask = _max_fine_entries - 1; ysr@777: #if SAMPLE_FOR_EVICTION ysr@777: assert(_fine_eviction_sample_size == 0 ysr@777: && _fine_eviction_stride == 0, "All init at same time."); ysr@777: _fine_eviction_sample_size = MAX2((size_t)4, (size_t)G1LogRSRegionEntries); ysr@777: _fine_eviction_stride = _max_fine_entries / _fine_eviction_sample_size; ysr@777: #endif ysr@777: } ysr@777: _fine_grain_regions = new PosParPRTPtr[_max_fine_entries]; ysr@777: if (_fine_grain_regions == NULL) ysr@777: vm_exit_out_of_memory(sizeof(void*)*_max_fine_entries, ysr@777: "Failed to allocate _fine_grain_entries."); ysr@777: for (size_t i = 0; i < _max_fine_entries; i++) { ysr@777: _fine_grain_regions[i] = NULL; ysr@777: } ysr@777: } ysr@777: ysr@777: int** OtherRegionsTable::_from_card_cache = NULL; ysr@777: size_t OtherRegionsTable::_from_card_cache_max_regions = 0; ysr@777: size_t OtherRegionsTable::_from_card_cache_mem_size = 0; ysr@777: ysr@777: void OtherRegionsTable::init_from_card_cache(size_t max_regions) { ysr@777: _from_card_cache_max_regions = max_regions; ysr@777: ysr@777: int n_par_rs = HeapRegionRemSet::num_par_rem_sets(); ysr@777: _from_card_cache = NEW_C_HEAP_ARRAY(int*, n_par_rs); ysr@777: for (int i = 0; i < n_par_rs; i++) { ysr@777: _from_card_cache[i] = NEW_C_HEAP_ARRAY(int, max_regions); ysr@777: for (size_t j = 0; j < max_regions; j++) { ysr@777: _from_card_cache[i][j] = -1; // An invalid value. ysr@777: } ysr@777: } ysr@777: _from_card_cache_mem_size = n_par_rs * max_regions * sizeof(int); ysr@777: } ysr@777: ysr@777: void OtherRegionsTable::shrink_from_card_cache(size_t new_n_regs) { ysr@777: for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets(); i++) { ysr@777: assert(new_n_regs <= _from_card_cache_max_regions, "Must be within max."); ysr@777: for (size_t j = new_n_regs; j < _from_card_cache_max_regions; j++) { ysr@777: _from_card_cache[i][j] = -1; // An invalid value. ysr@777: } ysr@777: } ysr@777: } ysr@777: ysr@777: #ifndef PRODUCT ysr@777: void OtherRegionsTable::print_from_card_cache() { ysr@777: for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets(); i++) { ysr@777: for (size_t j = 0; j < _from_card_cache_max_regions; j++) { ysr@777: gclog_or_tty->print_cr("_from_card_cache[%d][%d] = %d.", ysr@777: i, j, _from_card_cache[i][j]); ysr@777: } ysr@777: } ysr@777: } ysr@777: #endif ysr@777: ysr@777: void OtherRegionsTable::add_reference(oop* from, int tid) { ysr@777: size_t cur_hrs_ind = hr()->hrs_index(); ysr@777: ysr@777: #if HRRS_VERBOSE ysr@777: gclog_or_tty->print_cr("ORT::add_reference_work(" PTR_FORMAT "->" PTR_FORMAT ").", ysr@777: from, *from); ysr@777: #endif ysr@777: ysr@777: int from_card = (int)(uintptr_t(from) >> CardTableModRefBS::card_shift); ysr@777: ysr@777: #if HRRS_VERBOSE ysr@777: gclog_or_tty->print_cr("Table for [" PTR_FORMAT "...): card %d (cache = %d)", ysr@777: hr()->bottom(), from_card, ysr@777: _from_card_cache[tid][cur_hrs_ind]); ysr@777: #endif ysr@777: ysr@777: #define COUNT_CACHE 0 ysr@777: #if COUNT_CACHE ysr@777: jint p = Atomic::add(1, &_cache_probes); ysr@777: if ((p % 10000) == 0) { ysr@777: jint hits = _cache_hits; ysr@777: gclog_or_tty->print_cr("%d/%d = %5.2f%% RS cache hits.", ysr@777: _cache_hits, p, 100.0* (float)hits/(float)p); ysr@777: } ysr@777: #endif ysr@777: if (from_card == _from_card_cache[tid][cur_hrs_ind]) { ysr@777: #if HRRS_VERBOSE ysr@777: gclog_or_tty->print_cr(" from-card cache hit."); ysr@777: #endif ysr@777: #if COUNT_CACHE ysr@777: Atomic::inc(&_cache_hits); ysr@777: #endif ysr@777: assert(contains_reference(from), "We just added it!"); ysr@777: return; ysr@777: } else { ysr@777: _from_card_cache[tid][cur_hrs_ind] = from_card; ysr@777: } ysr@777: ysr@777: // Note that this may be a continued H region. ysr@777: HeapRegion* from_hr = _g1h->heap_region_containing_raw(from); ysr@777: size_t from_hrs_ind = (size_t)from_hr->hrs_index(); ysr@777: ysr@777: // If the region is already coarsened, return. ysr@777: if (_coarse_map.at(from_hrs_ind)) { ysr@777: #if HRRS_VERBOSE ysr@777: gclog_or_tty->print_cr(" coarse map hit."); ysr@777: #endif ysr@777: assert(contains_reference(from), "We just added it!"); ysr@777: return; ysr@777: } ysr@777: ysr@777: // Otherwise find a per-region table to add it to. ysr@777: size_t ind = from_hrs_ind & _mod_max_fine_entries_mask; ysr@777: PosParPRT* prt = find_region_table(ind, from_hr); ysr@777: if (prt == NULL) { ysr@777: MutexLockerEx x(&_m, Mutex::_no_safepoint_check_flag); ysr@777: // Confirm that it's really not there... ysr@777: prt = find_region_table(ind, from_hr); ysr@777: if (prt == NULL) { ysr@777: ysr@777: uintptr_t from_hr_bot_card_index = ysr@777: uintptr_t(from_hr->bottom()) ysr@777: >> CardTableModRefBS::card_shift; ysr@777: int card_index = from_card - from_hr_bot_card_index; ysr@777: assert(0 <= card_index && card_index < PosParPRT::CardsPerRegion, ysr@777: "Must be in range."); ysr@777: if (G1HRRSUseSparseTable && ysr@777: _sparse_table.add_card((short) from_hrs_ind, card_index)) { ysr@777: if (G1RecordHRRSOops) { ysr@777: HeapRegionRemSet::record(hr(), from); ysr@777: #if HRRS_VERBOSE ysr@777: gclog_or_tty->print(" Added card " PTR_FORMAT " to region " ysr@777: "[" PTR_FORMAT "...) for ref " PTR_FORMAT ".\n", ysr@777: align_size_down(uintptr_t(from), ysr@777: CardTableModRefBS::card_size), ysr@777: hr()->bottom(), from); ysr@777: #endif ysr@777: } ysr@777: #if HRRS_VERBOSE ysr@777: gclog_or_tty->print_cr(" added card to sparse table."); ysr@777: #endif ysr@777: assert(contains_reference_locked(from), "We just added it!"); ysr@777: return; ysr@777: } else { ysr@777: #if HRRS_VERBOSE ysr@777: gclog_or_tty->print_cr(" [tid %d] sparse table entry " ysr@777: "overflow(f: %d, t: %d)", ysr@777: tid, from_hrs_ind, cur_hrs_ind); ysr@777: #endif ysr@777: } ysr@777: ysr@777: // Otherwise, transfer from sparse to fine-grain. ysr@777: short cards[SparsePRTEntry::CardsPerEntry]; ysr@777: if (G1HRRSUseSparseTable) { ysr@777: bool res = _sparse_table.get_cards((short) from_hrs_ind, &cards[0]); ysr@777: assert(res, "There should have been an entry"); ysr@777: } ysr@777: ysr@777: if (_n_fine_entries == _max_fine_entries) { ysr@777: prt = delete_region_table(); ysr@777: } else { ysr@777: prt = PosParPRT::alloc(from_hr); ysr@777: } ysr@777: prt->init(from_hr); ysr@777: // Record the outgoing pointer in the from_region's outgoing bitmap. ysr@777: from_hr->rem_set()->add_outgoing_reference(hr()); ysr@777: ysr@777: PosParPRT* first_prt = _fine_grain_regions[ind]; ysr@777: prt->set_next(first_prt); // XXX Maybe move to init? ysr@777: _fine_grain_regions[ind] = prt; ysr@777: _n_fine_entries++; ysr@777: ysr@777: // Add in the cards from the sparse table. ysr@777: if (G1HRRSUseSparseTable) { ysr@777: for (int i = 0; i < SparsePRTEntry::CardsPerEntry; i++) { ysr@777: short c = cards[i]; ysr@777: if (c != SparsePRTEntry::NullEntry) { ysr@777: prt->add_card(c); ysr@777: } ysr@777: } ysr@777: // Now we can delete the sparse entry. ysr@777: bool res = _sparse_table.delete_entry((short) from_hrs_ind); ysr@777: assert(res, "It should have been there."); ysr@777: } ysr@777: } ysr@777: assert(prt != NULL && prt->hr() == from_hr, "consequence"); ysr@777: } ysr@777: // Note that we can't assert "prt->hr() == from_hr", because of the ysr@777: // possibility of concurrent reuse. But see head comment of ysr@777: // OtherRegionsTable for why this is OK. ysr@777: assert(prt != NULL, "Inv"); ysr@777: ysr@777: prt->add_reference(from, tid); ysr@777: if (G1RecordHRRSOops) { ysr@777: HeapRegionRemSet::record(hr(), from); ysr@777: #if HRRS_VERBOSE ysr@777: gclog_or_tty->print("Added card " PTR_FORMAT " to region " ysr@777: "[" PTR_FORMAT "...) for ref " PTR_FORMAT ".\n", ysr@777: align_size_down(uintptr_t(from), ysr@777: CardTableModRefBS::card_size), ysr@777: hr()->bottom(), from); ysr@777: #endif ysr@777: } ysr@777: assert(contains_reference(from), "We just added it!"); ysr@777: } ysr@777: ysr@777: PosParPRT* ysr@777: OtherRegionsTable::find_region_table(size_t ind, HeapRegion* hr) const { ysr@777: assert(0 <= ind && ind < _max_fine_entries, "Preconditions."); ysr@777: PosParPRT* prt = _fine_grain_regions[ind]; ysr@777: while (prt != NULL && prt->hr() != hr) { ysr@777: prt = prt->next(); ysr@777: } ysr@777: // Loop postcondition is the method postcondition. ysr@777: return prt; ysr@777: } ysr@777: ysr@777: ysr@777: #define DRT_CENSUS 0 ysr@777: ysr@777: #if DRT_CENSUS ysr@777: static const int HistoSize = 6; ysr@777: static int global_histo[HistoSize] = { 0, 0, 0, 0, 0, 0 }; ysr@777: static int coarsenings = 0; ysr@777: static int occ_sum = 0; ysr@777: #endif ysr@777: ysr@777: jint OtherRegionsTable::_n_coarsenings = 0; ysr@777: ysr@777: PosParPRT* OtherRegionsTable::delete_region_table() { ysr@777: #if DRT_CENSUS ysr@777: int histo[HistoSize] = { 0, 0, 0, 0, 0, 0 }; ysr@777: const int histo_limits[] = { 1, 4, 16, 64, 256, 2048 }; ysr@777: #endif ysr@777: ysr@777: assert(_m.owned_by_self(), "Precondition"); ysr@777: assert(_n_fine_entries == _max_fine_entries, "Precondition"); ysr@777: PosParPRT* max = NULL; ysr@777: jint max_occ = 0; ysr@777: PosParPRT** max_prev; ysr@777: size_t max_ind; ysr@777: ysr@777: #if SAMPLE_FOR_EVICTION ysr@777: size_t i = _fine_eviction_start; ysr@777: for (size_t k = 0; k < _fine_eviction_sample_size; k++) { ysr@777: size_t ii = i; ysr@777: // Make sure we get a non-NULL sample. ysr@777: while (_fine_grain_regions[ii] == NULL) { ysr@777: ii++; ysr@777: if (ii == _max_fine_entries) ii = 0; ysr@777: guarantee(ii != i, "We must find one."); ysr@777: } ysr@777: PosParPRT** prev = &_fine_grain_regions[ii]; ysr@777: PosParPRT* cur = *prev; ysr@777: while (cur != NULL) { ysr@777: jint cur_occ = cur->occupied(); ysr@777: if (max == NULL || cur_occ > max_occ) { ysr@777: max = cur; ysr@777: max_prev = prev; ysr@777: max_ind = i; ysr@777: max_occ = cur_occ; ysr@777: } ysr@777: prev = cur->next_addr(); ysr@777: cur = cur->next(); ysr@777: } ysr@777: i = i + _fine_eviction_stride; ysr@777: if (i >= _n_fine_entries) i = i - _n_fine_entries; ysr@777: } ysr@777: _fine_eviction_start++; ysr@777: if (_fine_eviction_start >= _n_fine_entries) ysr@777: _fine_eviction_start -= _n_fine_entries; ysr@777: #else ysr@777: for (int i = 0; i < _max_fine_entries; i++) { ysr@777: PosParPRT** prev = &_fine_grain_regions[i]; ysr@777: PosParPRT* cur = *prev; ysr@777: while (cur != NULL) { ysr@777: jint cur_occ = cur->occupied(); ysr@777: #if DRT_CENSUS ysr@777: for (int k = 0; k < HistoSize; k++) { ysr@777: if (cur_occ <= histo_limits[k]) { ysr@777: histo[k]++; global_histo[k]++; break; ysr@777: } ysr@777: } ysr@777: #endif ysr@777: if (max == NULL || cur_occ > max_occ) { ysr@777: max = cur; ysr@777: max_prev = prev; ysr@777: max_ind = i; ysr@777: max_occ = cur_occ; ysr@777: } ysr@777: prev = cur->next_addr(); ysr@777: cur = cur->next(); ysr@777: } ysr@777: } ysr@777: #endif ysr@777: // XXX ysr@777: guarantee(max != NULL, "Since _n_fine_entries > 0"); ysr@777: #if DRT_CENSUS ysr@777: gclog_or_tty->print_cr("In a coarsening: histo of occs:"); ysr@777: for (int k = 0; k < HistoSize; k++) { ysr@777: gclog_or_tty->print_cr(" <= %4d: %5d.", histo_limits[k], histo[k]); ysr@777: } ysr@777: coarsenings++; ysr@777: occ_sum += max_occ; ysr@777: if ((coarsenings % 100) == 0) { ysr@777: gclog_or_tty->print_cr("\ncoarsenings = %d; global summary:", coarsenings); ysr@777: for (int k = 0; k < HistoSize; k++) { ysr@777: gclog_or_tty->print_cr(" <= %4d: %5d.", histo_limits[k], global_histo[k]); ysr@777: } ysr@777: gclog_or_tty->print_cr("Avg occ of deleted region = %6.2f.", ysr@777: (float)occ_sum/(float)coarsenings); ysr@777: } ysr@777: #endif ysr@777: ysr@777: // Set the corresponding coarse bit. ysr@777: int max_hrs_index = max->hr()->hrs_index(); ysr@777: if (!_coarse_map.at(max_hrs_index)) { ysr@777: _coarse_map.at_put(max_hrs_index, true); ysr@777: _n_coarse_entries++; ysr@777: #if 0 ysr@777: gclog_or_tty->print("Coarsened entry in region [" PTR_FORMAT "...] " ysr@777: "for region [" PTR_FORMAT "...] (%d coarse entries).\n", ysr@777: hr()->bottom(), ysr@777: max->hr()->bottom(), ysr@777: _n_coarse_entries); ysr@777: #endif ysr@777: } ysr@777: ysr@777: // Unsplice. ysr@777: *max_prev = max->next(); ysr@777: Atomic::inc(&_n_coarsenings); ysr@777: _n_fine_entries--; ysr@777: return max; ysr@777: } ysr@777: ysr@777: ysr@777: // At present, this must be called stop-world single-threaded. ysr@777: void OtherRegionsTable::scrub(CardTableModRefBS* ctbs, ysr@777: BitMap* region_bm, BitMap* card_bm) { ysr@777: // First eliminated garbage regions from the coarse map. ysr@777: if (G1RSScrubVerbose) ysr@777: gclog_or_tty->print_cr("Scrubbing region %d:", hr()->hrs_index()); ysr@777: ysr@777: assert(_coarse_map.size() == region_bm->size(), "Precondition"); ysr@777: if (G1RSScrubVerbose) ysr@777: gclog_or_tty->print(" Coarse map: before = %d...", _n_coarse_entries); ysr@777: _coarse_map.set_intersection(*region_bm); ysr@777: _n_coarse_entries = _coarse_map.count_one_bits(); ysr@777: if (G1RSScrubVerbose) ysr@777: gclog_or_tty->print_cr(" after = %d.", _n_coarse_entries); ysr@777: ysr@777: // Now do the fine-grained maps. ysr@777: for (size_t i = 0; i < _max_fine_entries; i++) { ysr@777: PosParPRT* cur = _fine_grain_regions[i]; ysr@777: PosParPRT** prev = &_fine_grain_regions[i]; ysr@777: while (cur != NULL) { ysr@777: PosParPRT* nxt = cur->next(); ysr@777: // If the entire region is dead, eliminate. ysr@777: if (G1RSScrubVerbose) ysr@777: gclog_or_tty->print_cr(" For other region %d:", cur->hr()->hrs_index()); ysr@777: if (!region_bm->at(cur->hr()->hrs_index())) { ysr@777: *prev = nxt; ysr@777: cur->set_next(NULL); ysr@777: _n_fine_entries--; ysr@777: if (G1RSScrubVerbose) ysr@777: gclog_or_tty->print_cr(" deleted via region map."); ysr@777: PosParPRT::free(cur); ysr@777: } else { ysr@777: // Do fine-grain elimination. ysr@777: if (G1RSScrubVerbose) ysr@777: gclog_or_tty->print(" occ: before = %4d.", cur->occupied()); ysr@777: cur->scrub(ctbs, card_bm); ysr@777: if (G1RSScrubVerbose) ysr@777: gclog_or_tty->print_cr(" after = %4d.", cur->occupied()); ysr@777: // Did that empty the table completely? ysr@777: if (cur->occupied() == 0) { ysr@777: *prev = nxt; ysr@777: cur->set_next(NULL); ysr@777: _n_fine_entries--; ysr@777: PosParPRT::free(cur); ysr@777: } else { ysr@777: prev = cur->next_addr(); ysr@777: } ysr@777: } ysr@777: cur = nxt; ysr@777: } ysr@777: } ysr@777: // Since we may have deleted a from_card_cache entry from the RS, clear ysr@777: // the FCC. ysr@777: clear_fcc(); ysr@777: } ysr@777: ysr@777: ysr@777: size_t OtherRegionsTable::occupied() const { ysr@777: // Cast away const in this case. ysr@777: MutexLockerEx x((Mutex*)&_m, Mutex::_no_safepoint_check_flag); ysr@777: size_t sum = occ_fine(); ysr@777: sum += occ_sparse(); ysr@777: sum += occ_coarse(); ysr@777: return sum; ysr@777: } ysr@777: ysr@777: size_t OtherRegionsTable::occ_fine() const { ysr@777: size_t sum = 0; ysr@777: for (size_t i = 0; i < _max_fine_entries; i++) { ysr@777: PosParPRT* cur = _fine_grain_regions[i]; ysr@777: while (cur != NULL) { ysr@777: sum += cur->occupied(); ysr@777: cur = cur->next(); ysr@777: } ysr@777: } ysr@777: return sum; ysr@777: } ysr@777: ysr@777: size_t OtherRegionsTable::occ_coarse() const { ysr@777: return (_n_coarse_entries * PosParPRT::CardsPerRegion); ysr@777: } ysr@777: ysr@777: size_t OtherRegionsTable::occ_sparse() const { ysr@777: return _sparse_table.occupied(); ysr@777: } ysr@777: ysr@777: size_t OtherRegionsTable::mem_size() const { ysr@777: // Cast away const in this case. ysr@777: MutexLockerEx x((Mutex*)&_m, Mutex::_no_safepoint_check_flag); ysr@777: size_t sum = 0; ysr@777: for (size_t i = 0; i < _max_fine_entries; i++) { ysr@777: PosParPRT* cur = _fine_grain_regions[i]; ysr@777: while (cur != NULL) { ysr@777: sum += cur->mem_size(); ysr@777: cur = cur->next(); ysr@777: } ysr@777: } ysr@777: sum += (sizeof(PosParPRT*) * _max_fine_entries); ysr@777: sum += (_coarse_map.size_in_words() * HeapWordSize); ysr@777: sum += (_sparse_table.mem_size()); ysr@777: sum += sizeof(*this) - sizeof(_sparse_table); // Avoid double counting above. ysr@777: return sum; ysr@777: } ysr@777: ysr@777: size_t OtherRegionsTable::static_mem_size() { ysr@777: return _from_card_cache_mem_size; ysr@777: } ysr@777: ysr@777: size_t OtherRegionsTable::fl_mem_size() { ysr@777: return PerRegionTable::fl_mem_size() + PosParPRT::fl_mem_size(); ysr@777: } ysr@777: ysr@777: void OtherRegionsTable::clear_fcc() { ysr@777: for (int i = 0; i < HeapRegionRemSet::num_par_rem_sets(); i++) { ysr@777: _from_card_cache[i][hr()->hrs_index()] = -1; ysr@777: } ysr@777: } ysr@777: ysr@777: void OtherRegionsTable::clear() { ysr@777: MutexLockerEx x(&_m, Mutex::_no_safepoint_check_flag); ysr@777: for (size_t i = 0; i < _max_fine_entries; i++) { ysr@777: PosParPRT* cur = _fine_grain_regions[i]; ysr@777: while (cur != NULL) { ysr@777: PosParPRT* nxt = cur->next(); ysr@777: PosParPRT::free(cur); ysr@777: cur = nxt; ysr@777: } ysr@777: _fine_grain_regions[i] = NULL; ysr@777: } ysr@777: _sparse_table.clear(); ysr@777: _coarse_map.clear(); ysr@777: _n_fine_entries = 0; ysr@777: _n_coarse_entries = 0; ysr@777: ysr@777: clear_fcc(); ysr@777: } ysr@777: ysr@777: void OtherRegionsTable::clear_incoming_entry(HeapRegion* from_hr) { ysr@777: MutexLockerEx x(&_m, Mutex::_no_safepoint_check_flag); ysr@777: size_t hrs_ind = (size_t)from_hr->hrs_index(); ysr@777: size_t ind = hrs_ind & _mod_max_fine_entries_mask; ysr@777: if (del_single_region_table(ind, from_hr)) { ysr@777: assert(!_coarse_map.at(hrs_ind), "Inv"); ysr@777: } else { ysr@777: _coarse_map.par_at_put(hrs_ind, 0); ysr@777: } ysr@777: // Check to see if any of the fcc entries come from here. ysr@777: int hr_ind = hr()->hrs_index(); ysr@777: for (int tid = 0; tid < HeapRegionRemSet::num_par_rem_sets(); tid++) { ysr@777: int fcc_ent = _from_card_cache[tid][hr_ind]; ysr@777: if (fcc_ent != -1) { ysr@777: HeapWord* card_addr = (HeapWord*) ysr@777: (uintptr_t(fcc_ent) << CardTableModRefBS::card_shift); ysr@777: if (hr()->is_in_reserved(card_addr)) { ysr@777: // Clear the from card cache. ysr@777: _from_card_cache[tid][hr_ind] = -1; ysr@777: } ysr@777: } ysr@777: } ysr@777: } ysr@777: ysr@777: bool OtherRegionsTable::del_single_region_table(size_t ind, ysr@777: HeapRegion* hr) { ysr@777: assert(0 <= ind && ind < _max_fine_entries, "Preconditions."); ysr@777: PosParPRT** prev_addr = &_fine_grain_regions[ind]; ysr@777: PosParPRT* prt = *prev_addr; ysr@777: while (prt != NULL && prt->hr() != hr) { ysr@777: prev_addr = prt->next_addr(); ysr@777: prt = prt->next(); ysr@777: } ysr@777: if (prt != NULL) { ysr@777: assert(prt->hr() == hr, "Loop postcondition."); ysr@777: *prev_addr = prt->next(); ysr@777: PosParPRT::free(prt); ysr@777: _n_fine_entries--; ysr@777: return true; ysr@777: } else { ysr@777: return false; ysr@777: } ysr@777: } ysr@777: ysr@777: bool OtherRegionsTable::contains_reference(oop* from) const { ysr@777: // Cast away const in this case. ysr@777: MutexLockerEx x((Mutex*)&_m, Mutex::_no_safepoint_check_flag); ysr@777: return contains_reference_locked(from); ysr@777: } ysr@777: ysr@777: bool OtherRegionsTable::contains_reference_locked(oop* from) const { ysr@777: HeapRegion* hr = _g1h->heap_region_containing_raw(from); ysr@777: if (hr == NULL) return false; ysr@777: size_t hr_ind = hr->hrs_index(); ysr@777: // Is this region in the coarse map? ysr@777: if (_coarse_map.at(hr_ind)) return true; ysr@777: ysr@777: PosParPRT* prt = find_region_table(hr_ind & _mod_max_fine_entries_mask, ysr@777: hr); ysr@777: if (prt != NULL) { ysr@777: return prt->contains_reference(from); ysr@777: ysr@777: } else { ysr@777: uintptr_t from_card = ysr@777: (uintptr_t(from) >> CardTableModRefBS::card_shift); ysr@777: uintptr_t hr_bot_card_index = ysr@777: uintptr_t(hr->bottom()) >> CardTableModRefBS::card_shift; ysr@777: assert(from_card >= hr_bot_card_index, "Inv"); ysr@777: int card_index = from_card - hr_bot_card_index; ysr@777: return _sparse_table.contains_card((short)hr_ind, card_index); ysr@777: } ysr@777: ysr@777: ysr@777: } ysr@777: iveresov@1230: // Determines how many threads can add records to an rset in parallel. iveresov@1230: // This can be done by either mutator threads together with the iveresov@1230: // concurrent refinement threads or GC threads. ysr@777: int HeapRegionRemSet::num_par_rem_sets() { iveresov@1230: return (int)MAX2(DirtyCardQueueSet::num_par_ids() + ConcurrentG1Refine::thread_num(), ParallelGCThreads); ysr@777: } ysr@777: ysr@777: HeapRegionRemSet::HeapRegionRemSet(G1BlockOffsetSharedArray* bosa, ysr@777: HeapRegion* hr) ysr@777: : _bosa(bosa), _other_regions(hr), ysr@777: _outgoing_region_map(G1CollectedHeap::heap()->max_regions(), ysr@777: false /* in-resource-area */), ysr@777: _iter_state(Unclaimed) ysr@777: {} ysr@777: ysr@777: ysr@777: void HeapRegionRemSet::init_for_par_iteration() { ysr@777: _iter_state = Unclaimed; ysr@777: } ysr@777: ysr@777: bool HeapRegionRemSet::claim_iter() { ysr@777: if (_iter_state != Unclaimed) return false; ysr@777: jint res = Atomic::cmpxchg(Claimed, (jint*)(&_iter_state), Unclaimed); ysr@777: return (res == Unclaimed); ysr@777: } ysr@777: ysr@777: void HeapRegionRemSet::set_iter_complete() { ysr@777: _iter_state = Complete; ysr@777: } ysr@777: ysr@777: bool HeapRegionRemSet::iter_is_complete() { ysr@777: return _iter_state == Complete; ysr@777: } ysr@777: ysr@777: ysr@777: void HeapRegionRemSet::init_iterator(HeapRegionRemSetIterator* iter) const { ysr@777: iter->initialize(this); ysr@777: } ysr@777: ysr@777: #ifndef PRODUCT ysr@777: void HeapRegionRemSet::print() const { ysr@777: HeapRegionRemSetIterator iter; ysr@777: init_iterator(&iter); ysr@777: size_t card_index; ysr@777: while (iter.has_next(card_index)) { ysr@777: HeapWord* card_start = ysr@777: G1CollectedHeap::heap()->bot_shared()->address_for_index(card_index); ysr@777: gclog_or_tty->print_cr(" Card " PTR_FORMAT ".", card_start); ysr@777: } ysr@777: // XXX ysr@777: if (iter.n_yielded() != occupied()) { ysr@777: gclog_or_tty->print_cr("Yielded disagrees with occupied:"); ysr@777: gclog_or_tty->print_cr(" %6d yielded (%6d coarse, %6d fine).", ysr@777: iter.n_yielded(), ysr@777: iter.n_yielded_coarse(), iter.n_yielded_fine()); ysr@777: gclog_or_tty->print_cr(" %6d occ (%6d coarse, %6d fine).", ysr@777: occupied(), occ_coarse(), occ_fine()); ysr@777: } ysr@777: guarantee(iter.n_yielded() == occupied(), ysr@777: "We should have yielded all the represented cards."); ysr@777: } ysr@777: #endif ysr@777: ysr@777: void HeapRegionRemSet::cleanup() { ysr@777: SparsePRT::cleanup_all(); ysr@777: } ysr@777: ysr@777: void HeapRegionRemSet::par_cleanup() { ysr@777: PosParPRT::par_contract_all(); ysr@777: } ysr@777: ysr@777: void HeapRegionRemSet::add_outgoing_reference(HeapRegion* to_hr) { ysr@777: _outgoing_region_map.par_at_put(to_hr->hrs_index(), 1); ysr@777: } ysr@777: ysr@777: void HeapRegionRemSet::clear() { ysr@777: clear_outgoing_entries(); ysr@777: _outgoing_region_map.clear(); ysr@777: _other_regions.clear(); ysr@777: assert(occupied() == 0, "Should be clear."); ysr@777: } ysr@777: ysr@777: void HeapRegionRemSet::clear_outgoing_entries() { ysr@777: G1CollectedHeap* g1h = G1CollectedHeap::heap(); ysr@777: size_t i = _outgoing_region_map.get_next_one_offset(0); ysr@777: while (i < _outgoing_region_map.size()) { ysr@777: HeapRegion* to_region = g1h->region_at(i); apetrusenko@980: if (!to_region->in_collection_set()) { apetrusenko@980: to_region->rem_set()->clear_incoming_entry(hr()); apetrusenko@980: } ysr@777: i = _outgoing_region_map.get_next_one_offset(i+1); ysr@777: } ysr@777: } ysr@777: ysr@777: ysr@777: void HeapRegionRemSet::scrub(CardTableModRefBS* ctbs, ysr@777: BitMap* region_bm, BitMap* card_bm) { ysr@777: _other_regions.scrub(ctbs, region_bm, card_bm); ysr@777: } ysr@777: ysr@777: //-------------------- Iteration -------------------- ysr@777: ysr@777: HeapRegionRemSetIterator:: ysr@777: HeapRegionRemSetIterator() : ysr@777: _hrrs(NULL), ysr@777: _g1h(G1CollectedHeap::heap()), ysr@777: _bosa(NULL), ysr@777: _sparse_iter(size_t(G1CollectedHeap::heap()->reserved_region().start()) ysr@777: >> CardTableModRefBS::card_shift) ysr@777: {} ysr@777: ysr@777: void HeapRegionRemSetIterator::initialize(const HeapRegionRemSet* hrrs) { ysr@777: _hrrs = hrrs; ysr@777: _coarse_map = &_hrrs->_other_regions._coarse_map; ysr@777: _fine_grain_regions = _hrrs->_other_regions._fine_grain_regions; ysr@777: _bosa = _hrrs->bosa(); ysr@777: ysr@777: _is = Sparse; ysr@777: // Set these values so that we increment to the first region. ysr@777: _coarse_cur_region_index = -1; ysr@777: _coarse_cur_region_cur_card = (PosParPRT::CardsPerRegion-1);; ysr@777: ysr@777: _cur_region_cur_card = 0; ysr@777: ysr@777: _fine_array_index = -1; ysr@777: _fine_cur_prt = NULL; ysr@777: ysr@777: _n_yielded_coarse = 0; ysr@777: _n_yielded_fine = 0; ysr@777: _n_yielded_sparse = 0; ysr@777: ysr@777: _sparse_iter.init(&hrrs->_other_regions._sparse_table); ysr@777: } ysr@777: ysr@777: bool HeapRegionRemSetIterator::coarse_has_next(size_t& card_index) { ysr@777: if (_hrrs->_other_regions._n_coarse_entries == 0) return false; ysr@777: // Go to the next card. ysr@777: _coarse_cur_region_cur_card++; ysr@777: // Was the last the last card in the current region? ysr@777: if (_coarse_cur_region_cur_card == PosParPRT::CardsPerRegion) { ysr@777: // Yes: find the next region. This may leave _coarse_cur_region_index ysr@777: // Set to the last index, in which case there are no more coarse ysr@777: // regions. ysr@777: _coarse_cur_region_index = ysr@777: (int) _coarse_map->get_next_one_offset(_coarse_cur_region_index + 1); ysr@777: if ((size_t)_coarse_cur_region_index < _coarse_map->size()) { ysr@777: _coarse_cur_region_cur_card = 0; ysr@777: HeapWord* r_bot = ysr@777: _g1h->region_at(_coarse_cur_region_index)->bottom(); ysr@777: _cur_region_card_offset = _bosa->index_for(r_bot); ysr@777: } else { ysr@777: return false; ysr@777: } ysr@777: } ysr@777: // If we didn't return false above, then we can yield a card. ysr@777: card_index = _cur_region_card_offset + _coarse_cur_region_cur_card; ysr@777: return true; ysr@777: } ysr@777: ysr@777: void HeapRegionRemSetIterator::fine_find_next_non_null_prt() { ysr@777: // Otherwise, find the next bucket list in the array. ysr@777: _fine_array_index++; ysr@777: while (_fine_array_index < (int) OtherRegionsTable::_max_fine_entries) { ysr@777: _fine_cur_prt = _fine_grain_regions[_fine_array_index]; ysr@777: if (_fine_cur_prt != NULL) return; ysr@777: else _fine_array_index++; ysr@777: } ysr@777: assert(_fine_cur_prt == NULL, "Loop post"); ysr@777: } ysr@777: ysr@777: bool HeapRegionRemSetIterator::fine_has_next(size_t& card_index) { ysr@777: if (fine_has_next()) { ysr@777: _cur_region_cur_card = ysr@777: _fine_cur_prt->_bm.get_next_one_offset(_cur_region_cur_card + 1); ysr@777: } ysr@777: while (!fine_has_next()) { ysr@777: if (_cur_region_cur_card == PosParPRT::CardsPerRegion) { ysr@777: _cur_region_cur_card = 0; ysr@777: _fine_cur_prt = _fine_cur_prt->next(); ysr@777: } ysr@777: if (_fine_cur_prt == NULL) { ysr@777: fine_find_next_non_null_prt(); ysr@777: if (_fine_cur_prt == NULL) return false; ysr@777: } ysr@777: assert(_fine_cur_prt != NULL && _cur_region_cur_card == 0, ysr@777: "inv."); ysr@777: HeapWord* r_bot = ysr@777: _fine_cur_prt->hr()->bottom(); ysr@777: _cur_region_card_offset = _bosa->index_for(r_bot); ysr@777: _cur_region_cur_card = _fine_cur_prt->_bm.get_next_one_offset(0); ysr@777: } ysr@777: assert(fine_has_next(), "Or else we exited the loop via the return."); ysr@777: card_index = _cur_region_card_offset + _cur_region_cur_card; ysr@777: return true; ysr@777: } ysr@777: ysr@777: bool HeapRegionRemSetIterator::fine_has_next() { ysr@777: return ysr@777: _fine_cur_prt != NULL && ysr@777: _cur_region_cur_card < PosParPRT::CardsPerRegion; ysr@777: } ysr@777: ysr@777: bool HeapRegionRemSetIterator::has_next(size_t& card_index) { ysr@777: switch (_is) { ysr@777: case Sparse: ysr@777: if (_sparse_iter.has_next(card_index)) { ysr@777: _n_yielded_sparse++; ysr@777: return true; ysr@777: } ysr@777: // Otherwise, deliberate fall-through ysr@777: _is = Fine; ysr@777: case Fine: ysr@777: if (fine_has_next(card_index)) { ysr@777: _n_yielded_fine++; ysr@777: return true; ysr@777: } ysr@777: // Otherwise, deliberate fall-through ysr@777: _is = Coarse; ysr@777: case Coarse: ysr@777: if (coarse_has_next(card_index)) { ysr@777: _n_yielded_coarse++; ysr@777: return true; ysr@777: } ysr@777: // Otherwise... ysr@777: break; ysr@777: } ysr@777: assert(ParallelGCThreads > 1 || ysr@777: n_yielded() == _hrrs->occupied(), ysr@777: "Should have yielded all the cards in the rem set " ysr@777: "(in the non-par case)."); ysr@777: return false; ysr@777: } ysr@777: ysr@777: ysr@777: ysr@777: oop** HeapRegionRemSet::_recorded_oops = NULL; ysr@777: HeapWord** HeapRegionRemSet::_recorded_cards = NULL; ysr@777: HeapRegion** HeapRegionRemSet::_recorded_regions = NULL; ysr@777: int HeapRegionRemSet::_n_recorded = 0; ysr@777: ysr@777: HeapRegionRemSet::Event* HeapRegionRemSet::_recorded_events = NULL; ysr@777: int* HeapRegionRemSet::_recorded_event_index = NULL; ysr@777: int HeapRegionRemSet::_n_recorded_events = 0; ysr@777: ysr@777: void HeapRegionRemSet::record(HeapRegion* hr, oop* f) { ysr@777: if (_recorded_oops == NULL) { ysr@777: assert(_n_recorded == 0 ysr@777: && _recorded_cards == NULL ysr@777: && _recorded_regions == NULL, ysr@777: "Inv"); ysr@777: _recorded_oops = NEW_C_HEAP_ARRAY(oop*, MaxRecorded); ysr@777: _recorded_cards = NEW_C_HEAP_ARRAY(HeapWord*, MaxRecorded); ysr@777: _recorded_regions = NEW_C_HEAP_ARRAY(HeapRegion*, MaxRecorded); ysr@777: } ysr@777: if (_n_recorded == MaxRecorded) { ysr@777: gclog_or_tty->print_cr("Filled up 'recorded' (%d).", MaxRecorded); ysr@777: } else { ysr@777: _recorded_cards[_n_recorded] = ysr@777: (HeapWord*)align_size_down(uintptr_t(f), ysr@777: CardTableModRefBS::card_size); ysr@777: _recorded_oops[_n_recorded] = f; ysr@777: _recorded_regions[_n_recorded] = hr; ysr@777: _n_recorded++; ysr@777: } ysr@777: } ysr@777: ysr@777: void HeapRegionRemSet::record_event(Event evnt) { ysr@777: if (!G1RecordHRRSEvents) return; ysr@777: ysr@777: if (_recorded_events == NULL) { ysr@777: assert(_n_recorded_events == 0 ysr@777: && _recorded_event_index == NULL, ysr@777: "Inv"); ysr@777: _recorded_events = NEW_C_HEAP_ARRAY(Event, MaxRecordedEvents); ysr@777: _recorded_event_index = NEW_C_HEAP_ARRAY(int, MaxRecordedEvents); ysr@777: } ysr@777: if (_n_recorded_events == MaxRecordedEvents) { ysr@777: gclog_or_tty->print_cr("Filled up 'recorded_events' (%d).", MaxRecordedEvents); ysr@777: } else { ysr@777: _recorded_events[_n_recorded_events] = evnt; ysr@777: _recorded_event_index[_n_recorded_events] = _n_recorded; ysr@777: _n_recorded_events++; ysr@777: } ysr@777: } ysr@777: ysr@777: void HeapRegionRemSet::print_event(outputStream* str, Event evnt) { ysr@777: switch (evnt) { ysr@777: case Event_EvacStart: ysr@777: str->print("Evac Start"); ysr@777: break; ysr@777: case Event_EvacEnd: ysr@777: str->print("Evac End"); ysr@777: break; ysr@777: case Event_RSUpdateEnd: ysr@777: str->print("RS Update End"); ysr@777: break; ysr@777: } ysr@777: } ysr@777: ysr@777: void HeapRegionRemSet::print_recorded() { ysr@777: int cur_evnt = 0; ysr@777: Event cur_evnt_kind; ysr@777: int cur_evnt_ind = 0; ysr@777: if (_n_recorded_events > 0) { ysr@777: cur_evnt_kind = _recorded_events[cur_evnt]; ysr@777: cur_evnt_ind = _recorded_event_index[cur_evnt]; ysr@777: } ysr@777: ysr@777: for (int i = 0; i < _n_recorded; i++) { ysr@777: while (cur_evnt < _n_recorded_events && i == cur_evnt_ind) { ysr@777: gclog_or_tty->print("Event: "); ysr@777: print_event(gclog_or_tty, cur_evnt_kind); ysr@777: gclog_or_tty->print_cr(""); ysr@777: cur_evnt++; ysr@777: if (cur_evnt < MaxRecordedEvents) { ysr@777: cur_evnt_kind = _recorded_events[cur_evnt]; ysr@777: cur_evnt_ind = _recorded_event_index[cur_evnt]; ysr@777: } ysr@777: } ysr@777: gclog_or_tty->print("Added card " PTR_FORMAT " to region [" PTR_FORMAT "...]" ysr@777: " for ref " PTR_FORMAT ".\n", ysr@777: _recorded_cards[i], _recorded_regions[i]->bottom(), ysr@777: _recorded_oops[i]); ysr@777: } ysr@777: } ysr@777: ysr@777: #ifndef PRODUCT ysr@777: void HeapRegionRemSet::test() { ysr@777: os::sleep(Thread::current(), (jlong)5000, false); ysr@777: G1CollectedHeap* g1h = G1CollectedHeap::heap(); ysr@777: ysr@777: // Run with "-XX:G1LogRSRegionEntries=2", so that 1 and 5 end up in same ysr@777: // hash bucket. ysr@777: HeapRegion* hr0 = g1h->region_at(0); ysr@777: HeapRegion* hr1 = g1h->region_at(1); ysr@777: HeapRegion* hr2 = g1h->region_at(5); ysr@777: HeapRegion* hr3 = g1h->region_at(6); ysr@777: HeapRegion* hr4 = g1h->region_at(7); ysr@777: HeapRegion* hr5 = g1h->region_at(8); ysr@777: ysr@777: HeapWord* hr1_start = hr1->bottom(); ysr@777: HeapWord* hr1_mid = hr1_start + HeapRegion::GrainWords/2; ysr@777: HeapWord* hr1_last = hr1->end() - 1; ysr@777: ysr@777: HeapWord* hr2_start = hr2->bottom(); ysr@777: HeapWord* hr2_mid = hr2_start + HeapRegion::GrainWords/2; ysr@777: HeapWord* hr2_last = hr2->end() - 1; ysr@777: ysr@777: HeapWord* hr3_start = hr3->bottom(); ysr@777: HeapWord* hr3_mid = hr3_start + HeapRegion::GrainWords/2; ysr@777: HeapWord* hr3_last = hr3->end() - 1; ysr@777: ysr@777: HeapRegionRemSet* hrrs = hr0->rem_set(); ysr@777: ysr@777: // Make three references from region 0x101... ysr@777: hrrs->add_reference((oop*)hr1_start); ysr@777: hrrs->add_reference((oop*)hr1_mid); ysr@777: hrrs->add_reference((oop*)hr1_last); ysr@777: ysr@777: hrrs->add_reference((oop*)hr2_start); ysr@777: hrrs->add_reference((oop*)hr2_mid); ysr@777: hrrs->add_reference((oop*)hr2_last); ysr@777: ysr@777: hrrs->add_reference((oop*)hr3_start); ysr@777: hrrs->add_reference((oop*)hr3_mid); ysr@777: hrrs->add_reference((oop*)hr3_last); ysr@777: ysr@777: // Now cause a coarsening. ysr@777: hrrs->add_reference((oop*)hr4->bottom()); ysr@777: hrrs->add_reference((oop*)hr5->bottom()); ysr@777: ysr@777: // Now, does iteration yield these three? ysr@777: HeapRegionRemSetIterator iter; ysr@777: hrrs->init_iterator(&iter); ysr@777: size_t sum = 0; ysr@777: size_t card_index; ysr@777: while (iter.has_next(card_index)) { ysr@777: HeapWord* card_start = ysr@777: G1CollectedHeap::heap()->bot_shared()->address_for_index(card_index); ysr@777: gclog_or_tty->print_cr(" Card " PTR_FORMAT ".", card_start); ysr@777: sum++; ysr@777: } ysr@777: guarantee(sum == 11 - 3 + 2048, "Failure"); ysr@777: guarantee(sum == hrrs->occupied(), "Failure"); ysr@777: } ysr@777: #endif