hotspot/src/share/vm/memory/heapInspection.cpp
changeset 29071 73f45d04ad7a
parent 27880 afb974a04396
child 29085 7b01b134b727
equal deleted inserted replaced
29069:938c1c97ad3b 29071:73f45d04ad7a
     1 /*
     1 /*
     2  * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  *
    22  *
    23  */
    23  */
    24 
    24 
    25 #include "precompiled.hpp"
    25 #include "precompiled.hpp"
    26 #include "classfile/classLoaderData.hpp"
    26 #include "classfile/classLoaderData.hpp"
       
    27 #include "classfile/systemDictionary.hpp"
    27 #include "gc_interface/collectedHeap.hpp"
    28 #include "gc_interface/collectedHeap.hpp"
    28 #include "memory/genCollectedHeap.hpp"
    29 #include "memory/genCollectedHeap.hpp"
    29 #include "memory/heapInspection.hpp"
    30 #include "memory/heapInspection.hpp"
    30 #include "memory/resourceArea.hpp"
    31 #include "memory/resourceArea.hpp"
    31 #include "runtime/os.hpp"
    32 #include "runtime/os.hpp"
    36 #endif // INCLUDE_ALL_GCS
    37 #endif // INCLUDE_ALL_GCS
    37 
    38 
    38 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    39 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    39 
    40 
    40 // HeapInspection
    41 // HeapInspection
       
    42 
       
    43 inline KlassInfoEntry::~KlassInfoEntry() {
       
    44   if (_subclasses != NULL) {
       
    45     delete _subclasses;
       
    46   }
       
    47 }
       
    48 
       
    49 inline void KlassInfoEntry::add_subclass(KlassInfoEntry* cie) {
       
    50   if (_subclasses == NULL) {
       
    51     _subclasses = new  (ResourceObj::C_HEAP, mtInternal) GrowableArray<KlassInfoEntry*>(4, true);
       
    52   }
       
    53   _subclasses->append(cie);
       
    54 }
    41 
    55 
    42 int KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) {
    56 int KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) {
    43   if(e1->_instance_words > e2->_instance_words) {
    57   if(e1->_instance_words > e2->_instance_words) {
    44     return -1;
    58     return -1;
    45   } else if(e1->_instance_words < e2->_instance_words) {
    59   } else if(e1->_instance_words < e2->_instance_words) {
   127   // This has the SIDE EFFECT of creating a KlassInfoEntry
   141   // This has the SIDE EFFECT of creating a KlassInfoEntry
   128   // for <k>, if one doesn't exist yet.
   142   // for <k>, if one doesn't exist yet.
   129   _table->lookup(k);
   143   _table->lookup(k);
   130 }
   144 }
   131 
   145 
   132 KlassInfoTable::KlassInfoTable(bool need_class_stats) {
   146 KlassInfoTable::KlassInfoTable(bool add_all_classes) {
   133   _size_of_instances_in_words = 0;
   147   _size_of_instances_in_words = 0;
   134   _size = 0;
   148   _size = 0;
   135   _ref = (HeapWord*) Universe::boolArrayKlassObj();
   149   _ref = (HeapWord*) Universe::boolArrayKlassObj();
   136   _buckets =
   150   _buckets =
   137     (KlassInfoBucket*)  AllocateHeap(sizeof(KlassInfoBucket) * _num_buckets,
   151     (KlassInfoBucket*)  AllocateHeap(sizeof(KlassInfoBucket) * _num_buckets,
   139   if (_buckets != NULL) {
   153   if (_buckets != NULL) {
   140     _size = _num_buckets;
   154     _size = _num_buckets;
   141     for (int index = 0; index < _size; index++) {
   155     for (int index = 0; index < _size; index++) {
   142       _buckets[index].initialize();
   156       _buckets[index].initialize();
   143     }
   157     }
   144     if (need_class_stats) {
   158     if (add_all_classes) {
   145       AllClassesFinder finder(this);
   159       AllClassesFinder finder(this);
   146       ClassLoaderDataGraph::classes_do(&finder);
   160       ClassLoaderDataGraph::classes_do(&finder);
   147     }
   161     }
   148   }
   162   }
   149 }
   163 }
   297     st->print(",ClassLoader");
   311     st->print(",ClassLoader");
   298   }
   312   }
   299   st->cr();
   313   st->cr();
   300 }
   314 }
   301 
   315 
       
   316 class HierarchyClosure : public KlassInfoClosure {
       
   317 private:
       
   318   GrowableArray<KlassInfoEntry*> *_elements;
       
   319 public:
       
   320   HierarchyClosure(GrowableArray<KlassInfoEntry*> *_elements) : _elements(_elements) {}
       
   321 
       
   322   void do_cinfo(KlassInfoEntry* cie) {
       
   323     // ignore array classes
       
   324     if (cie->klass()->oop_is_instance()) {
       
   325       _elements->append(cie);
       
   326     }
       
   327   }
       
   328 };
       
   329 
       
   330 void KlassHierarchy::print_class_hierarchy(outputStream* st, bool print_interfaces,
       
   331                                            bool print_subclasses, char* classname) {
       
   332   ResourceMark rm;
       
   333   Stack <KlassInfoEntry*, mtClass> class_stack;
       
   334   GrowableArray<KlassInfoEntry*> elements;
       
   335 
       
   336   // Add all classes to the KlassInfoTable, which allows for quick lookup.
       
   337   // A KlassInfoEntry will be created for each class.
       
   338   KlassInfoTable cit(true);
       
   339   if (cit.allocation_failed()) {
       
   340     st->print_cr("ERROR: Ran out of C-heap; hierarchy not generated");
       
   341     return;
       
   342   }
       
   343 
       
   344   // Add all created KlassInfoEntry instances to the elements array for easy
       
   345   // iteration, and to allow each KlassInfoEntry instance to have a unique index.
       
   346   HierarchyClosure hc(&elements);
       
   347   cit.iterate(&hc);
       
   348 
       
   349   for(int i = 0; i < elements.length(); i++) {
       
   350     KlassInfoEntry* cie = elements.at(i);
       
   351     const InstanceKlass* k = (InstanceKlass*)cie->klass();
       
   352     Klass* super = ((InstanceKlass*)k)->java_super();
       
   353 
       
   354     // Set the index for the class.
       
   355     cie->set_index(i + 1);
       
   356 
       
   357     // Add the class to the subclass array of its superclass.
       
   358     if (super != NULL) {
       
   359       KlassInfoEntry* super_cie = cit.lookup(super);
       
   360       assert(super_cie != NULL, "could not lookup superclass");
       
   361       super_cie->add_subclass(cie);
       
   362     }
       
   363   }
       
   364 
       
   365   // Set the do_print flag for each class that should be printed.
       
   366   for(int i = 0; i < elements.length(); i++) {
       
   367     KlassInfoEntry* cie = elements.at(i);
       
   368     if (classname == NULL) {
       
   369       // We are printing all classes.
       
   370       cie->set_do_print(true);
       
   371     } else {
       
   372       // We are only printing the hierarchy of a specific class.
       
   373       if (strcmp(classname, cie->klass()->external_name()) == 0) {
       
   374         KlassHierarchy::set_do_print_for_class_hierarchy(cie, &cit, print_subclasses);
       
   375       }
       
   376     }
       
   377   }
       
   378 
       
   379   // Now we do a depth first traversal of the class hierachry. The class_stack will
       
   380   // maintain the list of classes we still need to process. Start things off
       
   381   // by priming it with java.lang.Object.
       
   382   KlassInfoEntry* jlo_cie = cit.lookup(SystemDictionary::Object_klass());
       
   383   assert(jlo_cie != NULL, "could not lookup java.lang.Object");
       
   384   class_stack.push(jlo_cie);
       
   385 
       
   386   // Repeatedly pop the top item off the stack, print its class info,
       
   387   // and push all of its subclasses on to the stack. Do this until there
       
   388   // are no classes left on the stack.
       
   389   while (!class_stack.is_empty()) {
       
   390     KlassInfoEntry* curr_cie = class_stack.pop();
       
   391     if (curr_cie->do_print()) {
       
   392       print_class(st, curr_cie, print_interfaces);
       
   393       if (curr_cie->subclasses() != NULL) {
       
   394         // Current class has subclasses, so push all of them onto the stack.
       
   395         for (int i = 0; i < curr_cie->subclasses()->length(); i++) {
       
   396           KlassInfoEntry* cie = curr_cie->subclasses()->at(i);
       
   397           if (cie->do_print()) {
       
   398             class_stack.push(cie);
       
   399           }
       
   400         }
       
   401       }
       
   402     }
       
   403   }
       
   404 
       
   405   st->flush();
       
   406 }
       
   407 
       
   408 // Sets the do_print flag for every superclass and subclass of the specified class.
       
   409 void KlassHierarchy::set_do_print_for_class_hierarchy(KlassInfoEntry* cie, KlassInfoTable* cit,
       
   410                                                       bool print_subclasses) {
       
   411   // Set do_print for all superclasses of this class.
       
   412   Klass* super = ((InstanceKlass*)cie->klass())->java_super();
       
   413   while (super != NULL) {
       
   414     KlassInfoEntry* super_cie = cit->lookup(super);
       
   415     super_cie->set_do_print(true);
       
   416     super = super->super();
       
   417   }
       
   418 
       
   419   // Set do_print for this class and all of its subclasses.
       
   420   Stack <KlassInfoEntry*, mtClass> class_stack;
       
   421   class_stack.push(cie);
       
   422   while (!class_stack.is_empty()) {
       
   423     KlassInfoEntry* curr_cie = class_stack.pop();
       
   424     curr_cie->set_do_print(true);
       
   425     if (print_subclasses && curr_cie->subclasses() != NULL) {
       
   426       // Current class has subclasses, so push all of them onto the stack.
       
   427       for (int i = 0; i < curr_cie->subclasses()->length(); i++) {
       
   428         KlassInfoEntry* cie = curr_cie->subclasses()->at(i);
       
   429         class_stack.push(cie);
       
   430       }
       
   431     }
       
   432   }
       
   433 }
       
   434 
       
   435 static void print_indent(outputStream* st, int indent) {
       
   436   while (indent != 0) {
       
   437     st->print("|");
       
   438     indent--;
       
   439     if (indent != 0) {
       
   440       st->print("  ");
       
   441     }
       
   442   }
       
   443 }
       
   444 
       
   445 // Print the class name and its unique ClassLoader identifer.
       
   446 static void print_classname(outputStream* st, Klass* klass) {
       
   447   oop loader_oop = klass->class_loader_data()->class_loader();
       
   448   st->print("%s/", klass->external_name());
       
   449   if (loader_oop == NULL) {
       
   450     st->print("null");
       
   451   } else {
       
   452     st->print(INTPTR_FORMAT, klass->class_loader_data());
       
   453   }
       
   454 }
       
   455 
       
   456 static void print_interface(outputStream* st, Klass* intf_klass, const char* intf_type, int indent) {
       
   457   print_indent(st, indent);
       
   458   st->print("  implements ");
       
   459   print_classname(st, intf_klass);
       
   460   st->print(" (%s intf)\n", intf_type);
       
   461 }
       
   462 
       
   463 void KlassHierarchy::print_class(outputStream* st, KlassInfoEntry* cie, bool print_interfaces) {
       
   464   ResourceMark rm;
       
   465   InstanceKlass* klass = (InstanceKlass*)cie->klass();
       
   466   int indent = 0;
       
   467 
       
   468   // Print indentation with proper indicators of superclass.
       
   469   Klass* super = klass->super();
       
   470   while (super != NULL) {
       
   471     super = super->super();
       
   472     indent++;
       
   473   }
       
   474   print_indent(st, indent);
       
   475   if (indent != 0) st->print("--");
       
   476 
       
   477   // Print the class name, its unique ClassLoader identifer, and if it is an interface.
       
   478   print_classname(st, klass);
       
   479   if (klass->is_interface()) {
       
   480     st->print(" (intf)");
       
   481   }
       
   482   st->print("\n");
       
   483 
       
   484   // Print any interfaces the class has.
       
   485   if (print_interfaces) {
       
   486     Array<Klass*>* local_intfs = klass->local_interfaces();
       
   487     Array<Klass*>* trans_intfs = klass->transitive_interfaces();
       
   488     for (int i = 0; i < local_intfs->length(); i++) {
       
   489       print_interface(st, local_intfs->at(i), "declared", indent);
       
   490     }
       
   491     for (int i = 0; i < trans_intfs->length(); i++) {
       
   492       Klass* trans_interface = trans_intfs->at(i);
       
   493       // Only print transitive interfaces if they are not also declared.
       
   494       if (!local_intfs->contains(trans_interface)) {
       
   495         print_interface(st, trans_interface, "inherited", indent);
       
   496       }
       
   497     }
       
   498   }
       
   499 }
       
   500 
   302 void KlassInfoHisto::print_class_stats(outputStream* st,
   501 void KlassInfoHisto::print_class_stats(outputStream* st,
   303                                       bool csv_format, const char *columns) {
   502                                       bool csv_format, const char *columns) {
   304   ResourceMark rm;
   503   ResourceMark rm;
   305   KlassSizeStats sz, sz_sum;
   504   KlassSizeStats sz, sz_sum;
   306   int i;
   505   int i;
   318 
   517 
   319   for(i=0; i < elements()->length(); i++) {
   518   for(i=0; i < elements()->length(); i++) {
   320     elements()->at(i)->set_index(i+1);
   519     elements()->at(i)->set_index(i+1);
   321   }
   520   }
   322 
   521 
       
   522   // First iteration is for accumulating stats totals in colsum_table[].
       
   523   // Second iteration is for printing stats for each class.
   323   for (int pass=1; pass<=2; pass++) {
   524   for (int pass=1; pass<=2; pass++) {
   324     if (pass == 2) {
   525     if (pass == 2) {
   325       print_title(st, csv_format, selected, width_table, name_table);
   526       print_title(st, csv_format, selected, width_table, name_table);
   326     }
   527     }
   327     for(i=0; i < elements()->length(); i++) {
   528     for(i=0; i < elements()->length(); i++) {
   328       KlassInfoEntry* e = (KlassInfoEntry*)elements()->at(i);
   529       KlassInfoEntry* e = (KlassInfoEntry*)elements()->at(i);
   329       const Klass* k = e->klass();
   530       const Klass* k = e->klass();
   330 
   531 
       
   532       // Get the stats for this class.
   331       memset(&sz, 0, sizeof(sz));
   533       memset(&sz, 0, sizeof(sz));
   332       sz._inst_count = e->count();
   534       sz._inst_count = e->count();
   333       sz._inst_bytes = HeapWordSize * e->words();
   535       sz._inst_bytes = HeapWordSize * e->words();
   334       k->collect_statistics(&sz);
   536       k->collect_statistics(&sz);
   335       sz._total_bytes = sz._ro_bytes + sz._rw_bytes;
   537       sz._total_bytes = sz._ro_bytes + sz._rw_bytes;
   336 
   538 
   337       if (pass == 1) {
   539       if (pass == 1) {
       
   540         // Add the stats for this class to the overall totals.
   338         for (int c=0; c<KlassSizeStats::_num_columns; c++) {
   541         for (int c=0; c<KlassSizeStats::_num_columns; c++) {
   339           colsum_table[c] += col_table[c];
   542           colsum_table[c] += col_table[c];
   340         }
   543         }
   341       } else {
   544       } else {
   342         int super_index = -1;
   545         int super_index = -1;
       
   546         // Print the stats for this class.
   343         if (k->oop_is_instance()) {
   547         if (k->oop_is_instance()) {
   344           Klass* super = ((InstanceKlass*)k)->java_super();
   548           Klass* super = ((InstanceKlass*)k)->java_super();
   345           if (super) {
   549           if (super) {
   346             KlassInfoEntry* super_e = _cit->lookup(super);
   550             KlassInfoEntry* super_e = _cit->lookup(super);
   347             if (super_e) {
   551             if (super_e) {
   371         st->cr();
   575         st->cr();
   372       }
   576       }
   373     }
   577     }
   374 
   578 
   375     if (pass == 1) {
   579     if (pass == 1) {
       
   580       // Calculate the minimum width needed for the column by accounting for the
       
   581       // column header width and the width of the largest value in the column.
   376       for (int c=0; c<KlassSizeStats::_num_columns; c++) {
   582       for (int c=0; c<KlassSizeStats::_num_columns; c++) {
   377         width_table[c] = col_width(colsum_table[c], name_table[c]);
   583         width_table[c] = col_width(colsum_table[c], name_table[c]);
   378       }
   584       }
   379     }
   585     }
   380   }
   586   }
   381 
   587 
   382   sz_sum._inst_size = 0;
   588   sz_sum._inst_size = 0;
   383 
   589 
       
   590   // Print the column totals.
   384   if (csv_format) {
   591   if (csv_format) {
   385     st->print(",");
   592     st->print(",");
   386     for (int c=0; c<KlassSizeStats::_num_columns; c++) {
   593     for (int c=0; c<KlassSizeStats::_num_columns; c++) {
   387       if (selected[c]) {st->print("," JULONG_FORMAT, colsum_table[c]);}
   594       if (selected[c]) {st->print("," JULONG_FORMAT, colsum_table[c]);}
   388     }
   595     }
   512     return;
   719     return;
   513   }
   720   }
   514 
   721 
   515   KlassInfoTable cit(_print_class_stats);
   722   KlassInfoTable cit(_print_class_stats);
   516   if (!cit.allocation_failed()) {
   723   if (!cit.allocation_failed()) {
       
   724     // populate table with object allocation info
   517     size_t missed_count = populate_table(&cit);
   725     size_t missed_count = populate_table(&cit);
   518     if (missed_count != 0) {
   726     if (missed_count != 0) {
   519       st->print_cr("WARNING: Ran out of C-heap; undercounted " SIZE_FORMAT
   727       st->print_cr("WARNING: Ran out of C-heap; undercounted " SIZE_FORMAT
   520                    " total instances in data below",
   728                    " total instances in data below",
   521                    missed_count);
   729                    missed_count);
   531     cit.iterate(&hc);
   739     cit.iterate(&hc);
   532 
   740 
   533     histo.sort();
   741     histo.sort();
   534     histo.print_histo_on(st, _print_class_stats, _csv_format, _columns);
   742     histo.print_histo_on(st, _print_class_stats, _csv_format, _columns);
   535   } else {
   743   } else {
   536     st->print_cr("WARNING: Ran out of C-heap; histogram not generated");
   744     st->print_cr("ERROR: Ran out of C-heap; histogram not generated");
   537   }
   745   }
   538   st->flush();
   746   st->flush();
   539 }
   747 }
   540 
   748 
   541 class FindInstanceClosure : public ObjectClosure {
   749 class FindInstanceClosure : public ObjectClosure {