Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/HeapSummary.java
41161 views
1
/*
2
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
package sun.jvm.hotspot.tools;
26
27
import java.io.*;
28
import java.util.*;
29
import sun.jvm.hotspot.gc.epsilon.*;
30
import sun.jvm.hotspot.gc.g1.*;
31
import sun.jvm.hotspot.gc.parallel.*;
32
import sun.jvm.hotspot.gc.serial.*;
33
import sun.jvm.hotspot.gc.shenandoah.*;
34
import sun.jvm.hotspot.gc.shared.*;
35
import sun.jvm.hotspot.gc.z.*;
36
import sun.jvm.hotspot.debugger.JVMDebugger;
37
import sun.jvm.hotspot.memory.*;
38
import sun.jvm.hotspot.oops.*;
39
import sun.jvm.hotspot.runtime.*;
40
41
public class HeapSummary extends Tool {
42
43
public HeapSummary() {
44
super();
45
}
46
47
public HeapSummary(JVMDebugger d) {
48
super(d);
49
}
50
51
public static void main(String[] args) {
52
HeapSummary hs = new HeapSummary();
53
hs.execute(args);
54
}
55
56
@Override
57
public String getName() {
58
return "heapSummary";
59
}
60
61
public void run() {
62
CollectedHeap heap = VM.getVM().getUniverse().heap();
63
VM.Flag[] flags = VM.getVM().getCommandLineFlags();
64
Map<String, VM.Flag> flagMap = new HashMap<>();
65
if (flags == null) {
66
System.out.println("WARNING: command line flags are not available");
67
} else {
68
for (int f = 0; f < flags.length; f++) {
69
flagMap.put(flags[f].getName(), flags[f]);
70
}
71
}
72
73
System.out.println();
74
printGCAlgorithm(flagMap);
75
System.out.println();
76
System.out.println("Heap Configuration:");
77
printValue("MinHeapFreeRatio = ", getFlagValue("MinHeapFreeRatio", flagMap));
78
printValue("MaxHeapFreeRatio = ", getFlagValue("MaxHeapFreeRatio", flagMap));
79
printValMB("MaxHeapSize = ", getFlagValue("MaxHeapSize", flagMap));
80
printValMB("NewSize = ", getFlagValue("NewSize", flagMap));
81
printValMB("MaxNewSize = ", getFlagValue("MaxNewSize", flagMap));
82
printValMB("OldSize = ", getFlagValue("OldSize", flagMap));
83
printValue("NewRatio = ", getFlagValue("NewRatio", flagMap));
84
printValue("SurvivorRatio = ", getFlagValue("SurvivorRatio", flagMap));
85
printValMB("MetaspaceSize = ", getFlagValue("MetaspaceSize", flagMap));
86
printValMB("CompressedClassSpaceSize = ", getFlagValue("CompressedClassSpaceSize", flagMap));
87
printValMB("MaxMetaspaceSize = ", getFlagValue("MaxMetaspaceSize", flagMap));
88
if (heap instanceof ShenandoahHeap) {
89
printValMB("ShenandoahRegionSize = ", ShenandoahHeapRegion.regionSizeBytes());
90
} else {
91
printValMB("G1HeapRegionSize = ", HeapRegion.grainBytes());
92
}
93
94
System.out.println();
95
System.out.println("Heap Usage:");
96
97
if (heap instanceof GenCollectedHeap) {
98
GenCollectedHeap genHeap = (GenCollectedHeap) heap;
99
for (int n = 0; n < genHeap.nGens(); n++) {
100
Generation gen = genHeap.getGen(n);
101
if (gen instanceof DefNewGeneration) {
102
System.out.println("New Generation (Eden + 1 Survivor Space):");
103
printGen(gen);
104
105
ContiguousSpace eden = ((DefNewGeneration)gen).eden();
106
System.out.println("Eden Space:");
107
printSpace(eden);
108
109
ContiguousSpace from = ((DefNewGeneration)gen).from();
110
System.out.println("From Space:");
111
printSpace(from);
112
113
ContiguousSpace to = ((DefNewGeneration)gen).to();
114
System.out.println("To Space:");
115
printSpace(to);
116
} else {
117
System.out.println(gen.name() + ":");
118
printGen(gen);
119
}
120
}
121
} else if (heap instanceof G1CollectedHeap) {
122
printG1HeapSummary((G1CollectedHeap)heap);
123
} else if (heap instanceof ParallelScavengeHeap) {
124
ParallelScavengeHeap psh = (ParallelScavengeHeap) heap;
125
PSYoungGen youngGen = psh.youngGen();
126
printPSYoungGen(youngGen);
127
128
PSOldGen oldGen = psh.oldGen();
129
long oldFree = oldGen.capacity() - oldGen.used();
130
System.out.println("PS Old Generation");
131
printValMB("capacity = ", oldGen.capacity());
132
printValMB("used = ", oldGen.used());
133
printValMB("free = ", oldFree);
134
System.out.println(alignment + (double)oldGen.used() * 100.0 / oldGen.capacity() + "% used");
135
} else if (heap instanceof ShenandoahHeap) {
136
ShenandoahHeap sh = (ShenandoahHeap) heap;
137
long num_regions = sh.numOfRegions();
138
System.out.println("Shenandoah Heap:");
139
System.out.println(" regions = " + num_regions);
140
printValMB("capacity = ", num_regions * ShenandoahHeapRegion.regionSizeBytes());
141
printValMB("used = ", sh.used());
142
printValMB("committed = ", sh.committed());
143
} else if (heap instanceof EpsilonHeap) {
144
EpsilonHeap eh = (EpsilonHeap) heap;
145
printSpace(eh.space());
146
} else if (heap instanceof ZCollectedHeap) {
147
ZCollectedHeap zheap = (ZCollectedHeap) heap;
148
zheap.printOn(System.out);
149
} else {
150
throw new RuntimeException("unknown CollectedHeap type : " + heap.getClass());
151
}
152
153
System.out.println();
154
}
155
156
// Helper methods
157
158
private void printGCAlgorithm(Map flagMap) {
159
long l = getFlagValue("UseTLAB", flagMap);
160
if (l == 1L) {
161
System.out.println("using thread-local object allocation.");
162
}
163
164
l = getFlagValue("UseParallelGC", flagMap);
165
if (l == 1L) {
166
System.out.print("Parallel GC ");
167
l = getFlagValue("ParallelGCThreads", flagMap);
168
System.out.println("with " + l + " thread(s)");
169
return;
170
}
171
172
l = getFlagValue("UseG1GC", flagMap);
173
if (l == 1L) {
174
System.out.print("Garbage-First (G1) GC ");
175
l = getFlagValue("ParallelGCThreads", flagMap);
176
System.out.println("with " + l + " thread(s)");
177
return;
178
}
179
180
l = getFlagValue("UseEpsilonGC", flagMap);
181
if (l == 1L) {
182
System.out.println("Epsilon (no-op) GC");
183
return;
184
}
185
186
l = getFlagValue("UseZGC", flagMap);
187
if (l == 1L) {
188
System.out.print("ZGC ");
189
l = getFlagValue("ParallelGCThreads", flagMap);
190
System.out.println("with " + l + " thread(s)");
191
return;
192
}
193
194
l = getFlagValue("UseShenandoahGC", flagMap);
195
if (l == 1L) {
196
System.out.print("Shenandoah GC ");
197
l = getFlagValue("ParallelGCThreads", flagMap);
198
System.out.println("with " + l + " thread(s)");
199
return;
200
}
201
202
System.out.println("Mark Sweep Compact GC");
203
}
204
205
private void printPSYoungGen(PSYoungGen youngGen) {
206
System.out.println("PS Young Generation");
207
MutableSpace eden = youngGen.edenSpace();
208
System.out.println("Eden Space:");
209
printMutableSpace(eden);
210
MutableSpace from = youngGen.fromSpace();
211
System.out.println("From Space:");
212
printMutableSpace(from);
213
MutableSpace to = youngGen.toSpace();
214
System.out.println("To Space:");
215
printMutableSpace(to);
216
}
217
218
private void printMutableSpace(MutableSpace space) {
219
printValMB("capacity = ", space.capacity());
220
printValMB("used = ", space.used());
221
long free = space.capacity() - space.used();
222
printValMB("free = ", free);
223
System.out.println(alignment + (double)space.used() * 100.0 / space.capacity() + "% used");
224
}
225
226
private static String alignment = " ";
227
228
private void printGen(Generation gen) {
229
printValMB("capacity = ", gen.capacity());
230
printValMB("used = ", gen.used());
231
printValMB("free = ", gen.free());
232
System.out.println(alignment + (double)gen.used() * 100.0 / gen.capacity() + "% used");
233
}
234
235
private void printSpace(ContiguousSpace space) {
236
printValMB("capacity = ", space.capacity());
237
printValMB("used = ", space.used());
238
printValMB("free = ", space.free());
239
System.out.println(alignment + (double)space.used() * 100.0 / space.capacity() + "% used");
240
}
241
242
public void printG1HeapSummary(G1CollectedHeap g1h) {
243
printG1HeapSummary(System.out, g1h);
244
}
245
246
public void printG1HeapSummary(PrintStream tty, G1CollectedHeap g1h) {
247
G1MonitoringSupport g1mm = g1h.g1mm();
248
long edenSpaceRegionNum = g1mm.edenSpaceRegionNum();
249
long survivorSpaceRegionNum = g1mm.survivorSpaceRegionNum();
250
HeapRegionSetBase oldSet = g1h.oldSet();
251
HeapRegionSetBase archiveSet = g1h.archiveSet();
252
HeapRegionSetBase humongousSet = g1h.humongousSet();
253
long oldGenRegionNum = oldSet.length() + archiveSet.length() + humongousSet.length();
254
printG1Space(tty, "G1 Heap:", g1h.n_regions(),
255
g1h.used(), g1h.capacity());
256
tty.println("G1 Young Generation:");
257
printG1Space(tty, "Eden Space:", edenSpaceRegionNum,
258
g1mm.edenSpaceUsed(), g1mm.edenSpaceCommitted());
259
printG1Space(tty, "Survivor Space:", survivorSpaceRegionNum,
260
g1mm.survivorSpaceUsed(), g1mm.survivorSpaceCommitted());
261
printG1Space(tty, "G1 Old Generation:", oldGenRegionNum,
262
g1mm.oldGenUsed(), g1mm.oldGenCommitted());
263
}
264
265
private void printG1Space(PrintStream tty, String spaceName, long regionNum,
266
long used, long capacity) {
267
long free = capacity - used;
268
tty.println(spaceName);
269
printValue(tty, "regions = ", regionNum);
270
printValMB(tty, "capacity = ", capacity);
271
printValMB(tty, "used = ", used);
272
printValMB(tty, "free = ", free);
273
double occPerc = (capacity > 0) ? (double) used * 100.0 / capacity : 0.0;
274
tty.println(alignment + occPerc + "% used");
275
}
276
277
private void printValMB(String title, long value) {
278
printValMB(System.out, title, value);
279
}
280
281
private static final double FACTOR = 1024*1024;
282
private void printValMB(PrintStream tty, String title, long value) {
283
if (value < 0) {
284
tty.println(alignment + title + (value >>> 20) + " MB");
285
} else {
286
double mb = value/FACTOR;
287
tty.println(alignment + title + value + " (" + mb + "MB)");
288
}
289
}
290
291
private void printValue(String title, long value) {
292
printValue(System.out, title, value);
293
}
294
295
private void printValue(PrintStream tty, String title, long value) {
296
tty.println(alignment + title + value);
297
}
298
299
private long getFlagValue(String name, Map flagMap) {
300
VM.Flag f = (VM.Flag) flagMap.get(name);
301
if (f != null) {
302
if (f.isBool()) {
303
return f.getBool()? 1L : 0L;
304
} else if (f.isUIntx() || f.isSizet() || f.isUint64t()) {
305
return Long.parseUnsignedLong(f.getValue());
306
} else {
307
return Long.parseLong(f.getValue());
308
}
309
} else {
310
return -1;
311
}
312
}
313
}
314
315