1 /*
   2  * Copyright (c) 2018, 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_HEAPREGIONMANAGERFORHETEROHEAP_HPP
  26 #define SHARE_VM_GC_G1_HEAPREGIONMANAGERFORHETEROHEAP_HPP
  27 
  28 // This class manages heap regions on heterogenous memory comprising of dram and nv-dimm.
  29 // Regions in dram (dram_set) are used for young objects and archive regions (CDS).
  30 // Regions in nv-dimm (nvdimm_set) are used for old objects and humongous objects.
  31 // At any point there are some regions committed on dram and some on nv-dimm with the following guarantees:
  32 //   1. The total number of regions committed in dram and nv-dimm equals the current size of heap.
  33 //   2. Consequently, total number of regions committed is less than or equal to Xmx.
  34 //   3. To maintain the guarantee stated by 1., whenever one set grows (new regions committed), the other set shrinks (regions un-committed).
  35 //      3a. If more dram regions are needed (young generation expansion), corresponding number of regions in nv-dimm are un-committed.
  36 //      3b. When old generation or humongous set grows, and new regions need to be committed to nv-dimm, corresponding number of regions
  37 //            are un-committed in dram.
  38 class HeapRegionManagerForHeteroHeap : public HeapRegionManager {
  39 
  40   uint _max_regions;
  41   uint _max_dram_regions;
  42   uint _max_nvdimm_regions;
  43   uint _total_commited_before_full_gc;
  44   uint _start_index_of_nvdimm;
  45 
  46   uint start_index_of_nvdimm() const;
  47   uint start_index_of_dram() const;
  48   uint end_index_of_nvdimm() const;
  49   uint end_index_of_dram() const;
  50 
  51   uint total_regions_committed() const;
  52   uint num_committed_dram() const;
  53   uint num_committed_nvdimm() const;
  54 
  55   // Similar to find_unavailable_from_idx() function from base class, difference is this function searches in range [start, end].
  56   uint find_unavailable_in_range(uint start_idx, uint end_idx, uint* res_idx) const;
  57 
  58   // Expand into dram. Maintains the invariant that total number of committed regions is less than current heap size.
  59   uint expand_dram(uint num_regions, WorkGang* pretouch_workers);
  60 
  61   // Expand into nv-dimm.
  62   uint expand_nvdimm(uint num_regions, WorkGang* pretouch_workers);
  63 
  64   // Expand by finding unavailable regions in [start, end] range.
  65   uint expand_in_range(uint start, uint end, uint num_regions, WorkGang* pretouch_workers);
  66 
  67   // Shrink dram set of regions.
  68   uint shrink_dram(uint num_regions, bool update_free_list = true);
  69 
  70   // Shrink nv-dimm set of regions.
  71   uint shrink_nvdimm(uint num_regions, bool update_free_list = true);
  72 
  73   // Shrink regions from [start, end] range.
  74   uint shrink_in_range(uint start, uint end, uint num_regions, bool update_free_list = true);
  75 
  76   // Similar to find_empty_from_idx_reverse() in base class. Only here it searches in a range.
  77   uint find_empty_in_range_reverse(uint start_idx, uint end_idx, uint* res_idx);
  78 
  79   // Similar to find_contiguous() in base class, with [start, end] range
  80   uint find_contiguous(size_t start, size_t end, size_t num, bool empty_only);
  81 
  82   uint free_list_dram_length() const;
  83   uint free_list_nvdimm_length() const;
  84 
  85   // is region with given index in nv-dimm?
  86   bool is_in_nvdimm(uint index) const;
  87   bool is_in_dram(uint index) const;
  88 
  89 public:
  90 
  91   // Empty constructor, we'll initialize it with the initialize() method.
  92   HeapRegionManagerForHeteroHeap(uint num_regions) : _max_regions(num_regions), _max_dram_regions(0),
  93                                                      _max_nvdimm_regions(0), _start_index_of_nvdimm(0)
  94   {}
  95 
  96   // Override.
  97   HeapRegion* get_dummy_region();
  98 
  99   // Resize dram_set to 'expected_num_regions'.
 100   void resize_dram_regions(uint expected_num_regions, WorkGang* pretouch_workers);
 101 
 102   // Should be called before starting full collection.
 103   void prepare_for_full_collection_start();
 104   void prepare_for_full_collection_end();
 105 
 106   virtual HeapRegion* allocate_free_region(bool is_old);
 107 
 108   // Return maximum number of regions that heap can expand to.
 109   uint max_expandable_length() const;
 110 
 111   // Override. Expand in nv-dimm.
 112   uint expand_by(uint num_regions, WorkGang* pretouch_workers);
 113 
 114   // Override.
 115   uint expand_at(uint start, uint num_regions, WorkGang* pretouch_workers);
 116 
 117   // Override. This function is called for humongous allocation, so we need to find empty regions in nv-dimm.
 118   uint find_contiguous_only_empty(size_t num);
 119 
 120   // Override. This function is called for humongous allocation, so we need to find empty or unavailable regions in nv-dimm.
 121   uint find_contiguous_empty_or_unavailable(size_t num);
 122 
 123   // Overrides base class implementation to find highest free region in dram.
 124   uint find_highest_free(bool* expanded);
 125 
 126   // Override. This fuction is called to shrink the heap, we shrink in dram first then in nv-dimm.
 127   uint shrink_by(uint num_regions_to_remove);
 128 
 129   void verify();
 130 };
 131 
 132 #endif