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