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 "runtime/threadSMR.hpp"
37 #include "services/management.hpp"
38 #include "services/serviceUtil.hpp"
39
40 class OopClosure;
41 class ThreadDumpResult;
42 class ThreadStackTrace;
43 class ThreadSnapshot;
44 class StackFrameInfo;
45 class ThreadConcurrentLocks;
46 class DeadlockCycle;
47
48 // VM monitoring and management support for the thread and
49 // synchronization subsystem
50 //
51 // Thread contention monitoring is disabled by default.
52 // When enabled, the VM will begin measuring the accumulated
53 // elapsed time a thread blocked on synchronization.
54 //
55 class ThreadService : public AllStatic {
56 private:
57 // These counters could be moved to Threads class
58 static PerfCounter* _total_threads_count;
531 ThreadStatistics* _stat;
532 bool _active;
533
534 static bool contended_enter_begin(JavaThread *java_thread) {
535 set_thread_status(java_thread, java_lang_Thread::BLOCKED_ON_MONITOR_ENTER);
536 ThreadStatistics* stat = java_thread->get_thread_stat();
537 stat->contended_enter();
538 bool active = ThreadService::is_thread_monitoring_contention();
539 if (active) {
540 stat->contended_enter_begin();
541 }
542 return active;
543 }
544
545 public:
546 // java_thread is waiting thread being blocked on monitor reenter.
547 // Current thread is the notifying thread which holds the monitor.
548 static bool wait_reenter_begin(JavaThread *java_thread, ObjectMonitor *obj_m) {
549 assert((java_thread != NULL), "Java thread should not be null here");
550 bool active = false;
551 if (is_alive(java_thread) && ServiceUtil::visible_oop((oop)obj_m->object())) {
552 active = contended_enter_begin(java_thread);
553 }
554 return active;
555 }
556
557 static void wait_reenter_end(JavaThread *java_thread, bool active) {
558 if (active) {
559 java_thread->get_thread_stat()->contended_enter_end();
560 }
561 set_thread_status(java_thread, java_lang_Thread::RUNNABLE);
562 }
563
564 JavaThreadBlockedOnMonitorEnterState(JavaThread *java_thread, ObjectMonitor *obj_m) :
565 _stat(NULL), _active(false), JavaThreadStatusChanger(java_thread) {
566 assert((java_thread != NULL), "Java thread should not be null here");
567 // Change thread status and collect contended enter stats for monitor contended
568 // enter done for external java world objects and it is contended. All other cases
569 // like for vm internal objects and for external objects which are not contended
570 // thread status is not changed and contended enter stat is not collected.
571 _active = false;
572 if (is_alive() && ServiceUtil::visible_oop((oop)obj_m->object()) && obj_m->contentions() > 0) {
573 _stat = java_thread->get_thread_stat();
574 _active = contended_enter_begin(java_thread);
575 }
576 }
577
578 ~JavaThreadBlockedOnMonitorEnterState() {
579 if (_active) {
580 _stat->contended_enter_end();
581 }
582 }
583 };
584
585 // Change status to sleeping
586 class JavaThreadSleepState : public JavaThreadStatusChanger {
587 private:
588 ThreadStatistics* _stat;
589 bool _active;
590 public:
591 JavaThreadSleepState(JavaThread *java_thread) :
592 JavaThreadStatusChanger(java_thread, java_lang_Thread::SLEEPING) {
|
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 "runtime/threadSMR.hpp"
37 #include "services/management.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;
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)) {
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() && 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) {
|