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