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