aoqi@0: /* aoqi@0: * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_UTILITIES_INTHISTO_HPP aoqi@0: #define SHARE_VM_UTILITIES_INTHISTO_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "utilities/growableArray.hpp" aoqi@0: aoqi@0: // This class implements a simple histogram. aoqi@0: aoqi@0: // A histogram summarizes a series of "measurements", each of which is aoqi@0: // assumed (required in this implementation) to have an outcome that is a aoqi@0: // non-negative integer. The histogram efficiently maps measurement outcomes aoqi@0: // to the number of measurements had that outcome. aoqi@0: aoqi@0: // To print the results, invoke print() on your Histogram*. aoqi@0: aoqi@0: // Note: there is already an existing "Histogram" class, in file aoqi@0: // histogram.{hpp,cpp}, but to my mind that's not a histogram, it's a table aoqi@0: // mapping strings to counts. To be a histogram (IMHO) it needs to map aoqi@0: // numbers (in fact, integers) to number of occurrences of that number. aoqi@0: aoqi@0: // ysr: (i am not sure i agree with the above note.) i suspect we want to have a aoqi@0: // histogram template that will map an arbitrary type (with a defined order aoqi@0: // relation) to a count. aoqi@0: aoqi@0: aoqi@0: class IntHistogram : public CHeapObj { aoqi@0: protected: aoqi@0: int _max; aoqi@0: int _tot; aoqi@0: GrowableArray* _elements; aoqi@0: aoqi@0: public: aoqi@0: // Create a new, empty table. "est" is an estimate of the maximum outcome aoqi@0: // that will be added, and "max" is an outcome such that all outcomes at aoqi@0: // least that large will be bundled with it. aoqi@0: IntHistogram(int est, int max); aoqi@0: // Add a measurement with the given outcome to the sequence. aoqi@0: void add_entry(int outcome); aoqi@0: // Return the number of entries recorded so far with the given outcome. aoqi@0: int entries_for_outcome(int outcome); aoqi@0: // Return the total number of entries recorded so far. aoqi@0: int total_entries() { return _tot; } aoqi@0: // Return the number of entries recorded so far with the given outcome as aoqi@0: // a fraction of the total number recorded so far. aoqi@0: double fraction_for_outcome(int outcome) { aoqi@0: return aoqi@0: (double)entries_for_outcome(outcome)/ aoqi@0: (double)total_entries(); aoqi@0: } aoqi@0: // Print the histogram on the given output stream. aoqi@0: void print_on(outputStream* st) const; aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_UTILITIES_INTHISTO_HPP