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_NUMBERSEQ_HPP aoqi@0: #define SHARE_VM_UTILITIES_NUMBERSEQ_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: aoqi@0: /** aoqi@0: ** This file contains a few classes that represent number sequence, aoqi@0: ** x1, x2, x3, ..., xN, and can calculate their avg, max, and sd. aoqi@0: ** aoqi@0: ** Here's a quick description of the classes: aoqi@0: ** aoqi@0: ** AbsSeq: abstract superclass aoqi@0: ** NumberSeq: the sequence is assumed to be very long and the aoqi@0: ** maximum, avg, sd, davg, and dsd are calculated over all its elements aoqi@0: ** TruncatedSeq: this class keeps track of the last L elements aoqi@0: ** of the sequence and calculates avg, max, and sd only over them aoqi@0: **/ aoqi@0: aoqi@0: #define DEFAULT_ALPHA_VALUE 0.7 aoqi@0: aoqi@0: class AbsSeq: public CHeapObj { aoqi@0: private: aoqi@0: void init(double alpha); aoqi@0: aoqi@0: protected: aoqi@0: int _num; // the number of elements in the sequence aoqi@0: double _sum; // the sum of the elements in the sequence aoqi@0: double _sum_of_squares; // the sum of squares of the elements in the sequence aoqi@0: aoqi@0: double _davg; // decaying average aoqi@0: double _dvariance; // decaying variance aoqi@0: double _alpha; // factor for the decaying average / variance aoqi@0: aoqi@0: // This is what we divide with to get the average. In a standard aoqi@0: // number sequence, this should just be the number of elements in it. aoqi@0: virtual double total() const { return (double) _num; }; aoqi@0: aoqi@0: public: aoqi@0: AbsSeq(double alpha = DEFAULT_ALPHA_VALUE); aoqi@0: aoqi@0: virtual void add(double val); // adds a new element to the sequence aoqi@0: void add(unsigned val) { add((double) val); } aoqi@0: virtual double maximum() const = 0; // maximum element in the sequence aoqi@0: virtual double last() const = 0; // last element added in the sequence aoqi@0: aoqi@0: // the number of elements in the sequence aoqi@0: int num() const { return _num; } aoqi@0: // the sum of the elements in the sequence aoqi@0: double sum() const { return _sum; } aoqi@0: aoqi@0: double avg() const; // the average of the sequence aoqi@0: double variance() const; // the variance of the sequence aoqi@0: double sd() const; // the standard deviation of the sequence aoqi@0: aoqi@0: double davg() const; // decaying average aoqi@0: double dvariance() const; // decaying variance aoqi@0: double dsd() const; // decaying "standard deviation" aoqi@0: aoqi@0: // Debugging/Printing aoqi@0: virtual void dump(); aoqi@0: virtual void dump_on(outputStream* s); aoqi@0: }; aoqi@0: aoqi@0: class NumberSeq: public AbsSeq { aoqi@0: private: aoqi@0: bool check_nums(NumberSeq* total, int n, NumberSeq** parts); aoqi@0: aoqi@0: protected: aoqi@0: double _last; aoqi@0: double _maximum; // keep track of maximum value aoqi@0: aoqi@0: public: aoqi@0: NumberSeq(double alpha = DEFAULT_ALPHA_VALUE); aoqi@0: aoqi@0: virtual void add(double val); aoqi@0: virtual double maximum() const { return _maximum; } aoqi@0: virtual double last() const { return _last; } aoqi@0: aoqi@0: // Debugging/Printing aoqi@0: virtual void dump_on(outputStream* s); aoqi@0: }; aoqi@0: aoqi@0: class TruncatedSeq: public AbsSeq { aoqi@0: private: aoqi@0: enum PrivateConstants { aoqi@0: DefaultSeqLength = 10 aoqi@0: }; aoqi@0: void init(); aoqi@0: protected: aoqi@0: double *_sequence; // buffers the last L elements in the sequence aoqi@0: int _length; // this is L aoqi@0: int _next; // oldest slot in the array, i.e. next to be overwritten aoqi@0: aoqi@0: public: aoqi@0: // accepts a value for L aoqi@0: TruncatedSeq(int length = DefaultSeqLength, aoqi@0: double alpha = DEFAULT_ALPHA_VALUE); aoqi@0: ~TruncatedSeq(); aoqi@0: virtual void add(double val); aoqi@0: virtual double maximum() const; aoqi@0: virtual double last() const; // the last value added to the sequence aoqi@0: aoqi@0: double oldest() const; // the oldest valid value in the sequence aoqi@0: double predict_next() const; // prediction based on linear regression aoqi@0: aoqi@0: // Debugging/Printing aoqi@0: virtual void dump_on(outputStream* s); aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_UTILITIES_NUMBERSEQ_HPP