1 /*
   2  * Copyright (c) 1998, 2004, 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/_histogram.cpp.incl"
  27 
  28 #ifdef ASSERT
  29 
  30 ////////////////// HistogramElement ////////////////////////
  31 
  32 HistogramElement::HistogramElement() {
  33   _count = 0;
  34 }
  35 
  36 int HistogramElement::count() {
  37   return _count;
  38 }
  39 
  40 const char* HistogramElement::name() {
  41   return _name;
  42 }
  43 
  44 void HistogramElement::increment_count() {
  45   // We can't use the accessor :-(.
  46   Atomic::inc(&_count);
  47 }
  48 
  49 int HistogramElement::compare(HistogramElement* e1,HistogramElement* e2) {
  50   if(e1->count() > e2->count()) {
  51     return -1;
  52   } else if(e1->count() < e2->count()) {
  53     return 1;
  54   }
  55   return 0;
  56 }
  57 
  58 void HistogramElement::print_on(outputStream* st) const {
  59   st->print("%10d   ",((HistogramElement*)this)->count());
  60   st->print_cr("%s",((HistogramElement*)this)->name());
  61 }
  62 
  63 ////////////////// Histogram ////////////////////////
  64 
  65 int Histogram::sort_helper(HistogramElement** e1, HistogramElement** e2) {
  66   return (*e1)->compare(*e1,*e2);
  67 }
  68 
  69 Histogram::Histogram(const char* title,int estimatedCount) {
  70   _title = title;
  71   _elements = new (ResourceObj::C_HEAP) GrowableArray<HistogramElement*>(estimatedCount,true);
  72 }
  73 
  74 void Histogram::add_element(HistogramElement* element) {
  75   // Note, we need to add locking !
  76   elements()->append(element);
  77 }
  78 
  79 void Histogram::print_header(outputStream* st) {
  80   st->print_cr("%s",title());
  81   st->print_cr("--------------------------------------------------");
  82 }
  83 
  84 void Histogram::print_elements(outputStream* st) {
  85   elements()->sort(Histogram::sort_helper);
  86   jint total = 0;
  87   for(int i=0; i < elements()->length(); i++) {
  88     elements()->at(i)->print();
  89     total += elements()->at(i)->count();
  90   }
  91   st->print("%10d   ", total);
  92   st->print_cr("Total");
  93 }
  94 
  95 void Histogram::print_on(outputStream* st) const {
  96   ((Histogram*)this)->print_header(st);
  97   ((Histogram*)this)->print_elements(st);
  98 }
  99 
 100 #endif