Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/interpreter/bytecodeHistogram.cpp
32285 views
/*1* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "interpreter/bytecodeHistogram.hpp"26#include "memory/resourceArea.hpp"27#include "runtime/os.hpp"28#include "utilities/growableArray.hpp"2930// ------------------------------------------------------------------------------------------------31// Non-product code32#ifndef PRODUCT3334// Implementation of BytecodeCounter3536int BytecodeCounter::_counter_value = 0;37jlong BytecodeCounter::_reset_time = 0;383940void BytecodeCounter::reset() {41_counter_value = 0;42_reset_time = os::elapsed_counter();43}444546double BytecodeCounter::elapsed_time() {47return (double)(os::elapsed_counter() - _reset_time) / (double)os::elapsed_frequency();48}495051double BytecodeCounter::frequency() {52return (double)counter_value() / elapsed_time();53}545556void BytecodeCounter::print() {57tty->print_cr(58"%d bytecodes executed in %.1fs (%.3fMHz)",59counter_value(),60elapsed_time(),61frequency() / 1000000.062);63}646566// Helper class for sorting6768class HistoEntry: public ResourceObj {69private:70int _index;71int _count;7273public:74HistoEntry(int index, int count) { _index = index; _count = count; }75int index() const { return _index; }76int count() const { return _count; }7778static int compare(HistoEntry** x, HistoEntry** y) { return (*x)->count() - (*y)->count(); }79};808182// Helper functions8384static GrowableArray<HistoEntry*>* sorted_array(int* array, int length) {85GrowableArray<HistoEntry*>* a = new GrowableArray<HistoEntry*>(length);86int i = length;87while (i-- > 0) a->append(new HistoEntry(i, array[i]));88a->sort(HistoEntry::compare);89return a;90}919293static int total_count(GrowableArray<HistoEntry*>* profile) {94int sum = 0;95int i = profile->length();96while (i-- > 0) sum += profile->at(i)->count();97return sum;98}99100101static const char* name_for(int i) {102return Bytecodes::is_defined(i) ? Bytecodes::name(Bytecodes::cast(i)) : "xxxunusedxxx";103}104105106// Implementation of BytecodeHistogram107108int BytecodeHistogram::_counters[Bytecodes::number_of_codes];109110111void BytecodeHistogram::reset() {112int i = Bytecodes::number_of_codes;113while (i-- > 0) _counters[i] = 0;114}115116117void BytecodeHistogram::print(float cutoff) {118ResourceMark rm;119GrowableArray<HistoEntry*>* profile = sorted_array(_counters, Bytecodes::number_of_codes);120// print profile121int tot = total_count(profile);122int abs_sum = 0;123tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789124tty->print_cr("Histogram of %d executed bytecodes:", tot);125tty->cr();126tty->print_cr(" absolute relative code name");127tty->print_cr("----------------------------------------------------------------------");128int i = profile->length();129while (i-- > 0) {130HistoEntry* e = profile->at(i);131int abs = e->count();132float rel = abs * 100.0F / tot;133if (cutoff <= rel) {134tty->print_cr("%10d %7.2f%% %02x %s", abs, rel, e->index(), name_for(e->index()));135abs_sum += abs;136}137}138tty->print_cr("----------------------------------------------------------------------");139float rel_sum = abs_sum * 100.0F / tot;140tty->print_cr("%10d %7.2f%% (cutoff = %.2f%%)", abs_sum, rel_sum, cutoff);141tty->cr();142}143144145// Implementation of BytecodePairHistogram146147int BytecodePairHistogram::_index;148int BytecodePairHistogram::_counters[BytecodePairHistogram::number_of_pairs];149150151void BytecodePairHistogram::reset() {152_index = Bytecodes::_nop << log2_number_of_codes;153154int i = number_of_pairs;155while (i-- > 0) _counters[i] = 0;156}157158159void BytecodePairHistogram::print(float cutoff) {160ResourceMark rm;161GrowableArray<HistoEntry*>* profile = sorted_array(_counters, number_of_pairs);162// print profile163int tot = total_count(profile);164int abs_sum = 0;165tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789166tty->print_cr("Histogram of %d executed bytecode pairs:", tot);167tty->cr();168tty->print_cr(" absolute relative codes 1st bytecode 2nd bytecode");169tty->print_cr("----------------------------------------------------------------------");170int i = profile->length();171while (i-- > 0) {172HistoEntry* e = profile->at(i);173int abs = e->count();174float rel = abs * 100.0F / tot;175if (cutoff <= rel) {176int c1 = e->index() % number_of_codes;177int c2 = e->index() / number_of_codes;178tty->print_cr("%10d %6.3f%% %02x %02x %-19s %s", abs, rel, c1, c2, name_for(c1), name_for(c2));179abs_sum += abs;180}181}182tty->print_cr("----------------------------------------------------------------------");183float rel_sum = abs_sum * 100.0F / tot;184tty->print_cr("%10d %6.3f%% (cutoff = %.3f%%)", abs_sum, rel_sum, cutoff);185tty->cr();186}187188#endif189190191