src/share/vm/services/threadService.hpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/services/threadService.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,592 @@
     1.4 +/*
     1.5 + * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_SERVICES_THREADSERVICE_HPP
    1.29 +#define SHARE_VM_SERVICES_THREADSERVICE_HPP
    1.30 +
    1.31 +#include "classfile/javaClasses.hpp"
    1.32 +#include "runtime/handles.hpp"
    1.33 +#include "runtime/init.hpp"
    1.34 +#include "runtime/jniHandles.hpp"
    1.35 +#include "runtime/objectMonitor.hpp"
    1.36 +#include "runtime/objectMonitor.inline.hpp"
    1.37 +#include "runtime/perfData.hpp"
    1.38 +#include "services/management.hpp"
    1.39 +#include "services/serviceUtil.hpp"
    1.40 +
    1.41 +class OopClosure;
    1.42 +class ThreadDumpResult;
    1.43 +class ThreadStackTrace;
    1.44 +class ThreadSnapshot;
    1.45 +class StackFrameInfo;
    1.46 +class ThreadConcurrentLocks;
    1.47 +class DeadlockCycle;
    1.48 +
    1.49 +// VM monitoring and management support for the thread and
    1.50 +// synchronization subsystem
    1.51 +//
    1.52 +// Thread contention monitoring is disabled by default.
    1.53 +// When enabled, the VM will begin measuring the accumulated
    1.54 +// elapsed time a thread blocked on synchronization.
    1.55 +//
    1.56 +class ThreadService : public AllStatic {
    1.57 +private:
    1.58 +  // These counters could be moved to Threads class
    1.59 +  static PerfCounter*  _total_threads_count;
    1.60 +  static PerfVariable* _live_threads_count;
    1.61 +  static PerfVariable* _peak_threads_count;
    1.62 +  static PerfVariable* _daemon_threads_count;
    1.63 +
    1.64 +  // These 2 counters are atomically incremented once the thread is exiting.
    1.65 +  // They will be atomically decremented when ThreadService::remove_thread is called.
    1.66 +  static volatile int  _exiting_threads_count;
    1.67 +  static volatile int  _exiting_daemon_threads_count;
    1.68 +
    1.69 +  static bool          _thread_monitoring_contention_enabled;
    1.70 +  static bool          _thread_cpu_time_enabled;
    1.71 +  static bool          _thread_allocated_memory_enabled;
    1.72 +
    1.73 +  // Need to keep the list of thread dump result that
    1.74 +  // keep references to Method* since thread dump can be
    1.75 +  // requested by multiple threads concurrently.
    1.76 +  static ThreadDumpResult* _threaddump_list;
    1.77 +
    1.78 +public:
    1.79 +  static void init();
    1.80 +  static void add_thread(JavaThread* thread, bool daemon);
    1.81 +  static void remove_thread(JavaThread* thread, bool daemon);
    1.82 +  static void current_thread_exiting(JavaThread* jt);
    1.83 +
    1.84 +  static bool set_thread_monitoring_contention(bool flag);
    1.85 +  static bool is_thread_monitoring_contention() { return _thread_monitoring_contention_enabled; }
    1.86 +
    1.87 +  static bool set_thread_cpu_time_enabled(bool flag);
    1.88 +  static bool is_thread_cpu_time_enabled()    { return _thread_cpu_time_enabled; }
    1.89 +
    1.90 +  static bool set_thread_allocated_memory_enabled(bool flag);
    1.91 +  static bool is_thread_allocated_memory_enabled() { return _thread_cpu_time_enabled; }
    1.92 +
    1.93 +  static jlong get_total_thread_count()       { return _total_threads_count->get_value(); }
    1.94 +  static jlong get_peak_thread_count()        { return _peak_threads_count->get_value(); }
    1.95 +  static jlong get_live_thread_count()        { return _live_threads_count->get_value() - _exiting_threads_count; }
    1.96 +  static jlong get_daemon_thread_count()      { return _daemon_threads_count->get_value() - _exiting_daemon_threads_count; }
    1.97 +
    1.98 +  static int   exiting_threads_count()        { return _exiting_threads_count; }
    1.99 +  static int   exiting_daemon_threads_count() { return _exiting_daemon_threads_count; }
   1.100 +
   1.101 +  // Support for thread dump
   1.102 +  static void   add_thread_dump(ThreadDumpResult* dump);
   1.103 +  static void   remove_thread_dump(ThreadDumpResult* dump);
   1.104 +
   1.105 +  static Handle get_current_contended_monitor(JavaThread* thread);
   1.106 +
   1.107 +  // This function is called by JVM_DumpThreads.
   1.108 +  static Handle dump_stack_traces(GrowableArray<instanceHandle>* threads,
   1.109 +                                  int num_threads, TRAPS);
   1.110 +
   1.111 +  static void   reset_peak_thread_count();
   1.112 +  static void   reset_contention_count_stat(JavaThread* thread);
   1.113 +  static void   reset_contention_time_stat(JavaThread* thread);
   1.114 +
   1.115 +  static DeadlockCycle*       find_deadlocks_at_safepoint(bool object_monitors_only);
   1.116 +
   1.117 +  // GC support
   1.118 +  static void   oops_do(OopClosure* f);
   1.119 +  static void   metadata_do(void f(Metadata*));
   1.120 +};
   1.121 +
   1.122 +// Per-thread Statistics for synchronization
   1.123 +class ThreadStatistics : public CHeapObj<mtInternal> {
   1.124 +private:
   1.125 +  // The following contention statistics are only updated by
   1.126 +  // the thread owning these statistics when contention occurs.
   1.127 +
   1.128 +  jlong        _contended_enter_count;
   1.129 +  elapsedTimer _contended_enter_timer;
   1.130 +  jlong        _monitor_wait_count;
   1.131 +  elapsedTimer _monitor_wait_timer;
   1.132 +  jlong        _sleep_count;
   1.133 +  elapsedTimer _sleep_timer;
   1.134 +
   1.135 +
   1.136 +  // These two reset flags are set to true when another thread
   1.137 +  // requests to reset the statistics.  The actual statistics
   1.138 +  // are reset when the thread contention occurs and attempts
   1.139 +  // to update the statistics.
   1.140 +  bool         _count_pending_reset;
   1.141 +  bool         _timer_pending_reset;
   1.142 +
   1.143 +  // Keep accurate times for potentially recursive class operations
   1.144 +  int           _perf_recursion_counts[6];
   1.145 +  elapsedTimer  _perf_timers[6];
   1.146 +
   1.147 +  // utility functions
   1.148 +  void  check_and_reset_count()            {
   1.149 +                                             if (!_count_pending_reset) return;
   1.150 +                                             _contended_enter_count = 0;
   1.151 +                                             _monitor_wait_count = 0;
   1.152 +                                             _sleep_count = 0;
   1.153 +                                             _count_pending_reset = 0;
   1.154 +                                           }
   1.155 +  void  check_and_reset_timer()            {
   1.156 +                                             if (!_timer_pending_reset) return;
   1.157 +                                             _contended_enter_timer.reset();
   1.158 +                                             _monitor_wait_timer.reset();
   1.159 +                                             _sleep_timer.reset();
   1.160 +                                             _timer_pending_reset = 0;
   1.161 +                                           }
   1.162 +
   1.163 +public:
   1.164 +  ThreadStatistics();
   1.165 +
   1.166 +  jlong contended_enter_count()            { return (_count_pending_reset ? 0 : _contended_enter_count); }
   1.167 +  jlong contended_enter_ticks()            { return (_timer_pending_reset ? 0 : _contended_enter_timer.active_ticks()); }
   1.168 +  jlong monitor_wait_count()               { return (_count_pending_reset ? 0 : _monitor_wait_count); }
   1.169 +  jlong monitor_wait_ticks()               { return (_timer_pending_reset ? 0 : _monitor_wait_timer.active_ticks()); }
   1.170 +  jlong sleep_count()                      { return (_count_pending_reset ? 0 : _sleep_count); }
   1.171 +  jlong sleep_ticks()                      { return (_timer_pending_reset ? 0 : _sleep_timer.active_ticks()); }
   1.172 +
   1.173 +  void monitor_wait()                      { check_and_reset_count(); _monitor_wait_count++; }
   1.174 +  void monitor_wait_begin()                { check_and_reset_timer(); _monitor_wait_timer.start(); }
   1.175 +  void monitor_wait_end()                  { _monitor_wait_timer.stop(); check_and_reset_timer(); }
   1.176 +
   1.177 +  void thread_sleep()                      { check_and_reset_count(); _sleep_count++; }
   1.178 +  void thread_sleep_begin()                { check_and_reset_timer(); _sleep_timer.start(); }
   1.179 +  void thread_sleep_end()                  { _sleep_timer.stop(); check_and_reset_timer(); }
   1.180 +
   1.181 +  void contended_enter()                   { check_and_reset_count(); _contended_enter_count++; }
   1.182 +  void contended_enter_begin()             { check_and_reset_timer(); _contended_enter_timer.start(); }
   1.183 +  void contended_enter_end()               { _contended_enter_timer.stop(); check_and_reset_timer(); }
   1.184 +
   1.185 +  void reset_count_stat()                  { _count_pending_reset = true; }
   1.186 +  void reset_time_stat()                   { _timer_pending_reset = true; }
   1.187 +
   1.188 +  int* perf_recursion_counts_addr()        { return _perf_recursion_counts; }
   1.189 +  elapsedTimer* perf_timers_addr()         { return _perf_timers; }
   1.190 +};
   1.191 +
   1.192 +// Thread snapshot to represent the thread state and statistics
   1.193 +class ThreadSnapshot : public CHeapObj<mtInternal> {
   1.194 +private:
   1.195 +  JavaThread* _thread;
   1.196 +  oop         _threadObj;
   1.197 +  java_lang_Thread::ThreadStatus _thread_status;
   1.198 +
   1.199 +  bool    _is_ext_suspended;
   1.200 +  bool    _is_in_native;
   1.201 +
   1.202 +  jlong   _contended_enter_ticks;
   1.203 +  jlong   _contended_enter_count;
   1.204 +  jlong   _monitor_wait_ticks;
   1.205 +  jlong   _monitor_wait_count;
   1.206 +  jlong   _sleep_ticks;
   1.207 +  jlong   _sleep_count;
   1.208 +  oop     _blocker_object;
   1.209 +  oop     _blocker_object_owner;
   1.210 +
   1.211 +  ThreadStackTrace*      _stack_trace;
   1.212 +  ThreadConcurrentLocks* _concurrent_locks;
   1.213 +  ThreadSnapshot*        _next;
   1.214 +
   1.215 +public:
   1.216 +  // Dummy snapshot
   1.217 +  ThreadSnapshot() : _thread(NULL), _threadObj(NULL), _stack_trace(NULL), _concurrent_locks(NULL), _next(NULL),
   1.218 +                     _blocker_object(NULL), _blocker_object_owner(NULL) {};
   1.219 +  ThreadSnapshot(JavaThread* thread);
   1.220 +  ~ThreadSnapshot();
   1.221 +
   1.222 +  java_lang_Thread::ThreadStatus thread_status() { return _thread_status; }
   1.223 +
   1.224 +  oop         threadObj() const           { return _threadObj; }
   1.225 +
   1.226 +  void        set_next(ThreadSnapshot* n) { _next = n; }
   1.227 +
   1.228 +  bool        is_ext_suspended()          { return _is_ext_suspended; }
   1.229 +  bool        is_in_native()              { return _is_in_native; }
   1.230 +
   1.231 +  jlong       contended_enter_count()     { return _contended_enter_count; }
   1.232 +  jlong       contended_enter_ticks()     { return _contended_enter_ticks; }
   1.233 +  jlong       monitor_wait_count()        { return _monitor_wait_count; }
   1.234 +  jlong       monitor_wait_ticks()        { return _monitor_wait_ticks; }
   1.235 +  jlong       sleep_count()               { return _sleep_count; }
   1.236 +  jlong       sleep_ticks()               { return _sleep_ticks; }
   1.237 +
   1.238 +
   1.239 +  oop         blocker_object()            { return _blocker_object; }
   1.240 +  oop         blocker_object_owner()      { return _blocker_object_owner; }
   1.241 +
   1.242 +  ThreadSnapshot*   next() const          { return _next; }
   1.243 +  ThreadStackTrace* get_stack_trace()     { return _stack_trace; }
   1.244 +  ThreadConcurrentLocks* get_concurrent_locks()     { return _concurrent_locks; }
   1.245 +
   1.246 +  void        dump_stack_at_safepoint(int max_depth, bool with_locked_monitors);
   1.247 +  void        set_concurrent_locks(ThreadConcurrentLocks* l) { _concurrent_locks = l; }
   1.248 +  void        oops_do(OopClosure* f);
   1.249 +  void        metadata_do(void f(Metadata*));
   1.250 +};
   1.251 +
   1.252 +class ThreadStackTrace : public CHeapObj<mtInternal> {
   1.253 + private:
   1.254 +  JavaThread*                     _thread;
   1.255 +  int                             _depth;  // number of stack frames added
   1.256 +  bool                            _with_locked_monitors;
   1.257 +  GrowableArray<StackFrameInfo*>* _frames;
   1.258 +  GrowableArray<oop>*             _jni_locked_monitors;
   1.259 +
   1.260 + public:
   1.261 +
   1.262 +  ThreadStackTrace(JavaThread* thread, bool with_locked_monitors);
   1.263 +  ~ThreadStackTrace();
   1.264 +
   1.265 +  JavaThread*     thread()              { return _thread; }
   1.266 +  StackFrameInfo* stack_frame_at(int i) { return _frames->at(i); }
   1.267 +  int             get_stack_depth()     { return _depth; }
   1.268 +
   1.269 +  void            add_stack_frame(javaVFrame* jvf);
   1.270 +  void            dump_stack_at_safepoint(int max_depth);
   1.271 +  Handle          allocate_fill_stack_trace_element_array(TRAPS);
   1.272 +  void            oops_do(OopClosure* f);
   1.273 +  void            metadata_do(void f(Metadata*));
   1.274 +  GrowableArray<oop>* jni_locked_monitors() { return _jni_locked_monitors; }
   1.275 +  int             num_jni_locked_monitors() { return (_jni_locked_monitors != NULL ? _jni_locked_monitors->length() : 0); }
   1.276 +
   1.277 +  bool            is_owned_monitor_on_stack(oop object);
   1.278 +  void            add_jni_locked_monitor(oop object) { _jni_locked_monitors->append(object); }
   1.279 +};
   1.280 +
   1.281 +// StackFrameInfo for keeping Method* and bci during
   1.282 +// stack walking for later construction of StackTraceElement[]
   1.283 +// Java instances
   1.284 +class StackFrameInfo : public CHeapObj<mtInternal> {
   1.285 + private:
   1.286 +  Method*             _method;
   1.287 +  int                 _bci;
   1.288 +  GrowableArray<oop>* _locked_monitors; // list of object monitors locked by this frame
   1.289 +  // We need to save the mirrors in the backtrace to keep the class
   1.290 +  // from being unloaded while we still have this stack trace.
   1.291 +  oop                 _class_holder;
   1.292 +
   1.293 + public:
   1.294 +
   1.295 +  StackFrameInfo(javaVFrame* jvf, bool with_locked_monitors);
   1.296 +  ~StackFrameInfo() {
   1.297 +    if (_locked_monitors != NULL) {
   1.298 +      delete _locked_monitors;
   1.299 +    }
   1.300 +  };
   1.301 +  Method*   method() const       { return _method; }
   1.302 +  int       bci()    const       { return _bci; }
   1.303 +  void      oops_do(OopClosure* f);
   1.304 +  void      metadata_do(void f(Metadata*));
   1.305 +
   1.306 +  int       num_locked_monitors()       { return (_locked_monitors != NULL ? _locked_monitors->length() : 0); }
   1.307 +  GrowableArray<oop>* locked_monitors() { return _locked_monitors; }
   1.308 +
   1.309 +  void      print_on(outputStream* st) const;
   1.310 +};
   1.311 +
   1.312 +class ThreadConcurrentLocks : public CHeapObj<mtInternal> {
   1.313 +private:
   1.314 +  GrowableArray<instanceOop>* _owned_locks;
   1.315 +  ThreadConcurrentLocks*      _next;
   1.316 +  JavaThread*                 _thread;
   1.317 + public:
   1.318 +  ThreadConcurrentLocks(JavaThread* thread);
   1.319 +  ~ThreadConcurrentLocks();
   1.320 +
   1.321 +  void                        add_lock(instanceOop o);
   1.322 +  void                        set_next(ThreadConcurrentLocks* n) { _next = n; }
   1.323 +  ThreadConcurrentLocks*      next() { return _next; }
   1.324 +  JavaThread*                 java_thread()                      { return _thread; }
   1.325 +  GrowableArray<instanceOop>* owned_locks()                      { return _owned_locks; }
   1.326 +  void                        oops_do(OopClosure* f);
   1.327 +};
   1.328 +
   1.329 +class ConcurrentLocksDump : public StackObj {
   1.330 + private:
   1.331 +  ThreadConcurrentLocks* _map;
   1.332 +  ThreadConcurrentLocks* _last;   // Last ThreadConcurrentLocks in the map
   1.333 +  bool                   _retain_map_on_free;
   1.334 +
   1.335 +  void build_map(GrowableArray<oop>* aos_objects);
   1.336 +  void add_lock(JavaThread* thread, instanceOop o);
   1.337 +
   1.338 + public:
   1.339 +  ConcurrentLocksDump(bool retain_map_on_free) : _map(NULL), _last(NULL), _retain_map_on_free(retain_map_on_free) {};
   1.340 +  ConcurrentLocksDump() : _map(NULL), _last(NULL), _retain_map_on_free(false) {};
   1.341 +  ~ConcurrentLocksDump();
   1.342 +
   1.343 +  void                        dump_at_safepoint();
   1.344 +  ThreadConcurrentLocks*      thread_concurrent_locks(JavaThread* thread);
   1.345 +  void                        print_locks_on(JavaThread* t, outputStream* st);
   1.346 +};
   1.347 +
   1.348 +class ThreadDumpResult : public StackObj {
   1.349 + private:
   1.350 +  int                  _num_threads;
   1.351 +  int                  _num_snapshots;
   1.352 +  ThreadSnapshot*      _snapshots;
   1.353 +  ThreadSnapshot*      _last;
   1.354 +  ThreadDumpResult*    _next;
   1.355 + public:
   1.356 +  ThreadDumpResult();
   1.357 +  ThreadDumpResult(int num_threads);
   1.358 +  ~ThreadDumpResult();
   1.359 +
   1.360 +  void                 add_thread_snapshot(ThreadSnapshot* ts);
   1.361 +  void                 set_next(ThreadDumpResult* next) { _next = next; }
   1.362 +  ThreadDumpResult*    next()                           { return _next; }
   1.363 +  int                  num_threads()                    { return _num_threads; }
   1.364 +  int                  num_snapshots()                  { return _num_snapshots; }
   1.365 +  ThreadSnapshot*      snapshots()                      { return _snapshots; }
   1.366 +  void                 oops_do(OopClosure* f);
   1.367 +  void                 metadata_do(void f(Metadata*));
   1.368 +};
   1.369 +
   1.370 +class DeadlockCycle : public CHeapObj<mtInternal> {
   1.371 + private:
   1.372 +  bool _is_deadlock;
   1.373 +  GrowableArray<JavaThread*>* _threads;
   1.374 +  DeadlockCycle*              _next;
   1.375 + public:
   1.376 +  DeadlockCycle();
   1.377 +  ~DeadlockCycle();
   1.378 +
   1.379 +  DeadlockCycle* next()                     { return _next; }
   1.380 +  void           set_next(DeadlockCycle* d) { _next = d; }
   1.381 +  void           add_thread(JavaThread* t)  { _threads->append(t); }
   1.382 +  void           reset()                    { _is_deadlock = false; _threads->clear(); }
   1.383 +  void           set_deadlock(bool value)   { _is_deadlock = value; }
   1.384 +  bool           is_deadlock()              { return _is_deadlock; }
   1.385 +  int            num_threads()              { return _threads->length(); }
   1.386 +  GrowableArray<JavaThread*>* threads()     { return _threads; }
   1.387 +  void           print_on(outputStream* st) const;
   1.388 +};
   1.389 +
   1.390 +// Utility class to get list of java threads.
   1.391 +class ThreadsListEnumerator : public StackObj {
   1.392 +private:
   1.393 +  GrowableArray<instanceHandle>* _threads_array;
   1.394 +public:
   1.395 +  ThreadsListEnumerator(Thread* cur_thread,
   1.396 +                        bool include_jvmti_agent_threads = false,
   1.397 +                        bool include_jni_attaching_threads = true);
   1.398 +  int            num_threads()            { return _threads_array->length(); }
   1.399 +  instanceHandle get_threadObj(int index) { return _threads_array->at(index); }
   1.400 +};
   1.401 +
   1.402 +
   1.403 +// abstract utility class to set new thread states, and restore previous after the block exits
   1.404 +class JavaThreadStatusChanger : public StackObj {
   1.405 + private:
   1.406 +  java_lang_Thread::ThreadStatus _old_state;
   1.407 +  JavaThread*  _java_thread;
   1.408 +  bool _is_alive;
   1.409 +
   1.410 +  void save_old_state(JavaThread* java_thread) {
   1.411 +    _java_thread  = java_thread;
   1.412 +    _is_alive = is_alive(java_thread);
   1.413 +    if (is_alive()) {
   1.414 +      _old_state = java_lang_Thread::get_thread_status(_java_thread->threadObj());
   1.415 +    }
   1.416 +  }
   1.417 +
   1.418 + public:
   1.419 +  static void set_thread_status(JavaThread* java_thread,
   1.420 +                                java_lang_Thread::ThreadStatus state) {
   1.421 +    java_lang_Thread::set_thread_status(java_thread->threadObj(), state);
   1.422 +  }
   1.423 +
   1.424 +  void set_thread_status(java_lang_Thread::ThreadStatus state) {
   1.425 +    if (is_alive()) {
   1.426 +      set_thread_status(_java_thread, state);
   1.427 +    }
   1.428 +  }
   1.429 +
   1.430 +  JavaThreadStatusChanger(JavaThread* java_thread,
   1.431 +                          java_lang_Thread::ThreadStatus state) {
   1.432 +    save_old_state(java_thread);
   1.433 +    set_thread_status(state);
   1.434 +  }
   1.435 +
   1.436 +  JavaThreadStatusChanger(JavaThread* java_thread) {
   1.437 +    save_old_state(java_thread);
   1.438 +  }
   1.439 +
   1.440 +  ~JavaThreadStatusChanger() {
   1.441 +    set_thread_status(_old_state);
   1.442 +  }
   1.443 +
   1.444 +  static bool is_alive(JavaThread* java_thread) {
   1.445 +    return java_thread != NULL && java_thread->threadObj() != NULL;
   1.446 +  }
   1.447 +
   1.448 +  bool is_alive() {
   1.449 +    return _is_alive;
   1.450 +  }
   1.451 +};
   1.452 +
   1.453 +// Change status to waiting on an object  (timed or indefinite)
   1.454 +class JavaThreadInObjectWaitState : public JavaThreadStatusChanger {
   1.455 + private:
   1.456 +  ThreadStatistics* _stat;
   1.457 +  bool _active;
   1.458 +
   1.459 + public:
   1.460 +  JavaThreadInObjectWaitState(JavaThread *java_thread, bool timed) :
   1.461 +    JavaThreadStatusChanger(java_thread,
   1.462 +                            timed ? java_lang_Thread::IN_OBJECT_WAIT_TIMED : java_lang_Thread::IN_OBJECT_WAIT) {
   1.463 +    if (is_alive()) {
   1.464 +      _stat = java_thread->get_thread_stat();
   1.465 +      _active = ThreadService::is_thread_monitoring_contention();
   1.466 +      _stat->monitor_wait();
   1.467 +      if (_active) {
   1.468 +        _stat->monitor_wait_begin();
   1.469 +      }
   1.470 +    } else {
   1.471 +      _active = false;
   1.472 +    }
   1.473 +  }
   1.474 +
   1.475 +  ~JavaThreadInObjectWaitState() {
   1.476 +    if (_active) {
   1.477 +      _stat->monitor_wait_end();
   1.478 +    }
   1.479 +  }
   1.480 +};
   1.481 +
   1.482 +// Change status to parked (timed or indefinite)
   1.483 +class JavaThreadParkedState : public JavaThreadStatusChanger {
   1.484 + private:
   1.485 +  ThreadStatistics* _stat;
   1.486 +  bool _active;
   1.487 +
   1.488 + public:
   1.489 +  JavaThreadParkedState(JavaThread *java_thread, bool timed) :
   1.490 +    JavaThreadStatusChanger(java_thread,
   1.491 +                            timed ? java_lang_Thread::PARKED_TIMED : java_lang_Thread::PARKED) {
   1.492 +    if (is_alive()) {
   1.493 +      _stat = java_thread->get_thread_stat();
   1.494 +      _active = ThreadService::is_thread_monitoring_contention();
   1.495 +      _stat->monitor_wait();
   1.496 +      if (_active) {
   1.497 +        _stat->monitor_wait_begin();
   1.498 +      }
   1.499 +    } else {
   1.500 +      _active = false;
   1.501 +    }
   1.502 +  }
   1.503 +
   1.504 +  ~JavaThreadParkedState() {
   1.505 +    if (_active) {
   1.506 +      _stat->monitor_wait_end();
   1.507 +    }
   1.508 +  }
   1.509 +};
   1.510 +
   1.511 +// Change status to blocked on (re-)entering a synchronization block
   1.512 +class JavaThreadBlockedOnMonitorEnterState : public JavaThreadStatusChanger {
   1.513 + private:
   1.514 +  ThreadStatistics* _stat;
   1.515 +  bool _active;
   1.516 +
   1.517 +  static bool contended_enter_begin(JavaThread *java_thread) {
   1.518 +    set_thread_status(java_thread, java_lang_Thread::BLOCKED_ON_MONITOR_ENTER);
   1.519 +    ThreadStatistics* stat = java_thread->get_thread_stat();
   1.520 +    stat->contended_enter();
   1.521 +    bool active = ThreadService::is_thread_monitoring_contention();
   1.522 +    if (active) {
   1.523 +      stat->contended_enter_begin();
   1.524 +    }
   1.525 +    return active;
   1.526 +  }
   1.527 +
   1.528 + public:
   1.529 +  // java_thread is waiting thread being blocked on monitor reenter.
   1.530 +  // Current thread is the notifying thread which holds the monitor.
   1.531 +  static bool wait_reenter_begin(JavaThread *java_thread, ObjectMonitor *obj_m) {
   1.532 +    assert((java_thread != NULL), "Java thread should not be null here");
   1.533 +    bool active  = false;
   1.534 +    if (is_alive(java_thread) && ServiceUtil::visible_oop((oop)obj_m->object())) {
   1.535 +      active = contended_enter_begin(java_thread);
   1.536 +    }
   1.537 +    return active;
   1.538 +  }
   1.539 +
   1.540 +  static void wait_reenter_end(JavaThread *java_thread, bool active) {
   1.541 +    if (active) {
   1.542 +      java_thread->get_thread_stat()->contended_enter_end();
   1.543 +    }
   1.544 +    set_thread_status(java_thread, java_lang_Thread::RUNNABLE);
   1.545 +  }
   1.546 +
   1.547 +  JavaThreadBlockedOnMonitorEnterState(JavaThread *java_thread, ObjectMonitor *obj_m) :
   1.548 +    JavaThreadStatusChanger(java_thread) {
   1.549 +    assert((java_thread != NULL), "Java thread should not be null here");
   1.550 +    // Change thread status and collect contended enter stats for monitor contended
   1.551 +    // enter done for external java world objects and it is contended. All other cases
   1.552 +    // like for vm internal objects and for external objects which are not contended
   1.553 +    // thread status is not changed and contended enter stat is not collected.
   1.554 +    _active = false;
   1.555 +    if (is_alive() && ServiceUtil::visible_oop((oop)obj_m->object()) && obj_m->contentions() > 0) {
   1.556 +      _stat = java_thread->get_thread_stat();
   1.557 +      _active = contended_enter_begin(java_thread);
   1.558 +    }
   1.559 +  }
   1.560 +
   1.561 +  ~JavaThreadBlockedOnMonitorEnterState() {
   1.562 +    if (_active) {
   1.563 +      _stat->contended_enter_end();
   1.564 +    }
   1.565 +  }
   1.566 +};
   1.567 +
   1.568 +// Change status to sleeping
   1.569 +class JavaThreadSleepState : public JavaThreadStatusChanger {
   1.570 + private:
   1.571 +  ThreadStatistics* _stat;
   1.572 +  bool _active;
   1.573 + public:
   1.574 +  JavaThreadSleepState(JavaThread *java_thread) :
   1.575 +    JavaThreadStatusChanger(java_thread, java_lang_Thread::SLEEPING) {
   1.576 +    if (is_alive()) {
   1.577 +      _stat = java_thread->get_thread_stat();
   1.578 +      _active = ThreadService::is_thread_monitoring_contention();
   1.579 +      _stat->thread_sleep();
   1.580 +      if (_active) {
   1.581 +        _stat->thread_sleep_begin();
   1.582 +      }
   1.583 +    } else {
   1.584 +      _active = false;
   1.585 +    }
   1.586 +  }
   1.587 +
   1.588 +  ~JavaThreadSleepState() {
   1.589 +    if (_active) {
   1.590 +      _stat->thread_sleep_end();
   1.591 +    }
   1.592 +  }
   1.593 +};
   1.594 +
   1.595 +#endif // SHARE_VM_SERVICES_THREADSERVICE_HPP

mercurial