/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "oops/markOop.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/virtualspace.hpp"
#include "services/memTracker.hpp"
#ifdef TARGET_OS_FAMILY_linux
# include "os_linux.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_solaris
# include "os_solaris.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_windows
# include "os_windows.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_bsd
# include "os_bsd.inline.hpp"
#endif
// ReservedSpace
}
bool large,
char* requested_address,
const size_t noaccess_prefix) {
noaccess_prefix, false);
}
bool large,
bool executable) {
}
char *
const size_t prefix_size,
const size_t prefix_align,
const size_t suffix_size,
const size_t suffix_align)
{
return NULL; // Cannot do proper alignment.
}
if (beg_delta != 0) {
}
if (end_delta != 0) {
}
return (char*) (s + beg_delta);
}
}
// On some systems (e.g., windows), the address returned by os::reserve_memory()
// is the only addr that can be passed to os::release_memory(). If alignment
// was done by this class, that original address is _raw_base.
bool ok;
} else {
}
if (!ok) {
fatal("os::release_memory failed");
}
}
const size_t prefix_size,
const size_t prefix_align,
const size_t suffix_size,
const size_t suffix_align)
{
fatal("os::release_memory failed");
}
if (!os::can_release_partial_region()) {
}
#ifdef ASSERT
"alignment increased end addr");
"bad alignment of suffix");
}
#endif
return result;
}
// Helper method.
char* requested_address,
bool special)
{
return false; // did not fail
// Different reserve address may be acceptable in other cases
// but for compressed oops heap should be at requested address.
if (PrintCompressedOopsMode) {
tty->print_cr("Reserved memory not at requested address: " PTR_FORMAT " vs " PTR_FORMAT, base, requested_address);
}
// OS ignored requested address. Try different address.
if (special) {
fatal("os::release_memory_special failed");
}
} else {
}
}
return true;
}
const size_t prefix_align,
const size_t suffix_size,
const size_t suffix_align,
char* requested_address,
const size_t noaccess_prefix)
{
"prefix_size not divisible by prefix_align");
"suffix_size not divisible by suffix_align");
"suffix_align not divisible by prefix_align");
// Assert that if noaccess_prefix is used, it is the same as prefix_align.
assert(noaccess_prefix == 0 ||
// Add in noaccess_prefix to prefix_size;
// On systems where the entire region has to be reserved and committed up
// front, the compound alignment normally done by this method is unnecessary.
false);
return;
}
_size = 0;
_alignment = 0;
_special = false;
_noaccess_prefix = 0;
_executable = false;
// Optimistically try to reserve the exact size needed.
char* addr;
if (requested_address != 0) {
// OS ignored requested address. Try different address.
}
} else {
}
// Check whether the result has the needed alignment (unlikely unless
// prefix_align < suffix_align).
if (ofs != 0) {
// Wrong alignment. Release, allocate more space and do manual alignment.
//
// On most operating systems, another allocation with a somewhat larger size
// will return an address "close to" that of the previous allocation. The
// result is often the same address (if the kernel hands out virtual
// addresses from low to high), or an address that is offset by the increase
// in size. Exploit that to minimize the amount of extra space requested.
// Try an even larger region. If this fails, address space is exhausted.
}
if (requested_address != 0 &&
// As a result of the alignment constraints, the allocated addr differs
// from the requested address. Return back to the caller who can
// take remedial action (like try again without a requested address).
return;
}
}
}
char* requested_address,
const size_t noaccess_prefix,
bool executable) {
"size not aligned to os::vm_allocation_granularity()");
"alignment not aligned to os::vm_allocation_granularity()");
"not a power of 2");
// Assert that if noaccess_prefix is used, it is the same as alignment.
assert(noaccess_prefix == 0 ||
_size = 0;
_special = false;
_alignment = 0;
_noaccess_prefix = 0;
if (size == 0) {
return;
}
// If OS doesn't support demand paging for large page memory, we need
// to use reserve_memory_special() to reserve and pin the entire region.
if (requested_address != 0) {
}
if (special) {
// OS ignored requested address. Try different address.
return;
}
// Check alignment constraints
"Large pages returned a non-aligned address");
_special = true;
} else {
// failed; try to reserve regular memory below
if (PrintCompressedOopsMode) {
}
}
}
}
// Optimistically assume that the OSes returns an aligned base pointer.
// When reserving a large address range, most OSes seem to align to at
// least 64K.
// If the memory was requested at a particular address, use
// os::attempt_reserve_memory_at() to avoid over mapping something
// important. If available space is not detected, return NULL.
if (requested_address != 0) {
// OS ignored requested address. Try different address.
}
} else {
}
// Check alignment constraints
// Base not aligned, retry
// Make sure that size is aligned
if (requested_address != 0 &&
// As a result of the alignment constraints, the allocated base differs
// from the requested address. Return back to the caller who can
// take remedial action (like try again without a requested address).
return;
}
}
}
// Done
// Assert that if noaccess_prefix is used, it is the same as alignment.
assert(noaccess_prefix == 0 ||
"area must be distinguisable from marks for mark-sweep");
"area must be distinguisable from marks for mark-sweep");
}
bool special, bool executable) {
"size not allocation aligned");
_noaccess_prefix = 0;
}
if (split) {
}
executable());
return result;
}
return result;
}
}
}
}
}
if (is_reserved()) {
if (special()) {
} else{
}
_size = 0;
_noaccess_prefix = 0;
_special = false;
_executable = false;
}
}
"noaccess_prefix should be used only with non zero based compressed oops");
// If there is no noaccess prefix, return.
if (_noaccess_prefix == 0) return;
"must be at least page size big");
// Protect memory at the base of the allocated region.
// If special, the page was committed (only matters on windows)
_special)) {
fatal("cannot protect protection page");
}
if (PrintCompressedOopsMode) {
tty->print_cr("Protected page at the reserved heap base: " PTR_FORMAT " / " INTX_FORMAT " bytes", _base, _noaccess_prefix);
}
"must be exactly of required size and alignment");
}
bool large, char* requested_address) :
if (base() > 0) {
}
// Only reserved space for the java heap should have a noaccess_prefix
// if using compressed oops.
}
const size_t prefix_align,
const size_t suffix_size,
const size_t suffix_align,
char* requested_address) :
if (base() > 0) {
}
}
// Reserve space for code segment. Same as Java heap only we mark this as
// executable.
bool large) :
}
// VirtualSpace
_lower_high = NULL;
_middle_high = NULL;
_upper_high = NULL;
_lower_alignment = 0;
_middle_alignment = 0;
_upper_alignment = 0;
_special = false;
_executable = false;
}
_low = low_boundary();
// When a VirtualSpace begins life at a large size, make all future expansion
// and shrinking occur aligned to a granularity of large pages. This avoids
// fragmentation of physical addresses that inhibits the use of large pages
// by the OS virtual memory system. Empirically, we see that with a 4MB
// page size, the only spaces that get handled this way are codecache and
// the heap itself, both of which provide a substantial performance
// boost in many benchmarks when covered by large pages.
//
// No attempt is made to force large page alignment at the very top and
// bottom of the space if they are not aligned so already.
// End of each region
// High address of each region
_lower_high = low_boundary();
// commit to initial size
if (committed_size > 0) {
if (!expand_by(committed_size)) {
return false;
}
}
return true;
}
VirtualSpace::~VirtualSpace() {
release();
}
// This does not release memory it never reserved.
// Caller must release via rs.release();
_lower_high = NULL;
_middle_high = NULL;
_upper_high = NULL;
_lower_alignment = 0;
_middle_alignment = 0;
_upper_alignment = 0;
_special = false;
_executable = false;
}
}
}
return reserved_size() - committed_size();
}
}
/*
First we need to determine if a particular virtual space is using large
pages. This is done at the initialize function and only virtual spaces
that are larger than LargePageSizeInBytes use large pages. Once we
have determined this, all expand_by and shrink_by calls must grow and
shrink by large page size chunks. If a particular request
is within the current large page, the call to commit and uncommit memory
can be ignored. In the case that the low and high boundaries of this
space is not large page aligned, the pages leading to the first large
page address and the pages after the last large page address must be
allocated with default pages.
*/
if (uncommitted_size() < bytes) return false;
if (special()) {
// don't commit memory if the entire space is pinned in memory
return true;
}
"cannot expand by more than upper boundary");
// Calculate where the new high for each of the regions should be. If
// the low_boundary() and high_boundary() are LargePageSizeInBytes aligned
// then the unaligned lower and upper new highs would be the
// lower_high() and upper_high() respectively.
char* unaligned_lower_new_high =
char* unaligned_middle_new_high =
char* unaligned_upper_new_high =
// Align the new highs based on the regions alignment. lower and upper
// alignment will always be default page size. middle alignment will be
// LargePageSizeInBytes if the actual size of the virtual space is in
// fact larger than LargePageSizeInBytes.
char* aligned_lower_new_high =
char* aligned_middle_new_high =
char* aligned_upper_new_high =
// Determine which regions need to grow in this expand_by call.
// If you are growing in the lower region, high() must be in that
// region so calcuate the size based on high(). For the middle and
// upper regions, determine the starting point of growth based on the
// location of high(). By getting the MAX of the region's low address
// (or the prevoius region's high address) and high(), we can tell if it
// is an intra or inter region growth.
if (aligned_lower_new_high > lower_high()) {
}
if (aligned_middle_new_high > middle_high()) {
}
if (aligned_upper_new_high > upper_high()) {
}
// Check contiguity.
lower_high() <= lower_high_boundary(),
"high address must be contained within the region");
middle_high() <= middle_high_boundary(),
"high address must be contained within the region");
upper_high() <= upper_high_boundary(),
"high address must be contained within the region");
// Commit regions
if (lower_needs > 0) {
"must not expand beyond region");
return false;
} else {
}
}
if (middle_needs > 0) {
"must not expand beyond region");
_executable)) {
middle_alignment(), _executable);)
return false;
}
}
if (upper_needs > 0) {
"must not expand beyond region");
return false;
} else {
}
}
if (pre_touch || AlwaysPreTouch) {
for (char* curr = previous_high;
// Note the use of a write here; originally we tried just a read, but
// since the value read was unused, the optimizer removed the read.
// If we ever have a concurrent touchahead thread, we'll want to use
// a read, to avoid the potential of overwriting data (if a mutator
// thread beats the touchahead thread to a page). There are various
// ways of making sure this read is not optimized away: for example,
// generating the code for a read procedure at runtime.
*curr = 0;
}
}
return true;
}
// A page is uncommitted if the contents of the entire page is deemed unusable.
// Continue to decrement the high() pointer until it reaches a page boundary
// in which case that particular page can now be uncommitted.
if (committed_size() < size)
fatal("Cannot shrink virtual space to negative size");
if (special()) {
// don't uncommit if the entire space is pinned in memory
return;
}
// Calculate new unaligned address
char* unaligned_upper_new_high =
char* unaligned_middle_new_high =
char* unaligned_lower_new_high =
// Align address to region's alignment
char* aligned_upper_new_high =
char* aligned_middle_new_high =
char* aligned_lower_new_high =
// Determine which regions need to shrink
if (aligned_upper_new_high < upper_high()) {
}
if (aligned_middle_new_high < middle_high()) {
}
if (aligned_lower_new_high < lower_high()) {
}
// Check contiguity.
upper_high() <= upper_high_boundary(),
"high address must be contained within the region");
middle_high() <= middle_high_boundary(),
"high address must be contained within the region");
lower_high() <= lower_high_boundary(),
"high address must be contained within the region");
// Uncommit
if (upper_needs > 0) {
"must not shrink beyond region");
return;
} else {
}
}
if (middle_needs > 0) {
"must not shrink beyond region");
return;
} else {
}
}
if (lower_needs > 0) {
"must not shrink beyond region");
return;
} else {
}
}
}
#ifndef PRODUCT
// Check contiguity.
lower_high() <= lower_high_boundary(),
"high address must be contained within the region");
middle_high() <= middle_high_boundary(),
"high address must be contained within the region");
upper_high() <= upper_high_boundary(),
"high address must be contained within the region");
}
tty->print_cr(" - [low_b, high_b]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", low_boundary(), high_boundary());
}
#endif