Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/tools/HeapSummary.java
41161 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.tools;2526import java.io.*;27import java.util.*;28import sun.jvm.hotspot.gc.epsilon.*;29import sun.jvm.hotspot.gc.g1.*;30import sun.jvm.hotspot.gc.parallel.*;31import sun.jvm.hotspot.gc.serial.*;32import sun.jvm.hotspot.gc.shenandoah.*;33import sun.jvm.hotspot.gc.shared.*;34import sun.jvm.hotspot.gc.z.*;35import sun.jvm.hotspot.debugger.JVMDebugger;36import sun.jvm.hotspot.memory.*;37import sun.jvm.hotspot.oops.*;38import sun.jvm.hotspot.runtime.*;3940public class HeapSummary extends Tool {4142public HeapSummary() {43super();44}4546public HeapSummary(JVMDebugger d) {47super(d);48}4950public static void main(String[] args) {51HeapSummary hs = new HeapSummary();52hs.execute(args);53}5455@Override56public String getName() {57return "heapSummary";58}5960public void run() {61CollectedHeap heap = VM.getVM().getUniverse().heap();62VM.Flag[] flags = VM.getVM().getCommandLineFlags();63Map<String, VM.Flag> flagMap = new HashMap<>();64if (flags == null) {65System.out.println("WARNING: command line flags are not available");66} else {67for (int f = 0; f < flags.length; f++) {68flagMap.put(flags[f].getName(), flags[f]);69}70}7172System.out.println();73printGCAlgorithm(flagMap);74System.out.println();75System.out.println("Heap Configuration:");76printValue("MinHeapFreeRatio = ", getFlagValue("MinHeapFreeRatio", flagMap));77printValue("MaxHeapFreeRatio = ", getFlagValue("MaxHeapFreeRatio", flagMap));78printValMB("MaxHeapSize = ", getFlagValue("MaxHeapSize", flagMap));79printValMB("NewSize = ", getFlagValue("NewSize", flagMap));80printValMB("MaxNewSize = ", getFlagValue("MaxNewSize", flagMap));81printValMB("OldSize = ", getFlagValue("OldSize", flagMap));82printValue("NewRatio = ", getFlagValue("NewRatio", flagMap));83printValue("SurvivorRatio = ", getFlagValue("SurvivorRatio", flagMap));84printValMB("MetaspaceSize = ", getFlagValue("MetaspaceSize", flagMap));85printValMB("CompressedClassSpaceSize = ", getFlagValue("CompressedClassSpaceSize", flagMap));86printValMB("MaxMetaspaceSize = ", getFlagValue("MaxMetaspaceSize", flagMap));87if (heap instanceof ShenandoahHeap) {88printValMB("ShenandoahRegionSize = ", ShenandoahHeapRegion.regionSizeBytes());89} else {90printValMB("G1HeapRegionSize = ", HeapRegion.grainBytes());91}9293System.out.println();94System.out.println("Heap Usage:");9596if (heap instanceof GenCollectedHeap) {97GenCollectedHeap genHeap = (GenCollectedHeap) heap;98for (int n = 0; n < genHeap.nGens(); n++) {99Generation gen = genHeap.getGen(n);100if (gen instanceof DefNewGeneration) {101System.out.println("New Generation (Eden + 1 Survivor Space):");102printGen(gen);103104ContiguousSpace eden = ((DefNewGeneration)gen).eden();105System.out.println("Eden Space:");106printSpace(eden);107108ContiguousSpace from = ((DefNewGeneration)gen).from();109System.out.println("From Space:");110printSpace(from);111112ContiguousSpace to = ((DefNewGeneration)gen).to();113System.out.println("To Space:");114printSpace(to);115} else {116System.out.println(gen.name() + ":");117printGen(gen);118}119}120} else if (heap instanceof G1CollectedHeap) {121printG1HeapSummary((G1CollectedHeap)heap);122} else if (heap instanceof ParallelScavengeHeap) {123ParallelScavengeHeap psh = (ParallelScavengeHeap) heap;124PSYoungGen youngGen = psh.youngGen();125printPSYoungGen(youngGen);126127PSOldGen oldGen = psh.oldGen();128long oldFree = oldGen.capacity() - oldGen.used();129System.out.println("PS Old Generation");130printValMB("capacity = ", oldGen.capacity());131printValMB("used = ", oldGen.used());132printValMB("free = ", oldFree);133System.out.println(alignment + (double)oldGen.used() * 100.0 / oldGen.capacity() + "% used");134} else if (heap instanceof ShenandoahHeap) {135ShenandoahHeap sh = (ShenandoahHeap) heap;136long num_regions = sh.numOfRegions();137System.out.println("Shenandoah Heap:");138System.out.println(" regions = " + num_regions);139printValMB("capacity = ", num_regions * ShenandoahHeapRegion.regionSizeBytes());140printValMB("used = ", sh.used());141printValMB("committed = ", sh.committed());142} else if (heap instanceof EpsilonHeap) {143EpsilonHeap eh = (EpsilonHeap) heap;144printSpace(eh.space());145} else if (heap instanceof ZCollectedHeap) {146ZCollectedHeap zheap = (ZCollectedHeap) heap;147zheap.printOn(System.out);148} else {149throw new RuntimeException("unknown CollectedHeap type : " + heap.getClass());150}151152System.out.println();153}154155// Helper methods156157private void printGCAlgorithm(Map flagMap) {158long l = getFlagValue("UseTLAB", flagMap);159if (l == 1L) {160System.out.println("using thread-local object allocation.");161}162163l = getFlagValue("UseParallelGC", flagMap);164if (l == 1L) {165System.out.print("Parallel GC ");166l = getFlagValue("ParallelGCThreads", flagMap);167System.out.println("with " + l + " thread(s)");168return;169}170171l = getFlagValue("UseG1GC", flagMap);172if (l == 1L) {173System.out.print("Garbage-First (G1) GC ");174l = getFlagValue("ParallelGCThreads", flagMap);175System.out.println("with " + l + " thread(s)");176return;177}178179l = getFlagValue("UseEpsilonGC", flagMap);180if (l == 1L) {181System.out.println("Epsilon (no-op) GC");182return;183}184185l = getFlagValue("UseZGC", flagMap);186if (l == 1L) {187System.out.print("ZGC ");188l = getFlagValue("ParallelGCThreads", flagMap);189System.out.println("with " + l + " thread(s)");190return;191}192193l = getFlagValue("UseShenandoahGC", flagMap);194if (l == 1L) {195System.out.print("Shenandoah GC ");196l = getFlagValue("ParallelGCThreads", flagMap);197System.out.println("with " + l + " thread(s)");198return;199}200201System.out.println("Mark Sweep Compact GC");202}203204private void printPSYoungGen(PSYoungGen youngGen) {205System.out.println("PS Young Generation");206MutableSpace eden = youngGen.edenSpace();207System.out.println("Eden Space:");208printMutableSpace(eden);209MutableSpace from = youngGen.fromSpace();210System.out.println("From Space:");211printMutableSpace(from);212MutableSpace to = youngGen.toSpace();213System.out.println("To Space:");214printMutableSpace(to);215}216217private void printMutableSpace(MutableSpace space) {218printValMB("capacity = ", space.capacity());219printValMB("used = ", space.used());220long free = space.capacity() - space.used();221printValMB("free = ", free);222System.out.println(alignment + (double)space.used() * 100.0 / space.capacity() + "% used");223}224225private static String alignment = " ";226227private void printGen(Generation gen) {228printValMB("capacity = ", gen.capacity());229printValMB("used = ", gen.used());230printValMB("free = ", gen.free());231System.out.println(alignment + (double)gen.used() * 100.0 / gen.capacity() + "% used");232}233234private void printSpace(ContiguousSpace space) {235printValMB("capacity = ", space.capacity());236printValMB("used = ", space.used());237printValMB("free = ", space.free());238System.out.println(alignment + (double)space.used() * 100.0 / space.capacity() + "% used");239}240241public void printG1HeapSummary(G1CollectedHeap g1h) {242printG1HeapSummary(System.out, g1h);243}244245public void printG1HeapSummary(PrintStream tty, G1CollectedHeap g1h) {246G1MonitoringSupport g1mm = g1h.g1mm();247long edenSpaceRegionNum = g1mm.edenSpaceRegionNum();248long survivorSpaceRegionNum = g1mm.survivorSpaceRegionNum();249HeapRegionSetBase oldSet = g1h.oldSet();250HeapRegionSetBase archiveSet = g1h.archiveSet();251HeapRegionSetBase humongousSet = g1h.humongousSet();252long oldGenRegionNum = oldSet.length() + archiveSet.length() + humongousSet.length();253printG1Space(tty, "G1 Heap:", g1h.n_regions(),254g1h.used(), g1h.capacity());255tty.println("G1 Young Generation:");256printG1Space(tty, "Eden Space:", edenSpaceRegionNum,257g1mm.edenSpaceUsed(), g1mm.edenSpaceCommitted());258printG1Space(tty, "Survivor Space:", survivorSpaceRegionNum,259g1mm.survivorSpaceUsed(), g1mm.survivorSpaceCommitted());260printG1Space(tty, "G1 Old Generation:", oldGenRegionNum,261g1mm.oldGenUsed(), g1mm.oldGenCommitted());262}263264private void printG1Space(PrintStream tty, String spaceName, long regionNum,265long used, long capacity) {266long free = capacity - used;267tty.println(spaceName);268printValue(tty, "regions = ", regionNum);269printValMB(tty, "capacity = ", capacity);270printValMB(tty, "used = ", used);271printValMB(tty, "free = ", free);272double occPerc = (capacity > 0) ? (double) used * 100.0 / capacity : 0.0;273tty.println(alignment + occPerc + "% used");274}275276private void printValMB(String title, long value) {277printValMB(System.out, title, value);278}279280private static final double FACTOR = 1024*1024;281private void printValMB(PrintStream tty, String title, long value) {282if (value < 0) {283tty.println(alignment + title + (value >>> 20) + " MB");284} else {285double mb = value/FACTOR;286tty.println(alignment + title + value + " (" + mb + "MB)");287}288}289290private void printValue(String title, long value) {291printValue(System.out, title, value);292}293294private void printValue(PrintStream tty, String title, long value) {295tty.println(alignment + title + value);296}297298private long getFlagValue(String name, Map flagMap) {299VM.Flag f = (VM.Flag) flagMap.get(name);300if (f != null) {301if (f.isBool()) {302return f.getBool()? 1L : 0L;303} else if (f.isUIntx() || f.isSizet() || f.isUint64t()) {304return Long.parseUnsignedLong(f.getValue());305} else {306return Long.parseLong(f.getValue());307}308} else {309return -1;310}311}312}313314315