src/share/vm/memory/heapInspection.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_MEMORY_HEAPINSPECTION_HPP
aoqi@0 26 #define SHARE_VM_MEMORY_HEAPINSPECTION_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.inline.hpp"
aoqi@0 29 #include "oops/oop.inline.hpp"
aoqi@0 30 #include "oops/annotations.hpp"
aoqi@0 31 #include "utilities/macros.hpp"
aoqi@0 32
aoqi@0 33 #if INCLUDE_SERVICES
aoqi@0 34
aoqi@0 35
aoqi@0 36 // HeapInspection
aoqi@0 37
aoqi@0 38 // KlassInfoTable is a bucket hash table that
aoqi@0 39 // maps Klass*s to extra information:
aoqi@0 40 // instance count and instance word size.
aoqi@0 41 //
aoqi@0 42 // A KlassInfoBucket is the head of a link list
aoqi@0 43 // of KlassInfoEntry's
aoqi@0 44 //
aoqi@0 45 // KlassInfoHisto is a growable array of pointers
aoqi@0 46 // to KlassInfoEntry's and is used to sort
aoqi@0 47 // the entries.
aoqi@0 48
aoqi@0 49 #define HEAP_INSPECTION_COLUMNS_DO(f) \
aoqi@0 50 f(inst_size, InstSize, \
aoqi@0 51 "Size of each object instance of the Java class") \
aoqi@0 52 f(inst_count, InstCount, \
aoqi@0 53 "Number of object instances of the Java class") \
aoqi@0 54 f(inst_bytes, InstBytes, \
aoqi@0 55 "This is usually (InstSize * InstNum). The only exception is " \
aoqi@0 56 "java.lang.Class, whose InstBytes also includes the slots " \
aoqi@0 57 "used to store static fields. InstBytes is not counted in " \
aoqi@0 58 "ROAll, RWAll or Total") \
aoqi@0 59 f(mirror_bytes, Mirror, \
aoqi@0 60 "Size of the Klass::java_mirror() object") \
aoqi@0 61 f(klass_bytes, KlassBytes, \
aoqi@0 62 "Size of the InstanceKlass or ArrayKlass for this class. " \
aoqi@0 63 "Note that this includes VTab, ITab, OopMap") \
aoqi@0 64 f(secondary_supers_bytes, K_secondary_supers, \
aoqi@0 65 "Number of bytes used by the Klass::secondary_supers() array") \
aoqi@0 66 f(vtab_bytes, VTab, \
aoqi@0 67 "Size of the embedded vtable in InstanceKlass") \
aoqi@0 68 f(itab_bytes, ITab, \
aoqi@0 69 "Size of the embedded itable in InstanceKlass") \
aoqi@0 70 f(nonstatic_oopmap_bytes, OopMap, \
aoqi@0 71 "Size of the embedded nonstatic_oop_map in InstanceKlass") \
aoqi@0 72 f(methods_array_bytes, IK_methods, \
aoqi@0 73 "Number of bytes used by the InstanceKlass::methods() array") \
aoqi@0 74 f(method_ordering_bytes, IK_method_ordering, \
aoqi@0 75 "Number of bytes used by the InstanceKlass::method_ordering() array") \
aoqi@0 76 f(default_methods_array_bytes, IK_default_methods, \
aoqi@0 77 "Number of bytes used by the InstanceKlass::default_methods() array") \
aoqi@0 78 f(default_vtable_indices_bytes, IK_default_vtable_indices, \
aoqi@0 79 "Number of bytes used by the InstanceKlass::default_vtable_indices() array") \
aoqi@0 80 f(local_interfaces_bytes, IK_local_interfaces, \
aoqi@0 81 "Number of bytes used by the InstanceKlass::local_interfaces() array") \
aoqi@0 82 f(transitive_interfaces_bytes, IK_transitive_interfaces, \
aoqi@0 83 "Number of bytes used by the InstanceKlass::transitive_interfaces() array") \
aoqi@0 84 f(fields_bytes, IK_fields, \
aoqi@0 85 "Number of bytes used by the InstanceKlass::fields() array") \
aoqi@0 86 f(inner_classes_bytes, IK_inner_classes, \
aoqi@0 87 "Number of bytes used by the InstanceKlass::inner_classes() array") \
aoqi@0 88 f(signers_bytes, IK_signers, \
aoqi@0 89 "Number of bytes used by the InstanceKlass::singers() array") \
aoqi@0 90 f(class_annotations_bytes, class_annotations, \
aoqi@0 91 "Size of class annotations") \
aoqi@0 92 f(class_type_annotations_bytes, class_type_annotations, \
aoqi@0 93 "Size of class type annotations") \
aoqi@0 94 f(fields_annotations_bytes, fields_annotations, \
aoqi@0 95 "Size of field annotations") \
aoqi@0 96 f(fields_type_annotations_bytes, fields_type_annotations, \
aoqi@0 97 "Size of field type annotations") \
aoqi@0 98 f(methods_annotations_bytes, methods_annotations, \
aoqi@0 99 "Size of method annotations") \
aoqi@0 100 f(methods_parameter_annotations_bytes, methods_parameter_annotations, \
aoqi@0 101 "Size of method parameter annotations") \
aoqi@0 102 f(methods_type_annotations_bytes, methods_type_annotations, \
aoqi@0 103 "Size of methods type annotations") \
aoqi@0 104 f(methods_default_annotations_bytes, methods_default_annotations, \
aoqi@0 105 "Size of methods default annotations") \
aoqi@0 106 f(annotations_bytes, annotations, \
aoqi@0 107 "Size of all annotations") \
aoqi@0 108 f(cp_bytes, Cp, \
aoqi@0 109 "Size of InstanceKlass::constants()") \
aoqi@0 110 f(cp_tags_bytes, CpTags, \
aoqi@0 111 "Size of InstanceKlass::constants()->tags()") \
aoqi@0 112 f(cp_cache_bytes, CpCache, \
aoqi@0 113 "Size of InstanceKlass::constants()->cache()") \
aoqi@0 114 f(cp_operands_bytes, CpOperands, \
aoqi@0 115 "Size of InstanceKlass::constants()->operands()") \
aoqi@0 116 f(cp_refmap_bytes, CpRefMap, \
aoqi@0 117 "Size of InstanceKlass::constants()->reference_map()") \
aoqi@0 118 f(cp_all_bytes, CpAll, \
aoqi@0 119 "Sum of Cp + CpTags + CpCache + CpOperands + CpRefMap") \
aoqi@0 120 f(method_count, MethodCount, \
aoqi@0 121 "Number of methods in this class") \
aoqi@0 122 f(method_bytes, MethodBytes, \
aoqi@0 123 "Size of the Method object") \
aoqi@0 124 f(const_method_bytes, ConstMethod, \
aoqi@0 125 "Size of the ConstMethod object") \
aoqi@0 126 f(method_data_bytes, MethodData, \
aoqi@0 127 "Size of the MethodData object") \
aoqi@0 128 f(stackmap_bytes, StackMap, \
aoqi@0 129 "Size of the stackmap_data") \
aoqi@0 130 f(bytecode_bytes, Bytecodes, \
aoqi@0 131 "Of the MethodBytes column, how much are the space taken up by bytecodes") \
aoqi@0 132 f(method_all_bytes, MethodAll, \
aoqi@0 133 "Sum of MethodBytes + Constmethod + Stackmap + Methoddata") \
aoqi@0 134 f(ro_bytes, ROAll, \
aoqi@0 135 "Size of all class meta data that could (potentially) be placed " \
aoqi@0 136 "in read-only memory. (This could change with CDS design)") \
aoqi@0 137 f(rw_bytes, RWAll, \
aoqi@0 138 "Size of all class meta data that must be placed in read/write " \
aoqi@0 139 "memory. (This could change with CDS design) ") \
aoqi@0 140 f(total_bytes, Total, \
aoqi@0 141 "ROAll + RWAll. Note that this does NOT include InstBytes.")
aoqi@0 142
aoqi@0 143 // Size statistics for a Klass - filled in by Klass::collect_statistics()
aoqi@0 144 class KlassSizeStats {
aoqi@0 145 public:
aoqi@0 146 #define COUNT_KLASS_SIZE_STATS_FIELD(field, name, help) _index_ ## field,
aoqi@0 147 #define DECLARE_KLASS_SIZE_STATS_FIELD(field, name, help) julong _ ## field;
aoqi@0 148
aoqi@0 149 enum {
aoqi@0 150 HEAP_INSPECTION_COLUMNS_DO(COUNT_KLASS_SIZE_STATS_FIELD)
aoqi@0 151 _num_columns
aoqi@0 152 };
aoqi@0 153
aoqi@0 154 HEAP_INSPECTION_COLUMNS_DO(DECLARE_KLASS_SIZE_STATS_FIELD)
aoqi@0 155
aoqi@0 156 static int count(oop x) {
aoqi@0 157 return (HeapWordSize * (((x) != NULL) ? (x)->size() : 0));
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 static int count_array(objArrayOop x) {
aoqi@0 161 return (HeapWordSize * (((x) != NULL) ? (x)->size() : 0));
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 template <class T> static int count(T* x) {
aoqi@0 165 return (HeapWordSize * ((x) ? (x)->size() : 0));
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 template <class T> static int count_array(T* x) {
aoqi@0 169 if (x == NULL) {
aoqi@0 170 return 0;
aoqi@0 171 }
aoqi@0 172 if (x->length() == 0) {
aoqi@0 173 // This is a shared array, e.g., Universe::the_empty_int_array(). Don't
aoqi@0 174 // count it to avoid double-counting.
aoqi@0 175 return 0;
aoqi@0 176 }
aoqi@0 177 return HeapWordSize * x->size();
aoqi@0 178 }
aoqi@0 179 };
aoqi@0 180
aoqi@0 181
aoqi@0 182
aoqi@0 183
aoqi@0 184 class KlassInfoEntry: public CHeapObj<mtInternal> {
aoqi@0 185 private:
aoqi@0 186 KlassInfoEntry* _next;
aoqi@0 187 Klass* _klass;
aoqi@0 188 long _instance_count;
aoqi@0 189 size_t _instance_words;
aoqi@0 190 long _index;
aoqi@0 191
aoqi@0 192 public:
aoqi@0 193 KlassInfoEntry(Klass* k, KlassInfoEntry* next) :
aoqi@0 194 _klass(k), _instance_count(0), _instance_words(0), _next(next), _index(-1)
aoqi@0 195 {}
aoqi@0 196 KlassInfoEntry* next() const { return _next; }
aoqi@0 197 bool is_equal(const Klass* k) { return k == _klass; }
aoqi@0 198 Klass* klass() const { return _klass; }
aoqi@0 199 long count() const { return _instance_count; }
aoqi@0 200 void set_count(long ct) { _instance_count = ct; }
aoqi@0 201 size_t words() const { return _instance_words; }
aoqi@0 202 void set_words(size_t wds) { _instance_words = wds; }
aoqi@0 203 void set_index(long index) { _index = index; }
aoqi@0 204 long index() const { return _index; }
aoqi@0 205 int compare(KlassInfoEntry* e1, KlassInfoEntry* e2);
aoqi@0 206 void print_on(outputStream* st) const;
aoqi@0 207 const char* name() const;
aoqi@0 208 };
aoqi@0 209
aoqi@0 210 class KlassInfoClosure : public StackObj {
aoqi@0 211 public:
aoqi@0 212 // Called for each KlassInfoEntry.
aoqi@0 213 virtual void do_cinfo(KlassInfoEntry* cie) = 0;
aoqi@0 214 };
aoqi@0 215
aoqi@0 216 class KlassInfoBucket: public CHeapObj<mtInternal> {
aoqi@0 217 private:
aoqi@0 218 KlassInfoEntry* _list;
aoqi@0 219 KlassInfoEntry* list() { return _list; }
aoqi@0 220 void set_list(KlassInfoEntry* l) { _list = l; }
aoqi@0 221 public:
aoqi@0 222 KlassInfoEntry* lookup(Klass* k);
aoqi@0 223 void initialize() { _list = NULL; }
aoqi@0 224 void empty();
aoqi@0 225 void iterate(KlassInfoClosure* cic);
aoqi@0 226 };
aoqi@0 227
aoqi@0 228 class KlassInfoTable: public StackObj {
aoqi@0 229 private:
aoqi@0 230 int _size;
aoqi@0 231 static const int _num_buckets = 20011;
aoqi@0 232 size_t _size_of_instances_in_words;
aoqi@0 233
aoqi@0 234 // An aligned reference address (typically the least
aoqi@0 235 // address in the perm gen) used for hashing klass
aoqi@0 236 // objects.
aoqi@0 237 HeapWord* _ref;
aoqi@0 238
aoqi@0 239 KlassInfoBucket* _buckets;
aoqi@0 240 uint hash(const Klass* p);
aoqi@0 241 KlassInfoEntry* lookup(Klass* k); // allocates if not found!
aoqi@0 242
aoqi@0 243 class AllClassesFinder : public KlassClosure {
aoqi@0 244 KlassInfoTable *_table;
aoqi@0 245 public:
aoqi@0 246 AllClassesFinder(KlassInfoTable* table) : _table(table) {}
aoqi@0 247 virtual void do_klass(Klass* k);
aoqi@0 248 };
aoqi@0 249
aoqi@0 250 public:
aoqi@0 251 KlassInfoTable(bool need_class_stats);
aoqi@0 252 ~KlassInfoTable();
aoqi@0 253 bool record_instance(const oop obj);
aoqi@0 254 void iterate(KlassInfoClosure* cic);
aoqi@0 255 bool allocation_failed() { return _buckets == NULL; }
aoqi@0 256 size_t size_of_instances_in_words() const;
aoqi@0 257
aoqi@0 258 friend class KlassInfoHisto;
aoqi@0 259 };
aoqi@0 260
aoqi@0 261 class KlassInfoHisto : public StackObj {
aoqi@0 262 private:
aoqi@0 263 static const int _histo_initial_size = 1000;
aoqi@0 264 KlassInfoTable *_cit;
aoqi@0 265 GrowableArray<KlassInfoEntry*>* _elements;
aoqi@0 266 GrowableArray<KlassInfoEntry*>* elements() const { return _elements; }
aoqi@0 267 const char* _title;
aoqi@0 268 const char* title() const { return _title; }
aoqi@0 269 static int sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2);
aoqi@0 270 void print_elements(outputStream* st) const;
aoqi@0 271 void print_class_stats(outputStream* st, bool csv_format, const char *columns);
aoqi@0 272 julong annotations_bytes(Array<AnnotationArray*>* p) const;
aoqi@0 273 const char *_selected_columns;
aoqi@0 274 bool is_selected(const char *col_name);
aoqi@0 275 void print_title(outputStream* st, bool csv_format,
aoqi@0 276 bool selected_columns_table[], int width_table[],
aoqi@0 277 const char *name_table[]);
aoqi@0 278
aoqi@0 279 template <class T> static int count_bytes(T* x) {
aoqi@0 280 return (HeapWordSize * ((x) ? (x)->size() : 0));
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 template <class T> static int count_bytes_array(T* x) {
aoqi@0 284 if (x == NULL) {
aoqi@0 285 return 0;
aoqi@0 286 }
aoqi@0 287 if (x->length() == 0) {
aoqi@0 288 // This is a shared array, e.g., Universe::the_empty_int_array(). Don't
aoqi@0 289 // count it to avoid double-counting.
aoqi@0 290 return 0;
aoqi@0 291 }
aoqi@0 292 return HeapWordSize * x->size();
aoqi@0 293 }
aoqi@0 294
aoqi@0 295 // returns a format string to print a julong with the given width. E.g,
aoqi@0 296 // printf(num_fmt(6), julong(10)) would print out the number 10 with 4
aoqi@0 297 // leading spaces.
aoqi@0 298 PRAGMA_DIAG_PUSH
aoqi@0 299 PRAGMA_FORMAT_NONLITERAL_IGNORED
aoqi@0 300
aoqi@0 301 static void print_julong(outputStream* st, int width, julong n) {
aoqi@0 302 int num_spaces = width - julong_width(n);
aoqi@0 303 if (num_spaces > 0) {
aoqi@0 304 st->print(str_fmt(num_spaces), "");
aoqi@0 305 }
aoqi@0 306 st->print(JULONG_FORMAT, n);
aoqi@0 307 }
aoqi@0 308 PRAGMA_DIAG_POP
aoqi@0 309
aoqi@0 310 static char* perc_fmt(int width) {
aoqi@0 311 static char buf[32];
aoqi@0 312 jio_snprintf(buf, sizeof(buf), "%%%d.1f%%%%", width-1);
aoqi@0 313 return buf;
aoqi@0 314 }
aoqi@0 315
aoqi@0 316 static char* str_fmt(int width) {
aoqi@0 317 static char buf[32];
aoqi@0 318 jio_snprintf(buf, sizeof(buf), "%%%ds", width);
aoqi@0 319 return buf;
aoqi@0 320 }
aoqi@0 321
aoqi@0 322 static int julong_width(julong n) {
aoqi@0 323 if (n == 0) {
aoqi@0 324 return 1;
aoqi@0 325 }
aoqi@0 326 int w = 0;
aoqi@0 327 while (n > 0) {
aoqi@0 328 n /= 10;
aoqi@0 329 w += 1;
aoqi@0 330 }
aoqi@0 331 return w;
aoqi@0 332 }
aoqi@0 333
aoqi@0 334 static int col_width(julong n, const char *name) {
aoqi@0 335 int w = julong_width(n);
aoqi@0 336 int min = (int)(strlen(name));
aoqi@0 337 if (w < min) {
aoqi@0 338 w = min;
aoqi@0 339 }
aoqi@0 340 // add a leading space for separation.
aoqi@0 341 return w + 1;
aoqi@0 342 }
aoqi@0 343
aoqi@0 344 public:
aoqi@0 345 KlassInfoHisto(KlassInfoTable* cit, const char* title);
aoqi@0 346 ~KlassInfoHisto();
aoqi@0 347 void add(KlassInfoEntry* cie);
aoqi@0 348 void print_histo_on(outputStream* st, bool print_class_stats, bool csv_format, const char *columns);
aoqi@0 349 void sort();
aoqi@0 350 };
aoqi@0 351
aoqi@0 352 #endif // INCLUDE_SERVICES
aoqi@0 353
aoqi@0 354 // These declarations are needed since teh declaration of KlassInfoTable and
aoqi@0 355 // KlassInfoClosure are guarded by #if INLCUDE_SERVICES
aoqi@0 356 class KlassInfoTable;
aoqi@0 357 class KlassInfoClosure;
aoqi@0 358
aoqi@0 359 class HeapInspection : public StackObj {
aoqi@0 360 bool _csv_format; // "comma separated values" format for spreadsheet.
aoqi@0 361 bool _print_help;
aoqi@0 362 bool _print_class_stats;
aoqi@0 363 const char* _columns;
aoqi@0 364 public:
aoqi@0 365 HeapInspection(bool csv_format, bool print_help,
aoqi@0 366 bool print_class_stats, const char *columns) :
aoqi@0 367 _csv_format(csv_format), _print_help(print_help),
aoqi@0 368 _print_class_stats(print_class_stats), _columns(columns) {}
aoqi@0 369 void heap_inspection(outputStream* st) NOT_SERVICES_RETURN;
aoqi@0 370 size_t populate_table(KlassInfoTable* cit, BoolObjectClosure* filter = NULL) NOT_SERVICES_RETURN;
aoqi@0 371 static void find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) NOT_SERVICES_RETURN;
aoqi@0 372 private:
aoqi@0 373 void iterate_over_heap(KlassInfoTable* cit, BoolObjectClosure* filter = NULL);
aoqi@0 374 };
aoqi@0 375
aoqi@0 376 #endif // SHARE_VM_MEMORY_HEAPINSPECTION_HPP

mercurial