duke@435: /* stefank@2314: * Copyright (c) 1997, 2010, 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 "interpreter/bytecodeHistogram.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "runtime/os.hpp" stefank@2314: #include "utilities/growableArray.hpp" duke@435: duke@435: // ------------------------------------------------------------------------------------------------ duke@435: // Non-product code duke@435: #ifndef PRODUCT duke@435: duke@435: // Implementation of BytecodeCounter duke@435: duke@435: int BytecodeCounter::_counter_value = 0; duke@435: jlong BytecodeCounter::_reset_time = 0; duke@435: duke@435: duke@435: void BytecodeCounter::reset() { duke@435: _counter_value = 0; duke@435: _reset_time = os::elapsed_counter(); duke@435: } duke@435: duke@435: duke@435: double BytecodeCounter::elapsed_time() { duke@435: return (double)(os::elapsed_counter() - _reset_time) / (double)os::elapsed_frequency(); duke@435: } duke@435: duke@435: duke@435: double BytecodeCounter::frequency() { duke@435: return (double)counter_value() / elapsed_time(); duke@435: } duke@435: duke@435: duke@435: void BytecodeCounter::print() { duke@435: tty->print_cr( duke@435: "%d bytecodes executed in %.1fs (%.3fMHz)", duke@435: counter_value(), duke@435: elapsed_time(), duke@435: frequency() / 1000000.0 duke@435: ); duke@435: } duke@435: duke@435: duke@435: // Helper class for sorting duke@435: duke@435: class HistoEntry: public ResourceObj { duke@435: private: duke@435: int _index; duke@435: int _count; duke@435: duke@435: public: duke@435: HistoEntry(int index, int count) { _index = index; _count = count; } duke@435: int index() const { return _index; } duke@435: int count() const { return _count; } duke@435: duke@435: static int compare(HistoEntry** x, HistoEntry** y) { return (*x)->count() - (*y)->count(); } duke@435: }; duke@435: duke@435: duke@435: // Helper functions duke@435: duke@435: static GrowableArray* sorted_array(int* array, int length) { duke@435: GrowableArray* a = new GrowableArray(length); duke@435: int i = length; duke@435: while (i-- > 0) a->append(new HistoEntry(i, array[i])); duke@435: a->sort(HistoEntry::compare); duke@435: return a; duke@435: } duke@435: duke@435: duke@435: static int total_count(GrowableArray* profile) { duke@435: int sum = 0; duke@435: int i = profile->length(); duke@435: while (i-- > 0) sum += profile->at(i)->count(); duke@435: return sum; duke@435: } duke@435: duke@435: duke@435: static const char* name_for(int i) { duke@435: return Bytecodes::is_defined(i) ? Bytecodes::name(Bytecodes::cast(i)) : "xxxunusedxxx"; duke@435: } duke@435: duke@435: duke@435: // Implementation of BytecodeHistogram duke@435: duke@435: int BytecodeHistogram::_counters[Bytecodes::number_of_codes]; duke@435: duke@435: duke@435: void BytecodeHistogram::reset() { duke@435: int i = Bytecodes::number_of_codes; duke@435: while (i-- > 0) _counters[i] = 0; duke@435: } duke@435: duke@435: duke@435: void BytecodeHistogram::print(float cutoff) { duke@435: ResourceMark rm; duke@435: GrowableArray* profile = sorted_array(_counters, Bytecodes::number_of_codes); duke@435: // print profile duke@435: int tot = total_count(profile); duke@435: int abs_sum = 0; duke@435: tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 duke@435: tty->print_cr("Histogram of %d executed bytecodes:", tot); duke@435: tty->cr(); duke@435: tty->print_cr(" absolute relative code name"); duke@435: tty->print_cr("----------------------------------------------------------------------"); duke@435: int i = profile->length(); duke@435: while (i-- > 0) { duke@435: HistoEntry* e = profile->at(i); duke@435: int abs = e->count(); duke@435: float rel = abs * 100.0F / tot; duke@435: if (cutoff <= rel) { duke@435: tty->print_cr("%10d %7.2f%% %02x %s", abs, rel, e->index(), name_for(e->index())); duke@435: abs_sum += abs; duke@435: } duke@435: } duke@435: tty->print_cr("----------------------------------------------------------------------"); duke@435: float rel_sum = abs_sum * 100.0F / tot; duke@435: tty->print_cr("%10d %7.2f%% (cutoff = %.2f%%)", abs_sum, rel_sum, cutoff); duke@435: tty->cr(); duke@435: } duke@435: duke@435: duke@435: // Implementation of BytecodePairHistogram duke@435: duke@435: int BytecodePairHistogram::_index; duke@435: int BytecodePairHistogram::_counters[BytecodePairHistogram::number_of_pairs]; duke@435: duke@435: duke@435: void BytecodePairHistogram::reset() { duke@435: _index = Bytecodes::_nop << log2_number_of_codes; duke@435: duke@435: int i = number_of_pairs; duke@435: while (i-- > 0) _counters[i] = 0; duke@435: } duke@435: duke@435: duke@435: void BytecodePairHistogram::print(float cutoff) { duke@435: ResourceMark rm; duke@435: GrowableArray* profile = sorted_array(_counters, number_of_pairs); duke@435: // print profile duke@435: int tot = total_count(profile); duke@435: int abs_sum = 0; duke@435: tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 duke@435: tty->print_cr("Histogram of %d executed bytecode pairs:", tot); duke@435: tty->cr(); duke@435: tty->print_cr(" absolute relative codes 1st bytecode 2nd bytecode"); duke@435: tty->print_cr("----------------------------------------------------------------------"); duke@435: int i = profile->length(); duke@435: while (i-- > 0) { duke@435: HistoEntry* e = profile->at(i); duke@435: int abs = e->count(); duke@435: float rel = abs * 100.0F / tot; duke@435: if (cutoff <= rel) { duke@435: int c1 = e->index() % number_of_codes; duke@435: int c2 = e->index() / number_of_codes; duke@435: tty->print_cr("%10d %6.3f%% %02x %02x %-19s %s", abs, rel, c1, c2, name_for(c1), name_for(c2)); duke@435: abs_sum += abs; duke@435: } duke@435: } duke@435: tty->print_cr("----------------------------------------------------------------------"); duke@435: float rel_sum = abs_sum * 100.0F / tot; duke@435: tty->print_cr("%10d %6.3f%% (cutoff = %.3f%%)", abs_sum, rel_sum, cutoff); duke@435: tty->cr(); duke@435: } duke@435: duke@435: #endif