/
HeapUsageHistogram.java
111 lines (97 loc) · 3.89 KB
/
HeapUsageHistogram.java
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
package com.cloudbees.jenkins.support.impl;
import com.cloudbees.jenkins.support.api.Component;
import com.cloudbees.jenkins.support.api.Container;
import com.cloudbees.jenkins.support.api.Content;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.security.Permission;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.management.ManagementFactory;
import java.util.Collections;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Heap histogram from master node.
*/
@Extension
@Restricted(NoExternalUse.class)
public class HeapUsageHistogram extends Component {
// first 200 classes so 203 lines required because of the header
private static final int MAX = 203;
// disabled by default because of JENKINS-49931
// to be reviewed in the future.
private static /*final*/ boolean DISABLED = Boolean.parseBoolean(System.getProperty(HeapUsageHistogram.class.getCanonicalName() + ".DISABLED", "true"));
private static final Logger logger = Logger.getLogger(HeapUsageHistogram.class.getName());
@NonNull
@Override
public Set<Permission> getRequiredPermissions() {
return Collections.singleton(Jenkins.ADMINISTER);
}
@NonNull
@Override
public String getDisplayName() {
return "Master Heap Histogram";
}
@Override
public boolean isSelectedByDefault() {
return false;
}
@Override
public void addContents(@NonNull Container result) {
result.add(
new Content("nodes/master/heap-histogram.txt") {
@Override
public void writeTo(OutputStream os) throws IOException {
os.write(getLiveHistogram().getBytes("UTF-8"));
}
}
);
}
private String getLiveHistogram() throws IOException {
final String raw = getRawLiveHistogram();
final String[] lines = raw.split("\n");
final int limit = MAX <= lines.length ? MAX : lines.length;
final StringBuilder bos = new StringBuilder();
//starting in 1 because of an empty line
for (int i=1; i<limit; i++) {
bos.append(lines[i]).append('\n');
}
return bos.toString();
}
private String getRawLiveHistogram() {
if (DISABLED) {
return new StringBuilder().append('\n')
.append("Histogram generation is disabled. If you want to enable it, do either:")
.append('\n')
.append("* Add the system property: -Dcom.cloudbees.jenkins.support.impl.HeapUsageHistogram.DISABLED=false")
.append('\n')
.append("* Run from Script Console the line: com.cloudbees.jenkins.support.impl.HeapUsageHistogram.DISABLED=false")
.toString();
}
String result;
try {
ObjectName objName = new ObjectName("com.sun.management:type=DiagnosticCommand");
MBeanServer platform = ManagementFactory.getPlatformMBeanServer();
if (platform == null) {
return "N/A";
}
result = (String) platform.invoke(objName, "gcClassHistogram", new Object[] {null}, new String[]{String[].class.getName()});
}
catch (InstanceNotFoundException | ReflectionException | MBeanException | MalformedObjectNameException e) {
logger.log(Level.WARNING,"Could not record heap live histogram.", e);
result = "N/A";
}
return result;
}
}