hotspot/src/share/vm/code/nmethod.cpp
changeset 35092 82170e5767c3
parent 35086 bbf32241d851
child 35108 ccb4e1f2a6cf
equal deleted inserted replaced
35091:e9d05f193287 35092:82170e5767c3
  1346   // Unregister must be done before the state change
  1346   // Unregister must be done before the state change
  1347   Universe::heap()->unregister_nmethod(this);
  1347   Universe::heap()->unregister_nmethod(this);
  1348 
  1348 
  1349   _state = unloaded;
  1349   _state = unloaded;
  1350 
  1350 
       
  1351   // Log the unloading.
       
  1352   log_state_change();
       
  1353 
  1351 #if INCLUDE_JVMCI
  1354 #if INCLUDE_JVMCI
  1352   // The method can only be unloaded after the pointer to the installed code
  1355   // The method can only be unloaded after the pointer to the installed code
  1353   // Java wrapper is no longer alive. Here we need to clear out this weak
  1356   // Java wrapper is no longer alive. Here we need to clear out this weak
  1354   // reference to the dead object. Nulling out the reference has to happen
  1357   // reference to the dead object. Nulling out the reference has to happen
  1355   // after the method is unregistered since the original value may be still
  1358   // after the method is unregistered since the original value may be still
  1356   // tracked by the rset.
  1359   // tracked by the rset.
  1357   maybe_invalidate_installed_code();
  1360   maybe_invalidate_installed_code();
  1358 #endif
  1361   // Clear these out after the nmethod has been unregistered and any
  1359 
  1362   // updates to the InstalledCode instance have been performed.
  1360   // Log the unloading.
  1363   _jvmci_installed_code = NULL;
  1361   log_state_change();
  1364   _speculation_log = NULL;
       
  1365 #endif
  1362 
  1366 
  1363   // The Method* is gone at this point
  1367   // The Method* is gone at this point
  1364   assert(_method == NULL, "Tautology");
  1368   assert(_method == NULL, "Tautology");
  1365 
  1369 
  1366   set_osr_link(NULL);
  1370   set_osr_link(NULL);
  1467     // Change state
  1471     // Change state
  1468     _state = state;
  1472     _state = state;
  1469 
  1473 
  1470     // Log the transition once
  1474     // Log the transition once
  1471     log_state_change();
  1475     log_state_change();
       
  1476 
       
  1477     // Invalidate while holding the patching lock
       
  1478     JVMCI_ONLY(maybe_invalidate_installed_code());
  1472 
  1479 
  1473     // Remove nmethod from method.
  1480     // Remove nmethod from method.
  1474     // We need to check if both the _code and _from_compiled_code_entry_point
  1481     // We need to check if both the _code and _from_compiled_code_entry_point
  1475     // refer to this nmethod because there is a race in setting these two fields
  1482     // refer to this nmethod because there is a race in setting these two fields
  1476     // in Method* as seen in bugid 4947125.
  1483     // in Method* as seen in bugid 4947125.
  1494       // safepoint can sneak in, otherwise the oops used by the
  1501       // safepoint can sneak in, otherwise the oops used by the
  1495       // dependency logic could have become stale.
  1502       // dependency logic could have become stale.
  1496       MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
  1503       MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
  1497       if (nmethod_needs_unregister) {
  1504       if (nmethod_needs_unregister) {
  1498         Universe::heap()->unregister_nmethod(this);
  1505         Universe::heap()->unregister_nmethod(this);
       
  1506 #ifdef JVMCI
       
  1507         _jvmci_installed_code = NULL;
       
  1508         _speculation_log = NULL;
       
  1509 #endif
  1499       }
  1510       }
  1500       flush_dependencies(NULL);
  1511       flush_dependencies(NULL);
  1501     }
  1512     }
  1502 
  1513 
  1503     // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload
  1514     // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload
  1516      // nmethod is in zombie state
  1527      // nmethod is in zombie state
  1517     set_method(NULL);
  1528     set_method(NULL);
  1518   } else {
  1529   } else {
  1519     assert(state == not_entrant, "other cases may need to be handled differently");
  1530     assert(state == not_entrant, "other cases may need to be handled differently");
  1520   }
  1531   }
  1521 
       
  1522   JVMCI_ONLY(maybe_invalidate_installed_code());
       
  1523 
  1532 
  1524   if (TraceCreateZombies) {
  1533   if (TraceCreateZombies) {
  1525     ResourceMark m;
  1534     ResourceMark m;
  1526     tty->print_cr("nmethod <" INTPTR_FORMAT "> %s code made %s", p2i(this), this->method() ? this->method()->name_and_sig_as_C_string() : "null", (state == not_entrant) ? "not entrant" : "zombie");
  1535     tty->print_cr("nmethod <" INTPTR_FORMAT "> %s code made %s", p2i(this), this->method() ? this->method()->name_and_sig_as_C_string() : "null", (state == not_entrant) ? "not entrant" : "zombie");
  1527   }
  1536   }
  3401 
  3410 
  3402 #endif // !PRODUCT
  3411 #endif // !PRODUCT
  3403 
  3412 
  3404 #if INCLUDE_JVMCI
  3413 #if INCLUDE_JVMCI
  3405 void nmethod::clear_jvmci_installed_code() {
  3414 void nmethod::clear_jvmci_installed_code() {
  3406   // This must be done carefully to maintain nmethod remembered sets properly
  3415   // write_ref_method_pre/post can only be safely called at a
  3407   BarrierSet* bs = Universe::heap()->barrier_set();
  3416   // safepoint or while holding the CodeCache_lock
  3408   bs->write_ref_nmethod_pre(&_jvmci_installed_code, this);
  3417   assert(CodeCache_lock->is_locked() ||
  3409   _jvmci_installed_code = NULL;
  3418          SafepointSynchronize::is_at_safepoint(), "should be performed under a lock for consistency");
  3410   bs->write_ref_nmethod_post(&_jvmci_installed_code, this);
  3419   if (_jvmci_installed_code != NULL) {
       
  3420     // This must be done carefully to maintain nmethod remembered sets properly
       
  3421     BarrierSet* bs = Universe::heap()->barrier_set();
       
  3422     bs->write_ref_nmethod_pre(&_jvmci_installed_code, this);
       
  3423     _jvmci_installed_code = NULL;
       
  3424     bs->write_ref_nmethod_post(&_jvmci_installed_code, this);
       
  3425   }
  3411 }
  3426 }
  3412 
  3427 
  3413 void nmethod::maybe_invalidate_installed_code() {
  3428 void nmethod::maybe_invalidate_installed_code() {
  3414   if (_jvmci_installed_code != NULL) {
  3429   assert(Patching_lock->is_locked() ||
  3415      if (!is_alive()) {
  3430          SafepointSynchronize::is_at_safepoint(), "should be performed under a lock for consistency");
  3416        // Break the link between nmethod and InstalledCode such that the nmethod
  3431   oop installed_code = jvmci_installed_code();
  3417        // can subsequently be flushed safely.  The link must be maintained while
  3432   if (installed_code != NULL) {
  3418        // the method could have live activations since invalidateInstalledCode
  3433     nmethod* nm = (nmethod*)InstalledCode::address(installed_code);
  3419        // might want to invalidate all existing activations.
  3434     if (nm == NULL || nm != this) {
  3420        InstalledCode::set_address(_jvmci_installed_code, 0);
  3435       // The link has been broken or the InstalledCode instance is
  3421        InstalledCode::set_entryPoint(_jvmci_installed_code, 0);
  3436       // associated with another nmethod so do nothing.
  3422        clear_jvmci_installed_code();
  3437       return;
  3423      } else if (is_not_entrant()) {
  3438     }
  3424        InstalledCode::set_entryPoint(_jvmci_installed_code, 0);
  3439     if (!is_alive()) {
  3425      }
  3440       // Break the link between nmethod and InstalledCode such that the nmethod
       
  3441       // can subsequently be flushed safely.  The link must be maintained while
       
  3442       // the method could have live activations since invalidateInstalledCode
       
  3443       // might want to invalidate all existing activations.
       
  3444       InstalledCode::set_address(installed_code, 0);
       
  3445       InstalledCode::set_entryPoint(installed_code, 0);
       
  3446     } else if (is_not_entrant()) {
       
  3447       // Remove the entry point so any invocation will fail but keep
       
  3448       // the address link around that so that existing activations can
       
  3449       // be invalidated.
       
  3450       InstalledCode::set_entryPoint(installed_code, 0);
       
  3451     }
       
  3452   }
       
  3453 }
       
  3454 
       
  3455 void nmethod::invalidate_installed_code(Handle installedCode, TRAPS) {
       
  3456   if (installedCode() == NULL) {
       
  3457     THROW(vmSymbols::java_lang_NullPointerException());
       
  3458   }
       
  3459   jlong nativeMethod = InstalledCode::address(installedCode);
       
  3460   nmethod* nm = (nmethod*)nativeMethod;
       
  3461   if (nm == NULL) {
       
  3462     // Nothing to do
       
  3463     return;
       
  3464   }
       
  3465 
       
  3466   nmethodLocker nml(nm);
       
  3467 #ifdef ASSERT
       
  3468   {
       
  3469     MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
       
  3470     // This relationship can only be checked safely under a lock
       
  3471     assert(nm == NULL || !nm->is_alive() || nm->jvmci_installed_code() == installedCode(), "sanity check");
       
  3472   }
       
  3473 #endif
       
  3474 
       
  3475   if (nm->is_alive()) {
       
  3476     // The nmethod state machinery maintains the link between the
       
  3477     // HotSpotInstalledCode and nmethod* so as long as the nmethod appears to be
       
  3478     // alive assume there is work to do and deoptimize the nmethod.
       
  3479     nm->mark_for_deoptimization();
       
  3480     VM_Deoptimize op;
       
  3481     VMThread::execute(&op);
       
  3482   }
       
  3483 
       
  3484   MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
       
  3485   // Check that it's still associated with the same nmethod and break
       
  3486   // the link if it is.
       
  3487   if (InstalledCode::address(installedCode) == nativeMethod) {
       
  3488     InstalledCode::set_address(installedCode, 0);
  3426   }
  3489   }
  3427 }
  3490 }
  3428 
  3491 
  3429 char* nmethod::jvmci_installed_code_name(char* buf, size_t buflen) {
  3492 char* nmethod::jvmci_installed_code_name(char* buf, size_t buflen) {
  3430   if (!this->is_compiled_by_jvmci()) {
  3493   if (!this->is_compiled_by_jvmci()) {