< prev index next >

src/hotspot/share/oops/method.cpp

Print this page

  43 #include "interpreter/oopMapCache.hpp"
  44 #include "logging/log.hpp"
  45 #include "logging/logStream.hpp"
  46 #include "logging/logTag.hpp"
  47 #include "memory/allocation.inline.hpp"
  48 #include "memory/metadataFactory.hpp"
  49 #include "memory/metaspaceClosure.hpp"
  50 #include "memory/oopFactory.hpp"
  51 #include "memory/resourceArea.hpp"
  52 #include "memory/universe.hpp"
  53 #include "nmt/memTracker.hpp"
  54 #include "oops/constMethod.hpp"
  55 #include "oops/constantPool.hpp"
  56 #include "oops/klass.inline.hpp"
  57 #include "oops/method.inline.hpp"
  58 #include "oops/methodData.hpp"
  59 #include "oops/objArrayKlass.hpp"
  60 #include "oops/objArrayOop.inline.hpp"
  61 #include "oops/oop.inline.hpp"
  62 #include "oops/symbol.hpp"

  63 #include "prims/jvmtiExport.hpp"
  64 #include "prims/methodHandles.hpp"
  65 #include "runtime/atomic.hpp"
  66 #include "runtime/arguments.hpp"
  67 #include "runtime/continuationEntry.hpp"
  68 #include "runtime/frame.inline.hpp"
  69 #include "runtime/handles.inline.hpp"
  70 #include "runtime/init.hpp"
  71 #include "runtime/java.hpp"
  72 #include "runtime/orderAccess.hpp"
  73 #include "runtime/perfData.hpp"
  74 #include "runtime/relocator.hpp"
  75 #include "runtime/safepointVerifiers.hpp"
  76 #include "runtime/sharedRuntime.hpp"
  77 #include "runtime/signature.hpp"
  78 #include "runtime/threads.hpp"
  79 #include "runtime/vm_version.hpp"
  80 #include "utilities/align.hpp"
  81 #include "utilities/quickSort.hpp"
  82 #include "utilities/vmError.hpp"

 164 }
 165 
 166 address Method::get_c2i_no_clinit_check_entry() {
 167   assert(VM_Version::supports_fast_class_init_checks(), "");
 168   assert(adapter() != nullptr, "must have");
 169   return adapter()->get_c2i_no_clinit_check_entry();
 170 }
 171 
 172 char* Method::name_and_sig_as_C_string() const {
 173   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
 174 }
 175 
 176 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
 177   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
 178 }
 179 
 180 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
 181   const char* klass_name = klass->external_name();
 182   int klass_name_len  = (int)strlen(klass_name);
 183   int method_name_len = method_name->utf8_length();
 184   int len             = klass_name_len + 1 + method_name_len + signature->utf8_length();
 185   char* dest          = NEW_RESOURCE_ARRAY(char, len + 1);
 186   strcpy(dest, klass_name);
 187   dest[klass_name_len] = '.';
 188   strcpy(&dest[klass_name_len + 1], method_name->as_C_string());
 189   strcpy(&dest[klass_name_len + 1 + method_name_len], signature->as_C_string());

 190   dest[len] = 0;
 191   return dest;
 192 }
 193 
 194 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size) {
 195   Symbol* klass_name = klass->name();
 196   klass_name->as_klass_external_name(buf, size);
 197   int len = (int)strlen(buf);
 198 
 199   if (len < size - 1) {
 200     buf[len++] = '.';
 201 
 202     method_name->as_C_string(&(buf[len]), size - len);
 203     len = (int)strlen(buf);
 204 
 205     signature->as_C_string(&(buf[len]), size - len);
 206   }
 207 
 208   return buf;
 209 }

 368 address Method::bcp_from(address bcp) const {
 369   if (is_native() && bcp == nullptr) {
 370     return code_base();
 371   } else {
 372     return bcp;
 373   }
 374 }
 375 
 376 int Method::size(bool is_native) {
 377   // If native, then include pointers for native_function and signature_handler
 378   int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
 379   int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
 380   return align_metadata_size(header_size() + extra_words);
 381 }
 382 
 383 Symbol* Method::klass_name() const {
 384   return method_holder()->name();
 385 }
 386 
 387 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
 388   log_trace(cds)("Iter(Method): %p", this);
 389 
 390   if (!method_holder()->is_rewritten()) {





 391     it->push(&_constMethod, MetaspaceClosure::_writable);
 392   } else {
 393     it->push(&_constMethod);
 394   }
 395   it->push(&_method_data);
 396   it->push(&_method_counters);
 397   NOT_PRODUCT(it->push(&_name);)
 398 }
 399 
 400 #if INCLUDE_CDS
 401 // Attempt to return method to original state.  Clear any pointers
 402 // (to objects outside the shared spaces).  We won't be able to predict
 403 // where they should point in a new JVM.  Further initialize some
 404 // entries now in order allow them to be write protected later.
 405 
 406 void Method::remove_unshareable_info() {
 407   unlink_method();






 408   JFR_ONLY(REMOVE_METHOD_ID(this);)
 409 }
 410 
 411 void Method::restore_unshareable_info(TRAPS) {
 412   assert(is_method() && is_valid_method(this), "ensure C++ vtable is restored");






 413   assert(!queued_for_compilation(), "method's queued_for_compilation flag should not be set");

 414 }
 415 #endif
 416 
 417 void Method::set_vtable_index(int index) {
 418   if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
 419     // At runtime initialize_vtable is rerun as part of link_class_impl()
 420     // for a shared class loaded by the non-boot loader to obtain the loader
 421     // constraints based on the runtime classloaders' context.
 422     return; // don't write into the shared class
 423   } else {
 424     _vtable_index = index;
 425   }
 426 }
 427 
 428 void Method::set_itable_index(int index) {
 429   if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
 430     // At runtime initialize_itable is rerun as part of link_class_impl()
 431     // for a shared class loaded by the non-boot loader to obtain the loader
 432     // constraints based on the runtime classloaders' context. The dumptime
 433     // itable index should be the same as the runtime index.

 560   // Counting based on signed int counters tends to overflow with
 561   // longer-running workloads on fast machines. The counters under
 562   // consideration here, however, are limited in range by counting
 563   // logic. See InvocationCounter:count_limit for example.
 564   // No "overflow precautions" need to be implemented here.
 565   st->print_cr ("  interpreter_invocation_count: " INT32_FORMAT_W(11), interpreter_invocation_count());
 566   st->print_cr ("  invocation_counter:           " INT32_FORMAT_W(11), invocation_count());
 567   st->print_cr ("  backedge_counter:             " INT32_FORMAT_W(11), backedge_count());
 568 
 569   if (method_data() != nullptr) {
 570     st->print_cr ("  decompile_count:              " UINT32_FORMAT_W(11), method_data()->decompile_count());
 571   }
 572 
 573 #ifndef PRODUCT
 574   if (CountCompiledCalls) {
 575     st->print_cr ("  compiled_invocation_count:    " INT64_FORMAT_W(11), compiled_invocation_count());
 576   }
 577 #endif
 578 }
 579 




























 580 // Build a MethodData* object to hold profiling information collected on this
 581 // method when requested.
 582 void Method::build_profiling_method_data(const methodHandle& method, TRAPS) {



 583   // Do not profile the method if metaspace has hit an OOM previously
 584   // allocating profiling data. Callers clear pending exception so don't
 585   // add one here.
 586   if (ClassLoaderDataGraph::has_metaspace_oom()) {
 587     return;
 588   }
 589 
 590   ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
 591   MethodData* method_data = MethodData::allocate(loader_data, method, THREAD);
 592   if (HAS_PENDING_EXCEPTION) {
 593     CompileBroker::log_metaspace_failure();
 594     ClassLoaderDataGraph::set_metaspace_oom(true);
 595     return;   // return the exception (which is cleared)
 596   }
 597 
 598   if (!Atomic::replace_if_null(&method->_method_data, method_data)) {
 599     MetadataFactory::free_metadata(loader_data, method_data);
 600     return;
 601   }
 602 
 603   if (PrintMethodData && (Verbose || WizardMode)) {













 604     ResourceMark rm(THREAD);
 605     tty->print("build_profiling_method_data for ");
 606     method->print_name(tty);
 607     tty->cr();
 608     // At the end of the run, the MDO, full of data, will be dumped.
 609   }
 610 }
 611 
 612 MethodCounters* Method::build_method_counters(Thread* current, Method* m) {
 613   // Do not profile the method if metaspace has hit an OOM previously
 614   if (ClassLoaderDataGraph::has_metaspace_oom()) {
 615     return nullptr;
 616   }
 617 
 618   methodHandle mh(current, m);
 619   MethodCounters* counters;
 620   if (current->is_Java_thread()) {
 621     JavaThread* THREAD = JavaThread::cast(current); // For exception macros.
 622     // Use the TRAPS version for a JavaThread so it will adjust the GC threshold
 623     // if needed.
 624     counters = MethodCounters::allocate_with_exception(mh, THREAD);
 625     if (HAS_PENDING_EXCEPTION) {
 626       CLEAR_PENDING_EXCEPTION;
 627     }
 628   } else {
 629     // Call metaspace allocation that doesn't throw exception if the
 630     // current thread isn't a JavaThread, ie. the VMThread.
 631     counters = MethodCounters::allocate_no_exception(mh);
 632   }
 633 
 634   if (counters == nullptr) {
 635     CompileBroker::log_metaspace_failure();
 636     ClassLoaderDataGraph::set_metaspace_oom(true);
 637     return nullptr;
 638   }
 639 
 640   if (!mh->init_method_counters(counters)) {
 641     MetadataFactory::free_metadata(mh->method_holder()->class_loader_data(), counters);
 642   }
 643 





 644   return mh->method_counters();
 645 }
 646 
 647 bool Method::init_method_counters(MethodCounters* counters) {
 648   // Try to install a pointer to MethodCounters, return true on success.
 649   return Atomic::replace_if_null(&_method_counters, counters);
 650 }
 651 
 652 void Method::set_exception_handler_entered(int handler_bci) {
 653   if (ProfileExceptionHandlers) {
 654     MethodData* mdo = method_data();
 655     if (mdo != nullptr) {
 656       BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
 657       handler_data.set_exception_handler_entered();
 658     }
 659   }
 660 }
 661 
 662 int Method::extra_stack_words() {
 663   // not an inline function, to avoid a header dependency on Interpreter

 850   return (is_static() ||
 851           method_holder()->major_version() < 51);
 852 }
 853 
 854 bool Method::is_static_initializer() const {
 855   // For classfiles version 51 or greater, ensure that the clinit method is
 856   // static.  Non-static methods with the name "<clinit>" are not static
 857   // initializers. (older classfiles exempted for backward compatibility)
 858   return name() == vmSymbols::class_initializer_name() &&
 859          has_valid_initializer_flags();
 860 }
 861 
 862 bool Method::is_object_initializer() const {
 863    return name() == vmSymbols::object_initializer_name();
 864 }
 865 
 866 bool Method::needs_clinit_barrier() const {
 867   return is_static() && !method_holder()->is_initialized();
 868 }
 869 





 870 bool Method::is_object_wait0() const {
 871   return klass_name() == vmSymbols::java_lang_Object()
 872          && name() == vmSymbols::wait_name();
 873 }
 874 
 875 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
 876   int length = method->checked_exceptions_length();
 877   if (length == 0) {  // common case
 878     return objArrayHandle(THREAD, Universe::the_empty_class_array());
 879   } else {
 880     methodHandle h_this(THREAD, method);
 881     objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
 882     objArrayHandle mirrors (THREAD, m_oop);
 883     for (int i = 0; i < length; i++) {
 884       CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
 885       Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
 886       if (log_is_enabled(Warning, exceptions) &&
 887           !k->is_subclass_of(vmClasses::Throwable_klass())) {
 888         ResourceMark rm(THREAD);
 889         log_warning(exceptions)(

1134 }
1135 
1136 #if INCLUDE_CDS
1137 // Called by class data sharing to remove any entry points (which are not shared)
1138 void Method::unlink_method() {
1139   assert(CDSConfig::is_dumping_archive(), "sanity");
1140   _code = nullptr;
1141   _adapter = nullptr;
1142   _i2i_entry = nullptr;
1143   _from_compiled_entry = nullptr;
1144   _from_interpreted_entry = nullptr;
1145 
1146   if (is_native()) {
1147     *native_function_addr() = nullptr;
1148     set_signature_handler(nullptr);
1149   }
1150   NOT_PRODUCT(set_compiled_invocation_count(0);)
1151 
1152   clear_method_data();
1153   clear_method_counters();






1154   remove_unshareable_flags();
1155 }
1156 
1157 void Method::remove_unshareable_flags() {
1158   // clear all the flags that shouldn't be in the archived version
1159   assert(!is_old(), "must be");
1160   assert(!is_obsolete(), "must be");
1161   assert(!is_deleted(), "must be");
1162 
1163   set_is_prefixed_native(false);
1164   set_queued_for_compilation(false);

1165   set_is_not_c2_compilable(false);
1166   set_is_not_c1_compilable(false);
1167   set_is_not_c2_osr_compilable(false);
1168   set_on_stack_flag(false);


1169 }
1170 #endif
1171 
1172 // Called when the method_holder is getting linked. Setup entrypoints so the method
1173 // is ready to be called from interpreter, compiler, and vtables.
1174 void Method::link_method(const methodHandle& h_method, TRAPS) {
1175   if (log_is_enabled(Info, perf, class, link)) {
1176     ClassLoader::perf_ik_link_methods_count()->inc();
1177   }
1178 
1179   // If the code cache is full, we may reenter this function for the
1180   // leftover methods that weren't linked.
1181   if (adapter() != nullptr) {
1182     return;
1183   }
1184   assert( _code == nullptr, "nothing compiled yet" );
1185 
1186   // Setup interpreter entrypoint
1187   assert(this == h_method(), "wrong h_method()" );
1188 

1207   // called from the vtable.  We need adapters on such methods that get loaded
1208   // later.  Ditto for mega-morphic itable calls.  If this proves to be a
1209   // problem we'll make these lazily later.
1210   (void) make_adapters(h_method, CHECK);
1211 
1212   // ONLY USE the h_method now as make_adapter may have blocked
1213 
1214   if (h_method->is_continuation_native_intrinsic()) {
1215     _from_interpreted_entry = nullptr;
1216     _from_compiled_entry = nullptr;
1217     _i2i_entry = nullptr;
1218     if (Continuations::enabled()) {
1219       assert(!Threads::is_vm_complete(), "should only be called during vm init");
1220       AdapterHandlerLibrary::create_native_wrapper(h_method);
1221       if (!h_method->has_compiled_code()) {
1222         THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Initial size of CodeCache is too small");
1223       }
1224       assert(_from_interpreted_entry == get_i2c_entry(), "invariant");
1225     }
1226   }





1227 }
1228 
1229 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1230   PerfTraceTime timer(ClassLoader::perf_method_adapters_time());
1231 
1232   // Adapters for compiled code are made eagerly here.  They are fairly
1233   // small (generally < 100 bytes) and quick to make (and cached and shared)
1234   // so making them eagerly shouldn't be too expensive.
1235   AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1236   if (adapter == nullptr ) {
1237     if (!is_init_completed()) {
1238       // Don't throw exceptions during VM initialization because java.lang.* classes
1239       // might not have been initialized, causing problems when constructing the
1240       // Java exception object.
1241       vm_exit_during_initialization("Out of space in CodeCache for adapters");
1242     } else {
1243       THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1244     }
1245   }
1246 
1247   mh->set_adapter_entry(adapter);
1248   mh->_from_compiled_entry = adapter->get_c2i_entry();
1249   return adapter->get_c2i_entry();
1250 }

1929   } else {
1930     return ((mcs != nullptr) ? mcs->invocation_counter()->count() : 0) +
1931            ((mdo != nullptr) ? mdo->invocation_counter()->count() : 0);
1932   }
1933 }
1934 
1935 int Method::backedge_count() const {
1936   MethodCounters* mcs = method_counters();
1937   MethodData* mdo = method_data();
1938   if (((mcs != nullptr) ? mcs->backedge_counter()->carry() : false) ||
1939       ((mdo != nullptr) ? mdo->backedge_counter()->carry() : false)) {
1940     return InvocationCounter::count_limit;
1941   } else {
1942     return ((mcs != nullptr) ? mcs->backedge_counter()->count() : 0) +
1943            ((mdo != nullptr) ? mdo->backedge_counter()->count() : 0);
1944   }
1945 }
1946 
1947 int Method::highest_comp_level() const {
1948   const MethodCounters* mcs = method_counters();


1949   if (mcs != nullptr) {
1950     return mcs->highest_comp_level();
1951   } else {
1952     return CompLevel_none;
1953   }
1954 }
1955 
1956 int Method::highest_osr_comp_level() const {
1957   const MethodCounters* mcs = method_counters();
1958   if (mcs != nullptr) {
1959     return mcs->highest_osr_comp_level();
1960   } else {
1961     return CompLevel_none;
1962   }
1963 }
1964 
1965 void Method::set_highest_comp_level(int level) {
1966   MethodCounters* mcs = method_counters();
1967   if (mcs != nullptr) {
1968     mcs->set_highest_comp_level(level);
1969   }
1970 }

  43 #include "interpreter/oopMapCache.hpp"
  44 #include "logging/log.hpp"
  45 #include "logging/logStream.hpp"
  46 #include "logging/logTag.hpp"
  47 #include "memory/allocation.inline.hpp"
  48 #include "memory/metadataFactory.hpp"
  49 #include "memory/metaspaceClosure.hpp"
  50 #include "memory/oopFactory.hpp"
  51 #include "memory/resourceArea.hpp"
  52 #include "memory/universe.hpp"
  53 #include "nmt/memTracker.hpp"
  54 #include "oops/constMethod.hpp"
  55 #include "oops/constantPool.hpp"
  56 #include "oops/klass.inline.hpp"
  57 #include "oops/method.inline.hpp"
  58 #include "oops/methodData.hpp"
  59 #include "oops/objArrayKlass.hpp"
  60 #include "oops/objArrayOop.inline.hpp"
  61 #include "oops/oop.inline.hpp"
  62 #include "oops/symbol.hpp"
  63 #include "oops/trainingData.hpp"
  64 #include "prims/jvmtiExport.hpp"
  65 #include "prims/methodHandles.hpp"
  66 #include "runtime/atomic.hpp"
  67 #include "runtime/arguments.hpp"
  68 #include "runtime/continuationEntry.hpp"
  69 #include "runtime/frame.inline.hpp"
  70 #include "runtime/handles.inline.hpp"
  71 #include "runtime/init.hpp"
  72 #include "runtime/java.hpp"
  73 #include "runtime/orderAccess.hpp"
  74 #include "runtime/perfData.hpp"
  75 #include "runtime/relocator.hpp"
  76 #include "runtime/safepointVerifiers.hpp"
  77 #include "runtime/sharedRuntime.hpp"
  78 #include "runtime/signature.hpp"
  79 #include "runtime/threads.hpp"
  80 #include "runtime/vm_version.hpp"
  81 #include "utilities/align.hpp"
  82 #include "utilities/quickSort.hpp"
  83 #include "utilities/vmError.hpp"

 165 }
 166 
 167 address Method::get_c2i_no_clinit_check_entry() {
 168   assert(VM_Version::supports_fast_class_init_checks(), "");
 169   assert(adapter() != nullptr, "must have");
 170   return adapter()->get_c2i_no_clinit_check_entry();
 171 }
 172 
 173 char* Method::name_and_sig_as_C_string() const {
 174   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
 175 }
 176 
 177 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
 178   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
 179 }
 180 
 181 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
 182   const char* klass_name = klass->external_name();
 183   int klass_name_len  = (int)strlen(klass_name);
 184   int method_name_len = method_name->utf8_length();
 185   int len             = klass_name_len + 2 + method_name_len + signature->utf8_length();
 186   char* dest          = NEW_RESOURCE_ARRAY(char, len + 1);
 187   strcpy(dest, klass_name);
 188   dest[klass_name_len + 0] = ':';
 189   dest[klass_name_len + 1] = ':';
 190   strcpy(&dest[klass_name_len + 2], method_name->as_C_string());
 191   strcpy(&dest[klass_name_len + 2 + method_name_len], signature->as_C_string());
 192   dest[len] = 0;
 193   return dest;
 194 }
 195 
 196 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature, char* buf, int size) {
 197   Symbol* klass_name = klass->name();
 198   klass_name->as_klass_external_name(buf, size);
 199   int len = (int)strlen(buf);
 200 
 201   if (len < size - 1) {
 202     buf[len++] = '.';
 203 
 204     method_name->as_C_string(&(buf[len]), size - len);
 205     len = (int)strlen(buf);
 206 
 207     signature->as_C_string(&(buf[len]), size - len);
 208   }
 209 
 210   return buf;
 211 }

 370 address Method::bcp_from(address bcp) const {
 371   if (is_native() && bcp == nullptr) {
 372     return code_base();
 373   } else {
 374     return bcp;
 375   }
 376 }
 377 
 378 int Method::size(bool is_native) {
 379   // If native, then include pointers for native_function and signature_handler
 380   int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
 381   int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
 382   return align_metadata_size(header_size() + extra_words);
 383 }
 384 
 385 Symbol* Method::klass_name() const {
 386   return method_holder()->name();
 387 }
 388 
 389 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
 390   LogStreamHandle(Trace, cds) lsh;
 391   if (lsh.is_enabled()) {
 392     lsh.print("Iter(Method): %p ", this);
 393     print_external_name(&lsh);
 394     lsh.cr();
 395   }
 396   if (method_holder() != nullptr && !method_holder()->is_rewritten()) {
 397     // holder is null for MH intrinsic methods
 398     it->push(&_constMethod, MetaspaceClosure::_writable);
 399   } else {
 400     it->push(&_constMethod);
 401   }
 402   it->push(&_method_data);
 403   it->push(&_method_counters);
 404   NOT_PRODUCT(it->push(&_name);)
 405 }
 406 
 407 #if INCLUDE_CDS
 408 // Attempt to return method to original state.  Clear any pointers
 409 // (to objects outside the shared spaces).  We won't be able to predict
 410 // where they should point in a new JVM.  Further initialize some
 411 // entries now in order allow them to be write protected later.
 412 
 413 void Method::remove_unshareable_info() {
 414   unlink_method();
 415   if (method_data() != nullptr) {
 416     method_data()->remove_unshareable_info();
 417   }
 418   if (method_counters() != nullptr) {
 419     method_counters()->remove_unshareable_info();
 420   }
 421   JFR_ONLY(REMOVE_METHOD_ID(this);)
 422 }
 423 
 424 void Method::restore_unshareable_info(TRAPS) {
 425   assert(is_method() && is_valid_method(this), "ensure C++ vtable is restored");
 426   if (method_data() != nullptr) {
 427     method_data()->restore_unshareable_info(CHECK);
 428   }
 429   if (method_counters() != nullptr) {
 430     method_counters()->restore_unshareable_info(CHECK);
 431   }
 432   assert(!queued_for_compilation(), "method's queued_for_compilation flag should not be set");
 433   assert(!pending_queue_processed(), "method's pending_queued_processed flag should not be set");
 434 }
 435 #endif
 436 
 437 void Method::set_vtable_index(int index) {
 438   if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
 439     // At runtime initialize_vtable is rerun as part of link_class_impl()
 440     // for a shared class loaded by the non-boot loader to obtain the loader
 441     // constraints based on the runtime classloaders' context.
 442     return; // don't write into the shared class
 443   } else {
 444     _vtable_index = index;
 445   }
 446 }
 447 
 448 void Method::set_itable_index(int index) {
 449   if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
 450     // At runtime initialize_itable is rerun as part of link_class_impl()
 451     // for a shared class loaded by the non-boot loader to obtain the loader
 452     // constraints based on the runtime classloaders' context. The dumptime
 453     // itable index should be the same as the runtime index.

 580   // Counting based on signed int counters tends to overflow with
 581   // longer-running workloads on fast machines. The counters under
 582   // consideration here, however, are limited in range by counting
 583   // logic. See InvocationCounter:count_limit for example.
 584   // No "overflow precautions" need to be implemented here.
 585   st->print_cr ("  interpreter_invocation_count: " INT32_FORMAT_W(11), interpreter_invocation_count());
 586   st->print_cr ("  invocation_counter:           " INT32_FORMAT_W(11), invocation_count());
 587   st->print_cr ("  backedge_counter:             " INT32_FORMAT_W(11), backedge_count());
 588 
 589   if (method_data() != nullptr) {
 590     st->print_cr ("  decompile_count:              " UINT32_FORMAT_W(11), method_data()->decompile_count());
 591   }
 592 
 593 #ifndef PRODUCT
 594   if (CountCompiledCalls) {
 595     st->print_cr ("  compiled_invocation_count:    " INT64_FORMAT_W(11), compiled_invocation_count());
 596   }
 597 #endif
 598 }
 599 
 600 MethodTrainingData* Method::training_data_or_null() const {
 601   MethodCounters* mcs = method_counters();
 602   if (mcs == nullptr) {
 603     return nullptr;
 604   } else {
 605     return mcs->method_training_data();
 606   }
 607 }
 608 
 609 bool Method::init_training_data(MethodTrainingData* tdata) {
 610   MethodCounters* mcs = method_counters();
 611   if (mcs == nullptr) {
 612     return false;
 613   } else {
 614     return mcs->init_method_training_data(tdata);
 615   }
 616 }
 617 
 618 bool Method::install_training_method_data(const methodHandle& method) {
 619   MethodTrainingData* mtd = MethodTrainingData::find(method);
 620   if (mtd != nullptr && mtd->has_holder() && mtd->final_profile() != nullptr &&
 621       mtd->holder() == method() && mtd->final_profile()->method() == method()) { // FIXME
 622     Atomic::replace_if_null(&method->_method_data, mtd->final_profile());
 623     return true;
 624   }
 625   return false;
 626 }
 627 
 628 // Build a MethodData* object to hold profiling information collected on this
 629 // method when requested.
 630 void Method::build_profiling_method_data(const methodHandle& method, TRAPS) {
 631   if (install_training_method_data(method)) {
 632     return;
 633   }
 634   // Do not profile the method if metaspace has hit an OOM previously
 635   // allocating profiling data. Callers clear pending exception so don't
 636   // add one here.
 637   if (ClassLoaderDataGraph::has_metaspace_oom()) {
 638     return;
 639   }
 640 
 641   ClassLoaderData* loader_data = method->method_holder()->class_loader_data();
 642   MethodData* method_data = MethodData::allocate(loader_data, method, THREAD);
 643   if (HAS_PENDING_EXCEPTION) {
 644     CompileBroker::log_metaspace_failure();
 645     ClassLoaderDataGraph::set_metaspace_oom(true);
 646     return;   // return the exception (which is cleared)
 647   }
 648 
 649   if (!Atomic::replace_if_null(&method->_method_data, method_data)) {
 650     MetadataFactory::free_metadata(loader_data, method_data);
 651     return;
 652   }
 653 
 654   /*
 655   LogStreamHandle(Info, mdo) lsh;
 656   if (lsh.is_enabled()) {
 657     ResourceMark rm(THREAD);
 658     lsh.print("build_profiling_method_data for ");
 659     method->print_name(&lsh);
 660     lsh.cr();
 661   }
 662   */
 663   if (ForceProfiling && TrainingData::need_data()) {
 664     MethodTrainingData* mtd = MethodTrainingData::make(method, false);
 665     guarantee(mtd != nullptr, "");
 666   }
 667   if (PrintMethodData) {
 668     ResourceMark rm(THREAD);
 669     tty->print("build_profiling_method_data for ");
 670     method->print_name(tty);
 671     tty->cr();
 672     // At the end of the run, the MDO, full of data, will be dumped.
 673   }
 674 }
 675 
 676 MethodCounters* Method::build_method_counters(Thread* current, Method* m) {
 677   // Do not profile the method if metaspace has hit an OOM previously
 678   if (ClassLoaderDataGraph::has_metaspace_oom()) {
 679     return nullptr;
 680   }
 681 
 682   methodHandle mh(current, m);
 683   MethodCounters* counters;
 684   if (current->is_Java_thread()) {
 685     JavaThread* THREAD = JavaThread::cast(current); // For exception macros.
 686     // Use the TRAPS version for a JavaThread so it will adjust the GC threshold
 687     // if needed.
 688     counters = MethodCounters::allocate_with_exception(mh, THREAD);
 689     if (HAS_PENDING_EXCEPTION) {
 690       CLEAR_PENDING_EXCEPTION;
 691     }
 692   } else {
 693     // Call metaspace allocation that doesn't throw exception if the
 694     // current thread isn't a JavaThread, ie. the VMThread.
 695     counters = MethodCounters::allocate_no_exception(mh);
 696   }
 697 
 698   if (counters == nullptr) {
 699     CompileBroker::log_metaspace_failure();
 700     ClassLoaderDataGraph::set_metaspace_oom(true);
 701     return nullptr;
 702   }
 703 
 704   if (!mh->init_method_counters(counters)) {
 705     MetadataFactory::free_metadata(mh->method_holder()->class_loader_data(), counters);
 706   }
 707 
 708   if (ForceProfiling && TrainingData::need_data()) {
 709     MethodTrainingData* mtd = MethodTrainingData::make(mh, false);
 710     guarantee(mtd != nullptr, "");
 711   }
 712 
 713   return mh->method_counters();
 714 }
 715 
 716 bool Method::init_method_counters(MethodCounters* counters) {
 717   // Try to install a pointer to MethodCounters, return true on success.
 718   return Atomic::replace_if_null(&_method_counters, counters);
 719 }
 720 
 721 void Method::set_exception_handler_entered(int handler_bci) {
 722   if (ProfileExceptionHandlers) {
 723     MethodData* mdo = method_data();
 724     if (mdo != nullptr) {
 725       BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
 726       handler_data.set_exception_handler_entered();
 727     }
 728   }
 729 }
 730 
 731 int Method::extra_stack_words() {
 732   // not an inline function, to avoid a header dependency on Interpreter

 919   return (is_static() ||
 920           method_holder()->major_version() < 51);
 921 }
 922 
 923 bool Method::is_static_initializer() const {
 924   // For classfiles version 51 or greater, ensure that the clinit method is
 925   // static.  Non-static methods with the name "<clinit>" are not static
 926   // initializers. (older classfiles exempted for backward compatibility)
 927   return name() == vmSymbols::class_initializer_name() &&
 928          has_valid_initializer_flags();
 929 }
 930 
 931 bool Method::is_object_initializer() const {
 932    return name() == vmSymbols::object_initializer_name();
 933 }
 934 
 935 bool Method::needs_clinit_barrier() const {
 936   return is_static() && !method_holder()->is_initialized();
 937 }
 938 
 939 bool Method::code_has_clinit_barriers() const {
 940   nmethod* nm = code();
 941   return (nm != nullptr) && nm->has_clinit_barriers();
 942 }
 943 
 944 bool Method::is_object_wait0() const {
 945   return klass_name() == vmSymbols::java_lang_Object()
 946          && name() == vmSymbols::wait_name();
 947 }
 948 
 949 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
 950   int length = method->checked_exceptions_length();
 951   if (length == 0) {  // common case
 952     return objArrayHandle(THREAD, Universe::the_empty_class_array());
 953   } else {
 954     methodHandle h_this(THREAD, method);
 955     objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
 956     objArrayHandle mirrors (THREAD, m_oop);
 957     for (int i = 0; i < length; i++) {
 958       CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
 959       Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
 960       if (log_is_enabled(Warning, exceptions) &&
 961           !k->is_subclass_of(vmClasses::Throwable_klass())) {
 962         ResourceMark rm(THREAD);
 963         log_warning(exceptions)(

1208 }
1209 
1210 #if INCLUDE_CDS
1211 // Called by class data sharing to remove any entry points (which are not shared)
1212 void Method::unlink_method() {
1213   assert(CDSConfig::is_dumping_archive(), "sanity");
1214   _code = nullptr;
1215   _adapter = nullptr;
1216   _i2i_entry = nullptr;
1217   _from_compiled_entry = nullptr;
1218   _from_interpreted_entry = nullptr;
1219 
1220   if (is_native()) {
1221     *native_function_addr() = nullptr;
1222     set_signature_handler(nullptr);
1223   }
1224   NOT_PRODUCT(set_compiled_invocation_count(0);)
1225 
1226   clear_method_data();
1227   clear_method_counters();
1228   clear_is_not_c1_compilable();
1229   clear_is_not_c1_osr_compilable();
1230   clear_is_not_c2_compilable();
1231   clear_is_not_c2_osr_compilable();
1232   clear_queued_for_compilation();
1233   set_pending_queue_processed(false);
1234   remove_unshareable_flags();
1235 }
1236 
1237 void Method::remove_unshareable_flags() {
1238   // clear all the flags that shouldn't be in the archived version
1239   assert(!is_old(), "must be");
1240   assert(!is_obsolete(), "must be");
1241   assert(!is_deleted(), "must be");
1242 
1243   set_is_prefixed_native(false);
1244   set_queued_for_compilation(false);
1245   set_pending_queue_processed(false);
1246   set_is_not_c2_compilable(false);
1247   set_is_not_c1_compilable(false);
1248   set_is_not_c2_osr_compilable(false);
1249   set_on_stack_flag(false);
1250   set_has_upcall_on_method_entry(false);
1251   set_has_upcall_on_method_exit(false);
1252 }
1253 #endif
1254 
1255 // Called when the method_holder is getting linked. Setup entrypoints so the method
1256 // is ready to be called from interpreter, compiler, and vtables.
1257 void Method::link_method(const methodHandle& h_method, TRAPS) {
1258   if (log_is_enabled(Info, perf, class, link)) {
1259     ClassLoader::perf_ik_link_methods_count()->inc();
1260   }
1261 
1262   // If the code cache is full, we may reenter this function for the
1263   // leftover methods that weren't linked.
1264   if (adapter() != nullptr) {
1265     return;
1266   }
1267   assert( _code == nullptr, "nothing compiled yet" );
1268 
1269   // Setup interpreter entrypoint
1270   assert(this == h_method(), "wrong h_method()" );
1271 

1290   // called from the vtable.  We need adapters on such methods that get loaded
1291   // later.  Ditto for mega-morphic itable calls.  If this proves to be a
1292   // problem we'll make these lazily later.
1293   (void) make_adapters(h_method, CHECK);
1294 
1295   // ONLY USE the h_method now as make_adapter may have blocked
1296 
1297   if (h_method->is_continuation_native_intrinsic()) {
1298     _from_interpreted_entry = nullptr;
1299     _from_compiled_entry = nullptr;
1300     _i2i_entry = nullptr;
1301     if (Continuations::enabled()) {
1302       assert(!Threads::is_vm_complete(), "should only be called during vm init");
1303       AdapterHandlerLibrary::create_native_wrapper(h_method);
1304       if (!h_method->has_compiled_code()) {
1305         THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Initial size of CodeCache is too small");
1306       }
1307       assert(_from_interpreted_entry == get_i2c_entry(), "invariant");
1308     }
1309   }
1310   if (_preload_code != nullptr) {
1311     MutexLocker ml(NMethodState_lock, Mutex::_no_safepoint_check_flag);
1312     set_code(h_method, _preload_code);
1313     assert(((nmethod*)_preload_code)->scc_entry() == _scc_entry, "sanity");
1314   }
1315 }
1316 
1317 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1318   PerfTraceElapsedTime timer(ClassLoader::perf_method_adapters_time());
1319 
1320   // Adapters for compiled code are made eagerly here.  They are fairly
1321   // small (generally < 100 bytes) and quick to make (and cached and shared)
1322   // so making them eagerly shouldn't be too expensive.
1323   AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1324   if (adapter == nullptr ) {
1325     if (!is_init_completed()) {
1326       // Don't throw exceptions during VM initialization because java.lang.* classes
1327       // might not have been initialized, causing problems when constructing the
1328       // Java exception object.
1329       vm_exit_during_initialization("Out of space in CodeCache for adapters");
1330     } else {
1331       THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1332     }
1333   }
1334 
1335   mh->set_adapter_entry(adapter);
1336   mh->_from_compiled_entry = adapter->get_c2i_entry();
1337   return adapter->get_c2i_entry();
1338 }

2017   } else {
2018     return ((mcs != nullptr) ? mcs->invocation_counter()->count() : 0) +
2019            ((mdo != nullptr) ? mdo->invocation_counter()->count() : 0);
2020   }
2021 }
2022 
2023 int Method::backedge_count() const {
2024   MethodCounters* mcs = method_counters();
2025   MethodData* mdo = method_data();
2026   if (((mcs != nullptr) ? mcs->backedge_counter()->carry() : false) ||
2027       ((mdo != nullptr) ? mdo->backedge_counter()->carry() : false)) {
2028     return InvocationCounter::count_limit;
2029   } else {
2030     return ((mcs != nullptr) ? mcs->backedge_counter()->count() : 0) +
2031            ((mdo != nullptr) ? mdo->backedge_counter()->count() : 0);
2032   }
2033 }
2034 
2035 int Method::highest_comp_level() const {
2036   const MethodCounters* mcs = method_counters();
2037   nmethod* nm = code();
2038   int level = (nm != nullptr) ? nm->comp_level() : CompLevel_none;
2039   if (mcs != nullptr) {
2040     return MAX2(mcs->highest_comp_level(), level);
2041   } else {
2042     return CompLevel_none;
2043   }
2044 }
2045 
2046 int Method::highest_osr_comp_level() const {
2047   const MethodCounters* mcs = method_counters();
2048   if (mcs != nullptr) {
2049     return mcs->highest_osr_comp_level();
2050   } else {
2051     return CompLevel_none;
2052   }
2053 }
2054 
2055 void Method::set_highest_comp_level(int level) {
2056   MethodCounters* mcs = method_counters();
2057   if (mcs != nullptr) {
2058     mcs->set_highest_comp_level(level);
2059   }
2060 }
< prev index next >