src/share/vm/runtime/virtualspace.cpp

Mon, 17 Sep 2012 07:36:31 -0400

author
dholmes
date
Mon, 17 Sep 2012 07:36:31 -0400
changeset 4077
a7509aff1b06
parent 4037
da91efe96a93
child 4369
730cc4ddd550
permissions
-rw-r--r--

7194254: jstack reports wrong thread priorities
Reviewed-by: dholmes, sla, fparain
Contributed-by: Dmytro Sheyko <dmytro_sheyko@hotmail.com>

duke@435 1 /*
coleenp@3561 2 * Copyright (c) 1997, 2012, 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");
duke@435 332 // Reserve size large enough to do manual alignment and
duke@435 333 // increase size to a multiple of the desired alignment
duke@435 334 size = align_size_up(size, alignment);
duke@435 335 size_t extra_size = size + alignment;
ysr@777 336 do {
ysr@777 337 char* extra_base = os::reserve_memory(extra_size, NULL, alignment);
ysr@777 338 if (extra_base == NULL) return;
ysr@777 339 // Do manual alignement
ysr@777 340 base = (char*) align_size_up((uintptr_t) extra_base, alignment);
ysr@777 341 assert(base >= extra_base, "just checking");
ysr@777 342 // Re-reserve the region at the aligned base address.
ysr@777 343 os::release_memory(extra_base, extra_size);
ysr@777 344 base = os::reserve_memory(size, base);
ysr@777 345 } while (base == NULL);
johnc@3022 346
johnc@3022 347 if (requested_address != 0 &&
johnc@3022 348 failed_to_reserve_as_requested(base, requested_address, size, false)) {
johnc@3022 349 // As a result of the alignment constraints, the allocated base differs
johnc@3022 350 // from the requested address. Return back to the caller who can
johnc@3022 351 // take remedial action (like try again without a requested address).
johnc@3022 352 assert(_base == NULL, "should be");
johnc@3022 353 return;
johnc@3022 354 }
duke@435 355 }
duke@435 356 }
duke@435 357 // Done
duke@435 358 _base = base;
duke@435 359 _size = size;
johnc@3022 360 _alignment = alignment;
coleenp@672 361 _noaccess_prefix = noaccess_prefix;
coleenp@672 362
coleenp@672 363 // Assert that if noaccess_prefix is used, it is the same as alignment.
coleenp@672 364 assert(noaccess_prefix == 0 ||
coleenp@672 365 noaccess_prefix == _alignment, "noaccess prefix wrong");
duke@435 366
duke@435 367 assert(markOopDesc::encode_pointer_as_mark(_base)->decode_pointer() == _base,
duke@435 368 "area must be distinguisable from marks for mark-sweep");
duke@435 369 assert(markOopDesc::encode_pointer_as_mark(&_base[size])->decode_pointer() == &_base[size],
duke@435 370 "area must be distinguisable from marks for mark-sweep");
duke@435 371 }
duke@435 372
duke@435 373
duke@435 374 ReservedSpace::ReservedSpace(char* base, size_t size, size_t alignment,
coleenp@1091 375 bool special, bool executable) {
duke@435 376 assert((size % os::vm_allocation_granularity()) == 0,
duke@435 377 "size not allocation aligned");
duke@435 378 _base = base;
duke@435 379 _size = size;
duke@435 380 _alignment = alignment;
coleenp@672 381 _noaccess_prefix = 0;
duke@435 382 _special = special;
coleenp@1091 383 _executable = executable;
duke@435 384 }
duke@435 385
duke@435 386
duke@435 387 ReservedSpace ReservedSpace::first_part(size_t partition_size, size_t alignment,
duke@435 388 bool split, bool realloc) {
duke@435 389 assert(partition_size <= size(), "partition failed");
duke@435 390 if (split) {
coleenp@1091 391 os::split_reserved_memory(base(), size(), partition_size, realloc);
duke@435 392 }
coleenp@1091 393 ReservedSpace result(base(), partition_size, alignment, special(),
coleenp@1091 394 executable());
duke@435 395 return result;
duke@435 396 }
duke@435 397
duke@435 398
duke@435 399 ReservedSpace
duke@435 400 ReservedSpace::last_part(size_t partition_size, size_t alignment) {
duke@435 401 assert(partition_size <= size(), "partition failed");
duke@435 402 ReservedSpace result(base() + partition_size, size() - partition_size,
coleenp@1091 403 alignment, special(), executable());
duke@435 404 return result;
duke@435 405 }
duke@435 406
duke@435 407
duke@435 408 size_t ReservedSpace::page_align_size_up(size_t size) {
duke@435 409 return align_size_up(size, os::vm_page_size());
duke@435 410 }
duke@435 411
duke@435 412
duke@435 413 size_t ReservedSpace::page_align_size_down(size_t size) {
duke@435 414 return align_size_down(size, os::vm_page_size());
duke@435 415 }
duke@435 416
duke@435 417
duke@435 418 size_t ReservedSpace::allocation_align_size_up(size_t size) {
duke@435 419 return align_size_up(size, os::vm_allocation_granularity());
duke@435 420 }
duke@435 421
duke@435 422
duke@435 423 size_t ReservedSpace::allocation_align_size_down(size_t size) {
duke@435 424 return align_size_down(size, os::vm_allocation_granularity());
duke@435 425 }
duke@435 426
duke@435 427
duke@435 428 void ReservedSpace::release() {
duke@435 429 if (is_reserved()) {
coleenp@672 430 char *real_base = _base - _noaccess_prefix;
coleenp@672 431 const size_t real_size = _size + _noaccess_prefix;
duke@435 432 if (special()) {
coleenp@672 433 os::release_memory_special(real_base, real_size);
duke@435 434 } else{
coleenp@672 435 os::release_memory(real_base, real_size);
duke@435 436 }
duke@435 437 _base = NULL;
duke@435 438 _size = 0;
coleenp@672 439 _noaccess_prefix = 0;
duke@435 440 _special = false;
coleenp@1091 441 _executable = false;
duke@435 442 }
duke@435 443 }
duke@435 444
coleenp@672 445 void ReservedSpace::protect_noaccess_prefix(const size_t size) {
kvn@1973 446 assert( (_noaccess_prefix != 0) == (UseCompressedOops && _base != NULL &&
coleenp@3561 447 (Universe::narrow_oop_base() != NULL) &&
kvn@1973 448 Universe::narrow_oop_use_implicit_null_checks()),
kvn@1973 449 "noaccess_prefix should be used only with non zero based compressed oops");
kvn@1973 450
kvn@1973 451 // If there is no noaccess prefix, return.
coleenp@672 452 if (_noaccess_prefix == 0) return;
coleenp@672 453
coleenp@672 454 assert(_noaccess_prefix >= (size_t)os::vm_page_size(),
coleenp@672 455 "must be at least page size big");
coleenp@672 456
coleenp@672 457 // Protect memory at the base of the allocated region.
coleenp@672 458 // If special, the page was committed (only matters on windows)
coleenp@672 459 if (!os::protect_memory(_base, _noaccess_prefix, os::MEM_PROT_NONE,
coleenp@672 460 _special)) {
coleenp@672 461 fatal("cannot protect protection page");
coleenp@672 462 }
kvn@1973 463 if (PrintCompressedOopsMode) {
kvn@1973 464 tty->cr();
kvn@1973 465 tty->print_cr("Protected page at the reserved heap base: " PTR_FORMAT " / " INTX_FORMAT " bytes", _base, _noaccess_prefix);
kvn@1973 466 }
coleenp@672 467
coleenp@672 468 _base += _noaccess_prefix;
coleenp@672 469 _size -= _noaccess_prefix;
coleenp@672 470 assert((size == _size) && ((uintptr_t)_base % _alignment == 0),
coleenp@672 471 "must be exactly of required size and alignment");
coleenp@672 472 }
coleenp@672 473
coleenp@672 474 ReservedHeapSpace::ReservedHeapSpace(size_t size, size_t alignment,
coleenp@672 475 bool large, char* requested_address) :
coleenp@672 476 ReservedSpace(size, alignment, large,
coleenp@672 477 requested_address,
kvn@1077 478 (UseCompressedOops && (Universe::narrow_oop_base() != NULL) &&
kvn@1077 479 Universe::narrow_oop_use_implicit_null_checks()) ?
coleenp@760 480 lcm(os::vm_page_size(), alignment) : 0) {
zgu@3900 481 if (base() > 0) {
zgu@3900 482 MemTracker::record_virtual_memory_type((address)base(), mtJavaHeap);
zgu@3900 483 }
zgu@3900 484
coleenp@672 485 // Only reserved space for the java heap should have a noaccess_prefix
coleenp@672 486 // if using compressed oops.
coleenp@672 487 protect_noaccess_prefix(size);
coleenp@672 488 }
coleenp@672 489
coleenp@4037 490 ReservedHeapSpace::ReservedHeapSpace(const size_t heap_space_size,
coleenp@4037 491 const size_t alignment,
kvn@1077 492 char* requested_address) :
coleenp@4037 493 ReservedSpace(heap_space_size, alignment,
kvn@1077 494 requested_address,
kvn@1077 495 (UseCompressedOops && (Universe::narrow_oop_base() != NULL) &&
kvn@1077 496 Universe::narrow_oop_use_implicit_null_checks()) ?
coleenp@4037 497 lcm(os::vm_page_size(), alignment) : 0) {
zgu@3900 498 if (base() > 0) {
zgu@3900 499 MemTracker::record_virtual_memory_type((address)base(), mtJavaHeap);
zgu@3900 500 }
coleenp@4037 501 protect_noaccess_prefix(heap_space_size);
coleenp@672 502 }
duke@435 503
coleenp@1091 504 // Reserve space for code segment. Same as Java heap only we mark this as
coleenp@1091 505 // executable.
coleenp@1091 506 ReservedCodeSpace::ReservedCodeSpace(size_t r_size,
coleenp@1091 507 size_t rs_align,
coleenp@1091 508 bool large) :
coleenp@1091 509 ReservedSpace(r_size, rs_align, large, /*executable*/ true) {
zgu@3900 510 MemTracker::record_virtual_memory_type((address)base(), mtCode);
coleenp@1091 511 }
coleenp@1091 512
duke@435 513 // VirtualSpace
duke@435 514
duke@435 515 VirtualSpace::VirtualSpace() {
duke@435 516 _low_boundary = NULL;
duke@435 517 _high_boundary = NULL;
duke@435 518 _low = NULL;
duke@435 519 _high = NULL;
duke@435 520 _lower_high = NULL;
duke@435 521 _middle_high = NULL;
duke@435 522 _upper_high = NULL;
duke@435 523 _lower_high_boundary = NULL;
duke@435 524 _middle_high_boundary = NULL;
duke@435 525 _upper_high_boundary = NULL;
duke@435 526 _lower_alignment = 0;
duke@435 527 _middle_alignment = 0;
duke@435 528 _upper_alignment = 0;
coleenp@672 529 _special = false;
coleenp@1091 530 _executable = false;
duke@435 531 }
duke@435 532
duke@435 533
duke@435 534 bool VirtualSpace::initialize(ReservedSpace rs, size_t committed_size) {
duke@435 535 if(!rs.is_reserved()) return false; // allocation failed.
duke@435 536 assert(_low_boundary == NULL, "VirtualSpace already initialized");
duke@435 537 _low_boundary = rs.base();
duke@435 538 _high_boundary = low_boundary() + rs.size();
duke@435 539
duke@435 540 _low = low_boundary();
duke@435 541 _high = low();
duke@435 542
duke@435 543 _special = rs.special();
coleenp@1091 544 _executable = rs.executable();
duke@435 545
duke@435 546 // When a VirtualSpace begins life at a large size, make all future expansion
duke@435 547 // and shrinking occur aligned to a granularity of large pages. This avoids
duke@435 548 // fragmentation of physical addresses that inhibits the use of large pages
duke@435 549 // by the OS virtual memory system. Empirically, we see that with a 4MB
duke@435 550 // page size, the only spaces that get handled this way are codecache and
duke@435 551 // the heap itself, both of which provide a substantial performance
duke@435 552 // boost in many benchmarks when covered by large pages.
duke@435 553 //
duke@435 554 // No attempt is made to force large page alignment at the very top and
duke@435 555 // bottom of the space if they are not aligned so already.
duke@435 556 _lower_alignment = os::vm_page_size();
duke@435 557 _middle_alignment = os::page_size_for_region(rs.size(), rs.size(), 1);
duke@435 558 _upper_alignment = os::vm_page_size();
duke@435 559
duke@435 560 // End of each region
duke@435 561 _lower_high_boundary = (char*) round_to((intptr_t) low_boundary(), middle_alignment());
duke@435 562 _middle_high_boundary = (char*) round_down((intptr_t) high_boundary(), middle_alignment());
duke@435 563 _upper_high_boundary = high_boundary();
duke@435 564
duke@435 565 // High address of each region
duke@435 566 _lower_high = low_boundary();
duke@435 567 _middle_high = lower_high_boundary();
duke@435 568 _upper_high = middle_high_boundary();
duke@435 569
duke@435 570 // commit to initial size
duke@435 571 if (committed_size > 0) {
duke@435 572 if (!expand_by(committed_size)) {
duke@435 573 return false;
duke@435 574 }
duke@435 575 }
duke@435 576 return true;
duke@435 577 }
duke@435 578
duke@435 579
duke@435 580 VirtualSpace::~VirtualSpace() {
duke@435 581 release();
duke@435 582 }
duke@435 583
duke@435 584
duke@435 585 void VirtualSpace::release() {
coleenp@672 586 // This does not release memory it never reserved.
coleenp@672 587 // Caller must release via rs.release();
duke@435 588 _low_boundary = NULL;
duke@435 589 _high_boundary = NULL;
duke@435 590 _low = NULL;
duke@435 591 _high = NULL;
duke@435 592 _lower_high = NULL;
duke@435 593 _middle_high = NULL;
duke@435 594 _upper_high = NULL;
duke@435 595 _lower_high_boundary = NULL;
duke@435 596 _middle_high_boundary = NULL;
duke@435 597 _upper_high_boundary = NULL;
duke@435 598 _lower_alignment = 0;
duke@435 599 _middle_alignment = 0;
duke@435 600 _upper_alignment = 0;
duke@435 601 _special = false;
coleenp@1091 602 _executable = false;
duke@435 603 }
duke@435 604
duke@435 605
duke@435 606 size_t VirtualSpace::committed_size() const {
duke@435 607 return pointer_delta(high(), low(), sizeof(char));
duke@435 608 }
duke@435 609
duke@435 610
duke@435 611 size_t VirtualSpace::reserved_size() const {
duke@435 612 return pointer_delta(high_boundary(), low_boundary(), sizeof(char));
duke@435 613 }
duke@435 614
duke@435 615
duke@435 616 size_t VirtualSpace::uncommitted_size() const {
duke@435 617 return reserved_size() - committed_size();
duke@435 618 }
duke@435 619
duke@435 620
duke@435 621 bool VirtualSpace::contains(const void* p) const {
duke@435 622 return low() <= (const char*) p && (const char*) p < high();
duke@435 623 }
duke@435 624
duke@435 625 /*
duke@435 626 First we need to determine if a particular virtual space is using large
duke@435 627 pages. This is done at the initialize function and only virtual spaces
duke@435 628 that are larger than LargePageSizeInBytes use large pages. Once we
duke@435 629 have determined this, all expand_by and shrink_by calls must grow and
duke@435 630 shrink by large page size chunks. If a particular request
duke@435 631 is within the current large page, the call to commit and uncommit memory
duke@435 632 can be ignored. In the case that the low and high boundaries of this
duke@435 633 space is not large page aligned, the pages leading to the first large
duke@435 634 page address and the pages after the last large page address must be
duke@435 635 allocated with default pages.
duke@435 636 */
duke@435 637 bool VirtualSpace::expand_by(size_t bytes, bool pre_touch) {
duke@435 638 if (uncommitted_size() < bytes) return false;
duke@435 639
duke@435 640 if (special()) {
duke@435 641 // don't commit memory if the entire space is pinned in memory
duke@435 642 _high += bytes;
duke@435 643 return true;
duke@435 644 }
duke@435 645
duke@435 646 char* previous_high = high();
duke@435 647 char* unaligned_new_high = high() + bytes;
duke@435 648 assert(unaligned_new_high <= high_boundary(),
duke@435 649 "cannot expand by more than upper boundary");
duke@435 650
duke@435 651 // Calculate where the new high for each of the regions should be. If
duke@435 652 // the low_boundary() and high_boundary() are LargePageSizeInBytes aligned
duke@435 653 // then the unaligned lower and upper new highs would be the
duke@435 654 // lower_high() and upper_high() respectively.
duke@435 655 char* unaligned_lower_new_high =
duke@435 656 MIN2(unaligned_new_high, lower_high_boundary());
duke@435 657 char* unaligned_middle_new_high =
duke@435 658 MIN2(unaligned_new_high, middle_high_boundary());
duke@435 659 char* unaligned_upper_new_high =
duke@435 660 MIN2(unaligned_new_high, upper_high_boundary());
duke@435 661
duke@435 662 // Align the new highs based on the regions alignment. lower and upper
duke@435 663 // alignment will always be default page size. middle alignment will be
duke@435 664 // LargePageSizeInBytes if the actual size of the virtual space is in
duke@435 665 // fact larger than LargePageSizeInBytes.
duke@435 666 char* aligned_lower_new_high =
duke@435 667 (char*) round_to((intptr_t) unaligned_lower_new_high, lower_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_upper_new_high =
duke@435 671 (char*) round_to((intptr_t) unaligned_upper_new_high, upper_alignment());
duke@435 672
duke@435 673 // Determine which regions need to grow in this expand_by call.
duke@435 674 // If you are growing in the lower region, high() must be in that
duke@435 675 // region so calcuate the size based on high(). For the middle and
duke@435 676 // upper regions, determine the starting point of growth based on the
duke@435 677 // location of high(). By getting the MAX of the region's low address
duke@435 678 // (or the prevoius region's high address) and high(), we can tell if it
duke@435 679 // is an intra or inter region growth.
duke@435 680 size_t lower_needs = 0;
duke@435 681 if (aligned_lower_new_high > lower_high()) {
duke@435 682 lower_needs =
duke@435 683 pointer_delta(aligned_lower_new_high, lower_high(), sizeof(char));
duke@435 684 }
duke@435 685 size_t middle_needs = 0;
duke@435 686 if (aligned_middle_new_high > middle_high()) {
duke@435 687 middle_needs =
duke@435 688 pointer_delta(aligned_middle_new_high, middle_high(), sizeof(char));
duke@435 689 }
duke@435 690 size_t upper_needs = 0;
duke@435 691 if (aligned_upper_new_high > upper_high()) {
duke@435 692 upper_needs =
duke@435 693 pointer_delta(aligned_upper_new_high, upper_high(), sizeof(char));
duke@435 694 }
duke@435 695
duke@435 696 // Check contiguity.
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 assert(lower_high_boundary() <= middle_high() &&
duke@435 701 middle_high() <= middle_high_boundary(),
duke@435 702 "high address must be contained within the region");
duke@435 703 assert(middle_high_boundary() <= upper_high() &&
duke@435 704 upper_high() <= upper_high_boundary(),
duke@435 705 "high address must be contained within the region");
duke@435 706
duke@435 707 // Commit regions
duke@435 708 if (lower_needs > 0) {
duke@435 709 assert(low_boundary() <= lower_high() &&
duke@435 710 lower_high() + lower_needs <= lower_high_boundary(),
duke@435 711 "must not expand beyond region");
coleenp@1091 712 if (!os::commit_memory(lower_high(), lower_needs, _executable)) {
duke@435 713 debug_only(warning("os::commit_memory failed"));
duke@435 714 return false;
duke@435 715 } else {
duke@435 716 _lower_high += lower_needs;
duke@435 717 }
duke@435 718 }
duke@435 719 if (middle_needs > 0) {
duke@435 720 assert(lower_high_boundary() <= middle_high() &&
duke@435 721 middle_high() + middle_needs <= middle_high_boundary(),
duke@435 722 "must not expand beyond region");
coleenp@1091 723 if (!os::commit_memory(middle_high(), middle_needs, middle_alignment(),
coleenp@1091 724 _executable)) {
duke@435 725 debug_only(warning("os::commit_memory failed"));
duke@435 726 return false;
duke@435 727 }
duke@435 728 _middle_high += middle_needs;
duke@435 729 }
duke@435 730 if (upper_needs > 0) {
duke@435 731 assert(middle_high_boundary() <= upper_high() &&
duke@435 732 upper_high() + upper_needs <= upper_high_boundary(),
duke@435 733 "must not expand beyond region");
coleenp@1091 734 if (!os::commit_memory(upper_high(), upper_needs, _executable)) {
duke@435 735 debug_only(warning("os::commit_memory failed"));
duke@435 736 return false;
duke@435 737 } else {
duke@435 738 _upper_high += upper_needs;
duke@435 739 }
duke@435 740 }
duke@435 741
duke@435 742 if (pre_touch || AlwaysPreTouch) {
duke@435 743 int vm_ps = os::vm_page_size();
duke@435 744 for (char* curr = previous_high;
duke@435 745 curr < unaligned_new_high;
duke@435 746 curr += vm_ps) {
duke@435 747 // Note the use of a write here; originally we tried just a read, but
duke@435 748 // since the value read was unused, the optimizer removed the read.
duke@435 749 // If we ever have a concurrent touchahead thread, we'll want to use
duke@435 750 // a read, to avoid the potential of overwriting data (if a mutator
duke@435 751 // thread beats the touchahead thread to a page). There are various
duke@435 752 // ways of making sure this read is not optimized away: for example,
duke@435 753 // generating the code for a read procedure at runtime.
duke@435 754 *curr = 0;
duke@435 755 }
duke@435 756 }
duke@435 757
duke@435 758 _high += bytes;
duke@435 759 return true;
duke@435 760 }
duke@435 761
duke@435 762 // A page is uncommitted if the contents of the entire page is deemed unusable.
duke@435 763 // Continue to decrement the high() pointer until it reaches a page boundary
duke@435 764 // in which case that particular page can now be uncommitted.
duke@435 765 void VirtualSpace::shrink_by(size_t size) {
duke@435 766 if (committed_size() < size)
duke@435 767 fatal("Cannot shrink virtual space to negative size");
duke@435 768
duke@435 769 if (special()) {
duke@435 770 // don't uncommit if the entire space is pinned in memory
duke@435 771 _high -= size;
duke@435 772 return;
duke@435 773 }
duke@435 774
duke@435 775 char* unaligned_new_high = high() - size;
duke@435 776 assert(unaligned_new_high >= low_boundary(), "cannot shrink past lower boundary");
duke@435 777
duke@435 778 // Calculate new unaligned address
duke@435 779 char* unaligned_upper_new_high =
duke@435 780 MAX2(unaligned_new_high, middle_high_boundary());
duke@435 781 char* unaligned_middle_new_high =
duke@435 782 MAX2(unaligned_new_high, lower_high_boundary());
duke@435 783 char* unaligned_lower_new_high =
duke@435 784 MAX2(unaligned_new_high, low_boundary());
duke@435 785
duke@435 786 // Align address to region's alignment
duke@435 787 char* aligned_upper_new_high =
duke@435 788 (char*) round_to((intptr_t) unaligned_upper_new_high, upper_alignment());
duke@435 789 char* aligned_middle_new_high =
duke@435 790 (char*) round_to((intptr_t) unaligned_middle_new_high, middle_alignment());
duke@435 791 char* aligned_lower_new_high =
duke@435 792 (char*) round_to((intptr_t) unaligned_lower_new_high, lower_alignment());
duke@435 793
duke@435 794 // Determine which regions need to shrink
duke@435 795 size_t upper_needs = 0;
duke@435 796 if (aligned_upper_new_high < upper_high()) {
duke@435 797 upper_needs =
duke@435 798 pointer_delta(upper_high(), aligned_upper_new_high, sizeof(char));
duke@435 799 }
duke@435 800 size_t middle_needs = 0;
duke@435 801 if (aligned_middle_new_high < middle_high()) {
duke@435 802 middle_needs =
duke@435 803 pointer_delta(middle_high(), aligned_middle_new_high, sizeof(char));
duke@435 804 }
duke@435 805 size_t lower_needs = 0;
duke@435 806 if (aligned_lower_new_high < lower_high()) {
duke@435 807 lower_needs =
duke@435 808 pointer_delta(lower_high(), aligned_lower_new_high, sizeof(char));
duke@435 809 }
duke@435 810
duke@435 811 // Check contiguity.
duke@435 812 assert(middle_high_boundary() <= upper_high() &&
duke@435 813 upper_high() <= upper_high_boundary(),
duke@435 814 "high address must be contained within the region");
duke@435 815 assert(lower_high_boundary() <= middle_high() &&
duke@435 816 middle_high() <= middle_high_boundary(),
duke@435 817 "high address must be contained within the region");
duke@435 818 assert(low_boundary() <= lower_high() &&
duke@435 819 lower_high() <= lower_high_boundary(),
duke@435 820 "high address must be contained within the region");
duke@435 821
duke@435 822 // Uncommit
duke@435 823 if (upper_needs > 0) {
duke@435 824 assert(middle_high_boundary() <= aligned_upper_new_high &&
duke@435 825 aligned_upper_new_high + upper_needs <= upper_high_boundary(),
duke@435 826 "must not shrink beyond region");
duke@435 827 if (!os::uncommit_memory(aligned_upper_new_high, upper_needs)) {
duke@435 828 debug_only(warning("os::uncommit_memory failed"));
duke@435 829 return;
duke@435 830 } else {
duke@435 831 _upper_high -= upper_needs;
duke@435 832 }
duke@435 833 }
duke@435 834 if (middle_needs > 0) {
duke@435 835 assert(lower_high_boundary() <= aligned_middle_new_high &&
duke@435 836 aligned_middle_new_high + middle_needs <= middle_high_boundary(),
duke@435 837 "must not shrink beyond region");
duke@435 838 if (!os::uncommit_memory(aligned_middle_new_high, middle_needs)) {
duke@435 839 debug_only(warning("os::uncommit_memory failed"));
duke@435 840 return;
duke@435 841 } else {
duke@435 842 _middle_high -= middle_needs;
duke@435 843 }
duke@435 844 }
duke@435 845 if (lower_needs > 0) {
duke@435 846 assert(low_boundary() <= aligned_lower_new_high &&
duke@435 847 aligned_lower_new_high + lower_needs <= lower_high_boundary(),
duke@435 848 "must not shrink beyond region");
duke@435 849 if (!os::uncommit_memory(aligned_lower_new_high, lower_needs)) {
duke@435 850 debug_only(warning("os::uncommit_memory failed"));
duke@435 851 return;
duke@435 852 } else {
duke@435 853 _lower_high -= lower_needs;
duke@435 854 }
duke@435 855 }
duke@435 856
duke@435 857 _high -= size;
duke@435 858 }
duke@435 859
duke@435 860 #ifndef PRODUCT
duke@435 861 void VirtualSpace::check_for_contiguity() {
duke@435 862 // Check contiguity.
duke@435 863 assert(low_boundary() <= lower_high() &&
duke@435 864 lower_high() <= lower_high_boundary(),
duke@435 865 "high address must be contained within the region");
duke@435 866 assert(lower_high_boundary() <= middle_high() &&
duke@435 867 middle_high() <= middle_high_boundary(),
duke@435 868 "high address must be contained within the region");
duke@435 869 assert(middle_high_boundary() <= upper_high() &&
duke@435 870 upper_high() <= upper_high_boundary(),
duke@435 871 "high address must be contained within the region");
duke@435 872 assert(low() >= low_boundary(), "low");
duke@435 873 assert(low_boundary() <= lower_high_boundary(), "lower high boundary");
duke@435 874 assert(upper_high_boundary() <= high_boundary(), "upper high boundary");
duke@435 875 assert(high() <= upper_high(), "upper high");
duke@435 876 }
duke@435 877
duke@435 878 void VirtualSpace::print() {
duke@435 879 tty->print ("Virtual space:");
duke@435 880 if (special()) tty->print(" (pinned in memory)");
duke@435 881 tty->cr();
duke@435 882 tty->print_cr(" - committed: %ld", committed_size());
duke@435 883 tty->print_cr(" - reserved: %ld", reserved_size());
duke@435 884 tty->print_cr(" - [low, high]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", low(), high());
duke@435 885 tty->print_cr(" - [low_b, high_b]: [" INTPTR_FORMAT ", " INTPTR_FORMAT "]", low_boundary(), high_boundary());
duke@435 886 }
duke@435 887
duke@435 888 #endif

mercurial