1 /*
   2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_GC_G1_COLLECTIONSETCHOOSER_HPP
  26 #define SHARE_VM_GC_G1_COLLECTIONSETCHOOSER_HPP
  27 
  28 #include "gc/g1/heapRegion.hpp"
  29 #include "utilities/growableArray.hpp"
  30 
  31 class CollectionSetChooser: public CHeapObj<mtGC> {
  32 
  33   GrowableArray<HeapRegion*> _regions;
  34 
  35   // Unfortunately, GrowableArray uses ints for length and indexes. To
  36   // avoid excessive casting in the rest of the class the following
  37   // wrapper methods are provided that use uints.
  38 
  39   uint regions_length()          { return (uint) _regions.length(); }
  40   HeapRegion* regions_at(uint i) { return _regions.at((int) i);     }
  41   void regions_at_put(uint i, HeapRegion* hr) {
  42     _regions.at_put((int) i, hr);
  43   }
  44   void regions_at_put_grow(uint i, HeapRegion* hr) {
  45     _regions.at_put_grow((int) i, hr);
  46   }
  47   void regions_trunc_to(uint i)  { _regions.trunc_to((uint) i); }
  48 
  49   // The index of the next candidate old region to be considered for
  50   // addition to the CSet.
  51   uint _curr_index;
  52 
  53   // The number of candidate old regions added to the CSet chooser.
  54   // Note: this is not updated when removing a region using
  55   // remove_and_move_to_next() below.
  56   uint _length;
  57 
  58   // Keeps track of the start of the next array chunk to be claimed by
  59   // parallel GC workers.
  60   uint _first_par_unreserved_idx;
  61 
  62   // If a region has more live bytes than this threshold, it will not
  63   // be added to the CSet chooser and will not be a candidate for
  64   // collection.
  65   size_t _region_live_threshold_bytes;
  66 
  67   // The sum of reclaimable bytes over all the regions in the CSet chooser.
  68   size_t _remaining_reclaimable_bytes;
  69 
  70 public:
  71 
  72   // Return the current candidate region to be considered for
  73   // collection without removing it from the CSet chooser.
  74   HeapRegion* peek() {
  75     HeapRegion* res = NULL;
  76     if (_curr_index < _length) {
  77       res = regions_at(_curr_index);
  78       assert(res != NULL,
  79              err_msg("Unexpected NULL hr in _regions at index %u",
  80                      _curr_index));
  81     }
  82     return res;
  83   }
  84 
  85   // Remove the given region from the CSet chooser and move to the
  86   // next one. The given region should be the current candidate region
  87   // in the CSet chooser.
  88   void remove_and_move_to_next(HeapRegion* hr) {
  89     assert(hr != NULL, "pre-condition");
  90     assert(_curr_index < _length, "pre-condition");
  91     assert(regions_at(_curr_index) == hr, "pre-condition");
  92     regions_at_put(_curr_index, NULL);
  93     assert(hr->reclaimable_bytes() <= _remaining_reclaimable_bytes,
  94            err_msg("remaining reclaimable bytes inconsistent "
  95                    "from region: "SIZE_FORMAT" remaining: "SIZE_FORMAT,
  96                    hr->reclaimable_bytes(), _remaining_reclaimable_bytes));
  97     _remaining_reclaimable_bytes -= hr->reclaimable_bytes();
  98     _curr_index += 1;
  99   }
 100 
 101   CollectionSetChooser();
 102 
 103   void sort_regions();
 104 
 105   // Determine whether to add the given region to the CSet chooser or
 106   // not. Currently, we skip pinned regions and regions whose live 
 107   // bytes are over the threshold. Humongous regions may be reclaimed during cleanup.
 108   bool should_add(HeapRegion* hr) {
 109     assert(hr->is_marked(), "pre-condition");
 110     assert(!hr->is_young(), "should never consider young regions");
 111     return !hr->is_pinned() &&
 112             hr->live_bytes() < _region_live_threshold_bytes;
 113   }
 114 
 115   // Returns the number candidate old regions added
 116   uint length() { return _length; }
 117 
 118   // Serial version.
 119   void add_region(HeapRegion *hr);
 120 
 121   // Must be called before calls to claim_array_chunk().
 122   // n_regions is the number of regions, chunk_size the chunk size.
 123   void prepare_for_par_region_addition(uint n_threads, uint n_regions, uint chunk_size);
 124   // Returns the first index in a contiguous chunk of chunk_size indexes
 125   // that the calling thread has reserved.  These must be set by the
 126   // calling thread using set_region() (to NULL if necessary).
 127   uint claim_array_chunk(uint chunk_size);
 128   // Set the marked array entry at index to hr.  Careful to claim the index
 129   // first if in parallel.
 130   void set_region(uint index, HeapRegion* hr);
 131   // Atomically increment the number of added regions by region_num
 132   // and the amount of reclaimable bytes by reclaimable_bytes.
 133   void update_totals(uint region_num, size_t reclaimable_bytes);
 134 
 135   void clear();
 136 
 137   // Return the number of candidate regions that remain to be collected.
 138   uint remaining_regions() { return _length - _curr_index; }
 139 
 140   // Determine whether the CSet chooser has more candidate regions or not.
 141   bool is_empty() { return remaining_regions() == 0; }
 142 
 143   // Return the reclaimable bytes that remain to be collected on
 144   // all the candidate regions in the CSet chooser.
 145   size_t remaining_reclaimable_bytes() { return _remaining_reclaimable_bytes; }
 146 
 147   // Returns true if the used portion of "_regions" is properly
 148   // sorted, otherwise asserts false.
 149   void verify() PRODUCT_RETURN;
 150 };
 151 
 152 class CSetChooserParUpdater : public StackObj {
 153 private:
 154   CollectionSetChooser* _chooser;
 155   bool _parallel;
 156   uint _chunk_size;
 157   uint _cur_chunk_idx;
 158   uint _cur_chunk_end;
 159   uint _regions_added;
 160   size_t _reclaimable_bytes_added;
 161 
 162 public:
 163   CSetChooserParUpdater(CollectionSetChooser* chooser,
 164                         bool parallel, uint chunk_size) :
 165     _chooser(chooser), _parallel(parallel), _chunk_size(chunk_size),
 166     _cur_chunk_idx(0), _cur_chunk_end(0),
 167     _regions_added(0), _reclaimable_bytes_added(0) { }
 168 
 169   ~CSetChooserParUpdater() {
 170     if (_parallel && _regions_added > 0) {
 171       _chooser->update_totals(_regions_added, _reclaimable_bytes_added);
 172     }
 173   }
 174 
 175   void add_region(HeapRegion* hr) {
 176     if (_parallel) {
 177       if (_cur_chunk_idx == _cur_chunk_end) {
 178         _cur_chunk_idx = _chooser->claim_array_chunk(_chunk_size);
 179         _cur_chunk_end = _cur_chunk_idx + _chunk_size;
 180       }
 181       assert(_cur_chunk_idx < _cur_chunk_end, "invariant");
 182       _chooser->set_region(_cur_chunk_idx, hr);
 183       _cur_chunk_idx += 1;
 184     } else {
 185       _chooser->add_region(hr);
 186     }
 187     _regions_added += 1;
 188     _reclaimable_bytes_added += hr->reclaimable_bytes();
 189   }
 190 
 191   bool should_add(HeapRegion* hr) { return _chooser->should_add(hr); }
 192 };
 193 
 194 #endif // SHARE_VM_GC_G1_COLLECTIONSETCHOOSER_HPP
 195