src/share/vm/runtime/objectMonitor.hpp

Mon, 10 Jun 2013 11:30:51 +0200

author
sla
date
Mon, 10 Jun 2013 11:30:51 +0200
changeset 5237
f2110083203d
parent 5103
f9be75d21404
child 5614
9758d9f36299
permissions
-rw-r--r--

8005849: JEP 167: Event-Based JVM Tracing
Reviewed-by: acorn, coleenp, sla
Contributed-by: Karen Kinnear <karen.kinnear@oracle.com>, Bengt Rutisson <bengt.rutisson@oracle.com>, Calvin Cheung <calvin.cheung@oracle.com>, Erik Gahlin <erik.gahlin@oracle.com>, Erik Helin <erik.helin@oracle.com>, Jesper Wilhelmsson <jesper.wilhelmsson@oracle.com>, Keith McGuigan <keith.mcguigan@oracle.com>, Mattias Tobiasson <mattias.tobiasson@oracle.com>, Markus Gronlund <markus.gronlund@oracle.com>, Mikael Auno <mikael.auno@oracle.com>, Nils Eliasson <nils.eliasson@oracle.com>, Nils Loodin <nils.loodin@oracle.com>, Rickard Backman <rickard.backman@oracle.com>, Staffan Larsen <staffan.larsen@oracle.com>, Stefan Karlsson <stefan.karlsson@oracle.com>, Yekaterina Kantserova <yekaterina.kantserova@oracle.com>

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

mercurial