1 /*
   2  * Copyright (c) 2003, 2007, 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 # include "incls/_precompiled.incl"
  26 # include "incls/_classify.cpp.incl"
  27 
  28 
  29 const char* ClassifyObjectClosure::object_type_name[number_object_types] = {
  30   "unknown",
  31   "instance",
  32   "instanceRef",
  33   "objArray",
  34   "symbol",
  35   "klass",
  36   "instanceKlass",
  37   "method",
  38   "constMethod",
  39   "methodData",
  40   "constantPool",
  41   "constantPoolCache",
  42   "typeArray",
  43   "compiledICHolder"
  44 };
  45 
  46 
  47 object_type ClassifyObjectClosure::classify_object(oop obj, bool count) {
  48   object_type type = unknown_type;
  49 
  50   Klass* k = obj->blueprint();
  51 
  52   if (k->as_klassOop() == SystemDictionary::Object_klass()) {
  53     tty->print_cr("Found the class!");
  54   }
  55 
  56   if (count) {
  57     k->set_alloc_count(k->alloc_count() + 1);
  58   }
  59 
  60   if (obj->is_instance()) {
  61     if (k->oop_is_instanceRef()) {
  62       type = instanceRef_type;
  63     } else {
  64       type = instance_type;
  65     }
  66   } else if (obj->is_typeArray()) {
  67     type = typeArray_type;
  68   } else if (obj->is_objArray()) {
  69     type = objArray_type;
  70   } else if (obj->is_symbol()) {
  71     type = symbol_type;
  72   } else if (obj->is_klass()) {
  73     Klass* k = ((klassOop)obj)->klass_part();
  74     if (k->oop_is_instance()) {
  75       type = instanceKlass_type;
  76     } else {
  77       type = klass_type;
  78     }
  79   } else if (obj->is_method()) {
  80     type = method_type;
  81   } else if (obj->is_constMethod()) {
  82     type = constMethod_type;
  83   } else if (obj->is_methodData()) {
  84     ShouldNotReachHere();
  85   } else if (obj->is_constantPool()) {
  86     type = constantPool_type;
  87   } else if (obj->is_constantPoolCache()) {
  88     type = constantPoolCache_type;
  89   } else if (obj->is_compiledICHolder()) {
  90     type = compiledICHolder_type;
  91   } else {
  92     ShouldNotReachHere();
  93   }
  94 
  95   assert(type != unknown_type, "found object of unknown type.");
  96   return type;
  97 }
  98 
  99 
 100 void ClassifyObjectClosure::reset() {
 101   for (int i = 0; i < number_object_types; ++i) {
 102     object_count[i] = 0;
 103     object_size[i] = 0;
 104   }
 105   total_object_count = 0;
 106   total_object_size = 0;
 107 }
 108 
 109 
 110 void ClassifyObjectClosure::do_object(oop obj) {
 111   int i = classify_object(obj, true);
 112   ++object_count[i];
 113   ++total_object_count;
 114   size_t size = obj->size() * HeapWordSize;
 115   object_size[i] += size;
 116   total_object_size += size;
 117 }
 118 
 119 
 120 size_t ClassifyObjectClosure::print() {
 121   int num_objects = 0;
 122   size_t size_objects = 0;
 123   for (int i = 0; i < number_object_types; ++i) {
 124     if (object_count[i] != 0) {
 125       tty->print_cr("%8d  %-22s  (%8d bytes, %5.2f bytes/object)",
 126                     object_count[i], object_type_name[i], object_size[i],
 127                     (float)object_size[i]/(float)object_count[i]);
 128     }
 129     num_objects += object_count[i];
 130     size_objects += object_size[i];
 131   }
 132   assert(num_objects == total_object_count, "Object count mismatch!");
 133   assert(size_objects == total_object_size, "Object size mismatch!");
 134 
 135   tty->print_cr(" Total:  %d objects, %d bytes", total_object_count,
 136                 total_object_size);
 137   return total_object_size;
 138 }
 139 
 140 
 141 void ClassifyInstanceKlassClosure::do_object(oop obj) {
 142   int type = classify_object(obj, false);
 143   if (type == instanceKlass_type || type == klass_type) {
 144     Klass* k = ((klassOop)obj)->klass_part();
 145     if (k->alloc_count() > 0) {
 146       ResourceMark rm;
 147       const char *name;
 148       if (k->name() == NULL) {
 149 
 150         if (obj == Universe::klassKlassObj()) {
 151           name = "_klassKlassObj";
 152         } else if (obj == Universe::arrayKlassKlassObj()) {
 153           name = "_arrayKlassKlassObj";
 154         } else if (obj == Universe::objArrayKlassKlassObj()) {
 155           name = "_objArrayKlassKlassObj";
 156         } else if (obj == Universe::typeArrayKlassKlassObj()) {
 157           name = "_typeArrayKlassKlassObj";
 158         } else if (obj == Universe::instanceKlassKlassObj()) {
 159           name = "_instanceKlassKlassObj";
 160         } else if (obj == Universe::symbolKlassObj()) {
 161           name = "_symbolKlassObj";
 162         } else if (obj == Universe::methodKlassObj()) {
 163           name = "_methodKlassObj";
 164         } else if (obj == Universe::constMethodKlassObj()) {
 165           name = "_constMethodKlassObj";
 166         } else if (obj == Universe::constantPoolKlassObj()) {
 167           name = "_constantPoolKlassObj";
 168         } else if (obj == Universe::constantPoolCacheKlassObj()) {
 169           name = "_constantPoolCacheKlassObj";
 170         } else if (obj == Universe::compiledICHolderKlassObj()) {
 171           name = "_compiledICHolderKlassObj";
 172         } else if (obj == Universe::systemObjArrayKlassObj()) {
 173           name = "_systemObjArrayKlassObj";
 174         } else {
 175           name = "[unnamed]";
 176         }
 177       } else {
 178         name = k->external_name();
 179       }
 180       tty->print_cr("% 8d  instances of %s", k->alloc_count(), name);
 181     }
 182     total_instances += k->alloc_count();
 183   }
 184 }
 185 
 186 
 187 void ClassifyInstanceKlassClosure::print() {
 188   tty->print_cr(" Total instances:  %d.", total_instances);
 189 }
 190 
 191 
 192 void ClassifyInstanceKlassClosure::reset() {
 193   total_instances = 0;
 194 }