< prev index next >

src/hotspot/share/code/nmethod.cpp

Print this page
rev 54838 : [mq]: 8221734-v2
rev 54839 : [mq]: 8221734-v3


  32 #include "code/nmethod.hpp"
  33 #include "code/scopeDesc.hpp"
  34 #include "compiler/abstractCompiler.hpp"
  35 #include "compiler/compileBroker.hpp"
  36 #include "compiler/compileLog.hpp"
  37 #include "compiler/compilerDirectives.hpp"
  38 #include "compiler/directivesParser.hpp"
  39 #include "compiler/disassembler.hpp"
  40 #include "interpreter/bytecode.hpp"
  41 #include "logging/log.hpp"
  42 #include "logging/logStream.hpp"
  43 #include "memory/allocation.inline.hpp"
  44 #include "memory/resourceArea.hpp"
  45 #include "memory/universe.hpp"
  46 #include "oops/access.inline.hpp"
  47 #include "oops/method.inline.hpp"
  48 #include "oops/methodData.hpp"
  49 #include "oops/oop.inline.hpp"
  50 #include "prims/jvmtiImpl.hpp"
  51 #include "runtime/atomic.hpp"

  52 #include "runtime/flags/flagSetting.hpp"
  53 #include "runtime/frame.inline.hpp"
  54 #include "runtime/handles.inline.hpp"
  55 #include "runtime/jniHandles.inline.hpp"
  56 #include "runtime/orderAccess.hpp"
  57 #include "runtime/os.hpp"
  58 #include "runtime/safepointVerifiers.hpp"
  59 #include "runtime/sharedRuntime.hpp"
  60 #include "runtime/sweeper.hpp"
  61 #include "runtime/vmThread.hpp"
  62 #include "utilities/align.hpp"
  63 #include "utilities/dtrace.hpp"
  64 #include "utilities/events.hpp"
  65 #include "utilities/resourceHash.hpp"
  66 #include "utilities/xmlstream.hpp"
  67 #if INCLUDE_JVMCI
  68 #include "jvmci/jvmciRuntime.hpp"
  69 #endif
  70 
  71 #ifdef DTRACE_ENABLED


1104   // Unlink the osr method, so we do not look this up again
1105   if (is_osr_method()) {
1106     // Invalidate the osr nmethod only once
1107     if (is_in_use()) {
1108       invalidate_osr_method();
1109     }
1110 #ifdef ASSERT
1111     if (method() != NULL) {
1112       // Make sure osr nmethod is invalidated, i.e. not on the list
1113       bool found = method()->method_holder()->remove_osr_nmethod(this);
1114       assert(!found, "osr nmethod should have been invalidated");
1115     }
1116 #endif
1117   }
1118 
1119   // If _method is already NULL the Method* is about to be unloaded,
1120   // so we don't have to break the cycle. Note that it is possible to
1121   // have the Method* live here, in case we unload the nmethod because
1122   // it is pointing to some oop (other than the Method*) being unloaded.
1123   if (_method != NULL) {
1124     // OSR methods point to the Method*, but the Method* does not
1125     // point back!
1126     if (_method->code() == this) {
1127       _method->clear_code(); // Break a cycle
1128     }
1129   }
1130 
1131   // Make the class unloaded - i.e., change state and notify sweeper
1132   assert(SafepointSynchronize::is_at_safepoint() || Thread::current()->is_ConcurrentGC_thread(),
1133          "must be at safepoint");
1134 
1135   {
1136     // Clear ICStubs and release any CompiledICHolders.
1137     CompiledICLocker ml(this);
1138     clear_ic_callsites();
1139   }
1140 
1141   // Unregister must be done before the state change
1142   {
1143     MutexLocker ml(SafepointSynchronize::is_at_safepoint() ? NULL : CodeCache_lock,
1144                      Mutex::_no_safepoint_check_flag);
1145     Universe::heap()->unregister_nmethod(this);
1146     CodeCache::unregister_old_nmethod(this);
1147   }
1148 


