1 /* 2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "interpreter/bytecodeHistogram.hpp" 26 #include "memory/resourceArea.hpp" 27 #include "runtime/os.hpp" 28 #include "utilities/growableArray.hpp" 29 30 // ------------------------------------------------------------------------------------------------ 31 // Non-product code 32 #ifndef PRODUCT 33 34 // Implementation of BytecodeCounter 35 36 uintx BytecodeCounter::_counter_value = 0; 37 jlong BytecodeCounter::_reset_time = 0; 38 39 40 void BytecodeCounter::reset() { 41 _counter_value = 0; 42 _reset_time = os::elapsed_counter(); 43 } 44 45 46 double BytecodeCounter::elapsed_time() { 47 return (double)(os::elapsed_counter() - _reset_time) / (double)os::elapsed_frequency(); 48 } 49 50 51 double BytecodeCounter::frequency() { 52 return (double)counter_value() / elapsed_time(); 53 } 54 55 56 void BytecodeCounter::print() { 57 tty->print_cr( 58 "%zu bytecodes executed in %.1fs (%.3fMHz)", 59 counter_value(), 60 elapsed_time(), 61 frequency() / 1000000.0 62 ); 63 } 64 65 66 // Helper class for sorting 67 68 class HistoEntry: public ResourceObj { 69 private: 70 int _index; 71 int _count; 72 73 public: 74 HistoEntry(int index, int count) { _index = index; _count = count; } 75 int index() const { return _index; } 76 int count() const { return _count; } 77 78 static int compare(HistoEntry** x, HistoEntry** y) { return (*x)->count() - (*y)->count(); } 79 }; 80 81 82 // Helper functions 83 84 static GrowableArray<HistoEntry*>* sorted_array(int* array, int length) { 85 GrowableArray<HistoEntry*>* a = new GrowableArray<HistoEntry*>(length); 86 int i = length; 87 while (i-- > 0) a->append(new HistoEntry(i, array[i])); 88 a->sort(HistoEntry::compare); 89 return a; 90 } 91 92 93 static int total_count(GrowableArray<HistoEntry*>* profile) { 94 int sum = 0; 95 int i = profile->length(); 96 while (i-- > 0) sum += profile->at(i)->count(); 97 return sum; 98 } 99 100 101 static const char* name_for(int i) { 102 return Bytecodes::is_defined(i) ? Bytecodes::name(Bytecodes::cast(i)) : "xxxunusedxxx"; 103 } 104 105 106 // Implementation of BytecodeHistogram 107 108 int BytecodeHistogram::_counters[Bytecodes::number_of_codes]; 109 110 111 void BytecodeHistogram::reset() { 112 int i = Bytecodes::number_of_codes; 113 while (i-- > 0) _counters[i] = 0; 114 } 115 116 117 void BytecodeHistogram::print(float cutoff) { 118 ResourceMark rm; 119 GrowableArray<HistoEntry*>* profile = sorted_array(_counters, Bytecodes::number_of_codes); 120 // print profile 121 int tot = total_count(profile); 122 int abs_sum = 0; 123 tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 124 tty->print_cr("Histogram of %d executed bytecodes:", tot); 125 tty->cr(); 126 tty->print_cr(" absolute relative code name"); 127 tty->print_cr("----------------------------------------------------------------------"); 128 int i = profile->length(); 129 while (i-- > 0) { 130 HistoEntry* e = profile->at(i); 131 int abs = e->count(); 132 float rel = (float)abs * 100.0F / (float)tot; 133 if (cutoff <= rel) { 134 tty->print_cr("%10d %7.2f%% %02x %s", abs, rel, e->index(), name_for(e->index())); 135 abs_sum += abs; 136 } 137 } 138 tty->print_cr("----------------------------------------------------------------------"); 139 float rel_sum = (float)abs_sum * 100.0F / (float)tot; 140 tty->print_cr("%10d %7.2f%% (cutoff = %.2f%%)", abs_sum, rel_sum, cutoff); 141 tty->cr(); 142 } 143 144 145 // Implementation of BytecodePairHistogram 146 147 int BytecodePairHistogram::_index; 148 int BytecodePairHistogram::_counters[BytecodePairHistogram::number_of_pairs]; 149 150 151 void BytecodePairHistogram::reset() { 152 _index = Bytecodes::_nop << log2_number_of_codes; 153 154 int i = number_of_pairs; 155 while (i-- > 0) _counters[i] = 0; 156 } 157 158 159 void BytecodePairHistogram::print(float cutoff) { 160 ResourceMark rm; 161 GrowableArray<HistoEntry*>* profile = sorted_array(_counters, number_of_pairs); 162 // print profile 163 int tot = total_count(profile); 164 int abs_sum = 0; 165 tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 166 tty->print_cr("Histogram of %d executed bytecode pairs:", tot); 167 tty->cr(); 168 tty->print_cr(" absolute relative codes 1st bytecode 2nd bytecode"); 169 tty->print_cr("----------------------------------------------------------------------"); 170 int i = profile->length(); 171 while (i-- > 0) { 172 HistoEntry* e = profile->at(i); 173 int abs = e->count(); 174 float rel = (float)abs * 100.0F / (float)tot; 175 if (cutoff <= rel) { 176 int c1 = e->index() % number_of_codes; 177 int c2 = e->index() / number_of_codes; 178 tty->print_cr("%10d %6.3f%% %02x %02x %-19s %s", abs, rel, c1, c2, name_for(c1), name_for(c2)); 179 abs_sum += abs; 180 } 181 } 182 tty->print_cr("----------------------------------------------------------------------"); 183 float rel_sum = (float)abs_sum * 100.0F / (float)tot; 184 tty->print_cr("%10d %6.3f%% (cutoff = %.3f%%)", abs_sum, rel_sum, cutoff); 185 tty->cr(); 186 } 187 188 #endif