1 /* 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. 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.hpp" 27 #include "gc_interface/collectedHeap.hpp" 28 #include "memory/genCollectedHeap.hpp" 29 #include "memory/heapInspection.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "runtime/os.hpp" 32 #include "utilities/globalDefinitions.hpp" 33 #include "utilities/macros.hpp" 34 #if INCLUDE_ALL_GCS 35 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp" 36 #endif // INCLUDE_ALL_GCS 37 38 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 39 40 // HeapInspection 41 42 inline KlassInfoEntry::~KlassInfoEntry() { 43 if (_subclasses != NULL) { 44 delete _subclasses; 45 } 46 } 47 48 inline void KlassInfoEntry::add_subclass(KlassInfoEntry* cie) { 49 if (_subclasses == NULL) { 50 _subclasses = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<KlassInfoEntry*>(4, true); 51 } 52 _subclasses->append(cie); 53 } 54 55 int KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) { 56 if(e1->_instance_words > e2->_instance_words) { 57 return -1; 58 } else if(e1->_instance_words < e2->_instance_words) { 59 return 1; 60 } 61 // Sort alphabetically, note 'Z' < '[' < 'a', but it's better to group 62 // the array classes before all the instance classes. 63 ResourceMark rm; 64 const char* name1 = e1->klass()->external_name(); 65 const char* name2 = e2->klass()->external_name(); 66 bool d1 = (name1[0] == '['); 67 bool d2 = (name2[0] == '['); 68 if (d1 && !d2) { 69 return -1; 70 } else if (d2 && !d1) { 71 return 1; 72 } else { 73 return strcmp(name1, name2); 74 } 75 } 76 77 const char* KlassInfoEntry::name() const { 78 const char* name; 79 if (_klass->name() != NULL) { 80 name = _klass->external_name(); 81 } else { 82 if (_klass == Universe::boolArrayKlassObj()) name = "<boolArrayKlass>"; else 83 if (_klass == Universe::charArrayKlassObj()) name = "<charArrayKlass>"; else 84 if (_klass == Universe::singleArrayKlassObj()) name = "<singleArrayKlass>"; else 85 if (_klass == Universe::doubleArrayKlassObj()) name = "<doubleArrayKlass>"; else 86 if (_klass == Universe::byteArrayKlassObj()) name = "<byteArrayKlass>"; else 87 if (_klass == Universe::shortArrayKlassObj()) name = "<shortArrayKlass>"; else 88 if (_klass == Universe::intArrayKlassObj()) name = "<intArrayKlass>"; else 89 if (_klass == Universe::longArrayKlassObj()) name = "<longArrayKlass>"; else 90 name = "<no name>"; 91 } 92 return name; 93 } 94 95 void KlassInfoEntry::print_on(outputStream* st) const { 96 ResourceMark rm; 97 98 // simplify the formatting (ILP32 vs LP64) - always cast the numbers to 64-bit 99 st->print_cr(INT64_FORMAT_W(13) " " UINT64_FORMAT_W(13) " %s", 100 (jlong) _instance_count, 101 (julong) _instance_words * HeapWordSize, 102 name()); 103 } 104 105 KlassInfoEntry* KlassInfoBucket::lookup(Klass* const k) { 106 KlassInfoEntry* elt = _list; 107 while (elt != NULL) { 108 if (elt->is_equal(k)) { 109 return elt; 110 } 111 elt = elt->next(); 112 } 113 elt = new (std::nothrow) KlassInfoEntry(k, list()); 114 // We may be out of space to allocate the new entry. 115 if (elt != NULL) { 116 set_list(elt); 117 } 118 return elt; 119 } 120 121 void KlassInfoBucket::iterate(KlassInfoClosure* cic) { 122 KlassInfoEntry* elt = _list; 123 while (elt != NULL) { 124 cic->do_cinfo(elt); 125 elt = elt->next(); 126 } 127 } 128 129 void KlassInfoBucket::empty() { 130 KlassInfoEntry* elt = _list; 131 _list = NULL; 132 while (elt != NULL) { 133 KlassInfoEntry* next = elt->next(); 134 delete elt; 135 elt = next; 136 } 137 } 138 139 void KlassInfoTable::AllClassesFinder::do_klass(Klass* k) { 140 // This has the SIDE EFFECT of creating a KlassInfoEntry 141 // for <k>, if one doesn't exist yet. 142 _table->lookup(k); 143 } 144 145 KlassInfoTable::KlassInfoTable(bool add_all_classes) { 146 _size_of_instances_in_words = 0; 147 _size = 0; 148 _ref = (HeapWord*) Universe::boolArrayKlassObj(); 149 _buckets = 150 (KlassInfoBucket*) AllocateHeap(sizeof(KlassInfoBucket) * _num_buckets, 151 mtInternal, CURRENT_PC, AllocFailStrategy::RETURN_NULL); 152 if (_buckets != NULL) { 153 _size = _num_buckets; 154 for (int index = 0; index < _size; index++) { 155 _buckets[index].initialize(); 156 } 157 if (add_all_classes) { 158 AllClassesFinder finder(this); 159 ClassLoaderDataGraph::classes_do(&finder); 160 } 161 } 162 } 163 164 KlassInfoTable::~KlassInfoTable() { 165 if (_buckets != NULL) { 166 for (int index = 0; index < _size; index++) { 167 _buckets[index].empty(); 168 } 169 FREE_C_HEAP_ARRAY(KlassInfoBucket, _buckets); 170 _size = 0; 171 } 172 } 173 174 uint KlassInfoTable::hash(const Klass* p) { 175 return (uint)(((uintptr_t)p - (uintptr_t)_ref) >> 2); 176 } 177 178 KlassInfoEntry* KlassInfoTable::lookup(Klass* k) { 179 uint idx = hash(k) % _size; 180 assert(_buckets != NULL, "Allocation failure should have been caught"); 181 KlassInfoEntry* e = _buckets[idx].lookup(k); 182 // Lookup may fail if this is a new klass for which we 183 // could not allocate space for an new entry. 184 assert(e == NULL || k == e->klass(), "must be equal"); 185 return e; 186 } 187 188 // Return false if the entry could not be recorded on account 189 // of running out of space required to create a new entry. 190 bool KlassInfoTable::record_instance(const oop obj) { 191 Klass* k = obj->klass(); 192 KlassInfoEntry* elt = lookup(k); 193 // elt may be NULL if it's a new klass for which we 194 // could not allocate space for a new entry in the hashtable. 195 if (elt != NULL) { 196 elt->set_count(elt->count() + 1); 197 elt->set_words(elt->words() + obj->size()); 198 _size_of_instances_in_words += obj->size(); 199 return true; 200 } else { 201 return false; 202 } 203 } 204 205 void KlassInfoTable::iterate(KlassInfoClosure* cic) { 206 assert(_size == 0 || _buckets != NULL, "Allocation failure should have been caught"); 207 for (int index = 0; index < _size; index++) { 208 _buckets[index].iterate(cic); 209 } 210 } 211 212 size_t KlassInfoTable::size_of_instances_in_words() const { 213 return _size_of_instances_in_words; 214 } 215 216 int KlassInfoHisto::sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2) { 217 return (*e1)->compare(*e1,*e2); 218 } 219 220 KlassInfoHisto::KlassInfoHisto(KlassInfoTable* cit, const char* title) : 221 _cit(cit), 222 _title(title) { 223 _elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<KlassInfoEntry*>(_histo_initial_size, true); 224 } 225 226 KlassInfoHisto::~KlassInfoHisto() { 227 delete _elements; 228 } 229 230 void KlassInfoHisto::add(KlassInfoEntry* cie) { 231 elements()->append(cie); 232 } 233 234 void KlassInfoHisto::sort() { 235 elements()->sort(KlassInfoHisto::sort_helper); 236 } 237 238 void KlassInfoHisto::print_elements(outputStream* st) const { 239 // simplify the formatting (ILP32 vs LP64) - store the sum in 64-bit 240 jlong total = 0; 241 julong totalw = 0; 242 for(int i=0; i < elements()->length(); i++) { 243 st->print("%4d: ", i+1); 244 elements()->at(i)->print_on(st); 245 total += elements()->at(i)->count(); 246 totalw += elements()->at(i)->words(); 247 } 248 st->print_cr("Total " INT64_FORMAT_W(13) " " UINT64_FORMAT_W(13), 249 total, totalw * HeapWordSize); 250 } 251 252 #define MAKE_COL_NAME(field, name, help) #name, 253 #define MAKE_COL_HELP(field, name, help) help, 254 255 static const char *name_table[] = { 256 HEAP_INSPECTION_COLUMNS_DO(MAKE_COL_NAME) 257 }; 258 259 static const char *help_table[] = { 260 HEAP_INSPECTION_COLUMNS_DO(MAKE_COL_HELP) 261 }; 262 263 bool KlassInfoHisto::is_selected(const char *col_name) { 264 if (_selected_columns == NULL) { 265 return true; 266 } 267 if (strcmp(_selected_columns, col_name) == 0) { 268 return true; 269 } 270 271 const char *start = strstr(_selected_columns, col_name); 272 if (start == NULL) { 273 return false; 274 } 275 276 // The following must be true, because _selected_columns != col_name 277 if (start > _selected_columns && start[-1] != ',') { 278 return false; 279 } 280 char x = start[strlen(col_name)]; 281 if (x != ',' && x != '\0') { 282 return false; 283 } 284 285 return true; 286 } 287 288 PRAGMA_FORMAT_NONLITERAL_IGNORED_EXTERNAL 289 void KlassInfoHisto::print_title(outputStream* st, bool csv_format, 290 bool selected[], int width_table[], 291 const char *name_table[]) { 292 if (csv_format) { 293 st->print("Index,Super"); 294 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 295 if (selected[c]) {st->print(",%s", name_table[c]);} 296 } 297 st->print(",ClassName"); 298 } else { 299 st->print("Index Super"); 300 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 301 PRAGMA_DIAG_PUSH 302 PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL 303 if (selected[c]) {st->print(str_fmt(width_table[c]), name_table[c]);} 304 PRAGMA_DIAG_POP 305 } 306 st->print(" ClassName"); 307 } 308 309 if (is_selected("ClassLoader")) { 310 st->print(",ClassLoader"); 311 } 312 st->cr(); 313 } 314 315 class HierarchyClosure : public KlassInfoClosure { 316 private: 317 GrowableArray<KlassInfoEntry*> *_elements; 318 public: 319 HierarchyClosure(GrowableArray<KlassInfoEntry*> *_elements) : _elements(_elements) {} 320 321 void do_cinfo(KlassInfoEntry* cie) { 322 // ignore array classes 323 if (cie->klass()->oop_is_instance()) { 324 _elements->append(cie); 325 } 326 } 327 }; 328 329 void KlassHierarchy::print_class_hierarchy(outputStream* st, bool print_interfaces, 330 bool print_subclasses, char* classname) { 331 ResourceMark rm; 332 Stack <KlassInfoEntry*, mtClass> class_stack; 333 GrowableArray<KlassInfoEntry*> elements; 334 335 // Add all classes to the KlassInfoTable, which allows for quick lookup. 336 // A KlassInfoEntry will be created for each class. 337 KlassInfoTable cit(true); 338 if (cit.allocation_failed()) { 339 st->print_cr("WARNING: Ran out of C-heap; hierarchy not generated"); 340 return; 341 } 342 343 // Add all created KlassInfoEntry instances to the elements array for easy 344 // iteration, and to allow each KlassInfoEntry instance to have a unique index. 345 HierarchyClosure hc(&elements); 346 cit.iterate(&hc); 347 348 for(int i = 0; i < elements.length(); i++) { 349 KlassInfoEntry* cie = elements.at(i); 350 const InstanceKlass* k = (InstanceKlass*)cie->klass(); 351 Klass* super = ((InstanceKlass*)k)->java_super(); 352 353 // Set the index for the class. 354 cie->set_index(i + 1); 355 356 // Add the class to the subclass array of its superclass. 357 if (super != NULL) { 358 KlassInfoEntry* super_cie = cit.lookup(super); 359 assert(super_cie != NULL, "could not lookup superclass"); 360 super_cie->add_subclass(cie); 361 } 362 } 363 364 // Set the do_print flag for each class that should be printed. 365 for(int i = 0; i < elements.length(); i++) { 366 KlassInfoEntry* cie = elements.at(i); 367 if (classname == NULL) { 368 // We are printing all classes. 369 cie->set_do_print(true); 370 } else { 371 // We are only printing the hierarchy of a specific class. 372 if (strcmp(classname, cie->klass()->external_name()) == 0) { 373 KlassHierarchy::set_do_print_for_class_hierarchy(cie, &cit, print_subclasses); 374 } 375 } 376 } 377 378 // Now we do a depth first traversal of the class hierachry. The class_stack will 379 // maintain the list of classes we still need to process. Start things off 380 // by priming it with java.lang.Object. 381 KlassInfoEntry* jlo_cie = cit.lookup(SystemDictionary::Object_klass()); 382 assert(jlo_cie != NULL, "could not lookup java.lang.Object"); 383 class_stack.push(jlo_cie); 384 385 // Repeatedly pop the top item off the stack, print its class info, 386 // and push all of its subclasses on to the stack. Do this until there 387 // are no classes left on the stack. 388 while (!class_stack.is_empty()) { 389 KlassInfoEntry* curr_cie = class_stack.pop(); 390 if (curr_cie->do_print()) { 391 print_class(st, curr_cie, print_interfaces); 392 if (curr_cie->subclasses() != NULL) { 393 // Current class has subclasses, so push all of them onto the stack. 394 for (int i = 0; i < curr_cie->subclasses()->length(); i++) { 395 KlassInfoEntry* cie = curr_cie->subclasses()->at(i); 396 if (cie->do_print()) { 397 class_stack.push(cie); 398 } 399 } 400 } 401 } 402 } 403 404 st->flush(); 405 } 406 407 // Sets the do_print flag for every superclass and subclass of the specified class. 408 void KlassHierarchy::set_do_print_for_class_hierarchy(KlassInfoEntry* cie, KlassInfoTable* cit, 409 bool print_subclasses) { 410 // Set do_print for all superclasses of this class. 411 Klass* super = ((InstanceKlass*)cie->klass())->java_super(); 412 while (super != NULL) { 413 KlassInfoEntry* super_cie = cit->lookup(super); 414 super_cie->set_do_print(true); 415 super = super->super(); 416 } 417 418 // Set do_print for this class and all of its subclasses. 419 Stack <KlassInfoEntry*, mtClass> class_stack; 420 class_stack.push(cie); 421 while (!class_stack.is_empty()) { 422 KlassInfoEntry* curr_cie = class_stack.pop(); 423 curr_cie->set_do_print(true); 424 if (print_subclasses && curr_cie->subclasses() != NULL) { 425 // Current class has subclasses, so push all of them onto the stack. 426 for (int i = 0; i < curr_cie->subclasses()->length(); i++) { 427 KlassInfoEntry* cie = curr_cie->subclasses()->at(i); 428 class_stack.push(cie); 429 } 430 } 431 } 432 } 433 434 static void print_indent(outputStream* st, int indent) { 435 while (indent != 0) { 436 st->print("|"); 437 indent--; 438 if (indent != 0) { 439 st->print(" "); 440 } 441 } 442 } 443 444 // Print the class name and its unique ClassLoader identifer. 445 static void print_classname(outputStream* st, Klass* klass) { 446 oop loader_oop = klass->class_loader_data()->class_loader(); 447 st->print("%s/", klass->external_name()); 448 if (loader_oop == NULL) { 449 st->print("null"); 450 } else { 451 st->print(INTPTR_FORMAT, loader_oop->klass()); 452 } 453 } 454 455 static void print_interface(outputStream* st, Klass* intf_klass, const char* intf_type, int indent) { 456 print_indent(st, indent); 457 st->print(" implements "); 458 print_classname(st, intf_klass); 459 st->print(" (%s intf)\n", intf_type); 460 } 461 462 void KlassHierarchy::print_class(outputStream* st, KlassInfoEntry* cie, bool print_interfaces) { 463 ResourceMark rm; 464 InstanceKlass* klass = (InstanceKlass*)cie->klass(); 465 int indent = 0; 466 467 // Print indentation with proper indicators of superclass. 468 Klass* super = klass->super(); 469 while (super != NULL) { 470 super = super->super(); 471 indent++; 472 } 473 print_indent(st, indent); 474 if (indent != 0) st->print("--"); 475 476 // Print the class name, its unique ClassLoader identifer, and if it is an interface. 477 print_classname(st, klass); 478 if (klass->is_interface()) { 479 st->print(" (intf)"); 480 } 481 st->print("\n"); 482 483 // Print any interfaces the class has. 484 if (print_interfaces) { 485 Array<Klass*>* local_intfs = klass->local_interfaces(); 486 Array<Klass*>* trans_intfs = klass->transitive_interfaces(); 487 for (int i = 0; i < local_intfs->length(); i++) { 488 print_interface(st, local_intfs->at(i), "declared", indent); 489 } 490 for (int i = 0; i < trans_intfs->length(); i++) { 491 Klass* trans_interface = trans_intfs->at(i); 492 // Only print transitive interfaces if they are not also declared. 493 if (!local_intfs->contains(trans_interface)) { 494 print_interface(st, trans_interface, "transitive", indent); 495 } 496 } 497 } 498 } 499 500 void KlassInfoHisto::print_class_stats(outputStream* st, 501 bool csv_format, const char *columns) { 502 KlassSizeStats sz, sz_sum; 503 int i; 504 julong *col_table = (julong*)(&sz); 505 julong *colsum_table = (julong*)(&sz_sum); 506 int width_table[KlassSizeStats::_num_columns]; 507 bool selected[KlassSizeStats::_num_columns]; 508 509 _selected_columns = columns; 510 511 memset(&sz_sum, 0, sizeof(sz_sum)); 512 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 513 selected[c] = is_selected(name_table[c]); 514 } 515 516 for(i=0; i < elements()->length(); i++) { 517 elements()->at(i)->set_index(i+1); 518 } 519 520 // First iteration is for accumulating stats totals in colsum_table[]. 521 // Second iteration is for printing stats for each class. 522 for (int pass=1; pass<=2; pass++) { 523 if (pass == 2) { 524 print_title(st, csv_format, selected, width_table, name_table); 525 } 526 for(i=0; i < elements()->length(); i++) { 527 KlassInfoEntry* e = (KlassInfoEntry*)elements()->at(i); 528 const Klass* k = e->klass(); 529 530 // Get the stats for this class. 531 memset(&sz, 0, sizeof(sz)); 532 sz._inst_count = e->count(); 533 sz._inst_bytes = HeapWordSize * e->words(); 534 k->collect_statistics(&sz); 535 sz._total_bytes = sz._ro_bytes + sz._rw_bytes; 536 537 if (pass == 1) { 538 // Add the stats for this class to the overall totals. 539 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 540 colsum_table[c] += col_table[c]; 541 } 542 } else { 543 int super_index = -1; 544 // Print the stats for this class. 545 if (k->oop_is_instance()) { 546 Klass* super = ((InstanceKlass*)k)->java_super(); 547 if (super) { 548 KlassInfoEntry* super_e = _cit->lookup(super); 549 if (super_e) { 550 super_index = super_e->index(); 551 } 552 } 553 } 554 555 if (csv_format) { 556 st->print("%d,%d", e->index(), super_index); 557 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 558 if (selected[c]) {st->print("," JULONG_FORMAT, col_table[c]);} 559 } 560 st->print(",%s",e->name()); 561 } else { 562 st->print("%5d %5d", e->index(), super_index); 563 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 564 if (selected[c]) {print_julong(st, width_table[c], col_table[c]);} 565 } 566 st->print(" %s", e->name()); 567 } 568 if (is_selected("ClassLoader")) { 569 ClassLoaderData* loader_data = k->class_loader_data(); 570 st->print(","); 571 loader_data->print_value_on(st); 572 } 573 st->cr(); 574 } 575 } 576 577 if (pass == 1) { 578 // Calculate the minimum width needed for the column by accounting for the 579 // column header width and the width of the largest value in the column. 580 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 581 width_table[c] = col_width(colsum_table[c], name_table[c]); 582 } 583 } 584 } 585 586 sz_sum._inst_size = 0; 587 588 // Print the column totals. 589 if (csv_format) { 590 st->print(","); 591 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 592 if (selected[c]) {st->print("," JULONG_FORMAT, colsum_table[c]);} 593 } 594 } else { 595 st->print(" "); 596 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 597 if (selected[c]) {print_julong(st, width_table[c], colsum_table[c]);} 598 } 599 st->print(" Total"); 600 if (sz_sum._total_bytes > 0) { 601 st->cr(); 602 st->print(" "); 603 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 604 if (selected[c]) { 605 switch (c) { 606 case KlassSizeStats::_index_inst_size: 607 case KlassSizeStats::_index_inst_count: 608 case KlassSizeStats::_index_method_count: 609 PRAGMA_DIAG_PUSH 610 PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL 611 st->print(str_fmt(width_table[c]), "-"); 612 PRAGMA_DIAG_POP 613 break; 614 default: 615 { 616 double perc = (double)(100) * (double)(colsum_table[c]) / (double)sz_sum._total_bytes; 617 PRAGMA_DIAG_PUSH 618 PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL 619 st->print(perc_fmt(width_table[c]), perc); 620 PRAGMA_DIAG_POP 621 } 622 } 623 } 624 } 625 } 626 } 627 st->cr(); 628 629 if (!csv_format) { 630 print_title(st, csv_format, selected, width_table, name_table); 631 } 632 } 633 634 julong KlassInfoHisto::annotations_bytes(Array<AnnotationArray*>* p) const { 635 julong bytes = 0; 636 if (p != NULL) { 637 for (int i = 0; i < p->length(); i++) { 638 bytes += count_bytes_array(p->at(i)); 639 } 640 bytes += count_bytes_array(p); 641 } 642 return bytes; 643 } 644 645 void KlassInfoHisto::print_histo_on(outputStream* st, bool print_stats, 646 bool csv_format, const char *columns) { 647 if (print_stats) { 648 print_class_stats(st, csv_format, columns); 649 } else { 650 st->print_cr("%s",title()); 651 print_elements(st); 652 } 653 } 654 655 class HistoClosure : public KlassInfoClosure { 656 private: 657 KlassInfoHisto* _cih; 658 public: 659 HistoClosure(KlassInfoHisto* cih) : _cih(cih) {} 660 661 void do_cinfo(KlassInfoEntry* cie) { 662 _cih->add(cie); 663 } 664 }; 665 666 class RecordInstanceClosure : public ObjectClosure { 667 private: 668 KlassInfoTable* _cit; 669 size_t _missed_count; 670 BoolObjectClosure* _filter; 671 public: 672 RecordInstanceClosure(KlassInfoTable* cit, BoolObjectClosure* filter) : 673 _cit(cit), _missed_count(0), _filter(filter) {} 674 675 void do_object(oop obj) { 676 if (should_visit(obj)) { 677 if (!_cit->record_instance(obj)) { 678 _missed_count++; 679 } 680 } 681 } 682 683 size_t missed_count() { return _missed_count; } 684 685 private: 686 bool should_visit(oop obj) { 687 return _filter == NULL || _filter->do_object_b(obj); 688 } 689 }; 690 691 size_t HeapInspection::populate_table(KlassInfoTable* cit, BoolObjectClosure *filter) { 692 ResourceMark rm; 693 694 RecordInstanceClosure ric(cit, filter); 695 Universe::heap()->object_iterate(&ric); 696 return ric.missed_count(); 697 } 698 699 void HeapInspection::heap_inspection(outputStream* st) { 700 ResourceMark rm; 701 702 if (_print_help) { 703 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 704 st->print("%s:\n\t", name_table[c]); 705 const int max_col = 60; 706 int col = 0; 707 for (const char *p = help_table[c]; *p; p++,col++) { 708 if (col >= max_col && *p == ' ') { 709 st->print("\n\t"); 710 col = 0; 711 } else { 712 st->print("%c", *p); 713 } 714 } 715 st->print_cr(".\n"); 716 } 717 return; 718 } 719 720 KlassInfoTable cit(_print_class_stats); 721 if (!cit.allocation_failed()) { 722 // populate table with object allocation info 723 size_t missed_count = populate_table(&cit); 724 if (missed_count != 0) { 725 st->print_cr("WARNING: Ran out of C-heap; undercounted " SIZE_FORMAT 726 " total instances in data below", 727 missed_count); 728 } 729 730 // Sort and print klass instance info 731 const char *title = "\n" 732 " num #instances #bytes class name\n" 733 "----------------------------------------------"; 734 KlassInfoHisto histo(&cit, title); 735 HistoClosure hc(&histo); 736 737 cit.iterate(&hc); 738 739 histo.sort(); 740 histo.print_histo_on(st, _print_class_stats, _csv_format, _columns); 741 } else { 742 st->print_cr("WARNING: Ran out of C-heap; histogram not generated"); 743 } 744 st->flush(); 745 } 746 747 class FindInstanceClosure : public ObjectClosure { 748 private: 749 Klass* _klass; 750 GrowableArray<oop>* _result; 751 752 public: 753 FindInstanceClosure(Klass* k, GrowableArray<oop>* result) : _klass(k), _result(result) {}; 754 755 void do_object(oop obj) { 756 if (obj->is_a(_klass)) { 757 _result->append(obj); 758 } 759 } 760 }; 761 762 void HeapInspection::find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) { 763 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped"); 764 assert(Heap_lock->is_locked(), "should have the Heap_lock"); 765 766 // Ensure that the heap is parsable 767 Universe::heap()->ensure_parsability(false); // no need to retire TALBs 768 769 // Iterate over objects in the heap 770 FindInstanceClosure fic(k, result); 771 // If this operation encounters a bad object when using CMS, 772 // consider using safe_object_iterate() which avoids metadata 773 // objects that may contain bad references. 774 Universe::heap()->object_iterate(&fic); 775 }