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 // Non-product code
33 #ifndef PRODUCT
34
35 // Implementation of BytecodeCounter
36
37 int BytecodeCounter::_counter_value = 0;
38 jlong BytecodeCounter::_reset_time = 0;
39
40
41 void BytecodeCounter::reset() {
42 _counter_value = 0;
43 _reset_time = os::elapsed_counter();
44 }
45
46
47 double BytecodeCounter::elapsed_time() {
48 return (double)(os::elapsed_counter() - _reset_time) / (double)os::elapsed_frequency();
49 }
50
51
52 double BytecodeCounter::frequency() {
53 return (double)counter_value() / elapsed_time();
54 }
55
56
57 void BytecodeCounter::print() {
58 tty->print_cr(
59 "%d bytecodes executed in %.1fs (%.3fMHz)",
60 counter_value(),
61 elapsed_time(),
62 frequency() / 1000000.0
63 );
64 }
65
66
67 // Helper class for sorting
68
69 class HistoEntry: public ResourceObj {
70 private:
71 int _index;
72 int _count;
73
74 public:
75 HistoEntry(int index, int count) { _index = index; _count = count; }
76 int index() const { return _index; }
77 int count() const { return _count; }
78
79 static int compare(HistoEntry** x, HistoEntry** y) { return (*x)->count() - (*y)->count(); }
80 };
81
82
83 // Helper functions
84
85 static GrowableArray<HistoEntry*>* sorted_array(int* array, int length) {
86 GrowableArray<HistoEntry*>* a = new GrowableArray<HistoEntry*>(length);
87 int i = length;
88 while (i-- > 0) a->append(new HistoEntry(i, array[i]));
89 a->sort(HistoEntry::compare);
90 return a;
91 }
92
93
94 static int total_count(GrowableArray<HistoEntry*>* profile) {
95 int sum = 0;
96 int i = profile->length();
97 while (i-- > 0) sum += profile->at(i)->count();
98 return sum;
99 }
100
101
102 static const char* name_for(int i) {
125 tty->print_cr("Histogram of %d executed bytecodes:", tot);
126 tty->cr();
127 tty->print_cr(" absolute relative code name");
128 tty->print_cr("----------------------------------------------------------------------");
129 int i = profile->length();
130 while (i-- > 0) {
131 HistoEntry* e = profile->at(i);
132 int abs = e->count();
133 float rel = (float)abs * 100.0F / (float)tot;
134 if (cutoff <= rel) {
135 tty->print_cr("%10d %7.2f%% %02x %s", abs, rel, e->index(), name_for(e->index()));
136 abs_sum += abs;
137 }
138 }
139 tty->print_cr("----------------------------------------------------------------------");
140 float rel_sum = (float)abs_sum * 100.0F / (float)tot;
141 tty->print_cr("%10d %7.2f%% (cutoff = %.2f%%)", abs_sum, rel_sum, cutoff);
142 tty->cr();
143 }
144
145
146 // Implementation of BytecodePairHistogram
147
148 int BytecodePairHistogram::_index;
149 int BytecodePairHistogram::_counters[BytecodePairHistogram::number_of_pairs];
150
151
152 void BytecodePairHistogram::reset() {
153 _index = Bytecodes::_nop << log2_number_of_codes;
154
155 int i = number_of_pairs;
156 while (i-- > 0) _counters[i] = 0;
157 }
158
159
160 void BytecodePairHistogram::print(float cutoff) {
161 ResourceMark rm;
162 GrowableArray<HistoEntry*>* profile = sorted_array(_counters, number_of_pairs);
163 // print profile
164 int tot = total_count(profile);
|
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) {
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);
|