1 /*
   2  * Copyright (c) 2016, 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_VM_GC_G1_G1COLLECTIONSET_HPP
  26 #define SHARE_VM_GC_G1_G1COLLECTIONSET_HPP
  27 
  28 #include "gc/g1/collectionSetChooser.hpp"
  29 #include "gc/g1/g1FixedSizeStack.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "utilities/debug.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 
  34 class G1CollectedHeap;
  35 class G1CollectorState;
  36 class G1GCPhaseTimes;
  37 class G1Policy;
  38 class G1SurvivorRegions;
  39 class HeapRegion;
  40 
  41 class G1CollectionSet VALUE_OBJ_CLASS_SPEC {
  42   G1CollectedHeap* _g1;
  43   G1Policy* _policy;
  44 
  45   CollectionSetChooser* _cset_chooser;
  46 
  47   uint _eden_region_length;
  48   uint _survivor_region_length;
  49   uint _old_region_length;
  50 
  51   // The actual collection set as a set of region indices.
  52   G1FixedSizeStack<uint> _collection_set_regions;
  53 
  54   // The number of bytes in the collection set before the pause. Set from
  55   // the incrementally built collection set at the start of an evacuation
  56   // pause, and incremented in finalize_old_part() when adding old regions
  57   // (if any) to the collection set.
  58   size_t _bytes_used_before;
  59 
  60   size_t _recorded_rs_lengths;
  61 
  62   // The associated information that is maintained while the incremental
  63   // collection set is being built with young regions. Used to populate
  64   // the recorded info for the evacuation pause.
  65 
  66   enum CSetBuildType {
  67     Active,             // We are actively building the collection set
  68     Inactive            // We are not actively building the collection set
  69   };
  70 
  71   CSetBuildType _inc_build_state;
  72  
  73   // The number of bytes in the incrementally built collection set.
  74   // Used to set _collection_set_bytes_used_before at the start of
  75   // an evacuation pause.
  76   size_t _inc_bytes_used_before;
  77 
  78   // The RSet lengths recorded for regions in the CSet. It is updated
  79   // by the thread that adds a new region to the CSet. We assume that
  80   // only one thread can be allocating a new CSet region (currently,
  81   // it does so after taking the Heap_lock) hence no need to
  82   // synchronize updates to this field.
  83   size_t _inc_recorded_rs_lengths;
  84 
  85   // A concurrent refinement thread periodically samples the young
  86   // region RSets and needs to update _inc_recorded_rs_lengths as
  87   // the RSets grow. Instead of having to synchronize updates to that
  88   // field we accumulate them in this field and add it to
  89   // _inc_recorded_rs_lengths_diffs at the start of a GC.
  90   ssize_t _inc_recorded_rs_lengths_diffs;
  91 
  92   // The predicted elapsed time it will take to collect the regions in
  93   // the CSet. This is updated by the thread that adds a new region to
  94   // the CSet. See the comment for _inc_recorded_rs_lengths about
  95   // MT-safety assumptions.
  96   double _inc_predicted_elapsed_time_ms;
  97 
  98   // See the comment for _inc_recorded_rs_lengths_diffs.
  99   double _inc_predicted_elapsed_time_ms_diffs;
 100 
 101   G1CollectorState* collector_state();
 102   G1GCPhaseTimes* phase_times();
 103 
 104   double predict_region_elapsed_time_ms(HeapRegion* hr);
 105 
 106   void verify_young_cset_indices() const NOT_DEBUG_RETURN;
 107 public:
 108   G1CollectionSet(G1CollectedHeap* g1h, G1Policy* policy);
 109   ~G1CollectionSet();
 110 
 111   // Set the maximum possible length of the collection set.
 112   void set_max_length(uint max_region_length);
 113 
 114   CollectionSetChooser* cset_chooser();
 115 
 116   void init_region_lengths(uint eden_cset_region_length,
 117                            uint survivor_cset_region_length);
 118 
 119   void set_recorded_rs_lengths(size_t rs_lengths);
 120 
 121   uint region_length() const       { return young_region_length() +
 122                                             old_region_length(); }
 123   uint young_region_length() const { return eden_region_length() +
 124                                             survivor_region_length(); }
 125 
 126   uint eden_region_length() const     { return _eden_region_length;     }
 127   uint survivor_region_length() const { return _survivor_region_length; }
 128   uint old_region_length() const      { return _old_region_length;      }
 129 
 130   // Incremental collection set Support
 131 
 132   // Initialize incremental collection set info.
 133   void start_incremental_building();
 134 
 135   // Perform any final calculations on the incremental collection set fields
 136   // before we can use them.
 137   void finalize_incremental_building();
 138 
 139   // Reset the contents of the collection set.
 140   void clear() {
 141     _collection_set_regions.clear();
 142   }
 143 
 144   // Iterate over the collection set, applying the given HeapRegionClosure on all of them.
 145   void iterate(HeapRegionClosure* cl);
 146 
 147   // Iterate over the collection set, applying the given HeapRegionClosure on all of them,
 148   // trying to optimally spread out starting position of total_workers workers given the
 149   // caller's worker_id.
 150   // If may_be_aborted is true, iteration may be aborted using the return value of the
 151   // called closure method.
 152   void iterate_from(HeapRegionClosure* cl, uint worker_id, uint total_workers, bool may_be_aborted = false);
 153   
 154   // Stop adding regions to the incremental collection set.
 155   void stop_incremental_building() { _inc_build_state = Inactive; }
 156 
 157   size_t recorded_rs_lengths() { return _recorded_rs_lengths; }
 158 
 159   size_t bytes_used_before() const {
 160     return _bytes_used_before;
 161   }
 162 
 163   void reset_bytes_used_before() {
 164     _bytes_used_before = 0;
 165   }
 166 
 167   // Choose a new collection set.  Marks the chosen regions as being
 168   // "in_collection_set".
 169   double finalize_young_part(double target_pause_time_ms, G1SurvivorRegions* survivors);
 170   void finalize_old_part(double time_remaining_ms);
 171 
 172   // Add old region "hr" to the collection set.
 173   void add_old_region(HeapRegion* hr);
 174 
 175   // Update information about hr in the aggregated information for
 176   // the incrementally built collection set.
 177   void update_young_region_prediction(HeapRegion* hr, size_t new_rs_length);
 178 
 179   // Add eden region to the collection set.
 180   void add_eden_region(HeapRegion* hr);
 181 
 182   // Add survivor region to the collection set.
 183   void add_survivor_regions(HeapRegion* hr);
 184 
 185 #ifndef PRODUCT
 186   bool verify_young_ages();
 187 
 188   void print(outputStream* st);
 189 #endif // !PRODUCT
 190 
 191 private:
 192   // Update the incremental collection set information when adding a region.
 193   void add_young_region_common(HeapRegion* hr);
 194 };
 195 
 196 #endif // SHARE_VM_GC_G1_G1COLLECTIONSET_HPP
 197