1 /* 2 * Copyright (c) 2003, 2018, 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.util.*; 28 import sun.jvm.hotspot.gc.epsilon.*; 29 import sun.jvm.hotspot.gc.g1.*; 30 import sun.jvm.hotspot.gc.parallel.*; 31 import sun.jvm.hotspot.gc.serial.*; 32 import sun.jvm.hotspot.gc.shenandoah.*; 33 import sun.jvm.hotspot.gc.shared.*; 34 import sun.jvm.hotspot.gc.z.*; 35 import sun.jvm.hotspot.debugger.JVMDebugger; 36 import sun.jvm.hotspot.memory.*; 37 import sun.jvm.hotspot.oops.*; 38 import sun.jvm.hotspot.runtime.*; 39 40 public class HeapSummary extends Tool { 41 42 public HeapSummary() { 43 super(); 44 } 45 46 public HeapSummary(JVMDebugger d) { 47 super(d); 48 } 49 50 public static void main(String[] args) { 51 HeapSummary hs = new HeapSummary(); 52 hs.execute(args); 53 } 54 55 @Override 56 public String getName() { 57 return "heapSummary"; 58 } 59 60 public void run() { 61 CollectedHeap heap = VM.getVM().getUniverse().heap(); 62 VM.Flag[] flags = VM.getVM().getCommandLineFlags(); 63 Map flagMap = new HashMap(); 64 if (flags == null) { 65 System.out.println("WARNING: command line flags are not available"); 66 } else { 67 for (int f = 0; f < flags.length; f++) { 68 flagMap.put(flags[f].getName(), flags[f]); 69 } 70 } 71 72 System.out.println(); 73 printGCAlgorithm(flagMap); 74 System.out.println(); 75 System.out.println("Heap Configuration:"); 76 printValue("MinHeapFreeRatio = ", getFlagValue("MinHeapFreeRatio", flagMap)); 77 printValue("MaxHeapFreeRatio = ", getFlagValue("MaxHeapFreeRatio", flagMap)); 78 printValMB("MaxHeapSize = ", getFlagValue("MaxHeapSize", flagMap)); 79 printValMB("NewSize = ", getFlagValue("NewSize", flagMap)); 80 printValMB("MaxNewSize = ", getFlagValue("MaxNewSize", flagMap)); 81 printValMB("OldSize = ", getFlagValue("OldSize", flagMap)); 82 printValue("NewRatio = ", getFlagValue("NewRatio", flagMap)); 83 printValue("SurvivorRatio = ", getFlagValue("SurvivorRatio", flagMap)); 84 printValMB("MetaspaceSize = ", getFlagValue("MetaspaceSize", flagMap)); 85 printValMB("CompressedClassSpaceSize = ", getFlagValue("CompressedClassSpaceSize", flagMap)); 86 printValMB("MaxMetaspaceSize = ", getFlagValue("MaxMetaspaceSize", flagMap)); 87 if (heap instanceof ShenandoahHeap) { 88 printValMB("ShenandoahRegionSize = ", ShenandoahHeapRegion.regionSizeBytes()); 89 } else { 90 printValMB("G1HeapRegionSize = ", HeapRegion.grainBytes()); 91 } 92 93 System.out.println(); 94 System.out.println("Heap Usage:"); 95 96 if (heap instanceof GenCollectedHeap) { 97 GenCollectedHeap genHeap = (GenCollectedHeap) heap; 98 for (int n = 0; n < genHeap.nGens(); n++) { 99 Generation gen = genHeap.getGen(n); 100 if (gen instanceof DefNewGeneration) { 101 System.out.println("New Generation (Eden + 1 Survivor Space):"); 102 printGen(gen); 103 104 ContiguousSpace eden = ((DefNewGeneration)gen).eden(); 105 System.out.println("Eden Space:"); 106 printSpace(eden); 107 108 ContiguousSpace from = ((DefNewGeneration)gen).from(); 109 System.out.println("From Space:"); 110 printSpace(from); 111 112 ContiguousSpace to = ((DefNewGeneration)gen).to(); 113 System.out.println("To Space:"); 114 printSpace(to); 115 } else { 116 System.out.println(gen.name() + ":"); 117 printGen(gen); 118 } 119 } 120 } else if (heap instanceof G1CollectedHeap) { 121 printG1HeapSummary((G1CollectedHeap)heap); 122 } else if (heap instanceof ParallelScavengeHeap) { 123 ParallelScavengeHeap psh = (ParallelScavengeHeap) heap; 124 PSYoungGen youngGen = psh.youngGen(); 125 printPSYoungGen(youngGen); 126 127 PSOldGen oldGen = psh.oldGen(); 128 long oldFree = oldGen.capacity() - oldGen.used(); 129 System.out.println("PS Old Generation"); 130 printValMB("capacity = ", oldGen.capacity()); 131 printValMB("used = ", oldGen.used()); 132 printValMB("free = ", oldFree); 133 System.out.println(alignment + (double)oldGen.used() * 100.0 / oldGen.capacity() + "% used"); 134 } else if (heap instanceof ShenandoahHeap) { 135 ShenandoahHeap sh = (ShenandoahHeap) heap; 136 long num_regions = sh.numOfRegions(); 137 System.out.println("Shenandoah Heap:"); 138 System.out.println(" regions = " + num_regions); 139 printValMB("capacity = ", num_regions * ShenandoahHeapRegion.regionSizeBytes()); 140 printValMB("used = ", sh.used()); 141 printValMB("committed = ", sh.committed()); 142 } else if (heap instanceof EpsilonHeap) { 143 EpsilonHeap eh = (EpsilonHeap) heap; 144 printSpace(eh.space()); 145 } else if (heap instanceof ZCollectedHeap) { 146 ZCollectedHeap zheap = (ZCollectedHeap) heap; 147 zheap.printOn(System.out); 148 } else { 149 throw new RuntimeException("unknown CollectedHeap type : " + heap.getClass()); 150 } 151 152 System.out.println(); 153 } 154 155 // Helper methods 156 157 private void printGCAlgorithm(Map flagMap) { 158 long l = getFlagValue("UseTLAB", flagMap); 159 if (l == 1L) { 160 System.out.println("using thread-local object allocation."); 161 } 162 163 l = getFlagValue("UseConcMarkSweepGC", flagMap); 164 if (l == 1L) { 165 System.out.println("Concurrent Mark-Sweep GC"); 166 return; 167 } 168 169 l = getFlagValue("UseParallelGC", flagMap); 170 if (l == 1L) { 171 System.out.print("Parallel GC "); 172 l = getFlagValue("ParallelGCThreads", flagMap); 173 System.out.println("with " + l + " thread(s)"); 174 return; 175 } 176 177 l = getFlagValue("UseG1GC", flagMap); 178 if (l == 1L) { 179 System.out.print("Garbage-First (G1) GC "); 180 l = getFlagValue("ParallelGCThreads", flagMap); 181 System.out.println("with " + l + " thread(s)"); 182 return; 183 } 184 185 l = getFlagValue("UseEpsilonGC", flagMap); 186 if (l == 1L) { 187 System.out.println("Epsilon (no-op) GC"); 188 return; 189 } 190 191 l = getFlagValue("UseZGC", flagMap); 192 if (l == 1L) { 193 System.out.print("ZGC "); 194 l = getFlagValue("ParallelGCThreads", flagMap); 195 System.out.println("with " + l + " thread(s)"); 196 return; 197 } 198 199 l = getFlagValue("UseShenandoahGC", flagMap); 200 if (l == 1L) { 201 System.out.print("Shenandoah GC "); 202 l = getFlagValue("ParallelGCThreads", flagMap); 203 System.out.println("with " + l + " thread(s)"); 204 return; 205 } 206 207 System.out.println("Mark Sweep Compact GC"); 208 } 209 210 private void printPSYoungGen(PSYoungGen youngGen) { 211 System.out.println("PS Young Generation"); 212 MutableSpace eden = youngGen.edenSpace(); 213 System.out.println("Eden Space:"); 214 printMutableSpace(eden); 215 MutableSpace from = youngGen.fromSpace(); 216 System.out.println("From Space:"); 217 printMutableSpace(from); 218 MutableSpace to = youngGen.toSpace(); 219 System.out.println("To Space:"); 220 printMutableSpace(to); 221 } 222 223 private void printMutableSpace(MutableSpace space) { 224 printValMB("capacity = ", space.capacity()); 225 printValMB("used = ", space.used()); 226 long free = space.capacity() - space.used(); 227 printValMB("free = ", free); 228 System.out.println(alignment + (double)space.used() * 100.0 / space.capacity() + "% used"); 229 } 230 231 private static String alignment = " "; 232 233 private void printGen(Generation gen) { 234 printValMB("capacity = ", gen.capacity()); 235 printValMB("used = ", gen.used()); 236 printValMB("free = ", gen.free()); 237 System.out.println(alignment + (double)gen.used() * 100.0 / gen.capacity() + "% used"); 238 } 239 240 private void printSpace(ContiguousSpace space) { 241 printValMB("capacity = ", space.capacity()); 242 printValMB("used = ", space.used()); 243 printValMB("free = ", space.free()); 244 System.out.println(alignment + (double)space.used() * 100.0 / space.capacity() + "% used"); 245 } 246 247 public void printG1HeapSummary(G1CollectedHeap g1h) { 248 G1MonitoringSupport g1mm = g1h.g1mm(); 249 long edenRegionNum = g1mm.edenRegionNum(); 250 long survivorRegionNum = g1mm.survivorRegionNum(); 251 HeapRegionSetBase oldSet = g1h.oldSet(); 252 HeapRegionSetBase humongousSet = g1h.humongousSet(); 253 long oldRegionNum = oldSet.length() + humongousSet.length(); 254 printG1Space("G1 Heap:", g1h.n_regions(), 255 g1h.used(), g1h.capacity()); 256 System.out.println("G1 Young Generation:"); 257 printG1Space("Eden Space:", edenRegionNum, 258 g1mm.edenUsed(), g1mm.edenCommitted()); 259 printG1Space("Survivor Space:", survivorRegionNum, 260 g1mm.survivorUsed(), g1mm.survivorCommitted()); 261 printG1Space("G1 Old Generation:", oldRegionNum, 262 g1mm.oldUsed(), g1mm.oldCommitted()); 263 } 264 265 private void printG1Space(String spaceName, long regionNum, 266 long used, long capacity) { 267 long free = capacity - used; 268 System.out.println(spaceName); 269 printValue("regions = ", regionNum); 270 printValMB("capacity = ", capacity); 271 printValMB("used = ", used); 272 printValMB("free = ", free); 273 double occPerc = (capacity > 0) ? (double) used * 100.0 / capacity : 0.0; 274 System.out.println(alignment + occPerc + "% used"); 275 } 276 277 private static final double FACTOR = 1024*1024; 278 private void printValMB(String title, long value) { 279 if (value < 0) { 280 System.out.println(alignment + title + (value >>> 20) + " MB"); 281 } else { 282 double mb = value/FACTOR; 283 System.out.println(alignment + title + value + " (" + mb + "MB)"); 284 } 285 } 286 287 private void printValue(String title, long value) { 288 System.out.println(alignment + title + value); 289 } 290 291 private long getFlagValue(String name, Map flagMap) { 292 VM.Flag f = (VM.Flag) flagMap.get(name); 293 if (f != null) { 294 if (f.isBool()) { 295 return f.getBool()? 1L : 0L; 296 } else { 297 return Long.parseLong(f.getValue()); 298 } 299 } else { 300 return -1; 301 } 302 } 303 }