1 /*
   2  * Copyright (c) 2016, 2019, 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 #ifndef SHARE_GC_G1_G1COLLECTIONSET_HPP
  26 #define SHARE_GC_G1_G1COLLECTIONSET_HPP
  27 
  28 #include "utilities/debug.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 
  31 class G1CollectedHeap;
  32 class G1CollectionSetCandidates;
  33 class G1CollectorState;
  34 class G1GCPhaseTimes;
  35 class G1ParScanThreadStateSet;
  36 class G1Policy;
  37 class G1SurvivorRegions;
  38 class HeapRegion;
  39 class HeapRegionClaimer;
  40 class HeapRegionClosure;
  41 
  42 // The collection set.
  43 //
  44 // The set of regions that are evacuated during an evacuation pause.
  45 //
  46 // At the end of a collection, before freeing the collection set, this set
  47 // contains all regions that were evacuated during this collection:
  48 //
  49 // - survivor regions from the last collection (if any)
  50 // - eden regions allocated by the mutator
  51 // - old gen regions evacuated during mixed gc
  52 //
  53 // This set is built incrementally at mutator time as regions are retired, and
  54 // if this had been a mixed gc, some additional (during gc) incrementally added
  55 // old regions from the collection set candidates built during the concurrent
  56 // cycle.
  57 //
  58 // A more detailed overview of how the collection set changes over time follows:
  59 //
  60 // 0) at the end of GC the survivor regions are added to this collection set.
  61 // 1) the mutator incrementally adds eden regions as they retire
  62 //
  63 // ----- gc starts
  64 //
  65 // 2) prepare (finalize) young regions of the collection set for collection
  66 //    - relabel the survivors as eden
  67 //    - finish up the incremental building that happened at mutator time
  68 //
  69 // iff this is a young-only collection:
  70 //
  71 // a3) evacuate the current collection set in one "initial evacuation" phase
  72 //
  73 // iff this is a mixed collection:
  74 //
  75 // b3) calculate the set of old gen regions we may be able to collect in this
  76 //     collection from the list of collection set candidates.
  77 //     - one part is added to the current collection set
  78 //     - the remainder regions are labeled as optional, and NOT yet added to the
  79 //     collection set.
  80 // b4) evacuate the current collection set in the "initial evacuation" phase
  81 // b5) evacuate the optional regions in the "optional evacuation" phase. This is
  82 //     done in increments (or rounds).
  83 //     b5-1) add a few of the optional regions to the current collection set
  84 //     b5-2) evacuate only these newly added optional regions. For this mechanism we
  85 //     reuse the incremental collection set building infrastructure (used also at
  86 //     mutator time).
  87 //     b5-3) repeat from b5-1 until the policy determines we are done
  88 //
  89 // all collections
  90 //
  91 // 6) free the collection set (contains all regions now; empties collection set
  92 //    afterwards)
  93 // 7) add survivors to this collection set
  94 //
  95 // ----- gc ends
  96 //
  97 // goto 1)
  98 //
  99 // Examples of how the collection set might look over time:
 100 //
 101 // Legend:
 102 // S = survivor, E = eden, O = old.
 103 // |xxxx| = increment (with increment markers), containing four regions
 104 //
 105 // |SSSS|                         ... after step 0), with four survivor regions
 106 // |SSSSEE|                       ... at step 1), after retiring two eden regions
 107 // |SSSSEEEE|                     ... after step 1), after retiring four eden regions
 108 // |EEEEEEEE|                     ... after step 2)
 109 //
 110 // iff this is a young-only collection
 111 //
 112 // EEEEEEEE||                      ... after step a3), after initial evacuation phase
 113 // ||                              ... after step 6)
 114 // |SS|                            ... after step 7), with two survivor regions
 115 //
 116 // iff this is a mixed collection
 117 //
 118 // |EEEEEEEEOOOO|                  ... after step b3), added four regions to be
 119 //                                     evacuated in the "initial evacuation" phase
 120 // EEEEEEEEOOOO||                  ... after step b4), incremental part is empty
 121 //                                     after evacuation
 122 // EEEEEEEEOOOO|OO|                ... after step b5.1), added two regions to be
 123 //                                     evacuated in the first round of the
 124 //                                     "optional evacuation" phase
 125 // EEEEEEEEOOOOOO|O|               ... after step b5.1), added one region to be
 126 //                                     evacuated in the second round of the
 127 //                                     "optional evacuation" phase
 128 // EEEEEEEEOOOOOOO||               ... after step b5), the complete collection set.
 129 // ||                              ... after step b6)
 130 // |SSS|                           ... after step 7), with three survivor regions
 131 //
 132 class G1CollectionSet {
 133   G1CollectedHeap* _g1h;
 134   G1Policy* _policy;
 135 
 136   // All old gen collection set candidate regions for the current mixed phase.
 137   G1CollectionSetCandidates* _candidates;
 138 
 139   uint _eden_region_length;
 140   uint _survivor_region_length;
 141   uint _old_region_length;
 142 
 143   // The actual collection set as a set of region indices.
 144   // All entries in _collection_set_regions below _collection_set_cur_length are
 145   // assumed to be part of the collection set.
 146   // We assume that at any time there is at most only one writer and (one or more)
 147   // concurrent readers. This means we are good with using storestore and loadload
 148   // barriers on the writer and reader respectively only.
 149   uint* _collection_set_regions;
 150   volatile size_t _collection_set_cur_length;
 151   size_t _collection_set_max_length;
 152 
 153   // When doing mixed collections we can add old regions to the collection set, which
 154   // will be collected only if there is enough time. We call these optional regions.
 155   // This member records the current number of regions that are of that type that
 156   // correspond to the first x entries in the collection set candidates.
 157   uint _num_optional_regions;
 158 
 159   // The number of bytes in the collection set before the pause. Set from
 160   // the incrementally built collection set at the start of an evacuation
 161   // pause, and updated as more regions are added to the collection set.
 162   size_t _bytes_used_before;
 163 
 164   // The number of cards in the remembered set in the collection set. Set from
 165   // the incrementally built collection set at the start of an evacuation
 166   // pause, and updated as more regions are added to the collection set.
 167   size_t _recorded_rs_length;
 168 
 169   enum CSetBuildType {
 170     Active,             // We are actively building the collection set
 171     Inactive            // We are not actively building the collection set
 172   };
 173 
 174   CSetBuildType _inc_build_state;
 175   size_t _inc_part_start;
 176 
 177   // Information about eden regions in the incremental collection set.
 178   struct IncCollectionSetRegionStat {
 179     // The predicted time without copy time that was added to total value
 180     // for the collection set.
 181     double _non_copy_time_ms;
 182     // The remembered set length that was added to the total value
 183     // for the collection set.
 184     size_t _rs_length;
 185   };
 186 
 187   IncCollectionSetRegionStat* _inc_collection_set_stats;
 188   // The associated information that is maintained while the incremental
 189   // collection set is being built with *young* regions. Used to populate
 190   // the recorded info for the evacuation pause.
 191 
 192   // The number of bytes in the incrementally built collection set.
 193   // Used to set _collection_set_bytes_used_before at the start of
 194   // an evacuation pause.
 195   size_t _inc_bytes_used_before;
 196 
 197   // The RSet lengths recorded for regions in the CSet. It is updated
 198   // by the thread that adds a new region to the CSet. We assume that
 199   // only one thread can be allocating a new CSet region (currently,
 200   // it does so after taking the Heap_lock) hence no need to
 201   // synchronize updates to this field.
 202   size_t _inc_recorded_rs_length;
 203 
 204   // A concurrent refinement thread periodically samples the young
 205   // region RSets and needs to update _inc_recorded_rs_length as
 206   // the RSets grow. Instead of having to synchronize updates to that
 207   // field we accumulate them in this field and add it to
 208   // _inc_recorded_rs_length_diff at the start of a GC.
 209   size_t _inc_recorded_rs_length_diff;
 210 
 211   // The predicted elapsed time it will take to collect the regions in
 212   // the CSet. This is updated by the thread that adds a new region to
 213   // the CSet. See the comment for _inc_recorded_rs_length about
 214   // MT-safety assumptions.
 215   double _inc_predicted_non_copy_time_ms;
 216 
 217   // See the comment for _inc_recorded_rs_length_diff.
 218   double _inc_predicted_non_copy_time_ms_diff;
 219 
 220   void set_recorded_rs_length(size_t rs_length);
 221 
 222   G1CollectorState* collector_state() const;
 223   G1GCPhaseTimes* phase_times();
 224 
 225   void verify_young_cset_indices() const NOT_DEBUG_RETURN;
 226 
 227   double predict_region_non_copy_time_ms(HeapRegion* hr) const;
 228 
 229   // Update the incremental collection set information when adding a region.
 230   void add_young_region_common(HeapRegion* hr);
 231 
 232   // Add old region "hr" to the collection set.
 233   void add_old_region(HeapRegion* hr);
 234   void free_optional_regions();
 235 
 236   // Add old region "hr" to optional collection set.
 237   void add_optional_region(HeapRegion* hr);
 238 
 239   void move_candidates_to_collection_set(uint num_regions);
 240 
 241   // Finalize the young part of the initial collection set. Relabel survivor regions
 242   // as Eden and calculate a prediction on how long the evacuation of all young regions
 243   // will take.
 244   double finalize_young_part(double target_pause_time_ms, G1SurvivorRegions* survivors);
 245   // Perform any final calculations on the incremental collection set fields before we
 246   // can use them.
 247   void finalize_incremental_building();
 248 
 249   // Select the old regions of the initial collection set and determine how many optional
 250   // regions we might be able to evacuate in this pause.
 251   void finalize_old_part(double time_remaining_ms);
 252 public:
 253   G1CollectionSet(G1CollectedHeap* g1h, G1Policy* policy);
 254   ~G1CollectionSet();
 255 
 256   // Initializes the collection set giving the maximum possible length of the collection set.
 257   void initialize(uint max_region_length);
 258 
 259   void clear_candidates();
 260 
 261   void set_candidates(G1CollectionSetCandidates* candidates) {
 262     assert(_candidates == NULL, "Trying to replace collection set candidates.");
 263     _candidates = candidates;
 264   }
 265   G1CollectionSetCandidates* candidates() { return _candidates; }
 266 
 267   void init_region_lengths(uint eden_cset_region_length,
 268                            uint survivor_cset_region_length);
 269 
 270   uint region_length() const       { return young_region_length() +
 271                                             old_region_length(); }
 272   uint young_region_length() const { return eden_region_length() +
 273                                             survivor_region_length(); }
 274 
 275   uint eden_region_length() const     { return _eden_region_length;     }
 276   uint survivor_region_length() const { return _survivor_region_length; }
 277   uint old_region_length() const      { return _old_region_length;      }
 278   uint optional_region_length() const { return _num_optional_regions; }
 279 
 280   // Reset the contents of the collection set.
 281   void clear();
 282 
 283   // Incremental collection set support
 284 
 285   // Initialize incremental collection set info.
 286   void start_incremental_building();
 287   // Start a new collection set increment.
 288   void update_incremental_marker() { _inc_build_state = Active; _inc_part_start = _collection_set_cur_length; }
 289   // Stop adding regions to the current collection set increment.
 290   void stop_incremental_building() { _inc_build_state = Inactive; }
 291 
 292   // Iterate over the current collection set increment applying the given HeapRegionClosure
 293   // from a starting position determined by the given worker id.
 294   void iterate_incremental_part_from(HeapRegionClosure* cl, HeapRegionClaimer* hr_claimer, uint worker_id, uint total_workers) const;
 295 
 296   // Returns the length of the current increment in number of regions.
 297   size_t increment_length() const { return _collection_set_cur_length - _inc_part_start; }
 298   // Returns the length of the whole current collection set in number of regions
 299   size_t cur_length() const { return _collection_set_cur_length; }
 300 
 301   // Iterate over the entire collection set (all increments calculated so far), applying
 302   // the given HeapRegionClosure on all of them.
 303   void iterate(HeapRegionClosure* cl) const;
 304 
 305   void iterate_optional(HeapRegionClosure* cl) const;
 306 
 307   size_t recorded_rs_length() { return _recorded_rs_length; }
 308 
 309   size_t bytes_used_before() const {
 310     return _bytes_used_before;
 311   }
 312 
 313   void reset_bytes_used_before() {
 314     _bytes_used_before = 0;
 315   }
 316 
 317   // Finalize the initial collection set consisting of all young regions potentially a
 318   // few old gen regions.
 319   void finalize_initial_collection_set(double target_pause_time_ms, G1SurvivorRegions* survivor);
 320   // Finalize the next collection set from the set of available optional old gen regions.
 321   bool finalize_optional_for_evacuation(double remaining_pause_time);
 322   // Abandon (clean up) optional collection set regions that were not evacuated in this
 323   // pause.
 324   void abandon_optional_collection_set(G1ParScanThreadStateSet* pss);
 325 
 326   // Update information about hr in the aggregated information for
 327   // the incrementally built collection set.
 328   void update_young_region_prediction(HeapRegion* hr, size_t new_rs_length);
 329 
 330   // Add eden region to the collection set.
 331   void add_eden_region(HeapRegion* hr);
 332 
 333   // Add survivor region to the collection set.
 334   void add_survivor_regions(HeapRegion* hr);
 335 
 336 #ifndef PRODUCT
 337   bool verify_young_ages();
 338 
 339   void print(outputStream* st);
 340 #endif // !PRODUCT
 341 };
 342 
 343 #endif // SHARE_GC_G1_G1COLLECTIONSET_HPP