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