1 /*
   2  * Copyright (c) 2002, 2020, 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 "precompiled.hpp"
  26 #include "classfile/classLoaderData.inline.hpp"
  27 #include "classfile/classLoaderDataGraph.hpp"
  28 #include "classfile/moduleEntry.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "gc/shared/collectedHeap.hpp"
  31 #include "memory/heapInspection.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "memory/universe.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "oops/reflectionAccessorImplKlassHelper.hpp"
  36 #include "runtime/os.hpp"
  37 #include "utilities/globalDefinitions.hpp"
  38 #include "utilities/macros.hpp"
  39 #include "utilities/stack.inline.hpp"
  40 
  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 }
  55 
  56 int KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) {
  57   if(e1->_instance_words > e2->_instance_words) {
  58     return -1;
  59   } else if(e1->_instance_words < e2->_instance_words) {
  60     return 1;
  61   }
  62   // Sort alphabetically, note 'Z' < '[' < 'a', but it's better to group
  63   // the array classes before all the instance classes.
  64   ResourceMark rm;
  65   const char* name1 = e1->klass()->external_name();
  66   const char* name2 = e2->klass()->external_name();
  67   bool d1 = (name1[0] == JVM_SIGNATURE_ARRAY);
  68   bool d2 = (name2[0] == JVM_SIGNATURE_ARRAY);
  69   if (d1 && !d2) {
  70     return -1;
  71   } else if (d2 && !d1) {
  72     return 1;
  73   } else {
  74     return strcmp(name1, name2);
  75   }
  76 }
  77 
  78 const char* KlassInfoEntry::name() const {
  79   const char* name;
  80   if (_klass->name() != NULL) {
  81     name = _klass->external_name();
  82   } else {
  83     if (_klass == Universe::boolArrayKlassObj())         name = "<boolArrayKlass>";         else
  84     if (_klass == Universe::charArrayKlassObj())         name = "<charArrayKlass>";         else
  85     if (_klass == Universe::floatArrayKlassObj())        name = "<floatArrayKlass>";        else
  86     if (_klass == Universe::doubleArrayKlassObj())       name = "<doubleArrayKlass>";       else
  87     if (_klass == Universe::byteArrayKlassObj())         name = "<byteArrayKlass>";         else
  88     if (_klass == Universe::shortArrayKlassObj())        name = "<shortArrayKlass>";        else
  89     if (_klass == Universe::intArrayKlassObj())          name = "<intArrayKlass>";          else
  90     if (_klass == Universe::longArrayKlassObj())         name = "<longArrayKlass>";         else
  91       name = "<no name>";
  92   }
  93   return name;
  94 }
  95 
  96 void KlassInfoEntry::print_on(outputStream* st) const {
  97   ResourceMark rm;
  98 
  99   // simplify the formatting (ILP32 vs LP64) - always cast the numbers to 64-bit
 100   ModuleEntry* module = _klass->module();
 101   if (module->is_named()) {
 102     st->print_cr(INT64_FORMAT_W(13) "  " UINT64_FORMAT_W(13) "  %s (%s@%s)",
 103                  (int64_t)_instance_count,
 104                  (uint64_t)_instance_words * HeapWordSize,
 105                  name(),
 106                  module->name()->as_C_string(),
 107                  module->version() != NULL ? module->version()->as_C_string() : "");
 108   } else {
 109     st->print_cr(INT64_FORMAT_W(13) "  " UINT64_FORMAT_W(13) "  %s",
 110                  (int64_t)_instance_count,
 111                  (uint64_t)_instance_words * HeapWordSize,
 112                  name());
 113   }
 114 }
 115 
 116 KlassInfoEntry* KlassInfoBucket::lookup(Klass* const k) {
 117   // Can happen if k is an archived class that we haven't loaded yet.
 118   if (k->java_mirror_no_keepalive() == NULL) {
 119     return NULL;
 120   }
 121 
 122   KlassInfoEntry* elt = _list;
 123   while (elt != NULL) {
 124     if (elt->is_equal(k)) {
 125       return elt;
 126     }
 127     elt = elt->next();
 128   }
 129   elt = new (std::nothrow) KlassInfoEntry(k, list());
 130   // We may be out of space to allocate the new entry.
 131   if (elt != NULL) {
 132     set_list(elt);
 133   }
 134   return elt;
 135 }
 136 
 137 void KlassInfoBucket::iterate(KlassInfoClosure* cic) {
 138   KlassInfoEntry* elt = _list;
 139   while (elt != NULL) {
 140     cic->do_cinfo(elt);
 141     elt = elt->next();
 142   }
 143 }
 144 
 145 void KlassInfoBucket::empty() {
 146   KlassInfoEntry* elt = _list;
 147   _list = NULL;
 148   while (elt != NULL) {
 149     KlassInfoEntry* next = elt->next();
 150     delete elt;
 151     elt = next;
 152   }
 153 }
 154 
 155 class KlassInfoTable::AllClassesFinder : public LockedClassesDo {
 156   KlassInfoTable *_table;
 157 public:
 158   AllClassesFinder(KlassInfoTable* table) : _table(table) {}
 159   virtual void do_klass(Klass* k) {
 160     // This has the SIDE EFFECT of creating a KlassInfoEntry
 161     // for <k>, if one doesn't exist yet.
 162     _table->lookup(k);
 163   }
 164 };
 165 
 166 
 167 KlassInfoTable::KlassInfoTable(bool add_all_classes) {
 168   _size_of_instances_in_words = 0;
 169   _ref = (HeapWord*) Universe::boolArrayKlassObj();
 170   _buckets =
 171     (KlassInfoBucket*)  AllocateHeap(sizeof(KlassInfoBucket) * _num_buckets,
 172        mtInternal, CURRENT_PC, AllocFailStrategy::RETURN_NULL);
 173   if (_buckets != NULL) {
 174     for (int index = 0; index < _num_buckets; index++) {
 175       _buckets[index].initialize();
 176     }
 177     if (add_all_classes) {
 178       AllClassesFinder finder(this);
 179       ClassLoaderDataGraph::classes_do(&finder);
 180     }
 181   }
 182 }
 183 
 184 KlassInfoTable::~KlassInfoTable() {
 185   if (_buckets != NULL) {
 186     for (int index = 0; index < _num_buckets; index++) {
 187       _buckets[index].empty();
 188     }
 189     FREE_C_HEAP_ARRAY(KlassInfoBucket, _buckets);
 190     _buckets = NULL;
 191   }
 192 }
 193 
 194 uint KlassInfoTable::hash(const Klass* p) {
 195   return (uint)(((uintptr_t)p - (uintptr_t)_ref) >> 2);
 196 }
 197 
 198 KlassInfoEntry* KlassInfoTable::lookup(Klass* k) {
 199   uint         idx = hash(k) % _num_buckets;
 200   assert(_buckets != NULL, "Allocation failure should have been caught");
 201   KlassInfoEntry*  e   = _buckets[idx].lookup(k);
 202   // Lookup may fail if this is a new klass for which we
 203   // could not allocate space for an new entry, or if it's
 204   // an archived class that we haven't loaded yet.
 205   assert(e == NULL || k == e->klass(), "must be equal");
 206   return e;
 207 }
 208 
 209 // Return false if the entry could not be recorded on account
 210 // of running out of space required to create a new entry.
 211 bool KlassInfoTable::record_instance(const oop obj) {
 212   Klass*        k = obj->klass();
 213   KlassInfoEntry* elt = lookup(k);
 214   // elt may be NULL if it's a new klass for which we
 215   // could not allocate space for a new entry in the hashtable.
 216   if (elt != NULL) {
 217     elt->set_count(elt->count() + 1);
 218     elt->set_words(elt->words() + obj->size());
 219     _size_of_instances_in_words += obj->size();
 220     return true;
 221   } else {
 222     return false;
 223   }
 224 }
 225 
 226 void KlassInfoTable::iterate(KlassInfoClosure* cic) {
 227   assert(_buckets != NULL, "Allocation failure should have been caught");
 228   for (int index = 0; index < _num_buckets; index++) {
 229     _buckets[index].iterate(cic);
 230   }
 231 }
 232 
 233 size_t KlassInfoTable::size_of_instances_in_words() const {
 234   return _size_of_instances_in_words;
 235 }
 236 
 237 int KlassInfoHisto::sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2) {
 238   return (*e1)->compare(*e1,*e2);
 239 }
 240 
 241 KlassInfoHisto::KlassInfoHisto(KlassInfoTable* cit) :
 242   _cit(cit) {
 243   _elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<KlassInfoEntry*>(_histo_initial_size, true);
 244 }
 245 
 246 KlassInfoHisto::~KlassInfoHisto() {
 247   delete _elements;
 248 }
 249 
 250 void KlassInfoHisto::add(KlassInfoEntry* cie) {
 251   elements()->append(cie);
 252 }
 253 
 254 void KlassInfoHisto::sort() {
 255   elements()->sort(KlassInfoHisto::sort_helper);
 256 }
 257 
 258 void KlassInfoHisto::print_elements(outputStream* st) const {
 259   // simplify the formatting (ILP32 vs LP64) - store the sum in 64-bit
 260   int64_t total = 0;
 261   uint64_t totalw = 0;
 262   for(int i=0; i < elements()->length(); i++) {
 263     st->print("%4d: ", i+1);
 264     elements()->at(i)->print_on(st);
 265     total += elements()->at(i)->count();
 266     totalw += elements()->at(i)->words();
 267   }
 268   st->print_cr("Total " INT64_FORMAT_W(13) "  " UINT64_FORMAT_W(13),
 269                total, totalw * HeapWordSize);
 270 }
 271 
 272 class HierarchyClosure : public KlassInfoClosure {
 273 private:
 274   GrowableArray<KlassInfoEntry*> *_elements;
 275 public:
 276   HierarchyClosure(GrowableArray<KlassInfoEntry*> *_elements) : _elements(_elements) {}
 277 
 278   void do_cinfo(KlassInfoEntry* cie) {
 279     // ignore array classes
 280     if (cie->klass()->is_instance_klass()) {
 281       _elements->append(cie);
 282     }
 283   }
 284 };
 285 
 286 void KlassHierarchy::print_class_hierarchy(outputStream* st, bool print_interfaces,
 287                                            bool print_subclasses, char* classname) {
 288   ResourceMark rm;
 289   Stack <KlassInfoEntry*, mtClass> class_stack;
 290   GrowableArray<KlassInfoEntry*> elements;
 291 
 292   // Add all classes to the KlassInfoTable, which allows for quick lookup.
 293   // A KlassInfoEntry will be created for each class.
 294   KlassInfoTable cit(true);
 295   if (cit.allocation_failed()) {
 296     st->print_cr("ERROR: Ran out of C-heap; hierarchy not generated");
 297     return;
 298   }
 299 
 300   // Add all created KlassInfoEntry instances to the elements array for easy
 301   // iteration, and to allow each KlassInfoEntry instance to have a unique index.
 302   HierarchyClosure hc(&elements);
 303   cit.iterate(&hc);
 304 
 305   for(int i = 0; i < elements.length(); i++) {
 306     KlassInfoEntry* cie = elements.at(i);
 307     Klass* super = cie->klass()->super();
 308 
 309     // Set the index for the class.
 310     cie->set_index(i + 1);
 311 
 312     // Add the class to the subclass array of its superclass.
 313     if (super != NULL) {
 314       KlassInfoEntry* super_cie = cit.lookup(super);
 315       assert(super_cie != NULL, "could not lookup superclass");
 316       super_cie->add_subclass(cie);
 317     }
 318   }
 319 
 320   // Set the do_print flag for each class that should be printed.
 321   for(int i = 0; i < elements.length(); i++) {
 322     KlassInfoEntry* cie = elements.at(i);
 323     if (classname == NULL) {
 324       // We are printing all classes.
 325       cie->set_do_print(true);
 326     } else {
 327       // We are only printing the hierarchy of a specific class.
 328       if (strcmp(classname, cie->klass()->external_name()) == 0) {
 329         KlassHierarchy::set_do_print_for_class_hierarchy(cie, &cit, print_subclasses);
 330       }
 331     }
 332   }
 333 
 334   // Now we do a depth first traversal of the class hierachry. The class_stack will
 335   // maintain the list of classes we still need to process. Start things off
 336   // by priming it with java.lang.Object.
 337   KlassInfoEntry* jlo_cie = cit.lookup(SystemDictionary::Object_klass());
 338   assert(jlo_cie != NULL, "could not lookup java.lang.Object");
 339   class_stack.push(jlo_cie);
 340 
 341   // Repeatedly pop the top item off the stack, print its class info,
 342   // and push all of its subclasses on to the stack. Do this until there
 343   // are no classes left on the stack.
 344   while (!class_stack.is_empty()) {
 345     KlassInfoEntry* curr_cie = class_stack.pop();
 346     if (curr_cie->do_print()) {
 347       print_class(st, curr_cie, print_interfaces);
 348       if (curr_cie->subclasses() != NULL) {
 349         // Current class has subclasses, so push all of them onto the stack.
 350         for (int i = 0; i < curr_cie->subclasses()->length(); i++) {
 351           KlassInfoEntry* cie = curr_cie->subclasses()->at(i);
 352           if (cie->do_print()) {
 353             class_stack.push(cie);
 354           }
 355         }
 356       }
 357     }
 358   }
 359 
 360   st->flush();
 361 }
 362 
 363 // Sets the do_print flag for every superclass and subclass of the specified class.
 364 void KlassHierarchy::set_do_print_for_class_hierarchy(KlassInfoEntry* cie, KlassInfoTable* cit,
 365                                                       bool print_subclasses) {
 366   // Set do_print for all superclasses of this class.
 367   Klass* super = ((InstanceKlass*)cie->klass())->java_super();
 368   while (super != NULL) {
 369     KlassInfoEntry* super_cie = cit->lookup(super);
 370     super_cie->set_do_print(true);
 371     super = super->super();
 372   }
 373 
 374   // Set do_print for this class and all of its subclasses.
 375   Stack <KlassInfoEntry*, mtClass> class_stack;
 376   class_stack.push(cie);
 377   while (!class_stack.is_empty()) {
 378     KlassInfoEntry* curr_cie = class_stack.pop();
 379     curr_cie->set_do_print(true);
 380     if (print_subclasses && curr_cie->subclasses() != NULL) {
 381       // Current class has subclasses, so push all of them onto the stack.
 382       for (int i = 0; i < curr_cie->subclasses()->length(); i++) {
 383         KlassInfoEntry* cie = curr_cie->subclasses()->at(i);
 384         class_stack.push(cie);
 385       }
 386     }
 387   }
 388 }
 389 
 390 static void print_indent(outputStream* st, int indent) {
 391   while (indent != 0) {
 392     st->print("|");
 393     indent--;
 394     if (indent != 0) {
 395       st->print("  ");
 396     }
 397   }
 398 }
 399 
 400 // Print the class name and its unique ClassLoader identifer.
 401 static void print_classname(outputStream* st, Klass* klass) {
 402   oop loader_oop = klass->class_loader_data()->class_loader();
 403   st->print("%s/", klass->external_name());
 404   if (loader_oop == NULL) {
 405     st->print("null");
 406   } else {
 407     st->print(INTPTR_FORMAT, p2i(klass->class_loader_data()));
 408   }
 409 }
 410 
 411 static void print_interface(outputStream* st, InstanceKlass* intf_klass, const char* intf_type, int indent) {
 412   print_indent(st, indent);
 413   st->print("  implements ");
 414   print_classname(st, intf_klass);
 415   st->print(" (%s intf)\n", intf_type);
 416 }
 417 
 418 void KlassHierarchy::print_class(outputStream* st, KlassInfoEntry* cie, bool print_interfaces) {
 419   ResourceMark rm;
 420   InstanceKlass* klass = (InstanceKlass*)cie->klass();
 421   int indent = 0;
 422 
 423   // Print indentation with proper indicators of superclass.
 424   Klass* super = klass->super();
 425   while (super != NULL) {
 426     super = super->super();
 427     indent++;
 428   }
 429   print_indent(st, indent);
 430   if (indent != 0) st->print("--");
 431 
 432   // Print the class name, its unique ClassLoader identifer, and if it is an interface.
 433   print_classname(st, klass);
 434   if (klass->is_interface()) {
 435     st->print(" (intf)");
 436   }
 437   // Special treatment for generated core reflection accessor classes: print invocation target.
 438   if (ReflectionAccessorImplKlassHelper::is_generated_accessor(klass)) {
 439     st->print(" (invokes: ");
 440     ReflectionAccessorImplKlassHelper::print_invocation_target(st, klass);
 441     st->print(")");
 442   }
 443   st->print("\n");
 444 
 445   // Print any interfaces the class has.
 446   if (print_interfaces) {
 447     Array<InstanceKlass*>* local_intfs = klass->local_interfaces();
 448     Array<InstanceKlass*>* trans_intfs = klass->transitive_interfaces();
 449     for (int i = 0; i < local_intfs->length(); i++) {
 450       print_interface(st, local_intfs->at(i), "declared", indent);
 451     }
 452     for (int i = 0; i < trans_intfs->length(); i++) {
 453       InstanceKlass* trans_interface = trans_intfs->at(i);
 454       // Only print transitive interfaces if they are not also declared.
 455       if (!local_intfs->contains(trans_interface)) {
 456         print_interface(st, trans_interface, "inherited", indent);
 457       }
 458     }
 459   }
 460 }
 461 
 462 void KlassInfoHisto::print_histo_on(outputStream* st) {
 463   st->print_cr(" num     #instances         #bytes  class name (module)");
 464   st->print_cr("-------------------------------------------------------");
 465   print_elements(st);
 466 }
 467 
 468 class HistoClosure : public KlassInfoClosure {
 469  private:
 470   KlassInfoHisto* _cih;
 471  public:
 472   HistoClosure(KlassInfoHisto* cih) : _cih(cih) {}
 473 
 474   void do_cinfo(KlassInfoEntry* cie) {
 475     _cih->add(cie);
 476   }
 477 };
 478 
 479 class RecordInstanceClosure : public ObjectClosure {
 480  private:
 481   KlassInfoTable* _cit;
 482   size_t _missed_count;
 483   BoolObjectClosure* _filter;
 484  public:
 485   RecordInstanceClosure(KlassInfoTable* cit, BoolObjectClosure* filter) :
 486     _cit(cit), _missed_count(0), _filter(filter) {}
 487 
 488   void do_object(oop obj) {
 489     if (should_visit(obj)) {
 490       if (!_cit->record_instance(obj)) {
 491         _missed_count++;
 492       }
 493     }
 494   }
 495 
 496   size_t missed_count() { return _missed_count; }
 497 
 498  private:
 499   bool should_visit(oop obj) {
 500     return _filter == NULL || _filter->do_object_b(obj);
 501   }
 502 };
 503 
 504 size_t HeapInspection::populate_table(KlassInfoTable* cit, BoolObjectClosure *filter) {
 505   ResourceMark rm;
 506 
 507   RecordInstanceClosure ric(cit, filter);
 508   Universe::heap()->object_iterate(&ric);
 509   return ric.missed_count();
 510 }
 511 
 512 void HeapInspection::heap_inspection(outputStream* st) {
 513   ResourceMark rm;
 514 
 515   KlassInfoTable cit(true);
 516   if (!cit.allocation_failed()) {
 517     // populate table with object allocation info
 518     size_t missed_count = populate_table(&cit);
 519     if (missed_count != 0) {
 520       st->print_cr("WARNING: Ran out of C-heap; undercounted " SIZE_FORMAT
 521                    " total instances in data below",
 522                    missed_count);
 523     }
 524 
 525     // Sort and print klass instance info
 526     KlassInfoHisto histo(&cit);
 527     HistoClosure hc(&histo);
 528 
 529     cit.iterate(&hc);
 530 
 531     histo.sort();
 532     histo.print_histo_on(st);
 533   } else {
 534     st->print_cr("ERROR: Ran out of C-heap; histogram not generated");
 535   }
 536   st->flush();
 537 }
 538 
 539 class FindInstanceClosure : public ObjectClosure {
 540  private:
 541   Klass* _klass;
 542   GrowableArray<oop>* _result;
 543 
 544  public:
 545   FindInstanceClosure(Klass* k, GrowableArray<oop>* result) : _klass(k), _result(result) {};
 546 
 547   void do_object(oop obj) {
 548     if (obj->is_a(_klass)) {
 549       // obj was read with AS_NO_KEEPALIVE, or equivalent.
 550       // The object needs to be kept alive when it is published.
 551       Universe::heap()->keep_alive(obj);
 552 
 553       _result->append(obj);
 554     }
 555   }
 556 };
 557 
 558 void HeapInspection::find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) {
 559   assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
 560   assert(Heap_lock->is_locked(), "should have the Heap_lock");
 561 
 562   // Ensure that the heap is parsable
 563   Universe::heap()->ensure_parsability(false);  // no need to retire TALBs
 564 
 565   // Iterate over objects in the heap
 566   FindInstanceClosure fic(k, result);
 567   Universe::heap()->object_iterate(&fic);
 568 }