1314 // Since this nmethod is being unloaded, make sure that dependencies
1315 // recorded in instanceKlasses get flushed and pass non-NULL closure to
1316 // indicate that this work is being done during a GC.
1317 assert(Universe::heap()->is_gc_active(), "should only be called during gc");
1318 assert(is_alive != NULL, "Should be non-NULL");
1319 // A non-NULL is_alive closure indicates that this is being called during GC.
1320 flush_dependencies(is_alive);
1321
1322 // Break cycle between nmethod & method
1323 if (log_is_enabled(Trace, classunload)) {
1324 outputStream* log = LogHandle(classunload)::trace_stream();
1325 log->print_cr("making nmethod " INTPTR_FORMAT
1326 " unloadable, Method*(" INTPTR_FORMAT
1327 "), cause(" INTPTR_FORMAT ")",
1328 p2i(this), p2i(_method), p2i(cause));
1329 if (!Universe::heap()->is_gc_active())
1330 cause->klass()->print_on(log);
1331 }
1332 // Unlink the osr method, so we do not look this up again
1333 if (is_osr_method()) {
1334 invalidate_osr_method();
1335 }
1336 // If _method is already NULL the Method* is about to be unloaded,
1337 // so we don't have to break the cycle. Note that it is possible to
1338 // have the Method* live here, in case we unload the nmethod because
1339 // it is pointing to some oop (other than the Method*) being unloaded.
1340 if (_method != NULL) {
1341 // OSR methods point to the Method*, but the Method* does not
1342 // point back!
1343 if (_method->code() == this) {
1344 _method->clear_code(); // Break a cycle
1345 }
1346 _method = NULL; // Clear the method of this dead nmethod
1347 }
1348
1349 // Make the class unloaded - i.e., change state and notify sweeper
1350 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1351 if (is_in_use()) {
1352 // Transitioning directly from live to unloaded -- so
1353 // we need to force a cache clean-up; remember this
1354 // for later on.
1355 CodeCache::set_needs_cache_clean(true);
1368 // Java wrapper is no longer alive. Here we need to clear out this weak
1369 // reference to the dead object. Nulling out the reference has to happen
1370 // after the method is unregistered since the original value may be still
1371 // tracked by the rset.
1372 maybe_invalidate_installed_code();
1373 // Clear these out after the nmethod has been unregistered and any
1374 // updates to the InstalledCode instance have been performed.
1375 _jvmci_installed_code = NULL;
1376 _speculation_log = NULL;
1377 #endif
1378
1379 // The Method* is gone at this point
1380 assert(_method == NULL, "Tautology");
1381
1382 set_osr_link(NULL);
1383 NMethodSweeper::report_state_change(this);
1384 }
1385
1386 void nmethod::invalidate_osr_method() {
1387 assert(_entry_bci != InvocationEntryBci, "wrong kind of nmethod");
1388 #ifndef ASSERT
1389 // Make sure osr nmethod is invalidated only once
1390 if (!is_in_use()) {
1391 return;
1392 }
1393 #endif
1394 // Remove from list of active nmethods
1395 if (method() != NULL) {
1396 bool removed = method()->method_holder()->remove_osr_nmethod(this);
1397 assert(!removed || is_in_use(), "unused osr nmethod should be invalidated");
1398 }
1399 }
1400
1401 void nmethod::log_state_change() const {
1402 if (LogCompilation) {
1403 if (xtty != NULL) {
1404 ttyLocker ttyl; // keep the following output all in one block
1405 if (_state == unloaded) {
1406 xtty->begin_elem("make_unloaded thread='" UINTX_FORMAT "'",
1407 os::current_thread_id());
1408 } else {
1409 xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'%s",
1410 os::current_thread_id(),
1411 (_state == zombie ? " zombie='1'" : ""));
1412 }
1413 log_identity(xtty);
1414 xtty->stamp();
1415 xtty->end_elem();
1416 }
1417 }
1426 bool nmethod::make_not_entrant_or_zombie(unsigned int state) {
1427 assert(state == zombie || state == not_entrant, "must be zombie or not_entrant");
1428 assert(!is_zombie(), "should not already be a zombie");
1429
1430 // Make sure neither the nmethod nor the method is flushed in case of a safepoint in code below.
1431 nmethodLocker nml(this);
1432 methodHandle the_method(method());
1433 NoSafepointVerifier nsv;
1434
1435 // during patching, depending on the nmethod state we must notify the GC that
1436 // code has been unloaded, unregistering it. We cannot do this right while
1437 // holding the Patching_lock because we need to use the CodeCache_lock. This
1438 // would be prone to deadlocks.
1439 // This flag is used to remember whether we need to later lock and unregister.
1440 bool nmethod_needs_unregister = false;
1441
1442 {
1443 // invalidate osr nmethod before acquiring the patching lock since
1444 // they both acquire leaf locks and we don't want a deadlock.
1445 // This logic is equivalent to the logic below for patching the
1446 // verified entry point of regular methods.
1447 if (is_osr_method()) {
1448 // this effectively makes the osr nmethod not entrant
1449 invalidate_osr_method();
1450 }
1451
1452 // Enter critical section. Does not block for safepoint.
1453 MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
1454
1455 if (_state == state) {
1456 // another thread already performed this transition so nothing
1457 // to do, but return false to indicate this.
1458 return false;
1459 }
1460
1461 // The caller can be calling the method statically or through an inline
1462 // cache call.
1463 if (!is_osr_method() && !is_not_entrant()) {
1464 NativeJump::patch_verified_entry(entry_point(), verified_entry_point(),
1465 SharedRuntime::get_handle_wrong_method_stub());
1466 }
1467
1492
1493 // Log the transition once
1494 log_state_change();
1495
1496 // Invalidate while holding the patching lock
1497 JVMCI_ONLY(maybe_invalidate_installed_code());
1498
1499 // Remove nmethod from method.
1500 // We need to check if both the _code and _from_compiled_code_entry_point
1501 // refer to this nmethod because there is a race in setting these two fields
1502 // in Method* as seen in bugid 4947125.
1503 // If the vep() points to the zombie nmethod, the memory for the nmethod
1504 // could be flushed and the compiler and vtable stubs could still call
1505 // through it.
1506 if (method() != NULL && (method()->code() == this ||
1507 method()->from_compiled_entry() == verified_entry_point())) {
1508 HandleMark hm;
1509 method()->clear_code();
1510 }
1511 } // leave critical region under Patching_lock
1512
1513 // When the nmethod becomes zombie it is no longer alive so the
1514 // dependencies must be flushed. nmethods in the not_entrant
1515 // state will be flushed later when the transition to zombie
1516 // happens or they get unloaded.
1517 if (state == zombie) {
1518 {
1519 // Flushing dependencies must be done before any possible
1520 // safepoint can sneak in, otherwise the oops used by the
1521 // dependency logic could have become stale.
1522 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1523 if (nmethod_needs_unregister) {
1524 Universe::heap()->unregister_nmethod(this);
1525 #ifdef JVMCI
1526 _jvmci_installed_code = NULL;
1527 _speculation_log = NULL;
1528 #endif
1529 }
1530 flush_dependencies(NULL);
1531 }
|
1314 // Since this nmethod is being unloaded, make sure that dependencies
1315 // recorded in instanceKlasses get flushed and pass non-NULL closure to
1316 // indicate that this work is being done during a GC.
1317 assert(Universe::heap()->is_gc_active(), "should only be called during gc");
1318 assert(is_alive != NULL, "Should be non-NULL");
1319 // A non-NULL is_alive closure indicates that this is being called during GC.
1320 flush_dependencies(is_alive);
1321
1322 // Break cycle between nmethod & method
1323 if (log_is_enabled(Trace, classunload)) {
1324 outputStream* log = LogHandle(classunload)::trace_stream();
1325 log->print_cr("making nmethod " INTPTR_FORMAT
1326 " unloadable, Method*(" INTPTR_FORMAT
1327 "), cause(" INTPTR_FORMAT ")",
1328 p2i(this), p2i(_method), p2i(cause));
1329 if (!Universe::heap()->is_gc_active())
1330 cause->klass()->print_on(log);
1331 }
1332 // Unlink the osr method, so we do not look this up again
1333 if (is_osr_method()) {
1334 // Invalidate the osr nmethod only once
1335 if (is_in_use()) {
1336 invalidate_osr_method();
1337 }
1338 #ifdef ASSERT
1339 // Make sure osr nmethod is invalidated, i.e. not on the list
1340 bool found = method()->method_holder()->remove_osr_nmethod(this);
1341 assert(!found, "osr nmethod should have been invalidated");
1342 #endif
1343 }
1344
1345 // If _method is already NULL the Method* is about to be unloaded,
1346 // so we don't have to break the cycle. Note that it is possible to
1347 // have the Method* live here, in case we unload the nmethod because
1348 // it is pointing to some oop (other than the Method*) being unloaded.
1349 if (_method != NULL) {
1350 // OSR methods point to the Method*, but the Method* does not
1351 // point back!
1352 if (_method->code() == this) {
1353 _method->clear_code(); // Break a cycle
1354 }
1355 _method = NULL; // Clear the method of this dead nmethod
1356 }
1357
1358 // Make the class unloaded - i.e., change state and notify sweeper
1359 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1360 if (is_in_use()) {
1361 // Transitioning directly from live to unloaded -- so
1362 // we need to force a cache clean-up; remember this
1363 // for later on.
1364 CodeCache::set_needs_cache_clean(true);
1377 // Java wrapper is no longer alive. Here we need to clear out this weak
1378 // reference to the dead object. Nulling out the reference has to happen
1379 // after the method is unregistered since the original value may be still
1380 // tracked by the rset.
1381 maybe_invalidate_installed_code();
1382 // Clear these out after the nmethod has been unregistered and any
1383 // updates to the InstalledCode instance have been performed.
1384 _jvmci_installed_code = NULL;
1385 _speculation_log = NULL;
1386 #endif
1387
1388 // The Method* is gone at this point
1389 assert(_method == NULL, "Tautology");
1390
1391 set_osr_link(NULL);
1392 NMethodSweeper::report_state_change(this);
1393 }
1394
1395 void nmethod::invalidate_osr_method() {
1396 assert(_entry_bci != InvocationEntryBci, "wrong kind of nmethod");
1397 // Remove from list of active nmethods
1398 if (method() != NULL) {
1399 method()->method_holder()->remove_osr_nmethod(this);
1400 }
1401 }
1402
1403 void nmethod::log_state_change() const {
1404 if (LogCompilation) {
1405 if (xtty != NULL) {
1406 ttyLocker ttyl; // keep the following output all in one block
1407 if (_state == unloaded) {
1408 xtty->begin_elem("make_unloaded thread='" UINTX_FORMAT "'",
1409 os::current_thread_id());
1410 } else {
1411 xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'%s",
1412 os::current_thread_id(),
1413 (_state == zombie ? " zombie='1'" : ""));
1414 }
1415 log_identity(xtty);
1416 xtty->stamp();
1417 xtty->end_elem();
1418 }
1419 }
1428 bool nmethod::make_not_entrant_or_zombie(unsigned int state) {
1429 assert(state == zombie || state == not_entrant, "must be zombie or not_entrant");
1430 assert(!is_zombie(), "should not already be a zombie");
1431
1432 // Make sure neither the nmethod nor the method is flushed in case of a safepoint in code below.
1433 nmethodLocker nml(this);
1434 methodHandle the_method(method());
1435 NoSafepointVerifier nsv;
1436
1437 // during patching, depending on the nmethod state we must notify the GC that
1438 // code has been unloaded, unregistering it. We cannot do this right while
1439 // holding the Patching_lock because we need to use the CodeCache_lock. This
1440 // would be prone to deadlocks.
1441 // This flag is used to remember whether we need to later lock and unregister.
1442 bool nmethod_needs_unregister = false;
1443
1444 {
1445 // invalidate osr nmethod before acquiring the patching lock since
1446 // they both acquire leaf locks and we don't want a deadlock.
1447 // This logic is equivalent to the logic below for patching the
1448 // verified entry point of regular methods. We check that the
1449 // nmethod is in use to ensure that it is invalidated only once.
1450 if (is_osr_method() && is_in_use()) {
1451 // this effectively makes the osr nmethod not entrant
1452 invalidate_osr_method();
1453 }
1454
1455 // Enter critical section. Does not block for safepoint.
1456 MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
1457
1458 if (_state == state) {
1459 // another thread already performed this transition so nothing
1460 // to do, but return false to indicate this.
1461 return false;
1462 }
1463
1464 // The caller can be calling the method statically or through an inline
1465 // cache call.
1466 if (!is_osr_method() && !is_not_entrant()) {
1467 NativeJump::patch_verified_entry(entry_point(), verified_entry_point(),
1468 SharedRuntime::get_handle_wrong_method_stub());
1469 }
1470
1495
1496 // Log the transition once
1497 log_state_change();
1498
1499 // Invalidate while holding the patching lock
1500 JVMCI_ONLY(maybe_invalidate_installed_code());
1501
1502 // Remove nmethod from method.
1503 // We need to check if both the _code and _from_compiled_code_entry_point
1504 // refer to this nmethod because there is a race in setting these two fields
1505 // in Method* as seen in bugid 4947125.
1506 // If the vep() points to the zombie nmethod, the memory for the nmethod
1507 // could be flushed and the compiler and vtable stubs could still call
1508 // through it.
1509 if (method() != NULL && (method()->code() == this ||
1510 method()->from_compiled_entry() == verified_entry_point())) {
1511 HandleMark hm;
1512 method()->clear_code();
1513 }
1514 } // leave critical region under Patching_lock
1515
1516 #ifdef ASSERT
1517 if (is_osr_method()) {
1518 // Make sure osr nmethod is invalidated, i.e. not on the list
1519 bool found = method()->method_holder()->remove_osr_nmethod(this);
1520 assert(!found, "osr nmethod should have been invalidated");
1521 }
1522 #endif
1523
1524 // When the nmethod becomes zombie it is no longer alive so the
1525 // dependencies must be flushed. nmethods in the not_entrant
1526 // state will be flushed later when the transition to zombie
1527 // happens or they get unloaded.
1528 if (state == zombie) {
1529 {
1530 // Flushing dependencies must be done before any possible
1531 // safepoint can sneak in, otherwise the oops used by the
1532 // dependency logic could have become stale.
1533 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1534 if (nmethod_needs_unregister) {
1535 Universe::heap()->unregister_nmethod(this);
1536 #ifdef JVMCI
1537 _jvmci_installed_code = NULL;
1538 _speculation_log = NULL;
1539 #endif
1540 }
1541 flush_dependencies(NULL);
1542 }
|