src/share/vm/runtime/virtualspace.cpp

Fri, 27 Sep 2013 08:39:19 +0200

author
rbackman
date
Fri, 27 Sep 2013 08:39:19 +0200
changeset 5791
c9ccd7b85f20
parent 5708
8c5e6482cbfc
child 5859
04b18a42c2f3
permissions
-rw-r--r--

8024924: Intrinsify java.lang.Math.addExact
Reviewed-by: kvn, twisti

duke@435 1 /*
hseigel@4465 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "oops/markOop.hpp"
stefank@2314 27 #include "oops/oop.inline.hpp"
stefank@2314 28 #include "runtime/virtualspace.hpp"
zgu@3900 29 #include "services/memTracker.hpp"
stefank@2314 30 #ifdef TARGET_OS_FAMILY_linux
stefank@2314 31 # include "os_linux.inline.hpp"
stefank@2314 32 #endif
stefank@2314 33 #ifdef TARGET_OS_FAMILY_solaris
stefank@2314 34 # include "os_solaris.inline.hpp"
stefank@2314 35 #endif
stefank@2314 36 #ifdef TARGET_OS_FAMILY_windows
stefank@2314 37 # include "os_windows.inline.hpp"
stefank@2314 38 #endif
never@3156 39 #ifdef TARGET_OS_FAMILY_bsd
never@3156 40 # include "os_bsd.inline.hpp"
never@3156 41 #endif
duke@435 42
duke@435 43
duke@435 44 // ReservedSpace
stefank@5578 45
stefank@5578 46 // Dummy constructor
stefank@5578 47 ReservedSpace::ReservedSpace() : _base(NULL), _size(0), _noaccess_prefix(0),
stefank@5578 48 _alignment(0), _special(false), _executable(false) {
stefank@5578 49 }
stefank@5578 50
duke@435 51 ReservedSpace::ReservedSpace(size_t size) {
stefank@5578 52 size_t page_size = os::page_size_for_region(size, size, 1);
stefank@5578 53 bool large_pages = page_size != (size_t)os::vm_page_size();
stefank@5578 54 // Don't force the alignment to be large page aligned,
stefank@5578 55 // since that will waste memory.
stefank@5578 56 size_t alignment = os::vm_allocation_granularity();
stefank@5578 57 initialize(size, alignment, large_pages, NULL, 0, false);
duke@435 58 }
duke@435 59
duke@435 60 ReservedSpace::ReservedSpace(size_t size, size_t alignment,
coleenp@672 61 bool large,
coleenp@672 62 char* requested_address,
coleenp@672 63 const size_t noaccess_prefix) {
coleenp@672 64 initialize(size+noaccess_prefix, alignment, large, requested_address,
coleenp@1091 65 noaccess_prefix, false);
coleenp@1091 66 }
coleenp@1091 67
coleenp@1091 68 ReservedSpace::ReservedSpace(size_t size, size_t alignment,
coleenp@1091 69 bool large,
coleenp@1091 70 bool executable) {
coleenp@1091 71 initialize(size, alignment, large, NULL, 0, executable);
duke@435 72 }
duke@435 73
kvn@1973 74 // Helper method.
kvn@1973 75 static bool failed_to_reserve_as_requested(char* base, char* requested_address,
kvn@1973 76 const size_t size, bool special)
kvn@1973 77 {
kvn@1973 78 if (base == requested_address || requested_address == NULL)
kvn@1973 79 return false; // did not fail
kvn@1973 80
kvn@1973 81 if (base != NULL) {
kvn@1973 82 // Different reserve address may be acceptable in other cases
kvn@1973 83 // but for compressed oops heap should be at requested address.
kvn@1973 84 assert(UseCompressedOops, "currently requested address used only for compressed oops");
kvn@1973 85 if (PrintCompressedOopsMode) {
kvn@1973 86 tty->cr();
johnc@3022 87 tty->print_cr("Reserved memory not at requested address: " PTR_FORMAT " vs " PTR_FORMAT, base, requested_address);
kvn@1973 88 }
kvn@1973 89 // OS ignored requested address. Try different address.
kvn@1973 90 if (special) {
kvn@1973 91 if (!os::release_memory_special(base, size)) {
kvn@1973 92 fatal("os::release_memory_special failed");
kvn@1973 93 }
kvn@1973 94 } else {
kvn@1973 95 if (!os::release_memory(base, size)) {
kvn@1973 96 fatal("os::release_memory failed");
kvn@1973 97 }
kvn@1973 98 }
kvn@1973 99 }
kvn@1973 100 return true;
kvn@1973 101 }
kvn@1973 102
duke@435 103 void ReservedSpace::initialize(size_t size, size_t alignment, bool large,
coleenp@672 104 char* requested_address,
coleenp@1091 105 const size_t noaccess_prefix,
coleenp@1091 106 bool executable) {
duke@435 107 const size_t granularity = os::vm_allocation_granularity();
johnc@3022 108 assert((size & (granularity - 1)) == 0,
duke@435 109 "size not aligned to os::vm_allocation_granularity()");
johnc@3022 110 assert((alignment & (granularity - 1)) == 0,
duke@435 111 "alignment not aligned to os::vm_allocation_granularity()");
duke@435 112 assert(alignment == 0 || is_power_of_2((intptr_t)alignment),
duke@435 113 "not a power of 2");
duke@435 114
johnc@3022 115 alignment = MAX2(alignment, (size_t)os::vm_page_size());
johnc@3022 116
johnc@3022 117 // Assert that if noaccess_prefix is used, it is the same as alignment.
johnc@3022 118 assert(noaccess_prefix == 0 ||
johnc@3022 119 noaccess_prefix == alignment, "noaccess prefix wrong");
johnc@3022 120
duke@435 121 _base = NULL;
duke@435 122 _size = 0;
duke@435 123 _special = false;
coleenp@1091 124 _executable = executable;
duke@435 125 _alignment = 0;
coleenp@672 126 _noaccess_prefix = 0;
duke@435 127 if (size == 0) {
duke@435 128 return;
duke@435 129 }
duke@435 130
duke@435 131 // If OS doesn't support demand paging for large page memory, we need
duke@435 132 // to use reserve_memory_special() to reserve and pin the entire region.
duke@435 133 bool special = large && !os::can_commit_large_page_memory();
duke@435 134 char* base = NULL;
duke@435 135
kvn@1973 136 if (requested_address != 0) {
kvn@1973 137 requested_address -= noaccess_prefix; // adjust requested address
kvn@1973 138 assert(requested_address != NULL, "huge noaccess prefix?");
kvn@1973 139 }
kvn@1973 140
duke@435 141 if (special) {
duke@435 142
stefank@5578 143 base = os::reserve_memory_special(size, alignment, requested_address, executable);
duke@435 144
duke@435 145 if (base != NULL) {
kvn@1973 146 if (failed_to_reserve_as_requested(base, requested_address, size, true)) {
kvn@1973 147 // OS ignored requested address. Try different address.
kvn@1973 148 return;
kvn@1973 149 }
stefank@5578 150 // Check alignment constraints.
johnc@3022 151 assert((uintptr_t) base % alignment == 0,
stefank@5578 152 err_msg("Large pages returned a non-aligned address, base: "
stefank@5578 153 PTR_FORMAT " alignment: " PTR_FORMAT,
stefank@5578 154 base, (void*)(uintptr_t)alignment));
duke@435 155 _special = true;
duke@435 156 } else {
duke@435 157 // failed; try to reserve regular memory below
kvn@1973 158 if (UseLargePages && (!FLAG_IS_DEFAULT(UseLargePages) ||
kvn@1973 159 !FLAG_IS_DEFAULT(LargePageSizeInBytes))) {
kvn@1973 160 if (PrintCompressedOopsMode) {
kvn@1973 161 tty->cr();
kvn@1973 162 tty->print_cr("Reserve regular memory without large pages.");
kvn@1973 163 }
kvn@1973 164 }
duke@435 165 }
duke@435 166 }
duke@435 167
duke@435 168 if (base == NULL) {
duke@435 169 // Optimistically assume that the OSes returns an aligned base pointer.
duke@435 170 // When reserving a large address range, most OSes seem to align to at
duke@435 171 // least 64K.
duke@435 172
duke@435 173 // If the memory was requested at a particular address, use
duke@435 174 // os::attempt_reserve_memory_at() to avoid over mapping something
duke@435 175 // important. If available space is not detected, return NULL.
duke@435 176
duke@435 177 if (requested_address != 0) {
kvn@1973 178 base = os::attempt_reserve_memory_at(size, requested_address);
kvn@1973 179 if (failed_to_reserve_as_requested(base, requested_address, size, false)) {
kvn@1973 180 // OS ignored requested address. Try different address.
kvn@1973 181 base = NULL;
kvn@1973 182 }
duke@435 183 } else {
duke@435 184 base = os::reserve_memory(size, NULL, alignment);
duke@435 185 }
duke@435 186
duke@435 187 if (base == NULL) return;
duke@435 188
duke@435 189 // Check alignment constraints
johnc@3022 190 if ((((size_t)base + noaccess_prefix) & (alignment - 1)) != 0) {
duke@435 191 // Base not aligned, retry
duke@435 192 if (!os::release_memory(base, size)) fatal("os::release_memory failed");
brutisso@4369 193 // Make sure that size is aligned
duke@435 194 size = align_size_up(size, alignment);
brutisso@4369 195 base = os::reserve_memory_aligned(size, alignment);
johnc@3022 196
johnc@3022 197 if (requested_address != 0 &&
johnc@3022 198 failed_to_reserve_as_requested(base, requested_address, size, false)) {
johnc@3022 199 // As a result of the alignment constraints, the allocated base differs
johnc@3022 200 // from the requested address. Return back to the caller who can
johnc@3022 201 // take remedial action (like try again without a requested address).
johnc@3022 202 assert(_base == NULL, "should be");
johnc@3022 203 return;
johnc@3022 204 }
duke@435 205 }
duke@435 206 }
duke@435 207 // Done
duke@435 208 _base = base;
duke@435 209 _size = size;
johnc@3022 210 _alignment = alignment;
coleenp@672 211 _noaccess_prefix = noaccess_prefix;
coleenp@672 212
coleenp@672 213 // Assert that if noaccess_prefix is used, it is the same as alignment.
coleenp@672 214 assert(noaccess_prefix == 0 ||
coleenp@672 215 noaccess_prefix == _alignment, "noaccess prefix wrong");
duke@435 216
duke@435 217 assert(markOopDesc::encode_pointer_as_mark(_base)->decode_pointer() == _base,
duke@435 218 "area must be distinguisable from marks for mark-sweep");
duke@435 219 assert(markOopDesc::encode_pointer_as_mark(&_base[size])->decode_pointer() == &_base[size],
duke@435 220 "area must be distinguisable from marks for mark-sweep");
duke@435 221 }
duke@435 222
duke@435 223
duke@435 224 ReservedSpace::ReservedSpace(char* base, size_t size, size_t alignment,
coleenp@1091 225 bool special, bool executable) {
duke@435 226 assert((size % os::vm_allocation_granularity()) == 0,
duke@435 227 "size not allocation aligned");
duke@435 228 _base = base;
duke@435 229 _size = size;
duke@435 230 _alignment = alignment;
coleenp@672 231 _noaccess_prefix = 0;
duke@435 232 _special = special;
coleenp@1091 233 _executable = executable;
duke@435 234 }
duke@435 235
duke@435 236
duke@435 237 ReservedSpace ReservedSpace::first_part(size_t partition_size, size_t alignment,
duke@435 238 bool split, bool realloc) {
duke@435 239 assert(partition_size <= size(), "partition failed");
duke@435 240 if (split) {
coleenp@1091 241 os::split_reserved_memory(base(), size(), partition_size, realloc);
duke@435 242 }
coleenp@1091 243 ReservedSpace result(base(), partition_size, alignment, special(),
coleenp@1091 244 executable());
duke@435 245 return result;
duke@435 246 }
duke@435 247
duke@435 248
duke@435 249 ReservedSpace
duke@435 250 ReservedSpace::last_part(size_t partition_size, size_t alignment) {
duke@435 251 assert(partition_size <= size(), "partition failed");
duke@435 252 ReservedSpace result(base() + partition_size, size() - partition_size,
coleenp@1091 253 alignment, special(), executable());
duke@435 254 return result;
duke@435 255 }
duke@435 256
duke@435 257
duke@435 258 size_t ReservedSpace::page_align_size_up(size_t size) {
duke@435 259 return align_size_up(size, os::vm_page_size());
duke@435 260 }
duke@435 261
duke@435 262
duke@435 263 size_t ReservedSpace::page_align_size_down(size_t size) {
duke@435 264 return align_size_down(size, os::vm_page_size());
duke@435 265 }
duke@435 266
duke@435 267
duke@435 268 size_t ReservedSpace::allocation_align_size_up(size_t size) {
duke@435 269 return align_size_up(size, os::vm_allocation_granularity());
duke@435 270 }
duke@435 271
duke@435 272
duke@435 273 size_t ReservedSpace::allocation_align_size_down(size_t size) {
duke@435 274 return align_size_down(size, os::vm_allocation_granularity());
duke@435 275 }
duke@435 276
duke@435 277
duke@435 278 void ReservedSpace::release() {
duke@435 279 if (is_reserved()) {
coleenp@672 280 char *real_base = _base - _noaccess_prefix;
coleenp@672 281 const size_t real_size = _size + _noaccess_prefix;
duke@435 282 if (special()) {
coleenp@672 283 os::release_memory_special(real_base, real_size);
duke@435 284 } else{
coleenp@672 285 os::release_memory(real_base, real_size);
duke@435 286 }
duke@435 287 _base = NULL;
duke@435 288 _size = 0;
coleenp@672 289 _noaccess_prefix = 0;
duke@435 290 _special = false;
coleenp@1091 291 _executable = false;
duke@435 292 }
duke@435 293 }
duke@435 294
coleenp@672 295 void ReservedSpace::protect_noaccess_prefix(const size_t size) {
kvn@1973 296 assert( (_noaccess_prefix != 0) == (UseCompressedOops && _base != NULL &&
coleenp@3561 297 (Universe::narrow_oop_base() != NULL) &&
kvn@1973 298 Universe::narrow_oop_use_implicit_null_checks()),
kvn@1973 299 "noaccess_prefix should be used only with non zero based compressed oops");
kvn@1973 300
kvn@1973 301 // If there is no noaccess prefix, return.
coleenp@672 302 if (_noaccess_prefix == 0) return;
coleenp@672 303
coleenp@672 304 assert(_noaccess_prefix >= (size_t)os::vm_page_size(),
coleenp@672 305 "must be at least page size big");
coleenp@672 306
coleenp@672 307 // Protect memory at the base of the allocated region.
coleenp@672 308 // If special, the page was committed (only matters on windows)
coleenp@672 309 if (!os::protect_memory(_base, _noaccess_prefix, os::MEM_PROT_NONE,
coleenp@672 310 _special)) {
coleenp@672 311 fatal("cannot protect protection page");
coleenp@672 312 }
kvn@1973 313 if (PrintCompressedOopsMode) {
kvn@1973 314 tty->cr();
kvn@1973 315 tty->print_cr("Protected page at the reserved heap base: " PTR_FORMAT " / " INTX_FORMAT " bytes", _base, _noaccess_prefix);
kvn@1973 316 }
coleenp@672 317
coleenp@672 318 _base += _noaccess_prefix;
coleenp@672 319 _size -= _noaccess_prefix;
coleenp@672 320 assert((size == _size) && ((uintptr_t)_base % _alignment == 0),
coleenp@672 321 "must be exactly of required size and alignment");
coleenp@672 322 }
coleenp@672 323
coleenp@672 324 ReservedHeapSpace::ReservedHeapSpace(size_t size, size_t alignment,
coleenp@672 325 bool large, char* requested_address) :
coleenp@672 326 ReservedSpace(size, alignment, large,
coleenp@672 327 requested_address,
kvn@1077 328 (UseCompressedOops && (Universe::narrow_oop_base() != NULL) &&
kvn@1077 329 Universe::narrow_oop_use_implicit_null_checks()) ?
coleenp@760 330 lcm(os::vm_page_size(), alignment) : 0) {
zgu@3900 331 if (base() > 0) {
zgu@3900 332 MemTracker::record_virtual_memory_type((address)base(), mtJavaHeap);
zgu@3900 333 }
zgu@3900 334
coleenp@672 335 // Only reserved space for the java heap should have a noaccess_prefix
coleenp@672 336 // if using compressed oops.
coleenp@672 337 protect_noaccess_prefix(size);
coleenp@672 338 }
coleenp@672 339
coleenp@1091 340 // Reserve space for code segment. Same as Java heap only we mark this as
coleenp@1091 341 // executable.
coleenp@1091 342 ReservedCodeSpace::ReservedCodeSpace(size_t r_size,
coleenp@1091 343 size_t rs_align,
coleenp@1091 344 bool large) :
coleenp@1091 345 ReservedSpace(r_size, rs_align, large, /*executable*/ true) {
zgu@3900 346 MemTracker::record_virtual_memory_type((address)base(), mtCode);
coleenp@1091 347 }
coleenp@1091 348
duke@435 349 // VirtualSpace
duke@435 350
duke@435 351 VirtualSpace::VirtualSpace() {
duke@435 352 _low_boundary = NULL;
duke@435 353 _high_boundary = NULL;
duke@435 354 _low = NULL;
duke@435 355 _high = NULL;
duke@435 356 _lower_high = NULL;
duke@435 357 _middle_high = NULL;
duke@435 358 _upper_high = NULL;
duke@435 359 _lower_high_boundary = NULL;
duke@435 360 _middle_high_boundary = NULL;
duke@435 361 _upper_high_boundary = NULL;
duke@435 362 _lower_alignment = 0;
duke@435 363 _middle_alignment = 0;
duke@435 364 _upper_alignment = 0;
coleenp@672 365 _special = false;
coleenp@1091 366 _executable = false;
duke@435 367 }
duke@435 368
duke@435 369
duke@435 370 bool VirtualSpace::initialize(ReservedSpace rs, size_t committed_size) {
duke@435 371 if(!rs.is_reserved()) return false; // allocation failed.
duke@435 372 assert(_low_boundary == NULL, "VirtualSpace already initialized");
duke@435 373 _low_boundary = rs.base();
duke@435 374 _high_boundary = low_boundary() + rs.size();
duke@435 375
duke@435 376 _low = low_boundary();
duke@435 377 _high = low();
duke@435 378
duke@435 379 _special = rs.special();
coleenp@1091 380 _executable = rs.executable();
duke@435 381
duke@435 382 // When a VirtualSpace begins life at a large size, make all future expansion
duke@435 383 // and shrinking occur aligned to a granularity of large pages. This avoids
duke@435 384 // fragmentation of physical addresses that inhibits the use of large pages
duke@435 385 // by the OS virtual memory system. Empirically, we see that with a 4MB
duke@435 386 // page size, the only spaces that get handled this way are codecache and
duke@435 387 // the heap itself, both of which provide a substantial performance
duke@435 388 // boost in many benchmarks when covered by large pages.
duke@435 389 //
duke@435 390 // No attempt is made to force large page alignment at the very top and
duke@435 391 // bottom of the space if they are not aligned so already.
duke@435 392 _lower_alignment = os::vm_page_size();
duke@435 393 _middle_alignment = os::page_size_for_region(rs.size(), rs.size(), 1);
duke@435 394 _upper_alignment = os::vm_page_size();
duke@435 395
duke@435 396 // End of each region
duke@435 397 _lower_high_boundary = (char*) round_to((intptr_t) low_boundary(), middle_alignment());
duke@435 398 _middle_high_boundary = (char*) round_down((intptr_t) high_boundary(), middle_alignment());
duke@435 399 _upper_high_boundary = high_boundary();
duke@435 400
duke@435 401 // High address of each region
duke@435 402 _lower_high = low_boundary();
duke@435 403 _middle_high = lower_high_boundary();
duke@435 404 _upper_high = middle_high_boundary();
duke@435 405
duke@435 406 // commit to initial size
duke@435 407 if (committed_size > 0) {
duke@435 408 if (!expand_by(committed_size)) {
duke@435 409 return false;
duke@435 410 }
duke@435 411 }
duke@435 412 return true;
duke@435 413 }
duke@435 414
duke@435 415
duke@435 416 VirtualSpace::~VirtualSpace() {
duke@435 417 release();
duke@435 418 }
duke@435 419
duke@435 420
duke@435 421 void VirtualSpace::release() {
coleenp@672 422 // This does not release memory it never reserved.
coleenp@672 423 // Caller must release via rs.release();
duke@435 424 _low_boundary = NULL;
duke@435 425 _high_boundary = NULL;
duke@435 426 _low = NULL;
duke@435 427 _high = NULL;
duke@435 428 _lower_high = NULL;
duke@435 429 _middle_high = NULL;
duke@435 430 _upper_high = NULL;
duke@435 431 _lower_high_boundary = NULL;
duke@435 432 _middle_high_boundary = NULL;
duke@435 433 _upper_high_boundary = NULL;
duke@435 434 _lower_alignment = 0;
duke@435 435 _middle_alignment = 0;
duke@435 436 _upper_alignment = 0;
duke@435 437 _special = false;
coleenp@1091 438 _executable = false;
duke@435 439 }
duke@435 440
duke@435 441
duke@435 442 size_t VirtualSpace::committed_size() const {
duke@435 443 return pointer_delta(high(), low(), sizeof(char));
duke@435 444 }
duke@435 445
duke@435 446
duke@435 447 size_t VirtualSpace::reserved_size() const {
duke@435 448 return pointer_delta(high_boundary(), low_boundary(), sizeof(char));
duke@435 449 }
duke@435 450
duke@435 451
duke@435 452 size_t VirtualSpace::uncommitted_size() const {
duke@435 453 return reserved_size() - committed_size();
duke@435 454 }
duke@435 455
stefank@5704 456 size_t VirtualSpace::actual_committed_size() const {
stefank@5704 457 // Special VirtualSpaces commit all reserved space up front.
stefank@5704 458 if (special()) {
stefank@5704 459 return reserved_size();
stefank@5704 460 }
stefank@5704 461
stefank@5704 462 size_t committed_low = pointer_delta(_lower_high, _low_boundary, sizeof(char));
stefank@5704 463 size_t committed_middle = pointer_delta(_middle_high, _lower_high_boundary, sizeof(char));
stefank@5704 464 size_t committed_high = pointer_delta(_upper_high, _middle_high_boundary, sizeof(char));
stefank@5704 465
stefank@5704 466 #ifdef ASSERT
stefank@5704 467 size_t lower = pointer_delta(_lower_high_boundary, _low_boundary, sizeof(char));
stefank@5704 468 size_t middle = pointer_delta(_middle_high_boundary, _lower_high_boundary, sizeof(char));
stefank@5704 469 size_t upper = pointer_delta(_upper_high_boundary, _middle_high_boundary, sizeof(char));
stefank@5704 470
stefank@5704 471 if (committed_high > 0) {
stefank@5704 472 assert(committed_low == lower, "Must be");
stefank@5704 473 assert(committed_middle == middle, "Must be");
stefank@5704 474 }
stefank@5704 475
stefank@5704 476 if (committed_middle > 0) {
stefank@5704 477 assert(committed_low == lower, "Must be");
stefank@5704 478 }
stefank@5704 479 if (committed_middle < middle) {
stefank@5704 480 assert(committed_high == 0, "Must be");
stefank@5704 481 }
stefank@5704 482
stefank@5704 483 if (committed_low < lower) {
stefank@5704 484 assert(committed_high == 0, "Must be");
stefank@5704 485 assert(committed_middle == 0, "Must be");
stefank@5704 486 }
stefank@5704 487 #endif
stefank@5704 488
stefank@5704 489 return committed_low + committed_middle + committed_high;
stefank@5704 490 }
stefank@5704 491
duke@435 492
duke@435 493 bool VirtualSpace::contains(const void* p) const {
duke@435 494 return low() <= (const char*) p && (const char*) p < high();
duke@435 495 }
duke@435 496
duke@435 497 /*
duke@435 498 First we need to determine if a particular virtual space is using large
duke@435 499 pages. This is done at the initialize function and only virtual spaces
duke@435 500 that are larger than LargePageSizeInBytes use large pages. Once we
duke@435 501 have determined this, all expand_by and shrink_by calls must grow and
duke@435 502 shrink by large page size chunks. If a particular request
duke@435 503 is within the current large page, the call to commit and uncommit memory
duke@435 504 can be ignored. In the case that the low and high boundaries of this
duke@435 505 space is not large page aligned, the pages leading to the first large
duke@435 506 page address and the pages after the last large page address must be
duke@435 507 allocated with default pages.
duke@435 508 */
duke@435 509 bool VirtualSpace::expand_by(size_t bytes, bool pre_touch) {
duke@435 510 if (uncommitted_size() < bytes) return false;
duke@435 511
duke@435 512 if (special()) {
duke@435 513 // don't commit memory if the entire space is pinned in memory
duke@435 514 _high += bytes;
duke@435 515 return true;
duke@435 516 }
duke@435 517
duke@435 518 char* previous_high = high();
duke@435 519 char* unaligned_new_high = high() + bytes;
duke@435 520 assert(unaligned_new_high <= high_boundary(),
duke@435 521 "cannot expand by more than upper boundary");
duke@435 522
duke@435 523 // Calculate where the new high for each of the regions should be. If
duke@435 524 // the low_boundary() and high_boundary() are LargePageSizeInBytes aligned
duke@435 525 // then the unaligned lower and upper new highs would be the
duke@435 526 // lower_high() and upper_high() respectively.
duke@435 527 char* unaligned_lower_new_high =
duke@435 528 MIN2(unaligned_new_high, lower_high_boundary());
duke@435 529 char* unaligned_middle_new_high =
duke@435 530 MIN2(unaligned_new_high, middle_high_boundary());
duke@435 531 char* unaligned_upper_new_high =
duke@435 532 MIN2(unaligned_new_high, upper_high_boundary());
duke@435 533
duke@435 534 // Align the new highs based on the regions alignment. lower and upper
duke@435 535 // alignment will always be default page size. middle alignment will be
duke@435 536 // LargePageSizeInBytes if the actual size of the virtual space is in
duke@435 537 // fact larger than LargePageSizeInBytes.
duke@435 538 char* aligned_lower_new_high =
duke@435 539 (char*) round_to((intptr_t) unaligned_lower_new_high, lower_alignment());
duke@435 540 char* aligned_middle_new_high =
duke@435 541 (char*) round_to((intptr_t) unaligned_middle_new_high, middle_alignment());
duke@435 542 char* aligned_upper_new_high =
duke@435 543 (char*) round_to((intptr_t) unaligned_upper_new_high, upper_alignment());
duke@435 544
duke@435 545 // Determine which regions need to grow in this expand_by call.
duke@435 546 // If you are growing in the lower region, high() must be in that
duke@435 547 // region so calcuate the size based on high(). For the middle and
duke@435 548 // upper regions, determine the starting point of growth based on the
duke@435 549 // location of high(). By getting the MAX of the region's low address
duke@435 550 // (or the prevoius region's high address) and high(), we can tell if it
duke@435 551 // is an intra or inter region growth.
duke@435 552 size_t lower_needs = 0;
duke@435 553 if (aligned_lower_new_high > lower_high()) {
duke@435 554 lower_needs =
duke@435 555 pointer_delta(aligned_lower_new_high, lower_high(), sizeof(char));
duke@435 556 }
duke@435 557 size_t middle_needs = 0;
duke@435 558 if (aligned_middle_new_high > middle_high()) {
duke@435 559 middle_needs =
duke@435 560 pointer_delta(aligned_middle_new_high, middle_high(), sizeof(char));
duke@435 561 }
duke@435 562 size_t upper_needs = 0;
duke@435 563 if (aligned_upper_new_high > upper_high()) {
duke@435 564 upper_needs =
duke@435 565 pointer_delta(aligned_upper_new_high, upper_high(), sizeof(char));
duke@435 566 }
duke@435 567
duke@435 568 // Check contiguity.
duke@435 569 assert(low_boundary() <= lower_high() &&
duke@435 570 lower_high() <= lower_high_boundary(),
duke@435 571 "high address must be contained within the region");
duke@435 572 assert(lower_high_boundary() <= middle_high() &&
duke@435 573 middle_high() <= middle_high_boundary(),
duke@435 574 "high address must be contained within the region");
duke@435 575 assert(middle_high_boundary() <= upper_high() &&
duke@435 576 upper_high() <= upper_high_boundary(),
duke@435 577 "high address must be contained within the region");
duke@435 578
duke@435 579 // Commit regions
duke@435 580 if (lower_needs > 0) {
duke@435 581 assert(low_boundary() <= lower_high() &&
duke@435 582 lower_high() + lower_needs <= lower_high_boundary(),
duke@435 583 "must not expand beyond region");
coleenp@1091 584 if (!os::commit_memory(lower_high(), lower_needs, _executable)) {
dcubed@5255 585 debug_only(warning("INFO: os::commit_memory(" PTR_FORMAT
dcubed@5255 586 ", lower_needs=" SIZE_FORMAT ", %d) failed",
dcubed@5255 587 lower_high(), lower_needs, _executable);)
duke@435 588 return false;
duke@435 589 } else {
duke@435 590 _lower_high += lower_needs;
dcubed@5255 591 }
duke@435 592 }
duke@435 593 if (middle_needs > 0) {
duke@435 594 assert(lower_high_boundary() <= middle_high() &&
duke@435 595 middle_high() + middle_needs <= middle_high_boundary(),
duke@435 596 "must not expand beyond region");
coleenp@1091 597 if (!os::commit_memory(middle_high(), middle_needs, middle_alignment(),
coleenp@1091 598 _executable)) {
dcubed@5255 599 debug_only(warning("INFO: os::commit_memory(" PTR_FORMAT
dcubed@5255 600 ", middle_needs=" SIZE_FORMAT ", " SIZE_FORMAT
dcubed@5255 601 ", %d) failed", middle_high(), middle_needs,
dcubed@5255 602 middle_alignment(), _executable);)
duke@435 603 return false;
duke@435 604 }
duke@435 605 _middle_high += middle_needs;
duke@435 606 }
duke@435 607 if (upper_needs > 0) {
duke@435 608 assert(middle_high_boundary() <= upper_high() &&
duke@435 609 upper_high() + upper_needs <= upper_high_boundary(),
duke@435 610 "must not expand beyond region");
coleenp@1091 611 if (!os::commit_memory(upper_high(), upper_needs, _executable)) {
dcubed@5255 612 debug_only(warning("INFO: os::commit_memory(" PTR_FORMAT
dcubed@5255 613 ", upper_needs=" SIZE_FORMAT ", %d) failed",
dcubed@5255 614 upper_high(), upper_needs, _executable);)
duke@435 615 return false;
duke@435 616 } else {
duke@435 617 _upper_high += upper_needs;
duke@435 618 }
duke@435 619 }
duke@435 620
duke@435 621 if (pre_touch || AlwaysPreTouch) {
duke@435 622 int vm_ps = os::vm_page_size();
duke@435 623 for (char* curr = previous_high;
duke@435 624 curr < unaligned_new_high;
duke@435 625 curr += vm_ps) {
duke@435 626 // Note the use of a write here; originally we tried just a read, but
duke@435 627 // since the value read was unused, the optimizer removed the read.
duke@435 628 // If we ever have a concurrent touchahead thread, we'll want to use
duke@435 629 // a read, to avoid the potential of overwriting data (if a mutator
duke@435 630 // thread beats the touchahead thread to a page). There are various
duke@435 631 // ways of making sure this read is not optimized away: for example,
duke@435 632 // generating the code for a read procedure at runtime.
duke@435 633 *curr = 0;
duke@435 634 }
duke@435 635 }
duke@435 636
duke@435 637 _high += bytes;
duke@435 638 return true;
duke@435 639 }
duke@435 640
duke@435 641 // A page is uncommitted if the contents of the entire page is deemed unusable.
duke@435 642 // Continue to decrement the high() pointer until it reaches a page boundary
duke@435 643 // in which case that particular page can now be uncommitted.
duke@435 644 void VirtualSpace::shrink_by(size_t size) {
duke@435 645 if (committed_size() < size)
duke@435 646 fatal("Cannot shrink virtual space to negative size");
duke@435 647
duke@435 648 if (special()) {
duke@435 649 // don't uncommit if the entire space is pinned in memory
duke@435 650 _high -= size;
duke@435 651 return;
duke@435 652 }
duke@435 653
duke@435 654 char* unaligned_new_high = high() - size;
duke@435 655 assert(unaligned_new_high >= low_boundary(), "cannot shrink past lower boundary");
duke@435 656
duke@435 657 // Calculate new unaligned address
duke@435 658 char* unaligned_upper_new_high =
duke@435 659 MAX2(unaligned_new_high, middle_high_boundary());
duke@435 660 char* unaligned_middle_new_high =
duke@435 661 MAX2(unaligned_new_high, lower_high_boundary());
duke@435 662 char* unaligned_lower_new_high =
duke@435 663 MAX2(unaligned_new_high, low_boundary());
duke@435 664
duke@435 665 // Align address to region's alignment
duke@435 666 char* aligned_upper_new_high =
duke@435 667 (char*) round_to((intptr_t) unaligned_upper_new_high, upper_alignment());
duke@435 668 char* aligned_middle_new_high =
duke@435 669 (char*) round_to((intptr_t) unaligned_middle_new_high, middle_alignment());
duke@435 670 char* aligned_lower_new_high =
duke@435 671 (char*) round_to((intptr_t) unaligned_lower_new_high, lower_alignment());
duke@435 672
duke@435 673 // Determine which regions need to shrink
duke@435 674 size_t upper_needs = 0;
duke@435 675 if (aligned_upper_new_high < upper_high()) {
duke@435 676 upper_needs =
duke@435 677 pointer_delta(upper_high(), aligned_upper_new_high, sizeof(char));
duke@435 678 }
duke@435 679 size_t middle_needs = 0;
duke@435 680 if (aligned_middle_new_high < middle_high()) {
duke@435 681 middle_needs =
duke@435 682 pointer_delta(middle_high(), aligned_middle_new_high, sizeof(char));
duke@435 683 }
duke@435 684 size_t lower_needs = 0;
duke@435 685 if (aligned_lower_new_high < lower_high()) {
duke@435 686 lower_needs =
duke@435 687 pointer_delta(lower_high(), aligned_lower_new_high, sizeof(char));
duke@435 688 }
duke@435 689
duke@435 690 // Check contiguity.
duke@435 691 assert(middle_high_boundary() <= upper_high() &&
duke@435 692 upper_high() <= upper_high_boundary(),
duke@435 693 "high address must be contained within the region");
duke@435 694 assert(lower_high_boundary() <= middle_high() &&
duke@435 695 middle_high() <= middle_high_boundary(),
duke@435 696 "high address must be contained within the region");
duke@435 697 assert(low_boundary() <= lower_high() &&
duke@435 698 lower_high() <= lower_high_boundary(),
duke@435 699 "high address must be contained within the region");
duke@435 700
duke@435 701 // Uncommit
duke@435 702 if (upper_needs > 0) {
duke@435 703 assert(middle_high_boundary() <= aligned_upper_new_high &&
duke@435 704 aligned_upper_new_high + upper_needs <= upper_high_boundary(),
duke@435 705 "must not shrink beyond region");
duke@435 706 if (!os::uncommit_memory(aligned_upper_new_high, upper_needs)) {
duke@435 707 debug_only(warning("os::uncommit_memory failed"));
duke@435 708 return;
duke@435 709 } else {
duke@435 710 _upper_high -= upper_needs;
duke@435 711 }
duke@435 712 }
duke@435 713 if (middle_needs > 0) {
duke@435 714 assert(lower_high_boundary() <= aligned_middle_new_high &&
duke@435 715 aligned_middle_new_high + middle_needs <= middle_high_boundary(),
duke@435 716 "must not shrink beyond region");
duke@435 717 if (!os::uncommit_memory(aligned_middle_new_high, middle_needs)) {
duke@435 718 debug_only(warning("os::uncommit_memory failed"));
duke@435 719 return;
duke@435 720 } else {
duke@435 721 _middle_high -= middle_needs;
duke@435 722 }
duke@435 723 }
duke@435 724 if (lower_needs > 0) {
duke@435 725 assert(low_boundary() <= aligned_lower_new_high &&
duke@435 726 aligned_lower_new_high + lower_needs <= lower_high_boundary(),
duke@435 727 "must not shrink beyond region");
duke@435 728 if (!os::uncommit_memory(aligned_lower_new_high, lower_needs)) {
duke@435 729 debug_only(warning("os::uncommit_memory failed"));
duke@435 730 return;
duke@435 731 } else {
duke@435 732 _lower_high -= lower_needs;
duke@435 733 }
duke@435 734 }
duke@435 735
duke@435 736 _high -= size;
duke@435 737 }
duke@435 738
duke@435 739 #ifndef PRODUCT
duke@435 740 void VirtualSpace::check_for_contiguity() {
duke@435 741 // Check contiguity.
duke@435 742 assert(low_boundary() <= lower_high() &&
duke@435 743 lower_high() <= lower_high_boundary(),
duke@435 744 "high address must be contained within the region");
duke@435 745 assert(lower_high_boundary() <= middle_high() &&
duke@435 746 middle_high() <= middle_high_boundary(),
duke@435 747 "high address must be contained within the region");
duke@435 748 assert(middle_high_boundary() <= upper_high() &&
duke@435 749 upper_high() <= upper_high_boundary(),
duke@435 750 "high address must be contained within the region");
duke@435 751 assert(low() >= low_boundary(), "low");
duke@435 752 assert(low_boundary() <= lower_high_boundary(), "lower high boundary");
duke@435 753 assert(upper_high_boundary() <= high_boundary(), "upper high boundary");
duke@435 754 assert(high() <= upper_high(), "upper high");
duke@435 755 }
duke@435 756
stefank@5708 757 void VirtualSpace::print_on(outputStream* out) {
stefank@5708 758 out->print ("Virtual space:");
stefank@5708 759 if (special()) out->print(" (pinned in memory)");
stefank@5708 760 out->cr();
stefank@5708 761 out->print_cr(" - committed: " SIZE_FORMAT, committed_size());
stefank@5708 762 out->print_cr(" - reserved: " SIZE_FORMAT, reserved_size());
stefank@5708 763 out->print_cr(" - [low, high]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", low(), high());
stefank@5708 764 out->print_cr(" - [low_b, high_b]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", low_boundary(), high_boundary());
duke@435 765 }
duke@435 766
stefank@5708 767 void VirtualSpace::print() {
stefank@5708 768 print_on(tty);
stefank@5708 769 }
stefank@5578 770
stefank@5578 771 /////////////// Unit tests ///////////////
stefank@5578 772
stefank@5578 773 #ifndef PRODUCT
stefank@5578 774
stefank@5578 775 #define test_log(...) \
stefank@5578 776 do {\
stefank@5578 777 if (VerboseInternalVMTests) { \
stefank@5578 778 tty->print_cr(__VA_ARGS__); \
stefank@5578 779 tty->flush(); \
stefank@5578 780 }\
stefank@5578 781 } while (false)
stefank@5578 782
stefank@5578 783 class TestReservedSpace : AllStatic {
stefank@5578 784 public:
stefank@5578 785 static void small_page_write(void* addr, size_t size) {
stefank@5578 786 size_t page_size = os::vm_page_size();
stefank@5578 787
stefank@5578 788 char* end = (char*)addr + size;
stefank@5578 789 for (char* p = (char*)addr; p < end; p += page_size) {
stefank@5578 790 *p = 1;
stefank@5578 791 }
stefank@5578 792 }
stefank@5578 793
stefank@5578 794 static void release_memory_for_test(ReservedSpace rs) {
stefank@5578 795 if (rs.special()) {
stefank@5578 796 guarantee(os::release_memory_special(rs.base(), rs.size()), "Shouldn't fail");
stefank@5578 797 } else {
stefank@5578 798 guarantee(os::release_memory(rs.base(), rs.size()), "Shouldn't fail");
stefank@5578 799 }
stefank@5578 800 }
stefank@5578 801
stefank@5578 802 static void test_reserved_space1(size_t size, size_t alignment) {
stefank@5578 803 test_log("test_reserved_space1(%p)", (void*) (uintptr_t) size);
stefank@5578 804
stefank@5578 805 assert(is_size_aligned(size, alignment), "Incorrect input parameters");
stefank@5578 806
stefank@5578 807 ReservedSpace rs(size, // size
stefank@5578 808 alignment, // alignment
stefank@5578 809 UseLargePages, // large
stefank@5578 810 NULL, // requested_address
stefank@5578 811 0); // noacces_prefix
stefank@5578 812
stefank@5578 813 test_log(" rs.special() == %d", rs.special());
stefank@5578 814
stefank@5578 815 assert(rs.base() != NULL, "Must be");
stefank@5578 816 assert(rs.size() == size, "Must be");
stefank@5578 817
stefank@5578 818 assert(is_ptr_aligned(rs.base(), alignment), "aligned sizes should always give aligned addresses");
stefank@5578 819 assert(is_size_aligned(rs.size(), alignment), "aligned sizes should always give aligned addresses");
stefank@5578 820
stefank@5578 821 if (rs.special()) {
stefank@5578 822 small_page_write(rs.base(), size);
stefank@5578 823 }
stefank@5578 824
stefank@5578 825 release_memory_for_test(rs);
stefank@5578 826 }
stefank@5578 827
stefank@5578 828 static void test_reserved_space2(size_t size) {
stefank@5578 829 test_log("test_reserved_space2(%p)", (void*)(uintptr_t)size);
stefank@5578 830
stefank@5578 831 assert(is_size_aligned(size, os::vm_allocation_granularity()), "Must be at least AG aligned");
stefank@5578 832
stefank@5578 833 ReservedSpace rs(size);
stefank@5578 834
stefank@5578 835 test_log(" rs.special() == %d", rs.special());
stefank@5578 836
stefank@5578 837 assert(rs.base() != NULL, "Must be");
stefank@5578 838 assert(rs.size() == size, "Must be");
stefank@5578 839
stefank@5578 840 if (rs.special()) {
stefank@5578 841 small_page_write(rs.base(), size);
stefank@5578 842 }
stefank@5578 843
stefank@5578 844 release_memory_for_test(rs);
stefank@5578 845 }
stefank@5578 846
stefank@5578 847 static void test_reserved_space3(size_t size, size_t alignment, bool maybe_large) {
stefank@5578 848 test_log("test_reserved_space3(%p, %p, %d)",
stefank@5578 849 (void*)(uintptr_t)size, (void*)(uintptr_t)alignment, maybe_large);
stefank@5578 850
stefank@5578 851 assert(is_size_aligned(size, os::vm_allocation_granularity()), "Must be at least AG aligned");
stefank@5578 852 assert(is_size_aligned(size, alignment), "Must be at least aligned against alignment");
stefank@5578 853
stefank@5578 854 bool large = maybe_large && UseLargePages && size >= os::large_page_size();
stefank@5578 855
stefank@5578 856 ReservedSpace rs(size, alignment, large, false);
stefank@5578 857
stefank@5578 858 test_log(" rs.special() == %d", rs.special());
stefank@5578 859
stefank@5578 860 assert(rs.base() != NULL, "Must be");
stefank@5578 861 assert(rs.size() == size, "Must be");
stefank@5578 862
stefank@5578 863 if (rs.special()) {
stefank@5578 864 small_page_write(rs.base(), size);
stefank@5578 865 }
stefank@5578 866
stefank@5578 867 release_memory_for_test(rs);
stefank@5578 868 }
stefank@5578 869
stefank@5578 870
stefank@5578 871 static void test_reserved_space1() {
stefank@5578 872 size_t size = 2 * 1024 * 1024;
stefank@5578 873 size_t ag = os::vm_allocation_granularity();
stefank@5578 874
stefank@5578 875 test_reserved_space1(size, ag);
stefank@5578 876 test_reserved_space1(size * 2, ag);
stefank@5578 877 test_reserved_space1(size * 10, ag);
stefank@5578 878 }
stefank@5578 879
stefank@5578 880 static void test_reserved_space2() {
stefank@5578 881 size_t size = 2 * 1024 * 1024;
stefank@5578 882 size_t ag = os::vm_allocation_granularity();
stefank@5578 883
stefank@5578 884 test_reserved_space2(size * 1);
stefank@5578 885 test_reserved_space2(size * 2);
stefank@5578 886 test_reserved_space2(size * 10);
stefank@5578 887 test_reserved_space2(ag);
stefank@5578 888 test_reserved_space2(size - ag);
stefank@5578 889 test_reserved_space2(size);
stefank@5578 890 test_reserved_space2(size + ag);
stefank@5578 891 test_reserved_space2(size * 2);
stefank@5578 892 test_reserved_space2(size * 2 - ag);
stefank@5578 893 test_reserved_space2(size * 2 + ag);
stefank@5578 894 test_reserved_space2(size * 3);
stefank@5578 895 test_reserved_space2(size * 3 - ag);
stefank@5578 896 test_reserved_space2(size * 3 + ag);
stefank@5578 897 test_reserved_space2(size * 10);
stefank@5578 898 test_reserved_space2(size * 10 + size / 2);
stefank@5578 899 }
stefank@5578 900
stefank@5578 901 static void test_reserved_space3() {
stefank@5578 902 size_t ag = os::vm_allocation_granularity();
stefank@5578 903
stefank@5578 904 test_reserved_space3(ag, ag , false);
stefank@5578 905 test_reserved_space3(ag * 2, ag , false);
stefank@5578 906 test_reserved_space3(ag * 3, ag , false);
stefank@5578 907 test_reserved_space3(ag * 2, ag * 2, false);
stefank@5578 908 test_reserved_space3(ag * 4, ag * 2, false);
stefank@5578 909 test_reserved_space3(ag * 8, ag * 2, false);
stefank@5578 910 test_reserved_space3(ag * 4, ag * 4, false);
stefank@5578 911 test_reserved_space3(ag * 8, ag * 4, false);
stefank@5578 912 test_reserved_space3(ag * 16, ag * 4, false);
stefank@5578 913
stefank@5578 914 if (UseLargePages) {
stefank@5578 915 size_t lp = os::large_page_size();
stefank@5578 916
stefank@5578 917 // Without large pages
stefank@5578 918 test_reserved_space3(lp, ag * 4, false);
stefank@5578 919 test_reserved_space3(lp * 2, ag * 4, false);
stefank@5578 920 test_reserved_space3(lp * 4, ag * 4, false);
stefank@5578 921 test_reserved_space3(lp, lp , false);
stefank@5578 922 test_reserved_space3(lp * 2, lp , false);
stefank@5578 923 test_reserved_space3(lp * 3, lp , false);
stefank@5578 924 test_reserved_space3(lp * 2, lp * 2, false);
stefank@5578 925 test_reserved_space3(lp * 4, lp * 2, false);
stefank@5578 926 test_reserved_space3(lp * 8, lp * 2, false);
stefank@5578 927
stefank@5578 928 // With large pages
stefank@5578 929 test_reserved_space3(lp, ag * 4 , true);
stefank@5578 930 test_reserved_space3(lp * 2, ag * 4, true);
stefank@5578 931 test_reserved_space3(lp * 4, ag * 4, true);
stefank@5578 932 test_reserved_space3(lp, lp , true);
stefank@5578 933 test_reserved_space3(lp * 2, lp , true);
stefank@5578 934 test_reserved_space3(lp * 3, lp , true);
stefank@5578 935 test_reserved_space3(lp * 2, lp * 2, true);
stefank@5578 936 test_reserved_space3(lp * 4, lp * 2, true);
stefank@5578 937 test_reserved_space3(lp * 8, lp * 2, true);
stefank@5578 938 }
stefank@5578 939 }
stefank@5578 940
stefank@5578 941 static void test_reserved_space() {
stefank@5578 942 test_reserved_space1();
stefank@5578 943 test_reserved_space2();
stefank@5578 944 test_reserved_space3();
stefank@5578 945 }
stefank@5578 946 };
stefank@5578 947
stefank@5578 948 void TestReservedSpace_test() {
stefank@5578 949 TestReservedSpace::test_reserved_space();
stefank@5578 950 }
stefank@5578 951
stefank@5704 952 #define assert_equals(actual, expected) \
stefank@5704 953 assert(actual == expected, \
stefank@5704 954 err_msg("Got " SIZE_FORMAT " expected " \
stefank@5704 955 SIZE_FORMAT, actual, expected));
stefank@5704 956
stefank@5704 957 #define assert_ge(value1, value2) \
stefank@5704 958 assert(value1 >= value2, \
stefank@5704 959 err_msg("'" #value1 "': " SIZE_FORMAT " '" \
stefank@5704 960 #value2 "': " SIZE_FORMAT, value1, value2));
stefank@5704 961
stefank@5704 962 #define assert_lt(value1, value2) \
stefank@5704 963 assert(value1 < value2, \
stefank@5704 964 err_msg("'" #value1 "': " SIZE_FORMAT " '" \
stefank@5704 965 #value2 "': " SIZE_FORMAT, value1, value2));
stefank@5704 966
stefank@5704 967
stefank@5704 968 class TestVirtualSpace : AllStatic {
stefank@5704 969 public:
stefank@5704 970 static void test_virtual_space_actual_committed_space(size_t reserve_size, size_t commit_size) {
stefank@5704 971 size_t granularity = os::vm_allocation_granularity();
stefank@5704 972 size_t reserve_size_aligned = align_size_up(reserve_size, granularity);
stefank@5704 973
stefank@5704 974 ReservedSpace reserved(reserve_size_aligned);
stefank@5704 975
stefank@5704 976 assert(reserved.is_reserved(), "Must be");
stefank@5704 977
stefank@5704 978 VirtualSpace vs;
stefank@5704 979 bool initialized = vs.initialize(reserved, 0);
stefank@5704 980 assert(initialized, "Failed to initialize VirtualSpace");
stefank@5704 981
stefank@5704 982 vs.expand_by(commit_size, false);
stefank@5704 983
stefank@5704 984 if (vs.special()) {
stefank@5704 985 assert_equals(vs.actual_committed_size(), reserve_size_aligned);
stefank@5704 986 } else {
stefank@5704 987 assert_ge(vs.actual_committed_size(), commit_size);
stefank@5704 988 // Approximate the commit granularity.
stefank@5704 989 size_t commit_granularity = UseLargePages ? os::large_page_size() : os::vm_page_size();
stefank@5704 990 assert_lt(vs.actual_committed_size(), commit_size + commit_granularity);
stefank@5704 991 }
stefank@5704 992
stefank@5704 993 reserved.release();
stefank@5704 994 }
stefank@5704 995
stefank@5704 996 static void test_virtual_space_actual_committed_space_one_large_page() {
stefank@5704 997 if (!UseLargePages) {
stefank@5704 998 return;
stefank@5704 999 }
stefank@5704 1000
stefank@5704 1001 size_t large_page_size = os::large_page_size();
stefank@5704 1002
stefank@5704 1003 ReservedSpace reserved(large_page_size, large_page_size, true, false);
stefank@5704 1004
stefank@5704 1005 assert(reserved.is_reserved(), "Must be");
stefank@5704 1006
stefank@5704 1007 VirtualSpace vs;
stefank@5704 1008 bool initialized = vs.initialize(reserved, 0);
stefank@5704 1009 assert(initialized, "Failed to initialize VirtualSpace");
stefank@5704 1010
stefank@5704 1011 vs.expand_by(large_page_size, false);
stefank@5704 1012
stefank@5704 1013 assert_equals(vs.actual_committed_size(), large_page_size);
stefank@5704 1014
stefank@5704 1015 reserved.release();
stefank@5704 1016 }
stefank@5704 1017
stefank@5704 1018 static void test_virtual_space_actual_committed_space() {
stefank@5704 1019 test_virtual_space_actual_committed_space(4 * K, 0);
stefank@5704 1020 test_virtual_space_actual_committed_space(4 * K, 4 * K);
stefank@5704 1021 test_virtual_space_actual_committed_space(8 * K, 0);
stefank@5704 1022 test_virtual_space_actual_committed_space(8 * K, 4 * K);
stefank@5704 1023 test_virtual_space_actual_committed_space(8 * K, 8 * K);
stefank@5704 1024 test_virtual_space_actual_committed_space(12 * K, 0);
stefank@5704 1025 test_virtual_space_actual_committed_space(12 * K, 4 * K);
stefank@5704 1026 test_virtual_space_actual_committed_space(12 * K, 8 * K);
stefank@5704 1027 test_virtual_space_actual_committed_space(12 * K, 12 * K);
stefank@5704 1028 test_virtual_space_actual_committed_space(64 * K, 0);
stefank@5704 1029 test_virtual_space_actual_committed_space(64 * K, 32 * K);
stefank@5704 1030 test_virtual_space_actual_committed_space(64 * K, 64 * K);
stefank@5704 1031 test_virtual_space_actual_committed_space(2 * M, 0);
stefank@5704 1032 test_virtual_space_actual_committed_space(2 * M, 4 * K);
stefank@5704 1033 test_virtual_space_actual_committed_space(2 * M, 64 * K);
stefank@5704 1034 test_virtual_space_actual_committed_space(2 * M, 1 * M);
stefank@5704 1035 test_virtual_space_actual_committed_space(2 * M, 2 * M);
stefank@5704 1036 test_virtual_space_actual_committed_space(10 * M, 0);
stefank@5704 1037 test_virtual_space_actual_committed_space(10 * M, 4 * K);
stefank@5704 1038 test_virtual_space_actual_committed_space(10 * M, 8 * K);
stefank@5704 1039 test_virtual_space_actual_committed_space(10 * M, 1 * M);
stefank@5704 1040 test_virtual_space_actual_committed_space(10 * M, 2 * M);
stefank@5704 1041 test_virtual_space_actual_committed_space(10 * M, 5 * M);
stefank@5704 1042 test_virtual_space_actual_committed_space(10 * M, 10 * M);
stefank@5704 1043 }
stefank@5704 1044
stefank@5704 1045 static void test_virtual_space() {
stefank@5704 1046 test_virtual_space_actual_committed_space();
stefank@5704 1047 test_virtual_space_actual_committed_space_one_large_page();
stefank@5704 1048 }
stefank@5704 1049 };
stefank@5704 1050
stefank@5704 1051 void TestVirtualSpace_test() {
stefank@5704 1052 TestVirtualSpace::test_virtual_space();
stefank@5704 1053 }
stefank@5704 1054
stefank@5578 1055 #endif // PRODUCT
stefank@5578 1056
duke@435 1057 #endif

mercurial