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 "memory/allocation.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 
  32 class G1CollectedHeap;
  33 class G1CollectorPolicy;
  34 class G1CollectorState;
  35 class G1GCPhaseTimes;
  36 class HeapRegion;
  37 
  38 class G1CollectionSet : public CHeapObj<mtGC> {
  39   G1CollectedHeap* _g1;
  40   G1CollectorPolicy* _g1p;
  41 
  42   CollectionSetChooser* _cset_chooser;
  43 
  44   uint _eden_region_length;
  45   uint _survivor_region_length;
  46   uint _old_region_length;
  47 
  48   // The head of the list (via "next_in_collection_set()") representing the
  49   // current collection set. Set from the incrementally built collection
  50   // set at the start of the pause.
  51   HeapRegion* _head;
  52 
  53   // The number of bytes in the collection set before the pause. Set from
  54   // the incrementally built collection set at the start of an evacuation
  55   // pause, and incremented in finalize_old_cset_part() when adding old regions
  56   // (if any) to the collection set.
  57   size_t _bytes_used_before;
  58 
  59   size_t _recorded_rs_lengths;
  60 
  61   // The associated information that is maintained while the incremental
  62   // collection set is being built with young regions. Used to populate
  63   // the recorded info for the evacuation pause.
  64 
  65   enum CSetBuildType {
  66     Active,             // We are actively building the collection set
  67     Inactive            // We are not actively building the collection set
  68   };
  69 
  70   CSetBuildType _inc_build_state;
  71 
  72   // The head of the incrementally built collection set.
  73   HeapRegion* _inc_head;
  74 
  75   // The tail of the incrementally built collection set.
  76   HeapRegion* _inc_tail;
  77 
  78   // The number of bytes in the incrementally built collection set.
  79   // Used to set _collection_set_bytes_used_before at the start of
  80   // an evacuation pause.
  81   size_t _inc_bytes_used_before;
  82 
  83   // The RSet lengths recorded for regions in the CSet. It is updated
  84   // by the thread that adds a new region to the CSet. We assume that
  85   // only one thread can be allocating a new CSet region (currently,
  86   // it does so after taking the Heap_lock) hence no need to
  87   // synchronize updates to this field.
  88   size_t _inc_recorded_rs_lengths;
  89 
  90   // A concurrent refinement thread periodically samples the young
  91   // region RSets and needs to update _inc_recorded_rs_lengths as
  92   // the RSets grow. Instead of having to synchronize updates to that
  93   // field we accumulate them in this field and add it to
  94   // _inc_recorded_rs_lengths_diffs at the start of a GC.
  95   ssize_t _inc_recorded_rs_lengths_diffs;
  96 
  97   // The predicted elapsed time it will take to collect the regions in
  98   // the CSet. This is updated by the thread that adds a new region to
  99   // the CSet. See the comment for _inc_recorded_rs_lengths about
 100   // MT-safety assumptions.
 101   double _inc_predicted_elapsed_time_ms;
 102 
 103   // See the comment for _inc_recorded_rs_lengths_diffs.
 104   double _inc_predicted_elapsed_time_ms_diffs;
 105 
 106   G1CollectorState* collector_state();
 107   G1GCPhaseTimes* phase_times();
 108 
 109   double predict_region_elapsed_time_ms(HeapRegion* hr);
 110 
 111 public:
 112   G1CollectionSet(G1CollectedHeap* g1h, G1CollectorPolicy* g1p);
 113   ~G1CollectionSet();
 114 
 115   CollectionSetChooser* cset_chooser();
 116 
 117   void init_region_lengths(uint eden_cset_region_length,
 118                            uint survivor_cset_region_length);
 119 
 120   void set_recorded_rs_lengths(size_t rs_lengths);
 121 
 122   uint region_length() const       { return young_region_length() +
 123                                             old_region_length(); }
 124   uint young_region_length() const { return eden_region_length() +
 125                                             survivor_region_length(); }
 126 
 127   uint eden_region_length() const     { return _eden_region_length;     }
 128   uint survivor_region_length() const { return _survivor_region_length; }
 129   uint old_region_length() const      { return _old_region_length;      }
 130 
 131   // Incremental CSet Support
 132 
 133   // The head of the incrementally built collection set.
 134   HeapRegion* inc_head() { return _inc_head; }
 135 
 136   // The tail of the incrementally built collection set.
 137   HeapRegion* inc_tail() { return _inc_tail; }
 138 
 139   // Initialize incremental collection set info.
 140   void start_incremental_building();
 141 
 142   // Perform any final calculations on the incremental CSet fields
 143   // before we can use them.
 144   void finalize_incremental_building();
 145 
 146   void clear_incremental() {
 147     _inc_head = NULL;
 148     _inc_tail = NULL;
 149   }
 150 
 151   // Stop adding regions to the incremental collection set
 152   void stop_incremental_building() { _inc_build_state = Inactive; }
 153 
 154   // The head of the list (via "next_in_collection_set()") representing the
 155   // current collection set.
 156   HeapRegion* head() { return _head; }
 157 
 158   void clear_head() { _head = NULL; }
 159 
 160   size_t recorded_rs_lengths() { return _recorded_rs_lengths; }
 161 
 162   size_t bytes_used_before() const {
 163     return _bytes_used_before;
 164   }
 165 
 166   void reset_bytes_used_before() {
 167     _bytes_used_before = 0;
 168   }
 169 
 170   // Choose a new collection set.  Marks the chosen regions as being
 171   // "in_collection_set", and links them together.  The head and number of
 172   // the collection set are available via access methods.
 173   double finalize_young_part(double target_pause_time_ms);
 174   // XXX MGFIXME detgc!
 175   virtual void finalize_old_part(double time_remaining_ms);
 176 
 177   // Add old region "hr" to the CSet.
 178   void add_old_region(HeapRegion* hr);
 179 
 180   // Update information about hr in the aggregated information for
 181   // the incrementally built collection set.
 182   void update_young_region_prediction(HeapRegion* hr, size_t new_rs_length);
 183 
 184   // Add hr to the LHS of the incremental collection set.
 185   void add_eden_region(HeapRegion* hr);
 186 
 187   // Add hr to the RHS of the incremental collection set.
 188   void add_survivor_regions(HeapRegion* hr);
 189 
 190 #ifndef PRODUCT
 191   void print(HeapRegion* list_head, outputStream* st);
 192 #endif // !PRODUCT
 193 
 194 private:
 195   // Update the incremental cset information when adding a region
 196   // (should not be called directly).
 197   void add_young_region_common(HeapRegion* hr);
 198 
 199 };
 200 
 201 #endif // SHARE_VM_GC_G1_G1COLLECTIONSET_HPP
 202