src/share/vm/memory/heap.cpp

Sun, 01 Apr 2012 17:04:26 -0400

author
acorn
date
Sun, 01 Apr 2012 17:04:26 -0400
changeset 3686
749b1464aa81
parent 2708
1d1603768966
child 3900
d2a62e0f25eb
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 1997, 2011, 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  */
    25 #include "precompiled.hpp"
    26 #include "memory/heap.hpp"
    27 #include "oops/oop.inline.hpp"
    28 #include "runtime/os.hpp"
    31 size_t CodeHeap::header_size() {
    32   return sizeof(HeapBlock);
    33 }
    36 // Implementation of Heap
    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 }
    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 }
    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 }
    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 }
    82 static size_t align_to_allocation_size(size_t size) {
    83   const size_t alignment = (size_t)os::vm_allocation_granularity();
    84   assert(is_power_of_2(alignment), "no kidding ???");
    85   return (size + alignment - 1) & ~(alignment - 1);
    86 }
    89 void CodeHeap::on_code_mapping(char* base, size_t size) {
    90 #ifdef LINUX
    91   extern void linux_wrap_code(char* base, size_t size);
    92   linux_wrap_code(base, size);
    93 #endif
    94 }
    97 bool CodeHeap::reserve(size_t reserved_size, size_t committed_size,
    98                        size_t segment_size) {
    99   assert(reserved_size >= committed_size, "reserved < committed");
   100   assert(segment_size >= sizeof(FreeBlock), "segment size is too small");
   101   assert(is_power_of_2(segment_size), "segment_size must be a power of 2");
   103   _segment_size      = segment_size;
   104   _log2_segment_size = exact_log2(segment_size);
   106   // Reserve and initialize space for _memory.
   107   const size_t page_size = os::can_execute_large_page_memory() ?
   108           os::page_size_for_region(committed_size, reserved_size, 8) :
   109           os::vm_page_size();
   110   const size_t granularity = os::vm_allocation_granularity();
   111   const size_t r_align = MAX2(page_size, granularity);
   112   const size_t r_size = align_size_up(reserved_size, r_align);
   113   const size_t c_size = align_size_up(committed_size, page_size);
   115   const size_t rs_align = page_size == (size_t) os::vm_page_size() ? 0 :
   116     MAX2(page_size, granularity);
   117   ReservedCodeSpace rs(r_size, rs_align, rs_align > 0);
   118   os::trace_page_sizes("code heap", committed_size, reserved_size, page_size,
   119                        rs.base(), rs.size());
   120   if (!_memory.initialize(rs, c_size)) {
   121     return false;
   122   }
   124   on_code_mapping(_memory.low(), _memory.committed_size());
   125   _number_of_committed_segments = number_of_segments(_memory.committed_size());
   126   _number_of_reserved_segments  = number_of_segments(_memory.reserved_size());
   127   assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
   129   // reserve space for _segmap
   130   if (!_segmap.initialize(align_to_page_size(_number_of_reserved_segments), align_to_page_size(_number_of_committed_segments))) {
   131     return false;
   132   }
   133   assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "could not commit  enough space for segment map");
   134   assert(_segmap.reserved_size()  >= (size_t) _number_of_reserved_segments , "could not reserve enough space for segment map");
   135   assert(_segmap.reserved_size()  >= _segmap.committed_size()     , "just checking");
   137   // initialize remaining instance variables
   138   clear();
   139   return true;
   140 }
   143 void CodeHeap::release() {
   144   Unimplemented();
   145 }
   148 bool CodeHeap::expand_by(size_t size) {
   149   // expand _memory space
   150   size_t dm = align_to_page_size(_memory.committed_size() + size) - _memory.committed_size();
   151   if (dm > 0) {
   152     char* base = _memory.low() + _memory.committed_size();
   153     if (!_memory.expand_by(dm)) return false;
   154     on_code_mapping(base, dm);
   155     size_t i = _number_of_committed_segments;
   156     _number_of_committed_segments = number_of_segments(_memory.committed_size());
   157     assert(_number_of_reserved_segments == number_of_segments(_memory.reserved_size()), "number of reserved segments should not change");
   158     assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
   159     // expand _segmap space
   160     size_t ds = align_to_page_size(_number_of_committed_segments) - _segmap.committed_size();
   161     if (ds > 0) {
   162       if (!_segmap.expand_by(ds)) return false;
   163     }
   164     assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "just checking");
   165     // initialize additional segmap entries
   166     mark_segmap_as_free(i, _number_of_committed_segments);
   167   }
   168   return true;
   169 }
   172 void CodeHeap::shrink_by(size_t size) {
   173   Unimplemented();
   174 }
   177 void CodeHeap::clear() {
   178   _next_segment = 0;
   179   mark_segmap_as_free(0, _number_of_committed_segments);
   180 }
   183 void* CodeHeap::allocate(size_t size) {
   184   size_t length = number_of_segments(size + sizeof(HeapBlock));
   185   assert(length *_segment_size >= sizeof(FreeBlock), "not enough room for FreeList");
   187   // First check if we can satify request from freelist
   188   debug_only(verify());
   189   HeapBlock* block = search_freelist(length);
   190   debug_only(if (VerifyCodeCacheOften) verify());
   191   if (block != NULL) {
   192     assert(block->length() >= length && block->length() < length + CodeCacheMinBlockLength, "sanity check");
   193     assert(!block->free(), "must be marked free");
   194 #ifdef ASSERT
   195     memset((void *)block->allocated_space(), badCodeHeapNewVal, size);
   196 #endif
   197     return block->allocated_space();
   198   }
   200   if (length < CodeCacheMinBlockLength) {
   201     length = CodeCacheMinBlockLength;
   202   }
   203   if (_next_segment + length <= _number_of_committed_segments) {
   204     mark_segmap_as_used(_next_segment, _next_segment + length);
   205     HeapBlock* b =  block_at(_next_segment);
   206     b->initialize(length);
   207     _next_segment += length;
   208 #ifdef ASSERT
   209     memset((void *)b->allocated_space(), badCodeHeapNewVal, size);
   210 #endif
   211     return b->allocated_space();
   212   } else {
   213     return NULL;
   214   }
   215 }
   218 void CodeHeap::deallocate(void* p) {
   219   assert(p == find_start(p), "illegal deallocation");
   220   // Find start of HeapBlock
   221   HeapBlock* b = (((HeapBlock *)p) - 1);
   222   assert(b->allocated_space() == p, "sanity check");
   223 #ifdef ASSERT
   224   memset((void *)b->allocated_space(),
   225          badCodeHeapFreeVal,
   226          size(b->length()) - sizeof(HeapBlock));
   227 #endif
   228   add_to_freelist(b);
   230   debug_only(if (VerifyCodeCacheOften) verify());
   231 }
   234 void* CodeHeap::find_start(void* p) const {
   235   if (!contains(p)) {
   236     return NULL;
   237   }
   238   size_t i = segment_for(p);
   239   address b = (address)_segmap.low();
   240   if (b[i] == 0xFF) {
   241     return NULL;
   242   }
   243   while (b[i] > 0) i -= (int)b[i];
   244   HeapBlock* h = block_at(i);
   245   if (h->free()) {
   246     return NULL;
   247   }
   248   return h->allocated_space();
   249 }
   252 size_t CodeHeap::alignment_unit() const {
   253   // this will be a power of two
   254   return _segment_size;
   255 }
   258 size_t CodeHeap::alignment_offset() const {
   259   // The lowest address in any allocated block will be
   260   // equal to alignment_offset (mod alignment_unit).
   261   return sizeof(HeapBlock) & (_segment_size - 1);
   262 }
   264 // Finds the next free heapblock. If the current one is free, that it returned
   265 void* CodeHeap::next_free(HeapBlock *b) const {
   266   // Since free blocks are merged, there is max. on free block
   267   // between two used ones
   268   if (b != NULL && b->free()) b = next_block(b);
   269   assert(b == NULL || !b->free(), "must be in use or at end of heap");
   270   return (b == NULL) ? NULL : b->allocated_space();
   271 }
   273 // Returns the first used HeapBlock
   274 HeapBlock* CodeHeap::first_block() const {
   275   if (_next_segment > 0)
   276     return block_at(0);
   277   return NULL;
   278 }
   280 HeapBlock *CodeHeap::block_start(void *q) const {
   281   HeapBlock* b = (HeapBlock*)find_start(q);
   282   if (b == NULL) return NULL;
   283   return b - 1;
   284 }
   286 // Returns the next Heap block an offset into one
   287 HeapBlock* CodeHeap::next_block(HeapBlock *b) const {
   288   if (b == NULL) return NULL;
   289   size_t i = segment_for(b) + b->length();
   290   if (i < _next_segment)
   291     return block_at(i);
   292   return NULL;
   293 }
   296 // Returns current capacity
   297 size_t CodeHeap::capacity() const {
   298   return _memory.committed_size();
   299 }
   301 size_t CodeHeap::max_capacity() const {
   302   return _memory.reserved_size();
   303 }
   305 size_t CodeHeap::allocated_capacity() const {
   306   // Start with the committed size in _memory;
   307   size_t l = _memory.committed_size();
   309   // Subtract the committed, but unused, segments
   310   l -= size(_number_of_committed_segments - _next_segment);
   312   // Subtract the size of the freelist
   313   l -= size(_free_segments);
   315   return l;
   316 }
   318 size_t CodeHeap::largest_free_block() const {
   319   // First check unused space excluding free blocks.
   320   size_t free_sz = size(_free_segments);
   321   size_t unused  = max_capacity() - allocated_capacity() - free_sz;
   322   if (unused >= free_sz)
   323     return unused;
   325   // Now check largest free block.
   326   size_t len = 0;
   327   for (FreeBlock* b = _freelist; b != NULL; b = b->link()) {
   328     if (b->length() > len)
   329       len = b->length();
   330   }
   331   return MAX2(unused, size(len));
   332 }
   334 // Free list management
   336 FreeBlock *CodeHeap::following_block(FreeBlock *b) {
   337   return (FreeBlock*)(((address)b) + _segment_size * b->length());
   338 }
   340 // Inserts block b after a
   341 void CodeHeap::insert_after(FreeBlock* a, FreeBlock* b) {
   342   assert(a != NULL && b != NULL, "must be real pointers");
   344   // Link b into the list after a
   345   b->set_link(a->link());
   346   a->set_link(b);
   348   // See if we can merge blocks
   349   merge_right(b); // Try to make b bigger
   350   merge_right(a); // Try to make a include b
   351 }
   353 // Try to merge this block with the following block
   354 void CodeHeap::merge_right(FreeBlock *a) {
   355   assert(a->free(), "must be a free block");
   356   if (following_block(a) == a->link()) {
   357     assert(a->link() != NULL && a->link()->free(), "must be free too");
   358     // Update block a to include the following block
   359     a->set_length(a->length() + a->link()->length());
   360     a->set_link(a->link()->link());
   361     // Update find_start map
   362     size_t beg = segment_for(a);
   363     mark_segmap_as_used(beg, beg + a->length());
   364   }
   365 }
   367 void CodeHeap::add_to_freelist(HeapBlock *a) {
   368   FreeBlock* b = (FreeBlock*)a;
   369   assert(b != _freelist, "cannot be removed twice");
   371   // Mark as free and update free space count
   372   _free_segments += b->length();
   373   b->set_free();
   375   // First element in list?
   376   if (_freelist == NULL) {
   377     _freelist = b;
   378     b->set_link(NULL);
   379     return;
   380   }
   382   // Scan for right place to put into list. List
   383   // is sorted by increasing addresseses
   384   FreeBlock* prev = NULL;
   385   FreeBlock* cur  = _freelist;
   386   while(cur != NULL && cur < b) {
   387     assert(prev == NULL || prev < cur, "must be ordered");
   388     prev = cur;
   389     cur  = cur->link();
   390   }
   392   assert( (prev == NULL && b < _freelist) ||
   393           (prev < b && (cur == NULL || b < cur)), "list must be ordered");
   395   if (prev == NULL) {
   396     // Insert first in list
   397     b->set_link(_freelist);
   398     _freelist = b;
   399     merge_right(_freelist);
   400   } else {
   401     insert_after(prev, b);
   402   }
   403 }
   405 // Search freelist for an entry on the list with the best fit
   406 // Return NULL if no one was found
   407 FreeBlock* CodeHeap::search_freelist(size_t length) {
   408   FreeBlock *best_block = NULL;
   409   FreeBlock *best_prev  = NULL;
   410   size_t best_length = 0;
   412   // Search for smallest block which is bigger than length
   413   FreeBlock *prev = NULL;
   414   FreeBlock *cur = _freelist;
   415   while(cur != NULL) {
   416     size_t l = cur->length();
   417     if (l >= length && (best_block == NULL || best_length > l)) {
   418       // Remember best block, its previous element, and its length
   419       best_block = cur;
   420       best_prev  = prev;
   421       best_length = best_block->length();
   422     }
   424     // Next element in list
   425     prev = cur;
   426     cur  = cur->link();
   427   }
   429   if (best_block == NULL) {
   430     // None found
   431     return NULL;
   432   }
   434   assert((best_prev == NULL && _freelist == best_block ) ||
   435          (best_prev != NULL && best_prev->link() == best_block), "sanity check");
   437   // Exact (or at least good enough) fit. Remove from list.
   438   // Don't leave anything on the freelist smaller than CodeCacheMinBlockLength.
   439   if (best_length < length + CodeCacheMinBlockLength) {
   440     length = best_length;
   441     if (best_prev == NULL) {
   442       assert(_freelist == best_block, "sanity check");
   443       _freelist = _freelist->link();
   444     } else {
   445       // Unmap element
   446       best_prev->set_link(best_block->link());
   447     }
   448   } else {
   449     // Truncate block and return a pointer to the following block
   450     best_block->set_length(best_length - length);
   451     best_block = following_block(best_block);
   452     // Set used bit and length on new block
   453     size_t beg = segment_for(best_block);
   454     mark_segmap_as_used(beg, beg + length);
   455     best_block->set_length(length);
   456   }
   458   best_block->set_used();
   459   _free_segments -= length;
   460   return best_block;
   461 }
   463 //----------------------------------------------------------------------------
   464 // Non-product code
   466 #ifndef PRODUCT
   468 void CodeHeap::print() {
   469   tty->print_cr("The Heap");
   470 }
   472 #endif
   474 void CodeHeap::verify() {
   475   // Count the number of blocks on the freelist, and the amount of space
   476   // represented.
   477   int count = 0;
   478   size_t len = 0;
   479   for(FreeBlock* b = _freelist; b != NULL; b = b->link()) {
   480     len += b->length();
   481     count++;
   482   }
   484   // Verify that freelist contains the right amount of free space
   485   //  guarantee(len == _free_segments, "wrong freelist");
   487   // Verify that the number of free blocks is not out of hand.
   488   static int free_block_threshold = 10000;
   489   if (count > free_block_threshold) {
   490     warning("CodeHeap: # of free blocks > %d", free_block_threshold);
   491     // Double the warning limit
   492     free_block_threshold *= 2;
   493   }
   495   // Verify that the freelist contains the same number of free blocks that is
   496   // found on the full list.
   497   for(HeapBlock *h = first_block(); h != NULL; h = next_block(h)) {
   498     if (h->free()) count--;
   499   }
   500   //  guarantee(count == 0, "missing free blocks");
   501 }

mercurial