Tue, 11 Sep 2012 14:59:23 +0200
7197350: NPG: jvmtiHeapReferenceCallback receives incorrect reference_kind for system class roots
Summary: Fix the iteration over the system classes and report the correct reference kind.
Reviewed-by: coleenp, rbackman
duke@435 | 1 | /* |
coleenp@4037 | 2 | * Copyright (c) 2002, 2012, 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 | #include "precompiled.hpp" |
stefank@2314 | 26 | #include "gc_interface/collectedHeap.hpp" |
stefank@2314 | 27 | #include "memory/genCollectedHeap.hpp" |
stefank@2314 | 28 | #include "memory/heapInspection.hpp" |
stefank@2314 | 29 | #include "memory/resourceArea.hpp" |
stefank@2314 | 30 | #include "runtime/os.hpp" |
stefank@2314 | 31 | #include "utilities/globalDefinitions.hpp" |
stefank@2314 | 32 | #ifndef SERIALGC |
stefank@2314 | 33 | #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp" |
stefank@2314 | 34 | #endif |
duke@435 | 35 | |
duke@435 | 36 | // HeapInspection |
duke@435 | 37 | |
duke@435 | 38 | int KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) { |
duke@435 | 39 | if(e1->_instance_words > e2->_instance_words) { |
duke@435 | 40 | return -1; |
duke@435 | 41 | } else if(e1->_instance_words < e2->_instance_words) { |
duke@435 | 42 | return 1; |
duke@435 | 43 | } |
duke@435 | 44 | return 0; |
duke@435 | 45 | } |
duke@435 | 46 | |
duke@435 | 47 | void KlassInfoEntry::print_on(outputStream* st) const { |
duke@435 | 48 | ResourceMark rm; |
duke@435 | 49 | const char* name;; |
coleenp@4037 | 50 | if (_klass->name() != NULL) { |
coleenp@4037 | 51 | name = _klass->external_name(); |
duke@435 | 52 | } else { |
duke@435 | 53 | if (_klass == Universe::boolArrayKlassObj()) name = "<boolArrayKlass>"; else |
duke@435 | 54 | if (_klass == Universe::charArrayKlassObj()) name = "<charArrayKlass>"; else |
duke@435 | 55 | if (_klass == Universe::singleArrayKlassObj()) name = "<singleArrayKlass>"; else |
duke@435 | 56 | if (_klass == Universe::doubleArrayKlassObj()) name = "<doubleArrayKlass>"; else |
duke@435 | 57 | if (_klass == Universe::byteArrayKlassObj()) name = "<byteArrayKlass>"; else |
duke@435 | 58 | if (_klass == Universe::shortArrayKlassObj()) name = "<shortArrayKlass>"; else |
duke@435 | 59 | if (_klass == Universe::intArrayKlassObj()) name = "<intArrayKlass>"; else |
duke@435 | 60 | if (_klass == Universe::longArrayKlassObj()) name = "<longArrayKlass>"; else |
duke@435 | 61 | name = "<no name>"; |
duke@435 | 62 | } |
duke@435 | 63 | // simplify the formatting (ILP32 vs LP64) - always cast the numbers to 64-bit |
ysr@446 | 64 | st->print_cr(INT64_FORMAT_W(13) " " UINT64_FORMAT_W(13) " %s", |
duke@435 | 65 | (jlong) _instance_count, |
duke@435 | 66 | (julong) _instance_words * HeapWordSize, |
duke@435 | 67 | name); |
duke@435 | 68 | } |
duke@435 | 69 | |
coleenp@4037 | 70 | KlassInfoEntry* KlassInfoBucket::lookup(Klass* const k) { |
duke@435 | 71 | KlassInfoEntry* elt = _list; |
duke@435 | 72 | while (elt != NULL) { |
duke@435 | 73 | if (elt->is_equal(k)) { |
duke@435 | 74 | return elt; |
duke@435 | 75 | } |
duke@435 | 76 | elt = elt->next(); |
duke@435 | 77 | } |
duke@435 | 78 | elt = new KlassInfoEntry(k, list()); |
ysr@446 | 79 | // We may be out of space to allocate the new entry. |
ysr@446 | 80 | if (elt != NULL) { |
ysr@446 | 81 | set_list(elt); |
ysr@446 | 82 | } |
duke@435 | 83 | return elt; |
duke@435 | 84 | } |
duke@435 | 85 | |
duke@435 | 86 | void KlassInfoBucket::iterate(KlassInfoClosure* cic) { |
duke@435 | 87 | KlassInfoEntry* elt = _list; |
duke@435 | 88 | while (elt != NULL) { |
duke@435 | 89 | cic->do_cinfo(elt); |
duke@435 | 90 | elt = elt->next(); |
duke@435 | 91 | } |
duke@435 | 92 | } |
duke@435 | 93 | |
duke@435 | 94 | void KlassInfoBucket::empty() { |
duke@435 | 95 | KlassInfoEntry* elt = _list; |
duke@435 | 96 | _list = NULL; |
duke@435 | 97 | while (elt != NULL) { |
duke@435 | 98 | KlassInfoEntry* next = elt->next(); |
duke@435 | 99 | delete elt; |
duke@435 | 100 | elt = next; |
duke@435 | 101 | } |
duke@435 | 102 | } |
duke@435 | 103 | |
duke@435 | 104 | KlassInfoTable::KlassInfoTable(int size, HeapWord* ref) { |
ysr@446 | 105 | _size = 0; |
duke@435 | 106 | _ref = ref; |
zgu@3900 | 107 | _buckets = NEW_C_HEAP_ARRAY(KlassInfoBucket, size, mtInternal); |
ysr@446 | 108 | if (_buckets != NULL) { |
ysr@446 | 109 | _size = size; |
ysr@446 | 110 | for (int index = 0; index < _size; index++) { |
ysr@446 | 111 | _buckets[index].initialize(); |
ysr@446 | 112 | } |
duke@435 | 113 | } |
duke@435 | 114 | } |
duke@435 | 115 | |
duke@435 | 116 | KlassInfoTable::~KlassInfoTable() { |
ysr@446 | 117 | if (_buckets != NULL) { |
ysr@446 | 118 | for (int index = 0; index < _size; index++) { |
ysr@446 | 119 | _buckets[index].empty(); |
ysr@446 | 120 | } |
zgu@3900 | 121 | FREE_C_HEAP_ARRAY(KlassInfoBucket, _buckets, mtInternal); |
ysr@446 | 122 | _size = 0; |
duke@435 | 123 | } |
duke@435 | 124 | } |
duke@435 | 125 | |
coleenp@4037 | 126 | uint KlassInfoTable::hash(Klass* p) { |
coleenp@4037 | 127 | assert(p->is_metadata(), "all klasses are metadata"); |
duke@435 | 128 | return (uint)(((uintptr_t)p - (uintptr_t)_ref) >> 2); |
duke@435 | 129 | } |
duke@435 | 130 | |
coleenp@4037 | 131 | KlassInfoEntry* KlassInfoTable::lookup(Klass* const k) { |
duke@435 | 132 | uint idx = hash(k) % _size; |
ysr@446 | 133 | assert(_buckets != NULL, "Allocation failure should have been caught"); |
duke@435 | 134 | KlassInfoEntry* e = _buckets[idx].lookup(k); |
ysr@446 | 135 | // Lookup may fail if this is a new klass for which we |
ysr@446 | 136 | // could not allocate space for an new entry. |
ysr@446 | 137 | assert(e == NULL || k == e->klass(), "must be equal"); |
duke@435 | 138 | return e; |
duke@435 | 139 | } |
duke@435 | 140 | |
ysr@446 | 141 | // Return false if the entry could not be recorded on account |
ysr@446 | 142 | // of running out of space required to create a new entry. |
ysr@446 | 143 | bool KlassInfoTable::record_instance(const oop obj) { |
coleenp@4037 | 144 | Klass* k = obj->klass(); |
duke@435 | 145 | KlassInfoEntry* elt = lookup(k); |
ysr@446 | 146 | // elt may be NULL if it's a new klass for which we |
ysr@446 | 147 | // could not allocate space for a new entry in the hashtable. |
ysr@446 | 148 | if (elt != NULL) { |
ysr@446 | 149 | elt->set_count(elt->count() + 1); |
ysr@446 | 150 | elt->set_words(elt->words() + obj->size()); |
ysr@446 | 151 | return true; |
ysr@446 | 152 | } else { |
ysr@446 | 153 | return false; |
ysr@446 | 154 | } |
duke@435 | 155 | } |
duke@435 | 156 | |
duke@435 | 157 | void KlassInfoTable::iterate(KlassInfoClosure* cic) { |
ysr@446 | 158 | assert(_size == 0 || _buckets != NULL, "Allocation failure should have been caught"); |
duke@435 | 159 | for (int index = 0; index < _size; index++) { |
duke@435 | 160 | _buckets[index].iterate(cic); |
duke@435 | 161 | } |
duke@435 | 162 | } |
duke@435 | 163 | |
duke@435 | 164 | int KlassInfoHisto::sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2) { |
duke@435 | 165 | return (*e1)->compare(*e1,*e2); |
duke@435 | 166 | } |
duke@435 | 167 | |
duke@435 | 168 | KlassInfoHisto::KlassInfoHisto(const char* title, int estimatedCount) : |
duke@435 | 169 | _title(title) { |
zgu@3900 | 170 | _elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<KlassInfoEntry*>(estimatedCount,true); |
duke@435 | 171 | } |
duke@435 | 172 | |
duke@435 | 173 | KlassInfoHisto::~KlassInfoHisto() { |
duke@435 | 174 | delete _elements; |
duke@435 | 175 | } |
duke@435 | 176 | |
duke@435 | 177 | void KlassInfoHisto::add(KlassInfoEntry* cie) { |
duke@435 | 178 | elements()->append(cie); |
duke@435 | 179 | } |
duke@435 | 180 | |
duke@435 | 181 | void KlassInfoHisto::sort() { |
duke@435 | 182 | elements()->sort(KlassInfoHisto::sort_helper); |
duke@435 | 183 | } |
duke@435 | 184 | |
duke@435 | 185 | void KlassInfoHisto::print_elements(outputStream* st) const { |
duke@435 | 186 | // simplify the formatting (ILP32 vs LP64) - store the sum in 64-bit |
duke@435 | 187 | jlong total = 0; |
duke@435 | 188 | julong totalw = 0; |
duke@435 | 189 | for(int i=0; i < elements()->length(); i++) { |
duke@435 | 190 | st->print("%4d: ", i+1); |
duke@435 | 191 | elements()->at(i)->print_on(st); |
duke@435 | 192 | total += elements()->at(i)->count(); |
duke@435 | 193 | totalw += elements()->at(i)->words(); |
duke@435 | 194 | } |
ysr@446 | 195 | st->print_cr("Total " INT64_FORMAT_W(13) " " UINT64_FORMAT_W(13), |
duke@435 | 196 | total, totalw * HeapWordSize); |
duke@435 | 197 | } |
duke@435 | 198 | |
duke@435 | 199 | void KlassInfoHisto::print_on(outputStream* st) const { |
duke@435 | 200 | st->print_cr("%s",title()); |
duke@435 | 201 | print_elements(st); |
duke@435 | 202 | } |
duke@435 | 203 | |
duke@435 | 204 | class HistoClosure : public KlassInfoClosure { |
duke@435 | 205 | private: |
duke@435 | 206 | KlassInfoHisto* _cih; |
duke@435 | 207 | public: |
duke@435 | 208 | HistoClosure(KlassInfoHisto* cih) : _cih(cih) {} |
duke@435 | 209 | |
duke@435 | 210 | void do_cinfo(KlassInfoEntry* cie) { |
duke@435 | 211 | _cih->add(cie); |
duke@435 | 212 | } |
duke@435 | 213 | }; |
duke@435 | 214 | |
duke@435 | 215 | class RecordInstanceClosure : public ObjectClosure { |
duke@435 | 216 | private: |
duke@435 | 217 | KlassInfoTable* _cit; |
ysr@446 | 218 | size_t _missed_count; |
duke@435 | 219 | public: |
ysr@446 | 220 | RecordInstanceClosure(KlassInfoTable* cit) : |
ysr@446 | 221 | _cit(cit), _missed_count(0) {} |
duke@435 | 222 | |
duke@435 | 223 | void do_object(oop obj) { |
ysr@446 | 224 | if (!_cit->record_instance(obj)) { |
ysr@446 | 225 | _missed_count++; |
ysr@446 | 226 | } |
duke@435 | 227 | } |
ysr@446 | 228 | |
ysr@446 | 229 | size_t missed_count() { return _missed_count; } |
duke@435 | 230 | }; |
duke@435 | 231 | |
ysr@1050 | 232 | void HeapInspection::heap_inspection(outputStream* st, bool need_prologue) { |
duke@435 | 233 | ResourceMark rm; |
coleenp@4037 | 234 | // Get some random number for ref (the hash key) |
coleenp@4037 | 235 | HeapWord* ref = (HeapWord*) Universe::boolArrayKlassObj(); |
duke@435 | 236 | CollectedHeap* heap = Universe::heap(); |
ysr@777 | 237 | bool is_shared_heap = false; |
coleenp@4037 | 238 | |
duke@435 | 239 | // Collect klass instance info |
ysr@446 | 240 | KlassInfoTable cit(KlassInfoTable::cit_size, ref); |
ysr@446 | 241 | if (!cit.allocation_failed()) { |
ysr@446 | 242 | // Iterate over objects in the heap |
ysr@446 | 243 | RecordInstanceClosure ric(&cit); |
ysr@446 | 244 | Universe::heap()->object_iterate(&ric); |
duke@435 | 245 | |
ysr@446 | 246 | // Report if certain classes are not counted because of |
ysr@446 | 247 | // running out of C-heap for the histogram. |
ysr@446 | 248 | size_t missed_count = ric.missed_count(); |
ysr@446 | 249 | if (missed_count != 0) { |
ysr@446 | 250 | st->print_cr("WARNING: Ran out of C-heap; undercounted " SIZE_FORMAT |
ysr@446 | 251 | " total instances in data below", |
ysr@446 | 252 | missed_count); |
ysr@446 | 253 | } |
ysr@446 | 254 | // Sort and print klass instance info |
ysr@446 | 255 | KlassInfoHisto histo("\n" |
ysr@446 | 256 | " num #instances #bytes class name\n" |
ysr@446 | 257 | "----------------------------------------------", |
ysr@446 | 258 | KlassInfoHisto::histo_initial_size); |
ysr@446 | 259 | HistoClosure hc(&histo); |
ysr@446 | 260 | cit.iterate(&hc); |
ysr@446 | 261 | histo.sort(); |
ysr@446 | 262 | histo.print_on(st); |
ysr@446 | 263 | } else { |
ysr@446 | 264 | st->print_cr("WARNING: Ran out of C-heap; histogram not generated"); |
ysr@446 | 265 | } |
duke@435 | 266 | st->flush(); |
duke@435 | 267 | |
ysr@1050 | 268 | if (need_prologue && is_shared_heap) { |
ysr@777 | 269 | SharedHeap* sh = (SharedHeap*)heap; |
ysr@777 | 270 | sh->gc_epilogue(false /* !full */); // release all acquired locks, etc. |
duke@435 | 271 | } |
duke@435 | 272 | } |
duke@435 | 273 | |
duke@435 | 274 | class FindInstanceClosure : public ObjectClosure { |
duke@435 | 275 | private: |
coleenp@4037 | 276 | Klass* _klass; |
duke@435 | 277 | GrowableArray<oop>* _result; |
duke@435 | 278 | |
duke@435 | 279 | public: |
coleenp@4037 | 280 | FindInstanceClosure(Klass* k, GrowableArray<oop>* result) : _klass(k), _result(result) {}; |
duke@435 | 281 | |
duke@435 | 282 | void do_object(oop obj) { |
duke@435 | 283 | if (obj->is_a(_klass)) { |
duke@435 | 284 | _result->append(obj); |
duke@435 | 285 | } |
duke@435 | 286 | } |
duke@435 | 287 | }; |
duke@435 | 288 | |
coleenp@4037 | 289 | void HeapInspection::find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) { |
duke@435 | 290 | assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped"); |
jcoomes@1844 | 291 | assert(Heap_lock->is_locked(), "should have the Heap_lock"); |
duke@435 | 292 | |
duke@435 | 293 | // Ensure that the heap is parsable |
duke@435 | 294 | Universe::heap()->ensure_parsability(false); // no need to retire TALBs |
duke@435 | 295 | |
duke@435 | 296 | // Iterate over objects in the heap |
duke@435 | 297 | FindInstanceClosure fic(k, result); |
jmasa@952 | 298 | // If this operation encounters a bad object when using CMS, |
coleenp@4037 | 299 | // consider using safe_object_iterate() which avoids metadata |
jmasa@952 | 300 | // objects that may contain bad references. |
duke@435 | 301 | Universe::heap()->object_iterate(&fic); |
duke@435 | 302 | } |