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 32 // Implementation of BytecodeCounter 33 34 uintx BytecodeCounter::_counter_value = 0; 35 jlong BytecodeCounter::_reset_time = 0; 36 37 void BytecodeCounter::reset() { 38 _counter_value = 0; 39 _reset_time = os::elapsed_counter(); 40 } 41 42 double BytecodeCounter::elapsed_time() { 43 return (double)(os::elapsed_counter() - _reset_time) / (double)os::elapsed_frequency(); 44 } 45 46 double BytecodeCounter::frequency() { 47 return (double)counter_value() / elapsed_time(); 48 } 49 50 void BytecodeCounter::print() { 51 tty->print_cr( 52 "%zu bytecodes executed in %.1fs (%.3fMHz)", 53 counter_value(), 54 elapsed_time(), 55 frequency() / 1000000.0 56 ); 57 } 58 59 60 // Helper class for sorting 61 62 class HistoEntry: public ResourceObj { 63 private: 64 int _index; 65 int _count; 66 67 public: 68 HistoEntry(int index, int count) { _index = index; _count = count; } 69 int index() const { return _index; } 70 int count() const { return _count; } 71 72 static int compare(HistoEntry** x, HistoEntry** y) { return (*x)->count() - (*y)->count(); } 73 }; 74 75 // Helper functions 76 77 static GrowableArray<HistoEntry*>* sorted_array(int* array, int length) { 78 GrowableArray<HistoEntry*>* a = new GrowableArray<HistoEntry*>(length); 79 int i = length; 80 while (i-- > 0) a->append(new HistoEntry(i, array[i])); 81 a->sort(HistoEntry::compare); 82 return a; 83 } 84 85 86 static int total_count(GrowableArray<HistoEntry*>* profile) { 87 int sum = 0; 88 int i = profile->length(); 89 while (i-- > 0) sum += profile->at(i)->count(); 90 return sum; 91 } 92 93 94 static const char* name_for(int i) { 95 return Bytecodes::is_defined(i) ? Bytecodes::name(Bytecodes::cast(i)) : "xxxunusedxxx"; 96 } 97 98 99 // Implementation of BytecodeHistogram 100 101 int BytecodeHistogram::_counters[Bytecodes::number_of_codes]; 102 103 104 void BytecodeHistogram::reset() { 105 int i = Bytecodes::number_of_codes; 106 while (i-- > 0) _counters[i] = 0; 107 } 108 109 110 void BytecodeHistogram::print(float cutoff) { 111 ResourceMark rm; 112 GrowableArray<HistoEntry*>* profile = sorted_array(_counters, Bytecodes::number_of_codes); 113 // print profile 114 int tot = total_count(profile); 115 int abs_sum = 0; 116 tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 117 tty->print_cr("Histogram of %d executed bytecodes:", tot); 118 tty->cr(); 119 tty->print_cr(" absolute relative code name"); 120 tty->print_cr("----------------------------------------------------------------------"); 121 int i = profile->length(); 122 while (i-- > 0) { 123 HistoEntry* e = profile->at(i); 124 int abs = e->count(); 125 float rel = (float)abs * 100.0F / (float)tot; 126 if (cutoff <= rel) { 127 tty->print_cr("%10d %7.2f%% %02x %s", abs, rel, e->index(), name_for(e->index())); 128 abs_sum += abs; 129 } 130 } 131 tty->print_cr("----------------------------------------------------------------------"); 132 float rel_sum = (float)abs_sum * 100.0F / (float)tot; 133 tty->print_cr("%10d %7.2f%% (cutoff = %.2f%%)", abs_sum, rel_sum, cutoff); 134 tty->cr(); 135 } 136 137 // Non-product code 138 #ifndef PRODUCT 139 140 // Implementation of BytecodePairHistogram 141 142 int BytecodePairHistogram::_index; 143 int BytecodePairHistogram::_counters[BytecodePairHistogram::number_of_pairs]; 144 145 146 void BytecodePairHistogram::reset() { 147 _index = Bytecodes::_nop << log2_number_of_codes; 148 149 int i = number_of_pairs; 150 while (i-- > 0) _counters[i] = 0; 151 } 152 153 154 void BytecodePairHistogram::print(float cutoff) { 155 ResourceMark rm; 156 GrowableArray<HistoEntry*>* profile = sorted_array(_counters, number_of_pairs); 157 // print profile 158 int tot = total_count(profile); 159 int abs_sum = 0; 160 tty->cr(); //0123456789012345678901234567890123456789012345678901234567890123456789 161 tty->print_cr("Histogram of %d executed bytecode pairs:", tot); 162 tty->cr(); 163 tty->print_cr(" absolute relative codes 1st bytecode 2nd bytecode"); 164 tty->print_cr("----------------------------------------------------------------------"); 165 int i = profile->length(); 166 while (i-- > 0) { 167 HistoEntry* e = profile->at(i); 168 int abs = e->count(); 169 float rel = (float)abs * 100.0F / (float)tot; 170 if (cutoff <= rel) { 171 int c1 = e->index() % number_of_codes; 172 int c2 = e->index() / number_of_codes; 173 tty->print_cr("%10d %6.3f%% %02x %02x %-19s %s", abs, rel, c1, c2, name_for(c1), name_for(c2)); 174 abs_sum += abs; 175 } 176 } 177 tty->print_cr("----------------------------------------------------------------------"); 178 float rel_sum = (float)abs_sum * 100.0F / (float)tot; 179 tty->print_cr("%10d %6.3f%% (cutoff = %.3f%%)", abs_sum, rel_sum, cutoff); 180 tty->cr(); 181 } 182 183 #endif