src/hotspot/share/code/nmethod.cpp
changeset 47794 e84aa2c71241
parent 47765 b7c7428eaab9
child 48007 ab3959df2115
equal deleted inserted replaced
47793:3dcd54513db1 47794:e84aa2c71241
   407   _rtm_state               = NoRTM;
   407   _rtm_state               = NoRTM;
   408 #endif
   408 #endif
   409 #if INCLUDE_JVMCI
   409 #if INCLUDE_JVMCI
   410   _jvmci_installed_code   = NULL;
   410   _jvmci_installed_code   = NULL;
   411   _speculation_log        = NULL;
   411   _speculation_log        = NULL;
       
   412   _jvmci_installed_code_triggers_unloading = false;
   412 #endif
   413 #endif
   413 }
   414 }
   414 
   415 
   415 nmethod* nmethod::new_native_nmethod(const methodHandle& method,
   416 nmethod* nmethod::new_native_nmethod(const methodHandle& method,
   416   int compile_id,
   417   int compile_id,
   459   ExceptionHandlerTable* handler_table,
   460   ExceptionHandlerTable* handler_table,
   460   ImplicitExceptionTable* nul_chk_table,
   461   ImplicitExceptionTable* nul_chk_table,
   461   AbstractCompiler* compiler,
   462   AbstractCompiler* compiler,
   462   int comp_level
   463   int comp_level
   463 #if INCLUDE_JVMCI
   464 #if INCLUDE_JVMCI
   464   , Handle installed_code,
   465   , jweak installed_code,
   465   Handle speculationLog
   466   jweak speculationLog
   466 #endif
   467 #endif
   467 )
   468 )
   468 {
   469 {
   469   assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
   470   assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
   470   code_buffer->finalize_oop_references(method);
   471   code_buffer->finalize_oop_references(method);
   640   ExceptionHandlerTable* handler_table,
   641   ExceptionHandlerTable* handler_table,
   641   ImplicitExceptionTable* nul_chk_table,
   642   ImplicitExceptionTable* nul_chk_table,
   642   AbstractCompiler* compiler,
   643   AbstractCompiler* compiler,
   643   int comp_level
   644   int comp_level
   644 #if INCLUDE_JVMCI
   645 #if INCLUDE_JVMCI
   645   , Handle installed_code,
   646   , jweak installed_code,
   646   Handle speculation_log
   647   jweak speculation_log
   647 #endif
   648 #endif
   648   )
   649   )
   649   : CompiledMethod(method, "nmethod", type, nmethod_size, sizeof(nmethod), code_buffer, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false),
   650   : CompiledMethod(method, "nmethod", type, nmethod_size, sizeof(nmethod), code_buffer, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false),
   650   _native_receiver_sp_offset(in_ByteSize(-1)),
   651   _native_receiver_sp_offset(in_ByteSize(-1)),
   651   _native_basic_lock_sp_offset(in_ByteSize(-1))
   652   _native_basic_lock_sp_offset(in_ByteSize(-1))
   669     _consts_offset           = content_offset()      + code_buffer->total_offset_of(code_buffer->consts());
   670     _consts_offset           = content_offset()      + code_buffer->total_offset_of(code_buffer->consts());
   670     _stub_offset             = content_offset()      + code_buffer->total_offset_of(code_buffer->stubs());
   671     _stub_offset             = content_offset()      + code_buffer->total_offset_of(code_buffer->stubs());
   671     set_ctable_begin(header_begin() + _consts_offset);
   672     set_ctable_begin(header_begin() + _consts_offset);
   672 
   673 
   673 #if INCLUDE_JVMCI
   674 #if INCLUDE_JVMCI
   674     _jvmci_installed_code = installed_code();
   675     _jvmci_installed_code = installed_code;
   675     _speculation_log = (instanceOop)speculation_log();
   676     _speculation_log = speculation_log;
       
   677     oop obj = JNIHandles::resolve(installed_code);
       
   678     if (obj == NULL || (obj->is_a(HotSpotNmethod::klass()) && HotSpotNmethod::isDefault(obj))) {
       
   679       _jvmci_installed_code_triggers_unloading = false;
       
   680     } else {
       
   681       _jvmci_installed_code_triggers_unloading = true;
       
   682     }
   676 
   683 
   677     if (compiler->is_jvmci()) {
   684     if (compiler->is_jvmci()) {
   678       // JVMCI might not produce any stub sections
   685       // JVMCI might not produce any stub sections
   679       if (offsets->value(CodeOffsets::Exceptions) != -1) {
   686       if (offsets->value(CodeOffsets::Exceptions) != -1) {
   680         _exception_offset        = code_offset()          + offsets->value(CodeOffsets::Exceptions);
   687         _exception_offset        = code_offset()          + offsets->value(CodeOffsets::Exceptions);
  1024     LogStream ls(lt);
  1031     LogStream ls(lt);
  1025     ls.print_cr("making nmethod " INTPTR_FORMAT
  1032     ls.print_cr("making nmethod " INTPTR_FORMAT
  1026                   " unloadable, Method*(" INTPTR_FORMAT
  1033                   " unloadable, Method*(" INTPTR_FORMAT
  1027                   "), cause(" INTPTR_FORMAT ")",
  1034                   "), cause(" INTPTR_FORMAT ")",
  1028                   p2i(this), p2i(_method), p2i(cause));
  1035                   p2i(this), p2i(_method), p2i(cause));
  1029     if (!Universe::heap()->is_gc_active())
       
  1030       cause->klass()->print_on(&ls);
       
  1031   }
  1036   }
  1032   // Unlink the osr method, so we do not look this up again
  1037   // Unlink the osr method, so we do not look this up again
  1033   if (is_osr_method()) {
  1038   if (is_osr_method()) {
  1034     // Invalidate the osr nmethod only once
  1039     // Invalidate the osr nmethod only once
  1035     if (is_in_use()) {
  1040     if (is_in_use()) {
  1075   log_state_change();
  1080   log_state_change();
  1076 
  1081 
  1077 #if INCLUDE_JVMCI
  1082 #if INCLUDE_JVMCI
  1078   // The method can only be unloaded after the pointer to the installed code
  1083   // The method can only be unloaded after the pointer to the installed code
  1079   // Java wrapper is no longer alive. Here we need to clear out this weak
  1084   // Java wrapper is no longer alive. Here we need to clear out this weak
  1080   // reference to the dead object. Nulling out the reference has to happen
  1085   // reference to the dead object.
  1081   // after the method is unregistered since the original value may be still
       
  1082   // tracked by the rset.
       
  1083   maybe_invalidate_installed_code();
  1086   maybe_invalidate_installed_code();
  1084   // Clear these out after the nmethod has been unregistered and any
       
  1085   // updates to the InstalledCode instance have been performed.
       
  1086   _jvmci_installed_code = NULL;
       
  1087   _speculation_log = NULL;
       
  1088 #endif
  1087 #endif
  1089 
  1088 
  1090   // The Method* is gone at this point
  1089   // The Method* is gone at this point
  1091   assert(_method == NULL, "Tautology");
  1090   assert(_method == NULL, "Tautology");
  1092 
  1091 
  1244       // safepoint can sneak in, otherwise the oops used by the
  1243       // safepoint can sneak in, otherwise the oops used by the
  1245       // dependency logic could have become stale.
  1244       // dependency logic could have become stale.
  1246       MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
  1245       MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
  1247       if (nmethod_needs_unregister) {
  1246       if (nmethod_needs_unregister) {
  1248         Universe::heap()->unregister_nmethod(this);
  1247         Universe::heap()->unregister_nmethod(this);
  1249 #ifdef JVMCI
       
  1250         _jvmci_installed_code = NULL;
       
  1251         _speculation_log = NULL;
       
  1252 #endif
       
  1253       }
  1248       }
  1254       flush_dependencies(NULL);
  1249       flush_dependencies(NULL);
  1255     }
  1250     }
  1256 
  1251 
  1257     // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload
  1252     // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload
  1311   }
  1306   }
  1312 
  1307 
  1313   if (on_scavenge_root_list()) {
  1308   if (on_scavenge_root_list()) {
  1314     CodeCache::drop_scavenge_root_nmethod(this);
  1309     CodeCache::drop_scavenge_root_nmethod(this);
  1315   }
  1310   }
       
  1311 
       
  1312 #if INCLUDE_JVMCI
       
  1313   assert(_jvmci_installed_code == NULL, "should have been nulled out when transitioned to zombie");
       
  1314   assert(_speculation_log == NULL, "should have been nulled out when transitioned to zombie");
       
  1315 #endif
  1316 
  1316 
  1317   CodeBlob::flush();
  1317   CodeBlob::flush();
  1318   CodeCache::free(this);
  1318   CodeCache::free(this);
  1319 }
  1319 }
  1320 
  1320 
  1498   return do_unloading_scopes(is_alive, unloading_occurred);
  1498   return do_unloading_scopes(is_alive, unloading_occurred);
  1499 }
  1499 }
  1500 
  1500 
  1501 #if INCLUDE_JVMCI
  1501 #if INCLUDE_JVMCI
  1502 bool nmethod::do_unloading_jvmci(BoolObjectClosure* is_alive, bool unloading_occurred) {
  1502 bool nmethod::do_unloading_jvmci(BoolObjectClosure* is_alive, bool unloading_occurred) {
  1503   bool is_unloaded = false;
       
  1504   // Follow JVMCI method
       
  1505   BarrierSet* bs = Universe::heap()->barrier_set();
       
  1506   if (_jvmci_installed_code != NULL) {
  1503   if (_jvmci_installed_code != NULL) {
  1507     if (_jvmci_installed_code->is_a(HotSpotNmethod::klass()) && HotSpotNmethod::isDefault(_jvmci_installed_code)) {
  1504     if (JNIHandles::is_global_weak_cleared(_jvmci_installed_code)) {
  1508       if (!is_alive->do_object_b(_jvmci_installed_code)) {
  1505       if (_jvmci_installed_code_triggers_unloading) {
       
  1506         // jweak reference processing has already cleared the referent
       
  1507         make_unloaded(is_alive, NULL);
       
  1508         return true;
       
  1509       } else {
  1509         clear_jvmci_installed_code();
  1510         clear_jvmci_installed_code();
  1510       }
  1511       }
  1511     } else {
  1512     }
  1512       if (can_unload(is_alive, (oop*)&_jvmci_installed_code, unloading_occurred)) {
  1513   }
  1513         return true;
  1514   return false;
  1514       }
       
  1515     }
       
  1516   }
       
  1517 
       
  1518   if (_speculation_log != NULL) {
       
  1519     if (!is_alive->do_object_b(_speculation_log)) {
       
  1520       bs->write_ref_nmethod_pre(&_speculation_log, this);
       
  1521       _speculation_log = NULL;
       
  1522       bs->write_ref_nmethod_post(&_speculation_log, this);
       
  1523     }
       
  1524   }
       
  1525   return is_unloaded;
       
  1526 }
  1515 }
  1527 #endif
  1516 #endif
  1528 
  1517 
  1529 // Iterate over metadata calling this function.   Used by RedefineClasses
  1518 // Iterate over metadata calling this function.   Used by RedefineClasses
  1530 void nmethod::metadata_do(void f(Metadata*)) {
  1519 void nmethod::metadata_do(void f(Metadata*)) {
  1591   if (is_not_entrant()) {
  1580   if (is_not_entrant()) {
  1592     low_boundary += NativeJump::instruction_size;
  1581     low_boundary += NativeJump::instruction_size;
  1593     // %%% Note:  On SPARC we patch only a 4-byte trap, not a full NativeJump.
  1582     // %%% Note:  On SPARC we patch only a 4-byte trap, not a full NativeJump.
  1594     // (See comment above.)
  1583     // (See comment above.)
  1595   }
  1584   }
  1596 
       
  1597 #if INCLUDE_JVMCI
       
  1598   if (_jvmci_installed_code != NULL) {
       
  1599     f->do_oop((oop*) &_jvmci_installed_code);
       
  1600   }
       
  1601   if (_speculation_log != NULL) {
       
  1602     f->do_oop((oop*) &_speculation_log);
       
  1603   }
       
  1604 #endif
       
  1605 
  1585 
  1606   RelocIterator iter(this, low_boundary);
  1586   RelocIterator iter(this, low_boundary);
  1607 
  1587 
  1608   while (iter.next()) {
  1588   while (iter.next()) {
  1609     if (iter.type() == relocInfo::oop_type ) {
  1589     if (iter.type() == relocInfo::oop_type ) {
  2858 
  2838 
  2859 #endif // !PRODUCT
  2839 #endif // !PRODUCT
  2860 
  2840 
  2861 #if INCLUDE_JVMCI
  2841 #if INCLUDE_JVMCI
  2862 void nmethod::clear_jvmci_installed_code() {
  2842 void nmethod::clear_jvmci_installed_code() {
  2863   // write_ref_method_pre/post can only be safely called at a
  2843   assert_locked_or_safepoint(Patching_lock);
  2864   // safepoint or while holding the CodeCache_lock
       
  2865   assert(CodeCache_lock->is_locked() ||
       
  2866          SafepointSynchronize::is_at_safepoint(), "should be performed under a lock for consistency");
       
  2867   if (_jvmci_installed_code != NULL) {
  2844   if (_jvmci_installed_code != NULL) {
  2868     // This must be done carefully to maintain nmethod remembered sets properly
  2845     JNIHandles::destroy_weak_global(_jvmci_installed_code);
  2869     BarrierSet* bs = Universe::heap()->barrier_set();
       
  2870     bs->write_ref_nmethod_pre(&_jvmci_installed_code, this);
       
  2871     _jvmci_installed_code = NULL;
  2846     _jvmci_installed_code = NULL;
  2872     bs->write_ref_nmethod_post(&_jvmci_installed_code, this);
  2847   }
       
  2848 }
       
  2849 
       
  2850 void nmethod::clear_speculation_log() {
       
  2851   assert_locked_or_safepoint(Patching_lock);
       
  2852   if (_speculation_log != NULL) {
       
  2853     JNIHandles::destroy_weak_global(_speculation_log);
       
  2854     _speculation_log = NULL;
  2873   }
  2855   }
  2874 }
  2856 }
  2875 
  2857 
  2876 void nmethod::maybe_invalidate_installed_code() {
  2858 void nmethod::maybe_invalidate_installed_code() {
  2877   assert(Patching_lock->is_locked() ||
  2859   assert(Patching_lock->is_locked() ||
  2878          SafepointSynchronize::is_at_safepoint(), "should be performed under a lock for consistency");
  2860          SafepointSynchronize::is_at_safepoint(), "should be performed under a lock for consistency");
  2879   oop installed_code = jvmci_installed_code();
  2861   oop installed_code = JNIHandles::resolve(_jvmci_installed_code);
  2880   if (installed_code != NULL) {
  2862   if (installed_code != NULL) {
       
  2863     // Update the values in the InstalledCode instance if it still refers to this nmethod
  2881     nmethod* nm = (nmethod*)InstalledCode::address(installed_code);
  2864     nmethod* nm = (nmethod*)InstalledCode::address(installed_code);
  2882     if (nm == NULL || nm != this) {
  2865     if (nm == this) {
  2883       // The link has been broken or the InstalledCode instance is
  2866       if (!is_alive()) {
  2884       // associated with another nmethod so do nothing.
  2867         // Break the link between nmethod and InstalledCode such that the nmethod
  2885       return;
  2868         // can subsequently be flushed safely.  The link must be maintained while
  2886     }
  2869         // the method could have live activations since invalidateInstalledCode
  2887     if (!is_alive()) {
  2870         // might want to invalidate all existing activations.
  2888       // Break the link between nmethod and InstalledCode such that the nmethod
  2871         InstalledCode::set_address(installed_code, 0);
  2889       // can subsequently be flushed safely.  The link must be maintained while
  2872         InstalledCode::set_entryPoint(installed_code, 0);
  2890       // the method could have live activations since invalidateInstalledCode
  2873       } else if (is_not_entrant()) {
  2891       // might want to invalidate all existing activations.
  2874         // Remove the entry point so any invocation will fail but keep
  2892       InstalledCode::set_address(installed_code, 0);
  2875         // the address link around that so that existing activations can
  2893       InstalledCode::set_entryPoint(installed_code, 0);
  2876         // be invalidated.
  2894     } else if (is_not_entrant()) {
  2877         InstalledCode::set_entryPoint(installed_code, 0);
  2895       // Remove the entry point so any invocation will fail but keep
  2878       }
  2896       // the address link around that so that existing activations can
  2879     }
  2897       // be invalidated.
  2880   }
  2898       InstalledCode::set_entryPoint(installed_code, 0);
  2881   if (!is_alive()) {
  2899     }
  2882     // Clear these out after the nmethod has been unregistered and any
       
  2883     // updates to the InstalledCode instance have been performed.
       
  2884     clear_jvmci_installed_code();
       
  2885     clear_speculation_log();
  2900   }
  2886   }
  2901 }
  2887 }
  2902 
  2888 
  2903 void nmethod::invalidate_installed_code(Handle installedCode, TRAPS) {
  2889 void nmethod::invalidate_installed_code(Handle installedCode, TRAPS) {
  2904   if (installedCode() == NULL) {
  2890   if (installedCode() == NULL) {
  2914   nmethodLocker nml(nm);
  2900   nmethodLocker nml(nm);
  2915 #ifdef ASSERT
  2901 #ifdef ASSERT
  2916   {
  2902   {
  2917     MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
  2903     MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
  2918     // This relationship can only be checked safely under a lock
  2904     // This relationship can only be checked safely under a lock
  2919     assert(nm == NULL || !nm->is_alive() || nm->jvmci_installed_code() == installedCode(), "sanity check");
  2905     assert(!nm->is_alive() || nm->jvmci_installed_code() == installedCode(), "sanity check");
  2920   }
  2906   }
  2921 #endif
  2907 #endif
  2922 
  2908 
  2923   if (nm->is_alive()) {
  2909   if (nm->is_alive()) {
  2924     // The nmethod state machinery maintains the link between the
  2910     // Invalidating the InstalledCode means we want the nmethod
  2925     // HotSpotInstalledCode and nmethod* so as long as the nmethod appears to be
  2911     // to be deoptimized.
  2926     // alive assume there is work to do and deoptimize the nmethod.
       
  2927     nm->mark_for_deoptimization();
  2912     nm->mark_for_deoptimization();
  2928     VM_Deoptimize op;
  2913     VM_Deoptimize op;
  2929     VMThread::execute(&op);
  2914     VMThread::execute(&op);
  2930   }
  2915   }
  2931 
  2916 
       
  2917   // Multiple threads could reach this point so we now need to
       
  2918   // lock and re-check the link to the nmethod so that only one
       
  2919   // thread clears it.
  2932   MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
  2920   MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
  2933   // Check that it's still associated with the same nmethod and break
       
  2934   // the link if it is.
       
  2935   if (InstalledCode::address(installedCode) == nativeMethod) {
  2921   if (InstalledCode::address(installedCode) == nativeMethod) {
  2936     InstalledCode::set_address(installedCode, 0);
  2922       InstalledCode::set_address(installedCode, 0);
  2937   }
  2923   }
       
  2924 }
       
  2925 
       
  2926 oop nmethod::jvmci_installed_code() {
       
  2927   return JNIHandles::resolve(_jvmci_installed_code);
       
  2928 }
       
  2929 
       
  2930 oop nmethod::speculation_log() {
       
  2931   return JNIHandles::resolve(_speculation_log);
  2938 }
  2932 }
  2939 
  2933 
  2940 char* nmethod::jvmci_installed_code_name(char* buf, size_t buflen) {
  2934 char* nmethod::jvmci_installed_code_name(char* buf, size_t buflen) {
  2941   if (!this->is_compiled_by_jvmci()) {
  2935   if (!this->is_compiled_by_jvmci()) {
  2942     return NULL;
  2936     return NULL;
  2943   }
  2937   }
  2944   oop installedCode = this->jvmci_installed_code();
  2938   oop installed_code = JNIHandles::resolve(_jvmci_installed_code);
  2945   if (installedCode != NULL) {
  2939   if (installed_code != NULL) {
  2946     oop installedCodeName = NULL;
  2940     oop installed_code_name = NULL;
  2947     if (installedCode->is_a(InstalledCode::klass())) {
  2941     if (installed_code->is_a(InstalledCode::klass())) {
  2948       installedCodeName = InstalledCode::name(installedCode);
  2942       installed_code_name = InstalledCode::name(installed_code);
  2949     }
  2943     }
  2950     if (installedCodeName != NULL) {
  2944     if (installed_code_name != NULL) {
  2951       return java_lang_String::as_utf8_string(installedCodeName, buf, (int)buflen);
  2945       return java_lang_String::as_utf8_string(installed_code_name, buf, (int)buflen);
  2952     } else {
  2946     }
  2953       jio_snprintf(buf, buflen, "null");
  2947   }
  2954       return buf;
  2948   return NULL;
  2955     }
  2949 }
  2956   }
  2950 #endif
  2957   jio_snprintf(buf, buflen, "noInstalledCode");
       
  2958   return buf;
       
  2959 }
       
  2960 #endif