src/share/vm/prims/jvmtiRawMonitor.cpp

Wed, 11 Jan 2012 17:34:02 -0500

author
phh
date
Wed, 11 Jan 2012 17:34:02 -0500
changeset 3427
94ec88ca68e2
parent 2314
f95d63e2154a
child 3900
d2a62e0f25eb
permissions
-rw-r--r--

7115199: Add event tracing hooks and Java Flight Recorder infrastructure
Summary: Added a nop tracing infrastructure, JFR makefile changes and other infrastructure used only by JFR.
Reviewed-by: acorn, sspitsyn
Contributed-by: markus.gronlund@oracle.com

acorn@2233 1 /*
stefank@2314 2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
acorn@2233 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
acorn@2233 4 *
acorn@2233 5 * This code is free software; you can redistribute it and/or modify it
acorn@2233 6 * under the terms of the GNU General Public License version 2 only, as
acorn@2233 7 * published by the Free Software Foundation.
acorn@2233 8 *
acorn@2233 9 * This code is distributed in the hope that it will be useful, but WITHOUT
acorn@2233 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
acorn@2233 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
acorn@2233 12 * version 2 for more details (a copy is included in the LICENSE file that
acorn@2233 13 * accompanied this code).
acorn@2233 14 *
acorn@2233 15 * You should have received a copy of the GNU General Public License version
acorn@2233 16 * 2 along with this work; if not, write to the Free Software Foundation,
acorn@2233 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
acorn@2233 18 *
acorn@2233 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
acorn@2233 20 * or visit www.oracle.com if you need additional information or have any
acorn@2233 21 * questions.
acorn@2233 22 *
acorn@2233 23 */
acorn@2233 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "prims/jvmtiRawMonitor.hpp"
stefank@2314 27 #include "runtime/interfaceSupport.hpp"
stefank@2314 28 #include "runtime/thread.hpp"
acorn@2233 29
acorn@2233 30 GrowableArray<JvmtiRawMonitor*> *JvmtiPendingMonitors::_monitors = new (ResourceObj::C_HEAP) GrowableArray<JvmtiRawMonitor*>(1,true);
acorn@2233 31
acorn@2233 32 void JvmtiPendingMonitors::transition_raw_monitors() {
acorn@2233 33 assert((Threads::number_of_threads()==1),
acorn@2233 34 "Java thread has not created yet or more than one java thread \
acorn@2233 35 is running. Raw monitor transition will not work");
acorn@2233 36 JavaThread *current_java_thread = JavaThread::current();
acorn@2233 37 assert(current_java_thread->thread_state() == _thread_in_vm, "Must be in vm");
acorn@2233 38 {
acorn@2233 39 ThreadBlockInVM __tbivm(current_java_thread);
acorn@2233 40 for(int i=0; i< count(); i++) {
acorn@2233 41 JvmtiRawMonitor *rmonitor = monitors()->at(i);
acorn@2233 42 int r = rmonitor->raw_enter(current_java_thread);
acorn@2233 43 assert(r == ObjectMonitor::OM_OK, "raw_enter should have worked");
acorn@2233 44 }
acorn@2233 45 }
acorn@2233 46 // pending monitors are converted to real monitor so delete them all.
acorn@2233 47 dispose();
acorn@2233 48 }
acorn@2233 49
acorn@2233 50 //
acorn@2233 51 // class JvmtiRawMonitor
acorn@2233 52 //
acorn@2233 53
acorn@2233 54 JvmtiRawMonitor::JvmtiRawMonitor(const char *name) {
acorn@2233 55 #ifdef ASSERT
acorn@2233 56 _name = strcpy(NEW_C_HEAP_ARRAY(char, strlen(name) + 1), name);
acorn@2233 57 #else
acorn@2233 58 _name = NULL;
acorn@2233 59 #endif
acorn@2233 60 _magic = JVMTI_RM_MAGIC;
acorn@2233 61 }
acorn@2233 62
acorn@2233 63 JvmtiRawMonitor::~JvmtiRawMonitor() {
acorn@2233 64 #ifdef ASSERT
acorn@2233 65 FreeHeap(_name);
acorn@2233 66 #endif
acorn@2233 67 _magic = 0;
acorn@2233 68 }
acorn@2233 69
acorn@2233 70
acorn@2233 71 bool
acorn@2233 72 JvmtiRawMonitor::is_valid() {
acorn@2233 73 int value = 0;
acorn@2233 74
acorn@2233 75 // This object might not be a JvmtiRawMonitor so we can't assume
acorn@2233 76 // the _magic field is properly aligned. Get the value in a safe
acorn@2233 77 // way and then check against JVMTI_RM_MAGIC.
acorn@2233 78
acorn@2233 79 switch (sizeof(_magic)) {
acorn@2233 80 case 2:
acorn@2233 81 value = Bytes::get_native_u2((address)&_magic);
acorn@2233 82 break;
acorn@2233 83
acorn@2233 84 case 4:
acorn@2233 85 value = Bytes::get_native_u4((address)&_magic);
acorn@2233 86 break;
acorn@2233 87
acorn@2233 88 case 8:
acorn@2233 89 value = Bytes::get_native_u8((address)&_magic);
acorn@2233 90 break;
acorn@2233 91
acorn@2233 92 default:
acorn@2233 93 guarantee(false, "_magic field is an unexpected size");
acorn@2233 94 }
acorn@2233 95
acorn@2233 96 return value == JVMTI_RM_MAGIC;
acorn@2233 97 }
acorn@2233 98
acorn@2233 99 // -------------------------------------------------------------------------
acorn@2233 100 // The raw monitor subsystem is entirely distinct from normal
acorn@2233 101 // java-synchronization or jni-synchronization. raw monitors are not
acorn@2233 102 // associated with objects. They can be implemented in any manner
acorn@2233 103 // that makes sense. The original implementors decided to piggy-back
acorn@2233 104 // the raw-monitor implementation on the existing Java objectMonitor mechanism.
acorn@2233 105 // This flaw needs to fixed. We should reimplement raw monitors as sui-generis.
acorn@2233 106 // Specifically, we should not implement raw monitors via java monitors.
acorn@2233 107 // Time permitting, we should disentangle and deconvolve the two implementations
acorn@2233 108 // and move the resulting raw monitor implementation over to the JVMTI directories.
acorn@2233 109 // Ideally, the raw monitor implementation would be built on top of
acorn@2233 110 // park-unpark and nothing else.
acorn@2233 111 //
acorn@2233 112 // raw monitors are used mainly by JVMTI
acorn@2233 113 // The raw monitor implementation borrows the ObjectMonitor structure,
acorn@2233 114 // but the operators are degenerate and extremely simple.
acorn@2233 115 //
acorn@2233 116 // Mixed use of a single objectMonitor instance -- as both a raw monitor
acorn@2233 117 // and a normal java monitor -- is not permissible.
acorn@2233 118 //
acorn@2233 119 // Note that we use the single RawMonitor_lock to protect queue operations for
acorn@2233 120 // _all_ raw monitors. This is a scalability impediment, but since raw monitor usage
acorn@2233 121 // is deprecated and rare, this is not of concern. The RawMonitor_lock can not
acorn@2233 122 // be held indefinitely. The critical sections must be short and bounded.
acorn@2233 123 //
acorn@2233 124 // -------------------------------------------------------------------------
acorn@2233 125
acorn@2233 126 int JvmtiRawMonitor::SimpleEnter (Thread * Self) {
acorn@2233 127 for (;;) {
acorn@2233 128 if (Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {
acorn@2233 129 return OS_OK ;
acorn@2233 130 }
acorn@2233 131
acorn@2233 132 ObjectWaiter Node (Self) ;
acorn@2233 133 Self->_ParkEvent->reset() ; // strictly optional
acorn@2233 134 Node.TState = ObjectWaiter::TS_ENTER ;
acorn@2233 135
acorn@2233 136 RawMonitor_lock->lock_without_safepoint_check() ;
acorn@2233 137 Node._next = _EntryList ;
acorn@2233 138 _EntryList = &Node ;
acorn@2233 139 OrderAccess::fence() ;
acorn@2233 140 if (_owner == NULL && Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {
acorn@2233 141 _EntryList = Node._next ;
acorn@2233 142 RawMonitor_lock->unlock() ;
acorn@2233 143 return OS_OK ;
acorn@2233 144 }
acorn@2233 145 RawMonitor_lock->unlock() ;
acorn@2233 146 while (Node.TState == ObjectWaiter::TS_ENTER) {
acorn@2233 147 Self->_ParkEvent->park() ;
acorn@2233 148 }
acorn@2233 149 }
acorn@2233 150 }
acorn@2233 151
acorn@2233 152 int JvmtiRawMonitor::SimpleExit (Thread * Self) {
acorn@2233 153 guarantee (_owner == Self, "invariant") ;
acorn@2233 154 OrderAccess::release_store_ptr (&_owner, NULL) ;
acorn@2233 155 OrderAccess::fence() ;
acorn@2233 156 if (_EntryList == NULL) return OS_OK ;
acorn@2233 157 ObjectWaiter * w ;
acorn@2233 158
acorn@2233 159 RawMonitor_lock->lock_without_safepoint_check() ;
acorn@2233 160 w = _EntryList ;
acorn@2233 161 if (w != NULL) {
acorn@2233 162 _EntryList = w->_next ;
acorn@2233 163 }
acorn@2233 164 RawMonitor_lock->unlock() ;
acorn@2233 165 if (w != NULL) {
acorn@2233 166 guarantee (w ->TState == ObjectWaiter::TS_ENTER, "invariant") ;
acorn@2233 167 ParkEvent * ev = w->_event ;
acorn@2233 168 w->TState = ObjectWaiter::TS_RUN ;
acorn@2233 169 OrderAccess::fence() ;
acorn@2233 170 ev->unpark() ;
acorn@2233 171 }
acorn@2233 172 return OS_OK ;
acorn@2233 173 }
acorn@2233 174
acorn@2233 175 int JvmtiRawMonitor::SimpleWait (Thread * Self, jlong millis) {
acorn@2233 176 guarantee (_owner == Self , "invariant") ;
acorn@2233 177 guarantee (_recursions == 0, "invariant") ;
acorn@2233 178
acorn@2233 179 ObjectWaiter Node (Self) ;
acorn@2233 180 Node._notified = 0 ;
acorn@2233 181 Node.TState = ObjectWaiter::TS_WAIT ;
acorn@2233 182
acorn@2233 183 RawMonitor_lock->lock_without_safepoint_check() ;
acorn@2233 184 Node._next = _WaitSet ;
acorn@2233 185 _WaitSet = &Node ;
acorn@2233 186 RawMonitor_lock->unlock() ;
acorn@2233 187
acorn@2233 188 SimpleExit (Self) ;
acorn@2233 189 guarantee (_owner != Self, "invariant") ;
acorn@2233 190
acorn@2233 191 int ret = OS_OK ;
acorn@2233 192 if (millis <= 0) {
acorn@2233 193 Self->_ParkEvent->park();
acorn@2233 194 } else {
acorn@2233 195 ret = Self->_ParkEvent->park(millis);
acorn@2233 196 }
acorn@2233 197
acorn@2233 198 // If thread still resides on the waitset then unlink it.
acorn@2233 199 // Double-checked locking -- the usage is safe in this context
acorn@2233 200 // as we TState is volatile and the lock-unlock operators are
acorn@2233 201 // serializing (barrier-equivalent).
acorn@2233 202
acorn@2233 203 if (Node.TState == ObjectWaiter::TS_WAIT) {
acorn@2233 204 RawMonitor_lock->lock_without_safepoint_check() ;
acorn@2233 205 if (Node.TState == ObjectWaiter::TS_WAIT) {
acorn@2233 206 // Simple O(n) unlink, but performance isn't critical here.
acorn@2233 207 ObjectWaiter * p ;
acorn@2233 208 ObjectWaiter * q = NULL ;
acorn@2233 209 for (p = _WaitSet ; p != &Node; p = p->_next) {
acorn@2233 210 q = p ;
acorn@2233 211 }
acorn@2233 212 guarantee (p == &Node, "invariant") ;
acorn@2233 213 if (q == NULL) {
acorn@2233 214 guarantee (p == _WaitSet, "invariant") ;
acorn@2233 215 _WaitSet = p->_next ;
acorn@2233 216 } else {
acorn@2233 217 guarantee (p == q->_next, "invariant") ;
acorn@2233 218 q->_next = p->_next ;
acorn@2233 219 }
acorn@2233 220 Node.TState = ObjectWaiter::TS_RUN ;
acorn@2233 221 }
acorn@2233 222 RawMonitor_lock->unlock() ;
acorn@2233 223 }
acorn@2233 224
acorn@2233 225 guarantee (Node.TState == ObjectWaiter::TS_RUN, "invariant") ;
acorn@2233 226 SimpleEnter (Self) ;
acorn@2233 227
acorn@2233 228 guarantee (_owner == Self, "invariant") ;
acorn@2233 229 guarantee (_recursions == 0, "invariant") ;
acorn@2233 230 return ret ;
acorn@2233 231 }
acorn@2233 232
acorn@2233 233 int JvmtiRawMonitor::SimpleNotify (Thread * Self, bool All) {
acorn@2233 234 guarantee (_owner == Self, "invariant") ;
acorn@2233 235 if (_WaitSet == NULL) return OS_OK ;
acorn@2233 236
acorn@2233 237 // We have two options:
acorn@2233 238 // A. Transfer the threads from the WaitSet to the EntryList
acorn@2233 239 // B. Remove the thread from the WaitSet and unpark() it.
acorn@2233 240 //
acorn@2233 241 // We use (B), which is crude and results in lots of futile
acorn@2233 242 // context switching. In particular (B) induces lots of contention.
acorn@2233 243
acorn@2233 244 ParkEvent * ev = NULL ; // consider using a small auto array ...
acorn@2233 245 RawMonitor_lock->lock_without_safepoint_check() ;
acorn@2233 246 for (;;) {
acorn@2233 247 ObjectWaiter * w = _WaitSet ;
acorn@2233 248 if (w == NULL) break ;
acorn@2233 249 _WaitSet = w->_next ;
acorn@2233 250 if (ev != NULL) { ev->unpark(); ev = NULL; }
acorn@2233 251 ev = w->_event ;
acorn@2233 252 OrderAccess::loadstore() ;
acorn@2233 253 w->TState = ObjectWaiter::TS_RUN ;
acorn@2233 254 OrderAccess::storeload();
acorn@2233 255 if (!All) break ;
acorn@2233 256 }
acorn@2233 257 RawMonitor_lock->unlock() ;
acorn@2233 258 if (ev != NULL) ev->unpark();
acorn@2233 259 return OS_OK ;
acorn@2233 260 }
acorn@2233 261
acorn@2233 262 // Any JavaThread will enter here with state _thread_blocked
acorn@2233 263 int JvmtiRawMonitor::raw_enter(TRAPS) {
acorn@2233 264 TEVENT (raw_enter) ;
acorn@2233 265 void * Contended ;
acorn@2233 266
acorn@2233 267 // don't enter raw monitor if thread is being externally suspended, it will
acorn@2233 268 // surprise the suspender if a "suspended" thread can still enter monitor
acorn@2233 269 JavaThread * jt = (JavaThread *)THREAD;
acorn@2233 270 if (THREAD->is_Java_thread()) {
acorn@2233 271 jt->SR_lock()->lock_without_safepoint_check();
acorn@2233 272 while (jt->is_external_suspend()) {
acorn@2233 273 jt->SR_lock()->unlock();
acorn@2233 274 jt->java_suspend_self();
acorn@2233 275 jt->SR_lock()->lock_without_safepoint_check();
acorn@2233 276 }
acorn@2233 277 // guarded by SR_lock to avoid racing with new external suspend requests.
acorn@2233 278 Contended = Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) ;
acorn@2233 279 jt->SR_lock()->unlock();
acorn@2233 280 } else {
acorn@2233 281 Contended = Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) ;
acorn@2233 282 }
acorn@2233 283
acorn@2233 284 if (Contended == THREAD) {
acorn@2233 285 _recursions ++ ;
acorn@2233 286 return OM_OK ;
acorn@2233 287 }
acorn@2233 288
acorn@2233 289 if (Contended == NULL) {
acorn@2233 290 guarantee (_owner == THREAD, "invariant") ;
acorn@2233 291 guarantee (_recursions == 0, "invariant") ;
acorn@2233 292 return OM_OK ;
acorn@2233 293 }
acorn@2233 294
acorn@2233 295 THREAD->set_current_pending_monitor(this);
acorn@2233 296
acorn@2233 297 if (!THREAD->is_Java_thread()) {
acorn@2233 298 // No other non-Java threads besides VM thread would acquire
acorn@2233 299 // a raw monitor.
acorn@2233 300 assert(THREAD->is_VM_thread(), "must be VM thread");
acorn@2233 301 SimpleEnter (THREAD) ;
acorn@2233 302 } else {
acorn@2233 303 guarantee (jt->thread_state() == _thread_blocked, "invariant") ;
acorn@2233 304 for (;;) {
acorn@2233 305 jt->set_suspend_equivalent();
acorn@2233 306 // cleared by handle_special_suspend_equivalent_condition() or
acorn@2233 307 // java_suspend_self()
acorn@2233 308 SimpleEnter (THREAD) ;
acorn@2233 309
acorn@2233 310 // were we externally suspended while we were waiting?
acorn@2233 311 if (!jt->handle_special_suspend_equivalent_condition()) break ;
acorn@2233 312
acorn@2233 313 // This thread was externally suspended
acorn@2233 314 //
acorn@2233 315 // This logic isn't needed for JVMTI raw monitors,
acorn@2233 316 // but doesn't hurt just in case the suspend rules change. This
acorn@2233 317 // logic is needed for the JvmtiRawMonitor.wait() reentry phase.
acorn@2233 318 // We have reentered the contended monitor, but while we were
acorn@2233 319 // waiting another thread suspended us. We don't want to reenter
acorn@2233 320 // the monitor while suspended because that would surprise the
acorn@2233 321 // thread that suspended us.
acorn@2233 322 //
acorn@2233 323 // Drop the lock -
acorn@2233 324 SimpleExit (THREAD) ;
acorn@2233 325
acorn@2233 326 jt->java_suspend_self();
acorn@2233 327 }
acorn@2233 328
acorn@2233 329 assert(_owner == THREAD, "Fatal error with monitor owner!");
acorn@2233 330 assert(_recursions == 0, "Fatal error with monitor recursions!");
acorn@2233 331 }
acorn@2233 332
acorn@2233 333 THREAD->set_current_pending_monitor(NULL);
acorn@2233 334 guarantee (_recursions == 0, "invariant") ;
acorn@2233 335 return OM_OK;
acorn@2233 336 }
acorn@2233 337
acorn@2233 338 // Used mainly for JVMTI raw monitor implementation
acorn@2233 339 // Also used for JvmtiRawMonitor::wait().
acorn@2233 340 int JvmtiRawMonitor::raw_exit(TRAPS) {
acorn@2233 341 TEVENT (raw_exit) ;
acorn@2233 342 if (THREAD != _owner) {
acorn@2233 343 return OM_ILLEGAL_MONITOR_STATE;
acorn@2233 344 }
acorn@2233 345 if (_recursions > 0) {
acorn@2233 346 --_recursions ;
acorn@2233 347 return OM_OK ;
acorn@2233 348 }
acorn@2233 349
acorn@2233 350 void * List = _EntryList ;
acorn@2233 351 SimpleExit (THREAD) ;
acorn@2233 352
acorn@2233 353 return OM_OK;
acorn@2233 354 }
acorn@2233 355
acorn@2233 356 // Used for JVMTI raw monitor implementation.
acorn@2233 357 // All JavaThreads will enter here with state _thread_blocked
acorn@2233 358
acorn@2233 359 int JvmtiRawMonitor::raw_wait(jlong millis, bool interruptible, TRAPS) {
acorn@2233 360 TEVENT (raw_wait) ;
acorn@2233 361 if (THREAD != _owner) {
acorn@2233 362 return OM_ILLEGAL_MONITOR_STATE;
acorn@2233 363 }
acorn@2233 364
acorn@2233 365 // To avoid spurious wakeups we reset the parkevent -- This is strictly optional.
acorn@2233 366 // The caller must be able to tolerate spurious returns from raw_wait().
acorn@2233 367 THREAD->_ParkEvent->reset() ;
acorn@2233 368 OrderAccess::fence() ;
acorn@2233 369
acorn@2233 370 // check interrupt event
acorn@2233 371 if (interruptible && Thread::is_interrupted(THREAD, true)) {
acorn@2233 372 return OM_INTERRUPTED;
acorn@2233 373 }
acorn@2233 374
acorn@2233 375 intptr_t save = _recursions ;
acorn@2233 376 _recursions = 0 ;
acorn@2233 377 _waiters ++ ;
acorn@2233 378 if (THREAD->is_Java_thread()) {
acorn@2233 379 guarantee (((JavaThread *) THREAD)->thread_state() == _thread_blocked, "invariant") ;
acorn@2233 380 ((JavaThread *)THREAD)->set_suspend_equivalent();
acorn@2233 381 }
acorn@2233 382 int rv = SimpleWait (THREAD, millis) ;
acorn@2233 383 _recursions = save ;
acorn@2233 384 _waiters -- ;
acorn@2233 385
acorn@2233 386 guarantee (THREAD == _owner, "invariant") ;
acorn@2233 387 if (THREAD->is_Java_thread()) {
acorn@2233 388 JavaThread * jSelf = (JavaThread *) THREAD ;
acorn@2233 389 for (;;) {
acorn@2233 390 if (!jSelf->handle_special_suspend_equivalent_condition()) break ;
acorn@2233 391 SimpleExit (THREAD) ;
acorn@2233 392 jSelf->java_suspend_self();
acorn@2233 393 SimpleEnter (THREAD) ;
acorn@2233 394 jSelf->set_suspend_equivalent() ;
acorn@2233 395 }
acorn@2233 396 }
acorn@2233 397 guarantee (THREAD == _owner, "invariant") ;
acorn@2233 398
acorn@2233 399 if (interruptible && Thread::is_interrupted(THREAD, true)) {
acorn@2233 400 return OM_INTERRUPTED;
acorn@2233 401 }
acorn@2233 402 return OM_OK ;
acorn@2233 403 }
acorn@2233 404
acorn@2233 405 int JvmtiRawMonitor::raw_notify(TRAPS) {
acorn@2233 406 TEVENT (raw_notify) ;
acorn@2233 407 if (THREAD != _owner) {
acorn@2233 408 return OM_ILLEGAL_MONITOR_STATE;
acorn@2233 409 }
acorn@2233 410 SimpleNotify (THREAD, false) ;
acorn@2233 411 return OM_OK;
acorn@2233 412 }
acorn@2233 413
acorn@2233 414 int JvmtiRawMonitor::raw_notifyAll(TRAPS) {
acorn@2233 415 TEVENT (raw_notifyAll) ;
acorn@2233 416 if (THREAD != _owner) {
acorn@2233 417 return OM_ILLEGAL_MONITOR_STATE;
acorn@2233 418 }
acorn@2233 419 SimpleNotify (THREAD, true) ;
acorn@2233 420 return OM_OK;
acorn@2233 421 }
acorn@2233 422

mercurial