/
MemoryUsageGaugeSet.java
198 lines (167 loc) · 6.32 KB
/
MemoryUsageGaugeSet.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
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
package com.codahale.metrics.jvm;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricSet;
import com.codahale.metrics.RatioGauge;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryUsage;
import java.util.*;
import java.util.regex.Pattern;
import static com.codahale.metrics.MetricRegistry.name;
/**
* A set of gauges for JVM memory usage, including stats on heap vs. non-heap memory, plus
* GC-specific memory pools.
*/
public class MemoryUsageGaugeSet implements MetricSet {
private static final Pattern WHITESPACE = Pattern.compile("[\\s]+");
private final MemoryMXBean mxBean;
private final List<MemoryPoolMXBean> memoryPools;
public MemoryUsageGaugeSet() {
this(ManagementFactory.getMemoryMXBean(),
ManagementFactory.getMemoryPoolMXBeans());
}
public MemoryUsageGaugeSet(MemoryMXBean mxBean,
Collection<MemoryPoolMXBean> memoryPools) {
this.mxBean = mxBean;
this.memoryPools = new ArrayList<MemoryPoolMXBean>(memoryPools);
}
@Override
public Map<String, Metric> getMetrics() {
final Map<String, Metric> gauges = new HashMap<String, Metric>();
gauges.put("total.init", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getInit() +
mxBean.getNonHeapMemoryUsage().getInit();
}
});
gauges.put("total.used", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getUsed() +
mxBean.getNonHeapMemoryUsage().getUsed();
}
});
gauges.put("total.max", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getMax() +
mxBean.getNonHeapMemoryUsage().getMax();
}
});
gauges.put("total.committed", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getCommitted() +
mxBean.getNonHeapMemoryUsage().getCommitted();
}
});
gauges.put("heap.init", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getInit();
}
});
gauges.put("heap.used", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getUsed();
}
});
gauges.put("heap.max", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getMax();
}
});
gauges.put("heap.committed", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getHeapMemoryUsage().getCommitted();
}
});
gauges.put("heap.usage", new RatioGauge() {
@Override
protected Ratio getRatio() {
final MemoryUsage usage = mxBean.getHeapMemoryUsage();
return Ratio.of(usage.getUsed(), usage.getMax());
}
});
gauges.put("non-heap.init", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getInit();
}
});
gauges.put("non-heap.used", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getUsed();
}
});
gauges.put("non-heap.max", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getMax();
}
});
gauges.put("non-heap.committed", new Gauge<Long>() {
@Override
public Long getValue() {
return mxBean.getNonHeapMemoryUsage().getCommitted();
}
});
gauges.put("non-heap.usage", new RatioGauge() {
@Override
protected Ratio getRatio() {
final MemoryUsage usage = mxBean.getNonHeapMemoryUsage();
return Ratio.of(usage.getUsed(), usage.getMax());
}
});
for (final MemoryPoolMXBean pool : memoryPools) {
final String poolName = name("pools", WHITESPACE.matcher(pool.getName()).replaceAll("-"));
gauges.put(name(poolName, "usage"),
new RatioGauge() {
@Override
protected Ratio getRatio() {
MemoryUsage usage = pool.getUsage();
return Ratio.of(usage.getUsed(),
usage.getMax() == -1 ? usage.getCommitted() : usage.getMax());
}
});
gauges.put(name(poolName, "max"),new Gauge<Long>() {
@Override
public Long getValue() {
return pool.getUsage().getMax();
}
});
gauges.put(name(poolName, "used"),new Gauge<Long>() {
@Override
public Long getValue() {
return pool.getUsage().getUsed();
}
});
gauges.put(name(poolName, "committed"),new Gauge<Long>() {
@Override
public Long getValue() {
return pool.getUsage().getCommitted();
}
});
gauges.put(name(poolName, "used-after-gc"),new Gauge<Long>() {
@Override
public Long getValue() {
return pool.getCollectionUsage().getUsed();
}
});
gauges.put(name(poolName, "init"),new Gauge<Long>() {
@Override
public Long getValue() {
return pool.getUsage().getInit();
}
});
}
return Collections.unmodifiableMap(gauges);
}
}