duke@435: /* acorn@4497: * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_MEMORY_HEAPINSPECTION_HPP stefank@2314: #define SHARE_VM_MEMORY_HEAPINSPECTION_HPP stefank@2314: stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "oops/oop.inline.hpp" acorn@4497: #include "oops/annotations.hpp" jprovino@4542: #include "utilities/macros.hpp" stefank@2314: jprovino@4165: #if INCLUDE_SERVICES duke@435: duke@435: duke@435: // HeapInspection duke@435: duke@435: // KlassInfoTable is a bucket hash table that coleenp@4037: // maps Klass*s to extra information: duke@435: // instance count and instance word size. duke@435: // duke@435: // A KlassInfoBucket is the head of a link list duke@435: // of KlassInfoEntry's duke@435: // duke@435: // KlassInfoHisto is a growable array of pointers duke@435: // to KlassInfoEntry's and is used to sort duke@435: // the entries. duke@435: acorn@4497: #define HEAP_INSPECTION_COLUMNS_DO(f) \ acorn@4497: f(inst_size, InstSize, \ acorn@4497: "Size of each object instance of the Java class") \ acorn@4497: f(inst_count, InstCount, \ acorn@4497: "Number of object instances of the Java class") \ acorn@4497: f(inst_bytes, InstBytes, \ acorn@4497: "This is usually (InstSize * InstNum). The only exception is " \ acorn@4497: "java.lang.Class, whose InstBytes also includes the slots " \ acorn@4497: "used to store static fields. InstBytes is not counted in " \ acorn@4497: "ROAll, RWAll or Total") \ acorn@4497: f(mirror_bytes, Mirror, \ acorn@4497: "Size of the Klass::java_mirror() object") \ acorn@4497: f(klass_bytes, KlassBytes, \ acorn@4497: "Size of the InstanceKlass or ArrayKlass for this class. " \ acorn@4497: "Note that this includes VTab, ITab, OopMap") \ acorn@4497: f(secondary_supers_bytes, K_secondary_supers, \ acorn@4497: "Number of bytes used by the Klass::secondary_supers() array") \ acorn@4497: f(vtab_bytes, VTab, \ acorn@4497: "Size of the embedded vtable in InstanceKlass") \ acorn@4497: f(itab_bytes, ITab, \ acorn@4497: "Size of the embedded itable in InstanceKlass") \ acorn@4497: f(nonstatic_oopmap_bytes, OopMap, \ acorn@4497: "Size of the embedded nonstatic_oop_map in InstanceKlass") \ acorn@4497: f(methods_array_bytes, IK_methods, \ acorn@4497: "Number of bytes used by the InstanceKlass::methods() array") \ acorn@4497: f(method_ordering_bytes, IK_method_ordering, \ acorn@4497: "Number of bytes used by the InstanceKlass::method_ordering() array") \ acorn@4497: f(local_interfaces_bytes, IK_local_interfaces, \ acorn@4497: "Number of bytes used by the InstanceKlass::local_interfaces() array") \ acorn@4497: f(transitive_interfaces_bytes, IK_transitive_interfaces, \ acorn@4497: "Number of bytes used by the InstanceKlass::transitive_interfaces() array") \ acorn@4497: f(fields_bytes, IK_fields, \ acorn@4497: "Number of bytes used by the InstanceKlass::fields() array") \ acorn@4497: f(inner_classes_bytes, IK_inner_classes, \ acorn@4497: "Number of bytes used by the InstanceKlass::inner_classes() array") \ acorn@4497: f(signers_bytes, IK_signers, \ acorn@4497: "Number of bytes used by the InstanceKlass::singers() array") \ acorn@4497: f(class_annotations_bytes, class_annotations, \ acorn@4497: "Size of class annotations") \ coleenp@4572: f(class_type_annotations_bytes, class_type_annotations, \ coleenp@4572: "Size of class type annotations") \ acorn@4497: f(fields_annotations_bytes, fields_annotations, \ acorn@4497: "Size of field annotations") \ coleenp@4572: f(fields_type_annotations_bytes, fields_type_annotations, \ coleenp@4572: "Size of field type annotations") \ acorn@4497: f(methods_annotations_bytes, methods_annotations, \ acorn@4497: "Size of method annotations") \ acorn@4497: f(methods_parameter_annotations_bytes, methods_parameter_annotations, \ acorn@4497: "Size of method parameter annotations") \ coleenp@4572: f(methods_type_annotations_bytes, methods_type_annotations, \ coleenp@4572: "Size of methods type annotations") \ acorn@4497: f(methods_default_annotations_bytes, methods_default_annotations, \ acorn@4497: "Size of methods default annotations") \ acorn@4497: f(annotations_bytes, annotations, \ acorn@4497: "Size of all annotations") \ acorn@4497: f(cp_bytes, Cp, \ acorn@4497: "Size of InstanceKlass::constants()") \ acorn@4497: f(cp_tags_bytes, CpTags, \ acorn@4497: "Size of InstanceKlass::constants()->tags()") \ acorn@4497: f(cp_cache_bytes, CpCache, \ acorn@4497: "Size of InstanceKlass::constants()->cache()") \ acorn@4497: f(cp_operands_bytes, CpOperands, \ acorn@4497: "Size of InstanceKlass::constants()->operands()") \ acorn@4497: f(cp_refmap_bytes, CpRefMap, \ acorn@4497: "Size of InstanceKlass::constants()->reference_map()") \ acorn@4497: f(cp_all_bytes, CpAll, \ acorn@4497: "Sum of Cp + CpTags + CpCache + CpOperands + CpRefMap") \ acorn@4497: f(method_count, MethodCount, \ acorn@4497: "Number of methods in this class") \ acorn@4497: f(method_bytes, MethodBytes, \ acorn@4497: "Size of the Method object") \ acorn@4497: f(const_method_bytes, ConstMethod, \ acorn@4497: "Size of the ConstMethod object") \ acorn@4497: f(method_data_bytes, MethodData, \ acorn@4497: "Size of the MethodData object") \ acorn@4497: f(stackmap_bytes, StackMap, \ acorn@4497: "Size of the stackmap_data") \ acorn@4497: f(bytecode_bytes, Bytecodes, \ acorn@4497: "Of the MethodBytes column, how much are the space taken up by bytecodes") \ acorn@4497: f(method_all_bytes, MethodAll, \ acorn@4497: "Sum of MethodBytes + Constmethod + Stackmap + Methoddata") \ acorn@4497: f(ro_bytes, ROAll, \ acorn@4497: "Size of all class meta data that could (potentially) be placed " \ acorn@4497: "in read-only memory. (This could change with CDS design)") \ acorn@4497: f(rw_bytes, RWAll, \ acorn@4497: "Size of all class meta data that must be placed in read/write " \ acorn@4497: "memory. (This could change with CDS design) ") \ acorn@4497: f(total_bytes, Total, \ acorn@4497: "ROAll + RWAll. Note that this does NOT include InstBytes.") acorn@4497: acorn@4497: // Size statistics for a Klass - filled in by Klass::collect_statistics() acorn@4497: class KlassSizeStats { acorn@4497: public: acorn@4497: #define COUNT_KLASS_SIZE_STATS_FIELD(field, name, help) _index_ ## field, acorn@4497: #define DECLARE_KLASS_SIZE_STATS_FIELD(field, name, help) julong _ ## field; acorn@4497: acorn@4497: enum { acorn@4497: HEAP_INSPECTION_COLUMNS_DO(COUNT_KLASS_SIZE_STATS_FIELD) acorn@4497: _num_columns acorn@4497: }; acorn@4497: acorn@4497: HEAP_INSPECTION_COLUMNS_DO(DECLARE_KLASS_SIZE_STATS_FIELD) acorn@4497: acorn@4497: static int count(oop x) { acorn@4497: return (HeapWordSize * ((x) ? (x)->size() : 0)); acorn@4497: } acorn@4497: acorn@4497: static int count_array(objArrayOop x) { acorn@4497: return (HeapWordSize * ((x) ? (x)->size() : 0)); acorn@4497: } acorn@4497: acorn@4497: template static int count(T* x) { acorn@4497: return (HeapWordSize * ((x) ? (x)->size() : 0)); acorn@4497: } acorn@4497: acorn@4497: template static int count_array(T* x) { acorn@4497: if (x == NULL) { acorn@4497: return 0; acorn@4497: } acorn@4497: if (x->length() == 0) { acorn@4497: // This is a shared array, e.g., Universe::the_empty_int_array(). Don't acorn@4497: // count it to avoid double-counting. acorn@4497: return 0; acorn@4497: } acorn@4497: return HeapWordSize * x->size(); acorn@4497: } acorn@4497: }; acorn@4497: acorn@4497: acorn@4497: acorn@4497: zgu@3900: class KlassInfoEntry: public CHeapObj { duke@435: private: duke@435: KlassInfoEntry* _next; coleenp@4037: Klass* _klass; duke@435: long _instance_count; duke@435: size_t _instance_words; acorn@4497: long _index; duke@435: duke@435: public: coleenp@4037: KlassInfoEntry(Klass* k, KlassInfoEntry* next) : acorn@4497: _klass(k), _instance_count(0), _instance_words(0), _next(next), _index(-1) duke@435: {} minqi@5097: KlassInfoEntry* next() const { return _next; } minqi@5097: bool is_equal(const Klass* k) { return k == _klass; } minqi@5097: Klass* klass() const { return _klass; } minqi@5097: long count() const { return _instance_count; } duke@435: void set_count(long ct) { _instance_count = ct; } minqi@5097: size_t words() const { return _instance_words; } duke@435: void set_words(size_t wds) { _instance_words = wds; } acorn@4497: void set_index(long index) { _index = index; } minqi@5097: long index() const { return _index; } duke@435: int compare(KlassInfoEntry* e1, KlassInfoEntry* e2); duke@435: void print_on(outputStream* st) const; acorn@4497: const char* name() const; duke@435: }; duke@435: ehelin@5386: class KlassInfoClosure : public StackObj { ehelin@5386: public: ehelin@5386: // Called for each KlassInfoEntry. ehelin@5386: virtual void do_cinfo(KlassInfoEntry* cie) = 0; ehelin@5386: }; ehelin@5386: zgu@3900: class KlassInfoBucket: public CHeapObj { duke@435: private: duke@435: KlassInfoEntry* _list; duke@435: KlassInfoEntry* list() { return _list; } duke@435: void set_list(KlassInfoEntry* l) { _list = l; } duke@435: public: minqi@5097: KlassInfoEntry* lookup(Klass* k); duke@435: void initialize() { _list = NULL; } duke@435: void empty(); duke@435: void iterate(KlassInfoClosure* cic); duke@435: }; duke@435: duke@435: class KlassInfoTable: public StackObj { duke@435: private: duke@435: int _size; sla@5237: static const int _num_buckets = 20011; sla@5237: size_t _size_of_instances_in_words; duke@435: duke@435: // An aligned reference address (typically the least duke@435: // address in the perm gen) used for hashing klass duke@435: // objects. duke@435: HeapWord* _ref; duke@435: duke@435: KlassInfoBucket* _buckets; minqi@5097: uint hash(const Klass* p); minqi@5097: KlassInfoEntry* lookup(Klass* k); // allocates if not found! acorn@4497: acorn@4497: class AllClassesFinder : public KlassClosure { acorn@4497: KlassInfoTable *_table; acorn@4497: public: acorn@4497: AllClassesFinder(KlassInfoTable* table) : _table(table) {} acorn@4497: virtual void do_klass(Klass* k); acorn@4497: }; duke@435: duke@435: public: sla@5237: KlassInfoTable(bool need_class_stats); duke@435: ~KlassInfoTable(); ysr@446: bool record_instance(const oop obj); duke@435: void iterate(KlassInfoClosure* cic); ysr@446: bool allocation_failed() { return _buckets == NULL; } sla@5237: size_t size_of_instances_in_words() const; acorn@4497: acorn@4497: friend class KlassInfoHisto; duke@435: }; duke@435: duke@435: class KlassInfoHisto : public StackObj { duke@435: private: sla@5237: static const int _histo_initial_size = 1000; acorn@4497: KlassInfoTable *_cit; duke@435: GrowableArray* _elements; duke@435: GrowableArray* elements() const { return _elements; } duke@435: const char* _title; duke@435: const char* title() const { return _title; } duke@435: static int sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2); duke@435: void print_elements(outputStream* st) const; acorn@4497: void print_class_stats(outputStream* st, bool csv_format, const char *columns); acorn@4497: julong annotations_bytes(Array* p) const; acorn@4497: const char *_selected_columns; acorn@4497: bool is_selected(const char *col_name); acorn@4497: void print_title(outputStream* st, bool csv_format, acorn@4497: bool selected_columns_table[], int width_table[], acorn@4497: const char *name_table[]); acorn@4497: acorn@4497: template static int count_bytes(T* x) { acorn@4497: return (HeapWordSize * ((x) ? (x)->size() : 0)); acorn@4497: } acorn@4497: acorn@4497: template static int count_bytes_array(T* x) { acorn@4497: if (x == NULL) { acorn@4497: return 0; acorn@4497: } acorn@4497: if (x->length() == 0) { acorn@4497: // This is a shared array, e.g., Universe::the_empty_int_array(). Don't acorn@4497: // count it to avoid double-counting. acorn@4497: return 0; acorn@4497: } acorn@4497: return HeapWordSize * x->size(); acorn@4497: } acorn@4497: acorn@4497: // returns a format string to print a julong with the given width. E.g, acorn@4497: // printf(num_fmt(6), julong(10)) would print out the number 10 with 4 acorn@4497: // leading spaces. acorn@4497: static void print_julong(outputStream* st, int width, julong n) { acorn@4497: int num_spaces = width - julong_width(n); acorn@4497: if (num_spaces > 0) { acorn@4497: st->print(str_fmt(num_spaces), ""); acorn@4497: } acorn@4497: st->print(JULONG_FORMAT, n); acorn@4497: } acorn@4497: acorn@4497: static char* perc_fmt(int width) { acorn@4497: static char buf[32]; acorn@4497: jio_snprintf(buf, sizeof(buf), "%%%d.1f%%%%", width-1); acorn@4497: return buf; acorn@4497: } acorn@4497: acorn@4497: static char* str_fmt(int width) { acorn@4497: static char buf[32]; acorn@4497: jio_snprintf(buf, sizeof(buf), "%%%ds", width); acorn@4497: return buf; acorn@4497: } acorn@4497: acorn@4497: static int julong_width(julong n) { acorn@4497: if (n == 0) { acorn@4497: return 1; acorn@4497: } acorn@4497: int w = 0; acorn@4497: while (n > 0) { acorn@4497: n /= 10; acorn@4497: w += 1; acorn@4497: } acorn@4497: return w; acorn@4497: } acorn@4497: acorn@4497: static int col_width(julong n, const char *name) { acorn@4497: int w = julong_width(n); acorn@4497: int min = (int)(strlen(name)); acorn@4497: if (w < min) { acorn@4497: w = min; acorn@4497: } acorn@4497: // add a leading space for separation. acorn@4497: return w + 1; acorn@4497: } acorn@4497: duke@435: public: sla@5237: KlassInfoHisto(KlassInfoTable* cit, const char* title); duke@435: ~KlassInfoHisto(); duke@435: void add(KlassInfoEntry* cie); acorn@4497: void print_histo_on(outputStream* st, bool print_class_stats, bool csv_format, const char *columns); duke@435: void sort(); duke@435: }; duke@435: jprovino@4165: #endif // INCLUDE_SERVICES duke@435: sla@5237: // These declarations are needed since teh declaration of KlassInfoTable and sla@5237: // KlassInfoClosure are guarded by #if INLCUDE_SERVICES sla@5237: class KlassInfoTable; sla@5237: class KlassInfoClosure; sla@5237: acorn@4497: class HeapInspection : public StackObj { acorn@4497: bool _csv_format; // "comma separated values" format for spreadsheet. acorn@4497: bool _print_help; acorn@4497: bool _print_class_stats; acorn@4497: const char* _columns; duke@435: public: acorn@4497: HeapInspection(bool csv_format, bool print_help, acorn@4497: bool print_class_stats, const char *columns) : acorn@4497: _csv_format(csv_format), _print_help(print_help), acorn@4497: _print_class_stats(print_class_stats), _columns(columns) {} sla@5237: void heap_inspection(outputStream* st) NOT_SERVICES_RETURN; sla@5237: size_t populate_table(KlassInfoTable* cit, BoolObjectClosure* filter = NULL) NOT_SERVICES_RETURN; jprovino@4165: static void find_instances_at_safepoint(Klass* k, GrowableArray* result) NOT_SERVICES_RETURN; sla@5237: private: sla@5237: void iterate_over_heap(KlassInfoTable* cit, BoolObjectClosure* filter = NULL); duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_MEMORY_HEAPINSPECTION_HPP