src/share/vm/asm/codeBuffer.cpp

Wed, 27 Aug 2014 08:19:12 -0400

author
zgu
date
Wed, 27 Aug 2014 08:19:12 -0400
changeset 7074
833b0f92429a
parent 6680
78bbf4d43a14
child 7149
094cbdffa87d
permissions
-rw-r--r--

8046598: Scalable Native memory tracking development
Summary: Enhance scalability of native memory tracking
Reviewed-by: coleenp, ctornqvi, gtriantafill

     1 /*
     2  * Copyright (c) 1997, 2014, 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 "asm/codeBuffer.hpp"
    27 #include "compiler/disassembler.hpp"
    28 #include "memory/gcLocker.hpp"
    29 #include "oops/methodData.hpp"
    30 #include "oops/oop.inline.hpp"
    31 #include "utilities/copy.hpp"
    32 #include "utilities/xmlstream.hpp"
    34 // The structure of a CodeSection:
    35 //
    36 //    _start ->           +----------------+
    37 //                        | machine code...|
    38 //    _end ->             |----------------|
    39 //                        |                |
    40 //                        |    (empty)     |
    41 //                        |                |
    42 //                        |                |
    43 //                        +----------------+
    44 //    _limit ->           |                |
    45 //
    46 //    _locs_start ->      +----------------+
    47 //                        |reloc records...|
    48 //                        |----------------|
    49 //    _locs_end ->        |                |
    50 //                        |                |
    51 //                        |    (empty)     |
    52 //                        |                |
    53 //                        |                |
    54 //                        +----------------+
    55 //    _locs_limit ->      |                |
    56 // The _end (resp. _limit) pointer refers to the first
    57 // unused (resp. unallocated) byte.
    59 // The structure of the CodeBuffer while code is being accumulated:
    60 //
    61 //    _total_start ->    \
    62 //    _insts._start ->              +----------------+
    63 //                                  |                |
    64 //                                  |     Code       |
    65 //                                  |                |
    66 //    _stubs._start ->              |----------------|
    67 //                                  |                |
    68 //                                  |    Stubs       | (also handlers for deopt/exception)
    69 //                                  |                |
    70 //    _consts._start ->             |----------------|
    71 //                                  |                |
    72 //                                  |   Constants    |
    73 //                                  |                |
    74 //                                  +----------------+
    75 //    + _total_size ->              |                |
    76 //
    77 // When the code and relocations are copied to the code cache,
    78 // the empty parts of each section are removed, and everything
    79 // is copied into contiguous locations.
    81 typedef CodeBuffer::csize_t csize_t;  // file-local definition
    83 // External buffer, in a predefined CodeBlob.
    84 // Important: The code_start must be taken exactly, and not realigned.
    85 CodeBuffer::CodeBuffer(CodeBlob* blob) {
    86   initialize_misc("static buffer");
    87   initialize(blob->content_begin(), blob->content_size());
    88   verify_section_allocation();
    89 }
    91 void CodeBuffer::initialize(csize_t code_size, csize_t locs_size) {
    92   // Compute maximal alignment.
    93   int align = _insts.alignment();
    94   // Always allow for empty slop around each section.
    95   int slop = (int) CodeSection::end_slop();
    97   assert(blob() == NULL, "only once");
    98   set_blob(BufferBlob::create(_name, code_size + (align+slop) * (SECT_LIMIT+1)));
    99   if (blob() == NULL) {
   100     // The assembler constructor will throw a fatal on an empty CodeBuffer.
   101     return;  // caller must test this
   102   }
   104   // Set up various pointers into the blob.
   105   initialize(_total_start, _total_size);
   107   assert((uintptr_t)insts_begin() % CodeEntryAlignment == 0, "instruction start not code entry aligned");
   109   pd_initialize();
   111   if (locs_size != 0) {
   112     _insts.initialize_locs(locs_size / sizeof(relocInfo));
   113   }
   115   verify_section_allocation();
   116 }
   119 CodeBuffer::~CodeBuffer() {
   120   verify_section_allocation();
   122   // If we allocate our code buffer from the CodeCache
   123   // via a BufferBlob, and it's not permanent, then
   124   // free the BufferBlob.
   125   // The rest of the memory will be freed when the ResourceObj
   126   // is released.
   127   for (CodeBuffer* cb = this; cb != NULL; cb = cb->before_expand()) {
   128     // Previous incarnations of this buffer are held live, so that internal
   129     // addresses constructed before expansions will not be confused.
   130     cb->free_blob();
   131   }
   133   // free any overflow storage
   134   delete _overflow_arena;
   136 #ifdef ASSERT
   137   // Save allocation type to execute assert in ~ResourceObj()
   138   // which is called after this destructor.
   139   assert(_default_oop_recorder.allocated_on_stack(), "should be embedded object");
   140   ResourceObj::allocation_type at = _default_oop_recorder.get_allocation_type();
   141   Copy::fill_to_bytes(this, sizeof(*this), badResourceValue);
   142   ResourceObj::set_allocation_type((address)(&_default_oop_recorder), at);
   143 #endif
   144 }
   146 void CodeBuffer::initialize_oop_recorder(OopRecorder* r) {
   147   assert(_oop_recorder == &_default_oop_recorder && _default_oop_recorder.is_unused(), "do this once");
   148   DEBUG_ONLY(_default_oop_recorder.freeze());  // force unused OR to be frozen
   149   _oop_recorder = r;
   150 }
   152 void CodeBuffer::initialize_section_size(CodeSection* cs, csize_t size) {
   153   assert(cs != &_insts, "insts is the memory provider, not the consumer");
   154   csize_t slop = CodeSection::end_slop();  // margin between sections
   155   int align = cs->alignment();
   156   assert(is_power_of_2(align), "sanity");
   157   address start  = _insts._start;
   158   address limit  = _insts._limit;
   159   address middle = limit - size;
   160   middle -= (intptr_t)middle & (align-1);  // align the division point downward
   161   guarantee(middle - slop > start, "need enough space to divide up");
   162   _insts._limit = middle - slop;  // subtract desired space, plus slop
   163   cs->initialize(middle, limit - middle);
   164   assert(cs->start() == middle, "sanity");
   165   assert(cs->limit() == limit,  "sanity");
   166   // give it some relocations to start with, if the main section has them
   167   if (_insts.has_locs())  cs->initialize_locs(1);
   168 }
   170 void CodeBuffer::freeze_section(CodeSection* cs) {
   171   CodeSection* next_cs = (cs == consts())? NULL: code_section(cs->index()+1);
   172   csize_t frozen_size = cs->size();
   173   if (next_cs != NULL) {
   174     frozen_size = next_cs->align_at_start(frozen_size);
   175   }
   176   address old_limit = cs->limit();
   177   address new_limit = cs->start() + frozen_size;
   178   relocInfo* old_locs_limit = cs->locs_limit();
   179   relocInfo* new_locs_limit = cs->locs_end();
   180   // Patch the limits.
   181   cs->_limit = new_limit;
   182   cs->_locs_limit = new_locs_limit;
   183   cs->_frozen = true;
   184   if (!next_cs->is_allocated() && !next_cs->is_frozen()) {
   185     // Give remaining buffer space to the following section.
   186     next_cs->initialize(new_limit, old_limit - new_limit);
   187     next_cs->initialize_shared_locs(new_locs_limit,
   188                                     old_locs_limit - new_locs_limit);
   189   }
   190 }
   192 void CodeBuffer::set_blob(BufferBlob* blob) {
   193   _blob = blob;
   194   if (blob != NULL) {
   195     address start = blob->content_begin();
   196     address end   = blob->content_end();
   197     // Round up the starting address.
   198     int align = _insts.alignment();
   199     start += (-(intptr_t)start) & (align-1);
   200     _total_start = start;
   201     _total_size  = end - start;
   202   } else {
   203 #ifdef ASSERT
   204     // Clean out dangling pointers.
   205     _total_start    = badAddress;
   206     _consts._start  = _consts._end  = badAddress;
   207     _insts._start   = _insts._end   = badAddress;
   208     _stubs._start   = _stubs._end   = badAddress;
   209 #endif //ASSERT
   210   }
   211 }
   213 void CodeBuffer::free_blob() {
   214   if (_blob != NULL) {
   215     BufferBlob::free(_blob);
   216     set_blob(NULL);
   217   }
   218 }
   220 const char* CodeBuffer::code_section_name(int n) {
   221 #ifdef PRODUCT
   222   return NULL;
   223 #else //PRODUCT
   224   switch (n) {
   225   case SECT_CONSTS:            return "consts";
   226   case SECT_INSTS:             return "insts";
   227   case SECT_STUBS:             return "stubs";
   228   default:                     return NULL;
   229   }
   230 #endif //PRODUCT
   231 }
   233 int CodeBuffer::section_index_of(address addr) const {
   234   for (int n = 0; n < (int)SECT_LIMIT; n++) {
   235     const CodeSection* cs = code_section(n);
   236     if (cs->allocates(addr))  return n;
   237   }
   238   return SECT_NONE;
   239 }
   241 int CodeBuffer::locator(address addr) const {
   242   for (int n = 0; n < (int)SECT_LIMIT; n++) {
   243     const CodeSection* cs = code_section(n);
   244     if (cs->allocates(addr)) {
   245       return locator(addr - cs->start(), n);
   246     }
   247   }
   248   return -1;
   249 }
   251 address CodeBuffer::locator_address(int locator) const {
   252   if (locator < 0)  return NULL;
   253   address start = code_section(locator_sect(locator))->start();
   254   return start + locator_pos(locator);
   255 }
   257 bool CodeBuffer::is_backward_branch(Label& L) {
   258   return L.is_bound() && insts_end() <= locator_address(L.loc());
   259 }
   261 address CodeBuffer::decode_begin() {
   262   address begin = _insts.start();
   263   if (_decode_begin != NULL && _decode_begin > begin)
   264     begin = _decode_begin;
   265   return begin;
   266 }
   269 GrowableArray<int>* CodeBuffer::create_patch_overflow() {
   270   if (_overflow_arena == NULL) {
   271     _overflow_arena = new (mtCode) Arena(mtCode);
   272   }
   273   return new (_overflow_arena) GrowableArray<int>(_overflow_arena, 8, 0, 0);
   274 }
   277 // Helper function for managing labels and their target addresses.
   278 // Returns a sensible address, and if it is not the label's final
   279 // address, notes the dependency (at 'branch_pc') on the label.
   280 address CodeSection::target(Label& L, address branch_pc) {
   281   if (L.is_bound()) {
   282     int loc = L.loc();
   283     if (index() == CodeBuffer::locator_sect(loc)) {
   284       return start() + CodeBuffer::locator_pos(loc);
   285     } else {
   286       return outer()->locator_address(loc);
   287     }
   288   } else {
   289     assert(allocates2(branch_pc), "sanity");
   290     address base = start();
   291     int patch_loc = CodeBuffer::locator(branch_pc - base, index());
   292     L.add_patch_at(outer(), patch_loc);
   294     // Need to return a pc, doesn't matter what it is since it will be
   295     // replaced during resolution later.
   296     // Don't return NULL or badAddress, since branches shouldn't overflow.
   297     // Don't return base either because that could overflow displacements
   298     // for shorter branches.  It will get checked when bound.
   299     return branch_pc;
   300   }
   301 }
   303 void CodeSection::relocate(address at, RelocationHolder const& spec, int format) {
   304   Relocation* reloc = spec.reloc();
   305   relocInfo::relocType rtype = (relocInfo::relocType) reloc->type();
   306   if (rtype == relocInfo::none)  return;
   308   // The assertion below has been adjusted, to also work for
   309   // relocation for fixup.  Sometimes we want to put relocation
   310   // information for the next instruction, since it will be patched
   311   // with a call.
   312   assert(start() <= at && at <= end()+1,
   313          "cannot relocate data outside code boundaries");
   315   if (!has_locs()) {
   316     // no space for relocation information provided => code cannot be
   317     // relocated.  Make sure that relocate is only called with rtypes
   318     // that can be ignored for this kind of code.
   319     assert(rtype == relocInfo::none              ||
   320            rtype == relocInfo::runtime_call_type ||
   321            rtype == relocInfo::internal_word_type||
   322            rtype == relocInfo::section_word_type ||
   323            rtype == relocInfo::external_word_type,
   324            "code needs relocation information");
   325     // leave behind an indication that we attempted a relocation
   326     DEBUG_ONLY(_locs_start = _locs_limit = (relocInfo*)badAddress);
   327     return;
   328   }
   330   // Advance the point, noting the offset we'll have to record.
   331   csize_t offset = at - locs_point();
   332   set_locs_point(at);
   334   // Test for a couple of overflow conditions; maybe expand the buffer.
   335   relocInfo* end = locs_end();
   336   relocInfo* req = end + relocInfo::length_limit;
   337   // Check for (potential) overflow
   338   if (req >= locs_limit() || offset >= relocInfo::offset_limit()) {
   339     req += (uint)offset / (uint)relocInfo::offset_limit();
   340     if (req >= locs_limit()) {
   341       // Allocate or reallocate.
   342       expand_locs(locs_count() + (req - end));
   343       // reload pointer
   344       end = locs_end();
   345     }
   346   }
   348   // If the offset is giant, emit filler relocs, of type 'none', but
   349   // each carrying the largest possible offset, to advance the locs_point.
   350   while (offset >= relocInfo::offset_limit()) {
   351     assert(end < locs_limit(), "adjust previous paragraph of code");
   352     *end++ = filler_relocInfo();
   353     offset -= filler_relocInfo().addr_offset();
   354   }
   356   // If it's a simple reloc with no data, we'll just write (rtype | offset).
   357   (*end) = relocInfo(rtype, offset, format);
   359   // If it has data, insert the prefix, as (data_prefix_tag | data1), data2.
   360   end->initialize(this, reloc);
   361 }
   363 void CodeSection::initialize_locs(int locs_capacity) {
   364   assert(_locs_start == NULL, "only one locs init step, please");
   365   // Apply a priori lower limits to relocation size:
   366   csize_t min_locs = MAX2(size() / 16, (csize_t)4);
   367   if (locs_capacity < min_locs)  locs_capacity = min_locs;
   368   relocInfo* locs_start = NEW_RESOURCE_ARRAY(relocInfo, locs_capacity);
   369   _locs_start    = locs_start;
   370   _locs_end      = locs_start;
   371   _locs_limit    = locs_start + locs_capacity;
   372   _locs_own      = true;
   373 }
   375 void CodeSection::initialize_shared_locs(relocInfo* buf, int length) {
   376   assert(_locs_start == NULL, "do this before locs are allocated");
   377   // Internal invariant:  locs buf must be fully aligned.
   378   // See copy_relocations_to() below.
   379   while ((uintptr_t)buf % HeapWordSize != 0 && length > 0) {
   380     ++buf; --length;
   381   }
   382   if (length > 0) {
   383     _locs_start = buf;
   384     _locs_end   = buf;
   385     _locs_limit = buf + length;
   386     _locs_own   = false;
   387   }
   388 }
   390 void CodeSection::initialize_locs_from(const CodeSection* source_cs) {
   391   int lcount = source_cs->locs_count();
   392   if (lcount != 0) {
   393     initialize_shared_locs(source_cs->locs_start(), lcount);
   394     _locs_end = _locs_limit = _locs_start + lcount;
   395     assert(is_allocated(), "must have copied code already");
   396     set_locs_point(start() + source_cs->locs_point_off());
   397   }
   398   assert(this->locs_count() == source_cs->locs_count(), "sanity");
   399 }
   401 void CodeSection::expand_locs(int new_capacity) {
   402   if (_locs_start == NULL) {
   403     initialize_locs(new_capacity);
   404     return;
   405   } else {
   406     int old_count    = locs_count();
   407     int old_capacity = locs_capacity();
   408     if (new_capacity < old_capacity * 2)
   409       new_capacity = old_capacity * 2;
   410     relocInfo* locs_start;
   411     if (_locs_own) {
   412       locs_start = REALLOC_RESOURCE_ARRAY(relocInfo, _locs_start, old_capacity, new_capacity);
   413     } else {
   414       locs_start = NEW_RESOURCE_ARRAY(relocInfo, new_capacity);
   415       Copy::conjoint_jbytes(_locs_start, locs_start, old_capacity * sizeof(relocInfo));
   416       _locs_own = true;
   417     }
   418     _locs_start    = locs_start;
   419     _locs_end      = locs_start + old_count;
   420     _locs_limit    = locs_start + new_capacity;
   421   }
   422 }
   425 /// Support for emitting the code to its final location.
   426 /// The pattern is the same for all functions.
   427 /// We iterate over all the sections, padding each to alignment.
   429 csize_t CodeBuffer::total_content_size() const {
   430   csize_t size_so_far = 0;
   431   for (int n = 0; n < (int)SECT_LIMIT; n++) {
   432     const CodeSection* cs = code_section(n);
   433     if (cs->is_empty())  continue;  // skip trivial section
   434     size_so_far = cs->align_at_start(size_so_far);
   435     size_so_far += cs->size();
   436   }
   437   return size_so_far;
   438 }
   440 void CodeBuffer::compute_final_layout(CodeBuffer* dest) const {
   441   address buf = dest->_total_start;
   442   csize_t buf_offset = 0;
   443   assert(dest->_total_size >= total_content_size(), "must be big enough");
   445   {
   446     // not sure why this is here, but why not...
   447     int alignSize = MAX2((intx) sizeof(jdouble), CodeEntryAlignment);
   448     assert( (dest->_total_start - _insts.start()) % alignSize == 0, "copy must preserve alignment");
   449   }
   451   const CodeSection* prev_cs      = NULL;
   452   CodeSection*       prev_dest_cs = NULL;
   454   for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
   455     // figure compact layout of each section
   456     const CodeSection* cs = code_section(n);
   457     csize_t csize = cs->size();
   459     CodeSection* dest_cs = dest->code_section(n);
   460     if (!cs->is_empty()) {
   461       // Compute initial padding; assign it to the previous non-empty guy.
   462       // Cf. figure_expanded_capacities.
   463       csize_t padding = cs->align_at_start(buf_offset) - buf_offset;
   464       if (padding != 0) {
   465         buf_offset += padding;
   466         assert(prev_dest_cs != NULL, "sanity");
   467         prev_dest_cs->_limit += padding;
   468       }
   469       #ifdef ASSERT
   470       if (prev_cs != NULL && prev_cs->is_frozen() && n < (SECT_LIMIT - 1)) {
   471         // Make sure the ends still match up.
   472         // This is important because a branch in a frozen section
   473         // might target code in a following section, via a Label,
   474         // and without a relocation record.  See Label::patch_instructions.
   475         address dest_start = buf+buf_offset;
   476         csize_t start2start = cs->start() - prev_cs->start();
   477         csize_t dest_start2start = dest_start - prev_dest_cs->start();
   478         assert(start2start == dest_start2start, "cannot stretch frozen sect");
   479       }
   480       #endif //ASSERT
   481       prev_dest_cs = dest_cs;
   482       prev_cs      = cs;
   483     }
   485     debug_only(dest_cs->_start = NULL);  // defeat double-initialization assert
   486     dest_cs->initialize(buf+buf_offset, csize);
   487     dest_cs->set_end(buf+buf_offset+csize);
   488     assert(dest_cs->is_allocated(), "must always be allocated");
   489     assert(cs->is_empty() == dest_cs->is_empty(), "sanity");
   491     buf_offset += csize;
   492   }
   494   // Done calculating sections; did it come out to the right end?
   495   assert(buf_offset == total_content_size(), "sanity");
   496   dest->verify_section_allocation();
   497 }
   499 // Append an oop reference that keeps the class alive.
   500 static void append_oop_references(GrowableArray<oop>* oops, Klass* k) {
   501   oop cl = k->klass_holder();
   502   if (cl != NULL && !oops->contains(cl)) {
   503     oops->append(cl);
   504   }
   505 }
   507 void CodeBuffer::finalize_oop_references(methodHandle mh) {
   508   No_Safepoint_Verifier nsv;
   510   GrowableArray<oop> oops;
   512   // Make sure that immediate metadata records something in the OopRecorder
   513   for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
   514     // pull code out of each section
   515     CodeSection* cs = code_section(n);
   516     if (cs->is_empty())  continue;  // skip trivial section
   517     RelocIterator iter(cs);
   518     while (iter.next()) {
   519       if (iter.type() == relocInfo::metadata_type) {
   520         metadata_Relocation* md = iter.metadata_reloc();
   521         if (md->metadata_is_immediate()) {
   522           Metadata* m = md->metadata_value();
   523           if (oop_recorder()->is_real(m)) {
   524             if (m->is_methodData()) {
   525               m = ((MethodData*)m)->method();
   526             }
   527             if (m->is_method()) {
   528               m = ((Method*)m)->method_holder();
   529             }
   530             if (m->is_klass()) {
   531               append_oop_references(&oops, (Klass*)m);
   532             } else {
   533               // XXX This will currently occur for MDO which don't
   534               // have a backpointer.  This has to be fixed later.
   535               m->print();
   536               ShouldNotReachHere();
   537             }
   538           }
   539         }
   540       }
   541     }
   542   }
   544   if (!oop_recorder()->is_unused()) {
   545     for (int i = 0; i < oop_recorder()->metadata_count(); i++) {
   546       Metadata* m = oop_recorder()->metadata_at(i);
   547       if (oop_recorder()->is_real(m)) {
   548         if (m->is_methodData()) {
   549           m = ((MethodData*)m)->method();
   550         }
   551         if (m->is_method()) {
   552           m = ((Method*)m)->method_holder();
   553         }
   554         if (m->is_klass()) {
   555           append_oop_references(&oops, (Klass*)m);
   556         } else {
   557           m->print();
   558           ShouldNotReachHere();
   559         }
   560       }
   561     }
   563   }
   565   // Add the class loader of Method* for the nmethod itself
   566   append_oop_references(&oops, mh->method_holder());
   568   // Add any oops that we've found
   569   Thread* thread = Thread::current();
   570   for (int i = 0; i < oops.length(); i++) {
   571     oop_recorder()->find_index((jobject)thread->handle_area()->allocate_handle(oops.at(i)));
   572   }
   573 }
   577 csize_t CodeBuffer::total_offset_of(CodeSection* cs) const {
   578   csize_t size_so_far = 0;
   579   for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
   580     const CodeSection* cur_cs = code_section(n);
   581     if (!cur_cs->is_empty()) {
   582       size_so_far = cur_cs->align_at_start(size_so_far);
   583     }
   584     if (cur_cs->index() == cs->index()) {
   585       return size_so_far;
   586     }
   587     size_so_far += cur_cs->size();
   588   }
   589   ShouldNotReachHere();
   590   return -1;
   591 }
   593 csize_t CodeBuffer::total_relocation_size() const {
   594   csize_t lsize = copy_relocations_to(NULL);  // dry run only
   595   csize_t csize = total_content_size();
   596   csize_t total = RelocIterator::locs_and_index_size(csize, lsize);
   597   return (csize_t) align_size_up(total, HeapWordSize);
   598 }
   600 csize_t CodeBuffer::copy_relocations_to(CodeBlob* dest) const {
   601   address buf = NULL;
   602   csize_t buf_offset = 0;
   603   csize_t buf_limit = 0;
   604   if (dest != NULL) {
   605     buf = (address)dest->relocation_begin();
   606     buf_limit = (address)dest->relocation_end() - buf;
   607     assert((uintptr_t)buf % HeapWordSize == 0, "buf must be fully aligned");
   608     assert(buf_limit % HeapWordSize == 0, "buf must be evenly sized");
   609   }
   610   // if dest == NULL, this is just the sizing pass
   612   csize_t code_end_so_far = 0;
   613   csize_t code_point_so_far = 0;
   614   for (int n = (int) SECT_FIRST; n < (int)SECT_LIMIT; n++) {
   615     // pull relocs out of each section
   616     const CodeSection* cs = code_section(n);
   617     assert(!(cs->is_empty() && cs->locs_count() > 0), "sanity");
   618     if (cs->is_empty())  continue;  // skip trivial section
   619     relocInfo* lstart = cs->locs_start();
   620     relocInfo* lend   = cs->locs_end();
   621     csize_t    lsize  = (csize_t)( (address)lend - (address)lstart );
   622     csize_t    csize  = cs->size();
   623     code_end_so_far = cs->align_at_start(code_end_so_far);
   625     if (lsize > 0) {
   626       // Figure out how to advance the combined relocation point
   627       // first to the beginning of this section.
   628       // We'll insert one or more filler relocs to span that gap.
   629       // (Don't bother to improve this by editing the first reloc's offset.)
   630       csize_t new_code_point = code_end_so_far;
   631       for (csize_t jump;
   632            code_point_so_far < new_code_point;
   633            code_point_so_far += jump) {
   634         jump = new_code_point - code_point_so_far;
   635         relocInfo filler = filler_relocInfo();
   636         if (jump >= filler.addr_offset()) {
   637           jump = filler.addr_offset();
   638         } else {  // else shrink the filler to fit
   639           filler = relocInfo(relocInfo::none, jump);
   640         }
   641         if (buf != NULL) {
   642           assert(buf_offset + (csize_t)sizeof(filler) <= buf_limit, "filler in bounds");
   643           *(relocInfo*)(buf+buf_offset) = filler;
   644         }
   645         buf_offset += sizeof(filler);
   646       }
   648       // Update code point and end to skip past this section:
   649       csize_t last_code_point = code_end_so_far + cs->locs_point_off();
   650       assert(code_point_so_far <= last_code_point, "sanity");
   651       code_point_so_far = last_code_point; // advance past this guy's relocs
   652     }
   653     code_end_so_far += csize;  // advance past this guy's instructions too
   655     // Done with filler; emit the real relocations:
   656     if (buf != NULL && lsize != 0) {
   657       assert(buf_offset + lsize <= buf_limit, "target in bounds");
   658       assert((uintptr_t)lstart % HeapWordSize == 0, "sane start");
   659       if (buf_offset % HeapWordSize == 0) {
   660         // Use wordwise copies if possible:
   661         Copy::disjoint_words((HeapWord*)lstart,
   662                              (HeapWord*)(buf+buf_offset),
   663                              (lsize + HeapWordSize-1) / HeapWordSize);
   664       } else {
   665         Copy::conjoint_jbytes(lstart, buf+buf_offset, lsize);
   666       }
   667     }
   668     buf_offset += lsize;
   669   }
   671   // Align end of relocation info in target.
   672   while (buf_offset % HeapWordSize != 0) {
   673     if (buf != NULL) {
   674       relocInfo padding = relocInfo(relocInfo::none, 0);
   675       assert(buf_offset + (csize_t)sizeof(padding) <= buf_limit, "padding in bounds");
   676       *(relocInfo*)(buf+buf_offset) = padding;
   677     }
   678     buf_offset += sizeof(relocInfo);
   679   }
   681   assert(code_end_so_far == total_content_size(), "sanity");
   683   // Account for index:
   684   if (buf != NULL) {
   685     RelocIterator::create_index(dest->relocation_begin(),
   686                                 buf_offset / sizeof(relocInfo),
   687                                 dest->relocation_end());
   688   }
   690   return buf_offset;
   691 }
   693 void CodeBuffer::copy_code_to(CodeBlob* dest_blob) {
   694 #ifndef PRODUCT
   695   if (PrintNMethods && (WizardMode || Verbose)) {
   696     tty->print("done with CodeBuffer:");
   697     ((CodeBuffer*)this)->print();
   698   }
   699 #endif //PRODUCT
   701   CodeBuffer dest(dest_blob);
   702   assert(dest_blob->content_size() >= total_content_size(), "good sizing");
   703   this->compute_final_layout(&dest);
   704   relocate_code_to(&dest);
   706   // transfer strings and comments from buffer to blob
   707   dest_blob->set_strings(_strings);
   709   // Done moving code bytes; were they the right size?
   710   assert(round_to(dest.total_content_size(), oopSize) == dest_blob->content_size(), "sanity");
   712   // Flush generated code
   713   ICache::invalidate_range(dest_blob->code_begin(), dest_blob->code_size());
   714 }
   716 // Move all my code into another code buffer.  Consult applicable
   717 // relocs to repair embedded addresses.  The layout in the destination
   718 // CodeBuffer is different to the source CodeBuffer: the destination
   719 // CodeBuffer gets the final layout (consts, insts, stubs in order of
   720 // ascending address).
   721 void CodeBuffer::relocate_code_to(CodeBuffer* dest) const {
   722   address dest_end = dest->_total_start + dest->_total_size;
   723   address dest_filled = NULL;
   724   for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
   725     // pull code out of each section
   726     const CodeSection* cs = code_section(n);
   727     if (cs->is_empty())  continue;  // skip trivial section
   728     CodeSection* dest_cs = dest->code_section(n);
   729     assert(cs->size() == dest_cs->size(), "sanity");
   730     csize_t usize = dest_cs->size();
   731     csize_t wsize = align_size_up(usize, HeapWordSize);
   732     assert(dest_cs->start() + wsize <= dest_end, "no overflow");
   733     // Copy the code as aligned machine words.
   734     // This may also include an uninitialized partial word at the end.
   735     Copy::disjoint_words((HeapWord*)cs->start(),
   736                          (HeapWord*)dest_cs->start(),
   737                          wsize / HeapWordSize);
   739     if (dest->blob() == NULL) {
   740       // Destination is a final resting place, not just another buffer.
   741       // Normalize uninitialized bytes in the final padding.
   742       Copy::fill_to_bytes(dest_cs->end(), dest_cs->remaining(),
   743                           Assembler::code_fill_byte());
   744     }
   745     // Keep track of the highest filled address
   746     dest_filled = MAX2(dest_filled, dest_cs->end() + dest_cs->remaining());
   748     assert(cs->locs_start() != (relocInfo*)badAddress,
   749            "this section carries no reloc storage, but reloc was attempted");
   751     // Make the new code copy use the old copy's relocations:
   752     dest_cs->initialize_locs_from(cs);
   753   }
   755   // Do relocation after all sections are copied.
   756   // This is necessary if the code uses constants in stubs, which are
   757   // relocated when the corresponding instruction in the code (e.g., a
   758   // call) is relocated. Stubs are placed behind the main code
   759   // section, so that section has to be copied before relocating.
   760   for (int n = (int) SECT_FIRST; n < (int)SECT_LIMIT; n++) {
   761     // pull code out of each section
   762     const CodeSection* cs = code_section(n);
   763     if (cs->is_empty()) continue;  // skip trivial section
   764     CodeSection* dest_cs = dest->code_section(n);
   765     { // Repair the pc relative information in the code after the move
   766       RelocIterator iter(dest_cs);
   767       while (iter.next()) {
   768         iter.reloc()->fix_relocation_after_move(this, dest);
   769       }
   770     }
   771   }
   773   if (dest->blob() == NULL && dest_filled != NULL) {
   774     // Destination is a final resting place, not just another buffer.
   775     // Normalize uninitialized bytes in the final padding.
   776     Copy::fill_to_bytes(dest_filled, dest_end - dest_filled,
   777                         Assembler::code_fill_byte());
   779   }
   780 }
   782 csize_t CodeBuffer::figure_expanded_capacities(CodeSection* which_cs,
   783                                                csize_t amount,
   784                                                csize_t* new_capacity) {
   785   csize_t new_total_cap = 0;
   787   for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
   788     const CodeSection* sect = code_section(n);
   790     if (!sect->is_empty()) {
   791       // Compute initial padding; assign it to the previous section,
   792       // even if it's empty (e.g. consts section can be empty).
   793       // Cf. compute_final_layout
   794       csize_t padding = sect->align_at_start(new_total_cap) - new_total_cap;
   795       if (padding != 0) {
   796         new_total_cap += padding;
   797         assert(n - 1 >= SECT_FIRST, "sanity");
   798         new_capacity[n - 1] += padding;
   799       }
   800     }
   802     csize_t exp = sect->size();  // 100% increase
   803     if ((uint)exp < 4*K)  exp = 4*K;       // minimum initial increase
   804     if (sect == which_cs) {
   805       if (exp < amount)  exp = amount;
   806       if (StressCodeBuffers)  exp = amount;  // expand only slightly
   807     } else if (n == SECT_INSTS) {
   808       // scale down inst increases to a more modest 25%
   809       exp = 4*K + ((exp - 4*K) >> 2);
   810       if (StressCodeBuffers)  exp = amount / 2;  // expand only slightly
   811     } else if (sect->is_empty()) {
   812       // do not grow an empty secondary section
   813       exp = 0;
   814     }
   815     // Allow for inter-section slop:
   816     exp += CodeSection::end_slop();
   817     csize_t new_cap = sect->size() + exp;
   818     if (new_cap < sect->capacity()) {
   819       // No need to expand after all.
   820       new_cap = sect->capacity();
   821     }
   822     new_capacity[n] = new_cap;
   823     new_total_cap += new_cap;
   824   }
   826   return new_total_cap;
   827 }
   829 void CodeBuffer::expand(CodeSection* which_cs, csize_t amount) {
   830 #ifndef PRODUCT
   831   if (PrintNMethods && (WizardMode || Verbose)) {
   832     tty->print("expanding CodeBuffer:");
   833     this->print();
   834   }
   836   if (StressCodeBuffers && blob() != NULL) {
   837     static int expand_count = 0;
   838     if (expand_count >= 0)  expand_count += 1;
   839     if (expand_count > 100 && is_power_of_2(expand_count)) {
   840       tty->print_cr("StressCodeBuffers: have expanded %d times", expand_count);
   841       // simulate an occasional allocation failure:
   842       free_blob();
   843     }
   844   }
   845 #endif //PRODUCT
   847   // Resizing must be allowed
   848   {
   849     if (blob() == NULL)  return;  // caller must check for blob == NULL
   850     for (int n = 0; n < (int)SECT_LIMIT; n++) {
   851       guarantee(!code_section(n)->is_frozen(), "resizing not allowed when frozen");
   852     }
   853   }
   855   // Figure new capacity for each section.
   856   csize_t new_capacity[SECT_LIMIT];
   857   csize_t new_total_cap
   858     = figure_expanded_capacities(which_cs, amount, new_capacity);
   860   // Create a new (temporary) code buffer to hold all the new data
   861   CodeBuffer cb(name(), new_total_cap, 0);
   862   if (cb.blob() == NULL) {
   863     // Failed to allocate in code cache.
   864     free_blob();
   865     return;
   866   }
   868   // Create an old code buffer to remember which addresses used to go where.
   869   // This will be useful when we do final assembly into the code cache,
   870   // because we will need to know how to warp any internal address that
   871   // has been created at any time in this CodeBuffer's past.
   872   CodeBuffer* bxp = new CodeBuffer(_total_start, _total_size);
   873   bxp->take_over_code_from(this);  // remember the old undersized blob
   874   DEBUG_ONLY(this->_blob = NULL);  // silence a later assert
   875   bxp->_before_expand = this->_before_expand;
   876   this->_before_expand = bxp;
   878   // Give each section its required (expanded) capacity.
   879   for (int n = (int)SECT_LIMIT-1; n >= SECT_FIRST; n--) {
   880     CodeSection* cb_sect   = cb.code_section(n);
   881     CodeSection* this_sect = code_section(n);
   882     if (new_capacity[n] == 0)  continue;  // already nulled out
   883     if (n != SECT_INSTS) {
   884       cb.initialize_section_size(cb_sect, new_capacity[n]);
   885     }
   886     assert(cb_sect->capacity() >= new_capacity[n], "big enough");
   887     address cb_start = cb_sect->start();
   888     cb_sect->set_end(cb_start + this_sect->size());
   889     if (this_sect->mark() == NULL) {
   890       cb_sect->clear_mark();
   891     } else {
   892       cb_sect->set_mark(cb_start + this_sect->mark_off());
   893     }
   894   }
   896   // Move all the code and relocations to the new blob:
   897   relocate_code_to(&cb);
   899   // Copy the temporary code buffer into the current code buffer.
   900   // Basically, do {*this = cb}, except for some control information.
   901   this->take_over_code_from(&cb);
   902   cb.set_blob(NULL);
   904   // Zap the old code buffer contents, to avoid mistakenly using them.
   905   debug_only(Copy::fill_to_bytes(bxp->_total_start, bxp->_total_size,
   906                                  badCodeHeapFreeVal));
   908   _decode_begin = NULL;  // sanity
   910   // Make certain that the new sections are all snugly inside the new blob.
   911   verify_section_allocation();
   913 #ifndef PRODUCT
   914   if (PrintNMethods && (WizardMode || Verbose)) {
   915     tty->print("expanded CodeBuffer:");
   916     this->print();
   917   }
   918 #endif //PRODUCT
   919 }
   921 void CodeBuffer::take_over_code_from(CodeBuffer* cb) {
   922   // Must already have disposed of the old blob somehow.
   923   assert(blob() == NULL, "must be empty");
   924 #ifdef ASSERT
   926 #endif
   927   // Take the new blob away from cb.
   928   set_blob(cb->blob());
   929   // Take over all the section pointers.
   930   for (int n = 0; n < (int)SECT_LIMIT; n++) {
   931     CodeSection* cb_sect   = cb->code_section(n);
   932     CodeSection* this_sect = code_section(n);
   933     this_sect->take_over_code_from(cb_sect);
   934   }
   935   _overflow_arena = cb->_overflow_arena;
   936   // Make sure the old cb won't try to use it or free it.
   937   DEBUG_ONLY(cb->_blob = (BufferBlob*)badAddress);
   938 }
   940 void CodeBuffer::verify_section_allocation() {
   941   address tstart = _total_start;
   942   if (tstart == badAddress)  return;  // smashed by set_blob(NULL)
   943   address tend   = tstart + _total_size;
   944   if (_blob != NULL) {
   946     guarantee(tstart >= _blob->content_begin(), "sanity");
   947     guarantee(tend   <= _blob->content_end(),   "sanity");
   948   }
   949   // Verify disjointness.
   950   for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) {
   951     CodeSection* sect = code_section(n);
   952     if (!sect->is_allocated() || sect->is_empty())  continue;
   953     guarantee((intptr_t)sect->start() % sect->alignment() == 0
   954            || sect->is_empty() || _blob == NULL,
   955            "start is aligned");
   956     for (int m = (int) SECT_FIRST; m < (int) SECT_LIMIT; m++) {
   957       CodeSection* other = code_section(m);
   958       if (!other->is_allocated() || other == sect)  continue;
   959       guarantee(!other->contains(sect->start()    ), "sanity");
   960       // limit is an exclusive address and can be the start of another
   961       // section.
   962       guarantee(!other->contains(sect->limit() - 1), "sanity");
   963     }
   964     guarantee(sect->end() <= tend, "sanity");
   965     guarantee(sect->end() <= sect->limit(), "sanity");
   966   }
   967 }
   969 void CodeBuffer::log_section_sizes(const char* name) {
   970   if (xtty != NULL) {
   971     // log info about buffer usage
   972     xtty->print_cr("<blob name='%s' size='%d'>", name, _total_size);
   973     for (int n = (int) CodeBuffer::SECT_FIRST; n < (int) CodeBuffer::SECT_LIMIT; n++) {
   974       CodeSection* sect = code_section(n);
   975       if (!sect->is_allocated() || sect->is_empty())  continue;
   976       xtty->print_cr("<sect index='%d' size='" SIZE_FORMAT "' free='" SIZE_FORMAT "'/>",
   977                      n, sect->limit() - sect->start(), sect->limit() - sect->end());
   978     }
   979     xtty->print_cr("</blob>");
   980   }
   981 }
   983 #ifndef PRODUCT
   985 void CodeSection::dump() {
   986   address ptr = start();
   987   for (csize_t step; ptr < end(); ptr += step) {
   988     step = end() - ptr;
   989     if (step > jintSize * 4)  step = jintSize * 4;
   990     tty->print(INTPTR_FORMAT ": ", p2i(ptr));
   991     while (step > 0) {
   992       tty->print(" " PTR32_FORMAT, *(jint*)ptr);
   993       ptr += jintSize;
   994     }
   995     tty->cr();
   996   }
   997 }
  1000 void CodeSection::decode() {
  1001   Disassembler::decode(start(), end());
  1005 void CodeBuffer::block_comment(intptr_t offset, const char * comment) {
  1006   _strings.add_comment(offset, comment);
  1009 const char* CodeBuffer::code_string(const char* str) {
  1010   return _strings.add_string(str);
  1013 class CodeString: public CHeapObj<mtCode> {
  1014  private:
  1015   friend class CodeStrings;
  1016   const char * _string;
  1017   CodeString*  _next;
  1018   intptr_t     _offset;
  1020   ~CodeString() {
  1021     assert(_next == NULL, "wrong interface for freeing list");
  1022     os::free((void*)_string, mtCode);
  1025   bool is_comment() const { return _offset >= 0; }
  1027  public:
  1028   CodeString(const char * string, intptr_t offset = -1)
  1029     : _next(NULL), _offset(offset) {
  1030     _string = os::strdup(string, mtCode);
  1033   const char * string() const { return _string; }
  1034   intptr_t     offset() const { assert(_offset >= 0, "offset for non comment?"); return _offset;  }
  1035   CodeString* next()    const { return _next; }
  1037   void set_next(CodeString* next) { _next = next; }
  1039   CodeString* first_comment() {
  1040     if (is_comment()) {
  1041       return this;
  1042     } else {
  1043       return next_comment();
  1046   CodeString* next_comment() const {
  1047     CodeString* s = _next;
  1048     while (s != NULL && !s->is_comment()) {
  1049       s = s->_next;
  1051     return s;
  1053 };
  1055 CodeString* CodeStrings::find(intptr_t offset) const {
  1056   CodeString* a = _strings->first_comment();
  1057   while (a != NULL && a->offset() != offset) {
  1058     a = a->next_comment();
  1060   return a;
  1063 // Convenience for add_comment.
  1064 CodeString* CodeStrings::find_last(intptr_t offset) const {
  1065   CodeString* a = find(offset);
  1066   if (a != NULL) {
  1067     CodeString* c = NULL;
  1068     while (((c = a->next_comment()) != NULL) && (c->offset() == offset)) {
  1069       a = c;
  1072   return a;
  1075 void CodeStrings::add_comment(intptr_t offset, const char * comment) {
  1076   CodeString* c      = new CodeString(comment, offset);
  1077   CodeString* inspos = (_strings == NULL) ? NULL : find_last(offset);
  1079   if (inspos) {
  1080     // insert after already existing comments with same offset
  1081     c->set_next(inspos->next());
  1082     inspos->set_next(c);
  1083   } else {
  1084     // no comments with such offset, yet. Insert before anything else.
  1085     c->set_next(_strings);
  1086     _strings = c;
  1090 void CodeStrings::assign(CodeStrings& other) {
  1091   _strings = other._strings;
  1094 void CodeStrings::print_block_comment(outputStream* stream, intptr_t offset) const {
  1095   if (_strings != NULL) {
  1096     CodeString* c = find(offset);
  1097     while (c && c->offset() == offset) {
  1098       stream->bol();
  1099       stream->print("  ;; ");
  1100       stream->print_cr("%s", c->string());
  1101       c = c->next_comment();
  1107 void CodeStrings::free() {
  1108   CodeString* n = _strings;
  1109   while (n) {
  1110     // unlink the node from the list saving a pointer to the next
  1111     CodeString* p = n->next();
  1112     n->set_next(NULL);
  1113     delete n;
  1114     n = p;
  1116   _strings = NULL;
  1119 const char* CodeStrings::add_string(const char * string) {
  1120   CodeString* s = new CodeString(string);
  1121   s->set_next(_strings);
  1122   _strings = s;
  1123   assert(s->string() != NULL, "should have a string");
  1124   return s->string();
  1127 void CodeBuffer::decode() {
  1128   ttyLocker ttyl;
  1129   Disassembler::decode(decode_begin(), insts_end());
  1130   _decode_begin = insts_end();
  1134 void CodeBuffer::skip_decode() {
  1135   _decode_begin = insts_end();
  1139 void CodeBuffer::decode_all() {
  1140   ttyLocker ttyl;
  1141   for (int n = 0; n < (int)SECT_LIMIT; n++) {
  1142     // dump contents of each section
  1143     CodeSection* cs = code_section(n);
  1144     tty->print_cr("! %s:", code_section_name(n));
  1145     if (cs != consts())
  1146       cs->decode();
  1147     else
  1148       cs->dump();
  1153 void CodeSection::print(const char* name) {
  1154   csize_t locs_size = locs_end() - locs_start();
  1155   tty->print_cr(" %7s.code = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d)%s",
  1156                 name, p2i(start()), p2i(end()), p2i(limit()), size(), capacity(),
  1157                 is_frozen()? " [frozen]": "");
  1158   tty->print_cr(" %7s.locs = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d) point=%d",
  1159                 name, p2i(locs_start()), p2i(locs_end()), p2i(locs_limit()), locs_size, locs_capacity(), locs_point_off());
  1160   if (PrintRelocations) {
  1161     RelocIterator iter(this);
  1162     iter.print();
  1166 void CodeBuffer::print() {
  1167   if (this == NULL) {
  1168     tty->print_cr("NULL CodeBuffer pointer");
  1169     return;
  1172   tty->print_cr("CodeBuffer:");
  1173   for (int n = 0; n < (int)SECT_LIMIT; n++) {
  1174     // print each section
  1175     CodeSection* cs = code_section(n);
  1176     cs->print(code_section_name(n));
  1180 #endif // PRODUCT

mercurial