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