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.shared.*;
  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 EpsilonHeap) {
 129          EpsilonHeap eh = (EpsilonHeap) heap;
 130          printSpace(eh.space());
 131       } else {
 132          throw new RuntimeException("unknown CollectedHeap type : " + heap.getClass());
 133       }
 134 
 135       System.out.println();
 136    }
 137 
 138    // Helper methods
 139 
 140    private void printGCAlgorithm(Map flagMap) {
 141        long l = getFlagValue("UseTLAB", flagMap);
 142        if (l == 1L) {
 143           System.out.println("using thread-local object allocation.");
 144        }
 145 
 146        l = getFlagValue("UseConcMarkSweepGC", flagMap);
 147        if (l == 1L) {
 148           System.out.println("Concurrent Mark-Sweep GC");
 149           return;
 150        }
 151 
 152        l = getFlagValue("UseParallelGC", flagMap);
 153        if (l == 1L) {
 154           System.out.print("Parallel GC ");
 155           l = getFlagValue("ParallelGCThreads", flagMap);
 156           System.out.println("with " + l + " thread(s)");
 157           return;
 158        }
 159 
 160        l = getFlagValue("UseG1GC", flagMap);
 161        if (l == 1L) {
 162            System.out.print("Garbage-First (G1) GC ");
 163            l = getFlagValue("ParallelGCThreads", flagMap);
 164            System.out.println("with " + l + " thread(s)");
 165            return;
 166        }
 167 
 168        l = getFlagValue("UseEpsilonGC", flagMap);
 169        if (l == 1L) {
 170            System.out.println("Epsilon (no-op) GC");
 171            return;
 172        }
 173 
 174        System.out.println("Mark Sweep Compact GC");
 175    }
 176 
 177    private void printPSYoungGen(PSYoungGen youngGen) {
 178       System.out.println("PS Young Generation");
 179       MutableSpace eden = youngGen.edenSpace();
 180       System.out.println("Eden Space:");
 181       printMutableSpace(eden);
 182       MutableSpace from = youngGen.fromSpace();
 183       System.out.println("From Space:");
 184       printMutableSpace(from);
 185       MutableSpace to = youngGen.toSpace();
 186       System.out.println("To Space:");
 187       printMutableSpace(to);
 188    }
 189 
 190    private void printMutableSpace(MutableSpace space) {
 191       printValMB("capacity = ", space.capacity());
 192       printValMB("used     = ", space.used());
 193       long free = space.capacity() - space.used();
 194       printValMB("free     = ", free);
 195       System.out.println(alignment + (double)space.used() * 100.0 / space.capacity() + "% used");
 196    }
 197 
 198    private static String alignment = "   ";
 199 
 200    private void printGen(Generation gen) {
 201       printValMB("capacity = ", gen.capacity());
 202       printValMB("used     = ", gen.used());
 203       printValMB("free     = ", gen.free());
 204       System.out.println(alignment + (double)gen.used() * 100.0 / gen.capacity() + "% used");
 205    }
 206 
 207    private void printSpace(ContiguousSpace space) {
 208       printValMB("capacity = ", space.capacity());
 209       printValMB("used     = ", space.used());
 210       printValMB("free     = ", space.free());
 211       System.out.println(alignment +  (double)space.used() * 100.0 / space.capacity() + "% used");
 212    }
 213 
 214    public void printG1HeapSummary(G1CollectedHeap g1h) {
 215       G1MonitoringSupport g1mm = g1h.g1mm();
 216       long edenRegionNum = g1mm.edenRegionNum();
 217       long survivorRegionNum = g1mm.survivorRegionNum();
 218       HeapRegionSetBase oldSet = g1h.oldSet();
 219       HeapRegionSetBase humongousSet = g1h.humongousSet();
 220       long oldRegionNum = oldSet.length() + humongousSet.length();
 221       printG1Space("G1 Heap:", g1h.n_regions(),
 222                    g1h.used(), g1h.capacity());
 223       System.out.println("G1 Young Generation:");
 224       printG1Space("Eden Space:", edenRegionNum,
 225                    g1mm.edenUsed(), g1mm.edenCommitted());
 226       printG1Space("Survivor Space:", survivorRegionNum,
 227                    g1mm.survivorUsed(), g1mm.survivorCommitted());
 228       printG1Space("G1 Old Generation:", oldRegionNum,
 229                    g1mm.oldUsed(), g1mm.oldCommitted());
 230    }
 231 
 232    private void printG1Space(String spaceName, long regionNum,
 233                              long used, long capacity) {
 234       long free = capacity - used;
 235       System.out.println(spaceName);
 236       printValue("regions  = ", regionNum);
 237       printValMB("capacity = ", capacity);
 238       printValMB("used     = ", used);
 239       printValMB("free     = ", free);
 240       double occPerc = (capacity > 0) ? (double) used * 100.0 / capacity : 0.0;
 241       System.out.println(alignment + occPerc + "% used");
 242    }
 243 
 244    private static final double FACTOR = 1024*1024;
 245    private void printValMB(String title, long value) {
 246       if (value < 0) {
 247         System.out.println(alignment + title +   (value >>> 20)  + " MB");
 248       } else {
 249         double mb = value/FACTOR;
 250         System.out.println(alignment + title + value + " (" + mb + "MB)");
 251       }
 252    }
 253 
 254    private void printValue(String title, long value) {
 255       System.out.println(alignment + title + value);
 256    }
 257 
 258    private long getFlagValue(String name, Map flagMap) {
 259       VM.Flag f = (VM.Flag) flagMap.get(name);
 260       if (f != null) {
 261          if (f.isBool()) {
 262             return f.getBool()? 1L : 0L;
 263          } else {
 264             return Long.parseLong(f.getValue());
 265          }
 266       } else {
 267          return -1;
 268       }
 269    }
 270 }