Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/heapInspection.cpp
32285 views
/*1* Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/classLoaderData.hpp"26#include "gc_interface/collectedHeap.hpp"27#include "memory/genCollectedHeap.hpp"28#include "memory/heapInspection.hpp"29#include "memory/resourceArea.hpp"30#include "runtime/os.hpp"31#include "utilities/globalDefinitions.hpp"32#include "utilities/macros.hpp"33#if INCLUDE_ALL_GCS34#include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"35#endif // INCLUDE_ALL_GCS3637PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC3839// HeapInspection4041int KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) {42if(e1->_instance_words > e2->_instance_words) {43return -1;44} else if(e1->_instance_words < e2->_instance_words) {45return 1;46}47// Sort alphabetically, note 'Z' < '[' < 'a', but it's better to group48// the array classes before all the instance classes.49ResourceMark rm;50const char* name1 = e1->klass()->external_name();51const char* name2 = e2->klass()->external_name();52bool d1 = (name1[0] == '[');53bool d2 = (name2[0] == '[');54if (d1 && !d2) {55return -1;56} else if (d2 && !d1) {57return 1;58} else {59return strcmp(name1, name2);60}61}6263const char* KlassInfoEntry::name() const {64const char* name;65if (_klass->name() != NULL) {66name = _klass->external_name();67} else {68if (_klass == Universe::boolArrayKlassObj()) name = "<boolArrayKlass>"; else69if (_klass == Universe::charArrayKlassObj()) name = "<charArrayKlass>"; else70if (_klass == Universe::singleArrayKlassObj()) name = "<singleArrayKlass>"; else71if (_klass == Universe::doubleArrayKlassObj()) name = "<doubleArrayKlass>"; else72if (_klass == Universe::byteArrayKlassObj()) name = "<byteArrayKlass>"; else73if (_klass == Universe::shortArrayKlassObj()) name = "<shortArrayKlass>"; else74if (_klass == Universe::intArrayKlassObj()) name = "<intArrayKlass>"; else75if (_klass == Universe::longArrayKlassObj()) name = "<longArrayKlass>"; else76name = "<no name>";77}78return name;79}8081void KlassInfoEntry::print_on(outputStream* st) const {82ResourceMark rm;8384// simplify the formatting (ILP32 vs LP64) - always cast the numbers to 64-bit85st->print_cr(INT64_FORMAT_W(13) " " UINT64_FORMAT_W(13) " %s",86(jlong) _instance_count,87(julong) _instance_words * HeapWordSize,88name());89}9091KlassInfoEntry* KlassInfoBucket::lookup(Klass* const k) {92KlassInfoEntry* elt = _list;93while (elt != NULL) {94if (elt->is_equal(k)) {95return elt;96}97elt = elt->next();98}99elt = new (std::nothrow) KlassInfoEntry(k, list());100// We may be out of space to allocate the new entry.101if (elt != NULL) {102set_list(elt);103}104return elt;105}106107void KlassInfoBucket::iterate(KlassInfoClosure* cic) {108KlassInfoEntry* elt = _list;109while (elt != NULL) {110cic->do_cinfo(elt);111elt = elt->next();112}113}114115void KlassInfoBucket::empty() {116KlassInfoEntry* elt = _list;117_list = NULL;118while (elt != NULL) {119KlassInfoEntry* next = elt->next();120delete elt;121elt = next;122}123}124125void KlassInfoTable::AllClassesFinder::do_klass(Klass* k) {126// This has the SIDE EFFECT of creating a KlassInfoEntry127// for <k>, if one doesn't exist yet.128_table->lookup(k);129}130131KlassInfoTable::KlassInfoTable(bool need_class_stats) {132_size_of_instances_in_words = 0;133_size = 0;134_ref = (HeapWord*) Universe::boolArrayKlassObj();135_buckets =136(KlassInfoBucket*) AllocateHeap(sizeof(KlassInfoBucket) * _num_buckets,137mtInternal, CURRENT_PC, AllocFailStrategy::RETURN_NULL);138if (_buckets != NULL) {139_size = _num_buckets;140for (int index = 0; index < _size; index++) {141_buckets[index].initialize();142}143if (need_class_stats) {144AllClassesFinder finder(this);145ClassLoaderDataGraph::classes_do(&finder);146}147}148}149150KlassInfoTable::~KlassInfoTable() {151if (_buckets != NULL) {152for (int index = 0; index < _size; index++) {153_buckets[index].empty();154}155FREE_C_HEAP_ARRAY(KlassInfoBucket, _buckets, mtInternal);156_size = 0;157}158}159160uint KlassInfoTable::hash(const Klass* p) {161return (uint)(((uintptr_t)p - (uintptr_t)_ref) >> 2);162}163164KlassInfoEntry* KlassInfoTable::lookup(Klass* k) {165uint idx = hash(k) % _size;166assert(_buckets != NULL, "Allocation failure should have been caught");167KlassInfoEntry* e = _buckets[idx].lookup(k);168// Lookup may fail if this is a new klass for which we169// could not allocate space for an new entry.170assert(e == NULL || k == e->klass(), "must be equal");171return e;172}173174// Return false if the entry could not be recorded on account175// of running out of space required to create a new entry.176bool KlassInfoTable::record_instance(const oop obj) {177Klass* k = obj->klass();178KlassInfoEntry* elt = lookup(k);179// elt may be NULL if it's a new klass for which we180// could not allocate space for a new entry in the hashtable.181if (elt != NULL) {182elt->set_count(elt->count() + 1);183elt->set_words(elt->words() + obj->size());184_size_of_instances_in_words += obj->size();185return true;186} else {187return false;188}189}190191void KlassInfoTable::iterate(KlassInfoClosure* cic) {192assert(_size == 0 || _buckets != NULL, "Allocation failure should have been caught");193for (int index = 0; index < _size; index++) {194_buckets[index].iterate(cic);195}196}197198size_t KlassInfoTable::size_of_instances_in_words() const {199return _size_of_instances_in_words;200}201202int KlassInfoHisto::sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2) {203return (*e1)->compare(*e1,*e2);204}205206KlassInfoHisto::KlassInfoHisto(KlassInfoTable* cit, const char* title) :207_cit(cit),208_title(title) {209_elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<KlassInfoEntry*>(_histo_initial_size, true);210}211212KlassInfoHisto::~KlassInfoHisto() {213delete _elements;214}215216void KlassInfoHisto::add(KlassInfoEntry* cie) {217elements()->append(cie);218}219220void KlassInfoHisto::sort() {221elements()->sort(KlassInfoHisto::sort_helper);222}223224void KlassInfoHisto::print_elements(outputStream* st) const {225// simplify the formatting (ILP32 vs LP64) - store the sum in 64-bit226jlong total = 0;227julong totalw = 0;228for(int i=0; i < elements()->length(); i++) {229st->print("%4d: ", i+1);230elements()->at(i)->print_on(st);231total += elements()->at(i)->count();232totalw += elements()->at(i)->words();233}234st->print_cr("Total " INT64_FORMAT_W(13) " " UINT64_FORMAT_W(13),235total, totalw * HeapWordSize);236}237238#define MAKE_COL_NAME(field, name, help) #name,239#define MAKE_COL_HELP(field, name, help) help,240241static const char *name_table[] = {242HEAP_INSPECTION_COLUMNS_DO(MAKE_COL_NAME)243};244245static const char *help_table[] = {246HEAP_INSPECTION_COLUMNS_DO(MAKE_COL_HELP)247};248249bool KlassInfoHisto::is_selected(const char *col_name) {250if (_selected_columns == NULL) {251return true;252}253if (strcmp(_selected_columns, col_name) == 0) {254return true;255}256257const char *start = strstr(_selected_columns, col_name);258if (start == NULL) {259return false;260}261262// The following must be true, because _selected_columns != col_name263if (start > _selected_columns && start[-1] != ',') {264return false;265}266char x = start[strlen(col_name)];267if (x != ',' && x != '\0') {268return false;269}270271return true;272}273274PRAGMA_FORMAT_NONLITERAL_IGNORED_EXTERNAL275void KlassInfoHisto::print_title(outputStream* st, bool csv_format,276bool selected[], int width_table[],277const char *name_table[]) {278if (csv_format) {279st->print("Index,Super");280for (int c=0; c<KlassSizeStats::_num_columns; c++) {281if (selected[c]) {st->print(",%s", name_table[c]);}282}283st->print(",ClassName");284} else {285st->print("Index Super");286for (int c=0; c<KlassSizeStats::_num_columns; c++) {287PRAGMA_DIAG_PUSH288PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL289if (selected[c]) {st->print(str_fmt(width_table[c]), name_table[c]);}290PRAGMA_DIAG_POP291}292st->print(" ClassName");293}294295if (is_selected("ClassLoader")) {296st->print(",ClassLoader");297}298st->cr();299}300301void KlassInfoHisto::print_class_stats(outputStream* st,302bool csv_format, const char *columns) {303ResourceMark rm;304KlassSizeStats sz, sz_sum;305int i;306julong *col_table = (julong*)(&sz);307julong *colsum_table = (julong*)(&sz_sum);308int width_table[KlassSizeStats::_num_columns];309bool selected[KlassSizeStats::_num_columns];310311_selected_columns = columns;312313memset(&sz_sum, 0, sizeof(sz_sum));314for (int c=0; c<KlassSizeStats::_num_columns; c++) {315selected[c] = is_selected(name_table[c]);316}317318for(i=0; i < elements()->length(); i++) {319elements()->at(i)->set_index(i+1);320}321322for (int pass=1; pass<=2; pass++) {323if (pass == 2) {324print_title(st, csv_format, selected, width_table, name_table);325}326for(i=0; i < elements()->length(); i++) {327KlassInfoEntry* e = (KlassInfoEntry*)elements()->at(i);328const Klass* k = e->klass();329330memset(&sz, 0, sizeof(sz));331sz._inst_count = e->count();332sz._inst_bytes = HeapWordSize * e->words();333k->collect_statistics(&sz);334sz._total_bytes = sz._ro_bytes + sz._rw_bytes;335336if (pass == 1) {337for (int c=0; c<KlassSizeStats::_num_columns; c++) {338colsum_table[c] += col_table[c];339}340} else {341int super_index = -1;342if (k->oop_is_instance()) {343Klass* super = ((InstanceKlass*)k)->java_super();344if (super) {345KlassInfoEntry* super_e = _cit->lookup(super);346if (super_e) {347super_index = super_e->index();348}349}350}351352if (csv_format) {353st->print("%d,%d", e->index(), super_index);354for (int c=0; c<KlassSizeStats::_num_columns; c++) {355if (selected[c]) {st->print("," JULONG_FORMAT, col_table[c]);}356}357st->print(",%s",e->name());358} else {359st->print("%5d %5d", e->index(), super_index);360for (int c=0; c<KlassSizeStats::_num_columns; c++) {361if (selected[c]) {print_julong(st, width_table[c], col_table[c]);}362}363st->print(" %s", e->name());364}365if (is_selected("ClassLoader")) {366ClassLoaderData* loader_data = k->class_loader_data();367st->print(",");368loader_data->print_value_on(st);369}370st->cr();371}372}373374if (pass == 1) {375for (int c=0; c<KlassSizeStats::_num_columns; c++) {376width_table[c] = col_width(colsum_table[c], name_table[c]);377}378}379}380381sz_sum._inst_size = 0;382383if (csv_format) {384st->print(",");385for (int c=0; c<KlassSizeStats::_num_columns; c++) {386if (selected[c]) {st->print("," JULONG_FORMAT, colsum_table[c]);}387}388} else {389st->print(" ");390for (int c=0; c<KlassSizeStats::_num_columns; c++) {391if (selected[c]) {print_julong(st, width_table[c], colsum_table[c]);}392}393st->print(" Total");394if (sz_sum._total_bytes > 0) {395st->cr();396st->print(" ");397for (int c=0; c<KlassSizeStats::_num_columns; c++) {398if (selected[c]) {399switch (c) {400case KlassSizeStats::_index_inst_size:401case KlassSizeStats::_index_inst_count:402case KlassSizeStats::_index_method_count:403PRAGMA_DIAG_PUSH404PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL405st->print(str_fmt(width_table[c]), "-");406PRAGMA_DIAG_POP407break;408default:409{410double perc = (double)(100) * (double)(colsum_table[c]) / (double)sz_sum._total_bytes;411PRAGMA_DIAG_PUSH412PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL413st->print(perc_fmt(width_table[c]), perc);414PRAGMA_DIAG_POP415}416}417}418}419}420}421st->cr();422423if (!csv_format) {424print_title(st, csv_format, selected, width_table, name_table);425}426}427428julong KlassInfoHisto::annotations_bytes(Array<AnnotationArray*>* p) const {429julong bytes = 0;430if (p != NULL) {431for (int i = 0; i < p->length(); i++) {432bytes += count_bytes_array(p->at(i));433}434bytes += count_bytes_array(p);435}436return bytes;437}438439void KlassInfoHisto::print_histo_on(outputStream* st, bool print_stats,440bool csv_format, const char *columns) {441if (print_stats) {442print_class_stats(st, csv_format, columns);443} else {444st->print_cr("%s",title());445print_elements(st);446}447}448449class HistoClosure : public KlassInfoClosure {450private:451KlassInfoHisto* _cih;452public:453HistoClosure(KlassInfoHisto* cih) : _cih(cih) {}454455void do_cinfo(KlassInfoEntry* cie) {456_cih->add(cie);457}458};459460class RecordInstanceClosure : public ObjectClosure {461private:462KlassInfoTable* _cit;463size_t _missed_count;464BoolObjectClosure* _filter;465public:466RecordInstanceClosure(KlassInfoTable* cit, BoolObjectClosure* filter) :467_cit(cit), _missed_count(0), _filter(filter) {}468469void do_object(oop obj) {470if (should_visit(obj)) {471if (!_cit->record_instance(obj)) {472_missed_count++;473}474}475}476477size_t missed_count() { return _missed_count; }478479private:480bool should_visit(oop obj) {481return _filter == NULL || _filter->do_object_b(obj);482}483};484485size_t HeapInspection::populate_table(KlassInfoTable* cit, BoolObjectClosure *filter) {486ResourceMark rm;487488RecordInstanceClosure ric(cit, filter);489Universe::heap()->object_iterate(&ric);490return ric.missed_count();491}492493void HeapInspection::heap_inspection(outputStream* st) {494ResourceMark rm;495496if (_print_help) {497for (int c=0; c<KlassSizeStats::_num_columns; c++) {498st->print("%s:\n\t", name_table[c]);499const int max_col = 60;500int col = 0;501for (const char *p = help_table[c]; *p; p++,col++) {502if (col >= max_col && *p == ' ') {503st->print("\n\t");504col = 0;505} else {506st->print("%c", *p);507}508}509st->print_cr(".\n");510}511return;512}513514KlassInfoTable cit(_print_class_stats);515if (!cit.allocation_failed()) {516size_t missed_count = populate_table(&cit);517if (missed_count != 0) {518st->print_cr("WARNING: Ran out of C-heap; undercounted " SIZE_FORMAT519" total instances in data below",520missed_count);521}522523// Sort and print klass instance info524const char *title = "\n"525" num #instances #bytes class name\n"526"----------------------------------------------";527KlassInfoHisto histo(&cit, title);528HistoClosure hc(&histo);529530cit.iterate(&hc);531532histo.sort();533histo.print_histo_on(st, _print_class_stats, _csv_format, _columns);534} else {535st->print_cr("WARNING: Ran out of C-heap; histogram not generated");536}537st->flush();538}539540class FindInstanceClosure : public ObjectClosure {541private:542Klass* _klass;543GrowableArray<oop>* _result;544545public:546FindInstanceClosure(Klass* k, GrowableArray<oop>* result) : _klass(k), _result(result) {};547548void do_object(oop obj) {549if (obj->is_a(_klass)) {550_result->append(obj);551}552}553};554555void HeapInspection::find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) {556assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");557assert(Heap_lock->is_locked(), "should have the Heap_lock");558559// Ensure that the heap is parsable560Universe::heap()->ensure_parsability(false); // no need to retire TALBs561562// Iterate over objects in the heap563FindInstanceClosure fic(k, result);564// If this operation encounters a bad object when using CMS,565// consider using safe_object_iterate() which avoids metadata566// objects that may contain bad references.567Universe::heap()->object_iterate(&fic);568}569570571