aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "interpreter/bytecodeHistogram.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "runtime/os.hpp" aoqi@0: #include "utilities/growableArray.hpp" aoqi@0: aoqi@0: // ------------------------------------------------------------------------------------------------ aoqi@0: // Non-product code aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: // Implementation of BytecodeCounter aoqi@0: aoqi@0: int BytecodeCounter::_counter_value = 0; aoqi@0: jlong BytecodeCounter::_reset_time = 0; aoqi@0: aoqi@0: aoqi@0: void BytecodeCounter::reset() { aoqi@0: _counter_value = 0; aoqi@0: _reset_time = os::elapsed_counter(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: double BytecodeCounter::elapsed_time() { aoqi@0: return (double)(os::elapsed_counter() - _reset_time) / (double)os::elapsed_frequency(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: double BytecodeCounter::frequency() { aoqi@0: return (double)counter_value() / elapsed_time(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BytecodeCounter::print() { aoqi@0: tty->print_cr( aoqi@0: "%d bytecodes executed in %.1fs (%.3fMHz)", aoqi@0: counter_value(), aoqi@0: elapsed_time(), aoqi@0: frequency() / 1000000.0 aoqi@0: ); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Helper class for sorting aoqi@0: aoqi@0: class HistoEntry: public ResourceObj { aoqi@0: private: aoqi@0: int _index; aoqi@0: int _count; aoqi@0: aoqi@0: public: aoqi@0: HistoEntry(int index, int count) { _index = index; _count = count; } aoqi@0: int index() const { return _index; } aoqi@0: int count() const { return _count; } aoqi@0: aoqi@0: static int compare(HistoEntry** x, HistoEntry** y) { return (*x)->count() - (*y)->count(); } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // Helper functions aoqi@0: aoqi@0: static GrowableArray* sorted_array(int* array, int length) { aoqi@0: GrowableArray* a = new GrowableArray(length); aoqi@0: int i = length; aoqi@0: while (i-- > 0) a->append(new HistoEntry(i, array[i])); aoqi@0: a->sort(HistoEntry::compare); aoqi@0: return a; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: static int total_count(GrowableArray* profile) { aoqi@0: int sum = 0; aoqi@0: int i = profile->length(); aoqi@0: while (i-- > 0) sum += profile->at(i)->count(); aoqi@0: return sum; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: static const char* name_for(int i) { aoqi@0: return Bytecodes::is_defined(i) ? Bytecodes::name(Bytecodes::cast(i)) : "xxxunusedxxx"; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of BytecodeHistogram aoqi@0: aoqi@0: int BytecodeHistogram::_counters[Bytecodes::number_of_codes]; aoqi@0: aoqi@0: aoqi@0: void BytecodeHistogram::reset() { aoqi@0: int i = Bytecodes::number_of_codes; aoqi@0: while (i-- > 0) _counters[i] = 0; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BytecodeHistogram::print(float cutoff) { aoqi@0: ResourceMark rm; aoqi@0: GrowableArray* profile = sorted_array(_counters, Bytecodes::number_of_codes); aoqi@0: // print profile aoqi@0: int tot = total_count(profile); aoqi@0: int abs_sum = 0; aoqi@0: tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 aoqi@0: tty->print_cr("Histogram of %d executed bytecodes:", tot); aoqi@0: tty->cr(); aoqi@0: tty->print_cr(" absolute relative code name"); aoqi@0: tty->print_cr("----------------------------------------------------------------------"); aoqi@0: int i = profile->length(); aoqi@0: while (i-- > 0) { aoqi@0: HistoEntry* e = profile->at(i); aoqi@0: int abs = e->count(); aoqi@0: float rel = abs * 100.0F / tot; aoqi@0: if (cutoff <= rel) { aoqi@0: tty->print_cr("%10d %7.2f%% %02x %s", abs, rel, e->index(), name_for(e->index())); aoqi@0: abs_sum += abs; aoqi@0: } aoqi@0: } aoqi@0: tty->print_cr("----------------------------------------------------------------------"); aoqi@0: float rel_sum = abs_sum * 100.0F / tot; aoqi@0: tty->print_cr("%10d %7.2f%% (cutoff = %.2f%%)", abs_sum, rel_sum, cutoff); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of BytecodePairHistogram aoqi@0: aoqi@0: int BytecodePairHistogram::_index; aoqi@0: int BytecodePairHistogram::_counters[BytecodePairHistogram::number_of_pairs]; aoqi@0: aoqi@0: aoqi@0: void BytecodePairHistogram::reset() { aoqi@0: _index = Bytecodes::_nop << log2_number_of_codes; aoqi@0: aoqi@0: int i = number_of_pairs; aoqi@0: while (i-- > 0) _counters[i] = 0; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void BytecodePairHistogram::print(float cutoff) { aoqi@0: ResourceMark rm; aoqi@0: GrowableArray* profile = sorted_array(_counters, number_of_pairs); aoqi@0: // print profile aoqi@0: int tot = total_count(profile); aoqi@0: int abs_sum = 0; aoqi@0: tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 aoqi@0: tty->print_cr("Histogram of %d executed bytecode pairs:", tot); aoqi@0: tty->cr(); aoqi@0: tty->print_cr(" absolute relative codes 1st bytecode 2nd bytecode"); aoqi@0: tty->print_cr("----------------------------------------------------------------------"); aoqi@0: int i = profile->length(); aoqi@0: while (i-- > 0) { aoqi@0: HistoEntry* e = profile->at(i); aoqi@0: int abs = e->count(); aoqi@0: float rel = abs * 100.0F / tot; aoqi@0: if (cutoff <= rel) { aoqi@0: int c1 = e->index() % number_of_codes; aoqi@0: int c2 = e->index() / number_of_codes; aoqi@0: tty->print_cr("%10d %6.3f%% %02x %02x %-19s %s", abs, rel, c1, c2, name_for(c1), name_for(c2)); aoqi@0: abs_sum += abs; aoqi@0: } aoqi@0: } aoqi@0: tty->print_cr("----------------------------------------------------------------------"); aoqi@0: float rel_sum = abs_sum * 100.0F / tot; aoqi@0: tty->print_cr("%10d %6.3f%% (cutoff = %.3f%%)", abs_sum, rel_sum, cutoff); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: #endif