src/share/vm/code/nmethod.cpp

Wed, 09 Apr 2008 15:10:22 -0700

author
rasbold
date
Wed, 09 Apr 2008 15:10:22 -0700
changeset 544
9f4457a14b58
parent 535
c7c777385a15
child 551
018d5b58dd4f
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright 1997-2007 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  */
    25 # include "incls/_precompiled.incl"
    26 # include "incls/_nmethod.cpp.incl"
    28 #ifdef DTRACE_ENABLED
    31 // Only bother with this argument setup if dtrace is available
    33 HS_DTRACE_PROBE_DECL8(hotspot, compiled__method__load,
    34   const char*, int, const char*, int, const char*, int, void*, size_t);
    36 HS_DTRACE_PROBE_DECL6(hotspot, compiled__method__unload,
    37   char*, int, char*, int, char*, int);
    39 #define DTRACE_METHOD_UNLOAD_PROBE(method)                                \
    40   {                                                                       \
    41     methodOop m = (method);                                               \
    42     if (m != NULL) {                                                      \
    43       symbolOop klass_name = m->klass_name();                             \
    44       symbolOop name = m->name();                                         \
    45       symbolOop signature = m->signature();                               \
    46       HS_DTRACE_PROBE6(hotspot, compiled__method__unload,                 \
    47         klass_name->bytes(), klass_name->utf8_length(),                   \
    48         name->bytes(), name->utf8_length(),                               \
    49         signature->bytes(), signature->utf8_length());                    \
    50     }                                                                     \
    51   }
    53 #else //  ndef DTRACE_ENABLED
    55 #define DTRACE_METHOD_UNLOAD_PROBE(method)
    57 #endif
    59 bool nmethod::is_compiled_by_c1() const {
    60   if (is_native_method()) return false;
    61   assert(compiler() != NULL, "must be");
    62   return compiler()->is_c1();
    63 }
    64 bool nmethod::is_compiled_by_c2() const {
    65   if (is_native_method()) return false;
    66   assert(compiler() != NULL, "must be");
    67   return compiler()->is_c2();
    68 }
    72 //---------------------------------------------------------------------------------
    73 // NMethod statistics
    74 // They are printed under various flags, including:
    75 //   PrintC1Statistics, PrintOptoStatistics, LogVMOutput, and LogCompilation.
    76 // (In the latter two cases, they like other stats are printed to the log only.)
    78 #ifndef PRODUCT
    79 // These variables are put into one block to reduce relocations
    80 // and make it simpler to print from the debugger.
    81 static
    82 struct nmethod_stats_struct {
    83   int nmethod_count;
    84   int total_size;
    85   int relocation_size;
    86   int code_size;
    87   int stub_size;
    88   int consts_size;
    89   int scopes_data_size;
    90   int scopes_pcs_size;
    91   int dependencies_size;
    92   int handler_table_size;
    93   int nul_chk_table_size;
    94   int oops_size;
    96   void note_nmethod(nmethod* nm) {
    97     nmethod_count += 1;
    98     total_size          += nm->size();
    99     relocation_size     += nm->relocation_size();
   100     code_size           += nm->code_size();
   101     stub_size           += nm->stub_size();
   102     consts_size         += nm->consts_size();
   103     scopes_data_size    += nm->scopes_data_size();
   104     scopes_pcs_size     += nm->scopes_pcs_size();
   105     dependencies_size   += nm->dependencies_size();
   106     handler_table_size  += nm->handler_table_size();
   107     nul_chk_table_size  += nm->nul_chk_table_size();
   108     oops_size += nm->oops_size();
   109   }
   110   void print_nmethod_stats() {
   111     if (nmethod_count == 0)  return;
   112     tty->print_cr("Statistics for %d bytecoded nmethods:", nmethod_count);
   113     if (total_size != 0)          tty->print_cr(" total in heap  = %d", total_size);
   114     if (relocation_size != 0)     tty->print_cr(" relocation     = %d", relocation_size);
   115     if (code_size != 0)           tty->print_cr(" main code      = %d", code_size);
   116     if (stub_size != 0)           tty->print_cr(" stub code      = %d", stub_size);
   117     if (consts_size != 0)         tty->print_cr(" constants      = %d", consts_size);
   118     if (scopes_data_size != 0)    tty->print_cr(" scopes data    = %d", scopes_data_size);
   119     if (scopes_pcs_size != 0)     tty->print_cr(" scopes pcs     = %d", scopes_pcs_size);
   120     if (dependencies_size != 0)   tty->print_cr(" dependencies   = %d", dependencies_size);
   121     if (handler_table_size != 0)  tty->print_cr(" handler table  = %d", handler_table_size);
   122     if (nul_chk_table_size != 0)  tty->print_cr(" nul chk table  = %d", nul_chk_table_size);
   123     if (oops_size != 0)           tty->print_cr(" oops           = %d", oops_size);
   124   }
   126   int native_nmethod_count;
   127   int native_total_size;
   128   int native_relocation_size;
   129   int native_code_size;
   130   int native_oops_size;
   131   void note_native_nmethod(nmethod* nm) {
   132     native_nmethod_count += 1;
   133     native_total_size       += nm->size();
   134     native_relocation_size  += nm->relocation_size();
   135     native_code_size        += nm->code_size();
   136     native_oops_size        += nm->oops_size();
   137   }
   138   void print_native_nmethod_stats() {
   139     if (native_nmethod_count == 0)  return;
   140     tty->print_cr("Statistics for %d native nmethods:", native_nmethod_count);
   141     if (native_total_size != 0)       tty->print_cr(" N. total size  = %d", native_total_size);
   142     if (native_relocation_size != 0)  tty->print_cr(" N. relocation  = %d", native_relocation_size);
   143     if (native_code_size != 0)        tty->print_cr(" N. main code   = %d", native_code_size);
   144     if (native_oops_size != 0)        tty->print_cr(" N. oops        = %d", native_oops_size);
   145   }
   147   int pc_desc_resets;   // number of resets (= number of caches)
   148   int pc_desc_queries;  // queries to nmethod::find_pc_desc
   149   int pc_desc_approx;   // number of those which have approximate true
   150   int pc_desc_repeats;  // number of _last_pc_desc hits
   151   int pc_desc_hits;     // number of LRU cache hits
   152   int pc_desc_tests;    // total number of PcDesc examinations
   153   int pc_desc_searches; // total number of quasi-binary search steps
   154   int pc_desc_adds;     // number of LUR cache insertions
   156   void print_pc_stats() {
   157     tty->print_cr("PcDesc Statistics:  %d queries, %.2f comparisons per query",
   158                   pc_desc_queries,
   159                   (double)(pc_desc_tests + pc_desc_searches)
   160                   / pc_desc_queries);
   161     tty->print_cr("  caches=%d queries=%d/%d, hits=%d+%d, tests=%d+%d, adds=%d",
   162                   pc_desc_resets,
   163                   pc_desc_queries, pc_desc_approx,
   164                   pc_desc_repeats, pc_desc_hits,
   165                   pc_desc_tests, pc_desc_searches, pc_desc_adds);
   166   }
   167 } nmethod_stats;
   168 #endif //PRODUCT
   170 //---------------------------------------------------------------------------------
   173 // The _unwind_handler is a special marker address, which says that
   174 // for given exception oop and address, the frame should be removed
   175 // as the tuple cannot be caught in the nmethod
   176 address ExceptionCache::_unwind_handler = (address) -1;
   179 ExceptionCache::ExceptionCache(Handle exception, address pc, address handler) {
   180   assert(pc != NULL, "Must be non null");
   181   assert(exception.not_null(), "Must be non null");
   182   assert(handler != NULL, "Must be non null");
   184   _count = 0;
   185   _exception_type = exception->klass();
   186   _next = NULL;
   188   add_address_and_handler(pc,handler);
   189 }
   192 address ExceptionCache::match(Handle exception, address pc) {
   193   assert(pc != NULL,"Must be non null");
   194   assert(exception.not_null(),"Must be non null");
   195   if (exception->klass() == exception_type()) {
   196     return (test_address(pc));
   197   }
   199   return NULL;
   200 }
   203 bool ExceptionCache::match_exception_with_space(Handle exception) {
   204   assert(exception.not_null(),"Must be non null");
   205   if (exception->klass() == exception_type() && count() < cache_size) {
   206     return true;
   207   }
   208   return false;
   209 }
   212 address ExceptionCache::test_address(address addr) {
   213   for (int i=0; i<count(); i++) {
   214     if (pc_at(i) == addr) {
   215       return handler_at(i);
   216     }
   217   }
   218   return NULL;
   219 }
   222 bool ExceptionCache::add_address_and_handler(address addr, address handler) {
   223   if (test_address(addr) == handler) return true;
   224   if (count() < cache_size) {
   225     set_pc_at(count(),addr);
   226     set_handler_at(count(), handler);
   227     increment_count();
   228     return true;
   229   }
   230   return false;
   231 }
   234 // private method for handling exception cache
   235 // These methods are private, and used to manipulate the exception cache
   236 // directly.
   237 ExceptionCache* nmethod::exception_cache_entry_for_exception(Handle exception) {
   238   ExceptionCache* ec = exception_cache();
   239   while (ec != NULL) {
   240     if (ec->match_exception_with_space(exception)) {
   241       return ec;
   242     }
   243     ec = ec->next();
   244   }
   245   return NULL;
   246 }
   249 //-----------------------------------------------------------------------------
   252 // Helper used by both find_pc_desc methods.
   253 static inline bool match_desc(PcDesc* pc, int pc_offset, bool approximate) {
   254   NOT_PRODUCT(++nmethod_stats.pc_desc_tests);
   255   if (!approximate)
   256     return pc->pc_offset() == pc_offset;
   257   else
   258     return (pc-1)->pc_offset() < pc_offset && pc_offset <= pc->pc_offset();
   259 }
   261 void PcDescCache::reset_to(PcDesc* initial_pc_desc) {
   262   if (initial_pc_desc == NULL) {
   263     _last_pc_desc = NULL;  // native method
   264     return;
   265   }
   266   NOT_PRODUCT(++nmethod_stats.pc_desc_resets);
   267   // reset the cache by filling it with benign (non-null) values
   268   assert(initial_pc_desc->pc_offset() < 0, "must be sentinel");
   269   _last_pc_desc = initial_pc_desc + 1;  // first valid one is after sentinel
   270   for (int i = 0; i < cache_size; i++)
   271     _pc_descs[i] = initial_pc_desc;
   272 }
   274 PcDesc* PcDescCache::find_pc_desc(int pc_offset, bool approximate) {
   275   NOT_PRODUCT(++nmethod_stats.pc_desc_queries);
   276   NOT_PRODUCT(if (approximate)  ++nmethod_stats.pc_desc_approx);
   278   // In order to prevent race conditions do not load cache elements
   279   // repeatedly, but use a local copy:
   280   PcDesc* res;
   282   // Step one:  Check the most recently returned value.
   283   res = _last_pc_desc;
   284   if (res == NULL)  return NULL;  // native method; no PcDescs at all
   285   if (match_desc(res, pc_offset, approximate)) {
   286     NOT_PRODUCT(++nmethod_stats.pc_desc_repeats);
   287     return res;
   288   }
   290   // Step two:  Check the LRU cache.
   291   for (int i = 0; i < cache_size; i++) {
   292     res = _pc_descs[i];
   293     if (res->pc_offset() < 0)  break;  // optimization: skip empty cache
   294     if (match_desc(res, pc_offset, approximate)) {
   295       NOT_PRODUCT(++nmethod_stats.pc_desc_hits);
   296       _last_pc_desc = res;  // record this cache hit in case of repeat
   297       return res;
   298     }
   299   }
   301   // Report failure.
   302   return NULL;
   303 }
   305 void PcDescCache::add_pc_desc(PcDesc* pc_desc) {
   306   NOT_PRODUCT(++nmethod_stats.pc_desc_adds);
   307   // Update the LRU cache by shifting pc_desc forward:
   308   for (int i = 0; i < cache_size; i++)  {
   309     PcDesc* next = _pc_descs[i];
   310     _pc_descs[i] = pc_desc;
   311     pc_desc = next;
   312   }
   313   // Note:  Do not update _last_pc_desc.  It fronts for the LRU cache.
   314 }
   316 // adjust pcs_size so that it is a multiple of both oopSize and
   317 // sizeof(PcDesc) (assumes that if sizeof(PcDesc) is not a multiple
   318 // of oopSize, then 2*sizeof(PcDesc) is)
   319 static int  adjust_pcs_size(int pcs_size) {
   320   int nsize = round_to(pcs_size,   oopSize);
   321   if ((nsize % sizeof(PcDesc)) != 0) {
   322     nsize = pcs_size + sizeof(PcDesc);
   323   }
   324   assert((nsize %  oopSize) == 0, "correct alignment");
   325   return nsize;
   326 }
   328 //-----------------------------------------------------------------------------
   331 void nmethod::add_exception_cache_entry(ExceptionCache* new_entry) {
   332   assert(ExceptionCache_lock->owned_by_self(),"Must hold the ExceptionCache_lock");
   333   assert(new_entry != NULL,"Must be non null");
   334   assert(new_entry->next() == NULL, "Must be null");
   336   if (exception_cache() != NULL) {
   337     new_entry->set_next(exception_cache());
   338   }
   339   set_exception_cache(new_entry);
   340 }
   342 void nmethod::remove_from_exception_cache(ExceptionCache* ec) {
   343   ExceptionCache* prev = NULL;
   344   ExceptionCache* curr = exception_cache();
   345   assert(curr != NULL, "nothing to remove");
   346   // find the previous and next entry of ec
   347   while (curr != ec) {
   348     prev = curr;
   349     curr = curr->next();
   350     assert(curr != NULL, "ExceptionCache not found");
   351   }
   352   // now: curr == ec
   353   ExceptionCache* next = curr->next();
   354   if (prev == NULL) {
   355     set_exception_cache(next);
   356   } else {
   357     prev->set_next(next);
   358   }
   359   delete curr;
   360 }
   363 // public method for accessing the exception cache
   364 // These are the public access methods.
   365 address nmethod::handler_for_exception_and_pc(Handle exception, address pc) {
   366   // We never grab a lock to read the exception cache, so we may
   367   // have false negatives. This is okay, as it can only happen during
   368   // the first few exception lookups for a given nmethod.
   369   ExceptionCache* ec = exception_cache();
   370   while (ec != NULL) {
   371     address ret_val;
   372     if ((ret_val = ec->match(exception,pc)) != NULL) {
   373       return ret_val;
   374     }
   375     ec = ec->next();
   376   }
   377   return NULL;
   378 }
   381 void nmethod::add_handler_for_exception_and_pc(Handle exception, address pc, address handler) {
   382   // There are potential race conditions during exception cache updates, so we
   383   // must own the ExceptionCache_lock before doing ANY modifications. Because
   384   // we dont lock during reads, it is possible to have several threads attempt
   385   // to update the cache with the same data. We need to check for already inserted
   386   // copies of the current data before adding it.
   388   MutexLocker ml(ExceptionCache_lock);
   389   ExceptionCache* target_entry = exception_cache_entry_for_exception(exception);
   391   if (target_entry == NULL || !target_entry->add_address_and_handler(pc,handler)) {
   392     target_entry = new ExceptionCache(exception,pc,handler);
   393     add_exception_cache_entry(target_entry);
   394   }
   395 }
   398 //-------------end of code for ExceptionCache--------------
   401 void nmFlags::clear() {
   402   assert(sizeof(nmFlags) == sizeof(int), "using more than one word for nmFlags");
   403   *(jint*)this = 0;
   404 }
   406 int nmethod::total_size() const {
   407   return
   408     code_size()          +
   409     stub_size()          +
   410     consts_size()        +
   411     scopes_data_size()   +
   412     scopes_pcs_size()    +
   413     handler_table_size() +
   414     nul_chk_table_size();
   415 }
   417 const char* nmethod::compile_kind() const {
   418   if (method() == NULL)    return "unloaded";
   419   if (is_native_method())  return "c2n";
   420   if (is_osr_method())     return "osr";
   421   return NULL;
   422 }
   424 // %%% This variable is no longer used?
   425 int nmethod::_zombie_instruction_size = NativeJump::instruction_size;
   428 nmethod* nmethod::new_native_nmethod(methodHandle method,
   429   CodeBuffer *code_buffer,
   430   int vep_offset,
   431   int frame_complete,
   432   int frame_size,
   433   ByteSize basic_lock_owner_sp_offset,
   434   ByteSize basic_lock_sp_offset,
   435   OopMapSet* oop_maps) {
   436   // create nmethod
   437   nmethod* nm = NULL;
   438   {
   439     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   440     int native_nmethod_size = allocation_size(code_buffer, sizeof(nmethod));
   441     const int dummy = -1;               // Flag to force proper "operator new"
   442     CodeOffsets offsets;
   443     offsets.set_value(CodeOffsets::Verified_Entry, vep_offset);
   444     offsets.set_value(CodeOffsets::Frame_Complete, frame_complete);
   445     nm = new (native_nmethod_size)
   446       nmethod(method(), native_nmethod_size, &offsets,
   447               code_buffer, frame_size,
   448               basic_lock_owner_sp_offset, basic_lock_sp_offset,
   449               oop_maps);
   450     NOT_PRODUCT(if (nm != NULL)  nmethod_stats.note_native_nmethod(nm));
   451     if (PrintAssembly && nm != NULL)
   452       Disassembler::decode(nm);
   453   }
   454   // verify nmethod
   455   debug_only(if (nm) nm->verify();) // might block
   457   if (nm != NULL) {
   458     nm->log_new_nmethod();
   459   }
   461   return nm;
   462 }
   464 nmethod* nmethod::new_nmethod(methodHandle method,
   465   int compile_id,
   466   int entry_bci,
   467   CodeOffsets* offsets,
   468   int orig_pc_offset,
   469   DebugInformationRecorder* debug_info,
   470   Dependencies* dependencies,
   471   CodeBuffer* code_buffer, int frame_size,
   472   OopMapSet* oop_maps,
   473   ExceptionHandlerTable* handler_table,
   474   ImplicitExceptionTable* nul_chk_table,
   475   AbstractCompiler* compiler,
   476   int comp_level
   477 )
   478 {
   479   assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
   480   // create nmethod
   481   nmethod* nm = NULL;
   482   { MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   483     int nmethod_size =
   484       allocation_size(code_buffer, sizeof(nmethod))
   485       + adjust_pcs_size(debug_info->pcs_size())
   486       + round_to(dependencies->size_in_bytes() , oopSize)
   487       + round_to(handler_table->size_in_bytes(), oopSize)
   488       + round_to(nul_chk_table->size_in_bytes(), oopSize)
   489       + round_to(debug_info->data_size()       , oopSize);
   490     nm = new (nmethod_size)
   491       nmethod(method(), nmethod_size, compile_id, entry_bci, offsets,
   492               orig_pc_offset, debug_info, dependencies, code_buffer, frame_size,
   493               oop_maps,
   494               handler_table,
   495               nul_chk_table,
   496               compiler,
   497               comp_level);
   498     if (nm != NULL) {
   499       // To make dependency checking during class loading fast, record
   500       // the nmethod dependencies in the classes it is dependent on.
   501       // This allows the dependency checking code to simply walk the
   502       // class hierarchy above the loaded class, checking only nmethods
   503       // which are dependent on those classes.  The slow way is to
   504       // check every nmethod for dependencies which makes it linear in
   505       // the number of methods compiled.  For applications with a lot
   506       // classes the slow way is too slow.
   507       for (Dependencies::DepStream deps(nm); deps.next(); ) {
   508         klassOop klass = deps.context_type();
   509         if (klass == NULL)  continue;  // ignore things like evol_method
   511         // record this nmethod as dependent on this klass
   512         instanceKlass::cast(klass)->add_dependent_nmethod(nm);
   513       }
   514     }
   515     NOT_PRODUCT(if (nm != NULL)  nmethod_stats.note_nmethod(nm));
   516     if (PrintAssembly && nm != NULL)
   517       Disassembler::decode(nm);
   518   }
   520   // verify nmethod
   521   debug_only(if (nm) nm->verify();) // might block
   523   if (nm != NULL) {
   524     nm->log_new_nmethod();
   525   }
   527   // done
   528   return nm;
   529 }
   532 // For native wrappers
   533 nmethod::nmethod(
   534   methodOop method,
   535   int nmethod_size,
   536   CodeOffsets* offsets,
   537   CodeBuffer* code_buffer,
   538   int frame_size,
   539   ByteSize basic_lock_owner_sp_offset,
   540   ByteSize basic_lock_sp_offset,
   541   OopMapSet* oop_maps )
   542   : CodeBlob("native nmethod", code_buffer, sizeof(nmethod),
   543              nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps),
   544   _compiled_synchronized_native_basic_lock_owner_sp_offset(basic_lock_owner_sp_offset),
   545   _compiled_synchronized_native_basic_lock_sp_offset(basic_lock_sp_offset)
   546 {
   547   {
   548     debug_only(No_Safepoint_Verifier nsv;)
   549     assert_locked_or_safepoint(CodeCache_lock);
   551     NOT_PRODUCT(_has_debug_info = false; )
   552     _method                  = method;
   553     _entry_bci               = InvocationEntryBci;
   554     _link                    = NULL;
   555     _compiler                = NULL;
   556     // We have no exception handler or deopt handler make the
   557     // values something that will never match a pc like the nmethod vtable entry
   558     _exception_offset        = 0;
   559     _deoptimize_offset       = 0;
   560     _orig_pc_offset          = 0;
   561     _stub_offset             = data_offset();
   562     _consts_offset           = data_offset();
   563     _scopes_data_offset      = data_offset();
   564     _scopes_pcs_offset       = _scopes_data_offset;
   565     _dependencies_offset     = _scopes_pcs_offset;
   566     _handler_table_offset    = _dependencies_offset;
   567     _nul_chk_table_offset    = _handler_table_offset;
   568     _nmethod_end_offset      = _nul_chk_table_offset;
   569     _compile_id              = 0;  // default
   570     _comp_level              = CompLevel_none;
   571     _entry_point             = instructions_begin();
   572     _verified_entry_point    = instructions_begin() + offsets->value(CodeOffsets::Verified_Entry);
   573     _osr_entry_point         = NULL;
   574     _exception_cache         = NULL;
   575     _pc_desc_cache.reset_to(NULL);
   577     flags.clear();
   578     flags.state              = alive;
   579     _markedForDeoptimization = 0;
   581     _lock_count = 0;
   582     _stack_traversal_mark    = 0;
   584     code_buffer->copy_oops_to(this);
   585     debug_only(check_store();)
   586     CodeCache::commit(this);
   587     VTune::create_nmethod(this);
   588   }
   590   if (PrintNativeNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) {
   591     ttyLocker ttyl;  // keep the following output all in one block
   592     // This output goes directly to the tty, not the compiler log.
   593     // To enable tools to match it up with the compilation activity,
   594     // be sure to tag this tty output with the compile ID.
   595     if (xtty != NULL) {
   596       xtty->begin_head("print_native_nmethod");
   597       xtty->method(_method);
   598       xtty->stamp();
   599       xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this);
   600     }
   601     // print the header part first
   602     print();
   603     // then print the requested information
   604     if (PrintNativeNMethods) {
   605       print_code();
   606       oop_maps->print();
   607     }
   608     if (PrintRelocations) {
   609       print_relocations();
   610     }
   611     if (xtty != NULL) {
   612       xtty->tail("print_native_nmethod");
   613     }
   614   }
   615   Events::log("Create nmethod " INTPTR_FORMAT, this);
   616 }
   619 void* nmethod::operator new(size_t size, int nmethod_size) {
   620   // Always leave some room in the CodeCache for I2C/C2I adapters
   621   if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) return NULL;
   622   return CodeCache::allocate(nmethod_size);
   623 }
   626 nmethod::nmethod(
   627   methodOop method,
   628   int nmethod_size,
   629   int compile_id,
   630   int entry_bci,
   631   CodeOffsets* offsets,
   632   int orig_pc_offset,
   633   DebugInformationRecorder* debug_info,
   634   Dependencies* dependencies,
   635   CodeBuffer *code_buffer,
   636   int frame_size,
   637   OopMapSet* oop_maps,
   638   ExceptionHandlerTable* handler_table,
   639   ImplicitExceptionTable* nul_chk_table,
   640   AbstractCompiler* compiler,
   641   int comp_level
   642   )
   643   : CodeBlob("nmethod", code_buffer, sizeof(nmethod),
   644              nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps),
   645   _compiled_synchronized_native_basic_lock_owner_sp_offset(in_ByteSize(-1)),
   646   _compiled_synchronized_native_basic_lock_sp_offset(in_ByteSize(-1))
   647 {
   648   assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
   649   {
   650     debug_only(No_Safepoint_Verifier nsv;)
   651     assert_locked_or_safepoint(CodeCache_lock);
   653     NOT_PRODUCT(_has_debug_info = false; )
   654     _method                  = method;
   655     _compile_id              = compile_id;
   656     _comp_level              = comp_level;
   657     _entry_bci               = entry_bci;
   658     _link                    = NULL;
   659     _compiler                = compiler;
   660     _orig_pc_offset          = orig_pc_offset;
   661     _stub_offset             = instructions_offset() + code_buffer->total_offset_of(code_buffer->stubs()->start());
   663     // Exception handler and deopt handler are in the stub section
   664     _exception_offset        = _stub_offset + offsets->value(CodeOffsets::Exceptions);
   665     _deoptimize_offset       = _stub_offset + offsets->value(CodeOffsets::Deopt);
   666     _consts_offset           = instructions_offset() + code_buffer->total_offset_of(code_buffer->consts()->start());
   667     _scopes_data_offset      = data_offset();
   668     _scopes_pcs_offset       = _scopes_data_offset   + round_to(debug_info->data_size         (), oopSize);
   669     _dependencies_offset     = _scopes_pcs_offset    + adjust_pcs_size(debug_info->pcs_size());
   670     _handler_table_offset    = _dependencies_offset  + round_to(dependencies->size_in_bytes (), oopSize);
   671     _nul_chk_table_offset    = _handler_table_offset + round_to(handler_table->size_in_bytes(), oopSize);
   672     _nmethod_end_offset      = _nul_chk_table_offset + round_to(nul_chk_table->size_in_bytes(), oopSize);
   674     _entry_point             = instructions_begin();
   675     _verified_entry_point    = instructions_begin() + offsets->value(CodeOffsets::Verified_Entry);
   676     _osr_entry_point         = instructions_begin() + offsets->value(CodeOffsets::OSR_Entry);
   677     _exception_cache         = NULL;
   678     _pc_desc_cache.reset_to(scopes_pcs_begin());
   680     flags.clear();
   681     flags.state              = alive;
   682     _markedForDeoptimization = 0;
   684     _unload_reported         = false;           // jvmti state
   686     _lock_count = 0;
   687     _stack_traversal_mark    = 0;
   689     // Copy contents of ScopeDescRecorder to nmethod
   690     code_buffer->copy_oops_to(this);
   691     debug_info->copy_to(this);
   692     dependencies->copy_to(this);
   693     debug_only(check_store();)
   695     CodeCache::commit(this);
   697     VTune::create_nmethod(this);
   699     // Copy contents of ExceptionHandlerTable to nmethod
   700     handler_table->copy_to(this);
   701     nul_chk_table->copy_to(this);
   703     // we use the information of entry points to find out if a method is
   704     // static or non static
   705     assert(compiler->is_c2() ||
   706            _method->is_static() == (entry_point() == _verified_entry_point),
   707            " entry points must be same for static methods and vice versa");
   708   }
   710   bool printnmethods = PrintNMethods
   711     || CompilerOracle::should_print(_method)
   712     || CompilerOracle::has_option_string(_method, "PrintNMethods");
   713   if (printnmethods || PrintDebugInfo || PrintRelocations || PrintDependencies || PrintExceptionHandlers) {
   714     print_nmethod(printnmethods);
   715   }
   717   // Note: Do not verify in here as the CodeCache_lock is
   718   //       taken which would conflict with the CompiledIC_lock
   719   //       which taken during the verification of call sites.
   720   //       (was bug - gri 10/25/99)
   722   Events::log("Create nmethod " INTPTR_FORMAT, this);
   723 }
   726 // Print a short set of xml attributes to identify this nmethod.  The
   727 // output should be embedded in some other element.
   728 void nmethod::log_identity(xmlStream* log) const {
   729   log->print(" compile_id='%d'", compile_id());
   730   const char* nm_kind = compile_kind();
   731   if (nm_kind != NULL)  log->print(" compile_kind='%s'", nm_kind);
   732   if (compiler() != NULL) {
   733     log->print(" compiler='%s'", compiler()->name());
   734   }
   735 #ifdef TIERED
   736   log->print(" level='%d'", comp_level());
   737 #endif // TIERED
   738 }
   741 #define LOG_OFFSET(log, name)                    \
   742   if ((intptr_t)name##_end() - (intptr_t)name##_begin()) \
   743     log->print(" " XSTR(name) "_offset='%d'"    , \
   744                (intptr_t)name##_begin() - (intptr_t)this)
   747 void nmethod::log_new_nmethod() const {
   748   if (LogCompilation && xtty != NULL) {
   749     ttyLocker ttyl;
   750     HandleMark hm;
   751     xtty->begin_elem("nmethod");
   752     log_identity(xtty);
   753     xtty->print(" entry='" INTPTR_FORMAT "' size='%d'",
   754                 instructions_begin(), size());
   755     xtty->print(" address='" INTPTR_FORMAT "'", (intptr_t) this);
   757     LOG_OFFSET(xtty, relocation);
   758     LOG_OFFSET(xtty, code);
   759     LOG_OFFSET(xtty, stub);
   760     LOG_OFFSET(xtty, consts);
   761     LOG_OFFSET(xtty, scopes_data);
   762     LOG_OFFSET(xtty, scopes_pcs);
   763     LOG_OFFSET(xtty, dependencies);
   764     LOG_OFFSET(xtty, handler_table);
   765     LOG_OFFSET(xtty, nul_chk_table);
   766     LOG_OFFSET(xtty, oops);
   768     xtty->method(method());
   769     xtty->stamp();
   770     xtty->end_elem();
   771   }
   772 }
   774 #undef LOG_OFFSET
   777 // Print out more verbose output usually for a newly created nmethod.
   778 void nmethod::print_on(outputStream* st, const char* title) const {
   779   if (st != NULL) {
   780     ttyLocker ttyl;
   781     // Print a little tag line that looks like +PrintCompilation output:
   782     st->print("%3d%c  %s",
   783               compile_id(),
   784               is_osr_method() ? '%' :
   785               method() != NULL &&
   786               is_native_method() ? 'n' : ' ',
   787               title);
   788 #ifdef TIERED
   789     st->print(" (%d) ", comp_level());
   790 #endif // TIERED
   791     if (WizardMode) st->print(" (" INTPTR_FORMAT ")", this);
   792     if (method() != NULL) {
   793       method()->print_short_name(st);
   794       if (is_osr_method())
   795         st->print(" @ %d", osr_entry_bci());
   796       if (method()->code_size() > 0)
   797         st->print(" (%d bytes)", method()->code_size());
   798     }
   799   }
   800 }
   803 void nmethod::print_nmethod(bool printmethod) {
   804   ttyLocker ttyl;  // keep the following output all in one block
   805   if (xtty != NULL) {
   806     xtty->begin_head("print_nmethod");
   807     xtty->stamp();
   808     xtty->end_head();
   809   }
   810   // print the header part first
   811   print();
   812   // then print the requested information
   813   if (printmethod) {
   814     print_code();
   815     print_pcs();
   816     oop_maps()->print();
   817   }
   818   if (PrintDebugInfo) {
   819     print_scopes();
   820   }
   821   if (PrintRelocations) {
   822     print_relocations();
   823   }
   824   if (PrintDependencies) {
   825     print_dependencies();
   826   }
   827   if (PrintExceptionHandlers) {
   828     print_handler_table();
   829     print_nul_chk_table();
   830   }
   831   if (xtty != NULL) {
   832     xtty->tail("print_nmethod");
   833   }
   834 }
   837 void nmethod::set_version(int v) {
   838   flags.version = v;
   839 }
   842 ScopeDesc* nmethod::scope_desc_at(address pc) {
   843   PcDesc* pd = pc_desc_at(pc);
   844   guarantee(pd != NULL, "scope must be present");
   845   return new ScopeDesc(this, pd->scope_decode_offset(),
   846                        pd->obj_decode_offset());
   847 }
   850 void nmethod::clear_inline_caches() {
   851   assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint");
   852   if (is_zombie()) {
   853     return;
   854   }
   856   RelocIterator iter(this);
   857   while (iter.next()) {
   858     iter.reloc()->clear_inline_cache();
   859   }
   860 }
   863 void nmethod::cleanup_inline_caches() {
   865   assert(SafepointSynchronize::is_at_safepoint() &&
   866         !CompiledIC_lock->is_locked() &&
   867         !Patching_lock->is_locked(), "no threads must be updating the inline caches by them selfs");
   869   // If the method is not entrant or zombie then a JMP is plastered over the
   870   // first few bytes.  If an oop in the old code was there, that oop
   871   // should not get GC'd.  Skip the first few bytes of oops on
   872   // not-entrant methods.
   873   address low_boundary = verified_entry_point();
   874   if (!is_in_use()) {
   875     low_boundary += NativeJump::instruction_size;
   876     // %%% Note:  On SPARC we patch only a 4-byte trap, not a full NativeJump.
   877     // This means that the low_boundary is going to be a little too high.
   878     // This shouldn't matter, since oops of non-entrant methods are never used.
   879     // In fact, why are we bothering to look at oops in a non-entrant method??
   880   }
   882   // Find all calls in an nmethod, and clear the ones that points to zombie methods
   883   ResourceMark rm;
   884   RelocIterator iter(this, low_boundary);
   885   while(iter.next()) {
   886     switch(iter.type()) {
   887       case relocInfo::virtual_call_type:
   888       case relocInfo::opt_virtual_call_type: {
   889         CompiledIC *ic = CompiledIC_at(iter.reloc());
   890         // Ok, to lookup references to zombies here
   891         CodeBlob *cb = CodeCache::find_blob_unsafe(ic->ic_destination());
   892         if( cb != NULL && cb->is_nmethod() ) {
   893           nmethod* nm = (nmethod*)cb;
   894           // Clean inline caches pointing to both zombie and not_entrant methods
   895           if (!nm->is_in_use()) ic->set_to_clean();
   896         }
   897         break;
   898       }
   899       case relocInfo::static_call_type: {
   900         CompiledStaticCall *csc = compiledStaticCall_at(iter.reloc());
   901         CodeBlob *cb = CodeCache::find_blob_unsafe(csc->destination());
   902         if( cb != NULL && cb->is_nmethod() ) {
   903           nmethod* nm = (nmethod*)cb;
   904           // Clean inline caches pointing to both zombie and not_entrant methods
   905           if (!nm->is_in_use()) csc->set_to_clean();
   906         }
   907         break;
   908       }
   909     }
   910   }
   911 }
   913 void nmethod::mark_as_seen_on_stack() {
   914   assert(is_not_entrant(), "must be a non-entrant method");
   915   set_stack_traversal_mark(NMethodSweeper::traversal_count());
   916 }
   918 // Tell if a non-entrant method can be converted to a zombie (i.e., there is no activations on the stack)
   919 bool nmethod::can_not_entrant_be_converted() {
   920   assert(is_not_entrant(), "must be a non-entrant method");
   921   assert(SafepointSynchronize::is_at_safepoint(), "must be called during a safepoint");
   923   // Since the nmethod sweeper only does partial sweep the sweeper's traversal
   924   // count can be greater than the stack traversal count before it hits the
   925   // nmethod for the second time.
   926   return stack_traversal_mark()+1 < NMethodSweeper::traversal_count();
   927 }
   929 void nmethod::inc_decompile_count() {
   930   // Could be gated by ProfileTraps, but do not bother...
   931   methodOop m = method();
   932   if (m == NULL)  return;
   933   methodDataOop mdo = m->method_data();
   934   if (mdo == NULL)  return;
   935   // There is a benign race here.  See comments in methodDataOop.hpp.
   936   mdo->inc_decompile_count();
   937 }
   939 void nmethod::make_unloaded(BoolObjectClosure* is_alive, oop cause) {
   941   post_compiled_method_unload();
   943   // Since this nmethod is being unloaded, make sure that dependencies
   944   // recorded in instanceKlasses get flushed and pass non-NULL closure to
   945   // indicate that this work is being done during a GC.
   946   assert(Universe::heap()->is_gc_active(), "should only be called during gc");
   947   assert(is_alive != NULL, "Should be non-NULL");
   948   // A non-NULL is_alive closure indicates that this is being called during GC.
   949   flush_dependencies(is_alive);
   951   // Break cycle between nmethod & method
   952   if (TraceClassUnloading && WizardMode) {
   953     tty->print_cr("[Class unloading: Making nmethod " INTPTR_FORMAT
   954                   " unloadable], methodOop(" INTPTR_FORMAT
   955                   "), cause(" INTPTR_FORMAT ")",
   956                   this, (address)_method, (address)cause);
   957     cause->klass()->print();
   958   }
   959   // If _method is already NULL the methodOop is about to be unloaded,
   960   // so we don't have to break the cycle. Note that it is possible to
   961   // have the methodOop live here, in case we unload the nmethod because
   962   // it is pointing to some oop (other than the methodOop) being unloaded.
   963   if (_method != NULL) {
   964     // OSR methods point to the methodOop, but the methodOop does not
   965     // point back!
   966     if (_method->code() == this) {
   967       _method->clear_code(); // Break a cycle
   968     }
   969     inc_decompile_count();     // Last chance to make a mark on the MDO
   970     _method = NULL;            // Clear the method of this dead nmethod
   971   }
   972   // Make the class unloaded - i.e., change state and notify sweeper
   973   check_safepoint();
   974   if (is_in_use()) {
   975     // Transitioning directly from live to unloaded -- so
   976     // we need to force a cache clean-up; remember this
   977     // for later on.
   978     CodeCache::set_needs_cache_clean(true);
   979   }
   980   flags.state = unloaded;
   982   // The methodOop is gone at this point
   983   assert(_method == NULL, "Tautology");
   985   set_link(NULL);
   986   NMethodSweeper::notify(this);
   987 }
   989 void nmethod::invalidate_osr_method() {
   990   assert(_entry_bci != InvocationEntryBci, "wrong kind of nmethod");
   991   if (_entry_bci != InvalidOSREntryBci)
   992     inc_decompile_count();
   993   // Remove from list of active nmethods
   994   if (method() != NULL)
   995     instanceKlass::cast(method()->method_holder())->remove_osr_nmethod(this);
   996   // Set entry as invalid
   997   _entry_bci = InvalidOSREntryBci;
   998 }
  1000 void nmethod::log_state_change(int state) const {
  1001   if (LogCompilation) {
  1002     if (xtty != NULL) {
  1003       ttyLocker ttyl;  // keep the following output all in one block
  1004       xtty->begin_elem("make_not_entrant %sthread='" UINTX_FORMAT "'",
  1005                        (state == zombie ? "zombie='1' " : ""),
  1006                        os::current_thread_id());
  1007       log_identity(xtty);
  1008       xtty->stamp();
  1009       xtty->end_elem();
  1012   if (PrintCompilation) {
  1013     print_on(tty, state == zombie ? "made zombie " : "made not entrant ");
  1014     tty->cr();
  1018 // Common functionality for both make_not_entrant and make_zombie
  1019 void nmethod::make_not_entrant_or_zombie(int state) {
  1020   assert(state == zombie || state == not_entrant, "must be zombie or not_entrant");
  1022   // Code for an on-stack-replacement nmethod is removed when a class gets unloaded.
  1023   // They never become zombie/non-entrant, so the nmethod sweeper will never remove
  1024   // them. Instead the entry_bci is set to InvalidOSREntryBci, so the osr nmethod
  1025   // will never be used anymore. That the nmethods only gets removed when class unloading
  1026   // happens, make life much simpler, since the nmethods are not just going to disappear
  1027   // out of the blue.
  1028   if (is_osr_only_method()) {
  1029     if (osr_entry_bci() != InvalidOSREntryBci) {
  1030       // only log this once
  1031       log_state_change(state);
  1033     invalidate_osr_method();
  1034     return;
  1037   // If the method is already zombie or set to the state we want, nothing to do
  1038   if (is_zombie() || (state == not_entrant && is_not_entrant())) {
  1039     return;
  1042   log_state_change(state);
  1044   // Make sure the nmethod is not flushed in case of a safepoint in code below.
  1045   nmethodLocker nml(this);
  1048     // Enter critical section.  Does not block for safepoint.
  1049     MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
  1050     // The caller can be calling the method statically or through an inline
  1051     // cache call.
  1052     if (!is_not_entrant()) {
  1053       NativeJump::patch_verified_entry(entry_point(), verified_entry_point(),
  1054                   SharedRuntime::get_handle_wrong_method_stub());
  1055       assert (NativeJump::instruction_size == nmethod::_zombie_instruction_size, "");
  1058     // When the nmethod becomes zombie it is no longer alive so the
  1059     // dependencies must be flushed.  nmethods in the not_entrant
  1060     // state will be flushed later when the transition to zombie
  1061     // happens or they get unloaded.
  1062     if (state == zombie) {
  1063       assert(SafepointSynchronize::is_at_safepoint(), "must be done at safepoint");
  1064       flush_dependencies(NULL);
  1065     } else {
  1066       assert(state == not_entrant, "other cases may need to be handled differently");
  1069     // Change state
  1070     flags.state = state;
  1071   } // leave critical region under Patching_lock
  1073   if (state == not_entrant) {
  1074     Events::log("Make nmethod not entrant " INTPTR_FORMAT, this);
  1075   } else {
  1076     Events::log("Make nmethod zombie " INTPTR_FORMAT, this);
  1079   if (TraceCreateZombies) {
  1080     tty->print_cr("nmethod <" INTPTR_FORMAT "> code made %s", this, (state == not_entrant) ? "not entrant" : "zombie");
  1083   // Make sweeper aware that there is a zombie method that needs to be removed
  1084   NMethodSweeper::notify(this);
  1086   // not_entrant only stuff
  1087   if (state == not_entrant) {
  1088     mark_as_seen_on_stack();
  1091   // It's a true state change, so mark the method as decompiled.
  1092   inc_decompile_count();
  1095   // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload event
  1096   // and it hasn't already been reported for this nmethod then report it now.
  1097   // (the event may have been reported earilier if the GC marked it for unloading).
  1098   if (state == zombie) {
  1100     DTRACE_METHOD_UNLOAD_PROBE(method());
  1102     if (JvmtiExport::should_post_compiled_method_unload() &&
  1103         !unload_reported()) {
  1104       assert(method() != NULL, "checking");
  1106         HandleMark hm;
  1107         JvmtiExport::post_compiled_method_unload_at_safepoint(
  1108             method()->jmethod_id(), code_begin());
  1110       set_unload_reported();
  1115   // Zombie only stuff
  1116   if (state == zombie) {
  1117     VTune::delete_nmethod(this);
  1120   // Check whether method got unloaded at a safepoint before this,
  1121   // if so we can skip the flushing steps below
  1122   if (method() == NULL) return;
  1124   // Remove nmethod from method.
  1125   // We need to check if both the _code and _from_compiled_code_entry_point
  1126   // refer to this nmethod because there is a race in setting these two fields
  1127   // in methodOop as seen in bugid 4947125.
  1128   // If the vep() points to the zombie nmethod, the memory for the nmethod
  1129   // could be flushed and the compiler and vtable stubs could still call
  1130   // through it.
  1131   if (method()->code() == this ||
  1132       method()->from_compiled_entry() == verified_entry_point()) {
  1133     HandleMark hm;
  1134     method()->clear_code();
  1139 #ifndef PRODUCT
  1140 void nmethod::check_safepoint() {
  1141   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
  1143 #endif
  1146 void nmethod::flush() {
  1147   // Note that there are no valid oops in the nmethod anymore.
  1148   assert(is_zombie() || (is_osr_method() && is_unloaded()), "must be a zombie method");
  1149   assert(is_marked_for_reclamation() || (is_osr_method() && is_unloaded()), "must be marked for reclamation");
  1151   assert (!is_locked_by_vm(), "locked methods shouldn't be flushed");
  1152   check_safepoint();
  1154   // completely deallocate this method
  1155   EventMark m("flushing nmethod " INTPTR_FORMAT " %s", this, "");
  1156   if (PrintMethodFlushing) {
  1157     tty->print_cr("*flushing nmethod " INTPTR_FORMAT ". Live blobs: %d", this, CodeCache::nof_blobs());
  1160   // We need to deallocate any ExceptionCache data.
  1161   // Note that we do not need to grab the nmethod lock for this, it
  1162   // better be thread safe if we're disposing of it!
  1163   ExceptionCache* ec = exception_cache();
  1164   set_exception_cache(NULL);
  1165   while(ec != NULL) {
  1166     ExceptionCache* next = ec->next();
  1167     delete ec;
  1168     ec = next;
  1171   ((CodeBlob*)(this))->flush();
  1173   CodeCache::free(this);
  1177 //
  1178 // Notify all classes this nmethod is dependent on that it is no
  1179 // longer dependent. This should only be called in two situations.
  1180 // First, when a nmethod transitions to a zombie all dependents need
  1181 // to be clear.  Since zombification happens at a safepoint there's no
  1182 // synchronization issues.  The second place is a little more tricky.
  1183 // During phase 1 of mark sweep class unloading may happen and as a
  1184 // result some nmethods may get unloaded.  In this case the flushing
  1185 // of dependencies must happen during phase 1 since after GC any
  1186 // dependencies in the unloaded nmethod won't be updated, so
  1187 // traversing the dependency information in unsafe.  In that case this
  1188 // function is called with a non-NULL argument and this function only
  1189 // notifies instanceKlasses that are reachable
  1191 void nmethod::flush_dependencies(BoolObjectClosure* is_alive) {
  1192   assert(SafepointSynchronize::is_at_safepoint(), "must be done at safepoint");
  1193   assert(Universe::heap()->is_gc_active() == (is_alive != NULL),
  1194   "is_alive is non-NULL if and only if we are called during GC");
  1195   if (!has_flushed_dependencies()) {
  1196     set_has_flushed_dependencies();
  1197     for (Dependencies::DepStream deps(this); deps.next(); ) {
  1198       klassOop klass = deps.context_type();
  1199       if (klass == NULL)  continue;  // ignore things like evol_method
  1201       // During GC the is_alive closure is non-NULL, and is used to
  1202       // determine liveness of dependees that need to be updated.
  1203       if (is_alive == NULL || is_alive->do_object_b(klass)) {
  1204         instanceKlass::cast(klass)->remove_dependent_nmethod(this);
  1211 // If this oop is not live, the nmethod can be unloaded.
  1212 bool nmethod::can_unload(BoolObjectClosure* is_alive,
  1213                          OopClosure* keep_alive,
  1214                          oop* root, bool unloading_occurred) {
  1215   assert(root != NULL, "just checking");
  1216   oop obj = *root;
  1217   if (obj == NULL || is_alive->do_object_b(obj)) {
  1218       return false;
  1220   if (obj->is_compiledICHolder()) {
  1221     compiledICHolderOop cichk_oop = compiledICHolderOop(obj);
  1222     if (is_alive->do_object_b(
  1223           cichk_oop->holder_method()->method_holder()) &&
  1224         is_alive->do_object_b(cichk_oop->holder_klass())) {
  1225       // The oop should be kept alive
  1226       keep_alive->do_oop(root);
  1227       return false;
  1230   if (!UseParallelOldGC || !VerifyParallelOldWithMarkSweep) {
  1231     // Cannot do this test if verification of the UseParallelOldGC
  1232     // code using the PSMarkSweep code is being done.
  1233     assert(unloading_occurred, "Inconsistency in unloading");
  1235   make_unloaded(is_alive, obj);
  1236   return true;
  1239 // ------------------------------------------------------------------
  1240 // post_compiled_method_load_event
  1241 // new method for install_code() path
  1242 // Transfer information from compilation to jvmti
  1243 void nmethod::post_compiled_method_load_event() {
  1245   methodOop moop = method();
  1246   HS_DTRACE_PROBE8(hotspot, compiled__method__load,
  1247       moop->klass_name()->bytes(),
  1248       moop->klass_name()->utf8_length(),
  1249       moop->name()->bytes(),
  1250       moop->name()->utf8_length(),
  1251       moop->signature()->bytes(),
  1252       moop->signature()->utf8_length(),
  1253       code_begin(), code_size());
  1255   if (JvmtiExport::should_post_compiled_method_load()) {
  1256     JvmtiExport::post_compiled_method_load(this);
  1260 void nmethod::post_compiled_method_unload() {
  1261   assert(_method != NULL && !is_unloaded(), "just checking");
  1262   DTRACE_METHOD_UNLOAD_PROBE(method());
  1264   // If a JVMTI agent has enabled the CompiledMethodUnload event then
  1265   // post the event. Sometime later this nmethod will be made a zombie by
  1266   // the sweeper but the methodOop will not be valid at that point.
  1267   if (JvmtiExport::should_post_compiled_method_unload()) {
  1268     assert(!unload_reported(), "already unloaded");
  1269     HandleMark hm;
  1270     JvmtiExport::post_compiled_method_unload_at_safepoint(
  1271                       method()->jmethod_id(), code_begin());
  1274   // The JVMTI CompiledMethodUnload event can be enabled or disabled at
  1275   // any time. As the nmethod is being unloaded now we mark it has
  1276   // having the unload event reported - this will ensure that we don't
  1277   // attempt to report the event in the unlikely scenario where the
  1278   // event is enabled at the time the nmethod is made a zombie.
  1279   set_unload_reported();
  1282 // This is called at the end of the strong tracing/marking phase of a
  1283 // GC to unload an nmethod if it contains otherwise unreachable
  1284 // oops.
  1286 void nmethod::do_unloading(BoolObjectClosure* is_alive,
  1287                            OopClosure* keep_alive, bool unloading_occurred) {
  1288   // Make sure the oop's ready to receive visitors
  1289   assert(!is_zombie() && !is_unloaded(),
  1290          "should not call follow on zombie or unloaded nmethod");
  1292   // If the method is not entrant then a JMP is plastered over the
  1293   // first few bytes.  If an oop in the old code was there, that oop
  1294   // should not get GC'd.  Skip the first few bytes of oops on
  1295   // not-entrant methods.
  1296   address low_boundary = verified_entry_point();
  1297   if (is_not_entrant()) {
  1298     low_boundary += NativeJump::instruction_size;
  1299     // %%% Note:  On SPARC we patch only a 4-byte trap, not a full NativeJump.
  1300     // (See comment above.)
  1303   // The RedefineClasses() API can cause the class unloading invariant
  1304   // to no longer be true. See jvmtiExport.hpp for details.
  1305   // Also, leave a debugging breadcrumb in local flag.
  1306   bool a_class_was_redefined = JvmtiExport::has_redefined_a_class();
  1307   if (a_class_was_redefined) {
  1308     // This set of the unloading_occurred flag is done before the
  1309     // call to post_compiled_method_unload() so that the unloading
  1310     // of this nmethod is reported.
  1311     unloading_occurred = true;
  1314   // Follow methodOop
  1315   if (can_unload(is_alive, keep_alive, (oop*)&_method, unloading_occurred)) {
  1316     return;
  1319   // Exception cache
  1320   ExceptionCache* ec = exception_cache();
  1321   while (ec != NULL) {
  1322     oop* ex_addr = (oop*)ec->exception_type_addr();
  1323     oop ex = *ex_addr;
  1324     ExceptionCache* next_ec = ec->next();
  1325     if (ex != NULL && !is_alive->do_object_b(ex)) {
  1326       assert(!ex->is_compiledICHolder(), "Possible error here");
  1327       remove_from_exception_cache(ec);
  1329     ec = next_ec;
  1332   // If class unloading occurred we first iterate over all inline caches and
  1333   // clear ICs where the cached oop is referring to an unloaded klass or method.
  1334   // The remaining live cached oops will be traversed in the relocInfo::oop_type
  1335   // iteration below.
  1336   if (unloading_occurred) {
  1337     RelocIterator iter(this, low_boundary);
  1338     while(iter.next()) {
  1339       if (iter.type() == relocInfo::virtual_call_type) {
  1340         CompiledIC *ic = CompiledIC_at(iter.reloc());
  1341         oop ic_oop = ic->cached_oop();
  1342         if (ic_oop != NULL && !is_alive->do_object_b(ic_oop)) {
  1343           // The only exception is compiledICHolder oops which may
  1344           // yet be marked below. (We check this further below).
  1345           if (ic_oop->is_compiledICHolder()) {
  1346             compiledICHolderOop cichk_oop = compiledICHolderOop(ic_oop);
  1347             if (is_alive->do_object_b(
  1348                   cichk_oop->holder_method()->method_holder()) &&
  1349                 is_alive->do_object_b(cichk_oop->holder_klass())) {
  1350               continue;
  1353           ic->set_to_clean();
  1354           assert(ic->cached_oop() == NULL, "cached oop in IC should be cleared")
  1360   // Compiled code
  1361   RelocIterator iter(this, low_boundary);
  1362   while (iter.next()) {
  1363     if (iter.type() == relocInfo::oop_type) {
  1364       oop_Relocation* r = iter.oop_reloc();
  1365       // In this loop, we must only traverse those oops directly embedded in
  1366       // the code.  Other oops (oop_index>0) are seen as part of scopes_oops.
  1367       assert(1 == (r->oop_is_immediate()) +
  1368                   (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()),
  1369              "oop must be found in exactly one place");
  1370       if (r->oop_is_immediate() && r->oop_value() != NULL) {
  1371         if (can_unload(is_alive, keep_alive, r->oop_addr(), unloading_occurred)) {
  1372           return;
  1379   // Scopes
  1380   for (oop* p = oops_begin(); p < oops_end(); p++) {
  1381     if (*p == Universe::non_oop_word())  continue;  // skip non-oops
  1382     if (can_unload(is_alive, keep_alive, p, unloading_occurred)) {
  1383       return;
  1387 #ifndef PRODUCT
  1388   // This nmethod was not unloaded; check below that all CompiledICs
  1389   // refer to marked oops.
  1391     RelocIterator iter(this, low_boundary);
  1392     while (iter.next()) {
  1393       if (iter.type() == relocInfo::virtual_call_type) {
  1394          CompiledIC *ic = CompiledIC_at(iter.reloc());
  1395          oop ic_oop = ic->cached_oop();
  1396          assert(ic_oop == NULL || is_alive->do_object_b(ic_oop),
  1397                 "Found unmarked ic_oop in reachable nmethod");
  1401 #endif // !PRODUCT
  1404 void nmethod::oops_do(OopClosure* f) {
  1405   // make sure the oops ready to receive visitors
  1406   assert(!is_zombie() && !is_unloaded(),
  1407          "should not call follow on zombie or unloaded nmethod");
  1409   // If the method is not entrant or zombie then a JMP is plastered over the
  1410   // first few bytes.  If an oop in the old code was there, that oop
  1411   // should not get GC'd.  Skip the first few bytes of oops on
  1412   // not-entrant methods.
  1413   address low_boundary = verified_entry_point();
  1414   if (is_not_entrant()) {
  1415     low_boundary += NativeJump::instruction_size;
  1416     // %%% Note:  On SPARC we patch only a 4-byte trap, not a full NativeJump.
  1417     // (See comment above.)
  1420   // Compiled code
  1421   f->do_oop((oop*) &_method);
  1422   ExceptionCache* ec = exception_cache();
  1423   while(ec != NULL) {
  1424     f->do_oop((oop*)ec->exception_type_addr());
  1425     ec = ec->next();
  1428   RelocIterator iter(this, low_boundary);
  1429   while (iter.next()) {
  1430     if (iter.type() == relocInfo::oop_type ) {
  1431       oop_Relocation* r = iter.oop_reloc();
  1432       // In this loop, we must only follow those oops directly embedded in
  1433       // the code.  Other oops (oop_index>0) are seen as part of scopes_oops.
  1434       assert(1 == (r->oop_is_immediate()) + (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()), "oop must be found in exactly one place");
  1435       if (r->oop_is_immediate() && r->oop_value() != NULL) {
  1436         f->do_oop(r->oop_addr());
  1441   // Scopes
  1442   for (oop* p = oops_begin(); p < oops_end(); p++) {
  1443     if (*p == Universe::non_oop_word())  continue;  // skip non-oops
  1444     f->do_oop(p);
  1448 // Method that knows how to preserve outgoing arguments at call. This method must be
  1449 // called with a frame corresponding to a Java invoke
  1450 void nmethod::preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) {
  1451   if (!method()->is_native()) {
  1452     SimpleScopeDesc ssd(this, fr.pc());
  1453     Bytecode_invoke* call = Bytecode_invoke_at(ssd.method(), ssd.bci());
  1454     bool is_static = call->is_invokestatic();
  1455     symbolOop signature = call->signature();
  1456     fr.oops_compiled_arguments_do(signature, is_static, reg_map, f);
  1461 oop nmethod::embeddedOop_at(u_char* p) {
  1462   RelocIterator iter(this, p, p + oopSize);
  1463   while (iter.next())
  1464     if (iter.type() == relocInfo::oop_type) {
  1465       return iter.oop_reloc()->oop_value();
  1467   return NULL;
  1471 inline bool includes(void* p, void* from, void* to) {
  1472   return from <= p && p < to;
  1476 void nmethod::copy_scopes_pcs(PcDesc* pcs, int count) {
  1477   assert(count >= 2, "must be sentinel values, at least");
  1479 #ifdef ASSERT
  1480   // must be sorted and unique; we do a binary search in find_pc_desc()
  1481   int prev_offset = pcs[0].pc_offset();
  1482   assert(prev_offset == PcDesc::lower_offset_limit,
  1483          "must start with a sentinel");
  1484   for (int i = 1; i < count; i++) {
  1485     int this_offset = pcs[i].pc_offset();
  1486     assert(this_offset > prev_offset, "offsets must be sorted");
  1487     prev_offset = this_offset;
  1489   assert(prev_offset == PcDesc::upper_offset_limit,
  1490          "must end with a sentinel");
  1491 #endif //ASSERT
  1493   int size = count * sizeof(PcDesc);
  1494   assert(scopes_pcs_size() >= size, "oob");
  1495   memcpy(scopes_pcs_begin(), pcs, size);
  1497   // Adjust the final sentinel downward.
  1498   PcDesc* last_pc = &scopes_pcs_begin()[count-1];
  1499   assert(last_pc->pc_offset() == PcDesc::upper_offset_limit, "sanity");
  1500   last_pc->set_pc_offset(instructions_size() + 1);
  1501   for (; last_pc + 1 < scopes_pcs_end(); last_pc += 1) {
  1502     // Fill any rounding gaps with copies of the last record.
  1503     last_pc[1] = last_pc[0];
  1505   // The following assert could fail if sizeof(PcDesc) is not
  1506   // an integral multiple of oopSize (the rounding term).
  1507   // If it fails, change the logic to always allocate a multiple
  1508   // of sizeof(PcDesc), and fill unused words with copies of *last_pc.
  1509   assert(last_pc + 1 == scopes_pcs_end(), "must match exactly");
  1512 void nmethod::copy_scopes_data(u_char* buffer, int size) {
  1513   assert(scopes_data_size() >= size, "oob");
  1514   memcpy(scopes_data_begin(), buffer, size);
  1518 #ifdef ASSERT
  1519 static PcDesc* linear_search(nmethod* nm, int pc_offset, bool approximate) {
  1520   PcDesc* lower = nm->scopes_pcs_begin();
  1521   PcDesc* upper = nm->scopes_pcs_end();
  1522   lower += 1; // exclude initial sentinel
  1523   PcDesc* res = NULL;
  1524   for (PcDesc* p = lower; p < upper; p++) {
  1525     NOT_PRODUCT(--nmethod_stats.pc_desc_tests);  // don't count this call to match_desc
  1526     if (match_desc(p, pc_offset, approximate)) {
  1527       if (res == NULL)
  1528         res = p;
  1529       else
  1530         res = (PcDesc*) badAddress;
  1533   return res;
  1535 #endif
  1538 // Finds a PcDesc with real-pc equal to "pc"
  1539 PcDesc* nmethod::find_pc_desc_internal(address pc, bool approximate) {
  1540   address base_address = instructions_begin();
  1541   if ((pc < base_address) ||
  1542       (pc - base_address) >= (ptrdiff_t) PcDesc::upper_offset_limit) {
  1543     return NULL;  // PC is wildly out of range
  1545   int pc_offset = (int) (pc - base_address);
  1547   // Check the PcDesc cache if it contains the desired PcDesc
  1548   // (This as an almost 100% hit rate.)
  1549   PcDesc* res = _pc_desc_cache.find_pc_desc(pc_offset, approximate);
  1550   if (res != NULL) {
  1551     assert(res == linear_search(this, pc_offset, approximate), "cache ok");
  1552     return res;
  1555   // Fallback algorithm: quasi-linear search for the PcDesc
  1556   // Find the last pc_offset less than the given offset.
  1557   // The successor must be the required match, if there is a match at all.
  1558   // (Use a fixed radix to avoid expensive affine pointer arithmetic.)
  1559   PcDesc* lower = scopes_pcs_begin();
  1560   PcDesc* upper = scopes_pcs_end();
  1561   upper -= 1; // exclude final sentinel
  1562   if (lower >= upper)  return NULL;  // native method; no PcDescs at all
  1564 #define assert_LU_OK \
  1565   /* invariant on lower..upper during the following search: */ \
  1566   assert(lower->pc_offset() <  pc_offset, "sanity"); \
  1567   assert(upper->pc_offset() >= pc_offset, "sanity")
  1568   assert_LU_OK;
  1570   // Use the last successful return as a split point.
  1571   PcDesc* mid = _pc_desc_cache.last_pc_desc();
  1572   NOT_PRODUCT(++nmethod_stats.pc_desc_searches);
  1573   if (mid->pc_offset() < pc_offset) {
  1574     lower = mid;
  1575   } else {
  1576     upper = mid;
  1579   // Take giant steps at first (4096, then 256, then 16, then 1)
  1580   const int LOG2_RADIX = 4 /*smaller steps in debug mode:*/ debug_only(-1);
  1581   const int RADIX = (1 << LOG2_RADIX);
  1582   for (int step = (1 << (LOG2_RADIX*3)); step > 1; step >>= LOG2_RADIX) {
  1583     while ((mid = lower + step) < upper) {
  1584       assert_LU_OK;
  1585       NOT_PRODUCT(++nmethod_stats.pc_desc_searches);
  1586       if (mid->pc_offset() < pc_offset) {
  1587         lower = mid;
  1588       } else {
  1589         upper = mid;
  1590         break;
  1593     assert_LU_OK;
  1596   // Sneak up on the value with a linear search of length ~16.
  1597   while (true) {
  1598     assert_LU_OK;
  1599     mid = lower + 1;
  1600     NOT_PRODUCT(++nmethod_stats.pc_desc_searches);
  1601     if (mid->pc_offset() < pc_offset) {
  1602       lower = mid;
  1603     } else {
  1604       upper = mid;
  1605       break;
  1608 #undef assert_LU_OK
  1610   if (match_desc(upper, pc_offset, approximate)) {
  1611     assert(upper == linear_search(this, pc_offset, approximate), "search ok");
  1612     _pc_desc_cache.add_pc_desc(upper);
  1613     return upper;
  1614   } else {
  1615     assert(NULL == linear_search(this, pc_offset, approximate), "search ok");
  1616     return NULL;
  1621 bool nmethod::check_all_dependencies() {
  1622   bool found_check = false;
  1623   // wholesale check of all dependencies
  1624   for (Dependencies::DepStream deps(this); deps.next(); ) {
  1625     if (deps.check_dependency() != NULL) {
  1626       found_check = true;
  1627       NOT_DEBUG(break);
  1630   return found_check;  // tell caller if we found anything
  1633 bool nmethod::check_dependency_on(DepChange& changes) {
  1634   // What has happened:
  1635   // 1) a new class dependee has been added
  1636   // 2) dependee and all its super classes have been marked
  1637   bool found_check = false;  // set true if we are upset
  1638   for (Dependencies::DepStream deps(this); deps.next(); ) {
  1639     // Evaluate only relevant dependencies.
  1640     if (deps.spot_check_dependency_at(changes) != NULL) {
  1641       found_check = true;
  1642       NOT_DEBUG(break);
  1645   return found_check;
  1648 bool nmethod::is_evol_dependent_on(klassOop dependee) {
  1649   instanceKlass *dependee_ik = instanceKlass::cast(dependee);
  1650   objArrayOop dependee_methods = dependee_ik->methods();
  1651   for (Dependencies::DepStream deps(this); deps.next(); ) {
  1652     if (deps.type() == Dependencies::evol_method) {
  1653       methodOop method = deps.method_argument(0);
  1654       for (int j = 0; j < dependee_methods->length(); j++) {
  1655         if ((methodOop) dependee_methods->obj_at(j) == method) {
  1656           // RC_TRACE macro has an embedded ResourceMark
  1657           RC_TRACE(0x01000000,
  1658             ("Found evol dependency of nmethod %s.%s(%s) compile_id=%d on method %s.%s(%s)",
  1659             _method->method_holder()->klass_part()->external_name(),
  1660             _method->name()->as_C_string(),
  1661             _method->signature()->as_C_string(), compile_id(),
  1662             method->method_holder()->klass_part()->external_name(),
  1663             method->name()->as_C_string(),
  1664             method->signature()->as_C_string()));
  1665           if (TraceDependencies || LogCompilation)
  1666             deps.log_dependency(dependee);
  1667           return true;
  1672   return false;
  1675 // Called from mark_for_deoptimization, when dependee is invalidated.
  1676 bool nmethod::is_dependent_on_method(methodOop dependee) {
  1677   for (Dependencies::DepStream deps(this); deps.next(); ) {
  1678     if (deps.type() != Dependencies::evol_method)
  1679       continue;
  1680     methodOop method = deps.method_argument(0);
  1681     if (method == dependee) return true;
  1683   return false;
  1687 bool nmethod::is_patchable_at(address instr_addr) {
  1688   assert (code_contains(instr_addr), "wrong nmethod used");
  1689   if (is_zombie()) {
  1690     // a zombie may never be patched
  1691     return false;
  1693   return true;
  1697 address nmethod::continuation_for_implicit_exception(address pc) {
  1698   // Exception happened outside inline-cache check code => we are inside
  1699   // an active nmethod => use cpc to determine a return address
  1700   int exception_offset = pc - instructions_begin();
  1701   int cont_offset = ImplicitExceptionTable(this).at( exception_offset );
  1702 #ifdef ASSERT
  1703   if (cont_offset == 0) {
  1704     Thread* thread = ThreadLocalStorage::get_thread_slow();
  1705     ResetNoHandleMark rnm; // Might be called from LEAF/QUICK ENTRY
  1706     HandleMark hm(thread);
  1707     ResourceMark rm(thread);
  1708     CodeBlob* cb = CodeCache::find_blob(pc);
  1709     assert(cb != NULL && cb == this, "");
  1710     tty->print_cr("implicit exception happened at " INTPTR_FORMAT, pc);
  1711     print();
  1712     method()->print_codes();
  1713     print_code();
  1714     print_pcs();
  1716 #endif
  1717   guarantee(cont_offset != 0, "unhandled implicit exception in compiled code");
  1718   return instructions_begin() + cont_offset;
  1723 void nmethod_init() {
  1724   // make sure you didn't forget to adjust the filler fields
  1725   assert(sizeof(nmFlags) <= 4,           "nmFlags occupies more than a word");
  1726   assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
  1730 //-------------------------------------------------------------------------------------------
  1733 // QQQ might we make this work from a frame??
  1734 nmethodLocker::nmethodLocker(address pc) {
  1735   CodeBlob* cb = CodeCache::find_blob(pc);
  1736   guarantee(cb != NULL && cb->is_nmethod(), "bad pc for a nmethod found");
  1737   _nm = (nmethod*)cb;
  1738   lock_nmethod(_nm);
  1741 void nmethodLocker::lock_nmethod(nmethod* nm) {
  1742   if (nm == NULL)  return;
  1743   Atomic::inc(&nm->_lock_count);
  1744   guarantee(!nm->is_zombie(), "cannot lock a zombie method");
  1747 void nmethodLocker::unlock_nmethod(nmethod* nm) {
  1748   if (nm == NULL)  return;
  1749   Atomic::dec(&nm->_lock_count);
  1750   guarantee(nm->_lock_count >= 0, "unmatched nmethod lock/unlock");
  1753 bool nmethod::is_deopt_pc(address pc) {
  1754   bool ret =  pc == deopt_handler_begin();
  1755   return ret;
  1759 // -----------------------------------------------------------------------------
  1760 // Verification
  1762 void nmethod::verify() {
  1764   // Hmm. OSR methods can be deopted but not marked as zombie or not_entrant
  1765   // seems odd.
  1767   if( is_zombie() || is_not_entrant() )
  1768     return;
  1770   // Make sure all the entry points are correctly aligned for patching.
  1771   NativeJump::check_verified_entry_alignment(entry_point(), verified_entry_point());
  1773   assert(method()->is_oop(), "must be valid");
  1775   ResourceMark rm;
  1777   if (!CodeCache::contains(this)) {
  1778     fatal1("nmethod at " INTPTR_FORMAT " not in zone", this);
  1781   if(is_native_method() )
  1782     return;
  1784   nmethod* nm = CodeCache::find_nmethod(verified_entry_point());
  1785   if (nm != this) {
  1786     fatal1("findNMethod did not find this nmethod (" INTPTR_FORMAT ")", this);
  1789   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
  1790     if (! p->verify(this)) {
  1791       tty->print_cr("\t\tin nmethod at " INTPTR_FORMAT " (pcs)", this);
  1795   verify_scopes();
  1799 void nmethod::verify_interrupt_point(address call_site) {
  1800   // This code does not work in release mode since
  1801   // owns_lock only is available in debug mode.
  1802   CompiledIC* ic = NULL;
  1803   Thread *cur = Thread::current();
  1804   if (CompiledIC_lock->owner() == cur ||
  1805       ((cur->is_VM_thread() || cur->is_ConcurrentGC_thread()) &&
  1806        SafepointSynchronize::is_at_safepoint())) {
  1807     ic = CompiledIC_at(call_site);
  1808     CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
  1809   } else {
  1810     MutexLocker ml_verify (CompiledIC_lock);
  1811     ic = CompiledIC_at(call_site);
  1813   PcDesc* pd = pc_desc_at(ic->end_of_call());
  1814   assert(pd != NULL, "PcDesc must exist");
  1815   for (ScopeDesc* sd = new ScopeDesc(this, pd->scope_decode_offset(),
  1816                                      pd->obj_decode_offset());
  1817        !sd->is_top(); sd = sd->sender()) {
  1818     sd->verify();
  1822 void nmethod::verify_scopes() {
  1823   if( !method() ) return;       // Runtime stubs have no scope
  1824   if (method()->is_native()) return; // Ignore stub methods.
  1825   // iterate through all interrupt point
  1826   // and verify the debug information is valid.
  1827   RelocIterator iter((nmethod*)this);
  1828   while (iter.next()) {
  1829     address stub = NULL;
  1830     switch (iter.type()) {
  1831       case relocInfo::virtual_call_type:
  1832         verify_interrupt_point(iter.addr());
  1833         break;
  1834       case relocInfo::opt_virtual_call_type:
  1835         stub = iter.opt_virtual_call_reloc()->static_stub();
  1836         verify_interrupt_point(iter.addr());
  1837         break;
  1838       case relocInfo::static_call_type:
  1839         stub = iter.static_call_reloc()->static_stub();
  1840         //verify_interrupt_point(iter.addr());
  1841         break;
  1842       case relocInfo::runtime_call_type:
  1843         address destination = iter.reloc()->value();
  1844         // Right now there is no way to find out which entries support
  1845         // an interrupt point.  It would be nice if we had this
  1846         // information in a table.
  1847         break;
  1849     assert(stub == NULL || stub_contains(stub), "static call stub outside stub section");
  1854 // -----------------------------------------------------------------------------
  1855 // Non-product code
  1856 #ifndef PRODUCT
  1858 void nmethod::check_store() {
  1859   // Make sure all oops in the compiled code are tenured
  1861   RelocIterator iter(this);
  1862   while (iter.next()) {
  1863     if (iter.type() == relocInfo::oop_type) {
  1864       oop_Relocation* reloc = iter.oop_reloc();
  1865       oop obj = reloc->oop_value();
  1866       if (obj != NULL && !obj->is_perm()) {
  1867         fatal("must be permanent oop in compiled code");
  1873 #endif // PRODUCT
  1875 // Printing operations
  1877 void nmethod::print() const {
  1878   ResourceMark rm;
  1879   ttyLocker ttyl;   // keep the following output all in one block
  1881   tty->print("Compiled ");
  1883   if (is_compiled_by_c1()) {
  1884     tty->print("(c1) ");
  1885   } else if (is_compiled_by_c2()) {
  1886     tty->print("(c2) ");
  1887   } else {
  1888     assert(is_native_method(), "Who else?");
  1889     tty->print("(nm) ");
  1892   print_on(tty, "nmethod");
  1893   tty->cr();
  1894   if (WizardMode) {
  1895     tty->print("((nmethod*) "INTPTR_FORMAT ") ", this);
  1896     tty->print(" for method " INTPTR_FORMAT , (address)method());
  1897     tty->print(" { ");
  1898     if (version())        tty->print("v%d ", version());
  1899     if (level())          tty->print("l%d ", level());
  1900     if (is_in_use())      tty->print("in_use ");
  1901     if (is_not_entrant()) tty->print("not_entrant ");
  1902     if (is_zombie())      tty->print("zombie ");
  1903     if (is_unloaded())    tty->print("unloaded ");
  1904     tty->print_cr("}:");
  1906   if (size              () > 0) tty->print_cr(" total in heap  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
  1907                                               (address)this,
  1908                                               (address)this + size(),
  1909                                               size());
  1910   if (relocation_size   () > 0) tty->print_cr(" relocation     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
  1911                                               relocation_begin(),
  1912                                               relocation_end(),
  1913                                               relocation_size());
  1914   if (code_size         () > 0) tty->print_cr(" main code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
  1915                                               code_begin(),
  1916                                               code_end(),
  1917                                               code_size());
  1918   if (stub_size         () > 0) tty->print_cr(" stub code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
  1919                                               stub_begin(),
  1920                                               stub_end(),
  1921                                               stub_size());
  1922   if (consts_size       () > 0) tty->print_cr(" constants      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
  1923                                               consts_begin(),
  1924                                               consts_end(),
  1925                                               consts_size());
  1926   if (scopes_data_size  () > 0) tty->print_cr(" scopes data    [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
  1927                                               scopes_data_begin(),
  1928                                               scopes_data_end(),
  1929                                               scopes_data_size());
  1930   if (scopes_pcs_size   () > 0) tty->print_cr(" scopes pcs     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
  1931                                               scopes_pcs_begin(),
  1932                                               scopes_pcs_end(),
  1933                                               scopes_pcs_size());
  1934   if (dependencies_size () > 0) tty->print_cr(" dependencies   [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
  1935                                               dependencies_begin(),
  1936                                               dependencies_end(),
  1937                                               dependencies_size());
  1938   if (handler_table_size() > 0) tty->print_cr(" handler table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
  1939                                               handler_table_begin(),
  1940                                               handler_table_end(),
  1941                                               handler_table_size());
  1942   if (nul_chk_table_size() > 0) tty->print_cr(" nul chk table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
  1943                                               nul_chk_table_begin(),
  1944                                               nul_chk_table_end(),
  1945                                               nul_chk_table_size());
  1946   if (oops_size         () > 0) tty->print_cr(" oops           [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
  1947                                               oops_begin(),
  1948                                               oops_end(),
  1949                                               oops_size());
  1952 void nmethod::print_code() {
  1953   HandleMark hm;
  1954   ResourceMark m;
  1955   Disassembler::decode(this);
  1959 #ifndef PRODUCT
  1961 void nmethod::print_scopes() {
  1962   // Find the first pc desc for all scopes in the code and print it.
  1963   ResourceMark rm;
  1964   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
  1965     if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null)
  1966       continue;
  1968     ScopeDesc* sd = scope_desc_at(p->real_pc(this));
  1969     sd->print_on(tty, p);
  1973 void nmethod::print_dependencies() {
  1974   ResourceMark rm;
  1975   ttyLocker ttyl;   // keep the following output all in one block
  1976   tty->print_cr("Dependencies:");
  1977   for (Dependencies::DepStream deps(this); deps.next(); ) {
  1978     deps.print_dependency();
  1979     klassOop ctxk = deps.context_type();
  1980     if (ctxk != NULL) {
  1981       Klass* k = Klass::cast(ctxk);
  1982       if (k->oop_is_instance() && ((instanceKlass*)k)->is_dependent_nmethod(this)) {
  1983         tty->print_cr("   [nmethod<=klass]%s", k->external_name());
  1986     deps.log_dependency();  // put it into the xml log also
  1991 void nmethod::print_relocations() {
  1992   ResourceMark m;       // in case methods get printed via the debugger
  1993   tty->print_cr("relocations:");
  1994   RelocIterator iter(this);
  1995   iter.print();
  1996   if (UseRelocIndex) {
  1997     jint* index_end   = (jint*)relocation_end() - 1;
  1998     jint  index_size  = *index_end;
  1999     jint* index_start = (jint*)( (address)index_end - index_size );
  2000     tty->print_cr("    index @" INTPTR_FORMAT ": index_size=%d", index_start, index_size);
  2001     if (index_size > 0) {
  2002       jint* ip;
  2003       for (ip = index_start; ip+2 <= index_end; ip += 2)
  2004         tty->print_cr("  (%d %d) addr=" INTPTR_FORMAT " @" INTPTR_FORMAT,
  2005                       ip[0],
  2006                       ip[1],
  2007                       header_end()+ip[0],
  2008                       relocation_begin()-1+ip[1]);
  2009       for (; ip < index_end; ip++)
  2010         tty->print_cr("  (%d ?)", ip[0]);
  2011       tty->print_cr("          @" INTPTR_FORMAT ": index_size=%d", ip, *ip++);
  2012       tty->print_cr("reloc_end @" INTPTR_FORMAT ":", ip);
  2018 void nmethod::print_pcs() {
  2019   ResourceMark m;       // in case methods get printed via debugger
  2020   tty->print_cr("pc-bytecode offsets:");
  2021   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
  2022     p->print(this);
  2026 #endif // PRODUCT
  2028 const char* nmethod::reloc_string_for(u_char* begin, u_char* end) {
  2029   RelocIterator iter(this, begin, end);
  2030   bool have_one = false;
  2031   while (iter.next()) {
  2032     have_one = true;
  2033     switch (iter.type()) {
  2034         case relocInfo::none:                  return "no_reloc";
  2035         case relocInfo::oop_type: {
  2036           stringStream st;
  2037           oop_Relocation* r = iter.oop_reloc();
  2038           oop obj = r->oop_value();
  2039           st.print("oop(");
  2040           if (obj == NULL) st.print("NULL");
  2041           else obj->print_value_on(&st);
  2042           st.print(")");
  2043           return st.as_string();
  2045         case relocInfo::virtual_call_type:     return "virtual_call";
  2046         case relocInfo::opt_virtual_call_type: return "optimized virtual_call";
  2047         case relocInfo::static_call_type:      return "static_call";
  2048         case relocInfo::static_stub_type:      return "static_stub";
  2049         case relocInfo::runtime_call_type:     return "runtime_call";
  2050         case relocInfo::external_word_type:    return "external_word";
  2051         case relocInfo::internal_word_type:    return "internal_word";
  2052         case relocInfo::section_word_type:     return "section_word";
  2053         case relocInfo::poll_type:             return "poll";
  2054         case relocInfo::poll_return_type:      return "poll_return";
  2055         case relocInfo::type_mask:             return "type_bit_mask";
  2058   return have_one ? "other" : NULL;
  2061 // Return a the last scope in (begin..end]
  2062 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
  2063   PcDesc* p = pc_desc_near(begin+1);
  2064   if (p != NULL && p->real_pc(this) <= end) {
  2065     return new ScopeDesc(this, p->scope_decode_offset(),
  2066                          p->obj_decode_offset());
  2068   return NULL;
  2071 void nmethod::print_code_comment_on(outputStream* st, int column, u_char* begin, u_char* end) {
  2072   // First, find an oopmap in (begin, end].
  2073   // We use the odd half-closed interval so that oop maps and scope descs
  2074   // which are tied to the byte after a call are printed with the call itself.
  2075   address base = instructions_begin();
  2076   OopMapSet* oms = oop_maps();
  2077   if (oms != NULL) {
  2078     for (int i = 0, imax = oms->size(); i < imax; i++) {
  2079       OopMap* om = oms->at(i);
  2080       address pc = base + om->offset();
  2081       if (pc > begin) {
  2082         if (pc <= end) {
  2083           st->move_to(column);
  2084           st->print("; ");
  2085           om->print_on(st);
  2087         break;
  2092   // Print any debug info present at this pc.
  2093   ScopeDesc* sd  = scope_desc_in(begin, end);
  2094   if (sd != NULL) {
  2095     st->move_to(column);
  2096     if (sd->bci() == SynchronizationEntryBCI) {
  2097       st->print(";*synchronization entry");
  2098     } else {
  2099       if (sd->method().is_null()) {
  2100         st->print("method is NULL");
  2101       } else if (sd->method()->is_native()) {
  2102         st->print("method is native");
  2103       } else {
  2104         address bcp  = sd->method()->bcp_from(sd->bci());
  2105         Bytecodes::Code bc = Bytecodes::java_code_at(bcp);
  2106         st->print(";*%s", Bytecodes::name(bc));
  2107         switch (bc) {
  2108         case Bytecodes::_invokevirtual:
  2109         case Bytecodes::_invokespecial:
  2110         case Bytecodes::_invokestatic:
  2111         case Bytecodes::_invokeinterface:
  2113             Bytecode_invoke* invoke = Bytecode_invoke_at(sd->method(), sd->bci());
  2114             st->print(" ");
  2115             if (invoke->name() != NULL)
  2116               invoke->name()->print_symbol_on(st);
  2117             else
  2118               st->print("<UNKNOWN>");
  2119             break;
  2121         case Bytecodes::_getfield:
  2122         case Bytecodes::_putfield:
  2123         case Bytecodes::_getstatic:
  2124         case Bytecodes::_putstatic:
  2126             methodHandle sdm = sd->method();
  2127             Bytecode_field* field = Bytecode_field_at(sdm(), sdm->bcp_from(sd->bci()));
  2128             constantPoolOop sdmc = sdm->constants();
  2129             symbolOop name = sdmc->name_ref_at(field->index());
  2130             st->print(" ");
  2131             if (name != NULL)
  2132               name->print_symbol_on(st);
  2133             else
  2134               st->print("<UNKNOWN>");
  2140     // Print all scopes
  2141     for (;sd != NULL; sd = sd->sender()) {
  2142       st->move_to(column);
  2143       st->print("; -");
  2144       if (sd->method().is_null()) {
  2145         st->print("method is NULL");
  2146       } else {
  2147         sd->method()->print_short_name(st);
  2149       int lineno = sd->method()->line_number_from_bci(sd->bci());
  2150       if (lineno != -1) {
  2151         st->print("@%d (line %d)", sd->bci(), lineno);
  2152       } else {
  2153         st->print("@%d", sd->bci());
  2155       st->cr();
  2159   // Print relocation information
  2160   const char* str = reloc_string_for(begin, end);
  2161   if (str != NULL) {
  2162     if (sd != NULL) st->cr();
  2163     st->move_to(column);
  2164     st->print(";   {%s}", str);
  2166   int cont_offset = ImplicitExceptionTable(this).at(begin - instructions_begin());
  2167   if (cont_offset != 0) {
  2168     st->move_to(column);
  2169     st->print("; implicit exception: dispatches to " INTPTR_FORMAT, instructions_begin() + cont_offset);
  2174 #ifndef PRODUCT
  2176 void nmethod::print_value_on(outputStream* st) const {
  2177   print_on(st, "nmethod");
  2180 void nmethod::print_calls(outputStream* st) {
  2181   RelocIterator iter(this);
  2182   while (iter.next()) {
  2183     switch (iter.type()) {
  2184     case relocInfo::virtual_call_type:
  2185     case relocInfo::opt_virtual_call_type: {
  2186       VerifyMutexLocker mc(CompiledIC_lock);
  2187       CompiledIC_at(iter.reloc())->print();
  2188       break;
  2190     case relocInfo::static_call_type:
  2191       st->print_cr("Static call at " INTPTR_FORMAT, iter.reloc()->addr());
  2192       compiledStaticCall_at(iter.reloc())->print();
  2193       break;
  2198 void nmethod::print_handler_table() {
  2199   ExceptionHandlerTable(this).print();
  2202 void nmethod::print_nul_chk_table() {
  2203   ImplicitExceptionTable(this).print(instructions_begin());
  2206 void nmethod::print_statistics() {
  2207   ttyLocker ttyl;
  2208   if (xtty != NULL)  xtty->head("statistics type='nmethod'");
  2209   nmethod_stats.print_native_nmethod_stats();
  2210   nmethod_stats.print_nmethod_stats();
  2211   DebugInformationRecorder::print_statistics();
  2212   nmethod_stats.print_pc_stats();
  2213   Dependencies::print_statistics();
  2214   if (xtty != NULL)  xtty->tail("statistics");
  2217 #endif // PRODUCT

mercurial