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