tschatzl@7051: /* tschatzl@7781: * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. tschatzl@7051: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. tschatzl@7051: * tschatzl@7051: * This code is free software; you can redistribute it and/or modify it tschatzl@7051: * under the terms of the GNU General Public License version 2 only, as tschatzl@7051: * published by the Free Software Foundation. tschatzl@7051: * tschatzl@7051: * This code is distributed in the hope that it will be useful, but WITHOUT tschatzl@7051: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or tschatzl@7051: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License tschatzl@7051: * version 2 for more details (a copy is included in the LICENSE file that tschatzl@7051: * accompanied this code). tschatzl@7051: * tschatzl@7051: * You should have received a copy of the GNU General Public License version tschatzl@7051: * 2 along with this work; if not, write to the Free Software Foundation, tschatzl@7051: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. tschatzl@7051: * tschatzl@7051: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA tschatzl@7051: * or visit www.oracle.com if you need additional information or have any tschatzl@7051: * questions. tschatzl@7051: * tschatzl@7051: */ tschatzl@7051: tschatzl@7051: #include "precompiled.hpp" tschatzl@7051: #include "gc_implementation/g1/g1PageBasedVirtualSpace.hpp" tschatzl@7051: #include "oops/markOop.hpp" tschatzl@7051: #include "oops/oop.inline.hpp" tschatzl@7051: #include "services/memTracker.hpp" tschatzl@7051: #ifdef TARGET_OS_FAMILY_linux tschatzl@7051: # include "os_linux.inline.hpp" tschatzl@7051: #endif tschatzl@7051: #ifdef TARGET_OS_FAMILY_solaris tschatzl@7051: # include "os_solaris.inline.hpp" tschatzl@7051: #endif tschatzl@7051: #ifdef TARGET_OS_FAMILY_windows tschatzl@7051: # include "os_windows.inline.hpp" tschatzl@7051: #endif tschatzl@7051: #ifdef TARGET_OS_FAMILY_aix tschatzl@7051: # include "os_aix.inline.hpp" tschatzl@7051: #endif tschatzl@7051: #ifdef TARGET_OS_FAMILY_bsd tschatzl@7051: # include "os_bsd.inline.hpp" tschatzl@7051: #endif tschatzl@7051: #include "utilities/bitMap.inline.hpp" tschatzl@7051: tschatzl@7781: G1PageBasedVirtualSpace::G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size) : tschatzl@7781: _low_boundary(NULL), _high_boundary(NULL), _committed(), _page_size(0), _special(false), sjohanss@7509: _dirty(), _executable(false) { tschatzl@7781: initialize_with_page_size(rs, used_size, page_size); tschatzl@7051: } tschatzl@7051: tschatzl@7781: void G1PageBasedVirtualSpace::initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size) { tschatzl@7781: guarantee(rs.is_reserved(), "Given reserved space must have been reserved already."); tschatzl@7781: tschatzl@7051: assert(_low_boundary == NULL, "VirtualSpace already initialized"); tschatzl@7781: assert(page_size > 0, "Page size must be non-zero."); tschatzl@7781: tschatzl@7781: guarantee(is_ptr_aligned(rs.base(), page_size), tschatzl@7781: err_msg("Reserved space base " PTR_FORMAT " is not aligned to requested page size " SIZE_FORMAT, p2i(rs.base()), page_size)); tschatzl@7781: guarantee(is_size_aligned(used_size, os::vm_page_size()), tschatzl@7781: 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)); tschatzl@7781: guarantee(used_size <= rs.size(), tschatzl@7781: err_msg("Used size of reserved space " SIZE_FORMAT " bytes is smaller than reservation at " SIZE_FORMAT " bytes", used_size, rs.size())); tschatzl@7781: guarantee(is_size_aligned(rs.size(), page_size), tschatzl@7781: 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)); tschatzl@7051: tschatzl@7051: _low_boundary = rs.base(); tschatzl@7781: _high_boundary = _low_boundary + used_size; tschatzl@7051: tschatzl@7051: _special = rs.special(); tschatzl@7051: _executable = rs.executable(); tschatzl@7051: tschatzl@7051: _page_size = page_size; tschatzl@7051: tschatzl@7051: assert(_committed.size() == 0, "virtual space initialized more than once"); tschatzl@7781: BitMap::idx_t size_in_pages = rs.size() / page_size; tschatzl@7781: _committed.resize(size_in_pages, /* in_resource_area */ false); sjohanss@7509: if (_special) { tschatzl@7781: _dirty.resize(size_in_pages, /* in_resource_area */ false); sjohanss@7509: } tschatzl@7051: tschatzl@7781: _tail_size = used_size % _page_size; tschatzl@7051: } tschatzl@7051: tschatzl@7051: G1PageBasedVirtualSpace::~G1PageBasedVirtualSpace() { tschatzl@7051: release(); tschatzl@7051: } tschatzl@7051: tschatzl@7051: void G1PageBasedVirtualSpace::release() { tschatzl@7051: // This does not release memory it never reserved. tschatzl@7051: // Caller must release via rs.release(); tschatzl@7051: _low_boundary = NULL; tschatzl@7051: _high_boundary = NULL; tschatzl@7051: _special = false; tschatzl@7051: _executable = false; tschatzl@7051: _page_size = 0; tschatzl@7781: _tail_size = 0; tschatzl@7051: _committed.resize(0, false); sjohanss@7509: _dirty.resize(0, false); tschatzl@7051: } tschatzl@7051: tschatzl@7051: size_t G1PageBasedVirtualSpace::committed_size() const { tschatzl@7781: size_t result = _committed.count_one_bits() * _page_size; tschatzl@7781: // The last page might not be in full. tschatzl@7781: if (is_last_page_partial() && _committed.at(_committed.size() - 1)) { tschatzl@7781: result -= _page_size - _tail_size; tschatzl@7781: } tschatzl@7781: return result; tschatzl@7051: } tschatzl@7051: tschatzl@7051: size_t G1PageBasedVirtualSpace::reserved_size() const { tschatzl@7051: return pointer_delta(_high_boundary, _low_boundary, sizeof(char)); tschatzl@7051: } tschatzl@7051: tschatzl@7051: size_t G1PageBasedVirtualSpace::uncommitted_size() const { tschatzl@7051: return reserved_size() - committed_size(); tschatzl@7051: } tschatzl@7051: tschatzl@7781: size_t G1PageBasedVirtualSpace::addr_to_page_index(char* addr) const { tschatzl@7051: return (addr - _low_boundary) / _page_size; tschatzl@7051: } tschatzl@7051: tschatzl@7781: bool G1PageBasedVirtualSpace::is_area_committed(size_t start_page, size_t size_in_pages) const { tschatzl@7781: size_t end_page = start_page + size_in_pages; tschatzl@7781: return _committed.get_next_zero_offset(start_page, end_page) >= end_page; tschatzl@7051: } tschatzl@7051: tschatzl@7781: bool G1PageBasedVirtualSpace::is_area_uncommitted(size_t start_page, size_t size_in_pages) const { tschatzl@7781: size_t end_page = start_page + size_in_pages; tschatzl@7781: return _committed.get_next_one_offset(start_page, end_page) >= end_page; tschatzl@7051: } tschatzl@7051: tschatzl@7781: char* G1PageBasedVirtualSpace::page_start(size_t index) const { tschatzl@7051: return _low_boundary + index * _page_size; tschatzl@7051: } tschatzl@7051: tschatzl@7781: bool G1PageBasedVirtualSpace::is_after_last_page(size_t index) const { tschatzl@7781: guarantee(index <= _committed.size(), tschatzl@7781: err_msg("Given boundary page " SIZE_FORMAT " is beyond managed page count " SIZE_FORMAT, index, _committed.size())); tschatzl@7781: return index == _committed.size(); tschatzl@7051: } tschatzl@7051: tschatzl@7781: void G1PageBasedVirtualSpace::commit_preferred_pages(size_t start, size_t num_pages) { tschatzl@7781: assert(num_pages > 0, "No full pages to commit"); tschatzl@7781: assert(start + num_pages <= _committed.size(), tschatzl@7781: err_msg("Tried to commit area from page " SIZE_FORMAT " to page " SIZE_FORMAT " " tschatzl@7781: "that is outside of managed space of " SIZE_FORMAT " pages", tschatzl@7781: start, start + num_pages, _committed.size())); tschatzl@7781: tschatzl@7781: char* start_addr = page_start(start); tschatzl@7781: size_t size = num_pages * _page_size; tschatzl@7781: tschatzl@7781: os::commit_memory_or_exit(start_addr, size, _page_size, _executable, tschatzl@7781: err_msg("Failed to commit area from " PTR_FORMAT " to " PTR_FORMAT " of length " SIZE_FORMAT ".", tschatzl@7781: p2i(start_addr), p2i(start_addr + size), size)); tschatzl@7781: } tschatzl@7781: tschatzl@7781: void G1PageBasedVirtualSpace::commit_tail() { tschatzl@7781: assert(_tail_size > 0, "The size of the tail area must be > 0 when reaching here"); tschatzl@7781: tschatzl@7781: char* const aligned_end_address = (char*)align_ptr_down(_high_boundary, _page_size); tschatzl@7781: os::commit_memory_or_exit(aligned_end_address, _tail_size, os::vm_page_size(), _executable, tschatzl@7781: err_msg("Failed to commit tail area from " PTR_FORMAT " to " PTR_FORMAT " of length " SIZE_FORMAT ".", tschatzl@7781: p2i(aligned_end_address), p2i(_high_boundary), _tail_size)); tschatzl@7781: } tschatzl@7781: tschatzl@7781: void G1PageBasedVirtualSpace::commit_internal(size_t start_page, size_t end_page) { tschatzl@7781: guarantee(start_page < end_page, tschatzl@7781: err_msg("Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page)); tschatzl@7781: guarantee(end_page <= _committed.size(), tschatzl@7781: err_msg("Given end page " SIZE_FORMAT " is beyond end of managed page amount of " SIZE_FORMAT, end_page, _committed.size())); tschatzl@7781: tschatzl@7781: size_t pages = end_page - start_page; tschatzl@7781: bool need_to_commit_tail = is_after_last_page(end_page) && is_last_page_partial(); tschatzl@7781: tschatzl@7781: // If we have to commit some (partial) tail area, decrease the amount of pages to avoid tschatzl@7781: // committing that in the full-page commit code. tschatzl@7781: if (need_to_commit_tail) { tschatzl@7781: pages--; tschatzl@7781: } tschatzl@7781: tschatzl@7781: if (pages > 0) { tschatzl@7781: commit_preferred_pages(start_page, pages); tschatzl@7781: } tschatzl@7781: tschatzl@7781: if (need_to_commit_tail) { tschatzl@7781: commit_tail(); tschatzl@7781: } tschatzl@7781: } tschatzl@7781: tschatzl@7781: char* G1PageBasedVirtualSpace::bounded_end_addr(size_t end_page) const { tschatzl@7781: return MIN2(_high_boundary, page_start(end_page)); tschatzl@7781: } tschatzl@7781: tschatzl@7781: void G1PageBasedVirtualSpace::pretouch_internal(size_t start_page, size_t end_page) { tschatzl@7781: guarantee(start_page < end_page, tschatzl@7781: err_msg("Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page)); tschatzl@7781: tschatzl@7781: os::pretouch_memory(page_start(start_page), bounded_end_addr(end_page)); tschatzl@7781: } tschatzl@7781: tschatzl@7781: bool G1PageBasedVirtualSpace::commit(size_t start_page, size_t size_in_pages) { tschatzl@7051: // We need to make sure to commit all pages covered by the given area. tschatzl@7781: guarantee(is_area_uncommitted(start_page, size_in_pages), "Specified area is not uncommitted"); tschatzl@7051: sjohanss@7509: bool zero_filled = true; tschatzl@7781: size_t end_page = start_page + size_in_pages; sjohanss@7509: sjohanss@7509: if (_special) { sjohanss@7509: // Check for dirty pages and update zero_filled if any found. tschatzl@7781: if (_dirty.get_next_one_offset(start_page, end_page) < end_page) { sjohanss@7509: zero_filled = false; tschatzl@7781: _dirty.clear_range(start_page, end_page); sjohanss@7509: } sjohanss@7509: } else { tschatzl@7781: commit_internal(start_page, end_page); tschatzl@7051: } tschatzl@7781: _committed.set_range(start_page, end_page); tschatzl@7051: tschatzl@7777: if (AlwaysPreTouch) { tschatzl@7781: pretouch_internal(start_page, end_page); tschatzl@7777: } sjohanss@7509: return zero_filled; tschatzl@7051: } tschatzl@7051: tschatzl@7781: void G1PageBasedVirtualSpace::uncommit_internal(size_t start_page, size_t end_page) { tschatzl@7781: guarantee(start_page < end_page, tschatzl@7781: err_msg("Given start page " SIZE_FORMAT " is larger or equal to end page " SIZE_FORMAT, start_page, end_page)); tschatzl@7051: tschatzl@7781: char* start_addr = page_start(start_page); tschatzl@7781: os::uncommit_memory(start_addr, pointer_delta(bounded_end_addr(end_page), start_addr, sizeof(char))); tschatzl@7781: } tschatzl@7781: tschatzl@7781: void G1PageBasedVirtualSpace::uncommit(size_t start_page, size_t size_in_pages) { tschatzl@7781: guarantee(is_area_committed(start_page, size_in_pages), "checking"); tschatzl@7781: tschatzl@7781: size_t end_page = start_page + size_in_pages; sjohanss@7509: if (_special) { sjohanss@7509: // Mark that memory is dirty. If committed again the memory might sjohanss@7509: // need to be cleared explicitly. tschatzl@7781: _dirty.set_range(start_page, end_page); sjohanss@7509: } else { tschatzl@7781: uncommit_internal(start_page, end_page); tschatzl@7051: } tschatzl@7051: tschatzl@7781: _committed.clear_range(start_page, end_page); tschatzl@7051: } tschatzl@7051: tschatzl@7051: bool G1PageBasedVirtualSpace::contains(const void* p) const { tschatzl@7051: return _low_boundary <= (const char*) p && (const char*) p < _high_boundary; tschatzl@7051: } tschatzl@7051: tschatzl@7051: #ifndef PRODUCT tschatzl@7051: void G1PageBasedVirtualSpace::print_on(outputStream* out) { tschatzl@7051: out->print ("Virtual space:"); sjohanss@7509: if (_special) out->print(" (pinned in memory)"); tschatzl@7051: out->cr(); tschatzl@7051: out->print_cr(" - committed: " SIZE_FORMAT, committed_size()); tschatzl@7051: out->print_cr(" - reserved: " SIZE_FORMAT, reserved_size()); tschatzl@7781: out->print_cr(" - preferred page size: " SIZE_FORMAT, _page_size); tschatzl@7781: out->print_cr(" - [low_b, high_b]: [" PTR_FORMAT ", " PTR_FORMAT "]", p2i(_low_boundary), p2i(_high_boundary)); tschatzl@7051: } tschatzl@7051: tschatzl@7051: void G1PageBasedVirtualSpace::print() { tschatzl@7051: print_on(tty); tschatzl@7051: } tschatzl@7051: #endif