< prev index next >

src/hotspot/share/runtime/synchronizer.cpp

Print this page

   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/vmSymbols.hpp"
  27 #include "jfr/jfrEvents.hpp"

  28 #include "logging/log.hpp"
  29 #include "logging/logStream.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/padded.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "memory/universe.hpp"
  34 #include "oops/markWord.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "runtime/atomic.hpp"
  37 #include "runtime/handles.inline.hpp"
  38 #include "runtime/handshake.hpp"
  39 #include "runtime/interfaceSupport.inline.hpp"
  40 #include "runtime/mutexLocker.hpp"
  41 #include "runtime/objectMonitor.hpp"
  42 #include "runtime/objectMonitor.inline.hpp"
  43 #include "runtime/os.inline.hpp"
  44 #include "runtime/osThread.hpp"
  45 #include "runtime/perfData.hpp"
  46 #include "runtime/safepointMechanism.inline.hpp"
  47 #include "runtime/safepointVerifiers.hpp"

 723         assert(ix >= 0 && ix < NINFLATIONLOCKS, "invariant");
 724         gInflationLocks[ix]->lock();
 725         while (obj->mark_acquire() == markWord::INFLATING()) {
 726           // Beware: naked_yield() is advisory and has almost no effect on some platforms
 727           // so we periodically call current->_ParkEvent->park(1).
 728           // We use a mixed spin/yield/block mechanism.
 729           if ((YieldThenBlock++) >= 16) {
 730             Thread::current()->_ParkEvent->park(1);
 731           } else {
 732             os::naked_yield();
 733           }
 734         }
 735         gInflationLocks[ix]->unlock();
 736       }
 737     } else {
 738       SpinPause();       // SMP-polite spinning
 739     }
 740   }
 741 }
 742 































































































 743 // hashCode() generation :
 744 //
 745 // Possibilities:
 746 // * MD5Digest of {obj,stw_random}
 747 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
 748 // * A DES- or AES-style SBox[] mechanism
 749 // * One of the Phi-based schemes, such as:
 750 //   2654435761 = 2^32 * Phi (golden ratio)
 751 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
 752 // * A variation of Marsaglia's shift-xor RNG scheme.
 753 // * (obj ^ stw_random) is appealing, but can result
 754 //   in undesirable regularity in the hashCode values of adjacent objects
 755 //   (objects allocated back-to-back, in particular).  This could potentially
 756 //   result in hashtable collisions and reduced hashtable efficiency.
 757 //   There are simple ways to "diffuse" the middle address bits over the
 758 //   generated hashCode values:
 759 
 760 static inline intptr_t get_next_hash(Thread* current, oop obj) {
 761   intptr_t value = 0;
 762   if (hashCode == 0) {

1335     if (current->is_Java_thread()) {
1336       // A JavaThread must check for a safepoint/handshake and honor it.
1337       chk_for_block_req(JavaThread::cast(current), "deflation", "deflated_count",
1338                         deflated_count, ls, timer_p);
1339     }
1340   }
1341 
1342   return deflated_count;
1343 }
1344 
1345 class HandshakeForDeflation : public HandshakeClosure {
1346  public:
1347   HandshakeForDeflation() : HandshakeClosure("HandshakeForDeflation") {}
1348 
1349   void do_thread(Thread* thread) {
1350     log_trace(monitorinflation)("HandshakeForDeflation::do_thread: thread="
1351                                 INTPTR_FORMAT, p2i(thread));
1352   }
1353 };
1354 










