< prev index next >

src/hotspot/share/gc/g1/g1RegionToSpaceMapper.cpp

Print this page
rev 51649 : version 1
rev 51652 : Added support for eager mixed collection of evacuation failure regions
rev 51878 : Minor changes
rev 52017 : All changes for G1 GC moved from 'combined' repo folder
rev 52487 : Worked on comments from Sangheon, Stefan
rev 52492 : Reverting back this change which does not work


   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/g1RegionToSpaceMapper.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "memory/virtualspace.hpp"

  30 #include "services/memTracker.hpp"
  31 #include "utilities/align.hpp"
  32 #include "utilities/bitMap.inline.hpp"

  33 
  34 G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs,
  35                                              size_t used_size,
  36                                              size_t page_size,
  37                                              size_t region_granularity,
  38                                              size_t commit_factor,
  39                                              MemoryType type) :
  40   _listener(NULL),
  41   _storage(rs, used_size, page_size),
  42   _region_granularity(region_granularity),
  43   _commit_map(rs.size() * commit_factor / region_granularity, mtGC) {
  44   guarantee(is_power_of_2(page_size), "must be");
  45   guarantee(is_power_of_2(region_granularity), "must be");
  46 
  47   MemTracker::record_virtual_memory_type((address)rs.base(), type);
  48 }
  49 
  50 // G1RegionToSpaceMapper implementation where the region granularity is larger than
  51 // or the same as the commit granularity.
  52 // Basically, the space corresponding to one region region spans several OS pages.


 153     for (uint i = start_idx; i < start_idx + num_regions; i++) {
 154       assert(_commit_map.at(i), "Trying to uncommit storage at region %u that is not committed", i);
 155       size_t idx = region_idx_to_page_idx(i);
 156       uint old_refcount = _refcounts.get_by_index(idx);
 157       assert(old_refcount > 0, "must be");
 158       if (old_refcount == 1) {
 159         _storage.uncommit(idx, 1);
 160       }
 161       _refcounts.set_by_index(idx, old_refcount - 1);
 162       _commit_map.clear_bit(i);
 163     }
 164   }
 165 };
 166 
 167 void G1RegionToSpaceMapper::fire_on_commit(uint start_idx, size_t num_regions, bool zero_filled) {
 168   if (_listener != NULL) {
 169     _listener->on_commit(start_idx, num_regions, zero_filled);
 170   }
 171 }
 172 
 173 G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_mapper(ReservedSpace rs,














































































































 174                                                             size_t actual_size,
 175                                                             size_t page_size,
 176                                                             size_t region_granularity,
 177                                                             size_t commit_factor,
 178                                                             MemoryType type) {











 179 






 180   if (region_granularity >= (page_size * commit_factor)) {
 181     return new G1RegionsLargerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
 182   } else {
 183     return new G1RegionsSmallerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
 184   }
 185 }


   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 "logging/log.hpp"
  26 #include "precompiled.hpp"
  27 #include "gc/g1/g1BiasedArray.hpp"
  28 #include "gc/g1/g1RegionToSpaceMapper.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "memory/virtualspace.hpp"
  31 #include "runtime/java.hpp"
  32 #include "services/memTracker.hpp"
  33 #include "utilities/align.hpp"
  34 #include "utilities/bitMap.inline.hpp"
  35 #include "utilities/formatBuffer.hpp"
  36 
  37 G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs,
  38                                              size_t used_size,
  39                                              size_t page_size,
  40                                              size_t region_granularity,
  41                                              size_t commit_factor,
  42                                              MemoryType type) :
  43   _listener(NULL),
  44   _storage(rs, used_size, page_size),
  45   _region_granularity(region_granularity),
  46   _commit_map(rs.size() * commit_factor / region_granularity, mtGC) {
  47   guarantee(is_power_of_2(page_size), "must be");
  48   guarantee(is_power_of_2(region_granularity), "must be");
  49 
  50   MemTracker::record_virtual_memory_type((address)rs.base(), type);
  51 }
  52 
  53 // G1RegionToSpaceMapper implementation where the region granularity is larger than
  54 // or the same as the commit granularity.
  55 // Basically, the space corresponding to one region region spans several OS pages.


 156     for (uint i = start_idx; i < start_idx + num_regions; i++) {
 157       assert(_commit_map.at(i), "Trying to uncommit storage at region %u that is not committed", i);
 158       size_t idx = region_idx_to_page_idx(i);
 159       uint old_refcount = _refcounts.get_by_index(idx);
 160       assert(old_refcount > 0, "must be");
 161       if (old_refcount == 1) {
 162         _storage.uncommit(idx, 1);
 163       }
 164       _refcounts.set_by_index(idx, old_refcount - 1);
 165       _commit_map.clear_bit(i);
 166     }
 167   }
 168 };
 169 
 170 void G1RegionToSpaceMapper::fire_on_commit(uint start_idx, size_t num_regions, bool zero_filled) {
 171   if (_listener != NULL) {
 172     _listener->on_commit(start_idx, num_regions, zero_filled);
 173   }
 174 }
 175 
 176 static bool map_nvdimm_space(ReservedSpace rs) {
 177   assert(AllocateOldGenAt != NULL, "");
 178   int _backing_fd = os::create_file_for_heap(AllocateOldGenAt);
 179   if (_backing_fd == -1) {
 180     return false;
 181     log_error(gc, init)("Could not create file for Old generation at location %s", AllocateOldGenAt);
 182   }
 183   // commit this memory in nv-dimm
 184   char* ret = os::attempt_reserve_memory_at(rs.size(), rs.base(), _backing_fd);
 185 
 186   if (ret != rs.base()) {
 187     if (ret != NULL) {
 188       os::unmap_memory(rs.base(), rs.size());
 189     }
 190     log_error(gc, init)("Error in mapping Old Gen to given AllocateOldGenAt = %s", AllocateOldGenAt);
 191     return false;
 192   }
 193   return true;
 194 }
 195 
 196 G1RegionToHeteroSpaceMapper::G1RegionToHeteroSpaceMapper(ReservedSpace rs,
 197   size_t actual_size,
 198   size_t page_size,
 199   size_t alloc_granularity,
 200   size_t commit_factor,
 201   MemoryType type) :
 202   G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
 203   _num_committed_dram(0), _num_committed_nvdimm(0), _success(false) {
 204   assert(actual_size == 2 * MaxHeapSize, "For 2-way heterogenuous heap, reserved space is two times MaxHeapSize");
 205 
 206   // 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.
 207   // Because on some OSes (e.g. Windows) you cannot do a file mapping on memory reserved with regular mapping.
 208   os::release_memory(rs.base(), rs.size());
 209   // First half of size Xmx is for nv-dimm.
 210   ReservedSpace rs_nvdimm = rs.first_part(MaxHeapSize);
 211   assert(rs_nvdimm.base() == rs.base(), "We should get the same base address");
 212 
 213   // Second half of reserved memory is mapped to dram.
 214   ReservedSpace rs_dram = rs.last_part(MaxHeapSize);
 215 
 216   assert(rs_dram.size() == rs_nvdimm.size() && rs_nvdimm.size() == MaxHeapSize, "They all should be same");
 217 
 218   // Reserve dram memory
 219   char* base = os::attempt_reserve_memory_at(rs_dram.size(), rs_dram.base());
 220   if (base != rs_dram.base()) {
 221     if (base != NULL) {
 222       os::release_memory(base, rs_dram.size());
 223     }
 224     log_error(gc, init)("Error in re-mapping memory on dram during G1 heterogenous memory initialization");
 225     return;
 226   }
 227 
 228   // We reserve and commit this entire space to NV-DIMM.
 229   if (!map_nvdimm_space(rs_nvdimm)) {
 230     log_error(gc, init)("Error in re-mapping memory to nv-dimm during G1 heterogenous memory initialization");
 231     return;
 232   }
 233 
 234   if (alloc_granularity >= (page_size * commit_factor)) {
 235     _dram_mapper = new G1RegionsLargerThanCommitSizeMapper(rs_dram, rs_dram.size(), page_size, alloc_granularity, commit_factor, type);
 236   }
 237   else {
 238     _dram_mapper = new G1RegionsSmallerThanCommitSizeMapper(rs_dram, rs_dram.size(), page_size, alloc_granularity, commit_factor, type);
 239   }
 240 
 241   _start_index_of_nvdimm = 0;
 242   _start_index_of_dram = (uint)(rs_nvdimm.size() / alloc_granularity);
 243   _success = true;
 244 }
 245 
 246 void G1RegionToHeteroSpaceMapper::commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) {
 247   uint end_idx = (start_idx + (uint)num_regions - 1);
 248 
 249   uint num_dram = end_idx >= _start_index_of_dram ? MIN2((end_idx - _start_index_of_dram + 1), (uint)num_regions) : 0;
 250   uint num_nvdimm = (uint)num_regions - num_dram;
 251 
 252   if (num_nvdimm > 0) {
 253     // We do not need to commit nv-dimm regions, since they are committed in the beginning.
 254     _num_committed_nvdimm += num_nvdimm;
 255   }
 256   if (num_dram > 0) {
 257     _dram_mapper->commit_regions(start_idx > _start_index_of_dram ? (start_idx - _start_index_of_dram) : 0, num_dram, pretouch_gang);
 258     _num_committed_dram += num_dram;
 259   }
 260 }
 261 
 262 void G1RegionToHeteroSpaceMapper::uncommit_regions(uint start_idx, size_t num_regions) {
 263   uint end_idx = (start_idx + (uint)num_regions - 1);
 264   uint num_dram = end_idx >= _start_index_of_dram ? MIN2((end_idx - _start_index_of_dram + 1), (uint)num_regions) : 0;
 265   uint num_nvdimm = (uint)num_regions - num_dram;
 266 
 267   if (num_nvdimm > 0) {
 268     // We do not uncommit memory for nv-dimm regions.
 269     _num_committed_nvdimm -= num_nvdimm;
 270   }
 271 
 272   if (num_dram > 0) {
 273     _dram_mapper->uncommit_regions(start_idx > _start_index_of_dram ? (start_idx - _start_index_of_dram) : 0, num_dram);
 274     _num_committed_dram -= num_dram;
 275   }
 276 }
 277 
 278 uint G1RegionToHeteroSpaceMapper::num_committed_dram() const {
 279   return _num_committed_dram;
 280 }
 281 
 282 uint G1RegionToHeteroSpaceMapper::num_committed_nvdimm() const {
 283   return _num_committed_nvdimm;
 284 }
 285 
 286 G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_heap_mapper(ReservedSpace rs,
 287   size_t actual_size,
 288   size_t page_size,
 289   size_t region_granularity,
 290   size_t commit_factor,
 291   MemoryType type) {
 292   if (AllocateOldGenAt != NULL) {
 293     G1RegionToHeteroSpaceMapper* mapper = new G1RegionToHeteroSpaceMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
 294     if (!mapper->success()) {
 295       delete mapper;
 296       return NULL;
 297     }
 298     return (G1RegionToSpaceMapper*)mapper;
 299   } else {
 300     return create_mapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
 301   }
 302 }
 303 
 304 G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_mapper(ReservedSpace rs,
 305                                                             size_t actual_size,
 306                                                             size_t page_size,
 307                                                             size_t region_granularity,
 308                                                             size_t commit_factor,
 309                                                             MemoryType type) {
 310   if (region_granularity >= (page_size * commit_factor)) {
 311     return new G1RegionsLargerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
 312   } else {
 313     return new G1RegionsSmallerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
 314   }
 315 }
< prev index next >