duke@435: /* drchase@6680: * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/parallelScavenge/psVirtualspace.hpp" stefank@2314: #include "runtime/os.hpp" stefank@2314: #include "runtime/virtualspace.hpp" stefank@2314: #ifdef TARGET_OS_FAMILY_linux stefank@2314: # include "os_linux.inline.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_OS_FAMILY_solaris stefank@2314: # include "os_solaris.inline.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_OS_FAMILY_windows stefank@2314: # include "os_windows.inline.hpp" stefank@2314: #endif goetz@6461: #ifdef TARGET_OS_FAMILY_aix goetz@6461: # include "os_aix.inline.hpp" goetz@6461: #endif never@3156: #ifdef TARGET_OS_FAMILY_bsd never@3156: # include "os_bsd.inline.hpp" never@3156: #endif duke@435: drchase@6680: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC drchase@6680: duke@435: // PSVirtualSpace duke@435: duke@435: PSVirtualSpace::PSVirtualSpace(ReservedSpace rs, size_t alignment) : duke@435: _alignment(alignment) duke@435: { duke@435: set_reserved(rs); duke@435: set_committed(reserved_low_addr(), reserved_low_addr()); duke@435: DEBUG_ONLY(verify()); duke@435: } duke@435: duke@435: PSVirtualSpace::PSVirtualSpace(ReservedSpace rs) : duke@435: _alignment(os::vm_page_size()) duke@435: { duke@435: set_reserved(rs); duke@435: set_committed(reserved_low_addr(), reserved_low_addr()); duke@435: DEBUG_ONLY(verify()); duke@435: } duke@435: duke@435: // Deprecated. duke@435: PSVirtualSpace::PSVirtualSpace(): _alignment(os::vm_page_size()) { duke@435: } duke@435: duke@435: // Deprecated. duke@435: bool PSVirtualSpace::initialize(ReservedSpace rs, duke@435: size_t commit_size) { duke@435: set_reserved(rs); duke@435: set_committed(reserved_low_addr(), reserved_low_addr()); duke@435: duke@435: // Commit to initial size. duke@435: assert(commit_size <= rs.size(), "commit_size too big"); duke@435: bool result = commit_size > 0 ? expand_by(commit_size) : true; duke@435: DEBUG_ONLY(verify()); duke@435: return result; duke@435: } duke@435: duke@435: PSVirtualSpace::~PSVirtualSpace() { duke@435: release(); duke@435: } duke@435: duke@435: bool PSVirtualSpace::contains(void* p) const { duke@435: char* const cp = (char*)p; duke@435: return cp >= committed_low_addr() && cp < committed_high_addr(); duke@435: } duke@435: duke@435: void PSVirtualSpace::release() { duke@435: DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); coleenp@672: // This may not release memory it didn't reserve. coleenp@672: // Use rs.release() to release the underlying memory instead. duke@435: _reserved_low_addr = _reserved_high_addr = NULL; duke@435: _committed_low_addr = _committed_high_addr = NULL; duke@435: _special = false; duke@435: } duke@435: iveresov@970: bool PSVirtualSpace::expand_by(size_t bytes) { duke@435: assert(is_aligned(bytes), "arg not aligned"); duke@435: DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); duke@435: duke@435: if (uncommitted_size() < bytes) { duke@435: return false; duke@435: } duke@435: duke@435: char* const base_addr = committed_high_addr(); dcubed@5255: bool result = special() || dcubed@5255: os::commit_memory(base_addr, bytes, alignment(), !ExecMem); duke@435: if (result) { duke@435: _committed_high_addr += bytes; duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: duke@435: bool PSVirtualSpace::shrink_by(size_t bytes) { duke@435: assert(is_aligned(bytes), "arg not aligned"); duke@435: DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); duke@435: duke@435: if (committed_size() < bytes) { duke@435: return false; duke@435: } duke@435: duke@435: char* const base_addr = committed_high_addr() - bytes; duke@435: bool result = special() || os::uncommit_memory(base_addr, bytes); duke@435: if (result) { duke@435: _committed_high_addr -= bytes; duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: duke@435: size_t duke@435: PSVirtualSpace::expand_into(PSVirtualSpace* other_space, size_t bytes) { duke@435: assert(is_aligned(bytes), "arg not aligned"); duke@435: assert(grows_up(), "this space must grow up"); duke@435: assert(other_space->grows_down(), "other space must grow down"); duke@435: assert(reserved_high_addr() == other_space->reserved_low_addr(), duke@435: "spaces not contiguous"); duke@435: assert(special() == other_space->special(), "one space is special, the other is not"); duke@435: DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); duke@435: DEBUG_ONLY(PSVirtualSpaceVerifier other_verifier(other_space)); duke@435: duke@435: size_t bytes_needed = bytes; duke@435: duke@435: // First use the uncommitted region in this space. duke@435: size_t tmp_bytes = MIN2(uncommitted_size(), bytes_needed); duke@435: if (tmp_bytes > 0) { duke@435: if (expand_by(tmp_bytes)) { duke@435: bytes_needed -= tmp_bytes; duke@435: } else { duke@435: return 0; duke@435: } duke@435: } duke@435: duke@435: // Next take from the uncommitted region in the other space, and commit it. duke@435: tmp_bytes = MIN2(other_space->uncommitted_size(), bytes_needed); duke@435: if (tmp_bytes > 0) { duke@435: char* const commit_base = committed_high_addr(); duke@435: if (other_space->special() || dcubed@5255: os::commit_memory(commit_base, tmp_bytes, alignment(), !ExecMem)) { duke@435: // Reduce the reserved region in the other space. duke@435: other_space->set_reserved(other_space->reserved_low_addr() + tmp_bytes, duke@435: other_space->reserved_high_addr(), duke@435: other_space->special()); duke@435: duke@435: // Grow both reserved and committed in this space. duke@435: _reserved_high_addr += tmp_bytes; duke@435: _committed_high_addr += tmp_bytes; duke@435: bytes_needed -= tmp_bytes; duke@435: } else { duke@435: return bytes - bytes_needed; duke@435: } duke@435: } duke@435: duke@435: // Finally take from the already committed region in the other space. duke@435: tmp_bytes = bytes_needed; duke@435: if (tmp_bytes > 0) { duke@435: // Reduce both committed and reserved in the other space. duke@435: other_space->set_committed(other_space->committed_low_addr() + tmp_bytes, duke@435: other_space->committed_high_addr()); duke@435: other_space->set_reserved(other_space->reserved_low_addr() + tmp_bytes, duke@435: other_space->reserved_high_addr(), duke@435: other_space->special()); duke@435: duke@435: // Grow both reserved and committed in this space. duke@435: _reserved_high_addr += tmp_bytes; duke@435: _committed_high_addr += tmp_bytes; duke@435: } duke@435: duke@435: return bytes; duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: bool PSVirtualSpace::is_aligned(size_t value, size_t align) { duke@435: const size_t tmp_value = value + align - 1; duke@435: const size_t mask = ~(align - 1); duke@435: return (tmp_value & mask) == value; duke@435: } duke@435: duke@435: bool PSVirtualSpace::is_aligned(size_t value) const { duke@435: return is_aligned(value, alignment()); duke@435: } duke@435: duke@435: bool PSVirtualSpace::is_aligned(char* value) const { duke@435: return is_aligned((size_t)value); duke@435: } duke@435: duke@435: void PSVirtualSpace::verify() const { duke@435: assert(is_aligned(alignment(), os::vm_page_size()), "bad alignment"); duke@435: assert(is_aligned(reserved_low_addr()), "bad reserved_low_addr"); duke@435: assert(is_aligned(reserved_high_addr()), "bad reserved_high_addr"); duke@435: assert(is_aligned(committed_low_addr()), "bad committed_low_addr"); duke@435: assert(is_aligned(committed_high_addr()), "bad committed_high_addr"); duke@435: duke@435: // Reserved region must be non-empty or both addrs must be 0. duke@435: assert(reserved_low_addr() < reserved_high_addr() || duke@435: reserved_low_addr() == NULL && reserved_high_addr() == NULL, duke@435: "bad reserved addrs"); duke@435: assert(committed_low_addr() <= committed_high_addr(), "bad committed addrs"); duke@435: duke@435: if (grows_up()) { duke@435: assert(reserved_low_addr() == committed_low_addr(), "bad low addrs"); duke@435: assert(reserved_high_addr() >= committed_high_addr(), "bad high addrs"); duke@435: } else { duke@435: assert(reserved_high_addr() == committed_high_addr(), "bad high addrs"); duke@435: assert(reserved_low_addr() <= committed_low_addr(), "bad low addrs"); duke@435: } duke@435: } duke@435: duke@435: void PSVirtualSpace::print() const { duke@435: gclog_or_tty->print_cr("virtual space [" PTR_FORMAT "]: alignment=" duke@435: SIZE_FORMAT "K grows %s%s", duke@435: this, alignment() / K, grows_up() ? "up" : "down", duke@435: special() ? " (pinned in memory)" : ""); duke@435: gclog_or_tty->print_cr(" reserved=" SIZE_FORMAT "K" duke@435: " [" PTR_FORMAT "," PTR_FORMAT "]" duke@435: " committed=" SIZE_FORMAT "K" duke@435: " [" PTR_FORMAT "," PTR_FORMAT "]", duke@435: reserved_size() / K, duke@435: reserved_low_addr(), reserved_high_addr(), duke@435: committed_size() / K, duke@435: committed_low_addr(), committed_high_addr()); duke@435: } duke@435: #endif // #ifndef PRODUCT duke@435: duke@435: void PSVirtualSpace::print_space_boundaries_on(outputStream* st) const { duke@435: st->print_cr(" [" PTR_FORMAT ", " PTR_FORMAT ", " PTR_FORMAT ")", duke@435: low_boundary(), high(), high_boundary()); duke@435: } duke@435: duke@435: PSVirtualSpaceHighToLow::PSVirtualSpaceHighToLow(ReservedSpace rs, duke@435: size_t alignment) : duke@435: PSVirtualSpace(alignment) duke@435: { duke@435: set_reserved(rs); duke@435: set_committed(reserved_high_addr(), reserved_high_addr()); duke@435: DEBUG_ONLY(verify()); duke@435: } duke@435: duke@435: PSVirtualSpaceHighToLow::PSVirtualSpaceHighToLow(ReservedSpace rs) { duke@435: set_reserved(rs); duke@435: set_committed(reserved_high_addr(), reserved_high_addr()); duke@435: DEBUG_ONLY(verify()); duke@435: } duke@435: iveresov@970: bool PSVirtualSpaceHighToLow::expand_by(size_t bytes) { duke@435: assert(is_aligned(bytes), "arg not aligned"); duke@435: DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); duke@435: duke@435: if (uncommitted_size() < bytes) { duke@435: return false; duke@435: } duke@435: duke@435: char* const base_addr = committed_low_addr() - bytes; dcubed@5255: bool result = special() || dcubed@5255: os::commit_memory(base_addr, bytes, alignment(), !ExecMem); duke@435: if (result) { duke@435: _committed_low_addr -= bytes; duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: duke@435: bool PSVirtualSpaceHighToLow::shrink_by(size_t bytes) { duke@435: assert(is_aligned(bytes), "arg not aligned"); duke@435: DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); duke@435: duke@435: if (committed_size() < bytes) { duke@435: return false; duke@435: } duke@435: duke@435: char* const base_addr = committed_low_addr(); duke@435: bool result = special() || os::uncommit_memory(base_addr, bytes); duke@435: if (result) { duke@435: _committed_low_addr += bytes; duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: duke@435: size_t PSVirtualSpaceHighToLow::expand_into(PSVirtualSpace* other_space, duke@435: size_t bytes) { duke@435: assert(is_aligned(bytes), "arg not aligned"); duke@435: assert(grows_down(), "this space must grow down"); duke@435: assert(other_space->grows_up(), "other space must grow up"); duke@435: assert(reserved_low_addr() == other_space->reserved_high_addr(), duke@435: "spaces not contiguous"); duke@435: assert(special() == other_space->special(), "one space is special in memory, the other is not"); duke@435: DEBUG_ONLY(PSVirtualSpaceVerifier this_verifier(this)); duke@435: DEBUG_ONLY(PSVirtualSpaceVerifier other_verifier(other_space)); duke@435: duke@435: size_t bytes_needed = bytes; duke@435: duke@435: // First use the uncommitted region in this space. duke@435: size_t tmp_bytes = MIN2(uncommitted_size(), bytes_needed); duke@435: if (tmp_bytes > 0) { duke@435: if (expand_by(tmp_bytes)) { duke@435: bytes_needed -= tmp_bytes; duke@435: } else { duke@435: return 0; duke@435: } duke@435: } duke@435: duke@435: // Next take from the uncommitted region in the other space, and commit it. duke@435: tmp_bytes = MIN2(other_space->uncommitted_size(), bytes_needed); duke@435: if (tmp_bytes > 0) { duke@435: char* const commit_base = committed_low_addr() - tmp_bytes; duke@435: if (other_space->special() || dcubed@5255: os::commit_memory(commit_base, tmp_bytes, alignment(), !ExecMem)) { duke@435: // Reduce the reserved region in the other space. duke@435: other_space->set_reserved(other_space->reserved_low_addr(), duke@435: other_space->reserved_high_addr() - tmp_bytes, duke@435: other_space->special()); duke@435: duke@435: // Grow both reserved and committed in this space. duke@435: _reserved_low_addr -= tmp_bytes; duke@435: _committed_low_addr -= tmp_bytes; duke@435: bytes_needed -= tmp_bytes; duke@435: } else { duke@435: return bytes - bytes_needed; duke@435: } duke@435: } duke@435: duke@435: // Finally take from the already committed region in the other space. duke@435: tmp_bytes = bytes_needed; duke@435: if (tmp_bytes > 0) { duke@435: // Reduce both committed and reserved in the other space. duke@435: other_space->set_committed(other_space->committed_low_addr(), duke@435: other_space->committed_high_addr() - tmp_bytes); duke@435: other_space->set_reserved(other_space->reserved_low_addr(), duke@435: other_space->reserved_high_addr() - tmp_bytes, duke@435: other_space->special()); duke@435: duke@435: // Grow both reserved and committed in this space. duke@435: _reserved_low_addr -= tmp_bytes; duke@435: _committed_low_addr -= tmp_bytes; duke@435: } duke@435: duke@435: return bytes; duke@435: } duke@435: duke@435: void duke@435: PSVirtualSpaceHighToLow::print_space_boundaries_on(outputStream* st) const { duke@435: st->print_cr(" (" PTR_FORMAT ", " PTR_FORMAT ", " PTR_FORMAT "]", duke@435: high_boundary(), low(), low_boundary()); duke@435: }