g1RemSet.cpp revision 890
342N/A/*
579N/A * Copyright 2001-2009 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 }
845N/A virtual void do_oop(narrowOop* p) { do_oop_work(p); }
845N/A virtual void do_oop( oop* p) { do_oop_work(p); }
845N/A template <class T> void do_oop_work(T* p) {
845N/A oop obj = oopDesc::load_decode_heap_oop(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 VerifyRSCleanCardOopClosure: public OopClosure {
342N/A G1CollectedHeap* _g1;
342N/Apublic:
342N/A VerifyRSCleanCardOopClosure(G1CollectedHeap* g1) : _g1(g1) {}
342N/A
845N/A virtual void do_oop(narrowOop* p) { do_oop_work(p); }
845N/A virtual void do_oop( oop* p) { do_oop_work(p); }
845N/A template <class T> void do_oop_work(T* p) {
845N/A oop obj = oopDesc::load_decode_heap_oop(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);
616N/A guarantee(n_workers() > 0, "There should be some workers");
845N/A _new_refs = NEW_C_HEAP_ARRAY(GrowableArray<OopOrNarrowOopStar>*, n_workers());
616N/A for (uint i = 0; i < n_workers(); i++) {
845N/A _new_refs[i] = new (ResourceObj::C_HEAP) GrowableArray<OopOrNarrowOopStar>(8192,true);
616N/A }
342N/A}
342N/A
342N/AHRInto_G1RemSet::~HRInto_G1RemSet() {
342N/A delete _seq_task;
616N/A for (uint i = 0; i < n_workers(); i++) {
616N/A delete _new_refs[i];
616N/A }
845N/A FREE_C_HEAP_ARRAY(GrowableArray<OopOrNarrowOopStar>*, _new_refs);
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;
747N/A size_t _min_skip_distance, _max_skip_distance;
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());
747N/A _min_skip_distance = 16;
747N/A _max_skip_distance = 2 * _g1h->n_par_threads() * _min_skip_distance;
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;
796N/A _g1h->push_dirty_cards_region(r);
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;
747N/A size_t skip_distance = 0, current_card = 0, jump_to_card = 0;
342N/A while (iter->has_next(card_index)) {
747N/A if (current_card < jump_to_card) {
747N/A ++current_card;
747N/A continue;
747N/A }
342N/A HeapWord* card_start = _g1h->bot_shared()->address_for_index(card_index);
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
796N/A if (!card_region->is_on_dirty_cards_region_list()) {
796N/A _g1h->push_dirty_cards_region(card_region);
796N/A }
796N/A
747N/A // If the card is dirty, then we will scan it during updateRS.
747N/A if (!card_region->in_collection_set() && !_ct_bs->is_card_dirty(card_index)) {
747N/A if (!_ct_bs->is_card_claimed(card_index) && _ct_bs->claim_card(card_index)) {
342N/A scanCard(card_index, card_region);
747N/A } else if (_try_claimed) {
747N/A if (jump_to_card == 0 || jump_to_card != current_card) {
747N/A // We did some useful work in the previous iteration.
747N/A // Decrease the distance.
747N/A skip_distance = MAX2(skip_distance >> 1, _min_skip_distance);
747N/A } else {
747N/A // Previous iteration resulted in a claim failure.
747N/A // Increase the distance.
747N/A skip_distance = MIN2(skip_distance << 1, _max_skip_distance);
747N/A }
747N/A jump_to_card = current_card + skip_distance;
747N/A }
342N/A }
747N/A ++current_card;
342N/A }
747N/A if (!_try_claimed) {
747N/A hrrs->set_iter_complete();
747N/A }
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);
616N/A
616N/A double scan_new_refs_time_ms = _g1p->get_scan_new_refs_time(worker_i);
616N/A if (scan_new_refs_time_ms > 0.0) {
616N/A closure_app_time_ms += scan_new_refs_time_ms;
342N/A }
616N/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
794N/A // Apply the appropriate closure to all remaining log entries.
794N/A _g1->iterate_dirty_card_closure(false, worker_i);
794N/A // Now there should be no dirty cards.
794N/A if (G1RSLogCheckCardTable) {
794N/A CountNonCleanMemRegionClosure cl(_g1);
794N/A _ct_bs->mod_card_iterate(&cl);
794N/A // XXX This isn't true any more: keeping cards of young regions
794N/A // marked dirty broke it. Need some reasonable fix.
794N/A guarantee(cl.n() == 0, "Card table should be clean.");
342N/A }
794N/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
845N/Atemplate <class T> void
845N/AHRInto_G1RemSet::scanNewRefsRS_work(OopsInHeapRegionClosure* oc,
845N/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());
616N/A for (int i = 0; i < _new_refs[worker_i]->length(); i++) {
845N/A T* p = (T*) _new_refs[worker_i]->at(i);
845N/A oop obj = oopDesc::load_decode_heap_oop(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 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::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) {
638N/A // The two flags below were introduced temporarily to serialize
638N/A // the updating and scanning of remembered sets. There are some
638N/A // race conditions when these two operations are done in parallel
638N/A // and they are causing failures. When we resolve said race
638N/A // conditions, we'll revert back to parallel remembered set
638N/A // updating and scanning. See CRs 6677707 and 6677708.
751N/A if (G1ParallelRSetUpdatingEnabled || (worker_i == 0)) {
342N/A updateRS(worker_i);
342N/A scanNewRefsRS(oc, worker_i);
648N/A } else {
794N/A _g1p->record_update_rs_start_time(worker_i, os::elapsedTime() * 1000.0);
648N/A _g1p->record_update_rs_processed_buffers(worker_i, 0.0);
648N/A _g1p->record_update_rs_time(worker_i, 0.0);
648N/A _g1p->record_scan_new_refs_time(worker_i, 0.0);
638N/A }
751N/A if (G1ParallelRSetScanningEnabled || (worker_i == 0)) {
342N/A scanRS(oc, worker_i);
648N/A } else {
794N/A _g1p->record_scan_rs_start_time(worker_i, os::elapsedTime() * 1000.0);
648N/A _g1p->record_scan_rs_time(worker_i, 0.0);
342N/A }
342N/A } else {
342N/A assert(worker_i == 0, "invariant");
342N/A updateRS(0);
616N/A scanNewRefsRS(oc, 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);
616N/A _seq_task->set_par_threads((int)n_workers());
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
616N/Aclass UpdateRSetOopsIntoCSImmediate : public OopClosure {
616N/A G1CollectedHeap* _g1;
616N/Apublic:
616N/A UpdateRSetOopsIntoCSImmediate(G1CollectedHeap* g1) : _g1(g1) { }
845N/A virtual void do_oop(narrowOop* p) { do_oop_work(p); }
845N/A virtual void do_oop( oop* p) { do_oop_work(p); }
845N/A template <class T> void do_oop_work(T* p) {
845N/A HeapRegion* to = _g1->heap_region_containing(oopDesc::load_decode_heap_oop(p));
616N/A if (to->in_collection_set()) {
677N/A to->rem_set()->add_reference(p, 0);
616N/A }
616N/A }
616N/A};
616N/A
616N/Aclass UpdateRSetOopsIntoCSDeferred : public OopClosure {
616N/A G1CollectedHeap* _g1;
616N/A CardTableModRefBS* _ct_bs;
616N/A DirtyCardQueue* _dcq;
616N/Apublic:
616N/A UpdateRSetOopsIntoCSDeferred(G1CollectedHeap* g1, DirtyCardQueue* dcq) :
616N/A _g1(g1), _ct_bs((CardTableModRefBS*)_g1->barrier_set()), _dcq(dcq) { }
845N/A virtual void do_oop(narrowOop* p) { do_oop_work(p); }
845N/A virtual void do_oop( oop* p) { do_oop_work(p); }
845N/A template <class T> void do_oop_work(T* p) {
845N/A oop obj = oopDesc::load_decode_heap_oop(p);
616N/A if (_g1->obj_in_cs(obj)) {
616N/A size_t card_index = _ct_bs->index_for(p);
616N/A if (_ct_bs->mark_card_deferred(card_index)) {
616N/A _dcq->enqueue((jbyte*)_ct_bs->byte_for_index(card_index));
616N/A }
616N/A }
616N/A }
616N/A};
616N/A
845N/Atemplate <class T> void HRInto_G1RemSet::new_refs_iterate_work(OopClosure* cl) {
616N/A for (size_t i = 0; i < n_workers(); i++) {
616N/A for (int j = 0; j < _new_refs[i]->length(); j++) {
845N/A T* p = (T*) _new_refs[i]->at(j);
616N/A cl->do_oop(p);
616N/A }
616N/A }
616N/A}
616N/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();
794N/A
342N/A if (ParallelGCThreads > 0) {
342N/A set_par_traversal(false);
342N/A }
616N/A
616N/A if (_g1->evacuation_failed()) {
616N/A // Restore remembered sets for the regions pointing into
616N/A // the collection set.
616N/A if (G1DeferredRSUpdate) {
616N/A DirtyCardQueue dcq(&_g1->dirty_card_queue_set());
616N/A UpdateRSetOopsIntoCSDeferred deferred_update(_g1, &dcq);
616N/A new_refs_iterate(&deferred_update);
616N/A } else {
616N/A UpdateRSetOopsIntoCSImmediate immediate_update(_g1);
616N/A new_refs_iterate(&immediate_update);
616N/A }
616N/A }
616N/A for (uint i = 0; i < n_workers(); i++) {
616N/A _new_refs[i]->clear();
616N/A }
616N/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/Astatic IntHistogram out_of_histo(50, 50);
342N/A
890N/Avoid HRInto_G1RemSet::concurrentRefineOneCard_impl(jbyte* card_ptr, int worker_i) {
890N/A // Construct the region representing the card.
890N/A HeapWord* start = _ct_bs->addr_for(card_ptr);
890N/A // And find the region containing it.
890N/A HeapRegion* r = _g1->heap_region_containing(start);
890N/A assert(r != NULL, "unexpected null");
890N/A
890N/A HeapWord* end = _ct_bs->addr_for(card_ptr + 1);
890N/A MemRegion dirtyRegion(start, end);
890N/A
890N/A#if CARD_REPEAT_HISTO
890N/A init_ct_freq_table(_g1->g1_reserved_obj_bytes());
890N/A ct_freq_note_card(_ct_bs->index_for(start));
890N/A#endif
890N/A
890N/A UpdateRSOopClosure update_rs_oop_cl(this, worker_i);
890N/A update_rs_oop_cl.set_from(r);
890N/A FilterOutOfRegionClosure filter_then_update_rs_oop_cl(r, &update_rs_oop_cl);
890N/A
890N/A // Undirty the card.
890N/A *card_ptr = CardTableModRefBS::clean_card_val();
890N/A // We must complete this write before we do any of the reads below.
890N/A OrderAccess::storeload();
890N/A // And process it, being careful of unallocated portions of TLAB's.
890N/A HeapWord* stop_point =
890N/A r->oops_on_card_seq_iterate_careful(dirtyRegion,
890N/A &filter_then_update_rs_oop_cl);
890N/A // If stop_point is non-null, then we encountered an unallocated region
890N/A // (perhaps the unfilled portion of a TLAB.) For now, we'll dirty the
890N/A // card and re-enqueue: if we put off the card until a GC pause, then the
890N/A // unallocated portion will be filled in. Alternatively, we might try
890N/A // the full complexity of the technique used in "regular" precleaning.
890N/A if (stop_point != NULL) {
890N/A // The card might have gotten re-dirtied and re-enqueued while we
890N/A // worked. (In fact, it's pretty likely.)
890N/A if (*card_ptr != CardTableModRefBS::dirty_card_val()) {
890N/A *card_ptr = CardTableModRefBS::dirty_card_val();
890N/A MutexLockerEx x(Shared_DirtyCardQ_lock,
890N/A Mutex::_no_safepoint_check_flag);
890N/A DirtyCardQueue* sdcq =
890N/A JavaThread::dirty_card_queue_set().shared_dirty_card_queue();
890N/A sdcq->enqueue(card_ptr);
890N/A }
890N/A } else {
890N/A out_of_histo.add_entry(filter_then_update_rs_oop_cl.out_of_region());
890N/A _conc_refine_cards++;
890N/A }
890N/A}
890N/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.
637N/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
890N/A // Should we defer processing the card?
890N/A //
890N/A // Previously the result from the insert_cache call would be
890N/A // either card_ptr (implying that card_ptr was currently "cold"),
890N/A // null (meaning we had inserted the card ptr into the "hot"
890N/A // cache, which had some headroom), or a "hot" card ptr
890N/A // extracted from the "hot" cache.
890N/A //
890N/A // Now that the _card_counts cache in the ConcurrentG1Refine
890N/A // instance is an evicting hash table, the result we get back
890N/A // could be from evicting the card ptr in an already occupied
890N/A // bucket (in which case we have replaced the card ptr in the
890N/A // bucket with card_ptr and "defer" is set to false). To avoid
890N/A // having a data structure (updates to which would need a lock)
890N/A // to hold these unprocessed dirty cards, we need to immediately
890N/A // process card_ptr. The actions needed to be taken on return
890N/A // from cache_insert are summarized in the following table:
890N/A //
890N/A // res defer action
890N/A // --------------------------------------------------------------
890N/A // null false card evicted from _card_counts & replaced with
890N/A // card_ptr; evicted ptr added to hot cache.
890N/A // No need to process res; immediately process card_ptr
890N/A //
890N/A // null true card not evicted from _card_counts; card_ptr added
890N/A // to hot cache.
890N/A // Nothing to do.
890N/A //
890N/A // non-null false card evicted from _card_counts & replaced with
890N/A // card_ptr; evicted ptr is currently "cold" or
890N/A // caused an eviction from the hot cache.
890N/A // Immediately process res; process card_ptr.
890N/A //
890N/A // non-null true card not evicted from _card_counts; card_ptr is
890N/A // currently cold, or caused an eviction from hot
890N/A // cache.
890N/A // Immediately process res; no need to process card_ptr.
342N/A
890N/A jbyte* res = card_ptr;
890N/A bool defer = false;
890N/A if (_cg1r->use_cache()) {
890N/A jbyte* res = _cg1r->cache_insert(card_ptr, &defer);
890N/A if (res != NULL && (res != card_ptr || defer)) {
890N/A start = _ct_bs->addr_for(res);
890N/A r = _g1->heap_region_containing(start);
890N/A if (r == NULL) {
890N/A assert(_g1->is_in_permanent(start), "Or else where?");
890N/A } else {
890N/A guarantee(!r->is_young(), "It was evicted in the current minor cycle.");
890N/A // Process card pointer we get back from the hot card cache
890N/A concurrentRefineOneCard_impl(res, worker_i);
890N/A }
342N/A }
342N/A }
342N/A
890N/A if (!defer) {
890N/A concurrentRefineOneCard_impl(card_ptr, worker_i);
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
794N/Aclass PrintRSThreadVTimeClosure : public ThreadClosure {
794N/Apublic:
794N/A virtual void do_thread(Thread *t) {
794N/A ConcurrentG1RefineThread* crt = (ConcurrentG1RefineThread*) t;
794N/A gclog_or_tty->print(" %5.2f", crt->vtime_accum());
794N/A }
794N/A};
794N/A
342N/Avoid HRInto_G1RemSet::print_summary_info() {
342N/A G1CollectedHeap* g1 = G1CollectedHeap::heap();
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 }
794N/A gclog_or_tty->print_cr("\n Concurrent RS processed %d cards",
794N/A _conc_refine_cards);
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);
794N/A gclog_or_tty->print_cr(" %8d (%5.1f%%) by conc RS threads.",
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);
794N/A gclog_or_tty->print_cr(" Conc RS threads times(s)");
794N/A PrintRSThreadVTimeClosure p;
794N/A gclog_or_tty->print(" ");
794N/A g1->concurrent_g1_refine()->threads_do(&p);
342N/A gclog_or_tty->print_cr("");
794N/A
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 " )"
677N/A ", 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()->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() {
637N/A if (G1HRRSFlushLogBuffersOnVerify &&
637N/A (VerifyBeforeGC || VerifyAfterGC)
637N/A && !_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);
637N/A
637N/A assert(JavaThread::dirty_card_queue_set().completed_buffers_num() == 0, "All should be consumed");
342N/A }
342N/A}