aboutsummaryrefslogtreecommitdiff
path: root/agent/src/share/classes/sun/jvm/hotspot/tools/HeapSummary.java
blob: 8762165526953c539b7022edb85ce9d49ae87551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

package sun.jvm.hotspot.tools;

import java.util.*;
import sun.jvm.hotspot.gc_interface.*;
import sun.jvm.hotspot.gc_implementation.g1.*;
import sun.jvm.hotspot.gc_implementation.parallelScavenge.*;
import sun.jvm.hotspot.gc_implementation.shared.*;
import sun.jvm.hotspot.memory.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.*;

public class HeapSummary extends Tool {

   public static void main(String[] args) {
      HeapSummary hs = new HeapSummary();
      hs.start(args);
      hs.stop();
   }

   public void run() {
      CollectedHeap heap = VM.getVM().getUniverse().heap();
      VM.Flag[] flags = VM.getVM().getCommandLineFlags();
      Map flagMap = new HashMap();
      if (flags == null) {
         System.out.println("WARNING: command line flags are not available");
      } else {
         for (int f = 0; f < flags.length; f++) {
            flagMap.put(flags[f].getName(), flags[f]);
         }
      }

      System.out.println();
      printGCAlgorithm(flagMap);
      System.out.println();
      System.out.println("Heap Configuration:");
      printValue("MinHeapFreeRatio   = ", getFlagValue("MinHeapFreeRatio", flagMap));
      printValue("MaxHeapFreeRatio   = ", getFlagValue("MaxHeapFreeRatio", flagMap));
      printValMB("MaxHeapSize        = ", getFlagValue("MaxHeapSize", flagMap));
      printValMB("NewSize            = ", getFlagValue("NewSize", flagMap));
      printValMB("MaxNewSize         = ", getFlagValue("MaxNewSize", flagMap));
      printValMB("OldSize            = ", getFlagValue("OldSize", flagMap));
      printValue("NewRatio           = ", getFlagValue("NewRatio", flagMap));
      printValue("SurvivorRatio      = ", getFlagValue("SurvivorRatio", flagMap));
      printValMB("MetaspaceSize      = ", getFlagValue("MetaspaceSize", flagMap));
      printValMB("ClassMetaspaceSize = ", getFlagValue("ClassMetaspaceSize", flagMap));
      printValMB("MaxMetaspaceSize   = ", getFlagValue("MaxMetaspaceSize", flagMap));
      printValMB("G1HeapRegionSize   = ", HeapRegion.grainBytes());

      System.out.println();
      System.out.println("Heap Usage:");

      if (heap instanceof SharedHeap) {
         SharedHeap sharedHeap = (SharedHeap) heap;
         if (sharedHeap instanceof GenCollectedHeap) {
            GenCollectedHeap genHeap = (GenCollectedHeap) sharedHeap;
            for (int n = 0; n < genHeap.nGens(); n++) {
               Generation gen = genHeap.getGen(n);
               if (gen instanceof sun.jvm.hotspot.memory.DefNewGeneration) {
                  System.out.println("New Generation (Eden + 1 Survivor Space):");
                  printGen(gen);

                  ContiguousSpace eden = ((DefNewGeneration)gen).eden();
                  System.out.println("Eden Space:");
                  printSpace(eden);

                  ContiguousSpace from = ((DefNewGeneration)gen).from();
                  System.out.println("From Space:");
                  printSpace(from);

                  ContiguousSpace to = ((DefNewGeneration)gen).to();
                  System.out.println("To Space:");
                  printSpace(to);
               } else {
                  System.out.println(gen.name() + ":");
                  printGen(gen);
               }
            }
         } else if (sharedHeap instanceof G1CollectedHeap) {
             G1CollectedHeap g1h = (G1CollectedHeap) sharedHeap;
             G1MonitoringSupport g1mm = g1h.g1mm();
             long edenRegionNum = g1mm.edenRegionNum();
             long survivorRegionNum = g1mm.survivorRegionNum();
             HeapRegionSetBase oldSet = g1h.oldSet();
             HeapRegionSetBase humongousSet = g1h.humongousSet();
             long oldRegionNum = oldSet.regionNum() + humongousSet.regionNum();
             printG1Space("G1 Heap:", g1h.n_regions(),
                          g1h.used(), g1h.capacity());
             System.out.println("G1 Young Generation:");
             printG1Space("Eden Space:", edenRegionNum,
                          g1mm.edenUsed(), g1mm.edenCommitted());
             printG1Space("Survivor Space:", survivorRegionNum,
                          g1mm.survivorUsed(), g1mm.survivorCommitted());
             printG1Space("G1 Old Generation:", oldRegionNum,
                          g1mm.oldUsed(), g1mm.oldCommitted());
         } else {
             throw new RuntimeException("unknown SharedHeap type : " + heap.getClass());
         }
      } else if (heap instanceof ParallelScavengeHeap) {
         ParallelScavengeHeap psh = (ParallelScavengeHeap) heap;
         PSYoungGen youngGen = psh.youngGen();
         printPSYoungGen(youngGen);

         PSOldGen oldGen = psh.oldGen();
         long oldFree = oldGen.capacity() - oldGen.used();
         System.out.println("PS Old Generation");
         printValMB("capacity = ", oldGen.capacity());
         printValMB("used     = ", oldGen.used());
         printValMB("free     = ", oldFree);
         System.out.println(alignment + (double)oldGen.used() * 100.0 / oldGen.capacity() + "% used");
      } else {
         throw new RuntimeException("unknown CollectedHeap type : " + heap.getClass());
      }

      System.out.println();
      printInternStringStatistics();
   }

