histogram.cpp revision 3465:d2a62e0f25eb
198944Sobrien/*
298944Sobrien * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
3130803Smarcel * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
498944Sobrien *
598944Sobrien * This code is free software; you can redistribute it and/or modify it
698944Sobrien * under the terms of the GNU General Public License version 2 only, as
798944Sobrien * published by the Free Software Foundation.
898944Sobrien *
998944Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1098944Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1198944Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1298944Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1398944Sobrien * accompanied this code).
1498944Sobrien *
1598944Sobrien * You should have received a copy of the GNU General Public License version
1698944Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1798944Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1898944Sobrien *
1998944Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2098944Sobrien * or visit www.oracle.com if you need additional information or have any
2198944Sobrien * questions.
2298944Sobrien *
23130803Smarcel */
24130803Smarcel
2598944Sobrien#include "precompiled.hpp"
2698944Sobrien#include "oops/oop.inline.hpp"
2798944Sobrien#include "utilities/histogram.hpp"
2898944Sobrien
29130803Smarcel#ifdef ASSERT
30130803Smarcel
31130803Smarcel////////////////// HistogramElement ////////////////////////
32130803Smarcel
33130803SmarcelHistogramElement::HistogramElement() {
34130803Smarcel  _count = 0;
35130803Smarcel}
3698944Sobrien
37130803Smarcelint HistogramElement::count() {
38130803Smarcel  return _count;
39130803Smarcel}
4098944Sobrien
4198944Sobrienconst char* HistogramElement::name() {
4298944Sobrien  return _name;
4398944Sobrien}
4498944Sobrien
4598944Sobrienvoid HistogramElement::increment_count() {
4698944Sobrien  // We can't use the accessor :-(.
4798944Sobrien  Atomic::inc(&_count);
4898944Sobrien}
49130803Smarcel
50130803Smarcelint HistogramElement::compare(HistogramElement* e1,HistogramElement* e2) {
51130803Smarcel  if(e1->count() > e2->count()) {
52130803Smarcel    return -1;
5398944Sobrien  } else if(e1->count() < e2->count()) {
5498944Sobrien    return 1;
5598944Sobrien  }
5698944Sobrien  return 0;
57130803Smarcel}
5898944Sobrien
5998944Sobrienvoid HistogramElement::print_on(outputStream* st) const {
6098944Sobrien  st->print("%10d   ",((HistogramElement*)this)->count());
6198944Sobrien  st->print_cr("%s",((HistogramElement*)this)->name());
6298944Sobrien}
6398944Sobrien
6498944Sobrien////////////////// Histogram ////////////////////////
6598944Sobrien
6698944Sobrienint Histogram::sort_helper(HistogramElement** e1, HistogramElement** e2) {
6798944Sobrien  return (*e1)->compare(*e1,*e2);
6898944Sobrien}
6998944Sobrien
7098944SobrienHistogram::Histogram(const char* title,int estimatedCount) {
7198944Sobrien  _title = title;
7298944Sobrien  _elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<HistogramElement*>(estimatedCount,true);
7398944Sobrien}
7498944Sobrien
7598944Sobrienvoid Histogram::add_element(HistogramElement* element) {
7698944Sobrien  // Note, we need to add locking !
7798944Sobrien  elements()->append(element);
7898944Sobrien}
7998944Sobrien
8098944Sobrienvoid Histogram::print_header(outputStream* st) {
8198944Sobrien  st->print_cr("%s",title());
8298944Sobrien  st->print_cr("--------------------------------------------------");
83130803Smarcel}
84130803Smarcel
85130803Smarcelvoid Histogram::print_elements(outputStream* st) {
86130803Smarcel  elements()->sort(Histogram::sort_helper);
8798944Sobrien  jint total = 0;
88130803Smarcel  for(int i=0; i < elements()->length(); i++) {
89130803Smarcel    elements()->at(i)->print();
90130803Smarcel    total += elements()->at(i)->count();
91130803Smarcel  }
9298944Sobrien  st->print("%10d   ", total);
93130803Smarcel  st->print_cr("Total");
94130803Smarcel}
95130803Smarcel
9698944Sobrienvoid Histogram::print_on(outputStream* st) const {
9798944Sobrien  ((Histogram*)this)->print_header(st);
9898944Sobrien  ((Histogram*)this)->print_elements(st);
9998944Sobrien}
10098944Sobrien
10198944Sobrien#endif
10298944Sobrien