40 #include "runtime/vframe.hpp"
41 #include "runtime/vmThread.hpp"
42 #include "runtime/vm_operations.hpp"
43 #include "services/threadService.hpp"
44
45 // TODO: we need to define a naming convention for perf counters
46 // to distinguish counters for:
47 // - standard JSR174 use
48 // - Hotspot extension (public and committed)
49 // - Hotspot extension (private/internal and uncommitted)
50
51 // Default is disabled.
52 bool ThreadService::_thread_monitoring_contention_enabled = false;
53 bool ThreadService::_thread_cpu_time_enabled = false;
54 bool ThreadService::_thread_allocated_memory_enabled = false;
55
56 PerfCounter* ThreadService::_total_threads_count = NULL;
57 PerfVariable* ThreadService::_live_threads_count = NULL;
58 PerfVariable* ThreadService::_peak_threads_count = NULL;
59 PerfVariable* ThreadService::_daemon_threads_count = NULL;
60 volatile int ThreadService::_exiting_threads_count = 0;
61 volatile int ThreadService::_exiting_daemon_threads_count = 0;
62
63 ThreadDumpResult* ThreadService::_threaddump_list = NULL;
64
65 static const int INITIAL_ARRAY_SIZE = 10;
66
67 void ThreadService::init() {
68 EXCEPTION_MARK;
69
70 // These counters are for java.lang.management API support.
71 // They are created even if -XX:-UsePerfData is set and in
72 // that case, they will be allocated on C heap.
73
74 _total_threads_count =
75 PerfDataManager::create_counter(JAVA_THREADS, "started",
76 PerfData::U_Events, CHECK);
77
78 _live_threads_count =
79 PerfDataManager::create_variable(JAVA_THREADS, "live",
80 PerfData::U_None, CHECK);
81
84 PerfData::U_None, CHECK);
85
86 _daemon_threads_count =
87 PerfDataManager::create_variable(JAVA_THREADS, "daemon",
88 PerfData::U_None, CHECK);
89
90 if (os::is_thread_cpu_time_supported()) {
91 _thread_cpu_time_enabled = true;
92 }
93
94 _thread_allocated_memory_enabled = true; // Always on, so enable it
95 }
96
97 void ThreadService::reset_peak_thread_count() {
98 // Acquire the lock to update the peak thread count
99 // to synchronize with thread addition and removal.
100 MutexLockerEx mu(Threads_lock);
101 _peak_threads_count->set_value(get_live_thread_count());
102 }
103
104 void ThreadService::add_thread(JavaThread* thread, bool daemon) {
105 // Do not count VM internal or JVMTI agent threads
106 if (thread->is_hidden_from_external_view() ||
107 thread->is_jvmti_agent_thread()) {
108 return;
109 }
110
111 _total_threads_count->inc();
112 _live_threads_count->inc();
113
114 if (_live_threads_count->get_value() > _peak_threads_count->get_value()) {
115 _peak_threads_count->set_value(_live_threads_count->get_value());
116 }
117
118 if (daemon) {
119 _daemon_threads_count->inc();
120 }
121 }
122
123 void ThreadService::remove_thread(JavaThread* thread, bool daemon) {
124 Atomic::dec(&_exiting_threads_count);
125 if (daemon) {
126 Atomic::dec(&_exiting_daemon_threads_count);
127 }
128
129 if (thread->is_hidden_from_external_view() ||
130 thread->is_jvmti_agent_thread()) {
131 return;
132 }
133
134 _live_threads_count->set_value(_live_threads_count->get_value() - 1);
135 if (daemon) {
136 _daemon_threads_count->set_value(_daemon_threads_count->get_value() - 1);
137 }
138 }
139
140 void ThreadService::current_thread_exiting(JavaThread* jt) {
141 assert(jt == JavaThread::current(), "Called by current thread");
142 Atomic::inc(&_exiting_threads_count);
143
144 oop threadObj = jt->threadObj();
145 if (threadObj != NULL && java_lang_Thread::is_daemon(threadObj)) {
146 Atomic::inc(&_exiting_daemon_threads_count);
147 }
148 }
149
150 // FIXME: JVMTI should call this function
151 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
152 assert(thread != NULL, "should be non-NULL");
153 debug_only(Thread::check_for_dangling_thread_pointer(thread);)
154
155 ObjectMonitor *wait_obj = thread->current_waiting_monitor();
156
157 oop obj = NULL;
158 if (wait_obj != NULL) {
159 // thread is doing an Object.wait() call
160 obj = (oop) wait_obj->object();
161 assert(obj != NULL, "Object.wait() should have an object");
162 } else {
163 ObjectMonitor *enter_obj = thread->current_pending_monitor();
164 if (enter_obj != NULL) {
165 // thread is trying to enter() or raw_enter() an ObjectMonitor.
166 obj = (oop) enter_obj->object();
167 }
|
40 #include "runtime/vframe.hpp"
41 #include "runtime/vmThread.hpp"
42 #include "runtime/vm_operations.hpp"
43 #include "services/threadService.hpp"
44
45 // TODO: we need to define a naming convention for perf counters
46 // to distinguish counters for:
47 // - standard JSR174 use
48 // - Hotspot extension (public and committed)
49 // - Hotspot extension (private/internal and uncommitted)
50
51 // Default is disabled.
52 bool ThreadService::_thread_monitoring_contention_enabled = false;
53 bool ThreadService::_thread_cpu_time_enabled = false;
54 bool ThreadService::_thread_allocated_memory_enabled = false;
55
56 PerfCounter* ThreadService::_total_threads_count = NULL;
57 PerfVariable* ThreadService::_live_threads_count = NULL;
58 PerfVariable* ThreadService::_peak_threads_count = NULL;
59 PerfVariable* ThreadService::_daemon_threads_count = NULL;
60
61 ThreadDumpResult* ThreadService::_threaddump_list = NULL;
62
63 static const int INITIAL_ARRAY_SIZE = 10;
64
65 void ThreadService::init() {
66 EXCEPTION_MARK;
67
68 // These counters are for java.lang.management API support.
69 // They are created even if -XX:-UsePerfData is set and in
70 // that case, they will be allocated on C heap.
71
72 _total_threads_count =
73 PerfDataManager::create_counter(JAVA_THREADS, "started",
74 PerfData::U_Events, CHECK);
75
76 _live_threads_count =
77 PerfDataManager::create_variable(JAVA_THREADS, "live",
78 PerfData::U_None, CHECK);
79
82 PerfData::U_None, CHECK);
83
84 _daemon_threads_count =
85 PerfDataManager::create_variable(JAVA_THREADS, "daemon",
86 PerfData::U_None, CHECK);
87
88 if (os::is_thread_cpu_time_supported()) {
89 _thread_cpu_time_enabled = true;
90 }
91
92 _thread_allocated_memory_enabled = true; // Always on, so enable it
93 }
94
95 void ThreadService::reset_peak_thread_count() {
96 // Acquire the lock to update the peak thread count
97 // to synchronize with thread addition and removal.
98 MutexLockerEx mu(Threads_lock);
99 _peak_threads_count->set_value(get_live_thread_count());
100 }
101
102 static bool is_hidden_thread(JavaThread *thread) {
103 // hide VM internal or JVMTI agent threads
104 return thread->is_hidden_from_external_view() || thread->is_jvmti_agent_thread();
105 }
106
107 void ThreadService::add_thread(JavaThread* thread, bool daemon) {
108 assert(Threads_lock->owned_by_self(), "must have threads lock");
109
110 // Do not count hidden threads
111 if (is_hidden_thread(thread)) {
112 return;
113 }
114
115 _total_threads_count->inc();
116 _live_threads_count->inc();
117
118 if (_live_threads_count->get_value() > _peak_threads_count->get_value()) {
119 _peak_threads_count->set_value(_live_threads_count->get_value());
120 }
121
122 if (daemon) {
123 _daemon_threads_count->inc();
124 }
125 }
126
127 void ThreadService::decrement_thread_counts(JavaThread* jt, bool daemon) {
128 assert(Threads_lock->owned_by_self(), "must have threads lock");
129
130 _live_threads_count->dec(1);
131
132 if (daemon) {
133 _daemon_threads_count->dec(1);
134 }
135 }
136
137 void ThreadService::remove_thread(JavaThread* thread, bool daemon) {
138 assert(Threads_lock->owned_by_self(), "must have threads lock");
139
140 // Do not count hidden threads
141 if (is_hidden_thread(thread)) {
142 return;
143 }
144
145 assert(!thread->is_terminated(), "must not be terminated");
146 if (!thread->is_exiting()) {
147 // JavaThread::exit() skipped calling current_thread_exiting()
148 decrement_thread_counts(thread, daemon);
149 }
150
151 }
152
153 void ThreadService::current_thread_exiting(JavaThread* jt, bool daemon) {
154 assert(Threads_lock->owned_by_self(), "must have threads lock");
155
156 // Do not count hidden threads
157 if (is_hidden_thread(jt)) {
158 return;
159 }
160
161 assert(jt == JavaThread::current(), "Called by current thread");
162 assert(!jt->is_terminated() && jt->is_exiting(), "must be exiting");
163
164 decrement_thread_counts(jt, daemon);
165 }
166
167 // FIXME: JVMTI should call this function
168 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
169 assert(thread != NULL, "should be non-NULL");
170 debug_only(Thread::check_for_dangling_thread_pointer(thread);)
171
172 ObjectMonitor *wait_obj = thread->current_waiting_monitor();
173
174 oop obj = NULL;
175 if (wait_obj != NULL) {
176 // thread is doing an Object.wait() call
177 obj = (oop) wait_obj->object();
178 assert(obj != NULL, "Object.wait() should have an object");
179 } else {
180 ObjectMonitor *enter_obj = thread->current_pending_monitor();
181 if (enter_obj != NULL) {
182 // thread is trying to enter() or raw_enter() an ObjectMonitor.
183 obj = (oop) enter_obj->object();
184 }
|