src/share/vm/runtime/objectMonitor.hpp

Thu, 24 Nov 2016 11:27:57 +0100

author
tschatzl
date
Thu, 24 Nov 2016 11:27:57 +0100
changeset 9982
72053ed6f8d4
parent 9858
b985cbb00e68
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

8057003: Large reference arrays cause extremely long synchronization times
Summary: Slice large object arrays into parts so that the synchronization of marking threads with an STW pause request does not take long.
Reviewed-by: ehelin, pliden
Contributed-by: maoliang.ml@alibaba-inc.com

     1 /*
     2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     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  */
    25 #ifndef SHARE_VM_RUNTIME_OBJECTMONITOR_HPP
    26 #define SHARE_VM_RUNTIME_OBJECTMONITOR_HPP
    28 #include "runtime/os.hpp"
    29 #include "runtime/park.hpp"
    30 #include "runtime/perfData.hpp"
    32 // ObjectWaiter serves as a "proxy" or surrogate thread.
    33 // TODO-FIXME: Eliminate ObjectWaiter and use the thread-specific
    34 // ParkEvent instead.  Beware, however, that the JVMTI code
    35 // knows about ObjectWaiters, so we'll have to reconcile that code.
    36 // See next_waiter(), first_waiter(), etc.
    38 class ObjectWaiter : public StackObj {
    39  public:
    40   enum TStates { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER, TS_CXQ } ;
    41   enum Sorted  { PREPEND, APPEND, SORTED } ;
    42   ObjectWaiter * volatile _next;
    43   ObjectWaiter * volatile _prev;
    44   Thread*       _thread;
    45   jlong         _notifier_tid;
    46   ParkEvent *   _event;
    47   volatile int  _notified ;
    48   volatile TStates TState ;
    49   Sorted        _Sorted ;           // List placement disposition
    50   bool          _active ;           // Contention monitoring is enabled
    51  public:
    52   ObjectWaiter(Thread* thread);
    54   void wait_reenter_begin(ObjectMonitor *mon);
    55   void wait_reenter_end(ObjectMonitor *mon);
    56 };
    58 // WARNING:
    59 //   This is a very sensitive and fragile class. DO NOT make any
    60 // change unless you are fully aware of the underlying semantics.
    62 //   This class can not inherit from any other class, because I have
    63 // to let the displaced header be the very first word. Otherwise I
    64 // have to let markOop include this file, which would export the
    65 // monitor data structure to everywhere.
    66 //
    67 // The ObjectMonitor class is used to implement JavaMonitors which have
    68 // transformed from the lightweight structure of the thread stack to a
    69 // heavy weight lock due to contention
    71 // It is also used as RawMonitor by the JVMTI
    74 class ObjectMonitor {
    75  public:
    76   enum {
    77     OM_OK,                    // no error
    78     OM_SYSTEM_ERROR,          // operating system error
    79     OM_ILLEGAL_MONITOR_STATE, // IllegalMonitorStateException
    80     OM_INTERRUPTED,           // Thread.interrupt()
    81     OM_TIMED_OUT              // Object.wait() timed out
    82   };
    84  public:
    85   // TODO-FIXME: the "offset" routines should return a type of off_t instead of int ...
    86   // ByteSize would also be an appropriate type.
    87   static int header_offset_in_bytes()      { return offset_of(ObjectMonitor, _header);     }
    88   static int object_offset_in_bytes()      { return offset_of(ObjectMonitor, _object);     }
    89   static int owner_offset_in_bytes()       { return offset_of(ObjectMonitor, _owner);      }
    90   static int count_offset_in_bytes()       { return offset_of(ObjectMonitor, _count);      }
    91   static int recursions_offset_in_bytes()  { return offset_of(ObjectMonitor, _recursions); }
    92   static int cxq_offset_in_bytes()         { return offset_of(ObjectMonitor, _cxq) ;       }
    93   static int succ_offset_in_bytes()        { return offset_of(ObjectMonitor, _succ) ;      }
    94   static int EntryList_offset_in_bytes()   { return offset_of(ObjectMonitor, _EntryList);  }
    95   static int FreeNext_offset_in_bytes()    { return offset_of(ObjectMonitor, FreeNext);    }
    96   static int WaitSet_offset_in_bytes()     { return offset_of(ObjectMonitor, _WaitSet) ;   }
    97   static int Responsible_offset_in_bytes() { return offset_of(ObjectMonitor, _Responsible);}
    98   static int Spinner_offset_in_bytes()     { return offset_of(ObjectMonitor, _Spinner);    }
   100  public:
   101   // Eventaully we'll make provisions for multiple callbacks, but
   102   // now one will suffice.
   103   static int (*SpinCallbackFunction)(intptr_t, int) ;
   104   static intptr_t SpinCallbackArgument ;
   107  public:
   108   markOop   header() const;
   109   void      set_header(markOop hdr);
   111   intptr_t is_busy() const {
   112     // TODO-FIXME: merge _count and _waiters.
   113     // TODO-FIXME: assert _owner == null implies _recursions = 0
   114     // TODO-FIXME: assert _WaitSet != null implies _count > 0
   115     return _count|_waiters|intptr_t(_owner)|intptr_t(_cxq)|intptr_t(_EntryList ) ;
   116   }
   118   intptr_t  is_entered(Thread* current) const;
   120   void*     owner() const;
   121   void      set_owner(void* owner);
   123   intptr_t  waiters() const;
   125   intptr_t  count() const;
   126   void      set_count(intptr_t count);
   127   intptr_t  contentions() const ;
   128   intptr_t  recursions() const                                         { return _recursions; }
   130   // JVM/DI GetMonitorInfo() needs this
   131   ObjectWaiter* first_waiter()                                         { return _WaitSet; }
   132   ObjectWaiter* next_waiter(ObjectWaiter* o)                           { return o->_next; }
   133   Thread* thread_of_waiter(ObjectWaiter* o)                            { return o->_thread; }
   135   // initialize the monitor, exception the semaphore, all other fields
   136   // are simple integers or pointers
   137   ObjectMonitor() {
   138     _header       = NULL;
   139     _count        = 0;
   140     _waiters      = 0,
   141     _recursions   = 0;
   142     _object       = NULL;
   143     _owner        = NULL;
   144     _WaitSet      = NULL;
   145     _WaitSetLock  = 0 ;
   146     _Responsible  = NULL ;
   147     _succ         = NULL ;
   148     _cxq          = NULL ;
   149     FreeNext      = NULL ;
   150     _EntryList    = NULL ;
   151     _SpinFreq     = 0 ;
   152     _SpinClock    = 0 ;
   153     OwnerIsThread = 0 ;
   154     _previous_owner_tid = 0;
   155   }
   157   ~ObjectMonitor() {
   158    // TODO: Add asserts ...
   159    // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
   160    // _count == 0 _EntryList  == NULL etc
   161   }
   163 private:
   164   void Recycle () {
   165     // TODO: add stronger asserts ...
   166     // _cxq == 0 _succ == NULL _owner == NULL _waiters == 0
   167     // _count == 0 EntryList  == NULL
   168     // _recursions == 0 _WaitSet == NULL
   169     // TODO: assert (is_busy()|_recursions) == 0
   170     _succ          = NULL ;
   171     _EntryList     = NULL ;
   172     _cxq           = NULL ;
   173     _WaitSet       = NULL ;
   174     _recursions    = 0 ;
   175     _SpinFreq      = 0 ;
   176     _SpinClock     = 0 ;
   177     OwnerIsThread  = 0 ;
   178   }
   180 public:
   182   void*     object() const;
   183   void*     object_addr();
   184   void      set_object(void* obj);
   186   bool      check(TRAPS);       // true if the thread owns the monitor.
   187   void      check_slow(TRAPS);
   188   void      clear();
   189   static void sanity_checks();  // public for -XX:+ExecuteInternalVMTests
   190                                 // in PRODUCT for -XX:SyncKnobs=Verbose=1
   191 #ifndef PRODUCT
   192   void      verify();
   193   void      print();
   194 #endif
   196   bool      try_enter (TRAPS) ;
   197   void      enter(TRAPS);
   198   void      exit(bool not_suspended, TRAPS);
   199   void      wait(jlong millis, bool interruptable, TRAPS);
   200   void      notify(TRAPS);
   201   void      notifyAll(TRAPS);
   203 // Use the following at your own risk
   204   intptr_t  complete_exit(TRAPS);
   205   void      reenter(intptr_t recursions, TRAPS);
   207  private:
   208   void      AddWaiter (ObjectWaiter * waiter) ;
   209   static    void DeferredInitialize();
   211   ObjectWaiter * DequeueWaiter () ;
   212   void      DequeueSpecificWaiter (ObjectWaiter * waiter) ;
   213   void      EnterI (TRAPS) ;
   214   void      ReenterI (Thread * Self, ObjectWaiter * SelfNode) ;
   215   void      UnlinkAfterAcquire (Thread * Self, ObjectWaiter * SelfNode) ;
   216   int       TryLock (Thread * Self) ;
   217   int       NotRunnable (Thread * Self, Thread * Owner) ;
   218   int       TrySpin_Fixed (Thread * Self) ;
   219   int       TrySpin_VaryFrequency (Thread * Self) ;
   220   int       TrySpin_VaryDuration  (Thread * Self) ;
   221   void      ctAsserts () ;
   222   void      ExitEpilog (Thread * Self, ObjectWaiter * Wakee) ;
   223   bool      ExitSuspendEquivalent (JavaThread * Self) ;
   225  private:
   226   friend class ObjectSynchronizer;
   227   friend class ObjectWaiter;
   228   friend class VMStructs;
   230   // WARNING: this must be the very first word of ObjectMonitor
   231   // This means this class can't use any virtual member functions.
   233   volatile markOop   _header;       // displaced object header word - mark
   234   void*     volatile _object;       // backward object pointer - strong root
   236   double SharingPad [1] ;           // temp to reduce false sharing
   238   // All the following fields must be machine word aligned
   239   // The VM assumes write ordering wrt these fields, which can be
   240   // read from other threads.
   242  protected:                         // protected for jvmtiRawMonitor
   243   void *  volatile _owner;          // pointer to owning thread OR BasicLock
   244   volatile jlong _previous_owner_tid; // thread id of the previous owner of the monitor
   245   volatile intptr_t  _recursions;   // recursion count, 0 for first entry
   246  private:
   247   int OwnerIsThread ;               // _owner is (Thread *) vs SP/BasicLock
   248   ObjectWaiter * volatile _cxq ;    // LL of recently-arrived threads blocked on entry.
   249                                     // The list is actually composed of WaitNodes, acting
   250                                     // as proxies for Threads.
   251  protected:
   252   ObjectWaiter * volatile _EntryList ;     // Threads blocked on entry or reentry.
   253  private:
   254   Thread * volatile _succ ;          // Heir presumptive thread - used for futile wakeup throttling
   255   Thread * volatile _Responsible ;
   256   int _PromptDrain ;                // rqst to drain cxq into EntryList ASAP
   258   volatile int _Spinner ;           // for exit->spinner handoff optimization
   259   volatile int _SpinFreq ;          // Spin 1-out-of-N attempts: success rate
   260   volatile int _SpinClock ;
   261   volatile int _SpinDuration ;
   262   volatile intptr_t _SpinState ;    // MCS/CLH list of spinners
   264   // TODO-FIXME: _count, _waiters and _recursions should be of
   265   // type int, or int32_t but not intptr_t.  There's no reason
   266   // to use 64-bit fields for these variables on a 64-bit JVM.
   268   volatile intptr_t  _count;        // reference count to prevent reclaimation/deflation
   269                                     // at stop-the-world time.  See deflate_idle_monitors().
   270                                     // _count is approximately |_WaitSet| + |_EntryList|
   271  protected:
   272   volatile intptr_t  _waiters;      // number of waiting threads
   273  private:
   274  protected:
   275   ObjectWaiter * volatile _WaitSet; // LL of threads wait()ing on the monitor
   276  private:
   277   volatile int _WaitSetLock;        // protects Wait Queue - simple spinlock
   279  public:
   280   int _QMix ;                       // Mixed prepend queue discipline
   281   ObjectMonitor * FreeNext ;        // Free list linkage
   282   intptr_t StatA, StatsB ;
   284  public:
   285   static void Initialize () ;
   286   static PerfCounter * _sync_ContendedLockAttempts ;
   287   static PerfCounter * _sync_FutileWakeups ;
   288   static PerfCounter * _sync_Parks ;
   289   static PerfCounter * _sync_EmptyNotifications ;
   290   static PerfCounter * _sync_Notifications ;
   291   static PerfCounter * _sync_SlowEnter ;
   292   static PerfCounter * _sync_SlowExit ;
   293   static PerfCounter * _sync_SlowNotify ;
   294   static PerfCounter * _sync_SlowNotifyAll ;
   295   static PerfCounter * _sync_FailedSpins ;
   296   static PerfCounter * _sync_SuccessfulSpins ;
   297   static PerfCounter * _sync_PrivateA ;
   298   static PerfCounter * _sync_PrivateB ;
   299   static PerfCounter * _sync_MonInCirculation ;
   300   static PerfCounter * _sync_MonScavenged ;
   301   static PerfCounter * _sync_Inflations ;
   302   static PerfCounter * _sync_Deflations ;
   303   static PerfLongVariable * _sync_MonExtant ;
   305  public:
   306   static int Knob_Verbose;
   307   static int Knob_SpinLimit;
   308   void* operator new (size_t size) throw() {
   309     return AllocateHeap(size, mtInternal);
   310   }
   311   void* operator new[] (size_t size) throw() {
   312     return operator new (size);
   313   }
   314   void operator delete(void* p) {
   315     FreeHeap(p, mtInternal);
   316   }
   317   void operator delete[] (void *p) {
   318     operator delete(p);
   319   }
   320 };
   322 #undef TEVENT
   323 #define TEVENT(nom) {if (SyncVerbose) FEVENT(nom); }
   325 #define FEVENT(nom) { static volatile int ctr = 0 ; int v = ++ctr ; if ((v & (v-1)) == 0) { ::printf (#nom " : %d \n", v); ::fflush(stdout); }}
   327 #undef  TEVENT
   328 #define TEVENT(nom) {;}
   331 #endif // SHARE_VM_RUNTIME_OBJECTMONITOR_HPP

mercurial