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