g1RemSet.cpp revision 545
342N/A/*
342N/A * Copyright 2001-2007 Sun Microsystems, Inc. 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 *
342N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
342N/A * CA 95054 USA or visit www.sun.com if you need additional information or
342N/A * have any questions.
342N/A *
342N/A */
342N/A
342N/A#include "incls/_precompiled.incl"
342N/A#include "incls/_g1RemSet.cpp.incl"
342N/A
342N/A#define CARD_REPEAT_HISTO 0
342N/A
342N/A#if CARD_REPEAT_HISTO
342N/Astatic size_t ct_freq_sz;
342N/Astatic jbyte* ct_freq = NULL;
342N/A
342N/Avoid init_ct_freq_table(size_t heap_sz_bytes) {
342N/A if (ct_freq == NULL) {
342N/A ct_freq_sz = heap_sz_bytes/CardTableModRefBS::card_size;
342N/A ct_freq = new jbyte[ct_freq_sz];
342N/A for (size_t j = 0; j < ct_freq_sz; j++) ct_freq[j] = 0;
342N/A }
342N/A}
342N/A
342N/Avoid ct_freq_note_card(size_t index) {
342N/A assert(0 <= index && index < ct_freq_sz, "Bounds error.");
342N/A if (ct_freq[index] < 100) { ct_freq[index]++; }
342N/A}
342N/A
342N/Astatic IntHistogram card_repeat_count(10, 10);
342N/A
342N/Avoid ct_freq_update_histo_and_reset() {
342N/A for (size_t j = 0; j < ct_freq_sz; j++) {
342N/A card_repeat_count.add_entry(ct_freq[j]);
342N/A ct_freq[j] = 0;
342N/A }
342N/A
342N/A}
342N/A#endif
342N/A
342N/A
342N/Aclass IntoCSOopClosure: public OopsInHeapRegionClosure {
342N/A OopsInHeapRegionClosure* _blk;
342N/A G1CollectedHeap* _g1;
342N/Apublic:
342N/A IntoCSOopClosure(G1CollectedHeap* g1, OopsInHeapRegionClosure* blk) :
342N/A _g1(g1), _blk(blk) {}
342N/A void set_region(HeapRegion* from) {
342N/A _blk->set_region(from);
342N/A }
342N/A virtual void do_oop(narrowOop* p) {
342N/A guarantee(false, "NYI");
342N/A }
342N/A virtual void do_oop(oop* p) {
342N/A oop obj = *p;
342N/A if (_g1->obj_in_cs(obj)) _blk->do_oop(p);
342N/A }
342N/A bool apply_to_weak_ref_discovered_field() { return true; }
342N/A bool idempotent() { return true; }
342N/A};
342N/A
342N/Aclass IntoCSRegionClosure: public HeapRegionClosure {
342N/A IntoCSOopClosure _blk;
342N/A G1CollectedHeap* _g1;
342N/Apublic:
342N/A IntoCSRegionClosure(G1CollectedHeap* g1, OopsInHeapRegionClosure* blk) :
342N/A _g1(g1), _blk(g1, blk) {}
342N/A bool doHeapRegion(HeapRegion* r) {
342N/A if (!r->in_collection_set()) {
342N/A _blk.set_region(r);
342N/A if (r->isHumongous()) {
342N/A if (r->startsHumongous()) {
342N/A oop obj = oop(r->bottom());
342N/A obj->oop_iterate(&_blk);
342N/A }
342N/A } else {
342N/A r->oop_before_save_marks_iterate(&_blk);
342N/A }
342N/A }
342N/A return false;
342N/A }
342N/A};
342N/A
342N/Avoid
342N/AStupidG1RemSet::oops_into_collection_set_do(OopsInHeapRegionClosure* oc,
342N/A int worker_i) {
342N/A IntoCSRegionClosure rc(_g1, oc);
342N/A _g1->heap_region_iterate(&rc);
342N/A}
342N/A
342N/Aclass UpdateRSOopClosure: public OopClosure {
342N/A HeapRegion* _from;
342N/A HRInto_G1RemSet* _rs;
342N/A int _worker_i;
342N/Apublic:
342N/A UpdateRSOopClosure(HRInto_G1RemSet* rs, int worker_i = 0) :
342N/A _from(NULL), _rs(rs), _worker_i(worker_i) {
342N/A guarantee(_rs != NULL, "Requires an HRIntoG1RemSet");
342N/A }
342N/A
342N/A void set_from(HeapRegion* from) {
342N/A assert(from != NULL, "from region must be non-NULL");
342N/A _from = from;
342N/A }
342N/A
342N/A virtual void do_oop(narrowOop* p) {
342N/A guarantee(false, "NYI");
342N/A }
342N/A virtual void do_oop(oop* p) {
342N/A assert(_from != NULL, "from region must be non-NULL");
342N/A _rs->par_write_ref(_from, p, _worker_i);
342N/A }
342N/A // Override: this closure is idempotent.
342N/A // bool idempotent() { return true; }
342N/A bool apply_to_weak_ref_discovered_field() { return true; }
342N/A};
342N/A
342N/Aclass UpdateRSOutOfRegionClosure: public HeapRegionClosure {
342N/A G1CollectedHeap* _g1h;
342N/A ModRefBarrierSet* _mr_bs;
342N/A UpdateRSOopClosure _cl;
342N/A int _worker_i;
342N/Apublic:
342N/A UpdateRSOutOfRegionClosure(G1CollectedHeap* g1, int worker_i = 0) :
342N/A _cl(g1->g1_rem_set()->as_HRInto_G1RemSet(), worker_i),
342N/A _mr_bs(g1->mr_bs()),
342N/A _worker_i(worker_i),
342N/A _g1h(g1)
342N/A {}
342N/A bool doHeapRegion(HeapRegion* r) {
342N/A if (!r->in_collection_set() && !r->continuesHumongous()) {
342N/A _cl.set_from(r);
342N/A r->set_next_filter_kind(HeapRegionDCTOC::OutOfRegionFilterKind);
342N/A _mr_bs->mod_oop_in_space_iterate(r, &_cl, true, true);
342N/A }
342N/A return false;
342N/A }
342N/A};
342N/A
342N/Aclass VerifyRSCleanCardOopClosure: public OopClosure {
342N/A G1CollectedHeap* _g1;
342N/Apublic:
342N/A VerifyRSCleanCardOopClosure(G1CollectedHeap* g1) : _g1(g1) {}
342N/A
342N/A virtual void do_oop(narrowOop* p) {
342N/A guarantee(false, "NYI");
342N/A }
342N/A virtual void do_oop(oop* p) {
342N/A oop obj = *p;
342N/A HeapRegion* to = _g1->heap_region_containing(obj);
342N/A guarantee(to == NULL || !to->in_collection_set(),
342N/A "Missed a rem set member.");
342N/A }
342N/A};
342N/A
342N/AHRInto_G1RemSet::HRInto_G1RemSet(G1CollectedHeap* g1, CardTableModRefBS* ct_bs)
342N/A : G1RemSet(g1), _ct_bs(ct_bs), _g1p(_g1->g1_policy()),
342N/A _cg1r(g1->concurrent_g1_refine()),
342N/A _par_traversal_in_progress(false), _new_refs(NULL),
342N/A _cards_scanned(NULL), _total_cards_scanned(0)
342N/A{
342N/A _seq_task = new SubTasksDone(NumSeqTasks);
342N/A _new_refs = NEW_C_HEAP_ARRAY(GrowableArray<oop*>*, ParallelGCThreads);
342N/A}
342N/A
342N/AHRInto_G1RemSet::~HRInto_G1RemSet() {
342N/A delete _seq_task;
342N/A}
342N/A
342N/Avoid CountNonCleanMemRegionClosure::do_MemRegion(MemRegion mr) {
342N/A if (_g1->is_in_g1_reserved(mr.start())) {
342N/A _n += (int) ((mr.byte_size() / CardTableModRefBS::card_size));
342N/A if (_start_first == NULL) _start_first = mr.start();
342N/A }
342N/A}
342N/A
342N/Aclass ScanRSClosure : public HeapRegionClosure {
342N/A size_t _cards_done, _cards;
342N/A G1CollectedHeap* _g1h;
342N/A OopsInHeapRegionClosure* _oc;
342N/A G1BlockOffsetSharedArray* _bot_shared;
342N/A CardTableModRefBS *_ct_bs;
342N/A int _worker_i;
342N/A bool _try_claimed;
342N/Apublic:
342N/A ScanRSClosure(OopsInHeapRegionClosure* oc, int worker_i) :
342N/A _oc(oc),
342N/A _cards(0),
342N/A _cards_done(0),
342N/A _worker_i(worker_i),
342N/A _try_claimed(false)
342N/A {
342N/A _g1h = G1CollectedHeap::heap();
342N/A _bot_shared = _g1h->bot_shared();
342N/A _ct_bs = (CardTableModRefBS*) (_g1h->barrier_set());
342N/A }
342N/A
342N/A void set_try_claimed() { _try_claimed = true; }
342N/A
342N/A void scanCard(size_t index, HeapRegion *r) {
342N/A _cards_done++;
342N/A DirtyCardToOopClosure* cl =
342N/A r->new_dcto_closure(_oc,
342N/A CardTableModRefBS::Precise,
342N/A HeapRegionDCTOC::IntoCSFilterKind);
342N/A
342N/A // Set the "from" region in the closure.
342N/A _oc->set_region(r);
342N/A HeapWord* card_start = _bot_shared->address_for_index(index);
342N/A HeapWord* card_end = card_start + G1BlockOffsetSharedArray::N_words;
342N/A Space *sp = SharedHeap::heap()->space_containing(card_start);
342N/A MemRegion sm_region;
342N/A if (ParallelGCThreads > 0) {
342N/A // first find the used area
342N/A sm_region = sp->used_region_at_save_marks();
342N/A } else {
342N/A // The closure is not idempotent. We shouldn't look at objects
342N/A // allocated during the GC.
342N/A sm_region = sp->used_region_at_save_marks();
342N/A }
342N/A MemRegion mr = sm_region.intersection(MemRegion(card_start,card_end));
342N/A if (!mr.is_empty()) {
342N/A cl->do_MemRegion(mr);
342N/A }
342N/A }
342N/A
342N/A void printCard(HeapRegion* card_region, size_t card_index,
342N/A HeapWord* card_start) {
342N/A gclog_or_tty->print_cr("T %d Region [" PTR_FORMAT ", " PTR_FORMAT ") "
342N/A "RS names card %p: "
342N/A "[" PTR_FORMAT ", " PTR_FORMAT ")",
342N/A _worker_i,
342N/A card_region->bottom(), card_region->end(),
342N/A card_index,
342N/A card_start, card_start + G1BlockOffsetSharedArray::N_words);
342N/A }
342N/A
342N/A bool doHeapRegion(HeapRegion* r) {
342N/A assert(r->in_collection_set(), "should only be called on elements of CS.");
342N/A HeapRegionRemSet* hrrs = r->rem_set();
342N/A if (hrrs->iter_is_complete()) return false; // All done.
342N/A if (!_try_claimed && !hrrs->claim_iter()) return false;
342N/A // If we didn't return above, then
342N/A // _try_claimed || r->claim_iter()
342N/A // is true: either we're supposed to work on claimed-but-not-complete
342N/A // regions, or we successfully claimed the region.
342N/A HeapRegionRemSetIterator* iter = _g1h->rem_set_iterator(_worker_i);
342N/A hrrs->init_iterator(iter);
342N/A size_t card_index;
342N/A while (iter->has_next(card_index)) {
342N/A HeapWord* card_start = _g1h->bot_shared()->address_for_index(card_index);
342N/A
342N/A#if 0
342N/A gclog_or_tty->print("Rem set iteration yielded card [" PTR_FORMAT ", " PTR_FORMAT ").\n",
342N/A card_start, card_start + CardTableModRefBS::card_size_in_words);
342N/A#endif
342N/A
342N/A HeapRegion* card_region = _g1h->heap_region_containing(card_start);
342N/A assert(card_region != NULL, "Yielding cards not in the heap?");
342N/A _cards++;
342N/A
342N/A if (!card_region->in_collection_set()) {
342N/A // If the card is dirty, then we will scan it during updateRS.
342N/A if (!_ct_bs->is_card_claimed(card_index) &&
342N/A !_ct_bs->is_card_dirty(card_index)) {
342N/A assert(_ct_bs->is_card_clean(card_index) ||
342N/A _ct_bs->is_card_claimed(card_index),
342N/A "Card is either dirty, clean, or claimed");
342N/A if (_ct_bs->claim_card(card_index))
342N/A scanCard(card_index, card_region);
342N/A }
342N/A }
342N/A }
342N/A hrrs->set_iter_complete();
342N/A return false;
342N/A }
342N/A // Set all cards back to clean.
342N/A void cleanup() {_g1h->cleanUpCardTable();}
342N/A size_t cards_done() { return _cards_done;}
342N/A size_t cards_looked_up() { return _cards;}
342N/A};
342N/A
342N/A// We want the parallel threads to start their scanning at
342N/A// different collection set regions to avoid contention.
342N/A// If we have:
342N/A// n collection set regions
342N/A// p threads
342N/A// Then thread t will start at region t * floor (n/p)
342N/A
342N/AHeapRegion* HRInto_G1RemSet::calculateStartRegion(int worker_i) {
342N/A HeapRegion* result = _g1p->collection_set();
342N/A if (ParallelGCThreads > 0) {
342N/A size_t cs_size = _g1p->collection_set_size();
342N/A int n_workers = _g1->workers()->total_workers();
342N/A size_t cs_spans = cs_size / n_workers;
342N/A size_t ind = cs_spans * worker_i;
342N/A for (size_t i = 0; i < ind; i++)
342N/A result = result->next_in_collection_set();
342N/A }
342N/A return result;
342N/A}
342N/A
342N/Avoid HRInto_G1RemSet::scanRS(OopsInHeapRegionClosure* oc, int worker_i) {
342N/A double rs_time_start = os::elapsedTime();
342N/A HeapRegion *startRegion = calculateStartRegion(worker_i);
342N/A
342N/A BufferingOopsInHeapRegionClosure boc(oc);
342N/A ScanRSClosure scanRScl(&boc, worker_i);
342N/A _g1->collection_set_iterate_from(startRegion, &scanRScl);
342N/A scanRScl.set_try_claimed();
342N/A _g1->collection_set_iterate_from(startRegion, &scanRScl);
342N/A
342N/A boc.done();
342N/A double closure_app_time_sec = boc.closure_app_seconds();
342N/A double scan_rs_time_sec = (os::elapsedTime() - rs_time_start) -
342N/A closure_app_time_sec;
342N/A double closure_app_time_ms = closure_app_time_sec * 1000.0;
342N/A
342N/A assert( _cards_scanned != NULL, "invariant" );
342N/A _cards_scanned[worker_i] = scanRScl.cards_done();
342N/A
342N/A _g1p->record_scan_rs_start_time(worker_i, rs_time_start * 1000.0);
342N/A _g1p->record_scan_rs_time(worker_i, scan_rs_time_sec * 1000.0);
342N/A if (ParallelGCThreads > 0) {
342N/A // In this case, we called scanNewRefsRS and recorded the corresponding
342N/A // time.
342N/A double scan_new_refs_time_ms = _g1p->get_scan_new_refs_time(worker_i);
342N/A if (scan_new_refs_time_ms > 0.0) {
342N/A closure_app_time_ms += scan_new_refs_time_ms;
342N/A }
342N/A }
342N/A _g1p->record_obj_copy_time(worker_i, closure_app_time_ms);
342N/A}
342N/A
342N/Avoid HRInto_G1RemSet::updateRS(int worker_i) {
342N/A ConcurrentG1Refine* cg1r = _g1->concurrent_g1_refine();
342N/A
342N/A double start = os::elapsedTime();
342N/A _g1p->record_update_rs_start_time(worker_i, start * 1000.0);
342N/A
342N/A if (G1RSBarrierUseQueue && !cg1r->do_traversal()) {
342N/A // Apply the appropriate closure to all remaining log entries.
342N/A _g1->iterate_dirty_card_closure(false, worker_i);
342N/A // Now there should be no dirty cards.
342N/A if (G1RSLogCheckCardTable) {
342N/A CountNonCleanMemRegionClosure cl(_g1);
342N/A _ct_bs->mod_card_iterate(&cl);
342N/A // XXX This isn't true any more: keeping cards of young regions
342N/A // marked dirty broke it. Need some reasonable fix.
342N/A guarantee(cl.n() == 0, "Card table should be clean.");
342N/A }
342N/A } else {
342N/A UpdateRSOutOfRegionClosure update_rs(_g1, worker_i);
342N/A _g1->heap_region_iterate(&update_rs);
342N/A // We did a traversal; no further one is necessary.
342N/A if (G1RSBarrierUseQueue) {
342N/A assert(cg1r->do_traversal(), "Or we shouldn't have gotten here.");
342N/A cg1r->set_pya_cancel();
342N/A }
342N/A if (_cg1r->use_cache()) {
342N/A _cg1r->clear_and_record_card_counts();
342N/A _cg1r->clear_hot_cache();
342N/A }
342N/A }
342N/A _g1p->record_update_rs_time(worker_i, (os::elapsedTime() - start) * 1000.0);
342N/A}
342N/A
342N/A#ifndef PRODUCT
342N/Aclass PrintRSClosure : public HeapRegionClosure {
342N/A int _count;
342N/Apublic:
342N/A PrintRSClosure() : _count(0) {}
342N/A bool doHeapRegion(HeapRegion* r) {
342N/A HeapRegionRemSet* hrrs = r->rem_set();
342N/A _count += (int) hrrs->occupied();
342N/A if (hrrs->occupied() == 0) {
342N/A gclog_or_tty->print("Heap Region [" PTR_FORMAT ", " PTR_FORMAT ") "
342N/A "has no remset entries\n",
342N/A r->bottom(), r->end());
342N/A } else {
342N/A gclog_or_tty->print("Printing rem set for heap region [" PTR_FORMAT ", " PTR_FORMAT ")\n",
342N/A r->bottom(), r->end());
342N/A r->print();
342N/A hrrs->print();
342N/A gclog_or_tty->print("\nDone printing rem set\n");
342N/A }
342N/A return false;
342N/A }
342N/A int occupied() {return _count;}
342N/A};
342N/A#endif
342N/A
342N/Aclass CountRSSizeClosure: public HeapRegionClosure {
342N/A size_t _n;
342N/A size_t _tot;
342N/A size_t _max;
342N/A HeapRegion* _max_r;
342N/A enum {
342N/A N = 20,
342N/A MIN = 6
342N/A };
342N/A int _histo[N];
342N/Apublic:
342N/A CountRSSizeClosure() : _n(0), _tot(0), _max(0), _max_r(NULL) {
342N/A for (int i = 0; i < N; i++) _histo[i] = 0;
342N/A }
342N/A bool doHeapRegion(HeapRegion* r) {
342N/A if (!r->continuesHumongous()) {
342N/A size_t occ = r->rem_set()->occupied();
342N/A _n++;
342N/A _tot += occ;
342N/A if (occ > _max) {
342N/A _max = occ;
342N/A _max_r = r;
342N/A }
342N/A // Fit it into a histo bin.
342N/A int s = 1 << MIN;
342N/A int i = 0;
342N/A while (occ > (size_t) s && i < (N-1)) {
342N/A s = s << 1;
342N/A i++;
342N/A }
342N/A _histo[i]++;
342N/A }
342N/A return false;
342N/A }
342N/A size_t n() { return _n; }
342N/A size_t tot() { return _tot; }
342N/A size_t mx() { return _max; }
342N/A HeapRegion* mxr() { return _max_r; }
342N/A void print_histo() {
342N/A int mx = N;
342N/A while (mx >= 0) {
342N/A if (_histo[mx-1] > 0) break;
342N/A mx--;
342N/A }
342N/A gclog_or_tty->print_cr("Number of regions with given RS sizes:");
342N/A gclog_or_tty->print_cr(" <= %8d %8d", 1 << MIN, _histo[0]);
342N/A for (int i = 1; i < mx-1; i++) {
342N/A gclog_or_tty->print_cr(" %8d - %8d %8d",
342N/A (1 << (MIN + i - 1)) + 1,
342N/A 1 << (MIN + i),
342N/A _histo[i]);
342N/A }
342N/A gclog_or_tty->print_cr(" > %8d %8d", (1 << (MIN+mx-2))+1, _histo[mx-1]);
342N/A }
342N/A};
342N/A
342N/Avoid
342N/AHRInto_G1RemSet::scanNewRefsRS(OopsInHeapRegionClosure* oc,
342N/A int worker_i) {
342N/A double scan_new_refs_start_sec = os::elapsedTime();
342N/A G1CollectedHeap* g1h = G1CollectedHeap::heap();
342N/A CardTableModRefBS* ct_bs = (CardTableModRefBS*) (g1h->barrier_set());
342N/A while (_new_refs[worker_i]->is_nonempty()) {
342N/A oop* p = _new_refs[worker_i]->pop();
342N/A oop obj = *p;
342N/A // *p was in the collection set when p was pushed on "_new_refs", but
342N/A // another thread may have processed this location from an RS, so it
342N/A // might not point into the CS any longer. If so, it's obviously been
342N/A // processed, and we don't need to do anything further.
342N/A if (g1h->obj_in_cs(obj)) {
342N/A HeapRegion* r = g1h->heap_region_containing(p);
342N/A
342N/A DEBUG_ONLY(HeapRegion* to = g1h->heap_region_containing(obj));
342N/A assert(ParallelGCThreads > 1
342N/A || to->rem_set()->contains_reference(p),
342N/A "Invariant: pushed after being added."
342N/A "(Not reliable in parallel code.)");
342N/A oc->set_region(r);
342N/A // If "p" has already been processed concurrently, this is
342N/A // idempotent.
342N/A oc->do_oop(p);
342N/A }
342N/A }
342N/A _g1p->record_scan_new_refs_time(worker_i,
342N/A (os::elapsedTime() - scan_new_refs_start_sec)
342N/A * 1000.0);
342N/A}
342N/A
342N/Avoid HRInto_G1RemSet::set_par_traversal(bool b) {
342N/A _par_traversal_in_progress = b;
342N/A HeapRegionRemSet::set_par_traversal(b);
342N/A}
342N/A
342N/Avoid HRInto_G1RemSet::cleanupHRRS() {
342N/A HeapRegionRemSet::cleanup();
342N/A}
342N/A
342N/Avoid
342N/AHRInto_G1RemSet::oops_into_collection_set_do(OopsInHeapRegionClosure* oc,
342N/A int worker_i) {
342N/A#if CARD_REPEAT_HISTO
342N/A ct_freq_update_histo_and_reset();
342N/A#endif
342N/A if (worker_i == 0) {
342N/A _cg1r->clear_and_record_card_counts();
342N/A }
342N/A
342N/A // Make this into a command-line flag...
342N/A if (G1RSCountHisto && (ParallelGCThreads == 0 || worker_i == 0)) {
342N/A CountRSSizeClosure count_cl;
342N/A _g1->heap_region_iterate(&count_cl);
342N/A gclog_or_tty->print_cr("Avg of %d RS counts is %f, max is %d, "
342N/A "max region is " PTR_FORMAT,
342N/A count_cl.n(), (float)count_cl.tot()/(float)count_cl.n(),
342N/A count_cl.mx(), count_cl.mxr());
342N/A count_cl.print_histo();
342N/A }
342N/A
342N/A if (ParallelGCThreads > 0) {
342N/A // This is a temporary change to serialize the update and scanning
342N/A // of remembered sets. There are some race conditions when this is
342N/A // done in parallel and they are causing failures. When we resolve
342N/A // said race conditions, we'll revert back to parallel remembered
342N/A // set updating and scanning. See CRs 6677707 and 6677708.
342N/A if (worker_i == 0) {
342N/A updateRS(worker_i);
342N/A scanNewRefsRS(oc, worker_i);
342N/A scanRS(oc, worker_i);
342N/A }
342N/A } else {
342N/A assert(worker_i == 0, "invariant");
342N/A
342N/A updateRS(0);
342N/A scanRS(oc, 0);
342N/A }
342N/A}
342N/A
342N/Avoid HRInto_G1RemSet::
342N/Aprepare_for_oops_into_collection_set_do() {
342N/A#if G1_REM_SET_LOGGING
342N/A PrintRSClosure cl;
342N/A _g1->collection_set_iterate(&cl);
342N/A#endif
342N/A cleanupHRRS();
342N/A ConcurrentG1Refine* cg1r = _g1->concurrent_g1_refine();
342N/A _g1->set_refine_cte_cl_concurrency(false);
342N/A DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
342N/A dcqs.concatenate_logs();
342N/A
342N/A assert(!_par_traversal_in_progress, "Invariant between iterations.");
342N/A if (ParallelGCThreads > 0) {
342N/A set_par_traversal(true);
342N/A int n_workers = _g1->workers()->total_workers();
342N/A _seq_task->set_par_threads(n_workers);
342N/A for (uint i = 0; i < ParallelGCThreads; i++)
342N/A _new_refs[i] = new (ResourceObj::C_HEAP) GrowableArray<oop*>(8192,true);
342N/A
342N/A if (cg1r->do_traversal()) {
342N/A updateRS(0);
342N/A // Have to do this again after updaters
342N/A cleanupHRRS();
342N/A }
342N/A }
342N/A guarantee( _cards_scanned == NULL, "invariant" );
342N/A _cards_scanned = NEW_C_HEAP_ARRAY(size_t, n_workers());
545N/A for (uint i = 0; i < n_workers(); ++i) {
545N/A _cards_scanned[i] = 0;
545N/A }
342N/A _total_cards_scanned = 0;
342N/A}
342N/A
342N/A
342N/Aclass cleanUpIteratorsClosure : public HeapRegionClosure {
342N/A bool doHeapRegion(HeapRegion *r) {
342N/A HeapRegionRemSet* hrrs = r->rem_set();
342N/A hrrs->init_for_par_iteration();
342N/A return false;
342N/A }
342N/A};
342N/A
342N/Avoid HRInto_G1RemSet::cleanup_after_oops_into_collection_set_do() {
342N/A guarantee( _cards_scanned != NULL, "invariant" );
342N/A _total_cards_scanned = 0;
342N/A for (uint i = 0; i < n_workers(); ++i)
342N/A _total_cards_scanned += _cards_scanned[i];
342N/A FREE_C_HEAP_ARRAY(size_t, _cards_scanned);
342N/A _cards_scanned = NULL;
342N/A // Cleanup after copy
342N/A#if G1_REM_SET_LOGGING
342N/A PrintRSClosure cl;
342N/A _g1->heap_region_iterate(&cl);
342N/A#endif
342N/A _g1->set_refine_cte_cl_concurrency(true);
342N/A cleanUpIteratorsClosure iterClosure;
342N/A _g1->collection_set_iterate(&iterClosure);
342N/A // Set all cards back to clean.
342N/A _g1->cleanUpCardTable();
342N/A if (ParallelGCThreads > 0) {
342N/A ConcurrentG1Refine* cg1r = _g1->concurrent_g1_refine();
342N/A if (cg1r->do_traversal()) {
342N/A cg1r->cg1rThread()->set_do_traversal(false);
342N/A }
342N/A for (uint i = 0; i < ParallelGCThreads; i++) {
342N/A delete _new_refs[i];
342N/A }
342N/A set_par_traversal(false);
342N/A }
342N/A assert(!_par_traversal_in_progress, "Invariant between iterations.");
342N/A}
342N/A
342N/Aclass UpdateRSObjectClosure: public ObjectClosure {
342N/A UpdateRSOopClosure* _update_rs_oop_cl;
342N/Apublic:
342N/A UpdateRSObjectClosure(UpdateRSOopClosure* update_rs_oop_cl) :
342N/A _update_rs_oop_cl(update_rs_oop_cl) {}
342N/A void do_object(oop obj) {
342N/A obj->oop_iterate(_update_rs_oop_cl);
342N/A }
342N/A
342N/A};
342N/A
342N/Aclass ScrubRSClosure: public HeapRegionClosure {
342N/A G1CollectedHeap* _g1h;
342N/A BitMap* _region_bm;
342N/A BitMap* _card_bm;
342N/A CardTableModRefBS* _ctbs;
342N/Apublic:
342N/A ScrubRSClosure(BitMap* region_bm, BitMap* card_bm) :
342N/A _g1h(G1CollectedHeap::heap()),
342N/A _region_bm(region_bm), _card_bm(card_bm),
342N/A _ctbs(NULL)
342N/A {
342N/A ModRefBarrierSet* bs = _g1h->mr_bs();
342N/A guarantee(bs->is_a(BarrierSet::CardTableModRef), "Precondition");
342N/A _ctbs = (CardTableModRefBS*)bs;
342N/A }
342N/A
342N/A bool doHeapRegion(HeapRegion* r) {
342N/A if (!r->continuesHumongous()) {
342N/A r->rem_set()->scrub(_ctbs, _region_bm, _card_bm);
342N/A }
342N/A return false;
342N/A }
342N/A};
342N/A
342N/Avoid HRInto_G1RemSet::scrub(BitMap* region_bm, BitMap* card_bm) {
342N/A ScrubRSClosure scrub_cl(region_bm, card_bm);
342N/A _g1->heap_region_iterate(&scrub_cl);
342N/A}
342N/A
342N/Avoid HRInto_G1RemSet::scrub_par(BitMap* region_bm, BitMap* card_bm,
342N/A int worker_num, int claim_val) {
342N/A ScrubRSClosure scrub_cl(region_bm, card_bm);
342N/A _g1->heap_region_par_iterate_chunked(&scrub_cl, worker_num, claim_val);
342N/A}
342N/A
342N/A
342N/Aclass ConcRefineRegionClosure: public HeapRegionClosure {
342N/A G1CollectedHeap* _g1h;
342N/A CardTableModRefBS* _ctbs;
342N/A ConcurrentGCThread* _cgc_thrd;
342N/A ConcurrentG1Refine* _cg1r;
342N/A unsigned _cards_processed;
342N/A UpdateRSOopClosure _update_rs_oop_cl;
342N/Apublic:
342N/A ConcRefineRegionClosure(CardTableModRefBS* ctbs,
342N/A ConcurrentG1Refine* cg1r,
342N/A HRInto_G1RemSet* g1rs) :
342N/A _ctbs(ctbs), _cg1r(cg1r), _cgc_thrd(cg1r->cg1rThread()),
342N/A _update_rs_oop_cl(g1rs), _cards_processed(0),
342N/A _g1h(G1CollectedHeap::heap())
342N/A {}
342N/A
342N/A bool doHeapRegion(HeapRegion* r) {
342N/A if (!r->in_collection_set() &&
342N/A !r->continuesHumongous() &&
342N/A !r->is_young()) {
342N/A _update_rs_oop_cl.set_from(r);
342N/A UpdateRSObjectClosure update_rs_obj_cl(&_update_rs_oop_cl);
342N/A
342N/A // For each run of dirty card in the region:
342N/A // 1) Clear the cards.
342N/A // 2) Process the range corresponding to the run, adding any
342N/A // necessary RS entries.
342N/A // 1 must precede 2, so that a concurrent modification redirties the
342N/A // card. If a processing attempt does not succeed, because it runs
342N/A // into an unparseable region, we will do binary search to find the
342N/A // beginning of the next parseable region.
342N/A HeapWord* startAddr = r->bottom();
342N/A HeapWord* endAddr = r->used_region().end();
342N/A HeapWord* lastAddr;
342N/A HeapWord* nextAddr;
342N/A
342N/A for (nextAddr = lastAddr = startAddr;
342N/A nextAddr < endAddr;
342N/A nextAddr = lastAddr) {
342N/A MemRegion dirtyRegion;
342N/A
342N/A // Get and clear dirty region from card table
342N/A MemRegion next_mr(nextAddr, endAddr);
342N/A dirtyRegion =
342N/A _ctbs->dirty_card_range_after_reset(
342N/A next_mr,
342N/A true, CardTableModRefBS::clean_card_val());
342N/A assert(dirtyRegion.start() >= nextAddr,
342N/A "returned region inconsistent?");
342N/A
342N/A if (!dirtyRegion.is_empty()) {
342N/A HeapWord* stop_point =
342N/A r->object_iterate_mem_careful(dirtyRegion,
342N/A &update_rs_obj_cl);
342N/A if (stop_point == NULL) {
342N/A lastAddr = dirtyRegion.end();
342N/A _cards_processed +=
342N/A (int) (dirtyRegion.word_size() / CardTableModRefBS::card_size_in_words);
342N/A } else {
342N/A // We're going to skip one or more cards that we can't parse.
342N/A HeapWord* next_parseable_card =
342N/A r->next_block_start_careful(stop_point);
342N/A // Round this up to a card boundary.
342N/A next_parseable_card =
342N/A _ctbs->addr_for(_ctbs->byte_after_const(next_parseable_card));
342N/A // Now we invalidate the intervening cards so we'll see them
342N/A // again.
342N/A MemRegion remaining_dirty =
342N/A MemRegion(stop_point, dirtyRegion.end());
342N/A MemRegion skipped =
342N/A MemRegion(stop_point, next_parseable_card);
342N/A _ctbs->invalidate(skipped.intersection(remaining_dirty));
342N/A
342N/A // Now start up again where we can parse.
342N/A lastAddr = next_parseable_card;
342N/A
342N/A // Count how many we did completely.
342N/A _cards_processed +=
342N/A (stop_point - dirtyRegion.start()) /
342N/A CardTableModRefBS::card_size_in_words;
342N/A }
342N/A // Allow interruption at regular intervals.
342N/A // (Might need to make them more regular, if we get big
342N/A // dirty regions.)
342N/A if (_cgc_thrd != NULL) {
342N/A if (_cgc_thrd->should_yield()) {
342N/A _cgc_thrd->yield();
342N/A switch (_cg1r->get_pya()) {
342N/A case PYA_continue:
342N/A // This may have changed: re-read.
342N/A endAddr = r->used_region().end();
342N/A continue;
342N/A case PYA_restart: case PYA_cancel:
342N/A return true;
342N/A }
342N/A }
342N/A }
342N/A } else {
342N/A break;
342N/A }
342N/A }
342N/A }
342N/A // A good yield opportunity.
342N/A if (_cgc_thrd != NULL) {
342N/A if (_cgc_thrd->should_yield()) {
342N/A _cgc_thrd->yield();
342N/A switch (_cg1r->get_pya()) {
342N/A case PYA_restart: case PYA_cancel:
342N/A return true;
342N/A default:
342N/A break;
342N/A }
342N/A
342N/A }
342N/A }
342N/A return false;
342N/A }
342N/A
342N/A unsigned cards_processed() { return _cards_processed; }
342N/A};
342N/A
342N/A
342N/Avoid HRInto_G1RemSet::concurrentRefinementPass(ConcurrentG1Refine* cg1r) {
342N/A ConcRefineRegionClosure cr_cl(ct_bs(), cg1r, this);
342N/A _g1->heap_region_iterate(&cr_cl);
342N/A _conc_refine_traversals++;
342N/A _conc_refine_cards += cr_cl.cards_processed();
342N/A}
342N/A
342N/Astatic IntHistogram out_of_histo(50, 50);
342N/A
342N/A
342N/A
342N/Avoid HRInto_G1RemSet::concurrentRefineOneCard(jbyte* card_ptr, int worker_i) {
342N/A // If the card is no longer dirty, nothing to do.
342N/A if (*card_ptr != CardTableModRefBS::dirty_card_val()) return;
342N/A
342N/A // Construct the region representing the card.
342N/A HeapWord* start = _ct_bs->addr_for(card_ptr);
342N/A // And find the region containing it.
342N/A HeapRegion* r = _g1->heap_region_containing(start);
342N/A if (r == NULL) {
342N/A guarantee(_g1->is_in_permanent(start), "Or else where?");
342N/A return; // Not in the G1 heap (might be in perm, for example.)
342N/A }
342N/A // Why do we have to check here whether a card is on a young region,
342N/A // given that we dirty young regions and, as a result, the
342N/A // post-barrier is supposed to filter them out and never to enqueue
342N/A // them? When we allocate a new region as the "allocation region" we
342N/A // actually dirty its cards after we release the lock, since card
342N/A // dirtying while holding the lock was a performance bottleneck. So,
342N/A // as a result, it is possible for other threads to actually
342N/A // allocate objects in the region (after the acquire the lock)
342N/A // before all the cards on the region are dirtied. This is unlikely,
342N/A // and it doesn't happen often, but it can happen. So, the extra
342N/A // check below filters out those cards.
342N/A if (r->is_young()) {
342N/A return;
342N/A }
342N/A // While we are processing RSet buffers during the collection, we
342N/A // actually don't want to scan any cards on the collection set,
342N/A // since we don't want to update remebered sets with entries that
342N/A // point into the collection set, given that live objects from the
342N/A // collection set are about to move and such entries will be stale
342N/A // very soon. This change also deals with a reliability issue which
342N/A // involves scanning a card in the collection set and coming across
342N/A // an array that was being chunked and looking malformed. Note,
342N/A // however, that if evacuation fails, we have to scan any objects
342N/A // that were not moved and create any missing entries.
342N/A if (r->in_collection_set()) {
342N/A return;
342N/A }
342N/A
342N/A // Should we defer it?
342N/A if (_cg1r->use_cache()) {
342N/A card_ptr = _cg1r->cache_insert(card_ptr);
342N/A // If it was not an eviction, nothing to do.
342N/A if (card_ptr == NULL) return;
342N/A
342N/A // OK, we have to reset the card start, region, etc.
342N/A start = _ct_bs->addr_for(card_ptr);
342N/A r = _g1->heap_region_containing(start);
342N/A if (r == NULL) {
342N/A guarantee(_g1->is_in_permanent(start), "Or else where?");
342N/A return; // Not in the G1 heap (might be in perm, for example.)
342N/A }
342N/A guarantee(!r->is_young(), "It was evicted in the current minor cycle.");
342N/A }
342N/A
342N/A HeapWord* end = _ct_bs->addr_for(card_ptr + 1);
342N/A MemRegion dirtyRegion(start, end);
342N/A
342N/A#if CARD_REPEAT_HISTO
342N/A init_ct_freq_table(_g1->g1_reserved_obj_bytes());
342N/A ct_freq_note_card(_ct_bs->index_for(start));
342N/A#endif
342N/A
342N/A UpdateRSOopClosure update_rs_oop_cl(this, worker_i);
342N/A update_rs_oop_cl.set_from(r);
342N/A FilterOutOfRegionClosure filter_then_update_rs_oop_cl(r, &update_rs_oop_cl);
342N/A
342N/A // Undirty the card.
342N/A *card_ptr = CardTableModRefBS::clean_card_val();
342N/A // We must complete this write before we do any of the reads below.
342N/A OrderAccess::storeload();
342N/A // And process it, being careful of unallocated portions of TLAB's.
342N/A HeapWord* stop_point =
342N/A r->oops_on_card_seq_iterate_careful(dirtyRegion,
342N/A &filter_then_update_rs_oop_cl);
342N/A // If stop_point is non-null, then we encountered an unallocated region
342N/A // (perhaps the unfilled portion of a TLAB.) For now, we'll dirty the
342N/A // card and re-enqueue: if we put off the card until a GC pause, then the
342N/A // unallocated portion will be filled in. Alternatively, we might try
342N/A // the full complexity of the technique used in "regular" precleaning.
342N/A if (stop_point != NULL) {
342N/A // The card might have gotten re-dirtied and re-enqueued while we
342N/A // worked. (In fact, it's pretty likely.)
342N/A if (*card_ptr != CardTableModRefBS::dirty_card_val()) {
342N/A *card_ptr = CardTableModRefBS::dirty_card_val();
342N/A MutexLockerEx x(Shared_DirtyCardQ_lock,
342N/A Mutex::_no_safepoint_check_flag);
342N/A DirtyCardQueue* sdcq =
342N/A JavaThread::dirty_card_queue_set().shared_dirty_card_queue();
342N/A sdcq->enqueue(card_ptr);
342N/A }
342N/A } else {
342N/A out_of_histo.add_entry(filter_then_update_rs_oop_cl.out_of_region());
342N/A _conc_refine_cards++;
342N/A }
342N/A}
342N/A
342N/Aclass HRRSStatsIter: public HeapRegionClosure {
342N/A size_t _occupied;
342N/A size_t _total_mem_sz;
342N/A size_t _max_mem_sz;
342N/A HeapRegion* _max_mem_sz_region;
342N/Apublic:
342N/A HRRSStatsIter() :
342N/A _occupied(0),
342N/A _total_mem_sz(0),
342N/A _max_mem_sz(0),
342N/A _max_mem_sz_region(NULL)
342N/A {}
342N/A
342N/A bool doHeapRegion(HeapRegion* r) {
342N/A if (r->continuesHumongous()) return false;
342N/A size_t mem_sz = r->rem_set()->mem_size();
342N/A if (mem_sz > _max_mem_sz) {
342N/A _max_mem_sz = mem_sz;
342N/A _max_mem_sz_region = r;
342N/A }
342N/A _total_mem_sz += mem_sz;
342N/A size_t occ = r->rem_set()->occupied();
342N/A _occupied += occ;
342N/A return false;
342N/A }
342N/A size_t total_mem_sz() { return _total_mem_sz; }
342N/A size_t max_mem_sz() { return _max_mem_sz; }
342N/A size_t occupied() { return _occupied; }
342N/A HeapRegion* max_mem_sz_region() { return _max_mem_sz_region; }
342N/A};
342N/A
342N/Avoid HRInto_G1RemSet::print_summary_info() {
342N/A G1CollectedHeap* g1 = G1CollectedHeap::heap();
342N/A ConcurrentG1RefineThread* cg1r_thrd =
342N/A g1->concurrent_g1_refine()->cg1rThread();
342N/A
342N/A#if CARD_REPEAT_HISTO
342N/A gclog_or_tty->print_cr("\nG1 card_repeat count histogram: ");
342N/A gclog_or_tty->print_cr(" # of repeats --> # of cards with that number.");
342N/A card_repeat_count.print_on(gclog_or_tty);
342N/A#endif
342N/A
342N/A if (FILTEROUTOFREGIONCLOSURE_DOHISTOGRAMCOUNT) {
342N/A gclog_or_tty->print_cr("\nG1 rem-set out-of-region histogram: ");
342N/A gclog_or_tty->print_cr(" # of CS ptrs --> # of cards with that number.");
342N/A out_of_histo.print_on(gclog_or_tty);
342N/A }
342N/A gclog_or_tty->print_cr("\n Concurrent RS processed %d cards in "
342N/A "%5.2fs.",
342N/A _conc_refine_cards, cg1r_thrd->vtime_accum());
342N/A
342N/A DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
342N/A jint tot_processed_buffers =
342N/A dcqs.processed_buffers_mut() + dcqs.processed_buffers_rs_thread();
342N/A gclog_or_tty->print_cr(" Of %d completed buffers:", tot_processed_buffers);
342N/A gclog_or_tty->print_cr(" %8d (%5.1f%%) by conc RS thread.",
342N/A dcqs.processed_buffers_rs_thread(),
342N/A 100.0*(float)dcqs.processed_buffers_rs_thread()/
342N/A (float)tot_processed_buffers);
342N/A gclog_or_tty->print_cr(" %8d (%5.1f%%) by mutator threads.",
342N/A dcqs.processed_buffers_mut(),
342N/A 100.0*(float)dcqs.processed_buffers_mut()/
342N/A (float)tot_processed_buffers);
342N/A gclog_or_tty->print_cr(" Did %d concurrent refinement traversals.",
342N/A _conc_refine_traversals);
342N/A if (!G1RSBarrierUseQueue) {
342N/A gclog_or_tty->print_cr(" Scanned %8.2f cards/traversal.",
342N/A _conc_refine_traversals > 0 ?
342N/A (float)_conc_refine_cards/(float)_conc_refine_traversals :
342N/A 0);
342N/A }
342N/A gclog_or_tty->print_cr("");
342N/A if (G1UseHRIntoRS) {
342N/A HRRSStatsIter blk;
342N/A g1->heap_region_iterate(&blk);
342N/A gclog_or_tty->print_cr(" Total heap region rem set sizes = " SIZE_FORMAT "K."
342N/A " Max = " SIZE_FORMAT "K.",
342N/A blk.total_mem_sz()/K, blk.max_mem_sz()/K);
342N/A gclog_or_tty->print_cr(" Static structures = " SIZE_FORMAT "K,"
342N/A " free_lists = " SIZE_FORMAT "K.",
342N/A HeapRegionRemSet::static_mem_size()/K,
342N/A HeapRegionRemSet::fl_mem_size()/K);
342N/A gclog_or_tty->print_cr(" %d occupied cards represented.",
342N/A blk.occupied());
342N/A gclog_or_tty->print_cr(" Max sz region = [" PTR_FORMAT ", " PTR_FORMAT " )"
342N/A " %s, cap = " SIZE_FORMAT "K, occ = " SIZE_FORMAT "K.",
342N/A blk.max_mem_sz_region()->bottom(), blk.max_mem_sz_region()->end(),
342N/A (blk.max_mem_sz_region()->popular() ? "POP" : ""),
342N/A (blk.max_mem_sz_region()->rem_set()->mem_size() + K - 1)/K,
342N/A (blk.max_mem_sz_region()->rem_set()->occupied() + K - 1)/K);
342N/A gclog_or_tty->print_cr(" Did %d coarsenings.",
342N/A HeapRegionRemSet::n_coarsenings());
342N/A
342N/A }
342N/A}
342N/Avoid HRInto_G1RemSet::prepare_for_verify() {
342N/A if (G1HRRSFlushLogBuffersOnVerify && VerifyBeforeGC && !_g1->full_collection()) {
342N/A cleanupHRRS();
342N/A _g1->set_refine_cte_cl_concurrency(false);
342N/A if (SafepointSynchronize::is_at_safepoint()) {
342N/A DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
342N/A dcqs.concatenate_logs();
342N/A }
342N/A bool cg1r_use_cache = _cg1r->use_cache();
342N/A _cg1r->set_use_cache(false);
342N/A updateRS(0);
342N/A _cg1r->set_use_cache(cg1r_use_cache);
342N/A }
342N/A}