Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/heapInspection.hpp
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#ifndef SHARE_VM_MEMORY_HEAPINSPECTION_HPP25#define SHARE_VM_MEMORY_HEAPINSPECTION_HPP2627#include "memory/allocation.inline.hpp"28#include "oops/oop.inline.hpp"29#include "oops/annotations.hpp"30#include "utilities/macros.hpp"3132#if INCLUDE_SERVICES333435// HeapInspection3637// KlassInfoTable is a bucket hash table that38// maps Klass*s to extra information:39// instance count and instance word size.40//41// A KlassInfoBucket is the head of a link list42// of KlassInfoEntry's43//44// KlassInfoHisto is a growable array of pointers45// to KlassInfoEntry's and is used to sort46// the entries.4748#define HEAP_INSPECTION_COLUMNS_DO(f) \49f(inst_size, InstSize, \50"Size of each object instance of the Java class") \51f(inst_count, InstCount, \52"Number of object instances of the Java class") \53f(inst_bytes, InstBytes, \54"This is usually (InstSize * InstNum). The only exception is " \55"java.lang.Class, whose InstBytes also includes the slots " \56"used to store static fields. InstBytes is not counted in " \57"ROAll, RWAll or Total") \58f(mirror_bytes, Mirror, \59"Size of the Klass::java_mirror() object") \60f(klass_bytes, KlassBytes, \61"Size of the InstanceKlass or ArrayKlass for this class. " \62"Note that this includes VTab, ITab, OopMap") \63f(secondary_supers_bytes, K_secondary_supers, \64"Number of bytes used by the Klass::secondary_supers() array") \65f(vtab_bytes, VTab, \66"Size of the embedded vtable in InstanceKlass") \67f(itab_bytes, ITab, \68"Size of the embedded itable in InstanceKlass") \69f(nonstatic_oopmap_bytes, OopMap, \70"Size of the embedded nonstatic_oop_map in InstanceKlass") \71f(methods_array_bytes, IK_methods, \72"Number of bytes used by the InstanceKlass::methods() array") \73f(method_ordering_bytes, IK_method_ordering, \74"Number of bytes used by the InstanceKlass::method_ordering() array") \75f(default_methods_array_bytes, IK_default_methods, \76"Number of bytes used by the InstanceKlass::default_methods() array") \77f(default_vtable_indices_bytes, IK_default_vtable_indices, \78"Number of bytes used by the InstanceKlass::default_vtable_indices() array") \79f(local_interfaces_bytes, IK_local_interfaces, \80"Number of bytes used by the InstanceKlass::local_interfaces() array") \81f(transitive_interfaces_bytes, IK_transitive_interfaces, \82"Number of bytes used by the InstanceKlass::transitive_interfaces() array") \83f(fields_bytes, IK_fields, \84"Number of bytes used by the InstanceKlass::fields() array") \85f(inner_classes_bytes, IK_inner_classes, \86"Number of bytes used by the InstanceKlass::inner_classes() array") \87f(signers_bytes, IK_signers, \88"Number of bytes used by the InstanceKlass::singers() array") \89f(class_annotations_bytes, class_annotations, \90"Size of class annotations") \91f(class_type_annotations_bytes, class_type_annotations, \92"Size of class type annotations") \93f(fields_annotations_bytes, fields_annotations, \94"Size of field annotations") \95f(fields_type_annotations_bytes, fields_type_annotations, \96"Size of field type annotations") \97f(methods_annotations_bytes, methods_annotations, \98"Size of method annotations") \99f(methods_parameter_annotations_bytes, methods_parameter_annotations, \100"Size of method parameter annotations") \101f(methods_type_annotations_bytes, methods_type_annotations, \102"Size of methods type annotations") \103f(methods_default_annotations_bytes, methods_default_annotations, \104"Size of methods default annotations") \105f(annotations_bytes, annotations, \106"Size of all annotations") \107f(cp_bytes, Cp, \108"Size of InstanceKlass::constants()") \109f(cp_tags_bytes, CpTags, \110"Size of InstanceKlass::constants()->tags()") \111f(cp_cache_bytes, CpCache, \112"Size of InstanceKlass::constants()->cache()") \113f(cp_operands_bytes, CpOperands, \114"Size of InstanceKlass::constants()->operands()") \115f(cp_refmap_bytes, CpRefMap, \116"Size of InstanceKlass::constants()->reference_map()") \117f(cp_all_bytes, CpAll, \118"Sum of Cp + CpTags + CpCache + CpOperands + CpRefMap") \119f(method_count, MethodCount, \120"Number of methods in this class") \121f(method_bytes, MethodBytes, \122"Size of the Method object") \123f(const_method_bytes, ConstMethod, \124"Size of the ConstMethod object") \125f(method_data_bytes, MethodData, \126"Size of the MethodData object") \127f(stackmap_bytes, StackMap, \128"Size of the stackmap_data") \129f(bytecode_bytes, Bytecodes, \130"Of the MethodBytes column, how much are the space taken up by bytecodes") \131f(method_all_bytes, MethodAll, \132"Sum of MethodBytes + Constmethod + Stackmap + Methoddata") \133f(ro_bytes, ROAll, \134"Size of all class meta data that could (potentially) be placed " \135"in read-only memory. (This could change with CDS design)") \136f(rw_bytes, RWAll, \137"Size of all class meta data that must be placed in read/write " \138"memory. (This could change with CDS design) ") \139f(total_bytes, Total, \140"ROAll + RWAll. Note that this does NOT include InstBytes.")141142// Size statistics for a Klass - filled in by Klass::collect_statistics()143class KlassSizeStats {144public:145#define COUNT_KLASS_SIZE_STATS_FIELD(field, name, help) _index_ ## field,146#define DECLARE_KLASS_SIZE_STATS_FIELD(field, name, help) julong _ ## field;147148enum {149HEAP_INSPECTION_COLUMNS_DO(COUNT_KLASS_SIZE_STATS_FIELD)150_num_columns151};152153HEAP_INSPECTION_COLUMNS_DO(DECLARE_KLASS_SIZE_STATS_FIELD)154155static int count(oop x) {156return (HeapWordSize * (((x) != NULL) ? (x)->size() : 0));157}158159static int count_array(objArrayOop x) {160return (HeapWordSize * (((x) != NULL) ? (x)->size() : 0));161}162163template <class T> static int count(T* x) {164return (HeapWordSize * ((x) ? (x)->size() : 0));165}166167template <class T> static int count_array(T* x) {168if (x == NULL) {169return 0;170}171if (x->length() == 0) {172// This is a shared array, e.g., Universe::the_empty_int_array(). Don't173// count it to avoid double-counting.174return 0;175}176return HeapWordSize * x->size();177}178};179180181182183class KlassInfoEntry: public CHeapObj<mtInternal> {184private:185KlassInfoEntry* _next;186Klass* _klass;187long _instance_count;188size_t _instance_words;189long _index;190191public:192KlassInfoEntry(Klass* k, KlassInfoEntry* next) :193_klass(k), _instance_count(0), _instance_words(0), _next(next), _index(-1)194{}195KlassInfoEntry* next() const { return _next; }196bool is_equal(const Klass* k) { return k == _klass; }197Klass* klass() const { return _klass; }198long count() const { return _instance_count; }199void set_count(long ct) { _instance_count = ct; }200size_t words() const { return _instance_words; }201void set_words(size_t wds) { _instance_words = wds; }202void set_index(long index) { _index = index; }203long index() const { return _index; }204int compare(KlassInfoEntry* e1, KlassInfoEntry* e2);205void print_on(outputStream* st) const;206const char* name() const;207};208209class KlassInfoClosure : public StackObj {210public:211// Called for each KlassInfoEntry.212virtual void do_cinfo(KlassInfoEntry* cie) = 0;213};214215class KlassInfoBucket: public CHeapObj<mtInternal> {216private:217KlassInfoEntry* _list;218KlassInfoEntry* list() { return _list; }219void set_list(KlassInfoEntry* l) { _list = l; }220public:221KlassInfoEntry* lookup(Klass* k);222void initialize() { _list = NULL; }223void empty();224void iterate(KlassInfoClosure* cic);225};226227class KlassInfoTable: public StackObj {228private:229int _size;230static const int _num_buckets = 20011;231size_t _size_of_instances_in_words;232233// An aligned reference address (typically the least234// address in the perm gen) used for hashing klass235// objects.236HeapWord* _ref;237238KlassInfoBucket* _buckets;239uint hash(const Klass* p);240KlassInfoEntry* lookup(Klass* k); // allocates if not found!241242class AllClassesFinder : public KlassClosure {243KlassInfoTable *_table;244public:245AllClassesFinder(KlassInfoTable* table) : _table(table) {}246virtual void do_klass(Klass* k);247};248249public:250KlassInfoTable(bool need_class_stats);251~KlassInfoTable();252bool record_instance(const oop obj);253void iterate(KlassInfoClosure* cic);254bool allocation_failed() { return _buckets == NULL; }255size_t size_of_instances_in_words() const;256257friend class KlassInfoHisto;258};259260class KlassInfoHisto : public StackObj {261private:262static const int _histo_initial_size = 1000;263KlassInfoTable *_cit;264GrowableArray<KlassInfoEntry*>* _elements;265GrowableArray<KlassInfoEntry*>* elements() const { return _elements; }266const char* _title;267const char* title() const { return _title; }268static int sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2);269void print_elements(outputStream* st) const;270void print_class_stats(outputStream* st, bool csv_format, const char *columns);271julong annotations_bytes(Array<AnnotationArray*>* p) const;272const char *_selected_columns;273bool is_selected(const char *col_name);274void print_title(outputStream* st, bool csv_format,275bool selected_columns_table[], int width_table[],276const char *name_table[]);277278template <class T> static int count_bytes(T* x) {279return (HeapWordSize * ((x) ? (x)->size() : 0));280}281282template <class T> static int count_bytes_array(T* x) {283if (x == NULL) {284return 0;285}286if (x->length() == 0) {287// This is a shared array, e.g., Universe::the_empty_int_array(). Don't288// count it to avoid double-counting.289return 0;290}291return HeapWordSize * x->size();292}293294// returns a format string to print a julong with the given width. E.g,295// printf(num_fmt(6), julong(10)) would print out the number 10 with 4296// leading spaces.297PRAGMA_DIAG_PUSH298PRAGMA_FORMAT_NONLITERAL_IGNORED299300static void print_julong(outputStream* st, int width, julong n) {301int num_spaces = width - julong_width(n);302if (num_spaces > 0) {303st->print(str_fmt(num_spaces), "");304}305st->print(JULONG_FORMAT, n);306}307PRAGMA_DIAG_POP308309static char* perc_fmt(int width) {310static char buf[32];311jio_snprintf(buf, sizeof(buf), "%%%d.1f%%%%", width-1);312return buf;313}314315static char* str_fmt(int width) {316static char buf[32];317jio_snprintf(buf, sizeof(buf), "%%%ds", width);318return buf;319}320321static int julong_width(julong n) {322if (n == 0) {323return 1;324}325int w = 0;326while (n > 0) {327n /= 10;328w += 1;329}330return w;331}332333static int col_width(julong n, const char *name) {334int w = julong_width(n);335int min = (int)(strlen(name));336if (w < min) {337w = min;338}339// add a leading space for separation.340return w + 1;341}342343public:344KlassInfoHisto(KlassInfoTable* cit, const char* title);345~KlassInfoHisto();346void add(KlassInfoEntry* cie);347void print_histo_on(outputStream* st, bool print_class_stats, bool csv_format, const char *columns);348void sort();349};350351#endif // INCLUDE_SERVICES352353// These declarations are needed since teh declaration of KlassInfoTable and354// KlassInfoClosure are guarded by #if INLCUDE_SERVICES355class KlassInfoTable;356class KlassInfoClosure;357358class HeapInspection : public StackObj {359bool _csv_format; // "comma separated values" format for spreadsheet.360bool _print_help;361bool _print_class_stats;362const char* _columns;363public:364HeapInspection(bool csv_format, bool print_help,365bool print_class_stats, const char *columns) :366_csv_format(csv_format), _print_help(print_help),367_print_class_stats(print_class_stats), _columns(columns) {}368void heap_inspection(outputStream* st) NOT_SERVICES_RETURN;369size_t populate_table(KlassInfoTable* cit, BoolObjectClosure* filter = NULL) NOT_SERVICES_RETURN_(0);370static void find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) NOT_SERVICES_RETURN;371private:372void iterate_over_heap(KlassInfoTable* cit, BoolObjectClosure* filter = NULL);373};374375#endif // SHARE_VM_MEMORY_HEAPINSPECTION_HPP376377378