利用JMX的MBEAN,输出线程栈、堆栈到本地;
使用时机1(线程池发生reject,打印线程栈)
1 | package com.mpush.tools.thread.pool; |
使用时机2(MPushServer监控,打印线程栈、堆栈)
1 | public class MonitorService extends BaseService implements Monitor, Runnable { |
源码: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/*
* (C) Copyright 2015-2016 the original author or authors.
*
* 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.
*
* Contributors:
* ohun@live.cn (夜色)
*/
package com.mpush.tools.common;
import com.mpush.tools.Utils;
import com.sun.management.HotSpotDiagnosticMXBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.management.*;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class JVMUtil {
private static final String HOT_SPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
private static final Logger LOGGER = LoggerFactory.getLogger(JVMUtil.class);
private static HotSpotDiagnosticMXBean hotSpotMXBean;
private static ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
public static void jstack(OutputStream stream) throws Exception {
PrintStream out = new PrintStream(stream);
boolean cpuTimeEnabled = threadMXBean.isThreadCpuTimeSupported() && threadMXBean.isThreadCpuTimeEnabled();
Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
for (Map.Entry<Thread, StackTraceElement[]> entry : map.entrySet()) {
Thread t = entry.getKey();
StackTraceElement[] elements = entry.getValue();
ThreadInfo tt = threadMXBean.getThreadInfo(t.getId());
long tid = t.getId();
Thread.State state = t.getState();
long cpuTimeMillis = cpuTimeEnabled ? threadMXBean.getThreadCpuTime(tid) / 1000000 : -1;
long userTimeMillis = cpuTimeEnabled ? threadMXBean.getThreadUserTime(tid) / 1000000 : -1;
out.printf("%s id=%d state=%s deamon=%s priority=%s cpu[total=%sms,user=%sms]", t.getName(),
tid, t.getState(), t.isDaemon(), t.getPriority(), cpuTimeMillis, userTimeMillis);
final LockInfo lock = tt.getLockInfo();
if (lock != null && state != Thread.State.BLOCKED) {
out.printf("%n - waiting on <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
out.printf("%n - locked <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
} else if (lock != null && state == Thread.State.BLOCKED) {
out.printf("%n - waiting to lock <0x%08x> (a %s)", lock.getIdentityHashCode(),
lock.getClassName());
}
if (tt.isSuspended()) {
out.print(" (suspended)");
}
if (tt.isInNative()) {
out.print(" (running in native)");
}
out.println();
if (tt.getLockOwnerName() != null) {
out.printf(" owned by %s id=%d%n", tt.getLockOwnerName(), tt.getLockOwnerId());
}
final MonitorInfo[] monitors = tt.getLockedMonitors();
for (int i = 0; i < elements.length; i++) {
final StackTraceElement element = elements[i];
out.printf(" at %s%n", element);
for (int j = 1; j < monitors.length; j++) {
final MonitorInfo monitor = monitors[j];
if (monitor.getLockedStackDepth() == i) {
out.printf(" - locked %s%n", monitor);
}
}
}
out.println();
final LockInfo[] locks = tt.getLockedSynchronizers();
if (locks.length > 0) {
out.printf(" Locked synchronizers: count = %d%n", locks.length);
for (LockInfo l : locks) {
out.printf(" - %s%n", l);
}
out.println();
}
}
}
public static void dumpJstack(final String jvmPath) {
Utils.newThread("dump-jstack-t", (() -> {
File path = new File(jvmPath);
if (path.exists() || path.mkdirs()) {
File file = new File(path, System.currentTimeMillis() + "-jstack.log");
try (FileOutputStream out = new FileOutputStream(file)) {
JVMUtil.jstack(out);
} catch (Throwable t) {
LOGGER.error("Dump JVM cache Error!", t);
}
}
})).start();
}
private static HotSpotDiagnosticMXBean getHotSpotMXBean() {
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<HotSpotDiagnosticMXBean>() {
public HotSpotDiagnosticMXBean run() throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> s = server.queryNames(new ObjectName(HOT_SPOT_BEAN_NAME), null);
Iterator<ObjectName> itr = s.iterator();
if (itr.hasNext()) {
ObjectName name = itr.next();
HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy(server,
name.toString(), HotSpotDiagnosticMXBean.class);
return bean;
} else {
return null;
}
}
});
} catch (Exception e) {
LOGGER.error("getHotSpotMXBean Error!", e);
return null;
}
}
private static void initHotSpotMBean() throws Exception {
if (hotSpotMXBean == null) {
synchronized (JVMUtil.class) {
if (hotSpotMXBean == null) {
hotSpotMXBean = getHotSpotMXBean();
}
}
}
}
public static void jMap(String fileName, boolean live) {
File f = new File(fileName, System.currentTimeMillis() + "-jmap.log");
String currentFileName = f.getPath();
try {
initHotSpotMBean();
if (f.exists()) {
f.delete();
}
hotSpotMXBean.dumpHeap(currentFileName, live);
} catch (Exception e) {
LOGGER.error("dumpHeap Error!" + currentFileName, e);
}
}
public static void dumpJmap(final String jvmPath) {
Utils.newThread("dump-jmap-t", () -> jMap(jvmPath, false)).start();
}
}
功能组件文章目录: