1 /*
   2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/g1/concurrentG1Refine.hpp"
  27 #include "gc/g1/concurrentG1RefineThread.hpp"
  28 #include "gc/g1/g1BlockOffsetTable.inline.hpp"
  29 #include "gc/g1/g1CollectedHeap.inline.hpp"
  30 #include "gc/g1/g1CollectorPolicy.hpp"
  31 #include "gc/g1/g1GCPhaseTimes.hpp"
  32 #include "gc/g1/g1HotCardCache.hpp"
  33 #include "gc/g1/g1OopClosures.inline.hpp"
  34 #include "gc/g1/g1RemSet.inline.hpp"
  35 #include "gc/g1/heapRegionManager.inline.hpp"
  36 #include "gc/g1/heapRegionRemSet.hpp"
  37 #include "memory/iterator.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "utilities/globalDefinitions.hpp"
  40 #include "utilities/intHisto.hpp"
  41 #include "utilities/stack.inline.hpp"
  42 
  43 #define CARD_REPEAT_HISTO 0
  44 
  45 #if CARD_REPEAT_HISTO
  46 static size_t ct_freq_sz;
  47 static jbyte* ct_freq = NULL;
  48 
  49 void init_ct_freq_table(size_t heap_sz_bytes) {
  50   if (ct_freq == NULL) {
  51     ct_freq_sz = heap_sz_bytes/CardTableModRefBS::card_size;
  52     ct_freq = new jbyte[ct_freq_sz];
  53     for (size_t j = 0; j < ct_freq_sz; j++) ct_freq[j] = 0;
  54   }
  55 }
  56 
  57 void ct_freq_note_card(size_t index) {
  58   assert(0 <= index && index < ct_freq_sz, "Bounds error.");
  59   if (ct_freq[index] < 100) { ct_freq[index]++; }
  60 }
  61 
  62 static IntHistogram card_repeat_count(10, 10);
  63 
  64 void ct_freq_update_histo_and_reset() {
  65   for (size_t j = 0; j < ct_freq_sz; j++) {
  66     card_repeat_count.add_entry(ct_freq[j]);
  67     ct_freq[j] = 0;
  68   }
  69 
  70 }
  71 #endif
  72 
  73 G1RemSet::G1RemSet(G1CollectedHeap* g1, CardTableModRefBS* ct_bs)
  74   : _g1(g1), _conc_refine_cards(0),
  75     _ct_bs(ct_bs), _g1p(_g1->g1_policy()),
  76     _cg1r(g1->concurrent_g1_refine()),
  77     _cset_rs_update_cl(NULL),
  78     _prev_period_summary()
  79 {
  80   _cset_rs_update_cl = NEW_C_HEAP_ARRAY(G1ParPushHeapRSClosure*, n_workers(), mtGC);
  81   for (uint i = 0; i < n_workers(); i++) {
  82     _cset_rs_update_cl[i] = NULL;
  83   }
  84   if (G1SummarizeRSetStats) {
  85     _prev_period_summary.initialize(this);
  86   }
  87 }
  88 
  89 G1RemSet::~G1RemSet() {
  90   for (uint i = 0; i < n_workers(); i++) {
  91     assert(_cset_rs_update_cl[i] == NULL, "it should be");
  92   }
  93   FREE_C_HEAP_ARRAY(G1ParPushHeapRSClosure*, _cset_rs_update_cl);
  94 }
  95 
  96 class ScanRSClosure : public HeapRegionClosure {
  97   size_t _cards_done, _cards;
  98   G1CollectedHeap* _g1h;
  99 
 100   G1ParPushHeapRSClosure* _oc;
 101   CodeBlobClosure* _code_root_cl;
 102 
 103   G1BlockOffsetSharedArray* _bot_shared;
 104   G1SATBCardTableModRefBS *_ct_bs;
 105 
 106   double _strong_code_root_scan_time_sec;
 107   uint   _worker_i;
 108   size_t _block_size;
 109   bool   _try_claimed;
 110 
 111 public:
 112   ScanRSClosure(G1ParPushHeapRSClosure* oc,
 113                 CodeBlobClosure* code_root_cl,
 114                 uint worker_i) :
 115     _oc(oc),
 116     _code_root_cl(code_root_cl),
 117     _strong_code_root_scan_time_sec(0.0),
 118     _cards(0),
 119     _cards_done(0),
 120     _worker_i(worker_i),
 121     _try_claimed(false)
 122   {
 123     _g1h = G1CollectedHeap::heap();
 124     _bot_shared = _g1h->bot_shared();
 125     _ct_bs = _g1h->g1_barrier_set();
 126     _block_size = MAX2<size_t>(G1RSetScanBlockSize, 1);
 127   }
 128 
 129   void set_try_claimed() { _try_claimed = true; }
 130 
 131   void scanCard(size_t index, HeapRegion *r) {
 132     // Stack allocate the DirtyCardToOopClosure instance
 133     HeapRegionDCTOC cl(_g1h, r, _oc,
 134                        CardTableModRefBS::Precise);
 135 
 136     // Set the "from" region in the closure.
 137     _oc->set_region(r);
 138     MemRegion card_region(_bot_shared->address_for_index(index), G1BlockOffsetSharedArray::N_words);
 139     MemRegion pre_gc_allocated(r->bottom(), r->scan_top());
 140     MemRegion mr = pre_gc_allocated.intersection(card_region);
 141     if (!mr.is_empty() && !_ct_bs->is_card_claimed(index)) {
 142       // We make the card as "claimed" lazily (so races are possible
 143       // but they're benign), which reduces the number of duplicate
 144       // scans (the rsets of the regions in the cset can intersect).
 145       _ct_bs->set_card_claimed(index);
 146       _cards_done++;
 147       cl.do_MemRegion(mr);
 148     }
 149   }
 150 
 151   void printCard(HeapRegion* card_region, size_t card_index,
 152                  HeapWord* card_start) {
 153     gclog_or_tty->print_cr("T %u Region [" PTR_FORMAT ", " PTR_FORMAT ") "
 154                            "RS names card " SIZE_FORMAT_HEX ": "
 155                            "[" PTR_FORMAT ", " PTR_FORMAT ")",
 156                            _worker_i,
 157                            p2i(card_region->bottom()), p2i(card_region->end()),
 158                            card_index,
 159                            p2i(card_start), p2i(card_start + G1BlockOffsetSharedArray::N_words));
 160   }
 161 
 162   void scan_strong_code_roots(HeapRegion* r) {
 163     double scan_start = os::elapsedTime();
 164     r->strong_code_roots_do(_code_root_cl);
 165     _strong_code_root_scan_time_sec += (os::elapsedTime() - scan_start);
 166   }
 167 
 168   bool doHeapRegion(HeapRegion* r) {
 169     assert(r->in_collection_set(), "should only be called on elements of CS.");
 170     HeapRegionRemSet* hrrs = r->rem_set();
 171     if (hrrs->iter_is_complete()) return false; // All done.
 172     if (!_try_claimed && !hrrs->claim_iter()) return false;
 173     // If we ever free the collection set concurrently, we should also
 174     // clear the card table concurrently therefore we won't need to
 175     // add regions of the collection set to the dirty cards region.
 176     _g1h->push_dirty_cards_region(r);
 177     // If we didn't return above, then
 178     //   _try_claimed || r->claim_iter()
 179     // is true: either we're supposed to work on claimed-but-not-complete
 180     // regions, or we successfully claimed the region.
 181 
 182     HeapRegionRemSetIterator iter(hrrs);
 183     size_t card_index;
 184 
 185     // We claim cards in block so as to reduce the contention. The block size is determined by
 186     // the G1RSetScanBlockSize parameter.
 187     size_t jump_to_card = hrrs->iter_claimed_next(_block_size);
 188     for (size_t current_card = 0; iter.has_next(card_index); current_card++) {
 189       if (current_card >= jump_to_card + _block_size) {
 190         jump_to_card = hrrs->iter_claimed_next(_block_size);
 191       }
 192       if (current_card < jump_to_card) continue;
 193       HeapWord* card_start = _g1h->bot_shared()->address_for_index(card_index);
 194 #if 0
 195       gclog_or_tty->print("Rem set iteration yielded card [" PTR_FORMAT ", " PTR_FORMAT ").\n",
 196                           card_start, card_start + CardTableModRefBS::card_size_in_words);
 197 #endif
 198 
 199       HeapRegion* card_region = _g1h->heap_region_containing(card_start);
 200       _cards++;
 201 
 202       if (!card_region->is_on_dirty_cards_region_list()) {
 203         _g1h->push_dirty_cards_region(card_region);
 204       }
 205 
 206       // If the card is dirty, then we will scan it during updateRS.
 207       if (!card_region->in_collection_set() &&
 208           !_ct_bs->is_card_dirty(card_index)) {
 209         scanCard(card_index, card_region);
 210       }
 211     }
 212     if (!_try_claimed) {
 213       // Scan the strong code root list attached to the current region
 214       scan_strong_code_roots(r);
 215 
 216       hrrs->set_iter_complete();
 217     }
 218     return false;
 219   }
 220 
 221   double strong_code_root_scan_time_sec() {
 222     return _strong_code_root_scan_time_sec;
 223   }
 224 
 225   size_t cards_done() { return _cards_done;}
 226   size_t cards_looked_up() { return _cards;}
 227 };
 228 
 229 size_t G1RemSet::scanRS(G1ParPushHeapRSClosure* oc,
 230                         CodeBlobClosure* heap_region_codeblobs,
 231                         uint worker_i) {
 232   double rs_time_start = os::elapsedTime();
 233 
 234   HeapRegion *startRegion = _g1->start_cset_region_for_worker(worker_i);
 235 
 236   ScanRSClosure scanRScl(oc, heap_region_codeblobs, worker_i);
 237 
 238   _g1->collection_set_iterate_from(startRegion, &scanRScl);
 239   scanRScl.set_try_claimed();
 240   _g1->collection_set_iterate_from(startRegion, &scanRScl);
 241 
 242   double scan_rs_time_sec = (os::elapsedTime() - rs_time_start)
 243                             - scanRScl.strong_code_root_scan_time_sec();
 244 
 245   _g1p->phase_times()->record_time_secs(G1GCPhaseTimes::ScanRS, worker_i, scan_rs_time_sec);
 246   _g1p->phase_times()->record_time_secs(G1GCPhaseTimes::CodeRoots, worker_i, scanRScl.strong_code_root_scan_time_sec());
 247 
 248   return scanRScl.cards_done();
 249 }
 250 
 251 // Closure used for updating RSets and recording references that
 252 // point into the collection set. Only called during an
 253 // evacuation pause.
 254 
 255 class RefineRecordRefsIntoCSCardTableEntryClosure: public CardTableEntryClosure {
 256   G1RemSet* _g1rs;
 257   DirtyCardQueue* _into_cset_dcq;
 258 public:
 259   RefineRecordRefsIntoCSCardTableEntryClosure(G1CollectedHeap* g1h,
 260                                               DirtyCardQueue* into_cset_dcq) :
 261     _g1rs(g1h->g1_rem_set()), _into_cset_dcq(into_cset_dcq)
 262   {}
 263   bool do_card_ptr(jbyte* card_ptr, uint worker_i) {
 264     // The only time we care about recording cards that
 265     // contain references that point into the collection set
 266     // is during RSet updating within an evacuation pause.
 267     // In this case worker_i should be the id of a GC worker thread.
 268     assert(SafepointSynchronize::is_at_safepoint(), "not during an evacuation pause");
 269     assert(worker_i < ParallelGCThreads, "should be a GC worker");
 270 
 271     if (_g1rs->refine_card(card_ptr, worker_i, true)) {
 272       // 'card_ptr' contains references that point into the collection
 273       // set. We need to record the card in the DCQS
 274       // (G1CollectedHeap::into_cset_dirty_card_queue_set())
 275       // that's used for that purpose.
 276       //
 277       // Enqueue the card
 278       _into_cset_dcq->enqueue(card_ptr);
 279     }
 280     return true;
 281   }
 282 };
 283 
 284 void G1RemSet::updateRS(DirtyCardQueue* into_cset_dcq, uint worker_i) {
 285   G1GCParPhaseTimesTracker x(_g1p->phase_times(), G1GCPhaseTimes::UpdateRS, worker_i);
 286   // Apply the given closure to all remaining log entries.
 287   RefineRecordRefsIntoCSCardTableEntryClosure into_cset_update_rs_cl(_g1, into_cset_dcq);
 288 
 289   _g1->iterate_dirty_card_closure(&into_cset_update_rs_cl, into_cset_dcq, false, worker_i);
 290 }
 291 
 292 void G1RemSet::cleanupHRRS() {
 293   HeapRegionRemSet::cleanup();
 294 }
 295 
 296 size_t G1RemSet::oops_into_collection_set_do(G1ParPushHeapRSClosure* oc,
 297                                              CodeBlobClosure* heap_region_codeblobs,
 298                                              uint worker_i) {
 299 #if CARD_REPEAT_HISTO
 300   ct_freq_update_histo_and_reset();
 301 #endif
 302 
 303   // We cache the value of 'oc' closure into the appropriate slot in the
 304   // _cset_rs_update_cl for this worker
 305   assert(worker_i < n_workers(), "sanity");
 306   _cset_rs_update_cl[worker_i] = oc;
 307 
 308   // A DirtyCardQueue that is used to hold cards containing references
 309   // that point into the collection set. This DCQ is associated with a
 310   // special DirtyCardQueueSet (see g1CollectedHeap.hpp).  Under normal
 311   // circumstances (i.e. the pause successfully completes), these cards
 312   // are just discarded (there's no need to update the RSets of regions
 313   // that were in the collection set - after the pause these regions
 314   // are wholly 'free' of live objects. In the event of an evacuation
 315   // failure the cards/buffers in this queue set are passed to the
 316   // DirtyCardQueueSet that is used to manage RSet updates
 317   DirtyCardQueue into_cset_dcq(&_g1->into_cset_dirty_card_queue_set());
 318 
 319   updateRS(&into_cset_dcq, worker_i);
 320   size_t cards_scanned = scanRS(oc, heap_region_codeblobs, worker_i);
 321 
 322   // We now clear the cached values of _cset_rs_update_cl for this worker
 323   _cset_rs_update_cl[worker_i] = NULL;
 324   return cards_scanned;
 325 }
 326 
 327 void G1RemSet::prepare_for_oops_into_collection_set_do() {
 328   cleanupHRRS();
 329   _g1->set_refine_cte_cl_concurrency(false);
 330   DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
 331   dcqs.concatenate_logs();
 332 }
 333 
 334 void G1RemSet::cleanup_after_oops_into_collection_set_do() {
 335   // Cleanup after copy
 336   _g1->set_refine_cte_cl_concurrency(true);
 337   // Set all cards back to clean.
 338   _g1->cleanUpCardTable();
 339 
 340   DirtyCardQueueSet& into_cset_dcqs = _g1->into_cset_dirty_card_queue_set();
 341   int into_cset_n_buffers = into_cset_dcqs.completed_buffers_num();
 342 
 343   if (_g1->evacuation_failed()) {
 344     double restore_remembered_set_start = os::elapsedTime();
 345 
 346     // Restore remembered sets for the regions pointing into the collection set.
 347     // We just need to transfer the completed buffers from the DirtyCardQueueSet
 348     // used to hold cards that contain references that point into the collection set
 349     // to the DCQS used to hold the deferred RS updates.
 350     _g1->dirty_card_queue_set().merge_bufferlists(&into_cset_dcqs);
 351     _g1->g1_policy()->phase_times()->record_evac_fail_restore_remsets((os::elapsedTime() - restore_remembered_set_start) * 1000.0);
 352   }
 353 
 354   // Free any completed buffers in the DirtyCardQueueSet used to hold cards
 355   // which contain references that point into the collection.
 356   _g1->into_cset_dirty_card_queue_set().clear();
 357   assert(_g1->into_cset_dirty_card_queue_set().completed_buffers_num() == 0,
 358          "all buffers should be freed");
 359   _g1->into_cset_dirty_card_queue_set().clear_n_completed_buffers();
 360 }
 361 
 362 class ScrubRSClosure: public HeapRegionClosure {
 363   G1CollectedHeap* _g1h;
 364   BitMap* _region_bm;
 365   BitMap* _card_bm;
 366   CardTableModRefBS* _ctbs;
 367 public:
 368   ScrubRSClosure(BitMap* region_bm, BitMap* card_bm) :
 369     _g1h(G1CollectedHeap::heap()),
 370     _region_bm(region_bm), _card_bm(card_bm),
 371     _ctbs(_g1h->g1_barrier_set()) {}
 372 
 373   bool doHeapRegion(HeapRegion* r) {
 374     if (!r->is_continues_humongous()) {
 375       r->rem_set()->scrub(_ctbs, _region_bm, _card_bm);
 376     }
 377     return false;
 378   }
 379 };
 380 
 381 void G1RemSet::scrub(BitMap* region_bm, BitMap* card_bm, uint worker_num, HeapRegionClaimer *hrclaimer) {
 382   ScrubRSClosure scrub_cl(region_bm, card_bm);
 383   _g1->heap_region_par_iterate(&scrub_cl, worker_num, hrclaimer);
 384 }
 385 
 386 G1TriggerClosure::G1TriggerClosure() :
 387   _triggered(false) { }
 388 
 389 G1InvokeIfNotTriggeredClosure::G1InvokeIfNotTriggeredClosure(G1TriggerClosure* t_cl,
 390                                                              OopClosure* oop_cl)  :
 391   _trigger_cl(t_cl), _oop_cl(oop_cl) { }
 392 
 393 G1Mux2Closure::G1Mux2Closure(OopClosure *c1, OopClosure *c2) :
 394   _c1(c1), _c2(c2) { }
 395 
 396 G1UpdateRSOrPushRefOopClosure::
 397 G1UpdateRSOrPushRefOopClosure(G1CollectedHeap* g1h,
 398                               G1RemSet* rs,
 399                               G1ParPushHeapRSClosure* push_ref_cl,
 400                               bool record_refs_into_cset,
 401                               uint worker_i) :
 402   _g1(g1h), _g1_rem_set(rs), _from(NULL),
 403   _record_refs_into_cset(record_refs_into_cset),
 404   _push_ref_cl(push_ref_cl), _worker_i(worker_i) { }
 405 
 406 // Returns true if the given card contains references that point
 407 // into the collection set, if we're checking for such references;
 408 // false otherwise.
 409 
 410 bool G1RemSet::refine_card(jbyte* card_ptr, uint worker_i,
 411                            bool check_for_refs_into_cset) {
 412   assert(_g1->is_in_exact(_ct_bs->addr_for(card_ptr)),
 413          "Card at " PTR_FORMAT " index " SIZE_FORMAT " representing heap at " PTR_FORMAT " (%u) must be in committed heap",
 414          p2i(card_ptr),
 415          _ct_bs->index_for(_ct_bs->addr_for(card_ptr)),
 416          p2i(_ct_bs->addr_for(card_ptr)),
 417          _g1->addr_to_region(_ct_bs->addr_for(card_ptr)));
 418 
 419   // If the card is no longer dirty, nothing to do.
 420   if (*card_ptr != CardTableModRefBS::dirty_card_val()) {
 421     // No need to return that this card contains refs that point
 422     // into the collection set.
 423     return false;
 424   }
 425 
 426   // Construct the region representing the card.
 427   HeapWord* start = _ct_bs->addr_for(card_ptr);
 428   // And find the region containing it.
 429   HeapRegion* r = _g1->heap_region_containing(start);
 430 
 431   // Why do we have to check here whether a card is on a young region,
 432   // given that we dirty young regions and, as a result, the
 433   // post-barrier is supposed to filter them out and never to enqueue
 434   // them? When we allocate a new region as the "allocation region" we
 435   // actually dirty its cards after we release the lock, since card
 436   // dirtying while holding the lock was a performance bottleneck. So,
 437   // as a result, it is possible for other threads to actually
 438   // allocate objects in the region (after the acquire the lock)
 439   // before all the cards on the region are dirtied. This is unlikely,
 440   // and it doesn't happen often, but it can happen. So, the extra
 441   // check below filters out those cards.
 442   if (r->is_young()) {
 443     return false;
 444   }
 445 
 446   // While we are processing RSet buffers during the collection, we
 447   // actually don't want to scan any cards on the collection set,
 448   // since we don't want to update remembered sets with entries that
 449   // point into the collection set, given that live objects from the
 450   // collection set are about to move and such entries will be stale
 451   // very soon. This change also deals with a reliability issue which
 452   // involves scanning a card in the collection set and coming across
 453   // an array that was being chunked and looking malformed. Note,
 454   // however, that if evacuation fails, we have to scan any objects
 455   // that were not moved and create any missing entries.
 456   if (r->in_collection_set()) {
 457     return false;
 458   }
 459 
 460   // The result from the hot card cache insert call is either:
 461   //   * pointer to the current card
 462   //     (implying that the current card is not 'hot'),
 463   //   * null
 464   //     (meaning we had inserted the card ptr into the "hot" card cache,
 465   //     which had some headroom),
 466   //   * a pointer to a "hot" card that was evicted from the "hot" cache.
 467   //
 468 
 469   G1HotCardCache* hot_card_cache = _cg1r->hot_card_cache();
 470   if (hot_card_cache->use_cache()) {
 471     assert(!check_for_refs_into_cset, "sanity");
 472     assert(!SafepointSynchronize::is_at_safepoint(), "sanity");
 473 
 474     card_ptr = hot_card_cache->insert(card_ptr);
 475     if (card_ptr == NULL) {
 476       // There was no eviction. Nothing to do.
 477       return false;
 478     }
 479 
 480     start = _ct_bs->addr_for(card_ptr);
 481     r = _g1->heap_region_containing(start);
 482 
 483     // Checking whether the region we got back from the cache
 484     // is young here is inappropriate. The region could have been
 485     // freed, reallocated and tagged as young while in the cache.
 486     // Hence we could see its young type change at any time.
 487   }
 488 
 489   // Don't use addr_for(card_ptr + 1) which can ask for
 490   // a card beyond the heap.  This is not safe without a perm
 491   // gen at the upper end of the heap.
 492   HeapWord* end   = start + CardTableModRefBS::card_size_in_words;
 493   MemRegion dirtyRegion(start, end);
 494 
 495 #if CARD_REPEAT_HISTO
 496   init_ct_freq_table(_g1->max_capacity());
 497   ct_freq_note_card(_ct_bs->index_for(start));
 498 #endif
 499 
 500   G1ParPushHeapRSClosure* oops_in_heap_closure = NULL;
 501   if (check_for_refs_into_cset) {
 502     // ConcurrentG1RefineThreads have worker numbers larger than what
 503     // _cset_rs_update_cl[] is set up to handle. But those threads should
 504     // only be active outside of a collection which means that when they
 505     // reach here they should have check_for_refs_into_cset == false.
 506     assert((size_t)worker_i < n_workers(), "index of worker larger than _cset_rs_update_cl[].length");
 507     oops_in_heap_closure = _cset_rs_update_cl[worker_i];
 508   }
 509   G1UpdateRSOrPushRefOopClosure update_rs_oop_cl(_g1,
 510                                                  _g1->g1_rem_set(),
 511                                                  oops_in_heap_closure,
 512                                                  check_for_refs_into_cset,
 513                                                  worker_i);
 514   update_rs_oop_cl.set_from(r);
 515 
 516   G1TriggerClosure trigger_cl;
 517   FilterIntoCSClosure into_cs_cl(NULL, _g1, &trigger_cl);
 518   G1InvokeIfNotTriggeredClosure invoke_cl(&trigger_cl, &into_cs_cl);
 519   G1Mux2Closure mux(&invoke_cl, &update_rs_oop_cl);
 520 
 521   FilterOutOfRegionClosure filter_then_update_rs_oop_cl(r,
 522                         (check_for_refs_into_cset ?
 523                                 (OopClosure*)&mux :
 524                                 (OopClosure*)&update_rs_oop_cl));
 525 
 526   // The region for the current card may be a young region. The
 527   // current card may have been a card that was evicted from the
 528   // card cache. When the card was inserted into the cache, we had
 529   // determined that its region was non-young. While in the cache,
 530   // the region may have been freed during a cleanup pause, reallocated
 531   // and tagged as young.
 532   //
 533   // We wish to filter out cards for such a region but the current
 534   // thread, if we're running concurrently, may "see" the young type
 535   // change at any time (so an earlier "is_young" check may pass or
 536   // fail arbitrarily). We tell the iteration code to perform this
 537   // filtering when it has been determined that there has been an actual
 538   // allocation in this region and making it safe to check the young type.
 539   bool filter_young = true;
 540 
 541   HeapWord* stop_point =
 542     r->oops_on_card_seq_iterate_careful(dirtyRegion,
 543                                         &filter_then_update_rs_oop_cl,
 544                                         filter_young,
 545                                         card_ptr);
 546 
 547   // If stop_point is non-null, then we encountered an unallocated region
 548   // (perhaps the unfilled portion of a TLAB.)  For now, we'll dirty the
 549   // card and re-enqueue: if we put off the card until a GC pause, then the
 550   // unallocated portion will be filled in.  Alternatively, we might try
 551   // the full complexity of the technique used in "regular" precleaning.
 552   if (stop_point != NULL) {
 553     // The card might have gotten re-dirtied and re-enqueued while we
 554     // worked.  (In fact, it's pretty likely.)
 555     if (*card_ptr != CardTableModRefBS::dirty_card_val()) {
 556       *card_ptr = CardTableModRefBS::dirty_card_val();
 557       MutexLockerEx x(Shared_DirtyCardQ_lock,
 558                       Mutex::_no_safepoint_check_flag);
 559       DirtyCardQueue* sdcq =
 560         JavaThread::dirty_card_queue_set().shared_dirty_card_queue();
 561       sdcq->enqueue(card_ptr);
 562     }
 563   } else {
 564     _conc_refine_cards++;
 565   }
 566 
 567   // This gets set to true if the card being refined has
 568   // references that point into the collection set.
 569   bool has_refs_into_cset = trigger_cl.triggered();
 570 
 571   // We should only be detecting that the card contains references
 572   // that point into the collection set if the current thread is
 573   // a GC worker thread.
 574   assert(!has_refs_into_cset || SafepointSynchronize::is_at_safepoint(),
 575            "invalid result at non safepoint");
 576 
 577   return has_refs_into_cset;
 578 }
 579 
 580 void G1RemSet::print_periodic_summary_info(const char* header) {
 581   G1RemSetSummary current;
 582   current.initialize(this);
 583 
 584   _prev_period_summary.subtract_from(&current);
 585   print_summary_info(&_prev_period_summary, header);
 586 
 587   _prev_period_summary.set(&current);
 588 }
 589 
 590 void G1RemSet::print_summary_info() {
 591   G1RemSetSummary current;
 592   current.initialize(this);
 593 
 594   print_summary_info(&current, " Cumulative RS summary");
 595 }
 596 
 597 void G1RemSet::print_summary_info(G1RemSetSummary * summary, const char * header) {
 598   assert(summary != NULL, "just checking");
 599 
 600   if (header != NULL) {
 601     gclog_or_tty->print_cr("%s", header);
 602   }
 603 
 604 #if CARD_REPEAT_HISTO
 605   gclog_or_tty->print_cr("\nG1 card_repeat count histogram: ");
 606   gclog_or_tty->print_cr("  # of repeats --> # of cards with that number.");
 607   card_repeat_count.print_on(gclog_or_tty);
 608 #endif
 609 
 610   summary->print_on(gclog_or_tty);
 611 }
 612 
 613 void G1RemSet::prepare_for_verify() {
 614   if (G1HRRSFlushLogBuffersOnVerify &&
 615       (VerifyBeforeGC || VerifyAfterGC)
 616       &&  (!_g1->collector_state()->full_collection() || G1VerifyRSetsDuringFullGC)) {
 617     cleanupHRRS();
 618     _g1->set_refine_cte_cl_concurrency(false);
 619     if (SafepointSynchronize::is_at_safepoint()) {
 620       DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
 621       dcqs.concatenate_logs();
 622     }
 623 
 624     G1HotCardCache* hot_card_cache = _cg1r->hot_card_cache();
 625     bool use_hot_card_cache = hot_card_cache->use_cache();
 626     hot_card_cache->set_use_cache(false);
 627 
 628     DirtyCardQueue into_cset_dcq(&_g1->into_cset_dirty_card_queue_set());
 629     updateRS(&into_cset_dcq, 0);
 630     _g1->into_cset_dirty_card_queue_set().clear();
 631 
 632     hot_card_cache->set_use_cache(use_hot_card_cache);
 633     assert(JavaThread::dirty_card_queue_set().completed_buffers_num() == 0, "All should be consumed");
 634   }
 635 }