42 // |
43 // |
43 // KlassInfoHisto is a growable array of pointers |
44 // KlassInfoHisto is a growable array of pointers |
44 // to KlassInfoEntry's and is used to sort |
45 // to KlassInfoEntry's and is used to sort |
45 // the entries. |
46 // the entries. |
46 |
47 |
|
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.") |
|
133 |
|
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; |
|
139 |
|
140 enum { |
|
141 HEAP_INSPECTION_COLUMNS_DO(COUNT_KLASS_SIZE_STATS_FIELD) |
|
142 _num_columns |
|
143 }; |
|
144 |
|
145 HEAP_INSPECTION_COLUMNS_DO(DECLARE_KLASS_SIZE_STATS_FIELD) |
|
146 |
|
147 static int count(oop x) { |
|
148 return (HeapWordSize * ((x) ? (x)->size() : 0)); |
|
149 } |
|
150 |
|
151 static int count_array(objArrayOop x) { |
|
152 return (HeapWordSize * ((x) ? (x)->size() : 0)); |
|
153 } |
|
154 |
|
155 template <class T> static int count(T* x) { |
|
156 return (HeapWordSize * ((x) ? (x)->size() : 0)); |
|
157 } |
|
158 |
|
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 }; |
|
171 |
|
172 |
|
173 |
|
174 |
47 class KlassInfoEntry: public CHeapObj<mtInternal> { |
175 class KlassInfoEntry: public CHeapObj<mtInternal> { |
48 private: |
176 private: |
49 KlassInfoEntry* _next; |
177 KlassInfoEntry* _next; |
50 Klass* _klass; |
178 Klass* _klass; |
51 long _instance_count; |
179 long _instance_count; |
52 size_t _instance_words; |
180 size_t _instance_words; |
|
181 long _index; |
53 |
182 |
54 public: |
183 public: |
55 KlassInfoEntry(Klass* k, KlassInfoEntry* next) : |
184 KlassInfoEntry(Klass* k, KlassInfoEntry* next) : |
56 _klass(k), _instance_count(0), _instance_words(0), _next(next) |
185 _klass(k), _instance_count(0), _instance_words(0), _next(next), _index(-1) |
57 {} |
186 {} |
58 KlassInfoEntry* next() { return _next; } |
187 KlassInfoEntry* next() { return _next; } |
59 bool is_equal(Klass* k) { return k == _klass; } |
188 bool is_equal(Klass* k) { return k == _klass; } |
60 Klass* klass() { return _klass; } |
189 Klass* klass() { return _klass; } |
61 long count() { return _instance_count; } |
190 long count() { return _instance_count; } |
62 void set_count(long ct) { _instance_count = ct; } |
191 void set_count(long ct) { _instance_count = ct; } |
63 size_t words() { return _instance_words; } |
192 size_t words() { return _instance_words; } |
64 void set_words(size_t wds) { _instance_words = wds; } |
193 void set_words(size_t wds) { _instance_words = wds; } |
|
194 void set_index(long index) { _index = index; } |
|
195 long index() { return _index; } |
65 int compare(KlassInfoEntry* e1, KlassInfoEntry* e2); |
196 int compare(KlassInfoEntry* e1, KlassInfoEntry* e2); |
66 void print_on(outputStream* st) const; |
197 void print_on(outputStream* st) const; |
|
198 const char* name() const; |
67 }; |
199 }; |
68 |
200 |
69 class KlassInfoClosure: public StackObj { |
201 class KlassInfoClosure: public StackObj { |
70 public: |
202 public: |
71 // Called for each KlassInfoEntry. |
203 // Called for each KlassInfoEntry. |
93 // objects. |
225 // objects. |
94 HeapWord* _ref; |
226 HeapWord* _ref; |
95 |
227 |
96 KlassInfoBucket* _buckets; |
228 KlassInfoBucket* _buckets; |
97 uint hash(Klass* p); |
229 uint hash(Klass* p); |
98 KlassInfoEntry* lookup(Klass* const k); |
230 KlassInfoEntry* lookup(Klass* const k); // allocates if not found! |
|
231 |
|
232 class AllClassesFinder : public KlassClosure { |
|
233 KlassInfoTable *_table; |
|
234 public: |
|
235 AllClassesFinder(KlassInfoTable* table) : _table(table) {} |
|
236 virtual void do_klass(Klass* k); |
|
237 }; |
99 |
238 |
100 public: |
239 public: |
101 // Table size |
240 // Table size |
102 enum { |
241 enum { |
103 cit_size = 20011 |
242 cit_size = 20011 |
104 }; |
243 }; |
105 KlassInfoTable(int size, HeapWord* ref); |
244 KlassInfoTable(int size, HeapWord* ref, bool need_class_stats); |
106 ~KlassInfoTable(); |
245 ~KlassInfoTable(); |
107 bool record_instance(const oop obj); |
246 bool record_instance(const oop obj); |
108 void iterate(KlassInfoClosure* cic); |
247 void iterate(KlassInfoClosure* cic); |
109 bool allocation_failed() { return _buckets == NULL; } |
248 bool allocation_failed() { return _buckets == NULL; } |
|
249 |
|
250 friend class KlassInfoHisto; |
110 }; |
251 }; |
111 |
252 |
112 class KlassInfoHisto : public StackObj { |
253 class KlassInfoHisto : public StackObj { |
113 private: |
254 private: |
|
255 KlassInfoTable *_cit; |
114 GrowableArray<KlassInfoEntry*>* _elements; |
256 GrowableArray<KlassInfoEntry*>* _elements; |
115 GrowableArray<KlassInfoEntry*>* elements() const { return _elements; } |
257 GrowableArray<KlassInfoEntry*>* elements() const { return _elements; } |
116 const char* _title; |
258 const char* _title; |
117 const char* title() const { return _title; } |
259 const char* title() const { return _title; } |
118 static int sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2); |
260 static int sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2); |
119 void print_elements(outputStream* st) const; |
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[]); |
|
269 |
|
270 template <class T> static int count_bytes(T* x) { |
|
271 return (HeapWordSize * ((x) ? (x)->size() : 0)); |
|
272 } |
|
273 |
|
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 } |
|
285 |
|
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 } |
|
296 |
|
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 } |
|
302 |
|
303 static char* str_fmt(int width) { |
|
304 static char buf[32]; |
|
305 jio_snprintf(buf, sizeof(buf), "%%%ds", width); |
|
306 return buf; |
|
307 } |
|
308 |
|
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 } |
|
320 |
|
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 } |
|
330 |
120 public: |
331 public: |
121 enum { |
332 enum { |
122 histo_initial_size = 1000 |
333 histo_initial_size = 1000 |
123 }; |
334 }; |
124 KlassInfoHisto(const char* title, |
335 KlassInfoHisto(KlassInfoTable* cit, const char* title, |
125 int estimatedCount); |
336 int estimatedCount); |
126 ~KlassInfoHisto(); |
337 ~KlassInfoHisto(); |
127 void add(KlassInfoEntry* cie); |
338 void add(KlassInfoEntry* cie); |
128 void print_on(outputStream* st) const; |
339 void print_histo_on(outputStream* st, bool print_class_stats, bool csv_format, const char *columns); |
129 void sort(); |
340 void sort(); |
130 }; |
341 }; |
131 |
342 |
132 #endif // INCLUDE_SERVICES |
343 #endif // INCLUDE_SERVICES |
133 |
344 |
134 class HeapInspection : public AllStatic { |
345 class HeapInspection : public StackObj { |
135 public: |
346 bool _csv_format; // "comma separated values" format for spreadsheet. |
136 static void heap_inspection(outputStream* st, bool need_prologue) NOT_SERVICES_RETURN; |
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; |
137 static void find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) NOT_SERVICES_RETURN; |
356 static void find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) NOT_SERVICES_RETURN; |
138 }; |
357 }; |
139 |
358 |
140 #endif // SHARE_VM_MEMORY_HEAPINSPECTION_HPP |
359 #endif // SHARE_VM_MEMORY_HEAPINSPECTION_HPP |