1 /*
   2  * Copyright (c) 2001, 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 #include "precompiled.hpp"
  26 #include "gc/g1/g1BiasedArray.hpp"
  27 #include "gc/g1/g1MemoryNodeManager.hpp"
  28 #include "gc/g1/g1RegionToSpaceMapper.hpp"
  29 #include "logging/log.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/virtualspace.hpp"
  32 #include "runtime/java.hpp"
  33 #include "runtime/os.inline.hpp"
  34 #include "services/memTracker.hpp"
  35 #include "utilities/align.hpp"
  36 #include "utilities/bitMap.inline.hpp"
  37 #include "utilities/formatBuffer.hpp"
  38 
  39 G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs,
  40                                              size_t used_size,
  41                                              size_t page_size,
  42                                              size_t region_granularity,
  43                                              size_t commit_factor,
  44                                              MemoryType type) :
  45   _listener(NULL),
  46   _storage(rs, used_size, page_size, type),
  47   _region_granularity(region_granularity),
  48   _commit_map(rs.size() * commit_factor / region_granularity, mtGC) {
  49   guarantee(is_power_of_2(page_size), "must be");
  50   guarantee(is_power_of_2(region_granularity), "must be");
  51 
  52   MemTracker::record_virtual_memory_type((address)rs.base(), type);
  53 }
  54 
  55 // G1RegionToSpaceMapper implementation where the region granularity is larger than
  56 // or the same as the commit granularity.
  57 // Basically, the space corresponding to one region region spans several OS pages.
  58 class G1RegionsLargerThanCommitSizeMapper : public G1RegionToSpaceMapper {
  59  private:
  60   size_t _pages_per_region;
  61 
  62  public:
  63   G1RegionsLargerThanCommitSizeMapper(ReservedSpace rs,
  64                                       size_t actual_size,
  65                                       size_t page_size,
  66                                       size_t alloc_granularity,
  67                                       size_t commit_factor,
  68                                       MemoryType type) :
  69     G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
  70     _pages_per_region(alloc_granularity / (page_size * commit_factor)) {
  71 
  72     guarantee(alloc_granularity >= page_size, "allocation granularity smaller than commit granularity");
  73   }
  74 
  75   virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
  76     const size_t start_page = (size_t)start_idx * _pages_per_region;
  77     const size_t size_in_pages = num_regions * _pages_per_region;
  78     bool zero_filled = _storage.commit(start_page, size_in_pages);
  79     if (AlwaysPreTouch) {
  80       _storage.pretouch(start_page, size_in_pages, pretouch_gang);
  81     }
  82     _commit_map.set_range(start_idx, start_idx + num_regions);
  83     fire_on_commit(start_idx, num_regions, zero_filled);
  84   }
  85 
  86   virtual void uncommit_regions(uint start_idx, size_t num_regions) {
  87     _storage.uncommit((size_t)start_idx * _pages_per_region, num_regions * _pages_per_region);
  88     _commit_map.clear_range(start_idx, start_idx + num_regions);
  89   }
  90 };
  91 
  92 // G1RegionToSpaceMapper implementation where the region granularity is smaller
  93 // than the commit granularity.
  94 // Basically, the contents of one OS page span several regions.
  95 class G1RegionsSmallerThanCommitSizeMapper : public G1RegionToSpaceMapper {
  96  private:
  97   class CommitRefcountArray : public G1BiasedMappedArray<uint> {
  98    protected:
  99      virtual uint default_value() const { return 0; }
 100   };
 101 
 102   size_t _regions_per_page;
 103 
 104   CommitRefcountArray _refcounts;
 105 
 106   uintptr_t region_idx_to_page_idx(uint region) const {
 107     return region / _regions_per_page;
 108   }
 109 
 110  public:
 111   G1RegionsSmallerThanCommitSizeMapper(ReservedSpace rs,
 112                                        size_t actual_size,
 113                                        size_t page_size,
 114                                        size_t alloc_granularity,
 115                                        size_t commit_factor,
 116                                        MemoryType type) :
 117     G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
 118     _regions_per_page((page_size * commit_factor) / alloc_granularity), _refcounts() {
 119 
 120     guarantee((page_size * commit_factor) >= alloc_granularity, "allocation granularity smaller than commit granularity");
 121     _refcounts.initialize((HeapWord*)rs.base(), (HeapWord*)(rs.base() + align_up(rs.size(), page_size)), page_size);
 122   }
 123 
 124   virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
 125     size_t const NoPage = ~(size_t)0;
 126 
 127     size_t first_committed = NoPage;
 128     size_t num_committed = 0;
 129 
 130     bool all_zero_filled = true;
 131 
 132     for (uint i = start_idx; i < start_idx + num_regions; i++) {
 133       assert(!_commit_map.at(i), "Trying to commit storage at region %u that is already committed", i);
 134       size_t idx = region_idx_to_page_idx(i);
 135       uint old_refcount = _refcounts.get_by_index(idx);
 136 
 137       bool zero_filled = false;
 138       if (old_refcount == 0) {
 139         if (first_committed == NoPage) {
 140           first_committed = idx;
 141           num_committed = 1;
 142         } else {
 143           num_committed++;
 144         }
 145         zero_filled = _storage.commit(idx, 1);
 146       }
 147       all_zero_filled &= zero_filled;
 148 
 149       _refcounts.set_by_index(idx, old_refcount + 1);
 150       _commit_map.set_bit(i);
 151     }
 152     if (AlwaysPreTouch && num_committed > 0) {
 153       _storage.pretouch(first_committed, num_committed, pretouch_gang);
 154     }
 155     fire_on_commit(start_idx, num_regions, all_zero_filled);
 156   }
 157 
 158   virtual void uncommit_regions(uint start_idx, size_t num_regions) {
 159     for (uint i = start_idx; i < start_idx + num_regions; i++) {
 160       assert(_commit_map.at(i), "Trying to uncommit storage at region %u that is not committed", i);
 161       size_t idx = region_idx_to_page_idx(i);
 162       uint old_refcount = _refcounts.get_by_index(idx);
 163       assert(old_refcount > 0, "must be");
 164       if (old_refcount == 1) {
 165         _storage.uncommit(idx, 1);
 166       }
 167       _refcounts.set_by_index(idx, old_refcount - 1);
 168       _commit_map.clear_bit(i);
 169     }
 170   }
 171 };
 172 
 173 void G1RegionToSpaceMapper::fire_on_commit(uint start_idx, size_t num_regions, bool zero_filled) {
 174   if (_listener != NULL) {
 175     _listener->on_commit(start_idx, num_regions, zero_filled);
 176   }
 177 }
 178 
 179 static bool map_nvdimm_space(ReservedSpace rs) {
 180   assert(AllocateOldGenAt != NULL, "");
 181   int _backing_fd = os::create_file_for_heap(AllocateOldGenAt);
 182   if (_backing_fd == -1) {
 183     log_error(gc, init)("Could not create file for Old generation at location %s", AllocateOldGenAt);
 184     return false;
 185   }
 186   // commit this memory in nv-dimm
 187   char* ret = os::attempt_reserve_memory_at(rs.size(), rs.base(), _backing_fd);
 188 
 189   if (ret != rs.base()) {
 190     if (ret != NULL) {
 191       os::unmap_memory(rs.base(), rs.size());
 192     }
 193     log_error(gc, init)("Error in mapping Old Gen to given AllocateOldGenAt = %s", AllocateOldGenAt);
 194     os::close(_backing_fd);
 195     return false;
 196   }
 197 
 198   os::close(_backing_fd);
 199   return true;
 200 }
 201 
 202 G1RegionToHeteroSpaceMapper::G1RegionToHeteroSpaceMapper(ReservedSpace rs,
 203                                                          size_t actual_size,
 204                                                          size_t page_size,
 205                                                          size_t alloc_granularity,
 206                                                          size_t commit_factor,
 207                                                          MemoryType type) :
 208   G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
 209   _rs(rs),
 210   _dram_mapper(NULL),
 211   _num_committed_dram(0),
 212   _num_committed_nvdimm(0),
 213   _start_index_of_dram(0),
 214   _page_size(page_size),
 215   _commit_factor(commit_factor),
 216   _type(type) {
 217   assert(actual_size == 2 * MaxHeapSize, "For 2-way heterogenuous heap, reserved space is two times MaxHeapSize");
 218 }
 219 
 220 bool G1RegionToHeteroSpaceMapper::initialize() {
 221   // Since we need to re-map the reserved space - 'Xmx' to nv-dimm and 'Xmx' to dram, we need to release the reserved memory first.
 222   // Because on some OSes (e.g. Windows) you cannot do a file mapping on memory reserved with regular mapping.
 223   os::release_memory(_rs.base(), _rs.size());
 224   // First half of size Xmx is for nv-dimm.
 225   ReservedSpace rs_nvdimm = _rs.first_part(MaxHeapSize);
 226   assert(rs_nvdimm.base() == _rs.base(), "We should get the same base address");
 227 
 228   // Second half of reserved memory is mapped to dram.
 229   ReservedSpace rs_dram = _rs.last_part(MaxHeapSize);
 230 
 231   assert(rs_dram.size() == rs_nvdimm.size() && rs_nvdimm.size() == MaxHeapSize, "They all should be same");
 232 
 233   // Reserve dram memory
 234   char* base = os::attempt_reserve_memory_at(rs_dram.size(), rs_dram.base());
 235   if (base != rs_dram.base()) {
 236     if (base != NULL) {
 237       os::release_memory(base, rs_dram.size());
 238     }
 239     log_error(gc, init)("Error in re-mapping memory on dram during G1 heterogenous memory initialization");
 240     return false;
 241   }
 242 
 243   // We reserve and commit this entire space to NV-DIMM.
 244   if (!map_nvdimm_space(rs_nvdimm)) {
 245     log_error(gc, init)("Error in re-mapping memory to nv-dimm during G1 heterogenous memory initialization");
 246     return false;
 247   }
 248 
 249   if (_region_granularity >= (_page_size * _commit_factor)) {
 250     _dram_mapper = new G1RegionsLargerThanCommitSizeMapper(rs_dram, rs_dram.size(), _page_size, _region_granularity, _commit_factor, _type);
 251   } else {
 252     _dram_mapper = new G1RegionsSmallerThanCommitSizeMapper(rs_dram, rs_dram.size(), _page_size, _region_granularity, _commit_factor, _type);
 253   }
 254 
 255   _start_index_of_dram = (uint)(rs_nvdimm.size() / _region_granularity);
 256   return true;
 257 }
 258 
 259 void G1RegionToHeteroSpaceMapper::commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
 260   uint end_idx = (start_idx + (uint)num_regions - 1);
 261 
 262   uint num_dram = end_idx >= _start_index_of_dram ? MIN2((end_idx - _start_index_of_dram + 1), (uint)num_regions) : 0;
 263   uint num_nvdimm = (uint)num_regions - num_dram;
 264 
 265   if (num_nvdimm > 0) {
 266     // We do not need to commit nv-dimm regions, since they are committed in the beginning.
 267     _num_committed_nvdimm += num_nvdimm;
 268   }
 269   if (num_dram > 0) {
 270     _dram_mapper->commit_regions(start_idx > _start_index_of_dram ? (start_idx - _start_index_of_dram) : 0, num_dram, pretouch_gang);
 271     _num_committed_dram += num_dram;
 272   }
 273 }
 274 
 275 void G1RegionToHeteroSpaceMapper::uncommit_regions(uint start_idx, size_t num_regions) {
 276   uint end_idx = (start_idx + (uint)num_regions - 1);
 277   uint num_dram = end_idx >= _start_index_of_dram ? MIN2((end_idx - _start_index_of_dram + 1), (uint)num_regions) : 0;
 278   uint num_nvdimm = (uint)num_regions - num_dram;
 279 
 280   if (num_nvdimm > 0) {
 281     // We do not uncommit memory for nv-dimm regions.
 282     _num_committed_nvdimm -= num_nvdimm;
 283   }
 284 
 285   if (num_dram > 0) {
 286     _dram_mapper->uncommit_regions(start_idx > _start_index_of_dram ? (start_idx - _start_index_of_dram) : 0, num_dram);
 287     _num_committed_dram -= num_dram;
 288   }
 289 }
 290 
 291 uint G1RegionToHeteroSpaceMapper::num_committed_dram() const {
 292   return _num_committed_dram;
 293 }
 294 
 295 uint G1RegionToHeteroSpaceMapper::num_committed_nvdimm() const {
 296   return _num_committed_nvdimm;
 297 }
 298 
 299 G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_heap_mapper(ReservedSpace rs,
 300                                                                  size_t actual_size,
 301                                                                  size_t page_size,
 302                                                                  size_t region_granularity,
 303                                                                  size_t commit_factor,
 304                                                                  MemoryType type) {
 305   if (AllocateOldGenAt != NULL) {
 306     G1RegionToHeteroSpaceMapper* mapper = new G1RegionToHeteroSpaceMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
 307     if (!mapper->initialize()) {
 308       delete mapper;
 309       return NULL;
 310     }
 311     return (G1RegionToSpaceMapper*)mapper;
 312   } else {
 313     return create_mapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
 314   }
 315 }
 316 
 317 G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_mapper(ReservedSpace rs,
 318                                                             size_t actual_size,
 319                                                             size_t page_size,
 320                                                             size_t region_granularity,
 321                                                             size_t commit_factor,
 322                                                             MemoryType type) {
 323   if (region_granularity >= (page_size * commit_factor)) {
 324     return new G1RegionsLargerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
 325   } else {
 326     return new G1RegionsSmallerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
 327   }
 328 }
 329 
 330 void G1RegionToSpaceMapper::commit_and_set_special() {
 331   _storage.commit_and_set_special();
 332 }