1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/interpreter/bytecodeHistogram.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,189 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "interpreter/bytecodeHistogram.hpp" 1.30 +#include "memory/resourceArea.hpp" 1.31 +#include "runtime/os.hpp" 1.32 +#include "utilities/growableArray.hpp" 1.33 + 1.34 +// ------------------------------------------------------------------------------------------------ 1.35 +// Non-product code 1.36 +#ifndef PRODUCT 1.37 + 1.38 +// Implementation of BytecodeCounter 1.39 + 1.40 +int BytecodeCounter::_counter_value = 0; 1.41 +jlong BytecodeCounter::_reset_time = 0; 1.42 + 1.43 + 1.44 +void BytecodeCounter::reset() { 1.45 + _counter_value = 0; 1.46 + _reset_time = os::elapsed_counter(); 1.47 +} 1.48 + 1.49 + 1.50 +double BytecodeCounter::elapsed_time() { 1.51 + return (double)(os::elapsed_counter() - _reset_time) / (double)os::elapsed_frequency(); 1.52 +} 1.53 + 1.54 + 1.55 +double BytecodeCounter::frequency() { 1.56 + return (double)counter_value() / elapsed_time(); 1.57 +} 1.58 + 1.59 + 1.60 +void BytecodeCounter::print() { 1.61 + tty->print_cr( 1.62 + "%d bytecodes executed in %.1fs (%.3fMHz)", 1.63 + counter_value(), 1.64 + elapsed_time(), 1.65 + frequency() / 1000000.0 1.66 + ); 1.67 +} 1.68 + 1.69 + 1.70 +// Helper class for sorting 1.71 + 1.72 +class HistoEntry: public ResourceObj { 1.73 + private: 1.74 + int _index; 1.75 + int _count; 1.76 + 1.77 + public: 1.78 + HistoEntry(int index, int count) { _index = index; _count = count; } 1.79 + int index() const { return _index; } 1.80 + int count() const { return _count; } 1.81 + 1.82 + static int compare(HistoEntry** x, HistoEntry** y) { return (*x)->count() - (*y)->count(); } 1.83 +}; 1.84 + 1.85 + 1.86 +// Helper functions 1.87 + 1.88 +static GrowableArray<HistoEntry*>* sorted_array(int* array, int length) { 1.89 + GrowableArray<HistoEntry*>* a = new GrowableArray<HistoEntry*>(length); 1.90 + int i = length; 1.91 + while (i-- > 0) a->append(new HistoEntry(i, array[i])); 1.92 + a->sort(HistoEntry::compare); 1.93 + return a; 1.94 +} 1.95 + 1.96 + 1.97 +static int total_count(GrowableArray<HistoEntry*>* profile) { 1.98 + int sum = 0; 1.99 + int i = profile->length(); 1.100 + while (i-- > 0) sum += profile->at(i)->count(); 1.101 + return sum; 1.102 +} 1.103 + 1.104 + 1.105 +static const char* name_for(int i) { 1.106 + return Bytecodes::is_defined(i) ? Bytecodes::name(Bytecodes::cast(i)) : "xxxunusedxxx"; 1.107 +} 1.108 + 1.109 + 1.110 +// Implementation of BytecodeHistogram 1.111 + 1.112 +int BytecodeHistogram::_counters[Bytecodes::number_of_codes]; 1.113 + 1.114 + 1.115 +void BytecodeHistogram::reset() { 1.116 + int i = Bytecodes::number_of_codes; 1.117 + while (i-- > 0) _counters[i] = 0; 1.118 +} 1.119 + 1.120 + 1.121 +void BytecodeHistogram::print(float cutoff) { 1.122 + ResourceMark rm; 1.123 + GrowableArray<HistoEntry*>* profile = sorted_array(_counters, Bytecodes::number_of_codes); 1.124 + // print profile 1.125 + int tot = total_count(profile); 1.126 + int abs_sum = 0; 1.127 + tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 1.128 + tty->print_cr("Histogram of %d executed bytecodes:", tot); 1.129 + tty->cr(); 1.130 + tty->print_cr(" absolute relative code name"); 1.131 + tty->print_cr("----------------------------------------------------------------------"); 1.132 + int i = profile->length(); 1.133 + while (i-- > 0) { 1.134 + HistoEntry* e = profile->at(i); 1.135 + int abs = e->count(); 1.136 + float rel = abs * 100.0F / tot; 1.137 + if (cutoff <= rel) { 1.138 + tty->print_cr("%10d %7.2f%% %02x %s", abs, rel, e->index(), name_for(e->index())); 1.139 + abs_sum += abs; 1.140 + } 1.141 + } 1.142 + tty->print_cr("----------------------------------------------------------------------"); 1.143 + float rel_sum = abs_sum * 100.0F / tot; 1.144 + tty->print_cr("%10d %7.2f%% (cutoff = %.2f%%)", abs_sum, rel_sum, cutoff); 1.145 + tty->cr(); 1.146 +} 1.147 + 1.148 + 1.149 +// Implementation of BytecodePairHistogram 1.150 + 1.151 +int BytecodePairHistogram::_index; 1.152 +int BytecodePairHistogram::_counters[BytecodePairHistogram::number_of_pairs]; 1.153 + 1.154 + 1.155 +void BytecodePairHistogram::reset() { 1.156 + _index = Bytecodes::_nop << log2_number_of_codes; 1.157 + 1.158 + int i = number_of_pairs; 1.159 + while (i-- > 0) _counters[i] = 0; 1.160 +} 1.161 + 1.162 + 1.163 +void BytecodePairHistogram::print(float cutoff) { 1.164 + ResourceMark rm; 1.165 + GrowableArray<HistoEntry*>* profile = sorted_array(_counters, number_of_pairs); 1.166 + // print profile 1.167 + int tot = total_count(profile); 1.168 + int abs_sum = 0; 1.169 + tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 1.170 + tty->print_cr("Histogram of %d executed bytecode pairs:", tot); 1.171 + tty->cr(); 1.172 + tty->print_cr(" absolute relative codes 1st bytecode 2nd bytecode"); 1.173 + tty->print_cr("----------------------------------------------------------------------"); 1.174 + int i = profile->length(); 1.175 + while (i-- > 0) { 1.176 + HistoEntry* e = profile->at(i); 1.177 + int abs = e->count(); 1.178 + float rel = abs * 100.0F / tot; 1.179 + if (cutoff <= rel) { 1.180 + int c1 = e->index() % number_of_codes; 1.181 + int c2 = e->index() / number_of_codes; 1.182 + tty->print_cr("%10d %6.3f%% %02x %02x %-19s %s", abs, rel, c1, c2, name_for(c1), name_for(c2)); 1.183 + abs_sum += abs; 1.184 + } 1.185 + } 1.186 + tty->print_cr("----------------------------------------------------------------------"); 1.187 + float rel_sum = abs_sum * 100.0F / tot; 1.188 + tty->print_cr("%10d %6.3f%% (cutoff = %.3f%%)", abs_sum, rel_sum, cutoff); 1.189 + tty->cr(); 1.190 +} 1.191 + 1.192 +#endif