aoqi@0: /* aoqi@0: * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/classLoaderData.hpp" aoqi@0: #include "gc_interface/collectedHeap.hpp" aoqi@0: #include "memory/genCollectedHeap.hpp" aoqi@0: #include "memory/heapInspection.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "runtime/os.hpp" aoqi@0: #include "utilities/globalDefinitions.hpp" aoqi@0: #include "utilities/macros.hpp" aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp" aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: // HeapInspection aoqi@0: aoqi@0: int KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) { aoqi@0: if(e1->_instance_words > e2->_instance_words) { aoqi@0: return -1; aoqi@0: } else if(e1->_instance_words < e2->_instance_words) { aoqi@0: return 1; aoqi@0: } aoqi@0: // Sort alphabetically, note 'Z' < '[' < 'a', but it's better to group aoqi@0: // the array classes before all the instance classes. aoqi@0: ResourceMark rm; aoqi@0: const char* name1 = e1->klass()->external_name(); aoqi@0: const char* name2 = e2->klass()->external_name(); aoqi@0: bool d1 = (name1[0] == '['); aoqi@0: bool d2 = (name2[0] == '['); aoqi@0: if (d1 && !d2) { aoqi@0: return -1; aoqi@0: } else if (d2 && !d1) { aoqi@0: return 1; aoqi@0: } else { aoqi@0: return strcmp(name1, name2); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: const char* KlassInfoEntry::name() const { aoqi@0: const char* name; aoqi@0: if (_klass->name() != NULL) { aoqi@0: name = _klass->external_name(); aoqi@0: } else { aoqi@0: if (_klass == Universe::boolArrayKlassObj()) name = ""; else aoqi@0: if (_klass == Universe::charArrayKlassObj()) name = ""; else aoqi@0: if (_klass == Universe::singleArrayKlassObj()) name = ""; else aoqi@0: if (_klass == Universe::doubleArrayKlassObj()) name = ""; else aoqi@0: if (_klass == Universe::byteArrayKlassObj()) name = ""; else aoqi@0: if (_klass == Universe::shortArrayKlassObj()) name = ""; else aoqi@0: if (_klass == Universe::intArrayKlassObj()) name = ""; else aoqi@0: if (_klass == Universe::longArrayKlassObj()) name = ""; else aoqi@0: name = ""; aoqi@0: } aoqi@0: return name; aoqi@0: } aoqi@0: aoqi@0: void KlassInfoEntry::print_on(outputStream* st) const { aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: // simplify the formatting (ILP32 vs LP64) - always cast the numbers to 64-bit aoqi@0: st->print_cr(INT64_FORMAT_W(13) " " UINT64_FORMAT_W(13) " %s", aoqi@0: (jlong) _instance_count, aoqi@0: (julong) _instance_words * HeapWordSize, aoqi@0: name()); aoqi@0: } aoqi@0: aoqi@0: KlassInfoEntry* KlassInfoBucket::lookup(Klass* const k) { aoqi@0: KlassInfoEntry* elt = _list; aoqi@0: while (elt != NULL) { aoqi@0: if (elt->is_equal(k)) { aoqi@0: return elt; aoqi@0: } aoqi@0: elt = elt->next(); aoqi@0: } aoqi@0: elt = new (std::nothrow) KlassInfoEntry(k, list()); aoqi@0: // We may be out of space to allocate the new entry. aoqi@0: if (elt != NULL) { aoqi@0: set_list(elt); aoqi@0: } aoqi@0: return elt; aoqi@0: } aoqi@0: aoqi@0: void KlassInfoBucket::iterate(KlassInfoClosure* cic) { aoqi@0: KlassInfoEntry* elt = _list; aoqi@0: while (elt != NULL) { aoqi@0: cic->do_cinfo(elt); aoqi@0: elt = elt->next(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void KlassInfoBucket::empty() { aoqi@0: KlassInfoEntry* elt = _list; aoqi@0: _list = NULL; aoqi@0: while (elt != NULL) { aoqi@0: KlassInfoEntry* next = elt->next(); aoqi@0: delete elt; aoqi@0: elt = next; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void KlassInfoTable::AllClassesFinder::do_klass(Klass* k) { aoqi@0: // This has the SIDE EFFECT of creating a KlassInfoEntry aoqi@0: // for , if one doesn't exist yet. aoqi@0: _table->lookup(k); aoqi@0: } aoqi@0: aoqi@0: KlassInfoTable::KlassInfoTable(bool need_class_stats) { aoqi@0: _size_of_instances_in_words = 0; aoqi@0: _size = 0; aoqi@0: _ref = (HeapWord*) Universe::boolArrayKlassObj(); aoqi@0: _buckets = aoqi@0: (KlassInfoBucket*) AllocateHeap(sizeof(KlassInfoBucket) * _num_buckets, zgu@7074: mtInternal, CURRENT_PC, AllocFailStrategy::RETURN_NULL); aoqi@0: if (_buckets != NULL) { aoqi@0: _size = _num_buckets; aoqi@0: for (int index = 0; index < _size; index++) { aoqi@0: _buckets[index].initialize(); aoqi@0: } aoqi@0: if (need_class_stats) { aoqi@0: AllClassesFinder finder(this); aoqi@0: ClassLoaderDataGraph::classes_do(&finder); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: KlassInfoTable::~KlassInfoTable() { aoqi@0: if (_buckets != NULL) { aoqi@0: for (int index = 0; index < _size; index++) { aoqi@0: _buckets[index].empty(); aoqi@0: } aoqi@0: FREE_C_HEAP_ARRAY(KlassInfoBucket, _buckets, mtInternal); aoqi@0: _size = 0; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: uint KlassInfoTable::hash(const Klass* p) { aoqi@0: return (uint)(((uintptr_t)p - (uintptr_t)_ref) >> 2); aoqi@0: } aoqi@0: aoqi@0: KlassInfoEntry* KlassInfoTable::lookup(Klass* k) { aoqi@0: uint idx = hash(k) % _size; aoqi@0: assert(_buckets != NULL, "Allocation failure should have been caught"); aoqi@0: KlassInfoEntry* e = _buckets[idx].lookup(k); aoqi@0: // Lookup may fail if this is a new klass for which we aoqi@0: // could not allocate space for an new entry. aoqi@0: assert(e == NULL || k == e->klass(), "must be equal"); aoqi@0: return e; aoqi@0: } aoqi@0: aoqi@0: // Return false if the entry could not be recorded on account aoqi@0: // of running out of space required to create a new entry. aoqi@0: bool KlassInfoTable::record_instance(const oop obj) { aoqi@0: Klass* k = obj->klass(); aoqi@0: KlassInfoEntry* elt = lookup(k); aoqi@0: // elt may be NULL if it's a new klass for which we aoqi@0: // could not allocate space for a new entry in the hashtable. aoqi@0: if (elt != NULL) { aoqi@0: elt->set_count(elt->count() + 1); aoqi@0: elt->set_words(elt->words() + obj->size()); aoqi@0: _size_of_instances_in_words += obj->size(); aoqi@0: return true; aoqi@0: } else { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void KlassInfoTable::iterate(KlassInfoClosure* cic) { aoqi@0: assert(_size == 0 || _buckets != NULL, "Allocation failure should have been caught"); aoqi@0: for (int index = 0; index < _size; index++) { aoqi@0: _buckets[index].iterate(cic); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: size_t KlassInfoTable::size_of_instances_in_words() const { aoqi@0: return _size_of_instances_in_words; aoqi@0: } aoqi@0: aoqi@0: int KlassInfoHisto::sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2) { aoqi@0: return (*e1)->compare(*e1,*e2); aoqi@0: } aoqi@0: aoqi@0: KlassInfoHisto::KlassInfoHisto(KlassInfoTable* cit, const char* title) : aoqi@0: _cit(cit), aoqi@0: _title(title) { aoqi@0: _elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray(_histo_initial_size, true); aoqi@0: } aoqi@0: aoqi@0: KlassInfoHisto::~KlassInfoHisto() { aoqi@0: delete _elements; aoqi@0: } aoqi@0: aoqi@0: void KlassInfoHisto::add(KlassInfoEntry* cie) { aoqi@0: elements()->append(cie); aoqi@0: } aoqi@0: aoqi@0: void KlassInfoHisto::sort() { aoqi@0: elements()->sort(KlassInfoHisto::sort_helper); aoqi@0: } aoqi@0: aoqi@0: void KlassInfoHisto::print_elements(outputStream* st) const { aoqi@0: // simplify the formatting (ILP32 vs LP64) - store the sum in 64-bit aoqi@0: jlong total = 0; aoqi@0: julong totalw = 0; aoqi@0: for(int i=0; i < elements()->length(); i++) { aoqi@0: st->print("%4d: ", i+1); aoqi@0: elements()->at(i)->print_on(st); aoqi@0: total += elements()->at(i)->count(); aoqi@0: totalw += elements()->at(i)->words(); aoqi@0: } aoqi@0: st->print_cr("Total " INT64_FORMAT_W(13) " " UINT64_FORMAT_W(13), aoqi@0: total, totalw * HeapWordSize); aoqi@0: } aoqi@0: aoqi@0: #define MAKE_COL_NAME(field, name, help) #name, aoqi@0: #define MAKE_COL_HELP(field, name, help) help, aoqi@0: aoqi@0: static const char *name_table[] = { aoqi@0: HEAP_INSPECTION_COLUMNS_DO(MAKE_COL_NAME) aoqi@0: }; aoqi@0: aoqi@0: static const char *help_table[] = { aoqi@0: HEAP_INSPECTION_COLUMNS_DO(MAKE_COL_HELP) aoqi@0: }; aoqi@0: aoqi@0: bool KlassInfoHisto::is_selected(const char *col_name) { aoqi@0: if (_selected_columns == NULL) { aoqi@0: return true; aoqi@0: } aoqi@0: if (strcmp(_selected_columns, col_name) == 0) { aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: const char *start = strstr(_selected_columns, col_name); aoqi@0: if (start == NULL) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // The following must be true, because _selected_columns != col_name aoqi@0: if (start > _selected_columns && start[-1] != ',') { aoqi@0: return false; aoqi@0: } aoqi@0: char x = start[strlen(col_name)]; aoqi@0: if (x != ',' && x != '\0') { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: PRAGMA_FORMAT_NONLITERAL_IGNORED_EXTERNAL aoqi@0: void KlassInfoHisto::print_title(outputStream* st, bool csv_format, aoqi@0: bool selected[], int width_table[], aoqi@0: const char *name_table[]) { aoqi@0: if (csv_format) { aoqi@0: st->print("Index,Super"); aoqi@0: for (int c=0; cprint(",%s", name_table[c]);} aoqi@0: } aoqi@0: st->print(",ClassName"); aoqi@0: } else { aoqi@0: st->print("Index Super"); aoqi@0: for (int c=0; cprint(str_fmt(width_table[c]), name_table[c]);} aoqi@0: PRAGMA_DIAG_POP aoqi@0: } aoqi@0: st->print(" ClassName"); aoqi@0: } aoqi@0: aoqi@0: if (is_selected("ClassLoader")) { aoqi@0: st->print(",ClassLoader"); aoqi@0: } aoqi@0: st->cr(); aoqi@0: } aoqi@0: aoqi@0: void KlassInfoHisto::print_class_stats(outputStream* st, aoqi@0: bool csv_format, const char *columns) { aoqi@0: ResourceMark rm; aoqi@0: KlassSizeStats sz, sz_sum; aoqi@0: int i; aoqi@0: julong *col_table = (julong*)(&sz); aoqi@0: julong *colsum_table = (julong*)(&sz_sum); aoqi@0: int width_table[KlassSizeStats::_num_columns]; aoqi@0: bool selected[KlassSizeStats::_num_columns]; aoqi@0: aoqi@0: _selected_columns = columns; aoqi@0: aoqi@0: memset(&sz_sum, 0, sizeof(sz_sum)); aoqi@0: for (int c=0; clength(); i++) { aoqi@0: elements()->at(i)->set_index(i+1); aoqi@0: } aoqi@0: aoqi@0: for (int pass=1; pass<=2; pass++) { aoqi@0: if (pass == 2) { aoqi@0: print_title(st, csv_format, selected, width_table, name_table); aoqi@0: } aoqi@0: for(i=0; i < elements()->length(); i++) { aoqi@0: KlassInfoEntry* e = (KlassInfoEntry*)elements()->at(i); aoqi@0: const Klass* k = e->klass(); aoqi@0: aoqi@0: memset(&sz, 0, sizeof(sz)); aoqi@0: sz._inst_count = e->count(); aoqi@0: sz._inst_bytes = HeapWordSize * e->words(); aoqi@0: k->collect_statistics(&sz); aoqi@0: sz._total_bytes = sz._ro_bytes + sz._rw_bytes; aoqi@0: aoqi@0: if (pass == 1) { aoqi@0: for (int c=0; coop_is_instance()) { aoqi@0: Klass* super = ((InstanceKlass*)k)->java_super(); aoqi@0: if (super) { aoqi@0: KlassInfoEntry* super_e = _cit->lookup(super); aoqi@0: if (super_e) { aoqi@0: super_index = super_e->index(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (csv_format) { aoqi@0: st->print("%d,%d", e->index(), super_index); aoqi@0: for (int c=0; cprint("," JULONG_FORMAT, col_table[c]);} aoqi@0: } aoqi@0: st->print(",%s",e->name()); aoqi@0: } else { aoqi@0: st->print("%5d %5d", e->index(), super_index); aoqi@0: for (int c=0; cprint(" %s", e->name()); aoqi@0: } aoqi@0: if (is_selected("ClassLoader")) { aoqi@0: ClassLoaderData* loader_data = k->class_loader_data(); aoqi@0: st->print(","); aoqi@0: loader_data->print_value_on(st); aoqi@0: } aoqi@0: st->cr(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (pass == 1) { aoqi@0: for (int c=0; cprint(","); aoqi@0: for (int c=0; cprint("," JULONG_FORMAT, colsum_table[c]);} aoqi@0: } aoqi@0: } else { aoqi@0: st->print(" "); aoqi@0: for (int c=0; cprint(" Total"); aoqi@0: if (sz_sum._total_bytes > 0) { aoqi@0: st->cr(); aoqi@0: st->print(" "); aoqi@0: for (int c=0; cprint(str_fmt(width_table[c]), "-"); aoqi@0: PRAGMA_DIAG_POP aoqi@0: break; aoqi@0: default: aoqi@0: { aoqi@0: double perc = (double)(100) * (double)(colsum_table[c]) / (double)sz_sum._total_bytes; aoqi@0: PRAGMA_DIAG_PUSH aoqi@0: PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL aoqi@0: st->print(perc_fmt(width_table[c]), perc); aoqi@0: PRAGMA_DIAG_POP aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: st->cr(); aoqi@0: aoqi@0: if (!csv_format) { aoqi@0: print_title(st, csv_format, selected, width_table, name_table); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: julong KlassInfoHisto::annotations_bytes(Array* p) const { aoqi@0: julong bytes = 0; aoqi@0: if (p != NULL) { aoqi@0: for (int i = 0; i < p->length(); i++) { aoqi@0: bytes += count_bytes_array(p->at(i)); aoqi@0: } aoqi@0: bytes += count_bytes_array(p); aoqi@0: } aoqi@0: return bytes; aoqi@0: } aoqi@0: aoqi@0: void KlassInfoHisto::print_histo_on(outputStream* st, bool print_stats, aoqi@0: bool csv_format, const char *columns) { aoqi@0: if (print_stats) { aoqi@0: print_class_stats(st, csv_format, columns); aoqi@0: } else { aoqi@0: st->print_cr("%s",title()); aoqi@0: print_elements(st); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class HistoClosure : public KlassInfoClosure { aoqi@0: private: aoqi@0: KlassInfoHisto* _cih; aoqi@0: public: aoqi@0: HistoClosure(KlassInfoHisto* cih) : _cih(cih) {} aoqi@0: aoqi@0: void do_cinfo(KlassInfoEntry* cie) { aoqi@0: _cih->add(cie); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: class RecordInstanceClosure : public ObjectClosure { aoqi@0: private: aoqi@0: KlassInfoTable* _cit; aoqi@0: size_t _missed_count; aoqi@0: BoolObjectClosure* _filter; aoqi@0: public: aoqi@0: RecordInstanceClosure(KlassInfoTable* cit, BoolObjectClosure* filter) : aoqi@0: _cit(cit), _missed_count(0), _filter(filter) {} aoqi@0: aoqi@0: void do_object(oop obj) { aoqi@0: if (should_visit(obj)) { aoqi@0: if (!_cit->record_instance(obj)) { aoqi@0: _missed_count++; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: size_t missed_count() { return _missed_count; } aoqi@0: aoqi@0: private: aoqi@0: bool should_visit(oop obj) { aoqi@0: return _filter == NULL || _filter->do_object_b(obj); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: size_t HeapInspection::populate_table(KlassInfoTable* cit, BoolObjectClosure *filter) { aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: RecordInstanceClosure ric(cit, filter); aoqi@0: Universe::heap()->object_iterate(&ric); aoqi@0: return ric.missed_count(); aoqi@0: } aoqi@0: aoqi@0: void HeapInspection::heap_inspection(outputStream* st) { aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: if (_print_help) { aoqi@0: for (int c=0; cprint("%s:\n\t", name_table[c]); aoqi@0: const int max_col = 60; aoqi@0: int col = 0; aoqi@0: for (const char *p = help_table[c]; *p; p++,col++) { aoqi@0: if (col >= max_col && *p == ' ') { aoqi@0: st->print("\n\t"); aoqi@0: col = 0; aoqi@0: } else { aoqi@0: st->print("%c", *p); aoqi@0: } aoqi@0: } aoqi@0: st->print_cr(".\n"); aoqi@0: } aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: KlassInfoTable cit(_print_class_stats); aoqi@0: if (!cit.allocation_failed()) { aoqi@0: size_t missed_count = populate_table(&cit); aoqi@0: if (missed_count != 0) { aoqi@0: st->print_cr("WARNING: Ran out of C-heap; undercounted " SIZE_FORMAT aoqi@0: " total instances in data below", aoqi@0: missed_count); aoqi@0: } aoqi@0: aoqi@0: // Sort and print klass instance info aoqi@0: const char *title = "\n" aoqi@0: " num #instances #bytes class name\n" aoqi@0: "----------------------------------------------"; aoqi@0: KlassInfoHisto histo(&cit, title); aoqi@0: HistoClosure hc(&histo); aoqi@0: aoqi@0: cit.iterate(&hc); aoqi@0: aoqi@0: histo.sort(); aoqi@0: histo.print_histo_on(st, _print_class_stats, _csv_format, _columns); aoqi@0: } else { aoqi@0: st->print_cr("WARNING: Ran out of C-heap; histogram not generated"); aoqi@0: } aoqi@0: st->flush(); aoqi@0: } aoqi@0: aoqi@0: class FindInstanceClosure : public ObjectClosure { aoqi@0: private: aoqi@0: Klass* _klass; aoqi@0: GrowableArray* _result; aoqi@0: aoqi@0: public: aoqi@0: FindInstanceClosure(Klass* k, GrowableArray* result) : _klass(k), _result(result) {}; aoqi@0: aoqi@0: void do_object(oop obj) { aoqi@0: if (obj->is_a(_klass)) { aoqi@0: _result->append(obj); aoqi@0: } aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: void HeapInspection::find_instances_at_safepoint(Klass* k, GrowableArray* result) { aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped"); aoqi@0: assert(Heap_lock->is_locked(), "should have the Heap_lock"); aoqi@0: aoqi@0: // Ensure that the heap is parsable aoqi@0: Universe::heap()->ensure_parsability(false); // no need to retire TALBs aoqi@0: aoqi@0: // Iterate over objects in the heap aoqi@0: FindInstanceClosure fic(k, result); aoqi@0: // If this operation encounters a bad object when using CMS, aoqi@0: // consider using safe_object_iterate() which avoids metadata aoqi@0: // objects that may contain bad references. aoqi@0: Universe::heap()->object_iterate(&fic); aoqi@0: }