annotate src/hotspot/share/services/threadService.hpp @ 53534:44a9dd4e4d96

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