< 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/frame.inline.hpp"
  38 #include "runtime/handles.inline.hpp"
  39 #include "runtime/handshake.hpp"
  40 #include "runtime/interfaceSupport.inline.hpp"
  41 #include "runtime/javaThread.hpp"
  42 #include "runtime/mutexLocker.hpp"
  43 #include "runtime/objectMonitor.hpp"
  44 #include "runtime/objectMonitor.inline.hpp"
  45 #include "runtime/os.inline.hpp"
  46 #include "runtime/osThread.hpp"
  47 #include "runtime/perfData.hpp"

 726   ObjectMonitor* monitor = inflate(current, obj(), inflate_cause_notify);
 727   monitor->notifyAll(CHECK);
 728 }
 729 
 730 // -----------------------------------------------------------------------------
 731 // Hash Code handling
 732 
 733 struct SharedGlobals {
 734   char         _pad_prefix[OM_CACHE_LINE_SIZE];
 735   // This is a highly shared mostly-read variable.
 736   // To avoid false-sharing it needs to be the sole occupant of a cache line.
 737   volatile int stw_random;
 738   DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(volatile int));
 739   // Hot RW variable -- Sequester to avoid false-sharing
 740   volatile int hc_sequence;
 741   DEFINE_PAD_MINUS_SIZE(2, OM_CACHE_LINE_SIZE, sizeof(volatile int));
 742 };
 743 
 744 static SharedGlobals GVars;
 745 
 746 static markWord read_stable_mark(oop obj) {
 747   markWord mark = obj->mark_acquire();
 748   if (!mark.is_being_inflated()) {
 749     return mark;       // normal fast-path return
 750   }
 751 
 752   int its = 0;
 753   for (;;) {
 754     markWord mark = obj->mark_acquire();
 755     if (!mark.is_being_inflated()) {
 756       return mark;    // normal fast-path return
 757     }
 758 
 759     // The object is being inflated by some other thread.
 760     // The caller of read_stable_mark() must wait for inflation to complete.
 761     // Avoid live-lock.
 762 
 763     ++its;
 764     if (its > 10000 || !os::is_MP()) {
 765       if (its & 1) {
 766         os::naked_yield();

 786         assert(ix >= 0 && ix < NINFLATIONLOCKS, "invariant");
 787         gInflationLocks[ix]->lock();
 788         while (obj->mark_acquire() == markWord::INFLATING()) {
 789           // Beware: naked_yield() is advisory and has almost no effect on some platforms
 790           // so we periodically call current->_ParkEvent->park(1).
 791           // We use a mixed spin/yield/block mechanism.
 792           if ((YieldThenBlock++) >= 16) {
 793             Thread::current()->_ParkEvent->park(1);
 794           } else {
 795             os::naked_yield();
 796           }
 797         }
 798         gInflationLocks[ix]->unlock();
 799       }
 800     } else {
 801       SpinPause();       // SMP-polite spinning
 802     }
 803   }
 804 }
 805 



































































 806 // hashCode() generation :
 807 //
 808 // Possibilities:
 809 // * MD5Digest of {obj,stw_random}
 810 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
 811 // * A DES- or AES-style SBox[] mechanism
 812 // * One of the Phi-based schemes, such as:
 813 //   2654435761 = 2^32 * Phi (golden ratio)
 814 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
 815 // * A variation of Marsaglia's shift-xor RNG scheme.
 816 // * (obj ^ stw_random) is appealing, but can result
 817 //   in undesirable regularity in the hashCode values of adjacent objects
 818 //   (objects allocated back-to-back, in particular).  This could potentially
 819 //   result in hashtable collisions and reduced hashtable efficiency.
 820 //   There are simple ways to "diffuse" the middle address bits over the
 821 //   generated hashCode values:
 822 
 823 static inline intptr_t get_next_hash(Thread* current, oop obj) {
 824   intptr_t value = 0;
 825   if (hashCode == 0) {

1454     if (current->is_Java_thread()) {
1455       // A JavaThread must check for a safepoint/handshake and honor it.
1456       chk_for_block_req(JavaThread::cast(current), "deflation", "deflated_count",
1457                         deflated_count, ls, timer_p);
1458     }
1459   }
1460 
1461   return deflated_count;
1462 }
1463 
1464 class HandshakeForDeflation : public HandshakeClosure {
1465  public:
1466   HandshakeForDeflation() : HandshakeClosure("HandshakeForDeflation") {}
1467 
1468   void do_thread(Thread* thread) {
1469     log_trace(monitorinflation)("HandshakeForDeflation::do_thread: thread="
1470                                 INTPTR_FORMAT, p2i(thread));
1471   }
1472 };
1473 










