1 /* 2 * Copyright (c) 2014, 2015, 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_implementation/g1/g1PageBasedVirtualSpace.hpp" 27 #include "oops/markOop.hpp" 28 #include "oops/oop.inline.hpp" 29 #include "services/memTracker.hpp" 30 #ifdef TARGET_OS_FAMILY_linux 31 # include "os_linux.inline.hpp" 32 #endif 33 #ifdef TARGET_OS_FAMILY_solaris 34 # include "os_solaris.inline.hpp" 35 #endif 36 #ifdef TARGET_OS_FAMILY_windows 37 # include "os_windows.inline.hpp" 38 #endif 39 #ifdef TARGET_OS_FAMILY_aix 40 # include "os_aix.inline.hpp" 41 #endif 42 #ifdef TARGET_OS_FAMILY_bsd 43 # include "os_bsd.inline.hpp" 44 #endif 45 #include "utilities/bitMap.inline.hpp" 46 47 G1PageBasedVirtualSpace::G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size) : 48 _low_boundary(NULL), _high_boundary(NULL), _committed(), _page_size(0), _special(false), 49 _dirty(), _executable(false) { 50 initialize_with_page_size(rs, used_size, page_size); 51 } 52 53 void G1PageBasedVirtualSpace::initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size) { 54 guarantee(rs.is_reserved(), "Given reserved space must have been reserved already."); 55 56 vmassert(_low_boundary == NULL, "VirtualSpace already initialized"); 57 vmassert(page_size > 0, "Page size must be non-zero."); 58 59 guarantee(is_ptr_aligned(rs.base(), page_size), 60 err_msg("Reserved space base " PTR_FORMAT " is not aligned to requested page size " SIZE_FORMAT, p2i(rs.base()), page_size)); 61 guarantee(is_size_aligned(used_size, os::vm_page_size()), 62 err_msg("Given used reserved space size needs to be OS page size aligned (%d bytes) but is " SIZE_FORMAT, os::vm_page_size(), used_size)); 63 guarantee(used_size <= rs.size(), 64 err_msg("Used size of reserved space " SIZE_FORMAT " bytes is smaller than reservation at " SIZE_FORMAT " bytes", used_size, rs.size())); 65 guarantee(is_size_aligned(rs.size(), page_size), 66 err_msg("Expected that the virtual space is size aligned, but " SIZE_FORMAT " is not aligned to page size " SIZE_FORMAT, rs.size(), page_size)); 67 68 _low_boundary = rs.base(); 69 _high_boundary = _low_boundary + used_size; 70 71 _special = rs.special(); 72 _executable = rs.executable(); 73 74 _page_size = page_size; 75 76 vmassert(_committed.size() == 0, "virtual space initialized more than once"); 77 BitMap::idx_t size_in_pages = rs.size() / page_size; 78 _committed.resize(size_in_pages, /* in_resource_area */ false); 79 if (_special) { 80 _dirty.resize(size_in_pages, /* in_resource_area */ false); 81 } 82 83 _tail_size = used_size % _page_size; 84 } 85 86 G1PageBasedVirtualSpace::~G1PageBasedVirtualSpace() { 87 release(); 88 } 89 90 void G1PageBasedVirtualSpace::release() { 91 // This does not release memory it never reserved. 92 // Caller must release via rs.release(); 93 _low_boundary = NULL; 94 _high_boundary = NULL; 95 _special = false; 96 _executable = false; 97 _page_size = 0; 98 _tail_size = 0; 99 _committed.resize(0, false); 100 _dirty.resize(0, false); 101 } 102 103 size_t G1PageBasedVirtualSpace::committed_size() const { 104 size_t result = _committed.count_one_bits() * _page_size; 105 // The last page might not be in full. 106 if (is_last_page_partial() && _committed.at(_committed.size() - 1)) { 107 result -= _page_size - _tail_size; 108 } 109 return result; 110 } 111 112 size_t G1PageBasedVirtualSpace::reserved_size() const { 113 return pointer_delta(_high_boundary, _low_boundary, sizeof(char)); 114 } 115 116 size_t G1PageBasedVirtualSpace::uncommitted_size() const { 117 return reserved_size() - committed_size(); 118 } 119 120 uintptr_t G1PageBasedVirtualSpace::addr_to_page_index(char* addr) const { 121 return (addr - _low_boundary) / _page_size; 122 } 123 124 bool G1PageBasedVirtualSpace::is_area_committed(uintptr_t start, size_t size_in_pages) const { 125 uintptr_t end = start + size_in_pages; 126 return _committed.get_next_zero_offset(start, end) >= end; 127 } 128 129 bool G1PageBasedVirtualSpace::is_area_uncommitted(uintptr_t start, size_t size_in_pages) const { 130 uintptr_t end = start + size_in_pages; 131 return _committed.get_next_one_offset(start, end) >= end; 132 } 133 134 char* G1PageBasedVirtualSpace::page_start(uintptr_t index) { 135 return _low_boundary + index * _page_size; 136 } 137 138 size_t G1PageBasedVirtualSpace::byte_size_for_pages(size_t num) { 139 return num * _page_size; 140 } 141 142 bool G1PageBasedVirtualSpace::commit(uintptr_t start, size_t size_in_pages) { 143 // We need to make sure to commit all pages covered by the given area. 144 guarantee(is_area_uncommitted(start, size_in_pages), "Specified area is not uncommitted"); 145 146 bool zero_filled = true; 147 uintptr_t end = start + size_in_pages; 148 149 if (_special) { 150 // Check for dirty pages and update zero_filled if any found. 151 if (_dirty.get_next_one_offset(start,end) < end) { 152 zero_filled = false; 153 _dirty.clear_range(start, end); 154 } 155 } else { 156 os::commit_memory_or_exit(page_start(start), byte_size_for_pages(size_in_pages), _executable, 157 err_msg("Failed to commit pages from "SIZE_FORMAT" of length "SIZE_FORMAT, start, size_in_pages)); 158 } 159 _committed.set_range(start, end); 160 161 if (AlwaysPreTouch) { 162 os::pretouch_memory(page_start(start), page_start(start) + byte_size_for_pages(size_in_pages)); 163 } 164 return zero_filled; 165 } 166 167 void G1PageBasedVirtualSpace::uncommit(uintptr_t start, size_t size_in_pages) { 168 guarantee(is_area_committed(start, size_in_pages), "checking"); 169 170 if (_special) { 171 // Mark that memory is dirty. If committed again the memory might 172 // need to be cleared explicitly. 173 _dirty.set_range(start, start + size_in_pages); 174 } else { 175 os::uncommit_memory(page_start(start), byte_size_for_pages(size_in_pages)); 176 } 177 178 _committed.clear_range(start, start + size_in_pages); 179 } 180 181 bool G1PageBasedVirtualSpace::contains(const void* p) const { 182 return _low_boundary <= (const char*) p && (const char*) p < _high_boundary; 183 } 184 185 #ifndef PRODUCT 186 void G1PageBasedVirtualSpace::print_on(outputStream* out) { 187 out->print ("Virtual space:"); 188 if (_special) out->print(" (pinned in memory)"); 189 out->cr(); 190 out->print_cr(" - committed: " SIZE_FORMAT, committed_size()); 191 out->print_cr(" - reserved: " SIZE_FORMAT, reserved_size()); 192 out->print_cr(" - preferred page size: " SIZE_FORMAT, _page_size); 193 out->print_cr(" - [low_b, high_b]: [" PTR_FORMAT ", " PTR_FORMAT "]", p2i(_low_boundary), p2i(_high_boundary)); 194 } 195 196 void G1PageBasedVirtualSpace::print() { 197 print_on(tty); 198 } 199 #endif