< 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"

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

 774         assert(ix >= 0 && ix < NINFLATIONLOCKS, "invariant");
 775         gInflationLocks[ix]->lock();
 776         while (obj->mark_acquire() == markWord::INFLATING()) {
 777           // Beware: naked_yield() is advisory and has almost no effect on some platforms
 778           // so we periodically call current->_ParkEvent->park(1).
 779           // We use a mixed spin/yield/block mechanism.
 780           if ((YieldThenBlock++) >= 16) {
 781             Thread::current()->_ParkEvent->park(1);
 782           } else {
 783             os::naked_yield();
 784           }
 785         }
 786         gInflationLocks[ix]->unlock();
 787       }
 788     } else {
 789       SpinPause();       // SMP-polite spinning
 790     }
 791   }
 792 }
 793 



































































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

1442     if (current->is_Java_thread()) {
1443       // A JavaThread must check for a safepoint/handshake and honor it.
1444       chk_for_block_req(JavaThread::cast(current), "deflation", "deflated_count",
1445                         deflated_count, ls, timer_p);
1446     }
1447   }
1448 
1449   return deflated_count;
1450 }
1451 
1452 class HandshakeForDeflation : public HandshakeClosure {
1453  public:
1454   HandshakeForDeflation() : HandshakeClosure("HandshakeForDeflation") {}
1455 
1456   void do_thread(Thread* thread) {
1457     log_trace(monitorinflation)("HandshakeForDeflation::do_thread: thread="
1458                                 INTPTR_FORMAT, p2i(thread));
1459   }
1460 };
1461 










1462 // This function is called by the MonitorDeflationThread to deflate
1463 // ObjectMonitors. It is also called via do_final_audit_and_print_stats()
1464 // and VM_ThreadDump::doit() by the VMThread.
1465 size_t ObjectSynchronizer::deflate_idle_monitors(ObjectMonitorsHashtable* table) {
1466   Thread* current = Thread::current();
1467   if (current->is_Java_thread()) {
1468     // The async deflation request has been processed.
1469     _last_async_deflation_time_ns = os::javaTimeNanos();
1470     set_is_async_deflation_requested(false);
1471   }
1472 
1473   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1474   LogStreamHandle(Info, monitorinflation) lsh_info;
1475   LogStream* ls = NULL;
1476   if (log_is_enabled(Debug, monitorinflation)) {
1477     ls = &lsh_debug;
1478   } else if (log_is_enabled(Info, monitorinflation)) {
1479     ls = &lsh_info;
1480   }
1481 

1494     // deflated, BUT the MonitorDeflationThread blocked for the final
1495     // safepoint during unlinking.
1496 
1497     // Unlink deflated ObjectMonitors from the in-use list.
1498     ResourceMark rm;
1499     GrowableArray<ObjectMonitor*> delete_list((int)deflated_count);
1500     size_t unlinked_count = _in_use_list.unlink_deflated(current, ls, &timer,
1501                                                          &delete_list);
1502     if (current->is_Java_thread()) {
1503       if (ls != NULL) {
1504         timer.stop();
1505         ls->print_cr("before handshaking: unlinked_count=" SIZE_FORMAT
1506                      ", in_use_list stats: ceiling=" SIZE_FORMAT ", count="
1507                      SIZE_FORMAT ", max=" SIZE_FORMAT,
1508                      unlinked_count, in_use_list_ceiling(),
1509                      _in_use_list.count(), _in_use_list.max());
1510       }
1511 
1512       // A JavaThread needs to handshake in order to safely free the
1513       // ObjectMonitors that were deflated in this cycle.



1514       HandshakeForDeflation hfd_hc;
1515       Handshake::execute(&hfd_hc);


1516 
1517       if (ls != NULL) {
1518         ls->print_cr("after handshaking: in_use_list stats: ceiling="
1519                      SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1520                      in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1521         timer.start();
1522       }
1523     }
1524 
1525     // After the handshake, safely free the ObjectMonitors that were
1526     // deflated in this cycle.
1527     size_t deleted_count = 0;
1528     for (ObjectMonitor* monitor: delete_list) {
1529       delete monitor;
1530       deleted_count++;
1531 
1532       if (current->is_Java_thread()) {
1533         // A JavaThread must check for a safepoint/handshake and honor it.
1534         chk_for_block_req(JavaThread::cast(current), "deletion", "deleted_count",
1535                           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"

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

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

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

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