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