1 /*
   2  * Copyright (c) 2001, 2017, 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_HEAPREGIONMANAGER_HPP
  26 #define SHARE_VM_GC_G1_HEAPREGIONMANAGER_HPP
  27 
  28 #include "gc/g1/g1BiasedArray.hpp"
  29 #include "gc/g1/g1RegionToSpaceMapper.hpp"
  30 #include "gc/g1/heapRegionSet.hpp"
  31 #include "gc/shared/workgroup.hpp"
  32 #include "services/memoryUsage.hpp"
  33 
  34 class HeapRegion;
  35 class HeapRegionClosure;
  36 class HeapRegionClaimer;
  37 class FreeRegionList;
  38 class WorkGang;
  39 
  40 class G1HeapRegionTable : public G1BiasedMappedArray<HeapRegion*> {
  41  protected:
  42   virtual HeapRegion* default_value() const { return NULL; }
  43 };
  44 
  45 // This class keeps track of the actual heap memory, auxiliary data
  46 // and its metadata (i.e., HeapRegion instances) and the list of free regions.
  47 //
  48 // This allows maximum flexibility for deciding what to commit or uncommit given
  49 // a request from outside.
  50 //
  51 // HeapRegions are kept in the _regions array in address order. A region's
  52 // index in the array corresponds to its index in the heap (i.e., 0 is the
  53 // region at the bottom of the heap, 1 is the one after it, etc.). Two
  54 // regions that are consecutive in the array should also be adjacent in the
  55 // address space (i.e., region(i).end() == region(i+1).bottom().
  56 //
  57 // We create a HeapRegion when we commit the region's address space
  58 // for the first time. When we uncommit the address space of a
  59 // region we retain the HeapRegion to be able to re-use it in the
  60 // future (in case we recommit it).
  61 //
  62 // We keep track of three lengths:
  63 //
  64 // * _num_committed (returned by length()) is the number of currently
  65 //   committed regions. These may not be contiguous.
  66 // * _allocated_heapregions_length (not exposed outside this class) is the
  67 //   number of regions+1 for which we have HeapRegions.
  68 // * max_length() returns the maximum number of regions the heap can have.
  69 //
  70 
  71 class HeapRegionManager: public CHeapObj<mtGC> {
  72   friend class VMStructs;
  73   friend class HeapRegionClaimer;
  74 
  75   G1HeapRegionTable _regions;
  76 
  77   G1RegionToSpaceMapper* _heap_mapper;
  78   G1RegionToSpaceMapper* _prev_bitmap_mapper;
  79   G1RegionToSpaceMapper* _next_bitmap_mapper;
  80   G1RegionToSpaceMapper* _bot_mapper;
  81   G1RegionToSpaceMapper* _cardtable_mapper;
  82   G1RegionToSpaceMapper* _card_counts_mapper;
  83 
  84   FreeRegionList _free_list;
  85 
  86   // Each bit in this bitmap indicates that the corresponding region is available
  87   // for allocation.
  88   CHeapBitMap _available_map;
  89 
  90    // The number of regions committed in the heap.
  91   uint _num_committed;
  92 
  93   // Internal only. The highest heap region +1 we allocated a HeapRegion instance for.
  94   uint _allocated_heapregions_length;
  95 
  96   HeapWord* heap_bottom() const { return _regions.bottom_address_mapped(); }
  97   HeapWord* heap_end() const {return _regions.end_address_mapped(); }
  98 
  99   void make_regions_available(uint index, uint num_regions = 1, WorkGang* pretouch_gang = NULL);
 100 
 101   // Pass down commit calls to the VirtualSpace.
 102   void commit_regions(uint index, size_t num_regions = 1, WorkGang* pretouch_gang = NULL);
 103   void uncommit_regions(uint index, size_t num_regions = 1);
 104 
 105   // Notify other data structures about change in the heap layout.
 106   void update_committed_space(HeapWord* old_end, HeapWord* new_end);
 107 
 108   // Find a contiguous set of empty or uncommitted regions of length num and return
 109   // the index of the first region or G1_NO_HRM_INDEX if the search was unsuccessful.
 110   // If only_empty is true, only empty regions are considered.
 111   // Searches from bottom to top of the heap, doing a first-fit.
 112   uint find_contiguous(size_t num, bool only_empty);
 113   // Finds the next sequence of unavailable regions starting from start_idx. Returns the
 114   // length of the sequence found. If this result is zero, no such sequence could be found,
 115   // otherwise res_idx indicates the start index of these regions.
 116   uint find_unavailable_from_idx(uint start_idx, uint* res_idx) const;
 117   // Finds the next sequence of empty regions starting from start_idx, going backwards in
 118   // the heap. Returns the length of the sequence found. If this value is zero, no
 119   // sequence could be found, otherwise res_idx contains the start index of this range.
 120   uint find_empty_from_idx_reverse(uint start_idx, uint* res_idx) const;
 121   // Allocate a new HeapRegion for the given index.
 122   HeapRegion* new_heap_region(uint hrm_index);
 123 #ifdef ASSERT
 124 public:
 125   bool is_free(HeapRegion* hr) const;
 126 #endif
 127   // Returns whether the given region is available for allocation.
 128   bool is_available(uint region) const;
 129 
 130  public:
 131   // Empty constructor, we'll initialize it with the initialize() method.
 132   HeapRegionManager() : _regions(), _heap_mapper(NULL), _num_committed(0),
 133                     _next_bitmap_mapper(NULL), _prev_bitmap_mapper(NULL), _bot_mapper(NULL),
 134                     _allocated_heapregions_length(0), _available_map(),
 135                     _free_list("Free list", new MasterFreeRegionListMtSafeChecker())
 136   { }
 137 
 138   void initialize(G1RegionToSpaceMapper* heap_storage,
 139                   G1RegionToSpaceMapper* prev_bitmap,
 140                   G1RegionToSpaceMapper* next_bitmap,
 141                   G1RegionToSpaceMapper* bot,
 142                   G1RegionToSpaceMapper* cardtable,
 143                   G1RegionToSpaceMapper* card_counts);
 144 
 145   // Return the "dummy" region used for G1AllocRegion. This is currently a hardwired
 146   // new HeapRegion that owns HeapRegion at index 0. Since at the moment we commit
 147   // the heap from the lowest address, this region (and its associated data
 148   // structures) are available and we do not need to check further.
 149   HeapRegion* get_dummy_region() { return new_heap_region(0); }
 150 
 151   // Return the HeapRegion at the given index. Assume that the index
 152   // is valid.
 153   inline HeapRegion* at(uint index) const;
 154 
 155   // Return the next region (by index) that is part of the same
 156   // humongous object that hr is part of.
 157   inline HeapRegion* next_region_in_humongous(HeapRegion* hr) const;
 158 
 159   // If addr is within the committed space return its corresponding
 160   // HeapRegion, otherwise return NULL.
 161   inline HeapRegion* addr_to_region(HeapWord* addr) const;
 162 
 163   // Insert the given region into the free region list.
 164   inline void insert_into_free_list(HeapRegion* hr);
 165 
 166   // Insert the given region list into the global free region list.
 167   void insert_list_into_free_list(FreeRegionList* list) {
 168     _free_list.add_ordered(list);
 169   }
 170 
 171   HeapRegion* allocate_free_region(bool is_old) {
 172     HeapRegion* hr = _free_list.remove_region(is_old);
 173 
 174     if (hr != NULL) {
 175       assert(hr->next() == NULL, "Single region should not have next");
 176       assert(is_available(hr->hrm_index()), "Must be committed");
 177     }
 178     return hr;
 179   }
 180 
 181   inline void allocate_free_regions_starting_at(uint first, uint num_regions);
 182 
 183   // Remove all regions from the free list.
 184   void remove_all_free_regions() {
 185     _free_list.remove_all();
 186   }
 187 
 188   // Return the number of committed free regions in the heap.
 189   uint num_free_regions() const {
 190     return _free_list.length();
 191   }
 192 
 193   size_t total_capacity_bytes() const {
 194     return num_free_regions() * HeapRegion::GrainBytes;
 195   }
 196 
 197   // Return the number of available (uncommitted) regions.
 198   uint available() const { return max_length() - length(); }
 199 
 200   // Return the number of regions that have been committed in the heap.
 201   uint length() const { return _num_committed; }
 202 
 203   // Return the maximum number of regions in the heap.
 204   uint max_length() const { return (uint)_regions.length(); }
 205 
 206   MemoryUsage get_auxiliary_data_memory_usage() const;
 207 
 208   MemRegion reserved() const { return MemRegion(heap_bottom(), heap_end()); }
 209 
 210   // Expand the sequence to reflect that the heap has grown. Either create new
 211   // HeapRegions, or re-use existing ones. Returns the number of regions the
 212   // sequence was expanded by. If a HeapRegion allocation fails, the resulting
 213   // number of regions might be smaller than what's desired.
 214   uint expand_by(uint num_regions, WorkGang* pretouch_workers);
 215 
 216   // Makes sure that the regions from start to start+num_regions-1 are available
 217   // for allocation. Returns the number of regions that were committed to achieve
 218   // this.
 219   uint expand_at(uint start, uint num_regions, WorkGang* pretouch_workers);
 220 
 221   // Find a contiguous set of empty regions of length num. Returns the start index of
 222   // that set, or G1_NO_HRM_INDEX.
 223   uint find_contiguous_only_empty(size_t num) { return find_contiguous(num, true); }
 224   // Find a contiguous set of empty or unavailable regions of length num. Returns the
 225   // start index of that set, or G1_NO_HRM_INDEX.
 226   uint find_contiguous_empty_or_unavailable(size_t num) { return find_contiguous(num, false); }
 227 
 228   HeapRegion* next_region_in_heap(const HeapRegion* r) const;
 229 
 230   // Find the highest free or uncommitted region in the reserved heap,
 231   // and if uncommitted, commit it. If none are available, return G1_NO_HRM_INDEX.
 232   // Set the 'expanded' boolean true if a new region was committed.
 233   uint find_highest_free(bool* expanded);
 234 
 235   // Allocate the regions that contain the address range specified, committing the
 236   // regions if necessary. Return false if any of the regions is already committed
 237   // and not free, and return the number of regions newly committed in commit_count.
 238   bool allocate_containing_regions(MemRegion range, size_t* commit_count, WorkGang* pretouch_workers);
 239 
 240   // Apply blk->doHeapRegion() on all committed regions in address order,
 241   // terminating the iteration early if doHeapRegion() returns true.
 242   void iterate(HeapRegionClosure* blk) const;
 243 
 244   void par_iterate(HeapRegionClosure* blk, uint worker_id, HeapRegionClaimer* hrclaimer, bool concurrent) const;
 245 
 246   // Uncommit up to num_regions_to_remove regions that are completely free.
 247   // Return the actual number of uncommitted regions.
 248   uint shrink_by(uint num_regions_to_remove);
 249 
 250   // Uncommit a number of regions starting at the specified index, which must be available,
 251   // empty, and free.
 252   void shrink_at(uint index, size_t num_regions);
 253 
 254   void verify();
 255 
 256   // Do some sanity checking.
 257   void verify_optional() PRODUCT_RETURN;
 258 };
 259 
 260 // The HeapRegionClaimer is used during parallel iteration over heap regions,
 261 // allowing workers to claim heap regions, gaining exclusive rights to these regions.
 262 class HeapRegionClaimer : public StackObj {
 263   uint           _n_workers;
 264   uint           _n_regions;
 265   volatile uint* _claims;
 266 
 267   static const uint Unclaimed = 0;
 268   static const uint Claimed   = 1;
 269 
 270  public:
 271   HeapRegionClaimer(uint n_workers);
 272   ~HeapRegionClaimer();
 273 
 274   inline uint n_regions() const {
 275     return _n_regions;
 276   }
 277 
 278   // Calculate the starting region for given worker so
 279   // that they do not all start from the same region.
 280   uint start_region_for_worker(uint worker_id) const;
 281 
 282   // Check if region has been claimed with this HRClaimer.
 283   bool is_region_claimed(uint region_index) const;
 284 
 285   // Claim the given region, returns true if successfully claimed.
 286   bool claim_region(uint region_index);
 287 };
 288 
 289 // Subclass of AbstractGangTask that uses a HeapRegionClaimer to parallelize work on a region basis
 290 class G1ParallelizeByRegionsTask: public AbstractGangTask {
 291   HeapRegionClaimer _hrclaimer;
 292 
 293 public:
 294   G1ParallelizeByRegionsTask(const char* name, uint num_workers) :
 295     AbstractGangTask(name),
 296     _hrclaimer(num_workers) {}
 297 
 298 protected:
 299   void all_heap_regions_work(HeapRegionClosure* cl,
 300                              uint worker_id,
 301                              bool concurrent = false);
 302 };
 303 #endif // SHARE_VM_GC_G1_HEAPREGIONMANAGER_HPP