diff agent/src/share/classes/sun/jvm/hotspot/oops/ObjectHistogramElement.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children ba764ed4b6f2
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/src/share/classes/sun/jvm/hotspot/oops/ObjectHistogramElement.java	Sat Dec 01 00:00:00 2007 +0000
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2000-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ */
+
+package sun.jvm.hotspot.oops;
+
+import java.io.*;
+import java.util.*;
+import sun.jvm.hotspot.memory.*;
+import sun.jvm.hotspot.runtime.*;
+
+public class ObjectHistogramElement {
+
+  private Klass klass;
+  private long  count; // Number of instances of klass
+  private long  size;  // Total size of all these instances
+
+  public ObjectHistogramElement(Klass k) {
+     klass = k;
+     count = 0;
+     size  = 0;
+  }
+
+  public void updateWith(Oop obj) {
+    count = count + 1;
+    size  = size  + obj.getObjectSize();
+  }
+
+  public int compare(ObjectHistogramElement other) {
+    return (int) (other.size - size);
+  }
+
+  /** Klass for this ObjectHistogramElement */
+  public Klass getKlass() {
+    return klass;
+  }
+
+  /** Number of instances of klass */
+  public long getCount() {
+    return count;
+  }
+
+  /** Total size of all these instances */
+  public long getSize() {
+    return size;
+  }
+
+  private String getInternalName(Klass k) {
+    ByteArrayOutputStream bos = new ByteArrayOutputStream();
+    getKlass().printValueOn(new PrintStream(bos));
+    // '*' is used to denote VM internal klasses.
+    return "* " + bos.toString();
+  }
+
+  /** Human readable description **/
+  public String getDescription() {
+     Klass k = getKlass();
+     if (k instanceof InstanceKlass) {
+        return k.getName().asString().replace('/', '.');
+     } else if (k instanceof ArrayKlass) {
+       ArrayKlass ak = (ArrayKlass) k;
+       if (k instanceof TypeArrayKlass) {
+          TypeArrayKlass tak = (TypeArrayKlass) ak;
+          return tak.getElementTypeName() + "[]";
+       } else if (k instanceof ObjArrayKlass) {
+          ObjArrayKlass oak = (ObjArrayKlass) ak;
+          // See whether it's a "system objArray"
+          if (oak.equals(VM.getVM().getUniverse().systemObjArrayKlassObj())) {
+            return "* System ObjArray";
+          }
+          Klass bottom = oak.getBottomKlass();
+          int dim = (int) oak.getDimension();
+          StringBuffer buf = new StringBuffer();
+          if (bottom instanceof TypeArrayKlass) {
+            buf.append(((TypeArrayKlass) bottom).getElementTypeName());
+          } else if (bottom instanceof InstanceKlass) {
+            buf.append(bottom.getName().asString().replace('/', '.'));
+          } else {
+            throw new RuntimeException("should not reach here");
+          }
+          for (int i=0; i < dim; i++) {
+            buf.append("[]");
+          }
+          return buf.toString();
+       }
+    }
+    return getInternalName(k);
+  }
+
+  public static void titleOn(PrintStream tty) {
+    tty.println("Object Histogram:");
+    tty.println();
+    tty.println("Size" + "\t" + "Count" + "\t" + "Class description");
+    tty.println("-------------------------------------------------------");
+  }
+
+  public void printOn(PrintStream tty) {
+    tty.print(size + "\t" + count + "\t");
+    tty.print(getDescription());
+    tty.println();
+  }
+}