1 /* 2 * Copyright (c) 2015, 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 #include "precompiled.hpp" 25 #include "gc/z/zPhysicalMemory.inline.hpp" 26 #include "logging/log.hpp" 27 #include "memory/allocation.inline.hpp" 28 #include "services/memTracker.hpp" 29 #include "utilities/debug.hpp" 30 #include "utilities/globalDefinitions.hpp" 31 32 ZPhysicalMemory::ZPhysicalMemory() : 33 _nsegments(0), 34 _segments(NULL) {} 35 36 ZPhysicalMemory::ZPhysicalMemory(size_t size) : 37 _nsegments(0), 38 _segments(NULL) { 39 add_segment(ZPhysicalMemorySegment(0, size)); 40 } 41 42 ZPhysicalMemory::ZPhysicalMemory(const ZPhysicalMemorySegment& segment) : 43 _nsegments(0), 44 _segments(NULL) { 45 add_segment(segment); 46 } 47 48 size_t ZPhysicalMemory::size() const { 49 size_t size = 0; 50 51 for (size_t i = 0; i < _nsegments; i++) { 52 size += _segments[i].size(); 53 } 54 55 return size; 56 } 57 58 void ZPhysicalMemory::add_segment(ZPhysicalMemorySegment segment) { 59 // Try merge with last segment 60 if (_nsegments > 0) { 61 ZPhysicalMemorySegment& last = _segments[_nsegments - 1]; 62 assert(last.end() <= segment.start(), "Segments added out of order"); 63 if (last.end() == segment.start()) { 64 // Merge 65 last.expand(segment.size()); 66 return; 67 } 68 } 69 70 // Make room for a new segment 71 const size_t size = sizeof(ZPhysicalMemorySegment) * (_nsegments + 1); 72 _segments = (ZPhysicalMemorySegment*)ReallocateHeap((char*)_segments, size, mtGC); 73 74 // Add new segment 75 _segments[_nsegments] = segment; 76 _nsegments++; 77 } 78 79 ZPhysicalMemory ZPhysicalMemory::split(size_t split_size) { 80 // Only splitting of single-segment instances have been implemented. 81 assert(nsegments() == 1, "Can only have one segment"); 82 assert(split_size <= size(), "Invalid size"); 83 return ZPhysicalMemory(_segments[0].split(split_size)); 84 } 85 86 void ZPhysicalMemory::clear() { 87 if (_segments != NULL) { 88 FreeHeap(_segments); 89 _segments = NULL; 90 _nsegments = 0; 91 } 92 } 93 94 ZPhysicalMemoryManager::ZPhysicalMemoryManager(size_t max_capacity) : 95 _backing(max_capacity), 96 _max_capacity(max_capacity), 97 _current_max_capacity(max_capacity), 98 _capacity(0), 99 _used(0) {} 100 101 bool ZPhysicalMemoryManager::is_initialized() const { 102 return _backing.is_initialized(); 103 } 104 105 void ZPhysicalMemoryManager::try_ensure_unused_capacity(size_t size) { 106 const size_t unused = unused_capacity(); 107 if (unused >= size) { 108 // Don't try to expand, enough unused capacity available 109 return; 110 } 111 112 const size_t current_max = current_max_capacity(); 113 if (_capacity == current_max) { 114 // Don't try to expand, current max capacity reached 115 return; 116 } 117 118 // Try to expand 119 const size_t old_capacity = capacity(); 120 const size_t new_capacity = MIN2(old_capacity + size - unused, current_max); 121 _capacity = _backing.try_expand(old_capacity, new_capacity); 122 123 if (_capacity != new_capacity) { 124 // Failed, or partly failed, to expand 125 log_error(gc, init)("Not enough space available on the backing filesystem to hold the current max"); 126 log_error(gc, init)("Java heap size (" SIZE_FORMAT "M). Forcefully lowering max Java heap size to " 127 SIZE_FORMAT "M (%.0lf%%).", current_max / M, _capacity / M, 128 percent_of(_capacity, current_max)); 129 130 // Adjust current max capacity to avoid further expand attempts 131 _current_max_capacity = _capacity; 132 } 133 } 134 135 void ZPhysicalMemoryManager::nmt_commit(ZPhysicalMemory pmem, uintptr_t offset) { 136 const uintptr_t addr = _backing.nmt_address(offset); 137 const size_t size = pmem.size(); 138 MemTracker::record_virtual_memory_commit((void*)addr, size, CALLER_PC); 139 } 140 141 void ZPhysicalMemoryManager::nmt_uncommit(ZPhysicalMemory pmem, uintptr_t offset) { 142 if (MemTracker::tracking_level() > NMT_minimal) { 143 const uintptr_t addr = _backing.nmt_address(offset); 144 const size_t size = pmem.size(); 145 146 Tracker tracker(Tracker::uncommit); 147 tracker.record((address)addr, size); 148 } 149 } 150 151 ZPhysicalMemory ZPhysicalMemoryManager::alloc(size_t size) { 152 if (unused_capacity() < size) { 153 // Not enough memory available 154 return ZPhysicalMemory(); 155 } 156 157 _used += size; 158 return _backing.alloc(size); 159 } 160 161 void ZPhysicalMemoryManager::free(ZPhysicalMemory pmem) { 162 _backing.free(pmem); 163 _used -= pmem.size(); 164 } 165 166 void ZPhysicalMemoryManager::map(ZPhysicalMemory pmem, uintptr_t offset) { 167 // Map page 168 _backing.map(pmem, offset); 169 170 // Update native memory tracker 171 nmt_commit(pmem, offset); 172 } 173 174 void ZPhysicalMemoryManager::unmap(ZPhysicalMemory pmem, uintptr_t offset) { 175 // Update native memory tracker 176 nmt_uncommit(pmem, offset); 177 178 // Unmap page 179 _backing.unmap(pmem, offset); 180 } 181 182 void ZPhysicalMemoryManager::flip(ZPhysicalMemory pmem, uintptr_t offset) { 183 _backing.flip(pmem, offset); 184 }