|
1 /* |
|
2 * Copyright 2002-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 # include "incls/_precompiled.incl" |
|
26 # include "incls/_heapInspection.cpp.incl" |
|
27 |
|
28 // HeapInspection |
|
29 |
|
30 int KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) { |
|
31 if(e1->_instance_words > e2->_instance_words) { |
|
32 return -1; |
|
33 } else if(e1->_instance_words < e2->_instance_words) { |
|
34 return 1; |
|
35 } |
|
36 return 0; |
|
37 } |
|
38 |
|
39 void KlassInfoEntry::print_on(outputStream* st) const { |
|
40 ResourceMark rm; |
|
41 const char* name;; |
|
42 if (_klass->klass_part()->name() != NULL) { |
|
43 name = _klass->klass_part()->external_name(); |
|
44 } else { |
|
45 if (_klass == Universe::klassKlassObj()) name = "<klassKlass>"; else |
|
46 if (_klass == Universe::arrayKlassKlassObj()) name = "<arrayKlassKlass>"; else |
|
47 if (_klass == Universe::objArrayKlassKlassObj()) name = "<objArrayKlassKlass>"; else |
|
48 if (_klass == Universe::instanceKlassKlassObj()) name = "<instanceKlassKlass>"; else |
|
49 if (_klass == Universe::typeArrayKlassKlassObj()) name = "<typeArrayKlassKlass>"; else |
|
50 if (_klass == Universe::symbolKlassObj()) name = "<symbolKlass>"; else |
|
51 if (_klass == Universe::boolArrayKlassObj()) name = "<boolArrayKlass>"; else |
|
52 if (_klass == Universe::charArrayKlassObj()) name = "<charArrayKlass>"; else |
|
53 if (_klass == Universe::singleArrayKlassObj()) name = "<singleArrayKlass>"; else |
|
54 if (_klass == Universe::doubleArrayKlassObj()) name = "<doubleArrayKlass>"; else |
|
55 if (_klass == Universe::byteArrayKlassObj()) name = "<byteArrayKlass>"; else |
|
56 if (_klass == Universe::shortArrayKlassObj()) name = "<shortArrayKlass>"; else |
|
57 if (_klass == Universe::intArrayKlassObj()) name = "<intArrayKlass>"; else |
|
58 if (_klass == Universe::longArrayKlassObj()) name = "<longArrayKlass>"; else |
|
59 if (_klass == Universe::methodKlassObj()) name = "<methodKlass>"; else |
|
60 if (_klass == Universe::constMethodKlassObj()) name = "<constMethodKlass>"; else |
|
61 if (_klass == Universe::methodDataKlassObj()) name = "<methodDataKlass>"; else |
|
62 if (_klass == Universe::constantPoolKlassObj()) name = "<constantPoolKlass>"; else |
|
63 if (_klass == Universe::constantPoolCacheKlassObj()) name = "<constantPoolCacheKlass>"; else |
|
64 if (_klass == Universe::compiledICHolderKlassObj()) name = "<compiledICHolderKlass>"; else |
|
65 name = "<no name>"; |
|
66 } |
|
67 // simplify the formatting (ILP32 vs LP64) - always cast the numbers to 64-bit |
|
68 st->print_cr("%13" FORMAT64_MODIFIER "d %13" FORMAT64_MODIFIER "u %s", |
|
69 (jlong) _instance_count, |
|
70 (julong) _instance_words * HeapWordSize, |
|
71 name); |
|
72 } |
|
73 |
|
74 KlassInfoEntry* KlassInfoBucket::lookup(const klassOop k) { |
|
75 KlassInfoEntry* elt = _list; |
|
76 while (elt != NULL) { |
|
77 if (elt->is_equal(k)) { |
|
78 return elt; |
|
79 } |
|
80 elt = elt->next(); |
|
81 } |
|
82 elt = new KlassInfoEntry(k, list()); |
|
83 set_list(elt); |
|
84 return elt; |
|
85 } |
|
86 |
|
87 void KlassInfoBucket::iterate(KlassInfoClosure* cic) { |
|
88 KlassInfoEntry* elt = _list; |
|
89 while (elt != NULL) { |
|
90 cic->do_cinfo(elt); |
|
91 elt = elt->next(); |
|
92 } |
|
93 } |
|
94 |
|
95 void KlassInfoBucket::empty() { |
|
96 KlassInfoEntry* elt = _list; |
|
97 _list = NULL; |
|
98 while (elt != NULL) { |
|
99 KlassInfoEntry* next = elt->next(); |
|
100 delete elt; |
|
101 elt = next; |
|
102 } |
|
103 } |
|
104 |
|
105 KlassInfoTable::KlassInfoTable(int size, HeapWord* ref) { |
|
106 _size = size; |
|
107 _ref = ref; |
|
108 _buckets = NEW_C_HEAP_ARRAY(KlassInfoBucket, _size); |
|
109 |
|
110 for (int index = 0; index < _size; index++) { |
|
111 _buckets[index].initialize(); |
|
112 } |
|
113 } |
|
114 |
|
115 KlassInfoTable::~KlassInfoTable() { |
|
116 for (int index = 0; index < _size; index++) { |
|
117 _buckets[index].empty(); |
|
118 } |
|
119 FREE_C_HEAP_ARRAY(KlassInfoBucket, _buckets); |
|
120 _size = 0; |
|
121 } |
|
122 |
|
123 uint KlassInfoTable::hash(klassOop p) { |
|
124 assert(Universe::heap()->is_in_permanent((HeapWord*)p), "all klasses in permgen"); |
|
125 return (uint)(((uintptr_t)p - (uintptr_t)_ref) >> 2); |
|
126 } |
|
127 |
|
128 KlassInfoEntry* KlassInfoTable::lookup(const klassOop k) { |
|
129 uint idx = hash(k) % _size; |
|
130 KlassInfoEntry* e = _buckets[idx].lookup(k); |
|
131 assert(k == e->klass(), "must be equal"); |
|
132 return e; |
|
133 } |
|
134 |
|
135 void KlassInfoTable::record_instance(const oop obj) { |
|
136 klassOop k = obj->klass(); |
|
137 KlassInfoEntry* elt = lookup(k); |
|
138 elt->set_count(elt->count() + 1); |
|
139 elt->set_words(elt->words() + obj->size()); |
|
140 } |
|
141 |
|
142 void KlassInfoTable::iterate(KlassInfoClosure* cic) { |
|
143 for (int index = 0; index < _size; index++) { |
|
144 _buckets[index].iterate(cic); |
|
145 } |
|
146 } |
|
147 |
|
148 int KlassInfoHisto::sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2) { |
|
149 return (*e1)->compare(*e1,*e2); |
|
150 } |
|
151 |
|
152 KlassInfoHisto::KlassInfoHisto(const char* title, int estimatedCount) : |
|
153 _title(title) { |
|
154 _elements = new (ResourceObj::C_HEAP) GrowableArray<KlassInfoEntry*>(estimatedCount,true); |
|
155 } |
|
156 |
|
157 KlassInfoHisto::~KlassInfoHisto() { |
|
158 delete _elements; |
|
159 } |
|
160 |
|
161 void KlassInfoHisto::add(KlassInfoEntry* cie) { |
|
162 elements()->append(cie); |
|
163 } |
|
164 |
|
165 void KlassInfoHisto::sort() { |
|
166 elements()->sort(KlassInfoHisto::sort_helper); |
|
167 } |
|
168 |
|
169 void KlassInfoHisto::print_elements(outputStream* st) const { |
|
170 // simplify the formatting (ILP32 vs LP64) - store the sum in 64-bit |
|
171 jlong total = 0; |
|
172 julong totalw = 0; |
|
173 for(int i=0; i < elements()->length(); i++) { |
|
174 st->print("%4d: ", i+1); |
|
175 elements()->at(i)->print_on(st); |
|
176 total += elements()->at(i)->count(); |
|
177 totalw += elements()->at(i)->words(); |
|
178 } |
|
179 st->print_cr("Total %13" FORMAT64_MODIFIER "d %13" FORMAT64_MODIFIER "u", |
|
180 total, totalw * HeapWordSize); |
|
181 } |
|
182 |
|
183 void KlassInfoHisto::print_on(outputStream* st) const { |
|
184 st->print_cr("%s",title()); |
|
185 print_elements(st); |
|
186 } |
|
187 |
|
188 class HistoClosure : public KlassInfoClosure { |
|
189 private: |
|
190 KlassInfoHisto* _cih; |
|
191 public: |
|
192 HistoClosure(KlassInfoHisto* cih) : _cih(cih) {} |
|
193 |
|
194 void do_cinfo(KlassInfoEntry* cie) { |
|
195 _cih->add(cie); |
|
196 } |
|
197 }; |
|
198 |
|
199 class RecordInstanceClosure : public ObjectClosure { |
|
200 private: |
|
201 KlassInfoTable* _cit; |
|
202 public: |
|
203 RecordInstanceClosure(KlassInfoTable* cit) : _cit(cit) {} |
|
204 |
|
205 void do_object(oop obj) { |
|
206 _cit->record_instance(obj); |
|
207 } |
|
208 }; |
|
209 |
|
210 void HeapInspection::heap_inspection(outputStream* st) { |
|
211 ResourceMark rm; |
|
212 HeapWord* ref; |
|
213 |
|
214 CollectedHeap* heap = Universe::heap(); |
|
215 switch (heap->kind()) { |
|
216 case CollectedHeap::GenCollectedHeap: { |
|
217 GenCollectedHeap* gch = (GenCollectedHeap*)heap; |
|
218 gch->gc_prologue(false /* !full */); // get any necessary locks |
|
219 ref = gch->perm_gen()->used_region().start(); |
|
220 break; |
|
221 } |
|
222 #ifndef SERIALGC |
|
223 case CollectedHeap::ParallelScavengeHeap: { |
|
224 ParallelScavengeHeap* psh = (ParallelScavengeHeap*)heap; |
|
225 ref = psh->perm_gen()->object_space()->used_region().start(); |
|
226 break; |
|
227 } |
|
228 #endif // SERIALGC |
|
229 default: |
|
230 ShouldNotReachHere(); // Unexpected heap kind for this op |
|
231 } |
|
232 // Collect klass instance info |
|
233 |
|
234 // Iterate over objects in the heap |
|
235 KlassInfoTable cit(KlassInfoTable::cit_size, ref); |
|
236 RecordInstanceClosure ric(&cit); |
|
237 Universe::heap()->object_iterate(&ric); |
|
238 |
|
239 // Sort and print klass instance info |
|
240 KlassInfoHisto histo("\n" |
|
241 " num #instances #bytes class name\n" |
|
242 "----------------------------------------------", |
|
243 KlassInfoHisto::histo_initial_size); |
|
244 HistoClosure hc(&histo); |
|
245 cit.iterate(&hc); |
|
246 histo.sort(); |
|
247 histo.print_on(st); |
|
248 st->flush(); |
|
249 |
|
250 if (Universe::heap()->kind() == CollectedHeap::GenCollectedHeap) { |
|
251 GenCollectedHeap* gch = GenCollectedHeap::heap(); |
|
252 gch->gc_epilogue(false /* !full */); // release all acquired locks |
|
253 } |
|
254 } |
|
255 |
|
256 class FindInstanceClosure : public ObjectClosure { |
|
257 private: |
|
258 klassOop _klass; |
|
259 GrowableArray<oop>* _result; |
|
260 |
|
261 public: |
|
262 FindInstanceClosure(klassOop k, GrowableArray<oop>* result) : _klass(k), _result(result) {}; |
|
263 |
|
264 void do_object(oop obj) { |
|
265 if (obj->is_a(_klass)) { |
|
266 _result->append(obj); |
|
267 } |
|
268 } |
|
269 }; |
|
270 |
|
271 void HeapInspection::find_instances_at_safepoint(klassOop k, GrowableArray<oop>* result) { |
|
272 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped"); |
|
273 assert(Heap_lock->is_locked(), "should have the Heap_lock") |
|
274 |
|
275 // Ensure that the heap is parsable |
|
276 Universe::heap()->ensure_parsability(false); // no need to retire TALBs |
|
277 |
|
278 // Iterate over objects in the heap |
|
279 FindInstanceClosure fic(k, result); |
|
280 Universe::heap()->object_iterate(&fic); |
|
281 } |