0N/A/*
2273N/A * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "gc_interface/collectedHeap.hpp"
1879N/A#include "memory/genCollectedHeap.hpp"
1879N/A#include "memory/heapInspection.hpp"
1879N/A#include "memory/resourceArea.hpp"
1879N/A#include "oops/klassOop.hpp"
1879N/A#include "runtime/os.hpp"
1879N/A#include "utilities/globalDefinitions.hpp"
1879N/A#ifndef SERIALGC
1879N/A#include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
1879N/A#endif
0N/A
0N/A// HeapInspection
0N/A
0N/Aint KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) {
0N/A if(e1->_instance_words > e2->_instance_words) {
0N/A return -1;
0N/A } else if(e1->_instance_words < e2->_instance_words) {
0N/A return 1;
0N/A }
0N/A return 0;
0N/A}
0N/A
0N/Avoid KlassInfoEntry::print_on(outputStream* st) const {
0N/A ResourceMark rm;
0N/A const char* name;;
0N/A if (_klass->klass_part()->name() != NULL) {
0N/A name = _klass->klass_part()->external_name();
0N/A } else {
0N/A if (_klass == Universe::klassKlassObj()) name = "<klassKlass>"; else
0N/A if (_klass == Universe::arrayKlassKlassObj()) name = "<arrayKlassKlass>"; else
0N/A if (_klass == Universe::objArrayKlassKlassObj()) name = "<objArrayKlassKlass>"; else
0N/A if (_klass == Universe::instanceKlassKlassObj()) name = "<instanceKlassKlass>"; else
0N/A if (_klass == Universe::typeArrayKlassKlassObj()) name = "<typeArrayKlassKlass>"; else
0N/A if (_klass == Universe::boolArrayKlassObj()) name = "<boolArrayKlass>"; else
0N/A if (_klass == Universe::charArrayKlassObj()) name = "<charArrayKlass>"; else
0N/A if (_klass == Universe::singleArrayKlassObj()) name = "<singleArrayKlass>"; else
0N/A if (_klass == Universe::doubleArrayKlassObj()) name = "<doubleArrayKlass>"; else
0N/A if (_klass == Universe::byteArrayKlassObj()) name = "<byteArrayKlass>"; else
0N/A if (_klass == Universe::shortArrayKlassObj()) name = "<shortArrayKlass>"; else
0N/A if (_klass == Universe::intArrayKlassObj()) name = "<intArrayKlass>"; else
0N/A if (_klass == Universe::longArrayKlassObj()) name = "<longArrayKlass>"; else
0N/A if (_klass == Universe::methodKlassObj()) name = "<methodKlass>"; else
0N/A if (_klass == Universe::constMethodKlassObj()) name = "<constMethodKlass>"; else
0N/A if (_klass == Universe::methodDataKlassObj()) name = "<methodDataKlass>"; else
0N/A if (_klass == Universe::constantPoolKlassObj()) name = "<constantPoolKlass>"; else
0N/A if (_klass == Universe::constantPoolCacheKlassObj()) name = "<constantPoolCacheKlass>"; else
0N/A if (_klass == Universe::compiledICHolderKlassObj()) name = "<compiledICHolderKlass>"; else
0N/A name = "<no name>";
0N/A }
0N/A // simplify the formatting (ILP32 vs LP64) - always cast the numbers to 64-bit
11N/A st->print_cr(INT64_FORMAT_W(13) " " UINT64_FORMAT_W(13) " %s",
0N/A (jlong) _instance_count,
0N/A (julong) _instance_words * HeapWordSize,
0N/A name);
0N/A}
0N/A
0N/AKlassInfoEntry* KlassInfoBucket::lookup(const klassOop k) {
0N/A KlassInfoEntry* elt = _list;
0N/A while (elt != NULL) {
0N/A if (elt->is_equal(k)) {
0N/A return elt;
0N/A }
0N/A elt = elt->next();
0N/A }
4344N/A elt = new (std::nothrow) KlassInfoEntry(k, list());
11N/A // We may be out of space to allocate the new entry.
11N/A if (elt != NULL) {
11N/A set_list(elt);
11N/A }
0N/A return elt;
0N/A}
0N/A
0N/Avoid KlassInfoBucket::iterate(KlassInfoClosure* cic) {
0N/A KlassInfoEntry* elt = _list;
0N/A while (elt != NULL) {
0N/A cic->do_cinfo(elt);
0N/A elt = elt->next();
0N/A }
0N/A}
0N/A
0N/Avoid KlassInfoBucket::empty() {
0N/A KlassInfoEntry* elt = _list;
0N/A _list = NULL;
0N/A while (elt != NULL) {
0N/A KlassInfoEntry* next = elt->next();
0N/A delete elt;
0N/A elt = next;
0N/A }
0N/A}
0N/A
4524N/AKlassInfoTable::KlassInfoTable(HeapWord* ref) :
4524N/A _size(0), _ref(ref), _size_of_instances_in_words(0) {
4344N/A _buckets = (KlassInfoBucket *) os::malloc(sizeof(KlassInfoBucket) * _num_buckets, mtInternal);
11N/A if (_buckets != NULL) {
4344N/A _size = _num_buckets;
11N/A for (int index = 0; index < _size; index++) {
11N/A _buckets[index].initialize();
11N/A }
0N/A }
0N/A}
0N/A
0N/AKlassInfoTable::~KlassInfoTable() {
11N/A if (_buckets != NULL) {
11N/A for (int index = 0; index < _size; index++) {
11N/A _buckets[index].empty();
11N/A }
3863N/A FREE_C_HEAP_ARRAY(KlassInfoBucket, _buckets, mtInternal);
11N/A _size = 0;
0N/A }
0N/A}
0N/A
0N/Auint KlassInfoTable::hash(klassOop p) {
0N/A assert(Universe::heap()->is_in_permanent((HeapWord*)p), "all klasses in permgen");
0N/A return (uint)(((uintptr_t)p - (uintptr_t)_ref) >> 2);
0N/A}
0N/A
0N/AKlassInfoEntry* KlassInfoTable::lookup(const klassOop k) {
0N/A uint idx = hash(k) % _size;
11N/A assert(_buckets != NULL, "Allocation failure should have been caught");
0N/A KlassInfoEntry* e = _buckets[idx].lookup(k);
11N/A // Lookup may fail if this is a new klass for which we
11N/A // could not allocate space for an new entry.
11N/A assert(e == NULL || k == e->klass(), "must be equal");
0N/A return e;
0N/A}
0N/A
11N/A// Return false if the entry could not be recorded on account
11N/A// of running out of space required to create a new entry.
11N/Abool KlassInfoTable::record_instance(const oop obj) {
0N/A klassOop k = obj->klass();
0N/A KlassInfoEntry* elt = lookup(k);
11N/A // elt may be NULL if it's a new klass for which we
11N/A // could not allocate space for a new entry in the hashtable.
11N/A if (elt != NULL) {
11N/A elt->set_count(elt->count() + 1);
11N/A elt->set_words(elt->words() + obj->size());
4524N/A _size_of_instances_in_words += obj->size();
11N/A return true;
11N/A } else {
11N/A return false;
11N/A }
0N/A}
0N/A
0N/Avoid KlassInfoTable::iterate(KlassInfoClosure* cic) {
11N/A assert(_size == 0 || _buckets != NULL, "Allocation failure should have been caught");
0N/A for (int index = 0; index < _size; index++) {
0N/A _buckets[index].iterate(cic);
0N/A }
0N/A}
0N/A
4524N/Asize_t KlassInfoTable::size_of_instances_in_words() const {
4524N/A return _size_of_instances_in_words;
4524N/A}
4524N/A
0N/Aint KlassInfoHisto::sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2) {
0N/A return (*e1)->compare(*e1,*e2);
0N/A}
0N/A
4344N/AKlassInfoHisto::KlassInfoHisto(const char* title) :
0N/A _title(title) {
4344N/A _elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<KlassInfoEntry*>(_histo_initial_size, true);
0N/A}
0N/A
0N/AKlassInfoHisto::~KlassInfoHisto() {
0N/A delete _elements;
0N/A}
0N/A
0N/Avoid KlassInfoHisto::add(KlassInfoEntry* cie) {
0N/A elements()->append(cie);
0N/A}
0N/A
0N/Avoid KlassInfoHisto::sort() {
0N/A elements()->sort(KlassInfoHisto::sort_helper);
0N/A}
0N/A
0N/Avoid KlassInfoHisto::print_elements(outputStream* st) const {
0N/A // simplify the formatting (ILP32 vs LP64) - store the sum in 64-bit
0N/A jlong total = 0;
0N/A julong totalw = 0;
0N/A for(int i=0; i < elements()->length(); i++) {
0N/A st->print("%4d: ", i+1);
0N/A elements()->at(i)->print_on(st);
0N/A total += elements()->at(i)->count();
0N/A totalw += elements()->at(i)->words();
0N/A }
11N/A st->print_cr("Total " INT64_FORMAT_W(13) " " UINT64_FORMAT_W(13),
0N/A total, totalw * HeapWordSize);
0N/A}
0N/A
0N/Avoid KlassInfoHisto::print_on(outputStream* st) const {
0N/A st->print_cr("%s",title());
0N/A print_elements(st);
0N/A}
0N/A
0N/Aclass HistoClosure : public KlassInfoClosure {
0N/A private:
0N/A KlassInfoHisto* _cih;
0N/A public:
0N/A HistoClosure(KlassInfoHisto* cih) : _cih(cih) {}
0N/A
0N/A void do_cinfo(KlassInfoEntry* cie) {
0N/A _cih->add(cie);
0N/A }
0N/A};
0N/A
0N/Aclass RecordInstanceClosure : public ObjectClosure {
0N/A private:
0N/A KlassInfoTable* _cit;
11N/A size_t _missed_count;
4344N/A BoolObjectClosure* _filter;
0N/A public:
4344N/A RecordInstanceClosure(KlassInfoTable* cit, BoolObjectClosure* filter) :
4344N/A _cit(cit), _missed_count(0), _filter(filter) {}
0N/A
0N/A void do_object(oop obj) {
4344N/A if (should_visit(obj)) {
4344N/A if (!_cit->record_instance(obj)) {
4344N/A _missed_count++;
4344N/A }
11N/A }
0N/A }
11N/A
11N/A size_t missed_count() { return _missed_count; }
4344N/A private:
4344N/A bool should_visit(oop obj) {
4344N/A return _filter == NULL || _filter->do_object_b(obj);
4344N/A }
0N/A};
0N/A
4344N/AHeapWord* HeapInspection::start_of_perm_gen() {
4344N/A if (is_shared_heap()) {
4344N/A SharedHeap* sh = SharedHeap::heap();
4344N/A return sh->perm_gen()->used_region().start();
4344N/A }
4344N/A#ifndef SERIALGC
4344N/A ParallelScavengeHeap* psh = (ParallelScavengeHeap*)Universe::heap();
4344N/A return psh->perm_gen()->object_space()->used_region().start();
4344N/A#else
4344N/A ShouldNotReachHere();
4344N/A return NULL;
4344N/A#endif // SERIALGC
4344N/A}
4344N/A
4344N/Abool HeapInspection::is_shared_heap() {
4344N/A CollectedHeap* heap = Universe::heap();
4344N/A return heap->kind() == CollectedHeap::G1CollectedHeap ||
4344N/A heap->kind() == CollectedHeap::GenCollectedHeap;
4344N/A}
4344N/A
4344N/Avoid HeapInspection::prologue() {
4344N/A if (is_shared_heap()) {
4344N/A SharedHeap* sh = SharedHeap::heap();
4344N/A sh->gc_prologue(false /* !full */); // get any necessary locks, etc.
4344N/A }
4344N/A}
4344N/A
4344N/Avoid HeapInspection::epilogue() {
4344N/A if (is_shared_heap()) {
4344N/A SharedHeap* sh = SharedHeap::heap();
4344N/A sh->gc_epilogue(false /* !full */); // release all acquired locks, etc.
4344N/A }
4344N/A}
4344N/A
4524N/Asize_t HeapInspection::populate_table(KlassInfoTable* cit,
4524N/A bool need_prologue,
4524N/A BoolObjectClosure *filter) {
4344N/A ResourceMark rm;
4344N/A
4344N/A if (need_prologue) {
4344N/A prologue();
4344N/A }
4344N/A
4344N/A RecordInstanceClosure ric(cit, filter);
4344N/A Universe::heap()->object_iterate(&ric);
4344N/A
4344N/A // need to run epilogue if we run prologue
4344N/A if (need_prologue) {
4344N/A epilogue();
4344N/A }
4344N/A
4344N/A return ric.missed_count();
4344N/A}
4344N/A
615N/Avoid HeapInspection::heap_inspection(outputStream* st, bool need_prologue) {
0N/A ResourceMark rm;
0N/A
4344N/A KlassInfoTable cit(start_of_perm_gen());
11N/A if (!cit.allocation_failed()) {
4524N/A size_t missed_count = populate_table(&cit, need_prologue);
4524N/A if (missed_count != 0) {
4524N/A st->print_cr("WARNING: Ran out of C-heap; undercounted " SIZE_FORMAT
4524N/A " total instances in data below",
4524N/A missed_count);
4524N/A }
4524N/A
4344N/A KlassInfoHisto histo("\n"
4344N/A " num #instances #bytes class name\n"
4344N/A "----------------------------------------------");
4344N/A HistoClosure hc(&histo);
0N/A
4524N/A cit.iterate(&hc);
4524N/A
11N/A histo.sort();
11N/A histo.print_on(st);
11N/A } else {
11N/A st->print_cr("WARNING: Ran out of C-heap; histogram not generated");
11N/A }
0N/A st->flush();
0N/A}
0N/A
0N/Aclass FindInstanceClosure : public ObjectClosure {
0N/A private:
0N/A klassOop _klass;
0N/A GrowableArray<oop>* _result;
0N/A
0N/A public:
0N/A FindInstanceClosure(klassOop k, GrowableArray<oop>* result) : _klass(k), _result(result) {};
0N/A
0N/A void do_object(oop obj) {
0N/A if (obj->is_a(_klass)) {
0N/A _result->append(obj);
0N/A }
0N/A }
0N/A};
0N/A
0N/Avoid HeapInspection::find_instances_at_safepoint(klassOop k, GrowableArray<oop>* result) {
0N/A assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
1409N/A assert(Heap_lock->is_locked(), "should have the Heap_lock");
0N/A
0N/A // Ensure that the heap is parsable
0N/A Universe::heap()->ensure_parsability(false); // no need to retire TALBs
0N/A
0N/A // Iterate over objects in the heap
0N/A FindInstanceClosure fic(k, result);
517N/A // If this operation encounters a bad object when using CMS,
517N/A // consider using safe_object_iterate() which avoids perm gen
517N/A // objects that may contain bad references.
0N/A Universe::heap()->object_iterate(&fic);
0N/A}