Print this page
rev 2691 : [mq]: g1-reference-processing

Split Close
Expand all
Collapse all
          --- old/src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp
          +++ new/src/share/vm/gc_implementation/g1/g1CollectedHeap.cpp
↓ open down ↓ 34 lines elided ↑ open up ↑
  35   35  #include "gc_implementation/g1/g1MarkSweep.hpp"
  36   36  #include "gc_implementation/g1/g1OopClosures.inline.hpp"
  37   37  #include "gc_implementation/g1/g1RemSet.inline.hpp"
  38   38  #include "gc_implementation/g1/heapRegionRemSet.hpp"
  39   39  #include "gc_implementation/g1/heapRegionSeq.inline.hpp"
  40   40  #include "gc_implementation/g1/vm_operations_g1.hpp"
  41   41  #include "gc_implementation/shared/isGCActiveMark.hpp"
  42   42  #include "memory/gcLocker.inline.hpp"
  43   43  #include "memory/genOopClosures.inline.hpp"
  44   44  #include "memory/generationSpec.hpp"
       45 +#include "memory/referenceProcessor.hpp"
  45   46  #include "oops/oop.inline.hpp"
  46   47  #include "oops/oop.pcgc.inline.hpp"
  47   48  #include "runtime/aprofiler.hpp"
  48   49  #include "runtime/vmThread.hpp"
  49   50  
  50   51  size_t G1CollectedHeap::_humongous_object_threshold_in_words = 0;
  51   52  
  52   53  // turn it on so that the contents of the young list (scan-only /
  53   54  // to-be-collected) are printed at "strategic" points before / during
  54   55  // / after the collection --- this is useful for debugging
↓ open down ↓ 1182 lines elided ↑ open up ↑
1237 1238        prepare_for_verify();
1238 1239        Universe::verify(/* allow dirty */ true,
1239 1240                         /* silent      */ false,
1240 1241                         /* option      */ VerifyOption_G1UsePrevMarking);
1241 1242  
1242 1243      }
1243 1244      pre_full_gc_dump();
1244 1245  
1245 1246      COMPILER2_PRESENT(DerivedPointerTable::clear());
1246 1247  
1247      -    // We want to discover references, but not process them yet.
1248      -    // This mode is disabled in
1249      -    // instanceRefKlass::process_discovered_references if the
1250      -    // generation does some collection work, or
1251      -    // instanceRefKlass::enqueue_discovered_references if the
1252      -    // generation returns without doing any work.
1253      -    ref_processor()->disable_discovery();
1254      -    ref_processor()->abandon_partial_discovery();
1255      -    ref_processor()->verify_no_references_recorded();
     1248 +    // Disable discovery and empty the discovered lists
     1249 +    // for the CM ref processor.
     1250 +    ref_processor_cm()->disable_discovery();
     1251 +    ref_processor_cm()->abandon_partial_discovery();
     1252 +    ref_processor_cm()->verify_no_references_recorded();
1256 1253  
1257 1254      // Abandon current iterations of concurrent marking and concurrent
1258 1255      // refinement, if any are in progress.
1259 1256      concurrent_mark()->abort();
1260 1257  
1261 1258      // Make sure we'll choose a new allocation region afterwards.
1262 1259      release_mutator_alloc_region();
1263 1260      abandon_gc_alloc_regions();
1264 1261      g1_rem_set()->cleanupHRRS();
1265 1262      tear_down_region_lists();
↓ open down ↓ 7 lines elided ↑ open up ↑
1273 1270      // set between the last GC or pause and now. We need to clear the
1274 1271      // incremental collection set and then start rebuilding it afresh
1275 1272      // after this full GC.
1276 1273      abandon_collection_set(g1_policy()->inc_cset_head());
1277 1274      g1_policy()->clear_incremental_cset();
1278 1275      g1_policy()->stop_incremental_cset_building();
1279 1276  
1280 1277      empty_young_list();
1281 1278      g1_policy()->set_full_young_gcs(true);
1282 1279  
1283      -    // See the comment in G1CollectedHeap::ref_processing_init() about
     1280 +    // See the comments in g1CollectedHeap.hpp and
     1281 +    // G1CollectedHeap::ref_processing_init() about
1284 1282      // how reference processing currently works in G1.
1285 1283  
1286      -    // Temporarily make reference _discovery_ single threaded (non-MT).
1287      -    ReferenceProcessorMTDiscoveryMutator rp_disc_ser(ref_processor(), false);
     1284 +    // Temporarily make discovery by the STW ref processor single threaded (non-MT).
     1285 +    ReferenceProcessorMTDiscoveryMutator stw_rp_disc_ser(ref_processor_stw(), false);
1288 1286  
1289      -    // Temporarily make refs discovery atomic
1290      -    ReferenceProcessorAtomicMutator rp_disc_atomic(ref_processor(), true);
     1287 +    // Temporarily clear the STW ref processor's _is_alive_non_header field.
     1288 +    ReferenceProcessorIsAliveMutator stw_rp_is_alive_null(ref_processor_stw(), NULL);
1291 1289  
1292      -    // Temporarily clear _is_alive_non_header
1293      -    ReferenceProcessorIsAliveMutator rp_is_alive_null(ref_processor(), NULL);
     1290 +    ref_processor_stw()->enable_discovery(true /*verify_disabled*/, true /*verify_no_refs*/);
     1291 +    ref_processor_stw()->setup_policy(do_clear_all_soft_refs);
1294 1292  
1295      -    ref_processor()->enable_discovery();
1296      -    ref_processor()->setup_policy(do_clear_all_soft_refs);
1297 1293      // Do collection work
1298 1294      {
1299 1295        HandleMark hm;  // Discard invalid handles created during gc
1300      -      G1MarkSweep::invoke_at_safepoint(ref_processor(), do_clear_all_soft_refs);
     1296 +      G1MarkSweep::invoke_at_safepoint(ref_processor_stw(), do_clear_all_soft_refs);
1301 1297      }
     1298 +
1302 1299      assert(free_regions() == 0, "we should not have added any free regions");
1303 1300      rebuild_region_lists();
1304 1301  
1305 1302      _summary_bytes_used = recalculate_used();
1306 1303  
1307      -    ref_processor()->enqueue_discovered_references();
     1304 +    // Enqueue any discovered reference objects that have
     1305 +    // not been removed from the discovered lists.
     1306 +    ref_processor_stw()->enqueue_discovered_references();
1308 1307  
1309 1308      COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
1310 1309  
1311 1310      MemoryService::track_memory_usage();
1312 1311  
1313 1312      if (VerifyAfterGC && total_collections() >= VerifyGCStartAt) {
1314 1313        HandleMark hm;  // Discard invalid handles created during verification
1315 1314        gclog_or_tty->print(" VerifyAfterGC:");
1316 1315        prepare_for_verify();
1317 1316        Universe::verify(/* allow dirty */ false,
1318 1317                         /* silent      */ false,
1319 1318                         /* option      */ VerifyOption_G1UsePrevMarking);
1320 1319  
1321 1320      }
1322      -    NOT_PRODUCT(ref_processor()->verify_no_references_recorded());
     1321 +
     1322 +    assert(!ref_processor_stw()->discovery_enabled(), "Postcondition");
     1323 +    ref_processor_stw()->verify_no_references_recorded();
     1324 +
     1325 +    // Note: since we've just done a full GC, concurrent
     1326 +    // marking is no longer active. Therefore we need not
     1327 +    // re-enable reference discovery for the CM ref processor.
     1328 +    // That will be done at the start of the next marking cycle.
     1329 +    assert(!ref_processor_cm()->discovery_enabled(), "Postcondition");
     1330 +    ref_processor_cm()->verify_no_references_recorded();
1323 1331  
1324 1332      reset_gc_time_stamp();
1325 1333      // Since everything potentially moved, we will clear all remembered
1326 1334      // sets, and clear all cards.  Later we will rebuild remebered
1327 1335      // sets. We will also reset the GC time stamps of the regions.
1328 1336      PostMCRemSetClearClosure rs_clear(mr_bs());
1329 1337      heap_region_iterate(&rs_clear);
1330 1338  
1331 1339      // Resize the heap if necessary.
1332 1340      resize_if_necessary_after_full_collection(explicit_gc ? 0 : word_size);
↓ open down ↓ 432 lines elided ↑ open up ↑
1765 1773  #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
1766 1774  #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
1767 1775  #endif // _MSC_VER
1768 1776  
1769 1777  
1770 1778  G1CollectedHeap::G1CollectedHeap(G1CollectorPolicy* policy_) :
1771 1779    SharedHeap(policy_),
1772 1780    _g1_policy(policy_),
1773 1781    _dirty_card_queue_set(false),
1774 1782    _into_cset_dirty_card_queue_set(false),
1775      -  _is_alive_closure(this),
1776      -  _ref_processor(NULL),
     1783 +  _is_alive_closure_cm(this),
     1784 +  _is_alive_closure_stw(this),
     1785 +  _ref_processor_cm(NULL),
     1786 +  _ref_processor_stw(NULL),
