src/share/vm/services/threadService.hpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 8604
04d83ba48607
permissions
-rw-r--r--

Merge

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

mercurial