annotate src/share/vm/memory/heap.cpp @ 2297:fe8ab1affd14

merge with jdk7-b138
author mcimadamore
date Thu, 14 Apr 2011 23:31:30 -0700
parents 1c0cf339481b
children
rev   line source
duke@0 1 /*
trims@2281 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1885 25 #include "precompiled.hpp"
stefank@1885 26 #include "memory/heap.hpp"
stefank@1885 27 #include "oops/oop.inline.hpp"
stefank@1885 28 #include "runtime/os.hpp"
duke@0 29
duke@0 30
duke@0 31 size_t CodeHeap::header_size() {
duke@0 32 return sizeof(HeapBlock);
duke@0 33 }
duke@0 34
duke@0 35
duke@0 36 // Implementation of Heap
duke@0 37
duke@0 38 CodeHeap::CodeHeap() {
duke@0 39 _number_of_committed_segments = 0;
duke@0 40 _number_of_reserved_segments = 0;
duke@0 41 _segment_size = 0;
duke@0 42 _log2_segment_size = 0;
duke@0 43 _next_segment = 0;
duke@0 44 _freelist = NULL;
duke@0 45 _free_segments = 0;
duke@0 46 }
duke@0 47
duke@0 48
duke@0 49 void CodeHeap::mark_segmap_as_free(size_t beg, size_t end) {
duke@0 50 assert(0 <= beg && beg < _number_of_committed_segments, "interval begin out of bounds");
duke@0 51 assert(beg < end && end <= _number_of_committed_segments, "interval end out of bounds");
duke@0 52 // setup _segmap pointers for faster indexing
duke@0 53 address p = (address)_segmap.low() + beg;
duke@0 54 address q = (address)_segmap.low() + end;
duke@0 55 // initialize interval
duke@0 56 while (p < q) *p++ = 0xFF;
duke@0 57 }
duke@0 58
duke@0 59
duke@0 60 void CodeHeap::mark_segmap_as_used(size_t beg, size_t end) {
duke@0 61 assert(0 <= beg && beg < _number_of_committed_segments, "interval begin out of bounds");
duke@0 62 assert(beg < end && end <= _number_of_committed_segments, "interval end out of bounds");
duke@0 63 // setup _segmap pointers for faster indexing
duke@0 64 address p = (address)_segmap.low() + beg;
duke@0 65 address q = (address)_segmap.low() + end;
duke@0 66 // initialize interval
duke@0 67 int i = 0;
duke@0 68 while (p < q) {
duke@0 69 *p++ = i++;
duke@0 70 if (i == 0xFF) i = 1;
duke@0 71 }
duke@0 72 }
duke@0 73
duke@0 74
duke@0 75 static size_t align_to_page_size(size_t size) {
duke@0 76 const size_t alignment = (size_t)os::vm_page_size();
duke@0 77 assert(is_power_of_2(alignment), "no kidding ???");
duke@0 78 return (size + alignment - 1) & ~(alignment - 1);
duke@0 79 }
duke@0 80
duke@0 81
duke@0 82 static size_t align_to_allocation_size(size_t size) {
duke@0 83 const size_t alignment = (size_t)os::vm_allocation_granularity();
duke@0 84 assert(is_power_of_2(alignment), "no kidding ???");
duke@0 85 return (size + alignment - 1) & ~(alignment - 1);
duke@0 86 }
duke@0 87
duke@0 88
duke@0 89 void CodeHeap::on_code_mapping(char* base, size_t size) {
duke@0 90 #ifdef LINUX
duke@0 91 extern void linux_wrap_code(char* base, size_t size);
duke@0 92 linux_wrap_code(base, size);
duke@0 93 #endif
duke@0 94 }
duke@0 95
duke@0 96
duke@0 97 bool CodeHeap::reserve(size_t reserved_size, size_t committed_size,
duke@0 98 size_t segment_size) {
duke@0 99 assert(reserved_size >= committed_size, "reserved < committed");
duke@0 100 assert(segment_size >= sizeof(FreeBlock), "segment size is too small");
duke@0 101 assert(is_power_of_2(segment_size), "segment_size must be a power of 2");
duke@0 102
duke@0 103 _segment_size = segment_size;
duke@0 104 _log2_segment_size = exact_log2(segment_size);
duke@0 105
duke@0 106 // Reserve and initialize space for _memory.
jcoomes@79 107 const size_t page_size = os::can_execute_large_page_memory() ?
jcoomes@79 108 os::page_size_for_region(committed_size, reserved_size, 8) :
jcoomes@79 109 os::vm_page_size();
duke@0 110 const size_t granularity = os::vm_allocation_granularity();
duke@0 111 const size_t r_align = MAX2(page_size, granularity);
duke@0 112 const size_t r_size = align_size_up(reserved_size, r_align);
duke@0 113 const size_t c_size = align_size_up(committed_size, page_size);
duke@0 114
duke@0 115 const size_t rs_align = page_size == (size_t) os::vm_page_size() ? 0 :
duke@0 116 MAX2(page_size, granularity);
coleenp@656 117 ReservedCodeSpace rs(r_size, rs_align, rs_align > 0);
duke@0 118 os::trace_page_sizes("code heap", committed_size, reserved_size, page_size,
duke@0 119 rs.base(), rs.size());
duke@0 120 if (!_memory.initialize(rs, c_size)) {
duke@0 121 return false;
duke@0 122 }
duke@0 123
duke@0 124 on_code_mapping(_memory.low(), _memory.committed_size());
duke@0 125 _number_of_committed_segments = number_of_segments(_memory.committed_size());
duke@0 126 _number_of_reserved_segments = number_of_segments(_memory.reserved_size());
duke@0 127 assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
duke@0 128
duke@0 129 // reserve space for _segmap
duke@0 130 if (!_segmap.initialize(align_to_page_size(_number_of_reserved_segments), align_to_page_size(_number_of_committed_segments))) {
duke@0 131 return false;
duke@0 132 }
duke@0 133 assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "could not commit enough space for segment map");
duke@0 134 assert(_segmap.reserved_size() >= (size_t) _number_of_reserved_segments , "could not reserve enough space for segment map");
duke@0 135 assert(_segmap.reserved_size() >= _segmap.committed_size() , "just checking");
duke@0 136
duke@0 137 // initialize remaining instance variables
duke@0 138 clear();
duke@0 139 return true;
duke@0 140 }
duke@0 141
duke@0 142
duke@0 143 void CodeHeap::release() {
duke@0 144 Unimplemented();
duke@0 145 }
duke@0 146
duke@0 147
duke@0 148 bool CodeHeap::expand_by(size_t size) {
duke@0 149 // expand _memory space
duke@0 150 size_t dm = align_to_page_size(_memory.committed_size() + size) - _memory.committed_size();
duke@0 151 if (dm > 0) {
duke@0 152 char* base = _memory.low() + _memory.committed_size();
duke@0 153 if (!_memory.expand_by(dm)) return false;
duke@0 154 on_code_mapping(base, dm);
duke@0 155 size_t i = _number_of_committed_segments;
duke@0 156 _number_of_committed_segments = number_of_segments(_memory.committed_size());
duke@0 157 assert(_number_of_reserved_segments == number_of_segments(_memory.reserved_size()), "number of reserved segments should not change");
duke@0 158 assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
duke@0 159 // expand _segmap space
duke@0 160 size_t ds = align_to_page_size(_number_of_committed_segments) - _segmap.committed_size();
duke@0 161 if (ds > 0) {
duke@0 162 if (!_segmap.expand_by(ds)) return false;
duke@0 163 }
duke@0 164 assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "just checking");
duke@0 165 // initialize additional segmap entries
duke@0 166 mark_segmap_as_free(i, _number_of_committed_segments);
duke@0 167 }
duke@0 168 return true;
duke@0 169 }
duke@0 170
duke@0 171
duke@0 172 void CodeHeap::shrink_by(size_t size) {
duke@0 173 Unimplemented();
duke@0 174 }
duke@0 175
duke@0 176
duke@0 177 void CodeHeap::clear() {
duke@0 178 _next_segment = 0;
duke@0 179 mark_segmap_as_free(0, _number_of_committed_segments);
duke@0 180 }
duke@0 181
duke@0 182
duke@0 183 void* CodeHeap::allocate(size_t size) {
duke@0 184 size_t length = number_of_segments(size + sizeof(HeapBlock));
duke@0 185 assert(length *_segment_size >= sizeof(FreeBlock), "not enough room for FreeList");
duke@0 186
duke@0 187 // First check if we can satify request from freelist
duke@0 188 debug_only(verify());
duke@0 189 HeapBlock* block = search_freelist(length);
duke@0 190 debug_only(if (VerifyCodeCacheOften) verify());
duke@0 191 if (block != NULL) {
duke@0 192 assert(block->length() >= length && block->length() < length + CodeCacheMinBlockLength, "sanity check");
duke@0 193 assert(!block->free(), "must be marked free");
duke@0 194 #ifdef ASSERT
duke@0 195 memset((void *)block->allocated_space(), badCodeHeapNewVal, size);
duke@0 196 #endif
duke@0 197 return block->allocated_space();
duke@0 198 }
duke@0 199
duke@0 200 if (length < CodeCacheMinBlockLength) {
duke@0 201 length = CodeCacheMinBlockLength;
duke@0 202 }
duke@0 203 if (_next_segment + length <= _number_of_committed_segments) {
duke@0 204 mark_segmap_as_used(_next_segment, _next_segment + length);
duke@0 205 HeapBlock* b = block_at(_next_segment);
duke@0 206 b->initialize(length);
duke@0 207 _next_segment += length;
duke@0 208 #ifdef ASSERT
duke@0 209 memset((void *)b->allocated_space(), badCodeHeapNewVal, size);
duke@0 210 #endif
duke@0 211 return b->allocated_space();
duke@0 212 } else {
duke@0 213 return NULL;
duke@0 214 }
duke@0 215 }
duke@0 216
duke@0 217
duke@0 218 void CodeHeap::deallocate(void* p) {
duke@0 219 assert(p == find_start(p), "illegal deallocation");
duke@0 220 // Find start of HeapBlock
duke@0 221 HeapBlock* b = (((HeapBlock *)p) - 1);
duke@0 222 assert(b->allocated_space() == p, "sanity check");
duke@0 223 #ifdef ASSERT
duke@0 224 memset((void *)b->allocated_space(),
duke@0 225 badCodeHeapFreeVal,
duke@0 226 size(b->length()) - sizeof(HeapBlock));
duke@0 227 #endif
duke@0 228 add_to_freelist(b);
duke@0 229
duke@0 230 debug_only(if (VerifyCodeCacheOften) verify());
duke@0 231 }
duke@0 232
duke@0 233
duke@0 234 void* CodeHeap::find_start(void* p) const {
duke@0 235 if (!contains(p)) {
duke@0 236 return NULL;
duke@0 237 }
duke@0 238 size_t i = segment_for(p);
duke@0 239 address b = (address)_segmap.low();
duke@0 240 if (b[i] == 0xFF) {
duke@0 241 return NULL;
duke@0 242 }
duke@0 243 while (b[i] > 0) i -= (int)b[i];
duke@0 244 HeapBlock* h = block_at(i);
duke@0 245 if (h->free()) {
duke@0 246 return NULL;
duke@0 247 }
duke@0 248 return h->allocated_space();
duke@0 249 }
duke@0 250
duke@0 251
duke@0 252 size_t CodeHeap::alignment_unit() const {
duke@0 253 // this will be a power of two
duke@0 254 return _segment_size;
duke@0 255 }
duke@0 256
duke@0 257
duke@0 258 size_t CodeHeap::alignment_offset() const {
duke@0 259 // The lowest address in any allocated block will be
duke@0 260 // equal to alignment_offset (mod alignment_unit).
duke@0 261 return sizeof(HeapBlock) & (_segment_size - 1);
duke@0 262 }
duke@0 263
duke@0 264 // Finds the next free heapblock. If the current one is free, that it returned
duke@0 265 void* CodeHeap::next_free(HeapBlock *b) const {
duke@0 266 // Since free blocks are merged, there is max. on free block
duke@0 267 // between two used ones
duke@0 268 if (b != NULL && b->free()) b = next_block(b);
duke@0 269 assert(b == NULL || !b->free(), "must be in use or at end of heap");
duke@0 270 return (b == NULL) ? NULL : b->allocated_space();
duke@0 271 }
duke@0 272
duke@0 273 // Returns the first used HeapBlock
duke@0 274 HeapBlock* CodeHeap::first_block() const {
duke@0 275 if (_next_segment > 0)
duke@0 276 return block_at(0);
duke@0 277 return NULL;
duke@0 278 }
duke@0 279
duke@0 280 HeapBlock *CodeHeap::block_start(void *q) const {
duke@0 281 HeapBlock* b = (HeapBlock*)find_start(q);
duke@0 282 if (b == NULL) return NULL;
duke@0 283 return b - 1;
duke@0 284 }
duke@0 285
duke@0 286 // Returns the next Heap block an offset into one
duke@0 287 HeapBlock* CodeHeap::next_block(HeapBlock *b) const {
duke@0 288 if (b == NULL) return NULL;
duke@0 289 size_t i = segment_for(b) + b->length();
duke@0 290 if (i < _next_segment)
duke@0 291 return block_at(i);
duke@0 292 return NULL;
duke@0 293 }
duke@0 294
duke@0 295
duke@0 296 // Returns current capacity
duke@0 297 size_t CodeHeap::capacity() const {
duke@0 298 return _memory.committed_size();
duke@0 299 }
duke@0 300
duke@0 301 size_t CodeHeap::max_capacity() const {
duke@0 302 return _memory.reserved_size();
duke@0 303 }
duke@0 304
duke@0 305 size_t CodeHeap::allocated_capacity() const {
duke@0 306 // Start with the committed size in _memory;
duke@0 307 size_t l = _memory.committed_size();
duke@0 308
duke@0 309 // Subtract the committed, but unused, segments
duke@0 310 l -= size(_number_of_committed_segments - _next_segment);
duke@0 311
duke@0 312 // Subtract the size of the freelist
duke@0 313 l -= size(_free_segments);
duke@0 314
duke@0 315 return l;
duke@0 316 }
duke@0 317
kvn@1986 318 size_t CodeHeap::largest_free_block() const {
kvn@2208 319 // First check unused space excluding free blocks.
kvn@2208 320 size_t free_sz = size(_free_segments);
kvn@2208 321 size_t unused = max_capacity() - allocated_capacity() - free_sz;
kvn@2208 322 if (unused >= free_sz)
kvn@2208 323 return unused;
kvn@2208 324
kvn@2208 325 // Now check largest free block.
kvn@1986 326 size_t len = 0;
kvn@1986 327 for (FreeBlock* b = _freelist; b != NULL; b = b->link()) {
kvn@1986 328 if (b->length() > len)
kvn@1986 329 len = b->length();
kvn@1986 330 }
kvn@2208 331 return MAX2(unused, size(len));
kvn@1986 332 }
kvn@1986 333
duke@0 334 // Free list management
duke@0 335
duke@0 336 FreeBlock *CodeHeap::following_block(FreeBlock *b) {
duke@0 337 return (FreeBlock*)(((address)b) + _segment_size * b->length());
duke@0 338 }
duke@0 339
duke@0 340 // Inserts block b after a
duke@0 341 void CodeHeap::insert_after(FreeBlock* a, FreeBlock* b) {
duke@0 342 assert(a != NULL && b != NULL, "must be real pointers");
duke@0 343
duke@0 344 // Link b into the list after a
duke@0 345 b->set_link(a->link());
duke@0 346 a->set_link(b);
duke@0 347
duke@0 348 // See if we can merge blocks
duke@0 349 merge_right(b); // Try to make b bigger
duke@0 350 merge_right(a); // Try to make a include b
duke@0 351 }
duke@0 352
duke@0 353 // Try to merge this block with the following block
duke@0 354 void CodeHeap::merge_right(FreeBlock *a) {
duke@0 355 assert(a->free(), "must be a free block");
duke@0 356 if (following_block(a) == a->link()) {
duke@0 357 assert(a->link() != NULL && a->link()->free(), "must be free too");
duke@0 358 // Update block a to include the following block
duke@0 359 a->set_length(a->length() + a->link()->length());
duke@0 360 a->set_link(a->link()->link());
duke@0 361 // Update find_start map
duke@0 362 size_t beg = segment_for(a);
duke@0 363 mark_segmap_as_used(beg, beg + a->length());
duke@0 364 }
duke@0 365 }
duke@0 366
duke@0 367 void CodeHeap::add_to_freelist(HeapBlock *a) {
duke@0 368 FreeBlock* b = (FreeBlock*)a;
duke@0 369 assert(b != _freelist, "cannot be removed twice");
duke@0 370
duke@0 371 // Mark as free and update free space count
duke@0 372 _free_segments += b->length();
duke@0 373 b->set_free();
duke@0 374
duke@0 375 // First element in list?
duke@0 376 if (_freelist == NULL) {
duke@0 377 _freelist = b;
duke@0 378 b->set_link(NULL);
duke@0 379 return;
duke@0 380 }
duke@0 381
duke@0 382 // Scan for right place to put into list. List
duke@0 383 // is sorted by increasing addresseses
duke@0 384 FreeBlock* prev = NULL;
duke@0 385 FreeBlock* cur = _freelist;
duke@0 386 while(cur != NULL && cur < b) {
duke@0 387 assert(prev == NULL || prev < cur, "must be ordered");
duke@0 388 prev = cur;
duke@0 389 cur = cur->link();
duke@0 390 }
duke@0 391
duke@0 392 assert( (prev == NULL && b < _freelist) ||
duke@0 393 (prev < b && (cur == NULL || b < cur)), "list must be ordered");
duke@0 394
duke@0 395 if (prev == NULL) {
duke@0 396 // Insert first in list
duke@0 397 b->set_link(_freelist);
duke@0 398 _freelist = b;
duke@0 399 merge_right(_freelist);
duke@0 400 } else {
duke@0 401 insert_after(prev, b);
duke@0 402 }
duke@0 403 }
duke@0 404
duke@0 405 // Search freelist for an entry on the list with the best fit
duke@0 406 // Return NULL if no one was found
duke@0 407 FreeBlock* CodeHeap::search_freelist(size_t length) {
duke@0 408 FreeBlock *best_block = NULL;
duke@0 409 FreeBlock *best_prev = NULL;
duke@0 410 size_t best_length = 0;
duke@0 411
duke@0 412 // Search for smallest block which is bigger than length
duke@0 413 FreeBlock *prev = NULL;
duke@0 414 FreeBlock *cur = _freelist;
duke@0 415 while(cur != NULL) {
duke@0 416 size_t l = cur->length();
duke@0 417 if (l >= length && (best_block == NULL || best_length > l)) {
duke@0 418 // Remember best block, its previous element, and its length
duke@0 419 best_block = cur;
duke@0 420 best_prev = prev;
duke@0 421 best_length = best_block->length();
duke@0 422 }
duke@0 423
duke@0 424 // Next element in list
duke@0 425 prev = cur;
duke@0 426 cur = cur->link();
duke@0 427 }
duke@0 428
duke@0 429 if (best_block == NULL) {
duke@0 430 // None found
duke@0 431 return NULL;
duke@0 432 }
duke@0 433
duke@0 434 assert((best_prev == NULL && _freelist == best_block ) ||
duke@0 435 (best_prev != NULL && best_prev->link() == best_block), "sanity check");
duke@0 436
duke@0 437 // Exact (or at least good enough) fit. Remove from list.
duke@0 438 // Don't leave anything on the freelist smaller than CodeCacheMinBlockLength.
duke@0 439 if (best_length < length + CodeCacheMinBlockLength) {
duke@0 440 length = best_length;
duke@0 441 if (best_prev == NULL) {
duke@0 442 assert(_freelist == best_block, "sanity check");
duke@0 443 _freelist = _freelist->link();
duke@0 444 } else {
duke@0 445 // Unmap element
duke@0 446 best_prev->set_link(best_block->link());
duke@0 447 }
duke@0 448 } else {
duke@0 449 // Truncate block and return a pointer to the following block
duke@0 450 best_block->set_length(best_length - length);
duke@0 451 best_block = following_block(best_block);
duke@0 452 // Set used bit and length on new block
duke@0 453 size_t beg = segment_for(best_block);
duke@0 454 mark_segmap_as_used(beg, beg + length);
duke@0 455 best_block->set_length(length);
duke@0 456 }
duke@0 457
duke@0 458 best_block->set_used();
duke@0 459 _free_segments -= length;
duke@0 460 return best_block;
duke@0 461 }
duke@0 462
duke@0 463 //----------------------------------------------------------------------------
duke@0 464 // Non-product code
duke@0 465
duke@0 466 #ifndef PRODUCT
duke@0 467
duke@0 468 void CodeHeap::print() {
duke@0 469 tty->print_cr("The Heap");
duke@0 470 }
duke@0 471
duke@0 472 #endif
duke@0 473
duke@0 474 void CodeHeap::verify() {
duke@0 475 // Count the number of blocks on the freelist, and the amount of space
duke@0 476 // represented.
duke@0 477 int count = 0;
duke@0 478 size_t len = 0;
duke@0 479 for(FreeBlock* b = _freelist; b != NULL; b = b->link()) {
duke@0 480 len += b->length();
duke@0 481 count++;
duke@0 482 }
duke@0 483
duke@0 484 // Verify that freelist contains the right amount of free space
phh@1123 485 // guarantee(len == _free_segments, "wrong freelist");
duke@0 486
duke@0 487 // Verify that the number of free blocks is not out of hand.
duke@0 488 static int free_block_threshold = 10000;
duke@0 489 if (count > free_block_threshold) {
duke@0 490 warning("CodeHeap: # of free blocks > %d", free_block_threshold);
duke@0 491 // Double the warning limit
duke@0 492 free_block_threshold *= 2;
duke@0 493 }
duke@0 494
duke@0 495 // Verify that the freelist contains the same number of free blocks that is
duke@0 496 // found on the full list.
duke@0 497 for(HeapBlock *h = first_block(); h != NULL; h = next_block(h)) {
duke@0 498 if (h->free()) count--;
duke@0 499 }
phh@1123 500 // guarantee(count == 0, "missing free blocks");
duke@0 501 }