hotspot/src/share/vm/interpreter/bytecodeHistogram.cpp
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2000 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_bytecodeHistogram.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// ------------------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
// Non-product code
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// Implementation of BytecodeCounter
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
int   BytecodeCounter::_counter_value = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
jlong BytecodeCounter::_reset_time    = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
void BytecodeCounter::reset() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  _counter_value = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  _reset_time    = os::elapsed_counter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
double BytecodeCounter::elapsed_time() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  return (double)(os::elapsed_counter() - _reset_time) / (double)os::elapsed_frequency();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
double BytecodeCounter::frequency() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  return (double)counter_value() / elapsed_time();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
void BytecodeCounter::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  tty->print_cr(
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    "%d bytecodes executed in %.1fs (%.3fMHz)",
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    counter_value(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    elapsed_time(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    frequency() / 1000000.0
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
// Helper class for sorting
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
class HistoEntry: public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  int             _index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  int             _count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  HistoEntry(int index, int count)                         { _index = index; _count = count; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  int             index() const                            { return _index; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  int             count() const                            { return _count; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  static int      compare(HistoEntry** x, HistoEntry** y)  { return (*x)->count() - (*y)->count(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
// Helper functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
static GrowableArray<HistoEntry*>* sorted_array(int* array, int length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  GrowableArray<HistoEntry*>* a = new GrowableArray<HistoEntry*>(length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  int i = length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  while (i-- > 0) a->append(new HistoEntry(i, array[i]));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  a->sort(HistoEntry::compare);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  return a;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
static int total_count(GrowableArray<HistoEntry*>* profile) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  int sum = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  int i = profile->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  while (i-- > 0) sum += profile->at(i)->count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  return sum;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
static const char* name_for(int i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  return Bytecodes::is_defined(i) ? Bytecodes::name(Bytecodes::cast(i)) : "xxxunusedxxx";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
// Implementation of BytecodeHistogram
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
int BytecodeHistogram::_counters[Bytecodes::number_of_codes];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
void BytecodeHistogram::reset() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  int i = Bytecodes::number_of_codes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  while (i-- > 0) _counters[i] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
void BytecodeHistogram::print(float cutoff) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  GrowableArray<HistoEntry*>* profile = sorted_array(_counters, Bytecodes::number_of_codes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  // print profile
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  int tot     = total_count(profile);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  int abs_sum = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  tty->cr();   //0123456789012345678901234567890123456789012345678901234567890123456789
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  tty->print_cr("Histogram of %d executed bytecodes:", tot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  tty->print_cr("  absolute  relative  code    name");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  tty->print_cr("----------------------------------------------------------------------");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  int i = profile->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  while (i-- > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    HistoEntry* e = profile->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    int       abs = e->count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    float     rel = abs * 100.0F / tot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    if (cutoff <= rel) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
      tty->print_cr("%10d  %7.2f%%    %02x    %s", abs, rel, e->index(), name_for(e->index()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
      abs_sum += abs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  tty->print_cr("----------------------------------------------------------------------");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  float rel_sum = abs_sum * 100.0F / tot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  tty->print_cr("%10d  %7.2f%%    (cutoff = %.2f%%)", abs_sum, rel_sum, cutoff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
// Implementation of BytecodePairHistogram
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
int BytecodePairHistogram::_index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
int BytecodePairHistogram::_counters[BytecodePairHistogram::number_of_pairs];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
void BytecodePairHistogram::reset() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  _index = Bytecodes::_nop << log2_number_of_codes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  int i = number_of_pairs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  while (i-- > 0) _counters[i] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
void BytecodePairHistogram::print(float cutoff) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  GrowableArray<HistoEntry*>* profile = sorted_array(_counters, number_of_pairs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  // print profile
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  int tot     = total_count(profile);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  int abs_sum = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  tty->cr();   //0123456789012345678901234567890123456789012345678901234567890123456789
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  tty->print_cr("Histogram of %d executed bytecode pairs:", tot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  tty->print_cr("  absolute  relative    codes    1st bytecode        2nd bytecode");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  tty->print_cr("----------------------------------------------------------------------");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  int i = profile->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  while (i-- > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    HistoEntry* e = profile->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    int       abs = e->count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    float     rel = abs * 100.0F / tot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
    if (cutoff <= rel) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
      int   c1 = e->index() % number_of_codes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
      int   c2 = e->index() / number_of_codes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
      tty->print_cr("%10d   %6.3f%%    %02x %02x    %-19s %s", abs, rel, c1, c2, name_for(c1), name_for(c2));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
      abs_sum += abs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  tty->print_cr("----------------------------------------------------------------------");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  float rel_sum = abs_sum * 100.0F / tot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  tty->print_cr("%10d   %6.3f%%    (cutoff = %.3f%%)", abs_sum, rel_sum, cutoff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
#endif