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) {
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);
|
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) {
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);
|