src/share/vm/utilities/histogram.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/histogram.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,101 @@
     1.4 +/*
     1.5 + * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "oops/oop.inline.hpp"
    1.30 +#include "utilities/histogram.hpp"
    1.31 +
    1.32 +#ifdef ASSERT
    1.33 +
    1.34 +////////////////// HistogramElement ////////////////////////
    1.35 +
    1.36 +HistogramElement::HistogramElement() {
    1.37 +  _count = 0;
    1.38 +}
    1.39 +
    1.40 +int HistogramElement::count() {
    1.41 +  return _count;
    1.42 +}
    1.43 +
    1.44 +const char* HistogramElement::name() {
    1.45 +  return _name;
    1.46 +}
    1.47 +
    1.48 +void HistogramElement::increment_count() {
    1.49 +  // We can't use the accessor :-(.
    1.50 +  Atomic::inc(&_count);
    1.51 +}
    1.52 +
    1.53 +int HistogramElement::compare(HistogramElement* e1,HistogramElement* e2) {
    1.54 +  if(e1->count() > e2->count()) {
    1.55 +    return -1;
    1.56 +  } else if(e1->count() < e2->count()) {
    1.57 +    return 1;
    1.58 +  }
    1.59 +  return 0;
    1.60 +}
    1.61 +
    1.62 +void HistogramElement::print_on(outputStream* st) const {
    1.63 +  st->print("%10d   ",((HistogramElement*)this)->count());
    1.64 +  st->print_cr("%s",((HistogramElement*)this)->name());
    1.65 +}
    1.66 +
    1.67 +////////////////// Histogram ////////////////////////
    1.68 +
    1.69 +int Histogram::sort_helper(HistogramElement** e1, HistogramElement** e2) {
    1.70 +  return (*e1)->compare(*e1,*e2);
    1.71 +}
    1.72 +
    1.73 +Histogram::Histogram(const char* title,int estimatedCount) {
    1.74 +  _title = title;
    1.75 +  _elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<HistogramElement*>(estimatedCount,true);
    1.76 +}
    1.77 +
    1.78 +void Histogram::add_element(HistogramElement* element) {
    1.79 +  // Note, we need to add locking !
    1.80 +  elements()->append(element);
    1.81 +}
    1.82 +
    1.83 +void Histogram::print_header(outputStream* st) {
    1.84 +  st->print_cr("%s",title());
    1.85 +  st->print_cr("--------------------------------------------------");
    1.86 +}
    1.87 +
    1.88 +void Histogram::print_elements(outputStream* st) {
    1.89 +  elements()->sort(Histogram::sort_helper);
    1.90 +  jint total = 0;
    1.91 +  for(int i=0; i < elements()->length(); i++) {
    1.92 +    elements()->at(i)->print();
    1.93 +    total += elements()->at(i)->count();
    1.94 +  }
    1.95 +  st->print("%10d   ", total);
    1.96 +  st->print_cr("Total");
    1.97 +}
    1.98 +
    1.99 +void Histogram::print_on(outputStream* st) const {
   1.100 +  ((Histogram*)this)->print_header(st);
   1.101 +  ((Histogram*)this)->print_elements(st);
   1.102 +}
   1.103 +
   1.104 +#endif

mercurial