intHisto.hpp revision 3718:b9a9ed0f8eeb
1193323Sed/*
2193323Sed * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
3193323Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4193323Sed *
5193323Sed * This code is free software; you can redistribute it and/or modify it
6193323Sed * under the terms of the GNU General Public License version 2 only, as
7193323Sed * published by the Free Software Foundation.
8193323Sed *
9193323Sed * This code is distributed in the hope that it will be useful, but WITHOUT
10193323Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11193323Sed * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12193323Sed * version 2 for more details (a copy is included in the LICENSE file that
13193323Sed * accompanied this code).
14193323Sed *
15193323Sed * You should have received a copy of the GNU General Public License version
16193323Sed * 2 along with this work; if not, write to the Free Software Foundation,
17193323Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18234353Sdim *
19193323Sed * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20234353Sdim * or visit www.oracle.com if you need additional information or have any
21193323Sed * questions.
22198090Srdivacky *
23193323Sed */
24193323Sed
25193323Sed#ifndef SHARE_VM_UTILITIES_INTHISTO_HPP
26249423Sdim#define SHARE_VM_UTILITIES_INTHISTO_HPP
27249423Sdim
28193323Sed#include "memory/allocation.hpp"
29193323Sed#include "utilities/growableArray.hpp"
30193323Sed
31193323Sed// This class implements a simple histogram.
32193323Sed
33193323Sed// A histogram summarizes a series of "measurements", each of which is
34193323Sed// assumed (required in this implementation) to have an outcome that is a
35193323Sed// non-negative integer.  The histogram efficiently maps measurement outcomes
36193323Sed// to the number of measurements had that outcome.
37193323Sed
38193323Sed// To print the results, invoke print() on your Histogram*.
39193323Sed
40198090Srdivacky// Note: there is already an existing "Histogram" class, in file
41193323Sed// histogram.{hpp,cpp}, but to my mind that's not a histogram, it's a table
42193323Sed// mapping strings to counts.  To be a histogram (IMHO) it needs to map
43193323Sed// numbers (in fact, integers) to number of occurrences of that number.
44193323Sed
45193323Sed// ysr: (i am not sure i agree with the above note.) i suspect we want to have a
46193323Sed// histogram template that will map an arbitrary type (with a defined order
47193323Sed// relation) to a count.
48198090Srdivacky
49198090Srdivacky
50198090Srdivackyclass IntHistogram : public CHeapObj<mtInternal> {
51198090Srdivacky protected:
52226633Sdim  int _max;
53198090Srdivacky  int _tot;
54198090Srdivacky  GrowableArray<int>* _elements;
55198090Srdivacky
56198090Srdivackypublic:
57198090Srdivacky  // Create a new, empty table.  "est" is an estimate of the maximum outcome
58226633Sdim  // that will be added, and "max" is an outcome such that all outcomes at
59198090Srdivacky  // least that large will be bundled with it.
60198090Srdivacky  IntHistogram(int est, int max);
61198090Srdivacky  // Add a measurement with the given outcome to the sequence.
62226633Sdim  void add_entry(int outcome);
63198090Srdivacky  // Return the number of entries recorded so far with the given outcome.
64226633Sdim  int  entries_for_outcome(int outcome);
65226633Sdim  // Return the total number of entries recorded so far.
66198090Srdivacky  int  total_entries() { return _tot; }
67198090Srdivacky  // Return the number of entries recorded so far with the given outcome as
68198090Srdivacky  // a fraction of the total number recorded so far.
69198090Srdivacky  double fraction_for_outcome(int outcome) {
70198090Srdivacky    return
71198090Srdivacky      (double)entries_for_outcome(outcome)/
72198090Srdivacky      (double)total_entries();
73198090Srdivacky  }
74198090Srdivacky  // Print the histogram on the given output stream.
75198090Srdivacky  void print_on(outputStream* st) const;
76193323Sed};
77193323Sed
78193323Sed#endif // SHARE_VM_UTILITIES_INTHISTO_HPP
79198090Srdivacky