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