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