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