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