1 /* 2 * Copyright (c) 2003, 2017, 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 */ 24 25 #ifndef SHARE_VM_SERVICES_THREADSERVICE_HPP 26 #define SHARE_VM_SERVICES_THREADSERVICE_HPP 27 28 #include "classfile/javaClasses.hpp" 29 #include "runtime/handles.hpp" 30 #include "runtime/init.hpp" 31 #include "runtime/jniHandles.hpp" 32 #include "runtime/objectMonitor.hpp" 33 #include "runtime/objectMonitor.inline.hpp" 34 #include "runtime/perfData.hpp" 35 #include "runtime/thread.hpp" 36 #include "services/management.hpp" 37 #include "services/serviceUtil.hpp" 38 39 class OopClosure; 40 class ThreadDumpResult; 41 class ThreadStackTrace; 42 class ThreadSnapshot; 43 class StackFrameInfo; 44 class ThreadConcurrentLocks; 45 class DeadlockCycle; 46 47 // VM monitoring and management support for the thread and 48 // synchronization subsystem 49 // 50 // Thread contention monitoring is disabled by default. 51 // When enabled, the VM will begin measuring the accumulated 52 // elapsed time a thread blocked on synchronization. 53 // 54 class ThreadService : public AllStatic { 55 private: 56 // These counters could be moved to Threads class 57 static PerfCounter* _total_threads_count; 58 static PerfVariable* _live_threads_count; 59 static PerfVariable* _peak_threads_count; 60 static PerfVariable* _daemon_threads_count; 61 62 // These 2 counters are atomically incremented once the thread is exiting. 63 // They will be atomically decremented when ThreadService::remove_thread is called. 64 static volatile int _exiting_threads_count; 65 static volatile int _exiting_daemon_threads_count; 66 67 static bool _thread_monitoring_contention_enabled; 68 static bool _thread_cpu_time_enabled; 69 static bool _thread_allocated_memory_enabled; 70 71 // Need to keep the list of thread dump result that 72 // keep references to Method* since thread dump can be 73 // requested by multiple threads concurrently. 74 static ThreadDumpResult* _threaddump_list; 75 76 public: 77 static void init(); 78 static void add_thread(JavaThread* thread, bool daemon); 79 static void remove_thread(JavaThread* thread, bool daemon); 80 static void current_thread_exiting(JavaThread* jt); 81 82 static bool set_thread_monitoring_contention(bool flag); 83 static bool is_thread_monitoring_contention() { return _thread_monitoring_contention_enabled; } 84 85 static bool set_thread_cpu_time_enabled(bool flag); 86 static bool is_thread_cpu_time_enabled() { return _thread_cpu_time_enabled; } 87 88 static bool set_thread_allocated_memory_enabled(bool flag); 89 static bool is_thread_allocated_memory_enabled() { return _thread_cpu_time_enabled; } 90 91 static jlong get_total_thread_count() { return _total_threads_count->get_value(); } 92 static jlong get_peak_thread_count() { return _peak_threads_count->get_value(); } 93 static jlong get_live_thread_count() { return _live_threads_count->get_value() - _exiting_threads_count; } 94 static jlong get_daemon_thread_count() { return _daemon_threads_count->get_value() - _exiting_daemon_threads_count; } 95 96 static int exiting_threads_count() { return _exiting_threads_count; } 97 static int exiting_daemon_threads_count() { return _exiting_daemon_threads_count; } 98 99 // Support for thread dump 100 static void add_thread_dump(ThreadDumpResult* dump); 101 static void remove_thread_dump(ThreadDumpResult* dump); 102 103 static Handle get_current_contended_monitor(JavaThread* thread); 104 105 // This function is called by JVM_DumpThreads. 106 static Handle dump_stack_traces(GrowableArray<instanceHandle>* threads, 107 int num_threads, TRAPS); 108 109 static void reset_peak_thread_count(); 110 static void reset_contention_count_stat(JavaThread* thread); 111 static void reset_contention_time_stat(JavaThread* thread); 112 113 static DeadlockCycle* find_deadlocks_at_safepoint(ThreadsList * t_list, bool object_monitors_only); 114 115 // GC support 116 static void oops_do(OopClosure* f); 117 static void metadata_do(void f(Metadata*)); 118 }; 119 120 // Per-thread Statistics for synchronization 121 class ThreadStatistics : public CHeapObj<mtInternal> { 122 private: 123 // The following contention statistics are only updated by 124 // the thread owning these statistics when contention occurs. 125 126 jlong _contended_enter_count; 127 elapsedTimer _contended_enter_timer; 128 jlong _monitor_wait_count; 129 elapsedTimer _monitor_wait_timer; 130 jlong _sleep_count; 131 elapsedTimer _sleep_timer; 132 133 134 // These two reset flags are set to true when another thread 135 // requests to reset the statistics. The actual statistics 136 // are reset when the thread contention occurs and attempts 137 // to update the statistics. 138 bool _count_pending_reset; 139 bool _timer_pending_reset; 140 141 // Keep accurate times for potentially recursive class operations 142 int _perf_recursion_counts[6]; 143 elapsedTimer _perf_timers[6]; 144 145 // utility functions 146 void check_and_reset_count() { 147 if (!_count_pending_reset) return; 148 _contended_enter_count = 0; 149 _monitor_wait_count = 0; 150 _sleep_count = 0; 151 _count_pending_reset = 0; 152 } 153 void check_and_reset_timer() { 154 if (!_timer_pending_reset) return; 155 _contended_enter_timer.reset(); 156 _monitor_wait_timer.reset(); 157 _sleep_timer.reset(); 158 _timer_pending_reset = 0; 159 } 160 161 public: 162 ThreadStatistics(); 163 164 jlong contended_enter_count() { return (_count_pending_reset ? 0 : _contended_enter_count); } 165 jlong contended_enter_ticks() { return (_timer_pending_reset ? 0 : _contended_enter_timer.active_ticks()); } 166 jlong monitor_wait_count() { return (_count_pending_reset ? 0 : _monitor_wait_count); } 167 jlong monitor_wait_ticks() { return (_timer_pending_reset ? 0 : _monitor_wait_timer.active_ticks()); } 168 jlong sleep_count() { return (_count_pending_reset ? 0 : _sleep_count); } 169 jlong sleep_ticks() { return (_timer_pending_reset ? 0 : _sleep_timer.active_ticks()); } 170 171 void monitor_wait() { check_and_reset_count(); _monitor_wait_count++; } 172 void monitor_wait_begin() { check_and_reset_timer(); _monitor_wait_timer.start(); } 173 void monitor_wait_end() { _monitor_wait_timer.stop(); check_and_reset_timer(); } 174 175 void thread_sleep() { check_and_reset_count(); _sleep_count++; } 176 void thread_sleep_begin() { check_and_reset_timer(); _sleep_timer.start(); } 177 void thread_sleep_end() { _sleep_timer.stop(); check_and_reset_timer(); } 178 179 void contended_enter() { check_and_reset_count(); _contended_enter_count++; } 180 void contended_enter_begin() { check_and_reset_timer(); _contended_enter_timer.start(); } 181 void contended_enter_end() { _contended_enter_timer.stop(); check_and_reset_timer(); } 182 183 void reset_count_stat() { _count_pending_reset = true; } 184 void reset_time_stat() { _timer_pending_reset = true; } 185 186 int* perf_recursion_counts_addr() { return _perf_recursion_counts; } 187 elapsedTimer* perf_timers_addr() { return _perf_timers; } 188 }; 189 190 // Thread snapshot to represent the thread state and statistics 191 class ThreadSnapshot : public CHeapObj<mtInternal> { 192 private: 193 // This JavaThread* is protected by being stored in objects that are 194 // protected by a ThreadsListSetter (ThreadDumpResult). 195 JavaThread* _thread; 196 oop _threadObj; 197 java_lang_Thread::ThreadStatus _thread_status; 198 199 bool _is_ext_suspended; 200 bool _is_in_native; 201 202 jlong _contended_enter_ticks; 203 jlong _contended_enter_count; 204 jlong _monitor_wait_ticks; 205 jlong _monitor_wait_count; 206 jlong _sleep_ticks; 207 jlong _sleep_count; 208 oop _blocker_object; 209 oop _blocker_object_owner; 210 211 ThreadStackTrace* _stack_trace; 212 ThreadConcurrentLocks* _concurrent_locks; 213 ThreadSnapshot* _next; 214 215 public: 216 // Dummy snapshot 217 ThreadSnapshot() : _thread(NULL), _threadObj(NULL), _stack_trace(NULL), _concurrent_locks(NULL), _next(NULL), 218 _blocker_object(NULL), _blocker_object_owner(NULL) {}; 219 ThreadSnapshot(ThreadsList * t_list, JavaThread* thread); 220 ~ThreadSnapshot(); 221 222 java_lang_Thread::ThreadStatus thread_status() { return _thread_status; } 223 224 oop threadObj() const { return _threadObj; } 225 226 void set_next(ThreadSnapshot* n) { _next = n; } 227 228 bool is_ext_suspended() { return _is_ext_suspended; } 229 bool is_in_native() { return _is_in_native; } 230 231 jlong contended_enter_count() { return _contended_enter_count; } 232 jlong contended_enter_ticks() { return _contended_enter_ticks; } 233 jlong monitor_wait_count() { return _monitor_wait_count; } 234 jlong monitor_wait_ticks() { return _monitor_wait_ticks; } 235 jlong sleep_count() { return _sleep_count; } 236 jlong sleep_ticks() { return _sleep_ticks; } 237 238 239 oop blocker_object() { return _blocker_object; } 240 oop blocker_object_owner() { return _blocker_object_owner; } 241 242 ThreadSnapshot* next() const { return _next; } 243 ThreadStackTrace* get_stack_trace() { return _stack_trace; } 244 ThreadConcurrentLocks* get_concurrent_locks() { return _concurrent_locks; } 245 246 void dump_stack_at_safepoint(int max_depth, bool with_locked_monitors); 247 void set_concurrent_locks(ThreadConcurrentLocks* l) { _concurrent_locks = l; } 248 void oops_do(OopClosure* f); 249 void metadata_do(void f(Metadata*)); 250 }; 251 252 class ThreadStackTrace : public CHeapObj<mtInternal> { 253 private: 254 JavaThread* _thread; 255 int _depth; // number of stack frames added 256 bool _with_locked_monitors; 257 GrowableArray<StackFrameInfo*>* _frames; 258 GrowableArray<oop>* _jni_locked_monitors; 259 260 public: 261 262 ThreadStackTrace(JavaThread* thread, bool with_locked_monitors); 263 ~ThreadStackTrace(); 264 265 JavaThread* thread() { return _thread; } 266 StackFrameInfo* stack_frame_at(int i) { return _frames->at(i); } 267 int get_stack_depth() { return _depth; } 268 269 void add_stack_frame(javaVFrame* jvf); 270 void dump_stack_at_safepoint(int max_depth); 271 Handle allocate_fill_stack_trace_element_array(TRAPS); 272 void oops_do(OopClosure* f); 273 void metadata_do(void f(Metadata*)); 274 GrowableArray<oop>* jni_locked_monitors() { return _jni_locked_monitors; } 275 int num_jni_locked_monitors() { return (_jni_locked_monitors != NULL ? _jni_locked_monitors->length() : 0); } 276 277 bool is_owned_monitor_on_stack(oop object); 278 void add_jni_locked_monitor(oop object) { _jni_locked_monitors->append(object); } 279 }; 280 281 // StackFrameInfo for keeping Method* and bci during 282 // stack walking for later construction of StackTraceElement[] 283 // Java instances 284 class StackFrameInfo : public CHeapObj<mtInternal> { 285 private: 286 Method* _method; 287 int _bci; 288 GrowableArray<oop>* _locked_monitors; // list of object monitors locked by this frame 289 // We need to save the mirrors in the backtrace to keep the class 290 // from being unloaded while we still have this stack trace. 291 oop _class_holder; 292 293 public: 294 295 StackFrameInfo(javaVFrame* jvf, bool with_locked_monitors); 296 ~StackFrameInfo() { 297 if (_locked_monitors != NULL) { 298 delete _locked_monitors; 299 } 300 }; 301 Method* method() const { return _method; } 302 int bci() const { return _bci; } 303 void oops_do(OopClosure* f); 304 void metadata_do(void f(Metadata*)); 305 306 int num_locked_monitors() { return (_locked_monitors != NULL ? _locked_monitors->length() : 0); } 307 GrowableArray<oop>* locked_monitors() { return _locked_monitors; } 308 309 void print_on(outputStream* st) const; 310 }; 311 312 class ThreadConcurrentLocks : public CHeapObj<mtInternal> { 313 private: 314 GrowableArray<instanceOop>* _owned_locks; 315 ThreadConcurrentLocks* _next; 316 // This JavaThread* is protected in one of two different ways 317 // depending on the usage of the ThreadConcurrentLocks object: 318 // 1) by being stored in objects that are only allocated and used at a 319 // safepoint (ConcurrentLocksDump), or 2) by being stored in objects 320 // that are protected by a ThreadsListSetter (ThreadSnapshot inside 321 // ThreadDumpResult). 322 JavaThread* _thread; 323 public: 324 ThreadConcurrentLocks(JavaThread* thread); 325 ~ThreadConcurrentLocks(); 326 327 void add_lock(instanceOop o); 328 void set_next(ThreadConcurrentLocks* n) { _next = n; } 329 ThreadConcurrentLocks* next() { return _next; } 330 JavaThread* java_thread() { return _thread; } 331 GrowableArray<instanceOop>* owned_locks() { return _owned_locks; } 332 void oops_do(OopClosure* f); 333 }; 334 335 class ConcurrentLocksDump : public StackObj { 336 private: 337 ThreadConcurrentLocks* _map; 338 ThreadConcurrentLocks* _last; // Last ThreadConcurrentLocks in the map 339 bool _retain_map_on_free; 340 341 void build_map(GrowableArray<oop>* aos_objects); 342 void add_lock(JavaThread* thread, instanceOop o); 343 344 public: 345 ConcurrentLocksDump(bool retain_map_on_free) : _map(NULL), _last(NULL), _retain_map_on_free(retain_map_on_free) { 346 assert(SafepointSynchronize::is_at_safepoint(), "Must be constructed at a safepoint."); 347 }; 348 ConcurrentLocksDump() : _map(NULL), _last(NULL), _retain_map_on_free(false) { 349 assert(SafepointSynchronize::is_at_safepoint(), "Must be constructed at a safepoint."); 350 }; 351 ~ConcurrentLocksDump(); 352 353 void dump_at_safepoint(); 354 ThreadConcurrentLocks* thread_concurrent_locks(JavaThread* thread); 355 void print_locks_on(JavaThread* t, outputStream* st); 356 }; 357 358 class ThreadDumpResult : public StackObj { 359 private: 360 int _num_threads; 361 int _num_snapshots; 362 ThreadSnapshot* _snapshots; 363 ThreadSnapshot* _last; 364 ThreadDumpResult* _next; 365 ThreadsListSetter _setter; // Helper to set hazard ptr in the originating thread 366 // which protects the JavaThreads in _snapshots. 367 368 public: 369 ThreadDumpResult(); 370 ThreadDumpResult(int num_threads); 371 ~ThreadDumpResult(); 372 373 void add_thread_snapshot(ThreadSnapshot* ts); 374 void set_next(ThreadDumpResult* next) { _next = next; } 375 ThreadDumpResult* next() { return _next; } 376 int num_threads() { return _num_threads; } 377 int num_snapshots() { return _num_snapshots; } 378 ThreadSnapshot* snapshots() { return _snapshots; } 379 void set_t_list() { _setter.set(); } 380 ThreadsList* t_list(); 381 bool t_list_has_been_set() { return _setter.target_needs_release(); } 382 void oops_do(OopClosure* f); 383 void metadata_do(void f(Metadata*)); 384 }; 385 386 class DeadlockCycle : public CHeapObj<mtInternal> { 387 private: 388 bool _is_deadlock; 389 GrowableArray<JavaThread*>* _threads; 390 DeadlockCycle* _next; 391 public: 392 DeadlockCycle(); 393 ~DeadlockCycle(); 394 395 DeadlockCycle* next() { return _next; } 396 void set_next(DeadlockCycle* d) { _next = d; } 397 void add_thread(JavaThread* t) { _threads->append(t); } 398 void reset() { _is_deadlock = false; _threads->clear(); } 399 void set_deadlock(bool value) { _is_deadlock = value; } 400 bool is_deadlock() { return _is_deadlock; } 401 int num_threads() { return _threads->length(); } 402 GrowableArray<JavaThread*>* threads() { return _threads; } 403 void print_on_with(ThreadsList * t_list, outputStream* st) const; 404 }; 405 406 // Utility class to get list of java threads. 407 class ThreadsListEnumerator : public StackObj { 408 private: 409 GrowableArray<instanceHandle>* _threads_array; 410 public: 411 ThreadsListEnumerator(Thread* cur_thread, 412 bool include_jvmti_agent_threads = false, 413 bool include_jni_attaching_threads = true); 414 int num_threads() { return _threads_array->length(); } 415 instanceHandle get_threadObj(int index) { return _threads_array->at(index); } 416 }; 417 418 419 // abstract utility class to set new thread states, and restore previous after the block exits 420 class JavaThreadStatusChanger : public StackObj { 421 private: 422 java_lang_Thread::ThreadStatus _old_state; 423 JavaThread* _java_thread; 424 bool _is_alive; 425 426 void save_old_state(JavaThread* java_thread) { 427 _java_thread = java_thread; 428 _is_alive = is_alive(java_thread); 429 if (is_alive()) { 430 _old_state = java_lang_Thread::get_thread_status(_java_thread->threadObj()); 431 } 432 } 433 434 public: 435 static void set_thread_status(JavaThread* java_thread, 436 java_lang_Thread::ThreadStatus state) { 437 java_lang_Thread::set_thread_status(java_thread->threadObj(), state); 438 } 439 440 void set_thread_status(java_lang_Thread::ThreadStatus state) { 441 if (is_alive()) { 442 set_thread_status(_java_thread, state); 443 } 444 } 445 446 JavaThreadStatusChanger(JavaThread* java_thread, 447 java_lang_Thread::ThreadStatus state) : _old_state(java_lang_Thread::NEW) { 448 save_old_state(java_thread); 449 set_thread_status(state); 450 } 451 452 JavaThreadStatusChanger(JavaThread* java_thread) : _old_state(java_lang_Thread::NEW) { 453 save_old_state(java_thread); 454 } 455 456 ~JavaThreadStatusChanger() { 457 set_thread_status(_old_state); 458 } 459 460 static bool is_alive(JavaThread* java_thread) { 461 return java_thread != NULL && java_thread->threadObj() != NULL; 462 } 463 464 bool is_alive() { 465 return _is_alive; 466 } 467 }; 468 469 // Change status to waiting on an object (timed or indefinite) 470 class JavaThreadInObjectWaitState : public JavaThreadStatusChanger { 471 private: 472 ThreadStatistics* _stat; 473 bool _active; 474 475 public: 476 JavaThreadInObjectWaitState(JavaThread *java_thread, bool timed) : 477 JavaThreadStatusChanger(java_thread, 478 timed ? java_lang_Thread::IN_OBJECT_WAIT_TIMED : java_lang_Thread::IN_OBJECT_WAIT) { 479 if (is_alive()) { 480 _stat = java_thread->get_thread_stat(); 481 _active = ThreadService::is_thread_monitoring_contention(); 482 _stat->monitor_wait(); 483 if (_active) { 484 _stat->monitor_wait_begin(); 485 } 486 } else { 487 _active = false; 488 } 489 } 490 491 ~JavaThreadInObjectWaitState() { 492 if (_active) { 493 _stat->monitor_wait_end(); 494 } 495 } 496 }; 497 498 // Change status to parked (timed or indefinite) 499 class JavaThreadParkedState : public JavaThreadStatusChanger { 500 private: 501 ThreadStatistics* _stat; 502 bool _active; 503 504 public: 505 JavaThreadParkedState(JavaThread *java_thread, bool timed) : 506 JavaThreadStatusChanger(java_thread, 507 timed ? java_lang_Thread::PARKED_TIMED : java_lang_Thread::PARKED) { 508 if (is_alive()) { 509 _stat = java_thread->get_thread_stat(); 510 _active = ThreadService::is_thread_monitoring_contention(); 511 _stat->monitor_wait(); 512 if (_active) { 513 _stat->monitor_wait_begin(); 514 } 515 } else { 516 _active = false; 517 } 518 } 519 520 ~JavaThreadParkedState() { 521 if (_active) { 522 _stat->monitor_wait_end(); 523 } 524 } 525 }; 526 527 // Change status to blocked on (re-)entering a synchronization block 528 class JavaThreadBlockedOnMonitorEnterState : public JavaThreadStatusChanger { 529 private: 530 ThreadStatistics* _stat; 531 bool _active; 532 533 static bool contended_enter_begin(JavaThread *java_thread) { 534 set_thread_status(java_thread, java_lang_Thread::BLOCKED_ON_MONITOR_ENTER); 535 ThreadStatistics* stat = java_thread->get_thread_stat(); 536 stat->contended_enter(); 537 bool active = ThreadService::is_thread_monitoring_contention(); 538 if (active) { 539 stat->contended_enter_begin(); 540 } 541 return active; 542 } 543 544 public: 545 // java_thread is waiting thread being blocked on monitor reenter. 546 // Current thread is the notifying thread which holds the monitor. 547 static bool wait_reenter_begin(JavaThread *java_thread, ObjectMonitor *obj_m) { 548 assert((java_thread != NULL), "Java thread should not be null here"); 549 bool active = false; 550 if (is_alive(java_thread) && ServiceUtil::visible_oop((oop)obj_m->object())) { 551 active = contended_enter_begin(java_thread); 552 } 553 return active; 554 } 555 556 static void wait_reenter_end(JavaThread *java_thread, bool active) { 557 if (active) { 558 java_thread->get_thread_stat()->contended_enter_end(); 559 } 560 set_thread_status(java_thread, java_lang_Thread::RUNNABLE); 561 } 562 563 JavaThreadBlockedOnMonitorEnterState(JavaThread *java_thread, ObjectMonitor *obj_m) : 564 _stat(NULL), _active(false), JavaThreadStatusChanger(java_thread) { 565 assert((java_thread != NULL), "Java thread should not be null here"); 566 // Change thread status and collect contended enter stats for monitor contended 567 // enter done for external java world objects and it is contended. All other cases 568 // like for vm internal objects and for external objects which are not contended 569 // thread status is not changed and contended enter stat is not collected. 570 _active = false; 571 if (is_alive() && ServiceUtil::visible_oop((oop)obj_m->object()) && obj_m->contentions() > 0) { 572 _stat = java_thread->get_thread_stat(); 573 _active = contended_enter_begin(java_thread); 574 } 575 } 576 577 ~JavaThreadBlockedOnMonitorEnterState() { 578 if (_active) { 579 _stat->contended_enter_end(); 580 } 581 } 582 }; 583 584 // Change status to sleeping 585 class JavaThreadSleepState : public JavaThreadStatusChanger { 586 private: 587 ThreadStatistics* _stat; 588 bool _active; 589 public: 590 JavaThreadSleepState(JavaThread *java_thread) : 591 JavaThreadStatusChanger(java_thread, java_lang_Thread::SLEEPING) { 592 if (is_alive()) { 593 _stat = java_thread->get_thread_stat(); 594 _active = ThreadService::is_thread_monitoring_contention(); 595 _stat->thread_sleep(); 596 if (_active) { 597 _stat->thread_sleep_begin(); 598 } 599 } else { 600 _active = false; 601 } 602 } 603 604 ~JavaThreadSleepState() { 605 if (_active) { 606 _stat->thread_sleep_end(); 607 } 608 } 609 }; 610 611 #endif // SHARE_VM_SERVICES_THREADSERVICE_HPP