0N/A/*
1879N/A * Copyright (c) 1997, 2010, 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 "classfile/systemDictionary.hpp"
1879N/A#include "gc_interface/collectedHeap.inline.hpp"
1879N/A#include "memory/permGen.hpp"
1879N/A#include "memory/resourceArea.hpp"
1879N/A#include "memory/space.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "oops/oop.inline2.hpp"
1879N/A#include "runtime/aprofiler.hpp"
0N/A
0N/A
0N/Abool AllocationProfiler::_active = false;
0N/AGrowableArray<klassOop>* AllocationProfiler::_print_array = NULL;
0N/A
0N/A
0N/Aclass AllocProfClosure : public ObjectClosure {
0N/A public:
0N/A void do_object(oop obj) {
0N/A Klass* k = obj->blueprint();
0N/A k->set_alloc_count(k->alloc_count() + 1);
0N/A k->set_alloc_size(k->alloc_size() + obj->size());
0N/A }
0N/A};
0N/A
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/Aclass AllocProfResetClosure : public ObjectClosure {
0N/A public:
0N/A void do_object(oop obj) {
0N/A if (obj->is_klass()) {
0N/A Klass* k = Klass::cast(klassOop(obj));
0N/A k->set_alloc_count(0);
0N/A k->set_alloc_size(0);
0N/A }
0N/A }
0N/A};
0N/A
0N/A#endif
0N/A
0N/A
0N/Avoid AllocationProfiler::iterate_since_last_gc() {
0N/A if (is_active()) {
0N/A AllocProfClosure blk;
0N/A GenCollectedHeap* heap = GenCollectedHeap::heap();
0N/A heap->object_iterate_since_last_GC(&blk);
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid AllocationProfiler::engage() {
0N/A _active = true;
0N/A}
0N/A
0N/A
0N/Avoid AllocationProfiler::disengage() {
0N/A _active = false;
0N/A}
0N/A
0N/A
0N/Avoid AllocationProfiler::add_class_to_array(klassOop k) {
0N/A _print_array->append(k);
0N/A}
0N/A
0N/A
0N/Avoid AllocationProfiler::add_classes_to_array(klassOop k) {
0N/A // Iterate over klass and all array klasses for klass
0N/A k->klass_part()->with_array_klasses_do(&AllocationProfiler::add_class_to_array);
0N/A}
0N/A
0N/A
0N/Aint AllocationProfiler::compare_classes(klassOop* k1, klassOop* k2) {
0N/A // Sort by total allocation size
0N/A return (*k2)->klass_part()->alloc_size() - (*k1)->klass_part()->alloc_size();
0N/A}
0N/A
0N/A
0N/Aint AllocationProfiler::average(size_t alloc_size, int alloc_count) {
0N/A return (int) ((double) (alloc_size * BytesPerWord) / MAX2(alloc_count, 1) + 0.5);
0N/A}
0N/A
0N/A
0N/Avoid AllocationProfiler::sort_and_print_array(size_t cutoff) {
0N/A _print_array->sort(&AllocationProfiler::compare_classes);
0N/A tty->print_cr("________________Size"
0N/A "__Instances"
0N/A "__Average"
0N/A "__Class________________");
0N/A size_t total_alloc_size = 0;
0N/A int total_alloc_count = 0;
0N/A for (int index = 0; index < _print_array->length(); index++) {
0N/A klassOop k = _print_array->at(index);
0N/A size_t alloc_size = k->klass_part()->alloc_size();
0N/A if (alloc_size > cutoff) {
0N/A int alloc_count = k->klass_part()->alloc_count();
0N/A#ifdef PRODUCT
0N/A const char* name = k->klass_part()->external_name();
0N/A#else
0N/A const char* name = k->klass_part()->internal_name();
0N/A#endif
0N/A tty->print_cr("%20u %10u %8u %s",
0N/A alloc_size * BytesPerWord,
0N/A alloc_count,
0N/A average(alloc_size, alloc_count),
0N/A name);
0N/A total_alloc_size += alloc_size;
0N/A total_alloc_count += alloc_count;
0N/A }
0N/A }
0N/A tty->print_cr("%20u %10u %8u --total--",
0N/A total_alloc_size * BytesPerWord,
0N/A total_alloc_count,
0N/A average(total_alloc_size, total_alloc_count));
0N/A tty->cr();
0N/A}
0N/A
0N/A
0N/Avoid AllocationProfiler::print(size_t cutoff) {
0N/A ResourceMark rm;
0N/A assert(!is_active(), "AllocationProfiler cannot be active while printing profile");
0N/A
0N/A tty->cr();
0N/A tty->print_cr("Allocation profile (sizes in bytes, cutoff = %ld bytes):", cutoff * BytesPerWord);
0N/A tty->cr();
0N/A
0N/A // Print regular instance klasses and basic type array klasses
0N/A _print_array = new GrowableArray<klassOop>(SystemDictionary::number_of_classes()*2);
0N/A SystemDictionary::classes_do(&add_classes_to_array);
0N/A Universe::basic_type_classes_do(&add_classes_to_array);
0N/A sort_and_print_array(cutoff);
0N/A
0N/A #ifndef PRODUCT
0N/A tty->print_cr("Allocation profile for system classes (sizes in bytes, cutoff = %d bytes):", cutoff * BytesPerWord);
0N/A tty->cr();
0N/A
0N/A // Print system klasses (methods, symbols, constant pools, etc.)
0N/A _print_array = new GrowableArray<klassOop>(64);
0N/A Universe::system_classes_do(&add_classes_to_array);
0N/A sort_and_print_array(cutoff);
0N/A
0N/A tty->print_cr("Permanent generation dump (sizes in bytes, cutoff = %d bytes):", cutoff * BytesPerWord);
0N/A tty->cr();
0N/A
0N/A AllocProfResetClosure resetblk;
0N/A Universe::heap()->permanent_object_iterate(&resetblk);
0N/A AllocProfClosure blk;
0N/A Universe::heap()->permanent_object_iterate(&blk);
0N/A
0N/A _print_array = new GrowableArray<klassOop>(SystemDictionary::number_of_classes()*2);
0N/A SystemDictionary::classes_do(&add_classes_to_array);
0N/A Universe::basic_type_classes_do(&add_classes_to_array);
0N/A Universe::system_classes_do(&add_classes_to_array);
0N/A sort_and_print_array(cutoff);
0N/A #endif
0N/A}