src/hotspot/share/services/threadService.cpp
author stefank
Thu, 09 May 2019 14:28:30 +0200
changeset 54786 ebf733a324d4
parent 54623 1126f0607c70
child 54795 fd08f5a976e6
permissions -rw-r--r--
8223624: Cleanup includes of universe.hpp Reviewed-by: coleenp, lkorinth
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
53463
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
     2
 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4571
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4571
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4571
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "classfile/systemDictionary.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/heapInspection.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "memory/oopFactory.hpp"
37248
11a660dbbb8e 8132524: Missing includes to resourceArea.hpp
jprovino
parents: 35498
diff changeset
    30
#include "memory/resourceArea.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
#include "oops/instanceKlass.hpp"
54786
ebf733a324d4 8223624: Cleanup includes of universe.hpp
stefank
parents: 54623
diff changeset
    32
#include "oops/objArrayKlass.hpp"
35498
392b50de06c6 8146401: Clean up oop.hpp: add inline directives and fix header files
goetz
parents: 33602
diff changeset
    33
#include "oops/objArrayOop.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    34
#include "oops/oop.inline.hpp"
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 37248
diff changeset
    35
#include "runtime/atomic.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    36
#include "runtime/handles.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    37
#include "runtime/init.hpp"
49480
d7df2dd501ce 8199809: Don't include frame.inline.hpp and other.inline.hpp from .hpp files
coleenp
parents: 48955
diff changeset
    38
#include "runtime/objectMonitor.inline.hpp"
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
    39
#include "runtime/thread.inline.hpp"
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
    40
#include "runtime/threadSMR.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    41
#include "runtime/vframe.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    42
#include "runtime/vmThread.hpp"
52877
9e041366c764 8214850: Rename vm_operations.?pp files to vmOperations.?pp files
tschatzl
parents: 52297
diff changeset
    43
#include "runtime/vmOperations.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    44
#include "services/threadService.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
// TODO: we need to define a naming convention for perf counters
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
// to distinguish counters for:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
//   - standard JSR174 use
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
//   - Hotspot extension (public and committed)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
//   - Hotspot extension (private/internal and uncommitted)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
// Default is disabled.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
bool ThreadService::_thread_monitoring_contention_enabled = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
bool ThreadService::_thread_cpu_time_enabled = false;
7724
a92d706dbdd5 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
phh
parents: 7397
diff changeset
    55
bool ThreadService::_thread_allocated_memory_enabled = false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
PerfCounter*  ThreadService::_total_threads_count = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
PerfVariable* ThreadService::_live_threads_count = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
PerfVariable* ThreadService::_peak_threads_count = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
PerfVariable* ThreadService::_daemon_threads_count = NULL;
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
    61
volatile int ThreadService::_atomic_threads_count = 0;
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
    62
