g1RemSet.cpp revision 2819
342N/A/*
2069N/A * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
342N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
342N/A *
342N/A * This code is free software; you can redistribute it and/or modify it
342N/A * under the terms of the GNU General Public License version 2 only, as
342N/A * published by the Free Software Foundation.
342N/A *
342N/A * This code is distributed in the hope that it will be useful, but WITHOUT
342N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
342N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
342N/A * version 2 for more details (a copy is included in the LICENSE file that
342N/A * accompanied this code).
342N/A *
342N/A * You should have received a copy of the GNU General Public License version
342N/A * 2 along with this work; if not, write to the Free Software Foundation,
342N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
342N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
342N/A *
342N/A */
342N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "gc_implementation/g1/bufferingOopClosure.hpp"
1879N/A#include "gc_implementation/g1/concurrentG1Refine.hpp"
1879N/A#include "gc_implementation/g1/concurrentG1RefineThread.hpp"
1879N/A#include "gc_implementation/g1/g1BlockOffsetTable.inline.hpp"
1879N/A#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
1879N/A#include "gc_implementation/g1/g1CollectorPolicy.hpp"
1879N/A#include "gc_implementation/g1/g1OopClosures.inline.hpp"
1879N/A#include "gc_implementation/g1/g1RemSet.inline.hpp"
1879N/A#include "gc_implementation/g1/heapRegionSeq.inline.hpp"
1879N/A#include "memory/iterator.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "utilities/intHisto.hpp"
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
1781N/AG1RemSet::G1RemSet(G1CollectedHeap* g1, CardTableModRefBS* ct_bs)
1781N/A : _g1(g1), _conc_refine_cards(0),
1781N/A _ct_bs(ct_bs), _g1p(_g1->g1_policy()),
342N/A _cg1r(g1->concurrent_g1_refine()),
1625N/A _cset_rs_update_cl(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");
1625N/A _cset_rs_update_cl = NEW_C_HEAP_ARRAY(OopsInHeapRegionClosure*, n_workers());
616N/A for (uint i = 0; i < n_workers(); i++) {
1625N/A _cset_rs_update_cl[i] = NULL;
616N/A }
342N/A}
342N/A
1781N/AG1RemSet::~G1RemSet() {
342N/A delete _seq_task;
616N/A for (uint i = 0; i < n_workers(); i++) {
1625N/A assert(_cset_rs_update_cl[i] == NULL, "it should be");
616N/A }
1625N/A FREE_C_HEAP_ARRAY(OopsInHeapRegionClosure*, _cset_rs_update_cl);
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;
1261N/A int _block_size;
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());
1261N/A _block_size = MAX2<int>(G1RSetScanBlockSize, 1);
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 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);
2414N/A MemRegion sm_region = sp->used_region_at_save_marks();
342N/A MemRegion mr = sm_region.intersection(MemRegion(card_start,card_end));
2414N/A if (!mr.is_empty() && !_ct_bs->is_card_claimed(index)) {
2414N/A // We make the card as "claimed" lazily (so races are possible
2414N/A // but they're benign), which reduces the number of duplicate
2414N/A // scans (the rsets of the regions in the cset can intersect).
2414N/A _ct_bs->set_card_claimed(index);
2414N/A _cards_done++;
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;
2414N/A // If we ever free the collection set concurrently, we should also
2414N/A // clear the card table concurrently therefore we won't need to
2414N/A // add regions of the collection set to the dirty cards region.
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;
1261N/A
1261N/A // We claim cards in block so as to recude the contention. The block size is determined by
1261N/A // the G1RSetScanBlockSize parameter.
1261N/A size_t jump_to_card = hrrs->iter_claimed_next(_block_size);
1261N/A for (size_t current_card = 0; iter->has_next(card_index); current_card++) {
1261N/A if (current_card >= jump_to_card + _block_size) {
1261N/A jump_to_card = hrrs->iter_claimed_next(_block_size);
747N/A }
1261N/A if (current_card < jump_to_card) continue;
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
2414N/A // If the card is dirty, then we will scan it during updateRS.
2414N/A if (!card_region->in_collection_set() &&
2414N/A !_ct_bs->is_card_dirty(card_index)) {
2414N/A scanCard(card_index, card_region);
342N/A }
342N/A }
747N/A if (!_try_claimed) {
747N/A hrrs->set_iter_complete();
747N/A }
342N/A return false;
342N/A }
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
1781N/AHeapRegion* 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
1781N/Avoid G1RemSet::scanRS(OopsInHeapRegionClosure* oc, int worker_i) {
342N/A double rs_time_start = os::elapsedTime();
342N/A HeapRegion *startRegion = calculateStartRegion(worker_i);
342N/A
1261N/A ScanRSClosure scanRScl(oc, worker_i);
2815N/A
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
1261N/A double scan_rs_time_sec = os::elapsedTime() - rs_time_start;
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_time(worker_i, scan_rs_time_sec * 1000.0);
342N/A}
342N/A
1625N/A// Closure used for updating RSets and recording references that
1625N/A// point into the collection set. Only called during an
1625N/A// evacuation pause.
1625N/A
1625N/Aclass RefineRecordRefsIntoCSCardTableEntryClosure: public CardTableEntryClosure {
1625N/A G1RemSet* _g1rs;
1625N/A DirtyCardQueue* _into_cset_dcq;
1625N/Apublic:
1625N/A RefineRecordRefsIntoCSCardTableEntryClosure(G1CollectedHeap* g1h,
1625N/A DirtyCardQueue* into_cset_dcq) :
1625N/A _g1rs(g1h->g1_rem_set()), _into_cset_dcq(into_cset_dcq)
1625N/A {}
1625N/A bool do_card_ptr(jbyte* card_ptr, int worker_i) {
1625N/A // The only time we care about recording cards that
1625N/A // contain references that point into the collection set
1625N/A // is during RSet updating within an evacuation pause.
1625N/A // In this case worker_i should be the id of a GC worker thread.
1625N/A assert(SafepointSynchronize::is_at_safepoint(), "not during an evacuation pause");
2211N/A assert(worker_i < (int) (ParallelGCThreads == 0 ? 1 : ParallelGCThreads), "should be a GC worker");
342N/A
1625N/A if (_g1rs->concurrentRefineOneCard(card_ptr, worker_i, true)) {
1625N/A // 'card_ptr' contains references that point into the collection
1625N/A // set. We need to record the card in the DCQS
1625N/A // (G1CollectedHeap::into_cset_dirty_card_queue_set())
1625N/A // that's used for that purpose.
1625N/A //
1625N/A // Enqueue the card
1625N/A _into_cset_dcq->enqueue(card_ptr);
1625N/A }
1625N/A return true;
1625N/A }
1625N/A};
1625N/A
1781N/Avoid G1RemSet::updateRS(DirtyCardQueue* into_cset_dcq, int worker_i) {
342N/A double start = os::elapsedTime();
1625N/A // Apply the given closure to all remaining log entries.
1625N/A RefineRecordRefsIntoCSCardTableEntryClosure into_cset_update_rs_cl(_g1, into_cset_dcq);
2815N/A
1625N/A _g1->iterate_dirty_card_closure(&into_cset_update_rs_cl, into_cset_dcq, false, worker_i);
1625N/A
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/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
1781N/Avoid G1RemSet::cleanupHRRS() {
342N/A HeapRegionRemSet::cleanup();
342N/A}
342N/A
1781N/Avoid 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
1625N/A // We cache the value of 'oc' closure into the appropriate slot in the
1625N/A // _cset_rs_update_cl for this worker
1625N/A assert(worker_i < (int)n_workers(), "sanity");
1625N/A _cset_rs_update_cl[worker_i] = oc;
1625N/A
1625N/A // A DirtyCardQueue that is used to hold cards containing references
1625N/A // that point into the collection set. This DCQ is associated with a
1625N/A // special DirtyCardQueueSet (see g1CollectedHeap.hpp). Under normal
1625N/A // circumstances (i.e. the pause successfully completes), these cards
1625N/A // are just discarded (there's no need to update the RSets of regions
1625N/A // that were in the collection set - after the pause these regions
1625N/A // are wholly 'free' of live objects. In the event of an evacuation
1625N/A // failure the cards/buffers in this queue set are:
1625N/A // * passed to the DirtyCardQueueSet that is used to manage deferred
1625N/A // RSet updates, or
1625N/A // * scanned for references that point into the collection set
1625N/A // and the RSet of the corresponding region in the collection set
1625N/A // is updated immediately.
1625N/A DirtyCardQueue into_cset_dcq(&_g1->into_cset_dirty_card_queue_set());
1625N/A
1628N/A assert((ParallelGCThreads > 0) || worker_i == 0, "invariant");
1628N/A
1628N/A // The two flags below were introduced temporarily to serialize
1628N/A // the updating and scanning of remembered sets. There are some
1628N/A // race conditions when these two operations are done in parallel
1628N/A // and they are causing failures. When we resolve said race
1628N/A // conditions, we'll revert back to parallel remembered set
1628N/A // updating and scanning. See CRs 6677707 and 6677708.
1628N/A if (G1UseParallelRSetUpdating || (worker_i == 0)) {
1628N/A updateRS(&into_cset_dcq, worker_i);
342N/A } else {
1628N/A _g1p->record_update_rs_processed_buffers(worker_i, 0.0);
1628N/A _g1p->record_update_rs_time(worker_i, 0.0);
1628N/A }
1628N/A if (G1UseParallelRSetScanning || (worker_i == 0)) {
1628N/A scanRS(oc, worker_i);
1628N/A } else {
1628N/A _g1p->record_scan_rs_time(worker_i, 0.0);
342N/A }
1625N/A
1625N/A // We now clear the cached values of _cset_rs_update_cl for this worker
1625N/A _cset_rs_update_cl[worker_i] = NULL;
342N/A}
342N/A
1781N/Avoid G1RemSet::prepare_for_oops_into_collection_set_do() {
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 if (ParallelGCThreads > 0) {
1753N/A _seq_task->set_n_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
1625N/A// This closure, applied to a DirtyCardQueueSet, is used to immediately
1625N/A// update the RSets for the regions in the CSet. For each card it iterates
1625N/A// through the oops which coincide with that card. It scans the reference
1625N/A// fields in each oop; when it finds an oop that points into the collection
1625N/A// set, the RSet for the region containing the referenced object is updated.
1625N/Aclass UpdateRSetCardTableEntryIntoCSetClosure: public CardTableEntryClosure {
616N/A G1CollectedHeap* _g1;
616N/A CardTableModRefBS* _ct_bs;
616N/Apublic:
1625N/A UpdateRSetCardTableEntryIntoCSetClosure(G1CollectedHeap* g1,
1625N/A CardTableModRefBS* bs):
1625N/A _g1(g1), _ct_bs(bs)
1625N/A { }
1625N/A
1625N/A bool do_card_ptr(jbyte* card_ptr, int worker_i) {
1625N/A // Construct the region representing the card.
1625N/A HeapWord* start = _ct_bs->addr_for(card_ptr);
1625N/A // And find the region containing it.
1625N/A HeapRegion* r = _g1->heap_region_containing(start);
1625N/A assert(r != NULL, "unexpected null");
1625N/A
1625N/A // Scan oops in the card looking for references into the collection set
1625N/A HeapWord* end = _ct_bs->addr_for(card_ptr + 1);
1625N/A MemRegion scanRegion(start, end);
1625N/A
1625N/A UpdateRSetImmediate update_rs_cl(_g1->g1_rem_set());
2819N/A FilterIntoCSClosure update_rs_cset_oop_cl(NULL, _g1, &update_rs_cl);
1625N/A FilterOutOfRegionClosure filter_then_update_rs_cset_oop_cl(r, &update_rs_cset_oop_cl);
1625N/A
1625N/A // We can pass false as the "filter_young" parameter here as:
1625N/A // * we should be in a STW pause,
1625N/A // * the DCQS to which this closure is applied is used to hold
1625N/A // references that point into the collection set from the prior
1625N/A // RSet updating,
1625N/A // * the post-write barrier shouldn't be logging updates to young
1625N/A // regions (but there is a situation where this can happen - see
1781N/A // the comment in G1RemSet::concurrentRefineOneCard below -
1625N/A // that should not be applicable here), and
1625N/A // * during actual RSet updating, the filtering of cards in young
1625N/A // regions in HeapRegion::oops_on_card_seq_iterate_careful is
1625N/A // employed.
1625N/A // As a result, when this closure is applied to "refs into cset"
1625N/A // DCQS, we shouldn't see any cards in young regions.
1625N/A update_rs_cl.set_region(r);
1625N/A HeapWord* stop_point =
1625N/A r->oops_on_card_seq_iterate_careful(scanRegion,
2414N/A &filter_then_update_rs_cset_oop_cl,
2414N/A false /* filter_young */,
2414N/A NULL /* card_ptr */);
1625N/A
1625N/A // Since this is performed in the event of an evacuation failure, we
1625N/A // we shouldn't see a non-null stop point
1625N/A assert(stop_point == NULL, "saw an unallocated region");
1625N/A return true;
616N/A }
616N/A};
616N/A
1781N/Avoid G1RemSet::cleanup_after_oops_into_collection_set_do() {
342N/A guarantee( _cards_scanned != NULL, "invariant" );
342N/A _total_cards_scanned = 0;
2602N/A for (uint i = 0; i < n_workers(); ++i) {
342N/A _total_cards_scanned += _cards_scanned[i];
2602N/A }
342N/A FREE_C_HEAP_ARRAY(size_t, _cards_scanned);
342N/A _cards_scanned = NULL;
342N/A // Cleanup after copy
342N/A _g1->set_refine_cte_cl_concurrency(true);
342N/A // Set all cards back to clean.
342N/A _g1->cleanUpCardTable();
794N/A
1625N/A DirtyCardQueueSet& into_cset_dcqs = _g1->into_cset_dirty_card_queue_set();
1625N/A int into_cset_n_buffers = into_cset_dcqs.completed_buffers_num();
1625N/A
616N/A if (_g1->evacuation_failed()) {
1625N/A // Restore remembered sets for the regions pointing into the collection set.
1625N/A
616N/A if (G1DeferredRSUpdate) {
1625N/A // If deferred RS updates are enabled then we just need to transfer
1625N/A // the completed buffers from (a) the DirtyCardQueueSet used to hold
1625N/A // cards that contain references that point into the collection set
1625N/A // to (b) the DCQS used to hold the deferred RS updates
1625N/A _g1->dirty_card_queue_set().merge_bufferlists(&into_cset_dcqs);
616N/A } else {
1625N/A
1625N/A CardTableModRefBS* bs = (CardTableModRefBS*)_g1->barrier_set();
1625N/A UpdateRSetCardTableEntryIntoCSetClosure update_rs_cset_immediate(_g1, bs);
1625N/A
1625N/A int n_completed_buffers = 0;
1625N/A while (into_cset_dcqs.apply_closure_to_completed_buffer(&update_rs_cset_immediate,
1625N/A 0, 0, true)) {
1625N/A n_completed_buffers++;
1625N/A }
1625N/A assert(n_completed_buffers == into_cset_n_buffers, "missed some buffers");
616N/A }
616N/A }
1625N/A
1625N/A // Free any completed buffers in the DirtyCardQueueSet used to hold cards
1625N/A // which contain references that point into the collection.
1625N/A _g1->into_cset_dirty_card_queue_set().clear();
1625N/A assert(_g1->into_cset_dirty_card_queue_set().completed_buffers_num() == 0,
1625N/A "all buffers should be freed");
1625N/A _g1->into_cset_dirty_card_queue_set().clear_n_completed_buffers();
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
1781N/Avoid 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
1781N/Avoid 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
1625N/Aclass TriggerClosure : public OopClosure {
1625N/A bool _trigger;
1625N/Apublic:
1625N/A TriggerClosure() : _trigger(false) { }
1625N/A bool value() const { return _trigger; }
1625N/A template <class T> void do_oop_nv(T* p) { _trigger = true; }
1625N/A virtual void do_oop(oop* p) { do_oop_nv(p); }
1625N/A virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
1625N/A};
1625N/A
1625N/Aclass InvokeIfNotTriggeredClosure: public OopClosure {
1625N/A TriggerClosure* _t;
1625N/A OopClosure* _oc;
1625N/Apublic:
1625N/A InvokeIfNotTriggeredClosure(TriggerClosure* t, OopClosure* oc):
1625N/A _t(t), _oc(oc) { }
1625N/A template <class T> void do_oop_nv(T* p) {
1625N/A if (!_t->value()) _oc->do_oop(p);
1625N/A }
1625N/A virtual void do_oop(oop* p) { do_oop_nv(p); }
1625N/A virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
1625N/A};
1625N/A
1625N/Aclass Mux2Closure : public OopClosure {
1625N/A OopClosure* _c1;
1625N/A OopClosure* _c2;
1625N/Apublic:
1625N/A Mux2Closure(OopClosure *c1, OopClosure *c2) : _c1(c1), _c2(c2) { }
1625N/A template <class T> void do_oop_nv(T* p) {
1625N/A _c1->do_oop(p); _c2->do_oop(p);
1625N/A }
1625N/A virtual void do_oop(oop* p) { do_oop_nv(p); }
1625N/A virtual void do_oop(narrowOop* p) { do_oop_nv(p); }
1625N/A};
1625N/A
1781N/Abool G1RemSet::concurrentRefineOneCard_impl(jbyte* card_ptr, int worker_i,
1625N/A bool check_for_refs_into_cset) {
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
2069N/A init_ct_freq_table(_g1->max_capacity());
890N/A ct_freq_note_card(_ct_bs->index_for(start));
890N/A#endif
890N/A
1867N/A assert(!check_for_refs_into_cset || _cset_rs_update_cl[worker_i] != NULL, "sanity");
1867N/A UpdateRSOrPushRefOopClosure update_rs_oop_cl(_g1,
1867N/A _g1->g1_rem_set(),
1867N/A _cset_rs_update_cl[worker_i],
1867N/A check_for_refs_into_cset,
1867N/A worker_i);
890N/A update_rs_oop_cl.set_from(r);
1625N/A
1625N/A TriggerClosure trigger_cl;
2819N/A FilterIntoCSClosure into_cs_cl(NULL, _g1, &trigger_cl);
1625N/A InvokeIfNotTriggeredClosure invoke_cl(&trigger_cl, &into_cs_cl);
1625N/A Mux2Closure mux(&invoke_cl, &update_rs_oop_cl);
1625N/A
1625N/A FilterOutOfRegionClosure filter_then_update_rs_oop_cl(r,
1625N/A (check_for_refs_into_cset ?
1625N/A (OopClosure*)&mux :
1625N/A (OopClosure*)&update_rs_oop_cl));
890N/A
1586N/A // The region for the current card may be a young region. The
1586N/A // current card may have been a card that was evicted from the
1586N/A // card cache. When the card was inserted into the cache, we had
1586N/A // determined that its region was non-young. While in the cache,
1586N/A // the region may have been freed during a cleanup pause, reallocated
1586N/A // and tagged as young.
1586N/A //
1586N/A // We wish to filter out cards for such a region but the current
2414N/A // thread, if we're running concurrently, may "see" the young type
1586N/A // change at any time (so an earlier "is_young" check may pass or
1586N/A // fail arbitrarily). We tell the iteration code to perform this
1586N/A // filtering when it has been determined that there has been an actual
1586N/A // allocation in this region and making it safe to check the young type.
1586N/A bool filter_young = true;
1586N/A
890N/A HeapWord* stop_point =
890N/A r->oops_on_card_seq_iterate_careful(dirtyRegion,
1586N/A &filter_then_update_rs_oop_cl,
2414N/A filter_young,
2414N/A card_ptr);
1586N/A
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 }
1625N/A
1625N/A return trigger_cl.value();
890N/A}
890N/A
1781N/Abool G1RemSet::concurrentRefineOneCard(jbyte* card_ptr, int worker_i,
1625N/A bool check_for_refs_into_cset) {
342N/A // If the card is no longer dirty, nothing to do.
1625N/A if (*card_ptr != CardTableModRefBS::dirty_card_val()) {
1625N/A // No need to return that this card contains refs that point
1625N/A // into the collection set.
1625N/A return false;
1625N/A }
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?");
1625N/A // Again no need to return that this card contains refs that
1625N/A // point into the collection set.
1625N/A return false; // 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()) {
1625N/A return false;
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()) {
1625N/A return false;
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
1625N/A
890N/A jbyte* res = card_ptr;
890N/A bool defer = false;
1625N/A
1625N/A // This gets set to true if the card being refined has references
1625N/A // that point into the collection set.
1625N/A bool oops_into_cset = false;
1625N/A
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 {
1586N/A // Checking whether the region we got back from the cache
1586N/A // is young here is inappropriate. The region could have been
1586N/A // freed, reallocated and tagged as young while in the cache.
1586N/A // Hence we could see its young type change at any time.
1586N/A //
1586N/A // Process card pointer we get back from the hot card cache. This
1586N/A // will check whether the region containing the card is young
1586N/A // _after_ checking that the region has been allocated from.
1625N/A oops_into_cset = concurrentRefineOneCard_impl(res, worker_i,
1625N/A false /* check_for_refs_into_cset */);
1625N/A // The above call to concurrentRefineOneCard_impl is only
1625N/A // performed if the hot card cache is enabled. This cache is
1625N/A // disabled during an evacuation pause - which is the only
1625N/A // time when we need know if the card contains references
1625N/A // that point into the collection set. Also when the hot card
1625N/A // cache is enabled, this code is executed by the concurrent
1625N/A // refine threads - rather than the GC worker threads - and
1625N/A // concurrentRefineOneCard_impl will return false.
1625N/A assert(!oops_into_cset, "should not see true here");
890N/A }
342N/A }
342N/A }
342N/A
890N/A if (!defer) {
1625N/A oops_into_cset =
1625N/A concurrentRefineOneCard_impl(card_ptr, worker_i, check_for_refs_into_cset);
1625N/A // We should only be detecting that the card contains references
1625N/A // that point into the collection set if the current thread is
1625N/A // a GC worker thread.
1625N/A assert(!oops_into_cset || SafepointSynchronize::is_at_safepoint(),
1625N/A "invalid result at non safepoint");
342N/A }
1625N/A return oops_into_cset;
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
1781N/Avoid 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
1781N/A HRRSStatsIter blk;
1781N/A g1->heap_region_iterate(&blk);
1781N/A gclog_or_tty->print_cr(" Total heap region rem set sizes = " SIZE_FORMAT "K."
1781N/A " Max = " SIZE_FORMAT "K.",
1781N/A blk.total_mem_sz()/K, blk.max_mem_sz()/K);
1781N/A gclog_or_tty->print_cr(" Static structures = " SIZE_FORMAT "K,"
1781N/A " free_lists = " SIZE_FORMAT "K.",
1781N/A HeapRegionRemSet::static_mem_size()/K,
1781N/A HeapRegionRemSet::fl_mem_size()/K);
1781N/A gclog_or_tty->print_cr(" %d occupied cards represented.",
1781N/A blk.occupied());
1781N/A gclog_or_tty->print_cr(" Max sz region = [" PTR_FORMAT ", " PTR_FORMAT " )"
1781N/A ", cap = " SIZE_FORMAT "K, occ = " SIZE_FORMAT "K.",
1781N/A blk.max_mem_sz_region()->bottom(), blk.max_mem_sz_region()->end(),
1781N/A (blk.max_mem_sz_region()->rem_set()->mem_size() + K - 1)/K,
1781N/A (blk.max_mem_sz_region()->rem_set()->occupied() + K - 1)/K);
1781N/A gclog_or_tty->print_cr(" Did %d coarsenings.", HeapRegionRemSet::n_coarsenings());
342N/A}
1625N/A
1781N/Avoid 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);
1625N/A DirtyCardQueue into_cset_dcq(&_g1->into_cset_dirty_card_queue_set());
1625N/A updateRS(&into_cset_dcq, 0);
1625N/A _g1->into_cset_dirty_card_queue_set().clear();
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}