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