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