1 /*
   2  * Copyright (c) 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_G1COLLECTIONSETCANDIDATES_HPP
  26 #define SHARE_GC_G1_G1COLLECTIONSETCANDIDATES_HPP
  27 
  28 #include "gc/g1/g1CollectionSetCandidates.hpp"
  29 #include "gc/shared/workgroup.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "runtime/globals.hpp"
  32 
  33 class HeapRegion;
  34 class HeapRegionClosure;
  35 
  36 // Set of collection set candidates, i.e. all old gen regions we consider worth
  37 // collecting in the remainder of the current mixed phase. Regions are sorted by decreasing
  38 // gc efficiency.
  39 // Maintains a cursor into the list that specifies the next collection set candidate
  40 // to put into the current collection set.
  41 class G1CollectionSetCandidates : public CHeapObj<mtGC> {
  42   HeapRegion** _regions;
  43   uint _num_regions; // Total number of regions in the collection set candidate set.
  44 
  45   // The sum of bytes that can be reclaimed in the remaining set of collection
  46   // set candidates.
  47   size_t _remaining_reclaimable_bytes;
  48   // The index of the next candidate old region to be considered for
  49   // addition to the current collection set.
  50   uint _front_idx;
  51 
  52 public:
  53   G1CollectionSetCandidates(HeapRegion** regions, uint num_regions, size_t remaining_reclaimable_bytes) :
  54     _regions(regions),
  55     _num_regions(num_regions),
  56     _remaining_reclaimable_bytes(remaining_reclaimable_bytes),
  57     _front_idx(0) { }
  58 
  59   ~G1CollectionSetCandidates() {
  60     FREE_C_HEAP_ARRAY(HeapRegion*, _regions);
  61   }
  62 
  63   // Returns the total number of collection set candidate old regions added.
  64   uint num_regions() { return _num_regions; }
  65 
  66   // Return the candidate region at the cursor position to be considered for collection without
  67   // removing it.
  68   HeapRegion* peek_front() {
  69     HeapRegion* res = NULL;
  70     if (_front_idx < _num_regions) {
  71       res = _regions[_front_idx];
  72       assert(res != NULL, "Unexpected NULL HeapRegion at index %u", _front_idx);
  73     }
  74     return res;
  75   }
  76 
  77   // Remove the given region from the candidates set and move the cursor to the next one.
  78   HeapRegion* pop_front();
  79 
  80   // Add the given HeapRegion to the front of the collection set candidate set again.
  81   void push_front(HeapRegion* hr);
  82 
  83   // Iterate over all remaining collection set candidate regions.
  84   void iterate(HeapRegionClosure* cl);
  85 
  86   // Return the number of candidate regions remaining.
  87   uint num_remaining() { return _num_regions - _front_idx; }
  88 
  89   bool is_empty() { return num_remaining() == 0; }
  90 
  91   // Return the amount of reclaimable bytes that may be collected by the remaining
  92   // candidate regions.
  93   size_t remaining_reclaimable_bytes() { return _remaining_reclaimable_bytes; }
  94 
  95   void verify() const PRODUCT_RETURN;
  96 };
  97 
  98 #endif /* SHARE_GC_G1_G1COLLECTIONSETCANDIDATES_HPP */
  99