-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadInfo.java
More file actions
203 lines (178 loc) · 7.08 KB
/
Copy pathThreadInfo.java
File metadata and controls
203 lines (178 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright 2008-2014 by Emeric Vernat
*
* This file is part of Java Melody.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitblit.sysinfo;
import java.io.Serializable;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* This code was extracted from JavaMelody and refactored.
*
* @author Emeric Vernat
* @author James Moger
*/
public class ThreadInfo implements Serializable, Comparable<ThreadInfo> {
private static final long serialVersionUID = 1L;
private static final ThreadMXBean THREAD_BEAN = ManagementFactory.getThreadMXBean();
private static final boolean CPU_TIME_ENABLED = THREAD_BEAN.isThreadCpuTimeSupported() && THREAD_BEAN.isThreadCpuTimeEnabled();
private final String name;
private final long id;
private final int priority;
private final boolean daemon;
private final Thread.State state;
private final long cpuTimeMillis;
private final long userTimeMillis;
private final boolean deadlocked;
private final String globalThreadId;
private final List<StackTraceElement> stackTrace;
ThreadInfo(Thread thread, List<StackTraceElement> stackTrace, long cpuTimeMillis, long userTimeMillis,
boolean deadlocked, String hostAddress) {
super();
assert thread != null;
assert stackTrace == null || stackTrace instanceof Serializable;
this.name = thread.getName();
this.id = thread.getId();
this.priority = thread.getPriority();
this.daemon = thread.isDaemon();
this.state = thread.getState();
this.stackTrace = stackTrace;
this.cpuTimeMillis = cpuTimeMillis;
this.userTimeMillis = userTimeMillis;
this.deadlocked = deadlocked;
this.globalThreadId = buildGlobalThreadId(thread, hostAddress);
}
public String getName() {
return name;
}
public long getId() {
return id;
}
public int getPriority() {
return priority;
}
public boolean isDaemon() {
return daemon;
}
public Thread.State getState() {
return state;
}
public List<StackTraceElement> getStackTrace() {
if (stackTrace != null) {
return Collections.unmodifiableList(stackTrace);
}
return stackTrace;
}
public String getExecutedMethod() {
List<StackTraceElement> trace = stackTrace;
if (trace != null && !trace.isEmpty()) {
return trace.get(0).toString();
}
return "";
}
public long getCpuTimeMillis() {
return cpuTimeMillis;
}
public long getUserTimeMillis() {
return userTimeMillis;
}
public boolean isDeadlocked() {
return deadlocked;
}
public String getGlobalThreadId() {
return globalThreadId;
}
private static String buildGlobalThreadId(Thread thread, String hostAddress) {
return PID.getPID() + '_' + hostAddress + '_' + thread.getId();
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return getClass().getSimpleName() + "[id=" + getId() + ", name=" + getName() + ", daemon="
+ isDaemon() + ", priority=" + getPriority() + ", deadlocked=" + isDeadlocked()
+ ", state=" + getState() + ']';
}
/**
* {@inheritDoc}
*/
@Override
public int compareTo(ThreadInfo thread2) {
return getName().compareToIgnoreCase(thread2.getName());
}
public static long getCurrentThreadCpuTime() {
return getThreadCpuTime(Thread.currentThread().getId());
}
public static long getThreadCpuTime(long threadId) {
if (CPU_TIME_ENABLED) {
return THREAD_BEAN.getThreadCpuTime(threadId);
}
return 0;
}
public static List<ThreadInfo> buildThreadInfoList() {
Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
List<Thread> threads = new ArrayList<>(stackTraces.keySet());
// si "1.6.0_01".compareTo(Parameters.JAVA_VERSION) > 0;
// on récupèrait les threads sans stack trace en contournant bug 6434648 avant 1.6.0_01
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6434648
// hormis pour le thread courant qui obtient sa stack trace différemment sans le bug
// threads = getThreadsFromThreadGroups();
// final Thread currentThread = Thread.currentThread();
// stackTraces = Collections.singletonMap(currentThread, currentThread.getStackTrace());
boolean cpuTimeEnabled = THREAD_BEAN.isThreadCpuTimeSupported() && THREAD_BEAN.isThreadCpuTimeEnabled();
long[] deadlockedThreads = getDeadlockedThreads(THREAD_BEAN);
List<ThreadInfo> threadInfosList = new ArrayList<>(threads.size());
// hostAddress récupéré ici car il peut y avoir plus de 20000 threads
String hostAddress = Parameters.getHostAddress();
for (Thread thread : threads) {
StackTraceElement[] stackTraceElements = stackTraces.get(thread);
List<StackTraceElement> stackTraceElementList = stackTraceElements == null ? null : Arrays.asList(stackTraceElements);
long cpuTimeMillis;
long userTimeMillis;
if (cpuTimeEnabled) {
cpuTimeMillis = THREAD_BEAN.getThreadCpuTime(thread.getId()) / 1000000;
userTimeMillis = THREAD_BEAN.getThreadUserTime(thread.getId()) / 1000000;
} else {
cpuTimeMillis = -1;
userTimeMillis = -1;
}
boolean deadlocked = deadlockedThreads != null && Arrays.binarySearch(deadlockedThreads, thread.getId()) >= 0;
// stackTraceElementList est une ArrayList et non unmodifiableList pour lisibilité xml
threadInfosList.add(
new ThreadInfo(thread, stackTraceElementList, cpuTimeMillis, userTimeMillis, deadlocked, hostAddress));
}
// on retourne ArrayList et non unmodifiableList pour lisibilité du xml par xstream
return threadInfosList;
}
static long[] getDeadlockedThreads(ThreadMXBean threadBean) {
long[] deadlockedThreads;
if (threadBean.isSynchronizerUsageSupported()) {
deadlockedThreads = threadBean.findDeadlockedThreads();
} else {
deadlockedThreads = threadBean.findMonitorDeadlockedThreads();
}
if (deadlockedThreads != null) {
Arrays.sort(deadlockedThreads);
}
return deadlockedThreads;
}
}