duke@435: /* hseigel@4465: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "gc_interface/collectedHeap.inline.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "memory/space.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "oops/oop.inline2.hpp" stefank@2314: #include "runtime/aprofiler.hpp" duke@435: duke@435: duke@435: bool AllocationProfiler::_active = false; coleenp@4037: GrowableArray* AllocationProfiler::_print_array = NULL; duke@435: duke@435: duke@435: class AllocProfClosure : public ObjectClosure { duke@435: public: duke@435: void do_object(oop obj) { coleenp@4037: Klass* k = obj->klass(); duke@435: k->set_alloc_count(k->alloc_count() + 1); duke@435: k->set_alloc_size(k->alloc_size() + obj->size()); duke@435: } duke@435: }; duke@435: duke@435: duke@435: void AllocationProfiler::iterate_since_last_gc() { duke@435: if (is_active()) { duke@435: AllocProfClosure blk; duke@435: GenCollectedHeap* heap = GenCollectedHeap::heap(); duke@435: heap->object_iterate_since_last_GC(&blk); duke@435: } duke@435: } duke@435: duke@435: duke@435: void AllocationProfiler::engage() { duke@435: _active = true; duke@435: } duke@435: duke@435: duke@435: void AllocationProfiler::disengage() { duke@435: _active = false; duke@435: } duke@435: duke@435: coleenp@4037: void AllocationProfiler::add_class_to_array(Klass* k) { duke@435: _print_array->append(k); duke@435: } duke@435: duke@435: coleenp@4037: void AllocationProfiler::add_classes_to_array(Klass* k) { duke@435: // Iterate over klass and all array klasses for klass coleenp@4037: k->with_array_klasses_do(&AllocationProfiler::add_class_to_array); duke@435: } duke@435: duke@435: coleenp@4037: int AllocationProfiler::compare_classes(Klass** k1, Klass** k2) { duke@435: // Sort by total allocation size coleenp@4037: return (*k2)->alloc_size() - (*k1)->alloc_size(); duke@435: } duke@435: duke@435: duke@435: int AllocationProfiler::average(size_t alloc_size, int alloc_count) { duke@435: return (int) ((double) (alloc_size * BytesPerWord) / MAX2(alloc_count, 1) + 0.5); duke@435: } duke@435: duke@435: duke@435: void AllocationProfiler::sort_and_print_array(size_t cutoff) { duke@435: _print_array->sort(&AllocationProfiler::compare_classes); duke@435: tty->print_cr("________________Size" duke@435: "__Instances" duke@435: "__Average" duke@435: "__Class________________"); duke@435: size_t total_alloc_size = 0; duke@435: int total_alloc_count = 0; duke@435: for (int index = 0; index < _print_array->length(); index++) { coleenp@4037: Klass* k = _print_array->at(index); coleenp@4037: size_t alloc_size = k->alloc_size(); duke@435: if (alloc_size > cutoff) { coleenp@4037: int alloc_count = k->alloc_count(); duke@435: #ifdef PRODUCT coleenp@4037: const char* name = k->external_name(); duke@435: #else coleenp@4037: const char* name = k->internal_name(); duke@435: #endif duke@435: tty->print_cr("%20u %10u %8u %s", duke@435: alloc_size * BytesPerWord, duke@435: alloc_count, duke@435: average(alloc_size, alloc_count), duke@435: name); duke@435: total_alloc_size += alloc_size; duke@435: total_alloc_count += alloc_count; duke@435: } coleenp@4037: k->set_alloc_count(0); coleenp@4037: k->set_alloc_size(0); duke@435: } duke@435: tty->print_cr("%20u %10u %8u --total--", duke@435: total_alloc_size * BytesPerWord, duke@435: total_alloc_count, duke@435: average(total_alloc_size, total_alloc_count)); duke@435: tty->cr(); duke@435: } duke@435: duke@435: duke@435: void AllocationProfiler::print(size_t cutoff) { duke@435: ResourceMark rm; duke@435: assert(!is_active(), "AllocationProfiler cannot be active while printing profile"); duke@435: duke@435: tty->cr(); hseigel@4465: tty->print_cr("Allocation profile (sizes in bytes, cutoff = " SIZE_FORMAT " bytes):", cutoff * BytesPerWord); duke@435: tty->cr(); duke@435: duke@435: // Print regular instance klasses and basic type array klasses coleenp@4037: _print_array = new GrowableArray(SystemDictionary::number_of_classes()*2); duke@435: SystemDictionary::classes_do(&add_classes_to_array); duke@435: Universe::basic_type_classes_do(&add_classes_to_array); duke@435: sort_and_print_array(cutoff); duke@435: coleenp@4037: // This used to print metadata in the permgen but since there isn't a permgen coleenp@4037: // anymore, it is not yet implemented. duke@435: }