src/share/vm/runtime/virtualspace.cpp

Wed, 25 Mar 2009 14:19:20 -0400

author
coleenp
date
Wed, 25 Mar 2009 14:19:20 -0400
changeset 1091
6bdd6923ba16
parent 1077
660978a2a31a
child 1279
bd02caa94611
permissions
-rw-r--r--

6541756: Reduce executable C-heap
Summary: Add executable parameters to reserve_memory and commit_memory to reduce executable memory to only the Code Heap.
Reviewed-by: xlu, kvn, acorn

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

mercurial