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