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.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    @Override
  53    public String getName() {
  54       return "heapSummary";
  55    }
  56 
  57    public void run() {
  58       CollectedHeap heap = VM.getVM().getUniverse().heap();
  59       VM.Flag[] flags = VM.getVM().getCommandLineFlags();
  60       Map flagMap = new HashMap();
  61       if (flags == null) {
  62          System.out.println("WARNING: command line flags are not available");
  63       } else {
  64          for (int f = 0; f < flags.length; f++) {
  65             flagMap.put(flags[f].getName(), flags[f]);
  66          }
  67       }
  68 
  69       System.out.println();
  70       printGCAlgorithm(flagMap);
  71       System.out.println();
  72       System.out.println("Heap Configuration:");
  73       printValue("MinHeapFreeRatio         = ", getFlagValue("MinHeapFreeRatio", flagMap));
  74       printValue("MaxHeapFreeRatio         = ", getFlagValue("MaxHeapFreeRatio", flagMap));
  75       printValMB("MaxHeapSize              = ", getFlagValue("MaxHeapSize", flagMap));
  76       printValMB("NewSize                  = ", getFlagValue("NewSize", flagMap));
  77       printValMB("MaxNewSize               = ", getFlagValue("MaxNewSize", flagMap));
  78       printValMB("OldSize                  = ", getFlagValue("OldSize", flagMap));
  79       printValue("NewRatio                 = ", getFlagValue("NewRatio", flagMap));
  80       printValue("SurvivorRatio            = ", getFlagValue("SurvivorRatio", flagMap));
  81       printValMB("MetaspaceSize            = ", getFlagValue("MetaspaceSize", flagMap));
  82       printValMB("CompressedClassSpaceSize = ", getFlagValue("CompressedClassSpaceSize", flagMap));
  83       printValMB("MaxMetaspaceSize         = ", getFlagValue("MaxMetaspaceSize", flagMap));
  84       printValMB("G1HeapRegionSize         = ", HeapRegion.grainBytes());
  85 
  86       System.out.println();
  87       System.out.println("Heap Usage:");
  88 
  89       if (heap instanceof GenCollectedHeap) {
  90          GenCollectedHeap genHeap = (GenCollectedHeap) heap;
  91          for (int n = 0; n < genHeap.nGens(); n++) {
  92             Generation gen = genHeap.getGen(n);
  93             if (gen instanceof DefNewGeneration) {
  94                System.out.println("New Generation (Eden + 1 Survivor Space):");
  95                printGen(gen);
  96 
  97                ContiguousSpace eden = ((DefNewGeneration)gen).eden();
  98                System.out.println("Eden Space:");
  99                printSpace(eden);
 100 
 101                ContiguousSpace from = ((DefNewGeneration)gen).from();
 102                System.out.println("From Space:");
 103                printSpace(from);
 104 
 105                ContiguousSpace to = ((DefNewGeneration)gen).to();
 106                System.out.println("To Space:");
 107                printSpace(to);
 108             } else {
 109                System.out.println(gen.name() + ":");
 110                printGen(gen);
 111             }
 112          }
 113       } else if (heap instanceof G1CollectedHeap) {
 114           printG1HeapSummary((G1CollectedHeap)heap);
 115       } else if (heap instanceof ParallelScavengeHeap) {
 116          ParallelScavengeHeap psh = (ParallelScavengeHeap) heap;
 117          PSYoungGen youngGen = psh.youngGen();
 118          printPSYoungGen(youngGen);
 119 
 120          PSOldGen oldGen = psh.oldGen();
 121          long oldFree = oldGen.capacity() - oldGen.used();
 122          System.out.println("PS Old Generation");
 123          printValMB("capacity = ", oldGen.capacity());
 124          printValMB("used     = ", oldGen.used());
 125          printValMB("free     = ", oldFree);
 126          System.out.println(alignment + (double)oldGen.used() * 100.0 / oldGen.capacity() + "% used");
 127       } else {
 128          throw new RuntimeException("unknown CollectedHeap type : " + heap.getClass());
 129       }
 130 
 131       System.out.println();
 132    }
 133 
 134    // Helper methods
 135 
 136    private void printGCAlgorithm(Map flagMap) {
 137        long l = getFlagValue("UseTLAB", flagMap);
 138        if (l == 1L) {
 139           System.out.println("using thread-local object allocation.");
 140        }
 141 
 142        l = getFlagValue("UseConcMarkSweepGC", flagMap);
 143        if (l == 1L) {
 144           System.out.println("Concurrent Mark-Sweep GC");
 145           return;
 146        }
 147 
 148        l = getFlagValue("UseParallelGC", flagMap);
 149        if (l == 1L) {
 150           System.out.print("Parallel GC ");
 151           l = getFlagValue("ParallelGCThreads", flagMap);
 152           System.out.println("with " + l + " thread(s)");
 153           return;
 154        }
 155 
 156        l = getFlagValue("UseG1GC", flagMap);
 157        if (l == 1L) {
 158            System.out.print("Garbage-First (G1) GC ");
 159            l = getFlagValue("ParallelGCThreads", flagMap);
 160            System.out.println("with " + l + " thread(s)");
 161            return;
 162        }
 163 
 164        System.out.println("Mark Sweep Compact GC");
 165    }
 166 
 167    private void printPSYoungGen(PSYoungGen youngGen) {
 168       System.out.println("PS Young Generation");
 169       MutableSpace eden = youngGen.edenSpace();
 170       System.out.println("Eden Space:");
 171       printMutableSpace(eden);
 172       MutableSpace from = youngGen.fromSpace();
 173       System.out.println("From Space:");
 174       printMutableSpace(from);
 175       MutableSpace to = youngGen.toSpace();
 176       System.out.println("To Space:");
 177       printMutableSpace(to);
 178    }
 179 
 180    private void printMutableSpace(MutableSpace space) {
 181       printValMB("capacity = ", space.capacity());
 182       printValMB("used     = ", space.used());
 183       long free = space.capacity() - space.used();
 184       printValMB("free     = ", free);
 185       System.out.println(alignment + (double)space.used() * 100.0 / space.capacity() + "% used");
 186    }
 187 
 188    private static String alignment = "   ";
 189 
 190    private void printGen(Generation gen) {
 191       printValMB("capacity = ", gen.capacity());
 192       printValMB("used     = ", gen.used());
 193       printValMB("free     = ", gen.free());
 194       System.out.println(alignment + (double)gen.used() * 100.0 / gen.capacity() + "% used");
 195    }
 196 
 197    private void printSpace(ContiguousSpace space) {
 198       printValMB("capacity = ", space.capacity());
 199       printValMB("used     = ", space.used());
 200       printValMB("free     = ", space.free());
 201       System.out.println(alignment +  (double)space.used() * 100.0 / space.capacity() + "% used");
 202    }
 203 
 204    public void printG1HeapSummary(G1CollectedHeap g1h) {
 205       G1MonitoringSupport g1mm = g1h.g1mm();
 206       long edenRegionNum = g1mm.edenRegionNum();
 207       long survivorRegionNum = g1mm.survivorRegionNum();
 208       HeapRegionSetBase oldSet = g1h.oldSet();
 209       HeapRegionSetBase humongousSet = g1h.humongousSet();
 210       long oldRegionNum = oldSet.length() + humongousSet.length();
 211       printG1Space("G1 Heap:", g1h.n_regions(),
 212                    g1h.used(), g1h.capacity());
 213       System.out.println("G1 Young Generation:");
 214       printG1Space("Eden Space:", edenRegionNum,
 215                    g1mm.edenUsed(), g1mm.edenCommitted());
 216       printG1Space("Survivor Space:", survivorRegionNum,
 217                    g1mm.survivorUsed(), g1mm.survivorCommitted());
 218       printG1Space("G1 Old Generation:", oldRegionNum,
 219                    g1mm.oldUsed(), g1mm.oldCommitted());
 220    }
 221 
 222    private void printG1Space(String spaceName, long regionNum,
 223                              long used, long capacity) {
 224       long free = capacity - used;
 225       System.out.println(spaceName);
 226       printValue("regions  = ", regionNum);
 227       printValMB("capacity = ", capacity);
 228       printValMB("used     = ", used);
 229       printValMB("free     = ", free);
 230       double occPerc = (capacity > 0) ? (double) used * 100.0 / capacity : 0.0;
 231       System.out.println(alignment + occPerc + "% used");
 232    }
 233 
 234    private static final double FACTOR = 1024*1024;
 235    private void printValMB(String title, long value) {
 236       if (value < 0) {
 237         System.out.println(alignment + title +   (value >>> 20)  + " MB");
 238       } else {
 239         double mb = value/FACTOR;
 240         System.out.println(alignment + title + value + " (" + mb + "MB)");
 241       }
 242    }
 243 
 244    private void printValue(String title, long value) {
 245       System.out.println(alignment + title + value);
 246    }
 247 
 248    private long getFlagValue(String name, Map flagMap) {
 249       VM.Flag f = (VM.Flag) flagMap.get(name);
 250       if (f != null) {
 251          if (f.isBool()) {
 252             return f.getBool()? 1L : 0L;
 253          } else {
 254             return Long.parseLong(f.getValue());
 255          }
 256       } else {
 257          return -1;
 258       }
 259    }
 260 }