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