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 int 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 "%d 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 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) {
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);
|