1777 1787    _process_strong_tasks(new SubTasksDone(G1H_PS_NumElements)),
1778 1788    _bot_shared(NULL),
1779 1789    _objs_with_preserved_marks(NULL), _preserved_marks_of_objs(NULL),
1780 1790    _evac_failure_scan_stack(NULL) ,
1781 1791    _mark_in_progress(false),
1782 1792    _cg1r(NULL), _summary_bytes_used(0),
1783 1793    _refine_cte_cl(NULL),
1784 1794    _full_collection(false),
1785 1795    _free_list("Master Free List"),
1786 1796    _secondary_free_list("Secondary Free List"),
↓ open down ↓ 273 lines elided ↑ open up ↑
2060 2070    // Do create of the monitoring and management support so that
2061 2071    // values in the heap have been properly initialized.
2062 2072    _g1mm = new G1MonitoringSupport(this, &_g1_storage);
2063 2073  
2064 2074    return JNI_OK;
2065 2075  }
2066 2076  
2067 2077  void G1CollectedHeap::ref_processing_init() {
2068 2078    // Reference processing in G1 currently works as follows:
2069 2079    //
2070      -  // * There is only one reference processor instance that
2071      -  //   'spans' the entire heap. It is created by the code
2072      -  //   below.
2073      -  // * Reference discovery is not enabled during an incremental
2074      -  //   pause (see 6484982).
2075      -  // * Discoverered refs are not enqueued nor are they processed
2076      -  //   during an incremental pause (see 6484982).
2077      -  // * Reference discovery is enabled at initial marking.
2078      -  // * Reference discovery is disabled and the discovered
2079      -  //   references processed etc during remarking.
2080      -  // * Reference discovery is MT (see below).
2081      -  // * Reference discovery requires a barrier (see below).
2082      -  // * Reference processing is currently not MT (see 6608385).
2083      -  // * A full GC enables (non-MT) reference discovery and
2084      -  //   processes any discovered references.
     2080 +  // * There are two reference processor instances. One is
     2081 +  //   used to record and process discovered references
     2082 +  //   during concurrent marking; the other is used to
     2083 +  //   record and process references during STW pauses
     2084 +  //   (both full and incremental).
     2085 +  // * Both ref processors need to 'span' the entire heap as
     2086 +  //   the regions in the collection set may be dotted around.
     2087 +  //
     2088 +  // * For the concurrent marking ref processor:
     2089 +  //   * Reference discovery is enabled at initial marking.
     2090 +  //   * Reference discovery is disabled and the discovered
     2091 +  //     references processed etc during remarking.
     2092 +  //   * Reference discovery is MT (see below).
     2093 +  //   * Reference discovery requires a barrier (see below).
     2094 +  //   * Reference processing may or may not be MT
     2095 +  //     (depending on the value of ParallelRefProcEnabled
     2096 +  //     and ParallelGCThreads).
     2097 +  //   * A full GC disables reference discovery by the CM
     2098 +  //     ref processor and abandons any entries on it's
     2099 +  //     discovered lists.
     2100 +  //
     2101 +  // * For the STW processor:
     2102 +  //   * Non MT discovery is enabled at the start of a full GC.
     2103 +  //   * Processing and enqueueing during a full GC is non-MT.
     2104 +  //   * During a full GC, references are processed after marking.
     2105 +  //
     2106 +  //   * Discovery (may or may not be MT) is enabled at the start
     2107 +  //     of an incremental evacuation pause.
     2108 +  //   * References are processed near the end of a STW evacuation pause.
     2109 +  //   * For both types of GC:
     2110 +  //     * Discovery is atomic - i.e. not concurrent.
     2111 +  //     * Reference discovery will not need a barrier.
2085 2112  
2086 2113    SharedHeap::ref_processing_init();
2087 2114    MemRegion mr = reserved_region();
2088      -  _ref_processor =
     2115 +
     2116 +  // Concurrent Mark ref processor
     2117 +  _ref_processor_cm =
     2118 +    new ReferenceProcessor(mr,    // span
     2119 +                           ParallelRefProcEnabled && (ParallelGCThreads > 1),
     2120 +                                // mt processing
     2121 +                           (int) ParallelGCThreads,
     2122 +                                // degree of mt processing
     2123 +                           (ParallelGCThreads > 1) || (ConcGCThreads > 1),
     2124 +                                // mt discovery
     2125 +                           (int) MAX2(ParallelGCThreads, ConcGCThreads),
     2126 +                                // degree of mt discovery
     2127 +                           false,
     2128 +                                // Reference discovery is not atomic
     2129 +                           &_is_alive_closure_cm,
     2130 +                                // is alive closure
     2131 +                                // (for efficiency/performance)
     2132 +                           true);
     2133 +                                // Setting next fields of discovered
     2134 +                                // lists requires a barrier.
     2135 +
     2136 +  // STW ref processor
     2137 +  _ref_processor_stw =
2089 2138      new ReferenceProcessor(mr,    // span
2090      -                           ParallelRefProcEnabled && (ParallelGCThreads > 1),    // mt processing
2091      -                           (int) ParallelGCThreads,   // degree of mt processing
2092      -                           ParallelGCThreads > 1 || ConcGCThreads > 1,  // mt discovery
2093      -                           (int) MAX2(ParallelGCThreads, ConcGCThreads), // degree of mt discovery
2094      -                           false,                     // Reference discovery is not atomic
2095      -                           &_is_alive_closure,        // is alive closure for efficiency
2096      -                           true);                     // Setting next fields of discovered
2097      -                                                      // lists requires a barrier.
     2139 +                           ParallelRefProcEnabled && (ParallelGCThreads > 1),
     2140 +                                // mt processing
     2141 +                           MAX2((int)ParallelGCThreads, 1),
     2142 +                                // degree of mt processing
     2143 +                           (ParallelGCThreads > 1),
     2144 +                                // mt discovery
     2145 +                           MAX2((int)ParallelGCThreads, 1),
     2146 +                                // degree of mt discovery
     2147 +                           true,
     2148 +                                // Reference discovery is atomic
     2149 +                           &_is_alive_closure_stw,
     2150 +                                // is alive closure
     2151 +                                // (for efficiency/performance)
     2152 +                           false);
     2153 +                                // Setting next fields of discovered
     2154 +                                // lists requires a barrier.
