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 int KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) { 43 if(e1->_instance_words > e2->_instance_words) { 44 return -1; 45 } else if(e1->_instance_words < e2->_instance_words) { 46 return 1; 47 } 48 // Sort alphabetically, note 'Z' < '[' < 'a', but it's better to group 49 // the array classes before all the instance classes. 50 ResourceMark rm; 51 const char* name1 = e1->klass()->external_name(); 52 const char* name2 = e2->klass()->external_name(); 53 bool d1 = (name1[0] == '['); 54 bool d2 = (name2[0] == '['); 55 if (d1 && !d2) { 56 return -1; 57 } else if (d2 && !d1) { 58 return 1; 59 } else { 60 return strcmp(name1, name2); 61 } 296 if (is_selected("ClassLoader")) { 297 st->print(",ClassLoader"); 298 } 299 st->cr(); 300 } 301 302 class HierarchyClosure : public KlassInfoClosure { 303 private: 304 GrowableArray<KlassInfoEntry*> *_elements; 305 public: 306 HierarchyClosure(GrowableArray<KlassInfoEntry*> *_elements) : _elements(_elements) {} 307 308 void do_cinfo(KlassInfoEntry* cie) { 309 // ignore array classes 310 if (cie->klass()->oop_is_instance()) { 311 _elements->append(cie); 312 } 313 } 314 }; 315 316 void KlassHierarchy::print_class_hierarchy(outputStream* st) { 317 ResourceMark rm; 318 int i; 319 Stack <KlassInfoEntry*, mtClass> class_stack; 320 Stack <KlassInfoEntry*, mtClass> super_stack; 321 GrowableArray<KlassInfoEntry*> elements; 322 323 // Add all classes to the KlassInfoTable, which allows for quick lookup. 324 // A KlassInfoEntry will be created for each class. 325 KlassInfoTable cit(true); 326 if (cit.allocation_failed()) { 327 st->print_cr("WARNING: Ran out of C-heap; hierarchy not generated"); 328 return; 329 } 330 331 // Add all created KlassInfoEntry instances to the elements array for easy 332 // iteration, and to allow each KlassInfoEntry instance to have a unique index. 333 HierarchyClosure hc(&elements); 334 cit.iterate(&hc); 335 336 // Set the index for each class 337 for(i=0; i < elements.length(); i++) { 338 elements.at(i)->set_index(i+1); 339 } 340 341 // Iterate over all the classes, adding each class to the subclass array of 342 // its superclass. 343 for(i=0; i < elements.length(); i++) { 344 KlassInfoEntry* e = (KlassInfoEntry*)elements.at(i); 345 const Klass* k = e->klass(); 346 Klass* super = ((InstanceKlass*)k)->java_super(); 347 if (super != NULL) { 348 KlassInfoEntry* super_e = cit.lookup(super); 349 assert(super_e != NULL, "could not lookup superclass"); 350 e->set_super_index(super_e->index()); 351 super_e->add_subclass(e); 352 } 353 } 354 355 // Now we do a depth first traversal of the class hierachry. The class_stack will 356 // maintain the list of classes we still need to process. Start things off 357 // by priming it with java.lang.Object. 358 KlassInfoEntry* jlo_cie = cit.lookup(SystemDictionary::Object_klass()); 359 assert(jlo_cie != NULL, "could not lookup java.lang.Object"); 360 class_stack.push(jlo_cie); 361 362 // Repeatedly pop the top item off the stack, print its class info, 363 // and push all of its subclasses on to the stack. Do this until there 364 // are no classes left on the stack. 365 // 366 // We also keep track of the stack of superclasses so we know 367 // all the current superclasses for the current class. This is how 368 // we determine the proper indentation when printing the class. 369 long curr_super_index = -1; 370 while (!class_stack.is_empty()) { 371 KlassInfoEntry* curr_cie = class_stack.pop(); 372 373 // Make sure super_stack is current with the class we just popped. 374 while (curr_cie->super_index() != curr_super_index) { 375 assert(!super_stack.is_empty(), "super_stack should not be empty"); 376 curr_super_index = super_stack.pop()->super_index(); 377 } 378 379 print_class(st, curr_cie, &super_stack); 380 381 if (curr_cie->subclasses() != NULL) { 382 // Current class has subclasses, so push all of them onto the stack 383 for (int i = 0; i < curr_cie->subclasses()->length(); i++) { 384 class_stack.push(curr_cie->subclasses()->at(i)); 385 } 386 // Add current class to superclass stack. 387 super_stack.push(curr_cie); 388 curr_super_index = curr_cie->index(); 389 } 390 } 391 392 st->flush(); 393 } 394 395 void KlassHierarchy::print_class(outputStream* st, KlassInfoEntry* cie, 396 Stack <KlassInfoEntry*, mtClass> *super_stack) { 397 ResourceMark rm; 398 399 // print indentation with proper indicators of superclass. 400 StackIterator<KlassInfoEntry*, mtClass> iter(*super_stack); 401 while (!iter.is_empty()) { 402 KlassInfoEntry* super_cie = iter.next(); 403 st->print("|"); 404 if (iter.is_empty()) { 405 st->print("--"); 406 } else { 407 st->print(" "); 408 } 409 } 410 411 // print the class name 412 st->print("%s\n", cie->name()); 413 } 414 415 void KlassInfoHisto::print_class_stats(outputStream* st, 416 bool csv_format, const char *columns) { 417 ResourceMark rm; 418 KlassSizeStats sz, sz_sum; 419 int i; 420 julong *col_table = (julong*)(&sz); 421 julong *colsum_table = (julong*)(&sz_sum); 422 int width_table[KlassSizeStats::_num_columns]; 423 bool selected[KlassSizeStats::_num_columns]; 424 425 _selected_columns = columns; 426 427 memset(&sz_sum, 0, sizeof(sz_sum)); 428 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 429 selected[c] = is_selected(name_table[c]); 430 } 431 432 for(i=0; i < elements()->length(); i++) { 433 elements()->at(i)->set_index(i+1); 434 } 435 436 // First iteration is for accumulating stats totals in colsum_table[]. 437 // Second iteration is for printing stats for each class. 438 for (int pass=1; pass<=2; pass++) { 439 if (pass == 2) { 440 print_title(st, csv_format, selected, width_table, name_table); 441 } 442 for(i=0; i < elements()->length(); i++) { 443 KlassInfoEntry* e = (KlassInfoEntry*)elements()->at(i); 444 const Klass* k = e->klass(); 445 446 // Get the stats for this class 447 memset(&sz, 0, sizeof(sz)); 448 sz._inst_count = e->count(); 449 sz._inst_bytes = HeapWordSize * e->words(); 450 k->collect_statistics(&sz); 451 sz._total_bytes = sz._ro_bytes + sz._rw_bytes; 452 453 if (pass == 1) { 454 // Add the stats for this class to the overall totals 455 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 456 colsum_table[c] += col_table[c]; 457 } 458 } else { 459 // Print the stats for this class. 460 if (k->oop_is_instance()) { 461 Klass* super = ((InstanceKlass*)k)->java_super(); 462 if (super) { 463 KlassInfoEntry* super_e = _cit->lookup(super); 464 if (super_e) { 465 e->set_super_index(super_e->index()); 466 } 467 } 468 } 469 470 if (csv_format) { 471 st->print("%d,%d", e->index(), e->super_index()); 472 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 473 if (selected[c]) {st->print("," JULONG_FORMAT, col_table[c]);} 474 } 475 st->print(",%s",e->name()); 476 } else { 477 st->print("%5d %5d", e->index(), e->super_index()); 478 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 479 if (selected[c]) {print_julong(st, width_table[c], col_table[c]);} 480 } 481 st->print(" %s", e->name()); 482 } 483 if (is_selected("ClassLoader")) { 484 ClassLoaderData* loader_data = k->class_loader_data(); 485 st->print(","); 486 loader_data->print_value_on(st); 487 } 488 st->cr(); 489 } 490 } 491 492 if (pass == 1) { 493 // Calculate the minimum width needed for the column by accounting for the 494 // column header width and the width of the largest value in the column. 495 for (int c=0; c<KlassSizeStats::_num_columns; c++) { 496 width_table[c] = col_width(colsum_table[c], name_table[c]); 497 } | 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 } 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 } |