   // Helper methods

   private void printGCAlgorithm(Map flagMap) {
       // print about new generation
       long l = getFlagValue("UseParNewGC", flagMap);
       if (l == 1L) {
          System.out.println("using parallel threads in the new generation.");
       }

       l = getFlagValue("UseTLAB", flagMap);
       if (l == 1L) {
          System.out.println("using thread-local object allocation.");
       }

       l = getFlagValue("UseConcMarkSweepGC", flagMap);
       if (l == 1L) {
          System.out.println("Concurrent Mark-Sweep GC");
          return;
       }

       l = getFlagValue("UseParallelGC", flagMap);
       if (l == 1L) {
          System.out.print("Parallel GC ");
          l = getFlagValue("ParallelGCThreads", flagMap);
          System.out.println("with " + l + " thread(s)");
          return;
       }

       l = getFlagValue("UseG1GC", flagMap);
       if (l == 1L) {
           System.out.print("Garbage-First (G1) GC ");
           l = getFlagValue("ParallelGCThreads", flagMap);
           System.out.println("with " + l + " thread(s)");
           return;
       }

       System.out.println("Mark Sweep Compact GC");
   }

   private void printPSYoungGen(PSYoungGen youngGen) {
      System.out.println("PS Young Generation");
      MutableSpace eden = youngGen.edenSpace();
      System.out.println("Eden Space:");
      printMutableSpace(eden);
      MutableSpace from = youngGen.fromSpace();
      System.out.println("From Space:");
      printMutableSpace(from);
      MutableSpace to = youngGen.toSpace();
      System.out.println("To Space:");
      printMutableSpace(to);
   }

   private void printMutableSpace(MutableSpace space) {
      printValMB("capacity = ", space.capacity());
      printValMB("used     = ", space.used());
      long free = space.capacity() - space.used();
      printValMB("free     = ", free);
      System.out.println(alignment + (double)space.used() * 100.0 / space.capacity() + "% used");
   }

   private static String alignment = "   ";

   private void printGen(Generation gen) {
      printValMB("capacity = ", gen.capacity());
      printValMB("used     = ", gen.used());
      printValMB("free     = ", gen.free());
      System.out.println(alignment + (double)gen.used() * 100.0 / gen.capacity() + "% used");
   }

   private void printSpace(ContiguousSpace space) {
      printValMB("capacity = ", space.capacity());
      printValMB("used     = ", space.used());
      printValMB("free     = ", space.free());
      System.out.println(alignment +  (double)space.used() * 100.0 / space.capacity() + "% used");
   }

   private void printG1Space(String spaceName, long regionNum,
                             long used, long capacity) {
      long free = capacity - used;
      System.out.println(spaceName);
      printValue("regions  = ", regionNum);
      printValMB("capacity = ", capacity);
      printValMB("used     = ", used);
      printValMB("free     = ", free);
      double occPerc = (capacity > 0) ? (double) used * 100.0 / capacity : 0.0;
      System.out.println(alignment + occPerc + "% used");
   }

   private static final double FACTOR = 1024*1024;
   private void printValMB(String title, long value) {
      if (value < 0) {
        System.out.println(alignment + title +   (value >>> 20)  + " MB");
      } else {
        double mb = value/FACTOR;
        System.out.println(alignment + title + value + " (" + mb + "MB)");
      }
   }

   private void printValue(String title, long value) {
      System.out.println(alignment + title + value);
   }

   private long getFlagValue(String name, Map flagMap) {
      VM.Flag f = (VM.Flag) flagMap.get(name);
      if (f != null) {
         if (f.isBool()) {
            return f.getBool()? 1L : 0L;
         } else {
            return Long.parseLong(f.getValue());
         }
      } else {
         return -1;
      }
   }

   private void printInternStringStatistics() {
      class StringStat implements StringTable.StringVisitor {
         private int count;
         private long size;
         private OopField stringValueField;

         StringStat() {
            VM vm = VM.getVM();
            SystemDictionary sysDict = vm.getSystemDictionary();
            InstanceKlass strKlass = sysDict.getStringKlass();
            // String has a field named 'value' of type 'char[]'.
            stringValueField = (OopField) strKlass.findField("value", "[C");
         }

         private long stringSize(Instance instance) {
            // We include String content in size calculation.
            return instance.getObjectSize() +
                   stringValueField.getValue(instance).getObjectSize();
         }

         public void visit(Instance str) {
            count++;
            size += stringSize(str);
         }

         public void print() {
            System.out.println(count +
                  " interned Strings occupying " + size + " bytes.");
         }
      }

      StringStat stat = new StringStat();
      StringTable strTable = VM.getVM().getStringTable();
      strTable.stringsDo(stat);
      stat.print();
   }
}