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