volatile int ThreadService::_atomic_daemon_threads_count = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
ThreadDumpResult* ThreadService::_threaddump_list = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
static const int INITIAL_ARRAY_SIZE = 10;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
void ThreadService::init() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  EXCEPTION_MARK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  // These counters are for java.lang.management API support.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  // They are created even if -XX:-UsePerfData is set and in
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // that case, they will be allocated on C heap.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  _total_threads_count =
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
                PerfDataManager::create_counter(JAVA_THREADS, "started",
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
                                                PerfData::U_Events, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  _live_threads_count =
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
                PerfDataManager::create_variable(JAVA_THREADS, "live",
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
                                                 PerfData::U_None, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  _peak_threads_count =
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
                PerfDataManager::create_variable(JAVA_THREADS, "livePeak",
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
                                                 PerfData::U_None, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  _daemon_threads_count =
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
                PerfDataManager::create_variable(JAVA_THREADS, "daemon",
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
                                                 PerfData::U_None, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  if (os::is_thread_cpu_time_supported()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    _thread_cpu_time_enabled = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  }
7724
a92d706dbdd5 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
phh
parents: 7397
diff changeset
    94
a92d706dbdd5 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
phh
parents: 7397
diff changeset
    95
  _thread_allocated_memory_enabled = true; // Always on, so enable it
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
void ThreadService::reset_peak_thread_count() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  // Acquire the lock to update the peak thread count
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  // to synchronize with thread addition and removal.
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 53463
diff changeset
   101
  MutexLocker mu(Threads_lock);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  _peak_threads_count->set_value(get_live_thread_count());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   105
static bool is_hidden_thread(JavaThread *thread) {
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   106
  // hide VM internal or JVMTI agent threads
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   107
  return thread->is_hidden_from_external_view() || thread->is_jvmti_agent_thread();
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   108
}
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   109
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
void ThreadService::add_thread(JavaThread* thread, bool daemon) {
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   111
  assert(Threads_lock->owned_by_self(), "must have threads lock");
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   112
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   113
  // Do not count hidden threads
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   114
  if (is_hidden_thread(thread)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  _total_threads_count->inc();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  _live_threads_count->inc();
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   120
  Atomic::inc(&_atomic_threads_count);
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   121
  int count = _atomic_threads_count;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   123
  if (count > _peak_threads_count->get_value()) {
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   124
    _peak_threads_count->set_value(count);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  if (daemon) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    _daemon_threads_count->inc();
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   129
    Atomic::inc(&_atomic_daemon_threads_count);
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   130
  }
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   131
}
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   132
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   133
void ThreadService::decrement_thread_counts(JavaThread* jt, bool daemon) {
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   134
  Atomic::dec(&_atomic_threads_count);
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   135
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   136
  if (daemon) {
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   137
    Atomic::dec(&_atomic_daemon_threads_count);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
void ThreadService::remove_thread(JavaThread* thread, bool daemon) {
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   142
  assert(Threads_lock->owned_by_self(), "must have threads lock");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   144
  // Do not count hidden threads
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   145
  if (is_hidden_thread(thread)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   149
  assert(!thread->is_terminated(), "must not be terminated");
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   150
  if (!thread->is_exiting()) {
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   151
    // JavaThread::exit() skipped calling current_thread_exiting()
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   152
    decrement_thread_counts(thread, daemon);
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   153
  }
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   154
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   155
  int daemon_count = _atomic_daemon_threads_count;
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   156
  int count = _atomic_threads_count;
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   157
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   158
  // Counts are incremented at the same time, but atomic counts are
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   159
  // decremented earlier than perf counts.
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   160
  assert(_live_threads_count->get_value() > count,
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   161
    "thread count mismatch %d : %d",
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   162
    (int)_live_threads_count->get_value(), count);
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   163
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   164
  _live_threads_count->dec(1);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  if (daemon) {
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   166
    assert(_daemon_threads_count->get_value() > daemon_count,
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   167
      "thread count mismatch %d : %d",
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   168
      (int)_daemon_threads_count->get_value(), daemon_count);
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   169
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   170
    _daemon_threads_count->dec(1);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  }
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   172
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   173
  // Counts are incremented at the same time, but atomic counts are
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   174
  // decremented earlier than perf counts.
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   175
  assert(_daemon_threads_count->get_value() >= daemon_count,
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   176
    "thread count mismatch %d : %d",
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   177
    (int)_daemon_threads_count->get_value(), daemon_count);
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   178
  assert(_live_threads_count->get_value() >= count,
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   179
    "thread count mismatch %d : %d",
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   180
    (int)_live_threads_count->get_value(), count);
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   181
  assert(_live_threads_count->get_value() > 0 ||
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   182
    (_live_threads_count->get_value() == 0 && count == 0 &&
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   183
    _daemon_threads_count->get_value() == 0 && daemon_count == 0),
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   184
    "thread counts should reach 0 at the same time, live %d,%d daemon %d,%d",
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   185
    (int)_live_threads_count->get_value(), count,
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   186
    (int)_daemon_threads_count->get_value(), daemon_count);
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   187
  assert(_daemon_threads_count->get_value() > 0 ||
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   188
    (_daemon_threads_count->get_value() == 0 && daemon_count == 0),
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   189
    "thread counts should reach 0 at the same time, daemon %d,%d",
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   190
    (int)_daemon_threads_count->get_value(), daemon_count);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   193
void ThreadService::current_thread_exiting(JavaThread* jt, bool daemon) {
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   194
  // Do not count hidden threads
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   195
  if (is_hidden_thread(jt)) {
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   196
    return;
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   197
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
52297
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   199
  assert(jt == JavaThread::current(), "Called by current thread");
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   200
  assert(!jt->is_terminated() && jt->is_exiting(), "must be exiting");
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   201
99962c340e73 8021335: Missing synchronization when reading counters for live threads and peak thread count
dlong
parents: 51334
diff changeset
   202
  decrement_thread_counts(jt, daemon);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
// FIXME: JVMTI should call this function
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  assert(thread != NULL, "should be non-NULL");
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   208
  debug_only(Thread::check_for_dangling_thread_pointer(thread);)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  ObjectMonitor *wait_obj = thread->current_waiting_monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  oop obj = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  if (wait_obj != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
    // thread is doing an Object.wait() call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
    obj = (oop) wait_obj->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
    assert(obj != NULL, "Object.wait() should have an object");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    ObjectMonitor *enter_obj = thread->current_pending_monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
    if (enter_obj != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
      // thread is trying to enter() or raw_enter() an ObjectMonitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
      obj = (oop) enter_obj->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
    // If obj == NULL, then ObjectMonitor is raw which doesn't count.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 40655
diff changeset
   226
  Handle h(Thread::current(), obj);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  return h;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
bool ThreadService::set_thread_monitoring_contention(bool flag) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  MutexLocker m(Management_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  bool prev = _thread_monitoring_contention_enabled;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  _thread_monitoring_contention_enabled = flag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  return prev;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
bool ThreadService::set_thread_cpu_time_enabled(bool flag) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  MutexLocker m(Management_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  bool prev = _thread_cpu_time_enabled;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  _thread_cpu_time_enabled = flag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  return prev;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
7724
a92d706dbdd5 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
phh
parents: 7397
diff changeset
   248
bool ThreadService::set_thread_allocated_memory_enabled(bool flag) {
a92d706dbdd5 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
phh
parents: 7397
diff changeset
   249
  MutexLocker m(Management_lock);
a92d706dbdd5 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
phh
parents: 7397
diff changeset
   250
a92d706dbdd5 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
phh
parents: 7397
diff changeset
   251
  bool prev = _thread_allocated_memory_enabled;
a92d706dbdd5 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
phh
parents: 7397
diff changeset
   252
  _thread_allocated_memory_enabled = flag;
a92d706dbdd5 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
phh
parents: 7397
diff changeset
   253
a92d706dbdd5 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
phh
parents: 7397
diff changeset
   254
  return prev;
a92d706dbdd5 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
phh
parents: 7397
diff changeset
   255
}
a92d706dbdd5 7003271: Hotspot should track cumulative Java heap bytes allocated on a per-thread basis
phh
parents: 7397
diff changeset
   256
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
// GC support
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
void ThreadService::oops_do(OopClosure* f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  for (ThreadDumpResult* dump = _threaddump_list; dump != NULL; dump = dump->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    dump->oops_do(f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
21735
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   264
void ThreadService::metadata_do(void f(Metadata*)) {
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   265
  for (ThreadDumpResult* dump = _threaddump_list; dump != NULL; dump = dump->next()) {
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   266
    dump->metadata_do(f);
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   267
  }
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   268
}
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   269
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
void ThreadService::add_thread_dump(ThreadDumpResult* dump) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  MutexLocker ml(Management_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  if (_threaddump_list == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
    _threaddump_list = dump;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
    dump->set_next(_threaddump_list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    _threaddump_list = dump;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
void ThreadService::remove_thread_dump(ThreadDumpResult* dump) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  MutexLocker ml(Management_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  ThreadDumpResult* prev = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  bool found = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  for (ThreadDumpResult* d = _threaddump_list; d != NULL; prev = d, d = d->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
    if (d == dump) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
      if (prev == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
        _threaddump_list = dump->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
        prev->set_next(dump->next());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
      found = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  assert(found, "The threaddump result to be removed must exist.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
// Dump stack trace of threads specified in the given threads array.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
// Returns StackTraceElement[][] each element is the stack trace of a thread in
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
// the corresponding entry in the given threads array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
Handle ThreadService::dump_stack_traces(GrowableArray<instanceHandle>* threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
                                        int num_threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
                                        TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
  assert(num_threads > 0, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  ThreadDumpResult dump_result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  VM_ThreadDump op(&dump_result,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
                   threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
                   num_threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
                   -1,    /* entire stack */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
                   false, /* with locked monitors */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
                   false  /* with locked synchronizers */);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  VMThread::execute(&op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  // Allocate the resulting StackTraceElement[][] object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  ResourceMark rm(THREAD);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   319
  Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_StackTraceElement_array(), true, CHECK_NH);
13952
e3cf184080bc 8000213: NPG: Should have renamed arrayKlass and typeArrayKlass
coleenp
parents: 13728
diff changeset
   320
  ObjArrayKlass* ik = ObjArrayKlass::cast(k);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   321
  objArrayOop r = oopFactory::new_objArray(ik, num_threads, CHECK_NH);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  objArrayHandle result_obj(THREAD, r);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  int num_snapshots = dump_result.num_snapshots();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  assert(num_snapshots == num_threads, "Must have num_threads thread snapshots");
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   326
  assert(num_snapshots == 0 || dump_result.t_list_has_been_set(), "ThreadsList must have been set if we have a snapshot");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  for (ThreadSnapshot* ts = dump_result.snapshots(); ts != NULL; i++, ts = ts->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    ThreadStackTrace* stacktrace = ts->get_stack_trace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    if (stacktrace == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
      // No stack trace
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
      result_obj->obj_at_put(i, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
      // Construct an array of java/lang/StackTraceElement object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
      Handle backtrace_h = stacktrace->allocate_fill_stack_trace_element_array(CHECK_NH);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
      result_obj->obj_at_put(i, backtrace_h());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
  return result_obj;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
void ThreadService::reset_contention_count_stat(JavaThread* thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  ThreadStatistics* stat = thread->get_thread_stat();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  if (stat != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
    stat->reset_count_stat();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
void ThreadService::reset_contention_time_stat(JavaThread* thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  ThreadStatistics* stat = thread->get_thread_stat();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  if (stat != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
    stat->reset_time_stat();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
// Find deadlocks involving object monitors and concurrent locks if concurrent_locks is true
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   358
DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(ThreadsList * t_list, bool concurrent_locks) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   359
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   360
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
  // This code was modified from the original Threads::find_deadlocks code.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  int globalDfn = 0, thisDfn;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
  ObjectMonitor* waitingToLockMonitor = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  oop waitingToLockBlocker = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  bool blocked_on_monitor = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  JavaThread *currentThread, *previousThread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  int num_deadlocks = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   369
  // Initialize the depth-first-number for each JavaThread.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   370
  JavaThreadIterator jti(t_list);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   371
  for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   372
    jt->set_depth_first_number(-1);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
  DeadlockCycle* deadlocks = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  DeadlockCycle* last = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  DeadlockCycle* cycle = new DeadlockCycle();
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   378
  for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    if (jt->depth_first_number() >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
      // this thread was already visited
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
    thisDfn = globalDfn;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
    jt->set_depth_first_number(globalDfn++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
    previousThread = jt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
    currentThread = jt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    cycle->reset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
    // When there is a deadlock, all the monitors involved in the dependency
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
    // cycle must be contended and heavyweight. So we only care about the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
    // heavyweight monitor a thread is waiting to lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    waitingToLockMonitor = (ObjectMonitor*)jt->current_pending_monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
    if (concurrent_locks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
      waitingToLockBlocker = jt->current_park_blocker();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
    while (waitingToLockMonitor != NULL || waitingToLockBlocker != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
      cycle->add_thread(currentThread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
      if (waitingToLockMonitor != NULL) {
18085
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   401
        address currentOwner = (address)waitingToLockMonitor->owner();
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   402
        if (currentOwner != NULL) {
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   403
          currentThread = Threads::owning_thread_from_monitor_owner(t_list,
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   404
                                                                    currentOwner);
18085
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   405
          if (currentThread == NULL) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   406
            // This function is called at a safepoint so the JavaThread
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   407
            // that owns waitingToLockMonitor should be findable, but
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   408
            // if it is not findable, then the previous currentThread is
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   409
            // blocked permanently. We record this as a deadlock.
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   410
            num_deadlocks++;
15853
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 13952
diff changeset
   411
18085
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   412
            cycle->set_deadlock(true);
15853
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 13952
diff changeset
   413
18085
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   414
            // add this cycle to the deadlocks list
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   415
            if (deadlocks == NULL) {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   416
              deadlocks = cycle;
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   417
            } else {
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   418
              last->set_next(cycle);
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   419
            }
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   420
            last = cycle;
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   421
            cycle = new DeadlockCycle();
fb6d02a9e1c5 8016304: ThreadMXBean.getDeadlockedThreads reports bogus deadlocks on JDK 8
sla
parents: 15853
diff changeset
   422
            break;
15853
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 13952
diff changeset
   423
          }
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 13952
diff changeset
   424
        }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
        if (concurrent_locks) {
50419
146c60525d4a 8199882: compiler/uncommontrap/TestDeoptOOM.java failed w/ fatal error: ExceptionMark constructor expects no pending exceptions
dholmes
parents: 50288
diff changeset
   427
          if (waitingToLockBlocker->is_a(SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass())) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
            oop threadObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   429
            // This JavaThread (if there is one) is protected by the
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   430
            // ThreadsListSetter in VM_FindDeadlocks::doit().
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
            currentThread = threadObj != NULL ? java_lang_Thread::thread(threadObj) : NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
          } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
            currentThread = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
      if (currentThread == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
        // No dependency on another thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
      if (currentThread->depth_first_number() < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
        // First visit to this thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
        currentThread->set_depth_first_number(globalDfn++);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
      } else if (currentThread->depth_first_number() < thisDfn) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
        // Thread already visited, and not on a (new) cycle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
      } else if (currentThread == previousThread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
        // Self-loop, ignore
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
        // We have a (new) cycle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
        num_deadlocks++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
        cycle->set_deadlock(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
        // add this cycle to the deadlocks list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
        if (deadlocks == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
          deadlocks = cycle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
          last->set_next(cycle);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
        last = cycle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
        cycle = new DeadlockCycle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
      previousThread = currentThread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
      waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
      if (concurrent_locks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
        waitingToLockBlocker = currentThread->current_park_blocker();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  }
11420
22ec1bad0dce 7125594: C-heap growth issue in ThreadService::find_deadlocks_at_safepoint
fparain
parents: 10739
diff changeset
   475
  delete cycle;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  return deadlocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
51334
cc2c79d22508 8208671: Runtime, JFR, Serviceability changes to allow enabling -Wreorder
tschatzl
parents: 50419
diff changeset
   479
ThreadDumpResult::ThreadDumpResult() : _num_threads(0), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
  // Create a new ThreadDumpResult object and append to the list.
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   482
  // If GC happens before this function returns, Method*
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  // in the stack trace will be visited.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  ThreadService::add_thread_dump(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
51334
cc2c79d22508 8208671: Runtime, JFR, Serviceability changes to allow enabling -Wreorder
tschatzl
parents: 50419
diff changeset
   487
ThreadDumpResult::ThreadDumpResult(int num_threads) : _num_threads(num_threads), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  // Create a new ThreadDumpResult object and append to the list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  // If GC happens before this function returns, oops
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
  // will be visited.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
  ThreadService::add_thread_dump(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
ThreadDumpResult::~ThreadDumpResult() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
  ThreadService::remove_thread_dump(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  // free all the ThreadSnapshot objects created during
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  // the VM_ThreadDump operation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  ThreadSnapshot* ts = _snapshots;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  while (ts != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
    ThreadSnapshot* p = ts;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
    ts = ts->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
    delete p;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
53463
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   507
ThreadSnapshot* ThreadDumpResult::add_thread_snapshot() {
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   508
  ThreadSnapshot* ts = new ThreadSnapshot();
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   509
  link_thread_snapshot(ts);
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   510
  return ts;
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   511
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
53463
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   513
ThreadSnapshot* ThreadDumpResult::add_thread_snapshot(JavaThread* thread) {
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   514
  // Note: it is very important that the ThreadSnapshot* gets linked before
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   515
  // ThreadSnapshot::initialize gets called. This is to ensure that
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   516
  // ThreadSnapshot::oops_do can get called prior to the field
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   517
  // ThreadSnapshot::_threadObj being assigned a value (to prevent a dangling
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   518
  // oop).
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   519
  ThreadSnapshot* ts = new ThreadSnapshot();
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   520
  link_thread_snapshot(ts);
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   521
  ts->initialize(t_list(), thread);
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   522
  return ts;
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   523
}
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   524
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   525
void ThreadDumpResult::link_thread_snapshot(ThreadSnapshot* ts) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
  assert(_num_threads == 0 || _num_snapshots < _num_threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
         "_num_snapshots must be less than _num_threads");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
  _num_snapshots++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
  if (_snapshots == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
    _snapshots = ts;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
    _last->set_next(ts);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
  _last = ts;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
void ThreadDumpResult::oops_do(OopClosure* f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
  for (ThreadSnapshot* ts = _snapshots; ts != NULL; ts = ts->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
    ts->oops_do(f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
21735
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   543
void ThreadDumpResult::metadata_do(void f(Metadata*)) {
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   544
  for (ThreadSnapshot* ts = _snapshots; ts != NULL; ts = ts->next()) {
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   545
    ts->metadata_do(f);
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   546
  }
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   547
}
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   548
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   549
ThreadsList* ThreadDumpResult::t_list() {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   550
  return _setter.list();
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   551
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   552
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
StackFrameInfo::StackFrameInfo(javaVFrame* jvf, bool with_lock_info) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
  _method = jvf->method();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
  _bci = jvf->bci();
21735
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   556
  _class_holder = _method->method_holder()->klass_holder();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
  _locked_monitors = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
  if (with_lock_info) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
    ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
    GrowableArray<MonitorInfo*>* list = jvf->locked_monitors();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
    int length = list->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
    if (length > 0) {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 11420
diff changeset
   563
      _locked_monitors = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(length, true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
      for (int i = 0; i < length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
        MonitorInfo* monitor = list->at(i);
48831
05894c073b7e 8196199: Remove miscellaneous oop comparison operators
coleenp
parents: 48105
diff changeset
   566
        assert(monitor->owner() != NULL, "This monitor must have an owning object");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
        _locked_monitors->append(monitor->owner());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
void StackFrameInfo::oops_do(OopClosure* f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
  if (_locked_monitors != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
    int length = _locked_monitors->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
    for (int i = 0; i < length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
      f->do_oop((oop*) _locked_monitors->adr_at(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
  }
21735
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   580
  f->do_oop(&_class_holder);
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   581
}
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   582
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   583
void StackFrameInfo::metadata_do(void f(Metadata*)) {
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   584
  f(_method);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
void StackFrameInfo::print_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
  java_lang_Throwable::print_stack_element(st, method(), bci());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
  int len = (_locked_monitors != NULL ? _locked_monitors->length() : 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
  for (int i = 0; i < len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
    oop o = _locked_monitors->at(i);
33602
16053580a684 8139163: InstanceKlass::cast passes through NULL
coleenp
parents: 33148
diff changeset
   593
    st->print_cr("\t- locked <" INTPTR_FORMAT "> (a %s)", p2i(o), o->klass()->external_name());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
// Iterate through monitor cache to find JNI locked monitors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
class InflatedMonitorsClosure: public MonitorClosure {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
  ThreadStackTrace* _stack_trace;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
  Thread* _thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
  InflatedMonitorsClosure(Thread* t, ThreadStackTrace* st) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
    _thread = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
    _stack_trace = st;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
  void do_monitor(ObjectMonitor* mid) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
    if (mid->owner() == _thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
      oop object = (oop) mid->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
      if (!_stack_trace->is_owned_monitor_on_stack(object)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
        _stack_trace->add_jni_locked_monitor(object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
ThreadStackTrace::ThreadStackTrace(JavaThread* t, bool with_locked_monitors) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
  _thread = t;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 11420
diff changeset
   620
  _frames = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<StackFrameInfo*>(INITIAL_ARRAY_SIZE, true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
  _depth = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
  _with_locked_monitors = with_locked_monitors;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
  if (_with_locked_monitors) {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 11420
diff changeset
   624
    _jni_locked_monitors = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(INITIAL_ARRAY_SIZE, true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
    _jni_locked_monitors = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
ThreadStackTrace::~ThreadStackTrace() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
  for (int i = 0; i < _frames->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
    delete _frames->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
  delete _frames;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
  if (_jni_locked_monitors != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
    delete _jni_locked_monitors;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
void ThreadStackTrace::dump_stack_at_safepoint(int maxDepth) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
  assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
  if (_thread->has_last_Java_frame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
    RegisterMap reg_map(_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
    vframe* start_vf = _thread->last_java_vframe(&reg_map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
    int count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
    for (vframe* f = start_vf; f; f = f->sender() ) {
47592
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   648
      if (maxDepth >= 0 && count == maxDepth) {
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   649
        // Skip frames if more than maxDepth
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   650
        break;
68d46cb9be45 8185003: JMX: Add a version of ThreadMXBean.dumpAllThreads with a maxDepth argument
uvangapally
parents: 47216
diff changeset
   651
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
      if (f->is_java_frame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
        javaVFrame* jvf = javaVFrame::cast(f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
        add_stack_frame(jvf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
        count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
        // Ignore non-Java frames
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
  if (_with_locked_monitors) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
    // Iterate inflated monitors and find monitors locked by this thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
    // not found in the stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
    InflatedMonitorsClosure imc(_thread, this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
    ObjectSynchronizer::monitors_iterate(&imc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
bool ThreadStackTrace::is_owned_monitor_on_stack(oop object) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
  assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
  bool found = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
  int num_frames = get_stack_depth();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
  for (int depth = 0; depth < num_frames; depth++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
    StackFrameInfo* frame = stack_frame_at(depth);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
    int len = frame->num_locked_monitors();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
    GrowableArray<oop>* locked_monitors = frame->locked_monitors();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
    for (int j = 0; j < len; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
      oop monitor = locked_monitors->at(j);
27471
6e56277909f1 8062370: Various minor code improvements
goetz
parents: 25351
diff changeset
   682
      assert(monitor != NULL, "must be a Java object");
49658
8237a91c1cca 8199781: Don't use naked == for comparing oops
rkennke
parents: 49480
diff changeset
   683
      if (oopDesc::equals(monitor, object)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
        found = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
  return found;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
Handle ThreadStackTrace::allocate_fill_stack_trace_element_array(TRAPS) {
46329
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 46271
diff changeset
   693
  InstanceKlass* ik = SystemDictionary::StackTraceElement_klass();
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 46271
diff changeset
   694
  assert(ik != NULL, "must be loaded in 1.4+");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
  // Allocate an array of java/lang/StackTraceElement object
46329
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 46271
diff changeset
   697
  objArrayOop ste = oopFactory::new_objArray(ik, _depth, CHECK_NH);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
  objArrayHandle backtrace(THREAD, ste);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
  for (int j = 0; j < _depth; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
    StackFrameInfo* frame = _frames->at(j);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
    methodHandle mh(THREAD, frame->method());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
    oop element = java_lang_StackTraceElement::create(mh, frame->bci(), CHECK_NH);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
    backtrace->obj_at_put(j, element);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
  return backtrace;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
void ThreadStackTrace::add_stack_frame(javaVFrame* jvf) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
  StackFrameInfo* frame = new StackFrameInfo(jvf, _with_locked_monitors);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
  _frames->append(frame);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
  _depth++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
void ThreadStackTrace::oops_do(OopClosure* f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
  int length = _frames->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
  for (int i = 0; i < length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
    _frames->at(i)->oops_do(f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
  length = (_jni_locked_monitors != NULL ? _jni_locked_monitors->length() : 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
  for (int j = 0; j < length; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
    f->do_oop((oop*) _jni_locked_monitors->adr_at(j));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
21735
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   726
void ThreadStackTrace::metadata_do(void f(Metadata*)) {
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   727
  int length = _frames->length();
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   728
  for (int i = 0; i < length; i++) {
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   729
    _frames->at(i)->metadata_do(f);
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   730
  }
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   731
}
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   732
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   733
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
ConcurrentLocksDump::~ConcurrentLocksDump() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
  if (_retain_map_on_free) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
  for (ThreadConcurrentLocks* t = _map; t != NULL;)  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
    ThreadConcurrentLocks* tcl = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
    t = t->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
    delete tcl;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
void ConcurrentLocksDump::dump_at_safepoint() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
  // dump all locked concurrent locks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
  assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
50288
3831655869bc 8203885: ConcurrentLocksDump::dump_at_safepoint() should not allocate array in resource area
pliden
parents: 50065
diff changeset
   750
  GrowableArray<oop>* aos_objects = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(INITIAL_ARRAY_SIZE, true /* C_heap */);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
25057
f38210f84f8c 8031819: Remove legacy jdk checks and code
hseigel
parents: 24424
diff changeset
   752
  // Find all instances of AbstractOwnableSynchronizer
50419
146c60525d4a 8199882: compiler/uncommontrap/TestDeoptOOM.java failed w/ fatal error: ExceptionMark constructor expects no pending exceptions
dholmes
parents: 50288
diff changeset
   753
  HeapInspection::find_instances_at_safepoint(SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass(),
146c60525d4a 8199882: compiler/uncommontrap/TestDeoptOOM.java failed w/ fatal error: ExceptionMark constructor expects no pending exceptions
dholmes
parents: 50288
diff changeset
   754
                                              aos_objects);
25057
f38210f84f8c 8031819: Remove legacy jdk checks and code
hseigel
parents: 24424
diff changeset
   755
  // Build a map of thread to its owned AQS locks
f38210f84f8c 8031819: Remove legacy jdk checks and code
hseigel
parents: 24424
diff changeset
   756
  build_map(aos_objects);
50288
3831655869bc 8203885: ConcurrentLocksDump::dump_at_safepoint() should not allocate array in resource area
pliden
parents: 50065
diff changeset
   757
3831655869bc 8203885: ConcurrentLocksDump::dump_at_safepoint() should not allocate array in resource area
pliden
parents: 50065
diff changeset
   758
  delete aos_objects;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
// build a map of JavaThread to all its owned AbstractOwnableSynchronizer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
void ConcurrentLocksDump::build_map(GrowableArray<oop>* aos_objects) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
  int length = aos_objects->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
  for (int i = 0; i < length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
    oop o = aos_objects->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
    oop owner_thread_obj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
    if (owner_thread_obj != NULL) {
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   769
      // See comments in ThreadConcurrentLocks to see how this
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   770
      // JavaThread* is protected.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
      JavaThread* thread = java_lang_Thread::thread(owner_thread_obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
      assert(o->is_instance(), "Must be an instanceOop");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
      add_lock(thread, (instanceOop) o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
void ConcurrentLocksDump::add_lock(JavaThread* thread, instanceOop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
  ThreadConcurrentLocks* tcl = thread_concurrent_locks(thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
  if (tcl != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
    tcl->add_lock(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
  // First owned lock found for this thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
  tcl = new ThreadConcurrentLocks(thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
  tcl->add_lock(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
  if (_map == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
    _map = tcl;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
    _last->set_next(tcl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
  _last = tcl;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
ThreadConcurrentLocks* ConcurrentLocksDump::thread_concurrent_locks(JavaThread* thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
  for (ThreadConcurrentLocks* tcl = _map; tcl != NULL; tcl = tcl->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
    if (tcl->java_thread() == thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
      return tcl;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
void ConcurrentLocksDump::print_locks_on(JavaThread* t, outputStream* st) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
  st->print_cr("   Locked ownable synchronizers:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
  ThreadConcurrentLocks* tcl = thread_concurrent_locks(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
  GrowableArray<instanceOop>* locks = (tcl != NULL ? tcl->owned_locks() : NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
  if (locks == NULL || locks->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
    st->print_cr("\t- None");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
    st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
  for (int i = 0; i < locks->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
    instanceOop obj = locks->at(i);
33602
16053580a684 8139163: InstanceKlass::cast passes through NULL
coleenp
parents: 33148
diff changeset
   817
    st->print_cr("\t- <" INTPTR_FORMAT "> (a %s)", p2i(obj), obj->klass()->external_name());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
  st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
ThreadConcurrentLocks::ThreadConcurrentLocks(JavaThread* thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
  _thread = thread;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 11420
diff changeset
   824
  _owned_locks = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<instanceOop>(INITIAL_ARRAY_SIZE, true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
  _next = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
ThreadConcurrentLocks::~ThreadConcurrentLocks() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
  delete _owned_locks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
void ThreadConcurrentLocks::add_lock(instanceOop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
  _owned_locks->append(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
void ThreadConcurrentLocks::oops_do(OopClosure* f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
  int length = _owned_locks->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
  for (int i = 0; i < length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
    f->do_oop((oop*) _owned_locks->adr_at(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
ThreadStatistics::ThreadStatistics() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
  _contended_enter_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
  _monitor_wait_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
  _sleep_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
  _count_pending_reset = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
  _timer_pending_reset = false;
3575
224791e7ecab 6857194: Add hotspot perf counters to aid class loading performance measurement
mchung
parents: 950
diff changeset
   849
  memset((void*) _perf_recursion_counts, 0, sizeof(_perf_recursion_counts));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
53463
b2d1c3b0bd31 8213231: ThreadSnapshot::_threadObj can become stale
ehelin
parents: 52877
diff changeset
   852
void ThreadSnapshot::initialize(ThreadsList * t_list, JavaThread* thread) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
  _thread = thread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
  _threadObj = thread->threadObj();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
  ThreadStatistics* stat = thread->get_thread_stat();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
  _contended_enter_ticks = stat->contended_enter_ticks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
  _contended_enter_count = stat->contended_enter_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
  _monitor_wait_ticks = stat->monitor_wait_ticks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
  _monitor_wait_count = stat->monitor_wait_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
  _sleep_ticks = stat->sleep_ticks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
  _sleep_count = stat->sleep_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
  _thread_status = java_lang_Thread::get_thread_status(_threadObj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
  _is_ext_suspended = thread->is_being_ext_suspended();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
  _is_in_native = (thread->thread_state() == _thread_in_native);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
  if (_thread_status == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
      _thread_status == java_lang_Thread::IN_OBJECT_WAIT ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
      _thread_status == java_lang_Thread::IN_OBJECT_WAIT_TIMED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
    Handle obj = ThreadService::get_current_contended_monitor(thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
    if (obj() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
      // monitor no longer exists; thread is not blocked
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
      _thread_status = java_lang_Thread::RUNNABLE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
      _blocker_object = obj();
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   878
      JavaThread* owner = ObjectSynchronizer::get_lock_owner(t_list, obj);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
      if ((owner == NULL && _thread_status == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER)
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 8076
diff changeset
   880
          || (owner != NULL && owner->is_attaching_via_jni())) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
        // ownership information of the monitor is not available
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
        // (may no longer be owned or releasing to some other thread)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
        // make this thread in RUNNABLE state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
        // And when the owner thread is in attaching state, the java thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
        // is not completely initialized. For example thread name and id
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
        // and may not be set, so hide the attaching thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
        _thread_status = java_lang_Thread::RUNNABLE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
        _blocker_object = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
      } else if (owner != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
        _blocker_object_owner = owner->threadObj();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
  // Support for JSR-166 locks
950
6112b627bb36 6721093: -XX:AppendRatio=N not supported
kamg
parents: 670
diff changeset
   896
  if (JDK_Version::current().supports_thread_park_blocker() &&
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
        (_thread_status == java_lang_Thread::PARKED ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
         _thread_status == java_lang_Thread::PARKED_TIMED)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
    _blocker_object = thread->current_park_blocker();
50419
146c60525d4a 8199882: compiler/uncommontrap/TestDeoptOOM.java failed w/ fatal error: ExceptionMark constructor expects no pending exceptions
dholmes
parents: 50288
diff changeset
   901
    if (_blocker_object != NULL && _blocker_object->is_a(SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass())) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
      _blocker_object_owner = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(_blocker_object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   905
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
ThreadSnapshot::~ThreadSnapshot() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
  delete _stack_trace;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
  delete _concurrent_locks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
void ThreadSnapshot::dump_stack_at_safepoint(int max_depth, bool with_locked_monitors) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
  _stack_trace = new ThreadStackTrace(_thread, with_locked_monitors);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
  _stack_trace->dump_stack_at_safepoint(max_depth);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
void ThreadSnapshot::oops_do(OopClosure* f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
  f->do_oop(&_threadObj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
  f->do_oop(&_blocker_object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
  f->do_oop(&_blocker_object_owner);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
  if (_stack_trace != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
    _stack_trace->oops_do(f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
  if (_concurrent_locks != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
    _concurrent_locks->oops_do(f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
21735
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   930
void ThreadSnapshot::metadata_do(void f(Metadata*)) {
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   931
  if (_stack_trace != NULL) {
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   932
    _stack_trace->metadata_do(f);
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   933
  }
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   934
}
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   935
379ba90c3dc4 8027630: SIGSEGV in const char*Klass::external_name()
sla
parents: 18085
diff changeset
   936
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
DeadlockCycle::DeadlockCycle() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
  _is_deadlock = false;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 11420
diff changeset
   939
  _threads = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
  _next = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
DeadlockCycle::~DeadlockCycle() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
  delete _threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   947
void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
  st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
  st->print_cr("Found one Java-level deadlock:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
  st->print("=============================");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
  JavaThread* currentThread;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
  ObjectMonitor* waitingToLockMonitor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
  oop waitingToLockBlocker;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
  int len = _threads->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
  for (int i = 0; i < len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
    currentThread = _threads->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
    waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
    waitingToLockBlocker = currentThread->current_park_blocker();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
    st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
    st->print_cr("\"%s\":", currentThread->get_thread_name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
    const char* owner_desc = ",\n  which is held by";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
    if (waitingToLockMonitor != NULL) {
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 31592
diff changeset
   964
      st->print("  waiting to lock monitor " INTPTR_FORMAT, p2i(waitingToLockMonitor));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
      oop obj = (oop)waitingToLockMonitor->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
      if (obj != NULL) {
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 31592
diff changeset
   967
        st->print(" (object " INTPTR_FORMAT ", a %s)", p2i(obj),
33602
16053580a684 8139163: InstanceKlass::cast passes through NULL
coleenp
parents: 33148
diff changeset
   968
                   obj->klass()->external_name());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
        if (!currentThread->current_pending_monitor_is_from_java()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
          owner_desc = "\n  in JNI, which is held by";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
        // No Java object associated - a JVMTI raw monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
        owner_desc = " (JVMTI raw monitor),\n  which is held by";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
      }
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   977
      currentThread = Threads::owning_thread_from_monitor_owner(t_list,
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   978
                                                                (address)waitingToLockMonitor->owner());
15853
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 13952
diff changeset
   979
      if (currentThread == NULL) {
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 13952
diff changeset
   980
        // The deadlock was detected at a safepoint so the JavaThread
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 13952
diff changeset
   981
        // that owns waitingToLockMonitor should be findable, but
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 13952
diff changeset
   982
        // if it is not findable, then the previous currentThread is
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 13952
diff changeset
   983
        // blocked permanently.
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 13952
diff changeset
   984
        st->print("%s UNKNOWN_owner_addr=" PTR_FORMAT, owner_desc,
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 31592
diff changeset
   985
                  p2i(waitingToLockMonitor->owner()));
15853
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 13952
diff changeset
   986
        continue;
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 13952
diff changeset
   987
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
      st->print("  waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)",
33148
68fa8b6c4340 8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents: 31592
diff changeset
   990
                p2i(waitingToLockBlocker),
33602
16053580a684 8139163: InstanceKlass::cast passes through NULL
coleenp
parents: 33148
diff changeset
   991
                waitingToLockBlocker->klass()->external_name());
50419
146c60525d4a 8199882: compiler/uncommontrap/TestDeoptOOM.java failed w/ fatal error: ExceptionMark constructor expects no pending exceptions
dholmes
parents: 50288
diff changeset
   992
      assert(waitingToLockBlocker->is_a(SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass()),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
             "Must be an AbstractOwnableSynchronizer");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   994
      oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   995
      currentThread = java_lang_Thread::thread(ownerObj);
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
   996
      assert(currentThread != NULL, "AbstractOwnableSynchronizer owning thread is unexpectedly NULL");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
    st->print("%s \"%s\"", owner_desc, currentThread->get_thread_name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
  st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
  st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
  // Print stack traces
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1005
  bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1006
  JavaMonitorsInStackTrace = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1007
  st->print_cr("Java stack information for the threads listed above:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1008
  st->print_cr("===================================================");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
  for (int j = 0; j < len; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
    currentThread = _threads->at(j);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1011
    st->print_cr("\"%s\":", currentThread->get_thread_name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1012
    currentThread->print_stack_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
  JavaMonitorsInStackTrace = oldJavaMonitorsInStackTrace;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1015
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1016
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1017
ThreadsListEnumerator::ThreadsListEnumerator(Thread* cur_thread,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1018
                                             bool include_jvmti_agent_threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1019
                                             bool include_jni_attaching_threads) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1020
  assert(cur_thread == Thread::current(), "Check current thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1021
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1022
  int init_size = ThreadService::get_live_thread_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1023
  _threads_array = new GrowableArray<instanceHandle>(init_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1024
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47592
diff changeset
  1025
  for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
    // skips JavaThreads in the process of exiting
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1027
    // and also skips VM internal JavaThreads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1028
    // Threads in _thread_new or _thread_new_trans state are included.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1029
    // i.e. threads have been started but not yet running.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
    if (jt->threadObj() == NULL   ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
        jt->is_exiting() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1032
        !java_lang_Thread::is_alive(jt->threadObj())   ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1033
        jt->is_hidden_from_external_view()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1034
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1035
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1036
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1037
    // skip agent threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1038
    if (!include_jvmti_agent_threads && jt->is_jvmti_agent_thread()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1039
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1040
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1041
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1042
    // skip jni threads in the process of attaching
10739
91935236600e 7098194: integrate macosx-port changes
dcubed
parents: 8076
diff changeset
  1043
    if (!include_jni_attaching_threads && jt->is_attaching_via_jni()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1044
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1045
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1046
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1047
    instanceHandle h(cur_thread, (instanceOop) jt->threadObj());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1048
    _threads_array->append(h);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1049
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1050
}