src/share/vm/runtime/virtualspace.cpp

Sun, 03 Feb 2013 22:43:57 +0100

author
ewendeli
date
Sun, 03 Feb 2013 22:43:57 +0100
changeset 4703
b5cb079ecaa4
parent 4465
203f64878aab
child 5019
b294421fa3c5
permissions
-rw-r--r--

Merge

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
duke@435 45 ReservedSpace::ReservedSpace(size_t size) {
coleenp@1091 46 initialize(size, 0, false, NULL, 0, false);
duke@435 47 }
duke@435 48
duke@435 49 ReservedSpace::ReservedSpace(size_t size, size_t alignment,
coleenp@672 50 bool large,
coleenp@672 51 char* requested_address,
coleenp@672 52 const size_t noaccess_prefix) {
coleenp@672 53 initialize(size+noaccess_prefix, alignment, large, requested_address,
coleenp@1091 54 noaccess_prefix, false);
coleenp@1091 55 }
coleenp@1091 56
coleenp@1091 57 ReservedSpace::ReservedSpace(size_t size, size_t alignment,
coleenp@1091 58 bool large,
coleenp@1091 59 bool executable) {
coleenp@1091 60 initialize(size, alignment, large, NULL, 0, executable);
duke@435 61 }
duke@435 62
duke@435 63 char *
duke@435 64 ReservedSpace::align_reserved_region(char* addr, const size_t len,
duke@435 65 const size_t prefix_size,
duke@435 66 const size_t prefix_align,
duke@435 67 const size_t suffix_size,
duke@435 68 const size_t suffix_align)
duke@435 69 {
duke@435 70 assert(addr != NULL, "sanity");
duke@435 71 const size_t required_size = prefix_size + suffix_size;
duke@435 72 assert(len >= required_size, "len too small");
duke@435 73
duke@435 74 const size_t s = size_t(addr);
johnc@3022 75 const size_t beg_ofs = (s + prefix_size) & (suffix_align - 1);
duke@435 76 const size_t beg_delta = beg_ofs == 0 ? 0 : suffix_align - beg_ofs;
duke@435 77
duke@435 78 if (len < beg_delta + required_size) {
duke@435 79 return NULL; // Cannot do proper alignment.
duke@435 80 }
duke@435 81 const size_t end_delta = len - (beg_delta + required_size);
duke@435 82
duke@435 83 if (beg_delta != 0) {
duke@435 84 os::release_memory(addr, beg_delta);
duke@435 85 }
duke@435 86
duke@435 87 if (end_delta != 0) {
duke@435 88 char* release_addr = (char*) (s + beg_delta + required_size);
duke@435 89 os::release_memory(release_addr, end_delta);
duke@435 90 }
duke@435 91
duke@435 92 return (char*) (s + beg_delta);
duke@435 93 }
duke@435 94
duke@435 95 char* ReservedSpace::reserve_and_align(const size_t reserve_size,
duke@435 96 const size_t prefix_size,
duke@435 97 const size_t prefix_align,
duke@435 98 const size_t suffix_size,
duke@435 99 const size_t suffix_align)
duke@435 100 {
duke@435 101 assert(reserve_size > prefix_size + suffix_size, "should not be here");
duke@435 102
duke@435 103 char* raw_addr = os::reserve_memory(reserve_size, NULL, prefix_align);
duke@435 104 if (raw_addr == NULL) return NULL;
duke@435 105
duke@435 106 char* result = align_reserved_region(raw_addr, reserve_size, prefix_size,
duke@435 107 prefix_align, suffix_size,
duke@435 108 suffix_align);
duke@435 109 if (result == NULL && !os::release_memory(raw_addr, reserve_size)) {
duke@435 110 fatal("os::release_memory failed");
duke@435 111 }
duke@435 112
duke@435 113 #ifdef ASSERT
duke@435 114 if (result != NULL) {
duke@435 115 const size_t raw = size_t(raw_addr);
duke@435 116 const size_t res = size_t(result);
duke@435 117 assert(res >= raw, "alignment decreased start addr");
duke@435 118 assert(res + prefix_size + suffix_size <= raw + reserve_size,
duke@435 119 "alignment increased end addr");
johnc@3022 120 assert((res & (prefix_align - 1)) == 0, "bad alignment of prefix");
johnc@3022 121 assert(((res + prefix_size) & (suffix_align - 1)) == 0,
duke@435 122 "bad alignment of suffix");
duke@435 123 }
duke@435 124 #endif
duke@435 125
duke@435 126 return result;
duke@435 127 }
duke@435 128
kvn@1973 129 // Helper method.
kvn@1973 130 static bool failed_to_reserve_as_requested(char* base, char* requested_address,
kvn@1973 131 const size_t size, bool special)
kvn@1973 132 {
kvn@1973 133 if (base == requested_address || requested_address == NULL)
kvn@1973 134 return false; // did not fail
kvn@1973 135
kvn@1973 136 if (base != NULL) {
kvn@1973 137 // Different reserve address may be acceptable in other cases
kvn@1973 138 // but for compressed oops heap should be at requested address.
kvn@1973 139 assert(UseCompressedOops, "currently requested address used only for compressed oops");
kvn@1973 140 if (PrintCompressedOopsMode) {
kvn@1973 141 tty->cr();
johnc@3022 142 tty->print_cr("Reserved memory not at requested address: " PTR_FORMAT " vs " PTR_FORMAT, base, requested_address);
kvn@1973 143 }
kvn@1973 144 // OS ignored requested address. Try different address.
kvn@1973 145 if (special) {
kvn@1973 146 if (!os::release_memory_special(base, size)) {
kvn@1973 147 fatal("os::release_memory_special failed");
kvn@1973 148 }
kvn@1973 149 } else {
kvn@1973 150 if (!os::release_memory(base, size)) {
kvn@1973 151 fatal("os::release_memory failed");
kvn@1973 152 }
kvn@1973 153 }
kvn@1973 154 }
kvn@1973 155 return true;
kvn@1973 156 }
kvn@1973 157
coleenp@4037 158 ReservedSpace::ReservedSpace(const size_t suffix_size,
coleenp@672 159 const size_t suffix_align,
kvn@1077 160 char* requested_address,
coleenp@672 161 const size_t noaccess_prefix)
duke@435 162 {
duke@435 163 assert(suffix_size != 0, "sanity");
duke@435 164 assert(suffix_align != 0, "sanity");
johnc@3022 165 assert((suffix_size & (suffix_align - 1)) == 0,
duke@435 166 "suffix_size not divisible by suffix_align");
duke@435 167
kvn@1973 168 // Assert that if noaccess_prefix is used, it is the same as prefix_align.
coleenp@4037 169 // Add in noaccess_prefix to prefix
coleenp@4037 170 const size_t adjusted_prefix_size = noaccess_prefix;
coleenp@672 171 const size_t size = adjusted_prefix_size + suffix_size;
coleenp@672 172
duke@435 173 // On systems where the entire region has to be reserved and committed up
duke@435 174 // front, the compound alignment normally done by this method is unnecessary.
duke@435 175 const bool try_reserve_special = UseLargePages &&
coleenp@4037 176 suffix_align == os::large_page_size();
duke@435 177 if (!os::can_commit_large_page_memory() && try_reserve_special) {
coleenp@4037 178 initialize(size, suffix_align, true, requested_address, noaccess_prefix,
coleenp@1091 179 false);
duke@435 180 return;
duke@435 181 }
duke@435 182
duke@435 183 _base = NULL;
duke@435 184 _size = 0;
duke@435 185 _alignment = 0;
duke@435 186 _special = false;
coleenp@672 187 _noaccess_prefix = 0;
coleenp@1091 188 _executable = false;
coleenp@672 189
duke@435 190 // Optimistically try to reserve the exact size needed.
kvn@1077 191 char* addr;
kvn@1077 192 if (requested_address != 0) {
kvn@1973 193 requested_address -= noaccess_prefix; // adjust address
kvn@1973 194 assert(requested_address != NULL, "huge noaccess prefix?");
kvn@1973 195 addr = os::attempt_reserve_memory_at(size, requested_address);
kvn@1973 196 if (failed_to_reserve_as_requested(addr, requested_address, size, false)) {
kvn@1973 197 // OS ignored requested address. Try different address.
kvn@1973 198 addr = NULL;
kvn@1973 199 }
kvn@1077 200 } else {
coleenp@4037 201 addr = os::reserve_memory(size, NULL, suffix_align);
kvn@1077 202 }
duke@435 203 if (addr == NULL) return;
duke@435 204
coleenp@4037 205 // Check whether the result has the needed alignment
johnc@3022 206 const size_t ofs = (size_t(addr) + adjusted_prefix_size) & (suffix_align - 1);
duke@435 207 if (ofs != 0) {
duke@435 208 // Wrong alignment. Release, allocate more space and do manual alignment.
duke@435 209 //
duke@435 210 // On most operating systems, another allocation with a somewhat larger size
duke@435 211 // will return an address "close to" that of the previous allocation. The
duke@435 212 // result is often the same address (if the kernel hands out virtual
duke@435 213 // addresses from low to high), or an address that is offset by the increase
duke@435 214 // in size. Exploit that to minimize the amount of extra space requested.
duke@435 215 if (!os::release_memory(addr, size)) {
duke@435 216 fatal("os::release_memory failed");
duke@435 217 }
duke@435 218
duke@435 219 const size_t extra = MAX2(ofs, suffix_align - ofs);
coleenp@4037 220 addr = reserve_and_align(size + extra, adjusted_prefix_size, suffix_align,
duke@435 221 suffix_size, suffix_align);
duke@435 222 if (addr == NULL) {
duke@435 223 // Try an even larger region. If this fails, address space is exhausted.
coleenp@672 224 addr = reserve_and_align(size + suffix_align, adjusted_prefix_size,
coleenp@4037 225 suffix_align, suffix_size, suffix_align);
duke@435 226 }
johnc@3022 227
johnc@3022 228 if (requested_address != 0 &&
johnc@3022 229 failed_to_reserve_as_requested(addr, requested_address, size, false)) {
johnc@3022 230 // As a result of the alignment constraints, the allocated addr differs
johnc@3022 231 // from the requested address. Return back to the caller who can
johnc@3022 232 // take remedial action (like try again without a requested address).
johnc@3022 233 assert(_base == NULL, "should be");
johnc@3022 234 return;
johnc@3022 235 }
duke@435 236 }
duke@435 237
duke@435 238 _base = addr;
duke@435 239 _size = size;
coleenp@4037 240 _alignment = suffix_align;
coleenp@672 241 _noaccess_prefix = noaccess_prefix;
duke@435 242 }
duke@435 243
duke@435 244 void ReservedSpace::initialize(size_t size, size_t alignment, bool large,
coleenp@672 245 char* requested_address,
coleenp@1091 246 const size_t noaccess_prefix,
coleenp@1091 247 bool executable) {
duke@435 248 const size_t granularity = os::vm_allocation_granularity();
johnc@3022 249 assert((size & (granularity - 1)) == 0,
duke@435 250 "size not aligned to os::vm_allocation_granularity()");
johnc@3022 251 assert((alignment & (granularity - 1)) == 0,
duke@435 252 "alignment not aligned to os::vm_allocation_granularity()");
duke@435 253 assert(alignment == 0 || is_power_of_2((intptr_t)alignment),
duke@435 254 "not a power of 2");
duke@435 255
johnc@3022 256 alignment = MAX2(alignment, (size_t)os::vm_page_size());
johnc@3022 257
johnc@3022 258 // Assert that if noaccess_prefix is used, it is the same as alignment.
johnc@3022 259 assert(noaccess_prefix == 0 ||
johnc@3022 260 noaccess_prefix == alignment, "noaccess prefix wrong");
johnc@3022 261
duke@435 262 _base = NULL;
duke@435 263 _size = 0;
duke@435 264 _special = false;
coleenp@1091 265 _executable = executable;
duke@435 266 _alignment = 0;
coleenp@672 267 _noaccess_prefix = 0;
duke@435 268 if (size == 0) {
duke@435 269 return;
duke@435 270 }
duke@435 271
duke@435 272 // If OS doesn't support demand paging for large page memory, we need
duke@435 273 // to use reserve_memory_special() to reserve and pin the entire region.
duke@435 274 bool special = large && !os::can_commit_large_page_memory();
duke@435 275 char* base = NULL;
duke@435 276
kvn@1973 277 if (requested_address != 0) {
kvn@1973 278 requested_address -= noaccess_prefix; // adjust requested address
kvn@1973 279 assert(requested_address != NULL, "huge noaccess prefix?");
kvn@1973 280 }
kvn@1973 281
duke@435 282 if (special) {
duke@435 283
coleenp@1091 284 base = os::reserve_memory_special(size, requested_address, executable);
duke@435 285
duke@435 286 if (base != NULL) {
kvn@1973 287 if (failed_to_reserve_as_requested(base, requested_address, size, true)) {
kvn@1973 288 // OS ignored requested address. Try different address.
kvn@1973 289 return;
kvn@1973 290 }
duke@435 291 // Check alignment constraints
johnc@3022 292 assert((uintptr_t) base % alignment == 0,
johnc@3022 293 "Large pages returned a non-aligned address");
duke@435 294 _special = true;
duke@435 295 } else {
duke@435 296 // failed; try to reserve regular memory below
kvn@1973 297 if (UseLargePages && (!FLAG_IS_DEFAULT(UseLargePages) ||
kvn@1973 298 !FLAG_IS_DEFAULT(LargePageSizeInBytes))) {
kvn@1973 299 if (PrintCompressedOopsMode) {
kvn@1973 300 tty->cr();
kvn@1973 301 tty->print_cr("Reserve regular memory without large pages.");
kvn@1973 302 }
kvn@1973 303 }
duke@435 304 }
duke@435 305 }
duke@435 306
duke@435 307 if (base == NULL) {
duke@435 308 // Optimistically assume that the OSes returns an aligned base pointer.
duke@435 309 // When reserving a large address range, most OSes seem to align to at
duke@435 310 // least 64K.
duke@435 311
duke@435 312 // If the memory was requested at a particular address, use
duke@435 313 // os::attempt_reserve_memory_at() to avoid over mapping something
duke@435 314 // important. If available space is not detected, return NULL.
duke@435 315
duke@435 316 if (requested_address != 0) {
kvn@1973 317 base = os::attempt_reserve_memory_at(size, requested_address);
kvn@1973 318 if (failed_to_reserve_as_requested(base, requested_address, size, false)) {
kvn@1973 319 // OS ignored requested address. Try different address.
kvn@1973 320 base = NULL;
kvn@1973 321 }
duke@435 322 } else {
duke@435 323 base = os::reserve_memory(size, NULL, alignment);
duke@435 324 }
duke@435 325
duke@435 326 if (base == NULL) return;
duke@435 327
duke@435 328 // Check alignment constraints
johnc@3022 329 if ((((size_t)base + noaccess_prefix) & (alignment - 1)) != 0) {
duke@435 330 // Base not aligned, retry
duke@435 331 if (!os::release_memory(base, size)) fatal("os::release_memory failed");
brutisso@4369 332 // Make sure that size is aligned
duke@435 333 size = align_size_up(size, alignment);
brutisso@4369 334 base = os::reserve_memory_aligned(size, alignment);
johnc@3022 335
johnc@3022 336 if (requested_address != 0 &&
johnc@3022 337 failed_to_reserve_as_requested(base, requested_address, size, false)) {
johnc@3022 338 // As a result of the alignment constraints, the allocated base differs
johnc@3022 339 // from the requested address. Return back to the caller who can
johnc@3022 340 // take remedial action (like try again without a requested address).
johnc@3022 341 assert(_base == NULL, "should be");
johnc@3022 342 return;
johnc@3022 343 }
duke@435 344 }
duke@435 345 }
duke@435 346 // Done
duke@435 347 _base = base;
duke@435 348 _size = size;
johnc@3022 349 _alignment = alignment;
coleenp@672 350 _noaccess_prefix = noaccess_prefix;
coleenp@672 351
coleenp@672 352 // Assert that if noaccess_prefix is used, it is the same as alignment.
coleenp@672 353 assert(noaccess_prefix == 0 ||
coleenp@672 354 noaccess_prefix == _alignment, "noaccess prefix wrong");
duke@435 355
duke@435 356 assert(markOopDesc::encode_pointer_as_mark(_base)->decode_pointer() == _base,
duke@435 357 "area must be distinguisable from marks for mark-sweep");
duke@435 358 assert(markOopDesc::encode_pointer_as_mark(&_base[size])->decode_pointer() == &_base[size],
duke@435 359 "area must be distinguisable from marks for mark-sweep");
duke@435 360 }
duke@435 361
duke@435 362
duke@435 363 ReservedSpace::ReservedSpace(char* base, size_t size, size_t alignment,
coleenp@1091 364 bool special, bool executable) {
duke@435 365 assert((size % os::vm_allocation_granularity()) == 0,
duke@435 366 "size not allocation aligned");
duke@435 367 _base = base;
duke@435 368 _size = size;
duke@435 369 _alignment = alignment;
coleenp@672 370 _noaccess_prefix = 0;
duke@435 371 _special = special;
coleenp@1091 372 _executable = executable;
duke@435 373 }
duke@435 374
duke@435 375
duke@435 376 ReservedSpace ReservedSpace::first_part(size_t partition_size, size_t alignment,
duke@435 377 bool split, bool realloc) {
duke@435 378 assert(partition_size <= size(), "partition failed");
duke@435 379 if (split) {
coleenp@1091 380 os::split_reserved_memory(base(), size(), partition_size, realloc);
duke@435 381 }
coleenp@1091 382 ReservedSpace result(base(), partition_size, alignment, special(),
coleenp@1091 383 executable());
duke@435 384 return result;
duke@435 385 }
duke@435 386
duke@435 387
duke@435 388 ReservedSpace
duke@435 389 ReservedSpace::last_part(size_t partition_size, size_t alignment) {
duke@435 390 assert(partition_size <= size(), "partition failed");
duke@435 391 ReservedSpace result(base() + partition_size, size() - partition_size,
coleenp@1091 392 alignment, special(), executable());
duke@435 393 return result;
duke@435 394 }
duke@435 395
duke@435 396
duke@435 397 size_t ReservedSpace::page_align_size_up(size_t size) {
duke@435 398 return align_size_up(size, os::vm_page_size());
duke@435 399 }
duke@435 400
duke@435 401
duke@435 402 size_t ReservedSpace::page_align_size_down(size_t size) {
duke@435 403 return align_size_down(size, os::vm_page_size());
duke@435 404 }
duke@435 405
duke@435 406
duke@435 407 size_t ReservedSpace::allocation_align_size_up(size_t size) {
duke@435 408 return align_size_up(size, os::vm_allocation_granularity());
duke@435 409 }
duke@435 410
duke@435 411
duke@435 412 size_t ReservedSpace::allocation_align_size_down(size_t size) {
duke@435 413 return align_size_down(size, os::vm_allocation_granularity());
duke@435 414 }
duke@435 415
duke@435 416
duke@435 417 void ReservedSpace::release() {
duke@435 418 if (is_reserved()) {
coleenp@672 419 char *real_base = _base - _noaccess_prefix;
coleenp@672 420 const size_t real_size = _size + _noaccess_prefix;
duke@435 421 if (special()) {
coleenp@672 422 os::release_memory_special(real_base, real_size);
duke@435 423 } else{
coleenp@672 424 os::release_memory(real_base, real_size);
duke@435 425 }
duke@435 426 _base = NULL;
duke@435 427 _size = 0;
coleenp@672 428 _noaccess_prefix = 0;
duke@435 429 _special = false;
coleenp@1091 430 _executable = false;
duke@435 431 }
duke@435 432 }
duke@435 433
coleenp@672 434 void ReservedSpace::protect_noaccess_prefix(const size_t size) {
kvn@1973 435 assert( (_noaccess_prefix != 0) == (UseCompressedOops && _base != NULL &&
coleenp@3561 436 (Universe::narrow_oop_base() != NULL) &&
kvn@1973 437 Universe::narrow_oop_use_implicit_null_checks()),
kvn@1973 438 "noaccess_prefix should be used only with non zero based compressed oops");
kvn@1973 439
kvn@1973 440 // If there is no noaccess prefix, return.
coleenp@672 441 if (_noaccess_prefix == 0) return;
coleenp@672 442
coleenp@672 443 assert(_noaccess_prefix >= (size_t)os::vm_page_size(),
coleenp@672 444 "must be at least page size big");
coleenp@672 445
coleenp@672 446 // Protect memory at the base of the allocated region.
coleenp@672 447 // If special, the page was committed (only matters on windows)
coleenp@672 448 if (!os::protect_memory(_base, _noaccess_prefix, os::MEM_PROT_NONE,
coleenp@672 449 _special)) {
coleenp@672 450 fatal("cannot protect protection page");
coleenp@672 451 }
kvn@1973 452 if (PrintCompressedOopsMode) {
kvn@1973 453 tty->cr();
kvn@1973 454 tty->print_cr("Protected page at the reserved heap base: " PTR_FORMAT " / " INTX_FORMAT " bytes", _base, _noaccess_prefix);
kvn@1973 455 }
coleenp@672 456
coleenp@672 457 _base += _noaccess_prefix;
coleenp@672 458 _size -= _noaccess_prefix;
coleenp@672 459 assert((size == _size) && ((uintptr_t)_base % _alignment == 0),
coleenp@672 460 "must be exactly of required size and alignment");
coleenp@672 461 }
coleenp@672 462
coleenp@672 463 ReservedHeapSpace::ReservedHeapSpace(size_t size, size_t alignment,
coleenp@672 464 bool large, char* requested_address) :
coleenp@672 465 ReservedSpace(size, alignment, large,
coleenp@672 466 requested_address,
kvn@1077 467 (UseCompressedOops && (Universe::narrow_oop_base() != NULL) &&
kvn@1077 468 Universe::narrow_oop_use_implicit_null_checks()) ?
coleenp@760 469 lcm(os::vm_page_size(), alignment) : 0) {
zgu@3900 470 if (base() > 0) {
zgu@3900 471 MemTracker::record_virtual_memory_type((address)base(), mtJavaHeap);
zgu@3900 472 }
zgu@3900 473
coleenp@672 474 // Only reserved space for the java heap should have a noaccess_prefix
coleenp@672 475 // if using compressed oops.
coleenp@672 476 protect_noaccess_prefix(size);
coleenp@672 477 }
coleenp@672 478
coleenp@4037 479 ReservedHeapSpace::ReservedHeapSpace(const size_t heap_space_size,
coleenp@4037 480 const size_t alignment,
kvn@1077 481 char* requested_address) :
coleenp@4037 482 ReservedSpace(heap_space_size, alignment,
kvn@1077 483 requested_address,
kvn@1077 484 (UseCompressedOops && (Universe::narrow_oop_base() != NULL) &&
kvn@1077 485 Universe::narrow_oop_use_implicit_null_checks()) ?
coleenp@4037 486 lcm(os::vm_page_size(), alignment) : 0) {
zgu@3900 487 if (base() > 0) {
zgu@3900 488 MemTracker::record_virtual_memory_type((address)base(), mtJavaHeap);
zgu@3900 489 }
coleenp@4037 490 protect_noaccess_prefix(heap_space_size);
coleenp@672 491 }
duke@435 492
coleenp@1091 493 // Reserve space for code segment. Same as Java heap only we mark this as
coleenp@1091 494 // executable.
coleenp@1091 495 ReservedCodeSpace::ReservedCodeSpace(size_t r_size,
coleenp@1091 496 size_t rs_align,
coleenp@1091 497 bool large) :
coleenp@1091 498 ReservedSpace(r_size, rs_align, large, /*executable*/ true) {
zgu@3900 499 MemTracker::record_virtual_memory_type((address)base(), mtCode);
coleenp@1091 500 }
coleenp@1091 501
duke@435 502 // VirtualSpace
duke@435 503
duke@435 504 VirtualSpace::VirtualSpace() {
duke@435 505 _low_boundary = NULL;
duke@435 506 _high_boundary = NULL;
duke@435 507 _low = NULL;
duke@435 508 _high = NULL;
duke@435 509 _lower_high = NULL;
duke@435 510 _middle_high = NULL;
duke@435 511 _upper_high = NULL;
duke@435 512 _lower_high_boundary = NULL;
duke@435 513 _middle_high_boundary = NULL;
duke@435 514 _upper_high_boundary = NULL;
duke@435 515 _lower_alignment = 0;
duke@435 516 _middle_alignment = 0;
duke@435 517 _upper_alignment = 0;
coleenp@672 518 _special = false;
coleenp@1091 519 _executable = false;
duke@435 520 }
duke@435 521
duke@435 522
duke@435 523 bool VirtualSpace::initialize(ReservedSpace rs, size_t committed_size) {
duke@435 524 if(!rs.is_reserved()) return false; // allocation failed.
duke@435 525 assert(_low_boundary == NULL, "VirtualSpace already initialized");
duke@435 526 _low_boundary = rs.base();
duke@435 527 _high_boundary = low_boundary() + rs.size();
duke@435 528
duke@435 529 _low = low_boundary();
duke@435 530 _high = low();
duke@435 531
duke@435 532 _special = rs.special();
coleenp@1091 533 _executable = rs.executable();
duke@435 534
duke@435 535 // When a VirtualSpace begins life at a large size, make all future expansion
duke@435 536 // and shrinking occur aligned to a granularity of large pages. This avoids
duke@435 537 // fragmentation of physical addresses that inhibits the use of large pages
duke@435 538 // by the OS virtual memory system. Empirically, we see that with a 4MB
duke@435 539 // page size, the only spaces that get handled this way are codecache and
duke@435 540 // the heap itself, both of which provide a substantial performance
duke@435 541 // boost in many benchmarks when covered by large pages.
duke@435 542 //
duke@435 543 // No attempt is made to force large page alignment at the very top and
duke@435 544 // bottom of the space if they are not aligned so already.
duke@435 545 _lower_alignment = os::vm_page_size();
duke@435 546 _middle_alignment = os::page_size_for_region(rs.size(), rs.size(), 1);
duke@435 547 _upper_alignment = os::vm_page_size();
duke@435 548
duke@435 549 // End of each region
duke@435 550 _lower_high_boundary = (char*) round_to((intptr_t) low_boundary(), middle_alignment());
duke@435 551 _middle_high_boundary = (char*) round_down((intptr_t) high_boundary(), middle_alignment());
duke@435 552 _upper_high_boundary = high_boundary();
duke@435 553
duke@435 554 // High address of each region
duke@435 555 _lower_high = low_boundary();
duke@435 556 _middle_high = lower_high_boundary();
duke@435 557 _upper_high = middle_high_boundary();
duke@435 558
duke@435 559 // commit to initial size
duke@435 560 if (committed_size > 0) {
duke@435 561 if (!expand_by(committed_size)) {
duke@435 562 return false;
duke@435 563 }
duke@435 564 }
duke@435 565 return true;
duke@435 566 }
duke@435 567
duke@435 568
duke@435 569 VirtualSpace::~VirtualSpace() {
duke@435 570 release();
duke@435 571 }
duke@435 572
duke@435 573
duke@435 574 void VirtualSpace::release() {
coleenp@672 575 // This does not release memory it never reserved.
coleenp@672 576 // Caller must release via rs.release();
duke@435 577 _low_boundary = NULL;
duke@435 578 _high_boundary = NULL;
duke@435 579 _low = NULL;
duke@435 580 _high = NULL;
duke@435 581 _lower_high = NULL;
duke@435 582 _middle_high = NULL;
duke@435 583 _upper_high = NULL;
duke@435 584 _lower_high_boundary = NULL;
duke@435 585 _middle_high_boundary = NULL;
duke@435 586 _upper_high_boundary = NULL;
duke@435 587 _lower_alignment = 0;
duke@435 588 _middle_alignment = 0;
duke@435 589 _upper_alignment = 0;
duke@435 590 _special = false;
coleenp@1091 591 _executable = false;
duke@435 592 }
duke@435 593
duke@435 594
duke@435 595 size_t VirtualSpace::committed_size() const {
duke@435 596 return pointer_delta(high(), low(), sizeof(char));
duke@435 597 }
duke@435 598
duke@435 599
duke@435 600 size_t VirtualSpace::reserved_size() const {
duke@435 601 return pointer_delta(high_boundary(), low_boundary(), sizeof(char));
duke@435 602 }
duke@435 603
duke@435 604
duke@435 605 size_t VirtualSpace::uncommitted_size() const {
duke@435 606 return reserved_size() - committed_size();
duke@435 607 }
duke@435 608
duke@435 609
duke@435 610 bool VirtualSpace::contains(const void* p) const {
duke@435 611 return low() <= (const char*) p && (const char*) p < high();
duke@435 612 }
duke@435 613
duke@435 614 /*
duke@435 615 First we need to determine if a particular virtual space is using large
duke@435 616 pages. This is done at the initialize function and only virtual spaces
duke@435 617 that are larger than LargePageSizeInBytes use large pages. Once we
duke@435 618 have determined this, all expand_by and shrink_by calls must grow and
duke@435 619 shrink by large page size chunks. If a particular request
duke@435 620 is within the current large page, the call to commit and uncommit memory
duke@435 621 can be ignored. In the case that the low and high boundaries of this
duke@435 622 space is not large page aligned, the pages leading to the first large
duke@435 623 page address and the pages after the last large page address must be
duke@435 624 allocated with default pages.
duke@435 625 */
duke@435 626 bool VirtualSpace::expand_by(size_t bytes, bool pre_touch) {
duke@435 627 if (uncommitted_size() < bytes) return false;
duke@435 628
duke@435 629 if (special()) {
duke@435 630 // don't commit memory if the entire space is pinned in memory
duke@435 631 _high += bytes;
duke@435 632 return true;
duke@435 633 }
duke@435 634
duke@435 635 char* previous_high = high();
duke@435 636 char* unaligned_new_high = high() + bytes;
duke@435 637 assert(unaligned_new_high <= high_boundary(),
duke@435 638 "cannot expand by more than upper boundary");
duke@435 639
duke@435 640 // Calculate where the new high for each of the regions should be. If
duke@435 641 // the low_boundary() and high_boundary() are LargePageSizeInBytes aligned
duke@435 642 // then the unaligned lower and upper new highs would be the
duke@435 643 // lower_high() and upper_high() respectively.
duke@435 644 char* unaligned_lower_new_high =
duke@435 645 MIN2(unaligned_new_high, lower_high_boundary());
duke@435 646 char* unaligned_middle_new_high =
duke@435 647 MIN2(unaligned_new_high, middle_high_boundary());
duke@435 648 char* unaligned_upper_new_high =
duke@435 649 MIN2(unaligned_new_high, upper_high_boundary());
duke@435 650
duke@435 651 // Align the new highs based on the regions alignment. lower and upper
duke@435 652 // alignment will always be default page size. middle alignment will be
duke@435 653 // LargePageSizeInBytes if the actual size of the virtual space is in
duke@435 654 // fact larger than LargePageSizeInBytes.
duke@435 655 char* aligned_lower_new_high =
duke@435 656 (char*) round_to((intptr_t) unaligned_lower_new_high, lower_alignment());
duke@435 657 char* aligned_middle_new_high =
duke@435 658 (char*) round_to((intptr_t) unaligned_middle_new_high, middle_alignment());
duke@435 659 char* aligned_upper_new_high =
duke@435 660 (char*) round_to((intptr_t) unaligned_upper_new_high, upper_alignment());
duke@435 661
duke@435 662 // Determine which regions need to grow in this expand_by call.
duke@435 663 // If you are growing in the lower region, high() must be in that
duke@435 664 // region so calcuate the size based on high(). For the middle and
duke@435 665 // upper regions, determine the starting point of growth based on the
duke@435 666 // location of high(). By getting the MAX of the region's low address
duke@435 667 // (or the prevoius region's high address) and high(), we can tell if it
duke@435 668 // is an intra or inter region growth.
duke@435 669 size_t lower_needs = 0;
duke@435 670 if (aligned_lower_new_high > lower_high()) {
duke@435 671 lower_needs =
duke@435 672 pointer_delta(aligned_lower_new_high, lower_high(), sizeof(char));
duke@435 673 }
duke@435 674 size_t middle_needs = 0;
duke@435 675 if (aligned_middle_new_high > middle_high()) {
duke@435 676 middle_needs =
duke@435 677 pointer_delta(aligned_middle_new_high, middle_high(), sizeof(char));
duke@435 678 }
duke@435 679 size_t upper_needs = 0;
duke@435 680 if (aligned_upper_new_high > upper_high()) {
duke@435 681 upper_needs =
duke@435 682 pointer_delta(aligned_upper_new_high, upper_high(), sizeof(char));
duke@435 683 }
duke@435 684
duke@435 685 // Check contiguity.
duke@435 686 assert(low_boundary() <= lower_high() &&
duke@435 687 lower_high() <= lower_high_boundary(),
duke@435 688 "high address must be contained within the region");
duke@435 689 assert(lower_high_boundary() <= middle_high() &&
duke@435 690 middle_high() <= middle_high_boundary(),
duke@435 691 "high address must be contained within the region");
duke@435 692 assert(middle_high_boundary() <= upper_high() &&
duke@435 693 upper_high() <= upper_high_boundary(),
duke@435 694 "high address must be contained within the region");
duke@435 695
duke@435 696 // Commit regions
duke@435 697 if (lower_needs > 0) {
duke@435 698 assert(low_boundary() <= lower_high() &&
duke@435 699 lower_high() + lower_needs <= lower_high_boundary(),
duke@435 700 "must not expand beyond region");
coleenp@1091 701 if (!os::commit_memory(lower_high(), lower_needs, _executable)) {
duke@435 702 debug_only(warning("os::commit_memory failed"));
duke@435 703 return false;
duke@435 704 } else {
duke@435 705 _lower_high += lower_needs;
duke@435 706 }
duke@435 707 }
duke@435 708 if (middle_needs > 0) {
duke@435 709 assert(lower_high_boundary() <= middle_high() &&
duke@435 710 middle_high() + middle_needs <= middle_high_boundary(),
duke@435 711 "must not expand beyond region");
coleenp@1091 712 if (!os::commit_memory(middle_high(), middle_needs, middle_alignment(),
coleenp@1091 713 _executable)) {
duke@435 714 debug_only(warning("os::commit_memory failed"));
duke@435 715 return false;
duke@435 716 }
duke@435 717 _middle_high += middle_needs;
duke@435 718 }
duke@435 719 if (upper_needs > 0) {
duke@435 720 assert(middle_high_boundary() <= upper_high() &&
duke@435 721 upper_high() + upper_needs <= upper_high_boundary(),
duke@435 722 "must not expand beyond region");
coleenp@1091 723 if (!os::commit_memory(upper_high(), upper_needs, _executable)) {
duke@435 724 debug_only(warning("os::commit_memory failed"));
duke@435 725 return false;
duke@435 726 } else {
duke@435 727 _upper_high += upper_needs;
duke@435 728 }
duke@435 729 }
duke@435 730
duke@435 731 if (pre_touch || AlwaysPreTouch) {
duke@435 732 int vm_ps = os::vm_page_size();
duke@435 733 for (char* curr = previous_high;
duke@435 734 curr < unaligned_new_high;
duke@435 735 curr += vm_ps) {
duke@435 736 // Note the use of a write here; originally we tried just a read, but
duke@435 737 // since the value read was unused, the optimizer removed the read.
duke@435 738 // If we ever have a concurrent touchahead thread, we'll want to use
duke@435 739 // a read, to avoid the potential of overwriting data (if a mutator
duke@435 740 // thread beats the touchahead thread to a page). There are various
duke@435 741 // ways of making sure this read is not optimized away: for example,
duke@435 742 // generating the code for a read procedure at runtime.
duke@435 743 *curr = 0;
duke@435 744 }
duke@435 745 }
duke@435 746
duke@435 747 _high += bytes;
duke@435 748 return true;
duke@435 749 }
duke@435 750
duke@435 751 // A page is uncommitted if the contents of the entire page is deemed unusable.
duke@435 752 // Continue to decrement the high() pointer until it reaches a page boundary
duke@435 753 // in which case that particular page can now be uncommitted.
duke@435 754 void VirtualSpace::shrink_by(size_t size) {
duke@435 755 if (committed_size() < size)
duke@435 756 fatal("Cannot shrink virtual space to negative size");
duke@435 757
duke@435 758 if (special()) {
duke@435 759 // don't uncommit if the entire space is pinned in memory
duke@435 760 _high -= size;
duke@435 761 return;
duke@435 762 }
duke@435 763
duke@435 764 char* unaligned_new_high = high() - size;
duke@435 765 assert(unaligned_new_high >= low_boundary(), "cannot shrink past lower boundary");
duke@435 766
duke@435 767 // Calculate new unaligned address
duke@435 768 char* unaligned_upper_new_high =
duke@435 769 MAX2(unaligned_new_high, middle_high_boundary());
duke@435 770 char* unaligned_middle_new_high =
duke@435 771 MAX2(unaligned_new_high, lower_high_boundary());
duke@435 772 char* unaligned_lower_new_high =
duke@435 773 MAX2(unaligned_new_high, low_boundary());
duke@435 774
duke@435 775 // Align address to region's alignment
duke@435 776 char* aligned_upper_new_high =
duke@435 777 (char*) round_to((intptr_t) unaligned_upper_new_high, upper_alignment());
duke@435 778 char* aligned_middle_new_high =
duke@435 779 (char*) round_to((intptr_t) unaligned_middle_new_high, middle_alignment());
duke@435 780 char* aligned_lower_new_high =
duke@435 781 (char*) round_to((intptr_t) unaligned_lower_new_high, lower_alignment());
duke@435 782
duke@435 783 // Determine which regions need to shrink
duke@435 784 size_t upper_needs = 0;
duke@435 785 if (aligned_upper_new_high < upper_high()) {
duke@435 786 upper_needs =
duke@435 787 pointer_delta(upper_high(), aligned_upper_new_high, sizeof(char));
duke@435 788 }
duke@435 789 size_t middle_needs = 0;
duke@435 790 if (aligned_middle_new_high < middle_high()) {
duke@435 791 middle_needs =
duke@435 792 pointer_delta(middle_high(), aligned_middle_new_high, sizeof(char));
duke@435 793 }
duke@435 794 size_t lower_needs = 0;
duke@435 795 if (aligned_lower_new_high < lower_high()) {
duke@435 796 lower_needs =
duke@435 797 pointer_delta(lower_high(), aligned_lower_new_high, sizeof(char));
duke@435 798 }
duke@435 799
duke@435 800 // Check contiguity.
duke@435 801 assert(middle_high_boundary() <= upper_high() &&
duke@435 802 upper_high() <= upper_high_boundary(),
duke@435 803 "high address must be contained within the region");
duke@435 804 assert(lower_high_boundary() <= middle_high() &&
duke@435 805 middle_high() <= middle_high_boundary(),
duke@435 806 "high address must be contained within the region");
duke@435 807 assert(low_boundary() <= lower_high() &&
duke@435 808 lower_high() <= lower_high_boundary(),
duke@435 809 "high address must be contained within the region");
duke@435 810
duke@435 811 // Uncommit
duke@435 812 if (upper_needs > 0) {
duke@435 813 assert(middle_high_boundary() <= aligned_upper_new_high &&
duke@435 814 aligned_upper_new_high + upper_needs <= upper_high_boundary(),
duke@435 815 "must not shrink beyond region");
duke@435 816 if (!os::uncommit_memory(aligned_upper_new_high, upper_needs)) {
duke@435 817 debug_only(warning("os::uncommit_memory failed"));
duke@435 818 return;
duke@435 819 } else {
duke@435 820 _upper_high -= upper_needs;
duke@435 821 }
duke@435 822 }
duke@435 823 if (middle_needs > 0) {
duke@435 824 assert(lower_high_boundary() <= aligned_middle_new_high &&
duke@435 825 aligned_middle_new_high + middle_needs <= middle_high_boundary(),
duke@435 826 "must not shrink beyond region");
duke@435 827 if (!os::uncommit_memory(aligned_middle_new_high, middle_needs)) {
duke@435 828 debug_only(warning("os::uncommit_memory failed"));
duke@435 829 return;
duke@435 830 } else {
duke@435 831 _middle_high -= middle_needs;
duke@435 832 }
duke@435 833 }
duke@435 834 if (lower_needs > 0) {
duke@435 835 assert(low_boundary() <= aligned_lower_new_high &&
duke@435 836 aligned_lower_new_high + lower_needs <= lower_high_boundary(),
duke@435 837 "must not shrink beyond region");
duke@435 838 if (!os::uncommit_memory(aligned_lower_new_high, lower_needs)) {
duke@435 839 debug_only(warning("os::uncommit_memory failed"));
duke@435 840 return;
duke@435 841 } else {
duke@435 842 _lower_high -= lower_needs;
duke@435 843 }
duke@435 844 }
duke@435 845
duke@435 846 _high -= size;
duke@435 847 }
duke@435 848
duke@435 849 #ifndef PRODUCT
duke@435 850 void VirtualSpace::check_for_contiguity() {
duke@435 851 // Check contiguity.
duke@435 852 assert(low_boundary() <= lower_high() &&
duke@435 853 lower_high() <= lower_high_boundary(),
duke@435 854 "high address must be contained within the region");
duke@435 855 assert(lower_high_boundary() <= middle_high() &&
duke@435 856 middle_high() <= middle_high_boundary(),
duke@435 857 "high address must be contained within the region");
duke@435 858 assert(middle_high_boundary() <= upper_high() &&
duke@435 859 upper_high() <= upper_high_boundary(),
duke@435 860 "high address must be contained within the region");
duke@435 861 assert(low() >= low_boundary(), "low");
duke@435 862 assert(low_boundary() <= lower_high_boundary(), "lower high boundary");
duke@435 863 assert(upper_high_boundary() <= high_boundary(), "upper high boundary");
duke@435 864 assert(high() <= upper_high(), "upper high");
duke@435 865 }
duke@435 866
duke@435 867 void VirtualSpace::print() {
duke@435 868 tty->print ("Virtual space:");
duke@435 869 if (special()) tty->print(" (pinned in memory)");
duke@435 870 tty->cr();
hseigel@4465 871 tty->print_cr(" - committed: " SIZE_FORMAT, committed_size());
hseigel@4465 872 tty->print_cr(" - reserved: " SIZE_FORMAT, reserved_size());
duke@435 873 tty->print_cr(" - [low, high]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", low(), high());
duke@435 874 tty->print_cr(" - [low_b, high_b]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", low_boundary(), high_boundary());
duke@435 875 }
duke@435 876
duke@435 877 #endif

mercurial