1355 // This function is called by the MonitorDeflationThread to deflate
1356 // ObjectMonitors. It is also called via do_final_audit_and_print_stats()
1357 // by the VMThread.
1358 size_t ObjectSynchronizer::deflate_idle_monitors() {
1359   Thread* current = Thread::current();
1360   if (current->is_Java_thread()) {
1361     // The async deflation request has been processed.
1362     _last_async_deflation_time_ns = os::javaTimeNanos();
1363     set_is_async_deflation_requested(false);
1364   }
1365 
1366   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1367   LogStreamHandle(Info, monitorinflation) lsh_info;
1368   LogStream* ls = NULL;
1369   if (log_is_enabled(Debug, monitorinflation)) {
1370     ls = &lsh_debug;
1371   } else if (log_is_enabled(Info, monitorinflation)) {
1372     ls = &lsh_info;
1373   }
1374 

1387     // deflated, BUT the MonitorDeflationThread blocked for the final
1388     // safepoint during unlinking.
1389 
1390     // Unlink deflated ObjectMonitors from the in-use list.
1391     ResourceMark rm;
1392     GrowableArray<ObjectMonitor*> delete_list((int)deflated_count);
1393     size_t unlinked_count = _in_use_list.unlink_deflated(current, ls, &timer,
1394                                                          &delete_list);
1395     if (current->is_Java_thread()) {
1396       if (ls != NULL) {
1397         timer.stop();
1398         ls->print_cr("before handshaking: unlinked_count=" SIZE_FORMAT
1399                      ", in_use_list stats: ceiling=" SIZE_FORMAT ", count="
1400                      SIZE_FORMAT ", max=" SIZE_FORMAT,
1401                      unlinked_count, in_use_list_ceiling(),
1402                      _in_use_list.count(), _in_use_list.max());
1403       }
1404 
1405       // A JavaThread needs to handshake in order to safely free the
1406       // ObjectMonitors that were deflated in this cycle.



1407       HandshakeForDeflation hfd_hc;
1408       Handshake::execute(&hfd_hc);


1409 
1410       if (ls != NULL) {
1411         ls->print_cr("after handshaking: in_use_list stats: ceiling="
1412                      SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1413                      in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1414         timer.start();
1415       }
1416     }
1417 
1418     // After the handshake, safely free the ObjectMonitors that were
1419     // deflated in this cycle.
1420     size_t deleted_count = 0;
1421     for (ObjectMonitor* monitor: delete_list) {
1422       delete monitor;
1423       deleted_count++;
1424 
1425       if (current->is_Java_thread()) {
1426         // A JavaThread must check for a safepoint/handshake and honor it.
1427         chk_for_block_req(JavaThread::cast(current), "deletion", "deleted_count",
1428                           deleted_count, ls, &timer);

   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/vmSymbols.hpp"
  27 #include "jfr/jfrEvents.hpp"
  28 #include "gc/shared/suspendibleThreadSet.hpp"
  29 #include "logging/log.hpp"
  30 #include "logging/logStream.hpp"
  31 #include "memory/allocation.inline.hpp"
  32 #include "memory/padded.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "memory/universe.hpp"
  35 #include "oops/markWord.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "runtime/atomic.hpp"
  38 #include "runtime/handles.inline.hpp"
  39 #include "runtime/handshake.hpp"
  40 #include "runtime/interfaceSupport.inline.hpp"
  41 #include "runtime/mutexLocker.hpp"
  42 #include "runtime/objectMonitor.hpp"
  43 #include "runtime/objectMonitor.inline.hpp"
  44 #include "runtime/os.inline.hpp"
  45 #include "runtime/osThread.hpp"
  46 #include "runtime/perfData.hpp"
  47 #include "runtime/safepointMechanism.inline.hpp"
  48 #include "runtime/safepointVerifiers.hpp"

 724         assert(ix >= 0 && ix < NINFLATIONLOCKS, "invariant");
 725         gInflationLocks[ix]->lock();
 726         while (obj->mark_acquire() == markWord::INFLATING()) {
 727           // Beware: naked_yield() is advisory and has almost no effect on some platforms
 728           // so we periodically call current->_ParkEvent->park(1).
 729           // We use a mixed spin/yield/block mechanism.
 730           if ((YieldThenBlock++) >= 16) {
 731             Thread::current()->_ParkEvent->park(1);
 732           } else {
 733             os::naked_yield();
 734           }
 735         }
 736         gInflationLocks[ix]->unlock();
 737       }
 738     } else {
 739       SpinPause();       // SMP-polite spinning
 740     }
 741   }
 742 }
 743 
 744 // Safely load a mark word from an object, even with racing stack-locking or monitor inflation.
 745 // The protocol is a partial inflation-protocol: it installs INFLATING into the object's mark
 746 // word in order to prevent an stack-locks or inflations from interferring (or detect such
 747 // interference and retry), but then, instead of creating and installing a monitor, simply
 748 // read and return the real mark word.
 749 markWord ObjectSynchronizer::safe_load_mark(oop object) {
 750   for (;;) {
 751     const markWord mark = object->mark_acquire();
 752 
 753     // The mark can be in one of the following states:
 754     // *  Inflated     - just return mark from inflated monitor
 755     // *  Stack-locked - coerce it to inflating, and then return displaced mark
 756     // *  INFLATING    - busy wait for conversion to complete
 757     // *  Neutral      - return mark
 758 
 759     // CASE: inflated
 760     if (mark.has_monitor()) {
 761       ObjectMonitor* inf = mark.monitor();
 762       markWord dmw = inf->header();
 763       assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
 764       return dmw;
 765     }
 766 
 767     // CASE: inflation in progress - inflating over a stack-lock.
 768     // Some other thread is converting from stack-locked to inflated.
 769     // Only that thread can complete inflation -- other threads must wait.
 770     // The INFLATING value is transient.
 771     // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
 772     // We could always eliminate polling by parking the thread on some auxiliary list.
 773     if (mark == markWord::INFLATING()) {
 774       read_stable_mark(object);
 775       continue;
 776     }
 777 
 778     // CASE: stack-locked
 779     // Could be stack-locked either by this thread or by some other thread.
 780     if (mark.has_locker()) {
 781       markWord cmp = object->cas_set_mark(markWord::INFLATING(), mark);
 782       if (cmp != mark) {
 783         continue;       // Interference -- just retry
 784       }
 785 
 786       // We've successfully installed INFLATING (0) into the mark-word.
 787       // This is the only case where 0 will appear in a mark-word.
 788       // Only the singular thread that successfully swings the mark-word
 789       // to 0 can perform (or more precisely, complete) inflation.
 790 
 791       // fetch the displaced mark from the owner's stack.
 792       // The owner can't die or unwind past the lock while our INFLATING
 793       // object is in the mark.  Furthermore the owner can't complete
 794       // an unlock on the object, either.
 795       markWord dmw = mark.displaced_mark_helper();
 796       // Catch if the object's header is not neutral (not locked and
 797       // not marked is what we care about here).
 798       assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
 799 
 800       // Must preserve store ordering. The monitor state must
 801       // be stable at the time of publishing the monitor address.
 802       guarantee(object->mark() == markWord::INFLATING(), "invariant");
 803       // Release semantics so that above set_object() is seen first.
 804       object->release_set_mark(mark);
 805 
 806       return dmw;
 807     }
 808 
 809     // CASE: neutral
 810     // Catch if the object's header is not neutral (not locked and
 811     // not marked is what we care about here).
 812     assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
 813     return mark;
 814   }
 815 }
 816 
 817 markWord ObjectSynchronizer::stable_mark(const oop obj) {
 818   markWord mark = read_stable_mark(obj);
 819   if (!mark.is_neutral() && !mark.is_marked()) {
 820     if (mark.has_monitor()) {
 821       ObjectMonitor* monitor = mark.monitor();
 822       mark = monitor->header();
 823       assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
 824     } else if (SafepointSynchronize::is_at_safepoint() || Thread::current()->is_lock_owned((address) mark.locker())) {
 825       // This is a stack lock owned by the calling thread so fetch the
 826       // displaced markWord from the BasicLock on the stack.
 827       assert(mark.has_displaced_mark_helper(), "must be displaced header here");
 828       mark = mark.displaced_mark_helper();
 829       assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
 830     } else {
 831       mark = safe_load_mark(obj);
 832       assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
 833       assert(!mark.is_marked(), "no forwarded objects here");
 834     }
 835   }
 836   return mark;
 837 }
 838 
 839 // hashCode() generation :
 840 //
 841 // Possibilities:
 842 // * MD5Digest of {obj,stw_random}
 843 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
 844 // * A DES- or AES-style SBox[] mechanism
 845 // * One of the Phi-based schemes, such as:
 846 //   2654435761 = 2^32 * Phi (golden ratio)
 847 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
 848 // * A variation of Marsaglia's shift-xor RNG scheme.
 849 // * (obj ^ stw_random) is appealing, but can result
 850 //   in undesirable regularity in the hashCode values of adjacent objects
 851 //   (objects allocated back-to-back, in particular).  This could potentially
 852 //   result in hashtable collisions and reduced hashtable efficiency.
 853 //   There are simple ways to "diffuse" the middle address bits over the
 854 //   generated hashCode values:
 855 
 856 static inline intptr_t get_next_hash(Thread* current, oop obj) {
 857   intptr_t value = 0;
 858   if (hashCode == 0) {

1431     if (current->is_Java_thread()) {
1432       // A JavaThread must check for a safepoint/handshake and honor it.
1433       chk_for_block_req(JavaThread::cast(current), "deflation", "deflated_count",
1434                         deflated_count, ls, timer_p);
1435     }
1436   }
1437 
1438   return deflated_count;
1439 }
1440 
1441 class HandshakeForDeflation : public HandshakeClosure {
1442  public:
1443   HandshakeForDeflation() : HandshakeClosure("HandshakeForDeflation") {}
1444 
1445   void do_thread(Thread* thread) {
1446     log_trace(monitorinflation)("HandshakeForDeflation::do_thread: thread="
1447                                 INTPTR_FORMAT, p2i(thread));
1448   }
1449 };
1450 
1451 class VM_RendezvousGCThreads : public VM_Operation {
1452 public:
1453   bool evaluate_at_safepoint() const override { return false; }
1454   VMOp_Type type() const override { return VMOp_RendezvousGCThreads; }
1455   void doit() override {
1456     SuspendibleThreadSet::synchronize();
1457     SuspendibleThreadSet::desynchronize();
1458   };
1459 };
1460 
1461 // This function is called by the MonitorDeflationThread to deflate
1462 // ObjectMonitors. It is also called via do_final_audit_and_print_stats()
1463 // by the VMThread.
1464 size_t ObjectSynchronizer::deflate_idle_monitors() {
1465   Thread* current = Thread::current();
1466   if (current->is_Java_thread()) {
1467     // The async deflation request has been processed.
1468     _last_async_deflation_time_ns = os::javaTimeNanos();
1469     set_is_async_deflation_requested(false);
1470   }
1471 
1472   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1473   LogStreamHandle(Info, monitorinflation) lsh_info;
1474   LogStream* ls = NULL;
1475   if (log_is_enabled(Debug, monitorinflation)) {
1476     ls = &lsh_debug;
1477   } else if (log_is_enabled(Info, monitorinflation)) {
1478     ls = &lsh_info;
1479   }
1480 

1493     // deflated, BUT the MonitorDeflationThread blocked for the final
1494     // safepoint during unlinking.
1495 
1496     // Unlink deflated ObjectMonitors from the in-use list.
1497     ResourceMark rm;
1498     GrowableArray<ObjectMonitor*> delete_list((int)deflated_count);
1499     size_t unlinked_count = _in_use_list.unlink_deflated(current, ls, &timer,
1500                                                          &delete_list);
1501     if (current->is_Java_thread()) {
1502       if (ls != NULL) {
1503         timer.stop();
1504         ls->print_cr("before handshaking: unlinked_count=" SIZE_FORMAT
1505                      ", in_use_list stats: ceiling=" SIZE_FORMAT ", count="
1506                      SIZE_FORMAT ", max=" SIZE_FORMAT,
1507                      unlinked_count, in_use_list_ceiling(),
1508                      _in_use_list.count(), _in_use_list.max());
1509       }
1510 
1511       // A JavaThread needs to handshake in order to safely free the
1512       // ObjectMonitors that were deflated in this cycle.
1513       // Also, we sync and desync GC threads around the handshake, so that they can
1514       // safely read the mark-word and look-through to the object-monitor, without
1515       // being afraid that the object-monitor is going away.
1516       HandshakeForDeflation hfd_hc;
1517       Handshake::execute(&hfd_hc);
1518       VM_RendezvousGCThreads sync_gc;
1519       VMThread::execute(&sync_gc);
1520 
1521       if (ls != NULL) {
1522         ls->print_cr("after handshaking: in_use_list stats: ceiling="
1523                      SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1524                      in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1525         timer.start();
1526       }
1527     }
1528 
1529     // After the handshake, safely free the ObjectMonitors that were
1530     // deflated in this cycle.
1531     size_t deleted_count = 0;
1532     for (ObjectMonitor* monitor: delete_list) {
1533       delete monitor;
1534       deleted_count++;
1535 
1536       if (current->is_Java_thread()) {
1537         // A JavaThread must check for a safepoint/handshake and honor it.
1538         chk_for_block_req(JavaThread::cast(current), "deletion", "deleted_count",
1539                           deleted_count, ls, &timer);
< prev index next >