2098 2155  }
2099 2156  
2100 2157  size_t G1CollectedHeap::capacity() const {
2101 2158    return _g1_committed.byte_size();
2102 2159  }
2103 2160  
2104 2161  void G1CollectedHeap::iterate_dirty_card_closure(CardTableEntryClosure* cl,
2105 2162                                                   DirtyCardQueue* into_cset_dcq,
2106 2163                                                   bool concurrent,
2107 2164                                                   int worker_i) {
↓ open down ↓ 1002 lines elided ↑ open up ↑
3110 3167    ensure_parsability(true);
3111 3168  }
3112 3169  
3113 3170  void G1CollectedHeap::gc_epilogue(bool full /* Ignored */) {
3114 3171    // FIXME: what is this about?
3115 3172    // I'm ignoring the "fill_newgen()" call if "alloc_event_enabled"
3116 3173    // is set.
3117 3174    COMPILER2_PRESENT(assert(DerivedPointerTable::is_empty(),
3118 3175                          "derived pointer present"));
3119 3176    // always_do_update_barrier = true;
     3177 +
     3178 +  // We have just completed a GC. Update the soft reference
     3179 +  // policy with the new heap occupancy
     3180 +  Universe::update_heap_info_at_gc();
3120 3181  }
3121 3182  
3122 3183  HeapWord* G1CollectedHeap::do_collection_pause(size_t word_size,
3123 3184                                                 unsigned int gc_count_before,
3124 3185                                                 bool* succeeded) {
3125 3186    assert_heap_not_locked_and_not_at_safepoint();
3126 3187    g1_policy()->record_stop_world_start();
3127 3188    VM_G1IncCollectionPause op(gc_count_before,
3128 3189                               word_size,
3129 3190                               false, /* should_initiate_conc_mark */
↓ open down ↓ 217 lines elided ↑ open up ↑
3347 3408          gclog_or_tty->print(" VerifyBeforeGC:");
3348 3409          prepare_for_verify();
3349 3410          Universe::verify(/* allow dirty */ false,
3350 3411                           /* silent      */ false,
3351 3412                           /* option      */ VerifyOption_G1UsePrevMarking);
3352 3413  
3353 3414        }
3354 3415  
3355 3416        COMPILER2_PRESENT(DerivedPointerTable::clear());
3356 3417  
3357      -      // Please see comment in G1CollectedHeap::ref_processing_init()
3358      -      // to see how reference processing currently works in G1.
3359      -      //
3360      -      // We want to turn off ref discovery, if necessary, and turn it back on
3361      -      // on again later if we do. XXX Dubious: why is discovery disabled?
3362      -      bool was_enabled = ref_processor()->discovery_enabled();
3363      -      if (was_enabled) ref_processor()->disable_discovery();
3364      -
3365      -      // Forget the current alloc region (we might even choose it to be part
3366      -      // of the collection set!).
3367      -      release_mutator_alloc_region();
3368      -
3369      -      // We should call this after we retire the mutator alloc
3370      -      // region(s) so that all the ALLOC / RETIRE events are generated
3371      -      // before the start GC event.
3372      -      _hr_printer.start_gc(false /* full */, (size_t) total_collections());
3373      -
3374      -      // The elapsed time induced by the start time below deliberately elides
3375      -      // the possible verification above.
3376      -      double start_time_sec = os::elapsedTime();
3377      -      size_t start_used_bytes = used();
     3418 +      // Please see comment in g1CollectedHeap.hpp and
     3419 +      // G1CollectedHeap::ref_processing_init() to see how
     3420 +      // reference processing currently works in G1.
     3421 +
     3422 +      // Enable discovery in the STW reference processor
     3423 +      ref_processor_stw()->enable_discovery(true /*verify_disabled*/,
     3424 +                                            true /*verify_no_refs*/);
     3425 +
     3426 +      {
     3427 +        // We want to temporarily turn off discovery by the
     3428 +        // CM ref processor, if necessary, and turn it back on
     3429 +        // on again later if we do. Using a scoped
     3430 +        // NoRefDiscovery object will do this.
     3431 +        NoRefDiscovery no_cm_discovery(ref_processor_cm());
     3432 +
     3433 +        // Forget the current alloc region (we might even choose it to be part
     3434 +        // of the collection set!).
     3435 +        release_mutator_alloc_region();
     3436 +
     3437 +        // We should call this after we retire the mutator alloc
     3438 +        // region(s) so that all the ALLOC / RETIRE events are generated
     3439 +        // before the start GC event.
     3440 +        _hr_printer.start_gc(false /* full */, (size_t) total_collections());
     3441 +
     3442 +        // The elapsed time induced by the start time below deliberately elides
     3443 +        // the possible verification above.
     3444 +        double start_time_sec = os::elapsedTime();
     3445 +        size_t start_used_bytes = used();
3378 3446  
3379 3447  #if YOUNG_LIST_VERBOSE
3380      -      gclog_or_tty->print_cr("\nBefore recording pause start.\nYoung_list:");
3381      -      _young_list->print();
3382      -      g1_policy()->print_collection_set(g1_policy()->inc_cset_head(), gclog_or_tty);
     3448 +        gclog_or_tty->print_cr("\nBefore recording pause start.\nYoung_list:");
     3449 +        _young_list->print();
     3450 +        g1_policy()->print_collection_set(g1_policy()->inc_cset_head(), gclog_or_tty);
3383 3451  #endif // YOUNG_LIST_VERBOSE
3384 3452  
3385      -      g1_policy()->record_collection_pause_start(start_time_sec,
3386      -                                                 start_used_bytes);
     3453 +        g1_policy()->record_collection_pause_start(start_time_sec,
     3454 +                                                   start_used_bytes);
3387 3455  
3388 3456  #if YOUNG_LIST_VERBOSE
3389      -      gclog_or_tty->print_cr("\nAfter recording pause start.\nYoung_list:");
3390      -      _young_list->print();
     3457 +        gclog_or_tty->print_cr("\nAfter recording pause start.\nYoung_list:");
     3458 +        _young_list->print();
3391 3459  #endif // YOUNG_LIST_VERBOSE
3392 3460  
3393      -      if (g1_policy()->during_initial_mark_pause()) {
3394      -        concurrent_mark()->checkpointRootsInitialPre();
3395      -      }
3396      -      perm_gen()->save_marks();
3397      -
3398      -      // We must do this before any possible evacuation that should propagate
3399      -      // marks.
3400      -      if (mark_in_progress()) {
3401      -        double start_time_sec = os::elapsedTime();
     3461 +        if (g1_policy()->during_initial_mark_pause()) {
     3462 +          concurrent_mark()->checkpointRootsInitialPre();
     3463 +        }
     3464 +        perm_gen()->save_marks();
3402 3465  
3403      -        _cm->drainAllSATBBuffers();
3404      -        double finish_mark_ms = (os::elapsedTime() - start_time_sec) * 1000.0;
3405      -        g1_policy()->record_satb_drain_time(finish_mark_ms);
3406      -      }
3407      -      // Record the number of elements currently on the mark stack, so we
3408      -      // only iterate over these.  (Since evacuation may add to the mark
3409      -      // stack, doing more exposes race conditions.)  If no mark is in
3410      -      // progress, this will be zero.
3411      -      _cm->set_oops_do_bound();
     3466 +        // We must do this before any possible evacuation that should propagate
     3467 +        // marks.
     3468 +        if (mark_in_progress()) {
     3469 +          double start_time_sec = os::elapsedTime();
     3470 +
     3471 +          _cm->drainAllSATBBuffers();
     3472 +          double finish_mark_ms = (os::elapsedTime() - start_time_sec) * 1000.0;
     3473 +          g1_policy()->record_satb_drain_time(finish_mark_ms);
     3474 +        }
     3475 +        // Record the number of elements currently on the mark stack, so we
     3476 +        // only iterate over these.  (Since evacuation may add to the mark
     3477 +        // stack, doing more exposes race conditions.)  If no mark is in
     3478 +        // progress, this will be zero.
     3479 +        _cm->set_oops_do_bound();
3412 3480  
3413      -      if (mark_in_progress()) {
3414      -        concurrent_mark()->newCSet();
3415      -      }
     3481 +        if (mark_in_progress()) {
     3482 +          concurrent_mark()->newCSet();
     3483 +        }
3416 3484  
3417 3485  #if YOUNG_LIST_VERBOSE
3418      -      gclog_or_tty->print_cr("\nBefore choosing collection set.\nYoung_list:");
3419      -      _young_list->print();
3420      -      g1_policy()->print_collection_set(g1_policy()->inc_cset_head(), gclog_or_tty);
     3486 +        gclog_or_tty->print_cr("\nBefore choosing collection set.\nYoung_list:");
     3487 +        _young_list->print();
     3488 +        g1_policy()->print_collection_set(g1_policy()->inc_cset_head(), gclog_or_tty);
3421 3489  #endif // YOUNG_LIST_VERBOSE
3422 3490  
3423      -      g1_policy()->choose_collection_set(target_pause_time_ms);
     3491 +        g1_policy()->choose_collection_set(target_pause_time_ms);
3424 3492  
3425      -      if (_hr_printer.is_active()) {
3426      -        HeapRegion* hr = g1_policy()->collection_set();
3427      -        while (hr != NULL) {
3428      -          G1HRPrinter::RegionType type;
3429      -          if (!hr->is_young()) {
3430      -            type = G1HRPrinter::Old;
3431      -          } else if (hr->is_survivor()) {
3432      -            type = G1HRPrinter::Survivor;
3433      -          } else {
3434      -            type = G1HRPrinter::Eden;
     3493 +        if (_hr_printer.is_active()) {
     3494 +          HeapRegion* hr = g1_policy()->collection_set();
     3495 +          while (hr != NULL) {
     3496 +            G1HRPrinter::RegionType type;
     3497 +            if (!hr->is_young()) {
     3498 +              type = G1HRPrinter::Old;
     3499 +            } else if (hr->is_survivor()) {
     3500 +              type = G1HRPrinter::Survivor;
     3501 +            } else {
     3502 +              type = G1HRPrinter::Eden;
     3503 +            }
     3504 +            _hr_printer.cset(hr);
     3505 +            hr = hr->next_in_collection_set();
3435 3506            }
3436      -          _hr_printer.cset(hr);
3437      -          hr = hr->next_in_collection_set();
3438 3507          }
3439      -      }
3440 3508  
3441      -      // We have chosen the complete collection set. If marking is
3442      -      // active then, we clear the region fields of any of the
3443      -      // concurrent marking tasks whose region fields point into
3444      -      // the collection set as these values will become stale. This
3445      -      // will cause the owning marking threads to claim a new region
3446      -      // when marking restarts.
3447      -      if (mark_in_progress()) {
3448      -        concurrent_mark()->reset_active_task_region_fields_in_cset();
3449      -      }
     3509 +        // We have chosen the complete collection set. If marking is
     3510 +        // active then, we clear the region fields of any of the
     3511 +        // concurrent marking tasks whose region fields point into
     3512 +        // the collection set as these values will become stale. This
     3513 +        // will cause the owning marking threads to claim a new region
     3514 +        // when marking restarts.
     3515 +        if (mark_in_progress()) {
     3516 +          concurrent_mark()->reset_active_task_region_fields_in_cset();
     3517 +        }
3450 3518  
3451 3519  #ifdef ASSERT
3452      -      VerifyCSetClosure cl;
3453      -      collection_set_iterate(&cl);
     3520 +        VerifyCSetClosure cl;
     3521 +        collection_set_iterate(&cl);
3454 3522  #endif // ASSERT
3455 3523  
3456      -      setup_surviving_young_words();
     3524 +        setup_surviving_young_words();
3457 3525  
3458      -      // Initialize the GC alloc regions.
3459      -      init_gc_alloc_regions();
     3526 +        // Initialize the GC alloc regions.
     3527 +        init_gc_alloc_regions();
3460 3528  
3461      -      // Actually do the work...
3462      -      evacuate_collection_set();
     3529 +        // Actually do the work...
     3530 +        evacuate_collection_set();
3463 3531  
3464      -      free_collection_set(g1_policy()->collection_set());
3465      -      g1_policy()->clear_collection_set();
     3532 +        free_collection_set(g1_policy()->collection_set());
     3533 +        g1_policy()->clear_collection_set();
3466 3534  
3467      -      cleanup_surviving_young_words();
     3535 +        cleanup_surviving_young_words();
3468 3536  
3469      -      // Start a new incremental collection set for the next pause.
3470      -      g1_policy()->start_incremental_cset_building();
     3537 +        // Start a new incremental collection set for the next pause.
     3538 +        g1_policy()->start_incremental_cset_building();
3471 3539  
3472      -      // Clear the _cset_fast_test bitmap in anticipation of adding
3473      -      // regions to the incremental collection set for the next
3474      -      // evacuation pause.
3475      -      clear_cset_fast_test();
     3540 +        // Clear the _cset_fast_test bitmap in anticipation of adding
     3541 +        // regions to the incremental collection set for the next
     3542 +        // evacuation pause.
     3543 +        clear_cset_fast_test();
3476 3544  
3477      -      _young_list->reset_sampled_info();
     3545 +        _young_list->reset_sampled_info();
3478 3546  
3479      -      // Don't check the whole heap at this point as the
3480      -      // GC alloc regions from this pause have been tagged
3481      -      // as survivors and moved on to the survivor list.
3482      -      // Survivor regions will fail the !is_young() check.
3483      -      assert(check_young_list_empty(false /* check_heap */),
3484      -        "young list should be empty");
     3547 +        // Don't check the whole heap at this point as the
     3548 +        // GC alloc regions from this pause have been tagged
     3549 +        // as survivors and moved on to the survivor list.
     3550 +        // Survivor regions will fail the !is_young() check.
     3551 +        assert(check_young_list_empty(false /* check_heap */),
     3552 +          "young list should be empty");
3485 3553  
3486 3554  #if YOUNG_LIST_VERBOSE
3487      -      gclog_or_tty->print_cr("Before recording survivors.\nYoung List:");
3488      -      _young_list->print();
     3555 +        gclog_or_tty->print_cr("Before recording survivors.\nYoung List:");
     3556 +        _young_list->print();
3489 3557  #endif // YOUNG_LIST_VERBOSE
3490 3558  
3491      -      g1_policy()->record_survivor_regions(_young_list->survivor_length(),
3492      -        _young_list->first_survivor_region(),
3493      -        _young_list->last_survivor_region());
     3559 +        g1_policy()->record_survivor_regions(_young_list->survivor_length(),
     3560 +                                            _young_list->first_survivor_region(),
     3561 +                                            _young_list->last_survivor_region());
3494 3562  
3495      -      _young_list->reset_auxilary_lists();
     3563 +        _young_list->reset_auxilary_lists();
3496 3564  
3497      -      if (evacuation_failed()) {
3498      -        _summary_bytes_used = recalculate_used();
3499      -      } else {
3500      -        // The "used" of the the collection set have already been subtracted
3501      -        // when they were freed.  Add in the bytes evacuated.
3502      -        _summary_bytes_used += g1_policy()->bytes_copied_during_gc();
3503      -      }
3504      -
3505      -      if (g1_policy()->during_initial_mark_pause()) {
3506      -        concurrent_mark()->checkpointRootsInitialPost();
3507      -        set_marking_started();
3508      -        // CAUTION: after the doConcurrentMark() call below,
3509      -        // the concurrent marking thread(s) could be running
3510      -        // concurrently with us. Make sure that anything after
3511      -        // this point does not assume that we are the only GC thread
3512      -        // running. Note: of course, the actual marking work will
3513      -        // not start until the safepoint itself is released in
3514      -        // ConcurrentGCThread::safepoint_desynchronize().
3515      -        doConcurrentMark();
3516      -      }
     3565 +        if (evacuation_failed()) {
     3566 +          _summary_bytes_used = recalculate_used();
     3567 +        } else {
     3568 +          // The "used" of the the collection set have already been subtracted
     3569 +          // when they were freed.  Add in the bytes evacuated.
     3570 +          _summary_bytes_used += g1_policy()->bytes_copied_during_gc();
     3571 +        }
     3572 +
     3573 +        if (g1_policy()->during_initial_mark_pause()) {
     3574 +          concurrent_mark()->checkpointRootsInitialPost();
     3575 +          set_marking_started();
     3576 +          // CAUTION: after the doConcurrentMark() call below,
     3577 +          // the concurrent marking thread(s) could be running
     3578 +          // concurrently with us. Make sure that anything after
     3579 +          // this point does not assume that we are the only GC thread
     3580 +          // running. Note: of course, the actual marking work will
     3581 +          // not start until the safepoint itself is released in
     3582 +          // ConcurrentGCThread::safepoint_desynchronize().
     3583 +          doConcurrentMark();
     3584 +        }
3517 3585  
3518      -      allocate_dummy_regions();
     3586 +        allocate_dummy_regions();
3519 3587  
3520 3588  #if YOUNG_LIST_VERBOSE
3521      -      gclog_or_tty->print_cr("\nEnd of the pause.\nYoung_list:");
3522      -      _young_list->print();
3523      -      g1_policy()->print_collection_set(g1_policy()->inc_cset_head(), gclog_or_tty);
     3589 +        gclog_or_tty->print_cr("\nEnd of the pause.\nYoung_list:");
     3590 +        _young_list->print();
     3591 +        g1_policy()->print_collection_set(g1_policy()->inc_cset_head(), gclog_or_tty);
3524 3592  #endif // YOUNG_LIST_VERBOSE
3525 3593  
3526      -      init_mutator_alloc_region();
     3594 +        init_mutator_alloc_region();
3527 3595  
3528      -      {
3529      -        size_t expand_bytes = g1_policy()->expansion_amount();
3530      -        if (expand_bytes > 0) {
3531      -          size_t bytes_before = capacity();
3532      -          if (!expand(expand_bytes)) {
3533      -            // We failed to expand the heap so let's verify that
3534      -            // committed/uncommitted amount match the backing store
3535      -            assert(capacity() == _g1_storage.committed_size(), "committed size mismatch");
3536      -            assert(max_capacity() == _g1_storage.reserved_size(), "reserved size mismatch");
     3596 +        {
     3597 +          size_t expand_bytes = g1_policy()->expansion_amount();
     3598 +          if (expand_bytes > 0) {
     3599 +            size_t bytes_before = capacity();
     3600 +            if (!expand(expand_bytes)) {
     3601 +              // We failed to expand the heap so let's verify that
     3602 +              // committed/uncommitted amount match the backing store
     3603 +              assert(capacity() == _g1_storage.committed_size(), "committed size mismatch");
     3604 +              assert(max_capacity() == _g1_storage.reserved_size(), "reserved size mismatch");
     3605 +            }
3537 3606            }
3538 3607          }
3539      -      }
3540 3608  
3541      -      double end_time_sec = os::elapsedTime();
3542      -      double pause_time_ms = (end_time_sec - start_time_sec) * MILLIUNITS;
3543      -      g1_policy()->record_pause_time_ms(pause_time_ms);
3544      -      g1_policy()->record_collection_pause_end();
3545      -
3546      -      MemoryService::track_memory_usage();
3547      -
3548      -      // In prepare_for_verify() below we'll need to scan the deferred
3549      -      // update buffers to bring the RSets up-to-date if
3550      -      // G1HRRSFlushLogBuffersOnVerify has been set. While scanning
3551      -      // the update buffers we'll probably need to scan cards on the
3552      -      // regions we just allocated to (i.e., the GC alloc
3553      -      // regions). However, during the last GC we called
3554      -      // set_saved_mark() on all the GC alloc regions, so card
3555      -      // scanning might skip the [saved_mark_word()...top()] area of
3556      -      // those regions (i.e., the area we allocated objects into
3557      -      // during the last GC). But it shouldn't. Given that
3558      -      // saved_mark_word() is conditional on whether the GC time stamp
3559      -      // on the region is current or not, by incrementing the GC time
3560      -      // stamp here we invalidate all the GC time stamps on all the
3561      -      // regions and saved_mark_word() will simply return top() for
3562      -      // all the regions. This is a nicer way of ensuring this rather
3563      -      // than iterating over the regions and fixing them. In fact, the
3564      -      // GC time stamp increment here also ensures that
3565      -      // saved_mark_word() will return top() between pauses, i.e.,
3566      -      // during concurrent refinement. So we don't need the
3567      -      // is_gc_active() check to decided which top to use when
3568      -      // scanning cards (see CR 7039627).
3569      -      increment_gc_time_stamp();
     3609 +        double end_time_sec = os::elapsedTime();
     3610 +        double pause_time_ms = (end_time_sec - start_time_sec) * MILLIUNITS;
     3611 +        g1_policy()->record_pause_time_ms(pause_time_ms);
     3612 +        g1_policy()->record_collection_pause_end();
     3613 +
     3614 +        MemoryService::track_memory_usage();
     3615 +
     3616 +        // In prepare_for_verify() below we'll need to scan the deferred
     3617 +        // update buffers to bring the RSets up-to-date if
     3618 +        // G1HRRSFlushLogBuffersOnVerify has been set. While scanning
     3619 +        // the update buffers we'll probably need to scan cards on the
     3620 +        // regions we just allocated to (i.e., the GC alloc
     3621 +        // regions). However, during the last GC we called
     3622 +        // set_saved_mark() on all the GC alloc regions, so card
     3623 +        // scanning might skip the [saved_mark_word()...top()] area of
     3624 +        // those regions (i.e., the area we allocated objects into
     3625 +        // during the last GC). But it shouldn't. Given that
     3626 +        // saved_mark_word() is conditional on whether the GC time stamp
     3627 +        // on the region is current or not, by incrementing the GC time
     3628 +        // stamp here we invalidate all the GC time stamps on all the
     3629 +        // regions and saved_mark_word() will simply return top() for
     3630 +        // all the regions. This is a nicer way of ensuring this rather
     3631 +        // than iterating over the regions and fixing them. In fact, the
     3632 +        // GC time stamp increment here also ensures that
     3633 +        // saved_mark_word() will return top() between pauses, i.e.,
     3634 +        // during concurrent refinement. So we don't need the
     3635 +        // is_gc_active() check to decided which top to use when
     3636 +        // scanning cards (see CR 7039627).
     3637 +        increment_gc_time_stamp();
     3638 +
     3639 +        if (VerifyAfterGC && total_collections() >= VerifyGCStartAt) {
     3640 +          HandleMark hm;  // Discard invalid handles created during verification
     3641 +          gclog_or_tty->print(" VerifyAfterGC:");
     3642 +          prepare_for_verify();
     3643 +          Universe::verify(/* allow dirty */ true,
     3644 +                           /* silent      */ false,
     3645 +                           /* option      */ VerifyOption_G1UsePrevMarking);
     3646 +        }
3570 3647  
3571      -      if (VerifyAfterGC && total_collections() >= VerifyGCStartAt) {
3572      -        HandleMark hm;  // Discard invalid handles created during verification
3573      -        gclog_or_tty->print(" VerifyAfterGC:");
3574      -        prepare_for_verify();
3575      -        Universe::verify(/* allow dirty */ true,
3576      -                         /* silent      */ false,
3577      -                         /* option      */ VerifyOption_G1UsePrevMarking);
3578      -      }
     3648 +        assert(!ref_processor_stw()->discovery_enabled(), "Postcondition");
     3649 +        ref_processor_stw()->verify_no_references_recorded();
3579 3650  
3580      -      if (was_enabled) ref_processor()->enable_discovery();
     3651 +        // CM reference discovery will be re-enabled if necessary.
     3652 +      }
3581 3653  
3582 3654        {
3583 3655          size_t expand_bytes = g1_policy()->expansion_amount();
3584 3656          if (expand_bytes > 0) {
3585 3657            size_t bytes_before = capacity();
3586 3658            // No need for an ergo verbose message here,
3587 3659            // expansion_amount() does this when it returns a value > 0.
3588 3660            if (!expand(expand_bytes)) {
3589 3661              // We failed to expand the heap so let's verify that
3590 3662              // committed/uncommitted amount match the backing store
↓ open down ↓ 130 lines elided ↑ open up ↑
3721 3793  
3722 3794  void G1CollectedHeap::finalize_for_evac_failure() {
3723 3795    assert(_evac_failure_scan_stack != NULL &&
3724 3796           _evac_failure_scan_stack->length() == 0,
3725 3797           "Postcondition");
3726 3798    assert(!_drain_in_progress, "Postcondition");
3727 3799    delete _evac_failure_scan_stack;
3728 3800    _evac_failure_scan_stack = NULL;
3729 3801  }
3730 3802  
3731      -// *** Sequential G1 Evacuation
3732      -
3733      -class G1IsAliveClosure: public BoolObjectClosure {
3734      -  G1CollectedHeap* _g1;
3735      -public:
3736      -  G1IsAliveClosure(G1CollectedHeap* g1) : _g1(g1) {}
3737      -  void do_object(oop p) { assert(false, "Do not call."); }
3738      -  bool do_object_b(oop p) {
3739      -    // It is reachable if it is outside the collection set, or is inside
3740      -    // and forwarded.
3741      -    return !_g1->obj_in_cs(p) || p->is_forwarded();
3742      -  }
3743      -};
3744      -
3745      -class G1KeepAliveClosure: public OopClosure {
3746      -  G1CollectedHeap* _g1;
3747      -public:
3748      -  G1KeepAliveClosure(G1CollectedHeap* g1) : _g1(g1) {}
3749      -  void do_oop(narrowOop* p) { guarantee(false, "Not needed"); }
3750      -  void do_oop(      oop* p) {
3751      -    oop obj = *p;
3752      -    if (_g1->obj_in_cs(obj)) {
3753      -      assert( obj->is_forwarded(), "invariant" );
3754      -      *p = obj->forwardee();
3755      -    }
3756      -  }
3757      -};
3758      -
3759 3803  class UpdateRSetDeferred : public OopsInHeapRegionClosure {
3760 3804  private:
3761 3805    G1CollectedHeap* _g1;
3762 3806    DirtyCardQueue *_dcq;
3763 3807    CardTableModRefBS* _ct_bs;
3764 3808  
3765 3809  public:
3766 3810    UpdateRSetDeferred(G1CollectedHeap* g1, DirtyCardQueue* dcq) :
3767 3811      _g1(g1), _ct_bs((CardTableModRefBS*)_g1->barrier_set()), _dcq(dcq) {}
3768 3812  
↓ open down ↓ 399 lines elided ↑ open up ↑
4168 4212  bool G1ParScanThreadState::verify_task(StarTask ref) const {
4169 4213    if (ref.is_narrow()) {
4170 4214      return verify_ref((narrowOop*) ref);
4171 4215    } else {
4172 4216      return verify_ref((oop*) ref);
4173 4217    }
4174 4218  }
4175 4219  #endif // ASSERT
4176 4220  
4177 4221  void G1ParScanThreadState::trim_queue() {
     4222 +  assert(_evac_cl != NULL, "not set");
     4223 +  assert(_evac_failure_cl != NULL, "not set");
     4224 +  assert(_partial_scan_cl != NULL, "not set");
     4225 +
4178 4226    StarTask ref;
4179 4227    do {
4180 4228      // Drain the overflow stack first, so other threads can steal.
4181 4229      while (refs()->pop_overflow(ref)) {
4182 4230        deal_with_reference(ref);
4183 4231      }
     4232 +
4184 4233      while (refs()->pop_local(ref)) {
4185 4234        deal_with_reference(ref);
4186 4235      }
4187 4236    } while (!refs()->is_empty());
4188 4237  }
4189 4238  
4190 4239  G1ParClosureSuper::G1ParClosureSuper(G1CollectedHeap* g1, G1ParScanThreadState* par_scan_state) :
4191 4240    _g1(g1), _g1_rem(_g1->g1_rem_set()), _cm(_g1->concurrent_mark()),
4192 4241    _par_scan_state(par_scan_state),
4193 4242    _during_initial_mark(_g1->g1_policy()->during_initial_mark_pause()),
↓ open down ↓ 300 lines elided ↑ open up ↑
4494 4543  
4495 4544    void work(int i) {
4496 4545      if (i >= _n_workers) return;  // no work needed this round
4497 4546  
4498 4547      double start_time_ms = os::elapsedTime() * 1000.0;
4499 4548      _g1h->g1_policy()->record_gc_worker_start_time(i, start_time_ms);
4500 4549  
4501 4550      ResourceMark rm;
4502 4551      HandleMark   hm;
4503 4552  
     4553 +    ReferenceProcessor*             rp = _g1h->ref_processor_stw();
     4554 +
4504 4555      G1ParScanThreadState            pss(_g1h, i);
4505      -    G1ParScanHeapEvacClosure        scan_evac_cl(_g1h, &pss);
4506      -    G1ParScanHeapEvacFailureClosure evac_failure_cl(_g1h, &pss);
4507      -    G1ParScanPartialArrayClosure    partial_scan_cl(_g1h, &pss);
     4556 +    G1ParScanHeapEvacClosure        scan_evac_cl(_g1h, &pss, rp);
     4557 +    G1ParScanHeapEvacFailureClosure evac_failure_cl(_g1h, &pss, rp);
     4558 +    G1ParScanPartialArrayClosure    partial_scan_cl(_g1h, &pss, rp);
4508 4559  
4509 4560      pss.set_evac_closure(&scan_evac_cl);
4510 4561      pss.set_evac_failure_closure(&evac_failure_cl);
4511 4562      pss.set_partial_scan_closure(&partial_scan_cl);
4512 4563  
4513      -    G1ParScanExtRootClosure         only_scan_root_cl(_g1h, &pss);
4514      -    G1ParScanPermClosure            only_scan_perm_cl(_g1h, &pss);
4515      -    G1ParScanHeapRSClosure          only_scan_heap_rs_cl(_g1h, &pss);
4516      -    G1ParPushHeapRSClosure          push_heap_rs_cl(_g1h, &pss);
4517      -
4518      -    G1ParScanAndMarkExtRootClosure  scan_mark_root_cl(_g1h, &pss);
4519      -    G1ParScanAndMarkPermClosure     scan_mark_perm_cl(_g1h, &pss);
4520      -    G1ParScanAndMarkHeapRSClosure   scan_mark_heap_rs_cl(_g1h, &pss);
     4564 +    G1ParScanExtRootClosure        only_scan_root_cl(_g1h, &pss, rp);
     4565 +    G1ParScanPermClosure           only_scan_perm_cl(_g1h, &pss, rp);
     4566 +
     4567 +    G1ParScanAndMarkExtRootClosure scan_mark_root_cl(_g1h, &pss, rp);
     4568 +    G1ParScanAndMarkPermClosure    scan_mark_perm_cl(_g1h, &pss, rp);
4521 4569  
4522      -    OopsInHeapRegionClosure        *scan_root_cl;
4523      -    OopsInHeapRegionClosure        *scan_perm_cl;
     4570 +    OopClosure*                    scan_root_cl = &only_scan_root_cl;
     4571 +    OopsInHeapRegionClosure*       scan_perm_cl = &only_scan_perm_cl;
4524 4572  
4525 4573      if (_g1h->g1_policy()->during_initial_mark_pause()) {
     4574 +      // We also need to mark copied objects.
4526 4575        scan_root_cl = &scan_mark_root_cl;
4527 4576        scan_perm_cl = &scan_mark_perm_cl;
4528      -    } else {
4529      -      scan_root_cl = &only_scan_root_cl;
4530      -      scan_perm_cl = &only_scan_perm_cl;
4531 4577      }
4532 4578  
     4579 +    // The following closure is used to scan RSets looking for reference
     4580 +    // fields that point into the collection set. The actual field iteration
     4581 +    // is performed by a FilterIntoCSClosure, whose do_oop method calls the
     4582 +    // do_oop method of the following closure.
     4583 +    // Therefore we want to record the reference processor in the
     4584 +    // FilterIntoCSClosure. To do so we record the STW reference
     4585 +    // processor into the following closure and pass it to the
     4586 +    // FilterIntoCSClosure in HeapRegionDCTOC::walk_mem_region_with_cl.
     4587 +    G1ParPushHeapRSClosure          push_heap_rs_cl(_g1h, &pss, rp);
     4588 +
4533 4589      pss.start_strong_roots();
4534 4590      _g1h->g1_process_strong_roots(/* not collecting perm */ false,
4535 4591                                    SharedHeap::SO_AllClasses,
4536 4592                                    scan_root_cl,
4537 4593                                    &push_heap_rs_cl,
4538 4594                                    scan_perm_cl,
4539 4595                                    i);
4540 4596      pss.end_strong_roots();
4541 4597  
4542 4598      {
↓ open down ↓ 27 lines elided ↑ open up ↑
4570 4626  // This method is run in a GC worker.
4571 4627  
4572 4628  void
4573 4629  G1CollectedHeap::
4574 4630  g1_process_strong_roots(bool collecting_perm_gen,
4575 4631                          SharedHeap::ScanningOption so,
4576 4632                          OopClosure* scan_non_heap_roots,
4577 4633                          OopsInHeapRegionClosure* scan_rs,
4578 4634                          OopsInGenClosure* scan_perm,
4579 4635                          int worker_i) {
     4636 +
4580 4637    // First scan the strong roots, including the perm gen.
4581 4638    double ext_roots_start = os::elapsedTime();
4582 4639    double closure_app_time_sec = 0.0;
4583 4640  
4584 4641    BufferingOopClosure buf_scan_non_heap_roots(scan_non_heap_roots);
4585 4642    BufferingOopsInGenClosure buf_scan_perm(scan_perm);
4586 4643    buf_scan_perm.set_generation(perm_gen());
4587 4644  
4588 4645    // Walk the code cache w/o buffering, because StarTask cannot handle
4589 4646    // unaligned oop locations.
4590 4647    CodeBlobToOopClosure eager_scan_code_roots(scan_non_heap_roots, /*do_marking=*/ true);
4591 4648  
4592 4649    process_strong_roots(false, // no scoping; this is parallel code
4593 4650                         collecting_perm_gen, so,
4594 4651                         &buf_scan_non_heap_roots,
4595 4652                         &eager_scan_code_roots,
4596 4653                         &buf_scan_perm);
4597 4654  
4598      -  // Now the ref_processor roots.
     4655 +  // Now the CM ref_processor roots.
4599 4656    if (!_process_strong_tasks->is_task_claimed(G1H_PS_refProcessor_oops_do)) {
4600      -    // We need to treat the discovered reference lists as roots and
4601      -    // keep entries (which are added by the marking threads) on them
4602      -    // live until they can be processed at the end of marking.
4603      -    ref_processor()->weak_oops_do(&buf_scan_non_heap_roots);
     4657 +    // We need to treat the discovered reference lists of the
     4658 +    // concurrent mark ref processor as roots and keep entries
     4659 +    // (which are added by the marking threads) on them live
     4660 +    // until they can be processed at the end of marking.
     4661 +    ref_processor_cm()->weak_oops_do(&buf_scan_non_heap_roots);
4604 4662    }
4605 4663  
4606 4664    // Finish up any enqueued closure apps (attributed as object copy time).
4607 4665    buf_scan_non_heap_roots.done();
4608 4666    buf_scan_perm.done();
4609 4667  
4610 4668    double ext_roots_end = os::elapsedTime();
4611 4669  
4612 4670    g1_policy()->reset_obj_copy_time(worker_i);
4613 4671    double obj_copy_time_sec = buf_scan_perm.closure_app_seconds() +
↓ open down ↓ 20 lines elided ↑ open up ↑
4634 4692    _process_strong_tasks->all_tasks_completed();
4635 4693  }
4636 4694  
4637 4695  void
4638 4696  G1CollectedHeap::g1_process_weak_roots(OopClosure* root_closure,
4639 4697                                         OopClosure* non_root_closure) {
4640 4698    CodeBlobToOopClosure roots_in_blobs(root_closure, /*do_marking=*/ false);
4641 4699    SharedHeap::process_weak_roots(root_closure, &roots_in_blobs, non_root_closure);
4642 4700  }
4643 4701  
     4702 +// Weak Reference Processing support
     4703 +
     4704 +// An always "is_alive" closure that is used to preserve referents.
     4705 +// If the object is non-null then it's alive.  Used in the preservation
     4706 +// of referent objects that are pointed to by reference objects
     4707 +// discovered by the CM ref processor.
     4708 +class G1AlwaysAliveClosure: public BoolObjectClosure {
     4709 +  G1CollectedHeap* _g1;
     4710 +public:
     4711 +  G1AlwaysAliveClosure(G1CollectedHeap* g1) : _g1(g1) {}
     4712 +  void do_object(oop p) { assert(false, "Do not call."); }
     4713 +  bool do_object_b(oop p) {
     4714 +    if (p != NULL) {
     4715 +      return true;
     4716 +    }
     4717 +    return false;
     4718 +  }
     4719 +};
     4720 +
     4721 +bool G1STWIsAliveClosure::do_object_b(oop p) {
     4722 +  // An object is reachable if it is outside the collection set,
     4723 +  // or is inside and copied.
     4724 +  return !_g1->obj_in_cs(p) || p->is_forwarded();
     4725 +}
     4726 +
     4727 +// Non Copying Keep Alive closure
     4728 +class G1KeepAliveClosure: public OopClosure {
     4729 +  G1CollectedHeap* _g1;
     4730 +public:
     4731 +  G1KeepAliveClosure(G1CollectedHeap* g1) : _g1(g1) {}
     4732 +  void do_oop(narrowOop* p) { guarantee(false, "Not needed"); }
     4733 +  void do_oop(      oop* p) {
     4734 +    oop obj = *p;
     4735 +
     4736 +    if (_g1->obj_in_cs(obj)) {
     4737 +      assert( obj->is_forwarded(), "invariant" );
     4738 +      *p = obj->forwardee();
     4739 +    }
     4740 +  }
     4741 +};
     4742 +
     4743 +// Copying Keep Alive closure - can be called from both
     4744 +// serial and parallel code as long as different worker
     4745 +// threads utilize different G1ParScanThreadState instances
     4746 +// and different queues.
     4747 +
     4748 +class G1CopyingKeepAliveClosure: public OopClosure {
     4749 +  G1CollectedHeap*         _g1h;
     4750 +  OopClosure*              _copy_non_heap_obj_cl;
     4751 +  OopsInHeapRegionClosure* _copy_perm_obj_cl;
     4752 +  G1ParScanThreadState*    _par_scan_state;
     4753 +
     4754 +public:
     4755 +  G1CopyingKeepAliveClosure(G1CollectedHeap* g1h,
     4756 +                            OopClosure* non_heap_obj_cl,
     4757 +                            OopsInHeapRegionClosure* perm_obj_cl,
     4758 +                            G1ParScanThreadState* pss):
     4759 +    _g1h(g1h),
     4760 +    _copy_non_heap_obj_cl(non_heap_obj_cl),
     4761 +    _copy_perm_obj_cl(perm_obj_cl),
     4762 +    _par_scan_state(pss)
     4763 +  {}
     4764 +
     4765 +  virtual void do_oop(narrowOop* p) { do_oop_work(p); }
     4766 +  virtual void do_oop(      oop* p) { do_oop_work(p); }
     4767 +
     4768 +  template <class T> void do_oop_work(T* p) {
     4769 +    oop obj = oopDesc::load_decode_heap_oop(p);
     4770 +
     4771 +    if (_g1h->obj_in_cs(obj)) {
     4772 +      // If the referent object has been forwarded (either copied
     4773 +      // to a new location or to itself in the event of an
     4774 +      // evacuation failure) then we need to update the reference
     4775 +      // field and, if both reference and referent are in the G1
     4776 +      // heap, update the RSet for the referent.
     4777 +      //
     4778 +      // If the referent has not been forwarded then we have to keep
     4779 +      // it alive by policy. Therefore we have copy the referent.
     4780 +      //
     4781 +      // If the reference field is in the G1 heap then we can push
     4782 +      // on the PSS queue. When the queue is drained (after each
     4783 +      // phase of reference processing) the object and it's followers
     4784 +      // will be copied, the reference field set to point to the
     4785 +      // new location, and the RSet updated. Otherwise we need to
     4786 +      // use the the non-heap or perm closures directly to copy
     4787 +      // the refernt object and update the pointer, while avoiding
     4788 +      // updating the RSet.
     4789 +
     4790 +      if (_g1h->is_in_g1_reserved(p)) {
     4791 +        _par_scan_state->push_on_queue(p);
     4792 +      } else {
     4793 +        // The reference field is not in the G1 heap.
     4794 +        if (_g1h->perm_gen()->is_in(p)) {
     4795 +          _copy_perm_obj_cl->do_oop(p);
     4796 +        } else {
     4797 +          _copy_non_heap_obj_cl->do_oop(p);
     4798 +        }
     4799 +      }
     4800 +    }
     4801 +  }
     4802 +};
     4803 +
     4804 +// Serial drain queue closure. Called as the 'complete_gc'
     4805 +// closure for each discovered list in some of the
     4806 +// reference processing phases.
     4807 +
     4808 +class G1STWDrainQueueClosure: public VoidClosure {
     4809 +protected:
     4810 +  G1CollectedHeap* _g1h;
     4811 +  G1ParScanThreadState* _par_scan_state;
     4812 +
     4813 +  G1ParScanThreadState*   par_scan_state() { return _par_scan_state; }
     4814 +
     4815 +public:
     4816 +  G1STWDrainQueueClosure(G1CollectedHeap* g1h, G1ParScanThreadState* pss) :
     4817 +    _g1h(g1h),
     4818 +    _par_scan_state(pss)
     4819 +  { }
     4820 +
     4821 +  void do_void() {
     4822 +    G1ParScanThreadState* const pss = par_scan_state();
     4823 +    pss->trim_queue();
     4824 +  }
     4825 +};
     4826 +
     4827 +// Parallel Reference Processing closures
     4828 +
     4829 +// Implementation of AbstractRefProcTaskExecutor for parallel reference
     4830 +// processing during G1 evacuation pauses.
     4831 +
     4832 +class G1STWRefProcTaskExecutor: public AbstractRefProcTaskExecutor {
     4833 +private:
     4834 +  G1CollectedHeap*   _g1h;
     4835 +  RefToScanQueueSet* _queues;
     4836 +  WorkGang*          _workers;
     4837 +  int                _active_workers;
     4838 +
     4839 +public:
     4840 +  G1STWRefProcTaskExecutor(G1CollectedHeap* g1h,
     4841 +                        WorkGang* workers,
     4842 +                        RefToScanQueueSet *task_queues,
     4843 +                        int n_workers) :
     4844 +    _g1h(g1h),
     4845 +    _queues(task_queues),
     4846 +    _workers(workers),
     4847 +    _active_workers(n_workers)
     4848 +  {
     4849 +    assert(n_workers > 0, "shouldn't call this otherwise");
     4850 +  }
     4851 +
     4852 +  // Executes the given task using concurrent marking worker threads.
     4853 +  virtual void execute(ProcessTask& task);
     4854 +  virtual void execute(EnqueueTask& task);
     4855 +};
     4856 +
     4857 +// Gang task for possibly parallel reference processing
     4858 +
     4859 +class G1STWRefProcTaskProxy: public AbstractGangTask {
     4860 +  typedef AbstractRefProcTaskExecutor::ProcessTask ProcessTask;
     4861 +  ProcessTask&     _proc_task;
     4862 +  G1CollectedHeap* _g1h;
     4863 +  RefToScanQueueSet *_task_queues;
     4864 +  ParallelTaskTerminator* _terminator;
     4865 +
     4866 +public:
     4867 +  G1STWRefProcTaskProxy(ProcessTask& proc_task,
     4868 +                     G1CollectedHeap* g1h,
     4869 +                     RefToScanQueueSet *task_queues,
     4870 +                     ParallelTaskTerminator* terminator) :
     4871 +    AbstractGangTask("Process reference objects in parallel"),
     4872 +    _proc_task(proc_task),
     4873 +    _g1h(g1h),
     4874 +    _task_queues(task_queues),
     4875 +    _terminator(terminator)
     4876 +  {}
     4877 +
     4878 +  virtual void work(int i) {
     4879 +    // The reference processing task executed by a single worker.
     4880 +    ResourceMark rm;
     4881 +    HandleMark   hm;
     4882 +
     4883 +    G1STWIsAliveClosure is_alive(_g1h);
     4884 +
     4885 +    G1ParScanThreadState pss(_g1h, i);
     4886 +
     4887 +    G1ParScanHeapEvacClosure        scan_evac_cl(_g1h, &pss, NULL);
     4888 +    G1ParScanHeapEvacFailureClosure evac_failure_cl(_g1h, &pss, NULL);
     4889 +    G1ParScanPartialArrayClosure    partial_scan_cl(_g1h, &pss, NULL);
     4890 +
     4891 +    pss.set_evac_closure(&scan_evac_cl);
     4892 +    pss.set_evac_failure_closure(&evac_failure_cl);
     4893 +    pss.set_partial_scan_closure(&partial_scan_cl);
     4894 +
     4895 +    G1ParScanExtRootClosure        only_copy_non_heap_cl(_g1h, &pss, NULL);
     4896 +    G1ParScanPermClosure           only_copy_perm_cl(_g1h, &pss, NULL);
     4897 +
     4898 +    G1ParScanAndMarkExtRootClosure copy_mark_non_heap_cl(_g1h, &pss, NULL);
     4899 +    G1ParScanAndMarkPermClosure    copy_mark_perm_cl(_g1h, &pss, NULL);
     4900 +
     4901 +    OopClosure*                    copy_non_heap_cl = &only_copy_non_heap_cl;
     4902 +    OopsInHeapRegionClosure*       copy_perm_cl = &only_copy_perm_cl;
     4903 +
     4904 +    if (_g1h->g1_policy()->during_initial_mark_pause()) {
     4905 +      // We also need to mark copied objects.
     4906 +      copy_non_heap_cl = &copy_mark_non_heap_cl;
     4907 +      copy_perm_cl = &copy_mark_perm_cl;
     4908 +    }
     4909 +
     4910 +    // Keep alive closure.
     4911 +    G1CopyingKeepAliveClosure keep_alive(_g1h, copy_non_heap_cl, copy_perm_cl, &pss);
     4912 +
     4913 +    // Complete GC closure
     4914 +    G1ParEvacuateFollowersClosure drain_queue(_g1h, &pss, _task_queues, _terminator);
     4915 +
     4916 +    // Call the reference processing task's work routine.
     4917 +    _proc_task.work(i, is_alive, keep_alive, drain_queue);
     4918 +
     4919 +    // Note we cannot assert that the refs array is empty here as not all
     4920 +    // of the processing tasks (specifically phase2 - pp2_work) execute
     4921 +    // the complete_gc closure (which ordinarily would drain the queue) so
     4922 +    // the queue may not be empty.
     4923 +  }
     4924 +};
     4925 +
     4926 +// Driver routine for parallel reference processing.
     4927 +// Creates an instance of the ref processing gang
     4928 +// task and has the worker threads execute it.
     4929 +void G1STWRefProcTaskExecutor::execute(ProcessTask& proc_task) {
     4930 +  assert(_workers != NULL, "Need parallel worker threads.");
     4931 +
     4932 +  ParallelTaskTerminator terminator(_active_workers, _queues);
     4933 +  G1STWRefProcTaskProxy proc_task_proxy(proc_task, _g1h, _queues, &terminator);
     4934 +
     4935 +  _g1h->set_par_threads(_active_workers);
     4936 +  _workers->run_task(&proc_task_proxy);
     4937 +  _g1h->set_par_threads(0);
     4938 +}
     4939 +
     4940 +// Gang task for parallel reference enqueueing.
     4941 +
     4942 +class G1STWRefEnqueueTaskProxy: public AbstractGangTask {
     4943 +  typedef AbstractRefProcTaskExecutor::EnqueueTask EnqueueTask;
     4944 +  EnqueueTask& _enq_task;
     4945 +
     4946 +public:
     4947 +  G1STWRefEnqueueTaskProxy(EnqueueTask& enq_task) :
     4948 +    AbstractGangTask("Enqueue reference objects in parallel"),
     4949 +    _enq_task(enq_task)
     4950 +  { }
     4951 +
     4952 +  virtual void work(int i) {
     4953 +    _enq_task.work(i);
     4954 +  }
     4955 +};
     4956 +
     4957 +// Driver routine for parallel reference enqueing.
     4958 +// Creates an instance of the ref enqueueing gang
     4959 +// task and has the worker threads execute it.
     4960 +
     4961 +void G1STWRefProcTaskExecutor::execute(EnqueueTask& enq_task) {
     4962 +  assert(_workers != NULL, "Need parallel worker threads.");
     4963 +
     4964 +  G1STWRefEnqueueTaskProxy enq_task_proxy(enq_task);
     4965 +
     4966 +  _g1h->set_par_threads(_active_workers);
     4967 +  _workers->run_task(&enq_task_proxy);
     4968 +  _g1h->set_par_threads(0);
     4969 +}
     4970 +
     4971 +// End of weak reference support closures
     4972 +
     4973 +// Abstract task used to preserve (i.e. copy) any referent objects
     4974 +// that are in the collection set and are pointed to by reference
     4975 +// objects discovered by the CM ref processor.
     4976 +
     4977 +class G1ParPreserveCMReferentsTask: public AbstractGangTask {
     4978 +protected:
     4979 +  G1CollectedHeap* _g1h;
     4980 +  RefToScanQueueSet      *_queues;
     4981 +  ParallelTaskTerminator _terminator;
     4982 +  int _n_workers;
     4983 +
     4984 +public:
     4985 +  G1ParPreserveCMReferentsTask(G1CollectedHeap* g1h,int workers, RefToScanQueueSet *task_queues) :
     4986 +    AbstractGangTask("ParPreserveCMReferents"),
     4987 +    _g1h(g1h),
     4988 +    _queues(task_queues),
     4989 +    _terminator(workers, _queues),
     4990 +    _n_workers(workers)
     4991 +  { }
     4992 +
     4993 +  void work(int i) {
     4994 +    ResourceMark rm;
     4995 +    HandleMark   hm;
     4996 +
     4997 +    G1ParScanThreadState            pss(_g1h, i);
     4998 +    G1ParScanHeapEvacClosure        scan_evac_cl(_g1h, &pss, NULL);
     4999 +    G1ParScanHeapEvacFailureClosure evac_failure_cl(_g1h, &pss, NULL);
     5000 +    G1ParScanPartialArrayClosure    partial_scan_cl(_g1h, &pss, NULL);
     5001 +
     5002 +    pss.set_evac_closure(&scan_evac_cl);
     5003 +    pss.set_evac_failure_closure(&evac_failure_cl);
     5004 +    pss.set_partial_scan_closure(&partial_scan_cl);
     5005 +
     5006 +    assert(pss.refs()->is_empty(), "both queue and overflow should be empty");
     5007 +
     5008 +
     5009 +    G1ParScanExtRootClosure        only_copy_non_heap_cl(_g1h, &pss, NULL);
     5010 +    G1ParScanPermClosure           only_copy_perm_cl(_g1h, &pss, NULL);
     5011 +
     5012 +    G1ParScanAndMarkExtRootClosure copy_mark_non_heap_cl(_g1h, &pss, NULL);
     5013 +    G1ParScanAndMarkPermClosure    copy_mark_perm_cl(_g1h, &pss, NULL);
     5014 +
     5015 +    OopClosure*                    copy_non_heap_cl = &only_copy_non_heap_cl;
     5016 +    OopsInHeapRegionClosure*       copy_perm_cl = &only_copy_perm_cl;
     5017 +
     5018 +    if (_g1h->g1_policy()->during_initial_mark_pause()) {
     5019 +      // We also need to mark copied objects.
     5020 +      copy_non_heap_cl = &copy_mark_non_heap_cl;
     5021 +      copy_perm_cl = &copy_mark_perm_cl;
     5022 +    }
     5023 +
     5024 +    // Is alive closure
     5025 +    G1AlwaysAliveClosure always_alive(_g1h);
     5026 +
     5027 +    // Copying keep alive closure. Applied to referent objects that need
     5028 +    // to be copied.
     5029 +    G1CopyingKeepAliveClosure keep_alive(_g1h, copy_non_heap_cl, copy_perm_cl, &pss);
     5030 +
     5031 +    ReferenceProcessor* rp = _g1h->ref_processor_cm();
     5032 +
     5033 +    int limit = ReferenceProcessor::number_of_subclasses_of_ref() * rp->max_num_q();
     5034 +    int stride = MIN2(MAX2(_n_workers, 1), limit);
     5035 +
     5036 +    // limit is set using max_num_q() - which was set using ParallelGCThreads.
     5037 +    // So this must be true - but assert just in case someone decides to
     5038 +    // change the worker ids.
     5039 +    assert(0 <= i && i < limit, "sanity");
     5040 +    assert(!rp->discovery_is_atomic(), "check this code");
     5041 +
     5042 +    // Select discovered lists [i, i+stride, i+2*stride,...,limit)
     5043 +    for (int idx = i; idx < limit; idx += stride) {
     5044 +      DiscoveredList& ref_list = rp->discovered_soft_refs()[idx];
     5045 +
     5046 +      DiscoveredListIterator iter(ref_list, &keep_alive, &always_alive);
     5047 +      while (iter.has_next()) {
     5048 +        // Since discovery is not atomic for the CM ref processor, we
     5049 +        // can see some null referent objects.
     5050 +        iter.load_ptrs(DEBUG_ONLY(true));
     5051 +        oop ref = iter.obj();
     5052 +
     5053 +        // This will filter nulls.
     5054 +        if (iter.is_referent_alive()) {
     5055 +          iter.make_referent_alive();
     5056 +        }
     5057 +        iter.move_to_next();
     5058 +      }
     5059 +    }
     5060 +
     5061 +    // Drain the queue - which may cause stealing
     5062 +    G1ParEvacuateFollowersClosure drain_queue(_g1h, &pss, _queues, &_terminator);
     5063 +    drain_queue.do_void();
     5064 +    // Allocation buffers were retired at the end of G1ParEvacuateFollowersClosure
     5065 +    assert(pss.refs()->is_empty(), "should be");
     5066 +  }
     5067 +};
     5068 +
     5069 +// Weak Reference processing during an evacuation pause (part 1).
     5070 +void G1CollectedHeap::process_discovered_references() {
     5071 +  double ref_proc_start = os::elapsedTime();
     5072 +
     5073 +  ReferenceProcessor* rp = _ref_processor_stw;
     5074 +  assert(rp->discovery_enabled(), "should have been enabled");
     5075 +
     5076 +  // Any reference objects, in the collection set, that were 'discovered'
     5077 +  // by the CM ref processor should have already been copied (either by
     5078 +  // applying the external root copy closure to the discovered lists, or
     5079 +  // by following an RSet entry).
     5080 +  //
     5081 +  // But some of the referents, that are in the collection set, that these
     5082 +  // reference objects point to may not have been copied: the STW ref
     5083 +  // processor would have seen that the reference object had already
     5084 +  // been 'discovered' and would have skipped discovering the reference,
     5085 +  // but would not have treated the reference object as a regular oop.
     5086 +  // As a reult the copy closure would not have been applied to the
     5087 +  // referent object.
     5088 +  //
     5089 +  // We need to explicitly copy these referent objects - the references
     5090 +  // will be processed at the end of remarking.
     5091 +  //
     5092 +  // We also need to do this copying before we process the reference
     5093 +  // objects discovered by the STW ref processor in case one of these
     5094 +  // referents points to another object which is also referenced by an
     5095 +  // object discovered by the STW ref processor.
     5096 +
     5097 +  int n_workers = (G1CollectedHeap::use_parallel_gc_threads() ?
     5098 +                        workers()->total_workers() : 1);
     5099 +
     5100 +  set_par_threads(n_workers);
     5101 +  G1ParPreserveCMReferentsTask keep_cm_referents(this, n_workers, _task_queues);
     5102 +
     5103 +  if (G1CollectedHeap::use_parallel_gc_threads()) {
     5104 +    workers()->run_task(&keep_cm_referents);
     5105 +  } else {
     5106 +    keep_cm_referents.work(0);
     5107 +  }
     5108 +
     5109 +  set_par_threads(0);
     5110 +
     5111 +  // Closure to test whether a referent is alive.
     5112 +  G1STWIsAliveClosure is_alive(this);
     5113 +
     5114 +  // Even when parallel reference processing is enabled, the processing
     5115 +  // of JNI refs is serial and performed serially by the current thread
     5116 +  // rather than by a worker. The following PSS will be used for processing
     5117 +  // JNI refs.
     5118 +
     5119 +  // Use only a single queue for this PSS.
     5120 +  G1ParScanThreadState pss(this, 0);
     5121 +
     5122 +  // We do not embed a reference processor in the copying/scanning
     5123 +  // closures while we're actually processing the discovered
     5124 +  // reference objects.
     5125 +  G1ParScanHeapEvacClosure        scan_evac_cl(this, &pss, NULL);
     5126 +  G1ParScanHeapEvacFailureClosure evac_failure_cl(this, &pss, NULL);
     5127 +  G1ParScanPartialArrayClosure    partial_scan_cl(this, &pss, NULL);
     5128 +
     5129 +  pss.set_evac_closure(&scan_evac_cl);
     5130 +  pss.set_evac_failure_closure(&evac_failure_cl);
     5131 +  pss.set_partial_scan_closure(&partial_scan_cl);
     5132 +
     5133 +  assert(pss.refs()->is_empty(), "pre-condition");
     5134 +
     5135 +  G1ParScanExtRootClosure        only_copy_non_heap_cl(this, &pss, NULL);
     5136 +  G1ParScanPermClosure           only_copy_perm_cl(this, &pss, NULL);
     5137 +
     5138 +  G1ParScanAndMarkExtRootClosure copy_mark_non_heap_cl(this, &pss, NULL);
     5139 +  G1ParScanAndMarkPermClosure    copy_mark_perm_cl(this, &pss, NULL);
     5140 +
     5141 +  OopClosure*                    copy_non_heap_cl = &only_copy_non_heap_cl;
     5142 +  OopsInHeapRegionClosure*       copy_perm_cl = &only_copy_perm_cl;
     5143 +
     5144 +  if (_g1h->g1_policy()->during_initial_mark_pause()) {
     5145 +    // We also need to mark copied objects.
     5146 +    copy_non_heap_cl = &copy_mark_non_heap_cl;
     5147 +    copy_perm_cl = &copy_mark_perm_cl;
     5148 +  }
     5149 +
     5150 +  // Keep alive closure.
     5151 +  G1CopyingKeepAliveClosure keep_alive(this, copy_non_heap_cl, copy_perm_cl, &pss);
     5152 +
     5153 +  // Serial Complete GC closure
     5154 +  G1STWDrainQueueClosure drain_queue(this, &pss);
     5155 +
     5156 +  // Setup the soft refs policy...
     5157 +  rp->setup_policy(false);
     5158 +
     5159 +  if (!rp->processing_is_mt()) {
     5160 +    // Serial reference processing...
     5161 +    rp->process_discovered_references(&is_alive,
     5162 +                                      &keep_alive,
     5163 +                                      &drain_queue,
     5164 +                                      NULL);
     5165 +  } else {
     5166 +    // Parallel reference processing
     5167 +    int active_workers = (ParallelGCThreads > 0 ? workers()->total_workers() : 1);
     5168 +    assert(rp->num_q() == active_workers, "sanity");
     5169 +    assert(active_workers <= rp->max_num_q(), "sanity");
     5170 +
     5171 +    G1STWRefProcTaskExecutor par_task_executor(this, workers(), _task_queues, active_workers);
     5172 +    rp->process_discovered_references(&is_alive, &keep_alive, &drain_queue, &par_task_executor);
     5173 +  }
     5174 +
     5175 +  // We have completed copying any necessary live referent objects
     5176 +  // (that were not copied during the actual pause) so we can
     5177 +  // retire any active alloc buffers
     5178 +  pss.retire_alloc_buffers();
     5179 +  assert(pss.refs()->is_empty(), "both queue and overflow should be empty");
     5180 +
     5181 +  double ref_proc_time = os::elapsedTime() - ref_proc_start;
     5182 +  g1_policy()->record_ref_proc_time(ref_proc_time * 1000.0);
     5183 +}
     5184 +
     5185 +// Weak Reference processing during an evacuation pause (part 2).
     5186 +void G1CollectedHeap::enqueue_discovered_references() {
     5187 +  double ref_enq_start = os::elapsedTime();
     5188 +
     5189 +  ReferenceProcessor* rp = _ref_processor_stw;
     5190 +  assert(!rp->discovery_enabled(), "should have been disabled as part of processing");
     5191 +
     5192 +  // Now enqueue any remaining on the discovered lists on to
     5193 +  // the pending list.
     5194 +  if (!rp->processing_is_mt()) {
     5195 +    // Serial reference processing...
     5196 +    rp->enqueue_discovered_references();
     5197 +  } else {
     5198 +    // Parallel reference enqueuing
     5199 +
     5200 +    int active_workers = (ParallelGCThreads > 0 ? workers()->total_workers() : 1);
     5201 +    assert(rp->num_q() == active_workers, "sanity");
     5202 +    assert(active_workers <= rp->max_num_q(), "sanity");
     5203 +
     5204 +    G1STWRefProcTaskExecutor par_task_executor(this, workers(), _task_queues, active_workers);
     5205 +    rp->enqueue_discovered_references(&par_task_executor);
     5206 +  }
     5207 +
     5208 +  rp->verify_no_references_recorded();
     5209 +  assert(!rp->discovery_enabled(), "should have been disabled");
     5210 +
     5211 +  // FIXME
     5212 +  // CM's reference processing also cleans up the string and symbol tables.
     5213 +  // Should we do that here also? We could, but it is a serial operation
     5214 +  // and could signicantly increase the pause time.
     5215 +
     5216 +  double ref_enq_time = os::elapsedTime() - ref_enq_start;
     5217 +  g1_policy()->record_ref_enq_time(ref_enq_time * 1000.0);
     5218 +}
     5219 +
4644 5220  void G1CollectedHeap::evacuate_collection_set() {
4645 5221    set_evacuation_failed(false);
4646 5222  
4647 5223    g1_rem_set()->prepare_for_oops_into_collection_set_do();
4648 5224    concurrent_g1_refine()->set_use_cache(false);
4649 5225    concurrent_g1_refine()->clear_hot_cache_claimed_index();
4650 5226  
4651 5227    int n_workers = (ParallelGCThreads > 0 ? workers()->total_workers() : 1);
4652 5228    set_par_threads(n_workers);
4653 5229    G1ParTask g1_par_task(this, n_workers, _task_queues);
4654 5230  
4655 5231    init_for_evac_failure(NULL);
4656 5232  
4657 5233    rem_set()->prepare_for_younger_refs_iterate(true);
4658 5234  
4659 5235    assert(dirty_card_queue_set().completed_buffers_num() == 0, "Should be empty");
4660 5236    double start_par = os::elapsedTime();
     5237 +
4661 5238    if (G1CollectedHeap::use_parallel_gc_threads()) {
4662 5239      // The individual threads will set their evac-failure closures.
4663 5240      StrongRootsScope srs(this);
4664 5241      if (ParallelGCVerbose) G1ParScanThreadState::print_termination_stats_hdr();
4665 5242      workers()->run_task(&g1_par_task);
4666 5243    } else {
4667 5244      StrongRootsScope srs(this);
4668 5245      g1_par_task.work(0);
4669 5246    }
4670 5247  
4671 5248    double par_time = (os::elapsedTime() - start_par) * 1000.0;
4672 5249    g1_policy()->record_par_time(par_time);
4673 5250    set_par_threads(0);
4674 5251  
     5252 +  // Process any discovered reference objects - we have
     5253 +  // to do this _before_ we retire the GC alloc regions
     5254 +  // as we may have to copy some 'reachable' referent
     5255 +  // objects (and their reachable sub-graphs) that were
     5256 +  // not copied during the pause.
     5257 +  process_discovered_references();
     5258 +
4675 5259    // Weak root processing.
4676 5260    // Note: when JSR 292 is enabled and code blobs can contain
4677 5261    // non-perm oops then we will need to process the code blobs
4678 5262    // here too.
4679 5263    {
4680      -    G1IsAliveClosure is_alive(this);
     5264 +    G1STWIsAliveClosure is_alive(this);
4681 5265      G1KeepAliveClosure keep_alive(this);
4682 5266      JNIHandles::weak_oops_do(&is_alive, &keep_alive);
4683 5267    }
     5268 +
4684 5269    release_gc_alloc_regions();
4685 5270    g1_rem_set()->cleanup_after_oops_into_collection_set_do();
4686 5271  
4687 5272    concurrent_g1_refine()->clear_hot_cache();
4688 5273    concurrent_g1_refine()->set_use_cache(true);
4689 5274  
4690 5275    finalize_for_evac_failure();
4691 5276  
4692 5277    // Must do this before removing self-forwarding pointers, which clears
4693 5278    // the per-region evac-failure flags.
↓ open down ↓ 1 lines elided ↑ open up ↑
4695 5280  
4696 5281    if (evacuation_failed()) {
4697 5282      remove_self_forwarding_pointers();
4698 5283      if (PrintGCDetails) {
4699 5284        gclog_or_tty->print(" (to-space overflow)");
4700 5285      } else if (PrintGC) {
4701 5286        gclog_or_tty->print("--");
4702 5287      }
4703 5288    }
4704 5289  
     5290 +  // Enqueue any remaining references remaining on the STW
     5291 +  // reference processor's discovered lists. We need to do
     5292 +  // this after the card table is cleaned (and verified) as
     5293 +  // the act of enqueuing entries on to the pending list
     5294 +  // will log these updates (and dirty their associated
     5295 +  // cards). We need these updates logged to update any
     5296 +  // RSets.
     5297 +  enqueue_discovered_references();
     5298 +
4705 5299    if (G1DeferredRSUpdate) {
4706 5300      RedirtyLoggedCardTableEntryFastClosure redirty;
4707 5301      dirty_card_queue_set().set_closure(&redirty);
4708 5302      dirty_card_queue_set().apply_closure_to_all_completed_buffers();
4709 5303  
4710 5304      DirtyCardQueueSet& dcq = JavaThread::dirty_card_queue_set();
4711 5305      dcq.merge_bufferlists(&dirty_card_queue_set());
4712 5306      assert(dirty_card_queue_set().completed_buffers_num() == 0, "All should be consumed");
4713 5307    }
4714 5308    COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
↓ open down ↓ 180 lines elided ↑ open up ↑
4895 5489        _dirty_cards_region_list = r->get_next_dirty_cards_region();
4896 5490        if (_dirty_cards_region_list == r) {
4897 5491          // The last region.
4898 5492          _dirty_cards_region_list = NULL;
4899 5493        }
4900 5494        r->set_next_dirty_cards_region(NULL);
4901 5495      }
4902 5496    }
4903 5497  
4904 5498    double elapsed = os::elapsedTime() - start;
4905      -  g1_policy()->record_clear_ct_time( elapsed * 1000.0);
     5499 +  g1_policy()->record_clear_ct_time(elapsed * 1000.0);
4906 5500  #ifndef PRODUCT
4907 5501    if (G1VerifyCTCleanup || VerifyAfterGC) {
4908 5502      G1VerifyCardTableCleanup cleanup_verifier(this, ct_bs);
4909 5503      heap_region_iterate(&cleanup_verifier);
4910 5504    }
4911 5505  #endif
4912 5506  }
4913 5507  
4914 5508  void G1CollectedHeap::free_collection_set(HeapRegion* cs_head) {
4915 5509    size_t pre_used = 0;
↓ open down ↓ 457 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX