View Javadoc
1   /*
2    * Copyright (c) 2000, 2012, 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.oops;
26  
27  import java.io.*;
28  import java.util.*;
29  import sun.jvm.hotspot.memory.*;
30  import sun.jvm.hotspot.runtime.*;
31  
32  public class ObjectHistogramElement {
33  
34    private Klass klass;
35    private long  count; // Number of instances of klass
36    private long  size;  // Total size of all these instances
37  
38    public ObjectHistogramElement(Klass k) {
39       klass = k;
40       count = 0;
41       size  = 0;
42    }
43  
44    public void updateWith(Oop obj) {
45      count = count + 1;
46      size  = size  + obj.getObjectSize();
47    }
48  
49    public int compare(ObjectHistogramElement other) {
50      return (int) (other.size - size);
51    }
52  
53    /** Klass for this ObjectHistogramElement */
54    public Klass getKlass() {
55      return klass;
56    }
57  
58    /** Number of instances of klass */
59    public long getCount() {
60      return count;
61    }
62  
63    /** Total size of all these instances */
64    public long getSize() {
65      return size;
66    }
67  
68    private String getInternalName(Klass k) {
69      ByteArrayOutputStream bos = new ByteArrayOutputStream();
70      getKlass().printValueOn(new PrintStream(bos));
71      // '*' is used to denote VM internal klasses.
72      return "* " + bos.toString();
73    }
74  
75    /** Human readable description **/
76    public String getDescription() {
77       Klass k = getKlass();
78       if (k instanceof InstanceKlass) {
79          return k.getName().asString().replace('/', '.');
80       } else if (k instanceof ArrayKlass) {
81         ArrayKlass ak = (ArrayKlass) k;
82         if (k instanceof TypeArrayKlass) {
83            TypeArrayKlass tak = (TypeArrayKlass) ak;
84            return tak.getElementTypeName() + "[]";
85         } else if (k instanceof ObjArrayKlass) {
86            ObjArrayKlass oak = (ObjArrayKlass) ak;
87            Klass bottom = oak.getBottomKlass();
88            int dim = (int) oak.getDimension();
89            StringBuffer buf = new StringBuffer();
90            if (bottom instanceof TypeArrayKlass) {
91              buf.append(((TypeArrayKlass) bottom).getElementTypeName());
92            } else if (bottom instanceof InstanceKlass) {
93              buf.append(bottom.getName().asString().replace('/', '.'));
94            } else {
95              throw new RuntimeException("should not reach here");
96            }
97            for (int i=0; i < dim; i++) {
98              buf.append("[]");
99            }
100           return buf.toString();
101        }
102     }
103     return getInternalName(k);
104   }
105 
106   public static void titleOn(PrintStream tty) {
107     tty.println("Object Histogram:");
108     tty.println();
109     tty.println("num " + "\t" + "  #instances" + "\t" + "#bytes" + "\t" + "Class description");
110     tty.println("--------------------------------------------------------------------------");
111   }
112 
113   public void printOn(PrintStream tty) {
114     tty.print(count + "\t" + size + "\t");
115     tty.print(getDescription());
116     tty.println();
117   }
118 }