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