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