1 /*
2 * Copyright (c) 1998, 2014, 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 *
86 #define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread) \
87 { \
88 if (DTraceMonitorProbes) { \
89 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \
90 HOTSPOT_MONITOR_##probe(jtid, \
91 (uintptr_t)(monitor), bytes, len); \
92 } \
93 }
94
95 #else // ndef DTRACE_ENABLED
96
97 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon) {;}
98 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon) {;}
99
100 #endif // ndef DTRACE_ENABLED
101
102 // Tunables ...
103 // The knob* variables are effectively final. Once set they should
104 // never be modified hence. Consider using __read_mostly with GCC.
105
106 int ObjectMonitor::Knob_Verbose = 0;
107 int ObjectMonitor::Knob_VerifyInUse = 0;
108 int ObjectMonitor::Knob_SpinLimit = 5000; // derived by an external tool -
109 static int Knob_LogSpins = 0; // enable jvmstat tally for spins
110 static int Knob_HandOff = 0;
111 static int Knob_ReportSettings = 0;
112
113 static int Knob_SpinBase = 0; // Floor AKA SpinMin
114 static int Knob_SpinBackOff = 0; // spin-loop backoff
115 static int Knob_CASPenalty = -1; // Penalty for failed CAS
116 static int Knob_OXPenalty = -1; // Penalty for observed _owner change
117 static int Knob_SpinSetSucc = 1; // spinners set the _succ field
118 static int Knob_SpinEarly = 1;
119 static int Knob_SuccEnabled = 1; // futile wake throttling
120 static int Knob_SuccRestrict = 0; // Limit successors + spinners to at-most-one
121 static int Knob_MaxSpinners = -1; // Should be a function of # CPUs
122 static int Knob_Bonus = 100; // spin success bonus
123 static int Knob_BonusB = 100; // spin success bonus
124 static int Knob_Penalty = 200; // spin failure penalty
125 static int Knob_Poverty = 1000;
126 static int Knob_SpinAfterFutile = 1; // Spin after returning from park()
127 static int Knob_FixedSpin = 0;
234 // unpark the notifyee. Unparking a notifee in notify() is inefficient -
235 // it's likely the notifyee would simply impale itself on the lock held
236 // by the notifier.
237 //
238 // * An interesting alternative is to encode cxq as (List,LockByte) where
239 // the LockByte is 0 iff the monitor is owned. _owner is simply an auxiliary
240 // variable, like _recursions, in the scheme. The threads or Events that form
241 // the list would have to be aligned in 256-byte addresses. A thread would
242 // try to acquire the lock or enqueue itself with CAS, but exiting threads
243 // could use a 1-0 protocol and simply STB to set the LockByte to 0.
244 // Note that is is *not* word-tearing, but it does presume that full-word
245 // CAS operations are coherent with intermix with STB operations. That's true
246 // on most common processors.
247 //
248 // * See also http://blogs.sun.com/dave
249
250
251 // -----------------------------------------------------------------------------
252 // Enter support
253
254 bool ObjectMonitor::try_enter(Thread* THREAD) {
255 if (THREAD != _owner) {
256 if (THREAD->is_lock_owned ((address)_owner)) {
257 assert(_recursions == 0, "internal state error");
258 _owner = THREAD;
259 _recursions = 1;
260 return true;
261 }
262 if (Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) != NULL) {
263 return false;
264 }
265 return true;
266 } else {
267 _recursions++;
268 return true;
269 }
270 }
271
272 void NOINLINE ObjectMonitor::enter(TRAPS) {
273 // The following code is ordered to check the most common cases first
274 // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
275 Thread * const Self = THREAD;
276
277 void * cur = Atomic::cmpxchg_ptr (Self, &_owner, NULL);
278 if (cur == NULL) {
279 // Either ASSERT _recursions == 0 or explicitly set _recursions = 0.
280 assert(_recursions == 0, "invariant");
281 assert(_owner == Self, "invariant");
282 return;
283 }
284
285 if (cur == Self) {
286 // TODO-FIXME: check for integer overflow! BUGID 6557169.
287 _recursions++;
288 return;
289 }
290
291 if (Self->is_lock_owned ((address)cur)) {
2255 _prev = NULL;
2256 _notified = 0;
2257 TState = TS_RUN;
2258 _thread = thread;
2259 _event = thread->_ParkEvent;
2260 _active = false;
2261 assert(_event != NULL, "invariant");
2262 }
2263
2264 void ObjectWaiter::wait_reenter_begin(ObjectMonitor * const mon) {
2265 JavaThread *jt = (JavaThread *)this->_thread;
2266 _active = JavaThreadBlockedOnMonitorEnterState::wait_reenter_begin(jt, mon);
2267 }
2268
2269 void ObjectWaiter::wait_reenter_end(ObjectMonitor * const mon) {
2270 JavaThread *jt = (JavaThread *)this->_thread;
2271 JavaThreadBlockedOnMonitorEnterState::wait_reenter_end(jt, _active);
2272 }
2273
2274 inline void ObjectMonitor::AddWaiter(ObjectWaiter* node) {
2275 assert(node != NULL, "should not dequeue NULL node");
2276 assert(node->_prev == NULL, "node already in list");
2277 assert(node->_next == NULL, "node already in list");
2278 // put node at end of queue (circular doubly linked list)
2279 if (_WaitSet == NULL) {
2280 _WaitSet = node;
2281 node->_prev = node;
2282 node->_next = node;
2283 } else {
2284 ObjectWaiter* head = _WaitSet;
2285 ObjectWaiter* tail = head->_prev;
2286 assert(tail->_next == head, "invariant check");
2287 tail->_next = node;
2288 head->_prev = node;
2289 node->_next = head;
2290 node->_prev = tail;
2291 }
2292 }
2293
2294 inline ObjectWaiter* ObjectMonitor::DequeueWaiter() {
2295 // dequeue the very first waiter
2390 }
2391 }
2392
2393 static char * kvGet(char * kvList, const char * Key) {
2394 if (kvList == NULL) return NULL;
2395 size_t n = strlen(Key);
2396 char * Search;
2397 for (Search = kvList; *Search; Search += strlen(Search) + 1) {
2398 if (strncmp (Search, Key, n) == 0) {
2399 if (Search[n] == '=') return Search + n + 1;
2400 if (Search[n] == 0) return(char *) "1";
2401 }
2402 }
2403 return NULL;
2404 }
2405
2406 static int kvGetInt(char * kvList, const char * Key, int Default) {
2407 char * v = kvGet(kvList, Key);
2408 int rslt = v ? ::strtol(v, NULL, 0) : Default;
2409 if (Knob_ReportSettings && v != NULL) {
2410 ::printf (" SyncKnob: %s %d(%d)\n", Key, rslt, Default) ;
2411 ::fflush(stdout);
2412 }
2413 return rslt;
2414 }
2415
2416 void ObjectMonitor::DeferredInitialize() {
2417 if (InitDone > 0) return;
2418 if (Atomic::cmpxchg (-1, &InitDone, 0) != 0) {
2419 while (InitDone != 1) /* empty */;
2420 return;
2421 }
2422
2423 // One-shot global initialization ...
2424 // The initialization is idempotent, so we don't need locks.
2425 // In the future consider doing this via os::init_2().
2426 // SyncKnobs consist of <Key>=<Value> pairs in the style
2427 // of environment variables. Start by converting ':' to NUL.
2428
2429 if (SyncKnobs == NULL) SyncKnobs = "";
2430
2431 size_t sz = strlen(SyncKnobs);
2432 char * knobs = (char *) malloc(sz + 2);
2433 if (knobs == NULL) {
2434 vm_exit_out_of_memory(sz + 2, OOM_MALLOC_ERROR, "Parse SyncKnobs");
2435 guarantee(0, "invariant");
2436 }
2437 strcpy(knobs, SyncKnobs);
2438 knobs[sz+1] = 0;
2439 for (char * p = knobs; *p; p++) {
2440 if (*p == ':') *p = 0;
2441 }
2442
2443 #define SETKNOB(x) { Knob_##x = kvGetInt(knobs, #x, Knob_##x); }
2444 SETKNOB(ReportSettings);
2445 SETKNOB(Verbose);
2446 SETKNOB(VerifyInUse);
2447 SETKNOB(FixedSpin);
2448 SETKNOB(SpinLimit);
2449 SETKNOB(SpinBase);
2450 SETKNOB(SpinBackOff);
2451 SETKNOB(CASPenalty);
2452 SETKNOB(OXPenalty);
2453 SETKNOB(LogSpins);
2454 SETKNOB(SpinSetSucc);
2455 SETKNOB(SuccEnabled);
2456 SETKNOB(SuccRestrict);
2457 SETKNOB(Penalty);
2458 SETKNOB(Bonus);
2459 SETKNOB(BonusB);
2460 SETKNOB(Poverty);
2461 SETKNOB(SpinAfterFutile);
2462 SETKNOB(UsePause);
2463 SETKNOB(SpinEarly);
2464 SETKNOB(OState);
2465 SETKNOB(MaxSpinners);
2466 SETKNOB(PreSpin);
2467 SETKNOB(ExitPolicy);
2468 SETKNOB(QMode);
2469 SETKNOB(ResetEvent);
2470 SETKNOB(MoveNotifyee);
2471 SETKNOB(FastHSSEC);
2472 #undef SETKNOB
2473
2474 if (Knob_Verbose) {
2475 sanity_checks();
2476 }
2477
2478 if (os::is_MP()) {
2479 BackOffMask = (1 << Knob_SpinBackOff) - 1;
2480 if (Knob_ReportSettings) ::printf("BackOffMask=%X\n", BackOffMask);
2481 // CONSIDER: BackOffMask = ROUNDUP_NEXT_POWER2 (ncpus-1)
2482 } else {
2483 Knob_SpinLimit = 0;
2484 Knob_SpinBase = 0;
2485 Knob_PreSpin = 0;
2486 Knob_FixedSpin = -1;
2487 }
2488
2489 if (Knob_LogSpins == 0) {
2490 ObjectMonitor::_sync_FailedSpins = NULL;
2491 }
2492
2493 free(knobs);
2494 OrderAccess::fence();
2495 InitDone = 1;
2496 }
2497
2498 void ObjectMonitor::sanity_checks() {
2499 int error_cnt = 0;
2500 int warning_cnt = 0;
|
1 /*
2 * Copyright (c) 1998, 2015, 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 *
86 #define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread) \
87 { \
88 if (DTraceMonitorProbes) { \
89 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \
90 HOTSPOT_MONITOR_##probe(jtid, \
91 (uintptr_t)(monitor), bytes, len); \
92 } \
93 }
94
95 #else // ndef DTRACE_ENABLED
96
97 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon) {;}
98 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon) {;}
99
100 #endif // ndef DTRACE_ENABLED
101
102 // Tunables ...
103 // The knob* variables are effectively final. Once set they should
104 // never be modified hence. Consider using __read_mostly with GCC.
105
106 int ObjectMonitor::Knob_ExitRelease = 0;
107 int ObjectMonitor::Knob_Verbose = 0;
108 int ObjectMonitor::Knob_VerifyInUse = 0;
109 int ObjectMonitor::Knob_VerifyMatch = 0;
110 int ObjectMonitor::Knob_SpinLimit = 5000; // derived by an external tool -
111 static int Knob_LogSpins = 0; // enable jvmstat tally for spins
112 static int Knob_HandOff = 0;
113 static int Knob_ReportSettings = 0;
114
115 static int Knob_SpinBase = 0; // Floor AKA SpinMin
116 static int Knob_SpinBackOff = 0; // spin-loop backoff
117 static int Knob_CASPenalty = -1; // Penalty for failed CAS
118 static int Knob_OXPenalty = -1; // Penalty for observed _owner change
119 static int Knob_SpinSetSucc = 1; // spinners set the _succ field
120 static int Knob_SpinEarly = 1;
121 static int Knob_SuccEnabled = 1; // futile wake throttling
122 static int Knob_SuccRestrict = 0; // Limit successors + spinners to at-most-one
123 static int Knob_MaxSpinners = -1; // Should be a function of # CPUs
124 static int Knob_Bonus = 100; // spin success bonus
125 static int Knob_BonusB = 100; // spin success bonus
126 static int Knob_Penalty = 200; // spin failure penalty
127 static int Knob_Poverty = 1000;
128 static int Knob_SpinAfterFutile = 1; // Spin after returning from park()
129 static int Knob_FixedSpin = 0;
236 // unpark the notifyee. Unparking a notifee in notify() is inefficient -
237 // it's likely the notifyee would simply impale itself on the lock held
238 // by the notifier.
239 //
240 // * An interesting alternative is to encode cxq as (List,LockByte) where
241 // the LockByte is 0 iff the monitor is owned. _owner is simply an auxiliary
242 // variable, like _recursions, in the scheme. The threads or Events that form
243 // the list would have to be aligned in 256-byte addresses. A thread would
244 // try to acquire the lock or enqueue itself with CAS, but exiting threads
245 // could use a 1-0 protocol and simply STB to set the LockByte to 0.
246 // Note that is is *not* word-tearing, but it does presume that full-word
247 // CAS operations are coherent with intermix with STB operations. That's true
248 // on most common processors.
249 //
250 // * See also http://blogs.sun.com/dave
251
252
253 // -----------------------------------------------------------------------------
254 // Enter support
255
256 void NOINLINE ObjectMonitor::enter(TRAPS) {
257 // The following code is ordered to check the most common cases first
258 // and to reduce RTS->RTO cache line upgrades on SPARC and IA32 processors.
259 Thread * const Self = THREAD;
260
261 void * cur = Atomic::cmpxchg_ptr (Self, &_owner, NULL);
262 if (cur == NULL) {
263 // Either ASSERT _recursions == 0 or explicitly set _recursions = 0.
264 assert(_recursions == 0, "invariant");
265 assert(_owner == Self, "invariant");
266 return;
267 }
268
269 if (cur == Self) {
270 // TODO-FIXME: check for integer overflow! BUGID 6557169.
271 _recursions++;
272 return;
273 }
274
275 if (Self->is_lock_owned ((address)cur)) {
2239 _prev = NULL;
2240 _notified = 0;
2241 TState = TS_RUN;
2242 _thread = thread;
2243 _event = thread->_ParkEvent;
2244 _active = false;
2245 assert(_event != NULL, "invariant");
2246 }
2247
2248 void ObjectWaiter::wait_reenter_begin(ObjectMonitor * const mon) {
2249 JavaThread *jt = (JavaThread *)this->_thread;
2250 _active = JavaThreadBlockedOnMonitorEnterState::wait_reenter_begin(jt, mon);
2251 }
2252
2253 void ObjectWaiter::wait_reenter_end(ObjectMonitor * const mon) {
2254 JavaThread *jt = (JavaThread *)this->_thread;
2255 JavaThreadBlockedOnMonitorEnterState::wait_reenter_end(jt, _active);
2256 }
2257
2258 inline void ObjectMonitor::AddWaiter(ObjectWaiter* node) {
2259 assert(node != NULL, "should not add NULL node");
2260 assert(node->_prev == NULL, "node already in list");
2261 assert(node->_next == NULL, "node already in list");
2262 // put node at end of queue (circular doubly linked list)
2263 if (_WaitSet == NULL) {
2264 _WaitSet = node;
2265 node->_prev = node;
2266 node->_next = node;
2267 } else {
2268 ObjectWaiter* head = _WaitSet;
2269 ObjectWaiter* tail = head->_prev;
2270 assert(tail->_next == head, "invariant check");
2271 tail->_next = node;
2272 head->_prev = node;
2273 node->_next = head;
2274 node->_prev = tail;
2275 }
2276 }
2277
2278 inline ObjectWaiter* ObjectMonitor::DequeueWaiter() {
2279 // dequeue the very first waiter
2374 }
2375 }
2376
2377 static char * kvGet(char * kvList, const char * Key) {
2378 if (kvList == NULL) return NULL;
2379 size_t n = strlen(Key);
2380 char * Search;
2381 for (Search = kvList; *Search; Search += strlen(Search) + 1) {
2382 if (strncmp (Search, Key, n) == 0) {
2383 if (Search[n] == '=') return Search + n + 1;
2384 if (Search[n] == 0) return(char *) "1";
2385 }
2386 }
2387 return NULL;
2388 }
2389
2390 static int kvGetInt(char * kvList, const char * Key, int Default) {
2391 char * v = kvGet(kvList, Key);
2392 int rslt = v ? ::strtol(v, NULL, 0) : Default;
2393 if (Knob_ReportSettings && v != NULL) {
2394 tty->print_cr("INFO: SyncKnob: %s %d(%d)", Key, rslt, Default) ;
2395 tty->flush();
2396 }
2397 return rslt;
2398 }
2399
2400 void ObjectMonitor::DeferredInitialize() {
2401 if (InitDone > 0) return;
2402 if (Atomic::cmpxchg (-1, &InitDone, 0) != 0) {
2403 while (InitDone != 1) /* empty */;
2404 return;
2405 }
2406
2407 // One-shot global initialization ...
2408 // The initialization is idempotent, so we don't need locks.
2409 // In the future consider doing this via os::init_2().
2410 // SyncKnobs consist of <Key>=<Value> pairs in the style
2411 // of environment variables. Start by converting ':' to NUL.
2412
2413 if (SyncKnobs == NULL) SyncKnobs = "";
2414
2415 size_t sz = strlen(SyncKnobs);
2416 char * knobs = (char *) malloc(sz + 2);
2417 if (knobs == NULL) {
2418 vm_exit_out_of_memory(sz + 2, OOM_MALLOC_ERROR, "Parse SyncKnobs");
2419 guarantee(0, "invariant");
2420 }
2421 strcpy(knobs, SyncKnobs);
2422 knobs[sz+1] = 0;
2423 for (char * p = knobs; *p; p++) {
2424 if (*p == ':') *p = 0;
2425 }
2426
2427 #define SETKNOB(x) { Knob_##x = kvGetInt(knobs, #x, Knob_##x); }
2428 SETKNOB(ReportSettings);
2429 SETKNOB(ExitRelease);
2430 SETKNOB(Verbose);
2431 SETKNOB(VerifyInUse);
2432 SETKNOB(VerifyMatch);
2433 SETKNOB(FixedSpin);
2434 SETKNOB(SpinLimit);
2435 SETKNOB(SpinBase);
2436 SETKNOB(SpinBackOff);
2437 SETKNOB(CASPenalty);
2438 SETKNOB(OXPenalty);
2439 SETKNOB(LogSpins);
2440 SETKNOB(SpinSetSucc);
2441 SETKNOB(SuccEnabled);
2442 SETKNOB(SuccRestrict);
2443 SETKNOB(Penalty);
2444 SETKNOB(Bonus);
2445 SETKNOB(BonusB);
2446 SETKNOB(Poverty);
2447 SETKNOB(SpinAfterFutile);
2448 SETKNOB(UsePause);
2449 SETKNOB(SpinEarly);
2450 SETKNOB(OState);
2451 SETKNOB(MaxSpinners);
2452 SETKNOB(PreSpin);
2453 SETKNOB(ExitPolicy);
2454 SETKNOB(QMode);
2455 SETKNOB(ResetEvent);
2456 SETKNOB(MoveNotifyee);
2457 SETKNOB(FastHSSEC);
2458 #undef SETKNOB
2459
2460 if (Knob_Verbose) {
2461 sanity_checks();
2462 }
2463
2464 if (os::is_MP()) {
2465 BackOffMask = (1 << Knob_SpinBackOff) - 1;
2466 if (Knob_ReportSettings) {
2467 tty->print_cr("INFO: BackOffMask=0x%X", BackOffMask);
2468 }
2469 // CONSIDER: BackOffMask = ROUNDUP_NEXT_POWER2 (ncpus-1)
2470 } else {
2471 Knob_SpinLimit = 0;
2472 Knob_SpinBase = 0;
2473 Knob_PreSpin = 0;
2474 Knob_FixedSpin = -1;
2475 }
2476
2477 if (Knob_LogSpins == 0) {
2478 ObjectMonitor::_sync_FailedSpins = NULL;
2479 }
2480
2481 free(knobs);
2482 OrderAccess::fence();
2483 InitDone = 1;
2484 }
2485
2486 void ObjectMonitor::sanity_checks() {
2487 int error_cnt = 0;
2488 int warning_cnt = 0;
|