1 /* 2 * Copyright (c) 2014, 2016, 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/g1PageBasedVirtualSpace.hpp" 27 #include "gc/shared/workgroup.hpp" 28 #include "oops/markOop.hpp" 29 #include "oops/oop.inline.hpp" 30 #include "runtime/atomic.hpp" 31 #include "runtime/os.inline.hpp" 32 #include "services/memTracker.hpp" 33 #include "utilities/bitMap.inline.hpp" 34 35 G1PageBasedVirtualSpace::G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size) : 36 _low_boundary(NULL), _high_boundary(NULL), _committed(), _page_size(0), _special(false), 37 _dirty(), _executable(false) { 38 initialize_with_page_size(rs, used_size, page_size); 39 } 40 41 void G1PageBasedVirtualSpace::initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size) { 42 guarantee(rs.is_reserved(), "Given reserved space must have been reserved already."); 43 44 vmassert(_low_boundary == NULL, "VirtualSpace already initialized"); 45 vmassert(page_size > 0, "Page size must be non-zero."); 46 47 guarantee(is_aligned(rs.base(), page_size), 48 "Reserved space base " PTR_FORMAT " is not aligned to requested page size " SIZE_FORMAT, p2i(rs.base()), page_size); 49 guarantee(is_aligned(used_size, os::vm_page_size()), 50 "Given used reserved space size needs to be OS page size aligned (%d bytes) but is " SIZE_FORMAT, os::vm_page_size(), used_size); 51 guarantee(used_size <= rs.size(), 52 "Used size of reserved space " SIZE_FORMAT " bytes is smaller than reservation at " SIZE_FORMAT " bytes", used_size, rs.size()); 53 guarantee(is_aligned(rs.size(), page_size), 54 "Expected that the virtual space is size aligned, but " SIZE_FORMAT " is not aligned to page size " SIZE_FORMAT, rs.size(), page_size); 55 56 _low_boundary = rs.base(); 57 _high_boundary = _low_boundary + used_size; 58 59 _special = rs.special(); 60 _executable = rs.executable(); 61 62 _page_size = page_size; 63 64 vmassert(_committed.size() == 0, "virtual space initialized more than once"); 65 BitMap::idx_t size_in_pages = rs.size() / page_size; 66 _committed.initialize(size_in_pages); 67 if (_special) { 68 _dirty.initialize(size_in_pages); 69 } 70 71 _tail_size = used_size % _page_size; 72 } 73 74 G1PageBasedVirtualSpace::~G1PageBasedVirtualSpace() { 75 // This does not release memory it never reserved. 76 // Caller must release via rs.release(); 77 _low_boundary = NULL; 78 _high_boundary = NULL; 79 _special = false; 80 _executable = false; 81 _page_size = 0; 82 _tail_size = 0; 83 } 84 85 size_t G1PageBasedVirtualSpace::committed_size() const { 86 size_t result = _committed.count_one_bits() * _page_size; 87 // The last page might not be in full. 88 if (is_last_page_partial() && _committed.at(_committed.size() - 1)) { 89 result -= _page_size - _tail_size; 90 } 91 return result; 92 } 93 94 size_t G1PageBasedVirtualSpace::reserved_size() const { 95 return pointer_delta(_high_boundary, _low_boundary, sizeof(char)); 96 } 97 98 size_t G1PageBasedVirtualSpace::uncommitted_size() const { 99 return reserved_size() - committed_size(); 100 } 101 102 size_t G1PageBasedVirtualSpace::addr_to_page_index(char* addr) const { 103 return (addr - _low_boundary) / _page_size; 104 } 105 106 bool G1PageBasedVirtualSpace::is_area_committed(size_t start_page, size_t size_in_pages) const { 107 size_t end_page = start_page + size_in_pages; 108 return _committed.get_next_zero_offset(start_page, end_page) >= end_page; 109 } 110 111 bool G1PageBasedVirtualSpace::is_area_uncommitted(size_t start_page, size_t size_in_pages) const { 112 size_t end_page = start_page + size_in_pages; 113 return _committed.get_next_one_offset(start_page, end_page) >= end_page; 114 } 115 116 char* G1PageBasedVirtualSpace::page_start(size_t index) const { 117 return _low_boundary + index * _page_size; 118 } 119 120 bool G1PageBasedVirtualSpace::is_after_last_page(size_t index) const { 121 guarantee(index <= _committed.size(), 122 "Given boundary page " SIZE_FORMAT " is beyond managed page count " SIZE_FORMAT, index, _committed.size()); 123 return index == _committed.size(); 124 } 125 126 void G1PageBasedVirtualSpace::commit_preferred_pages(size_t start, size_t num_pages) { 127 vmassert(num_pages > 0, "No full pages to commit"); 128 vmassert(start + num_pages <= _committed.size(), 129 "Tried to commit area from page " SIZE_FORMAT " to page " SIZE_FORMAT " " 130 "that is outside of managed space of " SIZE_FORMAT " pages", 131 start, start + num_pages, _committed.size()); 132 133 char* start_addr = page_start(start); 134 size_t size = num_pages * _page_size; 135 136 os::commit_memory_or_exit(start_addr, size, _page_size, _executable, 137 err_msg("Failed to commit area from " PTR_FORMAT " to " PTR_FORMAT " of length " SIZE_FORMAT ".", 138 p2i(start_addr), p2i(start_addr + size), size)); 139 } 140 141 void G1PageBasedVirtualSpace::commit_tail() { 142 vmassert(_tail_size > 0, "The size of the tail area must be > 0 when reaching here"); 143 144 char* const aligned_end_address = align_down(_high_boundary, _page_size); 145 os::commit_memory_or_exit(aligned_end_address, _tail_size, os::vm_page_size(), _executable, 146 err_msg("Failed to commit tail area from " PTR_FORMAT " to " PTR_FORMAT " of length " SIZE_FORMAT ".", 147 p2i(aligned_end_address), p2i(_high_boundary), _tail_size)); 148 } 149 150 void G1PageBasedVirtualSpace::commit_internal(size_t start_page, size_t end_page) { 151 guarantee(start_page < end_page, 152 "Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page); 153 guarantee(end_page <= _committed.size(), 154 "Given end page " SIZE_FORMAT " is beyond end of managed page amount of " SIZE_FORMAT, end_page, _committed.size()); 155 156 size_t pages = end_page - start_page; 157 bool need_to_commit_tail = is_after_last_page(end_page) && is_last_page_partial(); 158 159 // If we have to commit some (partial) tail area, decrease the amount of pages to avoid 160 // committing that in the full-page commit code. 161 if (need_to_commit_tail) { 162 pages--; 163 } 164 165 if (pages > 0) { 166 commit_preferred_pages(start_page, pages); 167 } 168 169 if (need_to_commit_tail) { 170 commit_tail(); 171 } 172 } 173 174 char* G1PageBasedVirtualSpace::bounded_end_addr(size_t end_page) const { 175 return MIN2(_high_boundary, page_start(end_page)); 176 } 177 178 void G1PageBasedVirtualSpace::pretouch_internal(size_t start_page, size_t end_page) { 179 guarantee(start_page < end_page, 180 "Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page); 181 182 os::pretouch_memory(page_start(start_page), bounded_end_addr(end_page), _page_size); 183 } 184 185 bool G1PageBasedVirtualSpace::commit(size_t start_page, size_t size_in_pages) { 186 // We need to make sure to commit all pages covered by the given area. 187 guarantee(is_area_uncommitted(start_page, size_in_pages), "Specified area is not uncommitted"); 188 189 bool zero_filled = true; 190 size_t end_page = start_page + size_in_pages; 191 192 if (_special) { 193 // Check for dirty pages and update zero_filled if any found. 194 if (_dirty.get_next_one_offset(start_page, end_page) < end_page) { 195 zero_filled = false; 196 _dirty.clear_range(start_page, end_page); 197 } 198 } else { 199 commit_internal(start_page, end_page); 200 } 201 _committed.set_range(start_page, end_page); 202 203 return zero_filled; 204 } 205 206 void G1PageBasedVirtualSpace::uncommit_internal(size_t start_page, size_t end_page) { 207 guarantee(start_page < end_page, 208 "Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page); 209 210 char* start_addr = page_start(start_page); 211 os::uncommit_memory(start_addr, pointer_delta(bounded_end_addr(end_page), start_addr, sizeof(char))); 212 } 213 214 void G1PageBasedVirtualSpace::uncommit(size_t start_page, size_t size_in_pages) { 215 guarantee(is_area_committed(start_page, size_in_pages), "checking"); 216 217 size_t end_page = start_page + size_in_pages; 218 if (_special) { 219 // Mark that memory is dirty. If committed again the memory might 220 // need to be cleared explicitly. 221 _dirty.set_range(start_page, end_page); 222 } else { 223 uncommit_internal(start_page, end_page); 224 } 225 226 _committed.clear_range(start_page, end_page); 227 } 228 229 class G1PretouchTask : public AbstractGangTask { 230 private: 231 char* volatile _cur_addr; 232 char* const _start_addr; 233 char* const _end_addr; 234 size_t const _page_size; 235 public: 236 G1PretouchTask(char* start_address, char* end_address, size_t page_size) : 237 AbstractGangTask("G1 PreTouch", 238 Universe::is_fully_initialized() && 239 Thread::current()->is_Named_thread() ? GCId::current_raw() : 240 // During VM initialization there is 241 // no GC cycle that this task can be 242 // associated with. 243 GCId::undefined()), 244 _cur_addr(start_address), 245 _start_addr(start_address), 246 _end_addr(end_address), 247 _page_size(page_size) { 248 } 249 250 virtual void work(uint worker_id) { 251 size_t const actual_chunk_size = MAX2(chunk_size(), _page_size); 252 while (true) { 253 char* touch_addr = (char*)Atomic::add_ptr((intptr_t)actual_chunk_size, (volatile void*) &_cur_addr) - actual_chunk_size; 254 if (touch_addr < _start_addr || touch_addr >= _end_addr) { 255 break; 256 } 257 char* end_addr = touch_addr + MIN2(actual_chunk_size, pointer_delta(_end_addr, touch_addr, sizeof(char))); 258 os::pretouch_memory(touch_addr, end_addr, _page_size); 259 } 260 } 261 262 static size_t chunk_size() { return PreTouchParallelChunkSize; } 263 }; 264 265 void G1PageBasedVirtualSpace::pretouch(size_t start_page, size_t size_in_pages, WorkGang* pretouch_gang) { 266 G1PretouchTask cl(page_start(start_page), bounded_end_addr(start_page + size_in_pages), _page_size); 267 268 if (pretouch_gang != NULL) { 269 size_t num_chunks = MAX2((size_t)1, size_in_pages * _page_size / MAX2(G1PretouchTask::chunk_size(), _page_size)); 270 271 uint num_workers = MIN2((uint)num_chunks, pretouch_gang->active_workers()); 272 log_debug(gc, heap)("Running %s with %u workers for " SIZE_FORMAT " work units pre-touching " SIZE_FORMAT "B.", 273 cl.name(), num_workers, num_chunks, size_in_pages * _page_size); 274 pretouch_gang->run_task(&cl, num_workers); 275 } else { 276 log_debug(gc, heap)("Running %s pre-touching " SIZE_FORMAT "B.", 277 cl.name(), size_in_pages * _page_size); 278 cl.work(0); 279 } 280 } 281 282 bool G1PageBasedVirtualSpace::contains(const void* p) const { 283 return _low_boundary <= (const char*) p && (const char*) p < _high_boundary; 284 } 285 286 #ifndef PRODUCT 287 void G1PageBasedVirtualSpace::print_on(outputStream* out) { 288 out->print ("Virtual space:"); 289 if (_special) out->print(" (pinned in memory)"); 290 out->cr(); 291 out->print_cr(" - committed: " SIZE_FORMAT, committed_size()); 292 out->print_cr(" - reserved: " SIZE_FORMAT, reserved_size()); 293 out->print_cr(" - preferred page size: " SIZE_FORMAT, _page_size); 294 out->print_cr(" - [low_b, high_b]: [" PTR_FORMAT ", " PTR_FORMAT "]", p2i(_low_boundary), p2i(_high_boundary)); 295 } 296 297 void G1PageBasedVirtualSpace::print() { 298 print_on(tty); 299 } 300 #endif