annotate src/share/vm/services/threadService.hpp @ 470:ad8c8ca4ab0f

6785258: Update copyright year Summary: Update copyright for files that have been modified starting July 2008 to Dec 2008 Reviewed-by: katleman, ohair, tbell
author xdono
date Mon, 15 Dec 2008 16:55:11 -0800
parents 4d05b7cb7842
children 6a93908f268f
rev   line source
duke@0 1 /*
xdono@470 2 * Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 class OopClosure;
duke@0 26 class ThreadDumpResult;
duke@0 27 class ThreadStackTrace;
duke@0 28 class ThreadSnapshot;
duke@0 29 class StackFrameInfo;
duke@0 30 class ThreadConcurrentLocks;
duke@0 31 class DeadlockCycle;
duke@0 32
duke@0 33 // VM monitoring and management support for the thread and
duke@0 34 // synchronization subsystem
duke@0 35 //
duke@0 36 // Thread contention monitoring is disabled by default.
duke@0 37 // When enabled, the VM will begin measuring the accumulated
duke@0 38 // elapsed time a thread blocked on synchronization.
duke@0 39 //
duke@0 40 class ThreadService : public AllStatic {
duke@0 41 private:
duke@0 42 // These counters could be moved to Threads class
duke@0 43 static PerfCounter* _total_threads_count;
duke@0 44 static PerfVariable* _live_threads_count;
duke@0 45 static PerfVariable* _peak_threads_count;
duke@0 46 static PerfVariable* _daemon_threads_count;
duke@0 47
duke@0 48 // These 2 counters are atomically incremented once the thread is exiting.
duke@0 49 // They will be atomically decremented when ThreadService::remove_thread is called.
duke@0 50 static volatile int _exiting_threads_count;
duke@0 51 static volatile int _exiting_daemon_threads_count;
duke@0 52
duke@0 53 static bool _thread_monitoring_contention_enabled;
duke@0 54 static bool _thread_cpu_time_enabled;
duke@0 55
duke@0 56 // Need to keep the list of thread dump result that
duke@0 57 // keep references to methodOop since thread dump can be
duke@0 58 // requested by multiple threads concurrently.
duke@0 59 static ThreadDumpResult* _threaddump_list;
duke@0 60
duke@0 61 public:
duke@0 62 static void init();
duke@0 63 static void add_thread(JavaThread* thread, bool daemon);
duke@0 64 static void remove_thread(JavaThread* thread, bool daemon);
duke@0 65 static void current_thread_exiting(JavaThread* jt);
duke@0 66
duke@0 67 static bool set_thread_monitoring_contention(bool flag);
duke@0 68 static bool is_thread_monitoring_contention() { return _thread_monitoring_contention_enabled; }
duke@0 69
duke@0 70 static bool set_thread_cpu_time_enabled(bool flag);
duke@0 71 static bool is_thread_cpu_time_enabled() { return _thread_cpu_time_enabled; }
duke@0 72
duke@0 73 static jlong get_total_thread_count() { return _total_threads_count->get_value(); }
duke@0 74 static jlong get_peak_thread_count() { return _peak_threads_count->get_value(); }
duke@0 75 static jlong get_live_thread_count() { return _live_threads_count->get_value() - _exiting_threads_count; }
duke@0 76 static jlong get_daemon_thread_count() { return _daemon_threads_count->get_value() - _exiting_daemon_threads_count; }
duke@0 77
duke@0 78 static int exiting_threads_count() { return _exiting_threads_count; }
duke@0 79 static int exiting_daemon_threads_count() { return _exiting_daemon_threads_count; }
duke@0 80
duke@0 81 // Support for thread dump
duke@0 82 static void add_thread_dump(ThreadDumpResult* dump);
duke@0 83 static void remove_thread_dump(ThreadDumpResult* dump);
duke@0 84
duke@0 85 static Handle get_current_contended_monitor(JavaThread* thread);
duke@0 86
duke@0 87 // This function is called by JVM_DumpThreads.
duke@0 88 static Handle dump_stack_traces(GrowableArray<instanceHandle>* threads,
duke@0 89 int num_threads, TRAPS);
duke@0 90
duke@0 91 static void reset_peak_thread_count();
duke@0 92 static void reset_contention_count_stat(JavaThread* thread);
duke@0 93 static void reset_contention_time_stat(JavaThread* thread);
duke@0 94
duke@0 95 static DeadlockCycle* find_deadlocks_at_safepoint(bool object_monitors_only);
duke@0 96
duke@0 97 // GC support
duke@0 98 static void oops_do(OopClosure* f);
duke@0 99 };
duke@0 100
duke@0 101 // Per-thread Statistics for synchronization
duke@0 102 class ThreadStatistics : public CHeapObj {
duke@0 103 private:
duke@0 104 // The following contention statistics are only updated by
duke@0 105 // the thread owning these statistics when contention occurs.
duke@0 106
duke@0 107 jlong _contended_enter_count;
duke@0 108 elapsedTimer _contended_enter_timer;
duke@0 109 jlong _monitor_wait_count;
duke@0 110 elapsedTimer _monitor_wait_timer;
duke@0 111 jlong _sleep_count;
duke@0 112 elapsedTimer _sleep_timer;
duke@0 113
duke@0 114
duke@0 115 // These two reset flags are set to true when another thread
duke@0 116 // requests to reset the statistics. The actual statistics
duke@0 117 // are reset when the thread contention occurs and attempts
duke@0 118 // to update the statistics.
duke@0 119 bool _count_pending_reset;
duke@0 120 bool _timer_pending_reset;
duke@0 121
duke@0 122 // Keep accurate times for potentially recursive class operations
duke@0 123 int _class_init_recursion_count;
duke@0 124 int _class_verify_recursion_count;
duke@0 125 int _class_link_recursion_count;
duke@0 126
duke@0 127 // utility functions
duke@0 128 void check_and_reset_count() {
duke@0 129 if (!_count_pending_reset) return;
duke@0 130 _contended_enter_count = 0;
duke@0 131 _monitor_wait_count = 0;
duke@0 132 _sleep_count = 0;
duke@0 133 _count_pending_reset = 0;
duke@0 134 }
duke@0 135 void check_and_reset_timer() {
duke@0 136 if (!_timer_pending_reset) return;
duke@0 137 _contended_enter_timer.reset();
duke@0 138 _monitor_wait_timer.reset();
duke@0 139 _sleep_timer.reset();
duke@0 140 _timer_pending_reset = 0;
duke@0 141 }
duke@0 142
duke@0 143 public:
duke@0 144 ThreadStatistics();
duke@0 145
duke@0 146 jlong contended_enter_count() { return (_count_pending_reset ? 0 : _contended_enter_count); }
duke@0 147 jlong contended_enter_ticks() { return (_timer_pending_reset ? 0 : _contended_enter_timer.active_ticks()); }
duke@0 148 jlong monitor_wait_count() { return (_count_pending_reset ? 0 : _monitor_wait_count); }
duke@0 149 jlong monitor_wait_ticks() { return (_timer_pending_reset ? 0 : _monitor_wait_timer.active_ticks()); }
duke@0 150 jlong sleep_count() { return (_count_pending_reset ? 0 : _sleep_count); }
duke@0 151 jlong sleep_ticks() { return (_timer_pending_reset ? 0 : _sleep_timer.active_ticks()); }
duke@0 152
duke@0 153 void monitor_wait() { check_and_reset_count(); _monitor_wait_count++; }
duke@0 154 void monitor_wait_begin() { check_and_reset_timer(); _monitor_wait_timer.start(); }
duke@0 155 void monitor_wait_end() { _monitor_wait_timer.stop(); check_and_reset_timer(); }
duke@0 156
duke@0 157 void thread_sleep() { check_and_reset_count(); _sleep_count++; }
duke@0 158 void thread_sleep_begin() { check_and_reset_timer(); _sleep_timer.start(); }
duke@0 159 void thread_sleep_end() { _sleep_timer.stop(); check_and_reset_timer(); }
duke@0 160
duke@0 161 void contended_enter() { check_and_reset_count(); _contended_enter_count++; }
duke@0 162 void contended_enter_begin() { check_and_reset_timer(); _contended_enter_timer.start(); }
duke@0 163 void contended_enter_end() { _contended_enter_timer.stop(); check_and_reset_timer(); }
duke@0 164
duke@0 165 void reset_count_stat() { _count_pending_reset = true; }
duke@0 166 void reset_time_stat() { _timer_pending_reset = true; }
duke@0 167
duke@0 168 int* class_init_recursion_count_addr() { return &_class_init_recursion_count; }
duke@0 169 int* class_verify_recursion_count_addr() { return &_class_verify_recursion_count; }
duke@0 170 int* class_link_recursion_count_addr() { return &_class_link_recursion_count; }
duke@0 171 };
duke@0 172
duke@0 173 // Thread snapshot to represent the thread state and statistics
duke@0 174 class ThreadSnapshot : public CHeapObj {
duke@0 175 private:
duke@0 176 JavaThread* _thread;
duke@0 177 oop _threadObj;
duke@0 178 java_lang_Thread::ThreadStatus _thread_status;
duke@0 179
duke@0 180 bool _is_ext_suspended;
duke@0 181 bool _is_in_native;
duke@0 182
duke@0 183 jlong _contended_enter_ticks;
duke@0 184 jlong _contended_enter_count;
duke@0 185 jlong _monitor_wait_ticks;
duke@0 186 jlong _monitor_wait_count;
duke@0 187 jlong _sleep_ticks;
duke@0 188 jlong _sleep_count;
duke@0 189 oop _blocker_object;
duke@0 190 oop _blocker_object_owner;
duke@0 191
duke@0 192 ThreadStackTrace* _stack_trace;
duke@0 193 ThreadConcurrentLocks* _concurrent_locks;
duke@0 194 ThreadSnapshot* _next;
duke@0 195
duke@0 196 public:
duke@0 197 // Dummy snapshot
duke@0 198 ThreadSnapshot() : _thread(NULL), _threadObj(NULL), _stack_trace(NULL), _concurrent_locks(NULL), _next(NULL),
duke@0 199 _blocker_object(NULL), _blocker_object_owner(NULL) {};
duke@0 200 ThreadSnapshot(JavaThread* thread);
duke@0 201 ~ThreadSnapshot();
duke@0 202
duke@0 203 java_lang_Thread::ThreadStatus thread_status() { return _thread_status; }
duke@0 204
duke@0 205 oop threadObj() const { return _threadObj; }
duke@0 206
duke@0 207 void set_next(ThreadSnapshot* n) { _next = n; }
duke@0 208
duke@0 209 bool is_ext_suspended() { return _is_ext_suspended; }
duke@0 210 bool is_in_native() { return _is_in_native; }
duke@0 211
duke@0 212 jlong contended_enter_count() { return _contended_enter_count; }
duke@0 213 jlong contended_enter_ticks() { return _contended_enter_ticks; }
duke@0 214 jlong monitor_wait_count() { return _monitor_wait_count; }
duke@0 215 jlong monitor_wait_ticks() { return _monitor_wait_ticks; }
duke@0 216 jlong sleep_count() { return _sleep_count; }
duke@0 217 jlong sleep_ticks() { return _sleep_ticks; }
duke@0 218
duke@0 219
duke@0 220 oop blocker_object() { return _blocker_object; }
duke@0 221 oop blocker_object_owner() { return _blocker_object_owner; }
duke@0 222
duke@0 223 ThreadSnapshot* next() const { return _next; }
duke@0 224 ThreadStackTrace* get_stack_trace() { return _stack_trace; }
duke@0 225 ThreadConcurrentLocks* get_concurrent_locks() { return _concurrent_locks; }
duke@0 226
duke@0 227 void dump_stack_at_safepoint(int max_depth, bool with_locked_monitors);
duke@0 228 void set_concurrent_locks(ThreadConcurrentLocks* l) { _concurrent_locks = l; }
duke@0 229 void oops_do(OopClosure* f);
duke@0 230 };
duke@0 231
duke@0 232 class ThreadStackTrace : public CHeapObj {
duke@0 233 private:
duke@0 234 JavaThread* _thread;
duke@0 235 int _depth; // number of stack frames added
duke@0 236 bool _with_locked_monitors;
duke@0 237 GrowableArray<StackFrameInfo*>* _frames;
duke@0 238 GrowableArray<oop>* _jni_locked_monitors;
duke@0 239
duke@0 240 public:
duke@0 241
duke@0 242 ThreadStackTrace(JavaThread* thread, bool with_locked_monitors);
duke@0 243 ~ThreadStackTrace();
duke@0 244
mchung@396 245 JavaThread* thread() { return _thread; }
duke@0 246 StackFrameInfo* stack_frame_at(int i) { return _frames->at(i); }
duke@0 247 int get_stack_depth() { return _depth; }
duke@0 248
duke@0 249 void add_stack_frame(javaVFrame* jvf);
duke@0 250 void dump_stack_at_safepoint(int max_depth);
duke@0 251 Handle allocate_fill_stack_trace_element_array(TRAPS);
duke@0 252 void oops_do(OopClosure* f);
duke@0 253 GrowableArray<oop>* jni_locked_monitors() { return _jni_locked_monitors; }
duke@0 254 int num_jni_locked_monitors() { return (_jni_locked_monitors != NULL ? _jni_locked_monitors->length() : 0); }
duke@0 255
duke@0 256 bool is_owned_monitor_on_stack(oop object);
duke@0 257 void add_jni_locked_monitor(oop object) { _jni_locked_monitors->append(object); }
duke@0 258 };
duke@0 259
duke@0 260 // StackFrameInfo for keeping methodOop and bci during
duke@0 261 // stack walking for later construction of StackTraceElement[]
duke@0 262 // Java instances
duke@0 263 class StackFrameInfo : public CHeapObj {
duke@0 264 private:
duke@0 265 methodOop _method;
duke@0 266 int _bci;
duke@0 267 GrowableArray<oop>* _locked_monitors; // list of object monitors locked by this frame
duke@0 268
duke@0 269 public:
duke@0 270
duke@0 271 StackFrameInfo(javaVFrame* jvf, bool with_locked_monitors);
duke@0 272 ~StackFrameInfo() {
duke@0 273 if (_locked_monitors != NULL) {
duke@0 274 delete _locked_monitors;
duke@0 275 }
duke@0 276 };
duke@0 277 methodOop method() const { return _method; }
duke@0 278 int bci() const { return _bci; }
duke@0 279 void oops_do(OopClosure* f);
duke@0 280
duke@0 281 int num_locked_monitors() { return (_locked_monitors != NULL ? _locked_monitors->length() : 0); }
duke@0 282 GrowableArray<oop>* locked_monitors() { return _locked_monitors; }
duke@0 283
duke@0 284 void print_on(outputStream* st) const;
duke@0 285 };
duke@0 286
duke@0 287 class ThreadConcurrentLocks : public CHeapObj {
duke@0 288 private:
duke@0 289 GrowableArray<instanceOop>* _owned_locks;
duke@0 290 ThreadConcurrentLocks* _next;
duke@0 291 JavaThread* _thread;
duke@0 292 public:
duke@0 293 ThreadConcurrentLocks(JavaThread* thread);
duke@0 294 ~ThreadConcurrentLocks();
duke@0 295
duke@0 296 void add_lock(instanceOop o);
duke@0 297 void set_next(ThreadConcurrentLocks* n) { _next = n; }
duke@0 298 ThreadConcurrentLocks* next() { return _next; }
duke@0 299 JavaThread* java_thread() { return _thread; }
duke@0 300 GrowableArray<instanceOop>* owned_locks() { return _owned_locks; }
duke@0 301 void oops_do(OopClosure* f);
duke@0 302 };
duke@0 303
duke@0 304 class ConcurrentLocksDump : public StackObj {
duke@0 305 private:
duke@0 306 ThreadConcurrentLocks* _map;
duke@0 307 ThreadConcurrentLocks* _last; // Last ThreadConcurrentLocks in the map
duke@0 308 bool _retain_map_on_free;
duke@0 309
duke@0 310 void build_map(GrowableArray<oop>* aos_objects);
duke@0 311 void add_lock(JavaThread* thread, instanceOop o);
duke@0 312
duke@0 313 public:
duke@0 314 ConcurrentLocksDump(bool retain_map_on_free) : _map(NULL), _last(NULL), _retain_map_on_free(retain_map_on_free) {};
duke@0 315 ConcurrentLocksDump() : _map(NULL), _last(NULL), _retain_map_on_free(false) {};
duke@0 316 ~ConcurrentLocksDump();
duke@0 317
duke@0 318 void dump_at_safepoint();
duke@0 319 ThreadConcurrentLocks* thread_concurrent_locks(JavaThread* thread);
duke@0 320 void print_locks_on(JavaThread* t, outputStream* st);
duke@0 321 };
duke@0 322
duke@0 323 class ThreadDumpResult : public StackObj {
duke@0 324 private:
duke@0 325 int _num_threads;
duke@0 326 int _num_snapshots;
duke@0 327 ThreadSnapshot* _snapshots;
duke@0 328 ThreadSnapshot* _last;
duke@0 329 ThreadDumpResult* _next;
duke@0 330 public:
duke@0 331 ThreadDumpResult();
duke@0 332 ThreadDumpResult(int num_threads);
duke@0 333 ~ThreadDumpResult();
duke@0 334
duke@0 335 void add_thread_snapshot(ThreadSnapshot* ts);
duke@0 336 void set_next(ThreadDumpResult* next) { _next = next; }
duke@0 337 ThreadDumpResult* next() { return _next; }
duke@0 338 int num_threads() { return _num_threads; }
duke@0 339 int num_snapshots() { return _num_snapshots; }
duke@0 340 ThreadSnapshot* snapshots() { return _snapshots; }
duke@0 341 void oops_do(OopClosure* f);
duke@0 342 };
duke@0 343
duke@0 344 class DeadlockCycle : public CHeapObj {
duke@0 345 private:
duke@0 346 bool _is_deadlock;
duke@0 347 GrowableArray<JavaThread*>* _threads;
duke@0 348 DeadlockCycle* _next;
duke@0 349 public:
duke@0 350 DeadlockCycle();
duke@0 351 ~DeadlockCycle();
duke@0 352
duke@0 353 DeadlockCycle* next() { return _next; }
duke@0 354 void set_next(DeadlockCycle* d) { _next = d; }
duke@0 355 void add_thread(JavaThread* t) { _threads->append(t); }
duke@0 356 void reset() { _is_deadlock = false; _threads->clear(); }
duke@0 357 void set_deadlock(bool value) { _is_deadlock = value; }
duke@0 358 bool is_deadlock() { return _is_deadlock; }
duke@0 359 int num_threads() { return _threads->length(); }
duke@0 360 GrowableArray<JavaThread*>* threads() { return _threads; }
duke@0 361 void print_on(outputStream* st) const;
duke@0 362 };
duke@0 363
duke@0 364 // Utility class to get list of java threads.
duke@0 365 class ThreadsListEnumerator : public StackObj {
duke@0 366 private:
duke@0 367 GrowableArray<instanceHandle>* _threads_array;
duke@0 368 public:
duke@0 369 ThreadsListEnumerator(Thread* cur_thread,
duke@0 370 bool include_jvmti_agent_threads = false,
duke@0 371 bool include_jni_attaching_threads = true);
duke@0 372 int num_threads() { return _threads_array->length(); }
duke@0 373 instanceHandle get_threadObj(int index) { return _threads_array->at(index); }
duke@0 374 };
duke@0 375
duke@0 376
duke@0 377 // abstract utility class to set new thread states, and restore previous after the block exits
duke@0 378 class JavaThreadStatusChanger : public StackObj {
duke@0 379 private:
duke@0 380 java_lang_Thread::ThreadStatus _old_state;
duke@0 381 JavaThread* _java_thread;
duke@0 382 bool _is_alive;
duke@0 383
duke@0 384 void save_old_state(JavaThread* java_thread) {
duke@0 385 _java_thread = java_thread;
duke@0 386 _is_alive = is_alive(java_thread);
duke@0 387 if (is_alive()) {
duke@0 388 _old_state = java_lang_Thread::get_thread_status(_java_thread->threadObj());
duke@0 389 }
duke@0 390 }
duke@0 391
duke@0 392 public:
duke@0 393 static void set_thread_status(JavaThread* java_thread,
duke@0 394 java_lang_Thread::ThreadStatus state) {
duke@0 395 java_lang_Thread::set_thread_status(java_thread->threadObj(), state);
duke@0 396 }
duke@0 397
duke@0 398 void set_thread_status(java_lang_Thread::ThreadStatus state) {
duke@0 399 if (is_alive()) {
duke@0 400 set_thread_status(_java_thread, state);
duke@0 401 }
duke@0 402 }
duke@0 403
duke@0 404 JavaThreadStatusChanger(JavaThread* java_thread,
duke@0 405 java_lang_Thread::ThreadStatus state) {
duke@0 406 save_old_state(java_thread);
duke@0 407 set_thread_status(state);
duke@0 408 }
duke@0 409
duke@0 410 JavaThreadStatusChanger(JavaThread* java_thread) {
duke@0 411 save_old_state(java_thread);
duke@0 412 }
duke@0 413
duke@0 414 ~JavaThreadStatusChanger() {
duke@0 415 set_thread_status(_old_state);
duke@0 416 }
duke@0 417
duke@0 418 static bool is_alive(JavaThread* java_thread) {
duke@0 419 return java_thread != NULL && java_thread->threadObj() != NULL;
duke@0 420 }
duke@0 421
duke@0 422 bool is_alive() {
duke@0 423 return _is_alive;
duke@0 424 }
duke@0 425 };
duke@0 426
duke@0 427 // Change status to waiting on an object (timed or indefinite)
duke@0 428 class JavaThreadInObjectWaitState : public JavaThreadStatusChanger {
duke@0 429 private:
duke@0 430 ThreadStatistics* _stat;
duke@0 431 bool _active;
duke@0 432
duke@0 433 public:
duke@0 434 JavaThreadInObjectWaitState(JavaThread *java_thread, bool timed) :
duke@0 435 JavaThreadStatusChanger(java_thread,
duke@0 436 timed ? java_lang_Thread::IN_OBJECT_WAIT_TIMED : java_lang_Thread::IN_OBJECT_WAIT) {
duke@0 437 if (is_alive()) {
duke@0 438 _stat = java_thread->get_thread_stat();
duke@0 439 _active = ThreadService::is_thread_monitoring_contention();
duke@0 440 _stat->monitor_wait();
duke@0 441 if (_active) {
duke@0 442 _stat->monitor_wait_begin();
duke@0 443 }
duke@0 444 } else {
duke@0 445 _active = false;
duke@0 446 }
duke@0 447 }
duke@0 448
duke@0 449 ~JavaThreadInObjectWaitState() {
duke@0 450 if (_active) {
duke@0 451 _stat->monitor_wait_end();
duke@0 452 }
duke@0 453 }
duke@0 454 };
duke@0 455
duke@0 456 // Change status to parked (timed or indefinite)
duke@0 457 class JavaThreadParkedState : public JavaThreadStatusChanger {
duke@0 458 private:
duke@0 459 ThreadStatistics* _stat;
duke@0 460 bool _active;
duke@0 461
duke@0 462 public:
duke@0 463 JavaThreadParkedState(JavaThread *java_thread, bool timed) :
duke@0 464 JavaThreadStatusChanger(java_thread,
duke@0 465 timed ? java_lang_Thread::PARKED_TIMED : java_lang_Thread::PARKED) {
duke@0 466 if (is_alive()) {
duke@0 467 _stat = java_thread->get_thread_stat();
duke@0 468 _active = ThreadService::is_thread_monitoring_contention();
duke@0 469 _stat->monitor_wait();
duke@0 470 if (_active) {
duke@0 471 _stat->monitor_wait_begin();
duke@0 472 }
duke@0 473 } else {
duke@0 474 _active = false;
duke@0 475 }
duke@0 476 }
duke@0 477
duke@0 478 ~JavaThreadParkedState() {
duke@0 479 if (_active) {
duke@0 480 _stat->monitor_wait_end();
duke@0 481 }
duke@0 482 }
duke@0 483 };
duke@0 484
duke@0 485 // Change status to blocked on (re-)entering a synchronization block
duke@0 486 class JavaThreadBlockedOnMonitorEnterState : public JavaThreadStatusChanger {
duke@0 487 private:
duke@0 488 ThreadStatistics* _stat;
duke@0 489 bool _active;
duke@0 490
duke@0 491 static bool contended_enter_begin(JavaThread *java_thread) {
duke@0 492 set_thread_status(java_thread, java_lang_Thread::BLOCKED_ON_MONITOR_ENTER);
duke@0 493 ThreadStatistics* stat = java_thread->get_thread_stat();
duke@0 494 stat->contended_enter();
duke@0 495 bool active = ThreadService::is_thread_monitoring_contention();
duke@0 496 if (active) {
duke@0 497 stat->contended_enter_begin();
duke@0 498 }
duke@0 499 return active;
duke@0 500 }
duke@0 501
duke@0 502 public:
duke@0 503 // java_thread is waiting thread being blocked on monitor reenter.
duke@0 504 // Current thread is the notifying thread which holds the monitor.
duke@0 505 static bool wait_reenter_begin(JavaThread *java_thread, ObjectMonitor *obj_m) {
duke@0 506 assert((java_thread != NULL), "Java thread should not be null here");
duke@0 507 bool active = false;
duke@0 508 if (is_alive(java_thread) && ServiceUtil::visible_oop((oop)obj_m->object())) {
duke@0 509 active = contended_enter_begin(java_thread);
duke@0 510 }
duke@0 511 return active;
duke@0 512 }
duke@0 513
duke@0 514 static void wait_reenter_end(JavaThread *java_thread, bool active) {
duke@0 515 if (active) {
duke@0 516 java_thread->get_thread_stat()->contended_enter_end();
duke@0 517 }
duke@0 518 set_thread_status(java_thread, java_lang_Thread::RUNNABLE);
duke@0 519 }
duke@0 520
duke@0 521 JavaThreadBlockedOnMonitorEnterState(JavaThread *java_thread, ObjectMonitor *obj_m) :
duke@0 522 JavaThreadStatusChanger(java_thread) {
duke@0 523 assert((java_thread != NULL), "Java thread should not be null here");
duke@0 524 // Change thread status and collect contended enter stats for monitor contended
duke@0 525 // enter done for external java world objects and it is contended. All other cases
duke@0 526 // like for vm internal objects and for external objects which are not contended
duke@0 527 // thread status is not changed and contended enter stat is not collected.
duke@0 528 _active = false;
duke@0 529 if (is_alive() && ServiceUtil::visible_oop((oop)obj_m->object()) && obj_m->contentions() > 0) {
duke@0 530 _stat = java_thread->get_thread_stat();
duke@0 531 _active = contended_enter_begin(java_thread);
duke@0 532 }
duke@0 533 }
duke@0 534
duke@0 535 ~JavaThreadBlockedOnMonitorEnterState() {
duke@0 536 if (_active) {
duke@0 537 _stat->contended_enter_end();
duke@0 538 }
duke@0 539 }
duke@0 540 };
duke@0 541
duke@0 542 // Change status to sleeping
duke@0 543 class JavaThreadSleepState : public JavaThreadStatusChanger {
duke@0 544 private:
duke@0 545 ThreadStatistics* _stat;
duke@0 546 bool _active;
duke@0 547 public:
duke@0 548 JavaThreadSleepState(JavaThread *java_thread) :
duke@0 549 JavaThreadStatusChanger(java_thread, java_lang_Thread::SLEEPING) {
duke@0 550 if (is_alive()) {
duke@0 551 _stat = java_thread->get_thread_stat();
duke@0 552 _active = ThreadService::is_thread_monitoring_contention();
duke@0 553 _stat->thread_sleep();
duke@0 554 if (_active) {
duke@0 555 _stat->thread_sleep_begin();
duke@0 556 }
duke@0 557 } else {
duke@0 558 _active = false;
duke@0 559 }
duke@0 560 }
duke@0 561
duke@0 562 ~JavaThreadSleepState() {
duke@0 563 if (_active) {
duke@0 564 _stat->thread_sleep_end();
duke@0 565 }
duke@0 566 }
duke@0 567 };