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