hotspot/agent/src/share/classes/sun/jvm/hotspot/tools/HeapSummary.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 989 04e4dadeec8a
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot.tools;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import sun.jvm.hotspot.gc_interface.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.gc_implementation.parallelScavenge.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import sun.jvm.hotspot.gc_implementation.shared.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
import sun.jvm.hotspot.memory.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
import sun.jvm.hotspot.runtime.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
import sun.jvm.hotspot.tools.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
public class HeapSummary extends Tool {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
   public static void main(String[] args) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
      HeapSummary hs = new HeapSummary();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
      hs.start(args);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
      hs.stop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
   public void run() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
      CollectedHeap heap = VM.getVM().getUniverse().heap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
      VM.Flag[] flags = VM.getVM().getCommandLineFlags();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
      Map flagMap = new HashMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
      if (flags == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
         System.out.println("WARNING: command line flags are not available");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
         for (int f = 0; f < flags.length; f++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
            flagMap.put(flags[f].getName(), flags[f]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
      System.out.println();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
      printGCAlgorithm(flagMap);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
      System.out.println();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
      System.out.println("Heap Configuration:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
      printValue("MinHeapFreeRatio = ", getFlagValue("MinHeapFreeRatio", flagMap));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
      printValue("MaxHeapFreeRatio = ", getFlagValue("MaxHeapFreeRatio", flagMap));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
      printValMB("MaxHeapSize      = ", getFlagValue("MaxHeapSize", flagMap));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
      printValMB("NewSize          = ", getFlagValue("NewSize", flagMap));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
      printValMB("MaxNewSize       = ", getFlagValue("MaxNewSize", flagMap));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
      printValMB("OldSize          = ", getFlagValue("OldSize", flagMap));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
      printValue("NewRatio         = ", getFlagValue("NewRatio", flagMap));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
      printValue("SurvivorRatio    = ", getFlagValue("SurvivorRatio", flagMap));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
      printValMB("PermSize         = ", getFlagValue("PermSize", flagMap));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
      printValMB("MaxPermSize      = ", getFlagValue("MaxPermSize", flagMap));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
      System.out.println();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
      System.out.println("Heap Usage:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
      if (heap instanceof GenCollectedHeap) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
         GenCollectedHeap genHeap = (GenCollectedHeap) heap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
         for (int n = 0; n < genHeap.nGens(); n++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
            Generation gen = genHeap.getGen(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
            if (gen instanceof sun.jvm.hotspot.memory.DefNewGeneration) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
               System.out.println("New Generation (Eden + 1 Survivor Space):");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
               printGen(gen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
               ContiguousSpace eden = ((DefNewGeneration)gen).eden();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
               System.out.println("Eden Space:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
               printSpace(eden);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
               ContiguousSpace from = ((DefNewGeneration)gen).from();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
               System.out.println("From Space:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
               printSpace(from);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
               ContiguousSpace to = ((DefNewGeneration)gen).to();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
               System.out.println("To Space:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
               printSpace(to);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
               System.out.println(gen.name() + ":");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
               printGen(gen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
         // Perm generation
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
         Generation permGen = genHeap.permGen();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
         System.out.println("Perm Generation:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
         printGen(permGen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
      } else if (heap instanceof ParallelScavengeHeap) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
         ParallelScavengeHeap psh = (ParallelScavengeHeap) heap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
         PSYoungGen youngGen = psh.youngGen();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
         printPSYoungGen(youngGen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
         PSOldGen oldGen = psh.oldGen();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
         long oldFree = oldGen.capacity() - oldGen.used();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
         System.out.println("PS Old Generation");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
         printValMB("capacity = ", oldGen.capacity());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
         printValMB("used     = ", oldGen.used());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
         printValMB("free     = ", oldFree);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
         System.out.println(alignment + (double)oldGen.used() * 100.0 / oldGen.capacity() + "% used");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
         PSPermGen permGen = psh.permGen();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
         long permFree = permGen.capacity() - permGen.used();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
         System.out.println("PS Perm Generation");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
         printValMB("capacity = ", permGen.capacity());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
         printValMB("used     = ", permGen.used());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
         printValMB("free     = ", permFree);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
         System.out.println(alignment + (double)permGen.used() * 100.0 / permGen.capacity() + "% used");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
         throw new RuntimeException("unknown heap type : " + heap.getClass());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
   // Helper methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
   private void printGCAlgorithm(Map flagMap) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
       // print about new generation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
       long l = getFlagValue("UseParNewGC", flagMap);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
       if (l == 1L) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
          System.out.println("using parallel threads in the new generation.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
       l = getFlagValue("UseTLAB", flagMap);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
       if (l == 1L) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
          System.out.println("using thread-local object allocation.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
       l = getFlagValue("UseConcMarkSweepGC", flagMap);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
       if (l == 1L) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
          System.out.println("Concurrent Mark-Sweep GC");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
          return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
       l = getFlagValue("UseParallelGC", flagMap);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
       if (l == 1L) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
          System.out.print("Parallel GC ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
          l = getFlagValue("ParallelGCThreads", flagMap);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
          System.out.println("with " + l + " thread(s)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
          return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
       System.out.println("Mark Sweep Compact GC");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
   private void printPSYoungGen(PSYoungGen youngGen) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
      System.out.println("PS Young Generation");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
      MutableSpace eden = youngGen.edenSpace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
      System.out.println("Eden Space:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
      printMutableSpace(eden);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
      MutableSpace from = youngGen.fromSpace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
      System.out.println("From Space:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
      printMutableSpace(from);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
      MutableSpace to = youngGen.toSpace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
      System.out.println("To Space:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
      printMutableSpace(to);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
   private void printMutableSpace(MutableSpace space) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
      printValMB("capacity = ", space.capacity());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
      printValMB("used     = ", space.used());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
      long free = space.capacity() - space.used();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
      printValMB("free     = ", free);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
      System.out.println(alignment + (double)space.used() * 100.0 / space.capacity() + "% used");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
   private static String alignment = "   ";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
   private void printGen(Generation gen) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
      printValMB("capacity = ", gen.capacity());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
      printValMB("used     = ", gen.used());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
      printValMB("free     = ", gen.free());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
      System.out.println(alignment + (double)gen.used() * 100.0 / gen.capacity() + "% used");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
   private void printSpace(ContiguousSpace space) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
      printValMB("capacity = ", space.capacity());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
      printValMB("used     = ", space.used());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
      printValMB("free     = ", space.free());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
      System.out.println(alignment +  (double)space.used() * 100.0 / space.capacity() + "% used");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
   private static final double FACTOR = 1024*1024;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
   private void printValMB(String title, long value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
      double mb = value / FACTOR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
      System.out.println(alignment + title + value + " (" + mb + "MB)");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
   private void printValue(String title, long value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
      System.out.println(alignment + title + value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
   private long getFlagValue(String name, Map flagMap) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
      VM.Flag f = (VM.Flag) flagMap.get(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
      if (f != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
         if (f.isBool()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
            return f.getBool()? 1L : 0L;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
         } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
            return Long.parseLong(f.getValue());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
         return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
}