342N/A/*
4539N/A * Copyright (c) 2001, 2013, 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"
4539N/A#include "gc_implementation/g1/g1HotCardCache.hpp"
3885N/A#include "gc_implementation/g1/g1GCPhaseTimes.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");
3863N/A _cset_rs_update_cl = NEW_C_HEAP_ARRAY(OopsInHeapRegionClosure*, n_workers(), mtGC);
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 }
3863N/A FREE_C_HEAP_ARRAY(OopsInHeapRegionClosure*, _cset_rs_update_cl, mtGC);
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) {
2861N/A // Stack allocate the DirtyCardToOopClosure instance
2861N/A HeapRegionDCTOC cl(_g1h, r, _oc,
2861N/A CardTableModRefBS::Precise,
2861N/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++;
2861N/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
1781N/Avoid G1RemSet::scanRS(OopsInHeapRegionClosure* oc, int worker_i) {
342N/A double rs_time_start = os::elapsedTime();
2943N/A HeapRegion *startRegion = _g1->start_cset_region_for_worker(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
3885N/A _g1p->phase_times()->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
4539N/A if (_g1rs->refine_card(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
3885N/A _g1p->phase_times()->record_update_rs_time(worker_i, (os::elapsedTime() - start) * 1000.0);
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
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 {
3978N/A _g1p->phase_times()->record_update_rs_processed_buffers(worker_i, 0);
3885N/A _g1p->phase_times()->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 {
3885N/A _g1p->phase_times()->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
2941N/A if (G1CollectedHeap::use_parallel_gc_threads()) {
2941N/A // Don't set the number of workers here. It will be set
2941N/A // when the task is run
2941N/A // _seq_task->set_n_termination((int)n_workers());
342N/A }
342N/A guarantee( _cards_scanned == NULL, "invariant" );
3863N/A _cards_scanned = NEW_C_HEAP_ARRAY(size_t, n_workers(), mtGC);
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
4539N/A // Don't use addr_for(card_ptr + 1) which can ask for
4539N/A // a card beyond the heap. This is not safe without a perm
4539N/A // gen.
4539N/A HeapWord* end = start + CardTableModRefBS::card_size_in_words;
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
4539N/A // the comment in G1RemSet::refine_card() 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 }
3863N/A FREE_C_HEAP_ARRAY(size_t, _cards_scanned, mtGC);
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,
3008N/A uint worker_num, int claim_val) {
342N/A ScrubRSClosure scrub_cl(region_bm, card_bm);
2941N/A _g1->heap_region_par_iterate_chunked(&scrub_cl,
2941N/A worker_num,
3008N/A n_workers(),
2941N/A claim_val);
342N/A}
342N/A
3122N/AG1TriggerClosure::G1TriggerClosure() :
3122N/A _triggered(false) { }
3122N/A
3122N/AG1InvokeIfNotTriggeredClosure::G1InvokeIfNotTriggeredClosure(G1TriggerClosure* t_cl,
3122N/A OopClosure* oop_cl) :
3122N/A _trigger_cl(t_cl), _oop_cl(oop_cl) { }
1625N/A
3122N/AG1Mux2Closure::G1Mux2Closure(OopClosure *c1, OopClosure *c2) :
3122N/A _c1(c1), _c2(c2) { }
1625N/A
3122N/AG1UpdateRSOrPushRefOopClosure::
3122N/AG1UpdateRSOrPushRefOopClosure(G1CollectedHeap* g1h,
3122N/A G1RemSet* rs,
3122N/A OopsInHeapRegionClosure* push_ref_cl,
3122N/A bool record_refs_into_cset,
3122N/A int worker_i) :
3122N/A _g1(g1h), _g1_rem_set(rs), _from(NULL),
3122N/A _record_refs_into_cset(record_refs_into_cset),
3122N/A _push_ref_cl(push_ref_cl), _worker_i(worker_i) { }
1625N/A
4539N/A// Returns true if the given card contains references that point
4539N/A// into the collection set, if we're checking for such references;
4539N/A// false otherwise.
4539N/A
4539N/Abool G1RemSet::refine_card(jbyte* card_ptr, int worker_i,
4539N/A bool check_for_refs_into_cset) {
4539N/A
4539N/A // If the card is no longer dirty, nothing to do.
4539N/A if (*card_ptr != CardTableModRefBS::dirty_card_val()) {
4539N/A // No need to return that this card contains refs that point
4539N/A // into the collection set.
4539N/A return false;
4539N/A }
4539N/A
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);
4539N/A if (r == NULL) {
4539N/A guarantee(_g1->is_in_permanent(start), "Or else where?");
4539N/A // Again no need to return that this card contains refs that
4539N/A // point into the collection set.
4539N/A return false; // Not in the G1 heap (might be in perm, for example.)
4539N/A }
4539N/A
4539N/A // Why do we have to check here whether a card is on a young region,
4539N/A // given that we dirty young regions and, as a result, the
4539N/A // post-barrier is supposed to filter them out and never to enqueue
4539N/A // them? When we allocate a new region as the "allocation region" we
4539N/A // actually dirty its cards after we release the lock, since card
4539N/A // dirtying while holding the lock was a performance bottleneck. So,
4539N/A // as a result, it is possible for other threads to actually
4539N/A // allocate objects in the region (after the acquire the lock)
4539N/A // before all the cards on the region are dirtied. This is unlikely,
4539N/A // and it doesn't happen often, but it can happen. So, the extra
4539N/A // check below filters out those cards.
4539N/A if (r->is_young()) {
4539N/A return false;
4539N/A }
4539N/A
4539N/A // While we are processing RSet buffers during the collection, we
4539N/A // actually don't want to scan any cards on the collection set,
4539N/A // since we don't want to update remebered sets with entries that
4539N/A // point into the collection set, given that live objects from the
4539N/A // collection set are about to move and such entries will be stale
4539N/A // very soon. This change also deals with a reliability issue which
4539N/A // involves scanning a card in the collection set and coming across
4539N/A // an array that was being chunked and looking malformed. Note,
4539N/A // however, that if evacuation fails, we have to scan any objects
4539N/A // that were not moved and create any missing entries.
4539N/A if (r->in_collection_set()) {
4539N/A return false;
4539N/A }
890N/A
4539N/A // The result from the hot card cache insert call is either:
4539N/A // * pointer to the current card
4539N/A // (implying that the current card is not 'hot'),
4539N/A // * null
4539N/A // (meaning we had inserted the card ptr into the "hot" card cache,
4539N/A // which had some headroom),
4539N/A // * a pointer to a "hot" card that was evicted from the "hot" cache.
4539N/A //
4539N/A
4539N/A G1HotCardCache* hot_card_cache = _cg1r->hot_card_cache();
4539N/A if (hot_card_cache->use_cache()) {
4539N/A assert(!check_for_refs_into_cset, "sanity");
4539N/A assert(!SafepointSynchronize::is_at_safepoint(), "sanity");
4539N/A
4539N/A card_ptr = hot_card_cache->insert(card_ptr);
4539N/A if (card_ptr == NULL) {
4539N/A // There was no eviction. Nothing to do.
4539N/A return false;
4539N/A }
4539N/A
4539N/A start = _ct_bs->addr_for(card_ptr);
4539N/A r = _g1->heap_region_containing(start);
4539N/A if (r == NULL) {
4539N/A guarantee(_g1->is_in_permanent(start), "Or else where?");
4539N/A // Not in the G1 heap
4539N/A return false;
4539N/A }
4539N/A
4539N/A // Checking whether the region we got back from the cache
4539N/A // is young here is inappropriate. The region could have been
4539N/A // freed, reallocated and tagged as young while in the cache.
4539N/A // Hence we could see its young type change at any time.
4539N/A }
4539N/A
4539N/A // Don't use addr_for(card_ptr + 1) which can ask for
4539N/A // a card beyond the heap. This is not safe without a perm
4539N/A // gen at the upper end of the heap.
4539N/A HeapWord* end = start + CardTableModRefBS::card_size_in_words;
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
2909N/A OopsInHeapRegionClosure* oops_in_heap_closure = NULL;
2909N/A if (check_for_refs_into_cset) {
2909N/A // ConcurrentG1RefineThreads have worker numbers larger than what
2909N/A // _cset_rs_update_cl[] is set up to handle. But those threads should
2909N/A // only be active outside of a collection which means that when they
2909N/A // reach here they should have check_for_refs_into_cset == false.
2909N/A assert((size_t)worker_i < n_workers(), "index of worker larger than _cset_rs_update_cl[].length");
2909N/A oops_in_heap_closure = _cset_rs_update_cl[worker_i];
2909N/A }
3122N/A G1UpdateRSOrPushRefOopClosure update_rs_oop_cl(_g1,
3122N/A _g1->g1_rem_set(),
3122N/A oops_in_heap_closure,
3122N/A check_for_refs_into_cset,
3122N/A worker_i);
890N/A update_rs_oop_cl.set_from(r);
1625N/A
3122N/A G1TriggerClosure trigger_cl;
2819N/A FilterIntoCSClosure into_cs_cl(NULL, _g1, &trigger_cl);
3122N/A G1InvokeIfNotTriggeredClosure invoke_cl(&trigger_cl, &into_cs_cl);
3122N/A G1Mux2Closure 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 _conc_refine_cards++;
890N/A }
1625N/A
4539N/A // This gets set to true if the card being refined has
4539N/A // references that point into the collection set.
4539N/A bool has_refs_into_cset = trigger_cl.triggered();
342N/A
4539N/A // We should only be detecting that the card contains references
4539N/A // that point into the collection set if the current thread is
4539N/A // a GC worker thread.
4539N/A assert(!has_refs_into_cset || SafepointSynchronize::is_at_safepoint(),
4539N/A "invalid result at non safepoint");
1625N/A
4539N/A return has_refs_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
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);
3920N/A gclog_or_tty->print_cr(" Total heap region rem set sizes = "SIZE_FORMAT"K."
3920N/A " Max = "SIZE_FORMAT"K.",
1781N/A blk.total_mem_sz()/K, blk.max_mem_sz()/K);
3920N/A gclog_or_tty->print_cr(" Static structures = "SIZE_FORMAT"K,"
3920N/A " free_lists = "SIZE_FORMAT"K.",
3920N/A HeapRegionRemSet::static_mem_size() / K,
3920N/A HeapRegionRemSet::fl_mem_size() / K);
3920N/A gclog_or_tty->print_cr(" "SIZE_FORMAT" occupied cards represented.",
1781N/A blk.occupied());
3920N/A HeapRegion* max_mem_sz_region = blk.max_mem_sz_region();
3920N/A HeapRegionRemSet* rem_set = max_mem_sz_region->rem_set();
3920N/A gclog_or_tty->print_cr(" Max size region = "HR_FORMAT", "
3920N/A "size = "SIZE_FORMAT "K, occupied = "SIZE_FORMAT"K.",
3920N/A HR_FORMAT_PARAMS(max_mem_sz_region),
3920N/A (rem_set->mem_size() + K - 1)/K,
3920N/A (rem_set->occupied() + K - 1)/K);
3920N/A gclog_or_tty->print_cr(" Did %d coarsenings.",
3920N/A 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 }
4539N/A
4539N/A G1HotCardCache* hot_card_cache = _cg1r->hot_card_cache();
4539N/A bool use_hot_card_cache = hot_card_cache->use_cache();
4539N/A hot_card_cache->set_use_cache(false);
4539N/A
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();
637N/A
4539N/A hot_card_cache->set_use_cache(use_hot_card_cache);
637N/A assert(JavaThread::dirty_card_queue_set().completed_buffers_num() == 0, "All should be consumed");
342N/A }
342N/A}