ysr@777: /* brutisso@3641: * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * 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. ysr@777: * ysr@777: */ ysr@777: stefank@2314: #ifndef SHARE_VM_UTILITIES_NUMBERSEQ_HPP stefank@2314: #define SHARE_VM_UTILITIES_NUMBERSEQ_HPP stefank@2314: zgu@3900: #include "memory/allocation.hpp" zgu@3900: ysr@777: /** ysr@777: ** This file contains a few classes that represent number sequence, ysr@777: ** x1, x2, x3, ..., xN, and can calculate their avg, max, and sd. ysr@777: ** ysr@777: ** Here's a quick description of the classes: ysr@777: ** ysr@777: ** AbsSeq: abstract superclass ysr@777: ** NumberSeq: the sequence is assumed to be very long and the ysr@777: ** maximum, avg, sd, davg, and dsd are calculated over all its elements ysr@777: ** TruncatedSeq: this class keeps track of the last L elements ysr@777: ** of the sequence and calculates avg, max, and sd only over them ysr@777: **/ ysr@777: ysr@777: #define DEFAULT_ALPHA_VALUE 0.7 ysr@777: zgu@3900: class AbsSeq: public CHeapObj { ysr@777: private: ysr@777: void init(double alpha); ysr@777: ysr@777: protected: ysr@777: int _num; // the number of elements in the sequence ysr@777: double _sum; // the sum of the elements in the sequence ysr@777: double _sum_of_squares; // the sum of squares of the elements in the sequence ysr@777: ysr@777: double _davg; // decaying average ysr@777: double _dvariance; // decaying variance ysr@777: double _alpha; // factor for the decaying average / variance ysr@777: ysr@777: // This is what we divide with to get the average. In a standard ysr@777: // number sequence, this should just be the number of elements in it. ysr@777: virtual double total() const { return (double) _num; }; ysr@777: ysr@777: public: ysr@777: AbsSeq(double alpha = DEFAULT_ALPHA_VALUE); ysr@777: ysr@777: virtual void add(double val); // adds a new element to the sequence ysr@777: void add(unsigned val) { add((double) val); } ysr@777: virtual double maximum() const = 0; // maximum element in the sequence ysr@777: virtual double last() const = 0; // last element added in the sequence ysr@777: ysr@777: // the number of elements in the sequence ysr@777: int num() const { return _num; } ysr@777: // the sum of the elements in the sequence ysr@777: double sum() const { return _sum; } ysr@777: ysr@777: double avg() const; // the average of the sequence ysr@777: double variance() const; // the variance of the sequence ysr@777: double sd() const; // the standard deviation of the sequence ysr@777: ysr@777: double davg() const; // decaying average ysr@777: double dvariance() const; // decaying variance ysr@777: double dsd() const; // decaying "standard deviation" ysr@1521: ysr@1521: // Debugging/Printing ysr@1521: virtual void dump(); ysr@1521: virtual void dump_on(outputStream* s); ysr@777: }; ysr@777: ysr@777: class NumberSeq: public AbsSeq { ysr@777: private: ysr@777: bool check_nums(NumberSeq* total, int n, NumberSeq** parts); ysr@777: ysr@777: protected: ysr@777: double _last; ysr@777: double _maximum; // keep track of maximum value ysr@777: ysr@777: public: ysr@777: NumberSeq(double alpha = DEFAULT_ALPHA_VALUE); ysr@777: ysr@777: virtual void add(double val); ysr@777: virtual double maximum() const { return _maximum; } ysr@777: virtual double last() const { return _last; } ysr@1521: ysr@1521: // Debugging/Printing ysr@1521: virtual void dump_on(outputStream* s); ysr@777: }; ysr@777: ysr@777: class TruncatedSeq: public AbsSeq { ysr@777: private: ysr@777: enum PrivateConstants { ysr@777: DefaultSeqLength = 10 ysr@777: }; ysr@777: void init(); ysr@777: protected: ysr@777: double *_sequence; // buffers the last L elements in the sequence ysr@777: int _length; // this is L ysr@777: int _next; // oldest slot in the array, i.e. next to be overwritten ysr@777: ysr@777: public: ysr@777: // accepts a value for L ysr@777: TruncatedSeq(int length = DefaultSeqLength, ysr@777: double alpha = DEFAULT_ALPHA_VALUE); brutisso@3641: ~TruncatedSeq(); ysr@777: virtual void add(double val); ysr@777: virtual double maximum() const; ysr@777: virtual double last() const; // the last value added to the sequence ysr@777: ysr@777: double oldest() const; // the oldest valid value in the sequence ysr@777: double predict_next() const; // prediction based on linear regression ysr@1521: ysr@1521: // Debugging/Printing ysr@1521: virtual void dump_on(outputStream* s); ysr@777: }; stefank@2314: stefank@2314: #endif // SHARE_VM_UTILITIES_NUMBERSEQ_HPP