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        System.out.println("Mark Sweep Compact GC");
 200    }
 201 
 202    private void printPSYoungGen(PSYoungGen youngGen) {
 203       System.out.println("PS Young Generation");
 204       MutableSpace eden = youngGen.edenSpace();
 205       System.out.println("Eden Space:");
 206       printMutableSpace(eden);
 207       MutableSpace from = youngGen.fromSpace();
 208       System.out.println("From Space:");
 209       printMutableSpace(from);
 210       MutableSpace to = youngGen.toSpace();
 211       System.out.println("To Space:");
 212       printMutableSpace(to);
 213    }
 214 
 215    private void printMutableSpace(MutableSpace space) {
 216       printValMB("capacity = ", space.capacity());
 217       printValMB("used     = ", space.used());
 218       long free = space.capacity() - space.used();
 219       printValMB("free     = ", free);
 220       System.out.println(alignment + (double)space.used() * 100.0 / space.capacity() + "% used");
 221    }
 222 
 223    private static String alignment = "   ";
 224 
 225    private void printGen(Generation gen) {
 226       printValMB("capacity = ", gen.capacity());
 227       printValMB("used     = ", gen.used());
 228       printValMB("free     = ", gen.free());
 229       System.out.println(alignment + (double)gen.used() * 100.0 / gen.capacity() + "% used");
 230    }
 231 
 232    private void printSpace(ContiguousSpace space) {
 233       printValMB("capacity = ", space.capacity());
 234       printValMB("used     = ", space.used());
 235       printValMB("free     = ", space.free());
 236       System.out.println(alignment +  (double)space.used() * 100.0 / space.capacity() + "% used");
 237    }
 238 
 239    public void printG1HeapSummary(G1CollectedHeap g1h) {
 240       G1MonitoringSupport g1mm = g1h.g1mm();
 241       long edenSpaceRegionNum = g1mm.edenSpaceRegionNum();
 242       long survivorSpaceRegionNum = g1mm.survivorSpaceRegionNum();
 243       HeapRegionSetBase oldSet = g1h.oldSet();
 244       HeapRegionSetBase archiveSet = g1h.archiveSet();
 245       HeapRegionSetBase humongousSet = g1h.humongousSet();
 246       long oldGenRegionNum = oldSet.length() + archiveSet.length() + humongousSet.length();
 247       printG1Space("G1 Heap:", g1h.n_regions(),
 248                    g1h.used(), g1h.capacity());
 249       System.out.println("G1 Young Generation:");
 250       printG1Space("Eden Space:", edenSpaceRegionNum,
 251                    g1mm.edenSpaceUsed(), g1mm.edenSpaceCommitted());
 252       printG1Space("Survivor Space:", survivorSpaceRegionNum,
 253                    g1mm.survivorSpaceUsed(), g1mm.survivorSpaceCommitted());
 254       printG1Space("G1 Old Generation:", oldGenRegionNum,
 255                    g1mm.oldGenUsed(), g1mm.oldGenCommitted());
 256    }
 257 
 258    private void printG1Space(String spaceName, long regionNum,
 259                              long used, long capacity) {
 260       long free = capacity - used;
 261       System.out.println(spaceName);
 262       printValue("regions  = ", regionNum);
 263       printValMB("capacity = ", capacity);
 264       printValMB("used     = ", used);
 265       printValMB("free     = ", free);
 266       double occPerc = (capacity > 0) ? (double) used * 100.0 / capacity : 0.0;
 267       System.out.println(alignment + occPerc + "% used");
 268    }
 269 
 270    private static final double FACTOR = 1024*1024;
 271    private void printValMB(String title, long value) {
 272       if (value < 0) {
 273         System.out.println(alignment + title +   (value >>> 20)  + " MB");
 274       } else {
 275         double mb = value/FACTOR;
 276         System.out.println(alignment + title + value + " (" + mb + "MB)");
 277       }
 278    }
 279 
 280    private void printValue(String title, long value) {
 281       System.out.println(alignment + title + value);
 282    }
 283 
 284    private long getFlagValue(String name, Map flagMap) {
 285       VM.Flag f = (VM.Flag) flagMap.get(name);
 286       if (f != null) {
 287          if (f.isBool()) {
 288             return f.getBool()? 1L : 0L;
 289          } else {
 290             return Long.parseLong(f.getValue());
 291          }
 292       } else {
 293          return -1;
 294       }
 295    }
 296 }