Print this page




  48     if (_klass == Universe::instanceKlassKlassObj())     name = "<instanceKlassKlass>";     else
  49     if (_klass == Universe::typeArrayKlassKlassObj())    name = "<typeArrayKlassKlass>";    else
  50     if (_klass == Universe::symbolKlassObj())            name = "<symbolKlass>";            else
  51     if (_klass == Universe::boolArrayKlassObj())         name = "<boolArrayKlass>";         else
  52     if (_klass == Universe::charArrayKlassObj())         name = "<charArrayKlass>";         else
  53     if (_klass == Universe::singleArrayKlassObj())       name = "<singleArrayKlass>";       else
  54     if (_klass == Universe::doubleArrayKlassObj())       name = "<doubleArrayKlass>";       else
  55     if (_klass == Universe::byteArrayKlassObj())         name = "<byteArrayKlass>";         else
  56     if (_klass == Universe::shortArrayKlassObj())        name = "<shortArrayKlass>";        else
  57     if (_klass == Universe::intArrayKlassObj())          name = "<intArrayKlass>";          else
  58     if (_klass == Universe::longArrayKlassObj())         name = "<longArrayKlass>";         else
  59     if (_klass == Universe::methodKlassObj())            name = "<methodKlass>";            else
  60     if (_klass == Universe::constMethodKlassObj())       name = "<constMethodKlass>";       else
  61     if (_klass == Universe::methodDataKlassObj())        name = "<methodDataKlass>";        else
  62     if (_klass == Universe::constantPoolKlassObj())      name = "<constantPoolKlass>";      else
  63     if (_klass == Universe::constantPoolCacheKlassObj()) name = "<constantPoolCacheKlass>"; else
  64     if (_klass == Universe::compiledICHolderKlassObj())  name = "<compiledICHolderKlass>";  else
  65       name = "<no name>";
  66   }
  67   // simplify the formatting (ILP32 vs LP64) - always cast the numbers to 64-bit
  68   st->print_cr("%13" FORMAT64_MODIFIER "d  %13" FORMAT64_MODIFIER "u  %s",
  69                (jlong)  _instance_count,
  70                (julong) _instance_words * HeapWordSize,
  71                name);
  72 }
  73 
  74 KlassInfoEntry* KlassInfoBucket::lookup(const klassOop k) {
  75   KlassInfoEntry* elt = _list;
  76   while (elt != NULL) {
  77     if (elt->is_equal(k)) {
  78       return elt;
  79     }
  80     elt = elt->next();
  81   }
  82   elt = new KlassInfoEntry(k, list());


  83   set_list(elt);

  84   return elt;
  85 }
  86 
  87 void KlassInfoBucket::iterate(KlassInfoClosure* cic) {
  88   KlassInfoEntry* elt = _list;
  89   while (elt != NULL) {
  90     cic->do_cinfo(elt);
  91     elt = elt->next();
  92   }
  93 }
  94 
  95 void KlassInfoBucket::empty() {
  96   KlassInfoEntry* elt = _list;
  97   _list = NULL;
  98   while (elt != NULL) {
  99     KlassInfoEntry* next = elt->next();
 100     delete elt;
 101     elt = next;
 102   }
 103 }
 104 
 105 KlassInfoTable::KlassInfoTable(int size, HeapWord* ref) {
 106   _size = size;
 107   _ref = ref;
 108   _buckets = NEW_C_HEAP_ARRAY(KlassInfoBucket, _size);
 109 

 110   for (int index = 0; index < _size; index++) {
 111     _buckets[index].initialize();
 112   }

 113 }
 114 
 115 KlassInfoTable::~KlassInfoTable() {

 116   for (int index = 0; index < _size; index++) {
 117     _buckets[index].empty();
 118   }
 119   FREE_C_HEAP_ARRAY(KlassInfoBucket, _buckets);
 120   _size = 0;

 121 }
 122 
 123 uint KlassInfoTable::hash(klassOop p) {
 124   assert(Universe::heap()->is_in_permanent((HeapWord*)p), "all klasses in permgen");
 125   return (uint)(((uintptr_t)p - (uintptr_t)_ref) >> 2);
 126 }
 127 
 128 KlassInfoEntry* KlassInfoTable::lookup(const klassOop k) {
 129   uint         idx = hash(k) % _size;

 130   KlassInfoEntry*  e   = _buckets[idx].lookup(k);
 131   assert(k == e->klass(), "must be equal");


 132   return e;
 133 }
 134 
 135 void KlassInfoTable::record_instance(const oop obj) {


 136   klassOop      k = obj->klass();
 137   KlassInfoEntry* elt = lookup(k);



 138   elt->set_count(elt->count() + 1);
 139   elt->set_words(elt->words() + obj->size());




 140 }
 141 
 142 void KlassInfoTable::iterate(KlassInfoClosure* cic) {

 143   for (int index = 0; index < _size; index++) {
 144     _buckets[index].iterate(cic);
 145   }
 146 }
 147 
 148 int KlassInfoHisto::sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2) {
 149   return (*e1)->compare(*e1,*e2);
 150 }
 151 
 152 KlassInfoHisto::KlassInfoHisto(const char* title, int estimatedCount) :
 153   _title(title) {
 154   _elements = new (ResourceObj::C_HEAP) GrowableArray<KlassInfoEntry*>(estimatedCount,true);
 155 }
 156 
 157 KlassInfoHisto::~KlassInfoHisto() {
 158   delete _elements;
 159 }
 160 
 161 void KlassInfoHisto::add(KlassInfoEntry* cie) {
 162   elements()->append(cie);
 163 }
 164 
 165 void KlassInfoHisto::sort() {
 166   elements()->sort(KlassInfoHisto::sort_helper);
 167 }
 168 
 169 void KlassInfoHisto::print_elements(outputStream* st) const {
 170   // simplify the formatting (ILP32 vs LP64) - store the sum in 64-bit
 171   jlong total = 0;
 172   julong totalw = 0;
 173   for(int i=0; i < elements()->length(); i++) {
 174     st->print("%4d: ", i+1);
 175     elements()->at(i)->print_on(st);
 176     total += elements()->at(i)->count();
 177     totalw += elements()->at(i)->words();
 178   }
 179   st->print_cr("Total %13" FORMAT64_MODIFIER "d  %13" FORMAT64_MODIFIER "u",
 180                total, totalw * HeapWordSize);
 181 }
 182 
 183 void KlassInfoHisto::print_on(outputStream* st) const {
 184   st->print_cr("%s",title());
 185   print_elements(st);
 186 }
 187 
 188 class HistoClosure : public KlassInfoClosure {
 189  private:
 190   KlassInfoHisto* _cih;
 191  public:
 192   HistoClosure(KlassInfoHisto* cih) : _cih(cih) {}
 193 
 194   void do_cinfo(KlassInfoEntry* cie) {
 195     _cih->add(cie);
 196   }
 197 };
 198 
 199 class RecordInstanceClosure : public ObjectClosure {
 200  private:
 201   KlassInfoTable* _cit;

 202  public:
 203   RecordInstanceClosure(KlassInfoTable* cit) : _cit(cit) {}

 204 
 205   void do_object(oop obj) {
 206     _cit->record_instance(obj);

 207   }



 208 };
 209 
 210 void HeapInspection::heap_inspection(outputStream* st) {
 211   ResourceMark rm;
 212   HeapWord* ref;
 213 
 214   CollectedHeap* heap = Universe::heap();
 215   switch (heap->kind()) {
 216     case CollectedHeap::GenCollectedHeap: {
 217       GenCollectedHeap* gch = (GenCollectedHeap*)heap;
 218       gch->gc_prologue(false /* !full */); // get any necessary locks
 219       ref = gch->perm_gen()->used_region().start();
 220       break;
 221     }
 222 #ifndef SERIALGC
 223     case CollectedHeap::ParallelScavengeHeap: {
 224       ParallelScavengeHeap* psh = (ParallelScavengeHeap*)heap;
 225       ref = psh->perm_gen()->object_space()->used_region().start();
 226       break;
 227     }
 228 #endif // SERIALGC
 229     default:
 230       ShouldNotReachHere(); // Unexpected heap kind for this op
 231   }
 232   // Collect klass instance info
 233 
 234   // Iterate over objects in the heap
 235   KlassInfoTable cit(KlassInfoTable::cit_size, ref);


 236   RecordInstanceClosure ric(&cit);
 237   Universe::heap()->object_iterate(&ric);
 238 







 239   // Sort and print klass instance info
 240   KlassInfoHisto histo("\n"
 241                    " num     #instances         #bytes  class name\n"
 242                    "----------------------------------------------",
 243                    KlassInfoHisto::histo_initial_size);
 244   HistoClosure hc(&histo);
 245   cit.iterate(&hc);
 246   histo.sort();
 247   histo.print_on(st);



 248   st->flush();
 249 
 250   if (Universe::heap()->kind() == CollectedHeap::GenCollectedHeap) {
 251     GenCollectedHeap* gch = GenCollectedHeap::heap();
 252     gch->gc_epilogue(false /* !full */); // release all acquired locks
 253   }
 254 }
 255 
 256 class FindInstanceClosure : public ObjectClosure {
 257  private:
 258   klassOop _klass;
 259   GrowableArray<oop>* _result;
 260 
 261  public:
 262   FindInstanceClosure(klassOop k, GrowableArray<oop>* result) : _klass(k), _result(result) {};
 263 
 264   void do_object(oop obj) {
 265     if (obj->is_a(_klass)) {
 266       _result->append(obj);
 267     }


  48     if (_klass == Universe::instanceKlassKlassObj())     name = "<instanceKlassKlass>";     else
  49     if (_klass == Universe::typeArrayKlassKlassObj())    name = "<typeArrayKlassKlass>";    else
  50     if (_klass == Universe::symbolKlassObj())            name = "<symbolKlass>";            else
  51     if (_klass == Universe::boolArrayKlassObj())         name = "<boolArrayKlass>";         else
  52     if (_klass == Universe::charArrayKlassObj())         name = "<charArrayKlass>";         else
  53     if (_klass == Universe::singleArrayKlassObj())       name = "<singleArrayKlass>";       else
  54     if (_klass == Universe::doubleArrayKlassObj())       name = "<doubleArrayKlass>";       else
  55     if (_klass == Universe::byteArrayKlassObj())         name = "<byteArrayKlass>";         else
  56     if (_klass == Universe::shortArrayKlassObj())        name = "<shortArrayKlass>";        else
  57     if (_klass == Universe::intArrayKlassObj())          name = "<intArrayKlass>";          else
  58     if (_klass == Universe::longArrayKlassObj())         name = "<longArrayKlass>";         else
  59     if (_klass == Universe::methodKlassObj())            name = "<methodKlass>";            else
  60     if (_klass == Universe::constMethodKlassObj())       name = "<constMethodKlass>";       else
  61     if (_klass == Universe::methodDataKlassObj())        name = "<methodDataKlass>";        else
  62     if (_klass == Universe::constantPoolKlassObj())      name = "<constantPoolKlass>";      else
  63     if (_klass == Universe::constantPoolCacheKlassObj()) name = "<constantPoolCacheKlass>"; else
  64     if (_klass == Universe::compiledICHolderKlassObj())  name = "<compiledICHolderKlass>";  else
  65       name = "<no name>";
  66   }
  67   // simplify the formatting (ILP32 vs LP64) - always cast the numbers to 64-bit
  68   st->print_cr(INT64_FORMAT_W(13) "  " UINT64_FORMAT_W(13) "  %s",
  69                (jlong)  _instance_count,
  70                (julong) _instance_words * HeapWordSize,
  71                name);
  72 }
  73 
  74 KlassInfoEntry* KlassInfoBucket::lookup(const klassOop k) {
  75   KlassInfoEntry* elt = _list;
  76   while (elt != NULL) {
  77     if (elt->is_equal(k)) {
  78       return elt;
  79     }
  80     elt = elt->next();
  81   }
  82   elt = new KlassInfoEntry(k, list());
  83   // We may be out of space to allocate the new entry.
  84   if (elt != NULL) {
  85     set_list(elt);
  86   }
  87   return elt;
  88 }
  89 
  90 void KlassInfoBucket::iterate(KlassInfoClosure* cic) {
  91   KlassInfoEntry* elt = _list;
  92   while (elt != NULL) {
  93     cic->do_cinfo(elt);
  94     elt = elt->next();
  95   }
  96 }
  97 
  98 void KlassInfoBucket::empty() {
  99   KlassInfoEntry* elt = _list;
 100   _list = NULL;
 101   while (elt != NULL) {
 102     KlassInfoEntry* next = elt->next();
 103     delete elt;
 104     elt = next;
 105   }
 106 }
 107 
 108 KlassInfoTable::KlassInfoTable(int size, HeapWord* ref) {
 109   _size = 0;
 110   _ref = ref;
 111   _buckets = NEW_C_HEAP_ARRAY(KlassInfoBucket, size);
 112   if (_buckets != NULL) {
 113     _size = size;
 114     for (int index = 0; index < _size; index++) {
 115       _buckets[index].initialize();
 116     }
 117   }
 118 }
 119 
 120 KlassInfoTable::~KlassInfoTable() {
 121   if (_buckets != NULL) {
 122     for (int index = 0; index < _size; index++) {
 123       _buckets[index].empty();
 124     }
 125     FREE_C_HEAP_ARRAY(KlassInfoBucket, _buckets);
 126     _size = 0;
 127   }
 128 }
 129 
 130 uint KlassInfoTable::hash(klassOop p) {
 131   assert(Universe::heap()->is_in_permanent((HeapWord*)p), "all klasses in permgen");
 132   return (uint)(((uintptr_t)p - (uintptr_t)_ref) >> 2);
 133 }
 134 
 135 KlassInfoEntry* KlassInfoTable::lookup(const klassOop k) {
 136   uint         idx = hash(k) % _size;
 137   assert(_buckets != NULL, "Allocation failure should have been caught");
 138   KlassInfoEntry*  e   = _buckets[idx].lookup(k);
 139   // Lookup may fail if this is a new klass for which we
 140   // could not allocate space for an new entry.
 141   assert(e == NULL || k == e->klass(), "must be equal");
 142   return e;
 143 }
 144 
 145 // Return false if the entry could not be recorded on account
 146 // of running out of space required to create a new entry.
 147 bool KlassInfoTable::record_instance(const oop obj) {
 148   klassOop      k = obj->klass();
 149   KlassInfoEntry* elt = lookup(k);
 150   // elt may be NULL if it's a new klass for which we
 151   // could not allocate space for a new entry in the hashtable.
 152   if (elt != NULL) {
 153     elt->set_count(elt->count() + 1);
 154     elt->set_words(elt->words() + obj->size());
 155     return true;
 156   } else {
 157     return false;
 158   }
 159 }
 160 
 161 void KlassInfoTable::iterate(KlassInfoClosure* cic) {
 162   assert(_size == 0 || _buckets != NULL, "Allocation failure should have been caught");
 163   for (int index = 0; index < _size; index++) {
 164     _buckets[index].iterate(cic);
 165   }
 166 }
 167 
 168 int KlassInfoHisto::sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2) {
 169   return (*e1)->compare(*e1,*e2);
 170 }
 171 
 172 KlassInfoHisto::KlassInfoHisto(const char* title, int estimatedCount) :
 173   _title(title) {
 174   _elements = new (ResourceObj::C_HEAP) GrowableArray<KlassInfoEntry*>(estimatedCount,true);
 175 }
 176 
 177 KlassInfoHisto::~KlassInfoHisto() {
 178   delete _elements;
 179 }
 180 
 181 void KlassInfoHisto::add(KlassInfoEntry* cie) {
 182   elements()->append(cie);
 183 }
 184 
 185 void KlassInfoHisto::sort() {
 186   elements()->sort(KlassInfoHisto::sort_helper);
 187 }
 188 
 189 void KlassInfoHisto::print_elements(outputStream* st) const {
 190   // simplify the formatting (ILP32 vs LP64) - store the sum in 64-bit
 191   jlong total = 0;
 192   julong totalw = 0;
 193   for(int i=0; i < elements()->length(); i++) {
 194     st->print("%4d: ", i+1);
 195     elements()->at(i)->print_on(st);
 196     total += elements()->at(i)->count();
 197     totalw += elements()->at(i)->words();
 198   }
 199   st->print_cr("Total " INT64_FORMAT_W(13) "  " UINT64_FORMAT_W(13),
 200                total, totalw * HeapWordSize);
 201 }
 202 
 203 void KlassInfoHisto::print_on(outputStream* st) const {
 204   st->print_cr("%s",title());
 205   print_elements(st);
 206 }
 207 
 208 class HistoClosure : public KlassInfoClosure {
 209  private:
 210   KlassInfoHisto* _cih;
 211  public:
 212   HistoClosure(KlassInfoHisto* cih) : _cih(cih) {}
 213 
 214   void do_cinfo(KlassInfoEntry* cie) {
 215     _cih->add(cie);
 216   }
 217 };
 218 
 219 class RecordInstanceClosure : public ObjectClosure {
 220  private:
 221   KlassInfoTable* _cit;
 222   size_t _missed_count;
 223  public:
 224   RecordInstanceClosure(KlassInfoTable* cit) :
 225     _cit(cit), _missed_count(0) {}
 226 
 227   void do_object(oop obj) {
 228     if (!_cit->record_instance(obj)) {
 229       _missed_count++;
 230     }
 231   }
 232 
 233   size_t missed_count() { return _missed_count; }
 234 };
 235 
 236 void HeapInspection::heap_inspection(outputStream* st) {
 237   ResourceMark rm;
 238   HeapWord* ref;
 239 
 240   CollectedHeap* heap = Universe::heap();
 241   switch (heap->kind()) {
 242     case CollectedHeap::GenCollectedHeap: {
 243       GenCollectedHeap* gch = (GenCollectedHeap*)heap;
 244       gch->gc_prologue(false /* !full */); // get any necessary locks
 245       ref = gch->perm_gen()->used_region().start();
 246       break;
 247     }
 248 #ifndef SERIALGC
 249     case CollectedHeap::ParallelScavengeHeap: {
 250       ParallelScavengeHeap* psh = (ParallelScavengeHeap*)heap;
 251       ref = psh->perm_gen()->object_space()->used_region().start();
 252       break;
 253     }
 254 #endif // SERIALGC
 255     default:
 256       ShouldNotReachHere(); // Unexpected heap kind for this op
 257   }
 258   // Collect klass instance info


 259   KlassInfoTable cit(KlassInfoTable::cit_size, ref);
 260   if (!cit.allocation_failed()) {
 261     // Iterate over objects in the heap
 262     RecordInstanceClosure ric(&cit);
 263     Universe::heap()->object_iterate(&ric);
 264   
 265     // Report if certain classes are not counted because of
 266     // running out of C-heap for the histogram.
 267     size_t missed_count = ric.missed_count();
 268     if (missed_count != 0) {
 269       st->print_cr("WARNING: Ran out of C-heap; undercounted " SIZE_FORMAT " instances in data below",
 270                    missed_count);
 271     }
 272     // Sort and print klass instance info
 273     KlassInfoHisto histo("\n"
 274                      " num     #instances         #bytes  class name\n"
 275                      "----------------------------------------------",
 276                      KlassInfoHisto::histo_initial_size);
 277     HistoClosure hc(&histo);
 278     cit.iterate(&hc);
 279     histo.sort();
 280     histo.print_on(st);
 281   } else {
 282     st->print_cr("WARNING: Ran out of C-heap; histogram not generated");
 283   }
 284   st->flush();
 285 
 286   if (Universe::heap()->kind() == CollectedHeap::GenCollectedHeap) {
 287     GenCollectedHeap* gch = GenCollectedHeap::heap();
 288     gch->gc_epilogue(false /* !full */); // release all acquired locks
 289   }
 290 }
 291 
 292 class FindInstanceClosure : public ObjectClosure {
 293  private:
 294   klassOop _klass;
 295   GrowableArray<oop>* _result;
 296 
 297  public:
 298   FindInstanceClosure(klassOop k, GrowableArray<oop>* result) : _klass(k), _result(result) {};
 299 
 300   void do_object(oop obj) {
 301     if (obj->is_a(_klass)) {
 302       _result->append(obj);
 303     }