duke@435: /* stefank@2314: * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "utilities/histogram.hpp" duke@435: duke@435: #ifdef ASSERT duke@435: duke@435: ////////////////// HistogramElement //////////////////////// duke@435: duke@435: HistogramElement::HistogramElement() { duke@435: _count = 0; duke@435: } duke@435: duke@435: int HistogramElement::count() { duke@435: return _count; duke@435: } duke@435: duke@435: const char* HistogramElement::name() { duke@435: return _name; duke@435: } duke@435: duke@435: void HistogramElement::increment_count() { duke@435: // We can't use the accessor :-(. duke@435: Atomic::inc(&_count); duke@435: } duke@435: duke@435: int HistogramElement::compare(HistogramElement* e1,HistogramElement* e2) { duke@435: if(e1->count() > e2->count()) { duke@435: return -1; duke@435: } else if(e1->count() < e2->count()) { duke@435: return 1; duke@435: } duke@435: return 0; duke@435: } duke@435: duke@435: void HistogramElement::print_on(outputStream* st) const { duke@435: st->print("%10d ",((HistogramElement*)this)->count()); duke@435: st->print_cr("%s",((HistogramElement*)this)->name()); duke@435: } duke@435: duke@435: ////////////////// Histogram //////////////////////// duke@435: duke@435: int Histogram::sort_helper(HistogramElement** e1, HistogramElement** e2) { duke@435: return (*e1)->compare(*e1,*e2); duke@435: } duke@435: duke@435: Histogram::Histogram(const char* title,int estimatedCount) { duke@435: _title = title; duke@435: _elements = new (ResourceObj::C_HEAP) GrowableArray(estimatedCount,true); duke@435: } duke@435: duke@435: void Histogram::add_element(HistogramElement* element) { duke@435: // Note, we need to add locking ! duke@435: elements()->append(element); duke@435: } duke@435: duke@435: void Histogram::print_header(outputStream* st) { duke@435: st->print_cr("%s",title()); duke@435: st->print_cr("--------------------------------------------------"); duke@435: } duke@435: duke@435: void Histogram::print_elements(outputStream* st) { duke@435: elements()->sort(Histogram::sort_helper); duke@435: jint total = 0; duke@435: for(int i=0; i < elements()->length(); i++) { duke@435: elements()->at(i)->print(); duke@435: total += elements()->at(i)->count(); duke@435: } duke@435: st->print("%10d ", total); duke@435: st->print_cr("Total"); duke@435: } duke@435: duke@435: void Histogram::print_on(outputStream* st) const { duke@435: ((Histogram*)this)->print_header(st); duke@435: ((Histogram*)this)->print_elements(st); duke@435: } duke@435: duke@435: #endif