hotspot/src/share/vm/utilities/histogram.cpp
author ysr
Thu, 05 Jun 2008 15:57:56 -0700
changeset 1374 4c24294029a9
parent 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
6711316: Open source the Garbage-First garbage collector Summary: First mercurial integration of the code for the Garbage-First garbage collector. Reviewed-by: apetrusenko, iveresov, jmasa, sgoldman, tonyp, ysr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2004 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_histogram.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
////////////////// HistogramElement ////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
HistogramElement::HistogramElement() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  _count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
int HistogramElement::count() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  return _count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
const char* HistogramElement::name() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  return _name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
void HistogramElement::increment_count() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  // We can't use the accessor :-(.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  Atomic::inc(&_count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
int HistogramElement::compare(HistogramElement* e1,HistogramElement* e2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  if(e1->count() > e2->count()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  } else if(e1->count() < e2->count()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    return 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
void HistogramElement::print_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  st->print("%10d   ",((HistogramElement*)this)->count());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  st->print_cr("%s",((HistogramElement*)this)->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
////////////////// Histogram ////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
int Histogram::sort_helper(HistogramElement** e1, HistogramElement** e2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  return (*e1)->compare(*e1,*e2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
Histogram::Histogram(const char* title,int estimatedCount) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  _title = title;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  _elements = new (ResourceObj::C_HEAP) GrowableArray<HistogramElement*>(estimatedCount,true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
void Histogram::add_element(HistogramElement* element) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  // Note, we need to add locking !
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  elements()->append(element);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
void Histogram::print_header(outputStream* st) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  st->print_cr("%s",title());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  st->print_cr("--------------------------------------------------");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
void Histogram::print_elements(outputStream* st) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  elements()->sort(Histogram::sort_helper);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  jint total = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  for(int i=0; i < elements()->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    elements()->at(i)->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    total += elements()->at(i)->count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  st->print("%10d   ", total);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  st->print_cr("Total");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
void Histogram::print_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  ((Histogram*)this)->print_header(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  ((Histogram*)this)->print_elements(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
#endif