1191         xtty->begin_elem("make_unloaded thread='" UINTX_FORMAT "'",
1192                          os::current_thread_id());
1193       } else {
1194         xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'%s",
1195                          os::current_thread_id(),
1196                          (_state == zombie ? " zombie='1'" : ""));
1197       }
1198       log_identity(xtty);
1199       xtty->stamp();
1200       xtty->end_elem();
1201     }
1202   }
1203 
1204   const char *state_msg = _state == zombie ? "made zombie" : "made not entrant";
1205   CompileTask::print_ul(this, state_msg);
1206   if (PrintCompilation && _state != unloaded) {
1207     print_on(tty, state_msg);
1208   }
1209 }
1210 
1211 void nmethod::unlink_from_method(bool acquire_lock) {
1212   // We need to check if both the _code and _from_compiled_code_entry_point
1213   // refer to this nmethod because there is a race in setting these two fields
1214   // in Method* as seen in bugid 4947125.
1215   // If the vep() points to the zombie nmethod, the memory for the nmethod
1216   // could be flushed and the compiler and vtable stubs could still call
1217   // through it.
1218   if (method() != NULL && (method()->code() == this ||
1219                            method()->from_compiled_entry() == verified_entry_point())) {
1220     method()->clear_code(acquire_lock);
1221   }
1222 }
1223 
1224 /**
1225  * Common functionality for both make_not_entrant and make_zombie
1226  */
1227 bool nmethod::make_not_entrant_or_zombie(int state) {
1228   assert(state == zombie || state == not_entrant, "must be zombie or not_entrant");
1229   assert(!is_zombie(), "should not already be a zombie");
1230 
1231   if (_state == state) {
1232     // Avoid taking the lock if already in required state.
1233     // This is safe from races because the state is an end-state,
1234     // which the nmethod cannot back out of once entered.
1235     // No need for fencing either.
1236     return false;
1237   }
1238 
1239   // Make sure neither the nmethod nor the method is flushed in case of a safepoint in code below.
1240   nmethodLocker nml(this);
1241   methodHandle the_method(method());
1242   // This can be called while the system is already at a safepoint which is ok
1243   NoSafepointVerifier nsv(true, !SafepointSynchronize::is_at_safepoint());
1244 
1245   // during patching, depending on the nmethod state we must notify the GC that
1246   // code has been unloaded, unregistering it. We cannot do this right while
1247   // holding the Patching_lock because we need to use the CodeCache_lock. This
1248   // would be prone to deadlocks.
1249   // This flag is used to remember whether we need to later lock and unregister.
1250   bool nmethod_needs_unregister = false;
1251 
1252   {
1253     // invalidate osr nmethod before acquiring the patching lock since
1254     // they both acquire leaf locks and we don't want a deadlock.
1255     // This logic is equivalent to the logic below for patching the
1256     // verified entry point of regular methods. We check that the
1257     // nmethod is in use to ensure that it is invalidated only once.
1258     if (is_osr_method() && is_in_use()) {
1259       // this effectively makes the osr nmethod not entrant
1260       invalidate_osr_method();
1261     }
1262 

1263     // Enter critical section.  Does not block for safepoint.
1264     MutexLocker pl(Patching_lock, Mutex::_no_safepoint_check_flag);
1265 
1266     if (_state == state) {
1267       // another thread already performed this transition so nothing
1268       // to do, but return false to indicate this.
1269       return false;
1270     }
1271 
1272     // The caller can be calling the method statically or through an inline
1273     // cache call.
1274     if (!is_osr_method() && !is_not_entrant()) {
1275       NativeJump::patch_verified_entry(entry_point(), verified_entry_point(),
1276                   SharedRuntime::get_handle_wrong_method_stub());
1277     }
1278 
1279     if (is_in_use() && update_recompile_counts()) {
1280       // It's a true state change, so mark the method as decompiled.
1281       // Do it only for transition from alive.
1282       inc_decompile_count();
1283     }
1284 


1288     if ((state == zombie) && !is_unloaded()) {
1289       nmethod_needs_unregister = true;
1290     }
1291 
1292     // Must happen before state change. Otherwise we have a race condition in
1293     // nmethod::can_not_entrant_be_converted(). I.e., a method can immediately
1294     // transition its state from 'not_entrant' to 'zombie' without having to wait
1295     // for stack scanning.
1296     if (state == not_entrant) {
1297       mark_as_seen_on_stack();
1298       OrderAccess::storestore(); // _stack_traversal_mark and _state
1299     }
1300 
1301     // Change state
1302     _state = state;
1303 
1304     // Log the transition once
1305     log_state_change();
1306 
1307     // Remove nmethod from method.
1308     unlink_from_method(false /* already owns Patching_lock */);
1309   } // leave critical region under Patching_lock

1310 
1311 #if INCLUDE_JVMCI
1312   // Invalidate can't occur while holding the Patching lock
1313   JVMCINMethodData* nmethod_data = jvmci_nmethod_data();
1314   if (nmethod_data != NULL) {
1315     nmethod_data->invalidate_nmethod_mirror(this);
1316   }
1317 #endif
1318 
1319 #ifdef ASSERT
1320   if (is_osr_method() && method() != NULL) {
1321     // Make sure osr nmethod is invalidated, i.e. not on the list
1322     bool found = method()->method_holder()->remove_osr_nmethod(this);
1323     assert(!found, "osr nmethod should have been invalidated");
1324   }
1325 #endif
1326 
1327   // When the nmethod becomes zombie it is no longer alive so the
1328   // dependencies must be flushed.  nmethods in the not_entrant
1329   // state will be flushed later when the transition to zombie




  32 #include "code/nmethod.hpp"
  33 #include "code/scopeDesc.hpp"
  34 #include "compiler/abstractCompiler.hpp"
  35 #include "compiler/compileBroker.hpp"
  36 #include "compiler/compileLog.hpp"
  37 #include "compiler/compilerDirectives.hpp"
  38 #include "compiler/directivesParser.hpp"
  39 #include "compiler/disassembler.hpp"
  40 #include "interpreter/bytecode.hpp"
  41 #include "logging/log.hpp"
  42 #include "logging/logStream.hpp"
  43 #include "memory/allocation.inline.hpp"
  44 #include "memory/resourceArea.hpp"
  45 #include "memory/universe.hpp"
  46 #include "oops/access.inline.hpp"
  47 #include "oops/method.inline.hpp"
  48 #include "oops/methodData.hpp"
  49 #include "oops/oop.inline.hpp"
  50 #include "prims/jvmtiImpl.hpp"
  51 #include "runtime/atomic.hpp"
  52 #include "runtime/deoptimization.hpp"
  53 #include "runtime/flags/flagSetting.hpp"
  54 #include "runtime/frame.inline.hpp"
  55 #include "runtime/handles.inline.hpp"
  56 #include "runtime/jniHandles.inline.hpp"
  57 #include "runtime/orderAccess.hpp"
  58 #include "runtime/os.hpp"
  59 #include "runtime/safepointVerifiers.hpp"
  60 #include "runtime/sharedRuntime.hpp"
  61 #include "runtime/sweeper.hpp"
  62 #include "runtime/vmThread.hpp"
  63 #include "utilities/align.hpp"
  64 #include "utilities/dtrace.hpp"
  65 #include "utilities/events.hpp"
  66 #include "utilities/resourceHash.hpp"
  67 #include "utilities/xmlstream.hpp"
  68 #if INCLUDE_JVMCI
  69 #include "jvmci/jvmciRuntime.hpp"
  70 #endif
  71 
  72 #ifdef DTRACE_ENABLED


1105   // Unlink the osr method, so we do not look this up again
1106   if (is_osr_method()) {
1107     // Invalidate the osr nmethod only once
1108     if (is_in_use()) {
1109       invalidate_osr_method();
1110     }
1111 #ifdef ASSERT
1112     if (method() != NULL) {
1113       // Make sure osr nmethod is invalidated, i.e. not on the list
1114       bool found = method()->method_holder()->remove_osr_nmethod(this);
1115       assert(!found, "osr nmethod should have been invalidated");
1116     }
1117 #endif
1118   }
1119 
1120   // If _method is already NULL the Method* is about to be unloaded,
1121   // so we don't have to break the cycle. Note that it is possible to
1122   // have the Method* live here, in case we unload the nmethod because
1123   // it is pointing to some oop (other than the Method*) being unloaded.
1124   if (_method != NULL) {
1125     _method->unlink_code(this);




1126   }
1127 
1128   // Make the class unloaded - i.e., change state and notify sweeper
1129   assert(SafepointSynchronize::is_at_safepoint() || Thread::current()->is_ConcurrentGC_thread(),
1130          "must be at safepoint");
1131 
1132   {
1133     // Clear ICStubs and release any CompiledICHolders.
1134     CompiledICLocker ml(this);
1135     clear_ic_callsites();
1136   }
1137 
1138   // Unregister must be done before the state change
1139   {
1140     MutexLocker ml(SafepointSynchronize::is_at_safepoint() ? NULL : CodeCache_lock,
1141                      Mutex::_no_safepoint_check_flag);
1142     Universe::heap()->unregister_nmethod(this);
1143     CodeCache::unregister_old_nmethod(this);
1144   }
1145 


1188         xtty->begin_elem("make_unloaded thread='" UINTX_FORMAT "'",
1189                          os::current_thread_id());
1190       } else {
1191         xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'%s",
1192                          os::current_thread_id(),
1193                          (_state == zombie ? " zombie='1'" : ""));
1194       }
1195       log_identity(xtty);
1196       xtty->stamp();
1197       xtty->end_elem();
1198     }
1199   }
1200 
1201   const char *state_msg = _state == zombie ? "made zombie" : "made not entrant";
1202   CompileTask::print_ul(this, state_msg);
1203   if (PrintCompilation && _state != unloaded) {
1204     print_on(tty, state_msg);
1205   }
1206 }
1207 
1208 void nmethod::unlink_from_method() {
1209   if (method() != NULL) {
1210     method()->unlink_code();







1211   }
1212 }
1213 
1214 /**
1215  * Common functionality for both make_not_entrant and make_zombie
1216  */
1217 bool nmethod::make_not_entrant_or_zombie(int state) {
1218   assert(state == zombie || state == not_entrant, "must be zombie or not_entrant");
1219   assert(!is_zombie(), "should not already be a zombie");
1220 
1221   if (_state == state) {
1222     // Avoid taking the lock if already in required state.
1223     // This is safe from races because the state is an end-state,
1224     // which the nmethod cannot back out of once entered.
1225     // No need for fencing either.
1226     return false;
1227   }
1228 
1229   // Make sure neither the nmethod nor the method is flushed in case of a safepoint in code below.
1230   nmethodLocker nml(this);
1231   methodHandle the_method(method());
1232   // This can be called while the system is already at a safepoint which is ok
1233   NoSafepointVerifier nsv(true, !SafepointSynchronize::is_at_safepoint());
1234 
1235   // during patching, depending on the nmethod state we must notify the GC that
1236   // code has been unloaded, unregistering it. We cannot do this right while
1237   // holding the CompiledMethod_lock because we need to use the CodeCache_lock. This
1238   // would be prone to deadlocks.
1239   // This flag is used to remember whether we need to later lock and unregister.
1240   bool nmethod_needs_unregister = false;
1241 

1242   // invalidate osr nmethod before acquiring the patching lock since
1243   // they both acquire leaf locks and we don't want a deadlock.
1244   // This logic is equivalent to the logic below for patching the
1245   // verified entry point of regular methods. We check that the
1246   // nmethod is in use to ensure that it is invalidated only once.
1247   if (is_osr_method() && is_in_use()) {
1248     // this effectively makes the osr nmethod not entrant
1249     invalidate_osr_method();
1250   }
1251 
1252   {
1253     // Enter critical section.  Does not block for safepoint.
1254     MutexLocker pl(CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
1255 
1256     if (_state == state) {
1257       // another thread already performed this transition so nothing
1258       // to do, but return false to indicate this.
1259       return false;
1260     }
1261 
1262     // The caller can be calling the method statically or through an inline
1263     // cache call.
1264     if (!is_osr_method() && !is_not_entrant()) {
1265       NativeJump::patch_verified_entry(entry_point(), verified_entry_point(),
1266                   SharedRuntime::get_handle_wrong_method_stub());
1267     }
1268 
1269     if (is_in_use() && update_recompile_counts()) {
1270       // It's a true state change, so mark the method as decompiled.
1271       // Do it only for transition from alive.
1272       inc_decompile_count();
1273     }
1274 


1278     if ((state == zombie) && !is_unloaded()) {
1279       nmethod_needs_unregister = true;
1280     }
1281 
1282     // Must happen before state change. Otherwise we have a race condition in
1283     // nmethod::can_not_entrant_be_converted(). I.e., a method can immediately
1284     // transition its state from 'not_entrant' to 'zombie' without having to wait
1285     // for stack scanning.
1286     if (state == not_entrant) {
1287       mark_as_seen_on_stack();
1288       OrderAccess::storestore(); // _stack_traversal_mark and _state
1289     }
1290 
1291     // Change state
1292     _state = state;
1293 
1294     // Log the transition once
1295     log_state_change();
1296 
1297     // Remove nmethod from method.
1298     unlink_from_method();
1299 
1300   } // leave critical region under CompiledMethod_lock
1301 
1302 #if INCLUDE_JVMCI
1303   // Invalidate can't occur while holding the Patching lock
1304   JVMCINMethodData* nmethod_data = jvmci_nmethod_data();
1305   if (nmethod_data != NULL) {
1306     nmethod_data->invalidate_nmethod_mirror(this);
1307   }
1308 #endif
1309 
1310 #ifdef ASSERT
1311   if (is_osr_method() && method() != NULL) {
1312     // Make sure osr nmethod is invalidated, i.e. not on the list
1313     bool found = method()->method_holder()->remove_osr_nmethod(this);
1314     assert(!found, "osr nmethod should have been invalidated");
1315   }
1316 #endif
1317 
1318   // When the nmethod becomes zombie it is no longer alive so the
1319   // dependencies must be flushed.  nmethods in the not_entrant
1320   // state will be flushed later when the transition to zombie


< prev index next >