1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/interpreter/bytecodeHistogram.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,186 @@ 1.4 +/* 1.5 + * Copyright 1997-2000 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_bytecodeHistogram.cpp.incl" 1.30 + 1.31 +// ------------------------------------------------------------------------------------------------ 1.32 +// Non-product code 1.33 +#ifndef PRODUCT 1.34 + 1.35 +// Implementation of BytecodeCounter 1.36 + 1.37 +int BytecodeCounter::_counter_value = 0; 1.38 +jlong BytecodeCounter::_reset_time = 0; 1.39 + 1.40 + 1.41 +void BytecodeCounter::reset() { 1.42 + _counter_value = 0; 1.43 + _reset_time = os::elapsed_counter(); 1.44 +} 1.45 + 1.46 + 1.47 +double BytecodeCounter::elapsed_time() { 1.48 + return (double)(os::elapsed_counter() - _reset_time) / (double)os::elapsed_frequency(); 1.49 +} 1.50 + 1.51 + 1.52 +double BytecodeCounter::frequency() { 1.53 + return (double)counter_value() / elapsed_time(); 1.54 +} 1.55 + 1.56 + 1.57 +void BytecodeCounter::print() { 1.58 + tty->print_cr( 1.59 + "%d bytecodes executed in %.1fs (%.3fMHz)", 1.60 + counter_value(), 1.61 + elapsed_time(), 1.62 + frequency() / 1000000.0 1.63 + ); 1.64 +} 1.65 + 1.66 + 1.67 +// Helper class for sorting 1.68 + 1.69 +class HistoEntry: public ResourceObj { 1.70 + private: 1.71 + int _index; 1.72 + int _count; 1.73 + 1.74 + public: 1.75 + HistoEntry(int index, int count) { _index = index; _count = count; } 1.76 + int index() const { return _index; } 1.77 + int count() const { return _count; } 1.78 + 1.79 + static int compare(HistoEntry** x, HistoEntry** y) { return (*x)->count() - (*y)->count(); } 1.80 +}; 1.81 + 1.82 + 1.83 +// Helper functions 1.84 + 1.85 +static GrowableArray<HistoEntry*>* sorted_array(int* array, int length) { 1.86 + GrowableArray<HistoEntry*>* a = new GrowableArray<HistoEntry*>(length); 1.87 + int i = length; 1.88 + while (i-- > 0) a->append(new HistoEntry(i, array[i])); 1.89 + a->sort(HistoEntry::compare); 1.90 + return a; 1.91 +} 1.92 + 1.93 + 1.94 +static int total_count(GrowableArray<HistoEntry*>* profile) { 1.95 + int sum = 0; 1.96 + int i = profile->length(); 1.97 + while (i-- > 0) sum += profile->at(i)->count(); 1.98 + return sum; 1.99 +} 1.100 + 1.101 + 1.102 +static const char* name_for(int i) { 1.103 + return Bytecodes::is_defined(i) ? Bytecodes::name(Bytecodes::cast(i)) : "xxxunusedxxx"; 1.104 +} 1.105 + 1.106 + 1.107 +// Implementation of BytecodeHistogram 1.108 + 1.109 +int BytecodeHistogram::_counters[Bytecodes::number_of_codes]; 1.110 + 1.111 + 1.112 +void BytecodeHistogram::reset() { 1.113 + int i = Bytecodes::number_of_codes; 1.114 + while (i-- > 0) _counters[i] = 0; 1.115 +} 1.116 + 1.117 + 1.118 +void BytecodeHistogram::print(float cutoff) { 1.119 + ResourceMark rm; 1.120 + GrowableArray<HistoEntry*>* profile = sorted_array(_counters, Bytecodes::number_of_codes); 1.121 + // print profile 1.122 + int tot = total_count(profile); 1.123 + int abs_sum = 0; 1.124 + tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 1.125 + tty->print_cr("Histogram of %d executed bytecodes:", tot); 1.126 + tty->cr(); 1.127 + tty->print_cr(" absolute relative code name"); 1.128 + tty->print_cr("----------------------------------------------------------------------"); 1.129 + int i = profile->length(); 1.130 + while (i-- > 0) { 1.131 + HistoEntry* e = profile->at(i); 1.132 + int abs = e->count(); 1.133 + float rel = abs * 100.0F / tot; 1.134 + if (cutoff <= rel) { 1.135 + tty->print_cr("%10d %7.2f%% %02x %s", abs, rel, e->index(), name_for(e->index())); 1.136 + abs_sum += abs; 1.137 + } 1.138 + } 1.139 + tty->print_cr("----------------------------------------------------------------------"); 1.140 + float rel_sum = abs_sum * 100.0F / tot; 1.141 + tty->print_cr("%10d %7.2f%% (cutoff = %.2f%%)", abs_sum, rel_sum, cutoff); 1.142 + tty->cr(); 1.143 +} 1.144 + 1.145 + 1.146 +// Implementation of BytecodePairHistogram 1.147 + 1.148 +int BytecodePairHistogram::_index; 1.149 +int BytecodePairHistogram::_counters[BytecodePairHistogram::number_of_pairs]; 1.150 + 1.151 + 1.152 +void BytecodePairHistogram::reset() { 1.153 + _index = Bytecodes::_nop << log2_number_of_codes; 1.154 + 1.155 + int i = number_of_pairs; 1.156 + while (i-- > 0) _counters[i] = 0; 1.157 +} 1.158 + 1.159 + 1.160 +void BytecodePairHistogram::print(float cutoff) { 1.161 + ResourceMark rm; 1.162 + GrowableArray<HistoEntry*>* profile = sorted_array(_counters, number_of_pairs); 1.163 + // print profile 1.164 + int tot = total_count(profile); 1.165 + int abs_sum = 0; 1.166 + tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 1.167 + tty->print_cr("Histogram of %d executed bytecode pairs:", tot); 1.168 + tty->cr(); 1.169 + tty->print_cr(" absolute relative codes 1st bytecode 2nd bytecode"); 1.170 + tty->print_cr("----------------------------------------------------------------------"); 1.171 + int i = profile->length(); 1.172 + while (i-- > 0) { 1.173 + HistoEntry* e = profile->at(i); 1.174 + int abs = e->count(); 1.175 + float rel = abs * 100.0F / tot; 1.176 + if (cutoff <= rel) { 1.177 + int c1 = e->index() % number_of_codes; 1.178 + int c2 = e->index() / number_of_codes; 1.179 + tty->print_cr("%10d %6.3f%% %02x %02x %-19s %s", abs, rel, c1, c2, name_for(c1), name_for(c2)); 1.180 + abs_sum += abs; 1.181 + } 1.182 + } 1.183 + tty->print_cr("----------------------------------------------------------------------"); 1.184 + float rel_sum = abs_sum * 100.0F / tot; 1.185 + tty->print_cr("%10d %6.3f%% (cutoff = %.3f%%)", abs_sum, rel_sum, cutoff); 1.186 + tty->cr(); 1.187 +} 1.188 + 1.189 +#endif