1474 // This function is called by the MonitorDeflationThread to deflate
1475 // ObjectMonitors. It is also called via do_final_audit_and_print_stats()
1476 // and VM_ThreadDump::doit() by the VMThread.
1477 size_t ObjectSynchronizer::deflate_idle_monitors(ObjectMonitorsHashtable* table) {
1478   Thread* current = Thread::current();
1479   if (current->is_Java_thread()) {
1480     // The async deflation request has been processed.
1481     _last_async_deflation_time_ns = os::javaTimeNanos();
1482     set_is_async_deflation_requested(false);
1483   }
1484 
1485   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1486   LogStreamHandle(Info, monitorinflation) lsh_info;
1487   LogStream* ls = NULL;
1488   if (log_is_enabled(Debug, monitorinflation)) {
1489     ls = &lsh_debug;
1490   } else if (log_is_enabled(Info, monitorinflation)) {
1491     ls = &lsh_info;
1492   }
1493 

1506     // deflated, BUT the MonitorDeflationThread blocked for the final
1507     // safepoint during unlinking.
1508 
1509     // Unlink deflated ObjectMonitors from the in-use list.
1510     ResourceMark rm;
1511     GrowableArray<ObjectMonitor*> delete_list((int)deflated_count);
1512     size_t unlinked_count = _in_use_list.unlink_deflated(current, ls, &timer,
1513                                                          &delete_list);
1514     if (current->is_Java_thread()) {
1515       if (ls != NULL) {
1516         timer.stop();
1517         ls->print_cr("before handshaking: unlinked_count=" SIZE_FORMAT
1518                      ", in_use_list stats: ceiling=" SIZE_FORMAT ", count="
1519                      SIZE_FORMAT ", max=" SIZE_FORMAT,
1520                      unlinked_count, in_use_list_ceiling(),
1521                      _in_use_list.count(), _in_use_list.max());
1522       }
1523 
1524       // A JavaThread needs to handshake in order to safely free the
1525       // ObjectMonitors that were deflated in this cycle.



1526       HandshakeForDeflation hfd_hc;
1527       Handshake::execute(&hfd_hc);


1528 
1529       if (ls != NULL) {
1530         ls->print_cr("after handshaking: in_use_list stats: ceiling="
1531                      SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1532                      in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1533         timer.start();
1534       }
1535     }
1536 
1537     // After the handshake, safely free the ObjectMonitors that were
1538     // deflated in this cycle.
1539     size_t deleted_count = 0;
1540     for (ObjectMonitor* monitor: delete_list) {
1541       delete monitor;
1542       deleted_count++;
1543 
1544       if (current->is_Java_thread()) {
1545         // A JavaThread must check for a safepoint/handshake and honor it.
1546         chk_for_block_req(JavaThread::cast(current), "deletion", "deleted_count",
1547                           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/frame.inline.hpp"
  39 #include "runtime/handles.inline.hpp"
  40 #include "runtime/handshake.hpp"
  41 #include "runtime/interfaceSupport.inline.hpp"
  42 #include "runtime/javaThread.hpp"
  43 #include "runtime/mutexLocker.hpp"
  44 #include "runtime/objectMonitor.hpp"
  45 #include "runtime/objectMonitor.inline.hpp"
  46 #include "runtime/os.inline.hpp"
  47 #include "runtime/osThread.hpp"
  48 #include "runtime/perfData.hpp"

 727   ObjectMonitor* monitor = inflate(current, obj(), inflate_cause_notify);
 728   monitor->notifyAll(CHECK);
 729 }
 730 
 731 // -----------------------------------------------------------------------------
 732 // Hash Code handling
 733 
 734 struct SharedGlobals {
 735   char         _pad_prefix[OM_CACHE_LINE_SIZE];
 736   // This is a highly shared mostly-read variable.
 737   // To avoid false-sharing it needs to be the sole occupant of a cache line.
 738   volatile int stw_random;
 739   DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(volatile int));
 740   // Hot RW variable -- Sequester to avoid false-sharing
 741   volatile int hc_sequence;
 742   DEFINE_PAD_MINUS_SIZE(2, OM_CACHE_LINE_SIZE, sizeof(volatile int));
 743 };
 744 
 745 static SharedGlobals GVars;
 746 
 747 markWord ObjectSynchronizer::read_stable_mark(oop obj) {
 748   markWord mark = obj->mark_acquire();
 749   if (!mark.is_being_inflated()) {
 750     return mark;       // normal fast-path return
 751   }
 752 
 753   int its = 0;
 754   for (;;) {
 755     markWord mark = obj->mark_acquire();
 756     if (!mark.is_being_inflated()) {
 757       return mark;    // normal fast-path return
 758     }
 759 
 760     // The object is being inflated by some other thread.
 761     // The caller of read_stable_mark() must wait for inflation to complete.
 762     // Avoid live-lock.
 763 
 764     ++its;
 765     if (its > 10000 || !os::is_MP()) {
 766       if (its & 1) {
 767         os::naked_yield();

 787         assert(ix >= 0 && ix < NINFLATIONLOCKS, "invariant");
 788         gInflationLocks[ix]->lock();
 789         while (obj->mark_acquire() == markWord::INFLATING()) {
 790           // Beware: naked_yield() is advisory and has almost no effect on some platforms
 791           // so we periodically call current->_ParkEvent->park(1).
 792           // We use a mixed spin/yield/block mechanism.
 793           if ((YieldThenBlock++) >= 16) {
 794             Thread::current()->_ParkEvent->park(1);
 795           } else {
 796             os::naked_yield();
 797           }
 798         }
 799         gInflationLocks[ix]->unlock();
 800       }
 801     } else {
 802       SpinPause();       // SMP-polite spinning
 803     }
 804   }
 805 }
 806 
 807 // Safely load a mark word from an object, even with racing stack-locking or monitor inflation.
 808 // The protocol is a partial inflation-protocol: it installs INFLATING into the object's mark
 809 // word in order to prevent an stack-locks or inflations from interferring (or detect such
 810 // interference and retry), but then, instead of creating and installing a monitor, simply
 811 // read and return the real mark word.
 812 markWord ObjectSynchronizer::stable_mark(oop object) {
 813   for (;;) {
 814     const markWord mark = read_stable_mark(object);
 815     assert(!mark.is_being_inflated(), "read_stable_mark must prevent inflating mark");
 816 
 817     // The mark can be in one of the following states:
 818     // *  Inflated     - just return mark from inflated monitor
 819     // *  Stack-locked - coerce it to inflating, and then return displaced mark
 820     // *  Neutral      - return mark
 821     // *  Marked       - return mark
 822 
 823     // CASE: inflated
 824     if (mark.has_monitor()) {
 825       ObjectMonitor* inf = mark.monitor();
 826       markWord dmw = inf->header();
 827       assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
 828       return dmw;
 829     }
 830 
 831     // CASE: stack-locked
 832     // Could be stack-locked either by this thread or by some other thread.
 833     if (mark.has_locker()) {
 834       BasicLock* lock = mark.locker();
 835       if (Thread::current()->is_lock_owned((address)lock)) {
 836         // If locked by this thread, it is safe to access the displaced header.
 837         markWord dmw = lock->displaced_header();
 838         assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
 839         return dmw;
 840       }
 841 
 842       // Otherwise, attempt to temporarily install INFLATING into the mark-word,
 843       // to prevent inflation or unlocking by competing thread.
 844       markWord cmp = object->cas_set_mark(markWord::INFLATING(), mark);
 845       if (cmp != mark) {
 846         continue;       // Interference -- just retry
 847       }
 848 
 849       // fetch the displaced mark from the owner's stack.
 850       // The owner can't die or unwind past the lock while our INFLATING
 851       // object is in the mark.  Furthermore the owner can't complete
 852       // an unlock on the object, either.
 853       markWord dmw = mark.displaced_mark_helper();
 854       // Catch if the object's header is not neutral (not locked and
 855       // not marked is what we care about here).
 856       assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
 857 
 858       // Must preserve store ordering. The monitor state must
 859       // be stable at the time of publishing the monitor address.
 860       assert(object->mark() == markWord::INFLATING(), "invariant");
 861       // Release semantics so that above set_object() is seen first.
 862       object->release_set_mark(mark);
 863 
 864       return dmw;
 865     }
 866 
 867     // CASE: neutral or marked (for GC)
 868     // Catch if the object's header is not neutral or marked (it must not be locked).
 869     assert(mark.is_neutral() || mark.is_marked(), "invariant: header=" INTPTR_FORMAT, mark.value());
 870     return mark;
 871   }
 872 }
 873 
 874 // hashCode() generation :
 875 //
 876 // Possibilities:
 877 // * MD5Digest of {obj,stw_random}
 878 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
 879 // * A DES- or AES-style SBox[] mechanism
 880 // * One of the Phi-based schemes, such as:
 881 //   2654435761 = 2^32 * Phi (golden ratio)
 882 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
 883 // * A variation of Marsaglia's shift-xor RNG scheme.
 884 // * (obj ^ stw_random) is appealing, but can result
 885 //   in undesirable regularity in the hashCode values of adjacent objects
 886 //   (objects allocated back-to-back, in particular).  This could potentially
 887 //   result in hashtable collisions and reduced hashtable efficiency.
 888 //   There are simple ways to "diffuse" the middle address bits over the
 889 //   generated hashCode values:
 890 
 891 static inline intptr_t get_next_hash(Thread* current, oop obj) {
 892   intptr_t value = 0;
 893   if (hashCode == 0) {

1522     if (current->is_Java_thread()) {
1523       // A JavaThread must check for a safepoint/handshake and honor it.
1524       chk_for_block_req(JavaThread::cast(current), "deflation", "deflated_count",
1525                         deflated_count, ls, timer_p);
1526     }
1527   }
1528 
1529   return deflated_count;
1530 }
1531 
1532 class HandshakeForDeflation : public HandshakeClosure {
1533  public:
1534   HandshakeForDeflation() : HandshakeClosure("HandshakeForDeflation") {}
1535 
1536   void do_thread(Thread* thread) {
1537     log_trace(monitorinflation)("HandshakeForDeflation::do_thread: thread="
1538                                 INTPTR_FORMAT, p2i(thread));
1539   }
1540 };
1541 
1542 class VM_RendezvousGCThreads : public VM_Operation {
1543 public:
1544   bool evaluate_at_safepoint() const override { return false; }
1545   VMOp_Type type() const override { return VMOp_RendezvousGCThreads; }
1546   void doit() override {
1547     SuspendibleThreadSet::synchronize();
1548     SuspendibleThreadSet::desynchronize();
1549   };
1550 };
1551 
1552 // This function is called by the MonitorDeflationThread to deflate
1553 // ObjectMonitors. It is also called via do_final_audit_and_print_stats()
1554 // and VM_ThreadDump::doit() by the VMThread.
1555 size_t ObjectSynchronizer::deflate_idle_monitors(ObjectMonitorsHashtable* table) {
1556   Thread* current = Thread::current();
1557   if (current->is_Java_thread()) {
1558     // The async deflation request has been processed.
1559     _last_async_deflation_time_ns = os::javaTimeNanos();
1560     set_is_async_deflation_requested(false);
1561   }
1562 
1563   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1564   LogStreamHandle(Info, monitorinflation) lsh_info;
1565   LogStream* ls = NULL;
1566   if (log_is_enabled(Debug, monitorinflation)) {
1567     ls = &lsh_debug;
1568   } else if (log_is_enabled(Info, monitorinflation)) {
1569     ls = &lsh_info;
1570   }
1571 

1584     // deflated, BUT the MonitorDeflationThread blocked for the final
1585     // safepoint during unlinking.
1586 
1587     // Unlink deflated ObjectMonitors from the in-use list.
1588     ResourceMark rm;
1589     GrowableArray<ObjectMonitor*> delete_list((int)deflated_count);
1590     size_t unlinked_count = _in_use_list.unlink_deflated(current, ls, &timer,
1591                                                          &delete_list);
1592     if (current->is_Java_thread()) {
1593       if (ls != NULL) {
1594         timer.stop();
1595         ls->print_cr("before handshaking: unlinked_count=" SIZE_FORMAT
1596                      ", in_use_list stats: ceiling=" SIZE_FORMAT ", count="
1597                      SIZE_FORMAT ", max=" SIZE_FORMAT,
1598                      unlinked_count, in_use_list_ceiling(),
1599                      _in_use_list.count(), _in_use_list.max());
1600       }
1601 
1602       // A JavaThread needs to handshake in order to safely free the
1603       // ObjectMonitors that were deflated in this cycle.
1604       // Also, we sync and desync GC threads around the handshake, so that they can
1605       // safely read the mark-word and look-through to the object-monitor, without
1606       // being afraid that the object-monitor is going away.
1607       HandshakeForDeflation hfd_hc;
1608       Handshake::execute(&hfd_hc);
1609       VM_RendezvousGCThreads sync_gc;
1610       VMThread::execute(&sync_gc);
1611 
1612       if (ls != NULL) {
1613         ls->print_cr("after handshaking: in_use_list stats: ceiling="
1614                      SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1615                      in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1616         timer.start();
1617       }
1618     }
1619 
1620     // After the handshake, safely free the ObjectMonitors that were
1621     // deflated in this cycle.
1622     size_t deleted_count = 0;
1623     for (ObjectMonitor* monitor: delete_list) {
1624       delete monitor;
1625       deleted_count++;
1626 
1627       if (current->is_Java_thread()) {
1628         // A JavaThread must check for a safepoint/handshake and honor it.
1629         chk_for_block_req(JavaThread::cast(current), "deletion", "deleted_count",
1630                           deleted_count, ls, &timer);
< prev index next >